diff --git a/app/controllers/competition_teams_controller.rb b/app/controllers/competition_teams_controller.rb index 6ecbb9aa..5b68c433 100644 --- a/app/controllers/competition_teams_controller.rb +++ b/app/controllers/competition_teams_controller.rb @@ -17,21 +17,27 @@ class CompetitionTeamsController < ApplicationController @team_user_ids = @team.team_members.pluck(:user_id) - shixuns = Shixun.where(user_id: @team_user_ids, status: 2) + shixuns = Shixun.where(user_id: @team_user_ids, status: 2).where('shixuns.created_at > ?', Time.parse('2018-06-01')) shixuns = shixuns.joins('left join shixuns forked_shixuns on forked_shixuns.fork_from = shixuns.id and forked_shixuns.status = 2') - shixuns = shixuns.joins('left join myshixuns on myshixuns.shixun_id = shixuns.id and exists(select 1 from games where games.myshixun_id = myshixuns.id and games.status = 2)') shixuns = shixuns.select('shixuns.id, shixuns.identifier, shixuns.user_id, shixuns.myshixuns_count, shixuns.name, shixuns.fork_from, sum(forked_shixuns.myshixuns_count) forked_myshixun_count') @shixuns = shixuns.group('shixuns.id').order('shixuns.myshixuns_count desc').includes(:creator) - @myshixun_count_map = Myshixun.where(shixun_id: @shixuns.map(&:id)) - .where('exists(select 1 from games where games.myshixun_id = myshixuns.id and games.status = 2)') - .group('shixun_id').count - course_ids = Course.joins('join members on members.course_id = courses.id') + shixun_ids = @shixuns.map(&:id) + @myshixun_count_map = get_valid_myshixun_count(shixun_ids) + # forked shixun valid myshixun count + forked_shixun_map = Shixun.where(status: 2, fork_from: shixun_ids).select('id, fork_from') + forked_shixun_map = forked_shixun_map.each_with_object({}) { |sx, obj| obj[sx.id] = sx.fork_from } + forked_myshixun_count_map = get_valid_myshixun_count(forked_shixun_map.keys) + forked_myshixun_count_map.each { |k, v| @myshixun_count_map[forked_shixun_map[k]] += v } + + # todo:使用新版course_members + course_ids = Course.where('courses.created_at > ?', Time.parse('2018-06-01')) + .joins('join members on members.course_id = courses.id') .joins('join member_roles on member_roles.member_id = members.id and member_roles.role_id in (3,7,9)') .where(members: { user_id: @team_user_ids }).pluck(:id) - courses = Course.where(id: course_ids).joins(:shixun_homework_commons) + courses = Course.where(id: course_ids).joins(:shixun_homework_commons).where('homework_commons.publish_time < now()') @courses = courses.select('courses.id, courses.name, courses.members_count, count(*) shixun_homework_count') - .group('courses.id').order('shixun_homework_count desc') + .group('courses.id').order('shixun_homework_count desc').having('shixun_homework_count > 0') @course_myshixun_map = Myshixun.joins(student_works: :homework_common) .where(homework_commons: { course_id: @courses.map(&:id) }) @@ -147,7 +153,7 @@ class CompetitionTeamsController < ApplicationController # 新增加的成员 ids = new_member_ids - team_member_ids - raise @message unless check_member_enroll_limited?(@competition, ids) # 有成员已经加入其他战队,并且只能一次报名 + raise @message unless check_member_enroll_limited?(@competition, ids - [@team.user_id]) # 有成员已经加入其他战队,并且只能一次报名; 减去创建者 ids.each do |user_id| next if user_id.to_i == @team.user_id @@ -164,7 +170,7 @@ class CompetitionTeamsController < ApplicationController # 新增加的老师 ids = new_teacher_ids - teacher_ids - raise @message unless check_teacher_enroll_limited?(@competition, ids) # 有老师已经加入其他战队,并且只能一次报名 + raise @message unless check_teacher_enroll_limited?(@competition, ids - [@team.user_id]) # 有老师已经加入其他战队,并且只能一次报名;减去创建者 ids.each do |user_id| next if user_id.to_i == @team.user_id @@ -321,4 +327,10 @@ class CompetitionTeamsController < ApplicationController true end + + def get_valid_myshixun_count(ids) + Myshixun.where(shixun_id: ids) + .where('exists(select 1 from games where games.myshixun_id = myshixuns.id and games.status = 2)') + .group('shixun_id').count + end end diff --git a/app/controllers/libraries_controller.rb b/app/controllers/libraries_controller.rb index 1d72f158..27e814d7 100644 --- a/app/controllers/libraries_controller.rb +++ b/app/controllers/libraries_controller.rb @@ -2,6 +2,7 @@ class LibrariesController < ApplicationController layout 'base_library' before_filter :require_login, :except => [:index] + after_filter :increment_visit_count, only: [:show, :create, :edit, :update] def index libraries = Library.where(nil) @@ -25,7 +26,6 @@ class LibrariesController < ApplicationController return render_403 unless admin_or_self? || @library.published? @library_applies = @library.library_applies.where(status: :refused).order('created_at desc') - @library.increment_visited_count! end def new @@ -39,10 +39,10 @@ class LibrariesController < ApplicationController Libraries::SubmitService.new(@library).call redirect_to publish_success_libraries_path else - flash[:message] = '保存成功' - redirect_to edit_library_path(id: @library.id) + redirect_to library_path(id: @library.id) end - rescue ActiveRecord::RecordInvalid => _ + rescue ActiveRecord::RecordInvalid => e + flash[:message] = e.record.errors.full_messages.join(',') render 'new' rescue Libraries::SubmitService::Error => ex flash[:message] = ex.message @@ -63,10 +63,10 @@ class LibrariesController < ApplicationController Libraries::SubmitService.new(@library).call redirect_to publish_success_libraries_path else - flash[:message] = '保存成功' - redirect_to edit_library_path(id: @library.id) + redirect_to library_path(id: @library.id) end - rescue ActiveRecord::RecordInvalid => _ + rescue ActiveRecord::RecordInvalid => e + flash[:message] = e.record.errors.full_messages.join(',') render 'edit' rescue Libraries::SubmitService::Error => ex flash[:message] = ex.message @@ -110,6 +110,7 @@ class LibrariesController < ApplicationController def form_params @_form_params ||= begin hash = params[:library].presence || {} + hash[:tag_ids] = params[:tag_ids].to_s.split(',') hash[:attachment_ids] = (params[:attachments].presence || []).values.map{|h| h[:attachment_id]} hash end @@ -122,4 +123,8 @@ class LibrariesController < ApplicationController def admin_or_self? current_library.user_id == current_user.id || admin_or_business? end + + def increment_visit_count + @library.increment_visited_count! if @library && @library.id + end end \ No newline at end of file diff --git a/app/helpers/libraries_helper.rb b/app/helpers/libraries_helper.rb new file mode 100644 index 00000000..433effd7 --- /dev/null +++ b/app/helpers/libraries_helper.rb @@ -0,0 +1,17 @@ +module LibrariesHelper + def show_library_tags(library) + html = '' + library.library_tags.each do |tag| + html += content_tag(:span, tag.name, class: "edu-filter-btn fl cdefault mt3 ml10 " + library_tag_class(tag)) + end + + raw html + end + + def library_tag_class(tag) + case tag.name + when '优秀案例' then 'edu-activity-red' + when '入库案例' then 'edu-activity-blue' + end + end +end \ No newline at end of file diff --git a/app/models/competition.rb b/app/models/competition.rb index 96747b0b..7ccf067b 100644 --- a/app/models/competition.rb +++ b/app/models/competition.rb @@ -2,7 +2,7 @@ class Competition < ActiveRecord::Base # status 0:下架, 1:上架 attr_accessible :end_time, :identifier, :name, :online_time, :start_time, :status, :visits, :competition_lists_count, - :min_num, :max_num, :enroll_end_time, :sub_title + :min_num, :max_num, :enroll_end_time, :sub_title, :published_at has_many :competition_modules, :dependent => :destroy has_many :competition_stages, :dependent => :destroy diff --git a/app/models/library.rb b/app/models/library.rb index 63df5c91..2ff3c4c4 100644 --- a/app/models/library.rb +++ b/app/models/library.rb @@ -5,6 +5,8 @@ class Library < ActiveRecord::Base has_many :library_applies, dependent: :delete_all has_many :attachments, as: :container + has_many :library_library_tags, dependent: :delete_all + has_many :library_tags, through: :library_library_tags attr_accessible :title, :content diff --git a/app/models/library_library_tag.rb b/app/models/library_library_tag.rb new file mode 100644 index 00000000..490daf08 --- /dev/null +++ b/app/models/library_library_tag.rb @@ -0,0 +1,4 @@ +class LibraryLibraryTag < ActiveRecord::Base + belongs_to :library + belongs_to :library_tag +end \ No newline at end of file diff --git a/app/models/library_tag.rb b/app/models/library_tag.rb new file mode 100644 index 00000000..2e356812 --- /dev/null +++ b/app/models/library_tag.rb @@ -0,0 +1,6 @@ +class LibraryTag < ActiveRecord::Base + has_many :library_library_tags, dependent: :delete_all + has_many :libraries, through: :library_library_tags + + validates :name, presence: true, uniqueness: true +end \ No newline at end of file diff --git a/app/models/shixun.rb b/app/models/shixun.rb index fe9a00b1..0d201ed3 100644 --- a/app/models/shixun.rb +++ b/app/models/shixun.rb @@ -66,18 +66,6 @@ class Shixun < ActiveRecord::Base #scope :visible, -> { where(status: -1) } after_create :send_tiding - def description - self.has_attribute?(:description) ? self[:description] : "" - end - - def propaedeutics - self.has_attribute?(:propaedeutics) ? self[:propaedeutics] : "" - end - - def evaluate_script - self.has_attribute?(:evaluate_script) ? self[:evaluate_script] : "" - end - def should_compile? self.mirror_repositories.published_main_mirror.first.try(:should_compile) end diff --git a/app/services/libraries/save_service.rb b/app/services/libraries/save_service.rb index db244836..393d3911 100644 --- a/app/services/libraries/save_service.rb +++ b/app/services/libraries/save_service.rb @@ -23,6 +23,19 @@ class Libraries::SaveService library.assign_attributes(params) library.save! + new_tag_ids = LibraryTag.where(id: params[:tag_ids].presence || []).pluck(:id) + old_tag_ids = library.library_library_tags.pluck(:library_tag_id) + + # 删除标签 + destroy_ids = old_tag_ids - new_tag_ids + library.library_library_tags.where(library_tag_id: destroy_ids).delete_all + + # 创建标签 + created_ids = new_tag_ids - old_tag_ids + created_ids.each do |id| + library.library_library_tags.create!(library_tag_id: id) + end + Attachment.where(id: attachment_ids).update_all(container_id: library.id, container_type: 'Library') end diff --git a/app/services/users_service.rb b/app/services/users_service.rb index 6ab6d3f6..d60de29c 100644 --- a/app/services/users_service.rb +++ b/app/services/users_service.rb @@ -360,7 +360,7 @@ class UsersService g = Gitlab.client g.edit_user(@current_user.gid, :password => params[:new_password]) rescue Exception => e - logger.error "change users password failed! ===> #{e}" + Rails.logger.error "change users password failed! ===> #{e}" end end end diff --git a/app/views/attachments/_from_libraries.html.erb b/app/views/attachments/_from_libraries.html.erb index a6fe347d..5d4d30bc 100644 --- a/app/views/attachments/_from_libraries.html.erb +++ b/app/views/attachments/_from_libraries.html.erb @@ -1,6 +1,6 @@ -
从我的电脑选择要上传的文档:按住CTRL可以上传多份文档
<%= section.name %> <%= format_time section.start_time %> ~ <%= com_end_time section.end_time %> diff --git a/app/views/competitions/_qg_second_opensource.html.erb b/app/views/competitions/_qg_second_opensource.html.erb new file mode 100644 index 00000000..b7e878e9 --- /dev/null +++ b/app/views/competitions/_qg_second_opensource.html.erb @@ -0,0 +1,71 @@ +<% index = 0 %> +
+<% index += 1 %> + +<% index += 1 %> + +<% @competition.competition_stages.each_with_index do |stage, i| %> ++ + <%= first_section.try(:name) %> + <%= first_section.start_time.try(:strftime, '%Y年%m月%d日') %>~<%= first_section.end_time.try(:strftime, '%Y年%m月%d日') %> + + + <%= second_section.try(:name) %> + <%= second_section.try(:start_time).try(:strftime, '%Y年%m月%d日') %>~<%= second_section.try(:end_time).try(:strftime, '%Y年%m月%d日') %> + +
+<%= @competition.name %>
<% if @maximum_staff > 1 %> @@ -67,7 +67,7 @@上传教学案例
请输入描述内容
请上传附件
+请上传附件
<%= link_to library.title, library_path(library) %>
++ <%= link_to library.title, library_path(library),:class => "task-hide font-16 library_l_name" %> + <%= show_library_tags(library) %> +
<%= link_to library.user.show_real_name, user_path(library.user) %> <%= library.visited_count || 0 %> 浏览 diff --git a/app/views/libraries/index.html.erb b/app/views/libraries/index.html.erb index cc34dc81..8e52e85b 100644 --- a/app/views/libraries/index.html.erb +++ b/app/views/libraries/index.html.erb @@ -5,8 +5,8 @@ 教学案例 <%= link_to '新建', new_library_path, class: 'fr color-blue font-16 mt3' %>
-- <%= @library.title %> + + <%= @library.title %> + + <%= show_library_tags(@library) %> <% if admin_or_self %> <% if @library.pending? %> - 草稿 + 草稿 <% elsif @library.processing? %> - 审核中 + 审核中 <% elsif @library.refused? %> - 未通过 + 未通过 <% end %> <% end %> <%= link_to('返回', libraries_path, class: 'fr color-grey-9 mt5') %> @@ -40,20 +43,19 @@
- 详情 - <% if admin_or_business? || @library.user_id == User.current.id && @library.pending? %> - <%= link_to '删除', 'javascript:void(0);', data: { id: @library.id }, - class: 'white-btn edu-blueline-btn fr ml20 delete-btn' %> - <% end %> - <% if admin_or_self %> - <%= link_to '编辑', edit_library_path(id: @library.id), class: 'white-btn edu-blueline-btn fr' %> - <% end %> -
-