diff --git a/app/services/courses_service.rb b/app/services/courses_service.rb index 4639f1c2b..1e0bfbec2 100644 --- a/app/services/courses_service.rb +++ b/app/services/courses_service.rb @@ -15,11 +15,11 @@ class CoursesService 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") + 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) + 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 @@ -308,15 +308,31 @@ class CoursesService if course_endTime_timeout? course @state = 2 else - if current_user.member_of_course?(course) - @state = 3 + if current_user.member_of_course?(course) #如果已经是成员 + if params[:course_password] == course.password + #如果加入角色为学生 + if params[:role] == "10" + @state = 3 + else + #如果加入角色为教师或者教辅 + CourseMessage.create(:user_id => course.tea_id, :course_id => course.id, :viewed => false,:content=> params[:role],:course_message_id=>User.current.id,:course_message_type=>'JoinCourseRequest',:status=>0) + @state = 6 + end + else + @state = 1 + end else if params[:course_password] == course.password - members = [] - members << Member.new(:role_ids => [10], :user_id => current_user.id) - course.members << members - StudentsForCourse.create(:student_id => current_user.id, :course_id => params[:object_id]) - @state = 0 + if params[:role] == "10" + members = [] + members << Member.new(:role_ids => [10], :user_id => current_user.id) + course.members << members + StudentsForCourse.create(:student_id => current_user.id, :course_id => params[:object_id]) + @state = 0 + else + CourseMessage.create(:user_id => course.tea_id, :course_id => course.id, :viewed => false,:content=> params[:role],:course_message_id=>User.current.id,:course_message_type=>'JoinCourseRequest',:status=>0) + @state = 6 + end else @state = 1 end @@ -336,11 +352,11 @@ class CoursesService if course.is_public != 0 || current_user.member_of_course?(course) bids = course.homework_commons.page(params[:page] || 1).per(20).order('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 + 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 @@ -411,10 +427,10 @@ class CoursesService #每个作业中学生最后提交的作业 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 + 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} @@ -433,21 +449,21 @@ class CoursesService result end - # 课程课件 - def course_attachments params - result = [] - course = Course.find(params[:course_id]) - attachments = course.attachments.order("created_on ") - if !params[:name].nil? && params[:name] != "" - attachments.each do |atta| - result << atta if atta.filename.include?(params[:name]) + # 课程课件 + def course_attachments params + result = [] + course = Course.find(params[:course_id]) + attachments = course.attachments.order("created_on ") + 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 + end + else + result = attachments + end + result + end # 课程学生列表 def course_members params @@ -488,20 +504,20 @@ class CoursesService 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.delete - end + 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.delete + end end def create_course_notice params ,current_user @@ -570,11 +586,11 @@ class CoursesService :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.course.members.count - bid.student_works.count, - :homework_submit_num => bid.student_works.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?} + :homework_notsubmit_num => bid.course.members.count - bid.student_works.count, + :homework_submit_num => bid.student_works.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?} end @@ -586,9 +602,9 @@ class CoursesService 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 + #if is_course_teacher(User.current, course) && @bid.open_anonymous_evaluation == 1 && @bid.homeworks.count >= 2 state = bid.comment_status - #end + #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} @@ -685,12 +701,12 @@ class CoursesService 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_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? @@ -727,29 +743,29 @@ class CoursesService 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 + 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 #返回数组集合 @@ -768,9 +784,9 @@ class CoursesService 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]}) " + " 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