require 'net/http' class TestController < ApplicationController helper :UserScore layout 'bootstrap_base' def bootstrap; end def view_office end def zip homeworks_attach_path = [] homework_id = params[:homework_id] bid = Bid.find_by_id(homework_id) bid.homeworks.each do |homeattach| homeattach.attachments.each do |attach| length = attach.storage_path.length homeworks_attach_path << attach.diskfile.to_s.slice((length+1)..-1) end end @paths = homeworks_attach_path zipfile = ziping homeworks_attach_path send_file zipfile, :filename => filename_for_content_disposition(bid.name), :type => detect_content_type(zipfile) rescue Errno::ENOENT => e logger.error "[Errno::ENOENT] ===> #{e}" end def courselist @courses = paginateHelper Course.includes(:homeworks).all, 10 end def ziping files_path ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE') folder = "#{Rails.root}/files" input_filename = files_path zipfile_name = "#{Rails.root}/tmp/archiveZip/archive_#{Time.now.to_i}.zip" 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| zipfile.add(ic.iconv(filename_to_real(File.basename(filename))), folder + '/' + filename) end zipfile.get_output_stream("ReadMe"){ |os| os.write "Homeworks" } end zipfile_name 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 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