socialforge/app/services/courses_service.rb

623 lines
28 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.

#coding=utf-8
class CoursesService
include ApplicationHelper
include CoursesHelper
include HomeworkAttachHelper
include ApiHelper
#参数school_id为0或不传时返回所有课程否则返回对应学校的课程
#参数per_page_count分页功能每页显示的课程数
#参数page分页功能当前页码
def course_list params,current_user
@school_id = params[:school_id]
per_page_option = params[:per_page_count] || 10
page_no = params[:page] || 1
if @school_id == "0" || @school_id.nil?
@courses_all = Course.active.visible.
joins("LEFT JOIN #{CourseStatus.table_name} ON #{Course.table_name}.id = #{CourseStatus.table_name}.course_id")
else
@courses_all = Course.active.visible.
joins("LEFT JOIN #{CourseStatus.table_name} ON #{Course.table_name}.id = #{CourseStatus.table_name}.course_id").
where("#{Course.table_name}.school_id = ?", @school_id)
end
@course_count = @courses_all.count
@course_pages = Redmine::Pagination::Paginator.new @course_count, per_page_option,page_no
@courses = @courses_all.order("created_at desc")
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
course_list = []
@courses.each do |course|
course_list << {:course => course,:img_url => url_to_avatar(course),:current_user_is_member => current_user.member_of_course?(course),:current_user_is_teacher => is_course_teacher(current_user,course)}
end
course_list
end
#搜索课程
def search_course params,current_user
courses_all = Course.all_course
name = params[:name]
if name.blank?
raise 'sumbit empty'
end
@courses = courses_all.visible(current_user)
if params[:name].present?
@courses_all = @courses.like(params[:name]).order("created_at desc")
else
@courses_all = @courses.order("created_at desc");
end
@courses_all
course_list = []
@courses_all.each do |course|
course_list << {:course => course,:img_url => url_to_avatar(course),:current_user_is_member => current_user.member_of_course?(course),:current_user_is_teacher => is_course_teacher(current_user,course)}
end
course_list
end
#获取头像
def get_img obj
url_to_avatar(obj)
end
#课程老师或课程学生列表
def course_teacher_or_student_list params,course,current_user
if course.is_a?(Course)
c = course
else
c = Course.find(course)
end
if current_user.nil? || !(current_user.admin? || c.is_public == 1 || (c.is_public == 0 && current_user.member_of_course?(c)))
raise '403'
end
@teachers= searchTeacherAndAssistant(c)
#@canShowCode = isCourseTeacher(User.current.id,course) && params[:role] != '1'
case params[:role]
when '1'
#@subPage_title = l :label_teacher_list
@members = searchTeacherAndAssistant(c)
when '2'
#@subPage_title = l :label_student_list
@members = searchStudent(c)
else
#@subPage_title = ''
@members = c.member_principals.includes(:roles, :principal).all.sort
end
users = []
@members.each do |m|
img_url = url_to_avatar(m.user)
gender = m.user.user_extensions.gender.nil? ? 0 : m.user.user_extensions.gender
work_unit = get_user_work_unit m.user
location = get_user_location m.user
users << {:id => m.user.id, :img_url => img_url, :nickname => m.user.login, :gender => gender, :work_unit => work_unit, :mail => m.user.mail, :location => location, :brief_introduction => m.user.user_extensions.brief_introduction,:realname=>m.user.realname}
end
users
end
#获取用户的工作单位
def get_user_work_unit user
work_unit = ""
if user.user_extensions.identity == 0 || user.user_extensions.identity == 1
work_unit = user.user_extensions.school.name unless user.user_extensions.school.nil?
elsif user.user_extensions.identity == 3
work_unit = user.user_extensions.occupation
elsif user.user_extensions.identity == 2
work_unit = user.firstname
end
work_unit
end
#获取用户地区
def get_user_location user
location = ""
location << (user.user_extensions.location || '')
location << (user.user_extensions.location_city || '')
location
end
#课程通知列表
def course_news_list params,current_user
if params[:course_id] && @course==nil
@course = Course.find(params[:course_id])
end
if current_user.nil? || !(current_user.admin? || @course.is_public == 1 || (@course.is_public == 0 && current_user.member_of_course?(@course)))
raise '403'
end
scope = @course ? @course.news.course_visible(current_user) : News.course_visible(current_user)
news = []
scope.each do |n|
news << {:id => n.id,:title => n.title,:author_name => n.author.name,:author_id => n.author.id, :description => n.description,:created_on => format_time(n.created_on),:comments_count => n.comments_count}
end
news
end
#查看新闻权限验证
def show_course_news_authorize(current_user)
unless current_user.allowed_to?({:controller => 'news', :action => 'show'}, false)
raise '403'
end
end
#显示课程通知(包括评论) 需验证权限
def show_course_news params,current_user
@news = News.find(params[:id])
@course = @news.course
if @course
if current_user.nil? || !(current_user.admin? || @course.is_public == 1 || (@course.is_public == 0 && current_user.member_of_course?(@course)))
raise '403'
end
end
@comments = @news.comments
@comments.reverse! if current_user.wants_comments_in_reverse_order?
{:news => @news,:comments => @comments}
#comments = []
#@comments.each do |comment|
# comments << {:author_id => comment.author_id,:author_name => comment.author.name,:commont_content => comment.comments,:time => format_time(comment.created_on)}
#end
#{:title => @news.title,:author_name => @news.author.name,:author_id => @news.author.id, :description => @news.description,:created_on => format_time(@news.created_on),
# :comments_count => @news.comments_count,:comments => comments}
end
#显示课程
def show_course(params,current_user)
course = Course.find(params[:id])
if course.school
work_unit = course.school.name
else
work_unit = get_user_work_unit course.teacher
end
unless (course.is_public == 1 || current_user.member_of_course?(course) || current_user.admin?)
raise '403'
end
{:course => course,:work_unit => work_unit, :img_url => url_to_avatar(course),:current_user_is_member => current_user.member_of_course?(course),:current_user_is_teacher => is_course_teacher(current_user,course),:course_student_num => course ? course.student.count.to_s : 0}
end
#创建课程
#current_user当前用户对象不是id
# params[:course][:name]:课程名称
#params[:course][:password]:密码
#params[:course][:description]:描述
#params[:course][:is_public]:是否公开1公开0私有
#params[:course][:open_student]:是否公开学生列表1公开0不公开不公开时非课程成员无法看到学生列表
#params[:course][:course_type]:暂时默认给1值。
#params[:term]:学期(秋季学期或春季学期)
#params[:time]: 年份2014
#params[:setup_time]:暂不传(貌似已经没用了)
#params[:endup_time]: 暂不传(貌似已经没用了)
#params[:class_period]:学时总数
def create_course(params,current_user)
if current_user.user_extensions.identity
@course = Course.new
@course.extra = 'course' + DateTime.parse(Time.now.to_s).strftime('%Y-%m-%d_%H-%M-%S').to_s
@course.send(:safe_attributes=, params[:course], current_user)
#@course.safe_attributes(current_user,params[:course])
@course.tea_id = current_user.id
@course.term = params[:term]
@course.time = params[:time]
#@course.school_id = params[:occupation]
@course.school_id = current_user.user_extensions.school_id
@course.setup_time = params[:setup_time]
@course.endup_time = params[:endup_time]
@course.class_period = params[:class_period].to_i
params[:course][:is_public] ? @course.is_public = 1 : @course.is_public = 0
params[:course][:open_student] ? @course.open_student = 1 : @course.open_student = 0
end
@issue_custom_fields = IssueCustomField.sorted.all
@trackers = Tracker.sorted.all
if @course.save
#unless User.current.admin?
r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
m = Member.new(:user => current_user, :roles => [r])
m.project_id = -1
course = CourseInfos.new(:user_id => current_user.id, :course_id => @course.id)
#user_grades = UserGrade.create(:user_id => User.current.id, :course_id => @course.id)
if params[:course][:is_public] == '1'
course_status = CourseStatus.create(:course_id => @course.id, :watchers_count => 0, :changesets_count => 0, :grade => 0, :course_type => @course_tag)
end
@course.members << m
@course.course_infos << course
end
{:course => @course,:img_url => url_to_avatar(@course),:current_user_is_member => current_user.member_of_course?(@course),:current_user_is_teacher => is_course_teacher(current_user,@course)}
end
#验证编辑课程的权限
#当前
def edit_course_authorize(current_user,course)
unless current_user.allowed_to?({:controller => 'courses', :action => 'update'}, course)
raise '403'
end
end
#编辑课程 需验证权限
# params[:course][:name]:课程名称
#params[:course][:password]:密码
#params[:course][:description]:描述
#params[:course][:is_public]:是否公开1公开0私有
#params[:course][:open_student]:是否公开学生列表1公开0不公开不公开时非课程成员无法看到学生列表
#params[:course][:course_type]:暂时默认给1值。
#params[:term]:学期(秋季学期或春季学期)
#params[:time]: 年份2014
#params[:class_period]:学时总数
def edit_course(params,course,current_user)
course.send(:safe_attributes=, params[:course], current_user)
#course.safe_attributes = params[:course]
course.time = params[:time]
course.term = params[:term]
course.class_period = params[:class_period].to_i
params[:course][:is_public] ? course.is_public = 1 : course.is_public = 0
params[:course][:open_student] ? course.open_student = 1 : course.open_student = 0
if course.save
if params[:course][:is_public] == '0'
course_status = CourseStatus.find_by_course_id(course.id)
course_status.destroy if course_status
elsif params[: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
end
{:course => course,:img_url => url_to_avatar(course),:current_user_is_member => current_user.member_of_course?(course),:current_user_is_teacher => is_course_teacher(current_user,course)}
end
#退出课程
#object_id: 课程id
#user当前用户
#@state == 0 退出成功
#@state == 1 不在课程中
#@state == 2 您还未登录
#@state 其他 未知错误,请稍后再试
def exit_course params,user
if user.nil?
@state = 2
return @state
end
@member = Member.where('course_id = ? and user_id = ?', params[:object_id], user.id)
if @member.nil? || @member.count == 0
@state = 1
return @state
end
@member.first.destroy
joined = StudentsForCourse.where('student_id = ? and course_id = ?', user.id, params[:object_id])
joined.each do |join|
join.delete
@state = 0
end
@state
end
#加入课程
#object_id课程id
#course_password :加入课程的密码
#@state == 0 加入成功
#@state == 1 密码错误
#@state == 2 课程已过期 请联系课程管理员重启课程。(在配置课程处)
#@state == 3 您已经加入了课程
#@state == 4 您加入的课程不存在
#@state == 5 您还未登录
#@state 其他 未知错误,请稍后再试
def join_course params,current_user
course = Course.find_by_id params[:object_id]
@state = 10
if course
if course_endTime_timeout? course
@state = 2
else
if current_user.member_of_course?(course)
@state = 3
else
if params[:course_password] == course.password
members = []
members << Member.new(:role_ids => [10], :user_id => current_user.id)
course.members << members
StudentsForCourse.create(:student_id => current_user.id, :course_id => params[:object_id])
@state = 0
else
@state = 1
end
end
end
else
@state = 4
end
{:state => @state,:course => course}
end
#作业列表
#已提交的作业数量获取 bid.homeworks.count
#学生提问数量获取 bid.commit.nil? ? 0 : bid.commit
def homework_list params,current_user
course = Course.find(params[:id])
if course.is_public != 0 || current_user.member_of_course?(course)
bids = course.homework_commons.order('end_time DESC')
bids = bids.like(params[:name]) if params[:name].present?
homeworks = []
bids.each do |bid|
homeworks << show_homework_info(course,bid,current_user,is_course_teacher(current_user,course))
end
homeworks
else
raise '403'
end
end
def course_dynamic(params,current_user)
@user = User.find(params[:id])
if current_user.nil? && !current_user.admin? && !@user.active?
raise '404'
return
end
if current_user == @user || current_user.admin?
membership = @user.coursememberships.all
else
membership = @user.coursememberships.all(:conditions => Course.visible_condition(current_user))
end
if membership.nil? || membership.count == 0
raise l(:label_no_courses,:locale => get_user_language(current_user))
end
membership.sort! {|older, newer| newer.created_on <=> older.created_on }
result = []
membership.each do |mp|
course = mp.course
latest_course_dynamics = []
latest_news = course.news.order("created_on desc").first
unless latest_news.nil?
latest_course_dynamics << {:type => 1,:time => latest_news.created_on,
:message =>latest_news.author.realname<< l(:label_recently_updated_notification,:locale => get_user_language(current_user))<<""<< latest_news.title }
end
latest_message = course.journals_for_messages.order("created_on desc").first
unless latest_message.nil?
latest_course_dynamics << {:type => 2,:time => latest_message.created_on,:message =>latest_message.user.realname << l(:label_recently_updated_message,:locale => get_user_language(current_user))<<""<<latest_message.notes}
end
latest_attachment = course.attachments.order("created_on desc").first
unless latest_attachment.nil?
latest_course_dynamics << {:type => 3,:time => latest_attachment.created_on,:message =>latest_attachment.author.realname<< l(:label_recently_updated_courseware,:locale => get_user_language(current_user))<<""<<latest_attachment.filename}
end
latest_bid = course.homework_commons.order('updated_at DESC').first
unless latest_bid.nil?
latest_course_dynamics << {:type => 4,:time => latest_bid.updated_at,:message => latest_bid.user.realname<<l(:label_recently_updated_homework,:locale => get_user_language(current_user))<<""<<latest_bid.name}
end
# Time 2015-04-07 14:58:30
# Author lizanle
# Description 添加课程创建动态
if(User.find_by_id(CourseInfos.find_by_course_id(course.id).try(:user_id)))
create_user_name = User.find_by_id(CourseInfos.find_by_course_id(course.id).user_id).realname
latest_course_dynamics << {:type => 5,:time => course.created_at,:message =>l(:label_recently,:locale => get_user_language(current_user)) << create_user_name << l(:label_creat,:locale => get_user_language(current_user))}
end
#每个作业中的最新留言
# messages = []
# course.homework_commons.each do |hc|
# jour = hc.journals_for_messages.order("created_on desc").first
# unless jour.nil?
# messages << jour
# end
# end
# unless messages.count == 0
# messages.sort!{|order,newer| newer.created_on <=> order.created_on}
# end
# latest_bid_message = messages.first
# unless latest_bid_message.nil?
# latest_course_dynamics << {:type => 4,:time => latest_bid_message.created_on,:message => l(:label_recently_updated_message,:locale => get_user_language(current_user))}
# end
#每个作业中学生最后提交的作业
homeworks = []
course.homework_commons.each do |bid|
homework_attach = bid.student_works.order('updated_at DESC').first
unless homework_attach.nil?
homeworks << homework_attach
end
end
unless homeworks.count == 0
homeworks.sort!{|order,newer| newer.updated_at <=> order.updated_at}
end
latest_homework_attach = homeworks.first
unless latest_homework_attach.nil?
latest_course_dynamics << {:type => 4,:time => latest_homework_attach.updated_at,:message =>latest_homework_attach.user.realname<< l(:label_recently_updated_homework,:locale => get_user_language(current_user))<<""<<(latest_homework_attach.name.nil? ? latest_homework_attach.description : latest_homework_attach.name)}
end
latest_course_dynamics.sort!{|order,newer| newer[:time] <=> order[:time]}
latest_course_dynamic = latest_course_dynamics.first
unless latest_course_dynamic.nil?
result << {:course_name => course.name,:course_id => course.id,:course_img_url => url_to_avatar(course),:course_time => course.time,:course_term => course.term,:type => latest_course_dynamic[:type],:update_time => latest_course_dynamic[:time],:message => latest_course_dynamic[:message],:count => nil}
end
end
result.sort!{|order,newer| newer[:update_time] <=> order[:update_time]}
result
end
# 课程课件
def course_attachments params
result = []
@course = Course.find(params[:course_id])
@attachments = @course.attachments.order("created_on desc")
if !params[:name].nil? && params[:name] != ""
@attachments.each do |atta|
result << {:filename => atta.filename,
:description => atta.description,
:downloads => atta.downloads,
:quotes => atta.quotes.nil? ? 0 :atta.quotes } if atta.filename.include?(params[:name])
end
else
@attachments.each do |atta|
result << {:filename => atta.filename,
:description => atta.description,
:downloads => atta.downloads,
:quotes => atta.quotes.nil? ? 0 :atta.quotes }
end
end
result
end
# 课程学生列表
def course_members params
@all_members = searchmember_by_name(student_homework_score(0,params[:course_id], 10,"desc"),params[:name])
end
def show_member_score params
@member_score = Member.find(params[:member_id]) if params[:member_id]
atta = @member_score.student_homework_score[0]
result = []
atta.each do |t|
if !params[:homeworkName].nil? && params[:homeworkName] != ""
result << {:name=>t[:name],:score=>t[:score]} if t[:name].include?(params[:homeworkName])
else
result << {:name=>t[:name],:score=>t[:score]}
end
end
result
end
# 设置人员为课程教辅
def set_as_assitant_teacher params
members = []
#找到课程
course = Course.find(params[:course_id])
#新建课程人员
member = Member.new(:role_ids =>[7], :user_id => params[:user_id],:course_id=>params[:course_id])
joined = StudentsForCourse.where('student_id = ? and course_id = ?', member.user_id,course.id)
joined.each do |join|
join.delete
end
member.course_group_id = 0
members << member
course.members << members
#将课程人员设置为教辅
end
def del_assitant_teacher params
member = Member.where("user_id = ? and course_id = ?",params[:user_id],params[:course_id])
member.each do |m|
m.destroy
end
user_admin = CourseInfos.where("user_id = ? and course_id = ?",params[:user_id], params[:course_id])
if user_admin.size > 0
user_admin.each do |user|
user.destroy
end
end
joined = StudentsForCourse.where('student_id = ? and course_id = ?', params[:user_id],params[:course_id])
joined.each do |join|
join.delete
end
end
def create_course_notice params ,current_user
n = News.new(:course_id =>params[:course_id], :author_id => current_user.id,:title =>params[:title],:description=> params[:desc])
n.save
{:id => n.id,:title => n.title,:author_name => n.author.name,:author_id => n.author.id, :description => n.description,:created_on => format_time(n.created_on),:comments_count => n.comments_count}
end
private
def searchmember_by_name members, name
#searchPeopleByRoles(project, StudentRoles)
mems = []
if name != ""
name = name.to_s.downcase
members.each do |m|
username = m.user[:lastname].to_s.downcase + m.user[:firstname].to_s.downcase
if(m.user[:login].to_s.downcase.include?(name) || m.user.user_extensions[:student_id].to_s.downcase.include?(name) || username.include?(name))
mems << m
end
end
else
mems = members
end
mems
end
def show_homework_info course,bid,current_user,is_course_teacher
author_real_name = bid.user.lastname + bid.user.firstname
many_times = course.homework_commons.index(bid) + 1
name = bid.name
homework_count = bid.student_works.count #已提交的作业数量
#student_questions_count = bid.journals_for_messages.where('m_parent_id IS NULL').count
description = bid.description
#if is_course_teacher(User.current, course) && @bid.open_anonymous_evaluation == 1 && @bid.homeworks.count >= 2
state = bid.homework_detail_manual.comment_status
unless is_course_teacher
homework_for_anonymous_comments = get_student_batch_homework_list bid,current_user
end
#end
open_anonymous_evaluation = bid.homework_detail_manual.comment_status
{:course_name => course.name,:course_id => course.id,:id => bid.id, :author => bid.user,:author_real_name => author_real_name, :homework_times => many_times, :homework_name => name, :homework_count => homework_count,:student_questions_count => 0,
:description => description, :homework_state => state,:open_anonymous_evaluation => open_anonymous_evaluation,:homework_for_anonymous_comments => homework_for_anonymous_comments,:created_on => bid.created_at,:deadline => bid.end_time}
end
#显示作业列表的同时显示分配给当前学生匿评的作业
def show_homework_info_with_batch course,bid
author = bid.author.lastname + bid.author.firstname
many_times = course.homeworks.index(bid) + 1
name = bid.name
homework_count = bid.homeworks.count #已提交的作业数量
student_questions_count = bid.journals_for_messages.where('m_parent_id IS NULL').count
description = bid.description
#if is_course_teacher(User.current, course) && @bid.open_anonymous_evaluation == 1 && @bid.homeworks.count >= 2
state = bid.comment_status
#end
open_anonymous_evaluation = bid.open_anonymous_evaluation
{:course_name => course.name,:id => bid.id, :course_teacher => author, :homework_times => many_times, :homework_name => name, :homework_count => homework_count,:student_questions_count => student_questions_count,
:description => description, :homework_state => state,:open_anonymous_evaluation => open_anonymous_evaluation}
end
def student_homework_score(groupid,course_id, nums, score_sort_by)
#teachers = find_course_teachers(@course)
#start_from = start_from * nums
sql_select = ""
if groupid == 0
if nums == 0
sql_select = "SELECT members.*, SUM(homework_attaches.score) as score FROM members, homework_attaches
WHERE members.course_id = #{course_id} AND members.user_id in (SELECT students_for_courses.student_id FROM students_for_courses WHERE course_id = #{course_id}) AND members.user_id = homework_attaches.user_id
AND homework_attaches.bid_id in (SELECT bid_id FROM homework_for_courses WHERE course_id = #{course_id}) GROUP BY members.user_id
UNION all
SELECT members.*, 0 as score FROM members,homework_attaches,students_for_courses WHERE members.course_id = #{course_id} AND
students_for_courses.course_id = #{course_id} and members.user_id = students_for_courses.student_id AND
members.user_id NOT IN (SELECT homework_attaches.user_id FROM homework_attaches WHERE homework_attaches.bid_id in (SELECT bid_id FROM homework_for_courses WHERE course_id = #{course_id} )
)
GROUP BY members.user_id ORDER BY score #{score_sort_by}"
else
sql_select = "SELECT members.*, SUM(homework_attaches.score) as score FROM members, homework_attaches
WHERE members.course_id = #{course_id} AND members.user_id in (SELECT students_for_courses.student_id FROM students_for_courses WHERE course_id = #{course_id}) AND members.user_id = homework_attaches.user_id
AND homework_attaches.bid_id in (SELECT bid_id FROM homework_for_courses WHERE course_id = #{course_id}) GROUP BY members.user_id
UNION all
SELECT members.*, 0 as score FROM members,homework_attaches,students_for_courses WHERE members.course_id = #{course_id} AND
students_for_courses.course_id = #{course_id} and members.user_id = students_for_courses.student_id AND
members.user_id NOT IN (SELECT homework_attaches.user_id FROM homework_attaches WHERE homework_attaches.bid_id in (SELECT bid_id FROM homework_for_courses WHERE course_id = #{course_id} )
)
GROUP BY members.user_id ORDER BY score #{score_sort_by} " #limit #{start_from}, #{nums}"
end
else
sql_select = "SELECT members.*, SUM(homework_attaches.score) as score FROM members, homework_attaches
WHERE members.course_id = #{course_id} AND members.user_id in (SELECT students_for_courses.student_id FROM students_for_courses WHERE course_id = #{course_id}) AND members.user_id = homework_attaches.user_id
and members.course_group_id = #{groupid} AND homework_attaches.bid_id in (SELECT bid_id FROM homework_for_courses WHERE course_id = #{course_id})
GROUP BY members.user_id
UNION all
SELECT members.*, 0 as score FROM members,homework_attaches,students_for_courses WHERE members.course_id = #{course_id}
and members.course_group_id = #{groupid} AND
students_for_courses.course_id = #{course_id} and members.user_id = students_for_courses.student_id AND
members.user_id NOT IN (SELECT homework_attaches.user_id FROM homework_attaches WHERE homework_attaches.bid_id in (SELECT bid_id FROM homework_for_courses WHERE course_id = #{course_id} )
)
GROUP BY members.user_id ORDER BY score #{score_sort_by}"
end
sql = ActiveRecord::Base.connection()
homework_scores = Member.find_by_sql(sql_select)
sql.close()
homework_scores
end
end