#encoding: utf-8 class SyllabusesController < ApplicationController include ApplicationHelper helper :attachments include AttachmentsHelper include CoursesHelper include SyllabusesHelper include UsersHelper before_filter :is_logged, :only => [:index, :show, :edit, :new, :update, :destroy, :delete_syllabus, :syllabus_resources, :syllabus_homeworks] before_filter :check_authentication before_filter :find_syllabus, :only => [:show, :edit, :update, :destroy, :syllabus_courselist, :edit_syllabus_eng_name, :edit_syllabus_title, :update_base_info, :delete_syllabus, :delete_des, :members, :update_des, :edit_ref, :update_ref, :add_sy_member_alert, :search_not_sy_member, :syllabus_resources, :syllabus_homeworks, :send_homeworks_to_course, :choose_user_course, :send_resources_to_course, :send_r_and_h_to_course] def index @status = params[:type].nil? ? 1 : params[:type].to_i # 1表示正在进行,2表示已结束 @tab = params[:tab].nil? ? 1 : params[:tab].to_i @my_syllabuses = "我的课堂" user = User.current if @status == 1 case @tab when 2 @courses = user.courses.not_deleted_not_end.order("updated_at desc").select{ |course| user.has_teacher_role(course)} when 3 @courses = user.courses.not_deleted_not_end.order("updated_at desc").select{ |course| !user.has_teacher_role(course)} when 1 @courses = user.courses.not_deleted_not_end.order("updated_at desc") end else case @tab when 2 @courses = user.courses.not_deleted_but_is_end.order("updated_at desc").select{ |course| user.has_teacher_role(course)} when 3 @courses = user.courses.not_deleted_but_is_end.order("updated_at desc").select{ |course| !user.has_teacher_role(course)} when 1 @courses = user.courses.not_deleted_but_is_end.order("updated_at desc") end end @limit = 15 @is_remote = true @courses_count = @courses.count @courses_pages = Paginator.new @courses_count, @limit, params['page'] || 1 @offset ||= @courses_pages.offset @courses = paginateHelper @courses, @limit respond_to do |format| format.html{ render :layout => 'base_edu'} format.js end end def show @is_syllabus_teacher = User.current.admin? || @syllabus.syllabus_members.map{|sm| sm.user_id}.include?(User.current.id) || !@syllabus.courses.not_deleted_not_end.select { |course| User.current.has_teacher_role(course)}.blank? @is_syllabus_member = !@syllabus.courses.not_deleted_not_end.select { |course| User.current.member_of_course?(course)}.blank? @is_syllabus_admin = User.current.admin? || @syllabus.syllabus_members.map{|sm| sm.user_id}.include?(User.current.id) @is_authen_teacher = User.current.authentication && User.current.user_extensions.try(:identity) != 1 @reference_materials = @syllabus.reference_materials @members = @syllabus.syllabus_members.includes(:user => {:user_extensions => []}).order("rank asc") @courses = @syllabus.courses.where("is_delete = 0 and is_end = 0").select("courses.*,(SELECT MAX(updated_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS ca_update").order("ca_update desc") @end_courses = @syllabus.courses.where("is_delete = 0 and is_end = 1").select("courses.*,(SELECT MAX(updated_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS ca_update").order("ca_update desc") respond_to do |format| format.js format.html{render :layout => 'base_edu_syllabus'} format.api end end def new @syllabus = Syllabus.new render :layout => 'base_edu' end def create if User.current.user_extensions.identity @syllabus = Syllabus.new @syllabus.title = params[:title].to_s.strip @syllabus.eng_name = params[:eng_name] @syllabus.user_id = User.current.id @syllabus.major_level = params[:major_level].to_i @syllabus.discipline_category_id = params[:discipline_category_id].to_i @syllabus.first_level_discipline_id = params[:first_level_discipline_id].to_i @syllabus.major_id = params[:major_id].to_i @syllabus.syllabus_type = params[:syllabus_type].to_i @syllabus.description = Message.where("id = 19412").first.nil? ? '' : Message.where("id = 19412").first.content if @syllabus && @syllabus.save member = SyllabusMember.create(:user_id => @syllabus.user_id, :rank => 1) @syllabus.syllabus_members << member respond_to do |format| #flash[:notice] = l(:notice_successful_create) format.html {redirect_to syllabus_path(@syllabus)} format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'syllabuses', :action => 'show', :id => @syllabus.id) } end else respond_to do |format| flash[:notice] = l(:notice_create_failed) format.html { redirect_to new_syllabus_path } #Added by young format.api { render_validation_errors(@syllabus) } end end end end def regex_syllabus_name data = {result:1} if params[:syllabus_id] if User.current.syllabuses.where("id != #{params[:syllabus_id]} and title = '#{params[:name]}'").count > 0 data[:result] = 0 end else if Syllabus.where(:user_id => User.current.id, :title => params[:name]).count > 0 data[:result] = 0 end end render :json => data end def edit respond_to do |format| format.html{render :layout => 'base_edu'} end end def update title = @syllabus.title @syllabus.title = params[:title].to_s.strip @syllabus.eng_name = params[:eng_name] @syllabus.major_level = params[:major_level].to_i @syllabus.discipline_category_id = params[:discipline_category_id].to_i @syllabus.first_level_discipline_id = params[:first_level_discipline_id].to_i @syllabus.major_id = params[:major_id].to_i @syllabus.syllabus_type = params[:syllabus_type].to_i if @syllabus.save if title != params[:title].to_s.strip @syllabus.courses.each do |course| course.name.gsub!(/^#{title}/, params[:title].to_s.strip) course.save end end end redirect_to syllabus_path(@syllabus) end def edit_ref @reference_materials = @syllabus.reference_materials respond_to do |format| format.js end end def update_ref @syllabus.reference_materials.destroy_all refer_inputs = params[:syllabus][:book] if params[:syllabus] if Array === refer_inputs refer_inputs.each_with_index do |val, i| @syllabus.reference_materials << ReferenceMaterial.new( book: val, editor: params[:syllabus][:editor][i], press: params[:syllabus][:press][i] ) end end sy_update_record = SyllabusUpdateRecord.new(:property => 2, :user_id => User.current.id) @syllabus.syllabus_update_records << sy_update_record @reference_materials = @syllabus.reference_materials @is_syllabus_admin = User.current.admin? || @syllabus.syllabus_members.map{|sm| sm.user_id}.include?(User.current.id) respond_to do |format| format.js end end def update_des @syllabus.description = params[:syllabus][:description] @syllabus.des_status = 1 #@syllabus.save_attachments(params[:attachments]) if @syllabus.save sy_update_record = SyllabusUpdateRecord.new(:property => 1, :user_id => User.current.id) @syllabus.syllabus_update_records << sy_update_record if params[:asset_id] ids = params[:asset_id].split(',') update_kindeditor_assets_owner ids,@syllabus.id,OwnerTypeHelper::SYLLABUS end end redirect_to syllabus_path(@syllabus) end def add_sy_member_alert respond_to do |format| format.js end end def search_not_sy_member if params[:q] && params[:q].lstrip.rstrip != "" @users = Principal.active.sorted.not_member_of_syllabus(@syllabus).like(params[:q]) else @users = [] end end def syllabus_resources @is_syllabus_teacher = User.current.admin? || @syllabus.syllabus_members.map{|sm| sm.user_id}.include?(User.current.id) || !@syllabus.courses.not_deleted_not_end.select { |course| User.current.has_teacher_role(course)}.blank? @is_syllabus_member = !@syllabus.courses.not_deleted_not_end.select { |course| User.current.member_of_course?(course)}.blank? @is_syllabus_admin = User.current.admin? || @syllabus.syllabus_members.map{|sm| sm.user_id}.include?(User.current.id) @is_authen_teacher = User.current.authentication && User.current.user_extensions.try(:identity) != 1 @order, @r_sort = params[:order] || "created_at", params[:sort] || "asc" @sort = @r_sort == "desc" ? "asc" : "desc" @resources = sy_resources @syllabus if @order == "created_at" @resources = @resources.order("#{@order} #{@sort}") else @resources = @resources.select("resource_banks.*, (quotes+downloads) as heat").order("#{@order} #{@sort}, created_at desc") end @resource_count = @resources.count @public_resource_count = @resources.where(:is_public => 1).count @private_resource_count = @resources.where(:is_public => 0).count if !(@is_syllabus_admin || @is_syllabus_member) @resources = @resources.where(:is_public => 1) end #分页 @page_resource_count = @resources.count @limit = 10 @is_remote = true @resource_pages = Paginator.new @page_resource_count, @limit, params['page'] || 1 @offset ||= @resource_pages.offset @resources = paginateHelper @resources,@limit respond_to do |format| format.js end end def syllabus_homeworks @is_syllabus_teacher = User.current.admin? || @syllabus.syllabus_members.map{|sm| sm.user_id}.include?(User.current.id) || !@syllabus.courses.not_deleted_not_end.select { |course| User.current.has_teacher_role(course)}.blank? @is_syllabus_member = !@syllabus.courses.not_deleted_not_end.select { |course| User.current.member_of_course?(course)}.blank? @is_syllabus_admin = User.current.admin? || @syllabus.syllabus_members.map{|sm| sm.user_id}.include?(User.current.id) @is_authen_teacher = User.current.authentication && User.current.user_extensions.try(:identity) != 1 @order, @r_sort = params[:order] || "created_at", params[:sort] || "asc" @sort = @r_sort == "desc" ? "asc" : "desc" @homeworks = @syllabus.homework_banks.order("#{@order} #{@sort}") @homework_count = @homeworks.count @public_homework_count = @homeworks.where(:is_public => 1).count @private_homework_count = @homeworks.where(:is_public => 0).count if !(@is_syllabus_admin || @is_syllabus_member) @homeworks = @homeworks.where(:is_public => 1) end @page_homework_count = @homeworks.count @limit = 10 @is_remote = true @homework_pages = Paginator.new @page_homework_count, @limit, params['page'] || 1 @offset ||= @homework_pages.offset @homeworks = paginateHelper @homeworks, @limit respond_to do |format| format.js end end def choose_user_course if !params[:search].nil? search = "%#{params[:search].to_s.strip.downcase}%" @courses = User.current.courses.where("is_delete = 0 and is_end = 0 and (#{Course.table_name}.id = #{params[:search].to_i } or #{Course.table_name}.name like :p)",:p=>search).select { |course| User.current.has_teacher_role(course)} else @courses = User.current.courses.where("is_delete = 0 and is_end = 0").select { |course| User.current.has_teacher_role(course)} end @resource_ids = params[:check_resource] if params[:check_resource] @homework_ids = params[:check_homework] if params[:check_homework] @search = params[:search] #这里仅仅是传递需要发送的资源id @type = params[:type] respond_to do |format| format.js end end def send_resources_to_course course = Course.find params[:course_id] if course params[:resource_id].each do |resource_id| resource = ResourceBank.find resource_id quote_resource_bank resource, course end end end def send_homeworks_to_course course = Course.find params[:course_id] if course params[:homework_id].each do |homework_id| homework = HomeworkBank.find homework_id quote_homework_bank homework, course end end end def send_r_and_h_to_course course = Course.find params[:course_id] if course resources = sy_resources @syllabus homeworks = @syllabus.homework_banks.order("#{@order} #{@sort}") ActiveRecord::Base.transaction do begin resources.each do |resource| quote_resource_bank resource, course end homeworks.each do |homework| quote_homework_bank homework, course end rescue Exception => e puts e end end end end def delete_syllabus respond_to do |format| format.js end end #删除课程大纲的描述 def delete_des if @syllabus @syllabus.description = Message.where("id = 19412").first.nil? ? '' : Message.where("id = 19412").first.content @syllabus.des_status = 0 @syllabus.attachments.destroy_all if @syllabus.save redirect_to syllabus_path(@syllabus) end end end def destroy if @syllabus && @syllabus.courses.not_deleted.empty? @syllabus.destroy redirect_to syllabuses_path() end end #班级列表 list_type: 1 班级列表 2 归档班级列表 def syllabus_courselist @list_type = params[:list_type].to_i || 0 @order, @c_sort,@type = params[:order] || 1, params[:sort] || 1, params[:type] || 1 #确定 sort_type if @order.to_i == @type.to_i @c_sort = @c_sort.to_i == 1 ? 2 : 1 #1升序 2降序 else @c_sort = 2 end sort_name = "updated_on" sort_type = @c_sort == 1 ? "asc" : "desc" @courses = @syllabus.courses.where("is_delete = ?", @list_type).select("courses.*,(SELECT MAX(updated_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS #{sort_name}").order("#{sort_name} #{sort_type}") #根据 作业+资源数排序 if @order.to_i == 2 @type = 2 @courses.each do |course| course[:infocount] = (User.current.admin? || User.current.allowed_to?(:as_teacher,course)) ? (course.homework_commons.count + visable_attachemnts_incourse(course).count) : (course.homework_commons.where("publish_time <= '#{Time.now}'").count + visable_attachemnts_incourse(course).count) if course[:infocount] < 0 course[:infocount] = 0 end end @c_sort == 1 ? (@courses = @courses.sort{|x,y| x[:infocount] <=> y[:infocount] }) : (@courses = @courses.sort{|x,y| y[:infocount] <=> x[:infocount]}) @courses = sortby_time_countcommon_nosticky @courses,sort_name else @type = 1 end #分页 @limit = 10 @is_remote = true @atta_count = @courses.count @atta_pages = Paginator.new @atta_count, @limit, params['page'] || 1 @offset ||= @atta_pages.offset @courses = paginateHelper @courses,@limit respond_to do |format| format.js format.html{render :layout => 'base_syllabus'} end end #修改课程名称 def edit_syllabus_title if @syllabus && params[:title] != "" @syllabus.update_column("title",params[:title]) end respond_to do |format| format.js end end #修改英文名称 def edit_syllabus_eng_name if @syllabus @syllabus.update_column("eng_name",params[:eng_name]) end respond_to do |format| format.js end end #编辑属性 def update_base_info if @syllabus @syllabus.update_attributes(:credit => params[:credit], :hours => params[:hours], :theory_hours => params[:theory_hours], :practice_hours => params[:practice_hours], :applicable_major => params[:applicable_major], :pre_course => params[:pre_course]) @syllabus.update_attributes(:syllabus_type => params[:syllabus_type]) respond_to do |format| format.js end end end def members @members = @syllabus.syllabus_members.includes(:user => {:user_extensions => [], :courses => []}).order("rank asc") @show = params[:show] ? params[:show].to_i : 0 respond_to do |format| format.js format.html{render :layout => 'base_syllabus'} end end def get_discipline_categories data = {result:0,options:[]} if params[:major_level] if DisciplineCategory.where(:major_level => params[:major_level].to_i).count > 0 data[:result] = 1 DisciplineCategory.where(:major_level => params[:major_level].to_i).each do |dis| option = [] option << dis.name.to_s option << dis.id data[:options] << option end end end render :json =>data end def get_first_level_disciplines data = {result:0,options:[]} if params[:discipline_category_id] if FirstLevelDiscipline.where(:discipline_category_id => params[:discipline_category_id].to_i).count > 0 data[:result] = 1 FirstLevelDiscipline.where(:discipline_category_id => params[:discipline_category_id].to_i).each do |dis| option = [] option << dis.name.to_s option << dis.id data[:options] << option end end end render :json =>data end def get_major data = {result:0,options:[]} if params[:first_level_discipline_id] if Major.where(:first_level_discipline_id => params[:first_level_discipline_id].to_i).count > 0 data[:result] = 1 Major.where(:first_level_discipline_id => params[:first_level_discipline_id].to_i).each do |dis| option = [] option << dis.name.to_s option << dis.id data[:options] << option end end end render :json =>data end private def find_syllabus @syllabus = Syllabus.find params[:id] end def is_logged redirect_to signin_path unless User.current.logged? end end