From 841b180bea1ef1c3d11af3ac5988821dcf706225 Mon Sep 17 00:00:00 2001 From: cxt Date: Fri, 1 Sep 2017 17:21:02 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8F=AD=E7=BA=A7=E8=B5=84=E6=BA=90=E7=9A=84?= =?UTF-8?q?=E5=A4=8D=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/attachments_controller.rb | 10 +++++ app/controllers/courses_controller.rb | 20 ++++++++++ app/controllers/files_controller.rb | 7 ++-- app/helpers/application_helper.rb | 4 +- app/helpers/courses_helper.rb | 5 +-- app/helpers/files_helper.rb | 4 +- app/views/attachments/batch_setting.js.erb | 0 app/views/files/_course_file.html.erb | 3 +- config/routes.rb | 1 + ...170901014745_add_attachments_to_courses.rb | 37 +++++++++++++++++++ public/javascripts/course.js | 2 +- 11 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 app/views/attachments/batch_setting.js.erb create mode 100644 db/migrate/20170901014745_add_attachments_to_courses.rb diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 3629138e8..245ca0e7b 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -70,6 +70,16 @@ class AttachmentsController < ApplicationController file.downcase.end_with?(".pdf") end + def batch_setting + @course = Course.find params[:course_id] + @attachments = @course.attachments + @limit = 10 + @attachment_count = @attachments.count + @attachment_pages = Paginator.new @attachment_count, @limit, params['page'] || 1 + @offset ||= @attachment_pages.offset + @attachments = paginateHelper @attachments,10 + end + def direct_download @attachment.increment_download file_type = detect_content_type(@attachment) diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 5260cd766..862169792 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -737,6 +737,26 @@ class CoursesController < ApplicationController def create cs = CoursesService.new @course = cs.create_course(params,User.current)[:course] + + attachments = Attachment.where(:container_type => 'Course', :container_id => @course.syllabus.courses.map(&:id)) + attachments.each do |attachment| + if Attachment.where(:container_type => 'Course', :container_id => @course.id, :filename => attachment.filename).count == 0 + attach_copied_obj = attachment.copy + attach_copied_obj.tag_list.add(attachment.tag_list) # tag关联 + attach_copied_obj.container = @course + attach_copied_obj.created_on = attachment.created_on + attach_copied_obj.author_id = attachment.author_id + attach_copied_obj.copy_from = attachment.copy_from.nil? ? attachment.id : attachment.copy_from + attach_copied_obj.is_publish = 1 + #attach_copied_obj.publish_time = Time.now + 30.days + if attach_copied_obj.attachtype == nil + attach_copied_obj.attachtype = 4 + end + attach_copied_obj.save + update_quotes attach_copied_obj + end + end + if params[:copy_course] copy_course = Course.find params[:copy_course].to_i @course.is_copy = params[:copy_course].to_i diff --git a/app/controllers/files_controller.rb b/app/controllers/files_controller.rb index 000dcab77..5a2ca13ac 100644 --- a/app/controllers/files_controller.rb +++ b/app/controllers/files_controller.rb @@ -226,11 +226,10 @@ class FilesController < ApplicationController if sort == "" sort = "created_on DESC" end - course_ids = "(" + course.syllabus.courses.map(&:id).join(",") + ")" if keywords != "%%" - resultSet = Attachment.where("attachments.container_type = 'Course' And attachments.container_id in #{course_ids} AND filename LIKE :like ", like: "%#{keywords}%").reorder(sort) + resultSet = Attachment.where("attachments.container_type = 'Course' And attachments.container_id = '#{course.id}' AND filename LIKE :like ", like: "%#{keywords}%").reorder(sort) else - resultSet = Attachment.where("attachments.container_type = 'Course' And attachments.container_id in #{course_ids} "). reorder(sort) + resultSet = Attachment.where("attachments.container_type = 'Course' And attachments.container_id = '#{course.id}' "). reorder(sort) end #resultSet = Attachment.find_by_sql("SELECT `attachments`.* FROM `attachments` LEFT OUTER JOIN `homework_attaches` ON `attachments`.container_type = 'HomeworkAttach' AND `attachments`.container_id = `homework_attaches`.id LEFT OUTER JOIN `homework_for_courses` ON `homework_attaches`.bid_id = `homework_for_courses`.bid_id LEFT OUTER JOIN `homework_for_courses` AS H_C ON `attachments`.container_type = 'Bid' AND `attachments`.container_id = H_C.bid_id WHERE (`homework_for_courses`.course_id = 117 OR H_C.course_id = 117 OR (`attachments`.container_type = 'Course' AND `attachments`.container_id = 117)) AND `attachments`.filename LIKE '%#{keywords}%'").reorder("created_on DESC") end @@ -386,7 +385,7 @@ class FilesController < ApplicationController sort = "#{Attachment.table_name}.created_on desc" end - @containers = @course.syllabus.courses.includes(:attachments).reorder(sort) + @containers = [ Course.includes(:attachments).reorder(sort).find(@course.id)] show_attachments @containers get_attachment_for_tip(@all_attachments) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 67cb8248f..d3359c18c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -3068,8 +3068,8 @@ module ApplicationHelper #获取课程资源的TAG云 def get_course_tag_list course - all_attachments = Attachment.where(:container_type => 'Course', :container_id => course.syllabus.courses.map(&:id)).select{|attachment| attachment.is_public? || - (attachment.container_type == "Course" && syllabus_course_member(User.current, Course.find(attachment.container_id)))|| + all_attachments = course.attachments.select{|attachment| attachment.is_public? || + (attachment.container_type == "Course" && User.current.member_of_course?(course))|| attachment.author_id == User.current.id } tag_list = attachment_tag_list all_attachments diff --git a/app/helpers/courses_helper.rb b/app/helpers/courses_helper.rb index 3f3eb9570..397f91f6b 100644 --- a/app/helpers/courses_helper.rb +++ b/app/helpers/courses_helper.rb @@ -812,9 +812,8 @@ module CoursesHelper def visable_attachemnts_incourse course return[] unless course result = [] - attachments = Attachment.where(:container_type => 'Course', :container_id => course.syllabus.courses.map(&:id)) - attachments.each do |attachment| - if attachment.is_public? && attachment.is_publish == 1 || User.current == attachment.author || syllabus_course_teacher(User.current, Course.find(attachment.container_id)) || (syllabus_course_member(User.current, Course.find(attachment.container_id)) && attachment.is_publish == 1) || User.current.admin? + course.attachments.each do |attachment| + if attachment.is_public? && attachment.is_publish == 1 || User.current == attachment.author || User.current.allowed_to?(:as_teacher,course) || (User.current.member_of_course?(course) && attachment.is_publish == 1) || User.current.admin? result << attachment end end diff --git a/app/helpers/files_helper.rb b/app/helpers/files_helper.rb index 099833d56..465abb9cb 100644 --- a/app/helpers/files_helper.rb +++ b/app/helpers/files_helper.rb @@ -140,8 +140,8 @@ module FilesHelper if (attachment.is_public? && attachment.container_type != "Course") || (attachment.is_public? && attachment.container_type == "Course" && attachment.is_publish == 1)|| (attachment.container_type == "Project" && User.current.member_of?(attachment.project)) || - (attachment.container_type == "Course" && syllabus_course_teacher(User.current, Course.find(attachment.container_id)))|| - (attachment.container_type == "Course" && syllabus_course_member(User.current, Course.find(attachment.container_id)) && attachment.is_publish == 1)|| + (attachment.container_type == "Course" && User.current.allowed_to?(:as_teacher,Course.find(attachment.container_id)))|| + (attachment.container_type == "Course" && User.current.member_of_course?(Course.find(attachment.container_id)) && attachment.is_publish == 1)|| attachment.author_id == User.current.id || (attachment.container_type == "OrgSubfield" and User.current.member_of_org?(attachment.container.organization)) result << attachment diff --git a/app/views/attachments/batch_setting.js.erb b/app/views/attachments/batch_setting.js.erb new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/files/_course_file.html.erb b/app/views/files/_course_file.html.erb index 904f283d8..323647fb3 100644 --- a/app/views/files/_course_file.html.erb +++ b/app/views/files/_course_file.html.erb @@ -103,7 +103,8 @@ <%= submit_tag "班内搜索", :class => "blueBtn mr5 fl",:name => "incourse",:id => "incourse" %> <%#= submit_tag "全站搜索", :class => "blueBtn mr5 fl",:name => "insite",:id => "insite" %> <% if is_course_teacher(User.current,@course) || (@course.publish_resource==1 && User.current.member_of_course?(@course) ) %> - + <%= link_to("批量设置", batch_setting_attachments_path(:course_id => @course.id), :class => "blue-btn fr mr5", :remote => true) %> + 上传资源 <%= link_to("导入资源", import_resources_user_path(User.current, :type => 6, :course_id => @course.id), :class => "blue-btn fr mr5", :remote => true) %> <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index aadcefd8f..9b5fc8a65 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1239,6 +1239,7 @@ RedmineApp::Application.routes.draw do match "update_file_dense", :via => [:post] match "renderTag" , :via => [:get, :post] match 'delete_softapplications', :via => [:get, :post] + get 'batch_setting' end end resources :groups do diff --git a/db/migrate/20170901014745_add_attachments_to_courses.rb b/db/migrate/20170901014745_add_attachments_to_courses.rb new file mode 100644 index 000000000..9ec0cdfb3 --- /dev/null +++ b/db/migrate/20170901014745_add_attachments_to_courses.rb @@ -0,0 +1,37 @@ +class AddAttachmentsToCourses < ActiveRecord::Migration + def change + Course.where(:id => CourseActivity.where("updated_at > '2017-06-01 00:00:00'").map(&:course_id).uniq).each do |course| + attachments = Attachment.where(:container_type => 'Course', :container_id => course.syllabus.courses.map(&:id)) + attachments.each do |attachment| + if Attachment.where(:container_type => 'Course', :container_id => course.id, :filename => attachment.filename).count == 0 + attach_copied_obj = attachment.copy + attach_copied_obj.tag_list.add(attachment.tag_list) # tag关联 + attach_copied_obj.container = course + attach_copied_obj.created_on = attachment.created_on + attach_copied_obj.author_id = attachment.author_id + attach_copied_obj.copy_from = attachment.copy_from.nil? ? attachment.id : attachment.copy_from + attach_copied_obj.is_publish = 1 + #attach_copied_obj.publish_time = Time.now + 30.days + if attach_copied_obj.attachtype == nil + attach_copied_obj.attachtype = 4 + end + attach_copied_obj.save + quo_attachments = Attachment.find_by_sql("select * from attachments where copy_from = #{attach_copied_obj.copy_from} or id = #{attach_copied_obj.copy_from}") + result = Attachment.find_by_sql("select count(*) as number from attachments where copy_from = #{attach_copied_obj.copy_from}") + if result.nil? || result.count <= 0 + count = 0 + else + count = result[0].number + end + attach_copied_obj.quotes = count + attach_copied_obj.save + quo_attachments.each do |att| + att.quotes = attach_copied_obj.quotes + att.save + end + #update_quotes attach_copied_obj + end + end + end + end +end diff --git a/public/javascripts/course.js b/public/javascripts/course.js index 22a5f1494..e1a3d9c71 100644 --- a/public/javascripts/course.js +++ b/public/javascripts/course.js @@ -224,7 +224,7 @@ function submit_new_course() { if(regex_syllabus_option('new')&®ex_course_name('new')&®ex_course_class_period('new')&®ex_time_term('new')) { - $("#new_course").submit(); + $(".new_course").submit(); } }