diff --git a/app/controllers/news_controller.rb b/app/controllers/news_controller.rb
index ee3db741d..d340e3bb2 100644
--- a/app/controllers/news_controller.rb
+++ b/app/controllers/news_controller.rb
@@ -38,31 +38,52 @@ class NewsController < ApplicationController
@limit = 10
end
- scope = @project ? @project.news.visible : News.visible
-
- @news_count = scope.count
- @news_pages = Paginator.new @news_count, @limit, params['page']
- @offset ||= @news_pages.offset
- @newss = scope.all(:include => [:author, :project],
- :order => "#{News.table_name}.created_on DESC",
- :offset => @offset,
- :limit => @limit)
-
- respond_to do |format|
- format.html {
- @news = News.new # for adding news inline
- # huang
-
- if @project.project_type == 1
- render :layout => 'base_courses'
- else
- render :layout => false if request.xhr?
- end
- }
- format.api
- format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") }
+ # modify by nwb
+ if params[:course_id] && @course==nil
+ @course = Course.find(params[:course_id])
end
- end
+ if @project
+ scope = @project ? @project.news.visible : News.visible
+
+ @news_count = scope.count
+ @news_pages = Paginator.new @news_count, @limit, params['page']
+ @offset ||= @news_pages.offset
+ @newss = scope.all(:include => [:author, :project],
+ :order => "#{News.table_name}.created_on DESC",
+ :offset => @offset,
+ :limit => @limit)
+
+ respond_to do |format|
+ format.html {
+ @news = News.new # for adding news inline
+ # huang
+
+ render :layout => false if request.xhr?
+ }
+ format.api
+ format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") }
+ end
+ elsif @course
+ scope = @course ? @course.news.visible : News.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)
+
+ respond_to do |format|
+ format.html {
+ @news = News.new
+ render :layout => 'base_courses'
+ }
+ format.api
+ format.atom { render_feed(@newss, :title => (@course ? @course.name : Setting.app_title) + ": #{l(:label_news_plural)}") }
+ end
+ end
+ end
def show
@comments = @news.comments
diff --git a/app/models/course.rb b/app/models/course.rb
index eedc0dff2..545330705 100644
--- a/app/models/course.rb
+++ b/app/models/course.rb
@@ -26,6 +26,7 @@ class Course < ActiveRecord::Base
has_many :enabled_modules, :dependent => :delete_all
has_many :boards, :dependent => :destroy, :order => "position ASC"
has_many :course_journals_for_messages, :class_name => 'CourseJournalsForMessage', :as => :jour, :dependent => :destroy
+ has_many :news, :dependent => :destroy, :include => :author
acts_as_taggable
acts_as_nested_set :order => 'name', :dependent => :destroy
diff --git a/app/models/news.rb b/app/models/news.rb
index f2dba2f8e..51bd7d81d 100644
--- a/app/models/news.rb
+++ b/app/models/news.rb
@@ -35,6 +35,7 @@ class News < ActiveRecord::Base
:author_key => :author_id
acts_as_watchable
+
after_create :add_author_as_watcher
# fq
after_create :act_as_activity
diff --git a/app/views/news/_course_news_html.erb b/app/views/news/_course_news_html.erb
new file mode 100644
index 000000000..d980cbacf
--- /dev/null
+++ b/app/views/news/_course_news_html.erb
@@ -0,0 +1,94 @@
+<%
+ btn_tips = l(:label_news_notice)
+ label_tips = l(:label_course_news)
+%>
+
+
+ <%= label_tips %>
+
+<%= link_to(btn_tips,
+ new_course_news_path(@course),
+ :class => 'icon icon-add',
+ :onclick => 'showAndScrollTo("add-news", "news_title"); return false;') %>
+
+<% if @course && User.current.allowed_to?(:manage_news, @course) %>
+
+ <%= labelled_form_for @news, :url => course_news_index_path(@course),
+ :html => {:id => 'news-form', :multipart => true} do |f| %>
+ <%= render :partial => 'news/form', :locals => {:f => f} %>
+ <%= submit_tag l(:button_create), :class => 'whiteButton m3p10 h30', :name => nil %> |
+ <%= preview_link preview_news_path(:course_id => @course), 'news-form', target='preview', {:class => 'whiteButton m3p10'} %>
+ |
+ <%= link_to l(:button_cancel), "#", :onclick => '$("#add-news").hide()', :class => 'whiteButton m3p10' %>
+ <% end if @course %>
+
+
+<% end %>
+
+ <% if @newss.empty? %>
+
+ <%= l(:label_no_data) %>
+
+ <% else %>
+ <% @newss.each do |news| %>
+
+
+
+ <%= link_to image_tag(url_to_avatar(news.author), :class => "avatar"), user_path(news.author) %> |
+
+
+
+
+ <%= link_to_user(news.author) if news.respond_to?(:author) %><%= l(:label_project_notice) %><%= link_to h(news.title), news_path(news) %>
+ <%= delete_link news_path(news) if User.current.allowed_to?(:manage_news, @couese) %>
+ |
+
+
+
+ <%= textilizable(news, :description) %> |
+
+
+ <%= l :label_update_time %>
+ : <%= format_time(news.created_on) %> |
+ <%= link_to l(:label_project_newother), news_path(news) %><%= "(#{l(:label_x_comments, :count => news.comments_count)})" if news.comments_count >= 0 %> |
+
+
+ |
+
+
+ <% end %>
+ <% end %>
+
+
+
+
+
+ <% other_formats_links do |f| %>
+ <%= f.link_to 'Atom', :url => {:course_id => @course, :key => User.current.rss_key} %>
+ <% end %>
+
+ <% content_for :header_tags do %>
+ <%= auto_discovery_link_tag(:atom, params.merge({:format => 'atom', :page => nil, :key => User.current.rss_key})) %>
+ <%= stylesheet_link_tag 'scm' %>
+ <% end %>
+
+ <% html_title(l(:label_news_plural)) -%>
+
+
+
diff --git a/app/views/news/_project_news_html.erb b/app/views/news/_project_news_html.erb
new file mode 100644
index 000000000..416b648a9
--- /dev/null
+++ b/app/views/news/_project_news_html.erb
@@ -0,0 +1,108 @@
+<%
+ if @project.project_type == Project::ProjectType_course
+ btn_tips = l(:label_news_notice)
+ label_tips = l(:label_course_news)
+ else
+ btn_tips = l(:label_news_new)
+ label_tips = l(:label_news)
+ end
+%>
+
+
+ <%= label_tips %>
+
+<%= link_to(btn_tips,
+ new_project_news_path(@project),
+ :class => 'icon icon-add',
+ :onclick => 'showAndScrollTo("add-news", "news_title"); return false;') %>
+
+<% if @project && User.current.allowed_to?(:manage_news, @project) %>
+
+ <%= labelled_form_for @news, :url => project_news_index_path(@project),
+ :html => {:id => 'news-form', :multipart => true} do |f| %>
+ <%= render :partial => 'news/form', :locals => {:f => f} %>
+ <%= submit_tag l(:button_create), :class => 'whiteButton m3p10 h30', :name => nil %> |
+ <%= preview_link preview_news_path(:project_id => @project), 'news-form', target='preview', {:class => 'whiteButton m3p10'} %>
+ |
+ <%= link_to l(:button_cancel), "#", :onclick => '$("#add-news").hide()', :class => 'whiteButton m3p10' %>
+ <% end if @project %>
+
+
+<% end %>
+
+ <% if @newss.empty? %>
+
+ <%= l(:label_no_data) %>
+
+ <% else %>
+ <% @newss.each do |news| %>
+
+
+
+ <%= link_to image_tag(url_to_avatar(news.author), :class => "avatar"), user_path(news.author) %> |
+
+
+ <% if @project.project_type == 1 %>
+
+
+ <%= link_to_user(news.author) if news.respond_to?(:author) %><%= l(:label_project_notice) %><%= link_to h(news.title), news_path(news) %>
+ <%= delete_link news_path(news) if User.current.allowed_to?(:manage_news, @project) %>
+ |
+
+ <% else %>
+
+
+ <%= link_to_user(news.author) if news.respond_to?(:author) %><%= l(:label_project_newshare) %> <%= link_to h(news.title), news_path(news) %>
+ <%= delete_link news_path(news) if User.current.allowed_to?(:manage_news, @project) %>
+ |
+
+ <% end %>
+
+
+ <%= textilizable(news, :description) %> |
+
+
+ <%= l :label_update_time %>
+ : <%= format_time(news.created_on) %> |
+ <%= link_to l(:label_project_newother), news_path(news) %><%= "(#{l(:label_x_comments, :count => news.comments_count)})" if news.comments_count >= 0 %> |
+
+
+ |
+
+
+ <% end %>
+ <% end %>
+
+
+
+
+
+ <% other_formats_links do |f| %>
+ <%= f.link_to 'Atom', :url => {:project_id => @project, :key => User.current.rss_key} %>
+ <% end %>
+
+ <% content_for :header_tags do %>
+ <%= auto_discovery_link_tag(:atom, params.merge({:format => 'atom', :page => nil, :key => User.current.rss_key})) %>
+ <%= stylesheet_link_tag 'scm' %>
+ <% end %>
+
+ <% html_title(l(:label_news_plural)) -%>
+
+
+
diff --git a/app/views/news/index.html.erb b/app/views/news/index.html.erb
index 4dfa21d52..2635e52a6 100644
--- a/app/views/news/index.html.erb
+++ b/app/views/news/index.html.erb
@@ -1,108 +1,5 @@
-<%
- if @project.project_type == Project::ProjectType_course
- btn_tips = l(:label_news_notice)
- label_tips = l(:label_course_news)
- else
- btn_tips = l(:label_news_new)
- label_tips = l(:label_news)
- end
-%>
-
-
- <%= label_tips %>
-
-<%= link_to(btn_tips,
- new_project_news_path(@project),
- :class => 'icon icon-add',
- :onclick => 'showAndScrollTo("add-news", "news_title"); return false;') %>
-
-<% if @project && User.current.allowed_to?(:manage_news, @project) %>
-
- <%= labelled_form_for @news, :url => project_news_index_path(@project),
- :html => {:id => 'news-form', :multipart => true} do |f| %>
- <%= render :partial => 'news/form', :locals => {:f => f} %>
- <%= submit_tag l(:button_create), :class => 'whiteButton m3p10 h30', :name => nil %> |
- <%= preview_link preview_news_path(:project_id => @project), 'news-form', target='preview', {:class => 'whiteButton m3p10'} %>
- |
- <%= link_to l(:button_cancel), "#", :onclick => '$("#add-news").hide()', :class => 'whiteButton m3p10' %>
- <% end if @project %>
-
-
+<% if @project %>
+ <%= render :partial => 'project_news', locals: {project: @project} %>
+<% elsif @course %>
+ <%= render :partial => 'course_news', locals: {course: @course} %>
<% end %>
-
- <% if @newss.empty? %>
-
- <%= l(:label_no_data) %>
-
- <% else %>
- <% @newss.each do |news| %>
-
-
-
- <%= link_to image_tag(url_to_avatar(news.author), :class => "avatar"), user_path(news.author) %> |
-
-
- <% if @project.project_type == 1 %>
-
-
- <%= link_to_user(news.author) if news.respond_to?(:author) %><%= l(:label_project_notice) %><%= link_to h(news.title), news_path(news) %>
- <%= delete_link news_path(news) if User.current.allowed_to?(:manage_news, @project) %>
- |
-
- <% else %>
-
-
- <%= link_to_user(news.author) if news.respond_to?(:author) %><%= l(:label_project_newshare) %> <%= link_to h(news.title), news_path(news) %>
- <%= delete_link news_path(news) if User.current.allowed_to?(:manage_news, @project) %>
- |
-
- <% end %>
-
-
- <%= textilizable(news, :description) %> |
-
-
- <%= l :label_update_time %>
- : <%= format_time(news.created_on) %> |
- <%= link_to l(:label_project_newother), news_path(news) %><%= "(#{l(:label_x_comments, :count => news.comments_count)})" if news.comments_count >= 0 %> |
-
-
- |
-
-
- <% end %>
- <% end %>
-
-
-
-
-
- <% other_formats_links do |f| %>
- <%= f.link_to 'Atom', :url => {:project_id => @project, :key => User.current.rss_key} %>
- <% end %>
-
- <% content_for :header_tags do %>
- <%= auto_discovery_link_tag(:atom, params.merge({:format => 'atom', :page => nil, :key => User.current.rss_key})) %>
- <%= stylesheet_link_tag 'scm' %>
- <% end %>
-
- <% html_title(l(:label_news_plural)) -%>
-
-
-
diff --git a/db/migrate/20140611161801_add_courseid_to_news.rb b/db/migrate/20140611161801_add_courseid_to_news.rb
new file mode 100644
index 000000000..9e08a446c
--- /dev/null
+++ b/db/migrate/20140611161801_add_courseid_to_news.rb
@@ -0,0 +1,10 @@
+class AddCourseidToNews < ActiveRecord::Migration
+ #迁移原课程新闻数据
+ def change
+ add_column :news, :course_id, :int
+
+ News.where('project_id IN (SELECT id FROM projects WHERE project_type=1)').update_all("course_id = project_id")
+ News.where('project_id IN (SELECT id FROM projects WHERE project_type=1)').update_all(project_id: -1)
+
+ end
+end