添加课程消息迁移

This commit is contained in:
huang 2015-08-29 16:08:09 +08:00
parent 8e70bc22e0
commit 353e2f5d18
8 changed files with 161 additions and 36 deletions

View File

@ -155,7 +155,7 @@ class UsersController < ApplicationController
@message_alls = CourseMessage.where("course_message_type =? and user_id =?", "JournalsForMessage", @user).order("created_at desc") @message_alls = CourseMessage.where("course_message_type =? and user_id =?", "JournalsForMessage", @user).order("created_at desc")
@message_alls_count = @message_alls.count @message_alls_count = @message_alls.count
when 'issue' when 'issue'
@message_alls = ForgeMessage.where("forge_message_type =? and user_id =?", "Issue", @user).order("created_at desc") @message_alls = ForgeMessage.where("forge_message_type =? and user_id =?", "Issue", @user).reverse
@message_alls_count = @message_alls.count @message_alls_count = @message_alls.count
when 'issue_update' # 缺陷状态更新、留言 when 'issue_update' # 缺陷状态更新、留言
@message_alls = ForgeMessage.where("forge_message_type =? and user_id =?", "Journal", @user).order("created_at desc") @message_alls = ForgeMessage.where("forge_message_type =? and user_id =?", "Journal", @user).order("created_at desc")

View File

@ -11,6 +11,7 @@ class CourseMessage < ActiveRecord::Base
validates :course_id,presence: true validates :course_id,presence: true
validates :course_message_id,presence: true validates :course_message_id,presence: true
validates :course_message_type, presence: true validates :course_message_type, presence: true
validates :content, length: {maximum: 1000}
after_create :add_user_message after_create :add_user_message
def add_user_message def add_user_message

View File

@ -92,7 +92,7 @@ class Project < ActiveRecord::Base
has_many :tags, :through => :project_tags, :class_name => 'Tag' has_many :tags, :through => :project_tags, :class_name => 'Tag'
has_many :project_tags, :class_name => 'ProjectTags' has_many :project_tags, :class_name => 'ProjectTags'
# 关联虚拟表 # 关联虚拟表
has_many :forge_messages has_many :forge_messages, :class_name =>'ForgeMessage', :as => :forge_message, :dependent => :destroy
belongs_to :organization belongs_to :organization

View File

@ -98,7 +98,7 @@
<% unless ma.content.nil? %> <% unless ma.content.nil? %>
<%= link_to ma.content.html_safe, student_work_index_path(:homework => ma.course_message.student_work.homework_common_id),:class=>"newsGrey",:title => "#{ma.content.html_safe}" %></a></li> <%= link_to ma.content.html_safe, student_work_index_path(:homework => ma.course_message.student_work.homework_common_id),:class=>"newsGrey",:title => "#{ma.content.html_safe}" %></a></li>
<% end %> <% end %>
<li class="homepageNewsTime fl"><%= time_tag(ma.created_at).html_safe %> </li> <li class="homepageNewsTime fl"><%= time_tag(ma.course_message.created_at).html_safe %> </li>
</ul> </ul>
<% end %> <% end %>
<% if ma.course_message_type == "JournalsForMessage" %> <% if ma.course_message_type == "JournalsForMessage" %>
@ -125,7 +125,7 @@
<li class="homepageNewsContent fl"><a href="javascript:void(0);" class="newsGrey"> <li class="homepageNewsContent fl"><a href="javascript:void(0);" class="newsGrey">
<%= link_to ma.forge_message.subject, issue_path(:id => ma.forge_message.id), :class => "#{ma.viewed == 0 ? "newsBlack" : "newsGrey"}",:title => "#{ma.forge_message.subject}" %></a> <%= link_to ma.forge_message.subject, issue_path(:id => ma.forge_message.id), :class => "#{ma.viewed == 0 ? "newsBlack" : "newsGrey"}",:title => "#{ma.forge_message.subject}" %></a>
</li> </li>
<li class="homepageNewsTime fl"><%= time_tag(ma.created_at).html_safe %> </li> <li class="homepageNewsTime fl"><%= time_tag(ma.forge_message.created_on).html_safe %> </li>
</ul> </ul>
<% end %> <% end %>
<% if ma.forge_message_type == "Journal" %> <% if ma.forge_message_type == "Journal" %>

View File

@ -1,15 +1,27 @@
# encoding: UTF-8
class ForgeMessages < ActiveRecord::Migration class ForgeMessages < ActiveRecord::Migration
def up def up
Project.all.each do |project| Project.all.each do |project|
transaction do transaction do
project.forge_messages << ForgeMessage.new(:user_id => project.user_id, :course_id => project.id) project.forge_messages << ForgeMessage.new(:user_id => project.user_id, :project_id => project.id)
# 新闻 # 新闻
project.news.each do |new| project.news.each do |new|
new.project.members.each do |m| new.project.members.each do |m|
if m.user_id != new.author_id if m.user_id != new.author_id
if m.created_on < new.created_on # 在成员加入项目之后 if m.created_on < new.created_on # 在成员加入项目之后
new.forge_messages << ForgeMessage.new(:user_id => m.user_id, :project_id => self.project_id, :viewed => true) new.forge_messages << ForgeMessage.new(:user_id => m.user_id, :project_id => new.project_id, :viewed => true)
end
end
end
end
# 新闻回复
project.news.each do |new|
if new.comments
new.comments.each do |comment|
if comment.author_id != comment.commented.author_id
comment.forge_messages << ForgeMessage.new(:user_id => comment.commented.author_id, :project_id => comment.commented.project.id, :viewed => true)
end end
end end
end end
@ -47,6 +59,7 @@ class ForgeMessages < ActiveRecord::Migration
# 缺陷更新 # 缺陷更新
project.issues.each do |issue| project.issues.each do |issue|
if issue.journals
issue.journals.each do |journal| issue.journals.each do |journal|
if journal.user_id != journal.issue.author_id if journal.user_id != journal.issue.author_id
journal.forge_messages << ForgeMessage.new(:user_id => journal.issue.author_id, :project_id => journal.issue.project_id, :viewed => true) journal.forge_messages << ForgeMessage.new(:user_id => journal.issue.author_id, :project_id => journal.issue.project_id, :viewed => true)
@ -57,9 +70,12 @@ class ForgeMessages < ActiveRecord::Migration
end end
end end
end end
end
end end
end end
def down def down
ForgeMessages.destroy_all
end end
end end

View File

@ -1,36 +1,114 @@
# encoding: UTF-8
class CourseMessages < ActiveRecord::Migration class CourseMessages < ActiveRecord::Migration
def up def up
Course.all.each do |course| Course.all.each do |course|
transaction do transaction do
course.course_messages << CourseActivity.new(:user_id => course.tea_id,:course_id => course.id) course.course_messages << CourseMessage.new(:user_id => course.tea_id,:course_id => course.id)
#作业 # 作业
course.homework_commons.each do |homework_common| course.homework_commons.each do |homework_common|
homework_common.course_acts << CourseActivity.new(:user_id => homework_common.user_id,:course_id => course.id) homework_common.course.members.each do |m|
if m.user_id != homework_common.user_id
if m.created_on < homework_common.created_at
homework_common.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => true)
end end
#通知 end
end
end
# 通知
course.news.each do |new| course.news.each do |new|
new.course_acts << CourseActivity.new(:user_id => new.author_id,:course_id => course.id) new.course.members.each do |m|
if m.user_id != new.author_id
if m.created_on < new.created_on # 在成员加入课程之后
new.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => true)
end end
#资源
course.attachments.each do |attachment|
attachment.course_acts << CourseActivity.new(:user_id => attachment.author_id,:course_id => course.id)
end end
#讨论区 end
end
# 通知的回复
course.news.each do |new|
if new.comments
new.comments.each do |comment|
if comment.author_id != comment.commented.author_id
comment.course_messages << CourseMessage.new(:user_id => comment.commented.author_id, :course_id => course.id, :viewed => true)
end
end
end
end
# 讨论区
if course.boards.first if course.boards.first
course.boards.first.messages.each do |message| course.boards.first.messages.each do |message|
message.course_acts << CourseActivity.new(:user_id => message.author_id,:course_id => course.id) if message.parent_id.nil? # 主贴
message.course.members.each do |m|
if message.author.allowed_to?(:as_teacher, message.course) && m.user_id != message.author_id # 老师 自己的帖子不给自己发送消息
if m.created_on < message.created_on
message.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => true)
end end
end end
#留言
course.journals_for_messages.each do |jour|
jour.course_acts << CourseActivity.new(:user_id => jour.user_id,:course_id => course.id)
end end
#问卷 else # 回帖
message.course.members.each do |m|
if m.user_id == Message.find(message.parent_id).author_id && m.user_id != message.author_id # 只针对主贴回复,回复自己的帖子不发消息
if m.created_on < message.created_on
message.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => true)
end
end
end
end
end
end
# 问卷
Poll.where("polls_type = 'Course' and polls_group_id = #{course.id}").each do |poll| Poll.where("polls_type = 'Course' and polls_group_id = #{course.id}").each do |poll|
poll.course_acts << CourseActivity.new(:user_id => poll.user_id,:course_id => course.id) if poll.polls_status == 2 #问卷是发布状态
Course.find(poll.polls_group_id).members.each do |m|
if m.user_id != poll.user_id
if m.created_on < poll.created_at
poll.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => true)
end end
end end
end end
elsif poll.polls_status == 1 #问卷是新建状态
poll.course_messages.destroy_all
end
end
# 作品评阅
course.homework_commons.each do |homework_common|
if homework_common.student_works
homework_common.student_works.each do |student_work|
if student_work.student_works_scores
student_work.student_works_scores.each do |student_works_score|
receiver = student_works_score.student_work.user
if student_works_score.created_at == student_works_score.updated_at
if student_works_score.comment.nil?
student_works_score.course_messages << CourseMessage.new(:user_id => receiver.id, :course_id => course.id,
:viewed => true, :content => "作业评分:#{student_works_score.score}", :status=> true)
else
student_works_score.course_messages << CourseMessage.new(:user_id => receiver.id, :course_id => course.id,
:viewed => true, :content => "作业评分:#{student_works_score.score}&nbsp;&nbsp;&nbsp; 评语:#{student_works_score.comment}", :status=> true)
end
else # 更新
if student_works_score.comment.nil?
student_works_score.course_messages << CourseMessage.new(:user_id => receiver.id, :course_id => course.id,
:viewed => true, :content => "作业评分:#{student_works_score.score}", :status=> true)
else
student_works_score.course_messages << CourseMessage.new(:user_id => receiver.id, :course_id => course.id,
:viewed => true, :content => "作业评分:#{student_works_score.score}&nbsp;&nbsp;&nbsp; 评语:#{student_works_score.comment}", :status=> true)
end
end
end
end
end
end
end
# 作品讨论
end
end
end end
def down def down

View File

@ -0,0 +1,31 @@
class AboutMessageAlls < ActiveRecord::Migration
def up
forge_count = ForgeMessage.all.count / 30 + 2
transaction do
for i in 1 ... forge_count do i
ForgeMessage.page(i).per(30).each do |forge_message|
message = MessageAll.new
message.message_id = forge_message.forge_message_id
message.message_type = forge_message.forge_message_type
message.user_id = forge_message.user_id
message.save
end
end
end
course_count = CourseMessage.all.count / 30 + 2
transaction do
for i in 1 ... course_count do i
CourseMessage.page(i).per(30).each do |course_message|
message = MessageAll.new
message.message_id = course_message.course_message_id
message.message_type = course_message.course_message_type
message.save
end
end
end
end
def down
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20150826061843) do ActiveRecord::Schema.define(:version => 20150829070453) do
create_table "activities", :force => true do |t| create_table "activities", :force => true do |t|
t.integer "act_id", :null => false t.integer "act_id", :null => false
@ -913,7 +913,6 @@ ActiveRecord::Schema.define(:version => 20150826061843) do
t.datetime "created_on" t.datetime "created_on"
t.integer "comments_count", :default => 0, :null => false t.integer "comments_count", :default => 0, :null => false
t.integer "course_id" t.integer "course_id"
t.datetime "updated_on"
end end
add_index "news", ["author_id"], :name => "index_news_on_author_id" add_index "news", ["author_id"], :name => "index_news_on_author_id"
@ -1318,7 +1317,6 @@ ActiveRecord::Schema.define(:version => 20150826061843) do
t.datetime "updated_at", :null => false t.datetime "updated_at", :null => false
t.integer "late_penalty", :default => 0 t.integer "late_penalty", :default => 0
t.integer "absence_penalty", :default => 0 t.integer "absence_penalty", :default => 0
t.integer "system_score"
end end
create_table "student_works_evaluation_distributions", :force => true do |t| create_table "student_works_evaluation_distributions", :force => true do |t|
@ -1536,6 +1534,7 @@ ActiveRecord::Schema.define(:version => 20150826061843) do
t.string "identity_url" t.string "identity_url"
t.string "mail_notification", :default => "", :null => false t.string "mail_notification", :default => "", :null => false
t.string "salt", :limit => 64 t.string "salt", :limit => 64
t.integer "gid"
end end
add_index "users", ["auth_source_id"], :name => "index_users_on_auth_source_id" add_index "users", ["auth_source_id"], :name => "index_users_on_auth_source_id"