2013-09-12 17:19:51 +08:00
|
|
|
class CoursesController < ApplicationController
|
2014-03-17 22:43:38 +08:00
|
|
|
include CoursesHelper
|
2014-05-29 16:35:34 +08:00
|
|
|
|
|
|
|
menu_item l(:label_sort_by_time), :only => :index
|
|
|
|
menu_item l(:label_sort_by_active), :only => :index
|
|
|
|
menu_item l(:label_sort_by_influence), :only => :index
|
|
|
|
|
2014-06-04 15:47:09 +08:00
|
|
|
before_filter :authorize, :only => [:show, :settings, :edit, :update, :modules, :close, :reopen, :view_homework_attaches, :course]
|
2014-06-04 16:45:20 +08:00
|
|
|
#before_filter :authorize_global, :only => [:view_homework_attaches, :new,:create]
|
2014-06-04 15:47:09 +08:00
|
|
|
before_filter :require_admin, :only => [:copy, :archive, :unarchive, :destroy, :calendar]
|
2014-05-29 16:35:34 +08:00
|
|
|
|
2013-09-12 10:41:15 +08:00
|
|
|
before_filter :require_login, :only => [:join, :unjoin]
|
2014-03-17 22:43:38 +08:00
|
|
|
before_filter :allow_join, :only => [:join]
|
2014-05-29 16:35:34 +08:00
|
|
|
|
|
|
|
|
2013-09-12 10:41:15 +08:00
|
|
|
def join
|
|
|
|
if User.current.logged?
|
|
|
|
course = Project.find(params[:object_id])
|
2014-03-19 15:50:02 +08:00
|
|
|
course_prefs = Course.find_by_extra(course.identifier)
|
|
|
|
if params[:course_password] == course_prefs.password
|
2014-03-17 22:43:38 +08:00
|
|
|
members = []
|
|
|
|
members << Member.new(:role_ids => [10], :user_id => User.current.id)
|
|
|
|
course.members << members
|
2014-06-04 15:47:09 +08:00
|
|
|
|
2014-03-17 22:43:38 +08:00
|
|
|
StudentsForCourse.create(:student_id => User.current.id, :course_id => params[:object_id])
|
|
|
|
@state = 0
|
2013-09-12 10:41:15 +08:00
|
|
|
else
|
2014-03-19 15:50:02 +08:00
|
|
|
@state = 1
|
2013-09-12 10:41:15 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
respond_to do |format|
|
|
|
|
# format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
|
2013-11-05 20:46:38 +08:00
|
|
|
format.js { render :partial => 'set_join', :locals => {:user => User.current, :course => Project.find(params[:object_id]), :object_id => params[:object_id]} }
|
2013-09-12 10:41:15 +08:00
|
|
|
end
|
|
|
|
end
|
2014-06-04 15:47:09 +08:00
|
|
|
|
2013-09-12 10:41:15 +08:00
|
|
|
def unjoin
|
|
|
|
if User.current.logged?
|
2014-06-04 15:47:09 +08:00
|
|
|
|
2013-09-12 10:41:15 +08:00
|
|
|
@member = Member.where('project_id = ? and user_id = ?', params[:object_id], User.current.id)
|
|
|
|
@member.first.destroy
|
2014-06-04 15:47:09 +08:00
|
|
|
|
2013-09-12 10:41:15 +08:00
|
|
|
joined = StudentsForCourse.where('student_id = ? and course_id = ?', User.current.id, params[:object_id])
|
|
|
|
joined.each do |join|
|
|
|
|
join.delete
|
|
|
|
end
|
|
|
|
end
|
|
|
|
respond_to do |format|
|
|
|
|
# format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
|
2013-11-05 20:46:38 +08:00
|
|
|
format.js { render :partial => 'set_join', :locals => {:user => User.current, :course => Project.find(params[:object_id]), :object_id => params[:object_id]} }
|
2013-09-12 10:41:15 +08:00
|
|
|
end
|
|
|
|
end
|
2014-03-17 22:43:38 +08:00
|
|
|
|
2014-06-04 15:47:09 +08:00
|
|
|
def handle_course courses, activities
|
2014-06-03 16:21:00 +08:00
|
|
|
course_activity_count_array=activities.values()
|
|
|
|
|
|
|
|
course_array=[]
|
|
|
|
i=0;
|
|
|
|
courses.each do |course|
|
|
|
|
course_array[i]=course
|
|
|
|
i=i+1
|
|
|
|
end
|
|
|
|
|
2014-06-04 15:47:09 +08:00
|
|
|
courses=desc_sort_course_by_avtivity(course_activity_count_array, course_array)
|
2014-06-03 16:21:00 +08:00
|
|
|
|
|
|
|
return courses
|
|
|
|
end
|
|
|
|
|
2014-06-04 16:45:20 +08:00
|
|
|
def settings
|
|
|
|
@issue_custom_fields = IssueCustomField.sorted.all
|
|
|
|
@issue_category ||= IssueCategory.new
|
|
|
|
@member ||= @courses.members.new
|
|
|
|
@trackers = Tracker.sorted.all
|
|
|
|
end
|
|
|
|
|
2014-06-04 15:47:09 +08:00
|
|
|
def create
|
|
|
|
if User.current.user_extensions.identity
|
|
|
|
@course = Course.new
|
|
|
|
@course.extra='course' + DateTime.parse(Time.now.to_s).strftime('%Y-%m-%d_%H-%M-%S').to_s
|
|
|
|
@course.safe_attributes = params[:course]
|
|
|
|
@course.tea_id = User.current.id
|
|
|
|
# added by bai
|
|
|
|
@course.term = params[:term]
|
|
|
|
@course.time = params[:time]
|
|
|
|
#@course.school_id = params[:occupation]
|
|
|
|
@course.school_id = User.current.user_extensions.school_id
|
|
|
|
@course.setup_time = params[:setup_time]
|
|
|
|
@course.endup_time = params[:endup_time]
|
|
|
|
@course.class_period = params[:class_period]
|
2014-06-04 16:45:20 +08:00
|
|
|
@course.description = params[:description]
|
2014-06-04 15:47:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@issue_custom_fields = IssueCustomField.sorted.all
|
|
|
|
@trackers = Tracker.sorted.all
|
|
|
|
|
|
|
|
if User.current.user_extensions.identity == 0
|
|
|
|
if @course.save
|
|
|
|
unless User.current.admin?
|
|
|
|
r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
|
|
|
|
m = Member.new(:user => User.current, :roles => [r])
|
|
|
|
#course = ProjectInfo.new(:user_id => User.current.id, :course_id => @course.id)
|
2014-06-04 16:45:20 +08:00
|
|
|
#user_grades = UserGrade.create(:user_id => User.current.id, :course_id => @course.id)
|
2014-06-04 15:47:09 +08:00
|
|
|
if params[:course][:is_public] == '1'
|
|
|
|
course_status = CourseStatus.create(:course_id => @course.id, :watchers_count => 0, :changesets_count => 0, :grade => 0, :course_type => @course_tag)
|
|
|
|
end
|
|
|
|
@course.members << m
|
|
|
|
#@course.course_infos << course
|
|
|
|
end
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
flash[:notice] = l(:notice_successful_create)
|
|
|
|
if params[:continue]
|
|
|
|
redirect_to new_course_path(attrs, :course => '0')
|
|
|
|
elsif params[:course_continue]
|
|
|
|
redirect_to new_course_path(:course => '1')
|
|
|
|
else
|
|
|
|
redirect_to settings_course_path(@course, :course_type => 1)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'courses', :action => 'show', :id => @course.id) }
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@course.destroy
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { render :action => 'new', :layout => 'base' } #Added by young
|
|
|
|
format.api { render_validation_errors(@course) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-06-04 14:09:37 +08:00
|
|
|
def new
|
2014-06-04 15:47:09 +08:00
|
|
|
@course_type = params[:course_type] ||= params[:course]
|
2014-06-04 14:09:37 +08:00
|
|
|
@issue_custom_fields = IssueCustomField.sorted.all
|
2014-06-04 15:47:09 +08:00
|
|
|
@trackers = Tracker.sorted.all
|
2014-06-04 14:09:37 +08:00
|
|
|
|
2014-06-04 15:47:09 +08:00
|
|
|
@course = Course.new
|
|
|
|
@course.safe_attributes = params[:course]
|
2014-06-04 14:09:37 +08:00
|
|
|
|
|
|
|
render :layout => 'base'
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-06-04 15:47:09 +08:00
|
|
|
def desc_sort_course_by_avtivity(activity_count, courses)
|
2014-06-03 16:21:00 +08:00
|
|
|
return courses if activity_count.size<2
|
|
|
|
(activity_count.size-2).downto(0) do |i|
|
|
|
|
(0..i).each do |j|
|
|
|
|
if activity_count[j]<activity_count[j+1]
|
2014-06-04 15:47:09 +08:00
|
|
|
courses[j], courses[j+1]=courses[j+1], courses[j]
|
|
|
|
activity_count[j], activity_count[j+1]=activity_count[j+1], activity_count[j]
|
2014-06-03 16:21:00 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return courses
|
|
|
|
end
|
2014-05-29 16:35:34 +08:00
|
|
|
|
2014-06-03 09:39:33 +08:00
|
|
|
def index
|
|
|
|
@course_type = params[:course_type]
|
2014-05-29 16:35:34 +08:00
|
|
|
@school_id = params[:school_id]
|
|
|
|
per_page_option = 10
|
2014-06-03 16:21:00 +08:00
|
|
|
|
|
|
|
|
2014-05-29 16:35:34 +08:00
|
|
|
if @school_id == "0" or @school_id.nil?
|
2014-06-03 11:16:06 +08:00
|
|
|
@courses_all = Course.active.visible.
|
|
|
|
joins("LEFT JOIN #{CourseStatus.table_name} ON #{Course.table_name}.id = #{CourseStatus.table_name}.course_id")
|
2014-05-29 16:35:34 +08:00
|
|
|
else
|
2014-06-03 11:16:06 +08:00
|
|
|
@courses_all = Course.active.visible.
|
|
|
|
joins("LEFT JOIN #{CourseStatus.table_name} ON #{Course.table_name}.id = #{CourseStatus.table_name}.course_id").
|
2014-05-29 16:35:34 +08:00
|
|
|
joins(:course_extra).
|
2014-06-03 11:16:06 +08:00
|
|
|
where("#{Course.table_name}.course_type = ? AND #{Course.table_name}.school_id = ?", Course::CourseType_course, @school_id)
|
2014-05-29 16:35:34 +08:00
|
|
|
end
|
|
|
|
|
2014-06-03 09:39:33 +08:00
|
|
|
@course_count = @courses_all.count
|
|
|
|
@course_pages = Paginator.new @course_count, per_page_option, params['page']
|
2014-05-29 16:35:34 +08:00
|
|
|
|
|
|
|
#gcm activity count
|
|
|
|
|
2014-06-03 09:39:33 +08:00
|
|
|
@course_activity_count=Hash.new
|
2014-05-29 16:35:34 +08:00
|
|
|
#count initialize
|
2014-06-03 09:39:33 +08:00
|
|
|
@courses_all.each do |course|
|
|
|
|
@course_activity_count[course.id]=0
|
2014-05-29 16:35:34 +08:00
|
|
|
end
|
|
|
|
|
2014-06-03 09:39:33 +08:00
|
|
|
#@course_activity_count=get_course_activity @courses_all,@course_activity_count
|
2014-05-29 16:35:34 +08:00
|
|
|
#gcm end
|
|
|
|
|
|
|
|
|
2014-06-03 09:39:33 +08:00
|
|
|
case params[:course_sort_type]
|
2014-05-29 16:35:34 +08:00
|
|
|
when '0'
|
2014-06-04 15:47:09 +08:00
|
|
|
@courses = @courses_all.order("created_at desc")
|
2014-05-29 16:35:34 +08:00
|
|
|
@s_type = 0
|
2014-06-04 15:47:09 +08:00
|
|
|
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
|
2014-05-29 16:35:34 +08:00
|
|
|
|
|
|
|
#gcm
|
2014-06-04 15:47:09 +08:00
|
|
|
@course_activity_count=get_course_activity @courses, @course_activity_count
|
2014-05-29 16:35:34 +08:00
|
|
|
#gcmend
|
|
|
|
|
|
|
|
when '1'
|
2014-06-03 09:39:33 +08:00
|
|
|
@courses = @courses_all.order("course_ac_para desc")
|
2014-05-29 16:35:34 +08:00
|
|
|
@s_type = 1
|
2014-06-04 15:47:09 +08:00
|
|
|
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
|
2014-05-29 16:35:34 +08:00
|
|
|
|
|
|
|
#gcm
|
2014-06-04 15:47:09 +08:00
|
|
|
@course_activity_count=get_course_activity @courses, @course_activity_count
|
2014-05-29 16:35:34 +08:00
|
|
|
#gcmend
|
|
|
|
|
|
|
|
when '2'
|
2014-06-03 09:39:33 +08:00
|
|
|
@courses = @courses_all.order("watchers_count desc")
|
2014-05-29 16:35:34 +08:00
|
|
|
@s_type = 2
|
2014-06-04 15:47:09 +08:00
|
|
|
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
|
2014-05-29 16:35:34 +08:00
|
|
|
|
|
|
|
#gcm
|
2014-06-04 15:47:09 +08:00
|
|
|
@course_activity_count=get_course_activity @courses, @course_activity_count
|
2014-05-29 16:35:34 +08:00
|
|
|
#gcmend
|
|
|
|
|
|
|
|
#gcm
|
|
|
|
when '3'
|
|
|
|
|
|
|
|
#gcm
|
2014-06-04 15:47:09 +08:00
|
|
|
@course_activity_count=get_course_activity @courses_all, @course_activity_count
|
2014-05-29 16:35:34 +08:00
|
|
|
#gcmend
|
|
|
|
|
2014-06-04 15:47:09 +08:00
|
|
|
@courses=handle_course @courses_all, @course_activity_count
|
2014-05-29 16:35:34 +08:00
|
|
|
@s_type = 3
|
2014-06-04 15:47:09 +08:00
|
|
|
@courses = @courses[@course_pages.offset, @course_pages.per_page]
|
2014-05-29 16:35:34 +08:00
|
|
|
else
|
|
|
|
@s_type = 0
|
2014-06-03 16:21:00 +08:00
|
|
|
@courses = @courses_all.order("created_at desc")
|
2014-06-04 15:47:09 +08:00
|
|
|
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
|
2014-05-29 16:35:34 +08:00
|
|
|
|
|
|
|
#gcm
|
2014-06-04 15:47:09 +08:00
|
|
|
@course_activity_count=get_course_activity @courses, @course_activity_count
|
2014-05-29 16:35:34 +08:00
|
|
|
#gcmend
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
render :layout => 'base'
|
|
|
|
}
|
|
|
|
format.atom {
|
2014-06-03 11:16:06 +08:00
|
|
|
courses = Course.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all
|
2014-06-03 09:39:33 +08:00
|
|
|
render_feed(courses, :title => "#{Setting.app_title}: #{l(:label_course_latest)}")
|
2014-05-29 16:35:34 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-04 17:06:28 +08:00
|
|
|
def homework
|
|
|
|
@offset, @limit = api_offset_and_limit({:limit => 10})
|
|
|
|
@bids = @course.homeworks.order('deadline DESC')
|
|
|
|
@bids = @bids.like(params[:name]) if params[:name].present?
|
|
|
|
@bid_count = @bids.count
|
|
|
|
@bid_pages = Paginator.new @bid_count, @limit, params['page']
|
|
|
|
|
|
|
|
@offset ||= @bid_pages.reverse_offset
|
|
|
|
unless @offset == 0
|
|
|
|
@bids = @bids.offset(@offset).limit(@limit).all.reverse
|
|
|
|
else
|
|
|
|
limit = @bid_count % @limit
|
|
|
|
@bids = @bids.offset(@offset).limit(limit).all.reverse
|
|
|
|
end
|
|
|
|
render :layout => 'base_courses'
|
|
|
|
end
|
2014-06-03 16:21:00 +08:00
|
|
|
|
2014-06-04 15:47:09 +08:00
|
|
|
def get_course_activity courses, activities
|
2014-06-03 11:16:06 +08:00
|
|
|
@course_ids=activities.keys()
|
|
|
|
|
|
|
|
days = Setting.activity_days_default.to_i
|
|
|
|
date_to ||= Date.today + 1
|
|
|
|
date_from = date_to - days-1.years
|
|
|
|
|
|
|
|
#file_count
|
2014-06-04 15:47:09 +08:00
|
|
|
Attachment.where(container_id: @course_ids, container_type: Project).where("created_on>?", date_from).each do |attachment|
|
2014-06-03 11:16:06 +08:00
|
|
|
activities[attachment.container_id]+=1
|
|
|
|
end
|
|
|
|
|
|
|
|
#message_count
|
|
|
|
Board.where(course_id: @course_ids).each do |board|
|
|
|
|
# activities[board.course_id]+=1
|
2014-06-04 15:47:09 +08:00
|
|
|
activities[board.course_id]+=board.messages.where("updated_on>?", date_from).count
|
2014-06-03 11:16:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
#feedbackc_count
|
|
|
|
JournalsForMessage.where(jour_id: @course_ids, jour_type: Project).each do |jourformess|
|
|
|
|
activities[jourformess.jour_id]+=1
|
|
|
|
end
|
|
|
|
|
|
|
|
#activities!=0
|
|
|
|
i=0;
|
|
|
|
courses.each do |course|
|
|
|
|
id=course.id
|
|
|
|
if activities[id]==0
|
|
|
|
activities[id]=1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return activities
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def allow_join
|
|
|
|
if course_endTime_timeout? Project.find(params[:object_id])
|
|
|
|
respond_to do |format|
|
2014-06-04 15:47:09 +08:00
|
|
|
format.js {
|
2014-06-03 11:16:06 +08:00
|
|
|
@state = 2
|
2014-06-04 15:47:09 +08:00
|
|
|
render :partial => 'set_join',
|
|
|
|
:locals => {:user => User.current,
|
|
|
|
:course => Project.find(params[:object_id]),
|
|
|
|
:object_id => params[:object_id]
|
|
|
|
}
|
2014-06-03 11:16:06 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-06-03 09:39:33 +08:00
|
|
|
def index1
|
2014-05-29 16:35:34 +08:00
|
|
|
|
|
|
|
per_page_option = 10
|
|
|
|
|
2014-06-03 09:39:33 +08:00
|
|
|
#当前所有的活动课程
|
2014-05-29 16:35:34 +08:00
|
|
|
@courses_all = Course.active.visible.
|
2014-05-30 10:48:06 +08:00
|
|
|
joins("LEFT JOIN #{CourseStatus.table_name} ON #{Course.table_name}.id = #{CourseStatus.table_name}.couse_id")
|
2014-05-29 16:35:34 +08:00
|
|
|
|
|
|
|
@course_count = @courses_all.count
|
|
|
|
@course_pages = Paginator.new @course_count, per_page_option, params['page']
|
|
|
|
|
|
|
|
#gcm activity count
|
|
|
|
|
|
|
|
@course_activity_count=Hash.new
|
|
|
|
|
|
|
|
@courses_all.each do |course|
|
|
|
|
@course_activity_count[course.id]=0
|
|
|
|
end
|
|
|
|
|
2014-06-04 15:47:09 +08:00
|
|
|
@course_activity_count=get_course_activity @courses_all, @course_activity_count
|
2014-05-29 16:35:34 +08:00
|
|
|
|
|
|
|
#gcm end
|
|
|
|
|
|
|
|
case params[:course_sort_type]
|
|
|
|
when '0'
|
2014-06-04 15:47:09 +08:00
|
|
|
@courses = @courses_all.order("created_on desc")
|
2014-05-29 16:35:34 +08:00
|
|
|
@s_type = 0
|
|
|
|
when '1'
|
|
|
|
@courses = @courses_all.order("grade desc")
|
|
|
|
@s_type = 1
|
|
|
|
when '2'
|
|
|
|
@courses = @courses_all.order("watchers_count desc")
|
|
|
|
@s_type = 2
|
|
|
|
|
|
|
|
#gcm
|
|
|
|
when '3'
|
|
|
|
#@courses=desc_sort_course_by_avtivity(@course_activity_count_array,@course_all_array)
|
2014-06-04 15:47:09 +08:00
|
|
|
@courses=handle_course @courses_all, @course_activity_count
|
2014-05-29 16:35:34 +08:00
|
|
|
@s_type = 3
|
2014-06-04 15:47:09 +08:00
|
|
|
@courses = @courses[@course_pages.offset, @course_pages.per_page]
|
2014-05-29 16:35:34 +08:00
|
|
|
|
|
|
|
else
|
|
|
|
@courses = @courses = @courses_all.order("grade desc")
|
|
|
|
@s_type = 1
|
|
|
|
end
|
2014-06-04 15:47:09 +08:00
|
|
|
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
|
2014-05-29 16:35:34 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html {
|
|
|
|
render :layout => 'base'
|
|
|
|
# scope = Project
|
|
|
|
# unless params[:closed]
|
|
|
|
# scope = scope.active
|
|
|
|
# end
|
|
|
|
}
|
2014-06-04 15:47:09 +08:00
|
|
|
format.api {
|
2014-05-29 16:35:34 +08:00
|
|
|
# @offset, @limit = api_offset_and_limit
|
|
|
|
# @course_count = Project.visible.count
|
|
|
|
# @courses = Project.visible.offset(@offset).limit(@limit).order('lft').all
|
|
|
|
}
|
|
|
|
format.atom {
|
|
|
|
courses = Project.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all
|
|
|
|
render_feed(courses, :title => "#{Setting.app_title}: #{l(:label_course_latest)}")
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-09-12 10:41:15 +08:00
|
|
|
end
|