From f7d4d0a38c310b30d7504b2211d7cc37e6bda7c5 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Sat, 20 Jul 2019 16:09:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=80=81=E7=89=88=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/colleges_controller.rb | 4 +- .../competition_teams_controller.rb | 5 +- app/controllers/courses_controller.rb | 4 +- app/controllers/managements_controller.rb | 16 ++--- app/controllers/users_controller.rb | 6 +- app/models/course.rb | 10 ++- app/models/course_group.rb | 1 + app/models/course_member.rb | 7 ++ app/models/message.rb | 5 +- app/models/message_detail.rb | 4 ++ app/models/user.rb | 4 +- app/services/users_service.rb | 4 +- .../_classroom_classment_list.html.erb | 2 +- .../managements/_course_message_list.html.erb | 2 +- .../managements/_shixun_task_list.html.erb | 2 +- app/views/users/show.html.erb | 68 ++++++++++--------- spec/factories/course_members.rb | 5 ++ spec/factories/message_details.rb | 5 ++ spec/models/course_member_spec.rb | 5 ++ spec/models/message_detail_spec.rb | 5 ++ 20 files changed, 103 insertions(+), 61 deletions(-) create mode 100644 app/models/course_member.rb create mode 100644 app/models/message_detail.rb create mode 100644 spec/factories/course_members.rb create mode 100644 spec/factories/message_details.rb create mode 100644 spec/models/course_member_spec.rb create mode 100644 spec/models/message_detail_spec.rb diff --git a/app/controllers/colleges_controller.rb b/app/controllers/colleges_controller.rb index 4bc6cd87..15f9ef8f 100644 --- a/app/controllers/colleges_controller.rb +++ b/app/controllers/colleges_controller.rb @@ -67,12 +67,12 @@ class CollegesController < ApplicationController user_extensions.`school_id`=#{@school.id}) and work_status between 1 and 2 and myshixun_id !=0").first.try(:sw_count) @teachers = User.find_by_sql("SELECT users.id, users.login, users.lastname, users.firstname, users.nickname, IFNULL((SELECT count(shixuns.id) FROM shixuns where shixuns.user_id =users.id group by shixuns.user_id), 0) AS publish_shixun_count, - (SELECT count(c.id) FROM courses c, members m, member_roles mr WHERE c.id != 1309 and m.course_id = c.id AND m.id=mr.member_id AND mr.role_id in (3,7,9) and c.school_id = #{@school.id} AND m.user_id=users.id AND c.is_delete = 0) as course_count + (SELECT count(c.id) FROM courses c, course_members m WHERE c.id != 1309 and m.course_id = c.id AND m.role in (1,2,3) and c.school_id = #{@school.id} AND m.user_id=users.id AND c.is_delete = 0) as course_count FROM `users`, user_extensions ue where users.id=ue.user_id and ue.identity=0 and ue.school_id=#{@school.id} ORDER BY publish_shixun_count desc, course_count desc, id desc LIMIT 10") # ).order("publish_shixun_count desc, experience desc").limit(10) @teachers = @teachers.map do |teacher| - course_ids = Course.find_by_sql("SELECT c.id FROM courses c, members m, member_roles mr WHERE c.id != 1309 and m.course_id = c.id AND m.id=mr.member_id AND mr.role_id in (3,7,9) AND m.user_id=#{teacher.id} AND c.is_delete = 0 and c.school_id = #{@school.id}") + course_ids = Course.find_by_sql("SELECT c.id FROM courses c, course_members m WHERE c.id != 1309 and m.course_id = c.id AND m.role in (1,2,3) AND m.user_id=#{teacher.id} AND c.is_delete = 0 and c.school_id = #{@school.id}") course_count = course_ids.size homeworks = HomeworkCommon.where(:homework_type => 4, :course_id => course_ids.map(&:id)) un_shixun_work_count = homeworks.where("publish_time > '#{Time.now}' or publish_time is null").count diff --git a/app/controllers/competition_teams_controller.rb b/app/controllers/competition_teams_controller.rb index c285fba7..2bbbd6ef 100644 --- a/app/controllers/competition_teams_controller.rb +++ b/app/controllers/competition_teams_controller.rb @@ -32,9 +32,8 @@ class CompetitionTeamsController < ApplicationController # todo:使用新版course_members course_ids = Course.where('courses.created_at > ?', Time.parse('2018-06-01')) - .joins('join members on members.course_id = courses.id') - .joins('join member_roles on member_roles.member_id = members.id and member_roles.role_id in (3,7,9)') - .where(members: { user_id: @team_user_ids }).pluck(:id) + .joins('join course_members on course_members.course_id = courses.id and course_members.role in (1,2,3)') + .where(course_members: { user_id: @team_user_ids }).pluck(:id) courses = Course.where(id: course_ids).joins(:shixun_homework_commons).where('homework_commons.publish_time < now()') @courses = courses.select('courses.id, courses.name, courses.members_count, count(*) shixun_homework_count') .group('courses.id').order('shixun_homework_count desc').having('shixun_homework_count > 0') diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index b830776b..19bf3039 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -87,7 +87,7 @@ class CoursesController < ApplicationController course_ids = course_ids.present? ? course_ids.join(",") : -1 @courses = Course.where("courses.is_delete = 0 and courses.is_hidden = 0 #{course_ids == -1 ? "" : "and courses.id in (#{course_ids})"}").select("courses.id, courses.tea_id, courses.name, courses.is_public, courses.members_count, courses.homework_commons_count,(SELECT MAX(created_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS a").order("courses.id = 1309 desc, a desc") elsif @order == "mine" - my_course_ids = Course.find_by_sql("SELECT c.id FROM courses c, members m, member_roles mr WHERE m.course_id = c.id AND m.id=mr.member_id AND mr.role_id in (3,7,9,10) AND m.user_id=#{@user.id} AND c.is_delete = 0") + my_course_ids = Course.find_by_sql("SELECT c.id FROM courses c, course_members m WHERE m.course_id = c.id AND m.role in (1,2,3,4) AND m.user_id=#{@user.id} AND c.is_delete = 0") course_ids = course_ids.present? ? my_course_ids.map(&:id) & course_ids : my_course_ids.map(&:id) course_ids = course_ids.size > 0 ? course_ids.join(",") : "-1" @courses = Course.find_by_sql("select c.id, c.tea_id, c.name, c.is_public, c.members_count, c.homework_commons_count, u.login, u.nickname, u.lastname, u.firstname, u.show_realname, ue.school_id from courses c, users u, user_extensions ue where c.tea_id = u.id and u.id = ue.user_id and c.is_delete = 0 and c.is_hidden = 0 and c.id in (#{course_ids}) order by c.id = 1309 desc, #{order_str} desc") @@ -894,7 +894,7 @@ class CoursesController < ApplicationController # @teacher_count = TeacherAndAssistantCount @course if @course.try(:id) != 1309 || User.current.admin? || User.current.try(:id) == 15582 - @teacher_count = @course.members.includes(:roles, :user).where("roles.id" => [3,7,9]).count + @teacher_count = @course.course_members.includes(:roles, :user).where("roles.id" => [3,7,9]).count else @teacher_count = @course.members.includes(:roles, :user).where("roles.id" => [3,7]).count end diff --git a/app/controllers/managements_controller.rb b/app/controllers/managements_controller.rb index 156ae951..730f991c 100644 --- a/app/controllers/managements_controller.rb +++ b/app/controllers/managements_controller.rb @@ -2944,20 +2944,20 @@ end course_group = CourseGroup.where(:course_id => course.id, :name => group_name).first || CourseGroup.create(:name => group_name, :course_id => course.id) if group_name - member = course.members.where(:user_id => user.id).first + member = course.course_members.where(:user_id => user.id, :role => 4).first # 如果已是课堂成员且是学生身份and不在指定的分班则移动到该分班 - if member.present? && member.member_roles.pluck(:role_id).include?(10) && member.course_group_id != course_group.try(:id).to_i + if member.present? && member.course_group_id != course_group.try(:id).to_i member.update_column("course_group_id", course_group.try(:id).to_i) member_count += 1 elsif !member.present? - member = Member.new(:role_ids => [member_role], :user_id => user.id) - if member_role == 10 - StudentsForCourse.create(:student_id => user.id, :course_id => course.id) - end + member = CourseMember.new(:role => 4, :user_id => user.id) + # if member_role == 10 + # StudentsForCourse.create(:student_id => user.id, :course_id => course.id) + # end Tiding.create(:user_id => user.id, :trigger_user_id => course.tea_id, :container_id => course.id, :container_type => 'TeacherJoinCourse', :belong_container_id => course.id, :belong_container_type => "Course", :tiding_type => "System", :extra => member_role) member.course_group_id = course_group.try(:id).to_i - course.members << member + course.course_members << member member_count += 1 end @@ -4262,7 +4262,7 @@ end teacher_name = course.teacher ? course.teacher.show_real_name : "" sheet1[count_row,0] = course.id sheet1[count_row,1] = course.name - sheet1[count_row,2] = course.members.count + sheet1[count_row,2] = course.course_members_count sheet1[count_row,3] = course.attachments.count sheet1[count_row,4] = course.homework_commons.where(:homework_type => 1).count sheet1[count_row,5] = course.homework_commons.where(:homework_type => 4).count diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 744b1d1f..3fd3759a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -2438,14 +2438,14 @@ class UsersController < ApplicationController case @type when "a_course" student_course_ids = Course.find_by_sql("SELECT c.id FROM courses c, students_for_courses sfc WHERE sfc.course_id = c.id AND sfc.student_id=#{@user.id} AND c.is_delete = 0 AND c.is_end in #{status}") - manage_course_ids = Course.find_by_sql("SELECT c.id FROM courses c, members m, member_roles mr WHERE m.course_id = c.id AND m.id=mr.member_id AND mr.role_id in (3,7,9) AND m.user_id=#{@user.id} AND c.is_delete = 0 AND c.is_end in #{status}") + manage_course_ids = Course.find_by_sql("SELECT c.id FROM courses c, course_members m WHERE m.course_id = c.id AND m.role in (1,2,3) AND m.user_id=#{@user.id} AND c.is_delete = 0 AND c.is_end in #{status}") course_ids = student_course_ids.map(&:id) + manage_course_ids.map(&:id) @objects = Course.where(:id => course_ids.uniq).includes(:attachments, :teacher => [:user_extensions => [:school]]).order("updated_at #{order}") when "l_course" - student_course_ids = Course.find_by_sql("SELECT c.id FROM courses c, students_for_courses sfc WHERE sfc.course_id = c.id AND sfc.student_id=#{@user.id} AND c.is_delete = 0 AND c.is_end in #{status}") + student_course_ids = Course.find_by_sql("SELECT c.id FROM courses c, course_members m WHERE m.course_id = c.id and m.role = 4 AND m.user_id=#{@user.id} AND c.is_delete = 0 AND c.is_end in #{status}") @objects = Course.where(:id => student_course_ids.map(&:id)).includes(:attachments, :teacher => [:user_extensions => [:school]]).order("updated_at #{order}") when "p_course" - course_ids = Course.find_by_sql("SELECT c.id FROM courses c, members m, member_roles mr WHERE m.course_id = c.id AND m.id=mr.member_id AND mr.role_id in (3,7,9) AND m.user_id=#{@user.id} AND c.is_delete = 0 AND c.is_end in #{status}") + course_ids = Course.find_by_sql("SELECT c.id FROM courses c, course_members m WHERE m.course_id = c.id AND m.role in (1,2,3) AND m.user_id=#{@user.id} AND c.is_delete = 0 AND c.is_end in #{status}") @objects = Course.where(:id => course_ids.map(&:id)).includes(:attachments, :teacher => [:user_extensions => [:school]]).order("id = 1309 desc, updated_at #{order}") when "a_shixun" shixun_ids = @user.shixun_members.map(&:shixun_id) + @user.myshixuns.map(&:shixun_id) diff --git a/app/models/course.rb b/app/models/course.rb index c6c6d3df..5068e38e 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -33,6 +33,8 @@ class Course < ActiveRecord::Base belongs_to :syllabus belongs_to :course_list # has_many :bid + has_many :course_members, dependent: :destroy + has_many :members, :include => [:principal, :roles], :conditions => "#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{Principal::STATUS_ACTIVE}" has_many :memberships, :class_name => 'Member' has_many :member_principals, :class_name => 'Member', @@ -290,12 +292,16 @@ class Course < ActiveRecord::Base # 课堂的老师不包括助教 def course_teachers - self.members.select{|m| m.roles.to_s.include?("Manager") || m.roles.to_s.include?("Teacher")} + self.course_members.where(:role => [1, 2]) end # 课堂的老师包括助教 def teachers - self.members.includes(:roles).where("roles.id" => [3,7,9]) + self.course_members.where(:role => [1, 2, 3]) + end + + def students + self.course_members.where(:role => [4]) end def self.visible_condition(user, options={}) diff --git a/app/models/course_group.rb b/app/models/course_group.rb index 9c80c85a..c5b66d87 100644 --- a/app/models/course_group.rb +++ b/app/models/course_group.rb @@ -1,6 +1,7 @@ class CourseGroup < ActiveRecord::Base # attr_accessible :title, :body belongs_to :course + has_many :course_members has_many :members has_many :memberships, :class_name => 'Member' has_many :member_principals, :class_name => 'Member', diff --git a/app/models/course_member.rb b/app/models/course_member.rb new file mode 100644 index 00000000..8de272a4 --- /dev/null +++ b/app/models/course_member.rb @@ -0,0 +1,7 @@ +class CourseMember < ActiveRecord::Base + # attr_accessible :title, :body + + belongs_to :course, counter_cache: true + belongs_to :user + belongs_to :course_group, counter_cache: true +end diff --git a/app/models/message.rb b/app/models/message.rb index 392bab48..9e516acb 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -54,6 +54,7 @@ class Message < ActiveRecord::Base belongs_to :reply, :class_name => 'Message', :foreign_key => 'reply_id' #has_one :message_detail, :dependent => :destroy + has_one :message_detail, dependent: :destroy #转发表 has_many :forwards, :as => :from, :dependent => :destroy @@ -102,7 +103,7 @@ class Message < ActiveRecord::Base } - safe_attributes 'subject', 'content', 'reply_id', 'root_id', 'visits' + safe_attributes 'subject', 'reply_id', 'root_id', 'visits' safe_attributes 'board_id','locked', 'sticky', :if => lambda {|message, user| if message.project @@ -115,7 +116,7 @@ class Message < ActiveRecord::Base } def description - self.content + message_detail.try(:content) end # def content diff --git a/app/models/message_detail.rb b/app/models/message_detail.rb new file mode 100644 index 00000000..a12a2b8f --- /dev/null +++ b/app/models/message_detail.rb @@ -0,0 +1,4 @@ +class MessageDetail < ActiveRecord::Base + # attr_accessible :title, :body + belongs_to :message, :touch => true +end diff --git a/app/models/user.rb b/app/models/user.rb index 0e034d32..dbb77124 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -97,6 +97,8 @@ class User < Principal has_many :homework_evaluations, :dependent => :destroy has_many :syllabuses, :dependent => :destroy has_many :syllabus_members, :dependent => :destroy + + has_many :course_members, :dependent => :destroy #问卷相关关关系 has_many :poll_users, :dependent => :destroy has_many :poll_votes, :dependent => :destroy @@ -1125,7 +1127,7 @@ class User < Principal end def member_of_course?(course) - courses.to_a.include?(course) || User.current.business? + course.course_members.exists?(user_id: self.id) || User.current.business? end def member_of_contest?(contest) diff --git a/app/services/users_service.rb b/app/services/users_service.rb index d60de29c..6df15d2a 100644 --- a/app/services/users_service.rb +++ b/app/services/users_service.rb @@ -313,8 +313,8 @@ class UsersService limit = params[:limit]||5 page = params[:page].to_i offset = (page-1) * limit - courses = Course.find_by_sql("SELECT c.* FROM courses c, members m, member_roles mr WHERE m.course_id = c.id AND m.id=mr.member_id AND - mr.role_id in (3,7,9,10) AND + courses = Course.find_by_sql("SELECT c.* FROM courses c, course_members m WHERE m.course_id = c.id AND + m.role in (1,2,3,4) AND m.user_id=#{current_user.id} AND c.is_delete = 0 order by id desc limit #{limit} offset #{offset} ") # 如果还没有课程则显示示例课堂 courses = Course.where(:id => 1309) if courses.count == 0 diff --git a/app/views/managements/_classroom_classment_list.html.erb b/app/views/managements/_classroom_classment_list.html.erb index 25b13911..2b9c98cb 100644 --- a/app/views/managements/_classroom_classment_list.html.erb +++ b/app/views/managements/_classroom_classment_list.html.erb @@ -24,7 +24,7 @@ <%= (@page-1)*@limit + index + 1 %> <%= course.id %> <%= link_to course.name, course_path(course.id) ,:target => '_blank'%> - <%= course.members.count %> + <%= course.course_members_count %> <%= course.attachments.count %> <%= course.homework_commons.where(:homework_type => 1).count %> <%= course.homework_commons.where(:homework_type => 4).count %> diff --git a/app/views/managements/_course_message_list.html.erb b/app/views/managements/_course_message_list.html.erb index e2264db9..4202aaad 100644 --- a/app/views/managements/_course_message_list.html.erb +++ b/app/views/managements/_course_message_list.html.erb @@ -16,7 +16,7 @@ <%= link_to message.board.course.name, course_path(message.board.course_id), target: '_blank'%> <%= message.board.course.teacher.show_real_name %> <%= format_time(message.created_on) %> - <%= link_to message_content(message.content), board_message_path(message.board_id, message), target: '_blank'%> + <%= link_to message_content(message.description), board_message_path(message.board_id, message), target: '_blank'%> <%= Message.where(:parent_id => message.id).count %> diff --git a/app/views/managements/_shixun_task_list.html.erb b/app/views/managements/_shixun_task_list.html.erb index 27bb4e67..924b456b 100644 --- a/app/views/managements/_shixun_task_list.html.erb +++ b/app/views/managements/_shixun_task_list.html.erb @@ -25,7 +25,7 @@ <%= link_to shixun_task.name,course_path(shixun_task),:target => '_blank' %> <%= shixun_task.teacher ? link_to(shixun_task.teacher.show_real_name, user_path(shixun_task.teacher)) : "" %> <%= shixun_task.teacher.try(:user_extensions).try(:school).try(:name).blank? ? "--" : shixun_task.teacher.try(:user_extensions).try(:school) %> - <%= shixun_task.student.count %> + <%= shixun_task.students.count %> <% homework_commons = shixun_task.homework_commons.where(:homework_type => 4).order("IF(ISNULL(publish_time),1,0),publish_time asc,created_at asc") %> <%= homework_commons.size %> # 1 diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index fb459160..e4b724ea 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -49,37 +49,37 @@
<%= current_user ? @user.identity : (@user.identity == "学生" ? "" : @user.identity) %> - <% if current_user %> - - <% if @user.authentication %> - - <% else %> - <% apply_auth = ApplyUserAuthentication.where(:user_id => @user.id, :auth_type => 1).last %> - - <% end %> - - - <% if @user.professional_certification %> - - <% else %> - <% apply_auth = ApplyUserAuthentication.where(:user_id => @user.id, :auth_type => 2).last %> - - <% end %> - - <% if @user.try(:phone).blank? %> - - <% else %> - - <% end %> - <% if @user.try(:mail).blank? %> - - <% else %> - - <% end %> - <% if @user.department_members.count > 0 %> - - <% end %> - <% else %> + <%# if current_user %> + + <%# if @user.authentication %> + + <%# else %> + <%# apply_auth = ApplyUserAuthentication.where(:user_id => @user.id, :auth_type => 1).last %> + + <%# end %> + + + <%# if @user.professional_certification %> + + <%# else %> + <%# apply_auth = ApplyUserAuthentication.where(:user_id => @user.id, :auth_type => 2).last %> + + <%# end %> + + <%# if @user.try(:phone).blank? %> + + <%# else %> + + <%# end %> + <%# if @user.try(:mail).blank? %> + + <%# else %> + + <%# end %> + <%# if @user.department_members.count > 0 %> + + <%# end %> + <%# else %> <% if @user.authentication %> @@ -106,10 +106,12 @@ <% else %> <% end %> - <% if @user.department_members.count > 0 %> + <% if @user.department_members.count > 0 && current_user %> + + <% elsif @user.department_members.count > 0 %> <% end %> - <% end %> + <%# end %>
diff --git a/spec/factories/course_members.rb b/spec/factories/course_members.rb new file mode 100644 index 00000000..174db783 --- /dev/null +++ b/spec/factories/course_members.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :course_member do + + end +end diff --git a/spec/factories/message_details.rb b/spec/factories/message_details.rb new file mode 100644 index 00000000..f49e24ba --- /dev/null +++ b/spec/factories/message_details.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :message_detail do + + end +end diff --git a/spec/models/course_member_spec.rb b/spec/models/course_member_spec.rb new file mode 100644 index 00000000..3972e0df --- /dev/null +++ b/spec/models/course_member_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CourseMember, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/message_detail_spec.rb b/spec/models/message_detail_spec.rb new file mode 100644 index 00000000..2a443ac1 --- /dev/null +++ b/spec/models/message_detail_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe MessageDetail, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end