diff --git a/app/services/homework_service.rb b/app/services/homework_service.rb new file mode 100644 index 000000000..f40c4f0ef --- /dev/null +++ b/app/services/homework_service.rb @@ -0,0 +1,63 @@ +class HomeworkService + + #作业详情(老师才显示启动匿评,学生不显示 ) + #bid.comment_status=0 启动匿评 + #bid.comment_status=1 关闭匿评 + #many_times 第几次(作业) + #is_teacher 判断是否为该课程老师 + def show_homework params + @bid = Bid.find(params[:id]) + course = @bid.courses.first + is_teacher = is_course_teacher(User.current,course) + author = @bid.author.firstname + @bid.author.lastname + many_times = course.homeworks.index(@bid) + 1 + name = @bid.name + homework_count = @bid.homeworks.count + description = @bid.description + if is_teacher && bid.open_anonymous_evaluation == 1 && bid.homeworks.count >= 2 + case bid.comment_status + when 0 + alert_anonymous_comment_bid_path(bid) + when 1 + alert_anonymous_comment_bid_path(bid) + when 2 + raise '匿评结束' + end + end + end + + #启动作业匿评 + def alert_homework_anonymous_comment params + @bid = Bid.find params[:id] + @course = @bid.courses.first + if @bid.comment_status == 0 + @totle_size = searchStudent(@course).size + @cur_size = @bid.homeworks.size + elsif @bid.comment_status == 1 + @totle_size = 0 + @bid.homeworks.map { |homework| @totle_size += homework.homework_evaluations.count} + teachers = "(" + teacher_members = searchTeacherAndAssistant(@course) + teacher_members.each do |member| + if member == teacher_members.last + teachers += member.user_id.to_s + ")" + else + teachers += member.user_id.to_s + "," + end + end + @cur_size = 0 + @bid.homeworks.map { |homework| @cur_size += homework.rates(:quality).where("seems_rateable_rates.rater_id not in #{teachers}").count} + end + @percent = format("%.2f",(@cur_size.to_f / ( @totle_size == 0 ? 1 : @totle_size)) * 100) + end + + #匿评作品详情 + def anonymous_works_show params + @homework = HomeworkAttach.find(params[:id]) + if User.current.admin? || User.current.member_of_course?(@homework.bid.courses.first) + + end + end + + # +end \ No newline at end of file diff --git a/app/services/users_service.rb b/app/services/users_service.rb index 928ae99ad..9fb698056 100644 --- a/app/services/users_service.rb +++ b/app/services/users_service.rb @@ -1,142 +1,158 @@ -class UsersService - include ApplicationHelper - include AccountHelper - include AvatarHelper - #将用户注册的功能函数写这里 - #参数约定 - #成功返回注册后的User实例,失败直接抛异常 - - def register(params) - @user = User.new - @user.admin = false - @user.register - @user.login = params[:login] - @user.mail = params[:mail] - password = params[:password] - password_confirmation = params[:password_confirmation] - should_confirmation_password = params[:should_confirmation_password] - if !password.blank? && !password_confirmation.blank? && should_confirmation_password - @user.password, @user.password_confirmation = password, password_confirmation - elsif !password.blank? && !should_confirmation_password - @user.password = password - else - @user.password = "" - end - case Setting.self_registration - when '1' - @user = email_activation_register(@user) - when '3' - @user = automatically_register(@user) - else - @user = administrator_manually__register(@user) - end - if @user.id != nil - ue = @user.user_extensions ||= UserExtensions.new - ue.user_id = @user.id - ue.save - end - @user - end - - #显示用户 - #id用户id - def show_user(params) - @user = User.find(params[:id]) - img_url = url_to_avatar(@user) - gender = @user.user_extensions.gender.nil? ? 0 : @user.user_extensions.gender - 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 - location = "" - location << (@user.user_extensions.location || '') - location << (@user.user_extensions.location_city || '') - {:id => @user.id, :img_url => img_url, :nickname => @user.login, :gender => gender, :work_unit => work_unit, :mail => @user.mail, :location => location, :brief_introduction => @user.user_extensions.brief_introduction} - end - - #编辑用户 - #gender 1:female 0:male 其他:male - def edit_user params - @user = User.find(params[:id]) - fileio = params[:file] - - @se = @user.extensions - if @user.user_extensions.identity == 0 || @user.user_extensions.identity == 1 - @se.school_id = params[:occupation] - elsif @user.user_extensions.identity == 3 - @se.occupation = params[:occupation] - elsif @user.user_extensions.identity == 2 - @user.firstname = params[:occupation] - end - @se.brief_introduction = params[:brief_introduction] - @se.gender = params[:gender] - @se.location = params[:province] if params[:province] - @se.location_city = params[:city] if params[:city] - raise @se.errors.full_message unless @se.save - unless fileio.nil? - file = fileio[:tempfile] - diskfile=disk_filename(@user.class.to_s, @user.id) - @image_file = fileio[:name] - @urlfile='/' << File.join("images", "avatars", avatar_directory(@user.class.to_s), avatar_filename(@user.id, @image_file)) - - path = File.dirname(diskfile) - unless File.directory?(path) - FileUtils.mkdir_p(path) - end - File.rename(file.path, @urlfile) - begin - f = Magick::ImageList.new(diskfile) - # gif格式不再做大小处理 - if f.format != 'GIF' - width = 300.0 - proportion = (width/f[0].columns) - height = (f[0].rows*proportion) - f.resize_to_fill!(width, height) - f.write(diskfile) - end - - rescue Exception => e - logger.error "[Error] avatar : users_service#edit_user ===> #{e}" - end - end - @se - end - - #关注列表 - def user_watcher params - @user = User.find(params[:id]) - User.watched_by(@user.id) - end - - #修改密码 - def change_password params - @current_user = User.find(params[:current_user_id]) - if @current_user.check_password?(params[:password]) - @current_user.password, @current_user.password_confirmation = params[:new_password], params[:new_password_confirmation] - @current_user.save - #raise @current_user.errors.full_message - #return @current_user - - else - raise 'wrong password' - end - @current_user - end - - #搜索用户 - def search_user params - @status = params[:status] || 1 - has = { - "show_changesets" => true - } - scope = User.logged.status(@status) - @search_by = params[:search_by] ? params[:search_by][:id] : 0 - scope = scope.like(params[:name],@search_by) if params[:name].present? - scope - end - -end +class UsersService + include ApplicationHelper + include AccountHelper + include AvatarHelper + include CoursesHelper + #将用户注册的功能函数写这里 + #参数约定 + #成功返回注册后的User实例,失败直接抛异常 + + def register(params) + @user = User.new + @user.admin = false + @user.register + @user.login = params[:login] + @user.mail = params[:mail] + password = params[:password] + password_confirmation = params[:password_confirmation] + should_confirmation_password = params[:should_confirmation_password] + if !password.blank? && !password_confirmation.blank? && should_confirmation_password + @user.password, @user.password_confirmation = password, password_confirmation + elsif !password.blank? && !should_confirmation_password + @user.password = password + else + @user.password = "" + end + case Setting.self_registration + when '1' + @user = email_activation_register(@user) + when '3' + @user = automatically_register(@user) + else + @user = administrator_manually__register(@user) + end + if @user.id != nil + ue = @user.user_extensions ||= UserExtensions.new + ue.user_id = @user.id + ue.save + end + @user + end + + #显示用户 + #id用户id + def show_user(params) + @user = User.find(params[:id]) + img_url = url_to_avatar(@user) + gender = @user.user_extensions.gender.nil? ? 0 : @user.user_extensions.gender + 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 + location = "" + location << (@user.user_extensions.location || '') + location << (@user.user_extensions.location_city || '') + {:id => @user.id, :img_url => img_url, :nickname => @user.login, :gender => gender, :work_unit => work_unit, :mail => @user.mail, :location => location, :brief_introduction => @user.user_extensions.brief_introduction} + end + + #编辑用户 + #gender 1:female 0:male 其他:male + def edit_user params + @user = User.find(params[:id]) + fileio = params[:file] + + @se = @user.extensions + if @user.user_extensions.identity == 0 || @user.user_extensions.identity == 1 + @se.school_id = params[:occupation] + elsif @user.user_extensions.identity == 3 + @se.occupation = params[:occupation] + elsif @user.user_extensions.identity == 2 + @user.firstname = params[:occupation] + end + @se.brief_introduction = params[:brief_introduction] + @se.gender = params[:gender] + @se.location = params[:province] if params[:province] + @se.location_city = params[:city] if params[:city] + raise @se.errors.full_message unless @se.save + unless fileio.nil? + file = fileio[:tempfile] + diskfile=disk_filename(@user.class.to_s, @user.id) + @image_file = fileio[:name] + @urlfile='/' << File.join("images", "avatars", avatar_directory(@user.class.to_s), avatar_filename(@user.id, @image_file)) + + path = File.dirname(diskfile) + unless File.directory?(path) + FileUtils.mkdir_p(path) + end + File.rename(file.path, @urlfile) + begin + f = Magick::ImageList.new(diskfile) + # gif格式不再做大小处理 + if f.format != 'GIF' + width = 300.0 + proportion = (width/f[0].columns) + height = (f[0].rows*proportion) + f.resize_to_fill!(width, height) + f.write(diskfile) + end + + rescue Exception => e + logger.error "[Error] avatar : users_service#edit_user ===> #{e}" + end + end + @se + end + + #关注列表 + def user_watcher params + @user = User.find(params[:id]) + User.watched_by(@user.id) + end + + #用户课程列表 + def user_courses_list params + if !User.current.admin? && !@user.active? + raise '404' + return + end + if User.current == @user || User.current.admin? + membership = @user.coursememberships.all + else + membership = @user.coursememberships.all(:conditions => Course.visible_condition(User.current)) + end + membership.sort! {|older, newer| newer.created_on <=> older.created_on } + membership + end + + #修改密码 + def change_password params + @current_user = User.find(params[:current_user_id]) + if @current_user.check_password?(params[:password]) + @current_user.password, @current_user.password_confirmation = params[:new_password], params[:new_password_confirmation] + @current_user.save + #raise @current_user.errors.full_message + #return @current_user + + else + raise 'wrong password' + end + @current_user + end + + #搜索用户 + def search_user params + @status = params[:status] || 1 + has = { + "show_changesets" => true + } + scope = User.logged.status(@status) + @search_by = params[:search_by] ? params[:search_by][:id] : 0 + scope = scope.like(params[:name],@search_by) if params[:name].present? + scope + end + +end