diff --git a/app/controllers/files_controller.rb b/app/controllers/files_controller.rb index cf94da8d9..7ec796b61 100644 --- a/app/controllers/files_controller.rb +++ b/app/controllers/files_controller.rb @@ -256,6 +256,13 @@ class FilesController < ApplicationController @order = "" @is_remote = false if params[:project_id] + # 更新资源申请消息为已读 + # ar_ids = ApplyResource.where("user_id =? and container_id =? and container_type =?", User.current.id, params[:project_id].to_i, "Project").map{|ar| ar.id} + # cms = CourseMessage.where("course_message_type =? and user_id =? and course_message_id in (#{ar_ids.empty? ? '0': ar_ids.join(',')})", "ApplyResource", User.current.id) + # cms.each do |cm| + # cm.update_column(:viewed, true) + # end + # over @page = params[:page] ? params[:page].to_i + 1 : 2 @container_type = 0 if params[:sort] diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index 1946378ef..3ab62bbea 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -164,7 +164,7 @@ class OrganizationsController < ApplicationController return end q = params[:search].nil? ? "" : "#{params[:search].strip}" - @field = OrgSubfield.find(params[:org_subfield_id]) + @field = params[:org_subfield_id].nil? ? OrgSubfield.where("organization_id =? and field_type =?", params[:id].to_i, "Comptec").first : OrgSubfield.find(params[:org_subfield_id]) @type = params[:type] if @type == "courses" || @type.nil? @org_teachers = User.find_by_sql("select u.*, ue.technical_title, ue.school_id,(select count(*) from courses where courses.tea_id = u.id) as course_count diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 4a148bb8b..531f0cb90 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -362,6 +362,8 @@ update # project_path_cut = RepositoriesHelper::PROJECT_PATH_CUT # ip = RepositoriesHelper::REPO_IP_ADDRESS gitlab_address = Redmine::Configuration['gitlab_address'] + # REDO:需优化,仅测试用 + @zip_path = gitlab_address.to_s + "/api/v3/projects/" + @project.gpid.to_s + "/repository/archive?&private_token=YTyCv4978MXmdL2B9C62" if @repository.type.to_s == "Repository::Gitlab" @repos_url = gitlab_address.to_s+"/"+@project.owner.to_s+"/"+@repository.identifier+"."+"git" else diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index f3e0908ba..15d731634 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -311,6 +311,28 @@ class UsersController < ApplicationController end end + # 处理资源引用请求 + # status + def deal_with_apply_resource + @msg = CourseMessage.find(params[:msg_id]) + ar = ApplyResource.where("id = ?", @msg.course_message_id).first + unless ar.nil? + case params[:agree] + when 'Y' + ar.update_column('status', 2) + @msg.update_attributes(:apply_result => 1, :viewed => 1) + ar.course_messages << CourseMessage.new(:user_id => ar.user_id, :course_id => -1, :viewed => false, :apply_result => 2, :status => 1) + when 'N' + ar.update_column('status', 3) + @msg.update_attributes(:apply_result => 2, :viewed => 1) + ar.course_messages << CourseMessage.new(:user_id => ar.user_id, :course_id => -1, :viewed => false, :apply_result => 3, :status => 1) + end + end + respond_to do |format| + format.js + end + end + #处理引用作业的请求 #status 1 同意 2 拒绝 def dealwith_apply_homework @@ -419,6 +441,19 @@ class UsersController < ApplicationController end # end + # 资源分享请求弹框 + def apply_resource + if User.current.logged? + @attachment = Attachment.find params[:attachment_id] + @state = 2 + else + @state = 1 + end + respond_to do |format| + format.js + end + end + #引用作业请求弹框 def apply_homework if User.current.logged? @@ -432,6 +467,27 @@ class UsersController < ApplicationController end end + # 申请引用资源 + def apply_for_resource + if User.current.logged? + @attachment = Attachment.where("id =?", params[:attachment_id].to_i).first + unless @attachment.nil? + ar = ApplyResource.where("user_id = ? and attachment_id = ?", User.current.id, params[:attacment_id].to_i) + if ar.empty? + ApplyResource.create(:user_id => params[:id].to_i, :attachment_id => params[:attachment_id].to_i, :status => true, :container_id => @attachment.container_id, :container_type => @attachment.container_type, :content => params[:content], :apply_user_id => @attachment.author_id) + @state = 2 + else + @state = 3 + end + end + else + @state = 1 + end + respond_to do |format| + format.js + end + end + #申请引用非公开作业 def apply_for_homework if User.current.logged? @@ -1886,7 +1942,7 @@ class UsersController < ApplicationController # 将资源发送到对应的课程,分为发送单个,或者批量发送 def add_exist_file_to_course @flag = true - if params[:send_id].present? + if params[:send_id].present? send_id = params[:send_id] @ori = Attachment.find_by_id(send_id) course_ids = params[:course_ids] @@ -1933,14 +1989,14 @@ class UsersController < ApplicationController end send_ids.each do |send_id| quotes = 0 - ori = Attachment.find_by_id(send_id) + @ori = Attachment.find_by_id(send_id) unless course_ids.nil? course_ids.each do |id| quotes = 0 - next if ori.blank? + next if @ori.blank? @exist = false Course.find(id).attachments.each do |att| #如果课程中包含该资源 - if att.id == ori.id || (!att.copy_from.nil? && !ori.copy_from.nil? && att.copy_from == ori.copy_from) || att.copy_from == ori.id || att.id == ori.copy_from + if att.id == @ori.id || (!att.copy_from.nil? && !@ori.copy_from.nil? && att.copy_from == @ori.copy_from) || att.copy_from == @ori.id || att.id == @ori.copy_from att.created_on = Time.now att.save @exist = true @@ -1948,21 +2004,21 @@ class UsersController < ApplicationController end end next if @exist - attach_copied_obj = ori.copy - attach_copied_obj.tag_list.add(ori.tag_list) # tag关联 + attach_copied_obj = @ori.copy + attach_copied_obj.tag_list.add(@ori.tag_list) # tag关联 attach_copied_obj.container = Course.find(id) attach_copied_obj.created_on = Time.now attach_copied_obj.author_id = User.current.id attach_copied_obj.is_public = 0 - attach_copied_obj.copy_from = ori.copy_from.nil? ? ori.id : ori.copy_from #发送要添加copy_from + attach_copied_obj.copy_from = @ori.copy_from.nil? ? @ori.id : @ori.copy_from #发送要添加copy_from if attach_copied_obj.attachtype == nil attach_copied_obj.attachtype = 4 end if attach_copied_obj.save # 更新引用次数 - quotes = ori.quotes.to_i + 1 - ori.update_attribute(:quotes, quotes) unless ori.nil? - ori.forwards << Forward.new(:to_type => attach_copied_obj.class.name, :to_id => attach_copied_obj.id,:created_at => Time.now) + quotes = @ori.quotes.to_i + 1 + @ori.update_attribute(:quotes, quotes) unless @ori.nil? + @ori.forwards << Forward.new(:to_type => attach_copied_obj.class.name, :to_id => attach_copied_obj.id,:created_at => Time.now) end @save_message = attach_copied_obj.errors.full_messages end @@ -2002,6 +2058,18 @@ class UsersController < ApplicationController # 公共资源库:所有公开资源或者我上传的私有资源 @attachments = get_public_resources(user_course_ids, user_project_ids, params[:order], @score) end + elsif params[:type] == "2" + apply_ids = ApplyResource.where("user_id =? and status =?", params[:id], 2).map { |ar| ar.attachment_id} + if params[:status] == "2" + resource_type = "'Course'" + elsif params[:status] == "3" + resource_type = "'Project'" + elsif params[:status] == "5" + resource_type = "'Principal'" + else + resource_type = "'Project','OrgSubfield','Principal','Course'" + end + @attachments = get_my_private_resources(apply_ids, resource_type, @order, @score) end @type = params[:type] @limit = 25 @@ -2143,6 +2211,18 @@ class UsersController < ApplicationController # 公共资源库:所有公开资源或者我上传的私有资源 @attachments = get_public_resources(user_course_ids, user_project_ids, params[:order], @score) end + elsif params[:type] == "2" + apply_ids = ApplyResource.where("user_id =? and status =?", params[:id], 2).map { |ar| ar.attachment_id} + if params[:status] == "2" + resource_type = "'Course'" + elsif params[:status] == "3" + resource_type = "'Project'" + elsif params[:status] == "5" + resource_type = "'Principal'" + else + resource_type = "'Project','OrgSubfield','Principal','Course'" + end + @attachments = get_my_private_resources(apply_ids, resource_type, @order, @score) end @status = params[:status] @type = params[:type] @@ -2272,6 +2352,18 @@ class UsersController < ApplicationController # 公共资源库:所有公开资源或者我上传的私有资源 @attachments = get_public_resources(user_course_ids, user_project_ids, params[:order], @score) end + elsif params[:type] == "2" + apply_ids = ApplyResource.where("user_id =? and status =?", params[:id], 2).map { |ar| ar.attachment_id} + if params[:status] == "2" + resource_type = "'Course'" + elsif params[:status] == "3" + resource_type = "'Project'" + elsif params[:status] == "5" + resource_type = "'Principal'" + else + resource_type = "'Project','OrgSubfield','Principal','Course'" + end + @attachments = get_my_private_resources(apply_ids, resource_type, @order, @score) end @type = params[:type] @limit = 25 @@ -2608,7 +2700,7 @@ class UsersController < ApplicationController # 获取公共资源 def get_public_resources user_course_ids, user_project_ids, order, score - attachments = Attachment.where("(is_publish = 1 and is_public =1 and container_id is not null and container_type in('Project','OrgSubfield','Principal','Course')) ").order("#{order.nil? ? 'created_on' : order} #{score}") + attachments = Attachment.where("(is_publish = 1 and container_id is not null and container_type in('Project','OrgSubfield','Principal','Course')) ").order("#{order.nil? ? 'created_on' : order} #{score}") end # 获取公共资源搜索 @@ -2637,6 +2729,16 @@ class UsersController < ApplicationController and is_publish = 1 and container_id is not null)" ).order("#{order.nil? ? 'created_on' : order} #{score}") end + # 获取我的私有资源分享结果 + def get_my_private_resources apply_ids, resource_type, order, score + attachments = Attachment.where("id in (#{apply_ids.empty? ? '0': apply_ids.join(',')}) and container_type in(#{resource_type})").order("#{order.nil? ? 'created_on' : order} #{score}") + end + + # 获取我的私有资源分享搜索结果 + def get_my_private_resources_search apply_ids, resource_type, order, score, search + attachments = Attachment.where("id in (#{apply_ids.empty? ? '0': apply_ids.join(',')}) and container_type in(#{resource_type}) and (filename like :p)", :p => search).order("#{order.nil? ? 'created_on' : order} #{score}") + end + # 获取我的课程资源中搜索结果 def get_course_resources_search author_id, user_course_ids, order, score, search attchments = Attachment.where("((author_id = #{author_id} and is_publish = 1 and container_id is not null and container_type = 'Course')"+ @@ -2646,12 +2748,12 @@ class UsersController < ApplicationController # 获取公共资源中课程资源 def get_course_resources_public user_course_ids, order, score - attchments = Attachment.where("(container_type = 'Course'and container_id is not null and is_publish = 1 and is_public =1)").order("#{order.nil? ? 'created_on' : order} #{score}") + attchments = Attachment.where("(container_type = 'Course'and container_id is not null and is_publish = 1)").order("#{order.nil? ? 'created_on' : order} #{score}") end # 获取公共资源中课程资源搜索结果 def get_course_resources_public_search user_course_ids, order, score, search - attchments = Attachment.where("(container_type = 'Course'and container_id is not null and is_publish = 1 and is_public =1) and (filename like :p)", :p => search ).order("#{order.nil? ? 'created_on' : order} #{score}") + attchments = Attachment.where("(container_type = 'Course'and container_id is not null and is_publish = 1) and (filename like :p)", :p => search ).order("#{order.nil? ? 'created_on' : order} #{score}") end # 获取我的项目资源 @@ -2670,12 +2772,12 @@ class UsersController < ApplicationController # 获取公共资源的项目资源 def get_project_resources_public user_project_ids, order, score - attchments = Attachment.where("container_type = 'Project' and container_id is not null and is_public =1").order("#{order.nil? ? 'created_on' : order} #{score}") + attchments = Attachment.where("container_type = 'Project' and container_id is not null").order("#{order.nil? ? 'created_on' : order} #{score}") end # 获取公共资源的项目资源搜索 def get_project_resources_public_search user_project_ids, order, score, search - attchments = Attachment.where("(container_type = 'Project' and container_id is not null and is_public =1) and (filename like :p)", :p => search ).order("#{order.nil? ? 'created_on' : order} #{score}") + attchments = Attachment.where("(container_type = 'Project' and container_id is not null) and (filename like :p)", :p => search ).order("#{order.nil? ? 'created_on' : order} #{score}") end # 获取我上传的附件 @@ -2693,13 +2795,13 @@ class UsersController < ApplicationController # 获取公共资源中我上传的附件 def get_attch_resources_public order, score attchments = Attachment.where("container_type in('Issue','Document','Message','News','StudentWorkScore','HomewCommon','OrgSubfield','Principal') - and container_id is not null and is_public =1").order("#{order.nil? ? 'created_on' : order} #{score}") + and container_id is not null").order("#{order.nil? ? 'created_on' : order} #{score}") end # 获取公共资源中我上传的附件 def get_attch_resources_public_search order, score, search attchments = Attachment.where("(container_type in('Issue','Document','Message','News','StudentWorkScore','HomewCommon','OrgSubfield','Principal') - and container_id is not null and is_public =1) and (filename like :p)", :p => search).order("#{order.nil? ? 'created_on' : order} #{score}") + and container_id is not null) and (filename like :p)", :p => search).order("#{order.nil? ? 'created_on' : order} #{score}") end # 获取我的用户类型资源 @@ -2714,12 +2816,12 @@ class UsersController < ApplicationController # 获取我的用户类型资源 def get_principal_resources_public order, score - attchments = Attachment.where("container_type = 'Principal'and container_id is not null and is_public =1").order("#{order.nil? ? 'created_on' : order} #{score}") + attchments = Attachment.where("container_type = 'Principal'and container_id is not null").order("#{order.nil? ? 'created_on' : order} #{score}") end # 获取我的用户类型资源 def get_principal_resources_public_search order, score, search - attchments = Attachment.where("(container_type = 'Principal'and container_id is not null and is_public =1) and (filename like :p)", :p => search).order("#{order.nil? ? 'created_on' : order} #{score}") + attchments = Attachment.where("(container_type = 'Principal'and container_id is not null) and (filename like :p)", :p => search).order("#{order.nil? ? 'created_on' : order} #{score}") end # 资源库 分为全部 课程资源 项目资源 附件 @@ -2734,7 +2836,7 @@ class UsersController < ApplicationController user_course_ids = User.current.courses.map { |c| c.is_delete == 0 && c.id} user_project_ids = User.current.projects.map {|p| p.status != 9 && p.id } # user_org_ids = User.current.organizations.map {|o| o.id} - if(params[:type].blank? || params[:type] == "1") # 我的资源 + if( params[:type] == "1") # 我的资源 # 修正:我的资源库的话,那么应该是我上传的所有资源加上,我加入的课程、项目、组织的所有资源 if params[:status] == "2" @attachments = get_course_resources(params[:id], user_course_ids, @order, @score) @@ -2748,7 +2850,7 @@ class UsersController < ApplicationController # 公共资源库:所有公开资源或者我上传的私有资源 @attachments = get_my_resources(params[:id], user_course_ids, user_project_ids, @order, @score) end - elsif params[:type] == "6" # 公共资源 + elsif (params[:type].blank? || params[:type] == "6") # 公共资源 if params[:status] == "2" @attachments = get_course_resources_public( user_course_ids, @order, @score) elsif params[:status] == "3" @@ -2761,6 +2863,18 @@ class UsersController < ApplicationController # 公共资源库:所有公开资源或者我上传的私有资源 @attachments = get_public_resources(user_course_ids, user_project_ids, params[:order], @score) end + elsif params[:type] == "2" # 私有资源 + apply_ids = ApplyResource.where("user_id =? and status =?", params[:id], 2).map { |ar| ar.attachment_id} + if params[:status] == "2" + resource_type = "'Course'" + elsif params[:status] == "3" + resource_type = "'Project'" + elsif params[:status] == "5" + resource_type = "'Principal'" + else + resource_type = "'Project','OrgSubfield','Principal','Course'" + end + @attachments = get_my_private_resources(apply_ids, resource_type, @order, @score) end @status = params[:status] @type = params[:type] @@ -2780,8 +2894,7 @@ class UsersController < ApplicationController # 导入资源 def import_resources # 别人的资源库是没有权限去看的 - @user = User.find_by_login(params[:id]) - if User.current != @user + if User.current.id != params[:id].to_i render_403 return end @@ -2941,6 +3054,19 @@ class UsersController < ApplicationController # 公共资源库:所有公开资源或者我上传的私有资源 @attachments = get_public_resources_search(user_course_ids, user_project_ids, @order, @score, search) end + elsif params[:type] == "2" # 私有资源 + apply_ids = ApplyResource.where("user_id =? and status =?", params[:id], 2).map { |ar| ar.attachment_id} + if params[:status] == "2" + resource_type = "'Course'" + elsif params[:status] == "3" + resource_type = "'Project'" + elsif params[:status] == "5" + resource_type = "'Principal'" + else + resource_type = "'Project','OrgSubfield','Principal','Course'" + end + @attachments = get_my_private_resources_search(apply_ids, resource_type, @order, @score, search) + @attachments end @status = params[:status] @type = params[:type] diff --git a/app/models/apply_resource.rb b/app/models/apply_resource.rb new file mode 100644 index 000000000..df646166c --- /dev/null +++ b/app/models/apply_resource.rb @@ -0,0 +1,19 @@ +class ApplyResource < ActiveRecord::Base + # status:1. 等待回复 2.审核通过 3.已拒绝 + attr_accessible :attachment_id, :status, :user_id, :container_type, :container_id, :apply_user_id, :content + belongs_to :user + belongs_to :attachment + has_many :course_messages, :class_name => 'CourseMessage', :as => :course_message, :dependent => :destroy + after_create :act_as_apply_resource_message + + def act_as_apply_resource_message + self.course_messages << CourseMessage.new(:user_id => self.apply_user_id, :course_id => -1, :viewed => false, :status => 0, ) + # REDO:发送邮件 + # Mailer.run.apply_for_resource_request(self.container_id, User.current) + end + + def find_attachment attachment_id + Attachment.find(attachment_id) + end + +end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index b3f5ce4ed..b7ec264fd 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -35,6 +35,7 @@ class Attachment < ActiveRecord::Base # end include UserScoreHelper has_many :attachment_histories + has_many :apply_resources, :dependent => :destroy validates :filename, presence: true, length: {maximum: 254} validates :author, presence: true @@ -139,6 +140,11 @@ class Attachment < ActiveRecord::Base } ) end + + def get_apply_resource_status attachment_id, author_id + ApplyResource.where("attachment_id =? and apply_user_id =?", attachment_id, author_id).first.try(:status) + end + # add by nwb # 公开的项目id列表 def self.public_project_id @@ -536,6 +542,11 @@ class Attachment < ActiveRecord::Base end end + # 获取资源申请状态 + def get_status_by_attach user_id + ApplyResource.where("user_id =? and attachment_id =?", user_id, self.id).first.try(:status) + end + private # Physically deletes the file from the file system diff --git a/app/models/course_message.rb b/app/models/course_message.rb index 703da936b..59fcfe456 100644 --- a/app/models/course_message.rb +++ b/app/models/course_message.rb @@ -1,5 +1,7 @@ class CourseMessage < ActiveRecord::Base # status说明: status在课程不同的类型,区分不同的功能 status = 9 作品的提交记录 + # ApplyResource status: + # 0: 发送申请 1:回复允许申请 2:拒绝申请消息 # HomeworkCommon:status: # nil:发布了作业; 1:作业截止时间到了提醒!;2:开启匿评; 3:关闭匿评; 4:匿评开始失败; 5:申请引用作业, 6:申请结果 # apply_user_id: 申请者的用户id diff --git a/app/models/user.rb b/app/models/user.rb index 3956d936e..44ef54c95 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -109,6 +109,7 @@ class User < Principal has_many :student_works_scores, :dependent => :destroy has_many :student_work_projects, :dependent => :destroy has_many :apply_homeworks, :dependent => :destroy + has_many :apply_resources, :dependent => :destroy #end has_and_belongs_to_many :groups, :after_add => Proc.new {|user, group| group.user_added(user)}, diff --git a/app/views/organizations/teachers.html.erb b/app/views/organizations/teachers.html.erb index c368adef7..dac36f7a5 100644 --- a/app/views/organizations/teachers.html.erb +++ b/app/views/organizations/teachers.html.erb @@ -9,8 +9,7 @@
+ <% if state == 2 %> + 请求已发送至<%= attachment.author.show_name %>,希望他同意与你分享:) + <% elsif state == 3 %> + 您已发送过分享请求,请勿重复发送。 + <% end %> +
++ 申请说明(必选): + +
+ +