diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index bd3998621..b650afe1b 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -66,6 +66,7 @@ class ProjectsController < ApplicationController helper :words helper :project_score helper :user_score + include UsersHelper ### added by william include ActsAsTaggableOn::TagsHelper diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index f62a897ff..9cdbe87a5 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -107,7 +107,7 @@ class UsersController < ApplicationController redirect_to signin_url return elsif @user != User.current && !User.current.admin? - return render_403 + return render_403 end # 初始化/更新 点击按钮时间 # 24小时内显示系统消息 @@ -422,11 +422,12 @@ class UsersController < ApplicationController end def choose_user_course + homework = HomeworkCommon.find params[:homework].to_i if !params[:search].nil? search = "%#{params[:search].to_s.strip.downcase}%" - @course = @user.courses.where(" #{Course.table_name}.id = #{params[:search].to_i } or #{Course.table_name}.name like :p",:p=>search).select { |course| @user.allowed_to?(:as_teacher,course)} + @course = @user.courses.where("#{Course.table_name}.id != #{homework.course_id} and #{Course.table_name}.id = #{params[:search].to_i } or #{Course.table_name}.name like :p",:p=>search).select { |course| @user.allowed_to?(:as_teacher,course)} else - @course = @user.courses.select { |course| @user.allowed_to?(:as_teacher,course)} + @course = @user.courses.where("#{Course.table_name}.id != #{homework.course_id}").select { |course| @user.allowed_to?(:as_teacher,course)} end @search = params[:search] #这里仅仅是传递需要发送的资源id @@ -539,6 +540,7 @@ class UsersController < ApplicationController @homeworks = @homeworks.where("homework_type = 3").reorder("created_at desc") end @type = params[:type] + @property = params[:property] @limit = 15 @is_remote = true @hw_count = @homeworks.count @@ -574,6 +576,13 @@ class UsersController < ApplicationController elsif params[:type] == "2" #课程资源 @homeworks = HomeworkCommon.where("user_id = #{@user.id} and publish_time <= '#{Date.today}' and (name like '%#{search}%')").order("created_at desc") end + if params[:property] && params[:property] == "1" + @homeworks = @homeworks.where("homework_type = 1").reorder("created_at desc") + elsif params[:property] && params[:property] == "2" + @homeworks = @homeworks.where("homework_type = 2").reorder("created_at desc") + elsif params[:property] && params[:property] == "3" + @homeworks = @homeworks.where("homework_type = 3").reorder("created_at desc") + end @type = params[:type] @limit = 15 @is_remote = true @@ -683,7 +692,7 @@ class UsersController < ApplicationController student_work.save flash[:notice] = l(:notice_successful_create) redirect_to student_work_index_url(:homework => params[:homework]) - else + else render_403 end end @@ -797,19 +806,23 @@ class UsersController < ApplicationController render_403 return end + user_course_ids = User.current.courses.map { |c| c.id} + user_project_ids = User.current.projects.map {|p| p.id} + # user_org_ids = User.current.organizations.map {|o| o.id} if(params[:type].blank? || params[:type] == "1") #全部 - user_course_ids = User.current.courses.map { |c| c.id} #我的资源库的话,那么应该是我上传的所有资源 加上 我加入的课程的所有资源 - @attachments = Attachment.where("(author_id = #{params[:id]} and container_type in('Project','OrgSubfield','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) "+ - "or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}))").order("created_on desc") - elsif params[:type] == "2" #课程资源 - user_course_ids = User.current.courses.map { |c| c.id} - @attachments = Attachment.where("(author_id = #{params[:id]} and container_type = 'Course') or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')})) ").order("created_on desc") - elsif params[:type] == "3" #项目资源 - @attachments = Attachment.where("author_id = #{params[:id]} and container_type = 'Project'").order("created_on desc") + # 修正:我的资源库的话,那么应该是我上传的所有资源加上,我加入的课程、项目、组织的所有资源 + @attachments = get_my_resources(params[:id], user_course_ids, user_project_ids) + elsif params[:type] == "2" # 课程资源 + @attachments = get_course_resources(params[:id], user_course_ids) + elsif params[:type] == "3" # 项目资源 + @attachments = get_project_resources(params[:id], user_project_ids) elsif params[:type] == "4" #附件 - @attachments = Attachment.where("author_id = #{params[:id]} and container_type in('Issue','Document','Message','News','StudentWorkScore','HomewCommon')").order("created_on desc") + @attachments = get_attch_resources params[:id] elsif params[:type] == "5" #用户资源 - @attachments = Attachment.where("author_id = #{params[:id]} and container_type = 'Principal'").order("created_on desc") + @attachments = get_principal_resources params[:id] + elsif params[:type] == "6" # 公共资源 + # 公共资源库:所有公开资源或者我上传的私有资源 + @attachments = get_public_resources(user_course_ids, user_project_ids) end @type = params[:type] @limit = 7 @@ -842,6 +855,8 @@ class UsersController < ApplicationController @attachments = Attachment.where("author_id = #{params[:id]} and container_type in('Project','Issue','Document','Message','News','StudentWorkScore','HomewCommon') and (filename like '%#{search}%')").order("created_on desc") elsif params[:type] == "5" #用户资源 @attachments = Attachment.where("author_id = #{params[:id]} and container_type = 'Principal' and (filename like '%#{search}%')").order("created_on desc") + elsif params[:type] == "6" #公共资源 + @attachments = Attachment.where("(is_public =1 and is_publish = 1 and container_id is not null)" + "or (author_id = #{params[:id]} and is_publish = 0)").order("created_on desc") end @type = params[:type] @limit = 7 @@ -882,7 +897,7 @@ class UsersController < ApplicationController def store_selected_resource session[:seleted_resource_ids] = [] if session[:seleted_resource_ids].nil? if params[:save] == 'y' - session[:seleted_resource_ids] << params[:res_id] + session[:seleted_resource_ids] << params[:res_id] else session[:seleted_resource_ids].delete( params[:res_id]) end @@ -1188,12 +1203,12 @@ class UsersController < ApplicationController else blog_ids = "("+@user.blog.id.to_s+","+((User.watched_by(@user.id).count == 0 )? '0' :User.watched_by(@user.id).map{|u| u.blog.id}.join(','))+")" @user_activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types})" + - "or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types}) "+ + "or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types}) "+ "or (container_type = 'Principal' and act_type= '#{principal_types}' and container_id = #{@user.id}) " + - "or (container_type = 'Blog' and act_type= 'BlogComment' and container_id in #{blog_ids})").order('updated_at desc').limit(10).offset(@page * 10) + "or (container_type = 'Blog' and act_type= 'BlogComment' and container_id in #{blog_ids})").order('updated_at desc').limit(10).offset(@page * 10) end else - # @user_activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types}) or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types})or (container_type = 'Principal' and act_type= '#{principal_types}' and container_id = #{@user.id})").order('updated_at desc').limit(10).offset(@page * 10) + # @user_activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types}) or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types})or (container_type = 'Principal' and act_type= '#{principal_types}' and container_id = #{@user.id})").order('updated_at desc').limit(10).offset(@page * 10) blog_ids = "("+@user.blog.id.to_s+","+((User.watched_by(@user.id).count == 0 )? '0' :User.watched_by(@user.id).map{|u| u.blog.id}.join(','))+")" @user_activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types})" + "or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types}) "+ @@ -1516,48 +1531,27 @@ class UsersController < ApplicationController # 上传用户资源 def user_resource_create + user_course_ids = User.current.courses.map { |c| c.id} + user_project_ids = User.current.projects.map {|p| p.id} + # user_org_ids = User.current.organizations.map {|o| o.id} @user = User.find(params[:id]) #@user.save_attachments(params[:attachments],User.current) # Container_type为Principal Attachment.attach_filesex(@user, params[:attachments], params[:attachment_type]) - if(params[:type].blank?|| params[:type] == "1") #全部 - if User.current.id.to_i == params[:id].to_i - user_course_ids = User.current.courses.map { |c| c.id} #我的资源库的话,那么应该是我上传的所有资源 加上 我加入的课程的所有资源 - @attachments = Attachment.where("(author_id = #{params[:id]} and container_type in('Project','OrgSubfield','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) "+ - "or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}))").order("created_on desc") - else - user_course_ids = User.find(params[:id]).courses.visible.map { |c| c.id} #Ta的资源库的话,应该是他上传的公开资源 加上 他加入的所有我可见课程里的公开资源 - @attachments = Attachment.where("(author_id = #{params[:id]} and is_public = 1 " + - "and container_type in('Project','OrgSubfield','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) " + - "or (container_type = 'Course' and is_public = 1 and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}))").order("created_on desc") - end - elsif params[:type] == "2" #课程资源 - if User.current.id.to_i == params[:id].to_i - user_course_ids = User.current.courses.map { |c| c.id} - @attachments = Attachment.where("(author_id = #{params[:id]} and container_type = 'Course') or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')})) ").order("created_on desc") - else - user_course_ids = User.find(params[:id]).courses.visible.map { |c| c.id} #如果课程私有资源,那么要看这个资源的课程是不是在 这个user的所有我可见的课程中 - @attachments = Attachment.where("(author_id = #{params[:id]} and is_public = 1 and container_type = 'Course')"+ - "or (container_type = 'Course' and is_public = 1 and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}))").order("created_on desc") - end - elsif params[:type] == "3" #项目资源 - if User.current.id.to_i == params[:id].to_i - @attachments = Attachment.where("author_id = #{params[:id]} and container_type = 'Project'").order("created_on desc") - else - @attachments = Attachment.where("author_id = #{params[:id]} and is_public = 1 and container_type = 'Project' ").order("created_on desc") - end + if(params[:type].nil? || params[:type].blank? || params[:type] == "1" || params[:type] == 'all') #全部 + # 修正:我的资源库的话,那么应该是我上传的所有资源加上,我加入的课程、项目、组织的所有资源 + @attachments = get_my_resources(params[:id], user_course_ids, user_project_ids) + elsif params[:type] == "2" # 课程资源 + @attachments = get_course_resources(params[:id], user_course_ids) + elsif params[:type] == "3" # 项目资源 + @attachments = get_project_resources(params[:id], user_project_ids) elsif params[:type] == "4" #附件 - if User.current.id.to_i == params[:id].to_i - @attachments = Attachment.where("author_id = #{params[:id]} and container_type in('Issue','Document','Message','News','StudentWorkScore','HomewCommon')").order("created_on desc") - else - @attachments = Attachment.where("author_id = #{params[:id]} and is_public = 1 and container_type in('Issue','Document','Message','News','StudentWorkScore','HomewCommon')").order("created_on desc") - end - elsif params[:type] == "5" #附件 - if User.current.id.to_i == params[:id].to_i - @attachments = Attachment.where("author_id = #{params[:id]} and container_type ='Principal'").order("created_on desc") - else - @attachments = Attachment.where("author_id = #{params[:id]} and is_public = 1 and container_type ='Principal'").order("created_on desc") - end + @attachments = get_attch_resources params[:id] + elsif params[:type] == "5" #用户资源 + @attachments = get_principal_resources params[:id] + elsif params[:type] == "6" # 公共资源 + # 公共资源库:所有公开资源或者我上传的私有资源 + @attachments = get_public_resources(user_course_ids, user_project_ids) end @type = params[:type] || 1 @limit = 25 @@ -1581,45 +1575,23 @@ class UsersController < ApplicationController Attachment.where("author_id = #{User.current.id}").delete(id) end end - + user_course_ids = User.current.courses.map { |c| c.id} + user_project_ids = User.current.projects.map {|p| p.id} + # user_org_ids = User.current.organizations.map {|o| o.id} if(params[:type].nil? || params[:type].blank? || params[:type] == "1" || params[:type] == 'all') #全部 - if User.current.id.to_i == params[:id].to_i - user_course_ids = User.current.courses.map { |c| c.id} #我的资源库的话,那么应该是我上传的所有资源 加上 我加入的课程的所有资源 - @attachments = Attachment.where("(author_id = #{params[:id]} and container_type in('Project','OrgSubfield','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) "+ - "or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}))").order("created_on desc") - else - user_course_ids = User.find(params[:id]).courses.visible.map { |c| c.id} #Ta的资源库的话,应该是他上传的公开资源 加上 他加入的所有我可见课程里的公开资源 - @attachments = Attachment.where("(author_id = #{params[:id]} and is_public = 1 " + - "and container_type in('Project','OrgSubfield','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) " + - "or (container_type = 'Course' and is_public = 1 and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}))").order("created_on desc") - end - elsif params[:type] == "2" #课程资源 - if User.current.id.to_i == params[:id].to_i - user_course_ids = User.current.courses.map { |c| c.id} - @attachments = Attachment.where("(author_id = #{params[:id]} and container_type = 'Course') or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')})) ").order("created_on desc") - else - user_course_ids = User.find(params[:id]).courses.visible.map { |c| c.id} #如果课程私有资源,那么要看这个资源的课程是不是在 这个user的所有我可见的课程中 - @attachments = Attachment.where("(author_id = #{params[:id]} and is_public = 1 and container_type = 'Course')"+ - "or (container_type = 'Course' and is_public = 1 and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}))").order("created_on desc") - end - elsif params[:type] == "3" #项目资源 - if User.current.id.to_i == params[:id].to_i - @attachments = Attachment.where("author_id = #{params[:id]} and container_type = 'Project'").order("created_on desc") - else - @attachments = Attachment.where("author_id = #{params[:id]} and is_public = 1 and container_type = 'Project' ").order("created_on desc") - end + # 修正:我的资源库的话,那么应该是我上传的所有资源加上,我加入的课程、项目、组织的所有资源 + @attachments = get_my_resources(params[:id], user_course_ids, user_project_ids) + elsif params[:type] == "2" # 课程资源 + @attachments = get_course_resources(params[:id], user_course_ids) + elsif params[:type] == "3" # 项目资源 + @attachments = get_project_resources(params[:id], user_project_ids) elsif params[:type] == "4" #附件 - if User.current.id.to_i == params[:id].to_i - @attachments = Attachment.where("author_id = #{params[:id]} and container_type in('Issue','Document','Message','News','StudentWorkScore','HomewCommon')").order("created_on desc") - else - @attachments = Attachment.where("author_id = #{params[:id]} and is_public = 1 and container_type in('Issue','Document','Message','News','StudentWorkScore','HomewCommon')").order("created_on desc") - end + @attachments = get_attch_resources params[:id] elsif params[:type] == "5" #用户资源 - if User.current.id.to_i == params[:id].to_i - @attachments = Attachment.where("author_id = #{params[:id]} and container_type ='Principal'").order("created_on desc") - else - @attachments = Attachment.where("author_id = #{params[:id]} and is_public = 1 and container_type ='Principal'").order("created_on desc") - end + @attachments = get_principal_resources params[:id] + elsif params[:type] == "6" # 公共资源 + # 公共资源库:所有公开资源或者我上传的私有资源 + @attachments = get_public_resources(user_course_ids, user_project_ids) end @type = params[:type] @limit = 25 @@ -1883,30 +1855,30 @@ class UsersController < ApplicationController end ori = Attachment.find_by_id(send_id) unless subfield_id.nil? - attach_copied_obj = ori.copy - @exist = false - OrgSubfield.find(subfield_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 - att.created_on = Time.now - att.save - @exist = true - break - end + attach_copied_obj = ori.copy + @exist = false + OrgSubfield.find(subfield_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 + att.created_on = Time.now + att.save + @exist = true + break end - if @exist == false #如果不存在该资源 - attach_copied_obj.tag_list.add(ori.tag_list) # tag关联 - attach_copied_obj.container = OrgSubfield.find(subfield_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 - if attach_copied_obj.attachtype == nil - attach_copied_obj.attachtype = 1 - end - if attach_copied_obj.save - ori.forwards << Forward.new(:to_type => attach_copied_obj.class.name, :to_id => attach_copied_obj.id,:created_at => Time.now) - end + end + if @exist == false #如果不存在该资源 + attach_copied_obj.tag_list.add(ori.tag_list) # tag关联 + attach_copied_obj.container = OrgSubfield.find(subfield_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 + if attach_copied_obj.attachtype == nil + attach_copied_obj.attachtype = 1 end + if attach_copied_obj.save + ori.forwards << Forward.new(:to_type => attach_copied_obj.class.name, :to_id => attach_copied_obj.id,:created_at => Time.now) + end + end end elsif params[:send_ids].present? send_ids = params[:send_ids].split(" ") @@ -1918,30 +1890,30 @@ class UsersController < ApplicationController ori = Attachment.find_by_id(send_id) unless subfield_id.nil? - next if ori.blank? - @exist = false - OrgSubfield.find(subfield_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 - att.created_on = Time.now - att.save - @exist = true - break - end - end - next if @exist - attach_copied_obj = ori.copy - attach_copied_obj.tag_list.add(ori.tag_list) # tag关联 - attach_copied_obj.container = OrgSubfield.find(subfield_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 - if attach_copied_obj.attachtype == nil - attach_copied_obj.attachtype = 1 - end - if attach_copied_obj.save - ori.forwards << Forward.new(:to_type => attach_copied_obj.class.name, :to_id => attach_copied_obj.id,:created_at => Time.now) + next if ori.blank? + @exist = false + OrgSubfield.find(subfield_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 + att.created_on = Time.now + att.save + @exist = true + break end + end + next if @exist + attach_copied_obj = ori.copy + attach_copied_obj.tag_list.add(ori.tag_list) # tag关联 + attach_copied_obj.container = OrgSubfield.find(subfield_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 + if attach_copied_obj.attachtype == nil + attach_copied_obj.attachtype = 1 + end + if attach_copied_obj.save + ori.forwards << Forward.new(:to_type => attach_copied_obj.class.name, :to_id => attach_copied_obj.id,:created_at => Time.now) + end end end else @@ -1976,8 +1948,8 @@ class UsersController < ApplicationController end news.attachments.each do |attach| course_news.attachments << Attachment.new(:filename => attach.filename, :disk_filename => attach.disk_filename, :filesize => attach.filesize, :content_type => attach.content_type, :digest => attach.digest, - :downloads => 0, :author_id => User.current.id, :created_on => Time.now, :description => attach.description, :disk_directory => attach.disk_directory, :attachtype => attach.attachtype, - :is_public => attach.is_public, :quotes => 0) + :downloads => 0, :author_id => User.current.id, :created_on => Time.now, :description => attach.description, :disk_directory => attach.disk_directory, :attachtype => attach.attachtype, + :is_public => attach.is_public, :quotes => 0) end end end @@ -1996,8 +1968,8 @@ class UsersController < ApplicationController end news.attachments.each do |attach| message.attachments << Attachment.new(:filename => attach.filename, :disk_filename => attach.disk_filename, :filesize => attach.filesize, :content_type => attach.content_type, :digest => attach.digest, - :downloads => 0, :author_id => User.current.id, :created_on => Time.now, :description => attach.description, :disk_directory => attach.disk_directory, :attachtype => attach.attachtype, - :is_public => attach.is_public, :quotes => 0) + :downloads => 0, :author_id => User.current.id, :created_on => Time.now, :description => attach.description, :disk_directory => attach.disk_directory, :attachtype => attach.attachtype, + :is_public => attach.is_public, :quotes => 0) end end end @@ -2013,8 +1985,8 @@ class UsersController < ApplicationController end news.attachments.each do |attach| org_news.attachments << Attachment.new(:filename => attach.filename, :disk_filename => attach.disk_filename, :filesize => attach.filesize, :content_type => attach.content_type, :digest => attach.digest, - :downloads => 0, :author_id => User.current.id, :created_on => Time.now, :description => attach.description, :disk_directory => attach.disk_directory, :attachtype => attach.attachtype, - :is_public => attach.is_public, :quotes => 0) + :downloads => 0, :author_id => User.current.id, :created_on => Time.now, :description => attach.description, :disk_directory => attach.disk_directory, :attachtype => attach.attachtype, + :is_public => attach.is_public, :quotes => 0) end OrgActivity.create(:container_type => 'OrgSubfield', :container_id => field_id.to_i, :org_act_type=>'News', :org_act_id => org_news.id, :user_id => User.current.id) end @@ -2282,53 +2254,64 @@ class UsersController < ApplicationController end end + # 获取公共资源 + def get_public_resources user_course_ids, user_project_ids + attachments = Attachment.where("(is_publish = 1 and container_id is not null and container_type in('Project','OrgSubfield','Principal','Course')) "+ + "or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}) and is_publish = 1 and container_id is not null)" + + "or (container_type = 'Project' and container_id in (#{user_project_ids.empty? ? '0': user_project_ids.join(',')}) and is_publish = 1 and container_id is not null)" ).order("created_on desc") + end + + # 获取我的资源 + def get_my_resources author_id, user_course_ids, user_project_ids + attachments = Attachment.where("(author_id = #{author_id} and is_publish = 1 and container_id is not null and container_type in('Project','OrgSubfield','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) "+ + "or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}) and is_publish = 1 and container_id is not null)" + + "or (container_type = 'Project' and container_id in (#{user_project_ids.empty? ? '0': user_project_ids.join(',')}) and is_publish = 1 and container_id is not null)" ).order("created_on desc") + end + + # 获取我的课程资源 + def get_course_resources author_id, user_course_ids + attchments = Attachment.where("(author_id = #{author_id} and container_type = 'Course' and is_publish = 1) or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}) and is_publish = 1) ").order("created_on desc") + end + + # 获取我的项目资源 + def get_project_resources author_id, user_project_ids + attchments = Attachment.where("author_id = #{author_id} and container_type = 'Project'").order("created_on desc") + end + + # 获取我上传的附件 + def get_attch_resources author_id + attchments = Attachment.where("author_id = #{author_id} and container_type in('Issue','Document','Message','News','StudentWorkScore','HomewCommon','OrgSubfield','Principal')").order("created_on desc") + end + + # 获取我的用户类型资源 + def get_principal_resources author_id + attchments = Attachment.where("author_id = #{author_id} and container_type = 'Principal'").order("created_on desc") + end + # 资源库 分为全部 课程资源 项目资源 附件 def user_resource - #确定container_type - # @user = User.find(params[:id]) # 别人的资源库是没有权限去看的 if User.current.id.to_i != params[:id].to_i render_403 return end - if(params[:type].blank? || params[:type] == "1") #全部 - if User.current.id.to_i == params[:id].to_i - user_course_ids = User.current.courses.map { |c| c.id} #我的资源库的话,那么应该是我上传的所有资源 加上 我加入的课程的所有资源 - @attachments = Attachment.where("(author_id = #{params[:id]} and is_publish = 1 and container_type in('Project','OrgSubfield','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) "+ - "or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}) and is_publish = 1)").order("created_on desc") - else - user_course_ids = User.find(params[:id]).courses.visible.map { |c| c.id} #Ta的资源库的话,应该是他上传的公开资源 加上 他加入的所有我可见课程里的公开资源 - @attachments = Attachment.where("(author_id = #{params[:id]} and is_public = 1 and is_publish = 1 " + - "and container_type in('Project','OrgSubfield','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) " + - "or (container_type = 'Course' and is_public = 1 and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}) and is_publish = 1)").order("created_on desc") - end - elsif params[:type] == "2" #课程资源 - if User.current.id.to_i == params[:id].to_i - user_course_ids = User.current.courses.map { |c| c.id} - @attachments = Attachment.where("(author_id = #{params[:id]} and container_type = 'Course' and is_publish = 1) or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}) and is_publish = 1) ").order("created_on desc") - else - user_course_ids = User.find(params[:id]).courses.visible.map { |c| c.id} #如果课程私有资源,那么要看这个资源的课程是不是在 这个user的所有我可见的课程中 - @attachments = Attachment.where("(author_id = #{params[:id]} and is_public = 1 and container_type = 'Course' and is_publish = 1)"+ - "or (container_type = 'Course' and is_public = 1 and is_publish = 1 and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}))").order("created_on desc") - end - elsif params[:type] == "3" #项目资源 - if User.current.id.to_i == params[:id].to_i - @attachments = Attachment.where("author_id = #{params[:id]} and container_type = 'Project'").order("created_on desc") - else - @attachments = Attachment.where("author_id = #{params[:id]} and is_public = 1 and container_type = 'Project' ").order("created_on desc") - end + user_course_ids = User.current.courses.map { |c| c.id} + user_project_ids = User.current.projects.map {|p| p.id} + # user_org_ids = User.current.organizations.map {|o| o.id} + if(params[:type].blank? || params[:type] == "1") # 我的资源 + # 修正:我的资源库的话,那么应该是我上传的所有资源加上,我加入的课程、项目、组织的所有资源 + @attachments = get_my_resources(params[:id], user_course_ids, user_project_ids) + elsif params[:type] == "2" # 课程资源 + @attachments = get_course_resources(params[:id], user_course_ids) + elsif params[:type] == "3" # 项目资源 + @attachments = get_project_resources(params[:id], user_project_ids) elsif params[:type] == "4" #附件 - if User.current.id.to_i == params[:id].to_i - @attachments = Attachment.where("author_id = #{params[:id]} and container_type in('Issue','Document','Message','News','StudentWorkScore','HomewCommon','OrgSubfield','Principal')").order("created_on desc") - else - @attachments = Attachment.where("author_id = #{params[:id]} and is_public = 1 and container_type in('Issue','Document','Message','News','StudentWorkScore','HomewCommon','OrgSubfield','Principal')").order("created_on desc") - end + @attachments = get_attch_resources params[:id] elsif params[:type] == "5" #用户资源 - if User.current.id.to_i == params[:id].to_i - @attachments = Attachment.where("author_id = #{params[:id]} and container_type = 'Principal'").order("created_on desc") - else - @attachments = Attachment.where("author_id = #{params[:id]} and is_public = 1 and container_type = 'Principal'").order("created_on desc") - end + @attachments = get_principal_resources params[:id] + elsif params[:type] == "6" # 公共资源 + # 公共资源库:所有公开资源或者我上传的私有资源 + @attachments = get_public_resources(user_course_ids, user_project_ids) end @type = params[:type] @limit = 25 @@ -2340,7 +2323,98 @@ class UsersController < ApplicationController @attachments = paginateHelper @attachments,25 respond_to do |format| format.js - format.html {render :layout => 'new_base_user'} + format.html {render :layout => 'new_base'} + end + end + + # 导入资源 + def import_resources + # 别人的资源库是没有权限去看的 + if User.current.id.to_i != params[:id].to_i + render_403 + return + end + user_course_ids = User.current.courses.map { |c| c.id} + user_project_ids = User.current.projects.map {|p| p.id} + # user_org_ids = User.current.organizations.map {|o| o.id} + if(params[:type].blank? || params[:type] == "1") # 我的资源 + # 修正:我的资源库的话,那么应该是我上传的所有资源加上,我加入的课程、项目、组织的所有资源 + @attachments = get_my_resources(params[:id], user_course_ids, user_project_ids) + elsif params[:type] == "6" # 公共资源 + # 公共资源库:所有公开资源或者我上传的私有资源 + @attachments = get_public_resources(user_course_ids, user_project_ids) + end + @type = params[:type] + @limit = 10 + @is_remote = true + @atta_count = @attachments.count + @atta_pages = Paginator.new @atta_count, @limit, params['page'] || 1 + @offset ||= @atta_pages.offset + #@curse_attachments_all = @all_attachments[@offset, @limit] + @attachments = paginateHelper @attachments,10 + respond_to do |format| + format.js + format.html {render :layout => 'new_base'} + end + end + + # 内容导入到对象中 + def import_into_container + # attachments = Attachment.where("id in (#{params[:checkbox1].join(',')})") + # if params[:mul_type] == "Project" + # + # elsif params[:mul_type] == "Course" + # Attachment.create(:container_id => params[:mul_id], :container_type => "Course", :name => attachment.name) + # elsif params[:mul_type] == "SubfieldFile" + # end + + unless params[:checkbox1].blank? + send_ids = params[:checkbox1] + # mul_id为当前课程id、项目id、组织id的多种形态 + mul_id = params[:mul_id] + if params[:mul_type] == "Course" + mul_container = Course.find(mul_id) + elsif params[:mul_type] == "Project" + mul_container = Project.find(mul_id) + elsif params[:mul_type] == "SubfieldFile" + mul_container = OrgSubfield.find(mul_id) + end + send_ids.each do |send_id| + ori = Attachment.find_by_id(send_id) + # 如果该附件已经存课程中,则只更新附件创建时间 + mul_container.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 + att.created_on = Time.now + att.save + @exist = true + break + end + end + next if @exist + attach_copied_obj = ori.copy + attach_copied_obj.tag_list.add(ori.tag_list) # tag关联 + attach_copied_obj.container = mul_container + 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 + if attach_copied_obj.attachtype == nil + attach_copied_obj.attachtype = 4 + end + attach_copied_obj.save + @save_message = attach_copied_obj.errors.full_messages + end + end + respond_to do |format| + format.html { + if params[:mul_type] == "Course" + redirect_to course_files_url(mul_container) + elsif params[:mul_type] == "Project" + redirect_to project_files_url(mul_container) + elsif params[:mul_type] == "SubfieldFile" + redirect_to org_subfield_files_url(mul_container) + end + } end end @@ -2387,6 +2461,10 @@ class UsersController < ApplicationController else @attachments = Attachment.where("author_id = #{params[:id]} and is_public = 1 and container_type = 'Principal' and (filename like :p)",:p=>search).order("created_on desc") end + elsif params[:type] == "6" #全部资源 + # 公共资源库:所有公开资源或者我上传的私有资源 + @attachments = Attachment.where("((is_public =1 and is_publish = 1 and container_id is not null)" + + "or (author_id = #{params[:id]} and is_publish = 0)) and (filename like :p) ", :p => search).order("created_on desc") end @type = params[:type] @limit = 25 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 431c09d15..c37d3ad55 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -115,6 +115,15 @@ module ApplicationHelper def course_member_score(course_id,user_id,type) course_contributor_score = CourseContributorScore.where("course_id =? and user_id =?", course_id, user_id).first case type + when "HomeworkCommon" + if course_contributor_score.nil? + CourseContributorScore.create(:course_id => course_id, :user_id => user_id, :message_num => 0, :message_reply_num => 0, + :news_reply_num => 0, :resource_num => 0, :journal_num => 0, :journal_reply_num => 0, :homework_journal_num => 1 , :total_score => 1) + else + score = course_contributor_score.homework_journal_num + 1 + total_score = course_contributor_score.total_score + 1 + course_contributor_score.update_attributes(:homework_journal_num => score, :total_score => total_score) + end when "JournalForMessage" if course_contributor_score.nil? CourseContributorScore.create(:course_id => course_id, :user_id => user_id, :message_num => 0, :message_reply_num => 0, diff --git a/app/helpers/courses_helper.rb b/app/helpers/courses_helper.rb index 229a7c7d5..6fb9380e7 100644 --- a/app/helpers/courses_helper.rb +++ b/app/helpers/courses_helper.rb @@ -25,7 +25,7 @@ module CoursesHelper # 获取tag匹配结果ID a_tags = [] # kc = keywords.to_a - Course.visible.where("is_excellent =?", 1).each do |ec| + Course.visible.where("is_excellent =? and is_public =?", 1, 1).each do |ec| if ec.tags.any?{|value| current_course.name.include?(value.to_s)} a_tags << ec.id end @@ -44,7 +44,7 @@ module CoursesHelper excellent_ids = a_courses.flatten.uniq.delete_if{|i| i == current_course.id} limit = 5 - excellent_ids.length.to_i sql = "SELECT distinct c.id FROM course_activities cs, courses c where cs.course_id = c.id - and c.is_excellent =1 and c.id != #{current_course.id} order by cs.updated_at desc;" + and c.is_excellent =1 and c.is_public = 1 and c.id != #{current_course.id} order by cs.updated_at desc;" default_ecourse_ids = Course.find_by_sql(sql).flatten # REDO:时间紧,待优化 default_ids =[] diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 36b81b353..8ff569e65 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -54,6 +54,61 @@ module UsersHelper end end + def get_resource_origin attach + type = attach.container_type + content = attach.container + unless content.nil? + case type + when 'Course' + result = current_time_and_term_resource content + when 'Project' + result = content.name + when 'Issue' + result = content.subject + when 'Message' + result = content.subject + when 'News' + result = content.title + when 'HomewCommon' + result = content.name + when 'StudentWorkScore' + result = content.name + when 'Principal' + result = content.name + when 'OrgSubfield' + result = content.name + end + end + end + + def current_time_and_term_resource course + str = "" + term = cur_course_term_resource + name = course.name + if (course.time == course.end_time && course.term == course.end_term) || (course.end_term.nil? && course.end_time.nil?) || course.time > Time.now.year + str = name + "(" + course.time.to_s + course.term.to_s + ")" + elsif course.time == Time.now.year && set_term_value(cur_course_term) <= set_term_value(course.term) + str = name + "(" + course.time.to_s + course.term.to_s + ")" + elsif course.end_time < Time.now.year || (course.end_time == Time.now.year && set_term_value(cur_course_term) >= set_term_value(course.term)) + str = name + "(" + course.end_time.to_s + course.end_term.to_s + ")" + else + str = name + "(" + Time.now.year.to_s + cur_course_term_resource.to_s + ")" + end + str + end + + def cur_course_term_resource + month = Time.now.month + if month >= 9 || month < 2 + term = "秋" + elsif (month >= 7 && Time.now.day >= 15) || month == 8 + term = "夏" + else + term = "春" + end + term + end + def title_for_message type case type when nil diff --git a/app/models/course_contributor_score.rb b/app/models/course_contributor_score.rb index f2b05458f..ca21ddb1c 100644 --- a/app/models/course_contributor_score.rb +++ b/app/models/course_contributor_score.rb @@ -1,5 +1,5 @@ class CourseContributorScore < ActiveRecord::Base - attr_accessible :course_id, :journal_num, :journal_reply_num, :message_num, :message_reply_num, :news_reply_num, :resource_num, :user_id, :total_score + attr_accessible :course_id, :journal_num, :journal_reply_num, :message_num, :message_reply_num, :news_reply_num, :resource_num, :user_id, :total_score, :homework_journal_num belongs_to :course belongs_to :user end diff --git a/app/models/journals_for_message.rb b/app/models/journals_for_message.rb index cdcf9d19e..044d30551 100644 --- a/app/models/journals_for_message.rb +++ b/app/models/journals_for_message.rb @@ -285,8 +285,10 @@ class JournalsForMessage < ActiveRecord::Base # 课程成员得分(英雄榜) def act_as_student_score - if !self.user.allowed_to?(:as_teacher, self.jour) && self.jour_type == "Course" + if self.jour_type == "Course" course_member_score(self.jour_id, self.user_id, "JournalForMessage") + elsif self.jour_type == "HomeworkCommon" + course_member_score(self.jour_id, self.user_id, "HomeworkCommon") end end diff --git a/app/views/courses/member.html.erb b/app/views/courses/member.html.erb index 2fe18c4eb..7a58315a2 100644 --- a/app/views/courses/member.html.erb +++ b/app/views/courses/member.html.erb @@ -2,7 +2,7 @@

<%= @subPage_title%>

<% if User.current.allowed_to?(:as_teacher,@course) %> - <%=link_to "修改角色", :controller => 'courses', :action => 'settings', :id => @course.id, :tab=>'member' %> + <%=link_to "成员管理", :controller => 'courses', :action => 'settings', :id => @course.id, :tab=>'member' %> <% end %> diff --git a/app/views/files/_course_file.html.erb b/app/views/files/_course_file.html.erb index cf91b7814..4a62c94aa 100644 --- a/app/views/files/_course_file.html.erb +++ b/app/views/files/_course_file.html.erb @@ -93,7 +93,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("导入资源", import_resources_user_path(User.current, :type => 6, :course_id => @course.id), :class => "blue-btn fr mr5", :remote => true) %> <% end %> <% end %> diff --git a/app/views/files/_import_files.html.erb b/app/views/files/_import_files.html.erb new file mode 100644 index 000000000..168cea962 --- /dev/null +++ b/app/views/files/_import_files.html.erb @@ -0,0 +1,138 @@ +<%#= render :partial => 'users/user_resource_info' %> +
选用资源库中的资源
+
公共资源 我的资源 + +
+
+ + + + + + + + + + + +
+
选用
+
取消
+
+ + + + + + + +
+
+
+
diff --git a/app/views/files/_project_file.html.erb b/app/views/files/_project_file.html.erb index e8f4f023b..98b9ad514 100644 --- a/app/views/files/_project_file.html.erb +++ b/app/views/files/_project_file.html.erb @@ -62,10 +62,12 @@ <%= form_tag( search_project_project_files_path(@project), method: 'get',:class => "re_search",:remote=>true) do %> <%= text_field_tag 'name', params[:name], name: "name", :class => 're_schbox',:style=>"padding: 0px"%> <%= submit_tag "项目内搜索", :class => "re_schbtn b_lblue",:name => "incourse",:id => "incourse", :onmouseover => "presscss('incourse')",:onmouseout =>"buttoncss()", :style =>"width:72px;" %> - <%= submit_tag "全站搜索", :class => "re_schbtn b_lblue",:name => "insite",:id => "insite",:onmouseover => "presscss('insite')",:onmouseout =>"buttoncss()" %> + <%#= submit_tag "全站搜索", :class => "re_schbtn b_lblue",:name => "insite",:id => "insite",:onmouseover => "presscss('insite')",:onmouseout =>"buttoncss()" %> <% if User.current.member_of?(@project) %> + <%= link_to("导入资源", import_resources_user_path(User.current, :type => 6, :project_id => @project.id), :class => "blue-btn fr mr5", :remote => true) %> + <% end %> <% end %> diff --git a/app/views/files/_subfield_files.html.erb b/app/views/files/_subfield_files.html.erb index 1de0e8dc2..2a8756c47 100644 --- a/app/views/files/_subfield_files.html.erb +++ b/app/views/files/_subfield_files.html.erb @@ -42,6 +42,7 @@ <%= submit_tag "栏目内搜索", :class => "blueBtn mr5 fl",:style => 'width:72px;',:name => "inorg_subfield",:id => "inorg_subfield", :onmouseover => "presscss('inorg_subfield')",:onmouseout =>"buttoncss()" %> <%= submit_tag "全站搜索", :class => "blueBtn mr5 fl",:name => "insite",:id => "insite",:onmouseover => "presscss('insite')",:onmouseout =>"buttoncss()" %> + <%= link_to("导入资源", import_resources_user_path(User.current, :type => 6, :subfield_file_id => @org_subfield.id), :class => "blue-btn fr mr5", :remote => true) %> <%#= link_to "上传资源",subfield_upload_file_org_subfield_files_path(@org_subfield.id, :in_org => 1),:method => "post",:class=>"blueBtn fr mr5",:remote => true %> <% end %> diff --git a/app/views/layouts/_logined_header.html.erb b/app/views/layouts/_logined_header.html.erb index 68e951896..f2de04ea1 100644 --- a/app/views/layouts/_logined_header.html.erb +++ b/app/views/layouts/_logined_header.html.erb @@ -8,11 +8,11 @@ <%= link_to "首页",user_activities_path(User.current.id), :class => "c_white f16 db p10", :title => "回到个人首页"%> + 资源库 <% if User.current.user_extensions && User.current.user_extensions.identity == 0 && User.current.allowed_to?(:add_course, nil, :global => true)%> - + <% else %>