class CoursesController < ApplicationController include CoursesHelper helper :activities helper :members 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 before_filter :find_course, :except => [ :index, :search,:list, :new, :create, :copy, :statistics, :new_join, :course, :enterprise_course, :course_enterprise,:view_homework_attaches] before_filter :authorize_course, :only => [:show, :settings, :edit, :update, :modules, :close, :reopen, :view_homework_attaches, :course] before_filter :authorize_course_global, :only => [:view_homework_attaches, :new,:create] before_filter :require_admin, :only => [:copy, :archive, :unarchive, :destroy, :calendar] before_filter :toggleCourse, only: [:finishcourse, :restartcourse] before_filter :require_login, :only => [:join, :unjoin] before_filter :allow_join, :only => [:join] def join if User.current.logged? course = Project.find(params[:object_id]) course_prefs = Course.find_by_extra(course.identifier) if params[:course_password] == course_prefs.password members = [] members << Member.new(:role_ids => [10], :user_id => User.current.id) course.members << members StudentsForCourse.create(:student_id => User.current.id, :course_id => params[:object_id]) @state = 0 else @state = 1 end end respond_to do |format| # format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} format.js { render :partial => 'set_join', :locals => {:user => User.current, :course => Project.find(params[:object_id]), :object_id => params[:object_id]} } end end def unjoin if User.current.logged? @member = Member.where('project_id = ? and user_id = ?', params[:object_id], User.current.id) @member.first.destroy 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}} format.js { render :partial => 'set_join', :locals => {:user => User.current, :course => Project.find(params[:object_id]), :object_id => params[:object_id]} } end end def update @course.safe_attributes = params[:course] @course.class_period = params[:class_period] if @course.save if params[:course][:is_public] == '0' course_status = CourseStatus.find_by_course_id(@course.id) course_status.destroy if course_status elsif params[:course][:is_public] == '1' course_status = CourseStatus.create(:course_id => @course.id, :grade => 0) end respond_to do |format| format.html { flash[:notice] = l(:notice_successful_update) redirect_to settings_course_path(@course) } format.api { render_api_ok } end else respond_to do |format| format.html { settings render :action => 'settings' } format.api { render_validation_errors(@course) } end end end def handle_course courses, activities course_activity_count_array=activities.values() course_array=[] i=0; courses.each do |course| course_array[i]=course i=i+1 end courses=desc_sort_course_by_avtivity(course_activity_count_array, course_array) return courses end def settings @issue_custom_fields = IssueCustomField.sorted.all @issue_category ||= IssueCategory.new @member ||= @course.members.new @trackers = Tracker.sorted.all end 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] @course.description = params[:description] 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]) m.project_id = -1 course = CourseInfos.new(:user_id => User.current.id, :course_id => @course.id) #user_grades = UserGrade.create(:user_id => User.current.id, :course_id => @course.id) 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 def course @school_id = params[:school_id] per_page_option = 10 if @school_id == "0" or @school_id.nil? @courses_all = Course.active.visible. joins("LEFT JOIN #{CourseStatus.table_name} ON #{Course.table_name}.id = #{CourseStatus.table_name}.course_id"). where("#{Course.table_name}.course_type = ? ", Course::CourseType_course) else @courses_all = Course.active.visible. joins("LEFT JOIN #{CourseStatus.table_name} ON #{Course.table_name}.id = #{CourseStatus.table_name}.course_id"). joins(:course_extra). where("#{Course.table_name}.course_type = ? AND #{Course.table_name}.school_id = ?", Course::CourseType_course, @school_id) end @course_count = @courses_all.count @course_pages = Paginator.new @course_count, per_page_option, params['page'] #gcm activity count @course_activity_count=Hash.new #count initialize @courses_all.each do |course| @course_activity_count[course.id]=0 end #@course_activity_count=get_course_activity @courses_all,@course_activity_count #gcm end case params[:course_sort_type] when '0' @courses = @courses_all.order("created_on desc") @s_type = 0 @courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page) #gcm @course_activity_count=get_course_activity @courses,@course_activity_count #gcmend when '1' @courses = @courses_all.order("course_ac_para desc") @s_type = 1 @courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page) #gcm @course_activity_count=get_course_activity @courses,@course_activity_count #gcmend when '2' @courses = @courses_all.order("watchers_count desc") @s_type = 2 @courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page) #gcm @course_activity_count=get_course_activity @courses,@course_activity_count #gcmend #gcm when '3' #gcm @course_activity_count=get_course_activity @courses_all,@course_activity_count #gcmend @courses=handle_course @courses_all,@course_activity_count @s_type = 3 @courses = @courses[@course_pages.offset, @course_pages.per_page] else @s_type = 0 @courses = @courses_all.order("created_on desc") @courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page) #gcm @course_activity_count=get_course_activity @courses,@course_activity_count #gcmend end respond_to do |format| format.html { render :layout => 'base' } format.api { # @offset, @limit = api_offset_and_limit # @course_count = Course.visible.count # @courses = Course.visible.offset(@offset).limit(@limit).order('lft').all } format.atom { courses = Course.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 def new @course_type = params[:course_type] ||= params[:course] @issue_custom_fields = IssueCustomField.sorted.all @trackers = Tracker.sorted.all @course = Course.new @course.safe_attributes = params[:course] render :layout => 'base' end def desc_sort_course_by_avtivity(activity_count, courses) return courses if activity_count.size<2 (activity_count.size-2).downto(0) do |i| (0..i).each do |j| if activity_count[j] 'base' } format.atom { courses = Course.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 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 # 新建作业 def new_homework if (User.current.logged? && (User.current.admin? || ( !Member.where('user_id = ? and course_id = ?', User.current.id, @course.id).first.nil? && ( Member.where('user_id = ? and course_id = ?', User.current.id, @course.id).first.roles && ( Role.where(id: [3, 4, 7, 9]).size > 0 ) ) ) ) ) @homework = Bid.new @homework.safe_attributes = params[:bid] render :layout => 'base_courses' else render_404 end end def get_course_activity courses, activities @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 Attachment.where(container_id: @course_ids, container_type: Course).where("created_on>?", date_from).each do |attachment| activities[attachment.container_id]+=1 end #message_count Board.where(course_id: @course_ids).each do |board| # activities[board.course_id]+=1 activities[board.course_id]+=board.messages.where("updated_on>?", date_from).count end #feedbackc_count JournalsForMessage.where(jour_id: @course_ids, jour_type: Course).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 def toggleCourse @course_prefs = Course.find_by_extra(@course.extra) unless (@course_prefs.teacher == User.current || User.current.admin?) render_403 end end def finishcourse yesterday = Date.today.prev_day.to_time @course_prefs.endup_time = yesterday @save_flag = @course_prefs.save respond_to do |format| format.js end end def restartcourse day = Time.parse("3000-01-01") @course_prefs.endup_time = day @save_flag = @course_prefs.save respond_to do |format| format.js { render action:'finishcourse' } end end def show # try to redirect to the requested menu item if params[:jump] && redirect_to_course_menu_item(@course, params[:jump]) return end @users_by_role = @course.users_by_role if(User.find_by_id(CourseInfos.find_by_course_id(@course.id).try(:user_id))) @user = User.find_by_id(CourseInfos.find_by_course_id(@course.id).user_id) end @key = User.current.rss_key #新增内容 @days = Setting.activity_days_default.to_i if params[:from] begin; @date_to = params[:from].to_date + 1; rescue; end end has = { "show_files" => true, "show_messages" => true, "show_bids" => true, "show_contests" => true, "show_journals_for_messages" => true } @date_to ||= Date.today + 1 @date_from = @date_to - @days-1.years @author = (params[:user_id].blank? ? nil : User.active.find(params[:user_id])) # 决定显示所用用户或单个用户活动 @activity = Redmine::Activity::Fetcher.new(User.current, :course => @course, :with_subprojects => false, :author => @author) @activity.scope_select {|t| !has["show_#{t}"].nil?} events = @activity.events(@date_from, @date_to) # @activity = Activity.where("update_on between " +@date_from.to_s + " and " +@date_to.to_s ).order('id desc') #@activity_count = @activity.count #@activity_pages = Paginator.new @activity_count, pre_count, params['page'] # @activity = activity.offset(@activity_pages.offset).limit(@activity_pages.per_page).all @offset, @limit = api_offset_and_limit({:limit => 10}) @events_count = events.count @events_pages = Paginator.new @events_count, @limit, params['page'] @offset ||= @events_pages.offset events = events.slice(@offset,@limit) @events_by_day = events.group_by {|event| User.current.time_to_date(event.event_datetime)} # documents @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category' # @teachers= searchTeacherAndAssistant(@course) @canShowRealName = isCourseTeacher(User.current.id) if(User.find_by_id(CourseInfos.find_by_course_id(@course.id).try(:user_id))) @user = User.find_by_id(CourseInfos.find_by_course_id(@course.id).user_id) end respond_to do |format| format.html{render :layout => 'base_courses'} format.api end end #判断指定用户是否为课程教师 def isCourseTeacher(id) result = false if @teachers && @teachers.find_by_user_id(id) != nil result = true end result end def feedback page = params[:page] # Find the page of the requested reply @jours = @course.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC') @limit = 10 if params[:r] && page.nil? offset = @jours.count(:conditions => ["#{JournalsForMessage.table_name}.id > ?", params[:r].to_i]) page = 1 + offset / @limit end @feedback_count = @jours.count @feedback_pages = Paginator.new @feedback_count, @limit, page @offset ||= @feedback_pages.offset @jour = @jours[@offset, @limit] @state = false @base_courses_tag = @course.course_type respond_to do |format| format.html{render :layout => 'base_courses' if @base_courses_tag==1} format.api end end private def allow_join if course_endTime_timeout? Project.find(params[:object_id]) respond_to do |format| format.js { @state = 2 render :partial => 'set_join', :locals => {:user => User.current, :course => Project.find(params[:object_id]), :object_id => params[:object_id] } } end end end end