((.|\s)*?)}m, '[...]') - @content = "> #{ll(User.current.language, :text_user_wrote, user)}\n> " - @content << text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" - @id = user.id - rescue ActiveRecord::RecordNotFound - render_404 - end - - ##新建需求 - def new_bid - @bid = Bid.new - @bid.safe_attributes = params[:bid] - end - - #huang - def create_contest - @bid = Bid.new - @bid.name = params[:bid][:name] - @bid.description = params[:bid][:description] - @bid.reward_type = 2 - @bid.budget = params[:bid][:budget] - @bid.deadline = params[:bid][:deadline] - @bid.password = params[:bid][:password] #added by bai - @bid.author_id = User.current.id - @bid.commit = 0 - if @bid.save - unless @bid.watched_by?(User.current) - if @bid.add_watcher(User.current) - flash[:notice] = l(:label_bid_succeed) - end - end - redirect_to respond_url(@bid) - else - @bid.safe_attributes = params[:bid] - render :action => 'new_bid' - end - end - -# added by bai - def update_contest - @bid = Bid.find(params[:id]) - @bid.name = params[:bid][:name] - @bid.description = params[:bid][:description] - @bid.reward_type = 2 - @bid.budget = params[:bid][:budget] - @bid.deadline = params[:bid][:deadline] - @bid.password = params[:bid][:password] - @bid.author_id = User.current.id - @bid.commit = 0 - if @bid.save - unless @bid.watched_by?(User.current) - if @bid.add_watcher(User.current) - flash[:notice] = l(:label_bid_succeed) - end - end - redirect_to respond_url(@bid) - else - @bid.safe_attributes = params[:bid] - render :action => 'new_bid' - end - end - #huang - def new_contest - @bid = Bid.new - @bid.safe_attributes = params[:bid] - end - - def create_bid - @bid = Bid.new - @bid.name = params[:bid][:name] - @bid.description = params[:bid][:description] - @bid.reward_type = 1 - @bid.budget = params[:bid][:budget] - @bid.deadline = params[:bid][:deadline] - @bid.author_id = User.current.id - @bid.commit = 0 - if @bid.save - unless @bid.watched_by?(User.current) - if @bid.add_watcher(User.current) - flash[:notice] = l(:label_bid_succeed) - end - end - redirect_to respond_url(@bid) - else - @bid.safe_attributes = params[:bid] - render :action => 'new_bid' - end - end - - def create_homework - @bid = Bid.new - @bid.name = params[:bid][:name] - @bid.description = params[:bid][:description] - @bid.is_evaluation = params[:bid][:is_evaluation] - @bid.proportion = params[:bid][:proportion] - @bid.evaluation_num = params[:bid][:evaluation_num] - @bid.open_anonymous_evaluation = params[:bid][:open_anonymous_evaluation] - @bid.reward_type = 3 - # @bid.budget = params[:bid][:budget] - @bid.deadline = params[:bid][:deadline] - @bid.budget = 0 - @bid.author_id = User.current.id - @bid.commit = 0 - @bid.homework_type = 1 - @bid.save_attachments(params[:attachments] || (params[:bid] && params[:bid][:uploads])) - # @bid. - if @bid.save - HomeworkForCourse.create(:course_id => params[:course_id], :bid_id => @bid.id) - unless @bid.watched_by?(User.current) - if @bid.add_watcher(User.current) - flash[:notice] = l(:label_bid_succeed) - end - end - redirect_to course_homework_url(params[:course_id]) - else - @bid.safe_attributes = params[:bid] - @homework = @bid - @course = Course.find_by_id(params[:course_id]) - @course_id = @course.id - #respond_to do |format| - # format.html { redirect_to new_homework_course_path(params[:course_id]),:layout => 'base_courses'} - # format.api { render_validation_errors(@bid) } - #end - render file: 'courses/new_homework', layout: 'base_courses' - end - end - - # modify by nwb\ - # 编辑作业 - def edit - @bid = Bid.find(params[:bid_id]) - if (User.current.admin?||User.current.allowed_to?(:as_teacher,@bid.courses.first)) - @course_id = params[:course_id] - respond_to do |format| - format.html { - @course = Course.find(params[:course_id]) - @user= User.find(User.current.id) - render :layout => 'base_courses' - } - end - else - render_403 - end - end - - def update - @bid = Bid.find(params[:id]) - @course = @bid.courses.first#Project.find(params[:course_id]) - @bid.name = params[:bid][:name] - @bid.description = params[:bid][:description] - @bid.is_evaluation = params[:bid][:is_evaluation] - @bid.proportion = params[:bid][:proportion] - @bid.evaluation_num = params[:bid][:evaluation_num] - @bid.open_anonymous_evaluation = params[:bid][:open_anonymous_evaluation] - @bid.reward_type = 3 - @bid.deadline = params[:bid][:deadline] - @bid.budget = 0 - #@bid.author_id = User.current.id - @bid.commit = 0 - @bid.homework_type = 1 - @bid.save_attachments(params[:attachments] || (params[:bid] && params[:bid][:uploads])) - if @bid.save - flash[:notice] = l(:label_update_homework_succeed) - redirect_to course_homework_url(@course) - else - @bid.safe_attributes = params[:bid] - render :action => 'edit', :layout =>'base_courses' - end - end - - def new_submit_homework - #render html to prepare create submit homework - find_bid - find_bid - render :layout => 'base_homework' - end - - def add_homework - if User.current.logged? && (!Member.where('user_id = ? and project_id = ?', User.current.id, @bid.courses.first.id).first.nil? && (Member.where('user_id = ? and project_id = ?', User.current.id, @bid.courses.first.id).first.roles&Role.where('id = ? or id = ? or id =?',5, 10, 7)).size >0) - # homework = HomeworkAttach.create(:bid_id => @bid.id, :user_id => User.current.id) - # homework.save_attachments(params[:attachments] || (params[:bid] && params[:bid][:uploads])) - - if hadcommittedhomework(User.current.id, @bid.id) == true - @homework_flag = l(:label_bidding_homework_committed) - else - @homework = HomeworkAttach.new - @homework.safe_attributes = params[:homeworkattach] - @homework.bid_id = @bid.id - @homework.user_id = User.current.id - @homework.save_attachments(params[:attachments]) - - render_attachment_warning_if_needed(@homework) - - @homework_flag = if @homework.save - l(:label_bidding_homework_succeed) - else - l(:label_bidding_homework_failed) - end - - if @homework.attachments.empty? - @homework.delete - #flash[:error] = l(:no_attachmens_allowed) - @homework_flag = l(:no_attachmens_allowed) - # else - end - end - end - - @homework_list = @bid.homeworks - respond_to do |format| - format.html{ - #redirect_to project_for_bid_path, notice: @homework_flag.to_s - flash[:notice] = @homework_flag.to_s - redirect_back_or_default(project_for_bid_path) - } - format.js - end - - end - - # 作业统计 - def homework_statistics - @course = @bid.courses.first - @member = [] - @course.memberships.each do |member| - unless (member.roles && Role.where('id = ? ', 3)).empty? - @member.push member - end - end - if @bid.homework_type = 1 - @student = User.where("id in (select DISTINCT user_id from #{HomeworkAttach.table_name} where bid_id = ? )", @bid.id) - @homework_type = true - else - - @homework_type = false - end - @user = @bid.author - render :layout => 'base_homework' - end - - def homework_respond - @user = @bid.author - render :layout => 'base_homework' - end - - def more - @jour = @bid.journals_for_messages - @jour.each_with_index {|j,i| j.indice = i+1} - @state = true - - respond_to do |format| - format.html { redirect_to :back } - format.js - #format.api { render_api_ok } - end - end - - def back - @jour = @bid.journals_for_messages - @jour.each_with_index {|j,i| j.indice = i+1} - @state = false - - respond_to do |format| - format.html { redirect_to :back } - format.js - #format.api { render_api_ok } - end - end - - #added by william - #used to set the bidding project reward - def set_reward - @b_p = nil - @biding_project_id = nil - - if params[:set_reward][:reward]&&((User.current.id==@bid.author_id)||User.current.admin) - # @bid_id = params[:id] - @biding_project_id = params[:set_reward][:b_id] - @b_p = BidingProject.find_by_id(@biding_project_id) - - # 把字段存进表中 - @b_p.update_reward(params[:set_reward][:reward].to_s) - end - - respond_to do |format| - format.js - end - end - - # added by william - # used to manage the bid and end the bid - def manage - - end - - # 启动匿评 - def start_anonymous_comment - @bid = Bid.find(params[:id]) - @course = @bid.courses.first - if(@bid.comment_status == 0) - homeworks = @bid.homeworks - if(homeworks && homeworks.size >= 2) - homeworks.each_with_index do |homework, index| - user = homework.user - n = @bid.evaluation_num - n = n < homeworks.size ? n : homeworks.size - 1 - assigned_homeworks = get_assigned_homeworks(homeworks, n, index) - assigned_homeworks.each do |h| - @homework_evaluation = HomeworkEvaluation.new(user_id: user.id, homework_attach_id: h.id) - @homework_evaluation.save - end - end - @bid.update_column('comment_status', 1) - @statue = 1 - else - @statue = 2 - end - else - @statue = 3 - end - - respond_to do |format| - format.js - end - end - - def stop_anonymous_comment - @bid = Bid.find(params[:id]) - - @bid.update_column('comment_status', 2) - - respond_to do |format| - format.js - end - end - - def alert_anonymous_comment - @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} - @cur_size = 0 - @bid.homeworks.map { |homework| @cur_size += homework.rates(:quality).where("seems_rateable_rates.is_teacher_score = 0").count} - end - @percent = format("%.2f",(@cur_size.to_f / ( @totle_size == 0 ? 1 : @totle_size)) * 100) - respond_to do |format| - format.js - end - end - - private - - def get_assigned_homeworks(homeworks, n, index) - homeworks += homeworks - homeworks[index + 1 .. index + n] - end - - def find_bid - if params[:id] - @bid = Bid.find(params[:id], :include => [{:homeworks => :user}]) - @user = @bid.author - end - rescue - render_404 - end - - def memberAccess - # 是课程,则判断当前用户是否参加了课程 - return true if current_user.admin? - #return 0 if @bid.courses.first.project_type == Project::ProjectType_project - currentUser = User.current - render_403 unless currentUser.member_of_course?(@bid.courses.first) - end - - #验证是否显示课程 - def can_show_course - @first_page = FirstPage.find_by_page_type('project') - if @first_page.show_course == 2 - render_404 - end - end - - #验证是否显示竞赛 - def can_show_contest - @first_page = FirstPage.find_by_page_type('project') - if @first_page.show_contest == 2 - render_404 - end - end -end - +# # fq +#Bid功能已经废弃,代码参考用 +# class BidsController < ApplicationController +# #Added by young +# menu_item l(:label_homework), :only => [:edit, :udpate] +# menu_item :respond +# menu_item :course, :only => :show_courseEx +# menu_item :project, :only => [:show_project,:show_results, :new_submit_homework] +# menu_item :homework_respond, :only => :homework_respond +# menu_item :homework_statistics, :only => :homework_statistics +# menu_item :edit, :only => :edit +# +# before_filter :auth_login1, :only => [:show_courseEx] +# +# before_filter :can_show_course,only: [] +# before_filter :can_show_contest,only: [] +# #Ended by young +# before_filter :find_bid, :only => [:show, :show_project, :create,:destroy,:more,:back,:add,:delete,:new,:show_results,:set_reward, :add_homework, :fork, :create_fork, +# :show_course, :show_courseEx,:show_bid_project, :show_bid_user, :join_in_contest, :unjoin_in_contest, :new_join,:show_participator, :settings] +# # added by fq +# before_filter :require_login, :only => [:join_in_contest, :unjoin_in_contest] +# # end +# before_filter :require_login,:only => [:set_reward, :destroy, :add, :new, ] +# +# #before_filter :memberAccess, only: :show_project +# +# helper :watchers +# helper :attachments +# include AttachmentsHelper +# include ApplicationHelper +# include BidsHelper +# +# helper :projects +# helper :words +# helper :welcome +# helper :project_score +# +# def find_project_by_bid_id +# @bid = Bid.find(params[:id]) +# @project = @bid.courses[0] +# rescue ActiveRecord::RecordNotFound +# render_404 +# end +# +# def homework_ajax_modal +# @bid = Bid.find_by_id(params[:id]) +# # find_bid +# respond_to do |format| +# format.js +# end +# end +# +# +# def index +# @project_type = params[:project_type] +# # Modified by nie +# # @requirement_title = "4" +# @offset, @limit = api_offset_and_limit({:limit => 10}) +# if @project_type == '1' +# @bids = Bid.visible.where('reward_type = ?', 3) +# # elsif +# # @bids = Bid.visible.where('reward_type = ? or reward_type = ?', 4) +# else +# @bids = Bid.visible.where('reward_type = ?', 1) +# end +# +# @bids = @bids.like(params[:name]) if params[:name].present? +# @bid_count = @bids.count +# @bid_pages = Paginator.new @bid_count, @limit, params['page'] +# +# @offset ||= @bid_pages.reverse_offset +# #added by nie +# if params[:bid_sort_type].present? +# case params[:bid_sort_type] +# when '0' +# unless @offset == 0 +# @bids = @bids.offset(@offset).limit(@limit).all.reverse +# else +# limit = @bid_count % @limit +# limit = @limit if limit == 0 +# @bids = @bids.offset(@offset).limit(limit).all.reverse +# end +# @s_state = 0 +# when '1' +# unless @offset == 0 +# @bids = @bids.reorder('bids.commit').offset(@offset).limit(@limit).all.reverse +# else +# limit = @bid_count % @limit +# limit = @limit if limit == 0 +# @bids = @bids.reorder('bids.commit').offset(@offset).limit(limit).all.reverse +# end +# @s_state = 1 +# when '2' +# unless @offset == 0 +# @bids = @bids.offset(@offset).limit(@limit).all.reverse +# else +# limit = @bid_count % @limit +# limit = @limit if limit == 0 +# @bids = @bids.offset(@offset).limit(@limit).all.reverse +# end +# @s_state = 0 +# end +# else +# unless @offset == 0 +# @bids = @bids.reorder('bids.commit').offset(@offset).limit(@limit).all.reverse +# else +# limit = @bid_count % @limit +# limit = @limit if limit == 0 +# @bids = @bids.reorder('bids.commit').offset(@offset).limit(limit).all.reverse +# end +# @s_state = 1 +# end +# #end +# end +# #huang +# def contest +# +# # Modified by nie +# # @requirement_title = "4" +# @offset, @limit = api_offset_and_limit({:limit => 10}) +# +# @bids = Bid.visible.where('reward_type = ?', 2) +# +# # elsif +# # @bids = Bid.visible.where('reward_type = ? or reward_type = ?', 4) +# @bids = @bids.like(params[:name]) if params[:name].present? +# @bid_count = @bids.count +# @bid_pages = Paginator.new @bid_count, @limit, params['page'] +# +# @offset ||= @bid_pages.reverse_offset +# #added by nie +# if params[:contest_sort_type].present? +# case params[:contest_sort_type] +# when '0' +# unless @offset == 0 +# @bids = @bids.offset(@offset).limit(@limit).all.reverse +# else +# limit = @bid_count % @limit +# limit = @limit if limit == 0 +# @bids = @bids.offset(@offset).limit(limit).all.reverse +# end +# @s_state = 0 +# when '1' +# unless @offset == 0 +# @bids = @bids.reorder('bids.commit').offset(@offset).limit(@limit).all.reverse +# else +# limit = @bid_count % @limit +# limit = @limit if limit == 0 +# @bids = @bids.reorder('bids.commit').offset(@offset).limit(limit).all.reverse +# end +# @s_state = 1 +# when '2' +# unless @offset == 0 +# @bids = @bids.offset(@offset).limit(@limit).all.reverse +# else +# limit = @bid_count % @limit +# limit = @limit if limit == 0 +# @bids = @bids.offset(@offset).limit(@limit).all.reverse +# end +# @s_state = 0 +# end +# else +# unless @offset == 0 +# @bids = @bids.reorder('bids.commit').offset(@offset).limit(@limit).all.reverse +# else +# limit = @bid_count % @limit +# limit = @limit if limit == 0 +# @bids = @bids.reorder('bids.commit').offset(@offset).limit(limit).all.reverse +# end +# @s_state = 1 +# end +# #end +# end +# +# def fork +# @courses = [] +# @membership = User.current.memberships.all(:conditions => Project.visible_condition(User.current)) +# @membership.each do |membership| +# if membership.project.project_type == 1 +# @courses << membership.project +# end +# end +# end +# +# #将某个企业外包需求选为作业,目前此功能已放弃 +# def create_fork +# @homework = Bid.new +# @homework.name = params[:bid][:name] +# @homework.description = params[:bid][:description] +# @homework.reward_type = 3 +# # @bid.budget = params[:bid][:budget] +# @homework.deadline = params[:bid][:deadline] +# @homework.budget = 0 +# @homework.author_id = User.current.id +# @homework.commit = 0 +# @homework.homework_type = 1 +# @homework.is_evaluation = params[:bid][:is_evaluation] +# @homework.parent_id = @bid.id +# @homework.save_attachments(params[:attachments] || (params[:bid] && params[:bid][:uploads])) +# # @bid. +# if @homework.save +# HomeworkForCourse.create(:course_id => params[:course], :bid_id => @homework.id) +# unless @bid.watched_by?(User.current) +# if @bid.add_watcher(User.current) +# flash[:notice] = l(:label_bid_succeed) +# end +# end +# redirect_to course_for_bid_path(@homework) +# else +# @bid.safe_attributes = params[:bid] +# @courses = [] +# @membership = User.current.coursememberships.all#(:conditions => Project.visible_condition(User.current)) +# @membership.each do |membership| +# @courses << membership.course +# end +# render :action => 'fork' +# end +# end +# +# #有两个路由链接到此方法:/bids/:id /calls/:id但是貌似这两个路由都不能访问。。方法作用有待确认 +# def show +# @user = @bid.author +# @jours = @bid.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC') +# @limit = 10 +# @feedback_count = @jours.count +# @feedback_pages = Paginator.new @feedback_count, @limit, params['page'] +# @offset ||= @feedback_pages.offset +# @jour = @jours[@offset, @limit] +# @state = false +# +# respond_to do |format| +# layout_file = '' +# case @bid.reward_type +# when 3 +# html_title(l(:label_question_student)) +# layout_file = 'base_homework' +# when 1 +# layout_file = 'base_bids' +# else +# layout_file = 'base_contest' +# end +# format.html { +# render :layout => layout_file +# } +# format.api +# end +# end +# +# def join_in_contest +# if @bid.reward_type == 2 && params[:course_password] == @bid.password +# JoinInContest.create(:user_id => User.current.id, :bid_id => @bid.id) +# @state = 0 +# else +# @state = 1 +# end +# respond_to do |format| +# format.js { render :partial => 'set_join', :locals => {:user => User.current, :object_id => params[:id]} } +# end +# end +# +# def unjoin_in_contest +# joined = JoinInContest.where('bid_id = ? and user_id = ?', @bid.id, User.current.id) +# joined.each do |join| +# join.delete +# end +# respond_to do |format| +# format.js { render :partial => 'set_join', :locals => {:user => User.current, :object_id => params[:id]} } +# end +# end +# +# def new_join +# +# end +# +# # added by bai 增加了参与者和竞赛设置 +# def show_participator +# render :layout => 'base_contest' +# end +# +# #配置竞赛 +# def settings +# if @bid.author.id == User.current.id +# if @bid.reward_type == 2 +# @contest = Bid.find_by_reward_type(@bid.reward_type) +# render :layout => 'base_contest' +# end +# else +# render_403 :message => :notice_not_contest_setting_authorized +# end +# end +# #end +# +# # 显示课程作业,但是好像已经废弃 +# def show_course +# bids = Bid.where('parent_id = ?', @bid.id) +# @courses = [] +# for bid in bids +# @courses << bid.courses.first +# end +# +# respond_to do |format| +# if @bid.reward_type == 3 +# format.html { +# render :layout => 'base_homework' +# } +# elsif @bid.reward_type == 1 +# format.html { +# render :layout => 'base_bids' +# } +# else +# format.html { +# render :layout => 'base_contest' +# } +# end +# format.api +# +# end +# end +# +# def show_bid_project +# bids = Bid.where('parent_id = ?', @bid.id) +# @projects = [] +# for bid in bids +# @projects += bid.biding_projects +# end +# +# respond_to do |format| +# if @bid.reward_type == 3 +# format.html { +# render :layout => 'base_homework' +# } +# elsif @bid.reward_type == 1 +# format.html { +# render :layout => 'base_bids' +# } +# else +# format.html { +# render :layout => 'base_contest' +# } +# end +# format.api +# +# end +# end +# +# def show_bid_user +# bids = Bid.where('parent_id = ?', @bid.id) +# @users = [] +# for bid in bids +# for project in bid.projects +# @users += project.users +# end +# end +# +# respond_to do |format| +# if @bid.reward_type == 3 +# format.html { +# render :layout => 'base_homework' +# } +# elsif @bid.reward_type == 1 +# format.html { +# render :layout => 'base_bids' +# } +# else +# format.html { +# render :layout => 'base_contest' +# } +# end +# format.api +# +# end +# end +# +# def show_project +# # flash[:notice] = "" +# @membership = User.current.memberships.all(:conditions => Project.visible_condition(User.current)) +# @option = [] +# @membership.each do |membership| +# unless(membership.project.project_type==1) +# if membership.user.allowed_to?(:quote_project,membership.project) +# @option << membership.project +# end +# end +# end +# +# # a = [1] +# # @project = Project.where("id in []", a) +# @user = @bid.author +# @bidding_project = @bid.biding_projects.all +# if params[:student_id].present? +# @temp = [] +# @bidding_project.each do |pro| +# if pro.project && pro.project.project_status +# if /#{params[:student_id]}/ =~ pro.user.user_extensions.student_id +# @temp << pro +# end +# end +# @temp +# end +# @bidding_project = @temp +# else +# #added by nie +# @temp = [] +# @bidding_project.each do |pro| +# if pro.project && pro.project.project_status +# @temp << pro +# end +# @temp +# end +# if @temp.size > 0 +# @bidding_project = @temp.sort {|a,b| b.project.project_status.grade <=> a.project.project_status.grade} +# end +# #ended +# end +# +# if @bid.homework_type == 1 +# @homework = HomeworkAttach.new +# #@homework_list = @bid.homeworks +# #增加作业按评分排序, +# @homework_list = @bid.homeworks.eager_load(:rate_averages, :user, :attachments).order('seems_rateable_cached_ratings.avg DESC').order("#{HomeworkAttach.table_name}.created_at ASC") +# if params[:student_id].present? +# @temp = [] +# @homework_list.each do |pro| +# if /#{params[:student_id]}/ =~ pro.user.user_extensions.student_id +# @temp << pro +# end +# @temp +# end +# @homework_list = @temp +# end +# end +# +# respond_to do |format| +# if @bid.reward_type == 3 +# format.html { +# render :layout => 'base_homework' +# } +# elsif @bid.reward_type == 1 +# format.html { +# render :layout => 'base_bids' +# } +# else +# format.html { +# render :layout => 'base_contest' +# } +# end +# format.api +# end +# end +# +# # 显示作业课程 +# # add by nwb +# def show_courseEx +# +# if (User.current.logged? && (User.current.member_of_course?(@bid.courses.first) || User.current.admin?)) +# @membership = User.current.coursememberships.all(:conditions => Course.visible_condition(User.current)) +# @user = @bid.author +# @bidding_project = @bid.biding_projects.all +# +# if params[:student_id].present? +# @temp = [] +# @bidding_project.each do |pro| +# if pro.project && pro.project.project_status +# if /#{params[:student_id]}/ =~ pro.user.user_extensions.student_id +# @temp << pro +# end +# end +# @temp +# end +# @bidding_project = @temp +# else +# #added by nie +# @temp = [] +# @bidding_project.each do |pro| +# if pro.project && pro.project.project_status +# @temp << pro +# end +# @temp +# end +# if @temp.size > 0 +# @bidding_project = @temp.sort {|a,b| b.project.project_status.grade <=> a.project.project_status.grade} +# end +# #ended +# end +# +# if @bid.homework_type +# @homework = HomeworkAttach.new +# @is_teacher = is_course_teacher(User.current,@bid.courses.first) +# if @is_teacher +# all_homework_list = HomeworkAttach.find_by_sql("SELECT * FROM (SELECT homework_attaches.*, +# (SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND is_teacher_score = 1 AND stars IS NOT NULL ORDER BY updated_at DESC limit 0,1) AS t_score, +# (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND is_teacher_score = 0) AS s_score +# FROM homework_attaches WHERE bid_id = #{@bid.id} ORDER BY s_score DESC,created_at ASC) AS table1 +# WHERE table1.t_score IS NULL") +# @not_batch_homework = true +# @cur_type = 1 +# else +# all_homework_list = HomeworkAttach.find_by_sql("SELECT homework_attaches.*, +# (SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND is_teacher_score = 1 AND stars IS NOT NULL ORDER BY updated_at DESC limit 0,1) AS t_score, +# (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND is_teacher_score = 0) AS s_score, +# (SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id = #{User.current.id} AND is_teacher_score = #{@is_teacher ? 1 : 0}) AS m_score +# FROM homework_attaches +# INNER JOIN homework_evaluations ON homework_evaluations.homework_attach_id = homework_attaches.id +# WHERE homework_attaches.bid_id = #{@bid.id} AND homework_evaluations.user_id = #{User.current.id} ORDER BY s_score DESC") +# @is_student_batch_homework = true +# @cur_type = 4 +# end +# +# @cur_page = params[:page] || 1 +# # @homework_list = paginateHelper all_homework_list,10 +# @homework_list = all_homework_list +# @jours_count = @bid.journals_for_messages.where('m_parent_id IS NULL').count +# if params[:student_id].present? +# @temp = [] +# @homework_list.each do |pro| +# if /#{params[:student_id]}/ =~ pro.user.user_extensions.student_id +# @temp << pro +# end +# @temp +# end +# @homework_list = @temp +# end +# end +# +# respond_to do |format| +# if @bid.reward_type == 3 +# format.html { +# html_title(l(:label_homework_info)) +# render :layout => 'base_homework' +# } +# elsif @bid.reward_type == 1 +# format.html { +# render :layout => 'base_bids' +# } +# else +# format.html { +# render :layout => 'base_contest' +# } +# end +# format.api +# end +# else +# render_403 :message => :notice_not_authorized +# end +# end +# +# ##### by huang +# def show_project_homework +# # flash[:notice] = "" +# @membership = User.current.memberships.all(:conditions => Project.visible_condition(User.current)) +# @option = [] +# @membership.each do |membership| +# end +# # a = [1] +# # @project = Project.where("id in []", a) +# @user = @bid.author +# @bidding_project = @bid.biding_projects +# respond_to do |format| +# if @bid.reward_type == 3 +# format.html { +# render :layout => 'base_homework' +# } +# elsif @bid.reward_type == 1 +# format.html { +# render :layout => 'base_bids' +# } +# else +# format.html { +# render :layout => 'base_contest' +# } +# end +# format.api +# end +# end +# +# ###添加应标项目 +# def add +# project = Project.find(params[:bid]) +# bid_message = params[:bid_for_save][:bid_message] +# if BidingProject.where("project_id = ? and bid_id = ?", project.id, @bid.id).size == 0 +# if BidingProject.cerate_bidding(@bid.id, project.id, bid_message) +# +# # added by bai type ==1 需求,type==2 竞赛, type==3 作业 +# if @bid.reward_type == 1 +# flash.now[:notice] = l(:label_bidding_succeed) +# +# elsif @bid.reward_type == 2 +# flash.now[:notice] = l(:label_bidding_contest_succeed) +# +# else @bid.reward_type == 3 +# flash.now[:notice] = l(:label_bidding_homework_succeed) +# end +# # end +# +# end +# else +# if @bid.reward_type == 3 +# flash.now[:error] = l(:label_bidding_homework_fail) +# else +# flash.now[:error] = l(:label_bidding_fail) +# end +# end +# @bidding_project = @bid.biding_projects +# respond_to do |format| +# # format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}} +# # format.html +# format.html { redirect_to :back } +# format.js +# #format.api { render_api_ok } +# end +# end +# +# #删除已提交的项目作业(不删项目) +# def delete +# binding_project = params[:binding_project] +# if can_delete_project_homework(BidingProject.find(binding_project),User.current) +# if BidingProject.delete(binding_project) +# redirect_to project_for_bid_url +# else +# render_403; +# end +# end +# end +# ## 新建留言 +# def create +# +# if params[:bid_message][:message].size>0 +# if params[:reference_content] +# message = params[:bid_message][:message] + "\n" + params[:reference_content] +# else +# message = params[:bid_message][:message] +# @m = message +# end +# refer_user_id = params[:bid_message][:reference_user_id].to_i +# @bid.add_jour(User.current, message, refer_user_id) +# end +# @user = @bid.author +# @jours = @bid.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC') +# @jour = paginateHelper @jours,10 +# @bid.set_commit(@feedback_count) +# respond_to do |format| +# format.js +# #format.api { render_api_ok } +# end +# +# end +# +# ##删除留言 +# def destroy +# @user = @bid.author +# if User.current.admin? || User.current.id == @user.id +# JournalsForMessage.delete_message(params[:object_id]) +# end +# @jours = @bid.journals_for_messages.reverse +# @limit = 10 +# @feedback_count = @jours.count +# @feedback_pages = Paginator.new @feedback_count, @limit, params['page'] +# @offset ||= @feedback_pages.offset +# @jour = @jours[@offset, @limit] +# +# @bid.set_commit(@feedback_count) +# # if a_message.size > 5 +# # @message = a_message[-5, 5] +# # else +# # @message = a_message +# # end +# # @message_count = a_message.count +# +# respond_to do |format| +# # format.html +# format.js +# #format.api { render_api_ok } +# end +# end +# +# #删除作业 +# #by xianbo +# def homework_destroy +# @bid_to_destroy = Bid.find params[:id] +# course_url = course_homework_path(@bid_to_destroy.courses.first) +# (render_403; return false) unless User.current.admin?||User.current.id==@bid_to_destroy.author_id +# @bid_to_destroy.destroy +# respond_to do |format| +# format.html { redirect_to course_url } +# format.js +# #format.api { render_api_ok } +# end +# end +# +# #end by xianbo +# ##引用 +# def new +# @jour = JournalsForMessage.find(params[:journal_id]) if params[:journal_id] +# if @jour +# user = @jour.user +# text = @jour.notes +# else +# user = @bid.author +# text = @bid.description +# end +# # Replaces pre blocks with [...] +# text = text.to_s.strip.gsub(%r{
((.|\s)*?)}m, '[...]') +# @content = "> #{ll(User.current.language, :text_user_wrote, user)}\n> " +# @content << text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" +# @id = user.id +# rescue ActiveRecord::RecordNotFound +# render_404 +# end +# +# ##新建需求 +# def new_bid +# @bid = Bid.new +# @bid.safe_attributes = params[:bid] +# end +# +# #huang +# def create_contest +# @bid = Bid.new +# @bid.name = params[:bid][:name] +# @bid.description = params[:bid][:description] +# @bid.reward_type = 2 +# @bid.budget = params[:bid][:budget] +# @bid.deadline = params[:bid][:deadline] +# @bid.password = params[:bid][:password] #added by bai +# @bid.author_id = User.current.id +# @bid.commit = 0 +# if @bid.save +# unless @bid.watched_by?(User.current) +# if @bid.add_watcher(User.current) +# flash[:notice] = l(:label_bid_succeed) +# end +# end +# redirect_to respond_url(@bid) +# else +# @bid.safe_attributes = params[:bid] +# render :action => 'new_bid' +# end +# end +# +# # added by bai +# def update_contest +# @bid = Bid.find(params[:id]) +# @bid.name = params[:bid][:name] +# @bid.description = params[:bid][:description] +# @bid.reward_type = 2 +# @bid.budget = params[:bid][:budget] +# @bid.deadline = params[:bid][:deadline] +# @bid.password = params[:bid][:password] +# @bid.author_id = User.current.id +# @bid.commit = 0 +# if @bid.save +# unless @bid.watched_by?(User.current) +# if @bid.add_watcher(User.current) +# flash[:notice] = l(:label_bid_succeed) +# end +# end +# redirect_to respond_url(@bid) +# else +# @bid.safe_attributes = params[:bid] +# render :action => 'new_bid' +# end +# end +# #huang +# def new_contest +# @bid = Bid.new +# @bid.safe_attributes = params[:bid] +# end +# +# def create_bid +# @bid = Bid.new +# @bid.name = params[:bid][:name] +# @bid.description = params[:bid][:description] +# @bid.reward_type = 1 +# @bid.budget = params[:bid][:budget] +# @bid.deadline = params[:bid][:deadline] +# @bid.author_id = User.current.id +# @bid.commit = 0 +# if @bid.save +# unless @bid.watched_by?(User.current) +# if @bid.add_watcher(User.current) +# flash[:notice] = l(:label_bid_succeed) +# end +# end +# redirect_to respond_url(@bid) +# else +# @bid.safe_attributes = params[:bid] +# render :action => 'new_bid' +# end +# end +# +# def create_homework +# @bid = Bid.new +# @bid.name = params[:bid][:name] +# @bid.description = params[:bid][:description] +# @bid.is_evaluation = params[:bid][:is_evaluation] +# @bid.proportion = params[:bid][:proportion] +# @bid.evaluation_num = params[:bid][:evaluation_num] +# params[:bid][:open_anonymous_evaluation] ? @bid.open_anonymous_evaluation = 1 : @bid.open_anonymous_evaluation = 0 +# @bid.reward_type = 3 +# # @bid.budget = params[:bid][:budget] +# @bid.deadline = params[:bid][:deadline] +# @bid.budget = 0 +# @bid.author_id = User.current.id +# @bid.commit = 0 +# @bid.homework_type = 1 +# @bid.save_attachments(params[:attachments] || (params[:bid] && params[:bid][:uploads])) +# # @bid. +# if @bid.save +# HomeworkForCourse.create(:course_id => params[:course_id], :bid_id => @bid.id) +# unless @bid.watched_by?(User.current) +# if @bid.add_watcher(User.current) +# flash[:notice] = l(:label_bid_succeed) +# end +# end +# redirect_to course_homework_url(params[:course_id]) +# else +# @bid.safe_attributes = params[:bid] +# @homework = @bid +# @course = Course.find_by_id(params[:course_id]) +# @course_id = @course.id +# #respond_to do |format| +# # format.html { redirect_to new_homework_course_path(params[:course_id]),:layout => 'base_courses'} +# # format.api { render_validation_errors(@bid) } +# #end +# render file: 'courses/new_homework', layout: 'base_courses' +# end +# end +# +# # modify by nwb\ +# # 编辑作业 +# def edit +# @bid = Bid.find(params[:bid_id]) +# if (User.current.admin?||User.current.allowed_to?(:as_teacher,@bid.courses.first)) +# @course_id = params[:course_id] +# respond_to do |format| +# format.html { +# @course = Course.find(params[:course_id]) +# @user= User.find(User.current.id) +# render :layout => 'base_courses' +# } +# end +# else +# render_403 +# end +# end +# +# def update +# @bid = Bid.find(params[:id]) +# @course = @bid.courses.first#Project.find(params[:course_id]) +# @bid.name = params[:bid][:name] +# @bid.description = params[:bid][:description] +# @bid.is_evaluation = params[:bid][:is_evaluation] +# @bid.proportion = params[:bid][:proportion] +# @bid.evaluation_num = params[:bid][:evaluation_num] +# params[:bid][:open_anonymous_evaluation] ? @bid.open_anonymous_evaluation = 1 : @bid.open_anonymous_evaluation = 0 +# @bid.reward_type = 3 +# @bid.deadline = params[:bid][:deadline] +# @bid.budget = 0 +# #@bid.author_id = User.current.id +# @bid.commit = 0 +# @bid.homework_type = 1 +# @bid.save_attachments(params[:attachments] || (params[:bid] && params[:bid][:uploads])) +# if @bid.save +# flash[:notice] = l(:label_update_homework_succeed) +# redirect_to course_homework_url(@course) +# else +# @bid.safe_attributes = params[:bid] +# render :action => 'edit', :layout =>'base_courses' +# end +# end +# +# def new_submit_homework +# #render html to prepare create submit homework +# find_bid +# find_bid +# render :layout => 'base_homework' +# end +# +# def add_homework +# if User.current.logged? && (!Member.where('user_id = ? and project_id = ?', User.current.id, @bid.courses.first.id).first.nil? && (Member.where('user_id = ? and project_id = ?', User.current.id, @bid.courses.first.id).first.roles&Role.where('id = ? or id = ? or id =?',5, 10, 7)).size >0) +# # homework = HomeworkAttach.create(:bid_id => @bid.id, :user_id => User.current.id) +# # homework.save_attachments(params[:attachments] || (params[:bid] && params[:bid][:uploads])) +# +# if hadcommittedhomework(User.current.id, @bid.id) == true +# @homework_flag = l(:label_bidding_homework_committed) +# else +# @homework = HomeworkAttach.new +# @homework.safe_attributes = params[:homeworkattach] +# @homework.bid_id = @bid.id +# @homework.user_id = User.current.id +# @homework.save_attachments(params[:attachments]) +# +# render_attachment_warning_if_needed(@homework) +# +# @homework_flag = if @homework.save +# l(:label_bidding_homework_succeed) +# else +# l(:label_bidding_homework_failed) +# end +# +# if @homework.attachments.empty? +# @homework.delete +# #flash[:error] = l(:no_attachmens_allowed) +# @homework_flag = l(:no_attachmens_allowed) +# # else +# end +# end +# end +# +# @homework_list = @bid.homeworks +# respond_to do |format| +# format.html{ +# #redirect_to project_for_bid_path, notice: @homework_flag.to_s +# flash[:notice] = @homework_flag.to_s +# redirect_back_or_default(project_for_bid_path) +# } +# format.js +# end +# +# end +# +# # 作业统计 +# def homework_statistics +# @course = @bid.courses.first +# @member = [] +# @course.memberships.each do |member| +# unless (member.roles && Role.where('id = ? ', 3)).empty? +# @member.push member +# end +# end +# if @bid.homework_type = 1 +# @student = User.where("id in (select DISTINCT user_id from #{HomeworkAttach.table_name} where bid_id = ? )", @bid.id) +# @homework_type = true +# else +# +# @homework_type = false +# end +# @user = @bid.author +# render :layout => 'base_homework' +# end +# +# def homework_respond +# @user = @bid.author +# render :layout => 'base_homework' +# end +# +# def more +# @jour = @bid.journals_for_messages +# @jour.each_with_index {|j,i| j.indice = i+1} +# @state = true +# +# respond_to do |format| +# format.html { redirect_to :back } +# format.js +# #format.api { render_api_ok } +# end +# end +# +# def back +# @jour = @bid.journals_for_messages +# @jour.each_with_index {|j,i| j.indice = i+1} +# @state = false +# +# respond_to do |format| +# format.html { redirect_to :back } +# format.js +# #format.api { render_api_ok } +# end +# end +# +# #added by william +# #used to set the bidding project reward +# def set_reward +# @b_p = nil +# @biding_project_id = nil +# +# if params[:set_reward][:reward]&&((User.current.id==@bid.author_id)||User.current.admin) +# # @bid_id = params[:id] +# @biding_project_id = params[:set_reward][:b_id] +# @b_p = BidingProject.find_by_id(@biding_project_id) +# +# # 把字段存进表中 +# @b_p.update_reward(params[:set_reward][:reward].to_s) +# end +# +# respond_to do |format| +# format.js +# end +# end +# +# # added by william +# # used to manage the bid and end the bid +# def manage +# +# end +# +# # 启动匿评 +# def start_anonymous_comment +# @bid = Bid.find(params[:id]) +# @course = @bid.courses.first +# if(@bid.comment_status == 0) +# homeworks = @bid.homeworks +# if(homeworks && homeworks.size >= 2) +# homeworks.each_with_index do |homework, index| +# user = homework.user +# n = @bid.evaluation_num +# n = n < homeworks.size ? n : homeworks.size - 1 +# assigned_homeworks = get_assigned_homeworks(homeworks, n, index) +# assigned_homeworks.each do |h| +# @homework_evaluation = HomeworkEvaluation.new(user_id: user.id, homework_attach_id: h.id) +# @homework_evaluation.save +# end +# end +# @bid.update_column('comment_status', 1) +# @statue = 1 +# else +# @statue = 2 +# end +# else +# @statue = 3 +# end +# +# respond_to do |format| +# format.js +# end +# end +# +# def stop_anonymous_comment +# @bid = Bid.find(params[:id]) +# +# @bid.update_column('comment_status', 2) +# +# respond_to do |format| +# format.js +# end +# end +# +# def alert_anonymous_comment +# @bid = Bid.find params[:id] +# @course = @bid.courses.first +# @cur_size = 0 +# @totle_size = 0 +# if @bid.comment_status == 0 +# @totle_size = searchStudent(@course).size +# @cur_size = @bid.homeworks.size +# elsif @bid.comment_status == 1 +# @bid.homeworks.map { |homework| @totle_size += homework.homework_evaluations.count} +# @cur_size = 0 +# @bid.homeworks.map { |homework| @cur_size += homework.rates(:quality).where("seems_rateable_rates.is_teacher_score = 0").count} +# end +# @percent = format("%.2f",(@cur_size.to_f / ( @totle_size == 0 ? 1 : @totle_size)) * 100) +# respond_to do |format| +# format.js +# end +# end +# +# private +# +# def get_assigned_homeworks(homeworks, n, index) +# homeworks += homeworks +# homeworks[index + 1 .. index + n] +# end +# +# def find_bid +# if params[:id] +# @bid = Bid.find(params[:id], :include => [{:homeworks => :user}]) +# @user = @bid.author +# end +# rescue +# render_404 +# end +# +# def memberAccess +# # 是课程,则判断当前用户是否参加了课程 +# return true if current_user.admin? +# #return 0 if @bid.courses.first.project_type == Project::ProjectType_project +# currentUser = User.current +# render_403 unless currentUser.member_of_course?(@bid.courses.first) +# end +# +# #验证是否显示课程 +# def can_show_course +# @first_page = FirstPage.find_by_page_type('project') +# if @first_page.show_course == 2 +# render_404 +# end +# end +# +# #验证是否显示竞赛 +# def can_show_contest +# @first_page = FirstPage.find_by_page_type('project') +# if @first_page.show_contest == 2 +# render_404 +# end +# end +# end +# diff --git a/app/controllers/boards_controller.rb b/app/controllers/boards_controller.rb index 383da0bbe..6ff4d2f97 100644 --- a/app/controllers/boards_controller.rb +++ b/app/controllers/boards_controller.rb @@ -27,26 +27,38 @@ class BoardsController < ApplicationController include SortHelper helper :watchers helper :project_score - + helper :attachments def index #modify by nwb + @flag = params[:flag] || false if @project - @boards = @project.boards.includes(:last_message => :author).all - @boards = [] << @boards[0] if @boards.any? - if @boards.size == 1 - @board = @boards.first - show and return - end - render :layout => false if request.xhr? - elsif @course - if (User.current.admin? || @course.is_public == 1 || (@course.is_public == 0 && User.current.member_of_course?(@course))) - @boards = @course.boards.includes(:last_message => :author).all + if !@project.is_public? && !User.current.member_of?(@project) && !User.current.admin? + render_403 + else + @boards = @project.boards.includes(:last_message => :author).all @boards = [] << @boards[0] if @boards.any? if @boards.size == 1 @board = @boards.first show and return end - render :layout => 'base_courses' + render :layout => false if request.xhr? + end + elsif @course + if (User.current.admin? || @course.is_public == 1 || (@course.is_public == 0 && User.current.member_of_course?(@course))) + @boards = @course.boards.includes(:last_message => :author).all + if @course.boards.empty? + @board = @course.boards.build + @board.name = " #{l(:label_borad_course) }" + @board.description = @course.name.to_s + @board.project_id = -1 + if @board.save + @boards = @course.boards.includes(:last_message => :author).all + end + end + unless @course.boards.empty? + @board = @course.boards.first + end + show and return else render_403 end @@ -57,27 +69,67 @@ class BoardsController < ApplicationController def show respond_to do |format| + format.js format.html { sort_init 'updated_on', 'desc' sort_update 'created_on' => "#{Message.table_name}.created_on", 'replies' => "#{Message.table_name}.replies_count", 'updated_on' => "COALESCE(last_replies_messages.created_on, #{Message.table_name}.created_on)" - @topic_count = @board.topics.count - @topic_pages = Paginator.new @topic_count, per_page_option, params['page'] - @topics = @board.topics. - reorder("#{Message.table_name}.sticky DESC"). - includes(:last_reply). - limit(@topic_pages.per_page). - offset(@topic_pages.offset). - order(sort_clause). - preload(:author, {:last_reply => :author}). - all + @topic_count = @board ? @board.topics.count : 0 + if @project + @topic_pages = Paginator.new @topic_count, per_page_option, params['page'] + @topics = @board.topics. + reorder("#{Message.table_name}.sticky DESC, #{Message.table_name}.created_on desc"). + includes(:last_reply). + limit(@topic_pages.per_page). + offset(@topic_pages.offset). + + preload(:author, {:last_reply => :author}). + all + elsif @course + # + # board_topics = @board ? @board.topics.reorder("#{Message.table_name}.sticky DESC, #{Message.table_name}.created_on desc"). + # includes(:last_reply). + # # limit(@topic_pages.per_page). + # # offset(@topic_pages.offset). + # + # preload(:author, {:last_reply => :author}). + # all : [] + # @topics = paginateHelper board_topics,10 + if( @board ) + limit = 10; + pageno = params[:page]; + if(pageno == nil || pageno=='') + dw_topic = nil; + if( params[:parent_id]!=nil && params[:parent_id]!='' ) + dw_topic = @board.topics.where(id:params[:parent_id]).first(); + end + if( dw_topic != nil ) + dw_count = @board.topics.where('(sticky>?) or (sticky=? and created_on>?)',dw_topic.sticky,dw_topic.sticky,dw_topic.created_on).count(); + dw_count = dw_count+1; + pageno = dw_count%10==0 ? (dw_count/limit) : (dw_count/limit+1) + end + end + if(pageno == nil || pageno=='') + pageno=1; + end + @topic_count = @board.topics.count(); + @topic_pages = Paginator.new @topic_count, limit, pageno + @topics = @board.topics.reorder("#{Message.table_name}.sticky DESC, #{Message.table_name}.created_on desc"). + limit(limit).offset(@topic_pages.offset).includes(:last_reply). + preload(:author, {:last_reply => :author}).all(); + else + @topics = []; + end + end + @message = Message.new(:board => @board) #modify by nwb if @project render :action => 'show', :layout => 'base_projects' elsif @course + @params=params render :action => 'show', :layout => 'base_courses' end } diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 9f61306ab..d226000c0 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -17,6 +17,7 @@ class CommentsController < ApplicationController default_search_scope :news + include ApplicationHelper model_object News before_filter :find_model_object before_filter :find_project_from_association @@ -26,9 +27,13 @@ class CommentsController < ApplicationController raise Unauthorized unless @news.commentable? @comment = Comment.new - @comment.safe_attributes = params[:comment] + @project ? @comment.comments = params[:comment][:comments] : @comment.comments = params[:comment] @comment.author = User.current if @news.comments << @comment + if params[:asset_id] + ids = params[:asset_id].split(',') + update_kindeditor_assets_owner ids,@comment.id,OwnerTypeHelper::COMMENT + end flash[:notice] = l(:label_comment_added) end diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 9d35b9fb0..00d740ee7 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -1,13 +1,18 @@ class CoursesController < ApplicationController - layout 'base_courses' + # layout 'base_courses' include CoursesHelper + include ActivitiesHelper helper :activities helper :members helper :words + helper :attachments + helper :activity_notifys + before_filter :auth_login1, :only => [:show, :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 @@ -15,6 +20,7 @@ class CoursesController < ApplicationController menu_item l(:label_sort_by_influence), :only => :index before_filter :can_show_course, :except => [] + before_filter :logged_user_by_apptoken,:only => [:show,:new_homework,:feedback] before_filter :find_course, :except => [ :index, :search,:list, :new,:join,:unjoin, :create, :copy, :statistics, :new_join, :course, :enterprise_course, :course_enterprise,:view_homework_attaches,:join_private_courses] before_filter :authorize_course, :only => [:show, :settings, :edit, :update, :modules, :close, :reopen, :view_homework_attaches, :course] before_filter :authorize_course_global, :only => [:view_homework_attaches, :new,:create] @@ -26,60 +32,34 @@ class CoursesController < ApplicationController def join if User.current.logged? - course = Course.find_by_id params[:object_id] - if course - if course_endTime_timeout? course - @state = 2 - else - if User.current.member_of_course?(course) - @state = 3 - else - if params[:course_password] == course.password - members = [] - members << Member.new(:role_ids => [10], :user_id => User.current.id) - course.members << members - StudentsForCourse.create(:student_id => User.current.id, :course_id => params[:object_id]) - @state = 0 - else - @state = 1 - end - end - end - else - @state = 4 - end + cs = CoursesService.new + user = User.current + join = cs.join_course params,user + @state = join[:state] + course = join[:course] else - @state = 5 + @state = 5 #未登录 end respond_to do |format| - format.js { render :partial => 'set_join', :locals => {:user => User.current, :course => course, :object_id => params[:object_id]} } + format.js { render :partial => 'set_join', :locals => {:user => user, :course => course, :object_id => params[:object_id]} } end rescue Exception => e - @state = 4 + @state = 4 #已经加入了课程 respond_to do |format| - # format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} format.js { render :partial => 'set_join', :locals => {:user => User.current, :course => nil, :object_id => nil} } end end def unjoin if User.current.logged? - - @member = Member.where('course_id = ? and user_id = ?', params[:object_id], User.current.id) - @member.first.destroy - - joined = StudentsForCourse.where('student_id = ? and course_id = ?', User.current.id, params[:object_id]) - joined.each do |join| - join.delete - end + cs = CoursesService.new + cs.exit_course params,User.current end respond_to do |format| - # format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} 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| @@ -89,32 +69,23 @@ class CoursesController < ApplicationController #更新课程信息 def update - @course.safe_attributes = params[:course] - @course.time = params[:time] - @course.term = params[:term] - @course.class_period = params[:class_period] - 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 - - respond_to do |format| - format.html { - flash[:notice] = l(:notice_successful_update) - redirect_to settings_course_url(@course) - } - format.api { render_api_ok } - end + 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 - render :action => 'settings' + redirect_to settings_course_url(@course) } format.api { render_validation_errors(@course) } end @@ -123,6 +94,9 @@ class CoursesController < ApplicationController def new_join @course = Course.find(params[:object_id]) + respond_to do |format| + format.js + end end # 课程搜索 @@ -130,7 +104,6 @@ class CoursesController < ApplicationController def search courses_all = Course.all_course name = params[:name] - #(redirect_to courses_url, :notice => l(:label_sumbit_empty);return) if name.blank? if name.blank? @courses = [] @courses_all = [] @@ -203,27 +176,30 @@ class CoursesController < ApplicationController 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 - @render_file = 'member_list' - @canShowCode = isCourseTeacher(User.current.id,@course) && params[:role] != '1' - @is_remote = true + @canShowCode = User.current.allowed_to?(:as_teacher,@course) && params[:role] != '1' + # @is_remote = true @score_sort_by = "desc" q = "#{params[:name].strip}" - #(redirect_to stores_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank? if params[:incourse] - @results = searchmember_by_name(student_homework_score(0,0,0,"desc"), q) - + 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) + results = searchmember_by_name(student_homework_score(@group.id,0,0,"desc"), q) end @is_remote = true - @result_count = @results.count - @results = paginateHelper @results - + #@result_count = results.count + #@results = paginateHelper results, 10 + @results = results + @search_name = q end def addgroups @@ -234,7 +210,7 @@ class CoursesController < ApplicationController group.course_id = @course.id group.save end - @canShowCode = isCourseTeacher(User.current.id,@course) && params[:role] != '1' + @canShowCode = User.current.allowed_to?(:as_teacher,@course) && params[:role] != '1' @is_remote = true @course_groups = @course.course_groups @@ -268,16 +244,26 @@ class CoursesController < ApplicationController valid_attr = params[:valid] valid_value = params[:value] - - faker = CourseGroup.new + group_id = params[:group_id] + # faker = CourseGroup.new if valid_attr.eql?('name') - faker.name = valid_value - faker.valid? - req[:valid] = faker.errors[:name].blank? - req[:message] = faker.errors[: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? + # req[:message] = l(:modal_valid_passing) if req[:message].blank? render :json => req end def join_group @@ -305,35 +291,27 @@ class CoursesController < ApplicationController end def searchgroupmembers @subPage_title = l :label_student_list - @render_file = 'member_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") - @results = paginateHelper_for_members @results, 10 end - - - end - def member ## 有角色参数的才是课程,没有的就是项目 if (User.current.admin? || @course.is_public == 1 || (@course.is_public == 0 && User.current.member_of_course?(@course))) - @render_file = 'member_list' + @render_file = 'new_member_list' @score_sort_by = "desc" - @canShowCode = isCourseTeacher(User.current.id,@course) && params[:role] != '1' + @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 @@ -342,95 +320,89 @@ class CoursesController < ApplicationController when '1' @subPage_title = l :label_teacher_list @all_members = searchTeacherAndAssistant(@course) - @members = paginateHelper @all_members, 10 + @members = @all_members when '2' @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 = paginateHelper_for_members @all_members, 10 - - + @members = @all_members 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 - - # render :layout => 'base_courses' else render_403 end - end def export_course_member_excel @all_members = student_homework_score(0,0,0,"desc") + filename="#{@course.teacher.lastname.to_s + @course.teacher.firstname.to_s }_#{@course.name}_#{@course.time.to_s + @course.term}#{l(:excel_member_list)}"; + # 如果是ie 需要转码 + if(/trident/.match(request.env["HTTP_USER_AGENT"]) != nil) + filename= URI::encode(filename) + end respond_to do |format| format.xls { send_data(member_to_xls(@all_members,@course.course_groups), :type => "text/excel;charset=utf-8; header=present", - :filename => "#{@course.teacher.lastname.to_s + @course.teacher.firstname.to_s }_#{@course.name}_#{@course.time.to_s + @course.term}#{l(:excel_member_list)}.xls") + :filename => "#{filename}.xls") } end end def member_score_sort - - # @teachers= searchTeacherAndAssistant(@course) @canShowCode = isCourseTeacher(User.current.id,@course) && params[:role] != '1' - # @role = params[:role] - # @course_groups = @course.course_groups if @course.course_groups - # @show_serch = params[:role] == '2' @subPage_title = l :label_student_list - @render_file = 'member_list' - # @results = params[:result] if params[:result] - # unless @result.nil? - # @results = @result.reverse - # - # end - # @results = paginateHelper @results@score_sort_by = "desc" + @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 group_id == '0' - page = params[:page].nil? ? 0 : (params['page'].to_i - 1) - @results = student_homework_score(0,page, 10,@score_sort_by) - - @results = paginateHelper_for_members @results, 10 + 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 - @group = CourseGroup.find(group_id) - @results = student_homework_score(group_id, 0, 0,@score_sort_by) - @results = paginateHelper @results, 10 + 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 @@ -440,68 +412,54 @@ class CoursesController < ApplicationController @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 create - if User.current.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.safe_attributes = params[:course] - @course.tea_id = User.current.id - # added by bai - @course.term = params[:term] - @course.time = params[:time] - #@course.school_id = params[:occupation] - @course.school_id = User.current.user_extensions.school_id - @course.setup_time = params[:setup_time] - @course.endup_time = params[:endup_time] - @course.class_period = params[:class_period] - 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 - @issue_custom_fields = IssueCustomField.sorted.all - @trackers = Tracker.sorted.all + else + render_403 + end + end - 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 => User.current, :roles => [r]) - m.project_id = -1 - course = CourseInfos.new(:user_id => User.current.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 - respond_to do |format| - format.html { - flash[:notice] = l(:notice_successful_create) - if params[:continue] - redirect_to new_course_url(attrs, :course => '0') - elsif params[:course_continue] - redirect_to new_course_url(:course => '1') - else - redirect_to settings_course_url(@course, :course_type => 1) - end - } - format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'courses', :action => 'show', :id => @course.id) } - end - else - #@course.destroy - respond_to do |format| - format.html { render :action => 'new', :layout => 'base' } #Added by young - format.api { render_validation_errors(@course) } - end - end + def create + cs = CoursesService.new + @course = cs.create_course(params,User.current)[:course] + if @course + respond_to do |format| + flash[:notice] = l(:notice_successful_create) + format.html {redirect_to settings_course_url(@course, :course_type => 1)} + 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 + def course @school_id = params[:school_id] per_page_option = 10 if @school_id == "0" or @school_id.nil? @@ -516,54 +474,28 @@ class CoursesController < ApplicationController @course_count = @courses_all.count @course_pages = Paginator.new @course_count, per_page_option, params['page'] - - #gcm activity count - @course_activity_count=Hash.new - #count initialize @courses_all.each do |course| @course_activity_count[course.id]=0 end - - #@course_activity_count=get_course_activity @courses_all,@course_activity_count - #gcm 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) - - #gcm @course_activity_count=get_course_activity @courses,@course_activity_count - #gcmend - when '1' @courses = @courses_all.order("course_ac_para desc") @s_type = 1 @courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page) - - #gcm @course_activity_count=get_course_activity @courses,@course_activity_count - #gcmend - when '2' @courses = @courses_all.order("watchers_count desc") @s_type = 2 @courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page) - - #gcm @course_activity_count=get_course_activity @courses,@course_activity_count - #gcmend - - #gcm when '3' - - #gcm @course_activity_count=get_course_activity @courses_all,@course_activity_count - #gcmend - @courses=handle_course @courses_all,@course_activity_count @s_type = 3 @courses = @courses[@course_pages.offset, @course_pages.per_page] @@ -571,21 +503,14 @@ class CoursesController < ApplicationController @s_type = 0 @courses = @courses_all.order("created_on desc") @courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page) - - #gcm @course_activity_count=get_course_activity @courses,@course_activity_count - #gcmend - end respond_to do |format| format.html { - render :layout => 'base' + render :layout => 'new_base' } format.api { - # @offset, @limit = api_offset_and_limit - # @course_count = Course.visible.count - # @courses = Course.visible.offset(@offset).limit(@limit).order('lft').all } format.atom { courses = Course.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all @@ -594,20 +519,16 @@ class CoursesController < ApplicationController end end - def new @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 => 'base' + # month = Time.now.month + render :layout => 'new_base' 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| @@ -622,7 +543,10 @@ class CoursesController < ApplicationController end def index - render_404 + if !User.current.admin? + render_404 + return + end @course_type = params[:course_type] @school_id = params[:school_id] per_page_option = 10 @@ -670,7 +594,7 @@ class CoursesController < ApplicationController respond_to do |format| format.html { - # render :layout => 'base' + render :layout => 'base' } format.atom { courses = Course.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all @@ -679,42 +603,6 @@ class CoursesController < ApplicationController end end - def homework - if @course.is_public != 0 || User.current.member_of_course?(@course) || User.current.admin? - @offset, @limit = api_offset_and_limit({:limit => 10}) - @bids = @course.homeworks.order('deadline DESC') - @bids = @bids.like(params[:name]) if params[:name].present? - @bid_count = @bids.count - @bid_pages = Paginator.new @bid_count, @limit, params['page'] - - @offset ||= @bid_pages.reverse_offset - unless @offset == 0 - @bids = @bids.offset(@offset).limit(@limit).all.reverse - else - limit = @bid_count % @limit - if limit == 0 - limit = 10 - end - @bids = @bids.offset(@offset).limit(limit).all.reverse - end - render :layout => 'base_courses' - else - render_403 - end - end - - # 新建作业 - def new_homework - @homework = Bid.new - @homework.safe_attributes = params[:bid] - @homework.open_anonymous_evaluation = 1 - if (User.current.logged? && User.current.member_of_course?(Course.find params[:id] )) - render :layout => 'base_courses' - else - render_403 - end - end - def toggleCourse @course_prefs = Course.find_by_extra(@course.extra) unless (@course_prefs.teacher == User.current || User.current.admin?) @@ -771,48 +659,71 @@ class CoursesController < ApplicationController end def show - # try to redirect to the requested menu item if params[:jump] && redirect_to_course_menu_item(@course, params[:jump]) return end - @users_by_role = @course.users_by_role if(User.find_by_id(CourseInfos.find_by_course_id(@course.id).try(:user_id))) @user = User.find_by_id(CourseInfos.find_by_course_id(@course.id).user_id) end - - @key = User.current.rss_key #新增内容 @days = Setting.activity_days_default.to_i - if params[:from] begin; @date_to = params[:from].to_date + 1; rescue; end end - has = { "show_course_files" => true, "show_course_news" => true, "show_course_messages" => true, - "show_bids" => true, - "show_course_journals_for_messages" => true + #"show_course_journals_for_messages" => true, + # "show_bids" => true, + # "show_homeworks" => true, + "show_polls" => true } @date_to ||= Date.today + 1 - # @date_from = (@date_to - @days) > @course.created_at.to_date ? (@date_to - @days) : @course.created_at.to_date - #@date_from = @date_to - @days-1.years @author = (params[:user_id].blank? ? nil : User.active.find(params[:user_id])) - # 决定显示所用用户或单个用户活动 - @activity = Redmine::Activity::Fetcher.new(User.current, :course => @course, - :with_subprojects => false, - :author => @author) - @activity.scope_select {|t| has["show_#{t}"]} - # modify by nwb - # 添加私密性判断 - if User.current.member_of_course?(@course)|| User.current.admin? - events = @activity.events(@days, @course.created_at) + if @author.nil? + # 显示老师和助教的活动 + # @authors = searchTeacherAndAssistant(@course) + @authors = course_all_member(@course) + events = [] + key = "course_events_#{@course.id}".to_sym + if Rails.env.production? && Setting.course_cahce_enabled? + events = Rails.cache.read(key) || [] + end + if events.empty? + @authors.each do |author| + @activity = Redmine::Activity::Fetcher.new(User.current, :course => @course, + :with_subprojects => false, + :author => author.user) + + @activity.scope_select {|t| has["show_#{t}"]} + # modify by nwb + # 添加私密性判断 + if User.current.member_of_course?(@course)|| User.current.admin? + events += @activity.events(@days, @course.created_at) + else + events += @activity.events(@days, @course.created_at, :is_public => 1) + end + end + Rails.cache.write(key, events) if Rails.env.production? && Setting.course_cahce_enabled? + end else - events = @activity.events(@days, @course.created_at, :is_public => 1) + # @author = @course.teacher + @activity = Redmine::Activity::Fetcher.new(User.current, :course => @course, + :with_subprojects => false, + :author => @author) + + @activity.scope_select {|t| has["show_#{t}"]} + # modify by nwb + # 添加私密性判断 + if User.current.member_of_course?(@course)|| User.current.admin? + events = @activity.events(@days, @course.created_at) + else + events = @activity.events(@days, @course.created_at, :is_public => 1) + end end # 无新动态时,显示老动态 @@ -823,24 +734,17 @@ class CoursesController < ApplicationController events = @activity.events(:is_public => 1) end end - - @offset, @limit = api_offset_and_limit({:limit => 10}) - @events_count = events.count - @events_pages = Paginator.new @events_count, @limit, params['page'] - @offset ||= @events_pages.offset - events = events.slice(@offset,@limit) - - @events_by_day = events.group_by {|event| User.current.time_to_date(event.event_datetime)} - # documents @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category' - # - @teachers= searchTeacherAndAssistant(@course) - @canShowRealName = isCourseTeacher(User.current.id,@course) - if(User.find_by_id(CourseInfos.find_by_course_id(@course.id).try(:user_id))) @user = User.find_by_id(CourseInfos.find_by_course_id(@course.id).user_id) end + sorted_events = sort_activity_events_course(events) + events = paginateHelper sorted_events,10 + @events_by_day = events.group_by {|event| User.current.time_to_date(event.event_datetime)} + # documents + + respond_to do |format| format.html{render :layout => 'base_courses'} format.api @@ -867,10 +771,6 @@ class CoursesController < ApplicationController offset = @jours.count(:conditions => ["#{JournalsForMessage.table_name}.id > ?", params[:r].to_i]) page = 1 + offset / @limit end - - #@feedback_count = @jours.count - #@feedback_pages = Paginator.new @feedback_count, @limit, page - #@offset ||= @feedback_pages.offset @jour = paginateHelper @jours,10 @state = false respond_to do |format| @@ -880,13 +780,36 @@ class CoursesController < ApplicationController else render_403 end - end - + #根据已有课程复制课程 + #param id:已有课程ID + def copy_course + if @course + @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 + else + render_404 + end + end private - def allow_join course if course_endTime_timeout? course respond_to do |format| @@ -905,50 +828,38 @@ class CoursesController < ApplicationController #验证是否显示课程 def can_show_course @first_page = FirstPage.find_by_page_type('project') - if @first_page.show_course == 2 + if @first_page.try(:show_course) == 2 render_404 end end def student_homework_score(groupid,start_from, 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 + sql_select = "SELECT members.*,( + SELECT AVG(student_works.final_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 = 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.*, 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}" + sql_select = "SELECT members.*,( + SELECT AVG(student_works.final_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 = 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) @@ -964,13 +875,12 @@ GROUP BY members.user_id ORDER BY score #{score_sort_by}" #当加入,退出分班时查询分班的学生 def search_group_members group @subPage_title = l :label_student_list - @render_file = 'member_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") - @results = paginateHelper @results, 10 end diff --git a/app/controllers/discuss_demos_controller.rb b/app/controllers/discuss_demos_controller.rb new file mode 100644 index 000000000..9a03f3b86 --- /dev/null +++ b/app/controllers/discuss_demos_controller.rb @@ -0,0 +1,42 @@ +class DiscussDemosController < ApplicationController + def index + + @discuss_demo_list = DiscussDemo.where("body is not null").order("created_at desc").page(params[:page] || 1).per(10) + end + + def new + @discuss_demo = DiscussDemo.create + @discuss_demo.save! + @discuss_demo + end + + def create + + end + + def update + @discuss_demo = DiscussDemo.find(params[:id]) + @discuss_demo.update_attributes(:title => params[:discuss_demo][:title],:body => params[:discuss_demo][:body]) + redirect_to :controller=> 'discuss_demos',:action => 'show',:id => params[:id] + end + + def delete + + end + + def destroy + asset = Kindeditor::Asset.find_by_owner_id(params[:id]) + if !asset.nil? + filepath = File.join(Rails.root,"public","files","uploads", + asset[:created_at].to_s.gsub("+0800","").to_datetime.strftime("%Y%m").to_s, + asset[:asset].to_s) + File.delete(filepath) if File.exist?filepath + end + DiscussDemo.destroy(params[:id]) + redirect_to :controller=> 'discuss_demos',:action => 'index' + end + + def show + @discuss_demo = DiscussDemo.find(params[:id]) + end +end diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb index 932ffaa7a..570726320 100644 --- a/app/controllers/documents_controller.rb +++ b/app/controllers/documents_controller.rb @@ -61,6 +61,10 @@ class DocumentsController < ApplicationController if @project.project_type ==1 render :action => 'show', :layout => 'base_courses' end + respond_to do |format| + format.html + format.api + end end def new @@ -87,6 +91,7 @@ class DocumentsController < ApplicationController def update @document.safe_attributes = params[:document] + @document.save_attachments(params[:attachments]) if request.put? and @document.save flash[:notice] = l(:notice_successful_update) redirect_to document_url(@document) @@ -105,7 +110,7 @@ class DocumentsController < ApplicationController render_attachment_warning_if_needed(@document) if attachments.present? && attachments[:files].present? && Setting.notified_events.include?('document_added') - Mailer.attachments_added(attachments[:files]).deliver + Mailer.run.attachments_added(attachments[:files]) end redirect_to document_url(@document) end diff --git a/app/controllers/files_controller.rb b/app/controllers/files_controller.rb index 56f760e13..19c1214e2 100644 --- a/app/controllers/files_controller.rb +++ b/app/controllers/files_controller.rb @@ -20,28 +20,37 @@ class FilesController < ApplicationController layout 'base_projects' #by young menu_item :files - + before_filter :auth_login1, :only => [:index] + before_filter :logged_user_by_apptoken,:only => [:index] before_filter :find_project_by_project_id#, :except => [:getattachtype] - before_filter :authorize, :except => [:getattachtype,:quote_resource_show,:search] + before_filter :authorize, :except => [:getattachtype,:quote_resource_show,:search,:searchone4reload,:search_project,:quote_resource_show_project,:search_tag_attachment] helper :sort include SortHelper include FilesHelper helper :project_score include CoursesHelper + include ApplicationHelper def show_attachments obj @attachments = [] obj.each do |container| @attachments += container.attachments end - @all_attachments = visable_attachemnts(@attachments) + @all_attachments = User.current.admin? ? @attachments : visable_attachemnts(@attachments) @limit = 10 @feedback_count = @all_attachments.count @feedback_pages = Paginator.new @feedback_count, @limit, params['page'] @offset ||= @feedback_pages.offset - @curse_attachments_all = @all_attachments[@offset, @limit] - @curse_attachments = paginateHelper @all_attachments,10 + #@curse_attachments_all = @all_attachments[@offset, @limit] + @obj_attachments = paginateHelper @all_attachments,10 + end + + def searchone4reload + attachment = Attachment.find_by_id(params[:fileid]); + respond_to do |format| + format.html{render :layout => nil,:locals=>{:file=>attachment,:course=>@course}} + end end def search @@ -49,6 +58,7 @@ class FilesController < ApplicationController @sort = "" @order = "" @is_remote = true + @q = params[:name].strip if params[:sort] order_by = params[:sort].split(":") @sort = order_by[0] @@ -57,26 +67,69 @@ class FilesController < ApplicationController end sort = "#{@sort} #{@order}" end - + # show_attachments [@course] begin q = "%#{params[:name].strip}%" #(redirect_to stores_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank? if params[:insite] - @result = find_public_attache q,sort - @result = visable_attachemnts_insite @result,@course - @searched_attach = paginateHelper @result,10 + if q == "%%" + @result = [] + @searched_attach = paginateHelper @result,10 + else + @result = find_public_attache q,sort + @result = visable_attachemnts_insite @result,@course + @searched_attach = paginateHelper @result,10 + end else @result = find_course_attache q,@course,sort @result = visable_attachemnts @result @searched_attach = paginateHelper @result,10 - end + @tag_list = get_course_tag_list @course + end #rescue Exception => e # #render 'stores' # redirect_to search_course_files_url end end + def search_project + sort = "" + @sort = "" + @order = "" + @is_remote = true + if params[:sort] + order_by = params[:sort].split(":") + @sort = order_by[0] + if order_by.count > 1 + @order = order_by[1] + end + sort = "#{@sort} #{@order}" + end + begin + q = "%#{params[:name].strip}%" + #(redirect_to stores_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank? + if params[:insite] + if q == "%%" + @result = [] + @searched_attach = paginateHelper @result,10 + else + @result = find_public_attache q,sort + @result = visable_attachemnts_insite @result,@project + @searched_attach = paginateHelper @result,10 + end + else + @result = find_project_attache q,@project,sort + @result = visable_attachemnts @result + @searched_attach = paginateHelper @result,10 + end + + #rescue Exception => e + # #render 'stores' + # redirect_to search_course_files_url + end + end + def find_course_attache keywords,course,sort = "" if sort == "" sort = "created_on DESC" @@ -86,6 +139,30 @@ class FilesController < ApplicationController #resultSet = Attachment.find_by_sql("SELECT `attachments`.* FROM `attachments` LEFT OUTER JOIN `homework_attaches` ON `attachments`.container_type = 'HomeworkAttach' AND `attachments`.container_id = `homework_attaches`.id LEFT OUTER JOIN `homework_for_courses` ON `homework_attaches`.bid_id = `homework_for_courses`.bid_id LEFT OUTER JOIN `homework_for_courses` AS H_C ON `attachments`.container_type = 'Bid' AND `attachments`.container_id = H_C.bid_id WHERE (`homework_for_courses`.course_id = 117 OR H_C.course_id = 117 OR (`attachments`.container_type = 'Course' AND `attachments`.container_id = 117)) AND `attachments`.filename LIKE '%#{keywords}%'").reorder("created_on DESC") end + def find_project_attache keywords,project,sort = "" + if sort == "" + sort = "created_on DESC" + end + ids = "" + len = 0 + count = project.versions.count + project.versions.each do |version| + len = len + 1 + if len != count + ids += version.id.to_s + ',' + else + ids += version.id.to_s + end + end + if ids.blank? + resultSet = Attachment.where("attachments.container_type = 'Project' And attachments.container_id = '#{project.id}' AND filename LIKE :like ", like: "%#{keywords}%"). + reorder(sort) + else + resultSet = Attachment.where("((attachments.container_type = 'Project' And attachments.container_id = '#{project.id}') OR (container_type = 'Version' AND container_id IN (#{ids}))) AND filename LIKE :like ", like: "%#{keywords}%"). + reorder(sort) + end + end + def find_public_attache keywords,sort = "" # StoresController#search 将每条文件都查出来,再次进行判断过滤。---> resultSet.to_a.map # 此时内容不多速度还可,但文件增长,每条判断多则进行3-4次表连接。 @@ -98,6 +175,7 @@ class FilesController < ApplicationController end def index + @flag = params[:flag] || false #sort_init 'filename', 'asc' sort_init 'created_on', 'desc' sort_update 'created_on' => "#{Attachment.table_name}.created_on", @@ -130,25 +208,44 @@ class FilesController < ApplicationController attribute = "downloads" when "created_on" attribute = "created_on" + when "quotes" + attribute = "quotes" + else + attribute = "created_on" end - - if order_by.count == 1 - sort += "#{Attachment.table_name}.#{attribute} asc " if attribute - elsif order_by.count == 2 - sort += "#{Attachment.table_name}.#{attribute} #{order_by[1]} " if attribute && order_by[1] - end - if sort_type != params[:sort].split(",").last - sort += "," + @sort = order_by[0] + @order = order_by[1] + if order_by.count == 1 && attribute + sort += "#{Attachment.table_name}.#{attribute} asc " + if sort_type != params[:sort].split(",").last + sort += "," + end + elsif order_by.count == 2 && order_by[1] + sort += "#{Attachment.table_name}.#{attribute} #{order_by[1]} " + if sort_type != params[:sort].split(",").last + sort += "," + end end end + else + sort = "#{Attachment.table_name}.created_on desc" end - @containers = [ Project.includes(:attachments).reorder(sort).find(@project.id)] - @containers += @project.versions.includes(:attachments).reorder(sort).all + @containers = [ Project.includes(:attachments).find(@project.id)] + @containers += @project.versions.includes(:attachments).all + + ids = [] + @containers.each do |c| + ids += c.attachments.pluck(:id) + end + @containers = [Struct.new(:attachments).new(Attachment.where('id in (?)',ids).reorder(sort))] show_attachments @containers - render :layout => !request.xhr? + respond_to do |format| + format.html + format.js + end elsif params[:course_id] @isproject = false @@ -173,6 +270,8 @@ class FilesController < ApplicationController attribute = "created_on" when "quotes" attribute = "quotes" + else + attribute = "created_on" end @sort = order_by[0] @order = order_by[1] @@ -188,12 +287,16 @@ class FilesController < ApplicationController end end end + else + sort = "#{Attachment.table_name}.created_on desc" end @containers = [ Course.includes(:attachments).reorder(sort).find(@course.id)] show_attachments @containers + @tag_list = attachment_tag_list @all_attachments + render :layout => 'base_courses' end @@ -204,6 +307,11 @@ class FilesController < ApplicationController @can_quote = attachment_candown @file end + def quote_resource_show_project + @file = Attachment.find(params[:id]) + @can_quote = attachment_candown @file + end + def new @versions = @project.versions.sort @course_tag = @project.project_type @@ -230,7 +338,7 @@ class FilesController < ApplicationController render_attachment_warning_if_needed(container) if !attachments.empty? && !attachments[:files].blank? && Setting.notified_events.include?('file_added') - Mailer.attachments_added(attachments[:files]).deliver + Mailer.run.attachments_added(attachments[:files]) end # TODO: 临时用 nyan @@ -259,7 +367,28 @@ class FilesController < ApplicationController attachments = Attachment.attach_filesex(@course, params[:attachments], params[:attachment_type]) if !attachments.empty? && !attachments[:files].blank? && Setting.notified_events.include?('file_added') - Mailer.attachments_added(attachments[:files]).deliver + Mailer.run.attachments_added(attachments[:files]) + end + + if params[:course_attachment_type] && params[:course_attachment_type] != "5" + case params[:course_attachment_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) + else + tag_name = "" + end + if !attachments.empty? && attachments[:files] && tag_name != "" + attachments[:files].each do |attachment| + attachment.tag_list.add(tag_name) + attachment.save + end + end end # TODO: 临时用 nyan @@ -272,6 +401,7 @@ class FilesController < ApplicationController @containers = [Course.includes(:attachments).reorder("#{Attachment.table_name}.created_on DESC").find(@course.id)] show_attachments @containers + @tag_list = attachment_tag_list @all_attachments @attachtype = 0 @contenttype = 0 @@ -405,8 +535,31 @@ class FilesController < ApplicationController format.html end end + end + #查找指定TAG的按条件过滤的资源列表,只有课程内搜索有此功能 + def search_tag_attachment + @q,@tag_name,@order = params[:q],params[:tag_name] + @is_remote = true + if params[:sort] + order_by = params[:sort].split(":") + @sort = order_by[0] + if order_by.count > 1 + @order = order_by[1] + end + sort = "#{@sort} #{@order}" + end + q = "%#{@q.strip}%" + @result = find_course_attache q,@course,sort + @result = visable_attachemnts @result + @result = @result.select{|attachment| attachment.tag_list.include?(@tag_name)} + @searched_attach = paginateHelper @result,10 + @tag_list = get_course_tag_list @course + respond_to do |format| + format.js + # format.html + end end end diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb index 03d1454ef..253e1d28d 100644 --- a/app/controllers/forums_controller.rb +++ b/app/controllers/forums_controller.rb @@ -1,7 +1,7 @@ # added by fq class ForumsController < ApplicationController layout "users_base" - + include ApplicationHelper # GET /forums # GET /forums.json before_filter :find_forum_if_available @@ -15,12 +15,14 @@ class ForumsController < ApplicationController PageLimit = 20 def create_feedback if User.current.logged? - @memo = Memo.new(params[:memo]) - @memo.forum_id = "1" - @memo.author_id = User.current.id + #@memo = Memo.new(params[:memo]) + #@memo.forum_id = "1" + #@memo.author_id = User.current.id #@forum = @memo.forum + cs = CommentService.new + @memo,message = cs.create_feedback params,User.current respond_to do |format| - if @memo.save + if !@memo.new_record? format.html { redirect_to forum_path(@memo.forum) } else sort_init 'updated_at', 'desc' @@ -61,6 +63,11 @@ class ForumsController < ApplicationController respond_to do |format| if @memo.save + if params[:asset_id] + ids = params[:asset_id].split(',') + update_kindeditor_assets_owner ids ,@memo.id,OwnerTypeHelper::MEMO + end + #end format.html { redirect_to (forum_memo_url(@forum, (@memo.parent_id.nil? ? @memo : @memo.parent_id))), notice: "#{l :label_memo_create_succ}" } format.json { render json: @memo, status: :created, location: @memo } else @@ -160,12 +167,24 @@ class ForumsController < ApplicationController def create @forum = Forum.new(params[:forum]) @forum.creator_id = User.current.id + if @forum.save + # Time 2015-03-24 17:07:05 + # Author lizanle + # Description after save后需要进行资源记录的更新 + # owner_type = 2 对应的是 forum + if params[:asset_id] + ids = params[:asset_id].split(',') + update_kindeditor_assets_owner ids ,@forum.id,OwnerTypeHelper::FORUM + end + #end + respond_to do |format| - respond_to do |format| - if @forum.save - format.html { redirect_to @forum, notice: l(:label_forum_create_succ) } - format.json { render json: @forum, status: :created, location: @forum } - else + format.html { redirect_to @forum, notice: l(:label_forum_create_succ) } + format.json { render json: @forum, status: :created, location: @forum } + end + + else + respond_to do |format| flash.now[:error] = "#{l :label_forum_create_fail}: #{@forum.errors.full_messages[0]}" format.html { render action: "new" } format.json { render json: @forum.errors, status: :unprocessable_entity } diff --git a/app/controllers/git_callback_controller.rb b/app/controllers/git_callback_controller.rb new file mode 100644 index 000000000..717a22961 --- /dev/null +++ b/app/controllers/git_callback_controller.rb @@ -0,0 +1,9 @@ +class GitCallbackController < ApplicationController + + def post_update + @repository = Repository.find_by_root_url(params[:root_url]) + @repository.fetch_changesets + render :text => 'success' + end + +end \ No newline at end of file diff --git a/app/controllers/git_usage_controller.rb b/app/controllers/git_usage_controller.rb index 4f9a96d6f..56aaa56f1 100644 --- a/app/controllers/git_usage_controller.rb +++ b/app/controllers/git_usage_controller.rb @@ -1,6 +1,6 @@ #added by baiyu class GitUsageController < ApplicationController - layout "project_base" + layout "new_base" def ch_usage end diff --git a/app/controllers/homework_attach_controller.rb b/app/controllers/homework_attach_controller.rb index 8b9bf9099..5dd5363b9 100644 --- a/app/controllers/homework_attach_controller.rb +++ b/app/controllers/homework_attach_controller.rb @@ -6,7 +6,7 @@ class HomeworkAttachController < ApplicationController ############################### before_filter :can_show_course,except: [] #判断当前角色权限时需先找到当前操作的project - before_filter :find_course_by_bid_id, :only => [:new] + before_filter :logged_user_by_apptoken,:find_course_by_bid_id, :only => [:new] before_filter :find_bid_and_course,:only => [:get_not_batch_homework,:get_batch_homeworks,:get_homeworks,:get_homework_jours, :get_student_batch_homework, :get_my_homework] before_filter :find_course_by_hoemwork_id, :only => [:edit,:update,:destroy,:show,:add_homework_users,:destory_homework_users, :praise_homework] #判断当前角色是否有操作权限 @@ -22,10 +22,12 @@ class HomeworkAttachController < ApplicationController #获取未批作业列表 def get_not_batch_homework @not_batch_homework = true + @search_name = params[:name] sort, direction = params[:sort] || "s_socre", params[:direction] || "desc" get_not_batch_homework_list sort,direction, @bid.id @cur_page = params[:page] || 1 @cur_type = 1 + @cur_sort,@cur_direction = params[:sort] || "s_socre", params[:direction] || "desc" @direction = direction == 'asc'? 'desc' : 'asc' respond_to do |format| format.js @@ -38,6 +40,7 @@ class HomeworkAttachController < ApplicationController #获取已评作业列表 def get_batch_homeworks + @search_name = params[:name] sort, direction = params[:sort] || "s_socre", params[:direction] || "desc" @is_batch_homeworks = true if sort == 't_socre' @@ -53,9 +56,11 @@ class HomeworkAttachController < ApplicationController FROM homework_attaches WHERE bid_id = #{@bid.id} ORDER BY #{order_by}) AS table1 WHERE table1.t_score IS NOT NULL") + all_homework_list = search_homework_member(all_homework_list,@search_name.to_s.downcase) if @search_name @cur_page = params[:page] || 1 @cur_type = 2 - @homework_list = paginateHelper all_homework_list,10 + # @homework_list = paginateHelper all_homework_list,10 + @homework_list = all_homework_list @direction = direction == 'asc'? 'desc' : 'asc' respond_to do |format| format.js @@ -71,6 +76,7 @@ class HomeworkAttachController < ApplicationController #获取所有作业列表 def get_homeworks @is_all_homeworks = true + @search_name = params[:name] sort, direction = params[:sort] || "s_socre", params[:direction] || "desc" if sort == 't_socre' order_by = "t_score #{direction}" @@ -84,9 +90,12 @@ class HomeworkAttachController < ApplicationController (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND is_teacher_score = 0) AS s_score FROM homework_attaches WHERE bid_id = #{@bid.id} ORDER BY #{order_by}") + + all_homework_list = search_homework_member(all_homework_list,@search_name.to_s.downcase) if @search_name @cur_page = params[:page] || 1 @cur_type = 3 - @homework_list = paginateHelper all_homework_list,10 + # @homework_list = paginateHelper all_homework_list,10 + @homework_list = all_homework_list @direction = direction == 'asc'? 'desc' : 'asc' respond_to do |format| format.js @@ -100,16 +109,11 @@ class HomeworkAttachController < ApplicationController #获取学生匿评列表 def get_student_batch_homework @is_student_batch_homework = true - all_homework_list = HomeworkAttach.eager_load(:attachments,:user,:rate_averages).find_by_sql("SELECT homework_attaches.*, - (SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND is_teacher_score = 1 AND stars IS NOT NULL ORDER BY updated_at DESC limit 0,1) AS t_score, - (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND is_teacher_score = 0) AS s_score, - (SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id = #{User.current.id} AND is_teacher_score = 0) AS m_score - FROM homework_attaches - INNER JOIN homework_evaluations ON homework_evaluations.homework_attach_id = homework_attaches.id - WHERE homework_attaches.bid_id = #{@bid.id} AND homework_evaluations.user_id = #{User.current.id} ORDER BY m_score DESC") + all_homework_list = get_student_batch_homework_list @bid,User.current @cur_page = params[:page] || 1 @cur_type = 4 - @homework_list = paginateHelper all_homework_list,10 + # @homework_list = paginateHelper all_homework_list,10 + @homework_list = all_homework_list respond_to do |format| format.js end @@ -133,7 +137,8 @@ class HomeworkAttachController < ApplicationController WHERE homework_attaches.bid_id = #{@bid.id} AND homework_users.user_id = #{User.current.id}") end @cur_page = params[:page] || 1 - @homework_list = paginateHelper all_homework_list,10 + # @homework_list = paginateHelper all_homework_list,10 + @homework_list = all_homework_list respond_to do |format| format.js end @@ -313,7 +318,7 @@ class HomeworkAttachController < ApplicationController def edit bid = @homework.bid - if (bid.comment_status == 0 || bid.open_anonymous_evaluation == 0) && (User.current.admin? || User.current.member_of_course?(bid.courses.first)) + if (bid.comment_status == 0 || bid.open_anonymous_evaluation == 0 || bid.comment_status == 2) && (User.current.admin? || User.current.member_of_course?(bid.courses.first)) get_homework_member @homework else render_403 :message => :notice_not_authorized @@ -329,9 +334,8 @@ class HomeworkAttachController < ApplicationController @homework.name = name @homework.description = description @homework.project_id = params[:project_id] || 0 - if params[:attachments] - @homework.save_attachments(params[:attachments]) - end + @homework.save_attachments(params[:attachments]) + render_attachment_warning_if_needed(@homework) if @homework.save respond_to do |format| format.html { redirect_to course_for_bid_url @homework.bid } @@ -346,7 +350,7 @@ class HomeworkAttachController < ApplicationController def destroy bid = @homework.bid - if (bid.comment_status == 0 || bid.open_anonymous_evaluation == 0) && (User.current.admin? || User.current == @homework.user) + if (bid.comment_status == 0 || bid.open_anonymous_evaluation == 0 || bid.comment_status == 0) && (User.current.admin? || User.current == @homework.user) if @homework.destroy #respond_to do |format| # format.html { redirect_to course_for_bid_url @homework.bid } @@ -433,20 +437,22 @@ class HomeworkAttachController < ApplicationController #添加留言 def addjours @is_teacher,@is_anonymous_comments,@m_score = params[:is_teacher]=="true",params[:is_anonymous_comments]=="true",params[:stars_value] - @cur_page,@cur_type = params[:cur_page] || 1,params[:cur_type] || 5 + @cur_page,@cur_type = params[:page] || 1,params[:cur_type] || 5 @homework = HomeworkAttach.find(params[:homework_id]) @stars_reates = @homework.rates(:quality) homework = @homework is_teacher = @is_teacher ? 1 : 0 #保存评分@homework.rate(@m_score.to_i,User.current.id,:quality, (@is_teacher ? 1 : 0)) - if @m_score + @is_comprehensive_evaluation = @is_teacher ? 1 : (@is_anonymous_comments ? 2 : 3) #判断当前评论是老师评论?匿评?留言 + if @is_teacher || @is_anonymous_comments + @m_score ||= 0 rate = @homework.rates(:quality).where(:rater_id => User.current.id, :is_teacher_score => is_teacher).first if rate rate.stars = @m_score - rate.save! else - @homework.rates(:quality).new(:stars => @m_score, :rater_id => User.current.id, :is_teacher_score => is_teacher).save! + rate = @homework.rates(:quality).new(:stars => @m_score, :rater_id => User.current.id, :is_teacher_score => is_teacher) end + rate.save! if homework.is_teacher_score == 0 if is_teacher == 1 @@ -464,12 +470,26 @@ class HomeworkAttachController < ApplicationController end end homework.save! - end + end #保存评论 - @is_comprehensive_evaluation = @is_teacher ? 1 : (@is_anonymous_comments ? 2 : 3) #判断当前评论是老师评论?匿评?留言 - if params[:new_form] && params[:new_form][:user_message] && params[:new_form][:user_message] != "" #有没有留言 - @homework.addjours User.current.id, params[:new_form][:user_message],0,@is_comprehensive_evaluation + if params[:new_form] && params[:new_form][:user_message] #有没有留言 + jour = @homework.journals_for_messages.where("is_comprehensive_evaluation = 1 and user_id = #{User.current.id}").order("created_on DESC").first + if params[:new_form][:user_message] == "" + if @is_teacher + unless jour + jour = @homework.addjours User.current.id, "",0,@is_comprehensive_evaluation + end + end + else + jour = @homework.addjours User.current.id, params[:new_form][:user_message],0,@is_comprehensive_evaluation + end + end + + if jour + jour.save_attachments(params[:attachments]) + render_attachment_warning_if_needed(jour) + jour.save end @teacher_stars = @stars_reates.where("is_teacher_score = 1") #老师评分列表 @@ -479,7 +499,7 @@ class HomeworkAttachController < ApplicationController if @cur_type == "1" #如果当前是老师未批列表,需要刷新整个作业列表界面 @bid = @homework.bid - get_not_batch_homework_list "s_socre","desc",@homework.bid_id + get_not_batch_homework_list params[:cur_sort] || "s_socre",params[:cur_direction] || "desc",@homework.bid_id elsif @cur_type == "2" #老师已批列表 @result_homework = HomeworkAttach.find_by_sql("SELECT homework_attaches.*, (SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND is_teacher_score = 1 AND stars IS NOT NULL ORDER BY updated_at DESC limit 0,1) AS t_score, @@ -543,6 +563,14 @@ class HomeworkAttachController < ApplicationController end private + #根据条件过滤作业结果 + def search_homework_member homeworks,name + select_homework = homeworks.select{ |homework| + homework.user[:login].to_s.downcase.include?(name) || homework.user.user_extensions[:student_id].to_s.downcase.include?(name) || (homework.user[:lastname].to_s.downcase + homework.user[:firstname].to_s.downcase).include?(name) + } + select_homework + end + #验证是否显示课程 def can_show_course @first_page = FirstPage.find_by_page_type('project') @@ -601,8 +629,10 @@ class HomeworkAttachController < ApplicationController (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND is_teacher_score = 0) AS s_score FROM homework_attaches WHERE bid_id = #{bid_id} ORDER BY #{order_by}) AS table1 - WHERE table1.t_score IS NULL") - @homework_list = paginateHelper @all_homework_list,10 + WHERE table1.t_score IS NULL ") + @all_homework_list = search_homework_member(@all_homework_list,@search_name.to_s.downcase) if @search_name + # @homework_list = paginateHelper @all_homework_list,10 + @homework_list = @all_homework_list end #获取指定作业的所有成员 diff --git a/app/controllers/homework_common_controller.rb b/app/controllers/homework_common_controller.rb new file mode 100644 index 000000000..6b2cd21ed --- /dev/null +++ b/app/controllers/homework_common_controller.rb @@ -0,0 +1,216 @@ +class HomeworkCommonController < ApplicationController + layout "base_courses" + before_filter :find_course, :only => [:index,:new,:create] + before_filter :find_homework, :only => [:edit,:update,:alert_anonymous_comment,:start_anonymous_comment,:stop_anonymous_comment,:destroy] + before_filter :teacher_of_course, :only => [:new, :create, :edit, :update, :destroy, :start_anonymous_comment, :stop_anonymous_comment, :alert_anonymous_comment] + before_filter :member_of_course, :only => [:index] + + def index + homeworks = @course.homework_commons.order("created_at desc") + @is_teacher = User.current.logged? && (User.current.admin? || User.current.allowed_to?(:as_teacher,@course)) + @is_student = User.current.logged? && (User.current.admin? || (User.current.member_of_course?(@course) && !@is_teacher)) + @homeworks = paginateHelper homeworks,20 + respond_to do |format| + format.html + end + end + + def new + @homework = HomeworkCommon.new + @homework.safe_attributes = params[:homework_common] + @homework.late_penalty = 0 + @homework.end_time = (Time.now + 3600 * 24).strftime('%Y-%m-%d') + @homework.publish_time = Time.now.strftime('%Y-%m-%d') + + #匿评作业相关属性 + @homework_detail_manual = HomeworkDetailManual.new + @homework_detail_manual.ta_proportion = 0.6 + @homework_detail_manual.absence_penalty = 0 + @homework_detail_manual.evaluation_num = 3 + @homework_detail_manual.evaluation_start = Time.now.strftime('%Y-%m-%d') + @homework_detail_manual.evaluation_end = (Time.now + 3600 * 24).strftime('%Y-%m-%d') + @homework.homework_detail_manual = @homework_detail_manual + respond_to do |format| + format.html + end + end + + def create + if params[:homework_common] + homework = HomeworkCommon.new + homework.name = params[:homework_common][:name] + homework.description = params[:homework_common][:description] + homework.end_time = params[:homework_common][:end_time] + homework.publish_time = params[:homework_common][:publish_time] + homework.homework_type = params[:homework_common][:homework_type] + homework.late_penalty = params[:late_penalty] + homework.user_id = User.current.id + homework.course_id = @course.id + + homework.save_attachments(params[:attachments]) + render_attachment_warning_if_needed(homework) + + #匿评作业相关属性 + homework_detail_manual = HomeworkDetailManual.new + homework_detail_manual.ta_proportion = params[:ta_proportion] || 0.6 + homework_detail_manual.comment_status = 1 + homework_detail_manual.evaluation_start = params[:evaluation_start] + homework_detail_manual.evaluation_end = params[:evaluation_end] + homework_detail_manual.evaluation_num = params[:evaluation_num] + homework_detail_manual.absence_penalty = params[:absence_penalty] + homework.homework_detail_manual = homework_detail_manual + + if homework.save + respond_to do |format| + format.html { + flash[:notice] = l(:notice_successful_create) + redirect_to homework_common_index_path(:course => @course.id) + } + end + return + end + end + + respond_to do |format| + format.html { + flash[:notice] = l(:notice_failed_create) + redirect_to new_homework_common_path(:course => @course.id) + } + end + end + + def edit + respond_to do |format| + format.html + end + end + + def update + @homework.name = params[:homework_common][:name] + @homework.description = params[:homework_common][:description] + @homework.end_time = params[:homework_common][:end_time] + @homework.publish_time = params[:homework_common][:publish_time] + @homework.homework_type = params[:homework_common][:homework_type] + @homework.late_penalty = params[:late_penalty] + @homework.course_id = @course.id + + #匿评作业相关属性 + @homework_detail_manual.ta_proportion = params[:ta_proportion] || 0.6 + @homework_detail_manual.evaluation_start = params[:evaluation_start] + @homework_detail_manual.evaluation_end = params[:evaluation_end] + @homework_detail_manual.evaluation_num = params[:evaluation_num] + @homework_detail_manual.absence_penalty = params[:absence_penalty] + + @homework.save_attachments(params[:attachments]) + render_attachment_warning_if_needed(@homework) + + if @homework.save && @homework_detail_manual.save + respond_to do |format| + format.html { + flash[:notice] = l(:notice_successful_edit) + redirect_to homework_common_index_path(:course => @course.id) + } + end + return + else + respond_to do |format| + format.html { + flash[:notice] = l(:notice_failed_edit) + redirect_to edit_homework_common_path(@homework) + } + end + end + end + + def destroy + if @homework.destroy + respond_to do |format| + format.html {redirect_to homework_common_index_path(:course => @course.id)} + end + end + end + + #开启匿评 + #statue 1:启动成功,2:启动失败,作业总数大于等于2份时才能启动匿评,3:已开启匿评,请务重复开启,4:没有开启匿评的权限 + def start_anonymous_comment + @statue =4 and return unless User.current.admin? || User.current.allowed_to?(:as_teacher,@course) + @statue = 5 and return if Time.parse(@homework.end_time.to_s).strftime("%Y-%m-%d") >= Time.now.strftime("%Y-%m-%d") + if @homework_detail_manual.comment_status == 1 + student_works = @homework.student_works + if student_works && student_works.size >=2 + student_works.each_with_index do |work, index| + user = work.user + n = @homework_detail_manual.evaluation_num + n = n < student_works.size ? n : student_works.size - 1 + assigned_homeworks = get_assigned_homeworks(student_works, n, index) + assigned_homeworks.each do |h| + student_works_evaluation_distributions = StudentWorksEvaluationDistribution.new(user_id: user.id, student_work_id: h.id) + student_works_evaluation_distributions.save + end + end + @homework_detail_manual.update_column('comment_status', 2) + @statue = 1 + else + @statue = 2 + end + else + @statue = 3 + end + end + + #关闭匿评 + def stop_anonymous_comment + @homework_detail_manual.update_column('comment_status', 3) + respond_to do |format| + format.js + end + end + + #提示 + def alert_anonymous_comment + @cur_size = 0 + @totle_size = 0 + if @homework_detail_manual.comment_status == 1 + @totle_size = @course.student.count + @cur_size = @homework.student_works.size + elsif @homework_detail_manual.comment_status == 2 + @homework.student_works.map { |work| @totle_size += work.student_works_evaluation_distributions.count} + @cur_size = 0 + @homework.student_works.map { |work| @cur_size += work.student_works_scores.where(:reviewer_role => 3).count} + end + @percent = format("%.2f",(@cur_size.to_f / ( @totle_size == 0 ? 1 : @totle_size)) * 100) + respond_to do |format| + format.js + end + end + + private + #获取课程 + def find_course + @course = Course.find params[:course] + rescue + render_404 + end + #获取作业 + def find_homework + @homework = HomeworkCommon.find params[:id] + @homework_detail_manual = @homework.homework_detail_manual + @course = @homework.course + rescue + render_404 + end + #是不是课程的老师 + def teacher_of_course + render_403 unless User.current.allowed_to?(:as_teacher,@course) || User.current.admin? + end + + #当前用户是不是课程的成员 + def member_of_course + render_403 unless @course.is_public || User.current.member_of_course?(@course) || User.current.admin? + end + + def get_assigned_homeworks(student_works, n, index) + student_works += student_works + student_works[index + 1 .. index + n] + end +end \ No newline at end of file diff --git a/app/controllers/issue_categories_controller.rb b/app/controllers/issue_categories_controller.rb index fb62821d8..68713aa43 100644 --- a/app/controllers/issue_categories_controller.rb +++ b/app/controllers/issue_categories_controller.rb @@ -16,7 +16,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class IssueCategoriesController < ApplicationController - layout "project_base" + layout "base_projects" menu_item :settings model_object IssueCategory before_filter :find_model_object, :except => [:index, :new, :create] diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index f3c2b199c..539d84e65 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -19,10 +19,13 @@ class IssuesController < ApplicationController layout 'base_projects'#Added by young default_search_scope :issues + before_filter :authorize1, :only => [:show] before_filter :find_issue, :only => [:show, :edit, :update] before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy] before_filter :find_project, :only => [:new, :create, :update_form] + #before_filter :authorize, :except => [:index, :show] before_filter :authorize, :except => [:index] + before_filter :find_optional_project, :only => [:index] before_filter :check_for_default_issue_status, :only => [:new, :create] before_filter :build_new_issue_from_params, :only => [:new, :create, :update_form] @@ -72,25 +75,26 @@ class IssuesController < ApplicationController else @limit = 10#per_page_option end - + @assign_to_id = params[:assigned_to_id] + @author_id = params[:author_id] + @priority_id = params[:priority_id] + @status_id = params[:status_id] + @subject = params[:subject] @issue_count = @query.issue_count @issue_pages = Paginator.new @issue_count, @limit, params['page'] @offset ||= @issue_pages.offset @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], - :order => sort_clause, - :offset => @offset, - :limit => @limit) + :order => sort_clause, + :offset => @offset, + :limit => @limit) @issue_count_by_group = @query.issue_count_by_group - - - - - respond_to do |format| + format.js format.html { render :template => 'issues/index', :layout => @project_base_tag }#by young format.api { Issue.load_visible_relations(@issues) if include_in_api_response?('relations') } + # format.json { render :json => @issues.map { |issue| issue.to_json}} #:json => @issues.map { |issue| issue.to_json} format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } format.csv { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'issues.csv') } format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'issues.pdf') } @@ -100,6 +104,7 @@ class IssuesController < ApplicationController format.html { render(:template => 'issues/index', :layout => @project_base_tag) }#by young format.any(:atom, :csv, :pdf) { render(:nothing => true) } format.api { render_validation_errors(@query) } + format.js end end rescue ActiveRecord::RecordNotFound @@ -107,7 +112,7 @@ class IssuesController < ApplicationController end def show - + @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all @journals.each_with_index {|j,i| j.indice = i+1} @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) @@ -121,11 +126,11 @@ class IssuesController < ApplicationController @edit_allowed = User.current.allowed_to?(:edit_issues, @project) @priorities = IssuePriority.active @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) - + @project_base_tag = (params[:project_id] || @issue.project) ? 'base_projects':'base'#by young @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq - - respond_to do |format| + + respond_to do |format|`` format.html { retrieve_previous_and_next_issue_ids render :template => 'issues/show', :layout => @project_base_tag#by young @@ -150,18 +155,21 @@ class IssuesController < ApplicationController def create call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) + # 给该issue在它所在的项目中所有的issues中所在的位置给一个序号 + @issue.project_issues_index = @issue.project.issues.last.nil? ? 1 : @issue.project.issues.last.project_issues_index + 1 if @issue.save call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) respond_to do |format| format.html { render_attachment_warning_if_needed(@issue) - flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("#{@issue.source_from}", issue_path(@issue), :title => @issue.subject)) + flash[:notice] = l(:label_successful_create) + #flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("#{@issue.subject}", issue_path(@issue), :title => @issue.subject)) #flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject)) if params[:continue] attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} redirect_to new_project_issue_url(@issue.project, :issue => attrs) else - redirect_to issue_url(@issue) + redirect_to issue_url(@issue.id) end } format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) } @@ -231,7 +239,8 @@ class IssuesController < ApplicationController flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? respond_to do |format| - format.html { redirect_back_or_default issue_path(@issue) } + + format.html { redirect_to issue_url(@issue.id) } format.api { render_api_ok } end else diff --git a/app/controllers/journals_controller.rb b/app/controllers/journals_controller.rb index f6e17593f..d8cd8eeec 100644 --- a/app/controllers/journals_controller.rb +++ b/app/controllers/journals_controller.rb @@ -56,7 +56,7 @@ class JournalsController < ApplicationController @diff = Redmine::Helpers::Diff.new(@detail.value, @detail.old_value) respond_to do |format| format.html { - render :layout => 'project_base' + render :layout => 'new_base' } end end @@ -74,6 +74,7 @@ class JournalsController < ApplicationController text = text.to_s.strip.gsub(%r{
((.|\s)*?)}m, '[...]') @content = "> #{ll(Setting.default_language, :text_user_wrote, user)}\n> " @content << text.gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" + @content = "
" << @content @id = user.id rescue ActiveRecord::RecordNotFound render_404 diff --git a/app/controllers/members_controller.rb b/app/controllers/members_controller.rb index ce899eff5..6271a6832 100644 --- a/app/controllers/members_controller.rb +++ b/app/controllers/members_controller.rb @@ -60,8 +60,12 @@ class MembersController < ApplicationController user_ids.each do |user_id| AppliedProject.deleteappiled(user_id, @project.id) end + @succes_message = "拒绝成功" end end + respond_to do |format| + format.js + end else #modify by nwb #更改课程成员逻辑 @@ -99,7 +103,7 @@ class MembersController < ApplicationController @project.members << members # added by nie @project.project_infos << project_info - @project.user_grades << user_grades + @project.user_grades << user_grades unless user_grades.first.user_id.nil? # end end if members.present? && members.all? { |m| m.valid? } @@ -107,21 +111,40 @@ class MembersController < ApplicationController AppliedProject.deleteappiled(member.user_id, @project.id) end end - respond_to do |format| - format.html { redirect_to_settings_in_projects } - format.js { @members = members; @applied_members = applied_members; } - format.api { - @member = members.first - if @member.valid? - render :action => 'show', :status => :created, :location => membership_url(@member) - else - render_validation_errors(@member) - end - } + if params[:flag] + unless members.present? && members.all? {|m| m.valid? } + flash[:error] = members.empty? ? l(:label_user_role_null) :members.collect {|m| m.errors.full_messages}.flatten.uniq.join(', ') + else + flash[:notice] = l(:label_invite_success) + end + respond_to do |format| + format.html { redirect_to invite_members_project_url(@project) } + end + else + unless members.present? && members.all? {|m| m.valid? } + @project_error_message = members.empty? ? l(:label_user_role_null) :members.collect {|m| m.errors.full_messages}.flatten.uniq.join(', ') + else + @succes_message = "添加成功" + end + respond_to do |format| + format.html { redirect_to_settings_in_projects } + format.js + format.api { + @member = members.first + if @member.valid? + render :action => 'show', :status => :created, :location => membership_url(@member) + else + render_validation_errors(@member) + end + } + end end elsif @course course_info = [] if params[:membership] + @create_member_error_messages = "角色不能留空" unless params[:membership][:role_ids] + @create_member_error_messages = "用户不能留空" unless params[:membership][:user_ids] + if params[:membership][:user_ids] attrs = params[:membership].dup user_ids = attrs.delete(:user_ids) @@ -129,7 +152,7 @@ class MembersController < ApplicationController member = Member.new(:role_ids => params[:membership][:role_ids], :user_id => user_id) role = Role.find_by_id(params[:membership][:role_ids]) # 这里的判断只能通过角色名,可以弄成常量 - if role.name == "学生" || role.name == "Student" + if role && (role.name == "学生" || role.name == "Student") StudentsForCourse.create(:student_id => user_id, :course_id =>@course.id) end members << member @@ -148,6 +171,11 @@ class MembersController < ApplicationController end @course.members << members @course.course_infos << course_info + + @roles = Role.givable.all[3..5] + members = @course.member_principals.includes(:roles, :principal).all.sort + else + @create_member_error_messages = l(:label_user_role_null) end respond_to do |format| format.html { redirect_to_settings_in_courses } @@ -165,6 +193,8 @@ class MembersController < ApplicationController end # end of params[:refusal_button] + + end def update @@ -231,6 +261,9 @@ class MembersController < ApplicationController end end end + @roles = Role.givable.all[3..5] + @members = @course.member_principals.includes(:roles, :principal).all.sort + @member = @course.members.new end saved = @member.save @@ -293,7 +326,8 @@ class MembersController < ApplicationController joined.each do |join| join.delete end - + @roles = Role.givable.all[3..5] + @members = @course.member_principals.includes(:roles, :principal).all.sort end respond_to do |format| format.html { redirect_to_settings_in_courses } @@ -310,6 +344,7 @@ class MembersController < ApplicationController end def autocomplete + @flag = params[:flag] || false respond_to do |format| format.js end diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb index aaa99e417..68f6f6473 100644 --- a/app/controllers/memos_controller.rb +++ b/app/controllers/memos_controller.rb @@ -59,6 +59,20 @@ class MemosController < ApplicationController @memo.content = @quote + @memo.content respond_to do |format| if @memo.save + # Time 2015-03-24 14:47:05 + # Author lizanle + # Description after save后需要进行资源记录的更新 + # owner_type = 1 对应的是 memo + if !params[:asset_id].nil? + ids = params[:asset_id].split(',') + ids.each do |id| + asset = Kindeditor::Asset.find(id.to_i) + asset.owner_id = @memo.id + asset.owner_type = 1 + asset.save + end + end + #end format.html { redirect_to back_memo_url, notice: "#{l :label_memo_create_succ}" } format.json { render json: @memo, status: :created, location: @memo } else @@ -143,7 +157,7 @@ class MemosController < ApplicationController def update respond_to do |format| - if( @memo.update_column(:subject, params[:memo][:subject]) && + if( #@memo.update_column(:subject, params[:memo][:subject]) && @memo.update_column(:content, params[:memo][:content]) && @memo.update_column(:sticky, params[:memo][:sticky]) && @memo.update_column(:lock, params[:memo][:lock])) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 8d6943f02..a242587fb 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -17,6 +17,7 @@ class MessagesController < ApplicationController include ApplicationHelper + before_filter :authorize1, :only => [:show] menu_item :boards default_search_scope :messages before_filter :find_board, :only => [:new, :preview,:edit] @@ -30,10 +31,20 @@ class MessagesController < ApplicationController include AttachmentsHelper helper :project_score + include CoursesHelper + REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE) # Show a topic and its replies def show + if @course + topic_id = params[:r]?params[:r]:params[:id] + parent_id = params[:id] + url = course_boards_path(@course,:topic_id => topic_id,:parent_id=>parent_id); + redirect_to url + return; + end + @isReply = true page = params[:page] # Find the page of the requested reply @@ -43,18 +54,35 @@ class MessagesController < ApplicationController end @reply_count = @topic.children.count - @reply_pages = Paginator.new @reply_count, REPLIES_PER_PAGE, page - @replies = @topic.children. - includes(:author, :attachments, {:board => :project}). - reorder("#{Message.table_name}.created_on DESC"). - limit(@reply_pages.per_page). - offset(@reply_pages.offset). - all + # @reply_pages = Paginator.new @reply_count, REPLIES_PER_PAGE, page + # @replies = @topic.children. + # includes(:author, :attachments, {:board => :project}). + # reorder("#{Message.table_name}.created_on DESC"). + # limit(@reply_pages.per_page). + # offset(@reply_pages.offset). + # all @reply = Message.new(:subject => "RE: #{@message.subject}") if @course + messages_replies = @topic.children. + includes(:author, :attachments, {:board => :project}). + reorder("#{Message.table_name}.created_on DESC"). + #limit(@reply_pages.per_page). + #offset(@reply_pages.offset). + all + @replies = paginateHelper messages_replies,10 + @reply = Message.new(:subject => "RE: #{@message.subject}") render :action => "show", :layout => "base_courses"#by young else + @reply_pages = Paginator.new @reply_count, REPLIES_PER_PAGE, page + @replies = @topic.children. + includes(:author, :attachments, {:board => :project}). + reorder("#{Message.table_name}.created_on DESC"). + limit(@reply_pages.per_page). + offset(@reply_pages.offset). + all + + @reply = Message.new(:subject => "RE: #{@message.subject}") render :action => "show", :layout => "base_projects"#by young end end @@ -68,12 +96,64 @@ class MessagesController < ApplicationController if request.post? @message.save_attachments(params[:attachments]) if @message.save + # 更新kindeditor上传的图片资源所有者 + if params[:asset_id] + ids = params[:asset_id].split(',') + update_kindeditor_assets_owner ids,@message.id,OwnerTypeHelper::MESSAGE + end + # 与我相关动态的记录add start + if(@board && @board.course) #项目的先不管 + teachers = searchTeacherAndAssistant(@board.course) + for teacher in teachers + if(teacher.user_id != User.current.id) + notify = ActivityNotify.new() + if(@board.course) + notify.activity_container_id = @board.course_id + notify.activity_container_type = 'Course' + else + notify.activity_container_id = @board.project_id + notify.activity_container_type = 'Project' + end + notify.activity_id = @message.id + notify.activity_type = 'Message' + notify.notify_to = teacher.user_id + notify.is_read = 0 + notify.save() + end + end + end + # 与我相关动态的记录add end + call_hook(:controller_messages_new_after_save, { :params => params, :message => @message}) render_attachment_warning_if_needed(@message) - redirect_to board_message_url(@board, @message) + if params[:is_board] + if @project + redirect_to project_boards_path(@project) + elsif @course + redirect_to course_boards_path(@course) + end + else + redirect_to board_message_url(@board, @message) + end else - layout_file = @project ? 'base_projects' : 'base_courses' - render :action => 'new', :layout => layout_file + if params[:is_board] + if @project + redirect_to project_boards_path(@project, :flag => true) + elsif @course + redirect_to course_boards_path(@course, :flag => true) + end + else + layout_file = @project ? 'base_projects' : 'base_courses' + render :action => 'new', :layout => layout_file + end + + end + else + respond_to do |format| + format.html { + layout_file = @project ? 'base_projects' : 'base_courses' + render :layout => layout_file + } end end end @@ -81,7 +161,15 @@ class MessagesController < ApplicationController # Reply to a topic def reply if params[:reply][:content] == "" - (redirect_to board_message_url(@board, @topic, :r => @reply), :notice => l(:label_reply_empty);return) + if params[:is_board] + if @project + (redirect_to project_boards_path(@project), :notice => l(:label_reply_empty);return) + elsif @course + (redirect_to course_boards_path(@course), :notice => l(:label_reply_empty);return) + end + else + (redirect_to board_message_url(@board, @topic, :r => @reply), :notice => l(:label_reply_empty);return) + end end @quote = params[:quote][:quote] @reply = Message.new @@ -92,13 +180,54 @@ class MessagesController < ApplicationController @topic.children << @reply #@topic.update_attribute(:updated_on, Time.now) if !@reply.new_record? + if params[:asset_id] + ids = params[:asset_id].split(',') + update_kindeditor_assets_owner ids,@reply.id,OwnerTypeHelper::MESSAGE + end + + # 与我相关动态的记录add start + if(@board && @board.course) #项目的先不管 + notifyto_arr = {} + notifyto_arr[@topic.author_id] = @topic.author_id + if( params[:parent_topic] != nil && params[:parent_topic] != '') + parent_topic = Message.find(params[:parent_topic]) + notifyto_arr[parent_topic.author_id] = parent_topic.author_id + end + notifyto_arr.each do |k,user_id| + if(user_id != User.current.id) + notify = ActivityNotify.new() + if(@board.course) + notify.activity_container_id = @board.course_id + notify.activity_container_type = 'Course' + else + notify.activity_container_id = @board.project_id + notify.activity_container_type = 'Project' + end + notify.activity_id = @reply.id + notify.activity_type = 'Message' + notify.notify_to = user_id + notify.is_read = 0 + notify.save() + end + end + end + # 与我相关动态的记录add end + call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply}) attachments = Attachment.attach_files(@reply, params[:attachments]) render_attachment_warning_if_needed(@reply) - else + else #render file: 'messages#show', layout: 'base_courses' end - redirect_to board_message_url(@board, @topic, :r => @reply) + if params[:is_board] + if @project + redirect_to project_boards_path(@project) + elsif @course + redirect_to course_boards_path(@course) + end + else + redirect_to board_message_url(@board, @topic, :r => @reply) + end end @@ -110,19 +239,36 @@ class MessagesController < ApplicationController else (render_403; return false) unless @message.course_editable_by?(User.current) end + @message.safe_attributes = params[:message] if request.post? && @message.save attachments = Attachment.attach_files(@message, params[:attachments]) render_attachment_warning_if_needed(@message) flash[:notice] = l(:notice_successful_update) @message.reload - redirect_to board_message_url(@message.board, @message.root, :r => (@message.parent_id && @message.id)) - elsif request.get? - respond_to do |format| - format.html { - layout_file = @project ? 'base_projects' : 'base_courses' - render :layout => layout_file - } + if params[:is_board] + if @project + redirect_to project_boards_path(@project) + elsif @course + redirect_to course_boards_path(@course) + end + else + redirect_to board_message_url(@message.board, @message.root, :r => (@message.parent_id && @message.id)) + end + elsif request.get? || request.post? + if params[:is_board] + if @project + redirect_to project_boards_path(@project) + elsif @course + redirect_to course_boards_path(@course) + end + else + respond_to do |format| + format.html { + layout_file = @project ? 'base_projects' : 'base_courses' + render :layout => layout_file + } + end end end end @@ -138,16 +284,20 @@ class MessagesController < ApplicationController @message.destroy # modify by nwb if @project - if @message.parent - redirect_to board_message_url(@board, @message.parent, :r => r) - else + if params[:is_board] redirect_to project_boards_url(@project) + else + redirect_to board_message_url(@board, @topic, :r => @reply) end elsif @course - if @message.parent - redirect_to board_message_url(@board, @message.parent, :r => r) + if params[:is_board] + redirect_to course_boards_url(@course) else - redirect_to course_board_url(@course, @board) + if @message.parent + redirect_to board_message_url(@board, @message.parent, :r => r) + else + redirect_to course_board_url(@course, @board) + end end end end @@ -158,16 +308,7 @@ class MessagesController < ApplicationController @content = "> #{ll(Setting.default_language, :text_user_wrote, @message.author)}\n> " @temp = Message.new - #@temp.content = "> #{ll(Setting.default_language, :text_user_wrote, @message.author)}> " - @content << @message.content.to_s.strip.gsub(%r{\n\n((.|\s)*?)}m, '[...]').gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n" - @content_html = textilizable(@content) - @temp.content = @content_html - #@content = "#{ll(Setting.default_language, :text_user_wrote, @message.author)}
" - #@content << @message.content.to_s.strip.gsub(%r{((.|\s)*?)}m, '[...]').gsub(/(\r?\n|\r\n?)/, "\n") + "
" << @content - #@temp = Message.new - #@temp.content = @content - + @temp.content = "#{ll(Setting.default_language, :text_user_wrote, @message.author)}".html_safe end def preview @@ -177,7 +318,7 @@ class MessagesController < ApplicationController render :partial => 'common/preview' end -private + private def find_message return unless find_board @message = @board.messages.find(params[:id], :include => :parent) diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rb index 8ee8d099e..69d8bc3a6 100644 --- a/app/controllers/my_controller.rb +++ b/app/controllers/my_controller.rb @@ -17,7 +17,10 @@ class MyController < ApplicationController layout "users_base" - before_filter :require_login + # edit + before_filter :auth_login1, :only => [:account] + # + before_filter :require_login, except: [:change_mail_notification] helper :issues helper :users @@ -72,6 +75,19 @@ class MyController < ApplicationController end end + def change_mail_notification + token = params[:token] + user = try_to_autologin1 + if user + user.mail_notification = params[:mail_notification] + user.save + flash[:notice] = l(:notice_mail_notification_updated) + redirect_to my_account_url + else + redirect_to signin_url + end + end + # Edit user's account def account @user = User.current @@ -79,76 +95,46 @@ class MyController < ApplicationController @pref = @user.pref diskfile = disk_filename('User', @user.id) diskfile1 = diskfile + 'temp' - if request.post? - @user.safe_attributes = params[:user] - @user.pref.attributes = params[:pref] - @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') - @user.login = params[:login] - unless @user.user_extensions.nil? - if @user.user_extensions.identity == 2 - @user.firstname = params[:enterprise_name] - end - end - - @se = @user.extensions - if params[:occupation].to_i.to_s == params[:occupation] - @se.school_id = params[:occupation] - else - @se.occupation = params[:occupation] - end - @se.gender = params[:gender] - @se.location = params[:province] if params[:province] - @se.location_city = params[:city] if params[:city] - @se.identity = params[:identity].to_i if params[:identity] - @se.technical_title = params[:technical_title] if params[:technical_title] - @se.student_id = params[:no] if params[:no] - - if @user.save && @se.save - # 头像保存 - if File.exist?(diskfile1) - if File.exist?(diskfile) - File.delete(diskfile) - end - File.open(diskfile1, "rb") do |f| - buffer = f.read(10) - if buffer != "DELETE" - File.open(diskfile1, "rb") do |f1| - File.open(diskfile, "wb") do |f| - buffer = "" - while (buffer = f1.read(8192)) - f.write(buffer) - end - end - end - - # File.rename(diskfile + 'temp',diskfile); - end + begin + if request.post? + @user.safe_attributes = params[:user] + @user.pref.attributes = params[:pref] + @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') + @user.login = params[:login] + unless @user.user_extensions.nil? + if @user.user_extensions.identity == 2 + @user.firstname = params[:enterprise_name] end end - # 确保文件被删除 - if File.exist?(diskfile1) - File.delete(diskfile1) + @se = @user.extensions + if params[:occupation].to_i.to_s == params[:occupation] + @se.school_id = params[:occupation] + else + @se.occupation = params[:occupation] end + @se.gender = params[:gender] + @se.location = params[:province] if params[:province] + @se.location_city = params[:city] if params[:city] + @se.identity = params[:identity].to_i if params[:identity] + @se.technical_title = params[:technical_title] if params[:technical_title] + @se.student_id = params[:no] if params[:no] - @user.pref.save - @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) - set_language_if_valid @user.language - flash[:notice] = l(:notice_account_updated) - redirect_to user_url(@user) - return - else - # 确保文件被删除 - if File.exist?(diskfile1) - File.delete(diskfile1) + if @user.save && @se.save + # 头像保存 + FileUtils.mv diskfile1, diskfile, force: true if File.exist? diskfile1 + @user.pref.save + @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) + set_language_if_valid @user.language + flash[:notice] = l(:notice_account_updated) + redirect_to user_url(@user) + return + else + @user.login = lg end - @user.login = lg - end - else - # 确保文件被删除 - if File.exist?(diskfile1) - File.delete(diskfile1) end + ensure + File.delete(diskfile1) if File.exist?(diskfile1) end end @@ -180,17 +166,24 @@ class MyController < ApplicationController return end if request.post? - if @user.check_password?(params[:password]) - @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] - - if @user.save - flash.now[:notice] = l(:notice_account_password_updated) - redirect_to my_account_url - end + us = UsersService.new + @user = us.change_password params.merge(:current_user_id => @user.id) + if @user.errors.full_messages.count <= 0 + flash.now[:notice] = l(:notice_account_password_updated) + # 修改完密码,让其重新登录,并更新Token + Token.delete_user_all_tokens(@user) + logout_user + redirect_to signin_url(back_url: my_account_path) else flash.now[:error] = l(:notice_account_wrong_password) end end + rescue Exception => e + if e.message == 'wrong password' + flash.now[:error] = l(:notice_account_wrong_password) + else + flash.now[:error] = e.message + end end # Create a new feeds key diff --git a/app/controllers/news_controller.rb b/app/controllers/news_controller.rb index 23f8c18e9..f71a77ca7 100644 --- a/app/controllers/news_controller.rb +++ b/app/controllers/news_controller.rb @@ -17,6 +17,8 @@ class NewsController < ApplicationController layout 'base_projects'# by young + include ApplicationHelper + before_filter :authorize1, :only => [:show] default_search_scope :news model_object News before_filter :find_model_object, :except => [:new, :create, :index] @@ -69,13 +71,13 @@ class NewsController < ApplicationController scope = @course ? @course.news.course_visible : News.course_visible @news_count = scope.count - @news_pages = Paginator.new @news_count, @limit, params['page'] - @offset ||= @news_pages.offset - @newss = scope.all(:include => [:author, :course], - :order => "#{News.table_name}.created_on DESC", - :offset => @offset, - :limit => @limit) - + #@news_pages = Paginator.new @news_count, @limit, params['page'] + #@offset ||= scope_page.offset + scope_order = scope.all(:include => [:author, :course], + :order => "#{News.table_name}.created_on DESC") + # :offset => @offset, + # :limit => @limit) + @newss = paginateHelper scope_order,10 respond_to do |format| format.html { @news = News.new @@ -91,8 +93,12 @@ class NewsController < ApplicationController end def show - @comments = @news.comments - @comments.reverse! if User.current.wants_comments_in_reverse_order? + cs = CoursesService.new + result = cs.show_course_news params,User.current + @news = result[:news] + @comments = result[:comments] + #@comments = @news.comments + #@comments.reverse! if User.current.wants_comments_in_reverse_order? #modify by nwb if @news.course_id @course = Course.find(@news.course_id) @@ -131,6 +137,10 @@ class NewsController < ApplicationController @news.safe_attributes = params[:news] @news.save_attachments(params[:attachments]) if @news.save + if params[:asset_id] + ids = params[:asset_id].split(',') + update_kindeditor_assets_owner ids,@news.id,OwnerTypeHelper::NEWS + end render_attachment_warning_if_needed(@news) flash[:notice] = l(:notice_successful_create) redirect_to course_news_index_url(@course) @@ -142,6 +152,9 @@ class NewsController < ApplicationController end def edit + if @course + render :layout => "base_courses" + end end def update diff --git a/app/controllers/organization_controller.rb b/app/controllers/organization_controller.rb new file mode 100644 index 000000000..62e6b85f6 --- /dev/null +++ b/app/controllers/organization_controller.rb @@ -0,0 +1,55 @@ +class OrganizationController < ApplicationController + layout 'base_projects' + before_filter :require_admin, :except => [:index] + + def index + #@projects = Project.find_by_sql("SELECT * FROM projects WHERE id IN (select MAX(id) from projects GROUP BY enterprise_name)") + @organizations = Organization.all + respond_to do |format| + format.html + end + end + + def new + @organizations = Organization.new + respond_to do |format| + format.html + end + end + + def create + @organizations = Organization.new + @organizations.name = params[:organization][:name] + if @organizations.save + redirect_to admin_organization_url + end + end + + def edit + @organization = Organization.find params[:id] + respond_to do |format| + format.html + end + rescue Exception => e + render_404 + end + + def update + @organization = Organization.find params[:id] + @organization.name = params[:organization][:name] + if @organization.save + redirect_to admin_organization_url + end + rescue Exception => e + render_404 + end + + def destroy + @organization = Organization.find params[:id] + if @organization.destroy + redirect_to admin_organization_url + end + rescue Exception => e + render_404 + end +end diff --git a/app/controllers/poll_controller.rb b/app/controllers/poll_controller.rb index 739adfe1c..d8a9d88c3 100644 --- a/app/controllers/poll_controller.rb +++ b/app/controllers/poll_controller.rb @@ -1,8 +1,8 @@ class PollController < ApplicationController - before_filter :find_poll_and_course, :only => [:edit,:update,:destroy,:show,:statistics_result,:create_poll_question,:commit_poll,:commit_answer,:publish_poll] + before_filter :find_poll_and_course, :only => [:edit,:update,:destroy,:show,:statistics_result,:create_poll_question,:commit_poll,:commit_answer,:publish_poll,:republish_poll,:poll_result,:close_poll,:export_poll] before_filter :find_container, :only => [:new,:create, :index] - before_filter :is_member_of_course, :only => [:index,:show] - before_filter :is_course_teacher, :only => [:new,:create,:edit,:update,:destroy,:publish_poll] + before_filter :is_member_of_course, :only => [:index,:show,:poll_result] + before_filter :is_course_teacher, :only => [:new,:create,:edit,:update,:destroy,:publish_poll,:republish_poll,:close_poll,:export_poll] include PollHelper def index if @course @@ -23,9 +23,13 @@ class PollController < ApplicationController def show @poll = Poll.find params[:id] + if @poll.polls_status != 2 && !User.current.allowed_to?(:as_teacher,@course) + render_403 + return + end #已提交问卷的用户不能再访问该界面 if has_commit_poll?(@poll.id,User.current.id) && (!User.current.admin?) - render_403 + redirect_to poll_index_url(:polls_type => "Course", :polls_group_id => @course.id) else @can_edit_poll = (!has_commit_poll?(@poll.id,User.current.id)) || User.current.admin? @percent = get_percent(@poll,User.current) @@ -40,13 +44,14 @@ class PollController < ApplicationController def new if @course option = { - :polls_name => l(:label_poll_new), + :polls_name => "", :polls_type => @course.class.to_s, :polls_group_id => @course.id, :polls_status => 1, :user_id => User.current.id, :published_at => Time.now, :closed_at => Time.now, + :show_result => 1, :polls_description => "" } @poll = Poll.create option @@ -69,8 +74,8 @@ class PollController < ApplicationController end def update - @poll.polls_name = params[:polls_name].empty? ? l(:label_poll_title) : params[:polls_name] - @poll.polls_description = params[:polls_description].empty? ? l(:label_poll_description) : params[:polls_description] + @poll.polls_name = params[:polls_name] + @poll.polls_description = params[:polls_description] if @poll.save respond_to do |format| format.js @@ -82,6 +87,12 @@ class PollController < ApplicationController def destroy if @poll && @poll.destroy + if @is_teacher + polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id}") + else + polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id} and polls_status = 2") + end + @polls = paginateHelper polls,20 #分页 respond_to do |format| format.js end @@ -187,7 +198,13 @@ class PollController < ApplicationController @poll.polls_status = 2 @poll.published_at = Time.now if @poll.save - redirect_to poll_index_url(:polls_type => "Course", :polls_group_id => @course.id) + if params[:is_remote] + redirect_to poll_index_url(:polls_type => "Course", :polls_group_id => @course.id) + else + respond_to do |format| + format.js + end + end end end @@ -311,6 +328,49 @@ class PollController < ApplicationController end end + #重新发布问卷 + def republish_poll + @poll.poll_questions.each do |poll_question| + poll_question.poll_votes.destroy_all + end + @poll.poll_users.destroy_all + @poll.polls_status = 1 + @poll.save + respond_to do |format| + format.js + end + end + + #显示某个学生某份问卷的填写结果 + def poll_result + @poll_questions = paginateHelper @poll.poll_questions,5 + respond_to do |format| + format.html{render :layout => 'base_courses'} + end + end + + #关闭问卷 + def close_poll + @poll.polls_status = 3 + @poll.closed_at = Time.now + if @poll.save + respond_to do |format| + format.js + end + end + end + + #导出问卷 + def export_poll + poll_questions = @poll.poll_questions + respond_to do |format| + format.xls { + send_data(poll_to_xls(poll_questions), :type => "text/excel;charset=utf-8; header=present", + :filename => "#{@poll.polls_name}.xls") + } + end + end + private def find_poll_and_course @poll = Poll.find params[:id] @@ -337,7 +397,8 @@ class PollController < ApplicationController end def is_course_teacher - render_403 unless(@course && User.current.allowed_to?(:as_teacher,@course)) + @is_teacher = User.current.allowed_to?(:as_teacher,@course) + render_403 unless(@course && @is_teacher) end #获取未完成的题目 @@ -388,4 +449,59 @@ class PollController < ApplicationController end pu end + + #将poll中题目转换为Excel + def poll_to_xls poll_questions + xls_report = StringIO.new + book = Spreadsheet::Workbook.new + sheet1 = book.create_worksheet :name => "poll" + blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10 + count_row = 0 + poll_questions.each do |poll_question| + if poll_question.question_type == 1 || poll_question.question_type == 2 + sheet1.row(count_row).default_format = blue + sheet1[count_row,0]= l(:label_poll_question_num,:num => poll_question.question_number) + sheet1[count_row + 1,0] = l(:label_poll_subtotal) + sheet1[count_row + 2,0] = l(:label_poll_proportion) + poll_question.poll_answers.each_with_index do |poll_answer,i| + sheet1[count_row, i + 1] = poll_answer.answer_text.gsub(/<\/?.*?>/,"").gsub(/ /," ") + sheet1[count_row + 1, i + 1] = poll_answer.poll_votes.count + sheet1[count_row + 2, i + 1] = statistics_result_percentage(poll_answer.poll_votes.count, total_answer(poll_question.id)).to_s + "%" + end + sheet1[count_row + 3,0] = l(:label_poll_valid_commit) + sheet1[count_row + 3,1] = total_answer(poll_question.id) + count_row += 5 + else + sheet1.row(count_row).default_format = blue + sheet1[count_row,0] = l(:label_poll_question_num,:num => poll_question.question_number) + sheet1[count_row,1] = poll_question.question_title + count_row += 1 + poll_question.poll_votes.each do |poll_vote| + sheet1[count_row,0] = poll_vote.vote_text.gsub(/<\/?.*?>/,"").gsub(/ /," ") + count_row += 1 + end + count_row += 1 + end + end + + sheet1.row(count_row).default_format = blue + sheet1[count_row ,0] = l(:label_bidding_user_studentname) + poll_questions.each_with_index do |poll_question, i| + sheet1[count_row ,i + 1] = poll_question.question_title + end + count_row += 1 + @poll.users.each do |user| + sheet1[count_row ,0] = user.show_name + poll_questions.each_with_index do |poll_question, i| + if poll_question.question_type == 1 || poll_question.question_type == 2 + sheet1[count_row ,i + 1] = user.poll_votes.where(:poll_question_id => poll_question.id).map{|poll_vote| poll_vote.poll_answer.answer_text.gsub(/<\/?.*?>/,"").gsub(/ /," ") if poll_vote.poll_answer}.join(";") + else + sheet1[count_row ,i + 1] = user.poll_votes.where(:poll_question_id => poll_question.id).map{|poll_vote| poll_vote.vote_text.gsub(/<\/?.*?>/,"").gsub(/ /," ")}.join(";") + end + end + count_row += 1 + end + book.write xls_report + xls_report.string + end end \ No newline at end of file diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 5ff9a3f17..e6e93947c 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -14,39 +14,28 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# Time 2015-01-28 16:34:21 +# Author lizanle +# Description 封装代码,简化代码,格式化代码, class ProjectsController < ApplicationController layout :select_project_layout - - menu_item :overview + before_filter :authorize1, :only => [:show] + menu_item :overview, :only => :show menu_item :roadmap, :only => :roadmap menu_item :settings, :only => :settings - - 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 - - # menu_item l(:label_homework), :only => :homework - # menu_item l(:label_course_feedback), :only => :feedback menu_item :homework, :only => [:homework, :new_homework] menu_item :feedback, :only => :feedback - menu_item l(:label_course_file), :only => :index - menu_item l(:label_course_news), :only => :index - + menu_item :share, :only => :share - before_filter :find_project, :except => [ :index, :search,:list, :new, :create, :copy, :statistics, :new_join, :course, :enterprise_course, :course_enterprise,:view_homework_attaches] - # before_filter :authorize, :except => [:new_join, :new_homework, :homework, :statistics, :search, :watcherlist, :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy, :member, :focus, :file, - # :statistics, :feedback, :course, :enterprise_course, :course_enterprise, :project_respond, :share, - # :show_projects_score, :issue_score_index, :news_score_index, :file_score_index, :code_submit_score_index, :projects_topic_score_index] - #此条勿删 课程相关权限 ,:new_homework,:homework,:feedback,,:member + before_filter :find_project, :except => [ :index, :search,:list, :new, :create, :copy, :statistics, :new_join, :course, :enterprise_course, :course_enterprise,:view_homework_attaches,:join_project] before_filter :authorize, :only => [:show, :settings, :edit, :sort_project_members, :update, :modules, :close, :reopen,:view_homework_attaches,:course] before_filter :authorize_global, :only => [:new, :create,:view_homework_attaches] before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy, :calendar] - before_filter :file, :statistics, :watcherlist - #before_filter :find_project_repository, :only => [:show] + before_filter :file, :statistics #:watcherlist # 除非项目内人员,不可查看成员, TODO: 完了写报表里去 before_filter :memberAccess, only: :member - accept_rss_auth :index + # accept_rss_auth :index accept_api_auth :index, :show, :create, :update, :destroy after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller| @@ -80,369 +69,66 @@ class ProjectsController < ApplicationController ### added by william include ActsAsTaggableOn::TagsHelper - def find_project_repository - unless @project.repositories.nil? - @project.repositories.each do |repository| - repository.fetch_changesets if Setting.autofetch_changesets? - end - end - end - - def enterprise_course - session[:enterprise_college] = 2 - respond_to do |format| - format.html { redirect_to :back } - #format.api { render_api_ok } - end - end - - def course_enterprise - session[:enterprise_college] = 1 - respond_to do |format| - format.html { redirect_to :back } - #format.api { render_api_ok } - end - end - def index render_404 - #调用存储过程更新提交次数 - #ActiveRecord::Base.connection.execute("CALL sp_project_status_cursor();") - #Modified by nie - @project_type = params[:project_type].to_i - per_page_option = 10 - - @projects_all = Project.active.visible. - joins("LEFT JOIN #{ProjectStatus.table_name} ON #{Project.table_name}.id = #{ProjectStatus.table_name}.project_id").joins("LEFT JOIN #{ProjectScore.table_name} ON #{Project.table_name}.id = #{ProjectScore.table_name}.project_id"). - where("#{Project.table_name}.project_type = ? ", Project::ProjectType_project) - - @poll_questions_count = @projects_all.count - @poll_questions_pages = Paginator.new @project_count, per_page_option, params['page'] - -#gcm activity count - - @project_activity_count=Hash.new - - @projects_all.each do |project| - @project_activity_count[project.id]=0 - end - - @project_activity_count=get_project_activity @projects_all,@project_activity_count - -#gcm end - - case params[:project_sort_type] - when '0' - @projects = @projects_all.order("created_on desc") - @s_type = 0 - when '1' - @projects = @projects_all.order("score desc") - @s_type = 1 - when '2' - @projects = @projects_all.order("watchers_count desc") - @s_type = 2 - - #gcm - when '3' - #@projects=desc_sort_course_by_avtivity(@project_activity_count_array,@project_all_array) - @projects=handle_project @projects_all,@project_activity_count - @s_type = 3 - @projects = @projects[@project_pages.offset, @project_pages.per_page] - - else - @projects = @projects = @projects_all.order("score desc") - @s_type = 1 - end - @projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page) - - - respond_to do |format| - format.html { - # render :layout => 'base' - # scope = Project - # unless params[:closed] - # scope = scope.active - # end - } - format.api { - # @offset, @limit = api_offset_and_limit - # @project_count = Project.visible.count - # @projects = Project.visible.offset(@offset).limit(@limit).order('lft').all - } - format.atom { - projects = Project.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all - render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}") - } - end end def course - @project_type = params[:project_type] - @school_id = params[:school_id] - per_page_option = 10 - if @school_id == "0" or @school_id.nil? - @projects_all = Project.active.visible. - joins("LEFT JOIN #{ProjectStatus.table_name} ON #{Project.table_name}.id = #{ProjectStatus.table_name}.project_id"). - where("#{Project.table_name}.project_type = ? ", Project::ProjectType_course) - else - @projects_all = Project.active.visible. - joins("LEFT JOIN #{ProjectStatus.table_name} ON #{Project.table_name}.id = #{ProjectStatus.table_name}.project_id"). - joins(:course_extra). - where("#{Project.table_name}.project_type = ? AND #{Course.table_name}.school_id = ?", Project::ProjectType_course, @school_id) - end - - @project_count = @projects_all.count - @project_pages = Paginator.new @project_count, per_page_option, params['page'] - - #gcm activity count - - @project_activity_count=Hash.new - #count initialize - @projects_all.each do |project| - @project_activity_count[project.id]=0 - end - - #@project_activity_count=get_project_activity @projects_all,@project_activity_count - #gcm end - - - case params[:project_sort_type] - when '0' - @projects = @projects_all.order("created_on desc") - @s_type = 0 - @projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page) - - #gcm - @project_activity_count=get_project_activity @projects,@project_activity_count - #gcmend - - when '1' - @projects = @projects_all.order("course_ac_para desc") - @s_type = 1 - @projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page) - - #gcm - @project_activity_count=get_project_activity @projects,@project_activity_count - #gcmend - - when '2' - @projects = @projects_all.order("watchers_count desc") - @s_type = 2 - @projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page) - - #gcm - @project_activity_count=get_project_activity @projects,@project_activity_count - #gcmend - - #gcm - when '3' - - #gcm - @project_activity_count=get_project_activity @projects_all,@project_activity_count - #gcmend - - @projects=handle_project @projects_all,@project_activity_count - @s_type = 3 - @projects = @projects[@project_pages.offset, @project_pages.per_page] - else - @s_type = 0 - @projects = @projects_all.order("created_on desc") - @projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page) - - #gcm - @project_activity_count=get_project_activity @projects,@project_activity_count - #gcmend - - end - - respond_to do |format| - format.html { - render :layout => 'base' - } - format.api { - # @offset, @limit = api_offset_and_limit - # @project_count = Project.visible.count - # @projects = Project.visible.offset(@offset).limit(@limit).order('lft').all - } - format.atom { - projects = Project.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all - render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}") - } - end + render_404 end -#gcm + # Time 2015-01-29 11:19:11 + # Author lizanle + # Description 项目搜索方法 def search - #modified by nie - project_type = params[:project_type].to_i - projects_all = (project_type.eql? Project::ProjectType_course) ? Project.course_entities : Project.project_entities - @projects = projects_all.visible - #@projects_all = @projects.visible.like(params[:name]) if params[:name].present? + # 如果有名字,就按名字搜索,如果没有,就展示所有,用Karminari分页 if params[:name].present? - @projects_all = @projects.visible.like(params[:name]) + @project_pages = Project.project_entities.visible.like(params[:name]).page(params[:page]).per(10) else - @projects_all = @projects; + @project_pages = Project.project_entities.visible.page(params[:page] ).per(10) end - @project_count = @projects_all.count - @project_pages = Paginator.new @project_count, per_page_option, params['page'] - - #gcm activity count - - @project_activity_count=Hash.new - # count initialize - @projects_all.each do |project| - @project_activity_count[project.id]=0 - end - - #@project_activity_count=get_project_activity @projects_all,@project_activity_count - - #gcm end - - - case params[:project_sort_type] - when '0' - @projects = @projects_all.order("created_on desc") - @s_type = 0 - @projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page) - - #gcm - @project_activity_count=get_project_activity @projects,@project_activity_count - #gcmend - - when '1' - @projects = @projects_all.order("course_ac_para desc") - @s_type = 1 - @projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page) - - #gcm - @project_activity_count=get_project_activity @projects,@project_activity_count - #gcmend - - when '2' - @projects = @projects_all.order("watchers_count desc") - @s_type = 2 - @projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page) - - #gcm - @project_activity_count=get_project_activity @projects,@project_activity_count - #gcmend - - when '3' - #@projects=desc_sort_course_by_avtivity(@project_activity_count_array,@project_all_array) - @project_activity_count=get_project_activity @projects_all,@project_activity_count_array #gcm - @projects=handle_project @projects_all,@project_activity_count - @s_type = 3 - @projects = @projects[@project_pages.offset, @project_pages.per_page] - - else - @s_type = 0 - @projects = @projects_all.order("created_on desc") - @projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page) - - #gcm - @project_activity_count=get_project_activity @projects,@project_activity_count - #gcmend - - end - + @projects = @project_pages.order("created_on desc") respond_to do |format| format.html { render :layout => 'base' scope = Project unless params[:closed] - scope = scope.active + scope = scope.active end } - format.api { - # @offset, @limit = api_offset_and_limit - # @project_count = Project.visible.count - # @projects = Project.visible.offset(@offset).limit(@limit).order('lft').all - } + # 需要到处atom使用的格式 (redmine自带) format.atom { projects = Project.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}") } end end -#gcmend - - - #Added by young - def homework - @offset, @limit = api_offset_and_limit({:limit => 10}) - @bids = @project.homeworks.order('deadline') - @bids = @bids.like(params[:name]) if params[:name].present? - - @bids = paginateHelper @bids - render :layout => 'base_courses' - - end - - def new_homework - # Dear maintainer: - # once you are done trying to 'optimize' this Magic routine, - # well, it's on you, I'll leave it to you. - if (User.current.logged? && - (User.current.admin? || - ( - !Member.where('user_id = ? and project_id = ?', User.current.id, @project.id).first.nil? && - ( - Member.where('user_id = ? and project_id = ?', User.current.id, @project.id).first.roles && - ( Role.where(id: [3, 4, 7, 9]).size > 0 ) - ) - ) - ) - ) - @homework = Bid.new - @homework.safe_attributes = params[:bid] - render :layout => 'base_courses' - else - render_404 - end - - end - #Ended by young + # Time 2015-01-29 16:13:20 + # Author lizanle + # Description 项目首页中用户反馈 方法 def feedback - @page = params[:page] - @page = @page.to_i + @page = params[:page].to_i # Find the page of the requested reply @jours = @project.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC') - @limit = 10 + limit = 10 offset = @jours.count(:conditions => ["#{JournalsForMessage.table_name}.id > ?", params[:r].to_i]) - page = 1 + offset / @limit + page = 1 + offset / limit if params[:r] && @page.nil? - @page = page + @page = @page < 0 ? 1 : @page end - - puts @page - if @page < 0 - @page = 1 - end - if @page > page - @page = page - end - - - #@feedback_count = @jours.count - #@feedback_pages = Paginator.new @feedback_count, @limit, @page - #@offset ||= @feedback_pages.offset + @page = @page > page ? page : @page @jour = paginateHelper @jours,10 @state = false - @base_courses_tag = @project.project_type - - respond_to do |format| - format.html{render :layout => 'base_courses' if @base_courses_tag==1} + format.html format.api end end def project_respond - # will_reply = JournalsForMessage.find(params[:reference_id]) if params[:reference_id] project_id = request.headers["Referer"].match((%r|/([0-9]{1,})/|))[1] - # @project = Project.find_by_id(project_id) parent_id = params[:reference_id] author_id = User.current.id reply_user_id = params[:reference_user_id] @@ -458,12 +144,8 @@ class ProjectsController < ApplicationController @jfm = Project.add_new_jour(nil, nil, project_id, options) @save_succ = @jfm.errors.empty? - # flash[:notice]=l(:label_projects_feedback_respond_success) - respond_to do |format| - # format.html { redirect_to :back } format.js - #format.api { render_api_ok } end end @@ -489,6 +171,9 @@ class ProjectsController < ApplicationController @trackers = Tracker.sorted.all @project = Project.new @project.safe_attributes = params[:project] + @project.organization_id = params[:organization_id] + @project.user_id = User.current.id + @project.project_new_type = 1 if validate_parent_id && @project.save @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') # Add current user as a project member if he is not admin @@ -554,40 +239,18 @@ class ProjectsController < ApplicationController render_404 end - # Show @project + # Time 2015-01-29 10:42:00 + # Author lizanle + # Description 项目动态展示方法,删除了不必要的代码 def show - @project_type = params[:project_type] - - # try to redirect to the requested menu item + if params[:jump] && redirect_to_project_menu_item(@project, params[:jump]) - return + return end - - @users_by_role = @project.users_by_role - @subprojects = @project.children.visible.all - @news = @project.news.limit(5).includes(:author, :project).reorder("#{News.table_name}.created_on DESC").all - @trackers = @project.rolled_up_trackers - if(User.find_by_id(ProjectInfo.find_by_project_id(@project.id).try(:user_id))) - @user = User.find_by_id(ProjectInfo.find_by_project_id(@project.id).user_id) - end +=begin cond = @project.project_condition(Setting.display_subprojects_issues?) - @open_issues_by_tracker = Issue.visible.open.where(cond).count(:group => :tracker) - @total_issues_by_tracker = Issue.visible.where(cond).count(:group => :tracker) - - if User.current.allowed_to?(:view_time_entries, @project) - @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f - end - - @key = User.current.rss_key - #新增内容 - @days = Setting.activity_days_default.to_i - - if params[:from] - begin; @date_to = params[:from].to_date + 1; rescue; end - end - has = { - "show_issues" => true, + "show_issues" => true , "show_files" => true, "show_documents" => true, "show_messages" => true, @@ -597,60 +260,49 @@ class ProjectsController < ApplicationController "show_wiki_edits"=>true, "show_journals_for_messages" => true } - - + # 读取项目默认展示的动态时间天数 + @days = Setting.activity_days_default.to_i + @date_to ||= Date.today + 1 + # 时间跨度不能太大,不然很慢,所以删掉了-1.years + @date_from = @date_to - @days @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1') - @author = (params[:user_id].blank? ? nil : User.active.find(params[:user_id])) +=end + + @author = params[:user_id].blank? ? nil : User.active.find(params[:user_id]) # 决定显示所用用户或单个用户活动 - @activity = Redmine::Activity::Fetcher.new(User.current, :project => @project, - :with_subprojects => @with_subprojects, - :author => @author) +=begin + @activity = Redmine::Activity::Fetcher.new(User.current, + :project => @project, + :with_subprojects => @with_subprojects, + :author => @author) @activity.scope_select {|t| !has["show_#{t}"].nil?} - # logger.debug "=========================================#{@activity.scope}" - # @activity.scope = (@author.nil? ? :default : :all) if @activity.scope.empty? +=end - # modify by nwb - # 添加私密性判断 - if User.current.member_of?(@project)|| User.current.admin? - events = @activity.events(@days) + # 根据私密性,取出符合条件的所有数据 + if User.current.member_of?(@project) || User.current.admin? + @events_pages = ForgeActivity.where("project_id = ?",@project).order("created_at desc").page(params['page'|| 1]).per(20); + #events = @activity.events(@date_from, @date_to) else - events = @activity.events(@days,nil, :is_public => 1) + @events_pages = ForgeActivity.includes(:project).where("forge_activities.project_id = ? and projects.is_public + = ?",@project,1).order("created_at desc") + .page(params['page'|| 1]).per(10); + # @events = @activity.events(@date_from, @date_to, :is_public => 1) end - @offset, @limit = api_offset_and_limit({:limit => 10}) - @events_count = events.count - @events_pages = Paginator.new @events_count, @limit, params['page'] - @offset ||= @events_pages.offset - events = events.slice(@offset,@limit) - #Ended by young +=begin + @events_pages = Paginator.new events.count, 10, params['page'] + # 总的数据中取出某一页 + events = events.slice(@events_pages.offset,10) + # 按天分组 @events_by_day = events.group_by {|event| User.current.time_to_date(event.event_datetime)} - # documents - @sort_by = %w(category date title author).include?(params[:sort_by]) ? params[:sort_by] : 'category' - documents = @project.documents.includes(:attachments, :category).all - case @sort_by - when 'date' - @grouped = documents.group_by {|d| d.updated_on.to_date } - when 'title' - @grouped = documents.group_by {|d| d.title.first.upcase} - when 'author' - @grouped = documents.select{|d| d.attachments.any?}.group_by {|d| d.attachments.last.author} - else - @grouped = documents.group_by(&:category) - end - @document = @project.documents.build - # - @base_courses_tag = @project.project_type - #判断能否显示真名(当前用户为课程的教师时显示真名) - if @project.project_type == Project::ProjectType_course - @teachers= searchTeacherAndAssistant(@project) - @canShowRealName = isCourseTeacher(User.current.id) - end - - # real_name action为虚拟的该方法并不存在,用来辅助判断真名权限 - # @canShowRealName = User.current.allowed_to?({:controller => "projects", :action => "real_name"}, @project || @projects, :global => false) +=end + boards = @project.boards.includes(:last_message => :author).all + @topic_count = @project.boards.count + # 根据对应的请求,返回对应的数据 respond_to do |format| - format.html{render :layout => 'base_courses' if @base_courses_tag==1} + format.html format.api + format.js end end @@ -660,26 +312,125 @@ class ProjectsController < ApplicationController @member ||= @project.members.new @trackers = Tracker.sorted.all @wiki ||= @project.wiki - #Added by young - # @course_tag = params[:course] - # if @course_tag == '1' - #@course = Course.find_by_extra(@project.identifier) - # if @project.project_type == 1 - # render :layout => 'base_courses' - # else - # render :layout => 'base_projects' - # end - #Ended by young + @select_tab = params[:tab] + + # 处理从新建版本库返回来的错误信息 + if !params[:repository_error_message].to_s.blank? + html = "" + errors = params[:repository_error_message].flatten + errors.each do |error| + # 版本库路径为空的错误信息不予提示 + if(error!=l(:label_repository_path_not_null)) + html << error << ";" + end + end + if params[:repository] == "pswd_is_null" + html << l(:label_password_not_null) + end + flash[:error] = html if !html.to_s.blank? + end + scm = params[:repository_scm] || (Redmine::Scm::Base.all & Setting.enabled_scm).first + @repository = Repository.factory(scm) + @repository.is_default = @project.repository.nil? + @repository.project = @project + + end + + # 两种情况:1、系统外用户;2、系统内用户 (通过邮件判定) + def send_mail_to_member + if !params[:mail].blank? && User.find_by_mail(params[:mail].to_s).nil? + email = params[:mail] + Mailer.run.send_invite_in_project(email, @project, User.current) + @is_zhuce = false + flash[:notice] = l(:notice_email_sent, :value => email) + elsif !User.find_by_mail(params[:mail].to_s).nil? + user = User.find_by_mail(params[:mail].to_s) + if !user.member_of?(@project) + email = params[:mail] + Mailer.run.request_member_to_project(email, @project, User.current) + flash[:notice] = l(:notice_email_sent, :value => email) + else + flash[:error] = l(:label_member_of_project, :value => email) + end + else + flash[:error] = l(:notice_registed_error, :value => email) + @is_zhuce = true + end + respond_to do |format| + format.html{redirect_to invite_members_by_mail_project_url(@project)} + end + end + + #发送邮件邀请新用户 + def invite_members_by_mail + if User.current.member_of?(@project) || User.current.admin? + @inviter_lists = InviteList.where(project_id:@project.id).all + @inviters = [] + @waiters = [] + unless @inviter_lists.blank? + @inviter_lists.each do|inviter_list| + unless inviter_list.user.nil? + if inviter_list.user.member_of?(@project) + @inviters << inviter_list.user + @inviters_count = @inviters.size + else + @waiters << inviter_list.user + @waiters_count = @waiters.size + end + end + end + end + @is_zhuce = false + respond_to do |format| + format.html + format.js + end + else + render_403 + end + end + + # 邀请Trustie注册用户 + def invite_members + if User.current.member_of?(@project) || User.current.admin? + @member ||= @project.members.new + respond_to do |format| + format.html + end + else + render_403 + end end def edit end - #by young - include CoursesHelper + # by young + # include CoursesHelper def member + # params[:login]为邮箱邀请用户加入,主要功能: + # 1、自动注册 + # 2、加入项目、创建角色 + # 3、用户得分 + if params[:mail] + userid = Token.find_by_value(params[:token]).user_id + user = User.find(userid) + user.activate! + Member.create(:role_ids => [4], :user_id => userid, :project_id => params[:id]) + UserGrade.create(:user_id => userid, :project_id => params[:id]) + token = Token.get_token_from_user(user, 'autologin') + #user = User.try_to_autologin(token.value) + if user + start_user_session(user) + user.save + redirect_to project_member_path(params[:id]) + return + # account_ project_member_path(params[:id]) + flash[:notice] = l(:label_mail_invite_success) + end + end ## 有角色参数的才是课程,没有的就是项目 - @render_file = 'member_list' + @render_file = 'project_member_list' # 判断是否课程 if @project.project_type == Project::ProjectType_course @teachers= searchTeacherAndAssistant(@project) @@ -687,23 +438,25 @@ class ProjectsController < ApplicationController case params[:role] when '1' @subPage_title = l :label_teacher_list - @members = searchTeacherAndAssistant(@project) + @members = searchTeacherAndAssistant(@project) when '2' - @subPage_title = l :label_student_list + @subPage_title = l :label_student_list @members = searchStudent(@project) else @subPage_title = '' @members = @project.member_principals.includes(:roles, :principal).all.sort end - else # @project.project_type == Project::ProjectType_project - roles = Role.find_all_givable - @subPage_title = l :label_member_list - @members = @project.member_principals.includes(:roles, :principal).joins("LEFT JOIN #{OptionNumber.table_name} ON #{OptionNumber.table_name}.user_id = #{Member.table_name}.user_id and #{OptionNumber.table_name}.score_type = 2 AND #{Member.table_name}.project_id = #{OptionNumber.table_name}.project_id").order("#{OptionNumber.table_name}.total_score DESC").all - #@members = sort_project_members(@project, @members) - @applied_members = appied_project_members(@project, @members) + else + if !@project.is_public? && !User.current.member_of?(@project) && !User.current.admin? + render_403 + else + roles = Role.find_all_givable + @subPage_title = l :label_member_list + @members = @project.member_principals.includes(:roles, :principal).joins("LEFT JOIN #{OptionNumber.table_name} ON #{OptionNumber.table_name}.user_id = #{Member.table_name}.user_id and #{OptionNumber.table_name}.score_type = 2 AND #{Member.table_name}.project_id = #{OptionNumber.table_name}.project_id").order("#{OptionNumber.table_name}.total_score DESC").all + @applied_members = appied_project_members(@project, @members) + end end @members = paginateHelper @members - render :layout => 'base_courses' if @project.project_type == 1 end #判断指定用户是否为课程教师 @@ -755,22 +508,11 @@ class ProjectsController < ApplicationController def update @project.safe_attributes = params[:project] - #@project.dts_test = params[:project][:dts_test] + @project.organization_id = params[:organization_id] + params[:project][:is_public] ? @project.is_public = 1 : @project.is_public = 0 + params[:project][:hidden_repo] ? @project.hidden_repo = 1 : @project.hidden_repo = 0 if validate_parent_id && @project.save - @course = Course.find_by_extra(@project.identifier) - unless @course.nil? - @course.password = params[:project][:course][:password] - # added by bai - @course.term = params[:term] - @course.time = params[:time] - @course.setup_time = params[:setup_time] - @course.endup_time = params[:endup_time] - @course.class_period = params[:class_period] - # end - @course.save - end @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') - if params[:project][:is_public] == '0' project_status = ProjectStatus.find_by_project_id(@project.id) project_status.destroy if project_status @@ -836,7 +578,7 @@ class ProjectsController < ApplicationController format.api { render_api_ok } end else - render :layout => "project_base" + render :layout => "base_projects" end # hide project in layout @project = nil @@ -844,7 +586,7 @@ class ProjectsController < ApplicationController def show_projects_score respond_to do |format| - format.html { render :layout => "project_base"} + format.html { render :layout => "base_projects"} format.js end end @@ -910,13 +652,32 @@ class ProjectsController < ApplicationController end end end + + #加入私有项目 + def join_project + respond_to do |format| + format.js + end + end + + #朋友圈、科研组、开发组之间的切换 + def change_project_type + @project.project_new_type = params[:project_type] + if @project.save + message = @project.project_new_type + else + message = "0" + end + render :json => message + end + private def memberAccess - # 是课程,则判断当前用户是否参加了课程 - return 0 if @project.project_type == Project::ProjectType_project - currentUser = User.current - render_403 unless currentUser.member_of?(@project) + # 如果是私有项目,项目成员不对外公开,公开项目成员列表对外公开。 + unless @project.is_public? + render_403 unless User.current.member_of?(@project) + end end def toggleCourse @@ -948,98 +709,15 @@ class ProjectsController < ApplicationController true end - # added by huang - def watcherlist - if @watched - @users -= watched.watcher_users - end - end - - - - #gcm - def get_project_activity projects,activities - @project_ids=activities.keys() - - days = Setting.activity_days_default.to_i - date_to ||= Date.today + 1 - date_from = date_to - days-1.years - - #issue_count - Issue.where(project_id: @project_ids).where("updated_on>?",date_from).each do |issue| -# activities[issue.project_id.to_s]+=1 - activities[issue.project_id]+=issue.journals.where("created_on>?",date_from).count - end - - #repository_count - Repository.where(project_id: @project_ids).each do |repository| -# activities[repository.project_id.to_s]+=1 - activities[repository.project_id]+=repository.changesets.where("committed_on>?",date_from).count - end - - - #news_count - News.where(project_id: @project_ids).where("created_on>?",date_from).each do |news| - activities[news.project_id]+=1 - end - - #document_count - Document.where(project_id: @project_ids).where("created_on>?",date_from).each do |document| - activities[document.project_id]+=1 - end - - #file_count - Attachment.where(container_id: @project_ids, container_type: Project).where("created_on>?",date_from).each do |attachment| - activities[attachment.container_id]+=1 - end - - #message_count - Board.where(project_id: @project_ids).each do |board| -# activities[board.project_id]+=1 - activities[board.project_id]+=board.messages.where("updated_on>?",date_from).count - end - - #time_entry_count - TimeEntry.where(project_id: @project_ids).where("updated_on>?",date_from).each do |timeentry| - activities[timeentry.project_id]+=1 - end - - #feedbackc_count - JournalsForMessage.where(jour_id: @project_ids, jour_type: Project).each do |jourformess| - activities[jourformess.jour_id]+=1 - end - - #activities!=0 - i=0; - projects.each do |project| - id=project.id - if activities[id]==0 - activities[id]=1 + unless @project.nil? + if !@project.is_public? && !User.current.member_of?(@project) && !User.current.admin? + render_403 + else + @users -= watched.watcher_users if @watched end end - - return activities end - #gcmend - - #gcm - def handle_project projects,activities - project_activity_count_array=activities.values() - - project_array=[] - i=0; - projects.each do |project| - project_array[i]=project - i=i+1 - end - - projects=desc_sort_course_by_avtivity(project_activity_count_array,project_array) - - return projects - end - #gcmend - #gcm def desc_sort_course_by_avtivity(activity_count,projects) diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 0c1f65317..607c9b5db 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -25,6 +25,7 @@ class ChangesetNotFound < Exception; end class InvalidRevisionParam < Exception; end class RepositoriesController < ApplicationController + include ApplicationHelper menu_item :repository menu_item :settings, :only => [:new, :create, :edit, :update, :destroy, :committers] default_search_scope :changesets @@ -92,6 +93,22 @@ class RepositoriesController < ApplicationController render :action => 'show', :layout => 'base_projects' end + + HOOK_TEMPLATE = %Q{#!/bin/sh +exec sh -c ' +function update() +{ + CMD_PATH=`dirname $0`; + cd $CMD_PATH; + PY_PATH=$PWD/../../git_refresh_changes.py; + [[ -s "$PY_PATH" ]] && $(which python) $PY_PATH $PWD; + cd -; +} +git update-server-info +update +' + } + def create if params[:repository_scm].to_s == 'Gitlab' # add by nwb @@ -106,7 +123,7 @@ class RepositoriesController < ApplicationController if request.post? && @repository.save redirect_to settings_project_url(@project, :tab => 'repositories') else - render :action => 'new' + redirect_to settings_project_url(@project, :tab => 'repositories') end else # 原逻辑 ##xianbo @@ -115,7 +132,7 @@ class RepositoriesController < ApplicationController @project_path=@root_path+"htdocs/"+@repository_name @repository_tag=params[:repository][:upassword] || params[:repository][:password] @repo_name=User.current.login.to_s+"_"+params[:repository][:identifier] - logger.info "htpasswd -mb "+@root_path+"user.passwd "+@repo_name+": "+@repository_tag + logger.info "htpasswd -mb "+@root_path+"htdocs/user.passwd "+@repo_name+": "+@repository_tag logger.info "the value of create repository"+@root_path+": "+@repository_name+": "+@project_path+": "+@repo_name attrs = pickup_extra_info if((@repository_tag!="")&¶ms[:repository_scm]=="Git") @@ -127,14 +144,13 @@ class RepositoriesController < ApplicationController if attrs[:attrs_extra].keys.any? @repository.merge_extra_info(attrs[:attrs_extra]) end - #by xianbo @repository.project = @project if request.post? && @repository.save if(params[:repository_scm]=="Git") - system "htpasswd -mb "+@root_path+"user.passwd "+@repo_name+" "+@repository_tag + system "htpasswd -mb "+@root_path+"htdocs/user.passwd "+@repo_name+" "+@repository_tag system "echo -e '"+@repo_name+"-write:"+ - " "+@repo_name+"' >> "+@root_path+"group.passwd" + " "+@repo_name+"' >> "+@root_path+"htdocs/group.passwd" system "mkdir "+@root_path+"htdocs/"+User.current.login.to_s system "git init --bare "+@project_path system "mv "+@project_path+"/hooks/post-update{.sample,}" @@ -145,21 +161,23 @@ class RepositoriesController < ApplicationController " \n ' >> "+ @root_path+"htdocs/"+ @repository_name+"/.htaccess" system "cd "+@project_path+" ;git update-server-info" - # if(create_repo_file&&create_passwd&&create_group&&init_repository&&add_privilege&&init_server_info) - # else - # logger.info "An error occured when authenticating "+"create passwd"+@creat_passwd+"create_group"+ - # crate_group+"create repository file "+create_repo_file+"init repository"+init_repostory+ - # "aad privilege to rpository"+add_privilege+"init server infos"+init_server_info - # end + + File.open(@project_path+"/hooks/post-update", "w+") do |f| + f.write(HOOK_TEMPLATE) + end + @repository.update_attributes(:login => User.current.login.to_s) end - redirect_to settings_project_url(@project, :tab => 'repositories') - else if(@repository_tag) - render :action => 'newrepo', :layout =>'base_projects' + redirect_to settings_project_url(@project, :tab => 'repositories',:repository_error_message=>@repository.errors.full_messages) + else if(@repository_tag.blank?) + #render :action => 'newrepo', :layout =>'base_projects' + redirect_to settings_project_url(@project, :tab => 'repositories',:repository => "pswd_is_null",:repository_error_message=>@repository.errors.full_messages) else - render :action => 'new', :layout =>'base_projects' + redirect_to settings_project_url(@project, :tab => 'repositories',:repository => @repository,:repository_error_message=>@repository.errors.full_messages) end end + + end end @@ -209,7 +227,7 @@ class RepositoriesController < ApplicationController elsif request.get? respond_to do |format| format.html{ - render :layout => "project_base" + render :layout => "base_projects" } end @@ -218,34 +236,20 @@ class RepositoriesController < ApplicationController end def destroy - @root_path=RepositoriesHelper::ROOT_PATH - @repo_name=User.current.login.to_s+"_"+@repository.identifier.to_s - @repository_name=User.current.login.to_s+"/"+@repository.identifier.to_s+".git" - @middle=User.current.login.to_s+"_"+@repository.identifier.to_s+"-write:" - @repository.destroy if request.delete? + DestroyRepositoryTask.new.destroy(User.current.id, @repository.id) + @repository.hidden = true + @repository.save redirect_to settings_project_url(@project, :tab => 'repositories') - if(@repository.type=="Repository::Git") - logger.info "destory the repository value"+"root path"+@root_path+"repo_name"+@repo_name+ - "repository_name"+@repository_name+"user group"+@middle - system "sed -i /"+@repo_name+"/{d} "+@root_path+"user.passwd" - system "sed -i /"+@middle+"/{d} "+@root_path+"group.passwd" - system "rm -r "+@root_path+"htdocs/"+@repository_name - # if(@sed_user&&@sed_group&&@remove) - # else - # logger.info "An error occured when destory the repository"+"delete form passwd: \n"+ - # @sed_user+"delete from group"+@sed_group+"delete from file"+@remove - # end - end end def show ## TODO: the below will move to filter, done. - # if !User.current.member_of?(@project) - # if @project.hidden_repo - # render_403 - # return -1 - # end - # end + if !User.current.member_of?(@project) + if @project.hidden_repo + render_403 + return -1 + end + end #if( !User.current.member_of?(@project) || @project.hidden_repo) @repository.fetch_changesets if Setting.autofetch_changesets? && @path.empty? @@ -372,7 +376,7 @@ class RepositoriesController < ApplicationController def revision respond_to do |format| - format.html{render :layout => 'project_base'} + format.html{render :layout => 'base_projects'} format.js {render :layout => false} end end diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index c60d1bd8f..93e0e9c4b 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -29,6 +29,9 @@ class SettingsController < ApplicationController end def edit + hidden_non_project = Setting.find_by_name("hidden_non_project") + @text = (hidden_non_project && hidden_non_project.value == "0") ? l(:label_show_non_project) : l(:label_hidden_non_project) + @notifiables = Redmine::Notifiable.all if request.post? && params[:settings] && params[:settings].is_a?(Hash) settings = (params[:settings] || {}).dup.symbolize_keys @@ -70,4 +73,20 @@ class SettingsController < ApplicationController rescue Redmine::PluginNotFound render_404 end + + #隐藏/显示非项目信息 + def hidden_non_project + @notifiable = Setting.find_by_name("hidden_non_project") + if @notifiable + @notifiable.value == "1" ? @notifiable.value = 0 : @notifiable.value = 1 + @notifiable.save + else + @notifiable = Setting.new() + @notifiable.name = "hidden_non_project" + @notifiable.value = 0 + @notifiable.save + end + + redirect_to settings_url + end end diff --git a/app/controllers/stores_controller.rb b/app/controllers/stores_controller.rb index b1705c826..85ea85241 100644 --- a/app/controllers/stores_controller.rb +++ b/app/controllers/stores_controller.rb @@ -72,6 +72,22 @@ class StoresController < ApplicationController l(:label_borad_project), #l(:label_contest_innovate), l(:label_forum) ] end + + #缺失文件列表 + def lost_file + attachments = [] + Attachment.where("container_id is not null and container_type is not null and container_type <> 'Bid' and container_type <> 'HomeworkAttach'").each do |attachment| + unless File.exist?(attachment.diskfile) + attachments << attachment + end + end + respond_to do |format| + format.xls { + send_data(homework_to_xls(attachments), :type => "text/excel;charset=utf-8; header=present", + :filename => "#{l(:label_file_lost_list)}.xls") + } + end + end private @@ -117,4 +133,27 @@ class StoresController < ApplicationController else end end + + #作品列表转换为excel + def homework_to_xls attachments + xls_report = StringIO.new + book = Spreadsheet::Workbook.new + sheet1 = book.create_worksheet :name => "homework" + blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10 + sheet1.row(0).default_format = blue + sheet1.row(0).concat(["文件ID","文件名","硬盘路径","上传时间","是否公开","所属对象","所属对象Id"]) + count_row = 1 + attachments.each do |attachment| + sheet1[count_row,0] = attachment.id + sheet1[count_row,1] = attachment.filename + sheet1[count_row,2] = attachment.diskfile + sheet1[count_row,3] = format_time(attachment.created_on) + sheet1[count_row,4] = (attachment.is_public == 1 || attachment.is_public) ? "是" :"否" + sheet1[count_row,5] = attachment.container_type + sheet1[count_row,6] = attachment.container_id + count_row += 1 + end + book.write xls_report + xls_report.string + end end diff --git a/app/controllers/student_work_controller.rb b/app/controllers/student_work_controller.rb new file mode 100644 index 000000000..deeefcd75 --- /dev/null +++ b/app/controllers/student_work_controller.rb @@ -0,0 +1,420 @@ +class StudentWorkController < ApplicationController + layout "base_courses" + include StudentWorkHelper + require 'bigdecimal' + before_filter :find_homework, :only => [:new, :index, :create, :student_work_absence_penalty, :absence_penalty_list, :evaluation_list] + before_filter :find_work, :only => [:edit, :update, :show, :destroy, :add_score, :praise_student_work] + before_filter :member_of_course, :only => [:index, :new, :create, :show, :add_score, :praise_student_work] + before_filter :author_of_work, :only => [:edit, :update, :destroy] + before_filter :teacher_of_course, :only => [:student_work_absence_penalty, :absence_penalty_list, :evaluation_list] + + def index + @order,@b_sort,@name = params[:order] || "final_score",params[:sort] || "desc",params[:name] || "" + @is_teacher = User.current.allowed_to?(:as_teacher,@course) + #老师 || 非匿评作业 || 匿评结束 显示所有的作品 + @show_all = @is_teacher || @homework.homework_type != 1 || @homework.homework_detail_manual.comment_status == 3 + if @show_all + if @homework.homework_type == 1 || @is_teacher || User.current.admin? + if @order == "name" + @stundet_works = search_homework_member @homework.student_works.joins(:user).order("users.lastname #{@b_sort}, users.firstname #{@b_sort}"),@name + else + @stundet_works = search_homework_member @homework.student_works.order("#{@order} #{@b_sort}"),@name + end + else + my_work = @homework.student_works.where(:user_id => User.current.id) + if my_work.empty? + @stundet_works = [] + else + if @order == "name" + @stundet_works = search_homework_member @homework.student_works.joins(:user).order("users.lastname #{@b_sort}, users.firstname #{@b_sort}"),@name + else + @stundet_works = search_homework_member @homework.student_works.order("#{@order} #{@b_sort}"),@name + end + end + end + else #学生 + if @homework.homework_detail_manual.comment_status == 1 #未开启匿评,只显示我的作品 + @stundet_works = @homework.student_works.where(:user_id => User.current.id) + elsif @homework.homework_detail_manual.comment_status == 2 #匿评列表,显示匿评作品和我的作品 + @is_evaluation = true + my_work = @homework.student_works.where(:user_id => User.current.id) + @stundet_works = my_work + User.current.student_works_evaluation_distributions.map(&:student_work).select { |work| work.homework_common_id == @homework.id} + end + end + @homework_commons = @course.homework_commons.order("created_at desc") + @score = @b_sort == "desc" ? "asc" : "desc" + respond_to do |format| + format.html + format.xls { + send_data(homework_to_xls(@stundet_works), :type => "text/excel;charset=utf-8; header=present", + :filename => "#{@course.teacher.lastname.to_s + @course.teacher.firstname}_#{@course.name}_#{@course.time.to_s + @course.term}_#{@homework.name}#{l(:excel_homework_list)}.xls") + } + end + end + + def new + student_work = @homework.student_works.where("user_id = ?",User.current.id).first + if student_work.nil? + @stundet_work = StudentWork.new + respond_to do |format| + format.html + end + else + render_403 + end + end + + def create + if params[:student_work] + stundet_work = StudentWork.new + stundet_work.name = params[:student_work][:name] + stundet_work.description = params[:student_work][:description] + stundet_work.project_id = params[:student_work][:project_id] + stundet_work.homework_common_id = @homework.id + stundet_work.user_id = User.current.id + stundet_work.save_attachments(params[:attachments]) + render_attachment_warning_if_needed(stundet_work) + if stundet_work.save + respond_to do |format| + format.html { + flash[:notice] = l(:notice_successful_create) + redirect_to student_work_index_url(:homework => @homework.id) + } + end + return + end + end + respond_to do |format| + format.html { + flash[:notice] = l(:notice_failed_create) + redirect_to new_student_work_url(:homework => @homework.id) + } + end + end + + def edit + respond_to do |format| + format.html + end + end + + def update + if params[:student_work] + @work.name = params[:student_work][:name] + @work.description = params[:student_work][:description] + @work.project_id = params[:student_work][:project] + @work.save_attachments(params[:attachments]) + render_attachment_warning_if_needed(@work) + if @work.save + respond_to do |format| + format.html { + flash[:notice] = l(:notice_successful_edit) + redirect_to student_work_index_url(:homework => @homework.id) + } + end + return + end + end + respond_to do |format| + format.html{redirect_to edit_student_work_url(@work)} + end + end + + def show + @score = student_work_score @work,User.current + @is_teacher = User.current.allowed_to?(:as_teacher,@course) + respond_to do |format| + format.js + end + end + + def destroy + if @work.destroy + respond_to do |format| + format.html { + redirect_to student_work_index_url(:homework => @homework.id) + } + end + end + end + + #添加评分,已评分则为修改评分 + def add_score + render_403 and return if User.current == @work.user #不可以匿评自己的作品 + @is_teacher = User.current.allowed_to?(:as_teacher,@course) + #老师、教辅可以随时评分,学生只能在匿评作业的匿评阶段进行评分 + render_403 and return unless @is_teacher || (@homework.homework_type == 1 && @homework.homework_detail_manual.comment_status == 2) + @score = student_work_score @work,User.current + if @score + @score.comment = params[:new_form][:user_message] if params[:new_form] && params[:new_form][:user_message] && params[:new_form][:user_message] != "" + @score.score = params[:score] if params[:score] + @is_new = false + else + @score = StudentWorksScore.new + @score.score = params[:score] if params[:score] + @score.comment = params[:new_form][:user_message] if params[:new_form] && params[:new_form][:user_message] && params[:new_form][:user_message] != "" + @score.user_id = User.current.id + @score.student_work_id = @work.id + role = User.current.members.where("course_id = ?",@course.id).first.roles.first.name + User.current.admin? ? @score.reviewer_role = 1 : @score.reviewer_role = get_role_by_name(role) + @is_new = true + end + + @score.save_attachments(params[:attachments]) + render_attachment_warning_if_needed(@score) + + if @score.save + case @score.reviewer_role + when 1 #教师评分:最后一个教师评分为最终评分 + @work.teacher_score = @score.score + @work.final_score = @score.score + when 2 #教辅评分 教辅评分显示平均分 + @work.teaching_asistant_score = @work.student_works_scores.where(:reviewer_role => 2).average(:score).try(:round, 2).to_f + if @work.teacher_score.nil? + if @work.student_score.nil? + @work.final_score = @work.teaching_asistant_score + else + final_ta_score = BigDecimal.new("#{@work.teaching_asistant_score}") * BigDecimal.new("#{@homework.homework_detail_manual.ta_proportion}") + final_s_score = BigDecimal.new("#{@work.student_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{@homework.homework_detail_manual.ta_proportion}")) + final_score = final_ta_score + final_s_score + @work.final_score = format("%.2f",final_score.to_f) + end + end + when 3 #学生评分 学生评分显示平均分 + @work.student_score = @work.student_works_scores.where(:reviewer_role => 3).average(:score).try(:round, 2).to_f + if @work.teacher_score.nil? + if @work.teaching_asistant_score.nil? + @work.final_score = @work.student_score + else + final_ta_score = BigDecimal.new("#{@work.teaching_asistant_score}") * BigDecimal.new("#{@homework.homework_detail_manual.ta_proportion}") + final_s_score = BigDecimal.new("#{@work.student_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{@homework.homework_detail_manual.ta_proportion}")) + final_score = final_ta_score + final_s_score + @work.final_score = format("%.2f",final_score.to_f) + end + end + end + + if @work.save + respond_to do |format| + format.js + end + end + end + end + + #添加评分的回复 + def add_score_reply + @score = StudentWorksScore.find params[:score_id] + @jour = @score.journals_for_messages.new(:user_id => User.current.id,:notes =>params[:message], :reply_id => 0) + if @jour.save + @status = 1 + else + @status = 2 + end + respond_to do |format| + format.js + end + end + + #删除评分的回复 + def destroy_score_reply + @jour = JournalsForMessage.find params[:jour_id] + if @jour.destroy + respond_to do |format| + format.js + end + end + end + + #为作品点赞 + def praise_student_work + pt = PraiseTread.new + pt.user_id = User.current.id + pt.praise_tread_object_id = @work.id + pt.praise_tread_object_type = "StudentWork" + pt.praise_or_tread = 1 + if pt.save + respond_to do |format| + format.js + end + else + render_404 + end + end + + #缺评列表显示 + def student_work_absence_penalty + order = params[:order] || "desc" + if @homework.student_works.empty? + @stundet_works = [] + else + work_ids = "(" + @homework.student_works.map(&:id).join(",") + ")" + @stundet_works = StudentWork.find_by_sql("SELECT *,(all_count - has_count) AS absence FROM( + SELECT * , + (SELECT evaluation_num FROM homework_detail_manuals WHERE homework_detail_manuals.homework_common_id = #{@homework.id}) AS all_count, + (SELECT COUNT(*) FROM `student_works_scores` WHERE user_id = student_works.user_id AND student_work_id IN #{work_ids}) AS has_count + FROM `student_works` + WHERE homework_common_id = #{@homework.id} + ) AS table_1 + ORDER BY absence #{order}") + end + @order = order == "desc" ? "asc" : "desc" + respond_to do |format| + format.html + end + end + + #导出缺评列表 + def absence_penalty_list + if @homework.student_works.empty? + @stundet_works = [] + else + work_ids = "(" + @homework.student_works.map(&:id).join(",") + ")" + @stundet_works = StudentWork.find_by_sql("SELECT * FROM (SELECT *,(all_count - has_count) AS absence FROM( + SELECT * , + (SELECT evaluation_num FROM homework_detail_manuals WHERE homework_detail_manuals.homework_common_id = #{@homework.id}) AS all_count, + (SELECT COUNT(*) FROM `student_works_scores` WHERE user_id = student_works.user_id AND student_work_id IN #{work_ids}) AS has_count + FROM `student_works` + WHERE homework_common_id = #{@homework.id} + ) AS table_1) AS table_2 + where absence > 0 order by absence") + end + respond_to do |format| + format.xls { + send_data(absence_penalty_list_xls(@stundet_works), :type => "text/excel;charset=utf-8; header=present", + :filename => "#{@course.teacher.lastname.to_s + @course.teacher.firstname}_#{@course.name}_#{@course.time.to_s + @course.term}_#{@homework.name}#{l(:excel_absence_list)}.xls") + } + end + end + + #导出匿评列表 + def evaluation_list + respond_to do |format| + format.xls { + send_data(evaluation_list_xls(@homework.student_works), :type => "text/excel;charset=utf-8; header=present", + :filename => "#{@course.teacher.lastname.to_s + @course.teacher.firstname}_#{@course.name}_#{@course.time.to_s + @course.term}_#{@homework.name}#{l(:excel_evaluation_list)}.xls") + } + end + end + + private + #获取作业 + def find_homework + @homework = HomeworkCommon.find params[:homework] + @course = @homework.course + rescue + render_404 + end + #获取作品 + def find_work + @work = StudentWork.find params[:id] + @homework = @work.homework_common + @course = @homework.course + rescue + render_404 + end + + #是不是当前课程的成员 + #当前课程成员才可以看到作品列表 + def member_of_course + render_403 unless User.current.member_of_course?(@course) || User.current.admin? + end + + #判断是不是当前作品的提交者 + #提交者 && (非匿评作业 || 未开启匿评) 可以编辑作品 + def author_of_work + render_403 unless (User.current.id == @work.user_id || User.current.admin?) && (@homework.homework_type != 1 || @homework.homework_detail_manual.comment_status == 1 ) + end + + def teacher_of_course + render_403 unless User.current.allowed_to?(:as_teacher,@course) + end + + #根据条件过滤作业结果 + def search_homework_member homeworks,name + name = name.downcase + select_homework = homeworks.select{ |homework| + homework.user[:login].to_s.downcase.include?(name) || homework.user.user_extensions[:student_id].to_s.downcase.include?(name) || (homework.user[:lastname].to_s.downcase + homework.user[:firstname].to_s.downcase).include?(name) + } + select_homework + end + + #作品列表转换为excel + def homework_to_xls items + xls_report = StringIO.new + book = Spreadsheet::Workbook.new + sheet1 = book.create_worksheet :name => "homework" + 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_homework_name), + l(:excel_t_score),l(:excel_ta_score),l(:excel_n_score),l(:excel_f_score),l(:excel_commit_time)]) + count_row = 1 + items.each do |homework| + sheet1[count_row,0]=homework.user.id + sheet1[count_row,1] = homework.user.lastname.to_s + homework.user.firstname.to_s + sheet1[count_row,2] = homework.user.login + sheet1[count_row,3] = homework.user.user_extensions.student_id + sheet1[count_row,4] = homework.user.mail + sheet1[count_row,5] = homework.name + sheet1[count_row,6] = homework.teacher_score.nil? ? l(:label_without_score) : format("%.2f",homework.teacher_score) + sheet1[count_row,7] = homework.teaching_asistant_score.nil? ? l(:label_without_score) : format("%.2f",homework.teaching_asistant_score) + sheet1[count_row,8] = homework.student_score.nil? ? l(:label_without_score) : format("%.2f",homework.student_score) + sheet1[count_row,9] = homework.final_score.nil? ? l(:label_without_score) : format("%.2f",homework.final_score) + sheet1[count_row,10] = format_time(homework.created_at) + count_row += 1 + end + book.write xls_report + xls_report.string + end + + #缺评列表转换为excel + def absence_penalty_list_xls items + xls_report = StringIO.new + book = Spreadsheet::Workbook.new + sheet1 = book.create_worksheet :name => "homework" + blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10 + sheet1.row(0).default_format = blue + + sheet1.row(0).concat([l(:excel_student_id),l(:excel_nickname),l(:excel_user_name),l(:lable_all_penalty),l(:lable_has_penalty),l(:lable_absence_penalty)]) + count_row = 1 + items.each do |homework| + sheet1[count_row,0] = homework.user.user_extensions.student_id + sheet1[count_row,1] = homework.user.login + sheet1[count_row,2] = homework.user.lastname.to_s + homework.user.firstname.to_s + sheet1[count_row,3] = homework.all_count + sheet1[count_row,4] = homework.has_count + sheet1[count_row,5] = homework.absence + count_row += 1 + end + book.write xls_report + xls_report.string + end + + #匿评列表转换为excel + def evaluation_list_xls items + xls_report = StringIO.new + book = Spreadsheet::Workbook.new + sheet1 = book.create_worksheet :name => "homework" + blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10 + sheet1.row(0).default_format = blue + + sheet1.row(0).concat([l(:label_work_name),l(:label_work_id),l(:label_work_autor),l(:label_evaluation_id),l(:label_evaluation_name), + l(:label_evaluation_score),l(:label_evaluation_common),l(:label_evaluation_time)]) + count_row = 1 + items.each do |homework| + homework.student_works_scores.where(:reviewer_role => 3).each do |score| + sheet1[count_row,0] = homework.name + sheet1[count_row,1] = homework.user.user_extensions.student_id + sheet1[count_row,2] = homework.user.show_name + sheet1[count_row,3] = score.user.user_extensions.student_id + sheet1[count_row,4] = score.user.show_name + sheet1[count_row,5] = score.score + sheet1[count_row,6] = score.comment + sheet1[count_row,7] = format_time(score.created_at) + count_row += 1 + end + end + book.write xls_report + xls_report.string + end +end \ No newline at end of file diff --git a/app/controllers/system_log_controller.rb b/app/controllers/system_log_controller.rb new file mode 100644 index 000000000..274cb18a9 --- /dev/null +++ b/app/controllers/system_log_controller.rb @@ -0,0 +1,59 @@ +# Time 2015-01-26 17:12:23 +# Author lizanle +# Description 显示和清理系统日志 +class SystemLogController < ApplicationController + + before_filter :require_admin + # 默认每页显示20条记录 + PER_PAGE = 20 + layout "base" + include SystemLogHelper + + # Time 2015-01-26 17:12:46 + # Author lizanle + # Description 查看所有日志 + def index + @logs = SystemLog.logo_data(params[:page]||1, params[:per]||PER_PAGE, params[:search], params[:day]) + end + + # Time 2015-01-26 14:42:38 + # Author lizanle + # Description 清除日志 + def clear + SystemLog.clear params[:day] + redirect_to :action => :index + end + + # Time 2015-01-26 17:24:25 + # Author lizanle + # Description 访问分析 + def access_analysis + #解析日志,然后逆序 + @log_result = SystemLog.analysis(params[:day]).reverse[1...-1] + @access_module = Hash.new + #日誌可能為空 + if @log_result && !@log_result.empty? + #将数组中的模块访问统计出来放到hash中 每条记录的第四个值是Controller#action的形式 + @log_result.collect! { |r| @access_module[r[3]].nil? ? + @access_module[r[3]] = 1 : @access_module[r[3]] +=1 } + # 去掉key可能为空记录 排序,然后取逆序 + @access_module = @access_module.delete_if { |k, v| k.nil? }.sort_by { |key, val| val }.reverse + else + @access_module + end + end + + # Time 2015-01-26 17:24:36 + # Author lizanle + # Description 耗时分析 + def time_analysis + #解析日志 + @log_result = SystemLog.analysis(params[:day]).reverse[1...-1] + if @log_result && !@log_result.empty? + #分页 + @log_result = Kaminari.paginate_array(@log_result).page(params[:page]||1).per(params[:per]||PER_PAGE) + else + @log_result = [] + end + end +end diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 5bd5fb0e3..253faa2b1 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -73,7 +73,7 @@ class TagsController < ApplicationController @issues_results, @bids_results, @forums_results, - @attachments_results, + @attachments_results, @contests_results, @courses_results, @open_source_projects_results= refresh_results(@obj_id,@obj_flag,@selected_tags) @@ -109,7 +109,7 @@ class TagsController < ApplicationController @issues_results, @bids_results, @forums_results, - @attachments_results, + @attachments_results, @contests_results, @courses_results, @open_source_projects_results= refresh_results(@obj_id,@show_flag) @@ -220,11 +220,18 @@ class TagsController < ApplicationController @tag = ActsAsTaggableOn::Tag.find_by_id(@tag_id) @tag.delete unless @tag.nil? end + + if @obj && @object_flag == '6' && @obj.container.kind_of?(Course) + @course = @obj.container + @tag_list = get_course_tag_list @course + @select_tag_name = params[:select_tag_name] + end # end end end def tag_save + @select_tag_name = params[:tag_for_save][:tag_name] @tags = params[:tag_for_save][:name] @obj_id = params[:tag_for_save][:object_id] @obj_flag = params[:tag_for_save][:object_flag] @@ -248,6 +255,8 @@ class TagsController < ApplicationController @obj = OpenSourceProject.find_by_id(@obj_id) when '9' @obj = Course.find_by_id(@obj_id) + when '10' + @obj = Attachment.find_by_id(@obj_id) else @obj = nil end @@ -261,6 +270,10 @@ class TagsController < ApplicationController else logger.error "#{__FILE__}:#{__LINE__} ===> #{@obj.errors.try(:full_messages)}" end + if @obj && @obj_flag == '6' && @obj.container.kind_of?(Course) + @course = @obj.container + @tag_list = @tag_list = get_course_tag_list @course + end respond_to do |format| format.js format.html @@ -315,6 +328,14 @@ class TagsController < ApplicationController when '8' @obj = OpenSourceProject.find_by_id(obj_id) @obj_pages, @open_source_projects_results, @results_count = for_pagination(get_open_source_projects_by_tag(selected_tags)) + when '10' + @obj = Attachment.find_by_id(obj_id) + + # modifed by Long Jun + # this is used to find the attachments that came from the same project and tagged with the same tag. + #@result = get_attachments_by_project_tag(selected_tags, @obj) + @result = get_attachments_by_tag(selected_tags) + @obj_pages, @attachments_results, @results_count = for_pagination(@result) when '9' then @obj = Course.find_by_id(obj_id) @obj_pages, @courses_results, @results_count = for_pagination(get_courses_by_tag(selected_tags)) @@ -380,7 +401,9 @@ class TagsController < ApplicationController when '8' return 'OpenSourceProject' when '9' - return 'Course' + return 'Course' + when '10' + return 'Attachment' else render_error :message => e.message return diff --git a/app/controllers/test_controller.rb b/app/controllers/test_controller.rb index 22cf7d1d5..71c445dc4 100644 --- a/app/controllers/test_controller.rb +++ b/app/controllers/test_controller.rb @@ -1,3 +1,5 @@ +require 'net/http' + class TestController < ApplicationController helper :UserScore @@ -5,6 +7,10 @@ class TestController < ApplicationController def bootstrap; end + def view_office + + end + def zip homeworks_attach_path = [] homework_id = params[:homework_id] @@ -18,7 +24,7 @@ class TestController < ApplicationController end @paths = homeworks_attach_path zipfile = ziping homeworks_attach_path - send_file zipfile, :filename => bid.name, + send_file zipfile, :filename => filename_for_content_disposition(bid.name), :type => detect_content_type(zipfile) rescue Errno::ENOENT => e logger.error "[Errno::ENOENT] ===> #{e}" @@ -57,5 +63,98 @@ class TestController < ApplicationController attach.filename end + def mailer() + raise unless Rails.env.development? + @user = User.find(params[:user_id]) + send_for_user_activities(@user, Time.now,1) + render 'mailer/send_for_user_activities' + end + def send_for_user_activities(user, date_to, days) + date_from = date_to - days.days + + subject = "[ #{user.show_name}#{l(:label_day_mail)}]" + @subject = " #{user.show_name}#{l(:label_day_mail)}" + + date_from = "#{date_from} 17:59:59" + date_to = "#{date_to} 17:59:59" + + # 生成token用于直接点击登录 + @user = user + token = Token.new(:user =>user , :action => 'autologin') + token.save + @token = token + + # 查询user参加的项目及课程 + projects = user.projects + courses = user.courses + project_ids = projects.map{|project| project.id}.join(",") + course_ids = courses.map {|course| course.id}.join(",") + + # 查询user的缺陷,包括发布的,跟踪的以及被指派的缺陷 + sql = "select DISTINCT i.* from issues i, watchers w + where (i.assigned_to_id = #{user.id} or i.author_id = #{user.id} + or (w.watchable_type = 'Issue' and w.watchable_id = i.id and w.user_id = #{user.id})) + and (i.created_on between '#{date_from}' and '#{date_to}') order by i.created_on desc" + @issues = Issue.find_by_sql(sql) + + # @bids 查询课程作业,包括老师发布的作业,以及user提交作业 + # @attachments查询课程课件更新 + @attachments ||= [] + + @bids ||= [] # 老师发布的作业 + + unless courses.first.nil? + count = courses.count + count = count - 1 + for i in 0..count do + bids = courses[i].homeworks.where("bids.created_on between '#{date_from}' and '#{date_to}'").order("bids.created_on desc") + attachments = courses[i].attachments.where("attachments.created_on between '#{date_from}' and '#{date_to}'").order('attachments.created_on DESC') + @bids += bids if bids.count > 0 + @attachments += attachments if attachments.count > 0 + end + end + # user 提交的作业 + @homeworks = HomeworkAttach.where("user_id=#{user.id} and (created_at between '#{date_from}' and '#{date_to}')").order("created_at desc") + + # 查询user在课程。项目中发布的讨论帖子 + messages = Message.find_by_sql("select DISTINCT * from messages where author_id = #{user.id} and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") + @course_messages ||= [] + @project_messages ||= [] + unless messages.first.nil? + messages.each do |msg| + if msg.project + @project_messages << msg + elsif msg.course + @course_messages << msg + end + end + end + # 查询user在课程中发布的通知,项目中发的新闻 + @course_news = (course_ids && !course_ids.empty?) ? News.find_by_sql("select DISTINCT n.* from news n + where n.course_id in (#{course_ids}) + and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") : [] + @project_news = (project_ids && !project_ids.empty?) ? News.find_by_sql("select DISTINCT n.* from news n where n.project_id in (#{project_ids}) + and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") : [] + + # 查询user在课程及个人中留言 + @course_journal_messages = JournalsForMessage.find_by_sql("select DISTINCT * from journals_for_messages where + jour_type='Course' and user_id = #{user.id} + and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") + @user_journal_messages = user.journals_for_messages.where("m_parent_id IS NULL and (created_on between '#{date_from}' and '#{date_to}')").order('created_on DESC') + + + # 查询user新建贴吧或发布帖子 + @forums = Forum.find_by_sql("select DISTINCT * from forums where creator_id = #{user.id} and (created_at between '#{date_from}' and '#{date_to}') order by created_at desc") + @memos = Memo.find_by_sql("select DISTINCT m.* from memos m, forums f where (m.author_id = #{user.id} or (m.forum_id = f.id and f.creator_id = #{user.id})) + and (m.created_at between '#{date_from}' and '#{date_to}') order by m.created_at desc") + + + has_content = [@issues,@homeworks,@course_messages,@project_messages,@course_news,@project_news, + @course_journal_messages,@user_journal_messages,@forums,@memos,@attachments,@bids].any? {|o| + !o.empty? + } + #有内容才发,没有不发 + end + -end \ No newline at end of file +end diff --git a/app/controllers/trackers_controller.rb b/app/controllers/trackers_controller.rb index 01bc47a2f..64ad9c83a 100644 --- a/app/controllers/trackers_controller.rb +++ b/app/controllers/trackers_controller.rb @@ -38,12 +38,13 @@ class TrackersController < ApplicationController @tracker ||= Tracker.new(params[:tracker]) @trackers = Tracker.sorted.all @projects = Project.where("project_type = #{Project::ProjectType_project}").all - @courses = Course.all - @course_activity_count=Hash.new - @courses.each do |course| - @course_activity_count[course.id]=0 - end - @course_activity_count=get_course_activity @courses,@course_activity_count + # 去掉原因,这块代码已经没有用到 + # @courses = Course.all + # @course_activity_count=Hash.new + # @courses.each do |course| + # @course_activity_count[course.id]=0 + # end + # @course_activity_count=get_course_activity @courses,@course_activity_count end def create diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 0fcf35aff..23053a48e 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -15,33 +15,39 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class UsersController < ApplicationController + layout :setting_layout #Added by young + before_filter :auth_login1, :only => [:show, :user_activities, :user_newfeedback] + before_filter :logged_user_by_apptoken, :only => [:show,:user_newfeedback] menu_item :activity menu_item :user_information, :only => :info menu_item :user_course, :only => :user_courses menu_item :user_homework, :only => :user_homeworks menu_item :user_project, :only => [:user_projects, :watch_projects] - menu_item :requirement_focus, :only => :watch_bids + # menu_item :requirement_focus, :only => :watch_bids menu_item :requirement_focus, :only => :watch_contests menu_item :user_newfeedback, :only => :user_newfeedback #Ended by young + # edit + + # before_filter :can_show_course, :only => [:user_courses,:user_homeworks] - before_filter :require_admin, :except => [:show, :index, :search, :tag_save, :tag_saveEx,:user_projects, :user_newfeedback, :user_comments, :watch_bids, :watch_contests, :info, + before_filter :require_admin, :except => [:show, :index, :search, :tag_save, :tag_saveEx,:user_projects, :user_newfeedback, :user_comments, :watch_contests, :info, :user_watchlist, :user_fanslist,:update, :user_courses, :user_homeworks, :watch_projects, :show_score, :topic_score_index, :project_score_index, :activity_score_index, :influence_score_index, :score_index,:show_new_score, :topic_new_score_index, :project_new_score_index, :activity_new_score_index, :influence_new_score_index, :score_new_index,:update_score,:user_activities,:user_projects_index] #edit has been deleted by huang, 2013-9-23 before_filter :find_user, :only => [:user_fanslist, :user_watchlist, :show, :edit, :update, :destroy, :edit_membership, :user_courses, :user_homeworks, :destroy_membership, :user_activities, :user_projects, :user_newfeedback, :user_comments, - :watch_bids, :watch_contests, :info, :watch_projects, :show_score, :topic_score_index, :project_score_index, + :watch_contests, :info, :watch_projects, :show_score, :topic_score_index, :project_score_index, :activity_score_index, :influence_score_index, :score_index,:show_new_score, :topic_new_score_index, :project_new_score_index, :activity_new_score_index, :influence_new_score_index, :score_new_index,:user_projects_index] before_filter :auth_user_extension, only: :show - before_filter :rest_user_score, only: :show + #before_filter :rest_user_score, only: :show #before_filter :select_entry, only: :user_projects accept_api_auth :index, :show, :create, :update, :destroy,:tag_save , :tag_saveEx @@ -135,25 +141,25 @@ class UsersController < ApplicationController ##added by fq def watch_bids - cond = 'bids.reward_type <> 1' - @bids = Bid.watched_by(@user).where('reward_type = ?', 1) # added by huang - @offset, @limit = api_offset_and_limit({:limit => 10}) - @bid_count = @bids.count - @bid_pages = Paginator.new @bid_count, @limit, params['page'] - @offset ||= @bid_pages.reverse_offset - unless @offset == 0 - @bid = @bids.offset(@offset).limit(@limit).all.reverse - else - limit = @bid_count % @limit - @bid = @bids.offset(@offset).limit(limit).all.reverse - end - - respond_to do |format| - format.html { - render :layout => 'base_users' - } - format.api - end + # cond = 'bids.reward_type <> 1' + # @bids = Bid.watched_by(@user).where('reward_type = ?', 1) # added by huang + # @offset, @limit = api_offset_and_limit({:limit => 10}) + # @bid_count = @bids.count + # @bid_pages = Paginator.new @bid_count, @limit, params['page'] + # @offset ||= @bid_pages.reverse_offset + # unless @offset == 0 + # @bid = @bids.offset(@offset).limit(@limit).all.reverse + # else + # limit = @bid_count % @limit + # @bid = @bids.offset(@offset).limit(limit).all.reverse + # end + # + # respond_to do |format| + # format.html { + # render :layout => 'base_users' + # } + # format.api + # end end #new add by linchun @@ -209,24 +215,24 @@ class UsersController < ApplicationController # added by huang def user_homeworks - @membership = @user.memberships.all(:conditions => Project.visible_condition(User.current)) - @memberships = [] - @membership.each do |membership| - if membership.project.project_type == 1 - @memberships << membership - end - end - @bid = [] - @memberships.each do |membership| - @bid += membership.project.homeworks - end - @bid = @bid.group_by {|bid| bid.courses.first.id} - unless User.current.admin? - if !@user.active? - render_404 - return - end - end + # @membership = @user.memberships.all(:conditions => Project.visible_condition(User.current)) + # @memberships = [] + # @membership.each do |membership| + # if membership.project.project_type == 1 + # @memberships << membership + # end + # end + # @bid = [] + # @memberships.each do |membership| + # @bid += membership.project.homeworks + # end + # @bid = @bid.group_by {|bid| bid.courses.first.id} + # unless User.current.admin? + # if !@user.active? + # render_404 + # return + # end + # end end @@ -353,8 +359,7 @@ class UsersController < ApplicationController respond_to do |format| format.html { @groups = Group.all.sort - # render :layout => @user_base_tag - render_404 + User.current.admin? ? (render :layout => @user_base_tag) : (render_404) } format.api end @@ -364,34 +369,37 @@ class UsersController < ApplicationController sort_init 'login', 'asc' sort_update %w(login firstname lastname mail admin created_on last_login_on) (redirect_to user_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank? - case params[:format] - when 'xml', 'json' - @offset, @limit = api_offset_and_limit({:limit => 15}) + case params[:format] + when 'xml', 'json' + @offset, @limit = api_offset_and_limit({:limit => 15}) + else + @limit = 15#per_page_option + end + # + #@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? + @search_by = params[:search_by] ? params[:search_by] : 0 + + us = UsersService.new + scope = us.search_user params + @user_count = scope.count + @user_pages = Paginator.new @user_count, @limit, params['page'] + @user_base_tag = params[:id] ? 'base_users':'users_base' + @offset ||= @user_pages.reverse_offset + unless @offset == 0 + @users = scope.offset(@offset).limit(@limit).all.reverse else - @limit = 15#per_page_option + limit = @user_count % @limit + if limit == 0 + limit = @limit + end + @users = scope.offset(@offset).limit(limit).all.reverse end - - @status = params[:status] || 1 - has = { - "show_changesets" => true - } - scope = User.logged.status(@status) - @search_by = params[:search_by] ? params[:search_by] :"0" - scope = scope.like(params[:name], @search_by) if params[:name].present? - @user_count = scope.count - @user_pages = Paginator.new @user_count, @limit, params['page'] - @user_base_tag = params[:id] ? 'base_users':'users_base' - @offset ||= @user_pages.reverse_offset - unless @offset == 0 - @users = scope.offset(@offset).limit(@limit).all.reverse - else - limit = @user_count % @limit - if limit == 0 - limit = @limit - end - @users = scope.offset(@offset).limit(limit).all.reverse - end - respond_to do |format| format.html { @groups = Group.all.sort @@ -403,6 +411,9 @@ class UsersController < ApplicationController def show pre_count = 10 #limit + # Time 2015-02-04 11:46:34 + # Author lizanle + # Description type 1 :所有动态包括我关注的人 type 2:我的动态 type 3:关于我的回复 case params[:type] when "1" if @user == User.current @@ -423,26 +434,104 @@ class UsersController < ApplicationController messages = message.sort {|x,y| y.created_on <=> x.created_on } @message = messages[@info_pages.offset, @info_pages.per_page] @state = 2 - else + else + # Time 2015-02-04 10:50:49 + # Author lizanle + # Description 所有动态 where_condition = nil; # where_condition = "act_type <> 'JournalsForMessage'" + user_ids = [] if @user == User.current watcher = User.watched_by(@user) watcher.push(User.current) - activity = Activity.where(where_condition).where('user_id in (?)', watcher).order('id desc') + user_ids = watcher.map{|x| x.id} else - activity = Activity.where(where_condition).where('user_id = ?', @user.id).order('id desc') + user_ids << @user.id + end + activity = Activity.where(where_condition).where(user_id: user_ids).order('id desc') + + permission = !User.current.admin? + if permission + #Issue + act_ids = activity.where(act_type: 'Issue').select('act_id').map{|x| x.act_id} + project_ids = Issue.where(id: act_ids).select('distinct project_id').map{|x| x.project_id} + p_ids = [] + Project.where(id: project_ids).each do |x| + p_ids << x.id unless x.visible?(User.current) + end + ids = [] + ids << Issue.where(id: act_ids, project_id: p_ids).map{|x| x.id} + + #HomeworkCommon + act_ids = activity.where(act_type: 'HomeworkCommon').select('act_id').map{|x| x.act_id} + course_ids = HomeworkCommon.where(id: act_ids).select('distinct course_id').map{|x| x.course_id} + c_ids = [] + Course.where(id: course_ids).each do |x| + c_ids << x.id unless x.is_public !=0 && User.current.member_of_course?(x) + end + ids << HomeworkCommon.where(id: act_ids, course_id: c_ids).map{|x| x.id} + + #Journal + act_ids = activity.where(act_type: 'Journal').select('act_id').map{|x| x.act_id} + project_ids = Journal.where(id:act_ids, journalized_type: 'Project').select('distinct journalized_id').map{|x| x.journalized_id} + p_ids = [] + Project.where(id: project_ids).each do |x| + p_ids << x.id unless x.visible?(User.current) + end + ids << Journal.where(id: act_ids, journalized_id: p_ids, journalized_type: 'Project').map{|x| x.id} + + #News + act_ids = activity.where(act_type: 'News').select('act_id').map{|x| x.act_id} + project_ids = News.where(id: act_ids).select('distinct project_id').map{|x| x.project_id} + p_ids = [] + Project.where(id: project_ids).each do |x| + p_ids << x.id unless x.visible?(User.current) + end + ids << News.where(id: act_ids, project_id: p_ids).map{|x| x.id} + + project_ids = News.where(id: act_ids).select('distinct course_id').map{|x| x.course_id} + c_ids = [] + Course.where(id: project_ids).each do |x| + c_ids << x.id unless x.is_public !=0 && User.current.member_of_course?(x) + end + ids << News.where(id: act_ids, course_id: p_ids).map{|x| x.id} + + #Message + act_ids = activity.where(act_type: 'Message').select('act_id').map{|x| x.act_id} + board_ids = Message.where(id: act_ids).select('distinct board_id').map{|x| x.board_id} + project_ids = Board.where(id: board_ids).select('distinct project_id').map{|x| x.project_id} + p_ids = [] + Project.where(id: project_ids).each do |x| + p_ids << x.id unless x.visible?(User.current) + end + ids << Message.where(id: act_ids, board_id: p_ids).map{|x| x.id} + + project_ids = Board.where(id: board_ids).select('distinct course_id').map{|x| x.course_id} + c_ids = [] + Course.where(id: project_ids).each do |x| + c_ids << x.id unless x.is_public !=0 && User.current.member_of_course?(x) + end + ids << Message.where(id: act_ids, board_id: c_ids).map{|x| x.id} + + logger.debug "filter ids #{ids}" + + activity = activity.where('act_id not in (?)', ids.flatten ).order('id desc') unless ids.flatten.empty? end + # activity = activity.reject { |e| + # e.act.nil? || + # (!User.current.admin? && !e.act.nil? + # (((e.act_type == "Issue") && !e.act.project.visible?(User.current)) || + # (e.act_type == "Bid" && !e.act.courses.first.nil? && e.act.courses.first.is_public == 0 && !User.current.member_of_course?(e.act.courses.first)) || + # (e.act_type == "Journal" && e.act.respond_to?("Project") && !e.act.project.visible?(User.current)) || + # (e.act_type == "News" && ((!e.act.project.nil? && !e.act.project.visible?(User.current)) || (!e.act.course.nil? && e.act.course.is_public == 0 && !User.current.member_of_course?(e.act.course)))) || + # (e.act_type == "Message" && !e.act.board.nil? && ((!e.act.board.project.nil? && !e.act.board.project.visible?(User.current)) || (!e.act.board.course.nil? && e.act.board.course.is_public == 0 && !User.current.member_of_course?(e.act.board.course)))))) + # } + # + @activity_count = activity.count @activity_pages = Paginator.new @activity_count, pre_count, params['page'] - activity_page = activity.slice(@activity_pages.offset,@activity_pages.per_page * 2) - activity_page = activity_page.reject { |e| - ((e.act_type=="Issue") && ( !e.act.visible?(User.current))) || - ((e.act_type == "Journal") && (!e.act.project.visible?(User.current))) || - ((e.act_type == "Bid") && ((!User.current.member_of_course?(e.act.courses.first) || !User.current.admin?))) - } - @activity = activity.slice(0,@activity_pages.per_page) + @activity = activity.slice(@activity_pages.offset,@activity_pages.per_page) @state = 0 end @@ -534,7 +623,7 @@ class UsersController < ApplicationController @user.pref.save @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) - Mailer.account_information(@user, params[:user][:password]).deliver if params[:send_information] + Mailer.run.account_information(@user, params[:user][:password]) if params[:send_information] respond_to do |format| format.html { @@ -601,9 +690,9 @@ class UsersController < ApplicationController @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) if was_activated - Mailer.account_activated(@user).deliver + Mailer.run.account_activated(@user) elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil? - Mailer.account_information(@user, params[:user][:password]).deliver + Mailer.run.account_information(@user, params[:user][:password]) end respond_to do |format| @@ -668,7 +757,7 @@ class UsersController < ApplicationController when '3' then @obj = Issue.find_by_id(@obj_id) when '4' then - @obj = Bid.find_by_id(@obj_id) + # @obj = Bid.find_by_id(@obj_id) when '5' then @obj = Forum.find_by_id(@obj_id) when '6' @@ -711,7 +800,7 @@ class UsersController < ApplicationController when '3' then @obj = Issue.find_by_id(@obj_id) when '4' then - @obj = Bid.find_by_id(@obj_id) + # @obj = Bid.find_by_id(@obj_id) when '5' then @obj = Forum.find_by_id(@obj_id) when '6' diff --git a/app/controllers/versions_controller.rb b/app/controllers/versions_controller.rb index 57f26103c..defc36868 100644 --- a/app/controllers/versions_controller.rb +++ b/app/controllers/versions_controller.rb @@ -85,13 +85,14 @@ class VersionsController < ApplicationController end def new - @version = @project.versions.build - @version.safe_attributes = params[:version] - - respond_to do |format| - format.html - format.js - end + # @version = @project.versions.build + # @version.safe_attributes = params[:version] + # + # respond_to do |format| + # format.html + # format.js + # end + redirect_to settings_project_url(@project, :tab => 'versions') end def create @@ -116,7 +117,8 @@ class VersionsController < ApplicationController end else respond_to do |format| - format.html { render :action => 'new' } + format.html { flash[:error] = @version.errors.full_messages.flatten.to_s + redirect_to settings_project_url(@project, :tab => 'versions') } format.js { render :action => 'new' } format.api { render_validation_errors(@version) } end @@ -136,7 +138,7 @@ class VersionsController < ApplicationController respond_to do |format| format.html { flash[:notice] = l(:notice_successful_update) - redirect_back_or_default settings_project_path(@project, :tab => 'versions') + redirect_to settings_project_path(@project, :tab => 'versions') } format.api { render_api_ok } end diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb index 50bb2ccd8..f6d01a03d 100644 --- a/app/controllers/watchers_controller.rb +++ b/app/controllers/watchers_controller.rb @@ -15,13 +15,37 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class WatchersController < ApplicationController - before_filter :require_login, :find_watchables, :only => [:watch, :unwatch] + before_filter :require_login#, :find_watchables, :only => [:watch, :unwatch] def watch - set_watcher(@watchables, User.current, true) + s = WatchesService.new + watchables = s.watch params.merge(:current_user_id => User.current.id) + respond_to do |format| + format.html { redirect_to_referer_or {render :text => (true ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} + format.js { render :partial => 'set_watcher', :locals => {:user => User.current, :watched => watchables} } + end + rescue Exception => e + if e.message == "404" + render_404 + else + raise e + end + #set_watcher(@watchables, User.current, true) end def unwatch - set_watcher(@watchables, User.current, false) + s = WatchesService.new + watchables = s.unwatch params.merge(:current_user_id => User.current.id) + respond_to do |format| + format.html { redirect_to_referer_or {render :text => (false ? 'Watcher added.' : 'Watcher removed.'), :layout => true}} + format.js { render :partial => 'set_watcher', :locals => {:user => User.current, :watched => watchables} } + end + rescue Exception => e + if e.message == "404" + render_404 + else + raise e + end + #set_watcher(@watchables, User.current, false) end def join @@ -162,4 +186,5 @@ class WatchersController < ApplicationController format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} } end end + end diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 8e11bceb5..9ce107f8b 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -16,41 +16,79 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class WelcomeController < ApplicationController + # layout "base_welcome" include ApplicationHelper include WelcomeHelper helper :project_score - caches_action :robots - before_filter :find_first_page, :only => [:index] + caches_action :robots, :course, :contest, expires_in: 2.hours, layout: false + #before_filter :find_first_page, :only => [:index] # before_filter :fake, :only => [:index, :course] before_filter :entry_select, :only => [:index] def index - if @first_page.nil? || @first_page.sort_type.nil? - @projects = find_miracle_project(10, 3,"score desc") - else - case @first_page.sort_type - when 0 - @projects = find_miracle_project(10, 3,"created_on desc") - #@projects = @projects_all.order("created_on desc") - when 1 - @projects = find_miracle_project(10, 3,"score desc") - #@projects = @projects_all.order("grade desc") - when 2 - @projects = find_miracle_project(10, 3,"watchers_count desc") - #@projects = @projects_all.order("watchers_count desc") - - #gcm - #when '3' - #@projects=desc_sort_course_by_avtivity(@project_activity_count_array,@project_all_array) - # @projects=handle_project @projects_all,@project_activity_count - # @s_type = 3 - # @projects = @projects[@project_pages.offset, @project_pages.per_page] + # 企业版定制: params[:project]为传过来的参数 - else - @projects = @projects_all.order("score desc") + unless params[:organization].nil? + @organization = Organization.find params[:organization] + # @organization_projects = Project.joins(:project_status).joins("LEFT JOIN project_scores ON projects.id = project_scores.project_id").where("projects.organization_id = ?", @organization.id).order("score DESC").limit(10).all + @organization_projects = @organization.projects.visible.joins("LEFT JOIN project_scores ON projects.id = project_scores.project_id").order("project_scores.score DESC").limit(10).all + @part_projects = @organization_projects.count < 9 ? find_miracle_project( 9 - @organization_projects.count, 3,"score desc") : [] + # @cur_projects = Project.find(params[:organization]) + # @organization = @cur_projects.enterprise_name + # @organization_projects = (current_user.admin? || User.current.member_of?(@cur_projects)) ? Project.where("enterprise_name =? ", @organization) : Project.all_public.where("enterprise_name =? ", @organization) + # @e_count = @organization_projects.count + # @part_projects = [] + # # 取十个 + # @organization_projects.each do |obj| + # break if(@organization_projects[10] == obj) + # @part_projects << Project.visible.find_by_id("#{obj.id}") unless obj.id.nil? + # end + # # 不够十个的用最火项目替代 + # @e_count < 9 ? @part_projects = find_miracle_project( 9 - @e_count, 3,"score desc") : @part_projects + # # 配置文件首页定制 + @enterprise_page = FirstPage.find_by_page_type('enterprise') + if @enterprise_page.nil? + @enterprise_page = FirstPage.new + @enterprise_page.page_type = 'enterprise' + end + # 主页配置部分结束 + end + # end 企业版定制结束 + + if @first_page.nil? || @first_page.sort_type.nil? + @projects = find_miracle_project(10, 3,"score desc") + else + case @first_page.sort_type + when 0 + @my_projects = find_my_projects + @other_projects = @my_projects.count < 9 ? find_miracle_project( 9 - @my_projects.count, 3,"score desc") : [] + @projects = find_miracle_project(10, 3,"created_on desc") + #@projects = @projects_all.order("created_on desc") + when 1 + @my_projects = find_my_projects + @other_projects = @my_projects.count < 9 ? find_miracle_project( 9 - @my_projects.count, 3,"score desc") : [] + @projects = find_miracle_project(10, 3,"score desc") + #@projects = @projects_all.order("grade desc") + when 2 + @my_projects = find_my_projects + @other_projects = @my_projects.count < 9 ? find_miracle_project( 9 - @my_projects.count, 3,"score desc") : [] + @projects = find_miracle_project(10, 3,"watchers_count desc") + #@projects = @projects_all.order("watchers_count desc") + + #gcm + #when '3' + #@projects=desc_sort_course_by_avtivity(@project_activity_count_array,@project_all_array) + # @projects=handle_project @projects_all,@project_activity_count + # @s_type = 3 + # @projects = @projects[@project_pages.offset, @project_pages.per_page] + + else + @projects = @projects_all.order("score desc") + end end - end + rescue Exception => e + render_404 end def robots @@ -60,7 +98,7 @@ class WelcomeController < ApplicationController def course @course_page = FirstPage.find_by_page_type('course') - @school_id = params[:school_id] || User.current.user_extensions.school.try(:id) + @school_id = params[:school_id] || User.current.user_extensions.school.try(:id) || 117 @logoLink ||= logolink() ##3-8月份为查找春季课程,9-2月份为查找秋季课程 @@ -181,7 +219,7 @@ class WelcomeController < ApplicationController # 判断网站的入口,是课程 course 则跳过index去渲染 course 方法 def entry_select url = request.original_url.gsub('/','') - if url.include?(Setting.host_course.gsub('/','')) + if url.include?(Setting.url_course.gsub('/','')) if @first_page.show_course == 1 course render :course @@ -190,7 +228,7 @@ class WelcomeController < ApplicationController end return 0 - elsif url.include?(Setting.host_contest.gsub('/','')) + elsif url.include?(Setting.url_contest.gsub('/','')) if @first_page.show_contest == 1 contest render :contest @@ -199,7 +237,7 @@ class WelcomeController < ApplicationController end return 0 - elsif url.include?(Setting.host_user.gsub('/','')) + elsif url.include?(Setting.url_user.gsub('/','')) #redirect_to(:controller => "users", :action => "index") end diff --git a/app/controllers/words_controller.rb b/app/controllers/words_controller.rb index 2fc4fef9a..cc6c4f47e 100644 --- a/app/controllers/words_controller.rb +++ b/app/controllers/words_controller.rb @@ -1,7 +1,7 @@ # encoding: utf-8 #####leave message fq class WordsController < ApplicationController - + include ApplicationHelper before_filter :find_user, :only => [:new, :create, :destroy, :more, :back] def create if params[:new_form][:user_message].size>0 @@ -81,9 +81,13 @@ class WordsController < ApplicationController @journal_destroyed = JournalsForMessage.delete_message(params[:object_id]) if @journal_destroyed.jour_type == "Bid" @bid = Bid.find(@journal_destroyed.jour_id) - end - if @bid @jours_count = @bid.journals_for_messages.where('m_parent_id IS NULL').count + elsif @journal_destroyed.jour_type == "Course" + @course = Course.find @journal_destroyed.jour_id + @jours_count = @course.journals_for_messages.where('m_parent_id IS NULL').count + elsif @journal_destroyed.jour_type == "Principal" + @user = User.find(@journal_destroyed.jour_id) + @jours_count = @user.journals_for_messages.where('m_parent_id IS NULL').count end respond_to do |format| format.js @@ -209,6 +213,10 @@ class WordsController < ApplicationController message = params[:new_form][:course_message] feedback = Course.add_new_jour(user, message, params[:id]) if(feedback.errors.empty?) + if params[:asset_id] + ids = params[:asset_id].split(',') + update_kindeditor_assets_owner ids,feedback[:id],OwnerTypeHelper::JOURNALSFORMESSAGE + end redirect_to course_feedback_url(params[:id]), notice: l(:label_feedback_success) else flash[:error] = feedback.errors.full_messages[0] diff --git a/app/controllers/zipdown_controller.rb b/app/controllers/zipdown_controller.rb index b329e4c27..53b40942a 100644 --- a/app/controllers/zipdown_controller.rb +++ b/app/controllers/zipdown_controller.rb @@ -5,25 +5,51 @@ class ZipdownController < ApplicationController #检查权限 #勿删 before_filter :authorize, :only => [:assort,:download_user_homework] SAVE_FOLDER = "#{Rails.root}/files" - OUTPUT_FOLDER = "#{Rails.root}/tmp/archiveZip" + OUTPUT_FOLDER = "#{Rails.root}/files/archiveZip" + #统一下载功能 + def download + if User.current.logged? + begin + send_file "#{OUTPUT_FOLDER}/#{params[:file]}", :filename => filename_for_content_disposition(params[:filename]), :type => detect_content_type(params[:file]) + rescue => e + render file: 'public/no_file_found.html' + end + else + render_403 + end + end + + #一个作业下所有文件打包下载,只有admin和课程老师有权限 def assort if params[:obj_class] == "Bid" bid = Bid.find params[:obj_id] + render_403 if User.current.allowed_to?(:as_teacher,bid.courses.first) file_count = 0 bid.homeworks.map { |homework| file_count += homework.attachments.count} if file_count > 0 zipfile = zip_bid bid else - render file: 'public/no_file_found.html' + zipfile = {:message => "no file"} + end + elsif params[:obj_class] == "HomeworkCommon" + homework = HomeworkCommon.find params[:obj_id] + render_403 if User.current.allowed_to?(:as_teacher,homework.course) + file_count = 0 + homework.student_works.map { |work| file_count += work.attachments.count} + if file_count > 0 + zipfile = zip_homework_common homework + else + zipfile = {:message => "no file"} end else logger.error "[ZipDown#assort] ===> #{params[:obj_class]} unKown !!" end - send_file zipfile, :filename => bid.name + ".zip", :type => detect_content_type(zipfile) if zipfile - - #rescue Exception => e - # render file: 'public/no_file_found.html' + respond_to do |format| + format.json { + render json: zipfile.to_json + } + end end #下载某一学生的作业的所有文件 @@ -33,9 +59,10 @@ class ZipdownController < ApplicationController if homework != nil unless homework.attachments.empty? zipfile = zip_homework_by_user homework - send_file zipfile, :filename => ((homework.user.user_extensions.nil? || homework.user.user_extensions.student_id.nil?) ? "" : homework.user.user_extensions.student_id) + - "_" + (homework.user.lastname.nil? ? "" : homework.user.lastname) + (homework.user.firstname.nil? ? "" : homework.user.firstname) + - "_" + homework.name + ".zip", :type => detect_content_type(zipfile) if(zipfile) + filename = ((homework.user.user_extensions.nil? || homework.user.user_extensions.student_id.nil?) ? "" : homework.user.user_extensions.student_id) + + "_" + homework.user.show_name + + "_" + homework.name + ".zip" + send_file zipfile.file_path, :filename => filename_for_content_disposition(filename), :type => detect_content_type(zipfile.file_path) if(zipfile) else render file: 'public/no_file_found.html' end @@ -65,68 +92,167 @@ class ZipdownController < ApplicationController def zip_bid(bid) # Todo: User Access Controll bid_homework_path = [] + digests = [] bid.homeworks.each do |homeattach| unless homeattach.attachments.empty? - bid_homework_path << zip_homework_by_user(homeattach) + out_file = zip_homework_by_user(homeattach) + bid_homework_path << out_file.file_path + digests << out_file.file_digest + end + end + homework_id = bid.id + user_id = bid.author_id + out_file = find_or_pack(homework_id, user_id, digests.sort){ + zipping("#{Time.now.to_i}_#{bid.name}.zip", + bid_homework_path, OUTPUT_FOLDER) + } + [{files:[out_file.file_path], count: 1, index: 1, + real_file: out_file.file_path, file: File.basename(out_file.file_path), + size:(out_file.pack_size / 1024.0 / 1024.0).round(2) + }] + end + + def zip_homework_common homework_common + bid_homework_path = [] + digests = [] + homework_common.student_works.each do |work| + unless work.attachments.empty? + out_file = zip_student_work_by_user(work) + bid_homework_path << out_file.file_path + digests << out_file.file_digest + end + end + homework_id = homework_common.id + user_id = homework_common.user_id + out_file = find_or_pack(homework_id, user_id, digests.sort){ + zipping("#{Time.now.to_i}_#{homework_common.name}.zip", + bid_homework_path, OUTPUT_FOLDER) + } + [{files:[out_file.file_path], count: 1, index: 1, + real_file: out_file.file_path, file: File.basename(out_file.file_path), + size:(out_file.pack_size / 1024.0 / 1024.0).round(2) + }] + end + + def zip_homework_by_user(homework_attach) + homeworks_attach_path = [] + not_exist_file = [] + # 需要将所有homework.attachments遍历加入zip + digests = [] + homework_attach.attachments.each do |attach| + if File.exist?(attach.diskfile) + homeworks_attach_path << attach.diskfile + digests << attach.digest + else + not_exist_file << attach.filename + digests << 'not_exist_file' end end - zipping "#{Time.now.to_i}_#{bid.name}.zip", bid_homework_path, OUTPUT_FOLDER + out_file = find_or_pack(homework_attach.bid_id, homework_attach.user_id, digests.sort){ + zipping("#{homework_attach.user.show_name}_#{((homework_attach.user.user_extensions.nil? || homework_attach.user.user_extensions.student_id.nil?) ? "" : homework_attach.user.user_extensions.student_id)}_#{Time.now.to_i.to_s}.zip", + homeworks_attach_path, OUTPUT_FOLDER, true, not_exist_file) + } end - def zip_homework_by_user(homeattach) + def zip_student_work_by_user work homeworks_attach_path = [] not_exist_file = [] # 需要将所有homework.attachments遍历加入zip - # 并且返回zip路径 - homeattach.attachments.each do |attach| + digests = [] + work.attachments.each do |attach| if File.exist?(attach.diskfile) homeworks_attach_path << attach.diskfile + digests << attach.digest else not_exist_file << attach.filename + digests << 'not_exist_file' end end - zipping("#{homeattach.user.lastname}#{homeattach.user.firstname}_#{((homeattach.user.user_extensions.nil? || homeattach.user.user_extensions.student_id.nil?) ? "" : homeattach.user.user_extensions.student_id)}_#{Time.now.to_i.to_s}.zip", homeworks_attach_path, OUTPUT_FOLDER, true, not_exist_file) + out_file = find_or_pack(work.homework_common_id, work.user_id, digests.sort){ + zipping("#{work.user.show_name}_#{((work.user.user_extensions.nil? || work.user.user_extensions.student_id.nil?) ? "" : work.user.user_extensions.student_id)}_#{Time.now.to_i.to_s}.zip", + homeworks_attach_path, OUTPUT_FOLDER, true, not_exist_file) + } end - def zipping(zip_name_refer, files_paths, output_path, is_attachment=false, not_exist_file=[]) - # 输入待打包的文件列表,已经打包文件定位到ouput_path - ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE') - input_filename = files_paths + def find_or_pack(homework_id, user_id, digests) + raise "please given a pack block" unless block_given? + + out_file = ZipPack.packed?(homework_id, user_id, digests.sort) + + unless out_file && out_file.file_valid? + file = yield + + ZipPack.where(homework_id: homework_id, + user_id: user_id).delete_all + + out_file = ZipPack.create(homework_id: homework_id, + user_id: user_id, + file_digest: Trustie::Utils.digest(file), + file_path: file, + pack_size: File.size(file), + file_digests: digests.join(',') + ) + else + out_file.pack_times = out_file.pack_times + 1 + out_file.save + end + + out_file + end + + def zipping(zip_name_refer, files_paths, output_path, is_attachment=false, not_exist_file=[]) rename_zipfile = zip_name_refer ||= "#{Time.now.to_i.to_s}.zip" zipfile_name = "#{output_path}/#{rename_zipfile}" Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name)) - Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile| - input_filename.each do |filename| - flag = true - index = 1 - rename_file = ic.iconv( (File.basename(filename)) ).to_s - rename_file = ic.iconv( filename_to_real( File.basename(filename))).to_s if is_attachment + files_paths.each do |filename| + rename_file = File.basename(filename) + rename_file = filename_to_real( File.basename(filename)) if is_attachment begin zipfile.add(rename_file, filename) - flag = false rescue Exception => e - zipfile.get_output_stream('FILE_NOTICE.txt') do |os| - os.write l(:label_file_exist) - end + zipfile.get_output_stream('FILE_NOTICE.txt'){|os| os.write l(:label_file_exist)} next end end unless not_exist_file.empty? - zipfile.get_output_stream('FILE_LOST.txt') do |os| - os.write l(:label_file_lost) + not_exist_file.join(',').to_s - end + zipfile.get_output_stream('FILE_LOST.txt'){|os| os.write l(:label_file_lost) + not_exist_file.join(',').to_s} end end zipfile_name - #rescue Errno => e - # logger.error "[zipdown#zipping] ===> #{e}" - # @error = e end + + # 合理分配文件打包 + # 如果小于 pack_attachment_max_size, 则返回单个文件 + # 反之则切分为多个文件组返回 + def split_pack_files(files, pack_attachment_max_size) + max_size = 0 + last_files = [] + ret_files = [] + files.each_with_index do |f,i| + if (max_size += File.size(f)) > pack_attachment_max_size + max_size = 0 + if last_files.empty? #如果单个文件超过大小,也将此文件作为一组 + ret_files << {files: [f], count: 1, index: ret_files.count+1} + last_files.clear + else + ret_files << {files:last_files, count: last_files.count, index: ret_files.count+1} + last_files.clear + redo + end + else + last_files << f + end + end + + ret_files << {files:last_files, count: last_files.count, index: ret_files.count+1} unless last_files.empty? + ret_files + end + def detect_content_type(name) content_type = Redmine::MimeType.of(name) content_type.to_s @@ -136,4 +262,4 @@ class ZipdownController < ApplicationController attach = Attachment.find_by_disk_filename(name) attach.filename end -end \ No newline at end of file +end diff --git a/app/helpers/account_helper.rb b/app/helpers/account_helper.rb index 8beeac363..827557a40 100644 --- a/app/helpers/account_helper.rb +++ b/app/helpers/account_helper.rb @@ -1,21 +1,74 @@ -# encoding: utf-8 -# -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -module AccountHelper -end +# encoding: utf-8 +# +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module AccountHelper + + def email_activation_register(user, &block) + token = Token.new(:user => user, :action => "register") + if user.save and token.save + UserStatus.create(:user_id => user.id, :changsets_count => 0, :watchers_count => 0) + Mailer.run.register(token) + #flash[:notice] = l(:notice_account_register_done) + #render action: 'email_valid', locals: {:mail => user.mail} + else + yield if block_given? + end + user + end + + def automatically_register(user, &block) + # Automatic activation + user.activate + user.last_login_on = Time.now + if user.save + UserStatus.create(:user_id => user.id, :changsets_count => 0, :watchers_count => 0) + #self.logged_user = user + #flash[:notice] = l(:notice_account_activated) + #redirect_to my_account_url + else + yield if block_given? + end + user + end + + # 自动创建一个新用户,但是初始状态是锁定的 + def automatically_register_lock(user, &block) + user.lock + user.last_login_on = Time.now + if user.save + UserStatus.create(:user_id => user.id, :changsets_count => 0, :watchers_count => 0) + else + yield if block_given? + end + user + end + + def administrator_manually__register(user, &block) + if user.save + UserStatus.create(:user_id => user.id ,:changsets_count => 0, :watchers_count => 0) + # Sends an email to the administrators + Mailer.run.account_activation_request(user) + #account_pending + else + yield if block_given? + end + user + end + +end diff --git a/app/helpers/activities_helper.rb b/app/helpers/activities_helper.rb index c15d89e0c..ede2ed78a 100644 --- a/app/helpers/activities_helper.rb +++ b/app/helpers/activities_helper.rb @@ -1,33 +1,45 @@ -# encoding: utf-8 -# -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -module ActivitiesHelper - def sort_activity_events(events) - events_by_group = events.group_by(&:event_group) - sorted_events = [] - events.sort {|x, y| y.event_datetime <=> x.event_datetime}.each do |event| - if group_events = events_by_group.delete(event.event_group) - group_events.sort {|x, y| y.event_datetime <=> x.event_datetime}.each_with_index do |e, i| - sorted_events << [e, i > 0] unless e.event_description.nil? - end - end - end - sorted_events - end -end +# encoding: utf-8 +# +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module ActivitiesHelper + def sort_activity_events(events) + events_by_group = events.group_by(&:event_group) + sorted_events = [] + events.sort {|x, y| y.event_datetime <=> x.event_datetime}.each do |event| + if group_events = events_by_group.delete(event.event_group) + group_events.sort {|x, y| y.event_datetime <=> x.event_datetime}.each_with_index do |e, i| + sorted_events << [e, i > 0] unless e.event_description.nil? + end + end + end + sorted_events + end + def sort_activity_events_course(events) + events_by_group = events.group_by(&:event_group) + sorted_events = [] + events.sort {|x, y| y.event_datetime <=> x.event_datetime}.each do |event| + if group_events = events_by_group.delete(event.event_group) + group_events.sort {|x, y| y.event_datetime <=> x.event_datetime}.each_with_index do |e, i| + sorted_events << e unless e.event_description.nil? + end + end + end + sorted_events + end +end diff --git a/app/helpers/activity_notifys_helper.rb b/app/helpers/activity_notifys_helper.rb new file mode 100644 index 000000000..f2069a2d3 --- /dev/null +++ b/app/helpers/activity_notifys_helper.rb @@ -0,0 +1,6 @@ +module ActivityNotifysHelper + def get_new_notify_count(container,type) + query = ActivityNotify.where('activity_container_id=? and activity_container_type=? and notify_to=? and is_read=0',container.id,type,User.current.id); + return query.count() + end +end \ No newline at end of file diff --git a/app/helpers/api_helper.rb b/app/helpers/api_helper.rb new file mode 100644 index 000000000..afdc306a4 --- /dev/null +++ b/app/helpers/api_helper.rb @@ -0,0 +1,67 @@ +module ApiHelper + #获取用户的工作单位 + 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 get_assigned_homeworks(homeworks, n, index) + homeworks += homeworks + homeworks[index + 1 .. index + n] + end + + + def stars_to_json_like starts,show_jour,homework,show_name + result = [] + starts.each do |s| + comment = get_homework_review homework,show_jour,s.rater + rater_name = show_name ? s.rater.login : l(:label_anonymous) + rater_id = show_name ? s.rater.id : '' + result << {:rater_id =>rater_id ,:rater_name => rater_name,:created_at => format_time(s.created_at),:stars => s.stars,:comment => comment} + end + result + end + + ######################################################### + #sw + #获取课程未匿评数量 + #param: user => "用户", course_id => "查询的课程ID" + #return: 作业的数量 + ######################################################### + def get_course_anonymous_evaluation user,course + count = 0 + if course + is_teacher = is_course_teacher user,course + if is_teacher #如果是老师,显示学生提交的作业数 + course.homeworks.each do |bid| + count += bid.homeworks.count + end + else #如果是学生,显示未匿评的数量 + course.homeworks.each do |bid| + count += get_student_not_batch_homework_list bid,user + end + end + end + [count,is_teacher] + end + + def get_user_language user + (user.language.nil? || user.language == "") ? 'zh':user.language + end +end \ No newline at end of file diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index cb88742ad..a31bd6189 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -33,6 +33,39 @@ module ApplicationHelper extend Forwardable def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter + # Time 2015-03-24 15:27:29 + # Author lizanle + # Description 从硬盘上删除对应的资源文件 + def delete_kindeditor_assets_from_disk owner_id,owner_type + assets = Kindeditor::Asset.where(["owner_id = ? and owner_type = ?",owner_id,owner_type]) + if !assets.nil? && !assets.blank? + assets.all.each do |asset| + next if asset.nil? + filepath = File.join(Rails.root,"public","files","uploads", + asset[:created_at].to_s.gsub("+0800","").to_datetime.strftime("%Y%m").to_s, + asset[:asset].to_s) + File.delete(filepath) if File.exist?filepath + end + end + end + + # Time 2015-03-24 16:38:05 + # Author lizanle + # Description after save后需要进行资源记录的更新 + # owner_type = 1 对应的是 memo + # owner_type = 2 对应的是forum + # owner_type = 3 对应的是message + # owner_type = 4 对应的是news + # owner_type = 5 对应的是comment + def update_kindeditor_assets_owner ids,owner_id,owner_type + ids.each do |id| + asset = Kindeditor::Asset.find(id.to_i) + asset.owner_id = owner_id + asset.owner_type = owner_type + asset.save + end + end + # Added by young # Define the course menu's link class # 不是数组的转化成数组,然后判断当前menu_item是否在给定的列表 @@ -85,11 +118,44 @@ module ApplicationHelper end #if user.active? || (User.current.admin? && user.logged?) - # link_to name, {:controller=> 'users', :action => 'show', id: user.id, host: Setting.user_domain}, :class => user.css_classes + # link_to name, {:controller=> 'users', :action => 'show', id: user.id, host: Setting.host_user}, :class => user.css_classes #else # name #end - link_to name, {:controller=> 'users', :action => 'show', id: user.id, host: Setting.user_domain}, :class => user.css_classes + link_to name, {:controller=> 'users', :action => 'show', id: user.id, host: Setting.host_user}, :class => user.css_classes + else + h(user.to_s) + end + end + + def link_to_isuue_user(user, options={}) + if user.is_a?(User) + name = h(user.name(options[:format])) + link_to name, {:controller=> 'users', :action => 'show', id: user.id, host: Setting.host_user}, :class => "pro_info_p" + else + h(user.to_s) + end + end + + def link_to_settings_user(user, options={}) + if user.is_a?(User) + name = h(user.name(options[:format])) + link_to name, {:controller=> 'users', :action => 'show', id: user.id, host: Setting.host_user}, :class => "w90 c_orange fl" + else + h(user.to_s) + end + end + + #重载上面方法,增加样式显示 + def link_to_user_header user,canShowRealName=false,options={} + if user.is_a?(User) + if canShowRealName + name = user.show_name + name = user.login if name == "" + else + name = user.login + end + link_to name, {:controller=> 'users', :action => 'show', id: user.id, host: Setting.host_user}, :class => options[:class] else h(user.to_s) end @@ -122,6 +188,28 @@ module ApplicationHelper s end + def link_to_issue_version(issue, options={}) + title = nil + subject = nil + text = options[:tracker] == false ? "##{issue.id}" : "#{issue.tracker} ##{issue.id}" + if options[:subject] == false + title = truncate(issue.subject, :length => 60) + else + subject = issue.subject + if options[:truncate] + subject = truncate(subject, :length => 60) + end + end + if issue.status_id == 5 + s = link_to text, issue_path(issue), :class => "text_line_s", :title => title + else + s = link_to text, issue_path(issue), :class => "c_blue", :title => title + end + s << h(": #{subject}") if subject + s = h("#{issue.project} - ") + s if options[:project] + s + end + # Generates a link to an attachment. # Options: # * :text - Link text (default to attachment filename) @@ -140,10 +228,12 @@ module ApplicationHelper # * :text - Link text (default to attachment filename) # * :download - Force download (default: false) def link_to_attachment(attachment, options={}) + token = options[:token] if options[:token] text = options.delete(:text) || attachment.filename route_method = options.delete(:download) ? :download_named_attachment_path : :named_attachment_path html_options = options.slice!(:only_path) url = send(route_method, attachment, attachment.filename, options) + url << "?token=#{token}" unless token.nil? link_to text, url, html_options end @@ -168,18 +258,18 @@ module ApplicationHelper h(text), {:controller => 'repositories', :action => 'revision', :id => repository.project, :repository_id => repository.identifier_param, :rev => rev}, :title => l(:label_revision_id, format_revision(revision)) - ) + ) end # Generates a link to a message def link_to_message(message, options={}, html_options = nil) link_to( - truncate(message.subject, :length => 60), - board_message_path(message.board_id, message.parent_id || message.id, { - :r => (message.parent_id && message.id), - :anchor => (message.parent_id ? "message-#{message.id}" : nil) - }.merge(options)), - html_options + truncate(message.subject, :length => 60), + board_message_path(message.board_id, message.parent_id || message.id, { + :r => (message.parent_id && message.id), + :anchor => (message.parent_id ? "message-#{message.id}" : nil) + }.merge(options)), + html_options ) end @@ -231,8 +321,23 @@ module ApplicationHelper def thumbnail_tag(attachment) link_to image_tag(thumbnail_path(attachment)), - named_attachment_path(attachment, attachment.filename), - :title => attachment.filename + named_attachment_path(attachment, attachment.filename), + :title => attachment.filename + end + + def thumbnail_issue_tag(attachment) + imagesize = attachment.thumbnail(:size => "200*200") + imagepath = named_attachment_path(attachment, attachment.filename) + if imagesize + link_to image_tag(thumbnail_path(attachment), height: '73', width: '100', class: 'issue_attachment_picture'), + imagepath, + :title => attachment.filename + + else + link_to image_tag(imagepath , height: '73', width: '100', class: 'issue_attachment_picture'), + imagepath, + :title => attachment.filename + end end # 图片缩略图链接 @@ -254,15 +359,15 @@ module ApplicationHelper onclick = "$('##{id}').slideToggle(); " onclick << (options[:focus] ? "$('##{options[:focus]}').focus(); " : "this.blur(); ") onclick << "return false;" - link_to(name, "#", :onclick => onclick) + link_to(name, "javascript:void(0)", :onclick => onclick,:class => options[:class]) end def image_to_function(name, function, html_options = {}) html_options.symbolize_keys! tag(:input, html_options.merge({ - :type => "image", :src => image_path(name), - :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" - })) + :type => "image", :src => image_path(name), + :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" + })) end def format_activity_title(text) @@ -280,9 +385,9 @@ module ApplicationHelper def format_version_name(version) if version.project == @project - h(version) + h(truncate(version.name,:length=>20)) else - h("#{version.project} - #{version}") + h("#{version.project} - #{truncate(version.name,:length=>20)}") end end @@ -296,7 +401,7 @@ module ApplicationHelper # The given collection may be a subset of the whole project tree # (eg. some intermediate nodes are private and can not be seen) #Modified by nie. - def render_project_nested_lists(projects) + def render_project_nested_lists(projects) s = '' if projects.any? ancestors = [] @@ -325,9 +430,9 @@ module ApplicationHelper if project.try(:project_type) == Project::ProjectType_project unless User.current.member_of?(@project) - s << "" - s << watcher_link(@project, User.current)#, ['whiteButton']) - s << "" + s << "" + s << watcher_link(@project, User.current)#, ['whiteButton']) + s << "" end s << (render :partial => 'projects/project', :locals => {:project => project}).to_s else @@ -340,7 +445,7 @@ module ApplicationHelper @project = original_project end s.html_safe - end + end def render_course_nested_lists(courses) s = '' @@ -375,7 +480,7 @@ module ApplicationHelper end - #added by young + #added by young def render_project_nested_lists_new(projects) s = '' if projects.any? @@ -404,7 +509,7 @@ module ApplicationHelper end s.html_safe end - #end + #end def render_page_hierarchy(pages, node=nil, options={}) content = '' if pages[node] @@ -439,14 +544,22 @@ module ApplicationHelper end end + def render_project_settings_tabs(tabs) + if tabs.any? + render :partial => 'common/project_tab', :locals => {:tabs => tabs} + else + content_tag 'p', l(:label_no_data), :class => "nodata" + end + end + # Renders the project quick-jump box def render_project_jump_box return unless User.current.logged? projects = User.current.memberships.collect(&:project).compact.select(&:active?).uniq if projects.any? options = - ("" + - '').html_safe + ("" + + '').html_safe options << project_tree_options_for_select(projects, :selected => @project) do |p| { :value => project_path(:id => p, :jump => current_menu_item) } @@ -480,6 +593,42 @@ module ApplicationHelper Project.project_tree(projects, &block) end + # 项目版本库可见权限判断 + # 条件:1、modules中设置不可见或项目没有版本库;2、如果项目是私有或者项目版本库隐藏则必须是项目成员才可见 + def visible_repository?(project) + @result = false + unless project.enabled_modules.where("name = 'repository'").empty? || project.repositories.count == 0 + if (project.hidden_repo || !project.is_public?) + if User.current.member_of?(project) + @result = true + end + else + @result = true + end + end + return @result + end + + # 判断当前用户是否为项目管理员 + def is_project_manager?(user_id, project_id) + @result = false + mem = Member.where("user_id = ? and project_id = ?",user_id, project_id) + unless mem.blank? + @result = mem.first.roles.to_s.include?("Manager") ? true : false + end + return @result + end + + # 公开项目资源可以引用,admin和管理员和资源上传者拥有设置公开私有权限 + def authority_pubilic_for_files(project, file) + @result = false + if (is_project_manager?(User.current.id, @project.id) && User.current.allowed_to?(:manage_files, project)) || file.author_id == User.current.id || User.current.admin && + project_contains_attachment?(project,file) && file.container_id == project.id && file.container_type == "Project" + @result = true + end + return @result + end + def principals_check_box_tags(name, principals) s = '' principals.each do |principal| @@ -488,11 +637,40 @@ module ApplicationHelper s.html_safe end + #项目成员列表复选框生成 + def project_member_check_box_tags_ex name, principals + s = '' + principals.each do |principal| + s << "
#{@message.content.html_safe}#{ check_box_tag name, principal.id, false, :id => nil } #{h link_to principal.userInfo, user_path( principal.id)} \n" + end + s.html_safe + end + + #缺陷追踪者列表复选框生成 + def issue_watcher_check_box_tags_ex name, principals + s = '' + principals.each do |principal| + s << "#{ check_box_tag name, principal.id, false, :id => nil } #{h link_to principal.userInfo, user_path( principal.id)} \n" + end + s.html_safe + end + + + #扩展的checkbox生成 def principals_check_box_tags_ex(name, principals) s = '' principals.each do |principal| - s << "\n" + s << "\n" + end + s.html_safe + end + + # li标签checkbos扩展 + def principals_check_box_tags_li(name, principals) + s = '' + principals.each do |principal| + s << "#{ check_box_tag name, principal.id, false, :id => nil } #{h link_to principal.userInfo, user_path( principal.id) } \n" end s.html_safe end @@ -607,24 +785,24 @@ module ApplicationHelper link_to(image_tag('2uparrow.png', :alt => l(:label_sort_highest)), url.merge({"#{name}[move_to]" => 'highest'}), :method => method, :title => l(:label_sort_highest)) + - link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)), - url.merge({"#{name}[move_to]" => 'higher'}), - :method => method, :title => l(:label_sort_higher)) + - link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)), - url.merge({"#{name}[move_to]" => 'lower'}), - :method => method, :title => l(:label_sort_lower)) + - link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)), - url.merge({"#{name}[move_to]" => 'lowest'}), - :method => method, :title => l(:label_sort_lowest)) + link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)), + url.merge({"#{name}[move_to]" => 'higher'}), + :method => method, :title => l(:label_sort_higher)) + + link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)), + url.merge({"#{name}[move_to]" => 'lower'}), + :method => method, :title => l(:label_sort_lower)) + + link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)), + url.merge({"#{name}[move_to]" => 'lowest'}), + :method => method, :title => l(:label_sort_lowest)) end def breadcrumb(*args) elements = args.flatten - elements.any? ? content_tag('p', (args.join(" \xc2\xbb ") + " \xc2\xbb ").html_safe, :class => 'breadcrumb') : nil + elements.any? ? content_tag('p', (args.join(" \xc2\xbb ") + " \xc2\xbb ").html_safe, :class => 'wiki_con_tit"') : nil end def other_formats_links(&block) - concat(''.html_safe + l(:label_export_to)) + concat('
'.html_safe + l(:label_export_to)) yield Redmine::Views::OtherFormatsBuilder.new(self) concat('
'.html_safe) end @@ -699,15 +877,15 @@ module ApplicationHelper def textilizable(*args) options = args.last.is_a?(Hash) ? args.pop : {} case args.size - when 1 - obj = options[:object] - text = args.shift - when 2 - obj = args.shift - attr = args.shift - text = obj.send(attr).to_s - else - raise ArgumentError, 'invalid arguments to textilizable' + when 1 + obj = options[:object] + text = args.shift + when 2 + obj = args.shift + attr = args.shift + text = obj.send(attr).to_s + else + raise ArgumentError, 'invalid arguments to textilizable' end return '' if text.blank? project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil) @@ -863,18 +1041,18 @@ module ApplicationHelper # check if page exists wiki_page = link_project.wiki.find_page(page) url = if anchor.present? && wiki_page.present? && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)) && obj.page == wiki_page - "##{anchor}" - else - case options[:wiki_links] - when :local; "#{page.present? ? Wiki.titleize(page) : ''}.html" + (anchor.present? ? "##{anchor}" : '') - when :anchor; "##{page.present? ? Wiki.titleize(page) : title}" + (anchor.present? ? "_#{anchor}" : '') # used for single-file wiki export - else - wiki_page_id = page.present? ? Wiki.titleize(page) : nil - parent = wiki_page.nil? && obj.is_a?(WikiContent) && obj.page && project == link_project ? obj.page.title : nil - url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project, - :id => wiki_page_id, :version => nil, :anchor => anchor, :parent => parent) - end - end + "##{anchor}" + else + case options[:wiki_links] + when :local; "#{page.present? ? Wiki.titleize(page) : ''}.html" + (anchor.present? ? "##{anchor}" : '') + when :anchor; "##{page.present? ? Wiki.titleize(page) : title}" + (anchor.present? ? "_#{anchor}" : '') # used for single-file wiki export + else + wiki_page_id = page.present? ? Wiki.titleize(page) : nil + parent = wiki_page.nil? && obj.is_a?(WikiContent) && obj.page && project == link_project ? obj.page.title : nil + url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project, + :id => wiki_page_id, :version => nil, :anchor => anchor, :parent => parent) + end + end link_to(title.present? ? title.html_safe : h(page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new'))) else # project or wiki doesn't exist @@ -949,110 +1127,110 @@ module ApplicationHelper # project.changesets.visible raises an SQL error because of a double join on repositories if repository && (changeset = Changeset.visible.find_by_repository_id_and_revision(repository.id, identifier)) link = link_to(h("#{project_prefix}#{repo_prefix}r#{identifier}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :repository_id => repository.identifier_param, :rev => changeset.revision}, - :class => 'changeset', - :title => truncate_single_line(changeset.comments, :length => 100)) + :class => 'changeset', + :title => truncate_single_line(changeset.comments, :length => 100)) end end elsif sep == '#' oid = identifier.to_i case prefix - when nil - if oid.to_s == identifier && issue = Issue.visible.find_by_id(oid, :include => :status) - anchor = comment_id ? "note-#{comment_id}" : nil - link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid, :anchor => anchor}, - :class => issue.css_classes, - :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})") - end - when 'document' - if document = Document.visible.find_by_id(oid) - link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, - :class => 'document' - end - when 'version' - if version = Version.visible.find_by_id(oid) - link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, - :class => 'version' - end - when 'message' - if message = Message.visible.find_by_id(oid, :include => :parent) - link = link_to_message(message, {:only_path => only_path}, :class => 'message') - end - when 'forum' - if board = Board.visible.find_by_id(oid) - link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project}, - :class => 'board' - end - when 'news' - if news = News.visible.find_by_id(oid) - link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news}, - :class => 'news' - end - when 'project' - if p = Project.visible.find_by_id(oid) - link = link_to_project(p, {:only_path => only_path}, :class => 'project') - end + when nil + if oid.to_s == identifier && issue = Issue.visible.find_by_id(oid, :include => :status) + anchor = comment_id ? "note-#{comment_id}" : nil + link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid, :anchor => anchor}, + :class => issue.css_classes, + :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})") + end + when 'document' + if document = Document.visible.find_by_id(oid) + link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, + :class => 'document' + end + when 'version' + if version = Version.visible.find_by_id(oid) + link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, + :class => 'version' + end + when 'message' + if message = Message.visible.find_by_id(oid, :include => :parent) + link = link_to_message(message, {:only_path => only_path}, :class => 'message') + end + when 'forum' + if board = Board.visible.find_by_id(oid) + link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project}, + :class => 'board' + end + when 'news' + if news = News.visible.find_by_id(oid) + link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news}, + :class => 'news' + end + when 'project' + if p = Project.visible.find_by_id(oid) + link = link_to_project(p, {:only_path => only_path}, :class => 'project') + end end elsif sep == ':' # removes the double quotes if any name = identifier.gsub(%r{^"(.*)"$}, "\\1") case prefix - when 'document' - if project && document = project.documents.visible.find_by_title(name) - link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, - :class => 'document' - end - when 'version' - if project && version = project.versions.visible.find_by_name(name) - link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, - :class => 'version' - end - when 'forum' - if project && board = project.boards.visible.find_by_name(name) - link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project}, - :class => 'board' - end - when 'news' - if project && news = project.news.visible.find_by_title(name) - link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news}, - :class => 'news' - end - when 'commit', 'source', 'export' - if project - repository = nil - if name =~ %r{^(([a-z0-9\-_]+)\|)(.+)$} - repo_prefix, repo_identifier, name = $1, $2, $3 - repository = project.repositories.detect {|repo| repo.identifier == repo_identifier} - else - repository = project.repository + when 'document' + if project && document = project.documents.visible.find_by_title(name) + link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document}, + :class => 'document' + end + when 'version' + if project && version = project.versions.visible.find_by_name(name) + link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version}, + :class => 'version' + end + when 'forum' + if project && board = project.boards.visible.find_by_name(name) + link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project}, + :class => 'board' + end + when 'news' + if project && news = project.news.visible.find_by_title(name) + link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news}, + :class => 'news' end - if prefix == 'commit' - if repository && (changeset = Changeset.visible.where("repository_id = ? AND scmid LIKE ?", repository.id, "#{name}%").first) - link = link_to h("#{project_prefix}#{repo_prefix}#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :repository_id => repository.identifier_param, :rev => changeset.identifier}, - :class => 'changeset', - :title => truncate_single_line(changeset.comments, :length => 100) + when 'commit', 'source', 'export' + if project + repository = nil + if name =~ %r{^(([a-z0-9\-_]+)\|)(.+)$} + repo_prefix, repo_identifier, name = $1, $2, $3 + repository = project.repositories.detect {|repo| repo.identifier == repo_identifier} + else + repository = project.repository end - else - if repository && User.current.allowed_to?(:browse_repository, project) - name =~ %r{^[/\\]*(.*?)(@([^/\\@]+?))?(#(L\d+))?$} - path, rev, anchor = $1, $3, $5 - link = link_to h("#{project_prefix}#{prefix}:#{repo_prefix}#{name}"), {:controller => 'repositories', :action => (prefix == 'export' ? 'raw' : 'entry'), :id => project, :repository_id => repository.identifier_param, - :path => to_path_param(path), - :rev => rev, - :anchor => anchor}, - :class => (prefix == 'export' ? 'source download' : 'source') + if prefix == 'commit' + if repository && (changeset = Changeset.visible.where("repository_id = ? AND scmid LIKE ?", repository.id, "#{name}%").first) + link = link_to h("#{project_prefix}#{repo_prefix}#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :repository_id => repository.identifier_param, :rev => changeset.identifier}, + :class => 'changeset', + :title => truncate_single_line(changeset.comments, :length => 100) + end + else + if repository && User.current.allowed_to?(:browse_repository, project) + name =~ %r{^[/\\]*(.*?)(@([^/\\@]+?))?(#(L\d+))?$} + path, rev, anchor = $1, $3, $5 + link = link_to h("#{project_prefix}#{prefix}:#{repo_prefix}#{name}"), {:controller => 'repositories', :action => (prefix == 'export' ? 'raw' : 'entry'), :id => project, :repository_id => repository.identifier_param, + :path => to_path_param(path), + :rev => rev, + :anchor => anchor}, + :class => (prefix == 'export' ? 'source download' : 'source') + end end + repo_prefix = nil + end + when 'attachment' + attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil) + if attachments && attachment = Attachment.latest_attach(attachments, name) + link = link_to_attachment(attachment, :only_path => only_path, :download => true, :class => 'attachment') + end + when 'project' + if p = Project.visible.where("identifier = :s OR LOWER(name) = :s", :s => name.downcase).first + link = link_to_project(p, {:only_path => only_path}, :class => 'project') end - repo_prefix = nil - end - when 'attachment' - attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil) - if attachments && attachment = Attachment.latest_attach(attachments, name) - link = link_to_attachment(attachment, :only_path => only_path, :download => true, :class => 'attachment') - end - when 'project' - if p = Project.visible.where("identifier = :s OR LOWER(name) = :s", :s => name.downcase).first - link = link_to_project(p, {:only_path => only_path}, :class => 'project') - end end end end @@ -1069,9 +1247,9 @@ module ApplicationHelper @current_section += 1 if @current_section > 1 content_tag('div', - link_to(image_tag('edit.png'), options[:edit_section_links].merge(:section => @current_section)), - :class => 'contextual', - :title => l(:button_edit_section)) + heading.html_safe + link_to(image_tag('edit.png'), options[:edit_section_links].merge(:section => @current_section)), + :class => 'contextual', + :title => l(:button_edit_section)) + heading.html_safe else heading end @@ -1190,14 +1368,25 @@ module ApplicationHelper # Same as Rails' simple_format helper without using paragraphs def simple_format_without_paragraph(text) text.to_s. - gsub(/\r\n?/, "\n"). # \r\n and \r -> \n - gsub(/\n\n+/, "
"). # 2+ newline -> 2 br - gsub(/([^\n]\n)(?=[^\n])/, '\1
'). # 1 newline -> br - html_safe + gsub(/\r\n?/, "\n"). # \r\n and \r -> \n + gsub(/\n\n+/, "
"). # 2+ newline -> 2 br + gsub(/([^\n]\n)(?=[^\n])/, '\1
'). # 1 newline -> br + html_safe + end + + def wiki_simple_format_without_paragraph(text) + text.to_s. + gsub(/\r\n?/, "\n"). # \r\n and \r -> \n + gsub(/\n\n+/, "
"). # 2+ newline -> 2 br + gsub(/([^\n]\n)(?=[^\n])/, '\1
'). # 1 newline -> br + gsub(" ", " "). #gsub(/<\/?.*?>/,""). + gsub(/<\/?.*?>/, ""). + gsub(""", "'"). + html_safe end def lang_options_for_select(blank=true) - { 'Chinese简体中文 '=> 'zh', :English => :en} + { 'Chinese简体中文 '=> 'zh', :English => :en} end def label_tag_for(name, option_tags = nil, options = {}) @@ -1264,9 +1453,31 @@ module ApplicationHelper def delete_link(url, options={}) options = { - :method => :delete, - :data => {:confirm => l(:text_are_you_sure)}, - :class => 'icon icon-del' + :method => :delete, + :data => {:confirm => l(:text_are_you_sure)}, + :class => 'icon icon-del' + }.merge(options) + + link_to l(:button_delete), url, options + end + + def delete_link_version(url, options={}) + options = { + :method => :delete, + :data => {:confirm => l(:text_are_you_sure)}, + :class => 'c_purple' + }.merge(options) + + link_to l(:button_delete), url, options + end + + + + def delete_new_link(url, options={}) + options = { + :method => :delete, + :data => {:confirm => l(:text_are_you_sure)}, + :class => "c_purple" }.merge(options) link_to l(:button_delete), url, options @@ -1277,11 +1488,11 @@ module ApplicationHelper :href => "#", :onclick => %|submitPreview("#{escape_javascript url_for(url)}", "#{escape_javascript form}", "#{escape_javascript target}"); return false;|, :accesskey => accesskey(:preview) - }.merge(options) + }.merge(options) end def link_to_function(name, function, html_options={}) - content_tag(:a, name, {:href => '#', :onclick => "#{function}; return false;"}.merge(html_options)) + content_tag(:a, name, {:href => '#', :onclick => "#{function}; return false;"}.merge(:class => " c_purple")) end # Helper to render JSON in views @@ -1303,9 +1514,8 @@ module ApplicationHelper end def check_all_links(form_name) - link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") + - " | ".html_safe + - link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)") + link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") + " ".html_safe + " | "+ " ".html_safe + + link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)") end def progress_bar(pcts, options={}) @@ -1316,12 +1526,12 @@ module ApplicationHelper width = options[:width] || '100px;' legend = options[:legend] || '' content_tag('table', - content_tag('tr', - (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0]}%;", :class => 'closed') : ''.html_safe) + - (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1]}%;", :class => 'done') : ''.html_safe) + - (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2]}%;", :class => 'todo') : ''.html_safe) - ), :class => 'progress', :style => "width: #{width};").html_safe + - content_tag('p', legend, :class => 'percent').html_safe + content_tag('tr', + (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0]}%;", :class => 'closed') : ''.html_safe) + + (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1]}%;", :class => 'done') : ''.html_safe) + + (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2]}%;", :class => 'todo') : ''.html_safe) + ), :class => 'progress', :style => "width: #{width};").html_safe + + content_tag('p', legend, :class => 'percent').html_safe end def checked_image(checked=true) @@ -1334,7 +1544,7 @@ module ApplicationHelper unless @context_menu_included content_for :header_tags do javascript_include_tag('context_menu') + - stylesheet_link_tag('context_menu') + stylesheet_link_tag('context_menu') end if l(:direction) == 'rtl' content_for :header_tags do @@ -1365,7 +1575,7 @@ module ApplicationHelper tags = javascript_tag( "var datepickerOptions={dateFormat: 'yy-mm-dd', firstDay: #{start_of_week}, " + "showOn: 'button', buttonImageOnly: true, buttonImage: '" + - path_to_image('/images/calendar.png') + + path_to_image('/images/public_icon.png') + "', showButtonPanel: true, showWeek: true, showOtherMonths: true, selectOtherMonths: true};") jquery_locale = l('jquery.locale', :default => current_language.to_s) unless jquery_locale == 'en' @@ -1387,7 +1597,7 @@ module ApplicationHelper tags = javascript_tag( "var datepickerOptions={dateFormat: 'yy-mm-dd',minDate: new Date(), firstDay: #{start_of_week}, " + "showOn: 'button', buttonImageOnly: true, buttonImage: '" + - path_to_image('/images/calendar.png') + + path_to_image('/images/public_icon.png') + "', showButtonPanel: true, showWeek: true, showOtherMonths: true, selectOtherMonths: true, onClose: function(dateText, inst) {TimeClose(dateText,inst);}, beforeShow : function(input){TimeBeforeShow(input);} };") jquery_locale = l('jquery.locale', :default => current_language.to_s) unless jquery_locale == 'en' @@ -1593,11 +1803,72 @@ module ApplicationHelper end s end - + def get_memo @new_memo = Memo.new - #@new_memo.subject = "有什么想说的,尽管来咆哮吧~~" - @public_forum = Forum.find(1) + @public_forum = Forum.find(1) rescue ActiveRecord::RecordNotFound + end + + #获取用户未过期的课程 + def get_user_course user + courses_doing = [] + user.courses.each do |course| + if !course_endTime_timeout?(course) + courses_doing.push course + end + end + courses_doing + end + + + def attachment_candown attachment + candown = false + if attachment.container + if attachment.container.class.to_s != "HomeworkAttach" && attachment.container.class.to_s != "StudentWork" && (attachment.container.has_attribute?(:project) || attachment.container.has_attribute?(:project_id)) && attachment.container.project + project = attachment.container.project + candown= User.current.member_of?(project) || (project.is_public && attachment.is_public == 1) + elsif attachment.container.is_a?(Project) + project = attachment.container + candown= User.current.member_of?(project) || (project.is_public && attachment.is_public == 1) + elsif (attachment.container.has_attribute?(:board) || attachment.container.has_attribute?(:board_id)) && attachment.container.board && + attachment.container.board.project + project = attachment.container.board.project + candown = User.current.member_of?(project) || (project.is_public && attachment.is_public == 1) + elsif (attachment.container.has_attribute?(:course) ||attachment.container.has_attribute?(:course_id) ) && attachment.container.course + course = attachment.container.course + candown = User.current.member_of_course?(course) || (course.is_public==1 && attachment.is_public == 1) + elsif attachment.container.is_a?(Course) + course = attachment.container + candown= User.current.member_of_course?(course) || (course.is_public==1 && attachment.is_public == 1) + elsif (attachment.container.has_attribute?(:board) || attachment.container.has_attribute?(:board_id)) && attachment.container.board && + attachment.container.board.course + course = attachment.container.board.course + candown= User.current.member_of_course?(course) || (course.is_public==1 && attachment.is_public == 1) + elsif attachment.container.class.to_s=="HomeworkAttach" + candown = true + elsif attachment.container.class.to_s=="StudentWorksScore" + candown = true + elsif attachment.container.class.to_s=="StudentWork" + candown = true + elsif attachment.container_type == "Bid" && attachment.container && attachment.container.courses + course = attachment.container.courses.first + candown = User.current.member_of_course?(attachment.container.courses.first) || (course.is_public == 1 && attachment.is_public == 1) + else + candown = (attachment.is_public == 1 || attachment.is_public == true) + end + end + candown + end + + def project_type_link(text, value) + if value == 1 + link_to "#{text}".html_safe,"javascript:void(0)" ,:onClick => "show_window();", :class => "pr_join_a",:id => "setting_project_type" + elsif value == 2 + link_to "#{text}".html_safe,"javascript:void(0)" ,:onClick => "show_window();", :class => "pr_join_a",:id => "setting_project_type" + else + link_to "#{text}".html_safe,"javascript:void(0)" ,:onClick => "show_window();", :class => "pr_join_a",:id => "setting_project_type" + end + end private @@ -1629,11 +1900,11 @@ module ApplicationHelper html << (content_tag "span", l(:label_no_current_watchers)) end for user in User.watched_by(obj.id) - html << (link_to image_tag(url_to_avatar(user), :class => "avatar"), user_path(user), :class => "avatar", :title => "#{user.name}") - count = count + 1 - if count >= 12 - break - end + html << (link_to image_tag(url_to_avatar(user), :class => "avatar"), user_path(user), :class => "avatar", :title => "#{user.name}") + count = count + 1 + if count >= 12 + break + end end html.html_safe end @@ -1659,13 +1930,13 @@ module ApplicationHelper html.html_safe end - def show_bid_fans_picture(obj) + def show_bid_fans_picture(obj) html = '' if obj.watcher_users.count == 0 html << (content_tag "span", l(:label_project_no_follow)) else obj.watcher_users.take(12).each do |user| - html << (link_to image_tag(url_to_avatar(user), :class => "avatar"), user_path(user), :class => "avatar", :title => user.name) + html << (link_to image_tag(url_to_avatar(user), :class => "avatar"), user_path(user), :class => "avatar", :title => user.name) end end html.html_safe @@ -1700,7 +1971,7 @@ module ApplicationHelper html.html_safe end - def show_contest_project(contest) + def show_contest_project(contest) html = '' if contest.projects.where('is_public = 1').count == 0 html << (content_tag "p", l(:label_no_bid_project), :class => "font_lighter") @@ -1712,7 +1983,7 @@ module ApplicationHelper html.html_safe end - def show_contest_softapplication(contest) + def show_contest_softapplication(contest) html = '' if contest.softapplications.where('is_public = 1').count == 0 html << (content_tag "p", l(:label_no_contest_softapplication), :class => "font_lighter") @@ -1724,17 +1995,17 @@ module ApplicationHelper html.html_safe end - def show_contest_fans_picture(obj) + def show_contest_fans_picture(obj) html = '' if obj.watcher_users.count == 0 html << (content_tag "span", l(:label_project_no_follow)) else obj.watcher_users.take(12).each do |user| - html << (link_to image_tag(url_to_avatar(user), :class => "avatar"), user_path(user), :class => "avatar", :title => user.name) + html << (link_to image_tag(url_to_avatar(user), :class => "avatar"), user_path(user), :class => "avatar", :title => user.name) end end html.html_safe - end + end #display fans picture def show_more_fans?(obj) @@ -1751,13 +2022,13 @@ module ApplicationHelper html << (content_tag "span", l(:label_no_current_fans)) else obj.watcher_users.take(12).each do |user| - html << (link_to image_tag(url_to_avatar(user), :class => "avatar"), user_path(user), :class => "avatar", :title => user.name) + html << (link_to image_tag(url_to_avatar(user), :class => "avatar"), user_path(user), :class => "avatar", :title => user.name) end end html.html_safe end - # added by bai + # added by bai def show_more_participate?(obj) if obj.join_in_contests.count > 12 return true @@ -1766,18 +2037,18 @@ module ApplicationHelper end end - def show_participate_picture(obj) + def show_participate_picture(obj) html = '' count = 0 if obj.join_in_contests.count == 0 html << (content_tag "span", l(:label_no_current_participate)) end for temp in obj.join_in_contests - html << (link_to image_tag(url_to_avatar(temp.user), :class => "avatar"), user_path(temp.user), :class => "avatar", :title => "#{temp.user.name}") - count = count + 1 - if count >= 12 - break - end + html << (link_to image_tag(url_to_avatar(temp.user), :class => "avatar"), user_path(temp.user), :class => "avatar", :title => "#{temp.user.name}") + count = count + 1 + if count >= 12 + break + end end html.html_safe end @@ -1786,14 +2057,14 @@ module ApplicationHelper # add by huang def show_watcher_list(user) - html = '' - count = 0 + html = '' + count = 0 for user in User.watched_by(user.id) - html << (link_to image_tag(url_to_avatar(user), :class => "avatar"), user_path(user), :class => "avatar", :title => "#{user.name}") - count = count + 1 - if count >= 12 - break - end + html << (link_to image_tag(url_to_avatar(user), :class => "avatar"), user_path(user), :class => "avatar", :title => "#{user.name}") + count = count + 1 + if count >= 12 + break + end end html.html_safe end @@ -1811,62 +2082,68 @@ module ApplicationHelper return true if bid.nil? case bid.homework_type - when Bid::HomeworkFile - attaches = HomeworkAttach.where(bid_id: curb) - attaches.map(&:user_id).include? cur - when Bid::HomeworkProject - attaches = BidingProject.where(user_id: User.current, bid_id: bid) - attaches.count > 0 # > 0 则有提交记录 - else - true + when Bid::HomeworkFile + attaches = HomeworkAttach.where(bid_id: curb) + attaches.map(&:user_id).include? cur + when Bid::HomeworkProject + attaches = BidingProject.where(user_id: User.current, bid_id: bid) + attaches.count > 0 # > 0 则有提交记录 + else + true end end def render_dynamic_nav home_link = link_to l(:field_homepage), {:controller => 'welcome', :action => 'index'} - home_link = "" << home_link << " " + home_link = "" << home_link << " " # bootstrap_render_dynamic_nav content_tag :ul, (home_link.html_safe+bootstrap_render_dynamic_nav) end def bootstrap_render_dynamic_nav + hidden_non_project = Setting.find_by_name("hidden_non_project") + visiable = !(hidden_non_project && hidden_non_project.value == "0") - main_course_link = link_to l(:label_course_practice), {:controller => 'welcome', :action => 'index', :host => Setting.course_domain} - main_project_link = link_to l(:label_project_deposit), {:controller => 'welcome', :action => 'index', :host => Setting.project_domain} - main_contest_link = link_to l(:label_contest_innovate), {:controller => 'welcome', :action => 'index', :host => Setting.contest_domain} + main_course_link = link_to l(:label_course_practice), {:controller => 'welcome', :action => 'index', :host => Setting.host_course} + main_project_link = link_to l(:label_project_deposit), {:controller => 'welcome', :action => 'index', :host => Setting.host_name} + main_contest_link = link_to l(:label_contest_innovate), {:controller => 'welcome', :action => 'index', :host => Setting.host_contest} # course_all_course_link = link_to l(:label_course_all), {:controller => 'courses', :action => 'index'} - course_teacher_all_link = link_to l(:label_teacher_all), {:controller => 'users', :action => 'index', :role => 'teacher', :host => Setting.course_domain} + course_teacher_all_link = link_to l(:label_teacher_all), {:controller => 'users', :action => 'index', :role => 'teacher', :host => Setting.host_course} # courses_link = link_to l(:label_course_practice), {:controller => 'courses', :action => 'index'} - users_link = link_to l(:label_software_user), {:controller => 'users', :action => 'index', :host => Setting.user_domain} + #users_link = link_to l(:label_software_user), {:controller => 'users', :action => 'index', :host => Setting.host_user} # contest_link = link_to l(:label_contest_innovate), {:controller => 'contests', :action => 'index'} - bids_link = link_to l(:label_requirement_enterprise), {:controller => 'bids', :action => 'index'} - forum_link = link_to l(:label_project_module_forums), {:controller => "forums", :action => "index"} + # bids_link = link_to l(:label_requirement_enterprise), {:controller => 'bids', :action => 'index'} + forum_link = link_to l(:label_forum_all), {:controller => "forums", :action => "index"} stores_link = link_to l(:label_stores_index), {:controller => 'stores', :action=> 'index'} school_all_school_link = link_to l(:label_school_all), {:controller => 'school', :action => 'index'} + project_new_link = link_to l(:label_project_new), {:controller => 'projects', :action => 'new', :host => Setting.host_name} + # project_mine_link = link_to l(:label_my_project), {:controller => 'users', :action => 'user_projects', :host => Setting.host_name} #@nav_dispaly_project_label nav_list = Array.new - nav_list.push(school_all_school_link) if @nav_dispaly_course_all_label && @show_course == 1 + nav_list.push(school_all_school_link) if @nav_dispaly_course_all_label && @show_course == 1 && visiable # nav_list.push(course_all_course_link) if @nav_dispaly_course_all_label && @show_course == 1 - nav_list.push(course_teacher_all_link) if @nav_dispaly_teacher_all_label && @show_course == 1 + nav_list.push(course_teacher_all_link) if @nav_dispaly_teacher_all_label && @show_course == 1 && visiable nav_list.push(main_project_link) if @nav_dispaly_main_project_label - nav_list.push(main_course_link) if @nav_dispaly_main_course_label && @show_course == 1 - nav_list.push(main_contest_link) if @nav_dispaly_main_contest_label && @show_contest == 1 + nav_list.push(main_course_link) if @nav_dispaly_main_course_label && @show_course == 1 && visiable + nav_list.push(main_contest_link) if @nav_dispaly_main_contest_label && @show_contest == 1 && visiable - nav_list.push(courses_link) if @nav_dispaly_course_label && @show_course == 1 + nav_list.push(courses_link) if @nav_dispaly_course_label && @show_course == 1 && visiable + nav_list.push(project_new_link) if @nav_dispaly_project_label + # nav_list.push(project_mine_link) if @nav_dispaly_main_project_label # nav_list.push(projects_link) if @nav_dispaly_project_label - nav_list.push(users_link) if @nav_dispaly_user_label + #nav_list.push(users_link) if @nav_dispaly_user_label # nav_list.push(contest_link) if @nav_dispaly_contest_label && @show_contest == 1 - nav_list.push(bids_link) if @nav_dispaly_bid_label - nav_list.push(forum_link) if @nav_dispaly_forum_label - nav_list.push(stores_link) if @nav_dispaly_store_all_label + nav_list.push(bids_link) if @nav_dispaly_bid_label && visiable + nav_list.push(forum_link) if @nav_dispaly_forum_label && visiable + nav_list.push(stores_link) if @nav_dispaly_store_all_label && visiable content_li = '' nav_list.collect do |nav_item| - content_li << content_tag(:li, nav_item) + content_li << content_tag(:li, nav_item, :class => 'topnav_a fl') end content_li.html_safe end @@ -1876,12 +2153,12 @@ module ApplicationHelper end # def hadcommittedforcontest(curu) - # message = JournalsForMessage.find_by_sql("select * from journals_for_messages where jour_type = 'Softapplication' ") - # message.each do |createmessage| - # if createmessage.user_id == curu - # return true - # end - # end + # message = JournalsForMessage.find_by_sql("select * from journals_for_messages where jour_type = 'Softapplication' ") + # message.each do |createmessage| + # if createmessage.user_id == curu + # return true + # end + # end # end def footer_logo(ul_class=nil, li_class=nil) @@ -1901,28 +2178,28 @@ module ApplicationHelper def sort_homework_path(bid, sort, direction) case self.action_name - when 'show_courseEx' - get_not_batch_homework_homework_attach_index_path(bid_id: bid.id, sort: sort, direction: 'asc') - when 'get_not_batch_homework' - get_not_batch_homework_homework_attach_index_path(bid_id: bid.id, sort: sort, direction: direction) - when 'get_batch_homeworks' - get_batch_homeworks_homework_attach_index_path(bid_id: bid.id, sort: sort, direction: direction) - when 'get_homeworks' - get_homeworks_homework_attach_index_path(bid_id: bid.id, sort: sort, direction: direction) - else - '#' + when 'show_courseEx' + get_not_batch_homework_homework_attach_index_path(bid_id: bid.id, sort: sort, direction: 'asc') + when 'get_not_batch_homework' + get_not_batch_homework_homework_attach_index_path(bid_id: bid.id, sort: sort, direction: direction) + when 'get_batch_homeworks' + get_batch_homeworks_homework_attach_index_path(bid_id: bid.id, sort: sort, direction: direction) + when 'get_homeworks' + get_homeworks_homework_attach_index_path(bid_id: bid.id, sort: sort, direction: direction) + else + '#' end end def anonymous_comment_link(bid, course) link = case bid.comment_status when 0 - confirm_info = "开启匿评后学生将不能对作业进行提交、修改、删除等操作\n" + confirm_info = "开启匿评后学生将不能对作品进行提交、修改、删除等操作\n" confirm_info += anonymous_comment_notice(bid,course) confirm_info += '是否确定开启匿评?' link_to '启动匿评', start_anonymous_comment_bid_path(bid), id: "#{bid.id}_start_anonymous_comment", remote: true, :confirm => confirm_info, disable_with: '加载中...' when 1 - confirm_info = "关闭匿评后所有同学将不能继续进行匿评,且将公开已提交作业列表\n" + confirm_info = "关闭匿评后所有同学将不能继续进行匿评,且将公开已提交作品列表\n" confirm_info += anonymous_comment_notice(bid,course) confirm_info += '是否确定关闭匿评?' link_to '关闭匿评', stop_anonymous_comment_bid_path(bid), id: "#{bid.id}_stop_anonymous_comment", remote: true, :confirm => confirm_info @@ -1938,7 +2215,7 @@ module ApplicationHelper @student_size ||= searchStudent(course).size @homework_size = bid.homeworks.size percent = @homework_size.to_f / (@student_size == 0 ? 1 : @student_size) - confirm_info = "目前#{@student_size}个学生,总共提交了#{@homework_size}份作业,占#{number_to_percentage(percent * 100, precision: 1)}\n" + confirm_info = "目前#{@student_size}个学生,总共提交了#{@homework_size}份作品,占#{number_to_percentage(percent * 100, precision: 1)}\n" when 1 @homework_evaluations = 0 bid.homeworks.map { |homework| @homework_evaluations += homework.homework_evaluations.count} @@ -1955,8 +2232,111 @@ module ApplicationHelper bid.homeworks.map { |homework| @has_evaluations += homework.rates(:quality).where("seems_rateable_rates.rater_id not in #{teachers}").count} percent = @has_evaluations.to_f / (@homework_evaluations == 0 ? 1 : @homework_evaluations) - confirm_info = "目前总共分配了#{@homework_evaluations}份匿评作业,已评价#{@has_evaluations}份作业,占#{number_to_percentage(percent * 100, precision: 1)}\n" + confirm_info = "目前总共分配了#{@homework_evaluations}份匿评作品,已评价#{@has_evaluations}份作品,占#{number_to_percentage(percent * 100, precision: 1)}\n" end confirm_info end + + def get_technical_title user + if user.user_extensions.technical_title == "Professor" || user.user_extensions.technical_title == "教授" + technical_title = l(:label_technicl_title_professor) + elsif user.user_extensions.technical_title == "Associate professor" || user.user_extensions.technical_title == "副教授" + technical_title = l(:label_technicl_title_associate_professor) + elsif user.user_extensions.technical_title == "Lecturer" || user.user_extensions.technical_title == "讲师" + technical_title = l(:label_technicl_title_lecturer) + elsif user.user_extensions.technical_title == "Teaching assistant" || user.user_extensions.technical_title == "助教" + technical_title = l(:label_technicl_title_teaching_assistant) + end + technical_title + end + + + def ie8? + request.env["HTTP_USER_AGENT"] =~ /MSIE 8.0/ + end + + + #获取指定资源列表的TAG的集合以及每个TAG的数量,降序排序 + def attachment_tag_list attachments + tag_list = Hash.new + attachments.each do |attachment| + attachment.tag_list.map{|tag| tag_list.has_key?(tag) ? tag_list[tag] = tag_list[tag] + 1 : tag_list[tag] = 1} + end + tag_list.sort {|a,b| b[1]<=>a[1]} + end + + #获取课程资源的TAG云 + def get_course_tag_list course + all_attachments = course.attachments.select{|attachment| attachment.is_public? || + (attachment.container_type == "Course" && User.current.member_of_course?(course))|| + attachment.author_id == User.current.id + } + tag_list = attachment_tag_list all_attachments + tag_list + end + + #获取匿评相关连接代码 + def homework_anonymous_comment homework + if homework.homework_type == 1 && homework.homework_detail_manual #匿评作业 + if Time.parse(homework.end_time.to_s).strftime("%Y-%m-%d") >= Time.now.strftime("%Y-%m-%d") + link = "启动匿评".html_safe + elsif homework.student_works.count >= 2 #作业份数大于2 + case homework.homework_detail_manual.comment_status + when 1 + link = link_to '启动匿评', alert_anonymous_comment_homework_common_path(homework), id: "#{homework.id}_start_anonymous_comment", remote: true, disable_with: '加载中...',:class => 'fr mr10 work_edit' + when 2 + link = link_to '关闭匿评', alert_anonymous_comment_homework_common_path(homework), id: "#{homework.id}_stop_anonymous_comment", remote: true,:class => 'fr mr10 work_edit' + when 3 + link = "匿评结束".html_safe + end + else + link = "启动匿评".html_safe + end + else + link = "启动匿评".html_safe + end + link + end + #学生根据传入作业确定显示为编辑作品还是新建作品 + def student_new_homework homework + work = cur_user_works_for_homework homework + if work.nil? + link_to l(:label_commit_homework), new_student_work_path(:homework => homework.id),:class => 'fr mr10 work_edit' + else + if homework.homework_type == 1 && homework.homework_detail_manual && homework.homework_detail_manual.comment_status != 1 #匿评作业,且作业状态不是在开启匿评之前 + "#{l(:label_edit_homework)}".html_safe + else + link_to l(:label_edit_homework), edit_student_work_path(work.id),:class => 'fr mr10 work_edit' + end + end + end + + def student_anonymous_comment homework + if homework.homework_type == 1 && homework.homework_detail_manual + case homework.homework_detail_manual.comment_status + when 1 + "未开启匿评".html_safe + when 2 + "正在匿评中".html_safe + when 3 + "匿评已结束".html_safe + end + elsif homework.homework_type == 0 + "未启用匿评".html_safe + elsif homework.homework_type == 2 + "编程作业".html_safe + end + end + + #获取当前用户在指定作业下提交的作业的集合 + def cur_user_works_for_homework homework + homework.student_works.where("user_id = ?",User.current).first + end + + def file_preview_tag(file, html_options={}) + if %w(pdf pptx doc docx xls xlsx).any?{|x| file.filename.downcase.end_with?(x)} + link_to '预览', download_named_attachment_path(file.id, file.filename, preview: true),html_options + end + end + end diff --git a/app/helpers/attachments_helper.rb b/app/helpers/attachments_helper.rb index 50dd79a08..be24629af 100644 --- a/app/helpers/attachments_helper.rb +++ b/app/helpers/attachments_helper.rb @@ -34,6 +34,26 @@ module AttachmentsHelper :locals => {:attachments => container.attachments, :options => options, :thumbnails => (options[:thumbnails] && Setting.thumbnails_enabled?)} end end + + def link_to_attachment_project(container, options = {}) + options.assert_valid_keys(:author, :thumbnails) + + if container.attachments.any? + options = {:deletable => container.attachments_deletable?, :author => true}.merge(options) + render :partial => 'attachments/project_file_links', + :locals => {:attachments => container.attachments, :options => options, :thumbnails => (options[:thumbnails] && Setting.thumbnails_enabled?)} + end + end + + def link_to_attachments_course(container, options = {}) + options.assert_valid_keys(:author, :thumbnails) + + if container.attachments.any? + options = {:deletable => container.attachments_deletable?, :author => true}.merge(options) + render :partial => 'attachments/course_file_links', + :locals => {:attachments => container.attachments, :options => options, :thumbnails => (options[:thumbnails] && Setting.thumbnails_enabled?)} + end + end def attach_delete(project) if User.current.logged? && (User.current.admin? || (!Member.where('user_id = ? and project_id = ?', User.current.id, project.bid.courses.first.id).first.nil? && (Member.where('user_id = ? and project_id = ?', User.current.id, project.bid.courses.first.id).first.roles&Role.where('id = ? or id = ?', 3, 7)).size >0) || project.user_id == User.current.id) @@ -67,6 +87,7 @@ module AttachmentsHelper end end + #判断课程course中是否包含课件attachment,course中引用了attachment也算作包含 def course_contains_attachment? course,attachment course.attachments.each do |att| if att.id == attachment.id || (!att.copy_from.nil? && !attachment.copy_from.nil? && att.copy_from == attachment.copy_from) || att.copy_from == attachment.id || att.id == attachment.copy_from @@ -75,6 +96,15 @@ module AttachmentsHelper end false end + #判断项目project中是否包含课件attachment,project中引用了attachment也算作包含 + def project_contains_attachment? project,attachment + project.attachments.each do |att| + if att.id == attachment.id || (!att.copy_from.nil? && !attachment.copy_from.nil? && att.copy_from == attachment.copy_from) || att.copy_from == attachment.id || att.id == attachment.copy_from + return true + end + end + false + end def get_qute_number attachment if attachment.copy_from diff --git a/app/helpers/courses_helper.rb b/app/helpers/courses_helper.rb index a862753ef..a0bd18582 100644 --- a/app/helpers/courses_helper.rb +++ b/app/helpers/courses_helper.rb @@ -20,11 +20,45 @@ module CoursesHelper # 返回教师数量,即roles表中定义的Manager def teacherCount project - project.members.count - studentCount(project).to_i + project ? project.members.count - studentCount(project).to_i : 0 # or # searchTeacherAndAssistant(project).count end + #课程模块需要展示的模块 + def course_model + @nav_dispaly_course_all_label = 1 + @nav_dispaly_forum_label = 1 + @nav_dispaly_course_label = nil + @nav_dispaly_store_all_label = 1 + end + + #生成课程老师成员链接 + def course_teacher_link teacher_num + if User.current.member_of_course?(@course) || User.current.admin? + link_to "#{teacher_num}", course_member_path(@course, :role => 1), :class => 'info_foot_num c_blue' + else + content_tag 'span',teacher_num, :class => 'info_foot_num c_blue' + end + end + + #生成课程学生列表连接 + def course_student_link student_num + if (User.current.logged? && @course.open_student == 1) || (User.current.member_of_course?(@course)) || User.current.admin? + link_to "#{student_num}", course_member_path(@course, :role => 2), :class => 'info_foot_num c_blue' + else + content_tag 'span',student_num, :class => 'info_foot_num c_blue' + end + end + + def course_poll_count + Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id} and polls_status in (2,3)").count + end + + def course_feedback_count + @course.journals_for_messages.where('m_parent_id IS NULL').count + end + # 返回学生数量,即roles表中定义的Reporter #def studentCount project # searchStudent(project).count @@ -111,10 +145,14 @@ module CoursesHelper #garble count # end + #获取课程所有成员 + def course_all_member course + course.members + end # 学生人数计算 # add by nwb def studentCount course - course.student.count.to_s#course.student.count + course ? course.student.count.to_s : 0#course.student.count end #课程成员数计算 @@ -163,14 +201,13 @@ module CoursesHelper end members end + def searchStudent project #searchPeopleByRoles(project, StudentRoles) members = [] - project.members.each do |m| if m && m.user && m.user.allowed_to?(:as_student,project) members << m - end end members @@ -195,10 +232,10 @@ module CoursesHelper #searchPeopleByRoles(project, StudentRoles) mems = [] if name != "" + name = name.to_s.downcase members.each do |m| - - username = m.user[:lastname].to_s + m.user[:firstname].to_s - if(m.user[:login].to_s.include?(name) || m.user.user_extensions[:student_id].to_s.include?(name) || username.include?(name)) + 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 @@ -347,10 +384,28 @@ module CoursesHelper str end - # added by nwb + # added by meng + # 课程time+term简写(2014.春/2014.秋)国际化输出 def get_course_term course - str = ( course.try(:time).to_s << '.' << course.try(:term).to_s ) - str[0..-4] + strterm = course.try(:term).to_s + if !(User.current.language == 'zh'||User.current.language == '') + strterm == '春季学期' ? strterm = 'spring term' : strterm = 'autumn term' + str = ( course.try(:time).to_s << '.' << strterm ) + str[0..-6] + else + str = ( course.try(:time).to_s << '.' << strterm ) + str[0..-4] + end + end + + # added by meng + # 课程term(春季学期/秋季学期)国际化输出 + def get_course_term_locales course + str = course.try(:term).to_s + if !(User.current.language == 'zh'||User.current.language == '') + str == '春季学期' ? str = ' ' + 'spring term' : str = ' ' + 'autumn term' + end + return str end def members_to_user_ids members @@ -372,40 +427,19 @@ module CoursesHelper now > cTime end + def find_by_extra_from_project extra Course.find_by_extra(try(extra)) end + #判断指定用户是不是当前课程的老师 def is_course_teacher (user,course) - #course.members.joins(:member_roles).where("member_roles.role_id IN (:role_id) and members.user_id = #{user.id}", {:role_id => TeacherRoles}).count != 0 user.allowed_to?(:as_teacher,course) - #修改为根据用户是否有发布任务的权限来判断用户是否是课程的老师 - #is_teacher = false - #@membership = user.memberships.all(:conditions => Project.visible_condition(User.current)) - #@membership.each do |membership| - # unless(membership.project.project_type==0) - # if user.allowed_to?({:controller => "projects", :action => "new_homework"}, membership.project, :global => false) - # is_teacher = true - # end - # end - #end - #is_teacher end + #当前用户是不是指定课程的学生 def is_cur_course_student course - #course.members.joins(:member_roles).where("member_roles.role_id IN (:role_id) and members.user_id = #{User.current.id}", {:role_id => StudentRoles}).count != 0 User.current.logged? && User.current.member_of_course?(course) && !(User.current.allowed_to?(:as_teacher,course)) - #修改:能新建占位且不能新建任务的角色判定为学生 - #is_student = false - #@membership = User.current.memberships.all(:conditions => Project.visible_condition(User.current)) - #@membership.each do |membership| - # unless(membership.project.project_type==0) - # if !User.current.allowed_to?({:controller => "projects", :action => "new_homework"}, membership.project, :global => false) && User.current.allowed_to?({:controller => "homework_attach", :action => "new"}, membership.project, :global => false) - # is_student = true - # end - # end - #end - #is_student end #获取当前用户在指定作业下提交的作业的集合 def cur_user_homework_for_bid bid @@ -423,61 +457,6 @@ module CoursesHelper homework.nil? ? [] : (homework.users + [homework.user]) end - #获取指定作业的最终评分 - #最终评分 = 学生评分的平均分 * 0.4 +教师评分 * 0.6 - def score_for_homework homework - if homework.bid.is_evaluation == 1 || homework.bid.is_evaluation == nil - return format("%.2f",(homework.bid.proportion * 1.0 / 100) * (teacher_score_for_homework(homework).to_f) + (1 - homework.bid.proportion * 1.0 / 100) * (student_score_for_homework(homework).to_f)) - else - return teacher_score_for_homework homework - end - end - def score_for_homework_new homework - if teacher_score_for_homework(homework) != 0 - return teacher_score_for_homework homework - else - return student_score_for_homework homework - end - end - #获取作业的互评得分 - def student_score_for_homework homework - #member = searchTeacherAndAssistant(homework.bid.courses.first).first#searchPeopleByRoles(homework.bid.courses.first,TeacherRoles).first - #if member.nil? - # return "0.00" - #end - #student_stars = homework.rates(:quality).where("rater_id <> #{member.user_id}").select("stars") - members = searchStudent(homework.bid.courses.first) - user_ids = [] - members.each do |user| - user_ids << user.user_id - end - student_stars = homework.rates(:quality).where("rater_id in (:user_ids)",{:user_ids => user_ids}).select("stars") - student_stars_count = 0 - student_stars.each do |star| - student_stars_count = student_stars_count + star.stars - end - return format("%.2f",student_stars_count / (student_stars.count == 0 ? 1 : student_stars.count)) - - - - end - - #获取作业的教师评分 - #多个教师只获取一份教师评分 - def teacher_score_for_homework homework - members = searchTeacherAndAssistant(homework.bid.courses.first) - teacher_ids = [] - members.each do |user| - teacher_ids << user.user_id - end - teacher_stars = homework.rates(:quality).where("rater_id in (:teacher_ids)",{:teacher_ids => teacher_ids}).select("stars") - teacher_stars_count = 0 - teacher_stars.each do |star| - teacher_stars_count = teacher_stars_count + star.stars - end - return format("%.2f",teacher_stars.count > 0 ? teacher_stars_count/teacher_stars.count : 0) - end - #获取指定项目的得分 def project_score project issue_count = project.issues.count @@ -561,12 +540,13 @@ module CoursesHelper def course_in_current_or_next_term course is_current_term = false is_next_term = false - if course.time == Time.now.year && course.term == cur_course_term + year_now = Time.now.month < 3 ? Time.now.year - 1:Time.now.year + if course.time == year_now && course.term == cur_course_term is_current_term = true end - if cur_course_term == "秋季学期" && course.time == (Time.now.year + 1) && course.term == "春季学期" + if cur_course_term == "秋季学期" && course.time == (year_now + 1) && course.term == "春季学期" is_next_term = true - elsif cur_course_term == "春季学期" && course.time == Time.now.year && course.term == "秋季学期" + elsif cur_course_term == "春季学期" && course.time == year_now && course.term == "秋季学期" is_next_term = true end is_current_term || is_next_term @@ -575,30 +555,120 @@ module CoursesHelper #获取课程动态 def get_course_activity courses, activities @course_ids=activities.keys() - days = Setting.activity_days_default.to_i - date_to ||= Date.today + 1 - date_from = date_to - days-1.years + #原来课程动态计算当期时间前(一年+一月)的动态 + # date_to ||= Date.today + 1 + # #date_from = date_to - days-1.years + date_from = @course.created_at.to_date-days #file_count Attachment.where(container_id: @course_ids, container_type: Course).where("created_on>?", date_from).each do |attachment| + if attachment.is_public? || User.current.member_of_course?(@course) || User.current.admin? activities[attachment.container_id]+=1 + else + activities[attachment.container_id] + end end #message_count Board.where(course_id: @course_ids).each do |board| -# activities[board.course_id]+=1 - activities[board.course_id]+=board.messages.where("updated_on>?", date_from).count + countmessage = 0 + # 课程人员退出课程后,之前在讨论区回帖不计入课程动态统计 + board.messages.where("updated_on>?", date_from).each do |message| + if message.author.member_of_course?(@course) + countmessage+=1 + end + end + activities[board.course_id]+=countmessage end #news News.where(course_id: @course_ids).where("created_on>?",date_from).each do |news| + if news.author.member_of_course?(@course) activities[news.course_id]+=1 + end end + #homework_count + # HomeworkForCourse.where(course_id: @course_ids).each do |homework| + # countbid=0 + # # @bid_ids<?",date_from).each do |bid| + # countbid+=1 + # end + # activities[homework.course_id]+=countbid + # end + + #poll_count + # 动态目前只统计发布的问卷,关闭的问卷不在动态内显示 + # Poll.where(polls_group_id: @course_ids, polls_type: Course, polls_status: 2||3).where("published_at>?",date_from).each do |poll| + Poll.where(polls_group_id: @course_ids, polls_type: Course, polls_status: 2||3).where("published_at>?",date_from).each do |poll| + activities[poll.polls_group_id]+=1 + end + #end + + # 动态数 + 1 ( 某某创建了该课程 ) activities.each_pair { |key, value| activities[key] = value + 1 } return activities end + #获取某个课程的动态数 + def course_activity_count course + course_activity_count=Hash.new + course_activity_count[course.id]=0 + count = get_course_activity([course],course_activity_count)[course.id] + count.nil? ? 0 : count + end + + #重启、关闭课程按钮 + def set_course_time course + id = "finish_course_#{course.id}" + linkPath = course_endTime_timeout?(course) ? restartcourse_course_path(course) : finishcourse_course_path(course, format: :js) + desc = course_endTime_timeout?(course) ? l(:label_course_reload) : l(:label_course_closed) + link_to "#{desc}".html_safe, linkPath, :remote => true, :method => :post, :id => id, :confirm => l(:label_course_closed_tips, :desc => desc), :class => "pr_join_a" + end + + #加入课程、退出课程按钮 + def join_in_course_header(course, user, options=[]) + if user.logged? + joined = course.members.map{|member| member.user_id}.include? user.id + text = joined ? ("".html_safe + l(:label_course_exit_student)) : ("".html_safe + l(:label_course_join_student)) + url = joined ? join_path(:object_id => course.id) : try_join_path(:object_id => course.id) + method = joined ? 'delete' : 'post' + if joined + link = "#{l(:label_course_join_student)}" + link_to(text, url, :remote => true, :method => method, :class => "pr_join_a", :id => "#{course.id}", :confirm => l(:text_are_you_sure_out)) + else + link = link_to(text, url, :remote => true, :method => method, :id => "#{course.id}", :class => "pr_join_a") + "#{l(:label_course_exit_student)}".html_safe + end + else + link = "#{l(:label_course_join_student)}" + + "#{l(:label_course_exit_student)}" + end + link.html_safe + end + + def visable_attachemnts_incourse course + return[] unless course + result = [] + course.attachments.each do |attachment| + if attachment.is_public? || User.current.member_of_course?(course) || User.current.admin? + result << attachment + end + end + result + end + + def zh_course_role role + if role == "TeachingAsistant" + result = l(:label_TA) + elsif role == "Teacher" + result = l(:label_teacher) + elsif role == "Student" + result = l(:label_student) + elsif role == "Manager" + result = l(:field_admin) + end + result + end end diff --git a/app/helpers/discuss_demos_helper.rb b/app/helpers/discuss_demos_helper.rb new file mode 100644 index 000000000..71bf8fb62 --- /dev/null +++ b/app/helpers/discuss_demos_helper.rb @@ -0,0 +1,2 @@ +module DiscussDemosHelper +end diff --git a/app/helpers/files_helper.rb b/app/helpers/files_helper.rb index aace20e6c..9a1765ddc 100644 --- a/app/helpers/files_helper.rb +++ b/app/helpers/files_helper.rb @@ -1,154 +1,150 @@ -# encoding: utf-8 -module FilesHelper - include AttachmentsHelper - - def downloadAll containers - paths = [] - files = [] - tmpfile = "tmp.zip" - - containers.each do |container| - next if container.attachments.empty? - if container.is_a?(Version);end - container.attachments.each do |attachment| - paths << attachment.diskfile - file = attachment.diskfile - # logger.error "[FilesHelper] downloadAll: #{e}" - begin - File.new(file, "r") - rescue Exception => e - logger.error e - next - end - files << file - # zip.add(file.path.dup.sub(directory, ''), file.path) - end - end - - zipfile_name = "archive.zip" - if File.exists? File.open(zipfile_name, "w+") - ff = File.open(zipfile_name, "w+") - ff.close - File.delete ff - end - Zip::ZipFile.open(zipfile_name, Zip::ZipFile::CREATE) do |zipfile| - files.each do |filename| - directory = File.dirname filename - # Two arguments: - # - The name of the file as it will appear in the archive - # - The original file, including the path to find it - dir = filename.sub(directory+"/", '') - zipfile.add(dir, filename) - - end - end - File.new(zipfile_name,'w+') - end - - def courses_check_box_tags(name,courses,current_course,attachment) - s = '' - courses.each do |course| - if !(attachment.container_type && attachment.container_id == course.id) && is_course_teacher(User.current,course) && course_in_current_or_next_term(course) - s << " [#{get_course_term course}]
" - end - end - s.html_safe - end - - #判断用户是否拥有不包含当前资源的课程,需用户在该课程中角色为教师且该课程属于当前学期或下一学期 - def has_course? user,file - result = false - user.courses.each do |course| - if !course_contains_attachment?(course,file) && is_course_teacher(User.current,course) && course_in_current_or_next_term(course) - return true - end - end - result - end - - # 判断指定的资源时候符合类型 - def isTypeOk(attachment, type, contentType) - result = false - if type != 0 - if attachment.attachtype == type - result = true - end - else - result = true - end - if result - if contentType != '0' && contentType != attachment.suffix_type - result = false - end - end - result - end - - def visable_attachemnts attachments - result = [] - attachments.each do |attachment| - if attachment.is_public? || (attachment.container_type == "Course" && User.current.member_of_course?(Course.find(attachment.container_id)))|| attachment.author_id == User.current.id - result << attachment - end - end - result - end - - def visable_attachemnts_incourse attachments - result = [] - attachments.each do |attachment| - if attachment.is_public? || (attachment.author.member_of_course?(Course.find(attachment.container_id)))|| attachment.author_id == User.current.id - result << attachment - end - end - result - end - - def visable_attachemnts_insite attachments,course - result = [] - attachments.each do |attachment| - if attachment.is_public? || (attachment.container_type == "Course" && attachment.container_id == course.id && User.current.member_of_course?(Course.find(attachment.container_id)))|| attachment.author_id == User.current.id - result << attachment - end - end - result - end - - def attachment_candown attachment - candown = false - if attachment.container - if attachment.container.class.to_s != "HomeworkAttach" && (attachment.container.has_attribute?(:project) || attachment.container.has_attribute?(:project_id)) && attachment.container.project - project = attachment.container.project - candown= User.current.member_of?(project) || (project.is_public && attachment.is_public == 1) - elsif attachment.container.is_a?(Project) - project = attachment.container - candown= User.current.member_of?(project) || (project.is_public && attachment.is_public == 1) - elsif (attachment.container.has_attribute?(:board) || attachment.container.has_attribute?(:board_id)) && attachment.container.board && - attachment.container.board.project - project = attachment.container.board.project - candown = User.current.member_of?(project) || (project.is_public && attachment.is_public == 1) - elsif (attachment.container.has_attribute?(:course) ||attachment.container.has_attribute?(:course_id) ) && attachment.container.course - course = attachment.container.course - candown = User.current.member_of_course?(course) || (course.is_public==1 && attachment.is_public == 1) - elsif attachment.container.is_a?(Course) - course = attachment.container - candown= User.current.member_of_course?(course) || (course.is_public==1 && attachment.is_public == 1) - elsif (attachment.container.has_attribute?(:board) || attachment.container.has_attribute?(:board_id)) && attachment.container.board && - attachment.container.board.course - course = attachment.container.board.course - candown= User.current.member_of_course?(course) || (course.is_public==1 && attachment.is_public == 1) - elsif attachment.container.class.to_s=="HomeworkAttach" && attachment.container.bid.reward_type == 3 - candown = true - elsif attachment.container_type == "Bid" && attachment.container && attachment.container.courses - course = attachment.container.courses.first - candown = User.current.member_of_course?(attachment.container.courses.first) || (course.is_public == 1 && attachment.is_public == 1) - else - candown = (attachment.is_public == 1 || attachment.is_public == true) - end - end - candown - end - - - +# encoding: utf-8 +module FilesHelper + include AttachmentsHelper + + def downloadAll containers + paths = [] + files = [] + tmpfile = "tmp.zip" + + containers.each do |container| + next if container.attachments.empty? + if container.is_a?(Version);end + container.attachments.each do |attachment| + paths << attachment.diskfile + file = attachment.diskfile + # logger.error "[FilesHelper] downloadAll: #{e}" + begin + File.new(file, "r") + rescue Exception => e + logger.error e + next + end + files << file + # zip.add(file.path.dup.sub(directory, ''), file.path) + end + end + + zipfile_name = "archive.zip" + if File.exists? File.open(zipfile_name, "w+") + ff = File.open(zipfile_name, "w+") + ff.close + File.delete ff + end + Zip::ZipFile.open(zipfile_name, Zip::ZipFile::CREATE) do |zipfile| + files.each do |filename| + directory = File.dirname filename + # Two arguments: + # - The name of the file as it will appear in the archive + # - The original file, including the path to find it + dir = filename.sub(directory+"/", '') + zipfile.add(dir, filename) + + end + end + File.new(zipfile_name,'w+') + end + + #带勾选框的课程列表 + def courses_check_box_tags(name,courses,current_course,attachment) + s = '' + courses.each do |course| + if !course_contains_attachment?(course,attachment) && is_course_teacher(User.current,course) && course_in_current_or_next_term(course) + s << " [#{get_course_term course}]
" + end + end + s.html_safe + end + + #带勾选框的项目列表 + def projects_check_box_tags(name,projects,current_project,attachment) + s = '' + projects.each do |project| + if !project_contains_attachment?(project,attachment) && User.current.allowed_to?(:manage_files, project) + s << "
" + end + end + s.html_safe + end + + #判断用户是否拥有不包含当前资源的课程,需用户在该课程中角色为教师且该课程属于当前学期或下一学期 + def has_course? user,file + result = false + user.courses.each do |course| + if !course_contains_attachment?(course,file) && is_course_teacher(User.current,course) && course_in_current_or_next_term(course) + return true + end + end + result + end + + #判断用户是否拥有不包含当前资源的项目,需用户在该项目中有资源管理相关资源 + def has_project? user,file + result = false + user.projects.each do |project| + if !project_contains_attachment?(project,file) && User.current.allowed_to?(:manage_files, project) + return true + end + end + result + end + + # 判断指定的资源时候符合类型 + def isTypeOk(attachment, type, contentType) + result = false + if type != 0 + if attachment.attachtype == type + result = true + end + else + result = true + end + if result + if contentType != '0' && contentType != attachment.suffix_type + result = false + end + end + result + end + + def visable_attachemnts attachments + result = [] + attachments.each do |attachment| + if attachment.is_public? || + (attachment.container_type == "Project" && User.current.member_of?(attachment.project)) || + (attachment.container_type == "Course" && User.current.member_of_course?(Course.find(attachment.container_id)))|| + attachment.author_id == User.current.id + result << attachment + end + end + result + end + + def get_attachments_by_tag attachments,tag + attachments.each do |attachment| + attachment.tag_list.include?(tag) + end + end + + def visable_attachemnts_insite attachments,obj + result = [] + if obj.is_a?(Course) + attachments.each do |attachment| + if attachment.is_public? || (attachment.container_type == "Course" && attachment.container_id == obj.id && User.current.member_of_course?(Course.find(attachment.container_id)))|| attachment.author_id == User.current.id + result << attachment + end + end + else if obj.is_a?(Project) + attachments.each do |attachment| + if attachment.is_public? || (attachment.container_type == "Project" && attachment.container_id == obj.id && User.current.member_of_course?(Project.find(attachment.container_id)))|| attachment.author_id == User.current.id + result << attachment + end + end + end + end + result + end + + + end \ No newline at end of file diff --git a/app/helpers/gitlab_helper.rb b/app/helpers/gitlab_helper.rb index ceaaf6d21..836a973a9 100644 --- a/app/helpers/gitlab_helper.rb +++ b/app/helpers/gitlab_helper.rb @@ -11,7 +11,7 @@ module GitlabHelper PROJECT_PATH_CUT = 40 # gitlab版本库所在服务器 # 注意REPO_IP_ADDRESS必须以http://开头,暂时只支持HTTP协议,未支持SSH - #REPO_IP_ADDRESS = "http://" + Setting.repository_domain + #REPO_IP_ADDRESS = "http://" + Setting.host_repository REPO_IP_ADDRESS = "http://192.168.137.100" GITLAB_API = "/api/v3" diff --git a/app/helpers/homework_attach_helper.rb b/app/helpers/homework_attach_helper.rb index 4744df624..72d381b28 100644 --- a/app/helpers/homework_attach_helper.rb +++ b/app/helpers/homework_attach_helper.rb @@ -119,4 +119,31 @@ module HomeworkAttachHelper #end ary end + + def get_student_batch_homework_list bid,user + student_batch_homework_list = HomeworkAttach.eager_load(:attachments,:user,:rate_averages).find_by_sql("SELECT homework_attaches.*, + (SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND is_teacher_score = 1 AND stars IS NOT NULL ORDER BY updated_at DESC limit 0,1) AS t_score, + (SELECT AVG(stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND is_teacher_score = 0) AS s_score, + (SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id = #{User.current.id} AND is_teacher_score = 0) AS m_score + FROM homework_attaches + INNER JOIN homework_evaluations ON homework_evaluations.homework_attach_id = homework_attaches.id + WHERE homework_attaches.bid_id = #{bid.id} AND homework_evaluations.user_id = #{user.id} ORDER BY m_score DESC") + student_batch_homework_list + end + + ######################################################### + #sw + #获取学生未进行匿评的数量 + #param: bid => 作业 user => 用户 + #return 指定用户未进行匿评的作业的数量 + #user必须是学生用户 + ####################################################### + def get_student_not_batch_homework_list bid,user + HomeworkAttach.find_by_sql("SELECT * FROM(SELECT homework_attaches.*, + (SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id = #{user.id} AND is_teacher_score = 0) AS m_score + FROM homework_attaches + INNER JOIN homework_evaluations ON homework_evaluations.homework_attach_id = homework_attaches.id + WHERE homework_attaches.bid_id = #{bid.id} AND homework_evaluations.user_id = #{user.id}) AS table1 + WHERE table1.m_score IS NULL").count + end end \ No newline at end of file diff --git a/app/helpers/homework_common_helper.rb b/app/helpers/homework_common_helper.rb new file mode 100644 index 000000000..b9940f4be --- /dev/null +++ b/app/helpers/homework_common_helper.rb @@ -0,0 +1,53 @@ +# encoding: utf-8 +module HomeworkCommonHelper + #迟交扣分下拉框 + def late_penalty_option + type = [] + for i in (0..5) + option = [] + option << i + option << i + type << option + end + type + end + + #教辅评分比例下拉框 + def ta_proportion_option + type = [] + i = 10 + while i <= 100 + option = [] + option << i.to_s + "%" + option << i.to_f / 100 + type << option + i += 10 + end + type + end + + #缺评扣分 + def absence_penalty_option + type = [] + i = 0 + while i <= 5 + option = [] + option << i + option << i + type << option + i += 1 + end + type + end + + #根据传入作业确定跳转到开启匿评还是关闭匿评功能 + def alert_anonyoms_path homework,homework_detail_manual + link = "" + if homework_detail_manual.comment_status == 1 + link = start_anonymous_comment_homework_common_url homework.id + elsif homework_detail_manual.comment_status == 2 + link = stop_anonymous_comment_homework_common_url homework.id + end + link + end +end \ No newline at end of file diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index 8390fc61c..aae150728 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -61,12 +61,42 @@ module IssuesHelper #h("#{issue.tracker} ##{issue.id}") # h("#{issue.tracker} #{issue.source_from}") s = '' - s << link_to(@issue.project.name, project_issues_path(@issue.project)) - s << " > #" - s << @issue.project_index + s << link_to(@issue.project.name, project_issues_path(@issue.project), :class => "pro_page_top") + s << " > " + s << link_to("#" + @issue.project_index, project_issues_path(@issue.project), :class => "pro_page_top") s.html_safe end + #获取跟踪类型及样式 + #REDO:时间紧可以优化. + def get_issue_type(value) + issuetype = [] + if value == "缺陷" || value == 1 + issuetype << "red_btn_cir ml10" + issuetype << "缺陷" + elsif value == "功能" || value == 2 + issuetype << "blue_btn_cir ml10" + issuetype << "功能" + elsif value == "支持" || value == 3 + issuetype << "green_btn_cir ml10" + issuetype << "支持" + elsif value == "任务" || value == 4 + issuetype << "orange_btn_cir ml10" + issuetype << "任务" + else + issuetype << "bgreen_btn_cir ml10" + issuetype << "周报" + end + end + + def principals_options_for_isuue_list(project) + if User.current.member_of?(project) + project.members.order("lower(users.login)").map{|c| [c.name, c.user_id]}.unshift(["<< #{l(:label_me)} >>", User.current.id]).unshift(["指派给", 0]) + else + project.members.order("lower(users.login)").map{|c| [c.name, c.user_id]}.unshift(["指派给", 0]) + end + end + def render_issue_subject_with_tree(issue) s = '' ancestors = issue.root? ? [] : issue.ancestors.visible.all @@ -224,6 +254,7 @@ module IssuesHelper # as an array of strings def details_to_strings(details, no_html=false, options={}) options[:only_path] = (options[:only_path] == false ? false : true) + options[:token] = options[:token] if options[:token] strings = [] values_by_field = {} details.each do |detail| @@ -312,14 +343,19 @@ module IssuesHelper old_value = content_tag("del", old_value) if detail.old_value and detail.value.blank? if detail.property == 'attachment' && !value.blank? && atta = Attachment.find_by_id(detail.prop_key) # Link to the attachment if it has not been removed - value = link_to_attachment(atta, :download => true, :only_path => options[:only_path]) - if options[:only_path] != false && atta.is_text? - value += link_to( - image_tag('magnifier.png'), - :controller => 'attachments', :action => 'show', - :id => atta, :filename => atta.filename - ) + if options[:token].nil? + value = atta.filename + else + value = atta.filename end + # 放大镜搜索功能 + # if options[:only_path] != false && atta.is_text? + # value += link_to( + # image_tag('magnifier.png'), + # :controller => 'attachments', :action => 'show', + # :id => atta, :filename => atta.filename + # ) + # end else value = content_tag("i", h(value)) if value end diff --git a/app/helpers/journals_helper.rb b/app/helpers/journals_helper.rb index cfebb4d30..0eee08b6b 100644 --- a/app/helpers/journals_helper.rb +++ b/app/helpers/journals_helper.rb @@ -46,6 +46,26 @@ module JournalsHelper content.html_safe end + def render_links_easy(issue, journal, options={}) + content = '' + editable = User.current.logged? && (User.current.allowed_to?(:edit_issue_notes, issue.project) || (journal.user == User.current && User.current.allowed_to?(:edit_own_issue_notes, issue.project))) + destroyable = User.current.logged? && ((journal.user == User.current) || (issue.author_id == User.current.id) || (User.current.admin == 1)) + links = [] + if !journal.notes.blank? + links << link_to(l(:button_quote), + {:controller => 'journals', :action => 'new', :id => issue.id, :journal_id => journal}, + :remote => true, + :method => 'post', + :title => l(:button_quote)) if options[:reply_links] + if destroyable + links << link_to(l(:button_delete), { :controller => 'journals', :action => 'destroy', :id => journal, :format => 'js' }, + :title => l(:button_delete)) + end + end + content << content_tag('div', links.join(' ').html_safe, :class => 'contextual') unless links.empty? + content.html_safe + end + def render_notes (issue, journal, options={}) content = '' editable = User.current.logged? && (User.current.allowed_to?(:edit_issue_notes, issue.project) || (journal.user == User.current && User.current.allowed_to?(:edit_own_issue_notes, issue.project))) @@ -73,6 +93,35 @@ module JournalsHelper content_tag('div', content.html_safe, :id => "journal-#{journal.id}-notes", :class => css_classes ,:style => "width:580px") end + # 缺陷回复内容、引用内容 + # Redo:后面需要统一扩展 + def render_notes_issue (issue, journal, options={}) + content = '' + editable = User.current.logged? && (User.current.allowed_to?(:edit_issue_notes, issue.project) || (journal.user == User.current && User.current.allowed_to?(:edit_own_issue_notes, issue.project))) + destroyable = User.current.logged? && ((journal.user == User.current) || (issue.author_id == User.current.id) || (User.current.admin == 1)) + links = [] + if !journal.notes.blank? + links << link_to(l(:button_quote), + {:controller => 'journals', :action => 'new', :id => issue.id, :journal_id => journal}, + :remote => true, + :method => 'post', + :title => l(:button_quote)) if options[:reply_links] + links << link_to_in_place_notes_editor(l(:button_edit), "journal-#{journal.id}-notes", + { :controller => 'journals', :action => 'edit', :id => journal, :format => 'js' }, + :title => l(:button_edit)) if editable + #Added by young + if destroyable + links << link_to(l(:button_delete), { :controller => 'journals', :action => 'destroy', :id => journal, :format => 'js' }, + :title => l(:button_delete)) + end + end + #content << content_tag('div', links.join(' ').html_safe, :class => 'contextual', :style => 'margin-top:-25px;') unless links.empty? + content << textilizable(journal, :notes) + css_classes = "wiki" + css_classes << " editable" if editable + content_tag('div', content.html_safe, :id => "journal-#{journal.id}-notes", :class => css_classes ,:style => "width:510px") + end + def link_to_in_place_notes_editor(text, field_id, url, options={}) onclick = "$.ajax({url: '#{url_for(url)}', type: 'get'}); return false;" link_to text, '#', options.merge(:onclick => onclick) diff --git a/app/helpers/members_helper.rb b/app/helpers/members_helper.rb index 6c11199fb..06b154d36 100644 --- a/app/helpers/members_helper.rb +++ b/app/helpers/members_helper.rb @@ -1,74 +1,110 @@ -# encoding: utf-8 -# -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -module MembersHelper - def render_principals_for_new_members(project) - scope = Principal.active.sorted.not_member_of(project).like(params[:q]) - principal_count = scope.count - principal_pages = Redmine::Pagination::Paginator.new principal_count, 10, params['page'] #by young - principals = scope.offset(principal_pages.offset).limit(principal_pages.per_page).all - - s = content_tag('div', principals_check_box_tags_ex('membership[user_ids][]', principals), :id => 'principals') - - links = pagination_links_full(principal_pages, principal_count, :per_page_links => false) {|text, parameters, options| - link_to text, autocomplete_project_memberships_path(project, parameters.merge(:q => params[:q], :format => 'js')), :remote => true - } - - s + content_tag('div', content_tag('ul', links), :class => 'pagination_new') - - end - - # add by nwb - # 课程可添加的成员列表 - def render_principals_for_new_course_members(course) - scope = Principal.active.sorted.not_member_of_course(course).like(params[:q]) - principal_count = scope.count - principal_pages = Redmine::Pagination::Paginator.new principal_count, 10, params['page'] - principals = scope.offset(principal_pages.offset).limit(principal_pages.per_page).all - - s = content_tag('div', principals_check_box_tags_ex('membership[user_ids][]', principals), :id => 'principals') - - links = pagination_links_full(principal_pages, principal_count, :per_page_links => false) {|text, parameters, options| - link_to text, autocomplete_course_memberships_path(course, parameters.merge(:q => params[:q], :format => 'js')), :remote => true - } - - s + content_tag('div', content_tag('ul', links), :class => 'pagination_new') - end - - - # 当前申请加入的成员名单 - def render_principals_for_applied_members(project) - scope = project.applied_projects.map(&:user) - principal_count = scope.count - principal_pages = Redmine::Pagination::Paginator.new principal_count, 10, params['page'] - offset ||= principal_pages.offset - principals = scope[offset, 10] - #principals = scope.offset(principal_pages.offset).limit(principal_pages.per_page).all - #principals = ApplicationController.new.paginateHelper scope,10 - - s = content_tag('div', principals_check_box_tags_ex('membership[user_ids][]', principals), :id => 'principals') - - links = pagination_links_full(principal_pages, principal_count, :per_page_links => false) {|text, parameters, options| - link_to text, appliedproject_project_memberships_path(project, parameters.merge(:q => params[:q], :format => 'js')), :remote => true - } - - s + content_tag('div', content_tag('ul', links), :class => 'applied_new') - end - -end +# encoding: utf-8 +# +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module MembersHelper + def render_principals_for_new_members(project) + scope = Principal.active.sorted.not_member_of(project).like(params[:q]) + principal_count = scope.count + principal_pages = Redmine::Pagination::Paginator.new principal_count, 10, params['page'] #by young + principals = scope.offset(principal_pages.offset).limit(principal_pages.per_page).all + s = content_tag('div', principals_check_box_tags_ex('membership[user_ids][]', principals), :id => 'principals') + links = pagination_links_full(principal_pages, principal_count, :per_page_links => false) {|text, parameters, options| + link_to text, autocomplete_project_memberships_path(project, parameters.merge(:q => params[:q], :format => 'js')), :remote => true + } + s + content_tag('div', content_tag('ul', links), :class => 'pagination_new') + end + + #获取项目可邀请的成员列表 + def render_project_members project + if params[:q] && params[:q].lstrip.rstrip != "" + scope = Principal.active.sorted.not_member_of(project).like(params[:q]) + else + scope = [] + end + principals = paginateHelper scope,10 + s = content_tag('ul', project_member_check_box_tags_ex('membership[user_ids][]', principals), :class => 'mb5') + links = pagination_links_full(@obj_pages, @obj_count, :per_page_links => false, :remote => false, :flag => true){|text, parameters, options| + link_to text, autocomplete_project_memberships_path(project, parameters.merge(:q => params[:q],:flag => true, :format => 'js')), :remote => true + } + s + content_tag('ul', links,:class => 'wlist', :id => "course_member_pagination_links" ) + end + + # add by nwb + # 课程可添加的成员列表 + def render_principals_for_new_course_members(course) + if params[:q] && params[:q].lstrip.rstrip != "" + scope = Principal.active.sorted.not_member_of_course(course).like(params[:q]) + else + scope = [] + end + principals = paginateHelper scope,10 + s = content_tag('ul', project_member_check_box_tags_ex('membership[user_ids][]', principals), :class => 'mb5', :id => 'principals') + + links = pagination_links_full(@obj_pages, @obj_count, :per_page_links => false, :remote => false, :flag => true) {|text, parameters, options| + link_to text, autocomplete_course_memberships_path(course, parameters.merge(:q => params[:q], :format => 'js')), :remote => true + } + + s + content_tag('ul', links,:class => 'wlist',:id => "course_member_pagination_links") + end + + # 新申请加入项目成员列表 + def render_principals_for_applied_members_new project + scope = project.applied_projects.map(&:user) + principals = paginateHelper scope,10 + s = content_tag('ul', principals_check_box_tags_li('membership[user_ids][]', principals), :class => 'mb5') + links = pagination_links_full(@obj_pages, @obj_count, :per_page_links => false, :remote => false, :flag => true){|text, parameters, options| + link_to text, appliedproject_project_memberships_path(project, parameters.merge(:q => params[:q],:flag => true, :format => 'js')), :remote => true + } + s + content_tag('ul', links,:class => 'wlist', :id => "course_member_pagination_links" ) + end + + # 当前申请加入的成员名单 + def render_principals_for_applied_members(project) + scope = project.applied_projects.map(&:user) + principal_count = scope.count + principal_pages = Redmine::Pagination::Paginator.new principal_count, 10, params['page'] + offset ||= principal_pages.offset + principals = scope[offset, 10] + #principals = scope.offset(principal_pages.offset).limit(principal_pages.per_page).all + #principals = ApplicationController.new.paginateHelper scope,10 + + s = content_tag('div', principals_check_box_tags_ex('membership[user_ids][]', principals), :id => 'principals') + + links = pagination_links_full(principal_pages, principal_count, :per_page_links => false) {|text, parameters, options| + link_to text, appliedproject_project_memberships_path(project, parameters.merge(:q => params[:q], :format => 'js')), :remote => true + } + + s + content_tag('div', content_tag('ul', links), :class => 'applied_new') + end + + private + def paginateHelper obj, pre_size=20 + @obj_count = obj.count + @obj_pages = Redmine::Pagination::Paginator.new @obj_count, pre_size, params['page'] + if obj.kind_of? ActiveRecord::Base or obj.kind_of? ActiveRecord::Relation + obj.limit(@obj_pages.per_page).offset(@obj_pages.offset) + elsif obj.kind_of? Array + obj[@obj_pages.offset, @obj_pages.per_page] + else + logger.error "[ApplicationController] Error : application_controller#paginateHelper ===> unknow category: #{obj.class}" + raise RuntimeError, 'unknow type, Please input you type into this helper.' + end + end + +end diff --git a/app/helpers/organizations_helper.rb b/app/helpers/organizations_helper.rb new file mode 100644 index 000000000..10321ba16 --- /dev/null +++ b/app/helpers/organizations_helper.rb @@ -0,0 +1,2 @@ +module EnterprisesHelper +end diff --git a/app/helpers/owner_type_helper.rb b/app/helpers/owner_type_helper.rb new file mode 100644 index 000000000..c03f2d19e --- /dev/null +++ b/app/helpers/owner_type_helper.rb @@ -0,0 +1,10 @@ +module OwnerTypeHelper + MEMO = 1 + FORUM = 2 + MESSAGE = 3 + NEWS = 4 + COMMENT = 5 + BID = 6 + JOURNALSFORMESSAGE = 7 + HOMEWORKCOMMON = 8 +end \ No newline at end of file diff --git a/app/helpers/poll_helper.rb b/app/helpers/poll_helper.rb index 17ef02a36..3156f1b3a 100644 --- a/app/helpers/poll_helper.rb +++ b/app/helpers/poll_helper.rb @@ -64,14 +64,14 @@ module PollHelper def options_show pq case pq when 1 - "单选题" + l(:label_MC) when 2 - "多选题" + l(:label_MCQ) when 3 - "单行主观题" + l(:label_single) else - "多行主观题" + l(:label_mulit) end end - + end \ No newline at end of file diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index 5df3644ad..12925b0b1 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -21,7 +21,12 @@ include AvatarHelper module ProjectsHelper def link_to_version(version, options = {}) return '' unless version && version.is_a?(Version) - link_to_if version.visible?, format_version_name(version), { :controller => 'versions', :action => 'show', :id => version }, options + link_to_if version.visible?, format_version_name(version), { :controller => 'versions', :action => 'show', :id => version }, :class => "c_blue02" + end + + def link_to_version_show(version, options = {}) + return '' unless version && version.is_a?(Version) + link_to_if version.visible?, format_version_name(version), { :controller => 'versions', :action => 'show', :id => version }, :class => " f16 fb c_dblue " end def project_settings_tabs @@ -29,18 +34,16 @@ module ProjectsHelper {:name => 'modules', :action => :select_project_modules, :partial => 'projects/settings/modules', :label => :label_module_plural}, {:name => 'members', :action => :manage_members, :partial => 'projects/settings/members', :label => :label_member_plural}, {:name => 'versions', :action => :manage_versions, :partial => 'projects/settings/versions', :label => :label_version_plural}, - {:name => 'categories', :action => :manage_categories, :partial => 'projects/settings/issue_categories', :label => :label_issue_category_plural}, + # {:name => 'categories', :action => :manage_categories, :partial => 'projects/settings/issue_categories', :label => :label_issue_category_plural}, # {:name => 'wiki', :action => :manage_wiki, :partial => 'projects/settings/wiki', :label => :label_wiki}, {:name => 'repositories', :action => :manage_repository, :partial => 'projects/settings/repositories', :label => :label_repository_plural}, #{:name => 'boards', :action => :manage_boards, :partial => 'projects/settings/boards', :label => :label_board_plural}, {:name => 'activities', :action => :manage_project_activities, :partial => 'projects/settings/activities', :label => :enumeration_activities} ] tabs.select {|tab| User.current.allowed_to?(tab[:action], @project)} - end - # added bu huang - def sort_project_enterprise(state, project_type) + def sort_project(state, project_type) content = ''.html_safe case state when 0 @@ -59,89 +62,19 @@ module ProjectsHelper content << content_tag('li', link_to(l(:label_sort_by_time), projects_path(:project_sort_type => '0', :project_type => project_type))) end content = content_tag('ul', content) - content_tag('div', content, :class => "tabs_enterprise") + content_tag('div', content, :class => "tabs") end - - def sort_course(state, project_type, school_id) - content = ''.html_safe - case state - when 0 - content << content_tag('li', link_to(l(:label_sort_by_time), course_path(:project_sort_type => '0', :project_type => project_type), :school_id => school_id, :class=>"selected"), :class=>"selected") - content << content_tag('li', link_to(l(:label_sort_by_active), course_path(:project_sort_type => '1', :project_type => project_type, :school_id => school_id))) - # content << content_tag('li', link_to(l(:label_sort_by_influence), course_path(:project_sort_type => '2', :project_type => project_type))) - content << content_tag('li', link_to(l(:label_sort_by_activity), course_path(:project_sort_type => '3', :project_type => project_type, :school_id => school_id))) - - when 1 - content << content_tag('li', link_to(l(:label_sort_by_time), course_path(:project_sort_type => '0', :project_type => project_type, :school_id => school_id))) - content << content_tag('li', link_to(l(:label_sort_by_active), course_path(:project_sort_type => '1', :project_type => project_type, :school_id => school_id), :class=>"selected"), :class=>"selected") - # content << content_tag('li', link_to(l(:label_sort_by_influence), course_path(:project_sort_type => '2', :project_type => project_type))) - content << content_tag('li', link_to(l(:label_sort_by_activity), course_path(:project_sort_type => '3', :project_type => project_type, :school_id => school_id))) - when 2 - content << content_tag('li', link_to(l(:label_sort_by_time), course_path(:project_sort_type => '0', :project_type => project_type, :school_id => school_id))) - content << content_tag('li', link_to(l(:label_sort_by_active), course_path(:project_sort_type => '1', :project_type => project_type, :school_id => school_id))) - # content << content_tag('li', link_to(l(:label_sort_by_influence), course_path(:project_sort_type => '2', :project_type => project_type), :class=>"selected"), :class=>"selected") - content << content_tag('li', link_to(l(:label_sort_by_activity), course_path(:project_sort_type => '3', :project_type => project_type, :school_id => school_id))) - - #gcm - when 3 - content << content_tag('li', link_to(l(:label_sort_by_time), course_path(:project_sort_type => '0', :project_type => project_type, :school_id => school_id))) - content << content_tag('li', link_to(l(:label_sort_by_active), course_path(:project_sort_type => '1', :project_type => project_type, :school_id => school_id))) - # content << content_tag('li', link_to(l(:label_sort_by_influence), course_path(:project_sort_type => '2', :project_type => project_type))) - content << content_tag('li', link_to(l(:label_sort_by_activity), course_path(:project_sort_type => '3', :project_type => project_type, :school_id => school_id), :class=>"selected"), :class=>"selected") + # 判断我的项目中是否有重名项目 + def judge_same_projectname(user, project_name) + result = false + my_projects = user.projects + my_projects.each do |mp| + result = true if mp.name == project_name end - #gcmend - - content = content_tag('ul', content) - content_tag('div', content, :class => "tabs") + return result end - # end - - def sort_project(state, project_type) - content = ''.html_safe - case state - when 0 - content << content_tag('li', link_to(l(:label_sort_by_active), projects_path(:project_sort_type => '1', :project_type => project_type))) - content << content_tag('li', link_to(l(:label_sort_by_influence), projects_path(:project_sort_type => '2', :project_type => project_type))) - content << content_tag('li', link_to(l(:label_sort_by_time), projects_path(:project_sort_type => '0', :project_type => project_type), :class=>"selected"), :class=>"selected") - when 1 - - content << content_tag('li', link_to(l(:label_sort_by_active), projects_path(:project_sort_type => '1', :project_type => project_type), :class=>"selected"), :class=>"selected") - content << content_tag('li', link_to(l(:label_sort_by_influence), projects_path(:project_sort_type => '2', :project_type => project_type))) - content << content_tag('li', link_to(l(:label_sort_by_time), projects_path(:project_sort_type => '0', :project_type => project_type))) - when 2 - content << content_tag('li', link_to(l(:label_sort_by_active), projects_path(:project_sort_type => '1', :project_type => project_type))) - content << content_tag('li', link_to(l(:label_sort_by_influence), projects_path(:project_sort_type => '2', :project_type => project_type), :class=>"selected"), :class=>"selected") - content << content_tag('li', link_to(l(:label_sort_by_time), projects_path(:project_sort_type => '0', :project_type => project_type))) - end - content = content_tag('ul', content) - content_tag('div', content, :class => "tabs") - end - - # def sort_course(state, project_type) - # content = ''.html_safe - # case state - # when 0 -# - # content << content_tag('li', link_to(l(:label_sort_by_active), course_path(:project_sort_type => '1', :project_type => project_type))) - # content << content_tag('li', link_to(l(:label_sort_by_influence), course_path(:project_sort_type => '2', :project_type => project_type))) - # content << content_tag('li', link_to(l(:label_sort_by_time), course_path(:project_sort_type => '0', :project_type => project_type), :class=>"selected"), :class=>"selected") - # when 1 -# - # content << content_tag('li', link_to(l(:label_sort_by_active), course_path(:project_sort_type => '1', :project_type => project_type), :class=>"selected"), :class=>"selected") - # content << content_tag('li', link_to(l(:label_sort_by_influence), course_path(:project_sort_type => '2', :project_type => project_type))) - # content << content_tag('li', link_to(l(:label_sort_by_time), course_path(:project_sort_type => '0', :project_type => project_type))) - # when 2 - # content << content_tag('li', link_to(l(:label_sort_by_active), course_path(:project_sort_type => '1', :project_type => project_type))) - # content << content_tag('li', link_to(l(:label_sort_by_influence), course_path(:project_sort_type => '2', :project_type => project_type), :class=>"selected"), :class=>"selected") - # content << content_tag('li', link_to(l(:label_sort_by_time), course_path(:project_sort_type => '0', :project_type => project_type))) - # end - # content = content_tag('ul', content) - # content_tag('div', content, :class => "tabs") - # end - - # Added by young def course_settings_tabs tabs = [{:name => 'info', :action => :edit_project, :partial => 'projects/edit', :label => :label_information_plural, :course=>'1'}, @@ -153,9 +86,6 @@ module ProjectsHelper end # Ended by young - - - def parent_project_select_tag(project) selected = project.parent # retrieve the requested parent project @@ -375,4 +305,116 @@ module ProjectsHelper end end end + + # Time 2015-01-29 11:39:02 + # Author lizanle + # Description 计算projects + def get_project_activity projects,activities + @project_ids=activities.keys() + + days = Setting.activity_days_default.to_i + date_to ||= Date.today + 1 + date_from = date_to - days-1.years + + #issue_count + Issue.where(project_id: @project_ids).where("updated_on>?",date_from).each do |issue| +# activities[issue.project_id.to_s]+=1 + activities[issue.project_id]+=issue.journals.where("created_on>?",date_from).count + end + + #repository_count + Repository.where(project_id: @project_ids).each do |repository| +# activities[repository.project_id.to_s]+=1 + activities[repository.project_id]+=repository.changesets.where("committed_on>?",date_from).count + end + + + #news_count + News.where(project_id: @project_ids).where("created_on>?",date_from).each do |news| + activities[news.project_id]+=1 + end + + #document_count + Document.where(project_id: @project_ids).where("created_on>?",date_from).each do |document| + activities[document.project_id]+=1 + end + + #file_count + Attachment.where(container_id: @project_ids, container_type: Project).where("created_on>?",date_from).each do |attachment| + activities[attachment.container_id]+=1 + end + + #message_count + Board.where(project_id: @project_ids).each do |board| +# activities[board.project_id]+=1 + activities[board.project_id]+=board.messages.where("updated_on>?",date_from).count + end + + #time_entry_count + TimeEntry.where(project_id: @project_ids).where("updated_on>?",date_from).each do |timeentry| + activities[timeentry.project_id]+=1 + end + + #feedbackc_count + JournalsForMessage.where(jour_id: @project_ids, jour_type: Project).each do |jourformess| + activities[jourformess.jour_id]+=1 + end + + #activities!=0 + i=0; + projects.each do |project| + id=project.id + if activities[id]==0 + activities[id]=1 + end + end + + return activities + end + + def handle_project projects,activities + project_activity_count_array=activities.values() + + project_array=[] + i=0; + projects.each do |project| + project_array[i]=project + i=i+1 + end + + projects=desc_sort_course_by_avtivity(project_activity_count_array,project_array) + + return projects + end + + def project_organizations_id_option + type = [] + option1 = [] + option1 << l(:label_organization_choose) + option1 << 0 + type << option1 + Organization.all.each do |org| + option = [] + option << org.name + option << org.id + type << option + end + type + end + + #显示项目配置菜单 + def show_project_memu user + if user.allowed_to?(:edit_project, @project) + result = "edit_project" + elsif user.allowed_to?(:select_project_modules, @project) + result = "select_project_modules" + elsif user.allowed_to?(:manage_members, @project) + result = "manage_members" + elsif user.allowed_to?(:manage_versions, @project) + result = "manage_versions" + elsif user.allowed_to?(:manage_repository, @project) + result = "manage_repository" + end + result + end end diff --git a/app/helpers/queries_helper.rb b/app/helpers/queries_helper.rb index f36d2ea94..43d01a5dd 100644 --- a/app/helpers/queries_helper.rb +++ b/app/helpers/queries_helper.rb @@ -243,26 +243,53 @@ module QueriesHelper # Retrieve query from session or build a new query def retrieve_query - if !params[:query_id].blank? - cond = "project_id IS NULL" - cond << " OR project_id = #{@project.id}" if @project - @query = IssueQuery.find(params[:query_id], :conditions => cond) - raise ::Unauthorized unless @query.visible? - @query.project = @project - session[:query] = {:id => @query.id, :project_id => @query.project_id} - sort_clear - elsif api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil) + # if !params[:query_id].blank? + # cond = "project_id IS NULL" + # cond << " OR project_id = #{@project.id}" if @project + # @query = IssueQuery.find(params[:query_id], :conditions => cond) + # raise ::Unauthorized unless @query.visible? + # @query.project = @project + # session[:query] = {:id => @query.id, :project_id => @query.project_id} + # sort_clear + # elsif api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil) # Give it a name, required to be valid @query = IssueQuery.new(:name => "_") @query.project = @project + params[:f] = %w(subject status_id priority_id author_id assigned_to_id created_on) unless params[:status_id].nil? + params[:op] = {'subject' => "~" , + 'status_id' => ( params[:status_id] == '0' ? "!":"=" ), + 'priority_id' => ( params[:priority_id] == '0' ? "!":"=" ), + 'author_id' => ( params[:author_id] == '0' ? "!":"=" ), + 'assigned_to_id' => ( params[:assigned_to_id] == '0' ? "!":"=" )} unless params[:status_id].nil? + params[:v] = {'subject' => [params[:subject]], + 'status_id' => [params[:status_id]], + 'priority_id' => [params[:priority_id]], + 'author_id' => [params[:author_id]], + 'assigned_to_id' => [params[:assigned_to_id]]} unless params[:status_id].nil? + if(params[:status_id] != nil) + if( params[:issue_create_date_start]!=nil && params[:issue_create_date_start]!='' && + params[:issue_create_date_end]!=nil && params[:issue_create_date_end]!='' ) + params[:op][:created_on]='><' + params[:v][:created_on]=[params[:issue_create_date_start],params[:issue_create_date_end]] + elsif(params[:issue_create_date_start]!=nil && params[:issue_create_date_start]!='') + params[:op][:created_on]='>=' + params[:v][:created_on]=[params[:issue_create_date_start]] + elsif(params[:issue_create_date_end]!=nil && params[:issue_create_date_end]!='') + params[:op][:created_on]='<=' + params[:v][:created_on]=[params[:issue_create_date_end]] + else + params[:op][:created_on]='!' + params[:v][:created_on]='' + end + end @query.build_from_params(params) - session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names} - else - # retrieve from session - @query = IssueQuery.find_by_id(session[:query][:id]) if session[:query][:id] - @query ||= IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names]) - @query.project = @project - end + #session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names} + # else + # # retrieve from session + # @query = IssueQuery.find_by_id(session[:query][:id]) if session[:query][:id] + # @query ||= IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names]) + # @query.project = @project + # end end def retrieve_query_from_session diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index 3c7123435..5cbc3af5a 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -18,9 +18,13 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. module RepositoriesHelper - ROOT_PATH="/home/pdl/redmine-2.3.2-0/apache2/" + if Rails.env.development? + ROOT_PATH="/tmp/" if Rails.env.development? + else + ROOT_PATH="/home/pdl/redmine-2.3.2-0/apache2/" + end PROJECT_PATH_CUT = 40 - REPO_IP_ADDRESS = Setting.repository_domain + REPO_IP_ADDRESS = Setting.host_repository def format_revision(revision) if revision.respond_to? :format_identifier @@ -228,6 +232,18 @@ module RepositoriesHelper :label => l(:label_git_report_last_commit) )) end + + # 判断项目是否有主版本库 + def judge_main_repository(pro) + if pro.repositories.blank? + return false + else + pro.repositories.sort.each do |rep| + rep.is_default? + return true + end + end + end # def cvs_field_tags(form, repository) # content_tag('p', form.text_field( # :root_url, diff --git a/app/helpers/student_work_helper.rb b/app/helpers/student_work_helper.rb new file mode 100644 index 000000000..f88b1ff21 --- /dev/null +++ b/app/helpers/student_work_helper.rb @@ -0,0 +1,83 @@ +# encoding: utf-8 +module StudentWorkHelper + def user_projects_option + cond = Project.visible_condition(User.current) + " AND projects.project_type <> 1" + memberships = User.current.memberships.all(:conditions => cond) + projects = memberships.map(&:project) + not_have_project = [] + not_have_project << Setting.please_chose + not_have_project << 0 + type = [] + type << not_have_project + projects.each do |project| + if project != nil + option = [] + option << project.name + option << project.id + type << option + end + end + type + end + + #获取指定用户对某一作业的评分结果 + def student_work_score work,user + StudentWorksScore.where(:user_id => user.id,:student_work_id => work.id).first + end + + #获取指定评分的角色 + def student_work_score_role score + case score.reviewer_role + when 1 + role = "教师" + when 2 + role = "助教" + when 3 + role = "学生" + end + end + + def get_role_by_name role + case role + when "Teacher" + result = 1 + when "Manager" + result = 1 + when "TeachingAsistant" + result = 2 + when "Student" + result = 3 + end + result + end + + #获取赞的总数 + def praise_homework_count obj_id + PraiseTread.where("praise_tread_object_id = #{obj_id} AND praise_tread_object_type = 'StudentWork'").count + end + + #判断指定用户是不是已经赞过该作业 + def is_praise_homework user_id, obj_id + PraiseTread.where("user_id = #{user_id} AND praise_tread_object_id = #{obj_id} AND praise_tread_object_type = 'StudentWork'").empty? + end + + #获取指定学生在指定作业内应匿评的数量 + def all_evaluation_count user,homework + StudentWorksEvaluationDistribution.joins(:student_work).where("student_works_evaluation_distributions.user_id = #{user.id} AND student_works.homework_common_id = #{homework.id}").count + end + + #获取指定学生在指定作业内已匿评的数量 + def has_evaluation_count user,homework + StudentWorksScore.joins(:student_work).where("student_works_scores.user_id = #{user.id} AND student_works.homework_common_id = #{homework.id}").count + end + + #传入分数,获取对应颜色 + def score_color score + if score + color = score >= 90 ? "c_red" : "c_green" + else + color = "c_grey" + end + color + end +end \ No newline at end of file diff --git a/app/helpers/system_log_helper.rb b/app/helpers/system_log_helper.rb new file mode 100644 index 000000000..2ca5baaed --- /dev/null +++ b/app/helpers/system_log_helper.rb @@ -0,0 +1,186 @@ +# Time 2015-01-26 17:30:16 +# Author lizanle +# Description 日志帮助类 +module SystemLogHelper + class SystemLog + class << self + # Time 2015-01-26 17:29:17 + # Author lizanle + # Description 分页(支持多关键字查询) + def logo_data(page, per, search, day) + logs = find_all_logs day + if logs.empty? #如果返回的是空數組,就說明日誌文件不存在 + return logs + end + # 根据search参数来决定是否需要查询 + keywords = search + if keywords && !keywords.strip.blank? + # 把keywords转化成正则表达式数组 + keywords = keywords.strip.split(/\s+/).collect! { |w| Regexp.new(w, 'i') } + # 一条记录应该匹配每个关键字 log =~ r 是对log记录进行判断是否符合r的正则表达式 + logs = logs.find_all do |log| + keywords.all? { |r| log =~ r && !(log =~ Regexp.new("SystemLogController#",'i')) } + end + #用Kaminari分页 + logs = Kaminari.paginate_array(logs).page(page).per(per).collect! { |log| parse(log) } + #将分页后的记录的搜索结果添加样式,样式中的\0是给给r占位置的。 + logs.collect! do |log| + keywords.each { |r| log.gsub!(r, '\0') } + log + end + + else + logs = Kaminari.paginate_array(logs).page(page).per(per).collect! { |log| parse(log) } + # 过滤掉日志中日志分析请求 + logs.collect! do |log| + log = "" if log =~ Regexp.new("SystemLogController",'i') + log + end + end + logs + end + + # Time 2015-01-26 17:28:57 + # Author lizanle + # Description 清除日誌 + def clear day + if File::exists?(logfile_path day) + File.open(logfile_path(day), 'w') do |f| + f.print '' + end + else + end + end + + # Time 2015-01-26 17:28:49 + # Author lizanle + # Description 讀取日誌 + private + def find_all_logs day + if File::exists?(logfile_path day) + File.open(logfile_path day) do |f| + #打开文件,并按照正则表达式切分,逆序,最新一个记录可以扔掉(因为最新的记录永远都是访问System_log) + f.read.split("Processing").reverse[1..-1] + end + else + [] + end + end + + # Time 2015-01-26 17:28:34 + # Author lizanle + # Description 日志文件的路径,一般在Rails.root/log下,根据环境配置 + # 依次记录到product.log development.log test.log中 + def logfile_path day + #将日期处理成2015-01-01的形式 + unless day.nil? + dayArr = day.split('-') + if dayArr[1].length == 1 + dayArr[1] = "0" + dayArr[1] + end + if dayArr[2].length == 1 + dayArr[2] = "0" + dayArr[2] + end + day = dayArr.join('-') + end + #如果不是當天,則需要加後綴 + if !day.nil? && !day.strip.blank? && day != Time.now.strftime("%Y-%m-%d") + File.join(Rails.root, "log", "#{Rails.env}.log.#{day.gsub('-', '')}") + else + File.join(Rails.root, "log", "#{Rails.env}.log") + end + end + + # Time 2015-01-26 17:28:22 + # Author lizanle + # Description 替換換行符 + def parse(log) + ERB::Util.html_escape(log.gsub(/\e\[[\d;m]+/, '')).gsub("\n", "
") + end + + # Time 2015-01-26 17:28:07 + # Author lizanle + # Description 定义响应正则表达式 2015-01-20 11:31:13 INFO -- Completed 200 OK in 125ms (Views: 81.0ms | ActiveRecord: 2.0ms) + def response_regex + 'Completed \d+ \w+ in (\d+)ms \(Views: (\d+\.\d+)?ms \| ActiveRecord: (\d+\.\d+)?ms\)' + end + + # Time 2015-01-26 17:27:51 + # Author lizanle + # Description 将一条记录中的地址主机等都分析出来 + def get_status(paragraph) + request_regex = 'Started GET \"(\/.*)\" for ([\d]+\.[\d]+\.[\d]+\.[\d]+) at [\d]*-([\d]*-[\d]* [\d]*:[\d]*:[\d]*)' + controller_regex = 'Processing by ([\w]+#[\w]+)' + page_time_regex = 'Views: \d+(\.\d+)?ms' + activeRecord_time_regex = 'ActiveRecord: \d+(\.\d+)?ms' + + #解析请求中的正则,主机,时间 + if paragraph.match(request_regex) != nil + request_url = paragraph.match(request_regex)[1] #正则表达式中的括号能够截取成数组 + request_host = paragraph.match(request_regex)[2] + request_at = paragraph.match(request_regex)[3] + end + + #解析控制器 + if paragraph.match(controller_regex) != nil + controller_name = paragraph.match(controller_regex)[1] + end + + #解析响应时间以及计算百分比 + if paragraph.match(response_regex) != nil + #print(paragraph.match(response_regex)) + total_time = paragraph.match(response_regex)[1] + page_time = paragraph.match(response_regex)[2] + activeRecord_time = paragraph.match(response_regex)[3] + page_time_percent = page_time.to_f/(total_time.to_f) + activeRecord_time_percent = activeRecord_time.to_f/(total_time.to_f) + else + end + #将解析结果当做一条记录数组返回 + request_status = [request_url, request_host, request_at, + controller_name, total_time, page_time, page_time_percent, activeRecord_time, activeRecord_time_percent] + request_status + end + + # Time 2015-01-26 16:41:51 + # Author lizanle + # Description 分析日志 + public + def analysis day + csv = Array.new + #如果文件不存在,则直接返回空数组 + if File::exists?(logfile_path day) + File.open(logfile_path(day), "r:utf-8") do |file| + paragraph = "" + begin_flag = false + # 对每一行进行判断 + file.each do |line| + # 以"Started GET "开头为一个paragraph + #print(line.match('[\d]*-([\d]*-[\d]* [\d]*:[\d]*:[\d]*) INFO -- Started GET ') == nil) + if (line.match('[\d]*-([\d]*-[\d]* [\d]*:[\d]*:[\d]*) \w+ -- Started GET ') != nil) + if !begin_flag + begin_flag = true + paragraph.concat(line) + else + # 另一个paragraph的开头 + if (paragraph.match(response_regex) != nil) + csv << get_status(paragraph) + end + begin_flag = true + paragraph = line + end + else + if begin_flag + paragraph.concat(line) + else + end + end + end + end + end + csv + end + end + end +end + diff --git a/app/helpers/tags_helper.rb b/app/helpers/tags_helper.rb index 17b68c3f6..284ee8c43 100644 --- a/app/helpers/tags_helper.rb +++ b/app/helpers/tags_helper.rb @@ -20,6 +20,8 @@ module TagsHelper @obj= Contest.find_by_id(obj_id) when '9' @obj= Course.find_by_id(obj_id) + when '10' + @obj = Attachment.find_by_id(obj_id) else raise Exception, '[TagsHelper] ===> tag type unknow.' end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 58935dd11..af2d5abc4 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -278,4 +278,29 @@ module UsersHelper end } end + + #获取用户留言相关的连接 + def user_jour_feed_back_url active + if active.act_type == "JournalsForMessage" + jour = JournalsForMessage.find active.act_id + if jour + case jour.jour_type + when "Principal" + link_to(l(:label_goto), user_newfeedback_user_path(jour.jour_id)) + when "Project" + link_to(l(:label_goto), project_feedback_path(jour.jour_id)) + when "Bid" + link_to(l(:label_goto), course_for_bid_path(jour.jour_id)) + when "Course" + link_to(l(:label_goto), course_feedback_path(jour.jour_id)) + when "Contest" + link_to(l(:label_goto), show_contest_contest_path(jour.jour_id)) + when "Softapplication" + link_to(l(:label_goto), softapplication_path(jour.jour_id)) + when "HomeworkAttach" + link_to(l(:label_goto), course_for_bid_path(jour.jour_id)) + end + end + end + end end diff --git a/app/helpers/watchers_helper.rb b/app/helpers/watchers_helper.rb index 842e1f1b7..d3b2a49b1 100644 --- a/app/helpers/watchers_helper.rb +++ b/app/helpers/watchers_helper.rb @@ -31,7 +31,7 @@ module WatchersHelper watched = objects.any? {|object| object.watched_by?(user)} @watch_flag = (objects.first.instance_of?(User) or objects.first.instance_of?(Project) or objects.first.instance_of?(Contest) or (objects.first.instance_of?(Bid))) - css = @watch_flag ? ([watcher_css(objects), watched ? 'icon ' : 'icon '].join(' ') << options[0].to_s) : + css = @watch_flag ? ([watcher_css(objects), watched ? 'icon ' : 'icon '].join(' ') << options[0].to_s) : ([watcher_css(objects), watched ? 'icon icon-fav ' : 'icon icon-fav-off '].join(' ') << options[0].to_s) text = @watch_flag ? @@ -42,9 +42,50 @@ module WatchersHelper :object_id => (objects.size == 1 ? objects.first.id : objects.map(&:id).sort) ) method = watched ? 'delete' : 'post' - + link_to text, url, :remote => true, :method => method, :class => css end + + def watcher_link_issue(objects, user, options=[]) + return '' unless user && user.logged? + objects = Array.wrap(objects) + + watched = objects.any? {|object| object.watched_by?(user)} + @watch_flag = (objects.first.instance_of?(User) or objects.first.instance_of?(Project) or objects.first.instance_of?(Contest) or (objects.first.instance_of?(Bid))) + css = @watch_flag ? ([watcher_css(objects), watched ? 'talk_edit ' : 'talk_edit '].join(' ') << options[0].to_s) : + ([watcher_css(objects), watched ? 'talk_edit fr ' : 'talk_edit fr '].join(' ') << options[0].to_s) + + text = @watch_flag ? + (watched ? l(:button_unfollow) : l(:button_follow)) : (watched ? l(:button_unwatch) : l(:button_watch)) + + url = watch_path( + :object_type => objects.first.class.to_s.underscore, + :object_id => (objects.size == 1 ? objects.first.id : objects.map(&:id).sort) + ) + method = watched ? 'delete' : 'post' + + link_to text, url, :remote => true, :method => method, :class => css + end + + def watcher_link_with_id(objects, user, options=[]) + return '' unless user && user.logged? + objects = Array.wrap(objects) + + watched = objects.any? {|object| object.watched_by?(user)} + @watch_flag = (objects.first.instance_of?(User) or objects.first.instance_of?(Project) or objects.first.instance_of?(Contest) or (objects.first.instance_of?(Bid))) + css = options[:class] + + text = @watch_flag ? + (watched ? l(:button_unfollow) : l(:button_follow)) : (watched ? l(:button_unwatch) : l(:button_watch)) + + url = watch_path( + :object_type => objects.first.class.to_s.underscore, + :object_id => (objects.size == 1 ? objects.first.id : objects.map(&:id).sort) + ) + method = watched ? 'delete' : 'post' + + link_to text, url, :remote => true, :method => method, :class => css,:id=>options[:id] + end ############## added by linchun def new_watcher_link(objects, user, options=[]) @@ -135,7 +176,7 @@ module WatchersHelper else text = l(:label_new_join_group) form_tag({:controller => "courses", :action => "join_group", :object_id => "#{group.id}"}, :remote => true, :method => 'post') do - submit_tag text, class: "group_in", style: "width: 43px;height: 21px;" + submit_tag text, class: "group_in", style: "width: 90px;height: 21px;" end end end @@ -241,17 +282,36 @@ module WatchersHelper content.present? ? content_tag('ul', content, :class => 'watchers') : content end + + + + + + +# 缺陷跟踪者列表复选框生成 def watchers_checkboxes(object, users, checked=nil) if users.nil? else + # tag = check_box_tag 'issue[watcher_user_ids][]', user.id, c, :id => nil + # content_tag 'label', "#{tag} #{h(user)}".html_safe, + # :id => "issue_watcher_user_ids_#{user.id}", + # :class => "floating" users.map do |user| c = checked.nil? ? object.watched_by?(user) : checked - tag = check_box_tag 'issue[watcher_user_ids][]', user.id, c, :id => nil - content_tag 'label', "#{tag} #{h(user)}".html_safe, - :id => "issue_watcher_user_ids_#{user.id}", - :class => "floating" + s = content_tag(:ul, + content_tag(:li, "#{check_box_tag 'issue[watcher_user_ids][]', user.id, c, :id => nil } #{h link_to user.userInfo, user_path( user.id)}".html_safe, + :id=>"issue_watcher_user_ids_#{user.id}",:style=>"float: left;width: 175px;margin: 0px 20px 10px 0px; overflow: hidden; line-height:1.6em;" ), + :class => "mb10 ml80") end.join.html_safe + + # scope = users.sort + # watchers = paginateHelper scope,10 + # s = content_tag('ul', issue_watcher_check_box_tags_ex('issue[watcher_user_ids][]', watchers), :class => 'mb10 ml80') + # links = pagination_links_full(@obj_pages, @obj_count, :per_page_links => false, :remote => false, :flag => true){|text, parameters, options| + # link_to text, watchers_autocomplete_for_user_path(@users, parameters.merge(:q => params[:q],:format => 'js',:flag => 'ture')), :remote => true + # } + # s + content_tag('ul', links,:class => 'wlist', :style =>"float:left;margin-top:0px;") end end @@ -278,11 +338,62 @@ module WatchersHelper ) method = applied ? 'delete' : 'post' - link_to text, url, :remote => true, :method => method ,:class=>css + link_to text, url, :remote => true, :method => method , :class => css end def exit_project_link(project) link_to(l(:label_exit_project),exit_cur_project_path(project.id), - :remote => true, :confirm => l(:lable_sure_exit_project) ) + :remote => true, :confirm => l(:lable_sure_exit_project), + :class => "pr_join_a_quit" ) + end + + #项目关注、取消关注 + #REDO:项目样式确定后方法需要对CSS变量进行改进 + def watcher_link_for_project(objects, user, options=[]) + return '' unless user && user.logged? + objects = Array.wrap(objects) + watched = objects.any? {|object| object.watched_by?(user)} + @watch_flag = objects.first.instance_of?(Project) + id = watcher_css(objects) + text = @watch_flag ? + (watched ? l(:button_unfollow) : l(:label_button_following)) : (watched ? l(:button_unwatch) : l(:label_button_following)) + url = watch_path(:object_type => objects.first.class.to_s.underscore, + :object_id => (objects.size == 1 ? objects.first.id : objects.map(&:id).sort)) + method = watched ? 'delete' : 'post' + link_to text, url, :remote => true, :method => method, + :class => "pr_join_a" ,:id=>id + end + + #申请加入项目 + def join_in_project_link(project, user, options=[]) + return '' unless user && user.logged? + applied = project.applied_projects.find_by_user_id(user.id) + text = applied ? l(:label_unapply_project) : l(:label_apply_project) + @applied_flag = project.instance_of?(Project) + if applied + appliedid = applied.id + end + id = applied_css(project) + url = appliedproject_path( + :id=>appliedid, + :user_id => user.id, + :project_id => project.id) + method = applied ? 'delete' : 'post' + link_to text, url, :remote => true, :method => method , :class => "pr_join_a",:id => id + end + + def paginateHelper obj, pre_size=20 + @obj_count = obj.count + @obj_pages = Redmine::Pagination::Paginator.new @obj_count, pre_size, params['page'] + if obj.kind_of? ActiveRecord::Base or obj.kind_of? ActiveRecord::Relation + obj.limit(@obj_pages.per_page).offset(@obj_pages.offset) + elsif obj.kind_of? Array + obj[@obj_pages.offset, @obj_pages.per_page] + else + logger.error "[ApplicationController] Error : application_controller#paginateHelper ===> unknow category: #{obj.class}" + raise RuntimeError, 'unknow type, Please input you type into this helper.' end end + + + end diff --git a/app/helpers/welcome_helper.rb b/app/helpers/welcome_helper.rb index 27caa8306..4800a949a 100644 --- a/app/helpers/welcome_helper.rb +++ b/app/helpers/welcome_helper.rb @@ -314,38 +314,38 @@ module WelcomeHelper str = ' '.html_safe case event.event_type when 'news' - str << content_tag("span", "发表了") << + str << content_tag("span", l('user.active.published')) << content_tag("span", find_all_event_type(event)) << ': '.html_safe << link_to(strip_tags(event.event_description).gsub(/ /,''), event.event_url, {:title => event.event_description}) when 'issue', 'message' , 'bid' , 'wiki-page' , 'document' - str << content_tag("span", "发表了") << + str << content_tag("span", l('user.active.published')) << content_tag("span", find_all_event_type(event)) << ': '.html_safe << link_to(event.event_title, event.event_url, {:title => event.event_title}) when 'reply' ,'Reply', 'Memo' - str << content_tag("span", "发表了") << - content_tag("span", find_all_event_type(event)) << + str << content_tag("span", l('user.active.published')) << + content_tag("span", find_all_event_type(event)) << ': '.html_safe << link_to(strip_tags(event.event_description).gsub(/ /,''), event.event_url, {:title => event.event_description}) when 'attachment' - str << content_tag('span', '上传了') << + str << content_tag('span', l('user.active.uploaded')) << content_tag('span', find_all_event_type(event)) << ': '.html_safe << link_to(event.event_title, event.event_url, {:title => event.event_title}) << link_to((' ['.html_safe+l(:label_downloads_list).to_s << ']'), project_files_path(event.container.project), :class => "attachments_list_color") else - str << content_tag("span", "更新了") << + str << content_tag("span", l('user.active.updated')) << content_tag("span", find_all_event_type(event)) << ': '.html_safe << link_to(event.event_title, event.event_url, {:title => event.event_title}) end str rescue Exception => e - str << content_tag("span", '未知内容') + str << content_tag("span", l('user.active.unknow')) end def show_event_reply event - str = "回复(" + str = l(:field_active_reply) case event.event_type when 'news' str << link_to( event.comments.count, news_path(event)) << ")" @@ -443,6 +443,10 @@ module WelcomeHelper resultSet.take(limit) end + def find_my_projects + my_projects = User.current.memberships.all(conditions: "projects.project_type = 0") + end + def sort_project_by_hot_rails project_type=0, order_by='score DESC', limit=15 # Project.find_by_sql(" # SELECT p.id, p.name, p.description, p.identifier, t.project_id @@ -463,25 +467,25 @@ module WelcomeHelper def find_all_event_type event case event.event_type when 'news' - '新闻' + l(:field_user_active_news) when 'issue' - '缺陷' + l(:field_user_active_issue) when 'attachment' - '附件' + l(:field_user_active_attachment) when 'message' - '主题' + l(:field_user_active_message) when 'Reply','reply' - '回复' + l(:field_user_active_reply) when 'bid' - '作业' + l(:field_user_active_bid) when 'Memo' - '主题' + l(:field_user_active_memo) when 'document' - '文件' + l(:field_user_active_document) when 'changeset' - '版本库' + l(:field_user_active_changeset) when 'issue-note' - '问题说明' + l(:field_user_active_issue_note) else event.event_type end diff --git a/app/models/activity.rb b/app/models/activity.rb index e871ae735..5ec778641 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -5,4 +5,6 @@ class Activity < ActiveRecord::Base validates :act_id, presence: true validates :act_type, presence: true validates :user_id, presence: true + + include Trustie::Cache::ClearCourseEvent end diff --git a/app/models/activity_notify.rb b/app/models/activity_notify.rb new file mode 100644 index 000000000..0fbe1f6f1 --- /dev/null +++ b/app/models/activity_notify.rb @@ -0,0 +1,3 @@ +class ActivityNotify < ActiveRecord::Base + belongs_to :activity, polymorphic: true +end \ No newline at end of file diff --git a/app/models/api_key.rb b/app/models/api_key.rb new file mode 100644 index 000000000..e71896bc0 --- /dev/null +++ b/app/models/api_key.rb @@ -0,0 +1,21 @@ +class ApiKey < ActiveRecord::Base + attr_accessible :access_token, :active, :expires_at, :user_id + before_create :generate_access_token + before_create :set_experation + + # validates_presence_of :user_id, :access_token + + def expired? + DateTime.now >= self.expires_at + end + + private + def generate_access_token + self.access_token = SecureRandom.hex + end + + def set_experation + self.expires_at = DateTime.now + 30 + end + +end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index de7912667..f999e27d6 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -25,7 +25,9 @@ class Attachment < ActiveRecord::Base belongs_to :softapplication, foreign_key: 'container_id', conditions: "attachments.container_type = 'Softapplication'" belongs_to :author, :class_name => "User", :foreign_key => "author_id" belongs_to :attachmentstype, :foreign_key => "attachtype",:primary_key => "id" - + # 被ForgeActivity虚拟关联 + has_many :forge_acts, :class_name => 'ForgeActivity',:as =>:forge_act ,:dependent => :destroy + # end include UserScoreHelper validates :filename, presence: true, length: {maximum: 254} @@ -70,8 +72,8 @@ class Attachment < ActiveRecord::Base @@thumbnails_storage_path = File.join(Rails.root, "tmp", "thumbnails") before_save :files_to_final_location - after_create :be_user_score # user_score - after_update :be_user_score + after_create :office_conver, :be_user_score,:act_as_forge_activity# user_score + after_update :office_conver, :be_user_score after_destroy :delete_from_disk,:down_user_score # add by nwb @@ -256,6 +258,17 @@ class Attachment < ActiveRecord::Base filename end + def office_conver + # 不在这里做后台转换,换为点击时做转换 + # return unless %w(Project Course).include? (self.container_type) + # saved_path = File.join(Rails.root, "files", "convered_office") + # unless Dir.exist?(saved_path) + # Dir.mkdir(saved_path) + # end + # convered_file = File.join(saved_path, self.disk_filename + ".pdf") + # OfficeConverTask.new.conver(self.diskfile, convered_file) + end + # Copies the temporary file to its final location # and computes its MD5 hash def files_to_final_location @@ -529,4 +542,14 @@ class Attachment < ActiveRecord::Base end end + # Time 2015-03-02 17:42:48 + # Author lizanle + # Description 上传该项目的文档资料也要保存一份在公共表中 + def act_as_forge_activity + if self.container_type == 'Project' + self.forge_acts << ForgeActivity.new(:user_id => self.author_id, + :project_id => self.container_id) + end + end + end diff --git a/app/models/bid.rb b/app/models/bid.rb index 88014477b..f5cb5a3b7 100644 --- a/app/models/bid.rb +++ b/app/models/bid.rb @@ -10,143 +10,158 @@ homework_type == 1 文件提交 homework_type == 2 Project提交 =end class Bid < ActiveRecord::Base - Enterprise = 1 - Contest = 2 - Homework = 3 - HomeworkFile = 1 - HomeworkProject = 2 - attr_accessible :author_id, :budget, :deadline, :name, :description, :homework_type, :password - include Redmine::SafeAttributes - - belongs_to :author, :class_name => 'User', :foreign_key => :author_id - belongs_to :course - has_many :biding_projects, :dependent => :destroy - has_many :projects, :through => :biding_projects - has_many :courses_member, :class_name => 'User', :through => :courses - has_many :journals_for_messages, :as => :jour, :dependent => :destroy - has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy - has_many :homework_for_courses, :dependent => :destroy - has_many :courses, :through => :homework_for_courses, :source => :course - has_many :homeworks, :class_name => 'HomeworkAttach', :dependent => :destroy - has_many :homework_evaluations, :through => :homeworks - has_many :join_in_contests, :dependent => :destroy - has_many :praise_tread, as: :praise_tread_object, dependent: :destroy - # has_many :fork_homework, :class_name => 'Bid', :conditions => "#{Bid.table_name}.parent_id = #{id}" - - - acts_as_attachable - - NAME_LENGTH_LIMIT = 60 - DESCRIPTION_LENGTH_LIMIT = 3000 - validates :name, length: {maximum: NAME_LENGTH_LIMIT}, presence: true - validates :description, length: {maximum: DESCRIPTION_LENGTH_LIMIT} - validates :author_id, presence: true - validates :deadline, presence: true, format: {:with => /^[\d]{4}[-][\d]{1,2}[-][\d]{1,2}$/} - validates :name, length: {maximum: NAME_LENGTH_LIMIT} - validates :budget, format: { with: ->(p) { if p.reward_type == 1 then /^(\d+)$|^(\d+).([0-9]{2})|^(\d+).([0-9]{1})$/ - elsif p.reward_type == 3 then /^(\d+)$|^(\d+).([0-9]{1})$/ end } } - - validate :validate_user - validate :validate_reward_type - after_create :act_as_activity - - scope :visible, lambda {|*args| - nil - } - - scope :like, lambda {|arg| - if arg.blank? - where(nil) - else - pattern = "%#{arg.to_s.strip.downcase}%" - where("LOWER(id) LIKE :p OR LOWER(name) LIKE :p OR LOWER(description) LIKE :p", :p => pattern) - end - } - - acts_as_watchable - acts_as_taggable - - acts_as_event :title => Proc.new {|o| "#{l(:label_requirement)} ##{o.id}: #{o.name}" }, - :description => :description, - :author => :author, - :url => Proc.new {|o| {:controller => 'bids', :action => 'show', :id => o.id}} - - acts_as_activity_provider :find_options => {:include => [:projects, :author]}, - :author_key => :author_id - - safe_attributes 'name', - 'description', - 'budget', - 'deadline', - 'homework_type', - 'reward_type', - 'password' - - + # Enterprise = 1 + # Contest = 2 + # Homework = 3 + # HomeworkFile = 1 + # HomeworkProject = 2 + # attr_accessible :author_id, :budget, :deadline, :name, :description, :homework_type, :password + # include Redmine::SafeAttributes + # include ApplicationHelper + # has_many_kindeditor_assets :assets, :dependent => :destroy + # belongs_to :author, :class_name => 'User', :foreign_key => :author_id + # belongs_to :course + # has_many :biding_projects, :dependent => :destroy + # has_many :projects, :through => :biding_projects + # has_many :courses_member, :class_name => 'User', :through => :courses + # has_many :journals_for_messages, :as => :jour, :dependent => :destroy + # has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy + # has_many :homework_for_courses, :dependent => :destroy + # has_many :courses, :through => :homework_for_courses, :source => :course + # has_many :homeworks, :class_name => 'HomeworkAttach', :dependent => :destroy + # has_many :homework_evaluations, :through => :homeworks + # has_many :join_in_contests, :dependent => :destroy + # has_many :praise_tread, as: :praise_tread_object, dependent: :destroy + # # has_many :fork_homework, :class_name => 'Bid', :conditions => "#{Bid.table_name}.parent_id = #{id}" + # acts_as_attachable + # + # NAME_LENGTH_LIMIT = 60 + # DESCRIPTION_LENGTH_LIMIT = 3000 + # validates :name, length: {maximum: NAME_LENGTH_LIMIT}, presence: true + # validates :description, length: {maximum: DESCRIPTION_LENGTH_LIMIT} + # validates :author_id, presence: true + # validates :deadline, presence: true, format: {:with => /^[\d]{4}[-][\d]{1,2}[-][\d]{1,2}$/} + # validates :name, length: {maximum: NAME_LENGTH_LIMIT} + # validates :budget, format: { with: ->(p) { if p.reward_type == 1 then /^(\d+)$|^(\d+).([0-9]{2})|^(\d+).([0-9]{1})$/ + # elsif p.reward_type == 3 then /^(\d+)$|^(\d+).([0-9]{1})$/ end } } + # + # validate :validate_user + # validate :validate_reward_type + # after_create :act_as_activity + # after_destroy :delete_kindeditor_assets + # scope :visible, lambda {|*args| + # nil + # } + # + # scope :like, lambda {|arg| + # if arg.blank? + # where(nil) + # else + # pattern = "%#{arg.to_s.strip.downcase}%" + # where("LOWER(id) LIKE :p OR LOWER(name) LIKE :p OR LOWER(description) LIKE :p", :p => pattern) + # end + # } + # + # scope :course_visible, lambda {|*args| + # includes(:courses).where(Course.allowed_to_condition(args.shift || User.current, :view_homeworks, *args)) + # } + # + # acts_as_watchable + # acts_as_taggable + # + # acts_as_event :title => Proc.new {|o| "#{l(:label_course_homework)} ##{o.id}: #{o.name}" }, + # :description => :description, + # :author => :author, + # :url => Proc.new {|o| {:controller => 'bids', :action => 'show', :id => o.id}} + # + # acts_as_activity_provider :type => 'homeworks', + # :author_key => :author_id + # + # acts_as_activity_provider :find_options => {:include => [:projects, :author]}, + # :author_key => :author_id + # # safe_attributes 'name', - # 'description', - # 'deadline' - def add_jour(user, notes, reference_user_id = 0, options = {}) - if options.count == 0 - self.journals_for_messages << JournalsForMessage.new(:user_id => user.id, :notes => notes, :reply_id => reference_user_id) - else - jfm = self.journals_for_messages.build(options) - jfm.save - jfm - end - end - - def self.creat_bids(budget, deadline, name, description=nil, reward_type) - self.create(:author_id => User.current.id, :budget => budget, - :deadline => deadline, :name => name, :description => description, :commit => 0, :reward_type => reward_type) - # self.acts << Activity.new(:user_id => self.author_id) - end - - def update_bids(budget, deadline, name, description=nil) - if(User.current.id == self.author_id) - self.name = name - self.budget = budget - self.deadline = deadline - self.description = description - self.save - end - end - - def delete_bids - unless self.nil? - if User.current.id == self.author_id - self.destroy - end - end - end - - def set_commit(commit) - self.update_attribute(:commit, commit) - end - - private - - def validate_user - errors.add :author_id, :invalid if author.nil? || !author.active? - end - - def validate_reward_type - errors.add :reward_type, :invalid if self.reward_type == 0 - end - - def act_as_activity - self.acts << Activity.new(:user_id => self.author_id) - end - - # used to validate weather the user is the creater of the bid - # added by william - def validate_bid_manager(user_id) - unless user_id.nil? - if self.author_id == user_id - return true - else - return false - end - end - end + # 'description', + # 'budget', + # 'deadline', + # 'homework_type', + # 'reward_type', + # 'password' + # + # + # # safe_attributes 'name', + # # 'description', + # # 'deadline' + # def add_jour(user, notes, reference_user_id = 0, options = {}) + # if options.count == 0 + # jfm = JournalsForMessage.new(:user_id => user.id, :notes => notes, :reply_id => reference_user_id) + # self.journals_for_messages << jfm + # jfm + # else + # jfm = self.journals_for_messages.build(options) + # jfm.save + # jfm + # end + # end + # + # def self.creat_bids(budget, deadline, name, description=nil, reward_type) + # self.create(:author_id => User.current.id, :budget => budget, + # :deadline => deadline, :name => name, :description => description, :commit => 0, :reward_type => reward_type) + # # self.acts << Activity.new(:user_id => self.author_id) + # end + # + # def update_bids(budget, deadline, name, description=nil) + # if(User.current.id == self.author_id) + # self.name = name + # self.budget = budget + # self.deadline = deadline + # self.description = description + # self.save + # end + # end + # + # def delete_bids + # unless self.nil? + # if User.current.id == self.author_id + # self.destroy + # end + # end + # end + # + # def set_commit(commit) + # self.update_attribute(:commit, commit) + # end + # + # private + # + # def validate_user + # errors.add :author_id, :invalid if author.nil? || !author.active? + # end + # + # def validate_reward_type + # errors.add :reward_type, :invalid if self.reward_type == 0 + # end + # + # def act_as_activity + # self.acts << Activity.new(:user_id => self.author_id) + # end + # + # # used to validate weather the user is the creater of the bid + # # added by william + # def validate_bid_manager(user_id) + # unless user_id.nil? + # if self.author_id == user_id + # return true + # else + # return false + # end + # end + # end + # + # # Time 2015-04-01 14:19:06 + # # Author lizanle + # # Description 删除对应课程通知的图片资源 + # def delete_kindeditor_assets + # delete_kindeditor_assets_from_disk self.id,OwnerTypeHelper::BID + # end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 539c62e85..bdb642d3c 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -17,8 +17,25 @@ class Comment < ActiveRecord::Base include Redmine::SafeAttributes + include ApplicationHelper + has_many_kindeditor_assets :assets, :dependent => :destroy belongs_to :commented, :polymorphic => true, :counter_cache => true belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' validates_presence_of :commented, :author, :comments safe_attributes 'comments' + after_create :send_mail + + def send_mail + if self.commented.is_a?(News) && Setting.notified_events.include?('news_comment_added') + Mailer.run.news_comment_added(self) + end + end + after_destroy :delete_kindeditor_assets + + # Time 2015-03-31 09:15:06 + # Author lizanle + # Description 删除对应评论的图片资源 + def delete_kindeditor_assets + delete_kindeditor_assets_from_disk self.id,OwnerTypeHelper::COMMENT + end end diff --git a/app/models/comment_observer.rb b/app/models/comment_observer.rb deleted file mode 100644 index a46e53ab9..000000000 --- a/app/models/comment_observer.rb +++ /dev/null @@ -1,27 +0,0 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class CommentObserver < ActiveRecord::Observer - def after_create(comment) - if comment.commented.is_a?(News) && Setting.notified_events.include?('news_comment_added') - ##by senluo - thread3=Thread.new do - Mailer.news_comment_added(comment).deliver - end - end - end -end diff --git a/app/models/contest.rb b/app/models/contest.rb index ad54e8fb4..650e363a8 100644 --- a/app/models/contest.rb +++ b/app/models/contest.rb @@ -15,7 +15,8 @@ class Contest < ActiveRecord::Base has_many :praise_tread, as: :praise_tread_object, dependent: :destroy has_many :contestnotifications, :dependent => :destroy, :include => :author - + + acts_as_attachable diff --git a/app/models/contest_notification.rb b/app/models/contest_notification.rb index 1613f1378..71c448f65 100644 --- a/app/models/contest_notification.rb +++ b/app/models/contest_notification.rb @@ -1,4 +1,6 @@ class ContestNotification < ActiveRecord::Base attr_accessible :content, :title validates :title, length: {maximum: 30} + + end diff --git a/app/models/course.rb b/app/models/course.rb index c11f66d49..6d71ad967 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -6,11 +6,11 @@ class Course < ActiveRecord::Base STATUS_CLOSED = 5 STATUS_ARCHIVED = 9 - attr_accessible :code, :extra, :name, :state, :tea_id, :time , :location, :state, :term, :password,:is_public,:description,:class_period, :open_student + attr_accessible :code, :extra, :name, :state, :tea_id, :time , :location, :state, :term, :password,:is_public,:description,:class_period, :open_student, :enterprise_name #belongs_to :project, :class_name => 'Course', :foreign_key => :extra, primary_key: :identifier belongs_to :teacher, :class_name => 'User', :foreign_key => :tea_id # 定义一个方法teacher,该方法通过tea_id来调用User表 belongs_to :school, :class_name => 'School', :foreign_key => :school_id #定义一个方法school,该方法通过school_id来调用School表 - has_many :bid + # has_many :bid has_many :members, :include => [:principal, :roles], :conditions => "#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{Principal::STATUS_ACTIVE}" has_many :memberships, :class_name => 'Member' has_many :member_principals, :class_name => 'Member', @@ -18,9 +18,9 @@ class Course < ActiveRecord::Base :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{Principal::STATUS_ACTIVE})" has_many :principals, :through => :member_principals, :source => :principal has_many :users, :through => :members - has_many :homeworks, :through => :homework_for_courses, :source => :bid, :dependent => :destroy + # has_many :homeworks, :through => :homework_for_courses, :source => :bid, :dependent => :destroy has_many :journals_for_messages, :as => :jour, :dependent => :destroy - has_many :homework_for_courses, :dependent => :destroy + # has_many :homework_for_courses, :dependent => :destroy has_many :student, :class_name => 'StudentsForCourse', :source => :user has_many :course_infos, :class_name => 'CourseInfos',:dependent => :destroy has_many :enabled_modules, :dependent => :delete_all @@ -29,13 +29,16 @@ class Course < ActiveRecord::Base has_many :news, :dependent => :destroy, :include => :author has_one :course_status, :class_name => "CourseStatus", :dependent => :destroy + has_many :homework_commons, :dependent => :destroy + has_many :student_works, :through => :homework_commons, :dependent => :destroy + has_many :course_groups, :dependent => :destroy acts_as_taggable acts_as_nested_set :order => 'name', :dependent => :destroy acts_as_attachable :view_permission => :view_course_files, :delete_permission => :manage_files - + validates_presence_of :password, :term,:name validates_format_of :class_period, :with =>/^[1-9]\d*$/ validates_format_of :name,:with =>/^[^ ]+[a-zA-Z0-9_\u4e00-\u9fa5\s\S]+$/ @@ -207,8 +210,8 @@ class Course < ActiveRecord::Base # 创建课程讨论区 def create_board_sync @board = self.boards.build - self.name=" #{l(:label_borad_course) }" - @board.name = self.name + #self.name=" #{l(:label_borad_course) }" + @board.name = " #{l(:label_borad_course) }"#self.name @board.description = self.name.to_s @board.project_id = -1 if @board.save diff --git a/app/models/course_group.rb b/app/models/course_group.rb index 144c43c6e..c6aa1299d 100644 --- a/app/models/course_group.rb +++ b/app/models/course_group.rb @@ -12,11 +12,14 @@ class CourseGroup < ActiveRecord::Base before_destroy :set_member_nil attr_accessible :name - validates :name, :presence => true, :length => {:maximum => 20}, - :uniqueness => {case_sensitive: false} + validates :name, :presence => true, :length => {:maximum => 20}, :uniqueness => { :scope => :course_id} + + + def set_member_nil if self.members && self.members.count > 0 self.members.update_all("course_group_id = 0") end end + end diff --git a/app/models/document.rb b/app/models/document.rb index 7c2fa5a6d..37983d6d4 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -1,79 +1,95 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class Document < ActiveRecord::Base - include Redmine::SafeAttributes - belongs_to :project - belongs_to :user - belongs_to :category, :class_name => "DocumentCategory", :foreign_key => "category_id" - include UserScoreHelper - after_save :be_user_score # user_score - after_destroy :down_user_score - - acts_as_attachable :delete_permission => :delete_documents - - acts_as_searchable :columns => ['title', "#{table_name}.description"], :include => :project - acts_as_event :title => Proc.new {|o| "#{l(:label_document)}: #{o.title}"}, - :author => Proc.new {|o| o.attachments.reorder("#{Attachment.table_name}.created_on ASC").first.try(:author) }, - :url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}} - acts_as_activity_provider :find_options => {:include => :project}, - :is_public => 'documents.is_public' - - validates_presence_of :project, :title, :category - validates_length_of :title, :maximum => 60 - - scope :visible, lambda {|*args| - includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_documents, *args)) - } - - safe_attributes 'category_id', 'title', 'description','is_public' - - def visible?(user=User.current) - !user.nil? && user.allowed_to?(:view_documents, project) - end - - def has_right?(project,user=User.current) - user.admin? || user.member_of?(project) || self.is_public==1 - end - - def initialize(attributes=nil, *args) - super - if new_record? - self.category ||= DocumentCategory.default - end - end - - def updated_on - unless @updated_on - a = attachments.last - @updated_on = (a && a.created_on) || created_on - end - @updated_on - end - - # update user score - def be_user_score - UserScore.project(:push_document, self.user,self,{ document_id: self.id }) - update_document(self.user,1) - update_document(self.user,2,self.project) - end - - def down_user_score - update_document(self.user,1) - update_document(self.user,2,self.project) - end -end +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class Document < ActiveRecord::Base + include Redmine::SafeAttributes + belongs_to :project + belongs_to :user + belongs_to :category, :class_name => "DocumentCategory", :foreign_key => "category_id" + include UserScoreHelper + after_save :be_user_score # user_score + after_destroy :down_user_score + acts_as_attachable :delete_permission => :delete_documents + after_create :send_mail + # 被ForgeActivity虚拟关联 + has_many :forge_acts, :class_name => 'ForgeActivity',:as =>:forge_act ,:dependent => :destroy + # end + acts_as_searchable :columns => ['title', "#{table_name}.description"], :include => :project + acts_as_event :title => Proc.new {|o| "#{l(:label_document)}: #{o.title}"}, + #:author => Proc.new {|o| o.attachments.reorder("#{Attachment.table_name}.created_on ASC").first.try(:author) }, + :author => Proc.new {|o| User.find(o.user_id)}, + :url => Proc.new {|o| {:controller => 'documents', :action => 'show', :id => o.id}} + acts_as_activity_provider :find_options => {:include => :project}, + :is_public => 'documents.is_public' + + validates_presence_of :project, :title, :category + validates_length_of :title, :maximum => 60 + after_create :act_as_forge_activity + scope :visible, lambda {|*args| + includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_documents, *args)) + } + + safe_attributes 'category_id', 'title', 'description','is_public' + + def visible?(user=User.current) + !user.nil? && user.allowed_to?(:view_documents, project) + end + + def has_right?(project,user=User.current) + user.admin? || user.member_of?(project) || self.is_public==1 + end + + def initialize(attributes=nil, *args) + super + if new_record? + self.category ||= DocumentCategory.default + end + end + + def updated_on + unless @updated_on + a = attachments.last + @updated_on = (a && a.created_on) || created_on + end + @updated_on + end + + # update user score + def be_user_score + UserScore.project(:push_document, self.user,self,{ document_id: self.id }) + update_document(self.user,1) + update_document(self.user,2,self.project) + end + + def down_user_score + update_document(self.user,1) + update_document(self.user,2,self.project) + end + + # Time 2015-03-02 10:51:16 + # Author lizanle + # Description 新创建的document要在公共表ForgeActivity中记录 + def act_as_forge_activity + self.forge_acts << ForgeActivity.new(:user_id => self.user_id, + :project_id => self.project_id) + end + + def send_mail + Mailer.run.document_added(self) if Setting.notified_events.include?('document_added') + end + +end diff --git a/app/models/document_observer.rb b/app/models/document_observer.rb deleted file mode 100644 index 447952c70..000000000 --- a/app/models/document_observer.rb +++ /dev/null @@ -1,25 +0,0 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class DocumentObserver < ActiveRecord::Observer - def after_create(document) - ##by senluo - thread2=Thread.new do - Mailer.document_added(document).deliver if Setting.notified_events.include?('document_added') - end - end -end diff --git a/app/models/forge_activity.rb b/app/models/forge_activity.rb new file mode 100644 index 000000000..6b75552c0 --- /dev/null +++ b/app/models/forge_activity.rb @@ -0,0 +1,23 @@ +# Time 2015-02-06 10:42:34 +# Author lizanle +# Description 这是保存Project相关的动态的公共表 +class ForgeActivity < ActiveRecord::Base + # 公共表中活动类型,命名规则:TYPE_OF_{类名}_ACT + TYPE_OF_ISSUE_ACT = "Issue" + TYPE_OF_MESSAGE_ACT = "Message" + TYPE_OF_ATTACHMENT_ACT = "Attachment" + TYPE_OF_DOCUMENT_ACT = "Document" + TYPE_OF_JOURNAL_ACT = "Journal" + TYPE_OF_WIKI_ACT = "Wiki" + TYPE_OF_NEWS_ACT = "News" + attr_accessible :forge_act_id, :forge_act_type,:project_id,:user_id,:org_id + # 虚拟关联 + belongs_to :forge_act ,:polymorphic => true + belongs_to :project + belongs_to :user + validates :user_id,presence: true + validates :project_id,presence: true + validates :forge_act_id,presence: true + validates :forge_act_type, presence: true + +end diff --git a/app/models/forum.rb b/app/models/forum.rb index 61ba528a0..2af1abf9e 100644 --- a/app/models/forum.rb +++ b/app/models/forum.rb @@ -1,44 +1,58 @@ -class Forum < ActiveRecord::Base - include Redmine::SafeAttributes - has_many :topics, :class_name => 'Memo', :conditions => "#{Memo.table_name}.parent_id IS NULL", :order => "#{Memo.table_name}.created_at DESC", :dependent => :destroy - has_many :memos, :dependent => :destroy, conditions: "parent_id IS NULL" - belongs_to :creator, :class_name => "User", :foreign_key => 'creator_id' - safe_attributes 'name', - 'description', - 'topic_count', - 'memo_count', - 'last_memo_id', - 'creator_id', - 'sticky', - 'locked' - validates_presence_of :name, :creator_id, :description - validates_length_of :name, maximum: 50 - #validates_length_of :description, maximum: 255 - validates :name, :uniqueness => true - - acts_as_taggable - scope :by_join_date, order("created_at DESC") - - def reset_counters! - self.class.reset_counters!(id) - end - - def editable_by? user - # user && user.logged? || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)) - self.creator == user || user.admin? - end - - def destroyable_by? user - # user && user.logged? && Forum.find(self.forum_id).creator_id == user.id || user.admin? - self.creator == user || user.admin? - end - - # Updates topic_count, memo_count and last_memo_id attributes for +board_id+ - def self.reset_counters!(forum_id) - forum_id = forum_id.to_i - update_all("topic_count = (SELECT COUNT(*) FROM #{Memo.table_name} WHERE forum_id=#{forum_id} AND parent_id IS NULL)," + - " memo_count = (SELECT COUNT(*) FROM #{Memo.table_name} WHERE forum_id=#{forum_id} AND parent_id IS NOT NULL)," + - " last_memo_id = (SELECT MAX(id) FROM #{Memo.table_name} WHERE forum_id=#{forum_id})", - ["id = ?", forum_id]) - end -end +class Forum < ActiveRecord::Base + include Redmine::SafeAttributes + include ApplicationHelper + has_many_kindeditor_assets :assets, :dependent => :destroy + has_many :topics, :class_name => 'Memo', :conditions => "#{Memo.table_name}.parent_id IS NULL", :order => "#{Memo.table_name}.created_at DESC", :dependent => :destroy + has_many :memos, :dependent => :destroy, conditions: "parent_id IS NULL" + belongs_to :creator, :class_name => "User", :foreign_key => 'creator_id' + safe_attributes 'name', + 'description', + 'topic_count', + 'memo_count', + 'last_memo_id', + 'creator_id', + 'sticky', + 'locked' + validates_presence_of :name, :creator_id, :description + validates_length_of :name, maximum: 50 + #validates_length_of :description, maximum: 255 + validates :name, :uniqueness => true + after_destroy :delete_kindeditor_assets + acts_as_taggable + scope :by_join_date, order("created_at DESC") + after_create :send_mail + def reset_counters! + self.class.reset_counters!(id) + end + + def editable_by? user + # user && user.logged? || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)) + self.creator == user || user.admin? + end + + def destroyable_by? user + # user && user.logged? && Forum.find(self.forum_id).creator_id == user.id || user.admin? + self.creator == user || user.admin? + end + + def send_mail + logger.debug "send mail for forum add." + Mailer.run.forum_add(self) if Setting.notified_events.include?('forum_add') + end + # Updates topic_count, memo_count and last_memo_id attributes for +board_id+ + def self.reset_counters!(forum_id) + forum_id = forum_id.to_i + update_all("topic_count = (SELECT COUNT(*) FROM #{Memo.table_name} WHERE forum_id=#{forum_id} AND parent_id IS NULL)," + + " memo_count = (SELECT COUNT(*) FROM #{Memo.table_name} WHERE forum_id=#{forum_id} AND parent_id IS NOT NULL)," + + " last_memo_id = (SELECT MAX(id) FROM #{Memo.table_name} WHERE forum_id=#{forum_id})", + ["id = ?", forum_id]) + end + + # Time 2015-03-26 15:50:54 + # Author lizanle + # Description 删除论坛后删除对应的资源 + def delete_kindeditor_assets + delete_kindeditor_assets_from_disk self.id,OwnerTypeHelper::FORUM + end + +end diff --git a/app/models/homework_common.rb b/app/models/homework_common.rb new file mode 100644 index 000000000..291c14563 --- /dev/null +++ b/app/models/homework_common.rb @@ -0,0 +1,36 @@ +#老师布置的作业表 +#homework_type: 0:普通作业;1:匿评作业;2:编程作业 +class HomeworkCommon < ActiveRecord::Base + # attr_accessible :name, :user_id, :description, :publish_time, :end_time, :homework_type, :late_penalty, :course_id + include Redmine::SafeAttributes + include ApplicationHelper + + belongs_to :course + belongs_to :user + has_one :homework_detail_manual, :dependent => :destroy + has_one :homework_detail_programing, :dependent => :destroy + has_many :homework_tests, :dependent => :destroy + has_many :student_works, :dependent => :destroy + has_many :student_works_evaluation_distributions, :through => :student_works #一个作业的分配的匿评列表 + has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy #用户活动 + acts_as_attachable + acts_as_event :title => Proc.new {|o| "#{l(:label_course_homework)} ##{o.id}: #{o.name}" }, + :description => :description, + :author => :author, + :url => Proc.new {|o| {:controller => 'student_work', :action => 'index', :homework => o.id}} + after_create :act_as_activity, :send_mail + after_destroy :delete_kindeditor_assets + + def act_as_activity + self.acts << Activity.new(:user_id => self.user_id) + end + #删除对应的图片 + def delete_kindeditor_assets + delete_kindeditor_assets_from_disk self.id,OwnerTypeHelper::HOMEWORKCOMMON + end + + def send_mail + Mailer.run.homework_added(self) + end + +end diff --git a/app/models/homework_detail_manual.rb b/app/models/homework_detail_manual.rb new file mode 100644 index 000000000..e0c4dcdcb --- /dev/null +++ b/app/models/homework_detail_manual.rb @@ -0,0 +1,7 @@ +#手动评分作业表 +#comment_status: 1:未开启匿评,2:开启匿评,3:匿评结束 +class HomeworkDetailManual < ActiveRecord::Base + attr_accessible :ta_proportion, :comment_status, :evaluation_start, :evaluation_end, :evaluation_num, :absence_penalty, :homework_common_id + + belongs_to :homework_common +end diff --git a/app/models/homework_detail_programing.rb b/app/models/homework_detail_programing.rb new file mode 100644 index 000000000..4c7134806 --- /dev/null +++ b/app/models/homework_detail_programing.rb @@ -0,0 +1,5 @@ +class HomeworkDetailPrograming < ActiveRecord::Base + attr_accessible :language, :standard_code, :homework_common_id + + belongs_to :homework_common +end diff --git a/app/models/homework_test.rb b/app/models/homework_test.rb new file mode 100644 index 000000000..f7d9b20bc --- /dev/null +++ b/app/models/homework_test.rb @@ -0,0 +1,5 @@ +class HomeworkTest < ActiveRecord::Base + attr_accessible :input, :output, :homework_common_id + + belongs_to :homework_common +end diff --git a/app/models/invite_list.rb b/app/models/invite_list.rb new file mode 100644 index 000000000..4868fc3d7 --- /dev/null +++ b/app/models/invite_list.rb @@ -0,0 +1,13 @@ +class InviteList < ActiveRecord::Base + attr_accessible :project_id, :user_id + belongs_to :user + belongs_to :project + + # 用户拒绝邀请后,删除记录 + def self.delete_inviter(userid, projectid) + @inviters = AppliedProject.where("user_id = ? and project_id = ?", userid, projectid) + @inviters.each do |inviter| + inviter.destroy + end + end +end diff --git a/app/models/issue.rb b/app/models/issue.rb index 4dc685b3b..c2670a0cc 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -19,7 +19,6 @@ class Issue < ActiveRecord::Base include Redmine::SafeAttributes include Redmine::Utils::DateCalculation include UserScoreHelper - belongs_to :project belongs_to :tracker belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id' @@ -46,6 +45,8 @@ class Issue < ActiveRecord::Base # added by fq has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy + # 被ForgeActivity虚拟关联 + has_many :forge_acts, :class_name => 'ForgeActivity',:as =>:forge_act ,:dependent => :destroy # end has_many :praise_tread, as: :praise_tread_object, dependent: :destroy @@ -64,7 +65,9 @@ class Issue < ActiveRecord::Base }, :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o}}, :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') } - + # Time 2015-01-31 13:52:53 + # Author lizanle + # Description 将hash传进去 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]}, :author_key => :author_id @@ -77,7 +80,7 @@ class Issue < ActiveRecord::Base attr_reader :current_journal # fq - after_create :act_as_activity,:be_user_score_new_issue + after_create :act_as_activity,:be_user_score_new_issue,:act_as_forge_activity after_update :be_user_score after_destroy :down_user_score # after_create :be_user_score @@ -126,7 +129,15 @@ class Issue < ActiveRecord::Base self.acts << Activity.new(:user_id => self.author_id) end # end - + + # Time 2015-02-26 10:51:16 + # Author lizanle + # Description 新创建的issue要在公共表ForgeActivity中记录 + def act_as_forge_activity + self.forge_acts << ForgeActivity.new(:user_id => self.author_id, + :project_id => self.project_id) + end + # end # Returns a SQL conditions string used to find all issues visible by the specified user @@ -1165,13 +1176,13 @@ class Issue < ActiveRecord::Base # back string obj which is belong to project. def source_from - "" << self.project.name.to_s << - "#" << project_index + "" << self.project.name.to_s end def project_index if self.project.issues.include?(self) (self.project.issues.index(self).to_i + 1).to_s + else issue_index = 1 self.project.issues.each do |issue| @@ -1182,8 +1193,10 @@ class Issue < ActiveRecord::Base issue_index = issue_index+1 end end + issue_index.to_s end + end private @@ -1553,4 +1566,5 @@ class Issue < ActiveRecord::Base end + end diff --git a/app/models/issue_observer.rb b/app/models/issue_observer.rb index 3369387ee..ea78349af 100644 --- a/app/models/issue_observer.rb +++ b/app/models/issue_observer.rb @@ -1,25 +1,28 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class IssueObserver < ActiveRecord::Observer - - def after_create(issue) - thread1=Thread.new do - Mailer.issue_add(issue).deliver if Setting.notified_events.include?('issue_added') - end - end -end +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class IssueObserver < ActiveRecord::Observer + + def after_create(issue) + # 将跟踪者与本项目的其他成员都设为收件方,并去重,不在进行抄送, + recipients = issue.recipients - issue.watcher_recipients + issue.watcher_recipients + recipients.each do |rec| + Mailer.run.issue_add(issue,rec) if Setting.notified_events.include?('issue_added') + end + + end +end diff --git a/app/models/issue_overdue.rb b/app/models/issue_overdue.rb index 5caec94c9..3002b74ef 100644 --- a/app/models/issue_overdue.rb +++ b/app/models/issue_overdue.rb @@ -20,7 +20,11 @@ class IssueOverdue < ActiveRecord::Base #发邮件 #puts "11" + issue.id.to_s #Mailer.issue_expire(issue).deliver - Mailer.issue_add(issue).deliver + recipients = issue.recipients + recipients.each do |rec| + + Mailer.issue_edit(issue,rec).deliver + end break end end diff --git a/app/models/issue_query.rb b/app/models/issue_query.rb index c55143ca8..dba45cb43 100644 --- a/app/models/issue_query.rb +++ b/app/models/issue_query.rb @@ -310,7 +310,7 @@ class IssueQuery < Query :order => options[:order], :limit => options[:limit], :offset => options[:offset] - ) + ).reverse rescue ::ActiveRecord::StatementInvalid => e raise StatementInvalid.new(e.message) end diff --git a/app/models/journal.rb b/app/models/journal.rb index a386c2b80..3b660132e 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -27,6 +27,8 @@ class Journal < ActiveRecord::Base # added by fq has_one :journal_reply has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy + # 被ForgeActivity虚拟关联 + has_many :forge_acts, :class_name => 'ForgeActivity',:as =>:forge_act ,:dependent => :destroy # end attr_accessor :indice @@ -46,7 +48,7 @@ class Journal < ActiveRecord::Base before_create :split_private_notes # fq - after_save :act_as_activity,:be_user_score + after_save :act_as_activity,:be_user_score,:act_as_forge_activity # end #after_destroy :down_user_score #before_save :be_user_score @@ -156,6 +158,15 @@ class Journal < ActiveRecord::Base end # end + # Time 2015-02-27 13:30:19 + # Author lizanle + # Description 公共表中需要保存一份该记录 + def act_as_forge_activity + self.forge_acts << ForgeActivity.new(:user_id => self.user_id, + :project_id => self.issue.project.id) + + end + # 更新用户分数 -by zjc def be_user_score #新建了缺陷留言且留言不为空,不为空白 diff --git a/app/models/journal_observer.rb b/app/models/journal_observer.rb index 0357fb74d..c5b0e496b 100644 --- a/app/models/journal_observer.rb +++ b/app/models/journal_observer.rb @@ -1,31 +1,34 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class JournalObserver < ActiveRecord::Observer - def after_create(journal) - if journal.notify? && - (Setting.notified_events.include?('issue_updated') || - (Setting.notified_events.include?('issue_note_added') && journal.notes.present?) || - (Setting.notified_events.include?('issue_status_updated') && journal.new_status.present?) || - (Setting.notified_events.include?('issue_priority_updated') && journal.new_value_for('priority_id').present?) - ) - Thread.new do - Mailer.issue_edit(journal).deliver - end - end - end -end +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class JournalObserver < ActiveRecord::Observer + def after_create(journal) + if journal.notify? && + (Setting.notified_events.include?('issue_updated') || + (Setting.notified_events.include?('issue_note_added') && journal.notes.present?) || + (Setting.notified_events.include?('issue_status_updated') && journal.new_status.present?) || + (Setting.notified_events.include?('issue_priority_updated') && journal.new_value_for('priority_id').present?) + ) + # 将跟踪者与本项目的其他成员都设为收件方,并去重,不在进行抄送, + recipients = journal.recipients - journal.watcher_recipients + journal.watcher_recipients + recipients.each do |rec| + + Mailer.run.issue_edit(journal,rec) + end + end + end +end diff --git a/app/models/journals_for_message.rb b/app/models/journals_for_message.rb index 4819cd253..b15c9b2d1 100644 --- a/app/models/journals_for_message.rb +++ b/app/models/journals_for_message.rb @@ -1,165 +1,180 @@ -# fq -# 数据库字段中带有m前缀和is_readed是二次开发添加,之前的字段基本复用 -# 注意reply_id 是提到人的id,不是留言id, Base中叫做 at_user -class JournalsForMessage < ActiveRecord::Base - include Redmine::SafeAttributes - include UserScoreHelper - safe_attributes "jour_type", # 留言所属类型 - "jour_id", # 留言所属类型的id - "notes", # 留言内容 - "reply_id", # 留言被回复留言者的用户id(用户a回复了用户b,这是b的id,用以查询谁给b留言了) - "status", # 留言是否被查看(弃用) - "user_id", # 留言者的id - "m_parent_id", # 留言信息的父留言id - "is_readed", # 留言是否已读 - "m_reply_count", # 留言的回复数量 - "m_reply_id" # 回复某留言的留言id(a留言回复了b留言,这是b留言的id) - "is_comprehensive_evaluation" # 1 教师评论、2 匿评、3 留言 - acts_as_tree :foreign_key => 'm_parent_id', :counter_cache => :m_reply_count, :order => "#{JournalsForMessage.table_name}.created_on ASC" - - belongs_to :project, - :foreign_key => 'jour_id', - :conditions => "#{self.table_name}.jour_type = 'Project' " - belongs_to :course, - :foreign_key => 'jour_id', - :conditions => "#{self.table_name}.jour_type = 'Course' " - - belongs_to :jour, :polymorphic => true - belongs_to :user - belongs_to :homework_attach - belongs_to :at_user, :class_name => "User", :foreign_key => 'reply_id' - - acts_as_event :title => Proc.new {|o| "#{l(:label_my_message)}"}, - :datetime => Proc.new {|o| o.updated_on }, - :author => Proc.new {|o| o.user }, - :description => Proc.new{|o| o.notes }, - :type => Proc.new {|o| o.jour_type }, - :url => Proc.new {|o| - if o.jour.kind_of? Project - {:controller => 'projects', :action => 'feedback', :id => o.jour, :r => o.id, :anchor => "word_li_#{o.id}"} - elsif o.jour.kind_of? Course - {:controller => 'courses', :action => 'feedback', :id => o.jour, :r => o.id, :anchor => "word_li_#{o.id}"} - end - } - acts_as_activity_provider :author_key => :user_id, - :timestamp => "#{self.table_name}.updated_on", - :find_options => {:include => :project } - - acts_as_activity_provider :type => 'course_journals_for_messages', - :author_key => :user_id, - :timestamp => "#{self.table_name}.updated_on", - :find_options => {:include => :course } - - - has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy - - validates :notes, presence: true - after_create :act_as_activity #huang - after_create :reset_counters! - after_destroy :reset_counters! - after_save :be_user_score - after_destroy :down_user_score - - # default_scope { where('m_parent_id IS NULL') } - - def self.create_by_user? user - if user.anonymous? - return false - else - return true - end - end - - - def self.remove_by_user? user - if( self.user == user || - ( self.jour.kind_of?(User) && self.jour== user ) - ) - true - else - false - end - end - - def self.delete_message(message_id) - self.find(message_id).destroy - # self.destroy_all "id = #{message_id}" - end - - def reference_user - User.find(reply_id) - end - - def delete_by_user?user - # 用户可删除自己的留言 - if self.user.id == user.id || user.admin? - return true - else - return false - end - end - - def self.reference_message(user_id) - @user = User.find(user_id) - message = JournalsForMessage.find_by_sql("select * from journals_for_messages where reply_id = #{@user.id} - or (jour_type = 'Bid' and jour_id in (select id from bids where author_id = #{@user.id}))") - message - end - - def act_as_activity - if self.jour_type == 'Principal' - unless self.user_id == self.jour.id && self.user_id != self.reply_id && self.reply_id != 0 - # self.acts << Activity.new(:user_id => self.user_id) - self.acts << Activity.new(:user_id => self.jour_id) - end - elsif self.jour_type == 'Project' - self.acts << Activity.new(:user_id => self.reply_id) - elsif self.jour_type == 'Course' - self.acts << Activity.new(:user_id => self.reply_id) - else - end - end - - def reset_counters! - self.class.reset_counters!(self) - end - def self.reset_counters! journals_for_messages - # jfm_id = journals_for_messages.id.to_i - count = find_all_by_m_parent_id(journals_for_messages.m_parent_id).count #(SELECT COUNT(*) FROM #{JournalsForMessage.table_name} WHERE m_parent_id = #{jfm_id} ) - update_all("m_reply_count = #{count.to_i}", ["id = ?", journals_for_messages.m_parent_id]) - end - - #如果是在项目中留言则返回该项目否则返回nil - zjc - def project - if self.jour_type == 'Project' - Project.find(self.jour_id) - else - nil - end - end - - # 更新用户分数 -by zjc - def be_user_score - #新建了留言回复 - if self.reply_id != 0 - #协同得分加分 - UserScore.joint(:reply_message, self.user,User.find(self.reply_id),self, { journals_for_messages_id: self.id }) - update_replay_for_message(self.user,1) - if self.jour_type == "Project" - update_replay_for_message(self.user,2,self.jour) - end - end - end - # 更新用户分数 -by zjc - def down_user_score - #删除了留言回复 - if self.reply_id != 0 - #协同得分减分 - UserScore.joint(:reply_message_delete, self.user,User.find(self.reply_id), { journals_for_messages_id: self.id }) - update_replay_for_message(self.user,1) - if self.jour_type == "Project" - update_replay_for_message(self.user,2,self.jour) - end - end - end -end +# fq +# 数据库字段中带有m前缀和is_readed是二次开发添加,之前的字段基本复用 +# 注意reply_id 是提到人的id,不是留言id, Base中叫做 at_user +class JournalsForMessage < ActiveRecord::Base + include Redmine::SafeAttributes + include UserScoreHelper + include ApplicationHelper + has_many_kindeditor_assets :assets, :dependent => :destroy + safe_attributes "jour_type", # 留言所属类型 + "jour_id", # 留言所属类型的id + "notes", # 留言内容 + "reply_id", # 留言被回复留言者的用户id(用户a回复了用户b,这是b的id,用以查询谁给b留言了) + "status", # 留言是否被查看(弃用) + "user_id", # 留言者的id + "m_parent_id", # 留言信息的父留言id + "is_readed", # 留言是否已读 + "m_reply_count", # 留言的回复数量 + "m_reply_id" # 回复某留言的留言id(a留言回复了b留言,这是b留言的id) + "is_comprehensive_evaluation" # 1 教师评论、2 匿评、3 留言 + acts_as_tree :foreign_key => 'm_parent_id', :counter_cache => :m_reply_count, :order => "#{JournalsForMessage.table_name}.created_on ASC" + after_destroy :delete_kindeditor_assets + belongs_to :project, + :foreign_key => 'jour_id', + :conditions => "#{self.table_name}.jour_type = 'Project' " + belongs_to :course, + :foreign_key => 'jour_id' + + + belongs_to :jour, :polymorphic => true + belongs_to :user + belongs_to :homework_attach + belongs_to :student_works_score + belongs_to :at_user, :class_name => "User", :foreign_key => 'reply_id' + + acts_as_event :title => Proc.new {|o| "#{l(:label_my_message)}"}, + :datetime => Proc.new {|o| o.updated_on }, + :author => Proc.new {|o| o.user }, + :description => Proc.new{|o| o.notes }, + :type => Proc.new {|o| o.jour_type }, + :url => Proc.new {|o| + if o.jour.kind_of? Project + {:controller => 'projects', :action => 'feedback', :id => o.jour, :r => o.id, :anchor => "word_li_#{o.id}"} + elsif o.jour.kind_of? Course + {:controller => 'courses', :action => 'feedback', :id => o.jour, :r => o.id, :anchor => "word_li_#{o.id}"} + end + } + acts_as_activity_provider :author_key => :user_id, + :timestamp => "#{self.table_name}.updated_on", + :find_options => {:include => :project } + + acts_as_activity_provider :type => 'course_journals_for_messages', + :author_key => :user_id, + :permission => :view_course_journals_for_messages, + :timestamp => "#{self.table_name}.updated_on", + :find_options => {:include => :course } + acts_as_attachable + + has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy + + validates :notes, presence: true, if: :is_homework_jour? + after_create :act_as_activity #huang + after_create :reset_counters! + after_destroy :reset_counters! + after_save :be_user_score + after_destroy :down_user_score + + # default_scope { where('m_parent_id IS NULL') } + + def self.create_by_user? user + if user.anonymous? + return false + else + return true + end + end + + + def self.remove_by_user? user + if( self.user == user || + ( self.jour.kind_of?(User) && self.jour== user ) + ) + true + else + false + end + end + + def self.delete_message(message_id) + self.find(message_id).destroy + # self.destroy_all "id = #{message_id}" + end + + def is_homework_jour? + self.jour_type != "HomeworkAttach" + end + + def reference_user + User.find(reply_id) + end + + def delete_by_user?user + # 用户可删除自己的留言 + if self.user.id == user.id || user.admin? + return true + else + return false + end + end + + def self.reference_message(user_id) + @user = User.find(user_id) + message = JournalsForMessage.find_by_sql("select * from journals_for_messages where reply_id = #{@user.id} + or (jour_type = 'Bid' and jour_id in (select id from bids where author_id = #{@user.id}))") + message + end + + def act_as_activity + if self.jour_type == 'Principal' + unless self.user_id == self.jour.id && self.user_id != self.reply_id && self.reply_id != 0 + # self.acts << Activity.new(:user_id => self.user_id) + self.acts << Activity.new(:user_id => self.jour_id) + end + elsif self.jour_type == 'Project' + self.acts << Activity.new(:user_id => self.reply_id) + elsif self.jour_type == 'Course' + self.acts << Activity.new(:user_id => self.reply_id) + else + end + end + + def reset_counters! + self.class.reset_counters!(self) + end + def self.reset_counters! journals_for_messages + # jfm_id = journals_for_messages.id.to_i + count = find_all_by_m_parent_id(journals_for_messages.m_parent_id).count #(SELECT COUNT(*) FROM #{JournalsForMessage.table_name} WHERE m_parent_id = #{jfm_id} ) + update_all("m_reply_count = #{count.to_i}", ["id = ?", journals_for_messages.m_parent_id]) + end + + #如果是在项目中留言则返回该项目否则返回nil - zjc + def project + if self.jour_type == 'Project' + Project.find(self.jour_id) + else + nil + end + end + + # 更新用户分数 -by zjc + def be_user_score + #新建了留言回复 + if self.reply_id != 0 + #协同得分加分 + UserScore.joint(:reply_message, self.user,User.find(self.reply_id),self, { journals_for_messages_id: self.id }) + update_replay_for_message(self.user,1) + if self.jour_type == "Project" + update_replay_for_message(self.user,2,self.jour) + end + end + end + # 更新用户分数 -by zjc + def down_user_score + #删除了留言回复 + if self.reply_id != 0 + #协同得分减分 + UserScore.joint(:reply_message_delete, self.user,User.find(self.reply_id), { journals_for_messages_id: self.id }) + update_replay_for_message(self.user,1) + if self.jour_type == "Project" + update_replay_for_message(self.user,2,self.jour) + end + end + end + + # Time 2015-04-01 14:15:06 + # Author lizanle + # Description 删除对应课程留言的图片资源 + def delete_kindeditor_assets + delete_kindeditor_assets_from_disk self.id,7 + end +end diff --git a/app/models/journals_for_message_observer.rb b/app/models/journals_for_message_observer.rb index 093002a2c..0db2e0043 100644 --- a/app/models/journals_for_message_observer.rb +++ b/app/models/journals_for_message_observer.rb @@ -1,7 +1,7 @@ -# Added by young -class JournalsForMessageObserver < ActiveRecord::Observer - def after_create(journals_for_message) - Mailer.journals_for_message_add(User.current, journals_for_message).deliver - end -end - +# Added by young +class JournalsForMessageObserver < ActiveRecord::Observer + def after_create(journals_for_message) + Mailer.run.journals_for_message_add(User.current, journals_for_message) + end +end + diff --git a/app/models/kindeditor/asset.rb b/app/models/kindeditor/asset.rb new file mode 100644 index 000000000..bae948c99 --- /dev/null +++ b/app/models/kindeditor/asset.rb @@ -0,0 +1,15 @@ +class Kindeditor::Asset < ActiveRecord::Base + self.table_name = 'kindeditor_assets' + mount_uploader :asset, Kindeditor::AssetUploader + validates_presence_of :asset + before_save :update_asset_attributes + attr_accessible :asset + + private + def update_asset_attributes + if asset.present? && asset_changed? + self.file_size = asset.file.size + self.file_type = asset.file.content_type + end + end +end \ No newline at end of file diff --git a/app/models/kindeditor/file.rb b/app/models/kindeditor/file.rb new file mode 100644 index 000000000..4b5f11441 --- /dev/null +++ b/app/models/kindeditor/file.rb @@ -0,0 +1,3 @@ +class Kindeditor::File < Kindeditor::Asset + mount_uploader :asset, Kindeditor::FileUploader +end \ No newline at end of file diff --git a/app/models/kindeditor/flash.rb b/app/models/kindeditor/flash.rb new file mode 100644 index 000000000..efaf5de9b --- /dev/null +++ b/app/models/kindeditor/flash.rb @@ -0,0 +1,3 @@ +class Kindeditor::Flash < Kindeditor::Asset + mount_uploader :asset, Kindeditor::FlashUploader +end \ No newline at end of file diff --git a/app/models/kindeditor/image.rb b/app/models/kindeditor/image.rb new file mode 100644 index 000000000..a400c816d --- /dev/null +++ b/app/models/kindeditor/image.rb @@ -0,0 +1,3 @@ +class Kindeditor::Image < Kindeditor::Asset + mount_uploader :asset, Kindeditor::ImageUploader +end \ No newline at end of file diff --git a/app/models/kindeditor/media.rb b/app/models/kindeditor/media.rb new file mode 100644 index 000000000..071763319 --- /dev/null +++ b/app/models/kindeditor/media.rb @@ -0,0 +1,3 @@ +class Kindeditor::Media < Kindeditor::Asset + mount_uploader :asset, Kindeditor::MediaUploader +end \ No newline at end of file diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb index 8421fb67d..bf268ad66 100644 --- a/app/models/mail_handler.rb +++ b/app/models/mail_handler.rb @@ -1,490 +1,490 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class MailHandler < ActionMailer::Base - include ActionView::Helpers::SanitizeHelper - include Redmine::I18n - - class UnauthorizedAction < StandardError; end - class MissingInformation < StandardError; end - - attr_reader :email, :user - - def self.receive(email, options={}) - @@handler_options = options.dup - - @@handler_options[:issue] ||= {} - - if @@handler_options[:allow_override].is_a?(String) - @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip) - end - @@handler_options[:allow_override] ||= [] - # Project needs to be overridable if not specified - @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project) - # Status overridable by default - @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status) - - @@handler_options[:no_account_notice] = (@@handler_options[:no_account_notice].to_s == '1') - @@handler_options[:no_notification] = (@@handler_options[:no_notification].to_s == '1') - @@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1') - - email.force_encoding('ASCII-8BIT') if email.respond_to?(:force_encoding) - super(email) - end - - def logger - Rails.logger - end - - cattr_accessor :ignored_emails_headers - @@ignored_emails_headers = { - 'X-Auto-Response-Suppress' => 'oof', - 'Auto-Submitted' => /^auto-/ - } - - # Processes incoming emails - # Returns the created object (eg. an issue, a message) or false - def receive(email) - @email = email - sender_email = email.from.to_a.first.to_s.strip - # Ignore emails received from the application emission address to avoid hell cycles - if sender_email.downcase == Setting.mail_from.to_s.strip.downcase - if logger && logger.info - logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]" - end - return false - end - # Ignore auto generated emails - self.class.ignored_emails_headers.each do |key, ignored_value| - value = email.header[key] - if value - value = value.to_s.downcase - if (ignored_value.is_a?(Regexp) && value.match(ignored_value)) || value == ignored_value - if logger && logger.info - logger.info "MailHandler: ignoring email with #{key}:#{value} header" - end - return false - end - end - end - @user = User.find_by_mail(sender_email) if sender_email.present? - if @user && !@user.active? - if logger && logger.info - logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]" - end - return false - end - if @user.nil? - # Email was submitted by an unknown user - case @@handler_options[:unknown_user] - when 'accept' - @user = User.anonymous - when 'create' - @user = create_user_from_email - if @user - if logger && logger.info - logger.info "MailHandler: [#{@user.login}] account created" - end - add_user_to_group(@@handler_options[:default_group]) - unless @@handler_options[:no_account_notice] - Mailer.account_information(@user, @user.password).deliver - end - else - if logger && logger.error - logger.error "MailHandler: could not create account for [#{sender_email}]" - end - return false - end - else - # Default behaviour, emails from unknown users are ignored - if logger && logger.info - logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]" - end - return false - end - end - User.current = @user - dispatch - end - - private - - MESSAGE_ID_RE = %r{^ e - # TODO: send a email to the user - logger.error e.message if logger - false - rescue MissingInformation => e - logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger - false - rescue UnauthorizedAction => e - logger.error "MailHandler: unauthorized attempt from #{user}" if logger - false - end - - def dispatch_to_default - receive_issue - end - - # Creates a new issue - def receive_issue - project = target_project - # check permission - unless @@handler_options[:no_permission_check] - raise UnauthorizedAction unless user.allowed_to?(:add_issues, project) - end - - issue = Issue.new(:author => user, :project => project) - issue.safe_attributes = issue_attributes_from_keywords(issue) - issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} - issue.subject = cleaned_up_subject - if issue.subject.blank? - issue.subject = '(no subject)' - end - issue.description = cleaned_up_text_body - - # add To and Cc as watchers before saving so the watchers can reply to Redmine - add_watchers(issue) - issue.save! - add_attachments(issue) - logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info - issue - end - - # Adds a note to an existing issue - def receive_issue_reply(issue_id, from_journal=nil) - issue = Issue.find_by_id(issue_id) - return unless issue - # check permission - unless @@handler_options[:no_permission_check] - unless user.allowed_to?(:add_issue_notes, issue.project) || - user.allowed_to?(:edit_issues, issue.project) - raise UnauthorizedAction - end - end - - # ignore CLI-supplied defaults for new issues - @@handler_options[:issue].clear - - journal = issue.init_journal(user) - if from_journal && from_journal.private_notes? - # If the received email was a reply to a private note, make the added note private - issue.private_notes = true - end - issue.safe_attributes = issue_attributes_from_keywords(issue) - issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} - journal.notes = cleaned_up_text_body - add_attachments(issue) - issue.save! - if logger && logger.info - logger.info "MailHandler: issue ##{issue.id} updated by #{user}" - end - journal - end - - # Reply will be added to the issue - def receive_journal_reply(journal_id) - journal = Journal.find_by_id(journal_id) - if journal && journal.journalized_type == 'Issue' - receive_issue_reply(journal.journalized_id, journal) - end - end - - # Receives a reply to a forum message - def receive_message_reply(message_id) - message = Message.find_by_id(message_id) - if message - message = message.root - - unless @@handler_options[:no_permission_check] - raise UnauthorizedAction unless user.allowed_to?(:add_messages, message.project) - end - - if !message.locked? - reply = Message.new(:subject => cleaned_up_subject.gsub(%r{^.*msg\d+\]}, '').strip, - :content => cleaned_up_text_body) - reply.author = user - reply.board = message.board - message.children << reply - add_attachments(reply) - reply - else - if logger && logger.info - logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic" - end - end - end - end - - def add_attachments(obj) - if email.attachments && email.attachments.any? - email.attachments.each do |attachment| - obj.attachments << Attachment.create(:container => obj, - :file => attachment.decoded, - :filename => attachment.filename, - :author => user, - :content_type => attachment.mime_type) - end - end - end - - # Adds To and Cc as watchers of the given object if the sender has the - # appropriate permission - def add_watchers(obj) - if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project) - addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase} - unless addresses.empty? - watchers = User.active.where('LOWER(mail) IN (?)', addresses).all - watchers.each {|w| obj.add_watcher(w)} - end - end - end - - def get_keyword(attr, options={}) - @keywords ||= {} - if @keywords.has_key?(attr) - @keywords[attr] - else - @keywords[attr] = begin - if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && - (v = extract_keyword!(plain_text_body, attr, options[:format])) - v - elsif !@@handler_options[:issue][attr].blank? - @@handler_options[:issue][attr] - end - end - end - end - - # Destructively extracts the value for +attr+ in +text+ - # Returns nil if no matching keyword found - def extract_keyword!(text, attr, format=nil) - keys = [attr.to_s.humanize] - if attr.is_a?(Symbol) - if user && user.language.present? - keys << l("field_#{attr}", :default => '', :locale => user.language) - end - if Setting.default_language.present? - keys << l("field_#{attr}", :default => '', :locale => Setting.default_language) - end - end - keys.reject! {|k| k.blank?} - keys.collect! {|k| Regexp.escape(k)} - format ||= '.+' - keyword = nil - regexp = /^(#{keys.join('|')})[ \t]*:[ \t]*(#{format})\s*$/i - if m = text.match(regexp) - keyword = m[2].strip - text.gsub!(regexp, '') - end - keyword - end - - def target_project - # TODO: other ways to specify project: - # * parse the email To field - # * specific project (eg. Setting.mail_handler_target_project) - target = Project.find_by_identifier(get_keyword(:project)) - raise MissingInformation.new('Unable to determine target project') if target.nil? - target - end - - # Returns a Hash of issue attributes extracted from keywords in the email body - def issue_attributes_from_keywords(issue) - assigned_to = (k = get_keyword(:assigned_to, :override => true)) && find_assignee_from_keyword(k, issue) - - attrs = { - 'tracker_id' => (k = get_keyword(:tracker)) && issue.project.trackers.named(k).first.try(:id), - 'status_id' => (k = get_keyword(:status)) && IssueStatus.named(k).first.try(:id), - 'priority_id' => (k = get_keyword(:priority)) && IssuePriority.named(k).first.try(:id), - 'category_id' => (k = get_keyword(:category)) && issue.project.issue_categories.named(k).first.try(:id), - 'assigned_to_id' => assigned_to.try(:id), - 'fixed_version_id' => (k = get_keyword(:fixed_version, :override => true)) && - issue.project.shared_versions.named(k).first.try(:id), - 'start_date' => get_keyword(:start_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'), - 'due_date' => get_keyword(:due_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'), - 'estimated_hours' => get_keyword(:estimated_hours, :override => true), - 'done_ratio' => get_keyword(:done_ratio, :override => true, :format => '(\d|10)?0') - }.delete_if {|k, v| v.blank? } - - if issue.new_record? && attrs['tracker_id'].nil? - attrs['tracker_id'] = issue.project.trackers.first.try(:id) - end - - attrs - end - - # Returns a Hash of issue custom field values extracted from keywords in the email body - def custom_field_values_from_keywords(customized) - customized.custom_field_values.inject({}) do |h, v| - if keyword = get_keyword(v.custom_field.name, :override => true) - h[v.custom_field.id.to_s] = v.custom_field.value_from_keyword(keyword, customized) - end - h - end - end - - # Returns the text/plain part of the email - # If not found (eg. HTML-only email), returns the body with tags removed - def plain_text_body - return @plain_text_body unless @plain_text_body.nil? - - part = email.text_part || email.html_part || email - @plain_text_body = Redmine::CodesetUtil.to_utf8(part.body.decoded, part.charset) - - # strip html tags and remove doctype directive - @plain_text_body = strip_tags(@plain_text_body.strip) - @plain_text_body.sub! %r{^$/) - addr, name = m[2], m[1] - end - if addr.present? - user = self.class.new_user_from_attributes(addr, name) - if @@handler_options[:no_notification] - user.mail_notification = 'none' - end - if user.save - user - else - logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger - nil - end - else - logger.error "MailHandler: failed to create User: no FROM address found" if logger - nil - end - end - - # Adds the newly created user to default group - def add_user_to_group(default_group) - if default_group.present? - default_group.split(',').each do |group_name| - if group = Group.named(group_name).first - group.users << @user - elsif logger - logger.warn "MailHandler: could not add user to [#{group_name}], group not found" - end - end - end - end - - # Removes the email body of text after the truncation configurations. - def cleanup_body(body) - delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).map {|s| Regexp.escape(s)} - unless delimiters.empty? - regex = Regexp.new("^[> ]*(#{ delimiters.join('|') })\s*[\r\n].*", Regexp::MULTILINE) - body = body.gsub(regex, '') - end - body.strip - end - - def find_assignee_from_keyword(keyword, issue) - keyword = keyword.to_s.downcase - assignable = issue.assignable_users - assignee = nil - assignee ||= assignable.detect {|a| - a.mail.to_s.downcase == keyword || - a.login.to_s.downcase == keyword - } - if assignee.nil? && keyword.match(/ /) - firstname, lastname = *(keyword.split) # "First Last Throwaway" - assignee ||= assignable.detect {|a| - a.is_a?(User) && a.firstname.to_s.downcase == firstname && - a.lastname.to_s.downcase == lastname - } - end - if assignee.nil? - assignee ||= assignable.detect {|a| a.name.downcase == keyword} - end - assignee - end -end +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class MailHandler < ActionMailer::Base + include ActionView::Helpers::SanitizeHelper + include Redmine::I18n + + class UnauthorizedAction < StandardError; end + class MissingInformation < StandardError; end + + attr_reader :email, :user + + def self.receive(email, options={}) + @@handler_options = options.dup + + @@handler_options[:issue] ||= {} + + if @@handler_options[:allow_override].is_a?(String) + @@handler_options[:allow_override] = @@handler_options[:allow_override].split(',').collect(&:strip) + end + @@handler_options[:allow_override] ||= [] + # Project needs to be overridable if not specified + @@handler_options[:allow_override] << 'project' unless @@handler_options[:issue].has_key?(:project) + # Status overridable by default + @@handler_options[:allow_override] << 'status' unless @@handler_options[:issue].has_key?(:status) + + @@handler_options[:no_account_notice] = (@@handler_options[:no_account_notice].to_s == '1') + @@handler_options[:no_notification] = (@@handler_options[:no_notification].to_s == '1') + @@handler_options[:no_permission_check] = (@@handler_options[:no_permission_check].to_s == '1') + + email.force_encoding('ASCII-8BIT') if email.respond_to?(:force_encoding) + super(email) + end + + def logger + Rails.logger + end + + cattr_accessor :ignored_emails_headers + @@ignored_emails_headers = { + 'X-Auto-Response-Suppress' => 'oof', + 'Auto-Submitted' => /^auto-/ + } + + # Processes incoming emails + # Returns the created object (eg. an issue, a message) or false + def receive(email) + @email = email + sender_email = email.from.to_a.first.to_s.strip + # Ignore emails received from the application emission address to avoid hell cycles + if sender_email.downcase == Setting.mail_from.to_s.strip.downcase + if logger && logger.info + logger.info "MailHandler: ignoring email from Redmine emission address [#{sender_email}]" + end + return false + end + # Ignore auto generated emails + self.class.ignored_emails_headers.each do |key, ignored_value| + value = email.header[key] + if value + value = value.to_s.downcase + if (ignored_value.is_a?(Regexp) && value.match(ignored_value)) || value == ignored_value + if logger && logger.info + logger.info "MailHandler: ignoring email with #{key}:#{value} header" + end + return false + end + end + end + @user = User.find_by_mail(sender_email) if sender_email.present? + if @user && !@user.active? + if logger && logger.info + logger.info "MailHandler: ignoring email from non-active user [#{@user.login}]" + end + return false + end + if @user.nil? + # Email was submitted by an unknown user + case @@handler_options[:unknown_user] + when 'accept' + @user = User.anonymous + when 'create' + @user = create_user_from_email + if @user + if logger && logger.info + logger.info "MailHandler: [#{@user.login}] account created" + end + add_user_to_group(@@handler_options[:default_group]) + unless @@handler_options[:no_account_notice] + Mailer.run.account_information(@user, @user.password) + end + else + if logger && logger.error + logger.error "MailHandler: could not create account for [#{sender_email}]" + end + return false + end + else + # Default behaviour, emails from unknown users are ignored + if logger && logger.info + logger.info "MailHandler: ignoring email from unknown user [#{sender_email}]" + end + return false + end + end + User.current = @user + dispatch + end + + private + + MESSAGE_ID_RE = %r{^ e + # TODO: send a email to the user + logger.error e.message if logger + false + rescue MissingInformation => e + logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger + false + rescue UnauthorizedAction => e + logger.error "MailHandler: unauthorized attempt from #{user}" if logger + false + end + + def dispatch_to_default + receive_issue + end + + # Creates a new issue + def receive_issue + project = target_project + # check permission + unless @@handler_options[:no_permission_check] + raise UnauthorizedAction unless user.allowed_to?(:add_issues, project) + end + + issue = Issue.new(:author => user, :project => project) + issue.safe_attributes = issue_attributes_from_keywords(issue) + issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} + issue.subject = cleaned_up_subject + if issue.subject.blank? + issue.subject = '(no subject)' + end + issue.description = cleaned_up_text_body + + # add To and Cc as watchers before saving so the watchers can reply to Redmine + add_watchers(issue) + issue.save! + add_attachments(issue) + logger.info "MailHandler: issue ##{issue.id} created by #{user}" if logger && logger.info + issue + end + + # Adds a note to an existing issue + def receive_issue_reply(issue_id, from_journal=nil) + issue = Issue.find_by_id(issue_id) + return unless issue + # check permission + unless @@handler_options[:no_permission_check] + unless user.allowed_to?(:add_issue_notes, issue.project) || + user.allowed_to?(:edit_issues, issue.project) + raise UnauthorizedAction + end + end + + # ignore CLI-supplied defaults for new issues + @@handler_options[:issue].clear + + journal = issue.init_journal(user) + if from_journal && from_journal.private_notes? + # If the received email was a reply to a private note, make the added note private + issue.private_notes = true + end + issue.safe_attributes = issue_attributes_from_keywords(issue) + issue.safe_attributes = {'custom_field_values' => custom_field_values_from_keywords(issue)} + journal.notes = cleaned_up_text_body + add_attachments(issue) + issue.save! + if logger && logger.info + logger.info "MailHandler: issue ##{issue.id} updated by #{user}" + end + journal + end + + # Reply will be added to the issue + def receive_journal_reply(journal_id) + journal = Journal.find_by_id(journal_id) + if journal && journal.journalized_type == 'Issue' + receive_issue_reply(journal.journalized_id, journal) + end + end + + # Receives a reply to a forum message + def receive_message_reply(message_id) + message = Message.find_by_id(message_id) + if message + message = message.root + + unless @@handler_options[:no_permission_check] + raise UnauthorizedAction unless user.allowed_to?(:add_messages, message.project) + end + + if !message.locked? + reply = Message.new(:subject => cleaned_up_subject.gsub(%r{^.*msg\d+\]}, '').strip, + :content => cleaned_up_text_body) + reply.author = user + reply.board = message.board + message.children << reply + add_attachments(reply) + reply + else + if logger && logger.info + logger.info "MailHandler: ignoring reply from [#{sender_email}] to a locked topic" + end + end + end + end + + def add_attachments(obj) + if email.attachments && email.attachments.any? + email.attachments.each do |attachment| + obj.attachments << Attachment.create(:container => obj, + :file => attachment.decoded, + :filename => attachment.filename, + :author => user, + :content_type => attachment.mime_type) + end + end + end + + # Adds To and Cc as watchers of the given object if the sender has the + # appropriate permission + def add_watchers(obj) + if user.allowed_to?("add_#{obj.class.name.underscore}_watchers".to_sym, obj.project) + addresses = [email.to, email.cc].flatten.compact.uniq.collect {|a| a.strip.downcase} + unless addresses.empty? + watchers = User.active.where('LOWER(mail) IN (?)', addresses).all + watchers.each {|w| obj.add_watcher(w)} + end + end + end + + def get_keyword(attr, options={}) + @keywords ||= {} + if @keywords.has_key?(attr) + @keywords[attr] + else + @keywords[attr] = begin + if (options[:override] || @@handler_options[:allow_override].include?(attr.to_s)) && + (v = extract_keyword!(plain_text_body, attr, options[:format])) + v + elsif !@@handler_options[:issue][attr].blank? + @@handler_options[:issue][attr] + end + end + end + end + + # Destructively extracts the value for +attr+ in +text+ + # Returns nil if no matching keyword found + def extract_keyword!(text, attr, format=nil) + keys = [attr.to_s.humanize] + if attr.is_a?(Symbol) + if user && user.language.present? + keys << l("field_#{attr}", :default => '', :locale => user.language) + end + if Setting.default_language.present? + keys << l("field_#{attr}", :default => '', :locale => Setting.default_language) + end + end + keys.reject! {|k| k.blank?} + keys.collect! {|k| Regexp.escape(k)} + format ||= '.+' + keyword = nil + regexp = /^(#{keys.join('|')})[ \t]*:[ \t]*(#{format})\s*$/i + if m = text.match(regexp) + keyword = m[2].strip + text.gsub!(regexp, '') + end + keyword + end + + def target_project + # TODO: other ways to specify project: + # * parse the email To field + # * specific project (eg. Setting.mail_handler_target_project) + target = Project.find_by_identifier(get_keyword(:project)) + raise MissingInformation.new('Unable to determine target project') if target.nil? + target + end + + # Returns a Hash of issue attributes extracted from keywords in the email body + def issue_attributes_from_keywords(issue) + assigned_to = (k = get_keyword(:assigned_to, :override => true)) && find_assignee_from_keyword(k, issue) + + attrs = { + 'tracker_id' => (k = get_keyword(:tracker)) && issue.project.trackers.named(k).first.try(:id), + 'status_id' => (k = get_keyword(:status)) && IssueStatus.named(k).first.try(:id), + 'priority_id' => (k = get_keyword(:priority)) && IssuePriority.named(k).first.try(:id), + 'category_id' => (k = get_keyword(:category)) && issue.project.issue_categories.named(k).first.try(:id), + 'assigned_to_id' => assigned_to.try(:id), + 'fixed_version_id' => (k = get_keyword(:fixed_version, :override => true)) && + issue.project.shared_versions.named(k).first.try(:id), + 'start_date' => get_keyword(:start_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'), + 'due_date' => get_keyword(:due_date, :override => true, :format => '\d{4}-\d{2}-\d{2}'), + 'estimated_hours' => get_keyword(:estimated_hours, :override => true), + 'done_ratio' => get_keyword(:done_ratio, :override => true, :format => '(\d|10)?0') + }.delete_if {|k, v| v.blank? } + + if issue.new_record? && attrs['tracker_id'].nil? + attrs['tracker_id'] = issue.project.trackers.first.try(:id) + end + + attrs + end + + # Returns a Hash of issue custom field values extracted from keywords in the email body + def custom_field_values_from_keywords(customized) + customized.custom_field_values.inject({}) do |h, v| + if keyword = get_keyword(v.custom_field.name, :override => true) + h[v.custom_field.id.to_s] = v.custom_field.value_from_keyword(keyword, customized) + end + h + end + end + + # Returns the text/plain part of the email + # If not found (eg. HTML-only email), returns the body with tags removed + def plain_text_body + return @plain_text_body unless @plain_text_body.nil? + + part = email.text_part || email.html_part || email + @plain_text_body = Redmine::CodesetUtil.to_utf8(part.body.decoded, part.charset) + + # strip html tags and remove doctype directive + @plain_text_body = strip_tags(@plain_text_body.strip) + @plain_text_body.sub! %r{^$/) + addr, name = m[2], m[1] + end + if addr.present? + user = self.class.new_user_from_attributes(addr, name) + if @@handler_options[:no_notification] + user.mail_notification = 'none' + end + if user.save + user + else + logger.error "MailHandler: failed to create User: #{user.errors.full_messages}" if logger + nil + end + else + logger.error "MailHandler: failed to create User: no FROM address found" if logger + nil + end + end + + # Adds the newly created user to default group + def add_user_to_group(default_group) + if default_group.present? + default_group.split(',').each do |group_name| + if group = Group.named(group_name).first + group.users << @user + elsif logger + logger.warn "MailHandler: could not add user to [#{group_name}], group not found" + end + end + end + end + + # Removes the email body of text after the truncation configurations. + def cleanup_body(body) + delimiters = Setting.mail_handler_body_delimiters.to_s.split(/[\r\n]+/).reject(&:blank?).map {|s| Regexp.escape(s)} + unless delimiters.empty? + regex = Regexp.new("^[> ]*(#{ delimiters.join('|') })\s*[\r\n].*", Regexp::MULTILINE) + body = body.gsub(regex, '') + end + body.strip + end + + def find_assignee_from_keyword(keyword, issue) + keyword = keyword.to_s.downcase + assignable = issue.assignable_users + assignee = nil + assignee ||= assignable.detect {|a| + a.mail.to_s.downcase == keyword || + a.login.to_s.downcase == keyword + } + if assignee.nil? && keyword.match(/ /) + firstname, lastname = *(keyword.split) # "First Last Throwaway" + assignee ||= assignable.detect {|a| + a.is_a?(User) && a.firstname.to_s.downcase == firstname && + a.lastname.to_s.downcase == lastname + } + end + if assignee.nil? + assignee ||= assignable.detect {|a| a.name.downcase == keyword} + end + assignee + end +end diff --git a/app/models/mailer.rb b/app/models/mailer.rb index a7f850766..0d0b82752 100644 --- a/app/models/mailer.rb +++ b/app/models/mailer.rb @@ -1,619 +1,986 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class Mailer < ActionMailer::Base - layout 'mailer' - helper :application - helper :issues - helper :custom_fields - - include Redmine::I18n - include CoursesHelper - def self.default_url_options - { :host => Setting.host_name, :protocol => Setting.protocol } - end - - # Builds a Mail::Message object used to email recipients of the added journals for message. - - # 留言分为直接留言,和对留言人留言的回复 - # 字段说明在JournalsForMessage.rb - # 直接留言后 reply_id,m_parent_id 为空,相对应的at_user取值为nil - - def journals_for_message_add(user, journals_for_message) - @user = journals_for_message.user # 留言人 - @mail = journals_for_message.jour if journals_for_message.at_user.nil? # 留言 - @mail = journals_for_message.at_user if journals_for_message.at_user - @message = journals_for_message.notes - @title = "#@user #{t(:label_leave_your_message, :locale => 'zh')}" - - @url = case journals_for_message.jour.class.to_s.to_sym # 判断留言的对象所属类型 - when :Bid - course_for_bid_url(journals_for_message.jour, anchor: "word_li_#{journals_for_message.id}") - when :Project - return -1 if journals_for_message.jour.project_type == Project::ProjectType_project - project_feedback_url(journals_for_message.jour, anchor: "word_li_#{journals_for_message.id}") - when :Course - course_feedback_url(journals_for_message.jour, anchor: "word_li_#{journals_for_message.id}") - when :Contest - show_contest_contest_url(journals_for_message.jour, anchor: "word_li_#{journals_for_message.id}") - when :User - user_newfeedback_user_url(journals_for_message.jour, anchor: "word_li_#{journals_for_message.id}") - else - Rails.logger.error "[Builds a Mail::Message ERROR] journalsForMessage's jour is unkown type, journalsForMessage.id = #{journals_for_message.id}" - return -1 - end - # modify by nwb - #如果是直接留言并且留言对象是课程 - if !journals_for_message.at_user && journals_for_message.jour.class.to_s.to_sym == :Course - course = journals_for_message.jour - @author = journals_for_message.user - #课程的教师 - @teachers = searchTeacherAndAssistant journals_for_message.jour - #收件人邮箱 - @recipients ||= [] - @teachers.each do |teacher| - if teacher.user.notify_about? journals_for_message - @recipients << teacher.user.mail - end - - end - mail :to => @recipients, - :subject => "#{l(:label_your_course)}#{journals_for_message.jour.name}#{l(:label_have_message)} " - elsif journals_for_message.jour.class.to_s.to_sym == :Bid - if !journals_for_message.jour.author.notify_about? journals_for_message - return -1 - end - mail :to => journals_for_message.jour.author.mail, :subject => @title - elsif journals_for_message.jour.class.to_s.to_sym == :Contest - if !journals_for_message.jour.author.notify_about? journals_for_message - return -1 - end - mail :to => journals_for_message.jour.author.mail, :subject => @title - else - mail :to => @mail.mail, :subject => @title - end - - - end - - # Builds a Mail::Message object used to email recipients of the added issue. - # - # Example: - # issue_add(issue) => Mail::Message object - # Mailer.issue_add(issue).deliver => sends an email to issue recipients - def issue_add(issue) - issue_id = issue.project_index - redmine_headers 'Project' => issue.project.identifier, - 'Issue-Id' => issue_id, - 'Issue-Author' => issue.author.login - redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to - message_id issue - @author = issue.author - @issue = issue - @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id) - recipients = issue.recipients - cc = issue.watcher_recipients - recipients - mail :to => recipients, - :cc => cc, - :subject => "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] (#{issue.status.name}) #{issue.subject}" - end - - # Builds a Mail::Message object used to email recipients of the edited issue. - # - # Example: - # issue_edit(journal) => Mail::Message object - # Mailer.issue_edit(journal).deliver => sends an email to issue recipients - def issue_edit(journal) - issue = journal.journalized.reload - issue_id = issue.project_index - redmine_headers 'Project' => issue.project.identifier, - 'Issue-Id' => issue_id.to_s, - 'Issue-Author' => issue.author.login - redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to - message_id journal - references issue - @author = journal.user - recipients = journal.recipients - # Watchers in cc - cc = journal.watcher_recipients - recipients - s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] " - s << "(#{issue.status.name}) " if journal.new_value_for('status_id') - s << issue.subject - @issue = issue - @journal = journal - @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}") - mail :to => recipients, - :cc => cc, - :subject => s - end - - # 用户申请加入项目邮件通知 - def applied_project(applied) - @project =applied.project - redmine_headers 'Project' => @project, - 'User' => applied.user - @user = applied.user - recipients = @project.manager_recipients - s = l(:text_applied_project, :id => "##{@user.show_name}", :project => @project.name) - @applied_url = url_for(:controller => 'projects', :action => 'settings', :id => @project.id,:tab=>'members') - mail :to => recipients, - :subject => s - end - - def reminder(user, issues, days) - set_language_if_valid user.language - @issues = issues - @days = days - @issues_url = url_for(:controller => 'issues', :action => 'index', - :set_filter => 1, :assigned_to_id => user.id, - :sort => 'due_date:asc') - mail :to => user.mail, - :subject => l(:mail_subject_reminder, :count => issues.size, :days => days) - end - - #缺陷到期邮件通知 - def issue_expire issue - issue_id = issue.project_index - redmine_headers 'Project' => issue.project.identifier, - 'Issue-Id' => issue_id, - 'Issue-Author' => issue.author.login - redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to - message_id issue - @author = issue.author - @issue = issue - @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id) - recipients = issue.recipients - s = l(:text_issue_expire,:issue => "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] (#{issue.status.name}) #{issue.subject}") - mail :to => recipients, - :subject => s - ######################################################################################################### - #@issues = issues - #s = l(:text_issue_expire,:issue => "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] (#{issue.status.name}) #{issue.subject}") - #puts s + "////" + issue.assigned_to.mail - #@issues_url = url_for(:controller => 'issues', :action => 'show',:id => issue.id) - #mail :to => issue.assigned_to.mail, - # :subject => s - ######################################################################################################### - #issue_id = issue.project_index - #redmine_headers 'Project' => issue.project.identifier, - # 'Issue-Id' => issue_id, - # 'Issue-Author' => issue.author.login - #redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to - #message_id issue - #@author = issue.author - #@issue = issue - #@issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue) - #recipients = issue.recipients - #cc = issue.watcher_recipients - recipients - #mail :to => recipients, - # :cc => cc, - # :subject => "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] (#{issue.status.name}) #{issue.subject}" - ###################################################################################################### - end - - - # Builds a Mail::Message object used to email users belonging to the added document's project. - # - # Example: - # document_added(document) => Mail::Message object - # Mailer.document_added(document).deliver => sends an email to the document's project recipients - def document_added(document) - redmine_headers 'Project' => document.project.identifier - @author = User.current - @document = document - @document_url = url_for(:controller => 'documents', :action => 'show', :id => document) - mail :to => document.recipients, - :subject => "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}" - end - - # Builds a Mail::Message object used to email recipients of a project when an attachements are added. - # - # Example: - # attachments_added(attachments) => Mail::Message object - # Mailer.attachments_added(attachments).deliver => sends an email to the project's recipients - def attachments_added(attachments) - container = attachments.first.container - added_to = '' - added_to_url = '' - @author = attachments.first.author - case container.class.name - when 'Project' - added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container) - added_to = "#{l(:label_project)}: #{container}" - recipients = container.notified_users.select { |user| user.allowed_to?(:view_files, container) }.collect { |u| u.mail } - when 'Course' - added_to_url = url_for(:controller => 'files', :action => 'index', :course_id => container) - added_to = "#{l(:label_course)}: #{container}" - recipients = container.notified_users.select { |user| user.allowed_to?(:view_files, container) }.collect { |u| u.mail } - when 'Version' - added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project) - added_to = "#{l(:label_version)}: #{container.name}" - recipients = container.project.notified_users.select { |user| user.allowed_to?(:view_files, container.project) }.collect { |u| u.mail } - when 'Document' - added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id) - added_to = "#{l(:label_document)}: #{container.title}" - recipients = container.recipients - end - if container.class.name == 'Course' - redmine_headers 'Course' => container.id - @attachments = attachments - @added_to = added_to - @added_to_url = added_to_url - mail :to => recipients, - :subject => "[#{container.name}] #{l(:label_attachment_new)}" - elsif container.class.name == 'Project' - redmine_headers 'Project' => container.id - @attachments = attachments - @added_to = added_to - @added_to_url = added_to_url - mail :to => recipients, - :subject => "[#{container.name}] #{l(:label_attachment_new)}" - else - redmine_headers 'Project' => container.project.identifier - @attachments = attachments - @added_to = added_to - @added_to_url = added_to_url - mail :to => recipients, - :subject => "[#{container.project.name}] #{l(:label_attachment_new)}" - end - end - - # Builds a Mail::Message object used to email recipients of a news' project when a news item is added. - # - # Example: - # news_added(news) => Mail::Message object - # Mailer.news_added(news).deliver => sends an email to the news' project recipients - def news_added(news) - redmine_headers 'Project' => news.project.identifier - @author = news.author - message_id news - @news = news - @news_url = url_for(:controller => 'news', :action => 'show', :id => news) - mail :to => news.recipients, - :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}" - end - - # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added. - # - # Example: - # news_comment_added(comment) => Mail::Message object - # Mailer.news_comment_added(comment) => sends an email to the news' project recipients - def news_comment_added(comment) - news = comment.commented - redmine_headers 'Project' => news.project.identifier - @author = comment.author - message_id comment - @news = news - @comment = comment - @news_url = url_for(:controller => 'news', :action => 'show', :id => news) - mail :to => news.recipients, - :cc => news.watcher_recipients, - :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}" - end - - # Builds a Mail::Message object used to email the recipients of the specified message that was posted. - # - # Example: - # message_posted(message) => Mail::Message object - # Mailer.message_posted(message).deliver => sends an email to the recipients - def message_posted(message) - redmine_headers 'Project' => message.project.identifier, - 'Topic-Id' => (message.parent_id || message.id) - @author = message.author - message_id message - references message.parent unless message.parent.nil? - recipients = message.recipients - cc = ((message.root.watcher_recipients + message.board.watcher_recipients).uniq - recipients) - @message = message - @message_url = url_for(message.event_url) - mail :to => recipients, - :cc => cc, - :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}" - end - - # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was added. - # - # Example: - # wiki_content_added(wiki_content) => Mail::Message object - # Mailer.wiki_content_added(wiki_content).deliver => sends an email to the project's recipients - def wiki_content_added(wiki_content) - redmine_headers 'Project' => wiki_content.project.identifier, - 'Wiki-Page-Id' => wiki_content.page.id - @author = wiki_content.author - message_id wiki_content - recipients = wiki_content.recipients - cc = wiki_content.page.wiki.watcher_recipients - recipients - @wiki_content = wiki_content - @wiki_content_url = url_for(:controller => 'wiki', :action => 'show', - :project_id => wiki_content.project, - :id => wiki_content.page.title) - mail :to => recipients, - :cc => cc, - :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}" - end - - # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated. - # - # Example: - # wiki_content_updated(wiki_content) => Mail::Message object - # Mailer.wiki_content_updated(wiki_content).deliver => sends an email to the project's recipients - def wiki_content_updated(wiki_content) - redmine_headers 'Project' => wiki_content.project.identifier, - 'Wiki-Page-Id' => wiki_content.page.id - @author = wiki_content.author - message_id wiki_content - recipients = wiki_content.recipients - cc = wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients - @wiki_content = wiki_content - @wiki_content_url = url_for(:controller => 'wiki', :action => 'show', - :project_id => wiki_content.project, - :id => wiki_content.page.title) - @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff', - :project_id => wiki_content.project, :id => wiki_content.page.title, - :version => wiki_content.version) - mail :to => recipients, - :cc => cc, - :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}" - end - - # Builds a Mail::Message object used to email the specified user their account information. - # - # Example: - # account_information(user, password) => Mail::Message object - # Mailer.account_information(user, password).deliver => sends account information to the user - def account_information(user, password) - set_language_if_valid user.language - @user = user - @password = password - @login_url = url_for(:controller => 'account', :action => 'login') - mail :to => user.mail, - :subject => l(:mail_subject_register, Setting.app_title) - end - - # Builds a Mail::Message object used to email all active administrators of an account activation request. - # - # Example: - # account_activation_request(user) => Mail::Message object - # Mailer.account_activation_request(user).deliver => sends an email to all active administrators - def account_activation_request(user) - # Send the email to all active administrators - recipients = User.active.where(:admin => true).all.collect { |u| u.mail }.compact - @user = user - @url = url_for(:controller => 'users', :action => 'index', - :status => User::STATUS_REGISTERED, - :sort_key => 'created_on', :sort_order => 'desc') - mail :to => recipients, - :subject => l(:mail_subject_account_activation_request, Setting.app_title) - end - - # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator. - # - # Example: - # account_activated(user) => Mail::Message object - # Mailer.account_activated(user).deliver => sends an email to the registered user - def account_activated(user) - set_language_if_valid user.language - @user = user - @login_url = url_for(:controller => 'account', :action => 'login') - mail :to => user.mail, - :subject => l(:mail_subject_register, Setting.app_title) - end - - def lost_password(token) - set_language_if_valid(token.user.language) - @token = token - @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value) - mail :to => token.user.mail, - :subject => l(:mail_subject_lost_password, Setting.app_title) - end - - def register(token) - set_language_if_valid(token.user.language) - @token = token - @url = url_for(:controller => 'account', :action => 'activate', :token => token.value) - mail :to => token.user.mail, - :subject => l(:mail_subject_register, Setting.app_title) - end - - def test_email(user) - set_language_if_valid(user.language) - @url = url_for(:controller => 'welcome') - mail :to => user.mail, - :subject => 'forge test' - end - - # Overrides default deliver! method to prevent from sending an email - # with no recipient, cc or bcc - def deliver!(mail = @mail) - set_language_if_valid @initial_language - return false if (recipients.nil? || recipients.empty?) && - (cc.nil? || cc.empty?) && - (bcc.nil? || bcc.empty?) - - - # Log errors when raise_delivery_errors is set to false, Rails does not - raise_errors = self.class.raise_delivery_errors - self.class.raise_delivery_errors = true - begin - return super(mail) - rescue Exception => e - if raise_errors - raise e - elsif mylogger - mylogger.error "The following error occured while sending email notification: \"#{e.message}\". Check your configuration in config/configuration.yml." - end - ensure - self.class.raise_delivery_errors = raise_errors - end - end - - # Sends reminders to issue assignees - # Available options: - # * :days => how many days in the future to remind about (defaults to 7) - # * :tracker => id of tracker for filtering issues (defaults to all trackers) - # * :project => id or identifier of project to process (defaults to all projects) - # * :users => array of user/group ids who should be reminded - def self.reminders(options={}) - days = options[:days] || 7 - project = options[:project] ? Project.find(options[:project]) : nil - tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil - user_ids = options[:users] - - scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" + - " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" + - " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date - ) - scope = scope.where(:assigned_to_id => user_ids) if user_ids.present? - scope = scope.where(:project_id => project.id) if project - scope = scope.where(:tracker_id => tracker.id) if tracker - - issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker).all.group_by(&:assigned_to) - issues_by_assignee.keys.each do |assignee| - if assignee.is_a?(Group) - assignee.users.each do |user| - issues_by_assignee[user] ||= [] - issues_by_assignee[user] += issues_by_assignee[assignee] - end - end - end - - issues_by_assignee.each do |assignee, issues| - reminder(assignee, issues, days).deliver if assignee.is_a?(User) && assignee.active? - end - end - - # Activates/desactivates email deliveries during +block+ - def self.with_deliveries(enabled = true, &block) - was_enabled = ActionMailer::Base.perform_deliveries - ActionMailer::Base.perform_deliveries = !!enabled - yield - ensure - ActionMailer::Base.perform_deliveries = was_enabled - end - - # Sends emails synchronously in the given block - def self.with_synched_deliveries(&block) - saved_method = ActionMailer::Base.delivery_method - if m = saved_method.to_s.match(%r{^async_(.+)$}) - synched_method = m[1] - ActionMailer::Base.delivery_method = synched_method.to_sym - ActionMailer::Base.send "#{synched_method}_settings=", ActionMailer::Base.send("async_#{synched_method}_settings") - end - yield - ensure - ActionMailer::Base.delivery_method = saved_method - end - - def mail(headers={}) - headers.merge! 'X-Mailer' => 'Redmine', - 'X-Redmine-Host' => Setting.host_name, - 'X-Redmine-Site' => Setting.app_title, - 'X-Auto-Response-Suppress' => 'OOF', - 'Auto-Submitted' => 'auto-generated', - 'From' => Setting.mail_from, - 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>" - - # Removes the author from the recipients and cc - # if he doesn't want to receive notifications about what he does - if @author && @author.logged? && @author.pref[:no_self_notified] - headers[:to].delete(@author.mail) if headers[:to].is_a?(Array) - headers[:cc].delete(@author.mail) if headers[:cc].is_a?(Array) - end - - if @author && @author.logged? - redmine_headers 'Sender' => @author.login - end - - # Blind carbon copy recipients - if Setting.bcc_recipients? - headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?) - headers[:to] = nil - headers[:cc] = nil - end - - if @message_id_object - headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>" - end - if @references_objects - headers[:references] = @references_objects.collect {|o| "<#{self.class.message_id_for(o)}>"}.join(' ') - end - - super headers do |format| - format.text - format.html unless Setting.plain_text_mail? - end - - set_language_if_valid @initial_language - end - - def initialize(*args) - @initial_language = current_language - set_language_if_valid Setting.default_language - super - end - - def self.deliver_mail(mail) - return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank? - Thread.new do - super - end - end - - def self.method_missing(method, *args, &block) - if m = method.to_s.match(%r{^deliver_(.+)$}) - ActiveSupport::Deprecation.warn "Mailer.deliver_#{m[1]}(*args) is deprecated. Use Mailer.#{m[1]}(*args).deliver instead." - send(m[1], *args).deliver - else - super - end - end - - - - private - - # Appends a Redmine header field (name is prepended with 'X-Redmine-') - def redmine_headers(h) - h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s } - end - - # Returns a predictable Message-Id for the given object - def self.message_id_for(object) - # id + timestamp should reduce the odds of a collision - # as far as we don't send multiple emails for the same object - timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on) - hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{timestamp.strftime("%Y%m%d%H%M%S")}" - host = Setting.mail_from.to_s.gsub(%r{^.*@}, '') - host = "#{::Socket.gethostname}.redmine" if host.empty? - "#{hash}@#{host}" - end - - def message_id(object) - @message_id_object = object - end - - def references(object) - @references_objects ||= [] - @references_objects << object - end - - def mylogger - Rails.logger - end - - -end +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class Mailer < ActionMailer::Base + layout 'mailer' + helper :application + helper :issues + helper :custom_fields + + include Redmine::I18n + include CoursesHelper + def self.default_url_options + { :host => Setting.host_name, :protocol => Setting.protocol } + end + + + class MailerProxy + def initialize(cls) + @target = cls + end + def method_missing(name, *args, &block) + if Setting.delayjob_enabled? && Object.const_defined?('Delayed') + @target.delay.send(name, *args, &block) + else + @target.send(name, *args, &block).deliver + end + end + end + + def self.run + MailerProxy.new(self) + end + + # author: alan + # 发送邀请未注册用户加入项目邮件 + # 功能: 在加入项目的同时自动注册用户 + def send_invite_in_project(email, project, invitor) + @email = email + @subject = "#{invitor.name} #{l(:label_invite_project)} #{project.name} " + @password = newpass(6) + + login = email + login = login.sub(/%40/,'@') + us = UsersService.new + # 自动激活用户 + user = us.register_auto(login, @email, @password) + InviteList.create(:user_id => user.id, :project_id => project.id) + User.current = user unless User.current.nil? + @user = user + @token = Token.get_token_from_user(user, 'autologin') + @project_url = url_for(:controller => 'projects', :action => 'member', :id => project.id, :mail => true, :token => @token.value) + mail :to => email, :subject => @subject + end + + # 邀请已注册的用户加入项目 + def request_member_to_project(email, project, invitor) + @subject = "#{invitor.name} #{l(:label_invite_project)}: #{project.name} " + user = User.find_by_mail(email.to_s) + @invitor_name = "#{invitor.name}" + @project_name = "#{project.name}" + @user = user + @project = project + inviter_lists = InviteList.where(project_id:@project.id, user_id:@user.id).all + if inviter_lists.blank? + InviteList.create(:user_id => user.id, :project_id => project.id) + end + @token = Token.get_token_from_user(user, 'autologin') + @project_url = url_for(:controller => 'projects', :action => 'member', :id => project.id, :user_id => user.id, :mail => true, :token => @token.value) + mail :to => email, :subject => @subject + end + + # author: alan + # 根据用户选择发送个人日报或周报 + # 发送内容: 项目【缺陷,讨论区,新闻】,课程【通知,留言,新闻】, 贴吧, 个人留言 + def send_for_user_activities(user, date_to, days) + date_from = date_to - days.days + subject = "[ #{user.show_name}#{l(:label_day_mail)}]" + @subject = " #{user.show_name}#{l(:label_day_mail)}" + date_from = "#{date_from} 17:59:59" + date_to = "#{date_to} 17:59:59" + # 生成token用于直接点击登录 + @user = user + @token = Token.get_token_from_user(user, 'autologin') + + # 查询user参加的项目及课程 + projects = user.projects + courses = user.courses + project_ids = projects.map{|project| project.id}.join(",") + course_ids = courses.map {|course| course.id}.join(",") + + # 查询user的缺陷,项目中成员都能收到 + sql = "select DISTINCT * from members m, issues i where i.project_id = m.project_id and m.user_id='#{user.id}' + and (i.updated_on between '#{date_from}' and '#{date_to}') order by i.updated_on desc" + @issues = Issue.find_by_sql(sql) + + # issue回复 + @issues_journals = Journal.find_by_sql("select j.* from journals j, members m, projects p, issues i + where m.user_id = '#{user.id}' and p.id = m.project_id and i.project_id = p.id and j.journalized_id = i.id + and j.journalized_type='Issue' and (j.created_on between '#{date_from}' and '#{date_to}') order by created_on desc") + + # @bids 查询课程作业,包括老师发布的作业,以及user提交作业 + # @attachments查询课程课件更新 + @attachments ||= [] + @bids ||= [] # 老师发布的作业 + unless courses.first.nil? + count = courses.count + count = count - 1 + for i in 0..count do + bids = courses[i].homework_commons.where("homework_commons.created_at between '#{date_from}' and '#{date_to}'").order("homework_commons.created_at desc") + attachments = courses[i].attachments.where("attachments.created_on between '#{date_from}' and '#{date_to}'").order('attachments.created_on DESC') + @bids += bids if bids.count > 0 + @attachments += attachments if attachments.count > 0 + end + @bids.sort {|a, b| a.created_at <=> b.created_at} + end + + # 项目附件 + @project_attachments = Attachment.find_by_sql("select DISTINCT a.* from members m, attachments a + where a.container_id = m.project_id and m.user_id='#{user.id}' and container_type = 'Project' and (a.created_on between '#{date_from}' and '#{date_to}') order by a.created_on desc") + + # user 提交的作业 + # @homeworks = HomeworkAttach.where("user_id=#{user.id} and (created_at between '#{date_from}' and '#{date_to}')").order("created_at desc") + + # 查询user所在项目添加wiki + @wiki_contents = WikiContent.find_by_sql("select DISTINCT wc.* from wikis w, members m, projects p, wiki_pages wp, wiki_contents wc where + m.user_id = '#{user.id}' and m.project_id = p.id and w.project_id = p.id and w.id = wp.wiki_id and wc.page_id = wp.id and w.project_id>0 + and (wc.updated_on between '#{date_from}' and '#{date_to}') order by updated_on desc") + + # 查询user在课程中发布的讨论帖子 + course_mesages = Message.find_by_sql("select DISTINCT me.* from messages me, boards b, members m where + b.id = me.board_id and b.course_id = m.course_id + and b.course_id is not Null and m.user_id = '#{user.id}' + and (me.created_on between '#{date_from}' and '#{date_to}') order by created_on desc") + + # 查询user在项目中发布的讨论帖子 + project_messages = Message.find_by_sql("select DISTINCT me.* from messages me, boards b, members m where + b.id = me.board_id and b.project_id = m.project_id + and b.project_id != '-1' and m.user_id = '#{user.id}' and (me.created_on between '#{date_from}' and '#{date_to}') order by created_on desc") + # messages = Message.find_by_sql("select DISTINCT * from messages where author_id = #{user.id} and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") + @course_messages ||= [] + @project_messages ||= [] + unless course_mesages.first.nil? + course_mesages.each do |msg| + @course_messages << msg + end + end + unless project_messages.first.nil? + project_messages.each do |msg| + @project_messages << msg + end + end + # wiki + + # 查询user在课程中发布的通知和回复通知 + @course_news = (course_ids && !course_ids.empty?) ? News.find_by_sql("select DISTINCT n.* from news n + where n.course_id in (#{course_ids}) + and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") : [] + + @course_news_comments = Comment.find_by_sql("select cm.* from comments cm, members m, courses c, news n + where m.user_id = '#{user.id}' and c.id = m.course_id and n.course_id = c.id and cm.commented_id = n.id + and cm.commented_type ='News' and (cm.created_on between '#{date_from}' and '#{date_to}') order by created_on desc") + + # 查询user在项目中添加新闻和回复新闻 + @project_news = (project_ids && !project_ids.empty?) ? News.find_by_sql("select DISTINCT n.* from news n where n.project_id in (#{project_ids}) + and (created_on between '#{date_from}' and '#{date_to}') order by created_on desc") : [] + + @project_news_comments = Comment.find_by_sql("select c.* from comments c, members m, projects p, news n + where m.user_id = '#{user.id}' and p.id = m.project_id and n.project_id = p.id and c.commented_id = n.id + and c.commented_type ='News' and (c.created_on between '#{date_from}' and '#{date_to}') order by created_on desc") + + # 查询user在课程及个人中留言 + @course_journal_messages = JournalsForMessage.find_by_sql("select DISTINCT jfm.* from journals_for_messages jfm, members m, courses c + where m.user_id = '#{user.id}' and c.id = m.course_id and jfm.jour_id = c.id + and jfm.jour_type='Course' and (jfm.created_on between '#{date_from}' and '#{date_to}') order by created_on desc") + + @user_journal_messages = user.journals_for_messages.where("jour_type='Principal' and (created_on between '#{date_from}' and '#{date_to}')").order('created_on DESC') + + # 查询user在项目中留言(用户反馈) + @project_journal_messages = JournalsForMessage.find_by_sql("select DISTINCT jfm.* from journals_for_messages jfm, members m, projects p + where m.user_id = '#{user.id}' and p.id = m.project_id and jfm.jour_id = p.id + and jfm.jour_type='Project' and (jfm.created_on between '#{date_from}' and '#{date_to}') order by created_on desc") + + # 查询user新建贴吧或发布帖子 + @forums = Forum.find_by_sql("select DISTINCT * from forums where creator_id = #{user.id} and (created_at between '#{date_from}' and '#{date_to}') order by created_at desc") + @memos = Memo.find_by_sql("select DISTINCT m.* from memos m, forums f where (m.author_id = #{user.id} or (m.forum_id = f.id and f.creator_id = #{user.id})) + and (m.created_at between '#{date_from}' and '#{date_to}') order by m.created_at desc") + + has_content = [@issues,@issues_journals,@course_messages,@project_messages,@course_news,@course_news_comments,@project_news,@project_news_comments,@project_attachments, + @course_journal_messages,@user_journal_messages,@project_journal_messages,@forums,@memos,@attachments,@bids,@wiki_contents].any? {|o| !o.empty?} + mylogger.debug "Sent activity mail : #{user.mail} - #{has_content}" + #有内容才发,没有不发 + mail :to => user.mail,:subject => subject if has_content + end + + # 公共讨论区发帖、回帖添加邮件发送信息 + def forum_message_added(memo) + @memo = memo + redmine_headers 'Memo' => memo.id + @forum = memo.forum + @author = memo.author + @forum_url = url_for(:controller => 'forums', :action => 'show', :id => @forum.id) + @issue_author_url = url_for(user_activities_url(@author)) + recipients ||= [] + #将帖子创建者邮箱地址加入数组 + recipients << @forum.creator.mail + #回复人邮箱地址加入数组 + recipients << @author.mail + # cc = wiki_content.page.wiki.watcher_recipients - recipients + + @memo_url = url_for(forum_memo_url(@forum, (@memo.parent_id.nil? ? @memo : @memo.parent_id))) + mail :to => recipients, + :subject => "[ #{l(:label_message_plural)} : #{memo.subject} #{l(:label_memo_create_succ)}]", + :filter => true + end + # Builds a Mail::Message object used to email recipients of the added journals for message. + + # 留言分为直接留言,和对留言人留言的回复 + # 字段说明在JournalsForMessage.rb + # 直接留言后 reply_id,m_parent_id 为空,相对应的at_user取值为nil + + def journals_for_message_add(user, journals_for_message) + @user = journals_for_message.user # 留言人 + @mail = journals_for_message.jour if journals_for_message.at_user.nil? # 留言 + @mail = journals_for_message.at_user if journals_for_message.at_user + @message = journals_for_message.notes + @title = "#@user #{t(:label_leave_your_message, :locale => 'zh')}" + @issue_author_url = url_for(user_activities_url(@user)) + @url = case journals_for_message.jour.class.to_s.to_sym # 判断留言的对象所属类型 + # when :Bid + # course_for_bid_url(journals_for_message.jour, anchor: "word_li_#{journals_for_message.id}") + when :Project + return -1 if journals_for_message.jour.project_type == Project::ProjectType_project + project_feedback_url(journals_for_message.jour, anchor: "word_li_#{journals_for_message.id}") + when :Course + course_feedback_url(journals_for_message.jour, anchor: "word_li_#{journals_for_message.id}") + when :Contest + show_contest_contest_url(journals_for_message.jour, anchor: "word_li_#{journals_for_message.id}") + when :User + user_newfeedback_user_url(journals_for_message.jour, anchor: "word_li_#{journals_for_message.id}") + else + Rails.logger.error "[Builds a Mail::Message ERROR] journalsForMessage's jour is unkown type, journalsForMessage.id = #{journals_for_message.id}" + return -1 + end + + # 验证用户的收取邮件的方式 + recipients ||= [] + recipients1 ||= [] + recipients1 = @mail.mail + recipients = journals_for_message.jour.author.mail + + # modify by nwb + #如果是直接留言并且留言对象是课程 + if !journals_for_message.at_user && journals_for_message.jour.class.to_s.to_sym == :Course + course = journals_for_message.jour + @author = journals_for_message.user + #课程的教师 + @members = course_all_member journals_for_message.jour + #收件人邮箱 + @recipients ||= [] + @members.each do |teacher| + @recipients << teacher.user.mail + end + mail :to => @recipients, + :subject => "#{l(:label_your_course)}#{journals_for_message.jour.name}#{l(:label_have_message)} ", + :filter => true + # elsif journals_for_message.jour.class.to_s.to_sym == :Bid + # if !journals_for_message.jour.author.notify_about? journals_for_message + # return -1 + # end + # + # mail :to => recipients, :subject => @title,:filter => true + elsif journals_for_message.jour.class.to_s.to_sym == :Contest + if !journals_for_message.jour.author.notify_about? journals_for_message + return -1 + end + mail :to => recipients, :subject => @title,:filter => true + else + mail :to => recipients1, :subject => @title,:filter => true + end + + + end + + # Builds a Mail::Message object used to email recipients of the added issue. + # + # Example: + # issue_add(issue) => Mail::Message object + # Mailer.issue_add(issue).deliver => sends an email to issue recipients + def issue_add(issue, recipients) + issue_id = issue.project_index + redmine_headers 'Project' => issue.project.identifier, + 'Issue-Id' => issue_id, + 'Issue-Author' => issue.author.login + redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to + message_id issue + + @author = issue.author + @issue = issue + user = User.find_by_mail(recipients) + @user = user + @token = Token.get_token_from_user(user, 'autologin') + @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id, :token => @token.value) + + # edit + @issue_author_url = url_for(user_activities_url(@author,:token => @token.value)) + @project_url = url_for(:controller => 'projects', :action => 'show', :id => issue.project_id, :token => @token.value) + + @user_url = url_for(my_account_url(user,:token => @token.value)) + + + subject = "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] (#{issue.status.name}) #{issue.subject}" + mail :to => recipients, + :subject => subject, + :filter => true + end + # issue.attachments.each do |attach| + # attachments["#{attach.filename}"] = File.read("#{attach.disk_filename}") + # end + # cc = issue.watcher_recipients - recipients + #mail.attachments['test'] = File.read("#{RAILS.root}/files/2015/01/150114094010_libegl.dll") + + + + + # Builds a Mail::Message object used to email recipients of the edited issue. + # + # Example: + # issue_edit(journal) => Mail::Message object + # Mailer.issue_edit(journal).deliver => sends an email to issue recipients + def issue_edit(journal,recipients) + issue = journal.journalized.reload + issue_id = issue.project_index + redmine_headers 'Project' => issue.project.identifier, + 'Issue-Id' => issue_id.to_s, + 'Issue-Author' => issue.author.login + redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to + message_id journal + references issue + @author = journal.user + + user = User.find_by_mail(recipients) + @user = user + @token = Token.get_token_from_user(user, 'autologin') + + + # edit + @issue_author_url = url_for(:controller => 'users', :action => 'show', :id => issue.author_id, :token => @token.value) + @project_url = url_for(:controller => 'projects', :action => 'show', :id => issue.project_id, :token => @token.value) + @user_url = url_for(my_account_url(user,:token => @token.value)) + + @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id, :anchor => "change-#{journal.id}", :token => @token.value) + s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] " + s << "(#{issue.status.name}) " if journal.new_value_for('status_id') + s << issue.subject + @issue = issue + @journal = journal + # @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}") + mail :to => recipients, + :subject => s, + :filter => true + end + + def self.deliver_mailer(to,cc, subject) + mail :to => to, + :cc => cc, + :subject => subject + end + + # 用户申请加入项目邮件通知 + def applied_project(applied) + @project =applied.project + redmine_headers 'Project' => @project, + 'User' => applied.user + @user = applied.user + recipients = @project.manager_recipients + s = l(:text_applied_project, :id => "##{@user.show_name}", :project => @project.name) + @applied_url = url_for(:controller => 'projects', :action => 'settings', :id => @project.id,:tab=>'members') + mail :to => recipients, + :subject => s + end + + def reminder(user, issues, days) + set_language_if_valid user.language + @issues = issues + @days = days + @issues_url = url_for(:controller => 'issues', :action => 'index', + :set_filter => 1, :assigned_to_id => user.id, + :sort => 'due_date:asc') + mail :to => user.mail, + :subject => l(:mail_subject_reminder, :count => issues.size, :days => days) + end + + #缺陷到期邮件通知 + def issue_expire issue + issue_id = issue.project_index + redmine_headers 'Project' => issue.project.identifier, + 'Issue-Id' => issue_id, + 'Issue-Author' => issue.author.login + redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to + message_id issue + @author = issue.author + @issue = issue + @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue.id) + recipients = issue.recipients + s = l(:text_issue_expire,:issue => "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] (#{issue.status.name}) #{issue.subject}") + mail :to => recipients, + :subject => s + ######################################################################################################### + #@issues = issues + #s = l(:text_issue_expire,:issue => "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] (#{issue.status.name}) #{issue.subject}") + #puts s + "////" + issue.assigned_to.mail + #@issues_url = url_for(:controller => 'issues', :action => 'show',:id => issue.id) + #mail :to => issue.assigned_to.mail, + # :subject => s + ######################################################################################################### + #issue_id = issue.project_index + #redmine_headers 'Project' => issue.project.identifier, + # 'Issue-Id' => issue_id, + # 'Issue-Author' => issue.author.login + #redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to + #message_id issue + #@author = issue.author + #@issue = issue + #@issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue) + #recipients = issue.recipients + #cc = issue.watcher_recipients - recipients + #mail :to => recipients, + # :cc => cc, + # :subject => "[#{issue.project.name} - #{issue.tracker.name} ##{issue_id}] (#{issue.status.name}) #{issue.subject}" + ###################################################################################################### + end + + + # Builds a Mail::Message object used to email users belonging to the added document's project. + # + # Example: + # document_added(document) => Mail::Message object + # Mailer.document_added(document).deliver => sends an email to the document's project recipients + def document_added(document) + redmine_headers 'Project' => document.project.identifier + @author = User.current + @document = document + @issue_author_url = url_for(user_activities_url(@author)) + @document_url = url_for(:controller => 'documents', :action => 'show', :id => document) + mail :to => document.recipients, + :subject => "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}", + :filter => true + end + + # Builds a Mail::Message object used to email recipients of a project when an attachements are added. + # + # Example: + # attachments_added(attachments) => Mail::Message object + # Mailer.attachments_added(attachments).deliver => sends an email to the project's recipients + def attachments_added(attachments) + container = attachments.first.container + added_to = '' + added_to_url = '' + @author = attachments.first.author + @issue_author_url = url_for(user_activities_url(@author)) + case container.class.name + when 'Project' + added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container) + added_to = "#{l(:label_project)}: #{container}" + recipients = container.notified_users.select { |user| user.allowed_to?(:view_files, container) }.collect { |u| u.mail } + when 'Course' + added_to_url = url_for(:controller => 'files', :action => 'index', :course_id => container) + added_to = "#{l(:label_course)}: #{container.name}" + recipients = container.notified_users.select { |user| user.allowed_to?(:view_files, container) }.collect { |u| u.mail } + when 'Version' + added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project) + added_to = "#{l(:label_version)}: #{container.name}" + recipients = container.project.notified_users.select { |user| user.allowed_to?(:view_files, container.project) }.collect { |u| u.mail } + when 'Document' + added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id) + added_to = "#{l(:label_document)}: #{container.title}" + recipients = container.recipients + end + if container.class.name == 'Course' + redmine_headers 'Course' => container.id + @attachments = attachments + @added_to = added_to + @added_to_url = added_to_url + mail :to => recipients, + :subject => "[#{container.name}] #{l(:label_attachment_new)}", + :filter => true + elsif container.class.name == 'Project' + redmine_headers 'Project' => container.id + @attachments = attachments + @added_to = added_to + @added_to_url = added_to_url + mail :to => recipients, + :subject => "[#{container.name}] #{l(:label_attachment_new)}", + :filter => true + else + redmine_headers 'Project' => container.project.identifier + @attachments = attachments + @added_to = added_to + @added_to_url = added_to_url + mail :to => recipients, + :subject => "[#{container.project.name}] #{l(:label_attachment_new)}", + :filter => true + end + end + + # Builds a Mail::Message object used to email recipients of a course when an homework are posted. + # + # Example: + # attachments_added(attachments) => Mail::Message object + # Mailer.attachments_added(attachments).deliver => sends an email to the project's recipients + def homework_added(homework_common) + @homework_common = homework_common + @author = homework_common.user + @homework_common_url = url_for(:controller => "homework_common", :action =>"index", :homework => @homework_common.id) + @homework_author_url = url_for(user_activities_url(@author)) + recipients ||= [] + #将帖子创建者邮箱地址加入数组 + @homework_common.course.members.each do |member| + recipients << member.user.mail + end + mail :to => recipients, + :subject => "[ #{l(:label_user_homework)} : #{homework_common.name} #{l(:label_memo_create_succ)}]", + :filter => true + end + + # Builds a Mail::Message object used to email recipients of a news' project when a news item is added. + # + # Example: + # news_added(news) => Mail::Message object + # Mailer.news_added(news).deliver => sends an email to the news' project recipients + def news_added(news) + + if news.project + redmine_headers 'Project' => news.project.identifier + @author = news.author + @issue_author_url = url_for(user_activities_url(@author)) + message_id news + @news = news + @news_url = url_for(:controller => 'news', :action => 'show', :id => news) + mail :to => news.recipients, + :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}", + :filter => true + elsif news.course + redmine_headers 'Course' => news.course.id + @author = news.author + @issue_author_url = url_for(user_activities_url(@author)) + message_id news + @news = news + recipients = news.course.notified_users.select { |user| user.allowed_to?(:view_files, news.course) }.collect { |u| u.mail } + @news_url = url_for(:controller => 'news', :action => 'show', :id => news) + mail :to => recipients, + :subject => "[#{news.course.name}] #{l(:label_news)}: #{news.title}", + :filter => true + end + end + + # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added. + # + # Example: + # news_comment_added(comment) => Mail::Message object + # Mailer.news_comment_added(comment) => sends an email to the news' project recipients + def news_comment_added(comment) + news = comment.commented + if news.project + redmine_headers 'Project' => news.project.identifier + @author = comment.author + @issue_author_url = url_for(user_activities_url(@author)) + message_id comment + @news = news + @comment = comment + @news_url = url_for(:controller => 'news', :action => 'show', :id => news) + mail :to => news.recipients, + :cc => news.watcher_recipients, + :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}", + :filter => true + elsif news.course + redmine_headers 'Course' => news.course.id + @author = comment.author + @issue_author_url = url_for(user_activities_url(@author)) + message_id comment + @news = news + @comment = comment + @news_url = url_for(:controller => 'news', :action => 'show', :id => news) + recipients = news.course.notified_users.select { |user| user.allowed_to?(:view_files, news.course) }.collect { |u| u.mail } + + mail :to => recipients, + :subject => "[#{news.course.name}] #{l(:label_news)}: #{news.title}", + :filter => true + end + end + + # Builds a Mail::Message object used to email the recipients of the specified message that was posted. + # + # Example: + # message_posted(message) => Mail::Message object + # Mailer.message_posted(message).deliver => sends an email to the recipients + def message_posted(message) + if message.project + redmine_headers 'Project' => message.project.identifier, + 'Topic-Id' => (message.parent_id || message.id) + @author = message.author + @issue_author_url = url_for(user_activities_url(@author)) + message_id message + references message.parent unless message.parent.nil? + recipients = message.recipients + cc = ((message.root.watcher_recipients + message.board.watcher_recipients).uniq - recipients) + @message = message + @message_url = url_for(message.event_url) + mail :to => recipients, + :cc => cc, + :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}", + :filter => true + elsif message.course + redmine_headers 'Course' => message.course.id, + 'Topic-Id' => (message.parent_id || message.id) + @author = message.author + @issue_author_url = url_for(user_activities_url(@author)) + message_id message + references message.parent unless message.parent.nil? + recipients = message.course.notified_users.select { |user| user.allowed_to?(:view_files, message.course) }.collect { |u| u.mail } + cc = ((message.root.watcher_recipients + message.board.watcher_recipients).uniq - recipients) + @message = message + @message_url = url_for(message.event_url) + mail :to => recipients, + :cc => cc, + :subject => "[#{message.board.course.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}", + :filter => true + end + end + + # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was added. + # + # Example: + # wiki_content_added(wiki_content) => Mail::Message object + # Mailer.wiki_content_added(wiki_content).deliver => sends an email to the project's recipients + def wiki_content_added(wiki_content) + redmine_headers 'Project' => wiki_content.project.identifier, + 'Wiki-Page-Id' => wiki_content.page.id + @author = wiki_content.author + message_id wiki_content + recipients = wiki_content.recipients + cc = wiki_content.page.wiki.watcher_recipients - recipients + @wiki_content = wiki_content + @wiki_content_url = url_for(:controller => 'wiki', :action => 'show', + :project_id => wiki_content.project, + :id => wiki_content.page.title) + mail :to => recipients, + :cc => cc, + :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}", + :filter => true + end + + # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated. + # + # Example: + # wiki_content_updated(wiki_content) => Mail::Message object + # Mailer.wiki_content_updated(wiki_content).deliver => sends an email to the project's recipients + def wiki_content_updated(wiki_content) + redmine_headers 'Project' => wiki_content.project.identifier, + 'Wiki-Page-Id' => wiki_content.page.id + @author = wiki_content.author + message_id wiki_content + recipients = wiki_content.recipients + cc = wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients + @wiki_content = wiki_content + @wiki_content_url = url_for(:controller => 'wiki', :action => 'show', + :project_id => wiki_content.project, + :id => wiki_content.page.title) + @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff', + :project_id => wiki_content.project, :id => wiki_content.page.title, + :version => wiki_content.version) + mail :to => recipients, + :cc => cc, + :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}", + :filter => true + end + + # Builds a Mail::Message object used to email the specified user their account information. + # + # Example: + # account_information(user, password) => Mail::Message object + # Mailer.account_information(user, password).deliver => sends account information to the user + def account_information(user, password) + set_language_if_valid user.language + @user = user + @password = password + @login_url = url_for(:controller => 'account', :action => 'login') + mail :to => user.mail, + :subject => l(:mail_subject_register, Setting.app_title) + end + + # Builds a Mail::Message object used to email all active administrators of an account activation request. + # + # Example: + # account_activation_request(user) => Mail::Message object + # Mailer.account_activation_request(user).deliver => sends an email to all active administrators + def account_activation_request(user) + # Send the email to all active administrators + recipients = User.active.where(:admin => true).all.collect { |u| u.mail }.compact + @user = user + @url = url_for(:controller => 'users', :action => 'index', + :status => User::STATUS_REGISTERED, + :sort_key => 'created_on', :sort_order => 'desc') + mail :to => recipients, + :subject => l(:mail_subject_account_activation_request, Setting.app_title) + end + + # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator. + # + # Example: + # account_activated(user) => Mail::Message object + # Mailer.account_activated(user).deliver => sends an email to the registered user + def account_activated(user) + set_language_if_valid user.language + @user = user + @login_url = url_for(:controller => 'account', :action => 'login') + mail :to => user.mail, + :subject => l(:mail_subject_register, Setting.app_title) + end + + def lost_password(token) + set_language_if_valid(token.user.language) + @token = token + @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value) + mail :to => token.user.mail, + :subject => l(:mail_subject_lost_password, Setting.app_title) + end + + def register(token) + set_language_if_valid(token.user.language) + @token = token + @url = url_for(:controller => 'account', :action => 'activate', :token => token.value) + mail :to => token.user.mail, + :subject => l(:mail_subject_register, Setting.app_title) + end + + def test_email(user) + set_language_if_valid(user.language) + @url = url_for(:controller => 'welcome') + mail :to => user.mail, + :subject => 'forge test' + end + + # Overrides default deliver! method to prevent from sending an email + # with no recipient, cc or bcc + def deliver!(mail = @mail) + set_language_if_valid @initial_language + return false if (recipients.nil? || recipients.empty?) && + (cc.nil? || cc.empty?) && + (bcc.nil? || bcc.empty?) + + + # Log errors when raise_delivery_errors is set to false, Rails does not + raise_errors = self.class.raise_delivery_errors + self.class.raise_delivery_errors = true + begin + return super(mail) + rescue Exception => e + if raise_errors + raise e + elsif mylogger + mylogger.error "The following error occured while sending email notification: \"#{e.message}\". Check your configuration in config/configuration.yml." + end + ensure + self.class.raise_delivery_errors = raise_errors + end + end + + # Sends reminders to issue assignees + # Available options: + # * :days => how many days in the future to remind about (defaults to 7) + # * :tracker => id of tracker for filtering issues (defaults to all trackers) + # * :project => id or identifier of project to process (defaults to all projects) + # * :users => array of user/group ids who should be reminded + def self.reminders(options={}) + days = options[:days] || 7 + project = options[:project] ? Project.find(options[:project]) : nil + tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil + user_ids = options[:users] + + scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" + + " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" + + " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date + ) + scope = scope.where(:assigned_to_id => user_ids) if user_ids.present? + scope = scope.where(:project_id => project.id) if project + scope = scope.where(:tracker_id => tracker.id) if tracker + + issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker).all.group_by(&:assigned_to) + issues_by_assignee.keys.each do |assignee| + if assignee.is_a?(Group) + assignee.users.each do |user| + issues_by_assignee[user] ||= [] + issues_by_assignee[user] += issues_by_assignee[assignee] + end + end + end + + issues_by_assignee.each do |assignee, issues| + reminder(assignee, issues, days).deliver if assignee.is_a?(User) && assignee.active? + end + end + + # Activates/desactivates email deliveries during +block+ + def self.with_deliveries(enabled = true, &block) + was_enabled = ActionMailer::Base.perform_deliveries + ActionMailer::Base.perform_deliveries = !!enabled + yield + ensure + ActionMailer::Base.perform_deliveries = was_enabled + end + + # Sends emails synchronously in the given block + def self.with_synched_deliveries(&block) + saved_method = ActionMailer::Base.delivery_method + if m = saved_method.to_s.match(%r{^async_(.+)$}) + synched_method = m[1] + ActionMailer::Base.delivery_method = synched_method.to_sym + ActionMailer::Base.send "#{synched_method}_settings=", ActionMailer::Base.send("async_#{synched_method}_settings") + end + yield + ensure + ActionMailer::Base.delivery_method = saved_method + end + + #过滤掉不是不合规则的收件人 + def filter(reps) + r_reps = [] + if reps.is_a? Array + reps.each do |r| + u = User.find_by_mail(r) + if u && u.mail_notification == 'all' + r_reps << r + end + end + elsif reps.is_a? String + u = User.find_by_mail(reps) + if u && u.mail_notification == 'all' + r_reps << reps + end + end + r_reps + end + + def mail(headers={}) + headers.merge! 'X-Mailer' => 'Redmine', + 'X-Redmine-Host' => Setting.host_name, + 'X-Redmine-Site' => Setting.app_title, + 'X-Auto-Response-Suppress' => 'OOF', + 'Auto-Submitted' => 'auto-generated', + 'From' => Setting.mail_from, + 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>" + + # Removes the author from the recipients and cc + # if he doesn't want to receive notifications about what he does + if @author && @author.logged? && @author.pref[:no_self_notified] + headers[:to].delete(@author.mail) if headers[:to].is_a?(Array) + headers[:cc].delete(@author.mail) if headers[:cc].is_a?(Array) + end + + if headers[:filter] + headers[:to] = filter(headers[:to]) + headers[:cc] = filter(headers[:cc]) + end + + if @author && @author.logged? + redmine_headers 'Sender' => @author.login + end + + # Blind carbon copy recipients + if Setting.bcc_recipients? + headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?) + headers[:to] = nil + headers[:cc] = nil + end + + if @message_id_object + headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>" + end + if @references_objects + headers[:references] = @references_objects.collect {|o| "<#{self.class.message_id_for(o)}>"}.join(' ') + end + + set_language_if_valid @initial_language + m = super headers do |format| + format.text + format.html unless Setting.plain_text_mail? + end + mylogger.debug "Sent a mail from #{m.from} to [#{m.to},#{m.cc}, #{m.bcc if Setting.bcc_recipients?}] subject: #{m.subject}" + m + end + + def initialize(*args) + @initial_language = current_language + set_language_if_valid Setting.default_language + super + end + + def self.deliver_mail(mail) + return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank? + Thread.new do + super + end + end + + def self.method_missing(method, *args, &block) + if m = method.to_s.match(%r{^deliver_(.+)$}) + ActiveSupport::Deprecation.warn "Mailer.deliver_#{m[1]}(*args) is deprecated. Use Mailer.#{m[1]}(*args).deliver instead." + send(m[1], *args).deliver + else + super + end + end + + + + private + + # Appends a Redmine header field (name is prepended with 'X-Redmine-') + def redmine_headers(h) + h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s } + end + + # Returns a predictable Message-Id for the given object + def self.message_id_for(object) + # id + timestamp should reduce the odds of a collision + # as far as we don't send multiple emails for the same object + timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on) + hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{timestamp.strftime("%Y%m%d%H%M%S")}" + host = Setting.mail_from.to_s.gsub(%r{^.*@}, '') + host = "#{::Socket.gethostname}.redmine" if host.empty? + "#{hash}@#{host}" + end + + def message_id(object) + @message_id_object = object + end + + def references(object) + @references_objects ||= [] + @references_objects << object + end + + def mylogger + if Setting.delayjob_enabled? + Delayed::Worker.logger + else + Rails.logger + end + end + + def add_attachments(obj) + if email.attachments && email.attachments.any? + email.attachments.each do |attachment| + obj.attachments << Attachment.create(:container => obj, + :file => attachment.decoded, + :filename => attachment.filename, + :author => user, + :content_type => attachment.mime_type) + end + end + end + + # author: alan + # 功能: 生成len位随机字符串 + def newpass(len) + chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a + newpass = "" + 1.upto(len) { |i| newpass << chars[rand(chars.size-1)] } + return newpass + end +end diff --git a/app/models/member.rb b/app/models/member.rb index f1a139f74..057ea9570 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -116,20 +116,33 @@ class Member < ActiveRecord::Base # 查找每个学生每个作业的评分 def student_homework_score - score_count = 0 - homework_score = HomeworkAttach.find_by_sql("SELECT bids.name, homework_attaches.score as score - FROM homework_attaches, bids where homework_attaches.user_id = #{self.user_id} - and homework_attaches.bid_id IN (SELECT bid_id FROM homework_for_courses where course_id = #{self.course_id}) - AND homework_attaches.bid_id = bids.id ") - homework_score.each do |homework| - mem_score = 0 - if homework[:score] - mem_score = homework[:score] - end - score_count = score_count + mem_score - end + homework_score = StudentWork.find_by_sql("SELECT homework_commons.name,student_works.final_score as score + FROM student_works,homework_commons + WHERE student_works.homework_common_id = homework_commons.id + AND homework_commons.course_id = #{self.course_id} + AND student_works.user_id = #{self.user_id}") + score_count = StudentWork.joins(:homework_common).where("student_works.user_id = #{self.user_id} and homework_commons.course_id = #{self.course_id}").average(:final_score).try(:round, 2).to_f [homework_score, format("%0.2f", score_count)] end + + def student_work_score + StudentWork.select("homework_commons.name, student_works.final_score").joins(:homework_common).where("student_works.user_id = #{self.user_id} and homework_commons.course_id = #{self.course_id}") + end + + #当前课程的作业列表 + def homework_common_list + HomeworkCommon.where(:course_id => self.course_id) + end + + #当前学生在指定作业内的得分 + def homework_common_score homework_common + StudentWork.select("final_score").where(:homework_common_id => homework_common.id,:user_id => self.user_id) + end + + def student_work_score_avg + StudentWork.joins(:homework_common).where("student_works.user_id = #{self.user_id} and homework_commons.course_id = #{self.course_id}").average(:final_score).try(:round, 2).to_f + end + protected def validate_role diff --git a/app/models/memo.rb b/app/models/memo.rb index cfc509923..2831effe3 100644 --- a/app/models/memo.rb +++ b/app/models/memo.rb @@ -1,168 +1,179 @@ -class Memo < ActiveRecord::Base - include Redmine::SafeAttributes - include UserScoreHelper - belongs_to :forum - belongs_to :author, :class_name => "User", :foreign_key => 'author_id' - - validates_presence_of :author_id, :forum_id, :subject,:content - # 若是主题帖,则内容可以是空 - #validates :content, presence: true, if: Proc.new{|o| !o.parent_id.nil? } - validates_length_of :subject, maximum: 50 - #validates_length_of :content, maximum: 3072 - validate :cannot_reply_to_locked_topic, :on => :create - - acts_as_tree :counter_cache => :replies_count, :order => "#{Memo.table_name}.created_at ASC" - acts_as_attachable - has_many :user_score_details, :class_name => 'UserScoreDetails',:as => :score_changeable_obj - has_many :praise_tread, as: :praise_tread_object, dependent: :destroy - belongs_to :last_reply, :class_name => 'Memo', :foreign_key => 'last_reply_id' - # acts_as_searchable :column => ['subject', 'content'], - # #:include => { :forum => :p} - # #:project_key => "#{Forum.table_name}.project_id" - # :date_column => "#{table_name}.created_at" - acts_as_event :title => Proc.new {|o| "#{o.forum.name}: #{o.subject}"}, - :datetime => :updated_at, - # :datetime => :created_at, - :description => :content, - :author => :author, - :type => Proc.new {|o| o.parent_id.nil? ? 'Memo' : 'Reply'}, - :url => Proc.new {|o| {:controller => 'memos', :action => 'show', :forum_id => o.forum_id}.merge(o.parent_id.nil? ? {:id => o.id} : {:id => o.parent_id, :r => o.id, :anchor => "reply-#{o.id}"})} - acts_as_activity_provider :author_key => :author_id, - :func => 'memos', - :timestamp => 'created_at' - # :find_options => {:type => 'memos'} - # acts_as_watchable - - safe_attributes "author_id", - "subject", - "content", - "forum_id", - "last_memo_id", - "lock", - "sticky", - "parent_id", - "replies_count" - - after_create :add_author_as_watcher, :reset_counters!#,:be_user_score -- 公共区发帖暂不计入得分 - # after_update :update_memos_forum - after_destroy :reset_counters!#,:down_user_score -- 公共区发帖暂不计入得分 - # after_create :send_notification - # after_save :plusParentAndForum - # after_destroy :minusParentAndForum - #before_save :be_user_score - # scope :visible, lambda { |*args| - # includes(:forum => ).where() - # } - - def cannot_reply_to_locked_topic - errors.add :base, l(:label_memo_locked) if root.locked? && self != root - end - - # def update_memos_forum - # if forum_id_changed? - # Message.update_all({:board_id => board_id}, ["id = ? OR parent_id = ?", root.id, root.id ]) - # Forum.reset_counters!(forum_id_was) - # Forum.reset_counters!(forum_id) - # end - # end - - def reset_counters! - if parent && parent.id - Memo.update_all({:last_reply_id => parent.children.maximum(:id)}, {:id => parent.id}) - parent.update_attribute(:updated_at, Time.now) - end - forum.reset_counters! - end - - def sticky? - sticky == 1 - end - - def replies - Memo.where("parent_id = ?", id) - end - - def locked? - self.lock - end - - def editable_by? user - # user && user.logged? || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)) - user.admin? || self.author == user - end - - def destroyable_by? user - (user && self.author == user) || user.admin? - #self.author == user || user.admin? - end - - def deleted_attach_able_by? user - (user && user.logged? && (self.author == user) ) || user.admin? - end - - private - - def add_author_as_watcher - Watcher.create(:watchable => self.root, :user => author) - end - - def send_notification - if Setting.notified_events.include?('message_posted') - Mailer.message_posted(self).deliver - end - end - - def plusParentAndForum - @forum = Forum.find(self.forum_id) - @forum.memo_count = @forum.memo_count.to_int + 1 - @forum.last_memo_id = self.id - if self.parent_id - @parent_memo = Memo.find_by_id(self.parent_id) - @parent_memo.last_reply_id = self - @parent_memo.replies_count = @parent_memo.replies_count.to_int + 1 - @parent_memo.save - else - @forum.topic_count = @forum.topic_count.to_int + 1 - end - @forum.save - end - - def minusParentAndForum - @forum = Forum.find(self.forum_id) - @forum.memo_count = @forum.memo_count.to_int - 1 - @forum.memo_count = 0 if @forum.memo_count.to_int < 0 - # @forum.last_memo_id = Memo.reorder('created_at ASC').find_all_by_forum_id(self.forum_id).last.id - if self.parent_id - @parent_memo = Memo.find_by_id(self.parent_id) - # @parent_memo.last_reply_id = Memo.reorder('created_at ASC').find_all_by_parent_id(self.parent_id).last.id - @parent_memo.replies_count = @parent_memo.replies_count.to_int - 1 - @parent_memo.replies_count = 0 if @parent_memo.replies_count.to_int < 0 - @parent_memo.save - else - @forum.topic_count = @forum.topic_count.to_int - 1 - @forum.topic_count = 0 if @forum.topic_count.to_int < 0 - end - @forum.save - end - - #更新用户分数 -by zjc - def be_user_score - #新建memo且无parent的为发帖 - if self.parent_id.nil? - UserScore.joint(:post_message, User.current,nil,self ,{ memo_id: self.id }) - update_memo_number(User.current,1) - - #新建memo且有parent的为回帖 - elsif !self.parent_id.nil? - UserScore.joint(:reply_posting, User.current,self.parent.author,self, { memo_id: self.id }) - update_replay_for_memo(User.current,1) - end - end - - #被删除时更新用户分数 - def down_user_score - update_memo_number(User.current,1) - update_replay_for_memo(User.current,1) - end - -end +class Memo < ActiveRecord::Base + include Redmine::SafeAttributes + include UserScoreHelper + include ApplicationHelper + belongs_to :forum + has_many_kindeditor_assets :assets, :dependent => :destroy + belongs_to :author, :class_name => "User", :foreign_key => 'author_id' + validates_presence_of :author_id, :forum_id, :subject,:content + # 若是主题帖,则内容可以是空 + #validates :content, presence: true, if: Proc.new{|o| !o.parent_id.nil? } + validates_length_of :subject, maximum: 50 + #validates_length_of :content, maximum: 3072 + validate :cannot_reply_to_locked_topic, :on => :create + + acts_as_tree :counter_cache => :replies_count, :order => "#{Memo.table_name}.created_at ASC" + acts_as_attachable + has_many :user_score_details, :class_name => 'UserScoreDetails',:as => :score_changeable_obj + has_many :praise_tread, as: :praise_tread_object, dependent: :destroy + belongs_to :last_reply, :class_name => 'Memo', :foreign_key => 'last_reply_id' + # acts_as_searchable :column => ['subject', 'content'], + # #:include => { :forum => :p} + # #:project_key => "#{Forum.table_name}.project_id" + # :date_column => "#{table_name}.created_at" + acts_as_event :title => Proc.new {|o| "#{o.forum.name}: #{o.subject}"}, + :datetime => :updated_at, + # :datetime => :created_at, + :description => :content, + :author => :author, + :type => Proc.new {|o| o.parent_id.nil? ? 'Memo' : 'Reply'}, + :url => Proc.new {|o| {:controller => 'memos', :action => 'show', :forum_id => o.forum_id}.merge(o.parent_id.nil? ? {:id => o.id} : {:id => o.parent_id, :r => o.id, :anchor => "reply-#{o.id}"})} + acts_as_activity_provider :author_key => :author_id, + :func => 'memos', + :timestamp => 'created_at' + # :find_options => {:type => 'memos'} + # acts_as_watchable + + safe_attributes "author_id", + "subject", + "content", + "forum_id", + "last_memo_id", + "lock", + "sticky", + "parent_id", + "replies_count" + + after_create :add_author_as_watcher, :reset_counters!, :send_mail + # after_update :update_memos_forum + after_destroy :reset_counters!,:delete_kindeditor_assets#,:down_user_score -- 公共区发帖暂不计入得分 + # after_create :send_notification + # after_save :plusParentAndForum + # after_destroy :minusParentAndForum + #before_save :be_user_score + # scope :visible, lambda { |*args| + # includes(:forum => ).where() + # } + + def send_mail + Mailer.run.forum_message_added(self) if Setting.notified_events.include?('forum_message_added') + end + + def cannot_reply_to_locked_topic + errors.add :base, l(:label_memo_locked) if root.locked? && self != root + end + + # def update_memos_forum + # if forum_id_changed? + # Message.update_all({:board_id => board_id}, ["id = ? OR parent_id = ?", root.id, root.id ]) + # Forum.reset_counters!(forum_id_was) + # Forum.reset_counters!(forum_id) + # end + # end + + def reset_counters! + if parent && parent.id + Memo.update_all({:last_reply_id => parent.children.maximum(:id)}, {:id => parent.id}) + parent.update_attribute(:updated_at, Time.now) + end + forum.reset_counters! + end + + def sticky? + sticky == 1 + end + + def replies + Memo.where("parent_id = ?", id) + end + + def locked? + self.lock + end + + def editable_by? user + # user && user.logged? || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)) + user.admin? || self.author == user + end + + def destroyable_by? user + (user && self.author == user) || user.admin? + #self.author == user || user.admin? + end + + def deleted_attach_able_by? user + (user && user.logged? && (self.author == user) ) || user.admin? + end + + private + + def add_author_as_watcher + Watcher.create(:watchable => self.root, :user => author) + end + + def send_notification + if Setting.notified_events.include?('message_posted') + Mailer.run.message_posted(self) + end + end + + def plusParentAndForum + @forum = Forum.find(self.forum_id) + @forum.memo_count = @forum.memo_count.to_int + 1 + @forum.last_memo_id = self.id + if self.parent_id + @parent_memo = Memo.find_by_id(self.parent_id) + @parent_memo.last_reply_id = self + @parent_memo.replies_count = @parent_memo.replies_count.to_int + 1 + @parent_memo.save + else + @forum.topic_count = @forum.topic_count.to_int + 1 + end + @forum.save + end + + def minusParentAndForum + @forum = Forum.find(self.forum_id) + @forum.memo_count = @forum.memo_count.to_int - 1 + @forum.memo_count = 0 if @forum.memo_count.to_int < 0 + # @forum.last_memo_id = Memo.reorder('created_at ASC').find_all_by_forum_id(self.forum_id).last.id + if self.parent_id + @parent_memo = Memo.find_by_id(self.parent_id) + # @parent_memo.last_reply_id = Memo.reorder('created_at ASC').find_all_by_parent_id(self.parent_id).last.id + @parent_memo.replies_count = @parent_memo.replies_count.to_int - 1 + @parent_memo.replies_count = 0 if @parent_memo.replies_count.to_int < 0 + @parent_memo.save + else + @forum.topic_count = @forum.topic_count.to_int - 1 + @forum.topic_count = 0 if @forum.topic_count.to_int < 0 + end + @forum.save + end + + #更新用户分数 -by zjc + def be_user_score + #新建memo且无parent的为发帖 + if self.parent_id.nil? + UserScore.joint(:post_message, User.current,nil,self ,{ memo_id: self.id }) + update_memo_number(User.current,1) + + #新建memo且有parent的为回帖 + elsif !self.parent_id.nil? + UserScore.joint(:reply_posting, User.current,self.parent.author,self, { memo_id: self.id }) + update_replay_for_memo(User.current,1) + end + end + + #被删除时更新用户分数 + def down_user_score + update_memo_number(User.current,1) + update_replay_for_memo(User.current,1) + end + + # Time 2015-03-26 15:20:24 + # Author lizanle + # Description 从硬盘上删除资源 + def delete_kindeditor_assets + delete_kindeditor_assets_from_disk self.id,OwnerTypeHelper::MEMO + end +end diff --git a/app/models/message.rb b/app/models/message.rb index 8af3265cc..15d358789 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -1,198 +1,237 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class Message < ActiveRecord::Base - include Redmine::SafeAttributes - include UserScoreHelper - - belongs_to :board - belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' - has_many :praise_tread, as: :praise_tread_object, dependent: :destroy - - acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC" - acts_as_attachable - belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id' - - # added by fq - has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy - # end - - acts_as_searchable :columns => ['subject', 'content'], - :include => {:board => :project}, - :project_key => "#{Board.table_name}.project_id", - :date_column => "#{table_name}.created_on" - acts_as_searchable :columns => ['subject', 'content'], - :include => {:board => :course}, - :course_key => "#{Board.table_name}.course_id", - :date_column => "#{table_name}.created_at" - acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"}, - :description => :content, - :datetime => :updated_on, - # :datetime => "#{Message.table_name}.created_on", - :group => :parent, - :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'}, - :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} : - {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})} - - acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]}, - :author_key => :author_id - acts_as_activity_provider :find_options => {:include => [{:board => :course}, :author]}, - :type => 'course_messages', - :author_key => :author_id - acts_as_watchable - - validates_presence_of :board, :subject, :content - validates_length_of :subject, :maximum => 255 - validate :cannot_reply_to_locked_topic, :on => :create - - after_create :add_author_as_watcher, :reset_counters! - after_update :update_messages_board - after_destroy :reset_counters!,:down_user_score - - # fq - after_create :act_as_activity,:be_user_score - #before_save :be_user_score - # end - - scope :visible, lambda {|*args| - includes(:board => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args)) - } - - scope :course_visible, lambda {|*args| - includes(:board => :course).where(Course.allowed_to_condition(args.shift || User.current, :view_course_messages, *args)) - } - - - safe_attributes 'subject', 'content' - safe_attributes 'board_id','locked', 'sticky', - :if => lambda {|message, user| - if message.project - user.allowed_to?(:edit_messages, message.project) - else - user.allowed_to?(:edit_messages, message.course) - end - } - - def visible?(user=User.current) - if project - !user.nil? && user.allowed_to?(:view_messages, project) - elsif course - !user.nil? && user.allowed_to?(:view_messages, course) - end - end - - def cannot_reply_to_locked_topic - # Can not reply to a locked topic - errors.add :base, 'Topic is locked' if root.locked? && self != root - end - - def update_messages_board - if board_id_changed? - Message.update_all({:board_id => board_id}, ["id = ? OR parent_id = ?", root.id, root.id]) - Board.reset_counters!(board_id_was) - Board.reset_counters!(board_id) - end - end - - def reset_counters! - if parent && parent.id - Message.update_all({:last_reply_id => parent.children.maximum(:id)}, {:id => parent.id}) - end - board.reset_counters! - end - - def sticky=(arg) - write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0) - end - - def sticky? - sticky == 1 - end - - def project - board.project - end - - def course - board.course - end - - def course_editable_by?(usr) - usr && usr.logged? && (usr.allowed_to?(:edit_messages, course) || (self.author == usr && usr.allowed_to?(:edit_own_messages, course))) - end - - def course_destroyable_by?(usr) - usr && usr.logged? && (usr.allowed_to?(:delete_messages, course) || (self.author == usr && usr.allowed_to?(:delete_own_messages, course))) - end - - def editable_by?(usr) - usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project))) - end - - def destroyable_by?(usr) - usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project))) - end - - private - - def add_author_as_watcher - Watcher.create(:watchable => self.root, :user => author) - end - - # fq - def act_as_activity - self.acts << Activity.new(:user_id => self.author_id) - end - # end - - #更新用户分数 -by zjc - def be_user_score - #新建message且无parent的为发帖 - - if self.parent_id.nil? && !self.board.project.nil? - UserScore.joint(:post_message, self.author,nil,self, { message_id: self.id }) - update_memo_number(self.author,1) - if self.board.project_id != -1 - update_memo_number(self.author,2,self.board.project) - end - #新建message且有parent的为回帖 - elsif !self.parent_id.nil? && !self.board.project.nil? - UserScore.joint(:reply_posting, self.author,self.parent.author,self, { message_id: self.id }) - update_replay_for_memo(self.author,1) - if self.board.project_id != -1 - update_replay_for_memo(self.author,2,self.board.project) - end - end - end - - #减少用户分数 - def down_user_score - if self.parent_id.nil? && !self.board.project.nil? - UserScore.joint(:delete_message, self.author,nil,self, { message_id: self.id }) - update_memo_number(User.current,1) - if self.board.project_id != -1 - update_memo_number(self.author,2,self.board.project) - end - elsif !self.parent_id.nil? && !self.board.project.nil? - UserScore.joint(:reply_deleting, self.author,self.parent.author,self, { message_id: self.id }) - update_replay_for_memo(User.current,1) - if self.board.project_id != -1 - update_replay_for_memo(self.author,2,self.board.project) - end - end - end -end +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class Message < ActiveRecord::Base + include Redmine::SafeAttributes + include UserScoreHelper + include ApplicationHelper + has_many_kindeditor_assets :assets, :dependent => :destroy + belongs_to :board + belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' + has_many :praise_tread, as: :praise_tread_object, dependent: :destroy + + acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC" + acts_as_attachable + belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id' + + # added by fq + has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy + # 被ForgeActivity虚拟关联 + has_many :forge_acts, :class_name => 'ForgeActivity',:as =>:forge_act ,:dependent => :destroy + # end + + has_many :ActivityNotifies,:as => :activity, :dependent => :destroy + + acts_as_searchable :columns => ['subject', 'content'], + :include => {:board => :project}, + :project_key => "#{Board.table_name}.project_id", + :date_column => "#{table_name}.created_on" + acts_as_searchable :columns => ['subject', 'content'], + :include => {:board => :course}, + :course_key => "#{Board.table_name}.course_id", + :date_column => "#{table_name}.created_at" + acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"}, + :description => :content, + :datetime => :updated_on, + # :datetime => "#{Message.table_name}.created_on", + :group => :parent, + :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'}, + :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} : + {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})} + + acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]}, + :author_key => :author_id + acts_as_activity_provider :find_options => {:include => [{:board => :course}, :author]}, + :type => 'course_messages', + :author_key => :author_id + acts_as_watchable + + validates_presence_of :board, :subject, :content + validates_length_of :subject, :maximum => 255 + validate :cannot_reply_to_locked_topic, :on => :create + + after_create :add_author_as_watcher, :reset_counters! + after_update :update_messages_board + after_destroy :reset_counters!,:down_user_score,:delete_kindeditor_assets + + after_create :act_as_activity,:be_user_score,:act_as_forge_activity, :send_mail + #before_save :be_user_score + + scope :visible, lambda {|*args| + includes(:board => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args)) + } + + scope :course_visible, lambda {|*args| + includes(:board => :course).where(Course.allowed_to_condition(args.shift || User.current, :view_course_messages, *args)) + } + + + safe_attributes 'subject', 'content' + safe_attributes 'board_id','locked', 'sticky', + :if => lambda {|message, user| + if message.project + user.allowed_to?(:edit_messages, message.project) + else + user.allowed_to?(:edit_messages, message.course) + end + } + + def visible?(user=User.current) + if project + !user.nil? && user.allowed_to?(:view_messages, project) + elsif course + !user.nil? && user.allowed_to?(:view_messages, course) + end + end + + def cannot_reply_to_locked_topic + # Can not reply to a locked topic + errors.add :base, 'Topic is locked' if root.locked? && self != root + end + + def update_messages_board + if board_id_changed? + Message.update_all({:board_id => board_id}, ["id = ? OR parent_id = ?", root.id, root.id]) + Board.reset_counters!(board_id_was) + Board.reset_counters!(board_id) + end + end + + def reset_counters! + if parent && parent.id + Message.update_all({:last_reply_id => parent.children.maximum(:id)}, {:id => parent.id}) + end + board.reset_counters! + end + + def sticky=(arg) + write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0) + end + + def sticky? + sticky == 1 + end + + def project + board.project + end + + def course + board.course + end + + def course_editable_by?(usr) + usr && usr.logged? && (usr.allowed_to?(:edit_messages, course) || (self.author == usr && usr.allowed_to?(:edit_own_messages, course))) + end + + def course_destroyable_by?(usr) + usr && usr.logged? && (usr.allowed_to?(:delete_messages, course) || (self.author == usr && usr.allowed_to?(:delete_own_messages, course))) + end + + def editable_by?(usr) + usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project))) + end + + def destroyable_by?(usr) + usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project))) + end + + def set_notify_id(notify_id) + @notify_id= notify_id + end + def get_notify_id() + return @notify_id + end + def set_notify_is_read(notify_is_read) + @notify_is_read = notify_is_read + end + def get_notify_is_read() + return @notify_is_read + end + + private + + def add_author_as_watcher + Watcher.create(:watchable => self.root, :user => author) + end + + # fq + def act_as_activity + self.acts << Activity.new(:user_id => self.author_id) + end + # end + + # Time 2015-02-27 14:32:25 + # Author lizanle + # Description + def act_as_forge_activity + # 如果project为空,那么就是课程相关的消息 + if !self.board.project.nil? + self.forge_acts << ForgeActivity.new(:user_id => self.author_id, + :project_id => self.board.project.id) + end + end + + #更新用户分数 -by zjc + def be_user_score + #新建message且无parent的为发帖 + + if self.parent_id.nil? && !self.board.project.nil? + UserScore.joint(:post_message, self.author,nil,self, { message_id: self.id }) + update_memo_number(self.author,1) + if self.board.project_id != -1 + update_memo_number(self.author,2,self.board.project) + end + #新建message且有parent的为回帖 + elsif !self.parent_id.nil? && !self.board.project.nil? + UserScore.joint(:reply_posting, self.author,self.parent.author,self, { message_id: self.id }) + update_replay_for_memo(self.author,1) + if self.board.project_id != -1 + update_replay_for_memo(self.author,2,self.board.project) + end + end + end + + #减少用户分数 + def down_user_score + if self.parent_id.nil? && !self.board.project.nil? + UserScore.joint(:delete_message, self.author,nil,self, { message_id: self.id }) + update_memo_number(User.current,1) + if self.board.project_id != -1 + update_memo_number(self.author,2,self.board.project) + end + elsif !self.parent_id.nil? && !self.board.project.nil? + UserScore.joint(:reply_deleting, self.author,self.parent.author,self, { message_id: self.id }) + update_replay_for_memo(User.current,1) + if self.board.project_id != -1 + update_replay_for_memo(self.author,2,self.board.project) + end + end + end + + def send_mail + Mailer.run.message_posted(self) if Setting.notified_events.include?('message_posted') + end + + # Time 2015-03-31 09:15:06 + # Author lizanle + # Description 删除对应消息的图片资源 + def delete_kindeditor_assets + delete_kindeditor_assets_from_disk self.id,OwnerTypeHelper::MESSAGE + end + +end diff --git a/app/models/message_observer.rb b/app/models/message_observer.rb deleted file mode 100644 index 383301664..000000000 --- a/app/models/message_observer.rb +++ /dev/null @@ -1,25 +0,0 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class MessageObserver < ActiveRecord::Observer - def after_create(message) - ##by senluo - thread5=Thread.new do - Mailer.message_posted(message).deliver if Setting.notified_events.include?('message_posted') - end - end -end diff --git a/app/models/news.rb b/app/models/news.rb index 4d153e81f..7e809cbfd 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -1,94 +1,120 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class News < ActiveRecord::Base - include Redmine::SafeAttributes - belongs_to :project - #added by nwb - belongs_to :course - belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' - has_many :comments, :as => :commented, :dependent => :delete_all, :order => "created_on" - # fq - has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy - #end - - validates_presence_of :title, :description - validates_length_of :title, :maximum => 60 - validates_length_of :summary, :maximum => 255 - validates_length_of :description, :maximum => 10000 - - acts_as_attachable :delete_permission => :manage_news - acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"], :include => :project - acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}} - acts_as_activity_provider :find_options => {:include => [:project, :author]}, - :author_key => :author_id - #added by nwb - #课程新闻独立于项目 - acts_as_activity_provider :type => 'course_news', - :find_options => {:include => [:course, :author]}, - :author_key => :author_id - acts_as_watchable - - after_create :add_author_as_watcher - # fq - after_create :act_as_activity - # end - - scope :visible, lambda {|*args| - includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_news, *args)) - } - - scope :course_visible, lambda {|*args| - includes(:course).where(Course.allowed_to_condition(args.shift || User.current, :view_course_news, *args)) - } - safe_attributes 'title', 'summary', 'description' - - def visible?(user=User.current) - !user.nil? && user.allowed_to?(:view_news, project) - end - - # Returns true if the news can be commented by user - def commentable?(user=User.current) - user.allowed_to?(:comment_news, project) - end - - def recipients - project.users.select {|user| user.notify_about?(self)}.map(&:mail) - end - - # returns latest news for projects visible by user - def self.latest(user = User.current, count = 5) - visible(user).includes([:author, :project]).order("#{News.table_name}.created_on DESC").limit(count).all - end - - # 新闻的短描述信息 - def short_description(length = 255) - description.gsub(/<\/?.*?>/,"").html_safe if description - #description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description - #description - end - - private - - def add_author_as_watcher - Watcher.create(:watchable => self, :user => author) - end - ## fq - def act_as_activity - self.acts << Activity.new(:user_id => self.author_id) - end -end +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class News < ActiveRecord::Base + include Redmine::SafeAttributes + belongs_to :project + include ApplicationHelper + has_many_kindeditor_assets :assets, :dependent => :destroy + #added by nwb + belongs_to :course + belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' + has_many :comments, :as => :commented, :dependent => :delete_all, :order => "created_on" + # fq + has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy + # 被ForgeActivity虚拟关联 + has_many :forge_acts, :class_name => 'ForgeActivity',:as =>:forge_act ,:dependent => :destroy + # end + + validates_presence_of :title, :description + validates_length_of :title, :maximum => 60 + validates_length_of :summary, :maximum => 255 + validates_length_of :description, :maximum => 10000 + + acts_as_attachable :delete_permission => :manage_news + acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"], :include => :project + acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}} + acts_as_activity_provider :find_options => {:include => [:project, :author]}, + :author_key => :author_id + #added by nwb + #课程新闻独立于项目 + acts_as_activity_provider :type => 'course_news', + :find_options => {:include => [:course, :author]}, + :author_key => :author_id + acts_as_watchable + + after_create :act_as_activity,:act_as_forge_activity,:add_author_as_watcher, :send_mail + + after_destroy :delete_kindeditor_assets + + scope :visible, lambda {|*args| + includes(:project).where(Project.allowed_to_condition(args.shift || User.current, :view_news, *args)) + } + + scope :course_visible, lambda {|*args| + includes(:course).where(Course.allowed_to_condition(args.shift || User.current, :view_course_news, *args)) + } + safe_attributes 'title', 'summary', 'description' + + def visible?(user=User.current) + !user.nil? && user.allowed_to?(:view_news, project) + end + + # Returns true if the news can be commented by user + def commentable?(user=User.current) + user.allowed_to?(:comment_news, project) + end + + def recipients + project.users.select {|user| user.notify_about?(self)}.map(&:mail) + end + + # returns latest news for projects visible by user + def self.latest(user = User.current, count = 5) + visible(user).includes([:author, :project]).order("#{News.table_name}.created_on DESC").limit(count).all + end + + # 新闻的短描述信息 + def short_description(length = 255) + description.gsub(/<\/?.*?>/,"").html_safe if description + #description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description + #description + end + + private + + def add_author_as_watcher + Watcher.create(:watchable => self, :user => author) + end + ## fq + def act_as_activity + self.acts << Activity.new(:user_id => self.author_id) + end + + # Time 2015-02-27 15:48:17 + # Author lizanle + # Description 公用表中也要记录 + def act_as_forge_activity + # 如果是project为空,那么是课程相关的,不需要保存 + if !self.project.nil? + self.forge_acts << ForgeActivity.new(:user_id => self.author_id, + :project_id => self.project.id) + end + end + + # Time 2015-03-31 13:50:54 + # Author lizanle + # Description 删除news后删除对应的资源 + def delete_kindeditor_assets + delete_kindeditor_assets_from_disk self.id,OwnerTypeHelper::NEWS + end + + def send_mail + Mailer.run.news_added(self) if Setting.notified_events.include?('news_added') + end + +end diff --git a/app/models/news_observer.rb b/app/models/news_observer.rb deleted file mode 100644 index 8b9bc7b4b..000000000 --- a/app/models/news_observer.rb +++ /dev/null @@ -1,25 +0,0 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class NewsObserver < ActiveRecord::Observer - def after_create(news) - ##by senluo - thread6=Thread.new do - Mailer.news_added(news).deliver if Setting.notified_events.include?('news_added') - end - end -end diff --git a/app/models/organization.rb b/app/models/organization.rb new file mode 100644 index 000000000..5f52dee98 --- /dev/null +++ b/app/models/organization.rb @@ -0,0 +1,5 @@ +class Organization < ActiveRecord::Base + attr_accessible :logo_link, :name + + has_many :projects +end diff --git a/app/models/phone_app_version.rb b/app/models/phone_app_version.rb new file mode 100644 index 000000000..92a32c0d3 --- /dev/null +++ b/app/models/phone_app_version.rb @@ -0,0 +1,6 @@ +class PhoneAppVersion < ActiveRecord::Base + attr_accessible :description, :version + validates_presence_of :description, :version + validates_uniqueness_of :version + acts_as_attachable +end diff --git a/app/models/poll.rb b/app/models/poll.rb index 06f1369c1..64e9df79a 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -6,4 +6,25 @@ class Poll < ActiveRecord::Base has_many :poll_questions, :dependent => :destroy,:order => "#{PollQuestion.table_name}.question_number" has_many :poll_users, :dependent => :destroy has_many :users, :through => :poll_users #该文件被哪些用户提交答案过 + # 添加课程的poll动态 + has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy + after_create :act_as_activity + + acts_as_event :title => Proc.new {|o| "#{l(:label_course_poll)}: #{o.polls_name}" }, + :description => :polls_description, + :datetime => :published_at, + :author => :user, + :url => Proc.new {|o| {:controller => 'poll', :action => 'show', :id => o.id}} + + acts_as_activity_provider :type => 'polls', + #:permission => :view_course_polls, + :find_options => {:select => "#{Poll.table_name}.*", + :joins => "LEFT JOIN #{Course.table_name} ON ( #{Poll.table_name}.polls_type='Course' AND #{Poll.table_name}.polls_status= 2 AND #{Poll.table_name}.polls_group_id = #{Course.table_name}.id )"}, + :timestamp => "#{self.table_name}.published_at", + :author_key => :user_id + + def act_as_activity + self.acts << Activity.new(:user_id => self.user_id) + end + end diff --git a/app/models/principal.rb b/app/models/principal.rb index 5aa21768f..0f0746f78 100644 --- a/app/models/principal.rb +++ b/app/models/principal.rb @@ -19,6 +19,7 @@ class Principal < ActiveRecord::Base self.table_name = "#{table_name_prefix}users#{table_name_suffix}" # Account statuses + # 0 全部;1 活动的; 2 已注册; 3 锁定 STATUS_ANONYMOUS = 0 STATUS_ACTIVE = 1 STATUS_REGISTERED = 2 @@ -42,7 +43,8 @@ class Principal < ActiveRecord::Base where({}) else pattern = "%#{q}%" - sql = %w(login firstname lastname mail).map {|column| "LOWER(#{table_name}.#{column}) LIKE LOWER(:p)"}.join(" OR ") + # sql = %w(login firstname lastname mail).map {|column| "LOWER(#{table_name}.#{column}) LIKE LOWER(:p)"}.join(" OR ") + sql= "LOWER(concat(lastname,firstname)) LIKE LOWER(:p) or LOWER(login) LIKE LOWER(:p) or LOWER(mail) LIKE LOWER(:p)" params = {:p => pattern} if q =~ /^(.+)\s+(.+)$/ a, b = "#{$1}%", "#{$2}%" diff --git a/app/models/project.rb b/app/models/project.rb index 14763347f..f201e6e3c 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -30,7 +30,7 @@ class Project < ActiveRecord::Base # Specific overidden Activities - belongs_to :homework_attach + has_many :student_works has_many :time_entry_activities has_many :members, :include => [:principal, :roles], :conditions => "#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{Principal::STATUS_ACTIVE}" has_many :memberships, :class_name => 'Member' @@ -41,7 +41,7 @@ class Project < ActiveRecord::Base has_many :principals, :through => :member_principals, :source => :principal has_many :enabled_modules, :dependent => :delete_all has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position" - has_many :issues, :dependent => :destroy, :include => [:status, :tracker],:order => "id ASC" + has_many :issues, :dependent => :destroy, :include => [:status, :tracker],:order => "issues.id ASC" has_many :issue_changes, :through => :issues, :source => :journals has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC" has_many :time_entries, :dependent => :delete_all @@ -51,7 +51,7 @@ class Project < ActiveRecord::Base has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name" has_many :boards, :dependent => :destroy, :order => "position ASC" has_one :repository, :conditions => ["is_default = ?", true] - has_many :repositories, :dependent => :destroy + has_many :repositories, :dependent => :destroy, conditions: "hidden=false" has_many :changesets, :through => :repository #added by xianbo for delete biding_project has_many :biding_projects, :dependent => :destroy @@ -67,7 +67,7 @@ class Project < ActiveRecord::Base has_many :student, :through => :students_for_courses, :source => :user has_one :course_extra, :class_name => 'Course', :foreign_key => :extra,:primary_key => :identifier, :dependent => :destroy has_many :applied_projects - + has_many :invite_lists # end #ADDED BY NIE @@ -89,7 +89,9 @@ class Project < ActiveRecord::Base :association_foreign_key => 'custom_field_id' has_many :tags, :through => :project_tags, :class_name => 'Tag' - has_many :project_tags, :class_name => 'ProjectTags' + has_many :project_tags, :class_name => 'ProjectTags' + + belongs_to :organization # has_many :journals @@ -112,10 +114,11 @@ class Project < ActiveRecord::Base validates_presence_of :name, :identifier validates_uniqueness_of :identifier - validates_uniqueness_of :name - validates_associated :repository, :wiki - # validates_length_of :description, :maximum => 255 + # validates_uniqueness_of :name + validates_associated :wiki#, :repository + # validates_length_of :description, :maximum => 255 validates_length_of :name, :maximum => 255 + validates_length_of :enterprise_name, :maximum => 255 validates_length_of :homepage, :maximum => 255 validates_length_of :identifier, :in => 1..IDENTIFIER_MAX_LENGTH # donwcase letters, digits, dashes but not digits only @@ -125,9 +128,10 @@ class Project < ActiveRecord::Base #此代码功能:为原redmine中项目的树形结构按名称首字母排序,本系统项目非树形结构,且项目排序方式无按首字母排序,另该代码执行会使空数据库时创建项目时出异常故注释掉 #after_save :update_position_under_parent, :if => Proc.new {|project| project.name_changed?} + #ActiveModel::Dirty 这里有一个changed方法。对任何对象都可以用 after_save :update_inherited_members, :if => Proc.new {|project| project.inherit_members_changed?} # 创建project之后默认创建一个board,之后的board去掉了board的概念 - after_create :create_board_sync + after_create :create_board_sync,:acts_as_forge_activities before_destroy :delete_all_members def remove_references_before_destroy return if self.id.nil? @@ -760,7 +764,8 @@ class Project < ActiveRecord::Base 'issue_custom_field_ids', 'project_type', 'dts_test', - 'attachmenttype' + 'attachmenttype', + 'enterprise_name' @@ -834,7 +839,7 @@ class Project < ActiveRecord::Base # Yields the given block for each project with its level in the tree def self.project_tree(projects, &block) ancestors = [] - projects.sort_by(&:lft).each do |project| + projects.sort_by(&:id).each do |project| while (ancestors.any? && !project.is_descendant_of?(ancestors.last)) ancestors.pop end @@ -1148,7 +1153,14 @@ class Project < ActiveRecord::Base logger.error "[Project Model] ===> Auto create board when Project saved, because #{@board.full_messages}" end end - - + + # Time 2015-03-10 15:33:16 + # Author lizanle + # Description 新建项目要在ForgeActivities中加一条数据。 + def acts_as_forge_activities + fa = ForgeActivity.new(:user_id => User.current.id,:project_id => self.id, + :forge_act_id => self.id,:forge_act_type => "ProjectCreateInfo") + fa.save! + end end diff --git a/app/models/relative_memo.rb b/app/models/relative_memo.rb index f087fce2b..07767c7d6 100644 --- a/app/models/relative_memo.rb +++ b/app/models/relative_memo.rb @@ -1,199 +1,199 @@ -class RelativeMemo < ActiveRecord::Base - # attr_accessible :title, :body - include Redmine::SafeAttributes - belongs_to :open_source_project, :class_name => "OpenSourceProject", :foreign_key => 'osp_id' - belongs_to :author, :class_name => "User", :foreign_key => 'author_id' - - has_many :tags, :through => :project_tags, :class_name => 'Tag' - has_many :project_tags, :class_name => 'ProjectTags' - - has_many :relation_topics, :class_name => 'RelativeMemoToOpenSourceProject' - - has_many :no_uses, :as => :no_use, :dependent => :delete_all - - has_many :bugs_to_osp, :class_name => 'BugToOsp', :foreign_key => 'relative_memo_id', :dependent => :destroy - - - acts_as_taggable - - validates_presence_of :subject - #validates :content, presence: true - # validates_length_of :subject, maximum: 50 - #validates_length_of :content, maximum: 3072 - validate :cannot_reply_to_locked_topic, :on => :create - validates_uniqueness_of :osp_id, :scope => [:subject, :content] - - acts_as_tree :counter_cache => :replies_count, :order => "#{RelativeMemo.table_name}.created_at ASC" - acts_as_attachable - belongs_to :last_reply, :class_name => 'RelativeMemo', :foreign_key => 'last_reply_id' - # acts_as_searchable :column => ['subject', 'content'], - # #:include => { :forum => :p} - # #:project_key => "#{Forum.table_name}.project_id" - # :date_column => "#{table_name}.created_at" - - # acts_as_event :title => Proc.new {|o| "#{o.forum.name}: #{o.subject}"}, - # :datetime => :updated_at, - # # :datetime => :created_at, - # :description => :content, - # :author => :author, - # :type => Proc.new {|o| o.parent_id.nil? ? 'Memo' : 'Reply'}, - # :url => Proc.new {|o| {:controller => 'memos', :action => 'show', :forum_id => o.forum_id}.merge(o.parent_id.nil? ? {:id => o.id} : {:id => o.parent_id, :r => o.id, :anchor => "reply-#{o.id}"})} - # acts_as_activity_provider :author_key => :author_id, - # :func => 'memos', - # :timestamp => 'created_at' - - # :find_options => {:type => 'memos'} - # acts_as_watchable - - safe_attributes "author_id", - "subject", - "content", - "osp_id", - "last_memo_id", - "lock", - "sticky", - "parent_id", - "replies_count", - "is_quote" - - after_create :add_author_as_watcher, :reset_counters! - # after_update :update_memos_forum - after_destroy :reset_counters! - # after_create :send_notification - # after_save :plusParentAndForum - # after_destroy :minusParentAndForum - - # scope :visible, lambda { |*args| - # includes(:forum => ).where() - # } - - def cannot_reply_to_locked_topic - errors.add :base, l(:label_memo_locked) if root.locked? && self != root - end - - def short_content(length = 25) - str = "^(.{,#{length}})[^\n\r]*.*$" - content.gsub(Regexp.new(str), '\1...').strip if content - end - - # def update_memos_forum - # if forum_id_changed? - # Message.update_all({:board_id => board_id}, ["id = ? OR parent_id = ?", root.id, root.id ]) - # Forum.reset_counters!(forum_id_was) - # Forum.reset_counters!(forum_id) - # end - # end - - - scope :no_use_for, lambda { |user_id| - { :include => :no_uses, - :conditions => ["#{NoUse.table_name}.user_id = ?", user_id] } - } - - # 获取帖子的回复 - def replies - memos = RelativeMemo.where("parent_id = ?", id) - end - - def no_use_for?(user) - self.no_uses.each do |no_use| - if no_use.user_id == user.id - return true - end - end - false - end - - def set_no_use(user, flag=true) - flag ? set_filter(user) : remove_filter(user) - end - - def set_filter(user) - self.no_uses << NoUse.new(:user => user) - end - - def remove_filter(user) - return nil unless user && user.is_a?(User) - NoUse.delete_all "no_use_type = '#{self.class}' AND no_use_id = #{self.id} AND user_id = #{user.id}" - end - - def reset_counters! - if parent && parent.id - RelativeMemo.update_all({:last_reply_id => parent.children.maximum(:id)}, {:id => parent.id}) - parent.update_attribute(:updated_at, Time.now) - end - # forum.reset_counters! - end - - def sticky? - sticky == 1 - end - - def replies - RelativeMemo.where("parent_id = ?", id) - end - - def locked? - self.lock - end - - def editable_by? user - # user && user.logged? || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)) - user.admin? - end - - # def destroyable_by? user - # (user && user.logged? && (Forum.find(self.forum_id).creator_id == user.id) ) || user.admin? - # #self.author == user || user.admin? - # end - - def deleted_attach_able_by? user - (user && user.logged? && (self.author == user) ) || user.admin? - end - - private - - def add_author_as_watcher - Watcher.create(:watchable => self.root, :user => author) - end - - def send_notification - if Setting.notified_events.include?('message_posted') - Mailer.message_posted(self).deliver - end - end - - # def plusParentAndForum - # @forum = Forum.find(self.forum_id) - # @forum.memo_count = @forum.memo_count.to_int + 1 - # @forum.last_memo_id = self.id - # if self.parent_id - # @parent_memo = Memo.find_by_id(self.parent_id) - # @parent_memo.last_reply_id = self - # @parent_memo.replies_count = @parent_memo.replies_count.to_int + 1 - # @parent_memo.save - # else - # @forum.topic_count = @forum.topic_count.to_int + 1 - # end - # @forum.save - # end - - # def minusParentAndForum - # @forum = Forum.find(self.forum_id) - # @forum.memo_count = @forum.memo_count.to_int - 1 - # @forum.memo_count = 0 if @forum.memo_count.to_int < 0 - # # @forum.last_memo_id = Memo.reorder('created_at ASC').find_all_by_forum_id(self.forum_id).last.id - # if self.parent_id - # @parent_memo = Memo.find_by_id(self.parent_id) - # # @parent_memo.last_reply_id = Memo.reorder('created_at ASC').find_all_by_parent_id(self.parent_id).last.id - # @parent_memo.replies_count = @parent_memo.replies_count.to_int - 1 - # @parent_memo.replies_count = 0 if @parent_memo.replies_count.to_int < 0 - # @parent_memo.save - # else - # @forum.topic_count = @forum.topic_count.to_int - 1 - # @forum.topic_count = 0 if @forum.topic_count.to_int < 0 - # end - # @forum.save - # end -end - +class RelativeMemo < ActiveRecord::Base + # attr_accessible :title, :body + include Redmine::SafeAttributes + belongs_to :open_source_project, :class_name => "OpenSourceProject", :foreign_key => 'osp_id' + belongs_to :author, :class_name => "User", :foreign_key => 'author_id' + + has_many :tags, :through => :project_tags, :class_name => 'Tag' + has_many :project_tags, :class_name => 'ProjectTags' + + has_many :relation_topics, :class_name => 'RelativeMemoToOpenSourceProject' + + has_many :no_uses, :as => :no_use, :dependent => :delete_all + + has_many :bugs_to_osp, :class_name => 'BugToOsp', :foreign_key => 'relative_memo_id', :dependent => :destroy + + + acts_as_taggable + + validates_presence_of :subject + #validates :content, presence: true + # validates_length_of :subject, maximum: 50 + #validates_length_of :content, maximum: 3072 + validate :cannot_reply_to_locked_topic, :on => :create + validates_uniqueness_of :osp_id, :scope => [:subject, :content] + + acts_as_tree :counter_cache => :replies_count, :order => "#{RelativeMemo.table_name}.created_at ASC" + acts_as_attachable + belongs_to :last_reply, :class_name => 'RelativeMemo', :foreign_key => 'last_reply_id' + # acts_as_searchable :column => ['subject', 'content'], + # #:include => { :forum => :p} + # #:project_key => "#{Forum.table_name}.project_id" + # :date_column => "#{table_name}.created_at" + + # acts_as_event :title => Proc.new {|o| "#{o.forum.name}: #{o.subject}"}, + # :datetime => :updated_at, + # # :datetime => :created_at, + # :description => :content, + # :author => :author, + # :type => Proc.new {|o| o.parent_id.nil? ? 'Memo' : 'Reply'}, + # :url => Proc.new {|o| {:controller => 'memos', :action => 'show', :forum_id => o.forum_id}.merge(o.parent_id.nil? ? {:id => o.id} : {:id => o.parent_id, :r => o.id, :anchor => "reply-#{o.id}"})} + # acts_as_activity_provider :author_key => :author_id, + # :func => 'memos', + # :timestamp => 'created_at' + + # :find_options => {:type => 'memos'} + # acts_as_watchable + + safe_attributes "author_id", + "subject", + "content", + "osp_id", + "last_memo_id", + "lock", + "sticky", + "parent_id", + "replies_count", + "is_quote" + + after_create :add_author_as_watcher, :reset_counters! + # after_update :update_memos_forum + after_destroy :reset_counters! + # after_create :send_notification + # after_save :plusParentAndForum + # after_destroy :minusParentAndForum + + # scope :visible, lambda { |*args| + # includes(:forum => ).where() + # } + + def cannot_reply_to_locked_topic + errors.add :base, l(:label_memo_locked) if root.locked? && self != root + end + + def short_content(length = 25) + str = "^(.{,#{length}})[^\n\r]*.*$" + content.gsub(Regexp.new(str), '\1...').strip if content + end + + # def update_memos_forum + # if forum_id_changed? + # Message.update_all({:board_id => board_id}, ["id = ? OR parent_id = ?", root.id, root.id ]) + # Forum.reset_counters!(forum_id_was) + # Forum.reset_counters!(forum_id) + # end + # end + + + scope :no_use_for, lambda { |user_id| + { :include => :no_uses, + :conditions => ["#{NoUse.table_name}.user_id = ?", user_id] } + } + + # 获取帖子的回复 + def replies + memos = RelativeMemo.where("parent_id = ?", id) + end + + def no_use_for?(user) + self.no_uses.each do |no_use| + if no_use.user_id == user.id + return true + end + end + false + end + + def set_no_use(user, flag=true) + flag ? set_filter(user) : remove_filter(user) + end + + def set_filter(user) + self.no_uses << NoUse.new(:user => user) + end + + def remove_filter(user) + return nil unless user && user.is_a?(User) + NoUse.delete_all "no_use_type = '#{self.class}' AND no_use_id = #{self.id} AND user_id = #{user.id}" + end + + def reset_counters! + if parent && parent.id + RelativeMemo.update_all({:last_reply_id => parent.children.maximum(:id)}, {:id => parent.id}) + parent.update_attribute(:updated_at, Time.now) + end + # forum.reset_counters! + end + + def sticky? + sticky == 1 + end + + def replies + RelativeMemo.where("parent_id = ?", id) + end + + def locked? + self.lock + end + + def editable_by? user + # user && user.logged? || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)) + user.admin? + end + + # def destroyable_by? user + # (user && user.logged? && (Forum.find(self.forum_id).creator_id == user.id) ) || user.admin? + # #self.author == user || user.admin? + # end + + def deleted_attach_able_by? user + (user && user.logged? && (self.author == user) ) || user.admin? + end + + private + + def add_author_as_watcher + Watcher.create(:watchable => self.root, :user => author) + end + + def send_notification + if Setting.notified_events.include?('message_posted') + Mailer.run.message_posted(self) + end + end + + # def plusParentAndForum + # @forum = Forum.find(self.forum_id) + # @forum.memo_count = @forum.memo_count.to_int + 1 + # @forum.last_memo_id = self.id + # if self.parent_id + # @parent_memo = Memo.find_by_id(self.parent_id) + # @parent_memo.last_reply_id = self + # @parent_memo.replies_count = @parent_memo.replies_count.to_int + 1 + # @parent_memo.save + # else + # @forum.topic_count = @forum.topic_count.to_int + 1 + # end + # @forum.save + # end + + # def minusParentAndForum + # @forum = Forum.find(self.forum_id) + # @forum.memo_count = @forum.memo_count.to_int - 1 + # @forum.memo_count = 0 if @forum.memo_count.to_int < 0 + # # @forum.last_memo_id = Memo.reorder('created_at ASC').find_all_by_forum_id(self.forum_id).last.id + # if self.parent_id + # @parent_memo = Memo.find_by_id(self.parent_id) + # # @parent_memo.last_reply_id = Memo.reorder('created_at ASC').find_all_by_parent_id(self.parent_id).last.id + # @parent_memo.replies_count = @parent_memo.replies_count.to_int - 1 + # @parent_memo.replies_count = 0 if @parent_memo.replies_count.to_int < 0 + # @parent_memo.save + # else + # @forum.topic_count = @forum.topic_count.to_int - 1 + # @forum.topic_count = 0 if @forum.topic_count.to_int < 0 + # end + # @forum.save + # end +end + diff --git a/app/models/student_work.rb b/app/models/student_work.rb new file mode 100644 index 000000000..e2be18423 --- /dev/null +++ b/app/models/student_work.rb @@ -0,0 +1,12 @@ +#学生提交作品表 +class StudentWork < ActiveRecord::Base + attr_accessible :name, :description, :homework_common_id, :user_id, :final_score, :teacher_score, :student_score, :teaching_asistant_score, :project_id + + belongs_to :homework_common + belongs_to :user + has_many :student_works_evaluation_distributions, :dependent => :destroy + has_many :student_works_scores, :dependent => :destroy + belongs_to :project + + acts_as_attachable +end diff --git a/app/models/student_works_evaluation_distribution.rb b/app/models/student_works_evaluation_distribution.rb new file mode 100644 index 000000000..60f786af8 --- /dev/null +++ b/app/models/student_works_evaluation_distribution.rb @@ -0,0 +1,7 @@ +#学生作品匿评分配表 +class StudentWorksEvaluationDistribution < ActiveRecord::Base + attr_accessible :student_work_id, :user_id + + belongs_to :student_work + belongs_to :user +end diff --git a/app/models/student_works_score.rb b/app/models/student_works_score.rb new file mode 100644 index 000000000..8fa14f8de --- /dev/null +++ b/app/models/student_works_score.rb @@ -0,0 +1,10 @@ +class StudentWorksScore < ActiveRecord::Base + #reviewer_role: 1:教师评分;2:教辅评分;3:学生匿评 + attr_accessible :student_work_id, :user_id, :score, :comment, :reviewer_role + + belongs_to :user + belongs_to :student_work + has_many :journals_for_messages, :as => :jour, :dependent => :destroy + + acts_as_attachable +end diff --git a/app/models/token.rb b/app/models/token.rb index 3131bce8d..e0accb59c 100644 --- a/app/models/token.rb +++ b/app/models/token.rb @@ -1,3 +1,4 @@ +#coding=utf-8 # Redmine - project management software # Copyright (C) 2006-2013 Jean-Philippe Lang # @@ -14,7 +15,7 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - +# class Token < ActiveRecord::Base belongs_to :user validates_uniqueness_of :value @@ -27,6 +28,24 @@ class Token < ActiveRecord::Base self.value = Token.generate_token_value end + def self.get_or_create_permanent_login_token(user) + token = Token.get_token_from_user(user, 'autologin') + unless token + token = Token.create(:user => user, :action => 'autologin') + else + token.update_attribute(:created_on, Time.now) + end + token + end + + def self.get_token_from_user(user, action) + token = Token.where(:action => action, :user_id => user).first + unless token + token = Token.create(user: user, action: action) + end + token + end + # Return true if token has expired def expired? return Time.now > self.created_on + @@validity_time @@ -34,7 +53,7 @@ class Token < ActiveRecord::Base # Delete all expired tokens def self.destroy_expired - Token.delete_all ["action NOT IN (?) AND created_on < ?", ['feeds', 'api'], Time.now - @@validity_time] + Token.delete_all ["action NOT IN (?) AND created_on < ?", ['feeds', 'api', 'autologin'], Time.now - @@validity_time] end # Returns the active user who owns the key for the given action @@ -72,6 +91,10 @@ class Token < ActiveRecord::Base Redmine::Utils.random_hex(20) end + def self.delete_user_all_tokens(user) + Token.delete_all(user_id: user.id) + end + private # Removes obsolete tokens (same user and action) diff --git a/app/models/tracker.rb b/app/models/tracker.rb index 6a0069975..af56d23bf 100644 --- a/app/models/tracker.rb +++ b/app/models/tracker.rb @@ -16,7 +16,9 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class Tracker < ActiveRecord::Base - + # Time 2015-02-6 09:34:44 + # Author lizanle + # Description freeze方法让字符串不可变 CORE_FIELDS_UNDISABLABLE = %w(project_id tracker_id subject description priority_id is_private).freeze # Fields that can be disabled # Other (future) fields should be appended, not inserted! diff --git a/app/models/user.rb b/app/models/user.rb index 735b80762..b4723cceb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,1025 +1,1032 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -require "digest/sha1" - -class User < Principal - TEACHER = 0 - STUDENT = 1 - ENTERPRISE = 2 - DEVELOPER = 3 - - include Redmine::SafeAttributes - - # Different ways of displaying/sorting users - USER_FORMATS = { - :firstname_lastname => { - :string => '#{firstname} #{lastname}', - :order => %w(firstname lastname id), - :setting_order => 1 - }, - :firstname_lastinitial => { - :string => '#{firstname} #{lastname.to_s.chars.first}.', - :order => %w(firstname lastname id), - :setting_order => 2 - }, - :firstname => { - :string => '#{firstname}', - :order => %w(firstname id), - :setting_order => 3 - }, - :lastname_firstname => { - :string => '#{lastname} #{firstname}', - :order => %w(lastname firstname id), - :setting_order => 4 - }, - :lastname_coma_firstname => { - :string => '#{lastname}, #{firstname}', - :order => %w(lastname firstname id), - :setting_order => 5 - }, - :lastname => { - :string => '#{lastname}', - :order => %w(lastname id), - :setting_order => 6 - }, - :username => { - :string => '#{login}', - :order => %w(login id), - :setting_order => 7 - }, - } - - MAIL_NOTIFICATION_OPTIONS = [ - ['all', :label_user_mail_option_all], - ['selected', :label_user_mail_option_selected], - ['only_my_events', :label_user_mail_option_only_my_events], - ['only_assigned', :label_user_mail_option_only_assigned], - ['only_owner', :label_user_mail_option_only_owner], - ['none', :label_user_mail_option_none] - ] - - has_many :homework_users - has_many :homework_attaches, :through => :homework_users - has_many :homework_evaluations - - #问卷相关关关系 - has_many :poll_users, :dependent => :destroy - has_many :poll_votes, :dependent => :destroy - has_many :poll, :dependent => :destroy #用户创建的问卷 - has_many :answers, :source => :poll, :through => :poll_users, :dependent => :destroy #用户已经完成问答的问卷 - # end - - has_and_belongs_to_many :groups, :after_add => Proc.new {|user, group| group.user_added(user)}, - :after_remove => Proc.new {|user, group| group.user_removed(user)} - has_many :changesets, :dependent => :nullify - has_one :preference, :dependent => :destroy, :class_name => 'UserPreference' - has_one :rss_token, :class_name => 'Token', :conditions => "action='feeds'" - has_one :api_token, :class_name => 'Token', :conditions => "action='api'" - belongs_to :auth_source - belongs_to :ucourse, :class_name => 'Course', :foreign_key => :id #huang -## added by xianbo for delete - has_many :biding_projects, :dependent => :destroy - has_many :contesting_projects, :dependent => :destroy - belongs_to :softapplication, :foreign_key => 'id', :dependent => :destroy -##ended by xianbo - -#####fq - has_many :jours, :class_name => 'JournalsForMessage', :dependent => :destroy - has_many :journals_messages, :class_name => 'JournalsForMessage', :foreign_key => "user_id", :dependent => :destroy - has_many :bids, :foreign_key => 'author_id', :dependent => :destroy - has_many :contests, :foreign_key => 'author_id', :dependent => :destroy - has_many :softapplications, :foreign_key => 'user_id', :dependent => :destroy - has_many :journals_for_messages, :as => :jour, :dependent => :destroy - has_many :new_jours, :as => :jour, :class_name => 'JournalsForMessage', :conditions => "status=1" - has_many :journal_replies, :dependent => :destroy - has_many :activities, :dependent => :destroy - has_many :students_for_courses - #has_many :courses, :through => :students_for_courses, :source => :project - has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy - has_many :file_commit, :class_name => 'Attachment', :foreign_key => 'author_id', :conditions => "container_type = 'Project' or container_type = 'Version'" -#### -# added by bai - has_many :join_in_contests, :dependent => :destroy - has_many :news, :foreign_key => 'author_id' - has_many :contestnotification, :foreign_key => 'author_id' - has_many :comments, :foreign_key => 'author_id' - has_many :notificationcomments, :foreign_key => 'author_id' - has_many :wiki_contents, :foreign_key => 'author_id' - has_many :journals - has_many :messages, :foreign_key => 'author_id' - has_one :user_score, :dependent => :destroy - has_many :documents # 项目中关联的文档再次与人关联 -# end - -######added by nie - has_many :project_infos, :dependent => :destroy - has_one :user_status, :dependent => :destroy - ##### - has_many :shares ,:dependent => :destroy - - # add by zjc - has_one :level, :class_name => 'UserLevels', :dependent => :destroy - has_many :memos , :foreign_key => 'author_id' - ##### - scope :logged, lambda { where("#{User.table_name}.status <> #{STATUS_ANONYMOUS}") } - scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) } - scope :visible, lambda {|*args| - nil - } - - - acts_as_customizable - ############################added by william - acts_as_taggable - scope :by_join_date, order("created_on DESC") - ############################# added by liuping 关注 - acts_as_watchable - seems_rateable_rater - has_one :user_extensions,:dependent => :destroy - ## end - - # default_scope -> { includes(:user_extensions, :user_score) } - scope :teacher, -> { - joins(:user_extensions).where('user_extensions.identity = ?', UserExtensions::TEACHER) - } - scope :student, -> { - joins(:user_extensions).where('user_extensions.identity = ?', UserExtensions::STUDENT) - } - scope :developer, -> { - joins(:user_extensions).where('user_extensions.identity = ?', UserExtensions::DEVELOPER) - } - scope :enterprise, -> { - joins(:user_extensions).where('user_extensions.identity = ?', UserExtensions::ENTERPRISE) - } - - attr_accessor :password, :password_confirmation - attr_accessor :last_before_login_on - # Prevents unauthorized assignments - attr_protected :login, :admin, :password, :password_confirmation, :hashed_password - - LOGIN_LENGTH_LIMIT = 25 - MAIL_LENGTH_LIMIT = 60 - - validates_presence_of :login, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) } - validates_uniqueness_of :login, :if => Proc.new { |user| user.login_changed? && user.login.present? }, :case_sensitive => false - validates_uniqueness_of :mail, :if => Proc.new { |user| user.mail_changed? && user.mail.present? }, :case_sensitive => false - # Login must contain letters, numbers, underscores only - validates_format_of :login, :with => /\A[a-z0-9_\-@\.]*\z/i - validates_length_of :login, :maximum => LOGIN_LENGTH_LIMIT - validates_length_of :firstname, :maximum => 30 - validates_length_of :lastname, :maximum => 30 - validates_format_of :mail, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, :allow_blank => true - validates_length_of :mail, :maximum => MAIL_LENGTH_LIMIT, :allow_nil => true - validates_confirmation_of :password, :allow_nil => true - validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true - validate :validate_password_length - - before_create :set_mail_notification - before_save :update_hashed_password - before_destroy :remove_references_before_destroy - # added by fq - after_create :act_as_activity - # end - - scope :in_group, lambda {|group| - group_id = group.is_a?(Group) ? group.id : group.to_i - where("#{User.table_name}.id IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id) - } - scope :not_in_group, lambda {|group| - group_id = group.is_a?(Group) ? group.id : group.to_i - where("#{User.table_name}.id NOT IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id) - } - scope :sorted, lambda { order(*User.fields_for_order_statement)} - - scope :like, lambda {|arg, type| - if arg.blank? - where(nil) - else - pattern = "%#{arg.to_s.strip.downcase}%" - #where(" LOWER(concat(lastname, firstname)) LIKE :p ", :p => pattern) - if type == "0" - where(" LOWER(login) LIKE '#{pattern}' ") - elsif type == "1" - where(" LOWER(concat(lastname, firstname)) LIKE '#{pattern}' ") - else - where(" LOWER(mail) LIKE '#{pattern}' ") - end - end - } - - - # ====================================================================== - - def extensions - self.user_extensions ||= UserExtensions.new - end - - def user_score_attr - self.user_score ||= UserScore.new - end - - # ====================================================================== - - #选择项目成员时显示的用户信息文字 - def userInfo - if self.realname.gsub(' ','') == "" || self.realname.nil? - info = self.nickname; - else - info=self.nickname + ' (' + self.realname + ')'; - end - info - end - - ###添加留言 fq - def add_jour(user, notes, reference_user_id = 0, options = {}) - if options.count == 0 - self.journals_for_messages << JournalsForMessage.new(:user_id => user.id, :notes => notes, :reply_id => reference_user_id, :status => true) - else - jfm = self.journals_for_messages.build(options) - jfm.save - jfm - end - end - - # 判断用户是否加入了竞赛中 fq - def join_in_contest?(bid) - joined = JoinInContest.where('user_id = ? and bid_id =?', self.id, bid.id) - if joined.size > 0 - true - else - false - end - end - - ### fq - def join_in?(course) - joined = StudentsForCourse.where('student_id = ? and course_id = ?', self.id, course.id) - if joined.size > 0 - true - else - false - end - end - - def show_name - unless self.user_extensions.nil? - if self.user_extensions.identity == 2 - firstname - else - lastname+firstname - end - else - lastname+firstname - end - end - ## end - - def count_new_jour - count = self.new_jours.count - end - - #added by nie - def count_new_journal_reply - count = self.journal_reply.count - end - - def set_mail_notification - ##add byxianbo - thread=Thread.new do - self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? - true - end - end - - def update_hashed_password - # update hashed_password if password was set - if self.password && self.auth_source_id.blank? - salt_password(password) - end - end - - alias :base_reload :reload - def reload(*args) - @name = nil - @projects_by_role = nil - @courses_by_role = nil - @membership_by_project_id = nil - base_reload(*args) - end - - def mail=(arg) - write_attribute(:mail, arg.to_s.strip) - end - - def identity_url=(url) - if url.blank? - write_attribute(:identity_url, '') - else - begin - write_attribute(:identity_url, OpenIdAuthentication.normalize_identifier(url)) - rescue OpenIdAuthentication::InvalidOpenId - # Invalid url, don't save - end - end - self.read_attribute(:identity_url) - end - - VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i - # VALID_EMAIL_REGEX = /^[0-9a-zA-Z_-]+@[0-9a-zA-Z_-]+(\.[0-9a-zA-Z_-]+)+$/ - # Returns the user that matches provided login and password, or nil - #登录,返回用户名与密码匹配的用户 - def self.try_to_login(login, password) - login = login.to_s.lstrip.rstrip - password = password.to_s - - # Make sure no one can sign in with an empty login or password - return nil if login.empty? || password.empty? - if (login =~ VALID_EMAIL_REGEX) - user = find_by_mail(login) - else - user = find_by_login(login) - end - if user - # user is already in local database - #return nil unless user.active? - return nil unless user.check_password?(password) - else - # user is not yet registered, try to authenticate with available sources - attrs = AuthSource.authenticate(login, password) - if attrs - user = new(attrs) - user.login = login - user.language = Setting.default_language - if user.save - user.reload - logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source - end - end - end - if user && !user.new_record? - last_login_on = user.last_login_on.nil? ? '' : user.last_login_on.to_s - user.update_column(:last_login_on, Time.now) - end - [user, last_login_on] - rescue => text - raise text - end - - # Returns the user who matches the given autologin +key+ or nil - def self.try_to_autologin(key) - user = Token.find_active_user('autologin', key, Setting.autologin.to_i) - if user - user.update_column(:last_login_on, Time.now) - user - end - end - - def self.name_formatter(formatter = nil) - USER_FORMATS[formatter || Setting.user_format] || USER_FORMATS[:firstname_lastname] - end - - # Returns an array of fields names than can be used to make an order statement for users - # according to how user names are displayed - # Examples: - # - # User.fields_for_order_statement => ['users.login', 'users.id'] - # User.fields_for_order_statement('authors') => ['authors.login', 'authors.id'] - def self.fields_for_order_statement(table=nil) - table ||= table_name - name_formatter[:order].map {|field| "#{table}.#{field}"} - end - - # Return user's full name for display - def realname(formatter = nil) - f = self.class.name_formatter(formatter) - if formatter - eval('"' + f[:string] + '"') - else - @name ||= eval('"' + f[:string] + '"') - end - end - - def nickname(formatter = nil) - login - end - - def name(formatter = nil) - login - end - - def active? - self.status == STATUS_ACTIVE - end - - def registered? - self.status == STATUS_REGISTERED - end - - def locked? - self.status == STATUS_LOCKED - end - - def activate - self.status = STATUS_ACTIVE - end - - def register - self.status = STATUS_REGISTERED - end - - def lock - self.status = STATUS_LOCKED - end - - def activate! - update_attribute(:status, STATUS_ACTIVE) - end - - def register! - update_attribute(:status, STATUS_REGISTERED) - end - - def lock! - update_attribute(:status, STATUS_LOCKED) - end - - # Returns true if +clear_password+ is the correct user's password, otherwise false - def check_password?(clear_password) - if auth_source_id.present? - auth_source.authenticate(self.login, clear_password) - else - User.hash_password("#{salt}#{User.hash_password clear_password}") == hashed_password - end - end - - # Generates a random salt and computes hashed_password for +clear_password+ - # The hashed password is stored in the following form: SHA1(salt + SHA1(password)) - def salt_password(clear_password) - self.salt = User.generate_salt - self.hashed_password = User.hash_password("#{salt}#{User.hash_password clear_password}") - end - - # Does the backend storage allow this user to change their password? - def change_password_allowed? - return true if auth_source.nil? - return auth_source.allow_password_changes? - end - - # Generate and set a random password. Useful for automated user creation - # Based on Token#generate_token_value - # - def random_password - chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a - password = '' - 40.times { |i| password << chars[rand(chars.size-1)] } - self.password = password - self.password_confirmation = password - self - end - - def pref - self.preference ||= UserPreference.new(:user => self) - end - - def time_zone - @time_zone ||= (self.pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[self.pref.time_zone]) - end - - def wants_comments_in_reverse_order? - self.pref[:comments_sorting] == 'desc' - end - - def wants_notificationcomments_in_reverse_order? - self.pref[:notificationcomments_sorting] == 'desc' - end - # Return user's RSS key (a 40 chars long string), used to access feeds - def rss_key - if rss_token.nil? - create_rss_token(:action => 'feeds') - end - rss_token.value - end - - # Return user's API key (a 40 chars long string), used to access the API - def api_key - if api_token.nil? - create_api_token(:action => 'api') - end - api_token.value - end - - # Return an array of project ids for which the user has explicitly turned mail notifications on - def notified_projects_ids - @notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id) - end - - def notified_project_ids=(ids) - Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id]) - Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty? - @notified_projects_ids = nil - notified_projects_ids - end - - def valid_notification_options - self.class.valid_notification_options(self) - end - - # Only users that belong to more than 1 project can select projects for which they are notified - def self.valid_notification_options(user=nil) - # Note that @user.membership.size would fail since AR ignores - # :include association option when doing a count - if user.nil? || user.memberships.length < 1 - MAIL_NOTIFICATION_OPTIONS.reject {|option| option.first == 'selected'} - else - MAIL_NOTIFICATION_OPTIONS - end - end - - # Find a user account by matching the exact login and then a case-insensitive - # version. Exact matches will be given priority. - #通过用户名查找相应的用户,若没有匹配到,则不区分大小写进行查询 - #修改:不再匹配不区分大小写情况 -zjc - def self.find_by_login(login) - if login.present? - login = login.to_s - # First look for an exact match - user = where(:login => login).all.detect {|u| u.login == login} - #unless user - # # Fail over to case-insensitive if none was found - # user = where("LOWER(login) = ?", login.downcase).first - #end - user - end - end - - def self.find_by_rss_key(key) - Token.find_active_user('feeds', key) - end - - def self.find_by_api_key(key) - Token.find_active_user('api', key) - end - - # Makes find_by_mail case-insensitive - def self.find_by_mail(mail) - where("LOWER(mail) = ?", mail.to_s.downcase).first - end - - # Returns true if the default admin account can no longer be used - def self.default_admin_account_changed? - !User.active.find_by_login("admin").try(:check_password?, "admin") - end - - def to_s - name - end - - CSS_CLASS_BY_STATUS = { - STATUS_ANONYMOUS => 'anon', - STATUS_ACTIVE => 'active', - STATUS_REGISTERED => 'registered', - STATUS_LOCKED => 'locked' - } - - def css_classes - "user #{CSS_CLASS_BY_STATUS[status]}" - end - - # Returns the current day according to user's time zone - def today - if time_zone.nil? - Date.today - else - Time.now.in_time_zone(time_zone).to_date - end - end - - # Returns the day of +time+ according to user's time zone - def time_to_date(time) - if time_zone.nil? - time.to_date - else - time.in_time_zone(time_zone).to_date - end - end - - def logged? - true - end - - def anonymous? - !logged? - end - - # Returns user's membership for the given project - # or nil if the user is not a member of project - def membership(project) - project_id = project.is_a?(Project) ? project.id : project - - @membership_by_project_id ||= Hash.new {|h, project_id| - h[project_id] = memberships.where(:project_id => project_id).first - } - @membership_by_project_id[project_id] - end - - def coursemembership(course) - course_id = course.is_a?(Course) ? course.id : course - - @membership_by_course_id ||= Hash.new {|h, course_id| - h[course_id] = coursememberships.where(:course_id => course_id).first - } - @membership_by_course_id[course_id] - end - - # Return user's roles for project - def roles_for_project(project) - roles = [] - # No role on archived projects - return roles if project.nil? || project.archived? - if logged? - # Find project membership - membership = membership(project) - if membership - roles = membership.roles - else - @role_non_member ||= Role.non_member - roles << @role_non_member - end - else - @role_anonymous ||= Role.anonymous - roles << @role_anonymous - end - roles - end - - # 用户课程权限判断 - def roles_for_course(course) - roles = [] - # No role on archived courses - return roles if course.nil? || course.archived? - if logged? - # Find course membership - membership = coursemembership(course) - if membership - roles = membership.roles - else - @role_non_member ||= Role.non_member - roles << @role_non_member - end - else - @role_anonymous ||= Role.anonymous - roles << @role_anonymous - end - roles - end - - # Return true if the user is a member of project - def member_of?(project) - projects.to_a.include?(project) - end - - def member_of_course?(course) - courses.to_a.include?(course) - end - - def member_of_course_group?(course_group) - course_groups.to_a.include?(course_group) - end - # Returns a hash of user's projects grouped by roles - def projects_by_role - return @projects_by_role if @projects_by_role - - @projects_by_role = Hash.new([]) - memberships.each do |membership| - if membership.project - membership.roles.each do |role| - @projects_by_role[role] = [] unless @projects_by_role.key?(role) - @projects_by_role[role] << membership.project - end - end - end - @projects_by_role.each do |role, projects| - projects.uniq! - end - - @projects_by_role - end - - # 课程的角色权限 - def courses_by_role - return @courses_by_role if @courses_by_role - - @courses_by_role = Hash.new([]) - coursememberships.each do |membership| - if membership.course - membership.roles.each do |role| - @courses_by_role[role] = [] unless @courses_by_role.key?(role) - @courses_by_role[role] << membership.course - end - end - end - @courses_by_role.each do |role, courses| - courses.uniq! - end - - @courses_by_role - end - # Returns true if user is arg or belongs to arg - def is_or_belongs_to?(arg) - if arg.is_a?(User) - self == arg - elsif arg.is_a?(Group) - arg.users.include?(self) - else - false - end - end - - - # Return true if the user is allowed to do the specified action on a specific context - # Action can be: - # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') - # * a permission Symbol (eg. :edit_project) - # Context can be: - # * a project : returns true if user is allowed to do the specified action on this project - # * an array of projects : returns true if user is allowed on every project - # * nil with options[:global] set : check if user has at least one role allowed for this action, - # or falls back to Non Member / Anonymous permissions depending if the user is logged - def allowed_to?(action, context, options={}, &block) - if context && context.is_a?(Project) - return false unless context.allows_to?(action) - # Admin users are authorized for anything else - return true if admin? - - roles = roles_for_project(context) - return false unless roles - roles.any? {|role| - (context.is_public? || role.member?) && - role.allowed_to?(action) && - (block_given? ? yield(role, self) : true) - } - #添加课程相关的权限判断 - elsif context && context.is_a?(Course) - return false unless context.allows_to?(action) - # Admin users are authorized for anything else - return true if admin? - - roles = roles_for_course(context) - return false unless roles - roles.any? {|role| - (context.is_public? || role.member?) && - role.allowed_to?(action) && - (block_given? ? yield(role, self) : true) - } - elsif context && context.is_a?(Array) - if context.empty? - false - else - # Authorize if user is authorized on every element of the array - context.map {|project| allowed_to?(action, project, options, &block)}.reduce(:&) - end - elsif options[:global] - # Admin users are always authorized - return true if admin? - - # authorize if user has at least one role that has this permission - roles = memberships.collect {|m| m.roles}.flatten.uniq - if roles.count == 0 - roles = coursememberships.collect {|m| m.roles}.flatten.uniq - end - roles << (self.logged? ? Role.non_member : Role.anonymous) - roles.any? {|role| - role.allowed_to?(action) && - (block_given? ? yield(role, self) : true) - } - else - if admin? - return true - end - #无项目时 查看Non member(id为1)角色是否有权限执行action - Role.find('1').allowed_to?(action) - # false - end - end - - # Is the user allowed to do the specified action on any project? - # See allowed_to? for the actions and valid options. - def allowed_to_globally?(action, options, &block) - allowed_to?(action, nil, options.reverse_merge(:global => true), &block) - end - - # Returns true if the user is allowed to delete his own account - def own_account_deletable? - Setting.unsubscribe? && - (!admin? || User.active.where("admin = ? AND id <> ?", true, id).exists?) - end - - safe_attributes 'login', - 'firstname', - 'lastname', - 'mail', - 'mail_notification', - 'language', - 'custom_field_values', - 'custom_fields', - 'identity_url' - - safe_attributes 'status', - 'auth_source_id', - :if => lambda {|user, current_user| current_user.admin?} - - safe_attributes 'group_ids', - :if => lambda {|user, current_user| current_user.admin? && !user.new_record?} - - # Utility method to help check if a user should be notified about an - # event. - # - # TODO: only supports Issue events currently - def notify_about?(object) - if mail_notification == 'all' - true - elsif mail_notification.blank? || mail_notification == 'none' - false - else - case object - when Issue - case mail_notification - when 'selected', 'only_my_events' - # user receives notifications for created/assigned issues on unselected projects - object.author == self || is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was) - when 'only_assigned' - is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was) - when 'only_owner' - object.author == self - end - when News - # always send to project members except when mail_notification is set to 'none' - true - #判定用户是否接受留言提醒邮件 - when JournalsForMessage - ##如果是直接留言并且留言对象是Project并且Project类型是课程(课程留言) - if !object.at_user && object.jour.class.to_s.to_sym == :Project && object.jour.project_type == 1 - #根据用户设置邮件接收模式判定当前用户是否接受邮件提醒 - is_notified_project object.jour - end - - end - end - end - - #用户是否接收project的消息提醒 - def is_notified_project arg - if arg.is_a?(Project) - case mail_notification - when 'selected' - notified_projects_ids.include?(arg.id) - when 'only_my_events' - projects.include?(arg) - when 'only_assigned' - false - when 'only_owner' - course = Course.find_by_extra(arg.identifier) - course.teacher == self - end - #勾选的项目或用户的项目 TODO:需改 - #notified_projects_ids.include?(arg) || projects.include?(arg) - else - false - end - end - - def self.current=(user) - Thread.current[:current_user] = user - end - - def self.current - Thread.current[:current_user] ||= User.anonymous - end - - # Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only - # one anonymous user per database. - def self.anonymous - anonymous_user = AnonymousUser.first - if anonymous_user.nil? - anonymous_user = AnonymousUser.create(:lastname => 'Anonymous', :firstname => '', :mail => '', :login => '', :status => 0) - raise 'Unable to create the anonymous user.' if anonymous_user.new_record? - end - anonymous_user - end - - # Salts all existing unsalted passwords - # It changes password storage scheme from SHA1(password) to SHA1(salt + SHA1(password)) - # This method is used in the SaltPasswords migration and is to be kept as is - def self.salt_unsalted_passwords! - transaction do - User.where("salt IS NULL OR salt = ''").find_each do |user| - next if user.hashed_password.blank? - salt = User.generate_salt - hashed_password = User.hash_password("#{salt}#{user.hashed_password}") - User.where(:id => user.id).update_all(:salt => salt, :hashed_password => hashed_password) - end - end - end - - protected - - def validate_password_length - # Password length validation based on setting - if !password.nil? && password.size < Setting.password_min_length.to_i - errors.add(:password, :too_short, :count => Setting.password_min_length.to_i) - end - end - private - - def act_as_activity - self.acts << Activity.new(:user_id => self.id) - end - - # Removes references that are not handled by associations - # Things that are not deleted are reassociated with the anonymous user - def remove_references_before_destroy - return if self.id.nil? - - substitute = User.anonymous - Attachment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] - Comment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] - Notificationcomment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] - Issue.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] - Issue.update_all 'assigned_to_id = NULL', ['assigned_to_id = ?', id] - Journal.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] - JournalDetail.update_all ['old_value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND old_value = ?", id.to_s] - JournalDetail.update_all ['value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND value = ?", id.to_s] - Message.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] - News.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] - # Remove private queries and keep public ones - ::Query.delete_all ['user_id = ? AND is_public = ?', id, false] - ::Query.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] - TimeEntry.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] - Token.delete_all ['user_id = ?', id] - Watcher.delete_all ['user_id = ?', id] - WikiContent.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] - WikiContent::Version.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] - end - - # Return password digest - def self.hash_password(clear_password) - Digest::SHA1.hexdigest(clear_password || "") - end - - # Returns a 128bits random salt as a hex string (32 chars long) - def self.generate_salt - Redmine::Utils.random_hex(16) - end - - - -end - -class AnonymousUser < User - validate :validate_anonymous_uniqueness, :on => :create - - def validate_anonymous_uniqueness - # There should be only one AnonymousUser in the database - errors.add :base, 'An anonymous user already exists.' if AnonymousUser.exists? - end - - def available_custom_fields - [] - end - - # Overrides a few properties - def logged?; false end - def admin; false end - def name(*args); I18n.t(:label_user_anonymous) end - def mail; nil end - def time_zone; nil end - def rss_key; nil end - - def pref - UserPreference.new(:user => self) - end - - # def member_of?(project) - # false - # end - - # Anonymous user can not be destroyed - def destroy - false - end -end +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require "digest/sha1" + +class User < Principal + TEACHER = 0 + STUDENT = 1 + ENTERPRISE = 2 + DEVELOPER = 3 + + include Redmine::SafeAttributes + seems_rateable_rater + # Different ways of displaying/sorting users + USER_FORMATS = { + :firstname_lastname => { + :string => '#{firstname} #{lastname}', + :order => %w(firstname lastname id), + :setting_order => 1 + }, + :firstname_lastinitial => { + :string => '#{firstname} #{lastname.to_s.chars.first}.', + :order => %w(firstname lastname id), + :setting_order => 2 + }, + :firstname => { + :string => '#{firstname}', + :order => %w(firstname id), + :setting_order => 3 + }, + :lastname_firstname => { + :string => '#{lastname} #{firstname}', + :order => %w(lastname firstname id), + :setting_order => 4 + }, + :lastname_coma_firstname => { + :string => '#{lastname}, #{firstname}', + :order => %w(lastname firstname id), + :setting_order => 5 + }, + :lastname => { + :string => '#{lastname}', + :order => %w(lastname id), + :setting_order => 6 + }, + :username => { + :string => '#{login}', + :order => %w(login id), + :setting_order => 7 + }, + } + + #每日一报、一事一报、不报 + MAIL_NOTIFICATION_OPTIONS = [ + ['all', :label_user_mail_option_all], + #['week', :label_user_mail_option_week], + ['day', :label_user_mail_option_day], + ['none', :label_user_mail_option_none] + ] + + has_many :homework_users + has_many :homework_attaches, :through => :homework_users + has_many :homework_evaluations + #问卷相关关关系 + has_many :poll_users, :dependent => :destroy + has_many :poll_votes, :dependent => :destroy + has_many :poll, :dependent => :destroy #用户创建的问卷 + has_many :answers, :source => :poll, :through => :poll_users, :dependent => :destroy #用户已经完成问答的问卷 + # end + #作业相关关系 + has_many :homework_commons, :dependent => :destroy + has_many :student_works, :dependent => :destroy + has_many :student_works_evaluation_distributions, :dependent => :destroy + has_many :student_works_scores, :dependent => :destroy + #end + + has_and_belongs_to_many :groups, :after_add => Proc.new {|user, group| group.user_added(user)}, + :after_remove => Proc.new {|user, group| group.user_removed(user)} + has_many :changesets, :dependent => :nullify + has_one :preference, :dependent => :destroy, :class_name => 'UserPreference' + has_one :rss_token, :class_name => 'Token', :conditions => "action='feeds'" + has_one :api_token, :class_name => 'Token', :conditions => "action='api'" + belongs_to :auth_source + belongs_to :ucourse, :class_name => 'Course', :foreign_key => :id #huang +## added by xianbo for delete + # has_many :biding_projects, :dependent => :destroy + has_many :contesting_projects, :dependent => :destroy + belongs_to :softapplication, :foreign_key => 'id', :dependent => :destroy +##ended by xianbo + +#####fq + has_many :jours, :class_name => 'JournalsForMessage', :dependent => :destroy + has_many :journals_messages, :class_name => 'JournalsForMessage', :foreign_key => "user_id", :dependent => :destroy + # has_many :bids, :foreign_key => 'author_id', :dependent => :destroy + has_many :contests, :foreign_key => 'author_id', :dependent => :destroy + has_many :softapplications, :foreign_key => 'user_id', :dependent => :destroy + has_many :journals_for_messages, :as => :jour, :dependent => :destroy + has_many :new_jours, :as => :jour, :class_name => 'JournalsForMessage', :conditions => "status=1" + has_many :journal_replies, :dependent => :destroy + has_many :activities, :dependent => :destroy + has_many :students_for_courses + #has_many :courses, :through => :students_for_courses, :source => :project + has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy + has_many :file_commit, :class_name => 'Attachment', :foreign_key => 'author_id', :conditions => "container_type = 'Project' or container_type = 'Version'" +#### +# added by bai + has_many :join_in_contests, :dependent => :destroy + has_many :news, :foreign_key => 'author_id' + has_many :contestnotification, :foreign_key => 'author_id' + has_many :comments, :foreign_key => 'author_id' + has_many :notificationcomments, :foreign_key => 'author_id' + has_many :wiki_contents, :foreign_key => 'author_id' + has_many :journals + has_many :messages, :foreign_key => 'author_id' + has_one :user_score, :dependent => :destroy + has_many :documents # 项目中关联的文档再次与人关联 +# end + + # 邮件邀请状态 + # has_many :invite_lists + # end + +######added by nie + has_many :project_infos, :dependent => :destroy + has_one :user_status, :dependent => :destroy + ##### + has_many :shares ,:dependent => :destroy + + # add by zjc + has_one :level, :class_name => 'UserLevels', :dependent => :destroy + has_many :memos , :foreign_key => 'author_id' + ##### + scope :logged, lambda { where("#{User.table_name}.status <> #{STATUS_ANONYMOUS}") } + scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) } + scope :visible, lambda {|*args| + nil + } + + + acts_as_customizable + ############################added by william + acts_as_taggable + scope :by_join_date, order("created_on DESC") + ############################# added by liuping 关注 + acts_as_watchable + + has_one :user_extensions,:dependent => :destroy + ## end + + # default_scope -> { includes(:user_extensions, :user_score) } + scope :teacher, -> { + joins(:user_extensions).where('user_extensions.identity = ?', UserExtensions::TEACHER) + } + scope :student, -> { + joins(:user_extensions).where('user_extensions.identity = ?', UserExtensions::STUDENT) + } + scope :developer, -> { + joins(:user_extensions).where('user_extensions.identity = ?', UserExtensions::DEVELOPER) + } + scope :enterprise, -> { + joins(:user_extensions).where('user_extensions.identity = ?', UserExtensions::ENTERPRISE) + } + + attr_accessor :password, :password_confirmation + attr_accessor :last_before_login_on + # Prevents unauthorized assignments + attr_protected :login, :admin, :password, :password_confirmation, :hashed_password + + LOGIN_LENGTH_LIMIT = 25 + MAIL_LENGTH_LIMIT = 60 + + validates_presence_of :login, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) } + validates_uniqueness_of :login, :if => Proc.new { |user| user.login_changed? && user.login.present? }, :case_sensitive => false + validates_uniqueness_of :mail, :if => Proc.new { |user| user.mail_changed? && user.mail.present? }, :case_sensitive => false + # Login must contain letters, numbers, underscores only + validates_format_of :login, :with => /\A[a-z0-9_\-@\.]*\z/i + validates_length_of :login, :maximum => LOGIN_LENGTH_LIMIT + validates_length_of :firstname, :maximum => 30 + validates_length_of :lastname, :maximum => 30 + validates_format_of :mail, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, :allow_blank => true + validates_length_of :mail, :maximum => MAIL_LENGTH_LIMIT, :allow_nil => true + validates_confirmation_of :password, :allow_nil => true + validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true + validate :validate_password_length + # validates_email_realness_of :mail + before_create :set_mail_notification + before_save :update_hashed_password + before_destroy :remove_references_before_destroy + # added by fq + after_create :act_as_activity + # end + + scope :in_group, lambda {|group| + group_id = group.is_a?(Group) ? group.id : group.to_i + where("#{User.table_name}.id IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id) + } + scope :not_in_group, lambda {|group| + group_id = group.is_a?(Group) ? group.id : group.to_i + where("#{User.table_name}.id NOT IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id) + } + scope :sorted, lambda { order(*User.fields_for_order_statement)} + + scope :like, lambda {|arg, type| + if arg.blank? + where(nil) + else + pattern = "%#{arg.to_s.strip.downcase}%" + #where(" LOWER(concat(lastname, firstname)) LIKE :p ", :p => pattern) + if type == "0" + where(" LOWER(login) LIKE '#{pattern}' ") + elsif type == "1" + where(" LOWER(concat(lastname, firstname)) LIKE '#{pattern}' ") + elsif type == "3" + where(" LOWER(concat(lastname, firstname,login)) LIKE '#{pattern}' ") + else + where(" LOWER(mail) LIKE '#{pattern}' ") + end + end + } + + + # ====================================================================== + + def extensions + self.user_extensions ||= UserExtensions.new + end + + def user_score_attr + self.user_score ||= UserScore.new + end + + # ====================================================================== + + #选择项目成员时显示的用户信息文字 + def userInfo + if self.realname.gsub(' ','') == "" || self.realname.nil? + info = self.nickname; + else + info=self.nickname + ' (' + self.realname + ')'; + end + info + end + + ###添加留言 fq + def add_jour(user, notes, reference_user_id = 0, options = {}) + if options.count == 0 + self.journals_for_messages << JournalsForMessage.new(:user_id => user.id, :notes => notes, :reply_id => reference_user_id, :status => true) + else + jfm = self.journals_for_messages.build(options) + jfm.save + jfm + end + end + + ### fq + def join_in?(course) + joined = StudentsForCourse.where('student_id = ? and course_id = ?', self.id, course.id) + if joined.size > 0 + true + else + false + end + end + + def show_name + name = "" + unless self.user_extensions.nil? + if self.user_extensions.identity == 2 + name = firstname + else + name = lastname+firstname + end + else + name = lastname+firstname + end + name = name.empty? || name.nil? ? login : name + name + end + ## end + + def count_new_jour + count = self.new_jours.count + end + + #added by nie + def count_new_journal_reply + count = self.journal_reply.count + end + + def set_mail_notification + ##add byxianbo + thread=Thread.new do + self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? + true + end + end + + def update_hashed_password + # update hashed_password if password was set + if self.password && self.auth_source_id.blank? + salt_password(password) + end + end + + alias :base_reload :reload + def reload(*args) + @name = nil + @projects_by_role = nil + @courses_by_role = nil + @membership_by_project_id = nil + base_reload(*args) + end + + def mail=(arg) + write_attribute(:mail, arg.to_s.strip) + end + + def identity_url=(url) + if url.blank? + write_attribute(:identity_url, '') + else + begin + write_attribute(:identity_url, OpenIdAuthentication.normalize_identifier(url)) + rescue OpenIdAuthentication::InvalidOpenId + # Invalid url, don't save + end + end + self.read_attribute(:identity_url) + end + + VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i + # VALID_EMAIL_REGEX = /^[0-9a-zA-Z_-]+@[0-9a-zA-Z_-]+(\.[0-9a-zA-Z_-]+)+$/ + # Returns the user that matches provided login and password, or nil + #登录,返回用户名与密码匹配的用户 + def self.try_to_login(login, password) + login = login.to_s.lstrip.rstrip + password = password.to_s + + # Make sure no one can sign in with an empty login or password + return nil if login.empty? || password.empty? + if (login =~ VALID_EMAIL_REGEX) + user = find_by_mail(login) + else + user = find_by_login(login) + end + if user + # user is already in local database + #return nil unless user.active? + return nil unless user.check_password?(password) + else + # user is not yet registered, try to authenticate with available sources + attrs = AuthSource.authenticate(login, password) + if attrs + user = new(attrs) + user.login = login + user.language = Setting.default_language + if user.save + user.reload + logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source + end + end + end + if user && !user.new_record? + last_login_on = user.last_login_on.nil? ? '' : user.last_login_on.to_s + user.update_column(:last_login_on, Time.now) + end + [user, last_login_on] + rescue => text + raise text + end + + + def self.try_to_autologin(key) + user = Token.find_active_user('autologin', key, Setting.autologin.to_i) + if user + user.update_column(:last_login_on, Time.now) + user + end + end + + def self.name_formatter(formatter = nil) + USER_FORMATS[formatter || Setting.user_format] || USER_FORMATS[:firstname_lastname] + end + + # Returns an array of fields names than can be used to make an order statement for users + # according to how user names are displayed + # Examples: + # + # User.fields_for_order_statement => ['users.login', 'users.id'] + # User.fields_for_order_statement('authors') => ['authors.login', 'authors.id'] + def self.fields_for_order_statement(table=nil) + table ||= table_name + name_formatter[:order].map {|field| "#{table}.#{field}"} + end + + # Return user's full name for display + def realname(formatter = nil) + f = self.class.name_formatter(formatter) + if formatter + eval('"' + f[:string] + '"') + else + @name ||= eval('"' + f[:string] + '"') + end + end + + def nickname(formatter = nil) + login + end + + def name(formatter = nil) + login + end + + def active? + self.status == STATUS_ACTIVE + end + + def registered? + self.status == STATUS_REGISTERED + end + + def locked? + self.status == STATUS_LOCKED + end + + def activate + self.status = STATUS_ACTIVE + end + + def register + self.status = STATUS_REGISTERED + end + + def lock + self.status = STATUS_LOCKED + end + + def activate! + update_attribute(:status, STATUS_ACTIVE) + end + + def register! + update_attribute(:status, STATUS_REGISTERED) + end + + def lock! + update_attribute(:status, STATUS_LOCKED) + end + + # Returns true if +clear_password+ is the correct user's password, otherwise false + def check_password?(clear_password) + if auth_source_id.present? + auth_source.authenticate(self.login, clear_password) + else + User.hash_password("#{salt}#{User.hash_password clear_password}") == hashed_password + end + end + def check_password1?(clear_password) + + clear_password == hashed_password + + end + # Generates a random salt and computes hashed_password for +clear_password+ + # The hashed password is stored in the following form: SHA1(salt + SHA1(password)) + def salt_password(clear_password) + self.salt = User.generate_salt + self.hashed_password = User.hash_password("#{salt}#{User.hash_password clear_password}") + end + + # Does the backend storage allow this user to change their password? + def change_password_allowed? + return true if auth_source.nil? + return auth_source.allow_password_changes? + end + + # Generate and set a random password. Useful for automated user creation + # Based on Token#generate_token_value + # + def random_password + chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a + password = '' + 40.times { |i| password << chars[rand(chars.size-1)] } + self.password = password + self.password_confirmation = password + self + end + + def pref + self.preference ||= UserPreference.new(:user => self) + end + + def time_zone + @time_zone ||= (self.pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[self.pref.time_zone]) + end + + def wants_comments_in_reverse_order? + self.pref[:comments_sorting] == 'desc' + end + + def wants_notificationcomments_in_reverse_order? + self.pref[:notificationcomments_sorting] == 'desc' + end + # Return user's RSS key (a 40 chars long string), used to access feeds + def rss_key + if rss_token.nil? + create_rss_token(:action => 'feeds') + end + rss_token.value + end + + # Return user's API key (a 40 chars long string), used to access the API + def api_key + if api_token.nil? + create_api_token(:action => 'api') + end + api_token.value + end + + # Return an array of project ids for which the user has explicitly turned mail notifications on + def notified_projects_ids + @notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id) + end + + def notified_project_ids=(ids) + Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id]) + Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty? + @notified_projects_ids = nil + notified_projects_ids + end + + def valid_notification_options + self.class.valid_notification_options(self) + end + + # Only users that belong to more than 1 project can select projects for which they are notified + def self.valid_notification_options(user=nil) + # Note that @user.membership.size would fail since AR ignores + # :include association option when doing a count + if user.nil? || user.memberships.length < 1 + MAIL_NOTIFICATION_OPTIONS.reject {|option| option.first == 'selected'} + else + MAIL_NOTIFICATION_OPTIONS + end + end + + # Find a user account by matching the exact login and then a case-insensitive + # version. Exact matches will be given priority. + #通过用户名查找相应的用户,若没有匹配到,则不区分大小写进行查询 + #修改:不再匹配不区分大小写情况 -zjc + def self.find_by_login(login) + if login.present? + login = login.to_s + # First look for an exact match + user = where(:login => login).all.detect {|u| u.login == login} + #unless user + # # Fail over to case-insensitive if none was found + # user = where("LOWER(login) = ?", login.downcase).first + #end + user + end + end + + def self.find_by_rss_key(key) + Token.find_active_user('feeds', key) + end + + def self.find_by_api_key(key) + Token.find_active_user('api', key) + end + + # Makes find_by_mail case-insensitive + def self.find_by_mail(mail) + where("LOWER(mail) = ?", mail.to_s.downcase).first + end + + # Returns true if the default admin account can no longer be used + def self.default_admin_account_changed? + !User.active.find_by_login("admin").try(:check_password?, "admin") + end + + def to_s + name + end + + CSS_CLASS_BY_STATUS = { + STATUS_ANONYMOUS => 'anon', + STATUS_ACTIVE => 'active', + STATUS_REGISTERED => 'registered', + STATUS_LOCKED => 'locked' + } + + def css_classes + "user #{CSS_CLASS_BY_STATUS[status]}" + end + + # Returns the current day according to user's time zone + def today + if time_zone.nil? + Date.today + else + Time.now.in_time_zone(time_zone).to_date + end + end + + # Returns the day of +time+ according to user's time zone + def time_to_date(time) + if time_zone.nil? + time.to_date + else + time.in_time_zone(time_zone).to_date + end + end + + def logged? + true + end + + def anonymous? + !logged? + end + + # Returns user's membership for the given project + # or nil if the user is not a member of project + def membership(project) + project_id = project.is_a?(Project) ? project.id : project + + @membership_by_project_id ||= Hash.new {|h, project_id| + h[project_id] = memberships.where(:project_id => project_id).first + } + @membership_by_project_id[project_id] + end + + def coursemembership(course) + course_id = course.is_a?(Course) ? course.id : course + + @membership_by_course_id ||= Hash.new {|h, course_id| + h[course_id] = coursememberships.where(:course_id => course_id).first + } + @membership_by_course_id[course_id] + end + + # Return user's roles for project + def roles_for_project(project) + roles = [] + # No role on archived projects + return roles if project.nil? || project.archived? + if logged? + # Find project membership + membership = membership(project) + if membership + roles = membership.roles + else + @role_non_member ||= Role.non_member + roles << @role_non_member + end + else + @role_anonymous ||= Role.anonymous + roles << @role_anonymous + end + roles + end + + # 用户课程权限判断 + def roles_for_course(course) + roles = [] + # No role on archived courses + return roles if course.nil? || course.archived? + if logged? + # Find course membership + membership = coursemembership(course) + if membership + roles = membership.roles + else + @role_non_member ||= Role.non_member + roles << @role_non_member + end + else + @role_anonymous ||= Role.anonymous + roles << @role_anonymous + end + roles + end + + # Return true if the user is a member of project + def member_of?(project) + projects.to_a.include?(project) + end + + def member_of_course?(course) + courses.to_a.include?(course) + end + + def member_of_course_group?(course_group) + course_groups.to_a.include?(course_group) + end + # Returns a hash of user's projects grouped by roles + def projects_by_role + return @projects_by_role if @projects_by_role + + @projects_by_role = Hash.new([]) + memberships.each do |membership| + if membership.project + membership.roles.each do |role| + @projects_by_role[role] = [] unless @projects_by_role.key?(role) + @projects_by_role[role] << membership.project + end + end + end + @projects_by_role.each do |role, projects| + projects.uniq! + end + + @projects_by_role + end + + # 课程的角色权限 + def courses_by_role + return @courses_by_role if @courses_by_role + + @courses_by_role = Hash.new([]) + coursememberships.each do |membership| + if membership.course + membership.roles.each do |role| + @courses_by_role[role] = [] unless @courses_by_role.key?(role) + @courses_by_role[role] << membership.course + end + end + end + @courses_by_role.each do |role, courses| + courses.uniq! + end + + @courses_by_role + end + # Returns true if user is arg or belongs to arg + def is_or_belongs_to?(arg) + if arg.is_a?(User) + self == arg + elsif arg.is_a?(Group) + arg.users.include?(self) + else + false + end + end + + + # Return true if the user is allowed to do the specified action on a specific context + # Action can be: + # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') + # * a permission Symbol (eg. :edit_project) + # Context can be: + # * a project : returns true if user is allowed to do the specified action on this project + # * an array of projects : returns true if user is allowed on every project + # * nil with options[:global] set : check if user has at least one role allowed for this action, + # or falls back to Non Member / Anonymous permissions depending if the user is logged + def allowed_to?(action, context, options={}, &block) + if Project === context + return false unless context.allows_to?(action) + # Admin users are authorized for anything else + return true if admin? + + roles = roles_for_project(context) + return false unless roles + roles.any? {|role| + (context.is_public? || role.member?) && + role.allowed_to?(action) && + (block_given? ? yield(role, self) : true) + } + #添加课程相关的权限判断 + elsif Course === context + return false unless context.allows_to?(action) + # Admin users are authorized for anything else + return true if admin? + + roles = roles_for_course(context) + return false unless roles + roles.any? {|role| + (context.is_public? || role.member?) && + role.allowed_to?(action) && + (block_given? ? yield(role, self) : true) + } + elsif context && context.is_a?(Array) + if context.empty? + false + else + # Authorize if user is authorized on every element of the array + context.map {|project| allowed_to?(action, project, options, &block)}.reduce(:&) + end + elsif options[:global] + # Admin users are always authorized + return true if admin? + + # authorize if user has at least one role that has this permission + roles = memberships.collect {|m| m.roles}.flatten.uniq + if roles.count == 0 + roles = coursememberships.collect {|m| m.roles}.flatten.uniq + end + roles << (self.logged? ? Role.non_member : Role.anonymous) + roles.any? {|role| + role.allowed_to?(action) && + (block_given? ? yield(role, self) : true) + } + else + if admin? + return true + end + #无项目时 查看Non member(id为1)角色是否有权限执行action + Role.find('1').allowed_to?(action) + # false + end + end + + # Is the user allowed to do the specified action on any project? + # See allowed_to? for the actions and valid options. + def allowed_to_globally?(action, options, &block) + allowed_to?(action, nil, options.reverse_merge(:global => true), &block) + end + + # Returns true if the user is allowed to delete his own account + def own_account_deletable? + Setting.unsubscribe? && + (!admin? || User.active.where("admin = ? AND id <> ?", true, id).exists?) + end + + safe_attributes 'login', + 'firstname', + 'lastname', + 'mail', + 'mail_notification', + 'language', + 'custom_field_values', + 'custom_fields', + 'identity_url' + + safe_attributes 'status', + 'auth_source_id', + :if => lambda {|user, current_user| current_user.admin?} + + safe_attributes 'group_ids', + :if => lambda {|user, current_user| current_user.admin? && !user.new_record?} + + # Utility method to help check if a user should be notified about an + # event. + # + # TODO: only supports Issue events currently + def notify_about?(object) + if mail_notification == 'all' + true + elsif mail_notification.blank? || mail_notification == 'none' + false + else + case object + when Issue + case mail_notification + when 'selected', 'only_my_events' + # user receives notifications for created/assigned issues on unselected projects + object.author == self || is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was) + when 'only_assigned' + is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was) + when 'only_owner' + object.author == self + end + when News + # always send to project members except when mail_notification is set to 'none' + true + #判定用户是否接受留言提醒邮件 + when JournalsForMessage + ##如果是直接留言并且留言对象是Project并且Project类型是课程(课程留言) + if !object.at_user && object.jour.class.to_s.to_sym == :Project && object.jour.project_type == 1 + #根据用户设置邮件接收模式判定当前用户是否接受邮件提醒 + is_notified_project object.jour + end + + end + end + end + + #用户是否接收project的消息提醒 + def is_notified_project arg + if arg.is_a?(Project) + case mail_notification + when 'selected' + notified_projects_ids.include?(arg.id) + when 'only_my_events' + projects.include?(arg) + when 'only_assigned' + false + when 'only_owner' + course = Course.find_by_extra(arg.identifier) + course.teacher == self + end + #勾选的项目或用户的项目 TODO:需改 + #notified_projects_ids.include?(arg) || projects.include?(arg) + else + false + end + end + + def self.current=(user) + Thread.current[:current_user] = user + end + + def self.current + Thread.current[:current_user] ||= User.anonymous + end + + # Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only + # one anonymous user per database. + def self.anonymous + anonymous_user = AnonymousUser.first + if anonymous_user.nil? + anonymous_user = AnonymousUser.create(:lastname => 'Anonymous', :firstname => '', :mail => '', :login => '', :status => 0) + raise 'Unable to create the anonymous user.' if anonymous_user.new_record? + end + anonymous_user + end + + # Salts all existing unsalted passwords + # It changes password storage scheme from SHA1(password) to SHA1(salt + SHA1(password)) + # This method is used in the SaltPasswords migration and is to be kept as is + def self.salt_unsalted_passwords! + transaction do + User.where("salt IS NULL OR salt = ''").find_each do |user| + next if user.hashed_password.blank? + salt = User.generate_salt + hashed_password = User.hash_password("#{salt}#{user.hashed_password}") + User.where(:id => user.id).update_all(:salt => salt, :hashed_password => hashed_password) + end + end + end + + protected + + def validate_password_length + # Password length validation based on setting + if !password.nil? && password.size < Setting.password_min_length.to_i + errors.add(:password, :too_short, :count => Setting.password_min_length.to_i) + end + end + private + + def act_as_activity + self.acts << Activity.new(:user_id => self.id) + end + + # Removes references that are not handled by associations + # Things that are not deleted are reassociated with the anonymous user + def remove_references_before_destroy + return if self.id.nil? + + substitute = User.anonymous + Attachment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] + Comment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] + Notificationcomment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] + Issue.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] + Issue.update_all 'assigned_to_id = NULL', ['assigned_to_id = ?', id] + Journal.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] + JournalDetail.update_all ['old_value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND old_value = ?", id.to_s] + JournalDetail.update_all ['value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND value = ?", id.to_s] + Message.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] + News.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] + # Remove private queries and keep public ones + ::Query.delete_all ['user_id = ? AND is_public = ?', id, false] + ::Query.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] + TimeEntry.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] + Token.delete_all ['user_id = ?', id] + Watcher.delete_all ['user_id = ?', id] + WikiContent.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] + WikiContent::Version.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] + end + + # Return password digest + def self.hash_password(clear_password) + Digest::SHA1.hexdigest(clear_password || "") + end + + # Returns a 128bits random salt as a hex string (32 chars long) + def self.generate_salt + Redmine::Utils.random_hex(16) + end + + + +end + +class AnonymousUser < User + validate :validate_anonymous_uniqueness, :on => :create + + def validate_anonymous_uniqueness + # There should be only one AnonymousUser in the database + errors.add :base, 'An anonymous user already exists.' if AnonymousUser.exists? + end + + def available_custom_fields + [] + end + + # Overrides a few properties + def logged?; false end + def admin; false end + def name(*args); I18n.t(:label_user_anonymous) end + def mail; nil end + def time_zone; nil end + def rss_key; nil end + + def pref + UserPreference.new(:user => self) + end + + # def member_of?(project) + # false + # end + + # Anonymous user can not be destroyed + def destroy + false + end +end diff --git a/app/models/user_extensions.rb b/app/models/user_extensions.rb index b21f0ad36..d9a0f520a 100644 --- a/app/models/user_extensions.rb +++ b/app/models/user_extensions.rb @@ -1,68 +1,86 @@ -# encoding: utf-8 -=begin - identity字段含义 - 0 教师教授 - 1 学生 - 2 企业 - 3 开发者 -=end -class UserExtensions < ActiveRecord::Base - validate :school, presence: true - - belongs_to :user - belongs_to :school, :class_name => 'School', :foreign_key => :school_id - attr_accessible :user_id,:birthday,:brief_introduction,:gender,:location,:occupation,:work_experience,:zip_code,:identity, :technical_title,:student_id - TEACHER = 0 - STUDENT = 1 - ENTERPRISE = 2 - DEVELOPER = 3 - #this method was used to update the table user_extensions - def update_user_extensions(birthday=nil,brief_introduction=nil, - gender=nil,location=nil,occupation=nil,work_experience=nil,zip_code=nil) - self.birthday = birthday - self.brief_introduction = brief_introduction - self.gender = gender - self.location = location - self.occupation = occupation - self.work_experience = work_experience - self.zip_code = zip_code - self.save - end - - def get_brief_introduction - return self.brief_introduction - end - -# added by bai - def show_identity - if self.identity == 0 - user_identity = '教师' - elsif self.identity == 1 - user_identity = '学生' - elsif self.identity == 2 - user_identity = '企业' - elsif self.identity == 3 - user_identity = '开发者' - else - user_identity = '' - end - return user_identity - end -# end - - def self.introduction(user, message) - unless user.user_extensions.nil? - info = user.user_extensions - info.brief_introduction = message - info.save - else - info = UserExtensions.new - info.user_id = user.id - info.brief_introduction = message - info.save - end - end - - - -end +# encoding: utf-8 +=begin + identity字段含义 + 0 教师教授 + 1 学生 + 2 企业 + 3 开发者 +=end +class UserExtensions < ActiveRecord::Base + validate :school, presence: true + + belongs_to :user + belongs_to :school, :class_name => 'School', :foreign_key => :school_id + attr_accessible :user_id,:birthday,:brief_introduction,:gender,:location,:occupation,:work_experience,:zip_code,:identity, :technical_title,:student_id + TEACHER = 0 + STUDENT = 1 + ENTERPRISE = 2 + DEVELOPER = 3 + #this method was used to update the table user_extensions + def update_user_extensions(birthday=nil,brief_introduction=nil, + gender=nil,location=nil,occupation=nil,work_experience=nil,zip_code=nil) + self.birthday = birthday + self.brief_introduction = brief_introduction + self.gender = gender + self.location = location + self.occupation = occupation + self.work_experience = work_experience + self.zip_code = zip_code + self.save + end + + def get_brief_introduction + return self.brief_introduction + end + + +# added by meng + def show_identity + if User.current.language == 'zh'||User.current.language == '' + case self.identity + when 0 + user_identity = l(:label_account_identity_teacher) + when 1 + user_identity = l(:label_account_identity_student) + when 2 + user_identity = l(:label_account_identity_enterprise) + when 3 + user_identity = l(:label_account_identity_developer) + else + user_identity = '' + end + else + case self.identity + when 0 + user_identity = l(:label_account_identity_teacher) + when 1 + user_identity = l(:label_account_identity_student) + when 2 + user_identity = l(:label_account_identity_enterprise) + when 3 + user_identity = l(:label_account_identity_developer) + else + user_identity = '' + end + end + return user_identity + end +# end + + + def self.introduction(user, message) + unless user.user_extensions.nil? + info = user.user_extensions + info.brief_introduction = message + info.save + else + info = UserExtensions.new + info.user_id = user.id + info.brief_introduction = message + info.save + end + end + + + +end diff --git a/app/models/wiki_content_observer.rb b/app/models/wiki_content_observer.rb index 187c02288..3ded4da86 100644 --- a/app/models/wiki_content_observer.rb +++ b/app/models/wiki_content_observer.rb @@ -1,34 +1,28 @@ -# Redmine - project management software -# Copyright (C) 2006-2013 Jean-Philippe Lang -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -class WikiContentObserver < ActiveRecord::Observer - def after_create(wiki_content) - ##by senluo - thread7=Thread.new do - Mailer.wiki_content_added(wiki_content).deliver if Setting.notified_events.include?('wiki_content_added') - end - end - - def after_update(wiki_content) - if wiki_content.text_changed? - ##by senluo - thread8=Thread.new do - Mailer.wiki_content_updated(wiki_content).deliver if Setting.notified_events.include?('wiki_content_updated') - end - end - end -end +# Redmine - project management software +# Copyright (C) 2006-2013 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class WikiContentObserver < ActiveRecord::Observer + def after_create(wiki_content) + Mailer.run.wiki_content_added(wiki_content) if Setting.notified_events.include?('wiki_content_added') + end + + def after_update(wiki_content) + if wiki_content.text_changed? + Mailer.run.wiki_content_updated(wiki_content) if Setting.notified_events.include?('wiki_content_updated') + end + end +end diff --git a/app/models/zip_pack.rb b/app/models/zip_pack.rb new file mode 100644 index 000000000..df6ad593f --- /dev/null +++ b/app/models/zip_pack.rb @@ -0,0 +1,18 @@ +class ZipPack < ActiveRecord::Base + # attr_accessible :title, :body + + def self.packed?(bid_id, user_id, digests) + zip_pack = ZipPack.where(homework_id: bid_id, user_id: user_id).first + return false unless zip_pack && zip_pack.digests == digests + zip_pack + end + + def file_valid? + return false unless File.exist?(self.file_path) + Trustie::Utils.digest(self.file_path) == self.file_digest + end + + def digests + self.file_digests.split(',').sort + end +end diff --git a/app/services/comment_service.rb b/app/services/comment_service.rb new file mode 100644 index 000000000..2464297da --- /dev/null +++ b/app/services/comment_service.rb @@ -0,0 +1,117 @@ +class CommentService + include ApiHelper + include Redmine::I18n + #评论 + def news_comments params,current_user + @news = News.find(params[:id]) + @course = @news.course + if @course.nil? + raise 'news in unknown course' + end + raise Unauthorized unless @news.commentable?(current_user) + if current_user.nil? || !(current_user.admin? || @course.is_public == 1 || (@course.is_public == 0 && current_user.member_of_course?(@course))) + raise '403' + end + @comment = Comment.new + @comment.send(:safe_attributes=,params[:comment],current_user) + @comment.author = current_user + @news.comments << @comment + @comment + end + + #作业留言 + def homework_message params,current_user + @bid = Bid.find(params[:id], :include => [{:homeworks => :user}]) + if params[:bid_message][:message].size>0 + if params[:reference_content] + message = params[:bid_message][:message] + "\n" + params[:reference_content] + else + message = params[:bid_message][:message] + @m = message + end + refer_user_id = params[:bid_message][:reference_user_id].to_i + jfm = @bid.add_jour(current_user, message, refer_user_id) + end + #@user = @bid.author + #@jours = @bid.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC') + #@jour = paginateHelper @jours,10 + @bid.set_commit(@feedback_count) + jfm + end + #课程留言接口 + def leave_course_message params,current_user + message = params[:new_form][:course_message] + feedback = Course.add_new_jour(current_user, message, params[:id]) + feedback + end + + #回复留言接口 + def create_reply params,current_user + # 这里是创建回复所使用的方法,此方法只针对回复,每一个新的留言并不在此方法管理范围内。 + # 由于多个地方用到了留言,而之前的表设计也有jour_type/jour_id这类信息 + # 所以在方法 add_reply_adapter 中判断所有调用此方法的来源页面, + # 为了保证兼容以往所有的代码,保证以往的方法可以调用,在返回页面中都做了各式各样的判断。 + # 页面保证 render new_respond/journal_reply + # 修改 add_reply_adapter 中可以确保留言创建成功 + # 删除留言功能要调用destroy,也记得在destroy.js中修改 + + # deny api. api useless + parent_id = params[:reference_id] + author_id = current_user.id + reply_user_id = params[:reference_user_id] + reply_id = params[:reference_message_id] # 暂时不实现 + content = params[:user_notes] + jour_type = params[:jour_type] + jour_id = params[:jour_id] + @show_name = params[:show_name] == "true" + options = { + :jour_id => jour_id, + :jour_type => jour_type, + :user_id => author_id, + :status => true, + :m_parent_id => parent_id, + :m_reply_id => reply_id, + :reply_id => reply_user_id, + :notes => content, + :is_readed => false} + @jfm = ::JournalsForMessage.new(options) + #@save_succ = true if @jfm.errors.empty? + @jfm.save + @jfm + end + + #发贴,用于意见反馈 + def create_feedback params,current_user + @memo = Memo.new(params[:memo]) + @memo.forum_id = "1" + @memo.author_id = current_user.id + @memo.save + message = "#{l(:label_commit_failed,:locale => get_user_language(current_user))}: #{@memo.errors.full_messages}" if @memo.new_record? + [@memo,message] + end + + #课程留言列表 + def course_messages params,current_user + @course = ::Course.find(params[:id]) + if (current_user.admin? || @course.is_public == 1 || (@course.is_public == 0 && current_user.member_of_course?(@course))) + @jours = @course.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC').page(params[:page] ||= 1).per(10) + else + raise '403' + end + @jours + end + + def comment_detail params,current_user + if !params[:course_id].nil? && params[:course_id] != 0 + course = Course.find(params[:course_id]) + jour = course.journals_for_messages.where("id = ? ",params[:comment_parent_id]) + jour + else + jour1 = JournalsForMessage.where("id = ? ",params[:comment_parent_id]) + jour1 + end + end + + + +end \ No newline at end of file diff --git a/app/services/courses_service.rb b/app/services/courses_service.rb new file mode 100644 index 000000000..68177552c --- /dev/null +++ b/app/services/courses_service.rb @@ -0,0 +1,740 @@ +#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, :author=>n.author, :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 + else + + end + + @issue_custom_fields = IssueCustomField.sorted.all + @trackers = Tracker.sorted.all + + if @course && @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.page(1).per(3).order('created_at 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))<<":"<3,:time => latest_attachment.created_on,:message =>latest_attachment.author.realname<< l(:label_recently_updated_courseware,:locale => get_user_language(current_user))<<":"< 4,:time => latest_bid.updated_at,:message => latest_bid.user.realname< get_user_language(current_user))<<":"< 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 + if !bid.nil? + if bid.homework_type == 1 && bid.homework_detail_manual + case bid.homework_detail_manual.comment_status + when 1 + state = show_homework_deadline bid + when 2 + state = "正在匿评中" + when 3 + state = "匿评已结束" + end + elsif bid.homework_type == 0 + state = "未启用匿评" + elsif bid.homework_type == 2 + state = "编程作业" + else + end + end + studentlist = [] + bid.student_works.order("created_at desc").page(1).per(6).each do |work| + studentlist << work.user + end + 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,:studentlist => studentlist} + + 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 + + #app新版api + # + # + #课程动态 + public + def all_course_dynamics 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.page(1).per(10) + else + membership = @user.coursememberships.page(1).per(10).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 } + + #定义一个数组集合,存放hash数组,该hash数组包括课程的信息,并包含课程的最新发布的资源,最新的讨论区留言,最新的作业,最新的通知 + result = [] + #对用户所有的课程进行循环,找到每个课程最新发布的资源,最新的讨论区留言,最新的作业,最新的通知,并存进数组 + membership.each do |mp| + course = mp.course + latest_course_dynamics = [] + dynamics_count = 0 + # 课程通知 + latest_news = course.news.order("created_on desc").first + unless latest_news.nil? + latest_course_dynamics << {:type => 1, :time => latest_news.created_on,:count=>course.news.count, + :news => latest_news} + dynamics_count += 1 + end + + # 课程讨论区 + latest_message = course.boards.first.topics[0] + unless latest_message.nil? + latest_course_dynamics << {:type => 2, :time => latest_message.created_on, :count =>course.boards.nil? ? 0 : course.boards.first.topics.count, + :topic => latest_message} + dynamics_count += 1 + end + + # 课程资源 + latest_attachment = course.attachments.order("created_on desc").first + unless latest_attachment.nil? + latest_course_dynamics << {:type => 3, :time => latest_attachment.created_on,:count =>course.attachments.count , :documents=>latest_attachment} + dynamics_count += 1 + end + + #课程作业 已经交的学生列表(暂定显示6人),未交的学生列表,作业的状态 + homework = course.homework_commons.order('created_at desc').first + homework_status = ""; + # 判断作业所处的状态,如果是刚发布,就获取剩余时间 + #如果是匿评状态,显示正在匿评 + #如果是匿评结束,显示匿评结束 + #获取作业提交的前6个人,不足6个显示所有 + studentlist = [] + if !homework.nil? + if homework.homework_type == 1 && homework.homework_detail_manual + case homework.homework_detail_manual.comment_status + when 1 + homework_status = show_homework_deadline homework + when 2 + homework_status = "正在匿评中" + when 3 + homework_status = "匿评已结束" + end + elsif homework.homework_type == 0 + homework_status = "未启用匿评" + elsif homework.homework_type == 2 + homework_status = "编程作业" + else + end + # 获取提交作业的前六个学生的名字 和 头像路径 + homework.student_works.order("created_at desc").page(1).per(6).each do |work| + studentlist << {:image_url=> url_to_avatar(work.user),:user_name=>work.user.realname} + end + latest_course_dynamics << {:type => 4, :time => homework.updated_at, :count=>course.homework_commons.count,:submit_count => homework.student_works.count , :homework => homework, :homework_status => homework_status, :studentlist => studentlist} + dynamics_count += 1 + 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,:message => dynamics_count, :dynamics => latest_course_dynamics, :count => dynamics_count} + end + end + #返回数组集合 + result.sort! { |order, newer| newer[:update_time] <=> order[:update_time] } + result + end + + #计算作业的截止日期,剩余日期 + def show_homework_deadline homework + "距作业截止还有" << (Date.parse(Time.now.to_s) - Date.parse(homework.end_time.to_s)).to_i.to_s << "天" + end + end + diff --git a/app/services/homework_service.rb b/app/services/homework_service.rb new file mode 100644 index 000000000..2a2c6613c --- /dev/null +++ b/app/services/homework_service.rb @@ -0,0 +1,324 @@ +#coding=utf-8 +class HomeworkService + include CoursesHelper + include AttachmentsHelper + include ApplicationHelper + include WordsHelper + include ApiHelper + include HomeworkAttachHelper + include CoursesHelper + + # 作业详情(老师才显示启动匿评,学生不显示 ) + # many_times 第几次(作业) + # state=0 启动匿评 + # state=1 关闭匿评 + # state=2 匿评结束 + def show_homework params + @bid = HomeworkCommon.find(params[:id]) + course = @bid.course + author = @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 + #end + open_anonymous_evaluation = @bid.homework_type + #jours = @bid.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC') + {:course_name => course.name,:course_id => course.id,:id => @bid.id, :author => @bid.user,:author_real_name =>author, :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,:created_on => @bid.created_at,:deadline => @bid.end_time,:jours => nil} + 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) + [@bid,@totle_size,@cur_size,@percent] + end + + #启动匿评 + #statue 1:启动成功,2:启动失败,作业总数大于等于2份时才能启动匿评,3:已开启匿评,请务重复开启 + def start_anonymous_comment params,current_user + @bid = Bid.find(params[:id]) + @course = @bid.courses.first + unless is_course_teacher(current_user,@course) || current_user.admin? + @statue = 4 + raise '403' + end + if(@bid.comment_status == 0) + homeworks = @bid.homeworks + if(homeworks && homeworks.size >= 2) + homeworks.each_with_index do |homework, index| + user = homework.user + n = @bid.evaluation_num + n = n < homeworks.size ? n : homeworks.size - 1 + assigned_homeworks = get_assigned_homeworks(homeworks, n, index) + assigned_homeworks.each do |h| + @homework_evaluation = HomeworkEvaluation.new(user_id: user.id, homework_attach_id: h.id) + @homework_evaluation.save + end + end + @bid.update_column('comment_status', 1) + @statue = 1 + else + @statue = 2 + end + else + @statue = 3 + end + @statue + end + #关闭匿评 + def stop_anonymous_comment params,current_user + @bid = Bid.find(params[:id]) + @course = @bid.courses.first + unless is_course_teacher(current_user,@course) || current_user.admin? + raise '403' + end + @bid.update_column('comment_status', 2) + end + + # 匿评作品详情 + # attachs 该作品的所有附件 + # filename 文件名 + # filedesc 文件描述 + def anonymous_works_show(params,current_user) + @homework = HomeworkAttach.find(params[:id]) + @bid = @homework.bid + @course = @bid.courses.first + if current_user.admin? || current_user.member_of_course?(@course) + @stars_reates = @homework.rates(:quality) + @is_teacher = is_course_teacher current_user,@course + @has_evaluation = @stars_reates.where("rater_id = #{current_user.id} and is_teacher_score=#{@is_teacher ? 1 : 0}").first + @m_score = @has_evaluation.nil? ? 0 : @has_evaluation.stars + @teacher_stars = @stars_reates.where("is_teacher_score = 1") #老师评分列表 + @student_stars = @stars_reates.where("is_teacher_score = 0") #学生评分列表 + @is_anonymous_comments = @bid.comment_status == 1 && !@homework.users.include?(current_user) && @homework.user != current_user && !@is_teacher #判断是不是匿评(开启匿评,当前用户不是作业的创建者或者参与者,不是老师) + jours = @homework.journals_for_messages.where("is_comprehensive_evaluation = 3 or is_comprehensive_evaluation is null").order("created_on DESC")#jours留言 is null条件用以兼容历史数据 + #@jour = paginateHelper jours,5 #留言 + @cur_page = params[:cur_page] || 1 + @cur_type = params[:cur_type] || 5 + teacher_stars_json_like = stars_to_json_like(@teacher_stars,true,@homework,true) + student_stars_json_like = stars_to_json_like(@student_stars,false,@homework,(false || @is_teacher)) + else + raise '403' + end + + [@homework,{:is_teacher => @is_teacher,:m_score => @m_score,:jours => jours,:teacher_stars => teacher_stars_json_like, + :student_stars => student_stars_json_like,:is_anonymous_comments => @is_anonymous_comments,:cur_type => @cur_type,:cur_page => @cur_page}] + #name = @homework.name + #desc = @homework.description + #datetime = @homework.created_at + #files = [] + #unless @homework.attachments.empty? + # attachs = @homework.attachments + # attachs.each do |attach| + # filename = attach.filename + # filedesc = attach.description unless attach.description.blank? + # end + #end + + #{:name => name, :description => desc, :datetime => format_time(datetime)} + end + + #作品打分/留言 + def add_score_and_jour params,current_user + @is_teacher,@is_anonymous_comments,@m_score = params[:is_teacher]=="true",params[:is_anonymous_comments]=="true",params[:stars_value] + @cur_page,@cur_type = params[:cur_page] || 1,params[:cur_type] || 5 + @homework = HomeworkAttach.find(params[:homework_id]) + comment_status = @homework.bid.comment_status + if @is_anonymous_comments && comment_status == 0 + raise '尚未开启匿评!' + end + if @is_anonymous_comments && ((@m_score.nil? || @m_score.blank?) || !(params[:new_form] && params[:new_form][:user_message] && params[:new_form][:user_message] != "")) + raise '您尚未打分或评论!' + end + #保存评分 + homework = @homework + is_teacher = @is_teacher ? 1 : 0 + #保存评分@homework.rate(@m_score.to_i,User.current.id,:quality, (@is_teacher ? 1 : 0)) + if @m_score + rate = @homework.rates(:quality).where(:rater_id => current_user.id, :is_teacher_score => is_teacher).first + if rate + rate.stars = @m_score + rate.save! + else + @homework.rates(:quality).new(:stars => @m_score, :rater_id => current_user.id, :is_teacher_score => is_teacher).save! + end + + if homework.is_teacher_score == 0 + if is_teacher == 1 + homework.score = @m_score + homework.is_teacher_score = 1 + else + sql = "SELECT AVG(stars) as stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = #{homework.id}" + score= HomeworkAttach.find_by_sql(sql).first.stars + homework.score = score + end + else + if is_teacher == 1 + homework.score = @m_score + homework.is_teacher_score = 1 + end + end + homework.save! + end + #保存评论 + @is_comprehensive_evaluation = @is_teacher ? 1 : (@is_anonymous_comments ? 2 : 3) #判断当前评论是老师评论?匿评?留言 + if params[:new_form] && params[:new_form][:user_message] && params[:new_form][:user_message] != "" #有没有留言 + @homework.addjours current_user.id, params[:new_form][:user_message],0,@is_comprehensive_evaluation + end + end + + #作品留言列表 + def get_works_jours_list params + @bid = Bid.find params[:id] + @user = @bid.author + @jours = @bid.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC') + @jour = paginateHelper @jours,10 + @jour + end + + # 学生匿评留言列表 + def anonymous_jour_list params + #jours留言 is null条件用以兼容历史数据 + jours = @homework.journals_for_messages.where("is_comprehensive_evaluation = 3 or is_comprehensive_evaluation is null").order("created_on DESC") + jours.each do |jour| + user = jour.user + img_url = url_to_avatar(jour.user) + datetime = jour.created_on + content = jour.notes + end + {:user => 'user', :img_url => 'img_url', :datetime => 'datetime', :content => 'content'} + end + + # 匿评教师留言/回复列表 + # 图像img_url = url_to_avatar(user) + # massage_user 留言者 + # parent_jour被回复的留言 + def teacher_jour_list params + @homework = HomeworkAttach.find(params[:homework_id]) + @stars_reates = @homework.rates(:quality) + @teacher_stars = @stars_reates.where("rater_id in (#{teachers})") #老师评分列表 + @teacher_stars.each do |ts| + #留言参数 + jour = get_homework_review @homework,true,massage_user + massage_content = jour.notes unless jour.nil? + massage_user = ts.rater + massage_score = ts.stars + #回复参数 + anonymous_repy(jour) + end + end + + # 发布作业 + def create_home_work params,current_user + @bid = Bid.new + @bid.name = params[:work_name] + @bid.description = params[:work_desc] + # @bid.is_evaluation = params[:is_blind_appr] + @bid.evaluation_num = params[:blind_appr_num] + @bid.open_anonymous_evaluation = params[:is_blind_appr] + @bid.reward_type = 3 + @bid.deadline = params[:work_deadline] + @bid.budget = 0 + @bid.author_id = current_user.id + @bid.commit = 0 + @bid.homework_type = 1 + # @bid. + if @bid.save + HomeworkForCourse.create(:course_id => params[:course_id], :bid_id => @bid.id) + unless @bid.watched_by?(current_user) + @bid.add_watcher(current_user) + end + end + end + + # 学生匿评列表 + def student_jour_list params + @homework = HomeworkAttach.find(params[:homework_id]) + @stars_reates = @homework.rates(:quality) + @student_stars = @stars_reates.where("rater_id not in (#{teachers})") #学生评分列表 + @student_stars.each do |ss| + #留言参数 + massage_user = ss.rater + jour = get_homework_review @homework,false,massage_user + massage_score = ss.stars + massage_content = jour.notes unless jour.nil? + #回复参数 + anonymous_repy(jour) + end + end + + def anonymous_repy jour + fetch_user_leaveWord_reply(jour).each do |fulr| + parent_jour = JournalsForMessage.where("id = #{fulr.m_reply_id}").first + reply_name = fulr.user.name + parent_name = parent_jour.user.name if parent_jour + reply_content = fulr.notes + reply_time = fulr.created_on + end + end + + #我的作品列表 + def my_homework_list params,current_user + @user = User.find(params[:user_id]) + if !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 + membership.sort! {|older, newer| newer.created_on <=> older.created_on } + course_list = [] + membership.each do |mp| + my_homeworks = [] + mp.course.homeworks.each do |bid| + #hw = bid.homeworks.where("user_id = #{current_user.id}") + @bid = bid + course = @bid.courses.first + 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 + + my_homeworks << {:course_name => course.name,:id => @bid.id, :author => @bid.author,:author_real_name =>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 + if mp.course.homeworks.count != 0 + course_list << {:course => mp.course,:img_url => url_to_avatar(mp.course),:my_homework => my_homeworks,:current_user_is_member => current_user.member_of_course?(mp.course),:current_user_is_teacher => is_course_teacher(current_user,mp.course)} + end + end + course_list + end +end \ No newline at end of file diff --git a/app/services/users_service.rb b/app/services/users_service.rb new file mode 100644 index 000000000..7e8e775ed --- /dev/null +++ b/app/services/users_service.rb @@ -0,0 +1,308 @@ +class UsersService + include ApplicationHelper + include AccountHelper + include AvatarHelper + include CoursesHelper + include ApiHelper + include WordsHelper + #将用户注册的功能函数写这里 + #参数约定 + #成功返回注册后的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 + #img_url = url_to_avatar(@user) + #gender = @user.user_extensions.gender.nil? ? 0 : @user.user_extensions.gender + #work_unit = get_user_work_unit @user + #location = get_user_location @user + #{: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 + + # 自动注册功能 FOR:邮件邀请 + def register_auto(login,mail,password) + @user = User.new + @user.admin = false + @user.register + @user.login = login + @user.mail = mail + password_confirmation = password + should_confirmation_password = true + 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 + @user = automatically_register_lock(@user) + 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 = get_user_work_unit @user + location = get_user_location @user + {:id => @user.id, :img_url => img_url,:realname => @user.realname, :nickname => @user.login, :gender => gender, :work_unit => work_unit, :mail => @user.mail, :location => location, :brief_introduction => @user.user_extensions.brief_introduction} + end + + #忘记密码 + def lost_password params + user = ::User.find_by_mail(params[:mail].to_s) + # user not found or not active + unless user && user.active? + raise l(:notice_account_unknown_email,:locale => 'zh') + end + # user cannot change its password + unless user.change_password_allowed? + raise l(:notice_can_t_change_password,:locale => user.language) + return + end + # create a new token for password recovery + token = Token.new(:user => user, :action => "recovery") + if token.save + Mailer.run.lost_password(token) + return l(:notice_account_lost_email_sent,:locale => user.language) + end + 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 + #img_url = url_to_avatar(@user) + #gender = @user.user_extensions.gender.nil? ? 0 : @user.user_extensions.gender + #work_unit = get_user_work_unit @user + #location = get_user_location @user + #{: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} + @user + end + + # 获取某个用户的所有留言信息 + def get_all_messages params + user = User.find(params[:user_id]) + jours = user.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC').page(params[:page] || 1).per(10) + jours.update_all(:is_readed => true, :status => false) + jours.each do |journal| + fetch_user_leaveWord_reply(journal).update_all(:is_readed => true, :status => false) + end + jours + end + + # 回复用户 + def reply_user_messages params,current_user + user = User.find(params[:user_id]) + + m_parent_id = params[:parent_id] + author_id = current_user.id + reply_id = params[:ref_user_id] + ref_message_id = params[:ref_message_id] + content = params[:content] + options = {:user_id => author_id, # 作者id + :status => true, + :m_parent_id => m_parent_id,# 父留言id + :m_reply_id => ref_message_id, # 子留言 id + :reply_id => reply_id, # 被留言用户id + :notes => content, + :is_readed => false} + if(params[:type] == 1) + user.add_jour(nil, nil,nil,options) + elsif(params[:type] == 2) + Course.find(params[:course_id]).journals_for_messages.build(options).save! unless params[:course_id].nil? + else + end + + end + + # 给用户留言 + def leave_message params,current_user + obj = User.find(params[:user_id]).add_jour(current_user, params[:content], 0) + obj + end + + + #关注列表 + def user_watcher params + @user = User.find(params[:id]) + User.watched_by(@user.id) + end + + #用户课程列表 + def user_courses_list params,current_user + @user = User.find(params[:id]) + if !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 + membership.sort! {|older, newer| newer.created_on <=> older.created_on } + course_list = [] + membership.each do |mp| + course_list << {:course => mp.course,:img_url => url_to_avatar(mp.course),:current_user_is_member => current_user.member_of_course?(mp.course),:current_user_is_teacher => is_course_teacher(current_user,mp.course)} + end + course_list + 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 l(:notice_account_wrong_password,:locale => 'zh') + 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] : "0" + if params[:is_search_assitant].nil? + #modify by yutao 2015/5/18 没有params[:user_id]参数时去掉"id not in (?)"条件(bug:#2270) start + #say by yutao: params[:user_id]这个是指谁发起的搜索么? 如果是 这个值貌似应该从session获取 怪怪的赶脚-_-! + if params[:name].present? + if !params[:user_id].nil? + watcher = User.watched_by(params[:user_id]) + watcher.push(params[:user_id]) + scope = scope.where("id not in (?)",watcher) + end + #scope = scope.like(params[:name],search_by) + scope = scope.where("( LOWER(login) LIKE ? or LOWER(concat(lastname, firstname)) LIKE ? or LOWER(mail) LIKE ? )", + "%#{params[:name]}%","%#{params[:name]}%","%#{params[:name]}%") + end + #modify by yutao 2015/5/18 没有params[:user_id]参数时去掉"id not in (?)"条件 end + else + teachers = searchTeacherAndAssistant(Course.find(params[:course_id])) + scope = scope.where("id not in (?)",teachers.map{|t| t.user_id}).like(params[:name],search_by) if params[:name].present? + end + scope + end + + # 课程留言中与我相关的回复 + def my_course_messages params,current_user + #找到我所有的课程 + @user = current_user + if !current_user.admin? && !@user.active? + raise '404' + return + end + if current_user == @user || current_user.admin? + membership = @user.coursememberships.all + end + # membership.sort! {|older, newer| newer.created_on <=> older.created_on } + message_list = [] + membership.each do |mp| + #课程轮询找到与我相关的回复 + message_list << mp.course.journals_for_messages.where("reply_id = ?",current_user.id) + end + message_list + end + + # 获取与我相关的留言:我的留言,回复我的留言 + def my_personal_messages params,current_user + jours = current_user.journals_for_messages.where('m_parent_id is null or reply_id = ?',current_user.id) + jours.update_all(:is_readed => true, :status => false) + jours + end + + # 所有的与我相关 + def reply_my_messages params,current_user + jours = my_personal_messages params,current_user + jours1 = my_course_messages params,current_user + my_jours = [] + my_jours << jours << jours1 + my_jours.flatten!.sort! {|older, newer| newer.created_on <=> older.created_on } + my_jours_arr = Kaminari.paginate_array(my_jours, total_count: my_jours.count).page(params[:page] || 1).per(10) + my_jours_arr + end + +end diff --git a/app/services/watches_service.rb b/app/services/watches_service.rb new file mode 100644 index 000000000..2aab7de81 --- /dev/null +++ b/app/services/watches_service.rb @@ -0,0 +1,62 @@ +#coding=utf-8 +class WatchesService + def watch params + @current_user = User.find(params[:current_user_id]) + if params[:object_type] == 'user' && params[:current_user_id] == params[:object_id] + raise '不能关注自己!' + end + @watchables = find_watchables params + + if @watchables.nil? + raise '404' + end + set_watcher(@watchables, @current_user, true) + end + + def unwatch params + @current_user = User.find(params[:current_user_id]) + @watchables = find_watchables params + if @watchables.nil? + raise '404' + end + set_watcher(@watchables, @current_user, false) + end + + def find_watchables params + #根据参数获取关注对象的类型(user、project) + klass = Object.const_get(params[:object_type].camelcase) rescue nil + #判断获取的对象类型能否响应‘watched_by’方法 + if klass && klass.respond_to?('watched_by') + @watchables = klass.find_all_by_id(Array.wrap(params[:object_id])) + raise Unauthorized if @watchables.any? {|w| w.respond_to?(:visible?) && !w.visible?} + end + @watchables.present? ? @watchables : nil + end + + def set_watcher(watchables, user, watching) + watchables.each do |watchable| + watchable.set_watcher(user, watching) + # @user = watchable # added by william + if watching + # 修改 user和project的状态 + if watchable.instance_of?(User) + #写user_statuses表 + UserStatus.find_by_user_id(watchable.id).update_watchers_count(1) + elsif watchable.instance_of?(Project) + #写project_statuese表 + ProjectStatus.find_by_project_id(watchable.id).update_watchers_count(1) + end + else + # 修改 user和project的状态 + if watchable.instance_of?(User) + #写user_statuses表 + UserStatus.find_by_user_id(watchable.id).update_watchers_count(-1) + elsif watchable.instance_of?(Project) + #写project_statuese表 :project_status + ProjectStatus.find_by_project_id(watchable.id).update_watchers_count(-1) + end + end + end + watchables + end +end \ No newline at end of file diff --git a/app/tasks/destroy_repository_task.rb b/app/tasks/destroy_repository_task.rb new file mode 100644 index 000000000..3e434422e --- /dev/null +++ b/app/tasks/destroy_repository_task.rb @@ -0,0 +1,25 @@ +#coding=utf-8 +# + +class DestroyRepositoryTask + def destroy(user_id, rep_id) + user = User.find(user_id) + repository = Repository.find(rep_id) + + Rails.logger.info "start delete repository #{user} #{repository}" + @root_path=RepositoriesHelper::ROOT_PATH + @repo_name=user.login.to_s+"_"+repository.identifier.to_s + @repository_name=user.login.to_s+"/"+repository.identifier.to_s+".git" + @middle=user.login.to_s+"_"+repository.identifier.to_s+"-write:" + repository.destroy + if(repository.type=="Repository::Git") + Rails.logger.info "destory the repository value"+"root path"+@root_path+"repo_name"+@repo_name+ + "repository_name"+@repository_name+"user group"+@middle + system "sed -i /"+@repo_name+"/{d} "+@root_path+"htdocs/user.passwd" + system "sed -i /"+@middle+"/{d} "+@root_path+"htdocs/group.passwd" + system "rm -r "+@root_path+"htdocs/"+@repository_name + end + end + + handle_asynchronously :destroy,:queue => 'repository' +end diff --git a/app/tasks/office_conver_task.rb b/app/tasks/office_conver_task.rb new file mode 100644 index 000000000..da950bf2e --- /dev/null +++ b/app/tasks/office_conver_task.rb @@ -0,0 +1,12 @@ +#coding=utf-8 +# +class OfficeConverTask + def conver(source_file, saved_file) + office = Trustie::Utils::Office.new(source_file) + if office.conver(saved_file) + Rails.logger.info "process ok: #{saved_file} " + end + end + handle_asynchronously :conver,:queue => 'office_conver' +end + diff --git a/app/views/account/email_valid.html.erb b/app/views/account/email_valid.html.erb index e6bc2a2db..1aecc6904 100644 --- a/app/views/account/email_valid.html.erb +++ b/app/views/account/email_valid.html.erb @@ -1,7 +1,7 @@ - 注册帐号 +<%= l(:label_regiter_account)%> +<% if defined?(container) && container && container.saved_attachments %> <% container.attachments.each_with_index do |attachment, i| %> - - + + <% end %> + <% container.saved_attachments.each_with_index do |attachment, i| %> + + <% end %> <% end %>diff --git a/app/views/attachments/_form_course.html.erb b/app/views/attachments/_form_course.html.erb index b2089d629..b0ab39b5a 100644 --- a/app/views/attachments/_form_course.html.erb +++ b/app/views/attachments/_form_course.html.erb @@ -1,27 +1,31 @@ - +