@ -2,8 +2,6 @@ class CoursesController < ApplicationController
include MessagesHelper
include ExportHelper
rescue_from :: ActionView :: MissingTemplate , with : :missing_template
rescue_from ActiveRecord :: RecordNotFound , with : :object_not_found
# model validation error
rescue_from ActiveRecord :: RecordInvalid do | ex |
render_error ( ex . record . errors . full_messages . join ( ',' ) )
@ -13,9 +11,9 @@ class CoursesController < ApplicationController
render_error ( ex . model . errors . full_messages . join ( ',' ) )
end
before_action :require_login , except: [ :index , :show , :students , :teachers , :board_list , :mine , :all_course_groups ]
before_action :require_login , :check_auth, except: [ :index , :show , :students , :teachers , :board_list , :mine , :all_course_groups , :left_banner , :top_banner ]
before_action :set_course , :user_course_identity , only : [ :show , :update , :destroy , :settings , :set_invite_code_halt ,
:set_public_or_private , :search_teacher_candidate , :teachers ,
:set_public_or_private , :search_teacher_candidate , :teachers , :apply_teachers ,
:top_banner , :left_banner , :add_teacher_popup , :add_teacher ,
:graduation_group_list , :create_graduation_group , :join_graduation_group ,
:course_group_list , :set_course_group , :change_course_admin , :change_course_teacher ,
@ -27,11 +25,11 @@ class CoursesController < ApplicationController
before_action :teacher_allowed , only : [ :update , :destroy , :settings , :search_teacher_candidate ,
:transfer_to_course_group , :delete_from_course ,
:search_users , :add_students_by_search , :get_historical_courses , :add_teacher_popup , :add_teacher ]
before_action :admin_allowed , only : [ :set_invite_code_halt , :set_public_or_private ,
:set_course_group , :change_course_admin , :change_course_teacher ,
:delete_course_teacher , :teacher_application_review , :create_group_by_importing_file ]
before_action :admin_allowed , only : [ :set_invite_code_halt , :set_public_or_private , :change_course_admin ,
:set_course_group , :delete_course_teacher , :create_group_by_importing_file ]
before_action :teacher_or_admin_allowed , only : [ :graduation_group_list , :create_graduation_group , :join_graduation_group ,
:export_member_scores_excel , :course_group_list ]
:change_course_teacher , :export_member_scores_excel , :course_group_list ,
:teacher_application_review , :apply_teachers ]
before_action :validate_course_name , only : [ :create , :update ]
before_action :find_board , only : :board_list
before_action :validate_page_size , only : :mine
@ -49,47 +47,55 @@ class CoursesController < ApplicationController
@order = params [ :order ] . present? ? params [ :order ] : " all "
order_str = @order != " course_members_count " && @order != " created_at " ? " updated_at " : @order
if @order == " all "
# @course = Course.where(is_delete: 0, is_hidden: 0). find_by_sql("select c.name, c.id, s.name, u.last_name from
# courses c, users u, user_extensions ue, schools s where c.is_delete=0 and c.tea_id=u.id and
# u.id=ue.user_id and ue.school_id=s.id limit 10; ")
# if @order == "all "
# @course = Course.where(is_delete: 0, is_hidden: 0). select("select c.name, c.id, s.name, u.login, ifnull(concat(u.lastname,u.firstname),
# u.login), s.name from courses c, users u, user_extensions ue, schools s where c.is_delete=0 and
# c.tea_id=u.id and u.id=ue.user_id and ue.school_id=s.id")
@courses = Course . where ( is_delete : 0 , is_hidden : 0 ) . select ( " courses.id, courses.tea_id, courses.name, courses.exercises_count, courses.polls_count,
courses . is_public , courses . is_end , courses . visits , courses . course_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 "
# @courses = Course.where(is_delete: 0, is_hidden: 0)
# .order("courses.id = 1309 desc, courses.created_at desc")
# @courses = Course.where(is_delete: 0, is_hidden: 0).select("courses.id, courses.tea_id, courses.name, courses.exercises_count, courses.polls_count,
# courses.is_public, courses.is_end, courses.visits, courses.course_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")
if @order == " mine "
tip_exception ( 401 , " .. " ) unless current_user . logged?
@courses = Course . joins ( :course_members )
. where ( " is_delete = 0 AND is_hidden = 0 AND course_members.user_id = ? " , @user . id )
. order ( " courses.id = 1309 DESC, courses. #{ order_str } DESC " )
. where ( " is_delete = 0 AND is_hidden = 0 AND course_members.user_id = ? " , @user . id ) . distinct
elsif @order == " created_at "
# REDO:Extension
@courses = Course . joins ( teacher : :user_extension )
. where ( is_delete : 0 , is_hidden : 0 , is_end : 0 )
. order ( " courses.id = 1309 DESC, courses. #{ order_str } DESC " )
@courses = Course . where ( is_delete : 0 , is_hidden : 0 , is_end : 0 ) . distinct
else
# REDO:Extension
@courses = Course . joins ( teacher : :user_extension )
. where ( is_delete : 0 , is_hidden : 0 )
. order ( " courses.id = 1309 DESC, courses. #{ order_str } DESC " )
@courses = Course . where ( is_delete : 0 , is_hidden : 0 ) . distinct
end
# 根据搜索关键字进一步筛选
if params [ :search ] . present?
# REDO:Extension
user_ids = User . includes ( user_extension : :school ) . where ( " schools.name like ? " , " % #{ params [ :search ] } % " ) . pluck ( :id )
course_ids = CourseMember . includes ( :user , :course ) . where ( " course_members.course_id in (?) and course_members.role in (1,2,3)
and CONCAT ( users . lastname , users . firstname ) like ?" , @courses . map ( & :id ) , " % #{ params [ :search ] } % " )
. pluck ( :course_id )
@courses = @courses . where ( " name like ? " , " % #{ params [ :search ] } % " ) . or ( @courses . where ( tea_id : user_ids ) ) . or ( @courses . where ( id : course_ids ) )
#user_ids = User.includes(user_extension: :school).where("schools.name like ?", "%#{params[:search]}%").pluck(:id)
#course_ids = CourseMember.includes(:user, :course).where("course_members.course_id in (?) and course_members.role in (1,2,3)
# and CONCAT(users.lastname, users.firstname) like ?", @courses.map(&:id), "%#{params[:search]}%")
# .pluck(:course_id)
#@courses = @courses.where("name like ?", "%#{params[:search]}%").or(@courses.where(tea_id: user_ids)).or(@courses.where(id: course_ids))
# 6:21 daiao
sql = %Q{
( course_members . role in ( 1 , 2 , 3 ) and CONCAT ( users . lastname , users . firstname ) like :keyword ) or courses . name like :keyword
or schools . name like :keyword
}
@courses = @courses . joins ( :school , course_members : :user )
. where ( " #{ sql } " , keyword : " % #{ params [ :search ] } % " ) . distinct
end
@courses_count = @courses . size
@courses_count = @courses . count ( " courses.id " )
@courses = @courses . order ( " courses.id = 1309 DESC, courses. #{ order_str } DESC " )
# 分页
page = params [ :page ] || 1
limit = params [ :limit ] || 16
@courses = @courses . page ( page ) . per ( limit )
@courses = @courses . preload ( :school , :none_hidden_course_modules , teacher : :user_extension )
end
def visits_plus_one
@ -101,6 +107,7 @@ class CoursesController < ApplicationController
# GET /courses/1
# GET /courses/1.json
def show
# render :json => {first_category_url: module_url(course.course_modules.where.not(module_type: "activity").where(hidden: 0).first, course)}
end
# GET /courses/new
@ -241,44 +248,52 @@ class CoursesController < ApplicationController
if @course . try ( :id ) != 1309 || current_user . admin? || current_user . try ( :id ) == 15582
@teacher_list = @course . course_members . joins ( :user ) . where ( " course_members.role in (1, 2, 3)
and LOWER ( concat ( users . lastname , users . firstname ) ) LIKE '%#{@search_str}%' " )
and LOWER ( concat ( users . lastname , users . firstname ) ) LIKE ?" , " % #{ @search_str } % " )
else
@teacher_list = @course . course_members . joins ( :user ) . where ( " (course_members.role in (1, 3) or (course_members.user_id = #{ current_user . id }
and course_members . role = 2 ) ) and LOWER ( concat ( users . lastname , users . firstname ) )
LIKE '%#{@search_str}%' " )
LIKE ?" , " % #{ @search_str } % " )
end
@teacher_list_size = @teacher_list . size
@has_graduation_design = @course . course_modules . graduation_module_not_hidden . any?
sort = params [ :sort ] || " desc "
@order = params [ :order ] . to_i
if @order . present?
case @order
when 1
@teacher_list = @teacher_list . includes( :user ) . order ( " CONVERT(CONCAT(users.lastname, users.firstname) USING gbk) COLLATE gbk_chinese_ci asc " )
@teacher_list = @teacher_list . order( " role #{ sort } " )
when 2
@teacher_list = @teacher_list . order( " created_at " )
@teacher_list = @teacher_list . includes( :user ) . order ( " CONVERT(CONCAT(users.lastname, users.firstname) USING gbk) COLLATE gbk_chinese_ci #{ sort } " )
when 3
@teacher_list = @teacher_list . includes ( :course , :graduation_group ) . order ( " graduation_groups.name " )
@teacher_list = @teacher_list . includes ( :course , :graduation_group ) . order ( " graduation_groups.name #{ sort } " )
else
@teacher_list = @teacher_list . order ( " created_at " )
@teacher_list = @teacher_list . order ( " role #{ sort } " )
end
else
if @has_graduation_design
@teacher_list = @teacher_list . includes ( :course , :graduation_group ) . order ( " graduation_groups.name " )
else
@teacher_list = @teacher_list . order ( " created_at " )
end
@teacher_list = @teacher_list . order ( " role #{ sort } " )
end
@is_admin = current_user . creator_of_course? @course
@is_admin = @user_course_identity < Course :: PROFESSOR
@applications = CourseMessage . unhandled_join_course_requests_by_course ( @course )
@applications _size = CourseMessage . unhandled_join_course_requests_by_course ( @course ) . size
page = params [ :page ] || 1
limit = params [ :limit ] || 20
@teacher_list = @teacher_list . page ( page ) . per ( limit )
@teacher_list = @teacher_list . preload ( :graduation_group , :user , :teacher_course_groups )
end
def apply_teachers
search_str = params [ :search ] . present? ? params [ :search ] . strip : " "
@applications = CourseMessage . unhandled_join_course_requests_by_course ( @course ) .
joins ( " join users on course_messages.course_message_id=users.id " ) .
where ( " LOWER(concat(users.lastname, users.firstname)) LIKE ? " , " % #{ search_str } % " )
@teacher_list_size = @course . teachers . size
@applications_size = CourseMessage . unhandled_join_course_requests_by_course ( @course ) . size
@is_admin = @user_course_identity < Course :: PROFESSOR
end
# 打开添加教师或助教弹窗时访问的接口(需要返回该课堂所有答辩组和所有)
@ -538,6 +553,7 @@ class CoursesController < ApplicationController
course_teacher = CourseMember . find_by ( user_id : current_user . id , role : % i [ CREATOR PROFESSOR ASSISTANT_PROFESSOR ] , course_id : @course . id )
course_student . destroy!
course_teacher . update_attributes ( is_active : 1 ) if course_teacher . present? && ! course_teacher . is_active
CourseDeleteStudentDeleteWorksJob . perform_later ( @course . id , [ current_user . id ] )
normal_status ( 0 , " 退出成功 " )
end
@ -566,6 +582,18 @@ class CoursesController < ApplicationController
end
new_teacher . save!
# 课堂管理员才有分配权限, 且课堂分班数大于0
if @user_course_identity < Course :: PROFESSOR && @course . course_groups_count > 0 && params [ :group_id ] && params [ :group_id ] . size > 0
# 分班全选则是不限,不需要做处理
unless @course . course_groups . where ( id : params [ :group_id ] ) . size == @course . course_groups_count
@course . course_groups . where ( id : params [ :group_id ] ) . each do | group |
unless TeacherCourseGroup . where ( course_id : @course . id , user_id : applier_user . id , course_group_id : group . id , course_member_id : new_teacher . id ) . exists?
TeacherCourseGroup . create ( course_id : @course . id , user_id : applier_user . id , course_member_id : new_teacher . id , course_group_id : group . id )
end
end
end
end
elsif approval == 2
course_message . reject!
else
@ -625,7 +653,8 @@ class CoursesController < ApplicationController
page = params [ :page ] || 1
limit = params [ :limit ] || 20
@students = Kaminari . paginate_array ( @students ) . page ( page ) . per ( limit )
@students = @students . page ( page ) . per ( limit )
@students = @students . includes ( :course_group , user : :user_extension )
end
# 获取当前课程所有分班
@ -674,11 +703,16 @@ class CoursesController < ApplicationController
ActiveRecord :: Base . transaction do
begin
students = params [ :students ]
student_ids = [ ]
students . each do | student |
course_member = CourseMember . find_by! ( id : student [ :course_member_id ] . to_i , course_id : @course . id )
course_member = CourseMember . find_by ( id : student [ :course_member_id ] . to_i , course_id : @course . id )
if course_member . present?
student_ids << course_member . user_id
course_member . destroy!
end
end
CourseDeleteStudentDeleteWorksJob . perform_later ( @course . id , student_ids ) if student_ids . present?
normal_status ( 0 , " 操作成功 " )
rescue = > e
uid_logger ( e . message )
@ -701,7 +735,7 @@ class CoursesController < ApplicationController
student_ids = [ ]
user_ids . each do | user_id |
existing_course_member = CourseMember . find_by ( user_id : user_id . to_i , course_id : @course . id )
existing_course_member = @course . course_members . find_by ( user_id : user_id . to_i )
new_student = CourseMember . new ( user_id : user_id . to_i , course_id : @course . id , course_group_id : course_group_id , role : 4 )
if existing_course_member . present?
@ -710,6 +744,7 @@ class CoursesController < ApplicationController
else
new_student . is_active = 0 if existing_course_member . is_active
new_student . save!
student_ids << user_id
end
else
new_student . save!
@ -717,6 +752,7 @@ class CoursesController < ApplicationController
end
end
CourseAddStudentCreateWorksJob . perform_later ( @course . id , student_ids ) if student_ids . present?
TeacherInviteJoinCourseNotifyJob . perform_later ( current_user . id , @course . id , 10 , student_ids ) if student_ids . present?
normal_status ( 0 , " 添加成功 " )
rescue = > e
@ -727,11 +763,11 @@ class CoursesController < ApplicationController
end
end
# 获取历史课堂,即用户管理的所有课堂以及课堂下的分班
# 获取历史课堂,即用户管理的所有课堂以及课堂下的分班 (去除当前课堂)
def get_historical_courses
user_id = current_user . id
@courses = Course . includes ( :course_groups ) . where ( tea_id : user_id , is_delete : 0 )
@courses = Course . where . not ( id : @course . id ) . joins ( :course_members ) .
where ( is_delete : 0 , course_members : { user_id : current_user . id , role : % i [ CREATOR PROFESSOR ASSISTANT_PROFESSOR ] } ) . includes ( :course_groups )
# @courses = Course.includes(:course_groups).where(id: current_user.course_members, is_delete: 0 )
end
# 根据历史课堂的课堂id和分班id获取所有学生
@ -744,6 +780,7 @@ class CoursesController < ApplicationController
@students = @students . where ( course_group_id : course_group_id )
end
@students = @students . includes ( user : [ user_extension : :school ] )
@students_count = @students . size
end
@ -765,7 +802,7 @@ class CoursesController < ApplicationController
name = attachment . disk_filename
if name . split ( " . " ) . last == " xls "
begin
attachment_folder = Rails . configuration . educoder [ 'attachment_folder' ]
attachment_folder = edu_setting ( 'attachment_folder' )
full_path = " #{ attachment_folder } / #{ path } / #{ name } "
xls = Roo :: Spreadsheet . open ( full_path , extension : :xls )
worksheet = xls . sheet ( 0 )
@ -788,6 +825,8 @@ class CoursesController < ApplicationController
uid_logger_error ( e . message )
normal_status ( - 1 , " 无法完成导入,原因:文件内容无法读取 " )
end
else
normal_status ( - 1 , " 只支持xls文件的导入 " )
end
end
end
@ -841,6 +880,13 @@ class CoursesController < ApplicationController
return normal_status ( - 1 , " 同一课堂不允许申请多个教师身份 " )
end
# 验证是否存在同学号的学生
u_extension = current_user . user_extension
if params [ :student ] . present? && u_extension . student?
same_student_id_users = UserExtension . where . not ( user_id : current_user . id ) . where ( student_id : u_extension . student_id , identity : % i [ student ] , school_id : u_extension . school_id ) . pluck ( :user_id )
tip_exception ( " 该课堂已存在同学号的学生,暂时无法加入,请联系老师 " ) if course . students . exists? ( user_id : same_student_id_users )
end
# 创建学生身份
if params [ :student ] . present?
existing_student = CourseMember . find_by ( course_id : course . id , role : % i [ STUDENT ] , user_id : current_user . id )
@ -855,6 +901,7 @@ class CoursesController < ApplicationController
new_student . course_group_id = course_group . id if course_group . present?
new_student . save!
CourseAddStudentCreateWorksJob . perform_later ( course . id , [ current_user . id ] )
StudentJoinCourseNotifyJob . perform_later ( current_user . id , course . id )
end
end
@ -895,15 +942,15 @@ class CoursesController < ApplicationController
def top_banner
@user = current_user
@is_teacher = @user . teacher_of_course? ( @course )
@is_student = @ course. course_members . where ( user_id : @user . id , role : 4 , is_active : 1 ) . present?
@is_teacher = @user _course_identity < Course :: STUDENT
@is_student = @ user_course_identity == Course :: STUDENT
@course . increment! ( :visits )
end
def left_banner
@user = current_user
@is_teacher = @user . teacher_of_course? ( @course )
@course_modules = @course . course_modules . where . not ( module_type : " activity " ) . where ( hidden : 0 )
@is_teacher = @user _course_identity < Course :: ASSISTANT_PROFESSOR
@course_modules = @course . course_modules . where ( hidden : 0 )
@hidden_modules = @course . course_modules . where ( hidden : 1 )
@second_category_type = [ " shixun_homework " , " graduation " , " attachment " , " board " , " course_group " ]
end
@ -930,7 +977,7 @@ class CoursesController < ApplicationController
@page = params [ :page ] || 1
@page_size = params [ :page_size ] || 15
@courses = Course . by_user ( current_user ) . hidden ( false ) . ended( false ) . deleted ( 0 ) . by_keywords ( params [ :search ] ) . distinct
@courses = Course . by_user ( current_user ) . hidden ( false ) . processing. not_deleted . by_keywords ( params [ :search ] ) . distinct
# @total_count = @courses.count
# offset = 0
@ -946,34 +993,33 @@ class CoursesController < ApplicationController
def export_member_scores_excel
ActiveRecord :: Base . transaction do
begin
name = params [ :name ] ? " #{ params [ :name ] . strip } " : " " #用户名或学生学号id搜索
search = params [ :search ] ? " #{ params [ :search ] . strip } " : " " #用户名或学生学号id搜索
group_id = params [ :group_id ] #分班的班级id
if group_id && group_id != " 0 " && group_id != " -1 "
@all_members = @course . students . course_find_by_ids ( " course_group_id " , group_id )
elsif group_id && group_id == " 0 " # 未分班
@all_members = @course . course_members . ungroup_students
else
@all_members = @course . students
end
if name . present?
nick_name_search = @all_members . joins ( :user ) . where ( " nickname like ? " , " % #{ name } % " )
if nick_name_search . present?
@all_members = nick_name_search
else
@all_members = @all_members . joins ( user : [ :user_extension ] ) . where ( 'user_extensions.student_id like ? OR user_extensions.student_realname like ?' , " % #{ name } % " , " % #{ name } % " )
end
end
# if group_id && group_id != "0" && group_id != "-1"
# @all_members = @course.students.course_find_by_ids("course_group_id",group_id)
# elsif group_id && group_id == "0" # 未分班
# @all_members = @course.course_members.ungroup_students
# else
# @all_members = @course.students
# end
# if name.present?
# @all_members = @all_members.joins(user: [:user_extension]).where('concat(users.lastname, users.firstname) like ? or user_extensions.student_id like ?',"%#{name}%","%#{name}%")
# end
@all_members = student_act_score group_id , search
@c_homeworks = @course . homework_commons . homework_published . order ( " publish_time asc, created_at asc" )
@c_exercises = @course . exercises . is_exercise_published . order ( " publish_time asc, created_at asc" )
@c_polls = @course . polls . publish_or_not . order ( " publish_time asc, created_at asc " )
@c_tasks = @course . graduation_tasks . task_published . order ( " publish_time asc, created_at asc" )
@c_homeworks = @course . homework_commons . homework_published . order ( " homework_commons.publish_time asc, homework_commons.created_at asc " )
@c_exercises = @course . exercises . is_exercise_published . order ( " exercises.publish_time asc, exercises.created_at asc " )
# @c_polls = @course.polls.publish_or_not.order("polls.publish_time asc, polls.created_at asc" )
@c_tasks = @course . graduation_tasks . task_published . order ( " graduation_tasks. publish_time asc, graduation_tasks. created_at asc" )
if @user_course_identity > Course :: ASSISTANT_PROFESSOR
tip_exception ( 403 , " 无权限操作 " )
elsif @all_members . size == 0
normal_status ( - 1 , " 课堂暂时没有学生 " )
else
member_to_xlsx ( @course , @all_members , @c_homeworks , @c_exercises , @c_tasks , @c_polls )
filename = current_user . real_name + " _ " + @course . name + " _全部成绩 " + Time . now . strftime ( '%Y%m%d_%H%M%S' )
render xlsx : " #{ f ilename. strip . first ( 30 ) } " , template : " courses/export_member_scores_excel.xlsx.axlsx " ,
member_to_xlsx ( @course , @all_members , @c_homeworks , @c_exercises , @c_task s)
filename _ = " #{ current_user . real_name } _ #{ @course . name } _全部成绩 "
render xlsx : " #{ f ormat_sheet_name f ilename_ . strip . first ( 30 ) } " , template : " courses/export_member_scores_excel.xlsx.axlsx " ,
locals : { course_info : @course_info , activity_level : @user_activity_level ,
course_scores : @course_user_scores , shixun_works : @shixun_work_arrays ,
common_works : @common_work_arrays , group_works : @group_work_arrays , task_works : @task_work_arrays ,
@ -981,12 +1027,26 @@ class CoursesController < ApplicationController
end
rescue Exception = > e
uid_logger_error ( e . message )
tip_exception ( " 没有权限 " )
tip_exception ( e . message )
raise ActiveRecord :: Rollback
end
end
end
def search_slim
courses = current_user . manage_courses . not_deleted . processing
keyword = params [ :keyword ] . to_s . strip
if keyword . present?
courses = courses . where ( 'name LIKE ?' , " % #{ keyword } % " )
end
count = courses . count
courses = paginate ( courses )
render_ok ( count : count , courses : courses . select ( :id , :name ) . as_json )
end
private
# Use callbacks to share common setup or constraints between actions.
@ -1005,6 +1065,7 @@ class CoursesController < ApplicationController
tip_exception ( " 课堂名称应以课程名称开头命名 " ) unless params [ :course ] [ :name ] . index ( params [ :course_list_name ] ) &&
params [ :course ] [ :name ] . index ( params [ :course_list_name ] ) == 0
tip_exception ( " 课堂所属单位不能为空! " ) if params [ :school ] . blank?
tip_exception ( " 请至少添加一个课堂模块 " ) if params [ :course_module_types ] . blank?
@school = School . find_by! ( name : params [ :school ] . strip )
end
@ -1029,16 +1090,72 @@ class CoursesController < ApplicationController
end
end
def member_to_xlsx ( course , all_members , homeworks , exercises , tasks , polls )
def student_act_score group_id , search
sql_select = %Q{ SELECT cm.*,(
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 = cm . user_id
) AS score ,
( SELECT max ( student_id ) FROM user_extensions WHERE user_extensions . user_id = cm . user_id ) AS student_id ,
( SELECT count ( messages . id ) FROM messages join boards on messages . board_id = boards . id WHERE boards . course_id = #{@course.id}
AND messages . author_id = cm . user_id and messages . parent_id is null ) AS message_num ,
( SELECT count ( messages . id ) FROM messages join boards on messages . board_id = boards . id WHERE boards . course_id = #{@course.id}
AND messages . author_id = cm . user_id and messages . parent_id is not null ) AS message_reply_num ,
( SELECT count ( attachments . id ) FROM attachments WHERE container_id = #{@course.id} and container_type = "Course"
AND attachments . author_id = cm . user_id ) AS resource_num ,
( SELECT count ( jfm . id ) FROM journals_for_messages AS jfm , homework_commons hs WHERE jfm . jour_id = hs . id AND
jfm . user_id = cm . user_id and jfm . jour_type = " HomeworkCommon " and hs . course_id = #{@course.id}) AS homework_journal_num,
( SELECT COUNT ( gw . id ) FROM graduation_works AS gw , graduation_tasks AS gt WHERE gw . graduation_task_id = gt . id AND
gt . course_id = #{@course.id} AND gw.work_status != 0 AND gw.user_id = cm.user_id) AS graduation_num,
( SELECT COUNT ( ss . id ) FROM student_works AS ss , homework_commons AS hc WHERE ss . homework_common_id = hc . id AND
hc . course_id = #{@course.id} AND ss.work_status != 0 AND ss.user_id = cm.user_id) AS homework_num,
( SELECT COUNT ( eu . id ) FROM exercise_users AS eu , exercises WHERE eu . exercise_id = exercises . id AND exercises . course_id = #{@course.id}
AND eu . commit_status = 1 AND eu . user_id = cm . user_id ) AS exercise_num ,
( SELECT COUNT ( pu . id ) FROM poll_users AS pu , polls WHERE pu . poll_id = polls . id AND polls . course_id = #{@course.id}
AND pu . commit_status = 1 AND pu . user_id = cm . user_id ) AS poll_num
FROM course_members cm }
if search . present? && group_id . present?
sql_select += %Q{ join users on cm.user_id = users.id
joins user_extensions ue on ue . user_id = users . id
WHERE cm . role = 4 and cm . course_id = #{@course.id} and cm.course_group_id = #{group_id} and
( concat ( users . lastname , users . firstname ) like '%#{search}%' or ue . student_id like '%#{search}%' ) ORDER BY score desc }
elsif search . present?
sql_select += %Q{ join users on cm.user_id = users.id
joins user_extensions ue on ue . user_id = users . id
WHERE cm . role = 4 and
( concat ( users . lastname , users . firstname ) like '%#{search}%' or ue . student_id like '%#{search}%' ) ORDER BY score desc }
elsif group_id . present?
sql_select += %Q{ WHERE cm.role = 4 and cm.course_id = #{ @course . id } and cm.course_group_id = #{ group_id } ORDER BY score desc }
else
sql_select += %Q{ WHERE cm.role = 4 and cm.course_id = #{ @course . id } ORDER BY score desc }
end
act_scores = CourseMember . find_by_sql ( sql_select )
act_scores
end
def member_to_xlsx ( course , all_members , homeworks , exercises , tasks )
#课堂的作业信息
shixun_homeworks = homeworks . search_homework_type ( 4 ) #全部实训作业
shixun_titles = shixun_homeworks . pluck ( :name ) + [ " 总得分 " ]
shixun_homeworks = shixun_homeworks & . includes ( score_student_works : :user )
common_homeworks = homeworks . search_homework_type ( 1 ) #全部普通作业
common_titles = common_homeworks . pluck ( :name ) + [ " 总得分 " ]
common_homeworks = common_homeworks & . includes ( score_student_works : :user )
group_homeworks = homeworks . search_homework_type ( 3 ) #全部分组作业
group_titles = group_homeworks . pluck ( :name ) + [ " 总得分 " ]
group_homeworks = group_homeworks & . includes ( score_student_works : :user )
task_titles = tasks . pluck ( :name ) + [ " 总得分 " ]
tasks = tasks & . includes ( user : :user_extension , score_graduation_works : :user )
exercise_titles = exercises . pluck ( :exercise_name ) + [ " 总得分 " ]
exercises = exercises & . includes ( user : :user_extension , score_exercise_users : :user )
total_user_score_array = [ ] #学生总成绩集合
#课堂信息
@ -1047,13 +1164,13 @@ class CoursesController < ApplicationController
course_id = course . id
course_name = course . name
course_list_name = course . course_list . present? ? course . course_list . name : " -- "
course_assistants = course . course_members. course_user_role ( % i [ PROFESSOR ASSISTANT_PROFESSOR ] )
course_assistants_count = course_assistants & . count
course_assistants = course . teachers
course_assistants_count = course_assistants & . size
course_assistants_name = course_assistants_count > 0 ? course_assistants . map { | m | m . user . real_name } . join ( '、' ) : " -- "
course_teacher_member = course . course_members . course_user_role ( % i [ CREATOR ] )
course_teacher = course_teacher_member . present? ? course_teacher_member . first . user . real_name : " -- "
course_class_counts = course . course_groups_count
course_students_count = course . students . count
course_students_count = course . students . size
course_1 = [ " 课堂编号 " , course_id ]
course_2 = [ " 课程名称 " , course_list_name ]
course_3 = [ " 课堂名称 " , course_name ]
@ -1064,25 +1181,32 @@ class CoursesController < ApplicationController
course_main_info = [ course_1 , course_2 , course_3 , course_4 , course_5 , course_6 , course_7 ]
course_group_info_head = %w( 序号 分班名称 邀请码 学生数量 )
course_group_info_body = [ ]
if course . course_groups . present?
none_group_counts = course . none_group_count
#当有未分班时,应该也做个统计
if none_group_counts > 0
none_group_index = 2
no_group_array = [ 1 , " 未分班 " , course . invite_code , none_group_counts ]
course_group_info_body . push ( no_group_array )
else
none_group_index = 1
end
if course . course_groups . exists?
course . course_groups . each_with_index do | group , index |
group_index = ( index + 1 )
group_index = ( index + none_group_index )
group_name = group . name
group_code = group . invite_code
group_count = group . course_members_count
group_array = [ group_index , group_name , group_code , group_count ]
course_group_info_body . push ( group_array )
end
end
course_group_info = [ course_group_info_head , course_group_info_body ]
@course_info += [ course_info_title , course_main_info , course_group_info ]
#课堂活跃度
course_homework_ids = homeworks . pluck ( :id ) #该课堂的全部作业id
course_graduate_task_ids = tasks . pluck ( :id ) #该课堂的全部毕业任务id
course_exercise_ids = exercises . pluck ( :id ) #课堂的全部试卷数
course_poll_ids = polls . pluck ( :id ) #课堂的全部问卷数
course_board_ids = course . boards . pluck ( :id ) #课堂的全部讨论区
@user_activity_level = [ ]
course_user_level = [ ]
course_activity_title = " 课堂活跃度统计 "
@ -1093,27 +1217,26 @@ class CoursesController < ApplicationController
user_login = user . login
user_name = user . real_name
user_mail = user . mail
user_stu_id = u ser . student_id . present? ? ( u ser . student_id . to_s + " \t " ) : " -- "
user_stu_id = u . student_id . present? ? ( u . student_id . to_s + " \t " ) : " -- "
user_course_group = u . course_group_name
user_info_array = [ user_login , user_name , user_mail , user_stu_id , user_course_group ] #用户的信息集合
user_work_scores = [ ]
#课堂活跃度统计
user_homeworks_num = user . student_works . find_by_homework ( course_homework_ids ) . has_committed . count #完成的作业数
user_graduate_num = user . graduation_works . find_by_task ( course_graduate_task_ids ) . has_committed . count #毕业任务完成数
user_exercise_num = user . exercise_users . search_by_exercise ( course_exercise_ids ) . commit_exercise_by_status ( 1 ) . count #根据试卷的id来查找
user_poll_num = user . poll_users . search_by_poll ( course_poll_ids ) . commit_by_status ( 1 ) . count #已完成问卷
user_file_num = user . attachments . search_by_container ( course . id ) . count
user_messages = user . messages
user_messages_num = user_messages . root_nodes . find_by_boards ( course_board_ids ) . count #帖子发布数
user_reply_num = user_messages . reply_nodes . find_by_boards ( course_board_ids ) . count #帖子回复数
user_work_reply_num = user . journals_for_messages . search_by_jour_type ( " HomeworkCommon " , course_homework_ids ) . count #作业回复数的数量
user_homeworks_num = u . homework_num . to_i #完成的作业数
user_graduate_num = u . graduation_num . to_i #毕业任务完成数
user_exercise_num = u . exercise_num . to_i #根据试卷的id来查找
user_poll_num = u . poll_num . to_i #已完成问卷
user_file_num = u . resource_num . to_i
user_messages_num = u . message_num . to_i #帖子发布数
user_reply_num = u . message_reply_num . to_i #帖子回复数
user_work_reply_num = u . homework_journal_num . to_i #作业回复数的数量
c_works_num = ( user_homeworks_num + user_graduate_num ) * 10
c_exercise_num = user_exercise_num * 10
c_poll_num = user_poll_num * 7
c_file_num = user_file_num * 5
c_message_num = user_messages_num * 2
c_reply_num = user_reply_num * 2
c_reply_num = user_reply_num
user_activity_levels = c_works_num + c_exercise_num + c_poll_num + c_file_num + c_message_num + c_reply_num + user_work_reply_num
user_ac_level = {
u_1 : user_name ,
@ -1145,14 +1268,13 @@ class CoursesController < ApplicationController
exercise_score_array = [ ]
#实训作业
if shixun_homeworks . count > 0
if shixun_homeworks . size > 0
shixun_homeworks . each do | s |
user_student_work = s . s tudent_works. homework_by_user ( user . id ) #当前用户的对该作业的回答
if user_student_work . blank ?
user_student_work = s . s core_student_works. select { | work | work . user_id == user . id } . first #当前用户的对该作业的回答
if user_student_work . nil ?
h_score = 0 . 0 #该作业的得分为0
else
user_stu_work = user_student_work . first
h_score = user_stu_work . work_score . nil? ? 0 . 0 : user_stu_work . work_score #用户对该作业的分数
h_score = user_student_work . work_score . nil? ? 0 . 0 : user_student_work . work_score #用户对该作业的分数
end
shixun_score_array . push ( h_score )
end
@ -1162,14 +1284,13 @@ class CoursesController < ApplicationController
user_work_scores += user_info_array + shixun_score_array #单个用户的实训作业得分信息
#普通作业
if common_homeworks . count > 0
if common_homeworks . size > 0
common_homeworks . each do | c |
user_student_work_1 = c . s tudent_works. homework_by_user ( user . id ) #当前用户的对该作业的回答
if user_student_work_1 . blank ?
user_student_work_1 = c . s core_student_works. select { | work | work . user_id == user . id } . first #当前用户的对该作业的回答
if user_student_work_1 . nil ?
h_score_1 = 0 . 0 #该作业的得分为0
else
user_stu_work_1 = user_student_work_1 . first
h_score_1 = user_stu_work_1 . work_score . nil? ? 0 . 0 : user_stu_work_1 . work_score #用户对该作业的分数
h_score_1 = user_student_work_1 . work_score . nil? ? 0 . 0 : user_student_work_1 . work_score #用户对该作业的分数
end
common_score_array . push ( h_score_1 )
end
@ -1179,14 +1300,13 @@ class CoursesController < ApplicationController
user_work_scores += common_score_array #单个用户的普通作业得分信息
#分组作业
if group_homeworks . count > 0
if group_homeworks . size > 0
group_homeworks . each do | g |
user_student_work_3 = g . s tudent_works. homework_by_user ( user . id ) #当前用户的对该作业的回答
if user_student_work_3 . blank ?
user_student_work_3 = g . s core_student_works. select { | work | work . user_id == user . id } . first #当前用户的对该作业的回答
if user_student_work_3 . nil ?
h_score_3 = 0 . 0 #该作业的得分为0
else
user_stu_work_3 = user_student_work_3 . first
h_score_3 = user_stu_work_3 . work_score . nil? ? 0 . 0 : user_stu_work_3 . work_score #用户对该作业的分数
h_score_3 = user_student_work_3 . work_score . nil? ? 0 . 0 : user_student_work_3 . work_score #用户对该作业的分数
end
group_score_array . push ( h_score_3 )
end
@ -1196,13 +1316,13 @@ class CoursesController < ApplicationController
user_work_scores += group_score_array #单个用户的分组作业得分信息
#毕设作业
if tasks . count > 0
if tasks . size > 0
tasks . each do | task |
graduation_work s = task . graduation_works . find_by_task_user ( user . id )
if graduation_work s. empty ?
graduation_work = task . score_graduation_works . select { | work | work . user_id == user . id } . first
if graduation_work . nil ?
t_score = 0 . 0
else
t_score = graduation_work s. first . work_score . nil? ? 0 . 0 : graduation_work s. first . work_score
t_score = graduation_work . work_score . nil? ? 0 . 0 : graduation_work . work_score
end
task_score_array . push ( t_score )
end
@ -1212,13 +1332,13 @@ class CoursesController < ApplicationController
user_work_scores += task_score_array #单个用户的分组作业得分信息
#试卷
if exercises . count > 0
if exercises . size > 0
exercises . each do | ex |
exercise_work s = ex . exercise_users . exercise_commit_users ( user . id )
if exercise_work s. empty ?
exercise_work = ex . score_exercise_users . select { | work | work . user_id == user . id } . first
if exercise_work . nil ?
e_score = 0 . 0
else
e_score = exercise_work s. first . score . nil? ? 0 . 0 : exercise_work s. first . score
e_score = exercise_work . score . nil? ? 0 . 0 : exercise_work . score
end
exercise_score_array . push ( e_score )
end
@ -1250,73 +1370,60 @@ class CoursesController < ApplicationController
@common_work_arrays = [ ]
@task_work_arrays = [ ]
@exercise_work_arrays = [ ]
count_1 = shixun_homeworks . count
count_2 = common_homeworks . count
count_3 = group_homeworks . count
count_4 = tasks . count
count_5 = exercises . count
count_1 = shixun_homeworks . size
count_2 = common_homeworks . size
count_3 = group_homeworks . size
count_4 = tasks . size
#实训作业
if count_1 > 0
shixun_homeworks . each_with_index do | s , index |
all_student_works = s . s tudent_works. has_committed . order ( " work_score desc " ) #该实训题的全部用户回答
all_student_works = s . s core_s tudent_works #该实训题的全部用户回答
title_no = index . to_i + 1
student_work_to_xlsx ( all_student_works , s )
shixun_work_display_name = ( title_no . to_s + " . " + s . name ) . strip . first ( 30 )
shixun_work_display_name = format_sheet_name ( title_no . to_s + " . " + s . name ) . strip . first ( 30 )
shixun_work_content = [ shixun_work_display_name , @work_head_cells , @work_cells_column ]
@shixun_work_arrays . push ( shixun_work_content )
end
end
#普通作业
if count_2 > 0
common_homeworks . each_with_index do | c , index |
all_student_works = c . s tudent_works. has_committed . order ( " work_score desc " ) #当前用户的对该作业的回答
all_student_works = c . s core_s tudent_works #当前用户的对该作业的回答
title_no = count_1 + index . to_i + 1
student_work_to_xlsx ( all_student_works , c )
work_name = ( title_no . to_s + " . " + c . name ) . strip . first ( 30 )
work_name = format_sheet_name ( title_no . to_s + " . " + c . name ) . strip . first ( 30 )
work_content = [ work_name , @work_head_cells , @work_cells_column ]
@common_work_arrays . push ( work_content )
title_no
end
end
#分组作业
if count_3 > 0
group_homeworks . each_with_index do | c , index |
all_student_works = c . s tudent_works. has_committed . order ( " work_score desc " ) #当前用户的对该作业的回答
all_student_works = c . s core_s tudent_works #当前用户的对该作业的回答
title_no = count_1 + count_2 + index . to_i + 1
student_work_to_xlsx ( all_student_works , c )
work_name = ( title_no . to_s + " . " + c . name ) . strip . first ( 30 )
work_name = format_sheet_name ( title_no . to_s + " . " + c . name ) . strip . first ( 30 )
work_content = [ work_name , @work_head_cells , @work_cells_column ]
@group_work_arrays . push ( work_content )
end
end
#毕设任务
if count_4 > 0
tasks . each_with_index do | c , index |
all_student_works = c . graduation_works. has_committed . order ( " work_score desc " ) #当前用户的对该作业的回答
all_student_works = c . score_ graduation_works #当前用户的对该作业的回答
title_no = count_1 + count_2 + count_3 + index . to_i + 1
graduation_work_to_xlsx ( all_student_works , c , current_user )
work_name = ( title_no . to_s + " . " + c . name ) . strip . first ( 30 )
# work_content = [work_name,@work_head_cells,@work_cells_column]
work_name = format_sheet_name ( title_no . to_s + " . " + c . name ) . strip . first ( 30 )
work_content = [ work_name , @head_cells_column , @task_cells_column ]
@task_work_arrays . push ( work_content )
end
end
#试卷的导出
if count_5 > 0
exercises . each_with_index do | c , index |
all_student_works = c . exercise_users. exercise_user_committed #当前用户的对该作业的回答
all_student_works = c . score_exercise_users #当前用户的对该作业的回答
title_no = count_1 + count_2 + count_3 + count_4 + index . to_i + 1
get_export_users ( c , course , all_student_works )
work_name = ( title_no . to_s + " . " + c . exercise_name ) . strip . first ( 30 )
# work_content = [work_name,@work_head_cells,@work_cells_column]
work_name = format_sheet_name ( title_no . to_s + " . " + c . exercise_name ) . strip . first ( 30 )
work_content = [ work_name , @table_columns , @user_columns ]
@exercise_work_arrays . push ( work_content )
end
end
end
end