2016-09-12 17:34:55 +08:00
#encoding: utf-8
2014-10-23 11:30:34 +08:00
# Redmine - project management software
# Copyright (C) 2006-2013 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class NewsController < ApplicationController
layout 'base_projects' # by young
2015-04-03 11:19:15 +08:00
include ApplicationHelper
2015-02-06 18:15:08 +08:00
before_filter :authorize1 , :only = > [ :show ]
2014-10-23 11:30:34 +08:00
default_search_scope :news
model_object News
before_filter :find_model_object , :except = > [ :new , :create , :index ]
before_filter :find_project_from_association , :except = > [ :new , :create , :index ]
before_filter :find_project_by_project_id , :only = > [ :new , :create ]
before_filter :authorize , :except = > [ :index ]
before_filter :find_optional_project , :only = > :index
accept_rss_auth :index
accept_api_auth :index
helper :watchers
helper :attachments
helper :project_score
def index
case params [ :format ]
when 'xml' , 'json'
@offset , @limit = api_offset_and_limit
else
@limit = 10
end
# modify by nwb
if params [ :course_id ] && @course == nil
@course = Course . find ( params [ :course_id ] )
end
if @project
2016-01-21 14:33:31 +08:00
@page = params [ :page ] ? params [ :page ] . to_i + 1 : 0
2014-10-23 11:30:34 +08:00
scope = @project ? @project . news . visible : News . visible
@news_count = scope . count
2016-01-21 14:33:31 +08:00
#@news_pages = Paginator.new @news_count, @limit, params['page']
#@offset ||= @news_pages.offset
2014-10-23 11:30:34 +08:00
@newss = scope . all ( :include = > [ :author , :project ] ,
:order = > " #{ News . table_name } .created_on DESC " ,
2016-01-21 14:33:31 +08:00
:offset = > @page * 10 ,
:limit = > 10 )
2016-07-22 15:32:37 +08:00
@left_nav_type = 4
2014-10-23 11:30:34 +08:00
respond_to do | format |
format . html {
@news = News . new # for adding news inline
# huang
render :layout = > false if request . xhr?
}
2016-01-21 14:33:31 +08:00
format . js
2014-10-23 11:30:34 +08:00
format . api
format . atom { render_feed ( @newss , :title = > ( @project ? @project . name : Setting . app_title ) + " : #{ l ( :label_news_plural ) } " ) }
end
elsif @course
2014-12-05 14:39:03 +08:00
if ( User . current . admin? || @course . is_public == 1 || ( @course . is_public == 0 && User . current . member_of_course? ( @course ) ) )
2016-04-29 13:42:58 +08:00
@order , @b_sort , @type = params [ :order ] || 1 , params [ :sort ] || 1 , params [ :type ] || 1
#确定 sort_type 1升序 2 降序
if @order . to_i == @type . to_i
@b_sort = @b_sort . to_i == 1 ? 2 : 1
else
@b_sort = 2
end
2016-05-03 16:34:31 +08:00
sort_name = " updated_at "
2016-04-29 13:42:58 +08:00
sort_type = @b_sort == 1 ? " asc " : " desc "
2016-05-03 17:57:24 +08:00
#scope = News.find_by_sql("select a.*,b.updated_at from news a, course_activities b where a.course_id = 532 and a.course_id = b.course_id and b.course_act_id = a.id ) ")
2014-12-05 14:39:03 +08:00
scope = @course ? @course . news . course_visible : News . course_visible
2015-12-24 09:57:32 +08:00
news_arr = scope . map { | news | news . id }
2015-03-24 17:12:51 +08:00
2015-12-24 09:57:32 +08:00
@page = params [ :page ] ? params [ :page ] . to_i + 1 : 0
2016-04-29 13:42:58 +08:00
news_page = 0 #@page *10
2014-12-05 14:39:03 +08:00
@news_count = scope . count
2015-12-24 09:57:32 +08:00
@is_new = params [ :is_new ]
2015-07-24 11:50:50 +08:00
@q = params [ :subject ]
2015-08-11 15:00:40 +08:00
if params [ :subject ] . nil? || params [ :subject ] . blank?
2016-05-03 16:34:31 +08:00
scope_order = scope . reorder ( " #{ News . table_name } .sticky DESC, #{ News . table_name } .created_on #{ sort_type } " ) . offset ( news_page ) . includes ( :author , :course ) . all ( )
2015-12-24 09:57:32 +08:00
#all(:include => [:author, :course],
#:order => "#{News.table_name}.sticky DESC, #{News.table_name}.created_on DESC").limit(10).offset(news_page)
2015-07-22 10:23:14 +08:00
else
2016-04-29 13:42:58 +08:00
scope_order = scope . where ( " #{ News . table_name } .title like ' #{ '%' << params [ :subject ] . to_s << '%' } ' " ) . reorder ( " #{ News . table_name } .sticky DESC, #{ News . table_name } . #{ sort_name } #{ sort_type } " ) . offset ( news_page ) . includes ( :author , :course ) . all ( )
2015-12-24 09:57:32 +08:00
#.all(:include => [:author, :course],:order => "#{News.table_name}.sticky DESC, #{News.table_name}.created_on DESC")
2015-07-22 10:23:14 +08:00
end
2016-05-03 16:34:31 +08:00
scope_order . each do | topic |
2016-05-04 11:03:08 +08:00
topic [ :updated_at ] = topic . course_acts . first . updated_at
#topic[:updated_at] = CourseActivity.where("course_act_type='#{topic.class}' and course_act_id =#{topic.id}").first.updated_at
2016-05-03 16:34:31 +08:00
end
2016-04-29 13:42:58 +08:00
#根据 赞+回复数排序
if @order . to_i == 2
@type = 2
2016-04-29 15:52:44 +08:00
scope_order . each do | topic |
topic [ :infocount ] = get_praise_num ( topic ) + topic . comments . count
2016-04-29 19:24:20 +08:00
if topic [ :infocount ] < 0
topic [ :infocount ] = 0
end
2016-04-29 15:52:44 +08:00
end
@b_sort == 1 ? scope_order = scope_order . sort { | x , y | x [ :infocount ] < = > y [ :infocount ] } : scope_order = scope_order . sort { | x , y | y [ :infocount ] < = > x [ :infocount ] }
2016-04-29 13:42:58 +08:00
scope_order = sort_by_sticky scope_order
2016-04-29 19:24:20 +08:00
scope_order = sortby_time_countcommon_hassticky scope_order , sort_name
2016-04-29 13:42:58 +08:00
else
@type = 1
2016-05-03 16:34:31 +08:00
@b_sort == 1 ? scope_order = scope_order . sort { | x , y | x [ :updated_at ] < = > y [ :updated_at ] } : scope_order = scope_order . sort { | x , y | y [ :updated_at ] < = > x [ :updated_at ] }
scope_order = sort_by_sticky scope_order
2016-04-29 13:42:58 +08:00
end
2015-12-24 09:57:32 +08:00
@newss = scope_order
2016-04-29 13:42:58 +08:00
#分页
@limit = 15
@is_remote = true
@atta_count = @newss . count
@atta_pages = Paginator . new @atta_count , @limit , params [ 'page' ] || 1
@offset || = @atta_pages . offset
@newss = paginateHelper @newss , @limit
2015-12-24 09:57:32 +08:00
#@newss = paginateHelper scope_order,10
2016-07-22 15:32:37 +08:00
@left_nav_type = 4
2014-12-05 14:39:03 +08:00
respond_to do | format |
format . html {
@news = News . new
render :layout = > 'base_courses'
}
2015-07-22 10:23:14 +08:00
format . js
2014-12-05 14:39:03 +08:00
format . api
format . atom { render_feed ( @newss , :title = > ( @course ? @course . name : Setting . app_title ) + " : #{ l ( :label_news_plural ) } " ) }
end
else
render_403
2014-10-23 11:30:34 +08:00
end
end
end
def show
2015-09-02 10:08:37 +08:00
# 更新news对应的forge_messages的消息viewed字段
2016-03-11 15:21:40 +08:00
ids = @news . comments . map { | comment | comment . id } . join ( " , " ) unless @news . comments . nil?
2015-09-02 10:08:37 +08:00
if @project
2016-03-11 15:21:40 +08:00
# 更新新闻
query_message_news = ForgeMessage . where ( " forge_message_id =? and user_id =? and viewed =? " , @news . id , User . current . id , 0 ) . first
# 更新新闻的时候一并更新回复
ForgeMessage . where ( " forge_message_id in ( #{ ids } ) and forge_message_type = 'Comment' and user_id = #{ User . current . id } " ) . update_all ( :viewed = > true ) unless ids . blank?
2015-09-02 10:08:37 +08:00
else
2016-03-11 15:21:40 +08:00
query_message_news = CourseMessage . where ( " course_message_id =? and user_id =? and viewed =? " , @news . id , User . current . id , 0 ) . first
CourseMessage . where ( " course_message_id in ( #{ ids } ) and course_message_type = 'Comment' and user_id = #{ User . current . id } " ) . update_all ( :viewed = > true ) unless ids . blank?
2015-08-24 17:44:13 +08:00
end
2016-03-11 15:21:40 +08:00
query_message_news . update_attribute ( :viewed , true ) unless query_message_news . nil?
# over
2014-12-15 16:53:20 +08:00
cs = CoursesService . new
2015-01-20 17:42:18 +08:00
result = cs . show_course_news params , User . current
@news = result [ :news ]
@comments = result [ :comments ]
2015-12-24 09:57:32 +08:00
@comment = Comment . new
2014-12-15 16:53:20 +08:00
#@comments = @news.comments
#@comments.reverse! if User.current.wants_comments_in_reverse_order?
2014-10-23 11:30:34 +08:00
#modify by nwb
if @news . course_id
@course = Course . find ( @news . course_id )
if @course
2016-07-22 15:32:37 +08:00
@left_nav_type = 4
2014-10-23 11:30:34 +08:00
render :layout = > 'base_courses'
end
2016-01-20 17:23:02 +08:00
elsif @news . org_subfield_id
@org_subfield = OrgSubfield . find ( @news . org_subfield_id )
@organization = @org_subfield . organization
render :layout = > 'base_org'
2014-10-23 11:30:34 +08:00
end
end
def new
#modify by nwb
if @project
@news = News . new ( :project = > @project , :author = > User . current )
2016-01-21 10:33:53 +08:00
@news . safe_attributes = params [ :news ]
@news . save_attachments ( params [ :attachments ] )
if @news . save
if params [ :asset_id ]
ids = params [ :asset_id ] . split ( ',' )
update_kindeditor_assets_owner ids , @news . id , OwnerTypeHelper :: NEWS
end
render_attachment_warning_if_needed ( @news )
#flash[:notice] = l(:notice_successful_create)
redirect_to project_news_index_url ( @project )
else
redirect_to project_news_index_url ( @project )
#layout_file = 'base_courses'
#render :action => 'new', :layout => layout_file
end
2014-10-23 11:30:34 +08:00
elsif @course
@news = News . new ( :course = > @course , :author = > User . current )
2015-12-24 09:57:32 +08:00
#render :layout => 'base_courses'
@news . safe_attributes = params [ :news ]
@news . save_attachments ( params [ :attachments ] )
if @news . save
2016-09-12 17:34:55 +08:00
@course . members . each do | m |
if m . user_id != User . current . id
#self.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => self.course_id, :viewed => false)
count = ShieldWechatMessage . where ( " container_type='User' and container_id= #{ m . user_id } and shield_type='Course' and shield_id= #{ @news . course_id } " ) . count
if count == 0
ws = WechatService . new
content = @news . title
2016-09-14 15:10:24 +08:00
name = @news . course . syllabus . nil? ? @news . course . name : @news . course . syllabus . title + " • " + @news . course . name
ws . class_notice m . user_id , " course_notice " , @news . id , " #{ l ( :label_new_notice_template ) } " , name , @news . author . show_name , format_time ( @news . created_on ) , content , " 点击查看通知详情 "
2016-09-12 17:34:55 +08:00
end
end
end
2015-12-24 09:57:32 +08:00
if params [ :asset_id ]
ids = params [ :asset_id ] . split ( ',' )
update_kindeditor_assets_owner ids , @news . id , OwnerTypeHelper :: NEWS
end
render_attachment_warning_if_needed ( @news )
#flash[:notice] = l(:notice_successful_create)
redirect_to course_news_index_url ( @course )
else
redirect_to course_news_index_url ( @course )
#layout_file = 'base_courses'
#render :action => 'new', :layout => layout_file
end
2014-10-23 11:30:34 +08:00
end
end
def create
#modify by nwb
if @project
@news = News . new ( :project = > @project , :author = > User . current )
@news . safe_attributes = params [ :news ]
@news . save_attachments ( params [ :attachments ] )
if @news . save
render_attachment_warning_if_needed ( @news )
flash [ :notice ] = l ( :notice_successful_create )
redirect_to project_news_index_url ( @project )
else
layout_file = @project ? 'base_projects' : 'base_courses'
render :action = > 'new' , :layout = > layout_file
end
elsif @course
@news = News . new ( :course = > @course , :author = > User . current )
@news . safe_attributes = params [ :news ]
@news . save_attachments ( params [ :attachments ] )
if @news . save
2015-04-03 16:35:46 +08:00
if params [ :asset_id ]
ids = params [ :asset_id ] . split ( ',' )
update_kindeditor_assets_owner ids , @news . id , OwnerTypeHelper :: NEWS
end
2015-07-22 10:23:14 +08:00
# <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ض<EFBFBD> ̬<EFBFBD> ļ<EFBFBD> ¼add start
2015-09-29 16:01:23 +08:00
# teachers = searchTeacherAndAssistant(@course)
# for teacher in teachers
# if(teacher.user_id != User.current.id)
# notify = ActivityNotify.new()
# notify.activity_container_id = @course.id
# notify.activity_container_type = 'Course'
# notify.activity_id = @news.id
# notify.activity_type = 'News'
# notify.notify_to = teacher.user_id
# notify.is_read = 0
# notify.save()
# end
# end
2015-07-22 10:23:14 +08:00
# <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ض<EFBFBD> ̬<EFBFBD> ļ<EFBFBD> ¼add end
2014-10-23 11:30:34 +08:00
render_attachment_warning_if_needed ( @news )
flash [ :notice ] = l ( :notice_successful_create )
redirect_to course_news_index_url ( @course )
else
layout_file = 'base_courses'
render :action = > 'new' , :layout = > layout_file
end
end
end
def edit
2016-01-20 17:23:02 +08:00
if @news . org_subfield_id
@org_subfield = OrgSubfield . find ( @news . org_subfield_id )
@organization = @org_subfield . organization
end
2015-04-28 10:31:55 +08:00
if @course
2016-07-22 15:32:37 +08:00
@left_nav_type = 4
2015-04-28 10:31:55 +08:00
render :layout = > " base_courses "
2016-01-20 17:23:02 +08:00
elsif @org_subfield
render :layout = > 'base_org'
2015-04-28 10:31:55 +08:00
end
2014-10-23 11:30:34 +08:00
end
def update
@news . safe_attributes = params [ :news ]
@news . save_attachments ( params [ :attachments ] )
if @news . save
render_attachment_warning_if_needed ( @news )
2015-12-24 09:57:32 +08:00
#flash[:notice] = l(:notice_successful_update)
2014-10-23 11:30:34 +08:00
redirect_to news_url ( @news )
else
#flash[:error] = l(:notice_successful_update)
redirect_to news_url ( @news )
end
end
def destroy
2016-01-20 17:23:02 +08:00
if @news . org_subfield_id
@org_subfield = OrgSubfield . find ( @news . org_subfield_id )
end
2014-10-23 11:30:34 +08:00
@news . destroy
# modify by nwb
if @project
redirect_to project_news_index_url ( @project )
elsif @course
redirect_to course_news_index_url ( @course )
2016-01-20 17:23:02 +08:00
elsif @org_subfield
redirect_to organization_path ( @org_subfield . organization , :org_subfield_id = > @org_subfield . id )
2014-10-23 11:30:34 +08:00
end
end
private
def find_optional_project
return true unless params [ :project_id ]
@project = Project . find ( params [ :project_id ] )
authorize
rescue ActiveRecord :: RecordNotFound
render_404
end
end