socialforge/app/controllers/test_controller.rb

161 lines
7.2 KiB
Ruby
Raw Normal View History

2015-06-01 23:02:55 +08:00
require 'net/http'
2013-08-12 17:30:31 +08:00
class TestController < ApplicationController
2014-04-22 14:53:25 +08:00
helper :UserScore
2014-05-23 17:07:58 +08:00
layout 'bootstrap_base'
2013-08-12 17:30:31 +08:00
2014-05-24 12:11:14 +08:00
def bootstrap; end
2015-06-01 23:02:55 +08:00
def view_office
end
2014-04-14 17:36:23 +08:00
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|
2014-04-15 14:33:19 +08:00
length = attach.storage_path.length
homeworks_attach_path << attach.diskfile.to_s.slice((length+1)..-1)
2014-04-14 17:36:23 +08:00
end
2013-08-12 17:30:31 +08:00
end
2014-04-14 17:36:23 +08:00
@paths = homeworks_attach_path
zipfile = ziping homeworks_attach_path
send_file zipfile, :filename => bid.name,
:type => detect_content_type(zipfile)
2014-04-15 21:13:56 +08:00
rescue Errno::ENOENT => e
logger.error "[Errno::ENOENT] ===> #{e}"
2014-04-14 17:36:23 +08:00
end
def courselist
2014-07-09 15:20:00 +08:00
@courses = paginateHelper Course.includes(:homeworks).all, 10
2014-04-14 17:36:23 +08:00
end
def ziping files_path
2014-04-15 21:13:56 +08:00
ic = Iconv.new('GBK//IGNORE', 'UTF-8//IGNORE')
2014-04-14 21:59:32 +08:00
folder = "#{Rails.root}/files"
2014-04-14 17:36:23 +08:00
input_filename = files_path
2014-04-15 14:42:00 +08:00
zipfile_name = "#{Rails.root}/tmp/archiveZip/archive_#{Time.now.to_i}.zip"
2014-04-15 21:13:56 +08:00
2014-04-16 10:42:49 +08:00
Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name))
2014-04-14 17:36:23 +08:00
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
input_filename.each do |filename|
2014-04-15 21:13:56 +08:00
zipfile.add(ic.iconv(filename_to_real(File.basename(filename))), folder + '/' + filename)
2014-04-14 17:36:23 +08:00
end
2014-04-15 14:33:19 +08:00
zipfile.get_output_stream("ReadMe"){ |os|
2014-04-14 21:59:32 +08:00
os.write "Homeworks"
2014-04-14 17:36:23 +08:00
}
end
zipfile_name
end
def detect_content_type(name)
content_type = Redmine::MimeType.of(name)
content_type.to_s
end
2014-04-15 21:13:56 +08:00
def filename_to_real name
attach = Attachment.find_by_disk_filename(name)
attach.filename
end
2015-04-23 17:42:03 +08:00
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
2014-04-15 21:13:56 +08:00
2015-04-23 17:42:03 +08:00
end