From 0877aae0d6012e638bd2ce78da2a2f5571987d45 Mon Sep 17 00:00:00 2001 From: huang Date: Fri, 23 Dec 2016 17:00:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AB=9E=E8=B5=9B=E9=80=9A=E7=9F=A5=20index?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/news_controller.rb | 79 +++++++++++++ app/models/contest.rb | 2 + app/models/news.rb | 5 + app/views/news/_contest_news.html.erb | 34 ++++++ app/views/news/index.html.erb | 2 + .../20161223083022_add_contest_id_to_news.rb | 5 + db/schema.rb | 105 +++++++++--------- lib/redmine.rb | 6 + 8 files changed, 188 insertions(+), 50 deletions(-) create mode 100644 app/views/news/_contest_news.html.erb create mode 100644 db/migrate/20161223083022_add_contest_id_to_news.rb diff --git a/app/controllers/news_controller.rb b/app/controllers/news_controller.rb index aa037b8d5..52739fd14 100644 --- a/app/controllers/news_controller.rb +++ b/app/controllers/news_controller.rb @@ -42,6 +42,8 @@ class NewsController < ApplicationController @limit = 10 end + @contest = Contest.find(params[:contest_id]) if params[:contest_id] + # modify by nwb if params[:course_id] && @course==nil @course = Course.find(params[:course_id]) @@ -69,6 +71,83 @@ class NewsController < ApplicationController format.api format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") } end + elsif @contest + if (User.current.admin? || @contest.is_public == 1 || (@contest.is_public == 0 && User.current.member_of_contest?(@contest))) + @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 + + sort_name = "updated_at" + + sort_type = @b_sort == 1 ? "asc" : "desc" + + #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 ) ") + + scope = @contest ? @contest.news.visible : News.visible + # @newss = scope.all(:include => [:author, :contest], :order => "#{News.table_name}.created_on DESC") + # + # @page = params[:page] ? params[:page].to_i + 1 : 0 + news_page = 0 #@page *10 + # @news_count = @newss.count + # @is_new = params[:is_new] + @q = params[:subject] + if params[:subject].nil? || params[:subject].blank? + scope_order = scope.reorder("#{News.table_name}.sticky DESC, #{News.table_name}.created_on #{sort_type}").offset(news_page).includes(:author,:course).all() + else + 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,:contest).all() + end + + scope_order.each do |topic| + topic[:updated_at] = topic.contest_acts.first.updated_at + #topic[:updated_at] = CourseActivity.where("course_act_type='#{topic.class}' and course_act_id =#{topic.id}").first.updated_at + end + + #根据 赞+回复数排序 + if @order.to_i == 2 + @type = 2 + scope_order.each do |topic| + topic[:infocount] = get_praise_num(topic) + topic.comments.count + if topic[:infocount] < 0 + topic[:infocount] = 0 + end + 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] } + scope_order = sort_by_sticky scope_order + scope_order = sortby_time_countcommon_hassticky scope_order,sort_name + else + @type = 1 + @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 + end + + @newss = scope_order + + #分页 + @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 + + #@newss = paginateHelper scope_order,10 + @left_nav_type = 4 + respond_to do |format| + format.html { + @news = News.new + render :layout => 'base_contests' + } + format.js + format.api + end + else + render_403 + end elsif @course if (User.current.admin? || @course.is_public == 1 || (@course.is_public == 0 && User.current.member_of_course?(@course))) @order, @b_sort,@type = params[:order] || 1, params[:sort] || 1, params[:type] || 1 diff --git a/app/models/contest.rb b/app/models/contest.rb index eda85aa29..b4f36330d 100644 --- a/app/models/contest.rb +++ b/app/models/contest.rb @@ -14,6 +14,8 @@ class Contest < ActiveRecord::Base has_many :contestants, :class_name => 'ContestantForContest', :source => :user has_many :works + has_many :news, :dependent => :destroy, :include => :author + has_many :contest_acts, :class_name => 'ContestActivity',:as =>:contest_act ,:dependent => :destroy has_many :contest_activities has_many :contest_messages, :class_name =>'ContestMessage', :as => :contest_message, :dependent => :destroy diff --git a/app/models/news.rb b/app/models/news.rb index d42565980..de37154d2 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -25,6 +25,7 @@ class News < ActiveRecord::Base has_many_kindeditor_assets :assets, :dependent => :destroy #added by nwb belongs_to :course,:touch => true + belongs_to :contest,:touch => true belongs_to :org_subfield, :touch => true belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' has_many :comments, :as => :commented, :dependent => :destroy, :order => "created_on" @@ -79,6 +80,10 @@ class News < ActiveRecord::Base scope :course_visible, lambda {|*args| includes(:course).where(Course.allowed_to_condition(args.shift || User.current, :view_course_news, *args)) } + + scope :contest_visible, lambda {|*args| + includes(:contest).where(Contest.allowed_to_condition(args.shift || User.current, :view_contest_news, *args)) + } safe_attributes 'title', 'summary', 'description', 'sticky' #动态的更新 diff --git a/app/views/news/_contest_news.html.erb b/app/views/news/_contest_news.html.erb new file mode 100644 index 000000000..05d5f9c28 --- /dev/null +++ b/app/views/news/_contest_news.html.erb @@ -0,0 +1,34 @@ + +
+
+
+ 班级通知 +
+
+ <% if @course && User.current.allowed_to?(:manage_news, @course) %> + <%= labelled_form_for @news, :url =>{:controller=>'news',:action => 'new', :course_id => @course.id}, + :html => {:nhname=>'form',:multipart => true, :id => 'news-form'} do |f| %> + <%= render :partial => 'course_news_new', :locals => {:f => f, :news => @news, :edit_mode => false, :course => @course} %> + <% end %> + <% end %> + <%= render :partial=> 'course_news_detail',:locals =>{:newss => @newss, :page => 0} %> +
\ No newline at end of file diff --git a/app/views/news/index.html.erb b/app/views/news/index.html.erb index c4dd11ea0..527cb19cb 100644 --- a/app/views/news/index.html.erb +++ b/app/views/news/index.html.erb @@ -2,6 +2,8 @@ <%= render :partial => 'project_news', locals: {project: @project} %> <% elsif @course %> <%= render :partial => 'course_news', locals: {course: @course} %> +<% elsif @contest %> + <%= render :partial => 'contest_news', locals: {course: @contest} %> <% end %> diff --git a/db/migrate/20161223083022_add_contest_id_to_news.rb b/db/migrate/20161223083022_add_contest_id_to_news.rb new file mode 100644 index 000000000..04afbe1e3 --- /dev/null +++ b/db/migrate/20161223083022_add_contest_id_to_news.rb @@ -0,0 +1,5 @@ +class AddContestIdToNews < ActiveRecord::Migration + def change + add_column :news, :contest_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index b366db955..43788b504 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20161222063638) do +ActiveRecord::Schema.define(:version => 20161223083022) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false @@ -337,14 +337,16 @@ ActiveRecord::Schema.define(:version => 20161222063638) do add_index "changeset_parents", ["parent_id"], :name => "changeset_parents_parent_ids" create_table "changesets", :force => true do |t| - t.integer "repository_id", :null => false - t.string "revision", :null => false + t.integer "repository_id", :null => false + t.string "revision", :null => false t.string "committer" - t.datetime "committed_on", :null => false + t.datetime "committed_on", :null => false t.text "comments" t.date "commit_date" t.string "scmid" t.integer "user_id" + t.integer "project_id" + t.integer "type", :default => 0 end add_index "changesets", ["committed_on"], :name => "index_changesets_on_committed_on" @@ -495,12 +497,12 @@ ActiveRecord::Schema.define(:version => 20161222063638) do t.integer "user_id" t.integer "contest_id" t.integer "contest_message_id" - t.string "contest_message_type" - t.boolean "viewed", :default => false + t.string "course_message_type" + t.boolean "viewed", :default => false t.text "content" t.integer "status" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end add_index "contest_messages", ["contest_id"], :name => "index_contest_messages_on_contest_id" @@ -605,20 +607,6 @@ ActiveRecord::Schema.define(:version => 20161222063638) do t.integer "container_id", :default => 0 end - create_table "course_class_post", :id => false, :force => true do |t| - t.integer "班级id", :default => 0, :null => false - t.string "班级名" - t.integer "帖子id", :default => 0, :null => false - t.integer "主贴id" - t.string "帖子标题", :default => "", :null => false - t.text "帖子内容" - t.integer "帖子用户id" - t.integer "帖子回复数", :default => 0, :null => false - t.integer "最后回帖id" - t.datetime "发帖时间", :null => false - t.datetime "帖子更新时间", :null => false - end - create_table "course_contributor_scores", :force => true do |t| t.integer "course_id" t.integer "user_id" @@ -687,8 +675,8 @@ ActiveRecord::Schema.define(:version => 20161222063638) do t.string "code" t.integer "time" t.string "extra" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "location" t.string "term" t.string "string" @@ -698,27 +686,28 @@ ActiveRecord::Schema.define(:version => 20161222063638) do t.string "class_period" t.integer "school_id" t.text "description" - t.integer "status", :default => 1 - t.integer "attachmenttype", :default => 2 + t.integer "status", :default => 1 + t.integer "attachmenttype", :default => 2 t.integer "lft" t.integer "rgt" - t.integer "is_public", :limit => 1, :default => 1 - t.integer "inherit_members", :limit => 1, :default => 1 - t.integer "open_student", :default => 0 - t.integer "outline", :default => 0 - t.integer "publish_resource", :default => 0 - t.integer "is_delete", :default => 0 + t.integer "is_public", :limit => 1, :default => 1 + t.integer "inherit_members", :limit => 1, :default => 1 + t.integer "open_student", :default => 0 + t.integer "outline", :default => 0 + t.integer "publish_resource", :default => 0 + t.integer "is_delete", :default => 0 t.integer "end_time" t.string "end_term" - t.integer "is_excellent", :default => 0 - t.integer "excellent_option", :default => 0 - t.integer "is_copy", :default => 0 - t.integer "visits", :default => 0 + t.integer "is_excellent", :default => 0 + t.integer "excellent_option", :default => 0 + t.integer "is_copy", :default => 0 + t.integer "visits", :default => 0 t.integer "syllabus_id" t.string "invite_code" t.string "qrcode" - t.integer "qrcode_expiretime", :default => 0 - t.integer "invite_code_halt", :limit => 1, :default => 0 + t.integer "qrcode_expiretime", :default => 0 + t.integer "professional_level_id", :limit => 1 + t.integer "invite_code_halt", :limit => 1, :default => 0 end add_index "courses", ["invite_code"], :name => "index_courses_on_invite_code", :unique => true @@ -793,6 +782,11 @@ ActiveRecord::Schema.define(:version => 20161222063638) do add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority" + create_table "disciplines", :force => true do |t| + t.string "name" + t.integer "professional_level_id", :limit => 1 + end + create_table "discuss_demos", :force => true do |t| t.string "title" t.text "body" @@ -918,13 +912,11 @@ ActiveRecord::Schema.define(:version => 20161222063638) do t.integer "exercise_status" t.integer "user_id" t.integer "time" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.datetime "publish_time" t.datetime "end_time" t.integer "show_result" - t.integer "question_random", :default => 0 - t.integer "choice_random", :default => 0 end create_table "first_pages", :force => true do |t| @@ -1113,6 +1105,10 @@ ActiveRecord::Schema.define(:version => 20161222063638) do t.datetime "updated_at", :null => false end + create_table "innodb_monitor", :id => false, :force => true do |t| + t.integer "a" + end + create_table "invite_lists", :force => true do |t| t.integer "project_id" t.integer "user_id" @@ -1376,6 +1372,7 @@ ActiveRecord::Schema.define(:version => 20161222063638) do t.integer "course_id" t.integer "sticky", :default => 0 t.integer "org_subfield_id" + t.integer "contest_id" end add_index "news", ["author_id"], :name => "index_news_on_author_id" @@ -1644,9 +1641,12 @@ ActiveRecord::Schema.define(:version => 20161222063638) do end create_table "professional_levels", :force => true do |t| - t.string "level" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.integer "level" + end + + create_table "professions", :force => true do |t| + t.string "name" + t.integer "discipline_id", :limit => 1 end create_table "project_infos", :force => true do |t| @@ -1709,7 +1709,6 @@ ActiveRecord::Schema.define(:version => 20161222063638) do t.boolean "hidden_repo", :default => false, :null => false t.integer "attachmenttype", :default => 1 t.integer "user_id" - t.integer "dts_test", :default => 0 t.string "enterprise_name" t.integer "organization_id" t.integer "project_new_type" @@ -1717,6 +1716,11 @@ ActiveRecord::Schema.define(:version => 20161222063638) do t.integer "forked_from_project_id" t.integer "forked_count" t.integer "publish_resource", :default => 0 + t.integer "boards_count", :default => 0 + t.integer "news_count", :default => 0 + t.integer "acts_count", :default => 0 + t.integer "journals_count", :default => 0 + t.integer "boards_reply_count", :default => 0 t.integer "visits", :default => 0 t.integer "hot", :default => 0 t.string "invite_code" @@ -2114,8 +2118,8 @@ ActiveRecord::Schema.define(:version => 20161222063638) do t.string "title" t.text "description" t.integer "user_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "eng_name" t.integer "syllabus_type" t.integer "credit" @@ -2124,8 +2128,9 @@ ActiveRecord::Schema.define(:version => 20161222063638) do t.integer "practice_hours" t.string "applicable_major" t.string "pre_course" - t.integer "visits", :default => 0 - t.integer "des_status", :default => 0 + t.integer "visits", :default => 0 + t.integer "des_status", :default => 0 + t.integer "professional_level_id", :limit => 1 end add_index "syllabuses", ["user_id"], :name => "index_syllabuses_on_user_id" diff --git a/lib/redmine.rb b/lib/redmine.rb index 885c61969..0f7134490 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -132,6 +132,12 @@ Redmine::AccessControl.map do |map| map.permission :comment_news, {:comments => :create} end + map.contest_module :news do |map| + map.permission :manage_news, {:news => [:new, :create, :edit, :update, :destroy], :comments => [:destroy]}, :require => :member + map.permission :view_contest_news, {:news => [:index, :show]}, :public => true, :read => true + map.permission :comment_news, {:comments => :create} + end + map.course_module :bids do |map| map.permission :view_homework_attaches, {:bids => [:show, :show_project, :revision]}, :read => true,:belong_to_course => true map.permission :paret_in_homework,{},:require => :member ,:belong_to_course => true