socialforge/app/controllers/courses_controller.rb

1191 lines
45 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#encoding: utf-8
class CoursesController < ApplicationController
# layout 'base_courses'
include CoursesHelper
include ActivitiesHelper
helper :activities
helper :members
helper :words
helper :attachments
helper :activity_notifys
before_filter :auth_login1, :only => [:show, :course_activity, :feedback]
menu_item :overview
menu_item :feedback, :only => :feedback
menu_item :homework, :only => :homework
menu_item :new_homework, :only => :new_homework
menu_item l(:label_sort_by_time), :only => :index
menu_item l(:label_sort_by_active), :only => :index
menu_item l(:label_sort_by_influence), :only => :index
before_filter :can_show_course, :except => []
before_filter :logged_user_by_apptoken,:only => [:show,:feedback]
before_filter :find_course, :except => [ :index, :search, :new,:join,:unjoin, :create, :new_join, :course,:join_private_courses]
before_filter :authorize_course, :only => [:show, :settings, :update, :course]
before_filter :authorize_course_global, :only => [:new,:create]
before_filter :toggleCourse, :only => [:finishcourse, :restartcourse]
before_filter :is_deleted, :only => [:show, :settings]
before_filter :require_login, :only => [:join, :unjoin]
#before_filter :allow_join, :only => [:join]
#查找组织
def search_public_orgs_not_in_course
condition = '%%'
if !params[:name].nil?
condition = "%#{params[:name].strip}%".gsub(" ","")
end
limit = 15
course_org_ids = OrgCourse.find_by_sql("select distinct organization_id from org_courses where course_id = #{params[:id]}").map(&:organization_id)
#@orgs_not_in_course = User.current.organizations.where("organizations.id not in (#{course_org_ids.join(',')}) and organizations.name like ?", condition).page(params[:page].to_i || 1).per(limit)
#@org_count = User.current.organizations.where("organizations.id not in (#{course_org_ids.join(',')}) and organizations.name like ?", condition).count
if course_org_ids.empty?
@orgs_not_in_course = User.current.organizations.where("name like ?",condition).page((params[:page].to_i || 1)).per(limit)
@org_count = @orgs_not_in_course.count
else
course_org_ids = "(" + course_org_ids.join(',') + ")"
@orgs_not_in_course = User.current.organizations.where("organizations.id not in #{course_org_ids} and organizations.name like ?", condition).page((params[:page].to_i || 1)).per(limit)
@org_count = @orgs_not_in_course.empty? ? 0 : @orgs_not_in_course.count
end
@course_count = Project.course_entities.visible.like(params[:name]).page(params[:page]).count
@orgs_page = Paginator.new @org_count, limit,params[:page]
@hint_flag = params[:hint_flag]
#render :json => {:orgs => @orgs_not_in_course, :count => @org_count}.to_json
respond_to do |format|
format.js
end
end
def join
if User.current.logged?
cs = CoursesService.new
@user = User.current
join = cs.join_course params,@user
@state = join[:state]
@course = join[:course]
# else
# @course = Course.find_by_id params[:object_id]
# 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')
# @state = 6
# end
else
@state = 5 #未登录
end
@object_id = params[:object_id]
respond_to do |format|
format.js #{ render :partial => 'set_join', :locals => {:user => @user, :course => @course, :object_id => params[:object_id]} }
end
end
def unjoin
if User.current.logged?
cs = CoursesService.new
cs.exit_course params,User.current
end
respond_to do |format|
format.js { render :partial => 'set_join', :locals => {:user => User.current, :course => Course.find(params[:object_id]), :object_id => params[:object_id]} }
end
end
def join_private_courses
respond_to do |format|
format.js
end
end
#更新课程信息
def update
cs = CoursesService.new
c = cs.edit_course params,@course,User.current
@course = c[:course]
if @course.errors.full_messages.count <= 0
respond_to do |format|
format.html {
# render :layout => 'base_courses'
flash[:notice] = l(:notice_successful_update)
redirect_to settings_course_url(@course)
}
format.api { render_api_ok }
end
else
respond_to do |format|
format.html {
settings
redirect_to settings_course_url(@course)
}
format.api { render_validation_errors(@course) }
end
end
end
def new_join
@course = Course.find(params[:object_id])
respond_to do |format|
format.js
end
end
# 课程搜索
# add by nwb
def search
if params[:name].empty?
courses = Course.visible
@courses = paginateHelper courses,10
else
courses = Course.visible.where("LOWER(name) like '%#{params[:name].to_s.downcase}%'").order("time desc, created_at desc")
@courses = paginateHelper courses,10
end
@name = params[:name]
@type = 'courses'
respond_to do |format|
format.html {
render :layout => 'course_base'
}
format.atom {
courses = Course.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all
render_feed(courses, :title => "#{Setting.app_title}: #{l(:label_course_latest)}")
}
end
rescue Exception => e
if e.message == 'sumbit empty'
(redirect_to courses_url, :notice => l(:label_sumbit_empty);return)
end
end
def searchmembers
@subPage_title = l :label_student_list
@canShowCode = User.current.allowed_to?(:as_teacher,@course) && params[:role] != '1'
# @is_remote = true
@score_sort_by = "desc"
q = "#{params[:name].strip}"
if params[:incourse]
results = searchmember_by_name(student_homework_score(0,0,0,"desc"), q)
elsif params[:ingroup]
@group = CourseGroup.find(params[:search_group_id])
results = searchmember_by_name(student_homework_score(@group.id,0,0,"desc"), q)
end
@is_remote = true
#@result_count = results.count
#@results = paginateHelper results, 10
@results = results
@search_name = q
end
def addgroups
@subPage_title = l :label_student_list
if params[:group_name]
group = CourseGroup.new
group.name = params[:group_name]
group.course_id = @course.id
group.save
end
@canShowCode = User.current.allowed_to?(:as_teacher,@course) && params[:role] != '1'
@is_remote = true
@course_groups = @course.course_groups
end
def deletegroup
CourseGroup.delete(params[:group_id])
@subPage_title = l :label_student_list
@canShowCode = isCourseTeacher(User.current.id,@course) && params[:role] != '1'
@is_remote = true
@course_groups = @course.course_groups
end
def updategroupname
@subPage_title = l :label_student_list
if params[:group_name]
group = CourseGroup.find(params[:group_id])
group.name = params[:group_name]
group.save
end
@canShowCode = isCourseTeacher(User.current.id,@course) && params[:role] != '1'
@is_remote = true
@course_groups = @course.course_groups
end
def valid_ajax
req = Hash.new(false)
req[:message] = ''
valid_attr = params[:valid]
valid_value = params[:value]
group_id = params[:group_id]
# faker = CourseGroup.new
if valid_attr.eql?('name')
course = Course.find params[:course_id]
group_names = course.course_groups.map{|group| group.name unless group.id.to_s == group_id}.select{|group| !group.nil?}
if group_names.include?(valid_value)
req[:valid] = false
req[:message] = l(:modal_valid_unpassing)
else
req[:valid] = true
req[:message] = l(:modal_valid_passing)
end
# faker.name = valid_value
# faker.course_id = params[:course_id]
# faker.valid?
# req[:valid] = faker.errors[:name].blank?
# req[:message] = faker.errors[:name]
end
# req[:message] = l(:modal_valid_passing) if req[:message].blank?
render :json => req
end
def join_group
@subPage_title = l :label_student_list
group = CourseGroup.find(params[:object_id])
member = Member.where(:course_id => @course.id, :user_id => User.current.id).first
member.course_group_id = group.id
member.save
@group = group
@course_groups = @course.course_groups
search_group_members group
end
def unjoin_group
@subPage_title = l :label_student_list
group = CourseGroup.find(params[:object_id])
member = Member.where(:course_id => @course.id, :user_id => User.current.id).first
member.course_group_id = 0
member.save
@group = group
@course_groups = @course.course_groups
search_group_members group
end
def searchgroupmembers
@subPage_title = l :label_student_list
@render_file = 'new_member_list'
@canShowCode = isCourseTeacher(User.current.id,@course) && params[:role] != '1'
@is_remote = true
@score_sort_by = "desc"
if params[:group_id] && params[:group_id] != "0"
@group = CourseGroup.find(params[:group_id])
@results = student_homework_score(@group.id,0, 0,"desc")
# @results = paginateHelper @results, 10
else
page_from = params[:page].nil? ? 0 : (params[:page].to_i - 1)
@results = student_homework_score(0,page_from, 10,"desc")
end
end
def member
## 有角色参数的才是课程,没有的就是项目
if (User.current.admin? || @course.is_public == 1 || (@course.is_public == 0 && User.current.member_of_course?(@course)))
@render_file = 'new_member_list'
@score_sort_by = "desc"
@canShowCode = User.current.allowed_to?(:as_teacher,@course) && params[:role] != '1'
@role = params[:role].nil? ? '2':params[:role]
@is_remote = true
@course_groups = @course.course_groups if @course.course_groups
@show_serch = params[:role] == '2'
case @role
when '1'
@subPage_title = l :label_teacher_list
@all_members = searchTeacherAndAssistant(@course)
@members = @all_members
when '2'
if @course.open_student == 1 || User.current.member_of_course?(@course)
@subPage_title = l :label_student_list
page = params[:page].nil? ? 0 : (params['page'].to_i - 1)
@all_members = student_homework_score(0,page, 10,"desc")
@members = @all_members
else
render_403
return
end
end
respond_to do |format|
if params[:page]
format.html {render :layout => 'base_courses'}
format.js
else
format.html {render :layout => 'base_courses'}
end
end
else
render_403
end
end
def export_course_member_excel
@all_members = student_homework_score(0,0,0,"desc")
@homeworks = @course.homework_commons.order("created_at desc")
filename="#{@course.teacher.lastname.to_s + @course.teacher.firstname.to_s }_#{@course.name}_#{@course.time.to_s + @course.term}#{l(:excel_member_list)}";
respond_to do |format|
format.xls {
send_data(member_to_xls(@homeworks,@course,@all_members,@course.course_groups), :type => "text/excel;charset=utf-8; header=present",
:filename => filename_for_content_disposition("#{filename}.xls"))
}
end
end
def member_score_sort
@canShowCode = isCourseTeacher(User.current.id,@course) && params[:role] != '1'
@subPage_title = l :label_student_list
@render_file = 'new_member_list'
@is_remote = true
@score_sort_by = params[:sort_by] if params[:sort_by]
@search_name = params[:search_name] if params[:search_name]
group_id = params[:group_id]
if !@search_name.nil?
if group_id == '0'
page = params[:page].nil? ? 0 : (params['page'].to_i - 1)
@results = searchmember_by_name(student_homework_score(0,0,0,@score_sort_by), @search_name)
@result_count = @results.count
# @results = paginateHelper @results, 10
else
@group = CourseGroup.find(group_id)
@results = searchmember_by_name(student_homework_score(group_id, 0, 0,@score_sort_by),@search_name)
@result_count = @results.count
# @results = paginateHelper @results, 10
end
else
if group_id == '0'
page = params[:page].nil? ? 0 : (params['page'].to_i - 1)
@results = student_homework_score(0,page, 10,@score_sort_by)
else
@group = CourseGroup.find(group_id)
@results = student_homework_score(group_id, 0, 0,@score_sort_by)
end
end
end
# 显示每个学生的作业评分详情
def show_member_score
@member_score = Member.find(params[:member_id]) if params[:member_id]
respond_to do |format|
format.html {render :layout => 'course_base'}
format.js
end
end
def handle_course courses, activities
course_activity_count_array=activities.values()
course_array=[]
i=0;
courses.each do |course|
course_array[i]=course
i=i+1
end
courses=desc_sort_course_by_avtivity(course_activity_count_array, course_array)
return courses
end
def settings
if User.current.allowed_to?(:as_teacher,@course)
@select_tab = params[:tab]
@issue_custom_fields = IssueCustomField.sorted.all
@issue_category ||= IssueCategory.new
@member ||= @course.members.new
@trackers = Tracker.sorted.all
@roles = Role.givable.all[3..5]
@members = @course.member_principals.includes(:roles, :principal).all.sort
respond_to do |format|
format.html { render :layout => 'base_courses' }
format.api { render_validation_errors(@course) }
end
else
render_403
end
end
def private_or_public
if @course.is_public == 0
@course.update_attributes(:is_public => 1)
else
@course.update_attributes(:is_public => 0)
end
if @course.is_public == 0
course_status = CourseStatus.find_by_course_id(@course.id)
course_status.destroy if course_status
elsif @course.is_public == 1
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)
end
respond_to do |format|
format.js
end
end
def search_member
if User.current.allowed_to?(:as_teacher,@course) || User.current.admin
q = "#{params[:name].strip}"
@roles = Role.givable.all[3..5]
if q.nil? || q == ""
@members = @course.member_principals.includes(:roles, :principal).all.sort
else
@members = searchmember_by_name(@course.member_principals.includes(:roles, :principal).all.sort,q)
end
else
render_403
end
end
def create
cs = CoursesService.new
@course = cs.create_course(params,User.current)[:course]
if params[:copy_course]
copy_course = Course.find params[:copy_course].to_i
@course.is_copy = params[:copy_course].to_i
@course.open_student = copy_course.open_student
@course.publish_resource = copy_course.publish_resource
@course.save
#copy avatar
copy_avatar(@course, copy_course)
if params[:course_content_type]
params[:course_content_type].each do |type|
case type
when "0"
homeworks = copy_course.homework_commons
homeworks.each do |homework|
new_homework = HomeworkCommon.new
new_homework.name = homework.name
new_homework.user_id = User.current.id
new_homework.description = homework.description
new_homework.publish_time = Date.today + 30
new_homework.end_time = Date.today + 60
new_homework.homework_type = homework.homework_type
new_homework.late_penalty = homework.late_penalty
new_homework.course_id = @course.id
new_homework.teacher_priority = homework.teacher_priority
new_homework.anonymous_comment = homework.anonymous_comment
new_homework.quotes = 0
new_homework.is_open = homework.is_open
homework.attachments.each do |attachment|
att = attachment.copy
att.container_id = nil
att.container_type = nil
att.copy_from = attachment.id
att.save
new_homework.attachments << att
end
homework_detail_manual = homework.homework_detail_manual
homework_detail_programing = homework.homework_detail_programing
homework_detail_group = homework.homework_detail_group
if homework_detail_manual
new_homework.homework_detail_manual = HomeworkDetailManual.new
new_homework_detail_manual = new_homework.homework_detail_manual
new_homework_detail_manual.ta_proportion = homework_detail_manual.ta_proportion
new_homework_detail_manual.comment_status = 0
new_homework_detail_manual.evaluation_start = Date.today + 67
new_homework_detail_manual.evaluation_end = Date.today + 74
new_homework_detail_manual.evaluation_num = homework_detail_manual.evaluation_num
new_homework_detail_manual.absence_penalty = homework_detail_manual.absence_penalty
end
if homework_detail_programing
new_homework.homework_detail_programing = HomeworkDetailPrograming.new
new_homework.homework_detail_programing.ta_proportion = homework_detail_programing.ta_proportion
new_homework.homework_detail_programing.language = homework_detail_programing.language
homework.homework_tests.each_with_index do |homework_test|
new_homework.homework_tests << HomeworkTest.new(
input: homework_test.input,
output: homework_test.output
)
end
end
if homework_detail_group
new_homework.homework_detail_group = HomeworkDetailGroup.new
new_homework.homework_detail_group.min_num = homework_detail_group.min_num
new_homework.homework_detail_group.max_num = homework_detail_group.max_num
new_homework.homework_detail_group.base_on_project = homework_detail_group.base_on_project
end
if new_homework.save
new_homework_detail_manual.save if new_homework_detail_manual
new_homework.homework_detail_programing.save if new_homework.homework_detail_programing
new_homework.homework_detail_group.save if new_homework.homework_detail_group
end
homework.update_attribute(:quotes, homework.quotes+1)
end
when "1"
attachments = copy_course.attachments
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.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
attach_copied_obj.publish_time = Date.today + 30
if attach_copied_obj.attachtype == nil
attach_copied_obj.attachtype = 4
end
attach_copied_obj.save
update_quotes attach_copied_obj
end
end
end
end
=begin
if params[:checkAll]
attachments = copy_course.attachments
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.author_id = User.current.id
attach_copied_obj.copy_from = attachment.copy_from.nil? ? attachment.id : attachment.copy_from
if attach_copied_obj.attachtype == nil
attach_copied_obj.attachtype = 4
end
attach_copied_obj.save
update_quotes attach_copied_obj
end
elsif params[:course_attachment_type]
copy_attachments = []
params[:course_attachment_type].each do |type|
case type
when "1"
tag_name = l(:label_courseware)
when "2"
tag_name = l(:label_software)
when "3"
tag_name = l(:label_media)
when "4"
tag_name = l(:label_code)
when "6"
tag_name = "论文"
else
tag_name = ""
end
if tag_name == ""
tag_attachments = copy_course.attachments.select{|attachment|
!attachment.tag_list.include?('课件') &&
!attachment.tag_list.include?('软件') &&
!attachment.tag_list.include?('媒体') &&
!attachment.tag_list.include?('代码') &&
!attachment.tag_list.include?('论文') }
else
tag_attachments = copy_course.attachments.select{|attachment| attachment.tag_list.include?(tag_name)}
end
tag_attachments.each do |attach|
next if copy_attachments.include?(attach)
copy_attachments << attach
end
end
unless copy_attachments.blank?
copy_attachments.each do |c_attach|
attach_copied_obj = c_attach.copy
attach_copied_obj.tag_list.add(c_attach.tag_list) # tag关联
attach_copied_obj.container = @course
attach_copied_obj.created_on = Time.now
attach_copied_obj.author_id = User.current.id
attach_copied_obj.copy_from = c_attach.copy_from.nil? ? c_attach.id : c_attach.copy_from
if attach_copied_obj.attachtype == nil
attach_copied_obj.attachtype = 4
end
attach_copied_obj.save
update_quotes attach_copied_obj
end
end
end
=end
end
if @course
respond_to do |format|
flash[:notice] = l(:notice_successful_create)
format.html {redirect_to course_url(@course)}
format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'courses', :action => 'show', :id => @course.id) }
end
else
respond_to do |format|
flash[:notice] = l(:notice_create_failed)
# @course = Course.new
format.html { redirect_to new_course_path } #Added by young
format.api { render_validation_errors(@course) }
end
end
end
def course
@school_id = params[:school_id]
per_page_option = 10
if @school_id == "0" or @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").
joins(:course_extra).
where("#{Course.table_name}.school_id = ?", @school_id)
end
@course_count = @courses_all.count
@course_pages = Paginator.new @course_count, per_page_option, params['page']
@course_activity_count=Hash.new
@courses_all.each do |course|
@course_activity_count[course.id]=0
end
case params[:course_sort_type]
when '0'
@courses = @courses_all.order("created_on desc")
@s_type = 0
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@course_activity_count=get_course_activity @courses,@course_activity_count
when '1'
@courses = @courses_all.order("course_ac_para desc")
@s_type = 1
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@course_activity_count=get_course_activity @courses,@course_activity_count
when '2'
@courses = @courses_all.order("watchers_count desc")
@s_type = 2
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@course_activity_count=get_course_activity @courses,@course_activity_count
when '3'
@course_activity_count=get_course_activity @courses_all,@course_activity_count
@courses=handle_course @courses_all,@course_activity_count
@s_type = 3
@courses = @courses[@course_pages.offset, @course_pages.per_page]
else
@s_type = 0
@courses = @courses_all.order("created_on desc")
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@course_activity_count=get_course_activity @courses,@course_activity_count
end
respond_to do |format|
format.html {
render :layout => 'new_base'
}
format.api {
}
format.atom {
courses = Course.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all
render_feed(courses, :title => "#{Setting.app_title}: #{l(:label_course_latest)}")
}
end
end
def new
if User.current.login?
@course_type = params[:course_type] ||= params[:course]
@issue_custom_fields = IssueCustomField.sorted.all
@trackers = Tracker.sorted.all
@course = Course.new
@course.safe_attributes = params[:course]
# month = Time.now.month
render :layout => 'new_base'
else
redirect_to signin_url
end
end
def desc_sort_course_by_avtivity(activity_count, courses)
return courses if activity_count.size<2
(activity_count.size-2).downto(0) do |i|
(0..i).each do |j|
if activity_count[j]<activity_count[j+1]
courses[j], courses[j+1]=courses[j+1], courses[j]
activity_count[j], activity_count[j+1]=activity_count[j+1], activity_count[j]
end
end
end
return courses
end
def index
if !User.current.admin?
render_404
return
end
@course_type = params[:course_type]
@school_id = params[:school_id]
per_page_option = 10
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 = Paginator.new @course_count, per_page_option, params['page']
@course_activity_count=Hash.new
@courses_all.each do |course|
@course_activity_count[course.id]=0
end
case params[:course_sort_type]
when '0'
@courses = @courses_all.order("created_at desc")
@s_type = 0
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@course_activity_count=get_course_activity @courses, @course_activity_count
when '1'
@courses = @courses_all.order("course_ac_para desc")
@s_type = 1
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@course_activity_count=get_course_activity @courses, @course_activity_count
when '2'
@courses = @courses_all.order("watchers_count desc")
@s_type = 2
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@course_activity_count=get_course_activity @courses, @course_activity_count
when '3'
@course_activity_count=get_course_activity @courses_all, @course_activity_count
@courses=handle_course @courses_all, @course_activity_count
@s_type = 3
@courses = @courses[@course_pages.offset, @course_pages.per_page]
else
@s_type = 0
@courses = @courses_all.order("created_at desc")
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@course_activity_count=get_course_activity @courses, @course_activity_count
end
respond_to do |format|
format.html {
render :layout => 'base'
}
format.atom {
courses = Course.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all
render_feed(courses, :title => "#{Setting.app_title}: #{l(:label_course_latest)}")
}
end
end
def toggleCourse
@course_prefs = Course.find_by_extra(@course.extra)
unless (User.current.allowed_to?(:as_teacher,@course_prefs) || User.current.admin?)
render_403
end
end
def is_deleted
if @course.is_delete == 1 and !User.current.admin?
render_404
return
end
end
def get_courses
@user = User.current
membership = @user.coursememberships.all
membership.sort! {|older, newer| newer.created_on <=> older.created_on }
@memberships = []
membership.collect { |e|
@memberships.push(e)
}
@memberships_doing = []
@memberships_done = []
now_time = Time.now.year
@memberships.map { |e|
end_time = e.course.get_time.year
isDone = course_endTime_timeout?(e.course)
if isDone
@memberships_done.push e
else
@memberships_doing.push e
end
}
end
def finishcourse
yesterday = Date.today.prev_day.to_time
@course_prefs.endup_time = yesterday
@save_flag = @course_prefs.save
get_courses
respond_to do |format|
format.js
end
end
def restartcourse
day = Time.parse("3000-01-01")
@course_prefs.endup_time = day
@save_flag = @course_prefs.save
get_courses
respond_to do |format|
format.js {
render action:'finishcourse'
}
end
end
def course_activity
redirect_to course_url(@course, type: params[:type], page: params[:page])
end
def show
# 被删除的课程只有超级管理员才能看到is_delete为1的时候标记课程被删除
# if @course.is_delete == 1 && !User.current.admin?
# render_403
# return
# end
#更新创建课程消息状态
create_course_messages = @course.course_messages.where("user_id =? and course_message_type =? and course_id =? and viewed =?", User.current.id, 'Course', @course.id, 0)
create_course_messages.update_all(:viewed => true)
#更新申请结果反馈消息的状态
course_request_messages = CourseMessage.where("user_id =? and course_id =? and course_message_type =? and viewed =?", User.current.id, @course.id, 'CourseRequestDealResult', false)
course_request_messages.update_all(:viewed => true)
course_activities = @course.course_activities
@canShowRealName = User.current.member_of_course? @course
@page = params[:page] ? params[:page].to_i + 1 : 0
if params[:type].present?
case params[:type]
when "homework"
@course_activities = course_activities.where("course_act_type = 'HomeworkCommon'").order('updated_at desc').limit(10).offset(@page * 10)
when "news"
@course_activities = course_activities.where("course_act_type = 'News'").order('updated_at desc').limit(10).offset(@page * 10)
when "message"
@course_activities = course_activities.where("course_act_type = 'Message'").order('updated_at desc').limit(10).offset(@page * 10)
when "poll"
@course_activities = course_activities.where("course_act_type = 'Poll'").order('updated_at desc').limit(10).offset(@page * 10)
when "attachment"
@course_activities = course_activities.where("course_act_type = 'Attachment'").order('updated_at desc').limit(10).offset(@page * 10)
when "journalsForMessage"
@course_activities = course_activities.where("course_act_type = 'JournalsForMessage'").order('updated_at desc').limit(10).offset(@page * 10)
else
@course_activities = course_activities.order('updated_at desc').limit(10).offset(@page * 10)
end
else
@course_activities = course_activities.order('updated_at desc').limit(10).offset(@page * 10)
end
@type = params[:type]
respond_to do |format|
format.js
format.html{render :layout => 'base_courses'}
format.api
end
end
#判断指定用户是否为课程教师
def isCourseTeacher(id,course)
result = false
user = User.find(id)
if !user.nil? && user.allowed_to?(:as_teacher,course)
result = true
end
result
end
def feedback
@course.journals_for_messages.each do |messages|
query = messages.course_messages.where("user_id = ?", User.current.id)
query.update_all(:viewed => true);
end
if (User.current.admin? || @course.is_public == 1 || (@course.is_public == 0 && User.current.member_of_course?(@course)))
page = params[:page]
# Find the page of the requested reply
@jours = @course.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC')
@limit = 10
if params[:r] && page.nil?
offset = @jours.count(:conditions => ["#{JournalsForMessage.table_name}.id > ?", params[:r].to_i])
page = 1 + offset / @limit
end
@jour = paginateHelper @jours,10
@state = false
respond_to do |format|
format.html{render :layout => 'base_courses'}
format.api
end
else
render_403
end
end
#根据已有课程复制课程
#param id:已有课程ID
def copy_course
if @course
@new_course = Course.new
respond_to do |format|
format.js
end
=begin
@new_course = Course.new @course.attributes
@new_course.tea_id = User.current.id
@new_course.created_at = DateTime.now
@new_course.updated_at = DateTime.now
@new_course.endup_time = nil
if @new_course.save
r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
m = Member.new(:user => User.current, :roles => [r])
m.project_id = -1
course = CourseInfos.new(:user_id => User.current.id, :course_id => @new_course.id)
#user_grades = UserGrade.create(:user_id => User.current.id, :course_id => @course.id)
if @new_course.is_public == 1
course_status = CourseStatus.create(:course_id => @new_course.id, :watchers_count => 0, :changesets_count => 0, :grade => 0, :course_type => 1)
end
@new_course.members << m
@new_course.course_infos << course
redirect_to settings_course_url @new_course
end
=end
else
render_404
end
end
#从课程创建的老师那里选择课程大纲
def course_outline
@teacher = User.find(@course.tea_id)
@blog_articles = @teacher.blog.articles
@is_in_show_outline_page = params[:is_in_show_outline_page]
respond_to do |format|
format.js
end
end
#根据关键字搜索,查找方法一样的,但返回内容不一样
def search_course_outline
@article_title = params[:title]
@teacher = User.find(@course.tea_id)
@blog_articles = @teacher.blog.articles.like(@article_title)
render :json=>@blog_articles.to_json
end
#设置或者更改课程的大纲
def set_course_outline
@course.outline = params[:outline_id]
@course.save
@is_in_show_outline_page = params[:is_in_show_outline_page]
respond_to do |format|
format.js
end
end
#显示课程大纲
def syllabus
@article = BlogComment.find(@course.outline)
respond_to do |format|
format.html {render :layout => 'base_courses'}
end
end
#删除课程
#删除课程只是将课程的is_deleted状态改为falseis_deleted为false状态的课程只有管理员可以看到
def destroy
@course.update_attributes(:is_delete => true)
@course = nil
redirect_to user_url(User.current)
end
# 恢复已删除的课程
def renew
if User.current.admin?
@course.update_attributes(:is_delete => false)
redirect_to course_path(@course)
else
return 404
end
end
#搜索作业
def homework_search
@search = "%#{params[:search].strip.downcase}%"
@is_teacher = User.current.logged? && (User.current.admin? || User.current.allowed_to?(:as_teacher,@course))
@page = params[:page] ? params[:page].to_i + 1 : 0
if @is_teacher
@homeworks = @course.homework_commons.where("name like '%#{@search}%'").order("created_at desc").limit(10).offset(@page * 10)
else
@homeworks = @course.homework_commons.where("name like '%#{@search}%' and publish_time <= '#{Date.today}'").order("created_at desc").limit(10).offset(@page * 10)
end
end
private
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 allow_join course
if course_endTime_timeout? course
respond_to do |format|
format.js {
@state = 2
render :partial => 'set_join',
:locals => {:user => User.current,
:course => Course.find(params[:object_id]),
:object_id => params[:object_id]
}
}
end
end
end
#验证是否显示课程
def can_show_course
@first_page = FirstPage.find_by_page_type('project')
if @first_page.try(:show_course) == 2
render_404
end
end
def student_homework_score(groupid,start_from, nums, score_sort_by)
start_from = start_from * nums
sql_select = ""
if groupid == 0
sql_select = "SELECT members.*,(
SELECT SUM(IF(student_works.final_score IS NULL,NULL,IF(student_works.final_score =0,0,student_works.final_score - student_works.absence_penalty - student_works.late_penalty)))
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
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 score #{score_sort_by}"
else
sql_select = "SELECT members.*,(
SELECT SUM(IF(student_works.final_score IS NULL,NULL,IF(student_works.final_score =0,0,student_works.final_score - student_works.absence_penalty - student_works.late_penalty)))
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
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 score #{score_sort_by}"
end
sql = ActiveRecord::Base.connection()
homework_scores = Member.find_by_sql(sql_select)
sql.close()
homework_scores
end
#获取课程的老师列表
def find_course_teachers course
searchTeacherAndAssistant(course).map{|teacher| teacher.user_id}.join(",")
end
#当加入,退出分班时查询分班的学生
def search_group_members group
@subPage_title = l :label_student_list
@render_file = 'new_member_list'
@canShowCode = isCourseTeacher(User.current.id,@course) && params[:role] != '1'
@is_remote = true
@score_sort_by = "desc"
page_from = params[:page].nil? ? 0 : (params[:page].to_i - 1)
@results = student_homework_score(group.id,0,0, "desc")
end
def member_to_xls homeworks, course, members,groups
xls_report = StringIO.new
book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet :name => "student"
blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10
#sheet1.row(0).default_format = blue
#sheet1.row(0).concat([l(:excel_user_id),l(:excel_user_name),l(:excel_nickname),l(:excel_student_id),l(:excel_mail),l(:excel_class),l(:excel_f_score),l(:excel_commit_time)])
sheet1[0,0] = "课程编号"
sheet1[0,1] = course.id
sheet1[1,0] = "课程学期"
sheet1[1,1] = course.time.to_s+""+course.term
sheet1[2,0] = "课程名称"
sheet1[2,1] = course.name
sheet1[3,0] = "教师团队"
sheet1[3,1] = (searchTeacherAndAssistant course).map{|member| member.user.show_name}.join('、')
sheet1[4,0] = "主讲教师"
sheet1[4,1] = course.teacher.show_name
sheet1[5,0] = "排名"
sheet1[5,1] = "学生姓名"
sheet1[5,2] = "昵称"
sheet1[5,3] = "学号"
for i in 0 ... homeworks.count
sheet1[5,i+4] = ""+(i+1).to_s+""
end
sheet1[5,homeworks.count+4] = "总成绩"
sheet1[5,0] = "排名"
sheet1[5,0] = "排名"
count_row = 6
members.each_with_index do |member, i|
sheet1[count_row,0]= i+1
sheet1[count_row,1] = member.user.lastname.to_s + member.user.firstname.to_s
sheet1[count_row,2] = member.user.login
sheet1[count_row,3] = member.user.user_extensions.student_id
homeworks.each_with_index do |homework, j|
student_works = homework.student_works.where("user_id = #{member.user.id}")
if student_works.empty?
sheet1[count_row,j+4] = format("%0.2f",0)
else
final_score = student_works.first.final_score.nil? ? 0 : student_works.first.final_score
score = final_score - student_works.first.absence_penalty - student_works.first.late_penalty
sheet1[count_row,j+4] = format("%0.2f",score <0 ? 0:score)
end
end
sheet1[count_row,homeworks.count+4] = format("%0.2f",member.score.nil? ? 0:member.score.to_s)
count_row += 1
end
=begin
group0 = CourseGroup.new();
group0.id = 0;
group0.name = l(:excel_member_with_out_class)
groups_dup = groups.dup
groups_dup << group0
groups_dup.each do |group|
sheet1[count_row,0] = l(:excel_class)
sheet1[count_row,1] = group.name
count_row += 1
members.each do |member|
if member.course_group_id == group.id
sheet1[count_row,0]= member.user.id
sheet1[count_row,1] = member.user.lastname.to_s + member.user.firstname.to_s
sheet1[count_row,2] = member.user.login
sheet1[count_row,3] = member.user.user_extensions.student_id
sheet1[count_row,4] = member.user.mail
sheet1[count_row,5] = format("%0.2f",member.score.nil? ? 0:member.score.to_s)
count_row += 1
end
end
end
=end
book.write xls_report
xls_report.string
end
end