diff --git a/app/api/mobile/apis/new_comment.rb b/app/api/mobile/apis/new_comment.rb index 3c57636b5..f3c337cd1 100644 --- a/app/api/mobile/apis/new_comment.rb +++ b/app/api/mobile/apis/new_comment.rb @@ -75,15 +75,9 @@ module Mobile authenticate! - subscribe = 0 #默认未关注 - - #-------------------获取用户是否关注此公众号----------------------------- - openid = session[:wechat_openid] - raise "无法获取到openid,请在微信中打开本页面" unless openid - user_info = Wechat.api.user(openid) - Rails.logger.info "user_info!!!!!!!!!" - Rails.logger.info user_info - subscribe = user_info["subscribe"] + ## 能进来的就是已关注 + ## 因为取消订阅的记录被删除了 + subscribe = 1 #默认未关注 status = 0 tip = 0 #0班级1项目 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 20e0463d0..4f723fb28 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -35,11 +35,11 @@ class UsersController < ApplicationController # before_filter :can_show_course, :only => [:user_courses,:user_homeworks] - before_filter :find_user, :only => [:user_fanslist, :user_watchlist, :show, :edit, :update, :destroy, :edit_membership, :user_courses, :unsolved_issues_list, :unfinished_homework_list, + before_filter :find_user, :only => [:user_fanslist, :user_watchlist, :show, :edit, :update, :destroy, :edit_membership, :user_courses, :unsolved_issues_list, :unfinished_homework_list, :user_manage_homeworks, :unfinished_poll_list, :user_homeworks,:student_homeworks, :destroy_membership, :user_activities, :user_projects, :user_newfeedback, :user_comments, - :anonymous_evaluation_list,:unfinished_test_list, :watch_contests, :info, :watch_projects, :show_score, :topic_score_index, :project_score_index, + :anonymous_evaluation_list,:unfinished_test_list, :watch_contests, :info, :watch_projects, :show_score, :topic_score_index, :project_score_index, :user_receive_homeworks, :unapproval_applied_list, :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,:user_resource,:user_contestlist, + :activity_new_score_index, :influence_new_score_index, :score_new_index,:user_projects_index,:user_resource,:user_contestlist, :user_manage_issues, :user_receive_issues, :user_courses4show,:user_projects4show,:user_contests4show,:user_course_activities,:user_project_activities,:user_feedback4show,:user_visitorlist,:user_messages,:edit_brief_introduction, :user_import_homeworks,:user_search_homeworks,:user_import_resource, :user_system_messages,:choose_user_course,:user_courselist,:user_projectlist,:sort_syllabus_list, :sort_project_list,:my_homeworks,:manage_or_receive_homeworks,:search_m_r_homeworks, :cancel_or_collect,:expand_courses,:homepage, :user_issues, :course_community, :project_community, :contest_community] @@ -150,6 +150,98 @@ class UsersController < ApplicationController end end +# 我发布的issue +def user_manage_issues + @manage_issues = "我发布的Issue" + # 排序(默认以更新时间降序) + order = "updated_on desc" + if params[:reorder_release_time] + order = "created_on #{params[:reorder_release_time]}" + elsif params[:reorder_turnover_time] + order = "updated_on #{params[:reorder_turnover_time]}" + end + + @subject = params[:subject] + params[:assigned_to_id].to_i == 0 ? @assigned_to = nil : @assigned_to = params[:assigned_to_id].to_i + params[:author_id].to_i == 0 ? author_id = nil : author_id = params[:author_id].to_i + params[:project_id].to_i == 0 ? @project_id = nil : @project_id = params[:project_id] + if @project_id.nil? + @issues = Issue.where("author_id =? and subject like ?", + @user.id, "%#{@subject}%").order(order) + else + @issues = Issue.where("author_id =? and project_id=? and subject like ?", + @user.id, @project_id, "%#{@subject}%").order(order) + end + @issues_filter = Issue.where("author_id =?", @user.id).order('updated_on desc') + @issue_open_count = Issue.where(:author_id => @user.id, :status_id => [1, 2, 3, 4, 6]).count + @issue_close_count = Issue.where(:author_id => @user.id, :status_id => 5).count + + # 导出excel的issues + @excel_issues = @issues + @issue_count = @issues.count + @limit = 10 + @is_remote = true + @issue_pages = Paginator.new @issue_count, @limit, params['page'] || 1 + @offset ||= @issue_pages.offset + @issues = paginateHelper @issues, @limit + # @issues.limit(@issue_pages.per_page).offset(@issue_pages.offset).reorder(order).all + respond_to do |format| + format.html{render :layout => 'static_base'} + format.xls{ + filename = "我发布的_#{l(:label_issue_list_xls)}.xls" + send_data(issue_list_xls(@excel_issues), :type => 'application/octet-stream', :filename => filename_for_content_disposition(filename)) + } + format.api + format.js + end +end + +# 我收到的issue +def user_receive_issues + @receive_issues = "我收到的Issue" + # 排序(默认以更新时间降序) + order = "updated_on desc" + if params[:reorder_release_time] + order = "created_on #{params[:reorder_release_time]}" + elsif params[:reorder_turnover_time] + order = "updated_on #{params[:reorder_turnover_time]}" + end + + @subject = params[:subject] + params[:assigned_to_id].to_i == 0 ? @assigned_to = nil : @assigned_to = params[:assigned_to_id].to_i + params[:author_id].to_i == 0 ? author_id = nil : author_id = params[:author_id].to_i + params[:project_id].to_i == 0 ? @project_id = nil : @project_id = params[:project_id] + if @project_id.nil? + @issues = Issue.where("assigned_to_id =? and subject like ?", + @user.id, "%#{@subject}%").order(order) + else + @issues = Issue.where("assigned_to_id =? and project_id=? and subject like ?", + @user.id, @project_id, "%#{@subject}%").order(order) + end + @issues_filter = Issue.where("assigned_to_id =?", @user.id).order('updated_on desc') + @issue_open_count = Issue.where(:assigned_to_id => @user.id, :status_id => [1, 2, 3, 4, 6]).count + @issue_close_count = Issue.where(:assigned_to_id => @user.id, :status_id => 5).count + + # 导出excel的issues + @excel_issues = @issues + @issue_count = @issues.count + @limit = 10 + @is_remote = true + @issue_pages = Paginator.new @issue_count, @limit, params['page'] || 1 + @offset ||= @issue_pages.offset + @issues = paginateHelper @issues, @limit + # @issues.limit(@issue_pages.per_page).offset(@issue_pages.offset).reorder(order).all + respond_to do |format| + format.html{render :layout => 'static_base'} + format.xls{ + filename = "我发布的_#{l(:label_issue_list_xls)}.xls" + send_data(issue_list_xls(@excel_issues), :type => 'application/octet-stream', :filename => filename_for_content_disposition(filename)) + } + format.api + format.js + end +end + #展开所有回复 def show_all_replies case params[:type] @@ -362,7 +454,7 @@ class UsersController < ApplicationController messages.each do |message_all| # 未读的消息存放在数组 mess = message_all.message - if (message_all.message_type != "SystemMessage"&& !mess.nil? && (mess.viewed == 0 || !mess.viewed)) || (message_all.message_type == "SystemMessage"&& !mess.nil? && mess.created_at > onclick_time) + if (message_all.message_type != "SystemMessage" && !mess.nil? && (mess.viewed == 0 || !mess.viewed)) || (message_all.message_type == "SystemMessage"&& !mess.nil? && mess.created_at > onclick_time) unless (message_all.message_type == 'CourseMessage' && mess && mess.course && mess.course.is_delete == 1) @message_alls << mess end @@ -1738,6 +1830,72 @@ class UsersController < ApplicationController end end + # 用户发布的作业 + def user_manage_homeworks + @manage_homeworks = "我发布的作业" + @order,@b_sort,@type = params[:order] || "created_at",params[:sort] || "desc", 1 + @r_sort = @b_sort == "desc" ? "asc" : "desc" + @type = @type.to_i + tea_courses = @user.courses.visible.not_deleted.select{|course| @user.has_teacher_role(course)} + tea_course_ids = tea_courses.empty? ? "(-1)" : "(" + tea_courses.map{|course| course.id}.join(',') + ")" + @homeworks = HomeworkCommon.where("course_id in #{tea_course_ids}").order("#{@order} #{@b_sort}") + if params[:property] + all_homework_ids = @homeworks.empty? ? "(-1)" : "(" + @homeworks.map{|h| h.id}.join(",") + ")" + if params[:property] == "1" + @homeworks = HomeworkCommon.find_by_sql("select * from homework_commons where id in #{all_homework_ids} and homework_type = 1") + elsif params[:property] == "2" + @homeworks = HomeworkCommon.find_by_sql("select * from homework_commons where id in #{all_homework_ids} and homework_type = 2") + elsif params[:property] == "3" + @homeworks = HomeworkCommon.find_by_sql("select * from homework_commons where id in #{all_homework_ids} and homework_type = 3") + end + end + @limit = 10 + @is_remote = true + @hw_count = @homeworks.count + @hw_pages = Paginator.new @hw_count, @limit, params['page'] || 1 + @offset ||= @hw_pages.offset + @homeworks = paginateHelper @homeworks,@limit + @search = "" + @property = params[:property] + respond_to do |format| + format.js + format.html {render :layout => 'static_base'} + end + end + + # 用户收到的作业 + def user_receive_homeworks + @receive_homeworks = "我收到的作业" + @order,@b_sort,@type = params[:order] || "created_at",params[:sort] || "desc", 2 + @r_sort = @b_sort == "desc" ? "asc" : "desc" + @type = @type.to_i + stu_courses = @user.courses.visible.not_deleted.select{|course| @user.has_student_role(course)} + stu_course_ids = stu_courses.empty? ? "(-1)" : "(" + stu_courses.map{|course| course.id}.join(',') + ")" + @homeworks = HomeworkCommon.where("course_id in #{stu_course_ids} and publish_time <= '#{Date.today}'").order("#{@order} #{@b_sort}") + if params[:property] + all_homework_ids = @homeworks.empty? ? "(-1)" : "(" + @homeworks.map{|h| h.id}.join(",") + ")" + if params[:property] == "1" + @homeworks = HomeworkCommon.find_by_sql("select * from homework_commons where id in #{all_homework_ids} and homework_type = 1") + elsif params[:property] == "2" + @homeworks = HomeworkCommon.find_by_sql("select * from homework_commons where id in #{all_homework_ids} and homework_type = 2") + elsif params[:property] == "3" + @homeworks = HomeworkCommon.find_by_sql("select * from homework_commons where id in #{all_homework_ids} and homework_type = 3") + end + end + @limit = 10 + @is_remote = true + @hw_count = @homeworks.count + @hw_pages = Paginator.new @hw_count, @limit, params['page'] || 1 + @offset ||= @hw_pages.offset + @homeworks = paginateHelper @homeworks,@limit + @search = "" + @property = params[:property] + respond_to do |format| + format.js + format.html {render :layout => 'static_base'} + end + end + #我管理/收到的作业 def manage_or_receive_homeworks @order,@b_sort,@type = params[:order] || "created_at",params[:sort] || "desc",params[:type] || 1 diff --git a/app/controllers/wechats_controller.rb b/app/controllers/wechats_controller.rb index 25ce1f83e..07177736e 100644 --- a/app/controllers/wechats_controller.rb +++ b/app/controllers/wechats_controller.rb @@ -515,8 +515,8 @@ class WechatsController < ActionController::Base private def get_openid_from_code(code) - if code =='only-for-test' - openid = 'o3ss_wHOOnHkz1khBJxH8RF4SfPY' + if code =='test' + openid = 'orgVLv8TlS6e7FDiI6xdTGHRaaRo' session[:wechat_openid] = openid return openid end @@ -545,6 +545,8 @@ class WechatsController < ActionController::Base return openid end + ## 能进来的就是已关注 + ## 因为取消订阅的记录被删除了 def user_binded?(openid) uw = UserWechat.where(openid: openid).first if uw && uw.bindtype == 0 diff --git a/app/controllers/zipdown_controller.rb b/app/controllers/zipdown_controller.rb index 15eccba1e..6ed1a2e4b 100644 --- a/app/controllers/zipdown_controller.rb +++ b/app/controllers/zipdown_controller.rb @@ -1,18 +1,16 @@ #coding=utf-8 -require "base64" -require 'zip' - class ZipdownController < ApplicationController #查找项目(课程) before_filter :find_project_by_bid_id, :only => [:assort] #检查权限 #勿删 before_filter :authorize, :only => [:assort,:download_user_homework] - SAVE_FOLDER = "#{Rails.root}/files" - OUTPUT_FOLDER = "#{Rails.root}/files/archiveZip" - MAX_PATH = 50 + ## 200M + MAX_DOWN_SIZE = 200 * 1024 * 1024 - #统一下载功能 + include ZipService + + #统一下载功能 def download if User.current.logged? begin @@ -35,34 +33,21 @@ class ZipdownController < ApplicationController 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 - zipfile = {:message => "no file"} - end + zipfile = checkfileSize(bid.homeworks) { + zip_bid bid + } 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 + zipfile = checkfileSize(homework.student_works) { + zip_homework_common homework + } elsif params[:obj_class] == "Work" homework = Work.find params[:obj_id] render_403 if User.current.admin_of_contest?(homework.contest) - file_count = 0 - homework.contestant_works.map { |work| file_count += work.attachments.count} - if file_count > 0 - zipfile = zip_homework_common homework - else - zipfile = {:message => "no file"} - end - + zipfile = checkfileSize(homework.contestant_works) { + zip_homework_common homework + } else logger.error "[ZipDown#assort] ===> #{params[:obj_class]} unKown !!" end @@ -98,7 +83,6 @@ class ZipdownController < ApplicationController end private - #通过作业Id找到项目(课程) def find_project_by_bid_id obj_class = params[:obj_class] @@ -110,275 +94,23 @@ class ZipdownController < ApplicationController end end - def zip_bid(bid) - # Todo: User Access Controll - bid_homework_path = [] - digests = [] - bid.homeworks.each do |homeattach| - unless homeattach.attachments.empty? - 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), - base64file: encode64(File.basename(out_file.file_path)), - size:(out_file.pack_size / 1024.0 / 1024.0).round(2) - }] - end - - def encode64(str) - Base64.urlsafe_encode64(str) - end - - def decode64(str) - Base64.urlsafe_decode64(str) - 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), - base64file: encode64(File.basename(out_file.file_path)), - size:(out_file.pack_size / 1024.0 / 1024.0).round(2) - }] - end - - def zip_contest_work homework_common - bid_homework_path = [] - digests = [] - homework_common.contestant_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), - base64file: encode64(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 - 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 make_zip_name(work) - "#{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}" - end - - def zip_student_work_by_user(work) - homeworks_attach_path = [] - not_exist_file = [] - # 需要将所有homework.attachments遍历加入zip - 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' + def checkfileSize(works) + file_count = 0 + file_size = 0 + works.each do |work| + file_count += work.attachments.count + work.attachments.each do |attach| + file_size += attach.filesize end end - #单个文件的话,不需要压缩,只改名 - out_file = nil - if homeworks_attach_path.size == 1 - out_file = find_or_pack(work.homework_common_id, work.user_id, digests.sort){ - des_path = "#{OUTPUT_FOLDER}/#{make_zip_name(work)}_#{File.basename(homeworks_attach_path.first)}" - FileUtils.cp homeworks_attach_path.first, des_path - des_path - } + if file_size > MAX_DOWN_SIZE + {err: -1, message: 'file size to large'} + elsif file_count > 0 + yield if block_given? else - out_file = find_or_pack(work.homework_common_id, work.user_id, digests.sort){ - zipping("#{make_zip_name(work)}.zip", - homeworks_attach_path, OUTPUT_FOLDER, true, not_exist_file) - } + {err: -2, :message => "no file"} end - out_file - end - def zip_student_work_by_user(work) - homeworks_attach_path = [] - not_exist_file = [] - # 需要将所有homework.attachments遍历加入zip - 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 - - #单个文件的话,不需要压缩,只改名 - out_file = nil - if homeworks_attach_path.size == 1 - out_file = find_or_pack(work.homework_common_id, work.user_id, digests.sort){ - des_path = "#{OUTPUT_FOLDER}/#{make_zip_name(work)}_#{File.basename(homeworks_attach_path.first)}" - FileUtils.cp homeworks_attach_path.first, des_path - des_path - } - else - out_file = find_or_pack(work.homework_common_id, work.user_id, digests.sort){ - zipping("#{make_zip_name(work)}.zip", - homeworks_attach_path, OUTPUT_FOLDER, true, not_exist_file) - } - end - out_file - - end - - - 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" - # 文件名过长 - - if rename_zipfile.size > MAX_PATH - rename_zipfile = rename_zipfile[0,rename_zipfile.size-4][0,MAX_PATH-4] + rename_zipfile[-4,4] - end - - 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| - 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) - rescue Exception => e - 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'){|os| os.write l(:label_file_lost) + not_exist_file.join(',').to_s} - end - end - zipfile_name - 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 - end - - def filename_to_real(name) - attach = Attachment.find_by_disk_filename(name) - attach.filename - end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e7fed44fb..a40254b97 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1348,6 +1348,14 @@ module ApplicationHelper title << @course_community elsif !@contest_community.blank? title << @contest_community + elsif !@manage_issues.blank? + title << @manage_issues + elsif !@receive_issues.blank? + title << @receive_issues + elsif !@manage_homeworks.blank? + title << @manage_homeworks + elsif !@receive_homeworks.blank? + title << @receive_homeworks else title << @user.try(:realname) end @@ -2920,6 +2928,20 @@ module ApplicationHelper @user_course_total = @my_joined_course_count + @my_course_count end + # 用户发布的作业数 + def user_manage_homework_count + tea_courses = @user.courses.visible.not_deleted.select{|course| @user.has_teacher_role(course)} + tea_course_ids = tea_courses.map{|course| course.id} + @manage_homeworks = HomeworkCommon.where(:course_id => tea_course_ids).count + end + + # 用户收到的作业数 + def user_receive_homework_count + stu_courses = @user.courses.visible.not_deleted.select{|course| @user.has_student_role(course)} + stu_course_ids = stu_courses.empty? ? "(-1)" : "(" + stu_courses.map{|course| course.id}.join(',') + ")" + @homeworks = HomeworkCommon.where("course_id in #{stu_course_ids} and publish_time <= '#{Date.today}'").count + end + # 用户发布的issue数 def issues_author_is_self_count @issues = Issue.where( :author_id => @user.id ) diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index bda7831fe..b2fd4161c 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -130,7 +130,7 @@ module IssuesHelper end def options_for_version_isuue_list(project) - versions = Version.where(:project_id => project, :status => "open").map{|version| [version.name, version.id]}.unshift(["里程碑", 0]) + versions = Version.where(:project_id => project, :status => "open").order("created_on desc").map{|version| [version.name, version.id]}.unshift(["里程碑", 0]) end def render_issue_subject_with_tree(issue) diff --git a/app/models/memo.rb b/app/models/memo.rb index a580f02a8..3c36875d1 100644 --- a/app/models/memo.rb +++ b/app/models/memo.rb @@ -203,7 +203,7 @@ class Memo < ActiveRecord::Base end def destroyable_by? user - (user && self.author == user) || user.admin? + (user && self.author == user) || user.admin? || self.forum.creator == user #self.author == user || user.admin? end diff --git a/app/models/news.rb b/app/models/news.rb index 4c0e58238..aa04cea08 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -235,6 +235,8 @@ class News < ActiveRecord::Base vs << {course_message_type:'News',course_message_id:self.id, :user_id => m.user_id, :course_id => self.course_id, :viewed => false} + #delayed_job卡住的原因是一次执行的条数太多,导致超时。 + #现在把每次只执行不超过30条,就不会超了。 if vs.size >= 30 self.delay.contain_news_message(vs) vs.clear diff --git a/app/services/zip_service.rb b/app/services/zip_service.rb new file mode 100644 index 000000000..0866033bd --- /dev/null +++ b/app/services/zip_service.rb @@ -0,0 +1,281 @@ +require "base64" +require 'zip' + +module ZipService + + SAVE_FOLDER = "#{Rails.root}/files" + OUTPUT_FOLDER = "#{Rails.root}/files/archiveZip" + MAX_PATH = 50 + + def zip_bid(bid) + # Todo: User Access Controll + bid_homework_path = [] + digests = [] + bid.homeworks.each do |homeattach| + unless homeattach.attachments.empty? + 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), + base64file: encode64(File.basename(out_file.file_path)), + size:(out_file.pack_size / 1024.0 / 1024.0).round(2) + }] + end + + def encode64(str) + Base64.urlsafe_encode64(str) + end + + def decode64(str) + Base64.urlsafe_decode64(str) + 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), + base64file: encode64(File.basename(out_file.file_path)), + size:(out_file.pack_size / 1024.0 / 1024.0).round(2) + }] + end + + def zip_contest_work homework_common + bid_homework_path = [] + digests = [] + homework_common.contestant_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), + base64file: encode64(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 + 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 make_zip_name(work) + "#{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}" + end + + def zip_student_work_by_user(work) + homeworks_attach_path = [] + not_exist_file = [] + # 需要将所有homework.attachments遍历加入zip + 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 + + #单个文件的话,不需要压缩,只改名 + out_file = nil + if homeworks_attach_path.size == 1 + out_file = find_or_pack(work.homework_common_id, work.user_id, digests.sort){ + des_path = "#{OUTPUT_FOLDER}/#{make_zip_name(work)}_#{File.basename(homeworks_attach_path.first)}" + FileUtils.cp homeworks_attach_path.first, des_path + des_path + } + else + out_file = find_or_pack(work.homework_common_id, work.user_id, digests.sort){ + zipping("#{make_zip_name(work)}.zip", + homeworks_attach_path, OUTPUT_FOLDER, true, not_exist_file) + } + end + out_file + + end + + def zip_student_work_by_user(work) + homeworks_attach_path = [] + not_exist_file = [] + # 需要将所有homework.attachments遍历加入zip + 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 + + #单个文件的话,不需要压缩,只改名 + out_file = nil + if homeworks_attach_path.size == 1 + out_file = find_or_pack(work.homework_common_id, work.user_id, digests.sort){ + des_path = "#{OUTPUT_FOLDER}/#{make_zip_name(work)}_#{File.basename(homeworks_attach_path.first)}" + FileUtils.cp homeworks_attach_path.first, des_path + des_path + } + else + out_file = find_or_pack(work.homework_common_id, work.user_id, digests.sort){ + zipping("#{make_zip_name(work)}.zip", + homeworks_attach_path, OUTPUT_FOLDER, true, not_exist_file) + } + end + out_file + + end + + + 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" + # 文件名过长 + + if rename_zipfile.size > MAX_PATH + rename_zipfile = rename_zipfile[0,rename_zipfile.size-4][0,MAX_PATH-4] + rename_zipfile[-4,4] + end + + 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| + 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) + rescue Exception => e + 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'){|os| os.write l(:label_file_lost) + not_exist_file.join(',').to_s} + end + end + zipfile_name + 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 + end + + def filename_to_real(name) + attach = Attachment.find_by_disk_filename(name) + attach.filename + end +end \ No newline at end of file diff --git a/app/views/layouts/_show_messages_list.html.erb b/app/views/layouts/_show_messages_list.html.erb index 654222978..a3ec254d3 100644 --- a/app/views/layouts/_show_messages_list.html.erb +++ b/app/views/layouts/_show_messages_list.html.erb @@ -194,6 +194,10 @@
<%= format_time(activity.created_on) %> 发布
+<%= format_time(activity.updated_on) %> 更新
+- 来源: - - <%= link_to homework.course.name, course_path(homework.course), :target => '_blank', :class => 'hw_cgrey' %> - - 类别:<%=homework.homework_type_ch %> + <%= cur_homework_end_time homework %> <% my_work = homework.student_works.where("user_id = ? && work_status != 0",User.current).first %> 状态:<%= my_work ? '已提交' : '未提交' %> - 作品:<%=homework.student_works.has_committed.count %> 成绩: <% if my_work && !my_work.work_score.nil? %> <%= format("%.1f",my_work.work_score.to_f) %> <% else %> -- <% end %> - 发布时间:<%= format_date homework.publish_time %> - <%=cur_homework_end_time homework %>
<%= l(:label_no_data) %>
+ <% else %> +<%= l(:label_no_data) %>
+ <% else %> +