#coding=utf-8 class CoursesService include ApplicationHelper include CoursesHelper include HomeworkAttachHelper include ApiHelper include ActionView::Helpers::DateHelper #参数school_id为0或不传时返回所有课程,否则返回对应学校的课程 #参数per_page_count分页功能,每页显示的课程数 #参数page分页功能,当前页码 def course_list params, current_user @school_id = params[:school_id] per_page_option = params[:per_page_count] || 10 page_no = params[:page] || 1 if @school_id == "0" || @school_id.nil? @courses_all = Course.active.visible. joins("LEFT JOIN #{CourseStatus.table_name} ON #{Course.table_name}.id = #{CourseStatus.table_name}.course_id") else @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}.school_id = ?", @school_id) end @course_count = @courses_all.count @course_pages = Redmine::Pagination::Paginator.new @course_count, per_page_option, page_no @courses = @courses_all.order("created_at desc") @courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page) course_list = [] @courses.each do |course| course_list << {:course => course, :img_url => url_to_avatar(course), :current_user_is_member => current_user.member_of_course?(course), :current_user_is_teacher => is_course_teacher(current_user, course)} end course_list end def add_board(current_user, course_id, board_name) course = Course.find(course_id) raise "权限不足" unless current_user.has_teacher_role(course) position = 1 last_board = ::Board.where(course_id: course.id).order('position desc').first if last_board.present? position = last_board.position+1 end ::Board.create!(name: board_name, description: board_name, project_id: -1, parent_id: 0, topics_count: 0, messages_count: 0, position: position, course_id: course.id ) end def edit_board(current_user, board_id, board_name) board = ::Board.find(board_id) raise "权限不足" unless current_user.has_teacher_role(board.course) board.name = board_name board.save! return board end def user_courses_list(current_user) courses = current_user.courses.not_deleted courses.inject([]) {|course_list, course| course_list << {:course => course, :img_url => url_to_avatar(course), :current_user_is_member => current_user.member_of_course?(course), :current_user_is_teacher => is_course_teacher(current_user, course), course_student_num: searchStudent(course).count } } end #搜索课程 def search_course params, current_user courses_all = Course.all_course name = params[:name] if name.blank? raise 'sumbit empty' end @courses = courses_all.visible(current_user) if params[:name].present? @courses_all = @courses.like(params[:name]).order("created_at desc") else @courses_all = @courses.order("created_at desc"); end @courses_all course_list = [] @courses_all.each do |course| course_list << {:course => course, :img_url => url_to_avatar(course), :current_user_is_member => current_user.member_of_course?(course), :current_user_is_teacher => is_course_teacher(current_user, course)} end course_list end #获取头像 def get_img obj url_to_avatar(obj) end #课程老师或课程学生列表 def course_teacher_or_student_list params, course, current_user if course.is_a?(Course) c = course else c = Course.find(course) end # if current_user.nil? || !(current_user.admin? || c.is_public == 1 || (c.is_public == 0 && current_user.member_of_course?(c))) if current_user.nil? raise '403' end #@canShowCode = isCourseTeacher(User.current.id,course) && params[:role] != '1' case params[:role] when '1' #@subPage_title = l :label_teacher_list @members = searchTeacherAndAssistant(c) when '2' #@subPage_title = l :label_student_list # @members = searchStudent(c) @members = searchmember_by_name(student_homework_score(0, c.id, 0, "desc"), "") else #@subPage_title = '' @members = c.member_principals.includes(:roles, :principal).all.sort end users = [] @members.each do |m| img_url = "/images/" + url_to_avatar(m.user) gender = m.user.user_extensions.gender.nil? ? 0 : m.user.user_extensions.gender work_unit = get_user_work_unit m.user location = get_user_location m.user role_ids = [] m.roles.each do |r| role_ids << r.id end #双重身份 学生列表中不显示 # unless (params[:role] == '2' && role_ids.length >= 2) # users << {:id => m.user.id, # :img_url => img_url, # :nickname => m.user.nickname, # :gender => gender, # :work_unit => work_unit, :mail => m.user.mail, :location => location, # role_name: m.roles.first.name, # name: m.user.show_name, # roles_id: role_ids.include?(7) ? 7 : (role_ids.include?(9) ? 9 : 10), # :brief_introduction => m.user.user_extensions.brief_introduction, # :realname => m.user.realname, # :is_me => current_user.id == m.user.id ? 1 : 0} # end # users << m.user end users end def reviewers_list course_id reviewers = [] c = Course.find(course_id) if c messages = CourseMessage.where("course_id=? and course_message_type = 'JoinCourseRequest' and status = 0 ", course_id) messages.each do |m| user = User.find(m.course_message_id) if user img_url = url_to_avatar(user) gender = user.user_extensions.gender.nil? ? 0 : user.user_extensions.gender work_unit = get_user_work_unit user location = get_user_location user roles_ids = m.content.split(",") reviewers << {:id => user.id, :img_url => img_url, :nickname => user.nickname, :gender => gender, :work_unit => work_unit, :mail => user.mail, :location => location, role_name: "", name: user.show_name, roles_id: roles_ids.include?("7") ? 7 : 9, :brief_introduction => user.user_extensions.brief_introduction, :realname => user.realname} end end end reviewers end def get_reviewer_info params status = -1 info = nil c = Course.find(params[:course_id]) if c messages = CourseMessage.where("course_id=? and course_message_id = ? and course_message_type = 'JoinCourseRequest' ", params[:course_id], params[:user_id]).last if messages #status 0未处理 1同意 2 通过 status = messages.status user = User.find(params[:user_id]) if user img_url = url_to_avatar(user) gender = user.user_extensions.gender.nil? ? 0 : user.user_extensions.gender work_unit = get_user_work_unit user location = get_user_location user roles_ids = messages.content.split(",") info = {:id => user.id, :img_url => img_url, :nickname => user.nickname, :gender => gender, :work_unit => work_unit, :mail => user.mail, :location => location, role_name: "", name: user.show_name, roles_id: roles_ids.include?("7") ? 7 : 9, :brief_introduction => user.user_extensions.brief_introduction, :realname => user.realname} end end end {status: status, reviewer: info} end def deal_join_apply params, current_user status = -1 message = "" c = Course.find(params[:course_id]) if c messages = CourseMessage.where("course_id=? and course_message_id = ? and course_message_type = 'JoinCourseRequest' and status = 0 ", params[:course_id], params[:user_id]).first if messages apply_user = User.find(params[:user_id]) ids = messages.content.split(",") # content保存的是申请的职位角色 integer_ids = [] ids.each do |role_id| integer_ids << role_id.to_i end if params[:type] == 0 if apply_user.member_of_course?(c) #将角色改为老师或者教辅 member = c.members.where(:user_id => apply_user.id).all[0] member.role_ids = integer_ids #删除为学生的记录 unless member.role_ids.include?(10) joined = StudentsForCourse.where('student_id = ? and course_id = ?', member.user_id, c.id) joined.each do |join| join.destroy end end member.course_group_id = 0 member.save CourseMessage.create(:user_id => apply_user.id, :course_id => c.id, :viewed => false, :content => messages.content, :course_message_id => current_user.id, :course_message_type => 'CourseRequestDealResult', :status => 1) messages.update_attributes(:status => 1, :viewed => 1) else members = [] members << Member.new(:role_ids => integer_ids, :user_id => apply_user.id) c.members << members CourseMessage.create(:user_id => apply_user.id, :course_id => c.id, :viewed => false, :content => messages.content, :course_message_id => current_user.id, :course_message_type => 'CourseRequestDealResult', :status => 1) messages.update_attributes(:status => 1, :viewed => 1) end if integer_ids.include?(9) message = "您已同意教师" + apply_user.show_name + ",加入班级" # send_wechat_join_class_notice apply_user,c,9,0 else message = "您已同意助教" + apply_user.show_name + ",加入班级" # send_wechat_join_class_notice apply_user,c,7,0 end else CourseMessage.create(:user_id => apply_user.id, :course_id => c.id, :viewed => false, :content => messages.content, :course_message_id => current_user.id, :course_message_type => 'CourseRequestDealResult', :status => 2) messages.update_attributes(:status => 2, :viewed => 1) if integer_ids.include?(9) message = "您已拒绝教师" + apply_user.show_name + ",加入班级" # send_wechat_join_class_notice apply_user,c,9,1 else message = "您已拒绝助教" + apply_user.show_name + ",加入班级" # send_wechat_join_class_notice apply_user,c,7,1 end end status = 0 else message = "该申请不存在或已被处理" end else message = "该班级不存在或已被删除" end {:status => status, :message => message} end #获取用户的工作单位 def get_user_work_unit user work_unit = "" if user.user_extensions.identity == 0 || user.user_extensions.identity == 1 work_unit = user.user_extensions.school.name unless user.user_extensions.school.nil? elsif user.user_extensions.identity == 3 work_unit = user.user_extensions.occupation elsif user.user_extensions.identity == 2 work_unit = user.firstname end work_unit end #获取用户地区 def get_user_location user location = "" location << (user.user_extensions.location || '') location << (user.user_extensions.location_city || '') location end #课程通知列表 def course_news_list params, current_user if params[:course_id] && @course == nil @course = Course.find(params[:course_id]) end if current_user.nil? || !(current_user.admin? || @course.is_public == 1 || (@course.is_public == 0 && current_user.member_of_course?(@course))) raise '403' end scope = @course ? @course.news.order("news.created_on desc").course_visible(current_user) : News.order("news.created_on desc").course_visible(current_user) news = [] scope.each do |n| news << {:id => n.id, :title => n.title, :author_name => n.author.name, :author_id => n.author.id, :author => n.author, :description => n.description, :created_on => format_time(n.created_on), :comments_count => n.comments_count} end news end #查看新闻权限验证 def show_course_news_authorize(current_user) unless current_user.allowed_to?({:controller => 'news', :action => 'show'}, false) raise '403' end end #显示课程通知(包括评论) 需验证权限 def show_course_news params, current_user @news = News.find(params[:id]) @course = @news.course if @course if current_user.nil? || !(current_user.admin? || @course.is_public == 1 || (@course.is_public == 0 && current_user.member_of_course?(@course))) raise '403' end end @comments = @news.comments.reorder("created_on desc") @comments.reverse! if current_user.wants_comments_in_reverse_order? {:news => @news, :comments => @comments} #comments = [] #@comments.each do |comment| # comments << {:author_id => comment.author_id,:author_name => comment.author.name,:commont_content => comment.comments,:time => format_time(comment.created_on)} #end #{:title => @news.title,:author_name => @news.author.name,:author_id => @news.author.id, :description => @news.description,:created_on => format_time(@news.created_on), # :comments_count => @news.comments_count,:comments => comments} end #显示课程 def show_course(params, current_user) course = Course.find(params[:id]) course.generate_invite_code course.generate_qrcode if course.school work_unit = course.school.name else work_unit = get_user_work_unit course.teacher end # unless (course.is_public == 1 || current_user.member_of_course?(course) || current_user.admin?) # raise '403' # end groupnum = 0 groupnum = course.course_groups.length if course.course_groups {:course => course, :syllabus_title => course.syllabus.nil? ? "" : course.syllabus.title, :work_unit => work_unit, :img_url => "/images/" + url_to_avatar(course), :current_user_is_member => current_user.nil? ? false : current_user.member_of_course?(course), :current_user_is_teacher => current_user.nil? ? false : is_course_teacher(current_user, course), :course_student_num => course ? course.student.count.to_s : 0, :groupnum => groupnum} end #创建课程 #current_user当前用户对象(不是id) # params[:course][:name]:课程名称 #params[:course][:password]:密码 #params[:course][:description]:描述 #params[:course][:is_public]:是否公开1公开,0私有 #params[:course][:open_student]:是否公开学生列表1公开,0不公开,不公开时非课程成员无法看到学生列表 #params[:course][:course_type]:暂时默认给1值。 #params[:term]:学期(秋季学期或春季学期) #params[:time]: 年份(例:2014) #params[:setup_time]:暂不传(貌似已经没用了) #params[:endup_time]: 暂不传(貌似已经没用了) #params[:class_period]:学时总数 #params[:course][:publish_resource]允许学生上传资源 def create_course(params, current_user) if current_user.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.send(:safe_attributes=, params[:course], current_user) #@course.safe_attributes(current_user,params[:course]) #@course.password = params[:course][:password] @course.name = params[:course][:name] @course.tea_id = current_user.id #@course.syllabus_id = params[:syllabus_id].to_i #@course.term = params[:term] #@course.time = params[:time] #@course.end_term = params[:end_term] #@course.end_time = params[:end_time] #@course.school_id = params[:occupation] @course.school_id = current_user.user_extensions.school_id #@course.setup_time = params[:setup_time] #@course.endup_time = params[:endup_time] @course.class_period = params[:period].to_i if params[:period] @course.credit = params[:credit] if params[:credit] @course.end_date = params[:end_date] if params[:end_date] params[:course][:is_public] ? @course.is_public = 1 : @course.is_public = 0 @course.open_student = 0 @course.publish_resource = 1 if CourseList.where(:name => params[:course][:course_list].strip).count > 0 @course.course_list_id = CourseList.where(:name => params[:course][:course_list].strip).first.id else course_list = CourseList.create(:name => params[:course][:course_list].strip, :user_id => User.current.id, :is_admin => 0) @course.course_list_id = course_list.id end else end @issue_custom_fields = IssueCustomField.sorted.all @trackers = Tracker.sorted.all if @course && @course.save #copy_avatar(@course, @course.syllabus) #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 => current_user, :roles => [r]) m.project_id = -1 course = CourseInfos.new(:user_id => current_user.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 {:course => @course, :img_url => url_to_avatar(@course), :current_user_is_member => current_user.member_of_course?(@course), :current_user_is_teacher => is_course_teacher(current_user, @course)} end # 复制课程 def copy_a_course origin_course ActiveRecord::Base.transaction do course = Course.new(:name => origin_course.name, :tea_id => User.current.id, :school_id => User.current.user_extensions.school_id, :class_period => origin_course.class_period, :credit => origin_course.credit, :end_date => Date.new() + 6 * 30, :is_public => 0, :course_list_id => origin_course.course_list_id, :is_copy => origin_course.id) course.extra = 'course' + DateTime.parse(Time.now.to_s).strftime('%Y-%m-%d_%H-%M-%S').to_s if course.save r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first m = Member.new(:user => current_user, :roles => [r]) m.project_id = -1 course.members << m course.course_infos << CourseInfos.new(:user_id => current_user.id, :course_id => course.id) # 作业复制 origin_course.homework_commons.where(:homework_type => [1, 3, 4]).each do |homework| new_homework = HomeworkCommon.new(:name => homework.name, :user_id => User.current.id, :description => homework.description, :homework_type => homework.homework_type, :late_penalty => 5, :course_id => course.id, :teacher_priority => 1, :anonymous_comment => 1, :quotes => 0, :is_open => 0, :homework_bank_id => homework.homework_bank_id, :score_open => 1, :anonymous_appeal => 0, :is_public => 0, :reference_answer => homework.reference_answer, :answer_public => 1, :allow_late => 1) new_homework.homework_detail_manual = HomeworkDetailManual.new(:te_proportion => 1.0, :ta_proportion => 0, :comment_status => 0, :evaluation_num => 0, :absence_penalty => 0) new_homework_detail_manual = new_homework.homework_detail_manual homework.attachments.each do |attachment| att = attachment.copy att.container_id = nil att.container_type = nil att.author_id = homework.user_id att.copy_from = attachment.id att.save new_homework.attachments << att attachment.update_attributes(:quotes => attachment.quotes.to_i + 1) end if new_homework.homework_type == 3 new_homework.homework_detail_group = HomeworkDetailGroup.new new_homework_detail_group = new_homework.homework_detail_group new_homework.homework_detail_group.min_num = homework.homework_detail_group.min_num new_homework.homework_detail_group.max_num = homework.homework_detail_group.max_num new_homework.homework_detail_group.base_on_project = homework.homework_detail_group.base_on_project end if new_homework.save if new_homework.homework_type == 4 HomeworkCommonsShixuns.create(:homework_common_id => new_homework.id, :shixun_id => homework.homework_commons_shixuns.shixun_id) create_shixun_homework_cha_setting new_homework, homework.shixuns.first end new_homework_detail_manual.save if new_homework_detail_manual new_homework_detail_group.save if new_homework_detail_group homework.update_column(:quotes, homework.quotes.to_i + 1) if homework.homework_bank homework.homework_bank.update_column(:quotes, homework.homework_bank.quotes.to_i + 1) QuestionBank.where(:container_id => homework.homework_bank_id, :container_type => ["Common", "Shixun"]).update_all(:quotes => homework.homework_bank.quotes) end end end # 试卷复制 origin_course.exercises.each do |exercise| new_exercise = Exercise.new(:exercise_name => exercise.exercise_name, :exercise_description => exercise.exercise_description, :user_id => User.current.id, :is_public => 0, :exercise_status => 1, :show_result => 1, :course_id => course.id, :time => -1, :exercise_bank_id => exercise.exercise_bank_id) exercise.exercise_questions.each do |q| option = { :question_title => q[:question_title], :question_type => q[:question_type] || 1, :question_number => q[:question_number], :question_score => q[:question_score] } exercise_question = new_exercise.exercise_questions.new option for i in 1..q.exercise_choices.count choice_option = { :choice_position => i, :choice_text => q.exercise_choices[i - 1][:choice_text] } exercise_question.exercise_choices.new choice_option end for i in 1..q.exercise_standard_answers.count standard_answer_option = { :exercise_choice_id => q.exercise_standard_answers[i - 1][:exercise_choice_id], :answer_text => q.exercise_standard_answers[i - 1][:answer_text] } exercise_question.exercise_standard_answers.new standard_answer_option end end if new_exercise.save if exercise.exercise_bank exercise.exercise_bank.update_column(:quotes, exercise.exercise_bank.quotes.to_i + 1) QuestionBank.where(:container_id => exercise.exercise_bank_id, :container_type => "Exercise").update_all(:quotes => exercise.exercise_bank.quotes) end end end # 问卷复制 origin_course.polls.each do |poll| new_poll = Poll.new(:polls_name => poll.polls_name, :polls_description => poll.polls_description, :user_id => User.current.id, :is_public => 0, :polls_status => 1, :show_result => 1, :course_id => course.id, :exercise_bank_id => poll.exercise_bank_id) poll.poll_questions.each do |q| option = { :question_title => q[:question_title], :question_type => q[:question_type] || 1, :is_necessary => q[:is_necessary], :question_number => q[:question_number], :max_choices => q[:max_choices], :min_choices => q[:min_choices] } poll_question = new_poll.poll_questions.new option for i in 1..q.poll_answers.count choice_option = { :answer_position => i, :answer_text => q.poll_answers[i - 1][:answer_text] } poll_question.poll_answers.new choice_option end end if new_poll.save if poll.exercise_bank poll.exercise_bank.update_column(:quotes, poll.exercise_bank.quotes.to_i + 1) QuestionBank.where(:container_id => poll.exercise_bank_id, :container_type => "Poll").update_all(:quotes => poll.exercise_bank.quotes) end end end # 资源复制 origin_course.attachments.each do |attachment| attach_copied_obj = attachment.copy attach_copied_obj.tag_list.add(attachment.tag_list) # tag关联 attach_copied_obj.container = course attach_copied_obj.created_on = Time.now attach_copied_obj.publish_time = nil attach_copied_obj.author_id = User.current.id attach_copied_obj.copy_from = attachment.copy_from.nil? ? attachment.id : attachment.copy_from attach_copied_obj.is_publish = 0 if attach_copied_obj.attachtype == nil attach_copied_obj.attachtype = 4 end attach_copied_obj.save update_quotes attach_copied_obj end end {:course => course} end end def update_quotes attachment if attachment.copy_from attachments = Attachment.find_by_sql("select * from attachments where copy_from = #{attachment.copy_from} or id = #{attachment.copy_from}") else attachments = Attachment.find_by_sql("select * from attachments where copy_from = #{attachment.id} or id = #{attachment.copy_from}") end attachment.quotes = get_qute_number attachment attachment.save attachments.each do |att| att.quotes = attachment.quotes att.save end end def get_qute_number attachment if attachment.copy_from result = Attachment.find_by_sql("select count(*) as number from attachments where copy_from = #{attachment.copy_from}") else result = Attachment.find_by_sql("select count(*) as number from attachments where copy_from = #{attachment.id}") end if result.nil? || result.count <= 0 return 0 else return result[0].number end end #验证编辑课程的权限 #当前 def edit_course_authorize(current_user, course) unless current_user.allowed_to?({:controller => 'courses', :action => 'update'}, course) raise '403' end end #编辑课程 需验证权限 # params[:course][:name]:课程名称 #params[:course][:password]:密码 #params[:course][:description]:描述 #params[:course][:is_public]:是否公开1公开,0私有 #params[:course][:open_student]:是否公开学生列表1公开,0不公开,不公开时非课程成员无法看到学生列表 #params[:course][:course_type]:暂时默认给1值。 #params[:term]:学期(秋季学期或春季学期) #params[:time]: 年份(例:2014) #params[:class_period]:学时总数 #params[:publish_resource] 允许学生上传资源 0 不允许 1 允许 def edit_course(params, course, current_user) course.send(:safe_attributes=, params[:course], current_user) #course.safe_attributes = params[:course] #course.password = params[:course][:password] course.name = params[:course][:name] #course.tea_id = current_user.id #course.syllabus_id = params[:syllabus_id].to_i #course.time = params[:time] #course.term = params[:term] #course.end_time = params[:end_time] #course.end_term = params[:end_term] course.class_period = params[:period].to_i if params[:period] course.credit = params[:credit] if params[:credit] course.end_date = params[:end_date] if params[:end_date] course.show_unit = params[:course][:show_unit] ? 1 : 0 if course.is_end && (course.end_date.nil? || course.end_date > Date.today) course.is_end = 0 Tiding.where(:container_id => course.id, :container_type => "ArchiveCourse").destroy_all elsif !course.is_end && course.end_date.present? && course.end_date < Date.today course.is_end = 1 end params[:course][:is_public] ? course.is_public = 1 : course.is_public = 0 if CourseList.where(:name => params[:course][:course_list].strip).count > 0 course.course_list_id = CourseList.where(:name => params[:course][:course_list].strip).first.id else course_list = CourseList.create(:name => params[:course][:course_list].strip, :user_id => User.current.id, :is_admin => 0) course.course_list_id = course_list.id end if course.save if params[:course][:is_public] course_status = CourseStatus.find_by_course_id(course.id) course_status.destroy if course_status course_status = CourseStatus.create(:course_id => course.id, :grade => 0) else course_status = CourseStatus.find_by_course_id(course.id) course_status.destroy if course_status end end # {:course => course,:img_url => url_to_avatar(course),:current_user_is_member => current_user.member_of_course?(course),:current_user_is_teacher => is_course_teacher(current_user,course)} {:course => course} end #退出课程 #object_id: 课程id #user:当前用户 #@state == 0 退出成功 #@state == 1 不在课程中 #@state == 2 您还未登录 #@state 其他 未知错误,请稍后再试 def exit_course params, user if user.nil? @state = 2 return @state end @member = Member.where('course_id = ? and user_id = ?', params[:object_id], user.id).first if @member.nil? @state = 1 return @state end student_role = @member.member_roles.where("role_id = 10").first teacher_role = @member.member_roles.where("role_id = 7 || role_id = 9").first ActiveRecord::Base.transaction do if @member.member_roles.count > 1 && student_role && teacher_role student_role.destroy teacher_role.update_attribute("is_current", 1) @member.update_attribute("course_group_id", 0) else @member.destroy end joined = StudentsForCourse.where('student_id = ? and course_id = ?', user.id, params[:object_id]) joined.destroy_all end @state = 0 @state end class JoinCourseError < Errors define_error [ 0, '加入成功', 1, '密码错误', 2, '班级已过期 请联系班级管理员重启班级。', 3, '您已经是该班级的成员了', 4, '您的邀请码不正确', 5, '您还未登录', 6, '您的申请已提交,请等待管理员审批', 7, '您已经发送过申请了,请耐心等待', 8, '您已经是该班级的教师了', 9, '您已经是该班级的教辅了', 10, '您已经是该班级的管理员了', 11, '该班级不存在或已被删除啦', 12, '您已经发送过申请了,请耐心等待', 13, '您的申请已提交,请等待管理员审批', '未知错误,请稍后再试' ] end #加入课程 #object_id:课程id #course_password :加入课程的密码 def join_course params, current_user course = Course.find_by_invite_code(params[:invite_code]) if params[:invite_code] @state = 10 if course if course_endTime_timeout? course @state = 2 elsif course[:is_delete] == 1 @state = 11 else if current_user.member_of_course?(course) #如果已经是成员 member = course.members.where("user_id=#{current_user.id} and course_id=#{course.id}")[0] roleName = member.roles[0].name if member if params[:invite_code].present? #如果加入角色为学生 并且当前是学生 if roleName == "Student" @state = 3 #如果加入的角色为老师,并且当前已经是老师 elsif roleName == "Teacher" @state = 8 #如果加入的角色教辅并且当前为教辅 elsif roleName == "TeachingAsistant" @state = 9 elsif roleName == "Manager" @state = 10 end else @state = 1 end else if params[:invite_code].present? members = [] members << Member.new(:role_ids => [10], :user_id => current_user.id) course.members << members StudentsForCourse.create(:student_id => current_user.id, :course_id => course.id) @state = 0 else @state = 1 end end end else @state = 4 end {:state => @state, :course => course} end #加入班级结果 # def send_wechat_join_class_notice user,course,role_id,result # count = ShieldWechatMessage.where("container_type='User' and container_id=#{user.id} and shield_type='Course' and shield_id=#{course.id}").count # if count == 0 # ws = WechatService.new # # role_name = role_id == 7 ? "助教" : role_id == 9 ? "教师" : "学生" # result_name = result == 0 ? "通过" : "被拒绝" # content = "您以"+role_name+"身份加入班级的申请已"+result_name+"。" # # if result == 0 # title = "恭喜您加入班级成功。" # remark = "点击查看班级详情。" # uid = 0 # type = "class" # else # title = "很遗憾您未能成功加入班级。" # remark = "点击查看申请详情。" # uid = user.id # type = "review_class_member" # end # # name = course.course_list.nil? ? course.name : course.course_list_name+" • "+course.name # ws = WechatService.new # ws.class_notice user.id, type, course.id, title,name, user.show_name, format_time(Time.now), content, remark,uid # # end # end #多个角色加入课程 def join_course_roles params, current_user course = Course.find_by_invite_code(params[:invite_code].strip) if params[:invite_code] unless course course_group = CourseGroup.find_by_invite_code(params[:invite_code].strip) if params[:invite_code] course = course_group.course if course_group end go_coursegroup_flag = 0 course_id = 0 @state = 10 if course if course_endTime_timeout? course @state = 2 elsif course[:is_delete] == 1 @state = 11 elsif course[:invite_code_halt] == 1 @state = 14 else role_ids = params[:role] if current_user.member_of_course?(course) #如果已经是成员 member = course.members.where("user_id=#{current_user.id}").first roleName = member.roles.map{|role| role.name}.join(",") if member if course_group.present? && roleName.include?("Student") member.update_attributes(:course_group_id => course_group.id) end #如果加入角色为学生 并且当前是学生 if role_ids.include?("10") && role_ids.length == 1 && roleName.include?("Student") @state = 16 #如果加入的角色为老师,并且当前已经是老师 elsif role_ids.include?("9") && role_ids.length == 1 && roleName.include?("Teacher") @state = 8 #如果加入的角色教辅并且当前为教辅 elsif role_ids.include?("7") && role_ids.length == 1 && roleName.include?("TeachingAsistant") @state = 9 elsif roleName == "Manager" @state = 10 #如果加入角色为教师或者教辅,并且当前是学生 elsif (role_ids.include?("7") || role_ids.include?("9")) && roleName == "Student" #如果已经发送过消息了,那么就要给个提示 if CourseMessage.where("course_message_type = 'JoinCourseRequest' and user_id = #{course.tea_id} and content = '#{role_ids.include?("9") ? 9 : 7}' and course_message_id = #{User.current.id} and course_id = #{course.id} and status = 0 ").count != 0 @state = 7 else # Mailer.run.join_course_request(course, User.current, params[:role]) course_message = CourseMessage.create(:user_id => course.tea_id, :course_id => course.id, :viewed => false,:content=> role_ids.include?("9") ? 9 : 7,:course_message_id=>User.current.id,:course_message_type=>'JoinCourseRequest',:status=>0) if CourseActivity.where(:course_act_type => 'JoinCourse', :user_id => User.current.id, :course_id => course.id, :course_act_id => course_message.id).count == 0 CourseActivity.create(:course_act_type => 'JoinCourse', :user_id => User.current.id, :course_id => course.id, :course_act_id => course_message.id) end @state = 6 end #如果加入角色是学生,但是是当前课程的教师或者教辅 elsif role_ids.include?("10") && !roleName.include?("Student") member.role_ids = member.role_ids + [10] member.course_group_id = course_group.try(:id).to_i if course_group.present? member.save StudentsForCourse.create(:student_id => current_user.id, :course_id => course.id) @state = 0 else @state = 3 end else if role_ids.include?("10") && !role_ids.include?("7") && !role_ids.include?("9") members = [] members << Member.new(:role_ids => [10], :user_id => current_user.id, :course_group_id => course_group.try(:id).to_i) course.members << members StudentsForCourse.create(:student_id => current_user.id, :course_id => course.id) @state = 0 go_coursegroup_flag = 1 if course.course_groups course_id = course.id # send_wechat_join_class_notice current_user,course,10,0 else is_stu = false if role_ids.include?("10") members = [] members << Member.new(:role_ids => [10], :user_id => current_user.id, :course_group_id => course_group.try(:id).to_i) course.members << members StudentsForCourse.create(:student_id => current_user.id, :course_id => course.id) is_stu = true go_coursegroup_flag = 1 if course.course_groups course_id = course.id # send_wechat_join_class_notice current_user,course,10,0 end #如果已经发送过消息了,那么就要给个提示 if CourseMessage.where("course_message_type = 'JoinCourseRequest' and user_id = #{course.tea_id} and content = '#{role_ids.include?("7") ? 7 : 9}' and course_message_id = #{current_user.id} and course_id = #{course.id} and status = 0").count != 0 course_message = CourseMessage.where("course_message_type = 'JoinCourseRequest' and user_id = #{course.tea_id} and content = '#{role_ids.include?("7") ? 7 : 9}' and course_message_id = #{current_user.id} and course_id = #{course.id} and status = 0").first if is_stu @state = 12 else @state = 7 end else # Mailer.run.join_course_request(course, current_user, params[:role]) course_message = CourseMessage.create(:user_id => course.tea_id, :course_id => course.id, :viewed => false, :content => role_ids.include?("7") ? 7 : 9, :course_message_id => current_user.id, :course_message_type => 'JoinCourseRequest', :status => 0) #----------------微信通知---------------------- # if role_ids.include?("7") || role_ids.include?("9") # tea_user = User.find(course.tea_id) # count = ShieldWechatMessage.where("container_type='User' and container_id=#{tea_user.id} and shield_type='Course' and shield_id=#{course.id}").count # if count == 0 # # rolename = role_ids.include?("7") ? "助教" : "教师" # content = current_user.show_name + "申请以"+rolename+"身份加入班级,等待您的审批。" # name = course.course_list.nil? ? course.name : course.course_list_name+" • "+course.name # ws = WechatService.new # ws.class_notice tea_user.id, "review_class_member", course.id, "班级成员审批通知。", name, tea_user.show_name, format_time(Time.now), content, "点击查看申请详情。",current_user.id # end # end #-------------------------------------------- if is_stu @state = 13 else @state = 6 end end # 创建动态 if role_ids.include?("7") || role_ids.include?("9") if CourseActivity.where(:course_act_type => 'JoinCourse', :user_id => User.current.id, :course_id => course.id, :course_act_id => course_message.id).count == 0 CourseActivity.create(:course_act_type => 'JoinCourse', :user_id => User.current.id, :course_id => course.id, :course_act_id => course_message.id) end end end end end else @state = 4 end {:state => @state, :course => course, :go_coursegroup_flag => go_coursegroup_flag, :course_id => course_id} end #作业列表 #已提交的作业数量获取 bid.homeworks.count #学生提问数量获取 bid.commit.nil? ? 0 : bid.commit def homework_list params, current_user course = Course.find(params[:id]) if course.is_public != 0 || current_user.member_of_course?(course) bids = course.homework_commons.where("publish_time <= ?", Time.now).page(params[:page] || 1).per(20).reorder('created_at DESC') bids = bids.like(params[:name]) if params[:name].present? homeworks = [] bids.each do |bid| homeworks << show_homework_info(course, bid, current_user, is_course_teacher(current_user, course)) end homeworks else raise '403' end end def course_dynamic(params, current_user) @user = User.find(params[:id]) if current_user.nil? && !current_user.admin? && !@user.active? raise '404' return end if current_user == @user || current_user.admin? membership = @user.coursememberships.all else membership = @user.coursememberships.all(:conditions => Course.visible_condition(current_user)) end if membership.nil? || membership.count == 0 raise l(:label_no_courses, :locale => get_user_language(current_user)) end membership.sort! {|older, newer| newer.created_on <=> older.created_on} result = [] membership.each do |mp| course = mp.course latest_course_dynamics = [] latest_news = course.news.order("created_on desc").first unless latest_news.nil? latest_course_dynamics << {:type => 1, :time => latest_news.created_on, :message => latest_news.author.realname << l(:label_recently_updated_notification, :locale => get_user_language(current_user)) << ":" << latest_news.title} end latest_message = course.journals_for_messages.order("created_on desc").first unless latest_message.nil? latest_course_dynamics << {:type => 2, :time => latest_message.created_on, :message => latest_message.user.realname << l(:label_recently_updated_message, :locale => get_user_language(current_user)) << ":" << latest_message.notes} end latest_attachment = course.attachments.order("created_on desc").first unless latest_attachment.nil? latest_course_dynamics << {:type => 3, :time => latest_attachment.created_on, :message => latest_attachment.author.realname << l(:label_recently_updated_courseware, :locale => get_user_language(current_user)) << ":" << latest_attachment.filename} end latest_bid = course.homework_commons.order('updated_at DESC').first unless latest_bid.nil? latest_course_dynamics << {:type => 4, :time => latest_bid.updated_at, :message => latest_bid.user.realname << l(:label_recently_updated_homework, :locale => get_user_language(current_user)) << ":" << latest_bid.name} end # Time 2015-04-07 14:58:30 # Author lizanle # Description 添加课程创建动态 if (User.find_by_id(CourseInfos.find_by_course_id(course.id).try(:user_id))) create_user_name = User.find_by_id(CourseInfos.find_by_course_id(course.id).user_id).realname latest_course_dynamics << {:type => 5, :time => course.created_at, :message => l(:label_recently, :locale => get_user_language(current_user)) << create_user_name << l(:label_creat, :locale => get_user_language(current_user))} end #每个作业中的最新留言 # messages = [] # course.homework_commons.each do |hc| # jour = hc.journals_for_messages.order("created_on desc").first # unless jour.nil? # messages << jour # end # end # unless messages.count == 0 # messages.sort!{|order,newer| newer.created_on <=> order.created_on} # end # latest_bid_message = messages.first # unless latest_bid_message.nil? # latest_course_dynamics << {:type => 4,:time => latest_bid_message.created_on,:message => l(:label_recently_updated_message,:locale => get_user_language(current_user))} # end #每个作业中学生最后提交的作业 homeworks = [] course.homework_commons.each do |bid| homework_attach = bid.student_works.order('updated_at DESC').first unless homework_attach.nil? homeworks << homework_attach end end unless homeworks.count == 0 homeworks.sort! {|order, newer| newer.updated_at <=> order.updated_at} end latest_homework_attach = homeworks.first unless latest_homework_attach.nil? latest_course_dynamics << {:type => 4, :time => latest_homework_attach.updated_at, :message => latest_homework_attach.user.realname << l(:label_recently_updated_homework, :locale => get_user_language(current_user)) << ":" << (latest_homework_attach.name.nil? ? latest_homework_attach.description : latest_homework_attach.name)} end latest_course_dynamics.sort! {|order, newer| newer[:time] <=> order[:time]} latest_course_dynamic = latest_course_dynamics.first unless latest_course_dynamic.nil? result << {:course_name => course.name, :course_id => course.id, :course_img_url => url_to_avatar(course), :course_time => course.time, :course_term => course.term, :type => latest_course_dynamic[:type], :update_time => latest_course_dynamic[:time], :message => latest_course_dynamic[:message], :count => nil} end end result.sort! {|order, newer| newer[:update_time] <=> order[:update_time]} result end # 课程课件 def course_attachments params result = [] course = Course.find(params[:course_id]) attachments = course.attachments.where("is_publish = 1").reorder("created_on desc") if !params[:name].nil? && params[:name] != "" attachments.each do |atta| result << atta if atta.filename.include?(params[:name]) end else result = attachments end result end # 课程学生列表 def course_members params @all_members = searchmember_by_name(student_homework_score(0, params[:course_id], 10, "desc"), params[:name]) end def show_member_score params @member_score = Member.find(params[:member_id]) if params[:member_id] atta = @member_score.student_homework_score[0] result = [] atta.each do |t| if !params[:homeworkName].nil? && params[:homeworkName] != "" result << {:name => t[:name], :score => t[:score]} if t[:name].include?(params[:homeworkName]) else result << {:name => t[:name], :score => t[:score]} end end result end # 设置人员为课程教辅 def set_as_assitant_teacher params members = [] #找到课程 course = Course.find(params[:course_id]) #新建课程人员 member = Member.new(:role_ids => [7], :user_id => params[:user_id], :course_id => params[:course_id]) joined = StudentsForCourse.where('student_id = ? and course_id = ?', member.user_id, course.id) joined.each do |join| join.destroy end member.course_group_id = 0 members << member course.members << members #将课程人员设置为教辅 end def del_assitant_teacher params member = Member.where("user_id = ? and course_id = ?", params[:user_id], params[:course_id]) member.each do |m| m.destroy end user_admin = CourseInfos.where("user_id = ? and course_id = ?", params[:user_id], params[:course_id]) if user_admin.size > 0 user_admin.each do |user| user.destroy end end joined = StudentsForCourse.where('student_id = ? and course_id = ?', params[:user_id], params[:course_id]) joined.each do |join| join.destroy end end def create_course_notice params, current_user n = News.new(:course_id => params[:course_id], :author_id => current_user.id, :title => params[:title], :description => params[:desc]) n.save {:id => n.id, :title => n.title, :author_name => n.author.name, :author_id => n.author.id, :description => n.description, :created_on => format_time(n.created_on), :comments_count => n.comments_count} end private def searchmember_by_name members, name #searchPeopleByRoles(project, StudentRoles) mems = [] if name != "" name = name.to_s.downcase members.each do |m| username = m.user[:lastname].to_s.downcase + m.user[:firstname].to_s.downcase if (m.user[:login].to_s.downcase.include?(name) || m.user.user_extensions[:student_id].to_s.downcase.include?(name) || username.include?(name)) mems << m end end else mems = members end mems end def show_homework_info course, bid, current_user, is_course_teacher author_real_name = bid.user.lastname + bid.user.firstname many_times = course.homework_commons.index(bid) + 1 name = bid.name homework_count = bid.student_works.count #已提交的作业数量 #student_questions_count = bid.journals_for_messages.where('m_parent_id IS NULL').count description = bid.description #if is_course_teacher(User.current, course) && @bid.open_anonymous_evaluation == 1 && @bid.homeworks.count >= 2 #state = bid.homework_detail_manual.comment_status state = get_homework_state(bid) # studentlist = [] # bid.student_works.order("created_at desc").page(1).per(6).each do |work| # studentlist << work.user # end # unless is_course_teacher # homework_for_anonymous_comments = get_student_batch_homework_list bid,current_user # end #end open_anonymous_evaluation = bid.homework_detail_manual.comment_status {:course_name => course.name, :course_id => course.id, :id => bid.id, :author => bid.user, :author_real_name => author_real_name, :homework_times => many_times, :homework_name => name, :homework_count => homework_count, :student_questions_count => 0, :description => description, :homework_state => state, :open_anonymous_evaluation => open_anonymous_evaluation, #:homework_for_anonymous_comments => homework_for_anonymous_comments, :created_on => bid.created_at, :deadline => bid.end_time, :homework_notsubmit_num => bid.student_works.count - bid.student_works.all_commit.count, :homework_submit_num => bid.student_works.all_commit.count, :homework_status_student => get_homework_status(bid), :homework_status_teacher => homework_status_desc(bid), :student_evaluation_part => get_evaluation_part(bid, 3), :ta_evaluation_part => get_evaluation_part(bid, 2), :homework_anony_type => bid.homework_type == 1 && !bid.homework_detail_manual.nil?, :current_user_is_teacher => is_course_teacher, ##学生提交作业的状态 0 未提交 1 已提交 :status => bid.student_works.where(user_id: current_user).first.try(:work_status).to_i, :homework_type => bid.homework_type } end #显示作业列表的同时显示分配给当前学生匿评的作业 def show_homework_info_with_batch course, bid author = bid.author.lastname + bid.author.firstname many_times = course.homeworks.index(bid) + 1 name = bid.name homework_count = bid.homeworks.count #已提交的作业数量 student_questions_count = bid.journals_for_messages.where('m_parent_id IS NULL').count description = bid.description #if is_course_teacher(User.current, course) && @bid.open_anonymous_evaluation == 1 && @bid.homeworks.count >= 2 state = bid.comment_status #end open_anonymous_evaluation = bid.open_anonymous_evaluation {:course_name => course.name, :id => bid.id, :course_teacher => author, :homework_times => many_times, :homework_name => name, :homework_count => homework_count, :student_questions_count => student_questions_count, :description => description, :homework_state => state, :open_anonymous_evaluation => open_anonymous_evaluation} end def student_homework_score(groupid, course_id, nums, score_sort_by, sort_type = 'score') sql_select = "" if groupid == 0 sql_select = "SELECT members.*,( SELECT SUM(student_works.work_score) FROM student_works,homework_commons WHERE student_works.homework_common_id = homework_commons.id AND homework_commons.course_id = course_id AND student_works.user_id = members.user_id ) AS score,(SELECT (message_num*2 + message_reply_num*1 + news_reply_num*1 + news_num*1 + resource_num*5 + journal_num*1 + homework_journal_num*1 ) FROM `course_contributor_scores` AS ccs WHERE ccs.course_id = #{course_id} AND ccs.user_id = members.user_id ) AS act_score FROM members JOIN students_for_courses ON students_for_courses.student_id = members.user_id AND students_for_courses.course_id = members.course_id WHERE members.course_id = #{course_id} ORDER BY #{sort_type} #{score_sort_by}" else sql_select = "SELECT members.*,( SELECT SUM(student_works.work_score) FROM student_works,homework_commons WHERE student_works.homework_common_id = homework_commons.id AND homework_commons.course_id = #{course_id} AND student_works.user_id = members.user_id ) AS score,(SELECT (message_num*2 + message_reply_num*1 + news_reply_num*1 + news_num*1 + resource_num*5 + journal_num*1 + homework_journal_num*1 ) FROM `course_contributor_scores` AS ccs WHERE ccs.course_id = #{course_id} AND ccs.user_id = members.user_id ) AS act_score FROM members JOIN students_for_courses ON students_for_courses.student_id = members.user_id AND students_for_courses.course_id = members.course_id WHERE members.course_id = #{course_id} AND members.course_group_id = #{groupid} ORDER BY #{sort_type} #{score_sort_by}" end sql = ActiveRecord::Base.connection() homework_scores = Member.find_by_sql(sql_select) sql.close() homework_scores end #app新版api # # #课程动态 public def all_course_dynamics params, current_user #获取当前用户的所有课程 @user = User.find(params[:id]) if current_user.nil? && !current_user.admin? && !@user.active? raise '404' return end if current_user == @user || current_user.admin? membership = @user.coursememberships else membership = @user.coursememberships.all(:conditions => Course.visible_condition(current_user)) end if membership.nil? || membership.count == 0 raise l(:label_no_courses, :locale => get_user_language(current_user)) end #membership.sort! { |older, newer| newer.created_on <=> older.created_on } #定义一个数组集合,存放hash数组,该hash数组包括课程的信息,并包含课程的最新发布的资源,最新的讨论区留言,最新的作业,最新的通知 result = [] #对用户所有的课程进行循环,找到每个课程最新发布的资源,最新的讨论区留言,最新的作业,最新的通知,并存进数组 membership.each do |mp| course = mp.course latest_course_dynamics = [] notices_count = 0 topic_count = 0 topics = nil homeworkss = nil notices = nil # 课程通知 latest_news = course.news.page(1).per(2).order("created_on desc") unless latest_news.first.nil? notices_count = course.news.count notices = latest_news.all latest_course_dynamics << {:time => latest_news.first.created_on} end # 课程讨论区 latest_message = course.boards.first.topics.page(1).per(2) unless latest_message.first.nil? topic_count = course.boards.nil? ? 0 : course.boards.first.topics.count topics = latest_message.all latest_course_dynamics << {:time => latest_message.first.created_on} end # 课程资源 # latest_attachment = course.attachments.order("created_on desc").page(1).per(2) # unless latest_attachment.first.nil? # latest_course_dynamics << {:type => 3, :time => latest_attachment.first.created_on,:count =>course.attachments.count , :documents=>latest_attachment} # dynamics_count += 1 # end #课程作业 已经交的学生列表(暂定显示6人),未交的学生列表,作业的状态 homeworks = course.homework_commons.page(1).per(2).order('created_at desc') unless homeworks.first.nil? homeworkss = homeworks latest_course_dynamics << {:time => homeworks.first.updated_at} end latest_course_dynamics.sort! {|order, newer| newer[:time] <=> order[:time]} # 课程学霸 学生总分数排名靠前的5个人 homework_count = course.homework_commons.count sql = "select users.*,ROUND(sum(student_works.final_score),2) score from student_works left outer join users on student_works.user_id = users.id" << " where homework_common_id in ( select id from homework_commons where homework_commons.course_id = #{course.id}) GROUP BY student_works.user_id ORDER BY score desc limit 0,4" better_students = User.find_by_sql(sql) # 找出在课程讨论区发帖回帖数最多的 active_students = [] sql1 = " select users.*,count(author_id)*2 active_count from messages " << " LEFT JOIN users on messages.author_id = users.id " << " where messages.board_id in (select id from boards where boards.course_id = #{course.id} ) " << " GROUP BY messages.author_id ORDER BY count(author_id) desc " << " limit 0,4" active_students = User.find_by_sql(sql1) if homework_count != 0 && !better_students.empty? latest_course_dynamics << {:time => "1970-01-01 0:0:0 +0800"} end unless active_students.empty? latest_course_dynamics << {:time => "1970-01-01 0:0:0 +0800"} end latest_course_dynamic = latest_course_dynamics.first unless latest_course_dynamic.nil? result << {:course_name => course.name, :current_user_is_member => current_user.member_of_course?(course), :current_user_is_teacher => is_course_teacher(current_user, course), :course_id => course.id, :course_img_url => url_to_avatar(course), :course_time => course.time, :course_term => course.term, :news_count => notices_count, :homework_count => homework_count, :topic_count => topic_count, :news => notices, :homeworks => homeworkss, :topics => topics, :better_students => better_students, :active_students => active_students, :message => "", :course_student_num => course ? course.members.count : 0, #:time_from_now=> distance_of_time_in_words(Time.now, latest_course_dynamic[:time].to_time) << "前", :time_from_now => time_from_now(latest_course_dynamic[:time].to_time), #.strftime('%Y-%m-%d %H:%M:%S').to_s, :time => latest_course_dynamic[:time].to_time} end end #返回数组集合 result.sort! {|order, newer| newer[:time] <=> order[:time]} result end # 获取课程历次作业的学生总成绩 def students_score_list params, current_user page = (params[:page] || 1) - 1 user_list = [] max_size = 0 if params[:type] == 1 sql = "select users.*,ROUND(sum(student_works.final_score),2) score from student_works left outer join users on student_works.user_id = users.id" << " where homework_common_id in ( select id from homework_commons where homework_commons.course_id = #{params[:course_id]}) GROUP BY student_works.user_id ORDER BY score desc limit #{page * 10},10" sql_count = " select count(distinct(student_works.user_id) ) " << " from student_works left outer join users on student_works.user_id = users.id " << " where homework_common_id in " << " ( select id from homework_commons where homework_commons.course_id = #{params[:course_id]}) " max_size = ActiveRecord::Base.connection().select_value(sql_count) user_list = User.find_by_sql(sql) else sql1 = " select users.*,count(author_id)*2 active_count from messages " << " LEFT JOIN users on messages.author_id = users.id " << " where messages.board_id in (select id from boards where boards.course_id = #{params[:course_id]} ) " << " GROUP BY messages.author_id ORDER BY count(author_id) desc " << " limit #{page * 10},10" sql1_count = " select count(DISTINCT(messages.author_id))" << " from messages LEFT JOIN users on messages.author_id = users.id " << " where messages.board_id in (select id from boards where boards.course_id = #{params[:course_id]} )" max_size = ActiveRecord::Base.connection().select_value(sql1_count) user_list = User.find_by_sql(sql1) end {:user_list => user_list, :max_size => max_size} end # 获取某次作业的所有作业列表 def student_work_list params, current_user is_teacher = current_user.allowed_to?(:as_teacher, Course.find(params[:course_id])) homework = HomeworkCommon.find(params[:homework_id]) student_works = [] #老师 || 非匿评作业 || 匿评结束 显示所有的作品 show_all = is_teacher || homework.homework_type != 1 || homework.homework_detail_manual.comment_status == 3 if show_all if homework.homework_type == 1 || is_teacher || current_user.admin? student_works = homework.student_works.page(params[:page] || 1).per(10).order("final_score desc ") else my_work = homework.student_works.where(:user_id => current_user.id) if my_work.empty? student_works = [] else student_works = homework.student_works.page(params[:page] || 1).per(10).order("final_score desc") end end else #学生 if homework.homework_detail_manual.comment_status == 1 #未开启匿评,只显示我的作品 student_works = homework.student_works.where(:user_id => current_user.id).page(params[:page] || 1).per(10) elsif homework.homework_detail_manual.comment_status == 2 #匿评列表,显示匿评作品和我的作品 #is_evaluation = true my_work = homework.student_works.where(:user_id => current_user.id).page(params[:page] || 1).per(10) student_works = my_work + current_user.student_works_evaluation_distributions.map(&:student_work).select {|work| work.homework_common_id == homework.id} end end student_works end # 获取课程的讨论区信息 def board_message_list params, current_user # 课程讨论区 course = Course.find(params[:course_id]) latest_message = course.boards.first.topics.page(params[:page] || 1).per(10) end #获取回复列表 def board_message_reply_list params, current_user board = Board.find(params[:board_id]) reply_list = board.topics.where("id = #{params[:msg_id]}").first.children.order("created_on desc").page(params[:page] || 1).per(10) end def board_all_messages params, user topics = Board.find(params[:board_id]).topics.page(params[:page]).page(params[:page] || 1).per(10) return nil unless topics.present? #取子评论 comments = [] topics.each do |topic| comments << {topic: topic, comments: Message.where(root_id: topic.id)} end comments end def topic_all_messages params, user topic = Message.find(params[:topic_id]) {topic: topic, comments: Message.where(root_id: topic.id)} end #回复讨论区 def board_message_reply params, current_user reply = Message.new reply.author_id = current_user.id reply.board_id = params[:board_id] reply.subject = params[:title] reply.content = params[:content] #本回复内容 reply.parent_id = params[:reply_id] # 父id reply.reply_id = params[:reply_id] #根id reply.root_id = params[:root_id] unless reply.subject.present? reply.subject = params[:content] end if reply.parent_id.nil? || reply.parent_id == 0 reply.parent_id = params[:root_id] end if reply.board_id == 0 #回帖时可以不传board_id parent_msg = Message.find(reply.parent_id) reply.board_id = parent_msg.board_id end reply.save! reply end # # 开启匿评 # #statue 1:启动成功,2:启动失败,作业总数大于等于2份时才能启动匿评,3:已开启匿评,请务重复开启,4:没有开启匿评的权限 # def start_anonymous_comment params,current_user # homework = HomeworkCommon.find(params[:homework_id]) # return {:status=>4} unless current_user.admin? || current_user.allowed_to?(:as_teacher,Course.find(params[:course_id])) # return {:status=>5} if Time.parse(homework.end_time.to_s).strftime("%Y-%m-%d") >= Time.now.strftime("%Y-%m-%d") # homework_detail_manual = homework.homework_detail_manual # if homework_detail_manual.comment_status == 1 # student_works = homework.student_works # if student_works && student_works.size >=2 # student_works.each_with_index do |work, index| # user = work.user # n = homework_detail_manual.evaluation_num # n = n < student_works.size ? n : student_works.size - 1 # assigned_homeworks = get_assigned_homeworks(student_works, n, index) # assigned_homeworks.each do |h| # student_works_evaluation_distributions = StudentWorksEvaluationDistribution.new(user_id: user.id, student_work_id: h.id) # student_works_evaluation_distributions.save # end # end # homework_detail_manual.update_column('comment_status', 2) # statue = 1 # else # statue = 2 # end # else # statue = 3 # end # {:status => statue} # end # # def get_assigned_homeworks(student_works, n, index) # student_works += student_works # student_works[index + 1 .. index + n] # end #修改班级成员角色 def modify_user_course_role params, roles_id status = -1 c = Course.find("#{params[:id]}") # member = c.member_principals.includes(:roles, :principal).where("user_id=?",params[:user_id]).first member = c.members.where("user_id=?", params[:user_id]).first if member member.role_ids = roles_id role = Role.find(roles_id[0]) # 这里的判断只能通过角色名,可以弄成常量 if roles_id && roles_id.include?(10) StudentsForCourse.create(:student_id => params[:user_id], :course_id => params[:id]) else joined = StudentsForCourse.where('student_id = ? and course_id = ?', params[:user_id], params[:id]) joined.each do |join| join.destroy end member.course_group_id = 0 end if role.allowed_to?(:is_manager) courseInfo = CourseInfos.new(:user_id => params[:user_id], :course_id => params[:id]) courseInfo.save else user_admin = CourseInfos.where("user_id = ? and course_id = ?", params[:user_id], params[:id]) if user_admin.size > 0 user_admin.each do |user| user.destroy end end end Role.givable.all[3..5] if member.save status = 0 end end status end #删除班级成员 def delete_course_member course, user_id, current_user if current_user.nil? state = -1 return state end member = course.members.where("user_id=?", user_id).first if member != nil member.destroy user_admin = CourseInfos.where("user_id = ? and course_id = ?", member.user_id, course.id) if user_admin.size > 0 user_admin.each do |user| user.destroy end end joined = StudentsForCourse.where('student_id = ? and course_id = ?', member.user_id, course.id) joined.each do |join| join.destroy end roles = Role.givable.all[3..5] #移出课程发送消息 CourseMessage.create(:user_id => member.user_id, :course_id => course.id, :course_message_type => "RemoveFromCourse", :viewed => false, :course_message_id => current_user.id) return 0 else return 1 end end end