diff --git a/Gemfile b/Gemfile
index 3027c16cd..c739e1085 100644
--- a/Gemfile
+++ b/Gemfile
@@ -30,14 +30,26 @@ gem 'rails_kindeditor',path:'lib/rails_kindeditor'
#gem "rmagick", ">= 2.0.0"
gem 'binding_of_caller'
gem 'chinese_pinyin'
+# gem 'sunspot_rails', '~> 1.3.3'
+# gem 'sunspot_solr'
+# gem 'sunspot'
+# gem 'progress_bar'
+gem 'ansi'
+
+gem 'kaminari'
+gem 'elasticsearch-model'
+gem 'elasticsearch-rails'
group :development do
gem 'grape-swagger'
gem 'better_errors', '~> 1.1.0'
gem 'rack-mini-profiler', '~> 0.9.3'
+ if RUBY_PLATFORM =~ /w32/
+ gem 'win32console'
+ end
end
-group :development, :test do
+group :development, :test do
unless RUBY_PLATFORM =~ /w32/
gem 'pry-rails'
if RUBY_VERSION >= '2.0.0'
@@ -46,7 +58,7 @@ group :development, :test do
gem 'pry-stack_explorer'
if RUBY_PLATFORM =~ /darwin/
gem 'puma'
- end
+ end
end
gem 'rspec-rails', '~> 3.0'
@@ -60,7 +72,7 @@ group :assets do
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
- gem 'therubyracer', :platforms => :ruby
+ gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
end
diff --git a/app/api/mobile/apis/courses.rb b/app/api/mobile/apis/courses.rb
index 076e0b1c7..591a4e288 100644
--- a/app/api/mobile/apis/courses.rb
+++ b/app/api/mobile/apis/courses.rb
@@ -272,7 +272,7 @@ module Mobile
params do
requires :token,type:String
requires :course_id,type:Integer,desc: '课程id'
- optional :name,type:String,desc:'学生的姓名或者昵称或者学号可能包含的字符'
+ optional :name,type:String,desc:'学生的姓名或者登录名或者学号可能包含的字符'
end
get ":course_id/members" do
cs = CoursesService.new
diff --git a/app/api/mobile/apis/users.rb b/app/api/mobile/apis/users.rb
index 583a130d1..6ce3cacbb 100644
--- a/app/api/mobile/apis/users.rb
+++ b/app/api/mobile/apis/users.rb
@@ -82,7 +82,7 @@ module Mobile
desc "用户搜索"
params do
requires :name, type: String, desc: '用户名关键字'
- requires :search_by, type: String,desc: '搜索依据:0 昵称,1 用户名,2 邮箱,3 昵称和姓名'
+ requires :search_by, type: String,desc: '搜索依据:0 登录名,1 用户名,2 邮箱,3 登录名和姓名'
optional :is_search_assitant,type:Integer,desc:'是否搜索注册用户来作为助教'
optional :course_id,type:Integer,desc: '课程id,搜索注册用户不为该课程教师的其他用户'
optional :user_id,type:Integer,desc:'用户id'
diff --git a/app/assets/javascripts/org_subfields.js.coffee b/app/assets/javascripts/org_subfields.js.coffee
new file mode 100644
index 000000000..761567942
--- /dev/null
+++ b/app/assets/javascripts/org_subfields.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
diff --git a/app/assets/javascripts/shield_activities.js.coffee b/app/assets/javascripts/shield_activities.js.coffee
new file mode 100644
index 000000000..761567942
--- /dev/null
+++ b/app/assets/javascripts/shield_activities.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
diff --git a/app/assets/javascripts/subfield_subdomain_dirs.js.coffee b/app/assets/javascripts/subfield_subdomain_dirs.js.coffee
new file mode 100644
index 000000000..761567942
--- /dev/null
+++ b/app/assets/javascripts/subfield_subdomain_dirs.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
diff --git a/app/assets/stylesheets/org_subfields.css.scss b/app/assets/stylesheets/org_subfields.css.scss
new file mode 100644
index 000000000..19263385e
--- /dev/null
+++ b/app/assets/stylesheets/org_subfields.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the org_subfields controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
diff --git a/app/assets/stylesheets/shield_activities.css.scss b/app/assets/stylesheets/shield_activities.css.scss
new file mode 100644
index 000000000..9ac6bff3e
--- /dev/null
+++ b/app/assets/stylesheets/shield_activities.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the shield_activities controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
diff --git a/app/assets/stylesheets/subfield_subdomain_dirs.css.scss b/app/assets/stylesheets/subfield_subdomain_dirs.css.scss
new file mode 100644
index 000000000..6843d92f8
--- /dev/null
+++ b/app/assets/stylesheets/subfield_subdomain_dirs.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the subfield_subdomain_dirs controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb
index 9e935ae40..68386de44 100644
--- a/app/controllers/admin_controller.rb
+++ b/app/controllers/admin_controller.rb
@@ -461,8 +461,8 @@ class AdminController < ApplicationController
scope = User.order('last_login_on desc')
scope = scope.where("last_login_on>= '#{params[:startdate]} 00:00:00'") if params[:startdate].present?
scope =scope.where("last_login_on <= '#{params[:enddate]} 23:59:59'") if params[:enddate].present?
- @user = scope
- @user = paginateHelper @user,30
+ @users = scope
+ @users = paginateHelper @users,30
@page = (params['page'] || 1).to_i - 1
respond_to do |format|
format.html
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 6387a97a4..d6470ce51 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -312,7 +312,8 @@ class ApplicationController < ActionController::Base
elsif @course
allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @course || @courses, :global => global)
else
- allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global)
+ # allowed = User.current.allowed_to?({:controller => ctrl, :action => action}, @project || @projects, :global => global)
+ allowed = true
end
allowed
end
@@ -418,6 +419,8 @@ class ApplicationController < ActionController::Base
@project = Project.find(params[:project_id])
elsif params[:course_id]
@course = Course.find(params[:course_id])
+ elsif params[:org_subfield_id]
+ @org_subfield = OrgSubfield.find(params[:org_subfield_id])
end
rescue ActiveRecord::RecordNotFound
render_404
@@ -767,6 +770,7 @@ class ApplicationController < ActionController::Base
end
def api_request?
+ return false if params[:controller] == 'at'
%w(xml json).include? params[:format]
end
diff --git a/app/controllers/at_controller.rb b/app/controllers/at_controller.rb
new file mode 100644
index 000000000..456306c64
--- /dev/null
+++ b/app/controllers/at_controller.rb
@@ -0,0 +1,164 @@
+#coding=utf-8
+
+class AtController < ApplicationController
+ respond_to :json
+
+ def show
+ @logger = Logger.new(Rails.root.join('log', 'at.log').to_s)
+ users = find_at_users(params[:type], params[:id])
+ @users = users
+ @users = users.uniq { |u| u.id }.delete_if { |u| u.id == User.current.id }.sort{|x,y| to_pinyin(x.show_name) <=> to_pinyin(y.show_name)} if users
+ end
+
+ private
+ def to_pinyin(s)
+ Pinyin.t(s).downcase
+ end
+
+ def find_at_users(type, id)
+ @logger.info("#{type}, #{id}")
+ case type
+ when "Issue"
+ find_issue(id)
+ when 'Project'
+ find_project(id)
+ when 'Course'
+ find_course(id)
+ when 'Activity', 'CourseActivity', 'ForgeActivity','UserActivity', 'OrgActivity','PrincipalActivity'
+ find_activity(id, type)
+ when 'Attachment'
+ find_attachment(id)
+ when 'Message'
+ find_message(id)
+ when 'HomeworkCommon'
+ find_homework(id)
+ when 'Topic'
+ find_topic(id)
+ when 'JournalsForMessage'
+ find_journals_for_message(id)
+ when 'Principal'
+ find_principal(id)
+ when 'All'
+ nil
+ else
+ nil
+ end
+ end
+
+ def find_topic(id)
+
+ end
+
+ def find_issue(id)
+ #1. issues list persons
+ #2. project persons
+ issue = Issue.find(id)
+ journals = issue.journals
+ at_persons = journals.map(&:user) + issue.project.users
+ at_persons.uniq { |u| u.id }.delete_if { |u| u.id == User.current.id }
+ end
+
+ def find_project(id)
+ return [] if id<0
+ at_persons = Project.find(id).users
+ at_persons.delete_if { |u| u.id == User.current.id }
+ end
+
+ def find_course(id)
+ at_persons = Course.find(id).users
+ at_persons.delete_if { |u| u.id == User.current.id }
+ end
+
+ def find_activity(id, type)
+
+ ## 基本上是本类型中的 加上所属类型的用户
+ case type
+ when 'Activity'
+ activity = Activity.find(id)
+ (find_at_users(activity.act_type, activity.act_id) ||[]) +
+ (find_at_users(activity.activity_container_type, activity.activity_container_id) || [])
+ when 'CourseActivity'
+ activity = CourseActivity.find(id)
+ (find_at_users(activity.course_act_type, activity.course_act_id) || []) + (find_course(activity.course.id) || [])
+ when 'ForgeActivity'
+ activity = ForgeActivity.find(id)
+ (find_at_users(activity.forge_act_type, activity.forge_act_id) ||[]) +
+ (find_project(activity.project_id) || [])
+ when 'UserActivity'
+ activity = UserActivity.find(id)
+ (find_at_users(activity.act_type, activity.act_id) || []) +
+ (find_at_users(activity.container_type, activity.container_id) || [])
+ when 'OrgActivity'
+ activity = OrgActivity.find(id)
+ (find_at_users(activity.org_act_type, activity.org_act_id) || []) +
+ (find_at_users(activity.container_type, activity.container_id) || [])
+ when 'PrincipalActivity'
+ activity = PrincipalActivity.find(id)
+ find_at_users(activity.principal_act_type, activity.principal_act_id)
+ else
+ nil
+ end
+ end
+
+ #作业应该是关联课程,取课程的用户列表
+ def find_homework(id)
+ homework = HomeworkCommon.find(id)
+ find_course(homework.course_id)
+ end
+
+ def find_attachment(id)
+ attachment = Attachment.find(id)
+ find_at_users(attachment.container_type, attachment.container_id)
+ end
+
+ #Message
+ def find_message(id)
+ message = Message.find(id)
+ at_persons = message.board.messages.map(&:author)
+ (at_persons || []) + (find_project(message.board.project_id)||[])
+ end
+
+ #News
+ def find_news(id)
+ find_project(News.find(id).project_id)
+ end
+
+ #JournalsForMessage
+ def find_journals_for_message(id)
+ jounrnal = JournalsForMessage.find(id)
+ if jounrnal.jour_type == 'Principal'
+ [jounrnal.user] + (JournalsForMessage.where(m_reply_id: id).map(&:user) || [])
+ else
+ find_at_users(jounrnal.jour_type, jounrnal.jour_id)
+ end
+ end
+
+ #Poll
+ def find_poll(id)
+ end
+
+ #Journal
+ def find_journal(id)
+ end
+
+ #Document
+ def find_document(id)
+ find_project(Document.find(id).project_id)
+ end
+
+ #ProjectCreateInfo
+ def find_project_create_info(id)
+
+ end
+
+ #Principal
+ def find_principal(id)
+ end
+
+ #BlogComment
+ def find_blog_comment(id)
+ blog = BlogComment.find(id).blog
+ blog.users
+ end
+
+end
\ No newline at end of file
diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb
index 37182dbcf..cf4e6a8e9 100644
--- a/app/controllers/attachments_controller.rb
+++ b/app/controllers/attachments_controller.rb
@@ -74,6 +74,45 @@ class AttachmentsController < ApplicationController
:disposition => 'attachment' #inline can open in browser
end
+ def direct_download_history
+ @attachment_history = AttachmentHistory.find(params[:id])
+ @attachment_history.increment_download
+ send_file @attachment_history.diskfile_history, :filename => filename_for_content_disposition(@attachment_history.filename),
+ :type => detect_content_type(@attachment_history),
+ :disposition => 'attachment' #inline can open in browser
+ end
+
+ def download_history
+ @attachment_history = AttachmentHistory.find(params[:id])
+ candown = attachment_history_candown @attachment_history
+ if candown || User.current.admin? || User.current.id == @attachment_history.author_id
+ if stale?(:etag => @attachment_history.digest)
+ if params[:preview] == 'true'
+ convered_file = @attachment_history.diskfile_history
+ #如果本身不是pdf文件,则先寻找是不是已转换化,如果没有则转化
+ unless pdf?(convered_file)
+ convered_file = File.join(Rails.root, "files", "convered_office", @attachment.disk_filename + ".pdf")
+ unless File.exist?(convered_file)
+ office = Trustie::Utils::Office.new(@attachment_history.diskfile)
+ office.conver(convered_file)
+ end
+ end
+ if File.exist?(convered_file) && pdf?(convered_file)
+ send_file convered_file, :type => 'application/pdf; charset=utf-8', :disposition => 'inline'
+ else
+ direct_download_history
+ end
+ else
+ direct_download_history
+ end
+ end
+ else
+ render_403 :message => :notice_not_authorized
+ end
+ rescue => e
+ redirect_to "http://" + (Setting.host_name.to_s) +"/file_not_found.html"
+ end
+
def download
# modify by nwb
# 下载添加权限设置
@@ -185,7 +224,6 @@ class AttachmentsController < ApplicationController
end
@attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
saved = @attachment.save
-
respond_to do |format|
format.js
format.api {
@@ -198,12 +236,43 @@ class AttachmentsController < ApplicationController
end
end
+ def upload_attachment_version
+ @flag = false
+ Attachment.transaction do
+ @old_attachment = Attachment.find params[:old_attachment_id]
+ #取出当前上传的文件
+ @attachment = Attachment.find(params[:attachments ].first[1][:attachment_id])
+ #将需要修改的记录保存到历史记录
+ @history = AttachmentHistory.new
+ @history.attributes = @old_attachment.attributes.dup.except("id")
+ @history.attachment_id = params[:old_attachment_id]
+ #需要更新版本号,需要拿到原来该文件最大的历史版本号
+ @old_history = @old_attachment.attachment_histories.reorder('version desc').first
+ @history.version = @old_history.nil? ? 1 : @old_history.version + 1
+ @history.save #历史记录保存完毕
+ #将最新保存的记录 数据替换到 需要修改的文件记录
+ @old_attachment.attributes = @attachment.attributes.dup.except("id","container_id","container_type","is_public","downloads")
+ @old_attachment.save
+ #删除当前记录
+ @attachment.delete
+ @flag = true
+ end
+
+ respond_to do |format|
+ format.js
+ end
+ end
+
def destroy
if @attachment.container.respond_to?(:init_journal)
@attachment.container.init_journal(User.current)
end
if @attachment.container
- @attachment.container.attachments.delete(@attachment)
+ if @attachment.container_type == "Issue"
+ @attachment.destroy
+ else
+ @attachment.container.attachments.delete(@attachment)
+ end
else
@attachment.destroy
end
@@ -240,6 +309,10 @@ class AttachmentsController < ApplicationController
format.html { redirect_to_referer_or respond_path(@attachment.container) }
elsif !@attachment.container.nil? && @attachment.container.is_a?(PhoneAppVersion)
format.html { redirect_to_referer_or mobile_version_path }
+ elsif !@attachment.container.nil? && @attachment.container.is_a?(OrgSubfield)
+ format.html {redirect_to_referer_or org_subfield_files_path(@attachment.container)}
+ elsif !@attachment.container.nil? && @attachment.container.is_a?(OrgDocumentComment)
+ format.html {redirect_to_referer_or org_document_comment_path(@attachment.container)}
else
if @project.nil?
format.html { redirect_to_referer_or forum_memo_path(@attachment.container.forum, @attachment.container) }
@@ -251,7 +324,7 @@ class AttachmentsController < ApplicationController
format.js
end
end
-
+
def delete_homework
@bid = @attachment.container.bid
# Make sure association callbacks are called
@@ -314,13 +387,13 @@ class AttachmentsController < ApplicationController
end
respond_to do |format|
- format.js
+ format.js
end
rescue NoMethodError
@save_flag = false
@save_message = [] << l(:error_attachment_empty)
respond_to do |format|
- format.js
+ format.js
end
end
@@ -436,6 +509,46 @@ class AttachmentsController < ApplicationController
end
end
+ def add_exist_file_to_org_subfield
+ file = Attachment.find(params[:file_id])
+ org_subfields = params[:org_subfields][:org_subfield]
+ @message = ""
+ org_subfields.each do |org_subfield|
+ s = OrgSubfield.find(org_subfield)
+ if s.attachments.include?file
+ if @message && @message == ""
+ @message += l(:label_resource_subfield_prompt) + c.name + l(:label_contain_resource) + file.filename + l(:label_quote_resource_failed)
+ next
+ else
+ @message += "
" + l(:label_resource_subfield_prompt) + c.name + l(:label_contain_resource) + file.filename + l(:label_quote_resource_failed)
+ next
+ end
+ end
+ attach_copied_obj = file.copy
+ attach_copied_obj.tag_list.add(file.tag_list) # tag关联
+ attach_copied_obj.container = s
+ attach_copied_obj.created_on = Time.now
+ attach_copied_obj.author_id = User.current.id
+ attach_copied_obj.copy_from = file.copy_from.nil? ? file.id : file.copy_from
+ if attach_copied_obj.attachtype == nil
+ attach_copied_obj.attachtype = 4
+ end
+ @obj = s
+ @save_flag = attach_copied_obj.save
+ @save_message = attach_copied_obj.errors.full_messages
+ update_quotes attach_copied_obj
+ end
+ respond_to do |format|
+ format.js
+ end
+ rescue NoMethodError
+ @save_flag = false
+ @save_message = [] << l(:label_resource_subfield_empty_select)
+ respond_to do |format|
+ format.js
+ end
+ end
+
def update_quotes attachment
if attachment.copy_from
attachments = Attachment.find_by_sql("select * from attachments where copy_from = #{attachment.copy_from} or id = #{attachment.copy_from}")
@@ -450,6 +563,15 @@ class AttachmentsController < ApplicationController
end
end
+ #找到文件的所有的历史版本
+ def attachment_versions
+ @attachment = Attachment.find(params[:id])
+ @attachment_histories = @attachment.attachment_histories
+ respond_to do |format|
+ format.js
+ end
+ end
+
private
def find_project
@attachment = Attachment.find(params[:id])
@@ -517,7 +639,7 @@ private
def has_login
unless @attachment && @attachment.container_type == "PhoneAppVersion"
- render_403 unless User.current.logged?
+ render_403 if !User.current.logged? && !(@attachment.container_type == 'OrgSubfield' && @attachment.container.organization.allow_guest_download) && !(@attachment.container_type == 'OrgDocumentComment' && @attachment.container.organization.allow_guest_download)
end
end
end
diff --git a/app/controllers/avatar_controller.rb b/app/controllers/avatar_controller.rb
index a7c0e7b95..b5c174b0c 100644
--- a/app/controllers/avatar_controller.rb
+++ b/app/controllers/avatar_controller.rb
@@ -10,7 +10,7 @@ class AvatarController < ApplicationController
unless request.content_type == 'application/octet-stream'
@source_type = params[:source_type]
@source_id = params[:source_id]
- @temp_file = params[:avatar][:image]
+ @temp_file = params[:avatar][:image]
@image_file = @temp_file.original_filename
else
unless request.raw_post.nil?
@@ -23,7 +23,7 @@ class AvatarController < ApplicationController
#image_file.force_encoding("UTF-8") if filename.respond_to?(:force_encoding)
else
@image_file=params[:filename]
- end
+ end
@temp_file = StringIO.new(@temp_file)
end
end
diff --git a/app/controllers/blog_comments_controller.rb b/app/controllers/blog_comments_controller.rb
index b92223edc..f5e3314bb 100644
--- a/app/controllers/blog_comments_controller.rb
+++ b/app/controllers/blog_comments_controller.rb
@@ -52,7 +52,15 @@ class BlogCommentsController < ApplicationController
render_attachment_warning_if_needed(@article)
else
end
- redirect_to user_blog_blog_comment_path(:user_id=>params[:user_id],:blog_id=>params[:blog_id],:id=>params[:id])
+ if params[:in_act]
+ redirect_to user_path(params[:user_id])
+ else
+ if @article.id.eql?(User.find(params[:user_id]).blog.homepage_id)
+ redirect_to user_path(params[:user_id])
+ else
+ redirect_to user_blog_blog_comment_path(:user_id=>params[:user_id],:blog_id=>params[:blog_id],:id=>params[:id])
+ end
+ end
end
def destroy
@article = BlogComment.find(params[:id])
@@ -118,6 +126,8 @@ class BlogCommentsController < ApplicationController
@blogComment.content = @quote + @blogComment.content
@blogComment.title = "RE: #{@article.title}" unless params[:blog_comment][:title]
@article.children << @blogComment
+ @article.save
+ # @article.update_attribute(:updated_on, @blogComment.updated_on)
@user_activity_id = params[:user_activity_id]
user_activity = UserActivity.where("act_type='BlogComment' and act_id =#{@article.id}").first
if user_activity
diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb
index 07bf60464..842433ec6 100644
--- a/app/controllers/blogs_controller.rb
+++ b/app/controllers/blogs_controller.rb
@@ -1,8 +1,7 @@
class BlogsController < ApplicationController
- before_filter :find_blog,:except => [:index,:create,:new]
+ before_filter :find_blog,:except => [:index,:create,:new,:set_homepage, :cancel_homepage]
before_filter :find_user
def index
- @articls = @user.blog.articles
@article = BlogComment.new
respond_to do |format|
format.html {render :layout=>'new_base_user'}
@@ -26,6 +25,18 @@ class BlogsController < ApplicationController
def edit
end
+
+ def set_homepage
+ @blog = Blog.find(params[:id])
+ @blog.update_attribute(:homepage_id, params[:article_id])
+ redirect_to user_blogs_path(params[:user_id])
+ end
+
+ def cancel_homepage
+ @blog = Blog.find(params[:id])
+ @blog.update_attribute(:homepage_id, nil)
+ redirect_to user_blogs_path(params[:user_id])
+ end
private
def find_blog
if params[:blog_id]
diff --git a/app/controllers/boards_controller.rb b/app/controllers/boards_controller.rb
index 2ec3cac0d..c8518e8a3 100644
--- a/app/controllers/boards_controller.rb
+++ b/app/controllers/boards_controller.rb
@@ -18,8 +18,8 @@
class BoardsController < ApplicationController
layout 'base_projects'#by young
default_search_scope :messages
- before_filter :find_project_by_project_id, :find_board_if_available
- before_filter :authorize, :except => [:new, :show, :create, :index]
+ before_filter :find_project_by_project_id, :find_board_if_available, :except => [:join_to_org_subfields]
+ before_filter :authorize, :except => [:new, :show, :create, :index, :join_to_org_subfields]
accept_rss_auth :index, :show
@@ -79,6 +79,12 @@ class BoardsController < ApplicationController
end
end
end
+ # 更新@消息为已读
+ @project.boards.each do |board|
+ board.messages.each do |m|
+ User.current.at_messages.unviewed('Message', m.id).each {|x| x.viewed!}
+ end
+ end
elsif @course
query_course_messages = @board.messages
query_course_messages.each do |query_course_message|
@@ -99,43 +105,19 @@ class BoardsController < ApplicationController
@is_new = params[:is_new]
@topic_count = @board ? @board.topics.count : 0
if @project
- @topic_pages = Paginator.new @topic_count, per_page_option, params['page']
- #现在发布帖子的时候置顶功能已经没有了。所以取消这个置顶排序 #{Message.table_name}.sticky DESC,
- @topics = @board.topics.
- reorder("#{Message.table_name}.created_on desc").
- includes(:last_reply).
- limit(@topic_pages.per_page).
- offset(@topic_pages.offset).
-
- preload(:author, {:last_reply => :author}).
- all
+ if @board
+ limit = 10;
+ @topic_count = @board.topics.count();
+ @topic_pages = (params[:page] ? params[:page].to_i + 1 : 0) *10
+ @topics = @board.topics.reorder("#{Message.table_name}.sticky DESC, COALESCE(last_replies_messages.created_on, #{Message.table_name}.created_on) desc").
+ limit(limit).offset(@topic_pages).includes(:last_reply).
+ preload(:author, {:last_reply => :author}).all();
+ else
+ @topics = [];
+ end
elsif @course
- #
- # board_topics = @board ? @board.topics.reorder("#{Message.table_name}.sticky DESC, #{Message.table_name}.created_on desc").
- # includes(:last_reply).
- # # limit(@topic_pages.per_page).
- # # offset(@topic_pages.offset).
- #
- # preload(:author, {:last_reply => :author}).
- # all : []
- # @topics = paginateHelper board_topics,10
if (@board)
limit = 10;
- #pageno = params[:page];
- #if(pageno == nil || pageno=='')
- # dw_topic = nil;
- # if( params[:parent_id]!=nil && params[:parent_id]!='' )
- # dw_topic = @board.topics.where(id:params[:parent_id]).first();
- # end
- # if( dw_topic != nil )
- # dw_count = @board.topics.where('(sticky>?) or (sticky=? and created_on>?)',dw_topic.sticky,dw_topic.sticky,dw_topic.created_on).count();
- # dw_count = dw_count+1;
- # pageno = dw_count%10==0 ? (dw_count/limit) : (dw_count/limit+1)
- # end
- #end
- #if(pageno == nil || pageno=='')
- # pageno=1;
- #end
@topic_count = @board.topics.count();
@topic_pages = (params[:page] ? params[:page].to_i + 1 : 0) *10
@topics = @board.topics.reorder("#{Message.table_name}.sticky DESC, COALESCE(last_replies_messages.created_on, #{Message.table_name}.created_on) desc").
@@ -146,6 +128,7 @@ class BoardsController < ApplicationController
end
end
+ @page = params[:page] ? params[:page].to_i + 1 : 0
@message = Message.new(:board => @board)
#modify by nwb
respond_to do |format|
@@ -217,6 +200,16 @@ class BoardsController < ApplicationController
redirect_to_settings_in_projects
end
+ def join_to_org_subfields
+ if params[:id]
+ @board = Board.find(params[:id])
+ @org_subfield_ids = params[:org_subfield_ids]
+ @org_subfield_ids.each do |id|
+ OrgSubfieldBoard.create(:org_subfield_id => id.to_i, :board_id => params[:id].to_i)
+ end
+ end
+ end
+
private
def redirect_to_settings_in_projects
redirect_to settings_project_url(@project, :tab => 'boards')
diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb
index 8a64918ae..5966b310f 100644
--- a/app/controllers/comments_controller.rb
+++ b/app/controllers/comments_controller.rb
@@ -25,9 +25,16 @@ class CommentsController < ApplicationController
def create
raise Unauthorized unless @news.commentable?
-
+ if !@news.org_subfield_id.nil?
+ @org_subfield = OrgSubfield.find(@news.org_subfield_id)
+ end
@comment = Comment.new
- @project ? @comment.comments = params[:comment][:comments] : @comment.comments = params[:comment]
+ #@project ? @comment.comments = params[:comment][:comments] : @comment.comments = params[:comment]
+ if params[:user_activity_id]
+ @comment.comments = params[:comment]
+ else
+ @comment.comments = params[:comment][:comments]
+ end
@comment.author = User.current
if @news.comments << @comment
if params[:asset_id]
@@ -48,17 +55,10 @@ class CommentsController < ApplicationController
# end
# end
# # ������ض�̬�ļ�¼add end
- flash[:notice] = l(:label_comment_added)
- course_activity = CourseActivity.where("course_act_type='News' and course_act_id =#{@news.id}").first
- if course_activity
- course_activity.updated_at = Time.now
- course_activity.save
- end
- user_activity = UserActivity.where("act_type='News' and act_id =#{@news.id}").first
- if user_activity
- user_activity.updated_at = Time.now
- user_activity.save
- end
+ #flash[:notice] = l(:label_comment_added)
+ update_course_activity(@news.class,@news.id)
+ update_user_activity(@news.class,@news.id)
+ update_org_activity(@news.class,@news.id)
end
if params[:user_activity_id]
diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb
index cd1de16fc..36060d41c 100644
--- a/app/controllers/courses_controller.rb
+++ b/app/controllers/courses_controller.rb
@@ -1,3 +1,4 @@
+#encoding: utf-8
class CoursesController < ApplicationController
# layout 'base_courses'
include CoursesHelper
@@ -25,6 +26,7 @@ class CoursesController < ApplicationController
before_filter :authorize_course, :only => [:show, :settings, :update, :course]
before_filter :authorize_course_global, :only => [:new,:create]
before_filter :toggleCourse, :only => [:finishcourse, :restartcourse]
+ before_filter :is_deleted, :only => [:show, :settings]
before_filter :require_login, :only => [:join, :unjoin]
#before_filter :allow_join, :only => [:join]
@@ -35,17 +37,20 @@ class CoursesController < ApplicationController
if !params[:name].nil?
condition = "%#{params[:name].strip}%".gsub(" ","")
end
+ limit = 15
course_org_ids = OrgCourse.find_by_sql("select distinct organization_id from org_courses where course_id = #{params[:id]}").map(&:organization_id)
- if course_org_ids.empty?
- @orgs_not_in_course = Organization.where("(is_public or creator_id =?) and name like ?",User.current.id, condition).page((params[:page].to_i || 1)).per(10)
- @org_count = Organization.where("is_public = 1 or creator_id =?", User.current.id).where("name like ?", condition).count
- else
- course_org_ids = "(" + course_org_ids.join(',') + ")"
- @orgs_not_in_course = Organization.where("id not in #{course_org_ids} and (is_public = 1 or creator_id =?) and name like ?", User.current.id, condition).page((params[:page].to_i || 1)).per(10)
- @org_count = Organization.where("id not in #{course_org_ids} and (is_public = 1 or creator_id =?)", User.current.id).where("name like ?", condition).count
- end
- # @course_count = Project.course_entities.visible.like(params[:name]).page(params[:page]).count
- @orgs_page = Paginator.new @org_count, 10,params[:page]
+ #@orgs_not_in_course = User.current.organizations.where("organizations.id not in (#{course_org_ids.join(',')}) and organizations.name like ?", condition).page(params[:page].to_i || 1).per(limit)
+ #@org_count = User.current.organizations.where("organizations.id not in (#{course_org_ids.join(',')}) and organizations.name like ?", condition).count
+ if course_org_ids.empty?
+ @orgs_not_in_course = User.current.organizations.where("name like ?",condition).page((params[:page].to_i || 1)).per(limit)
+ @org_count = @orgs_not_in_course.count
+ else
+ course_org_ids = "(" + course_org_ids.join(',') + ")"
+ @orgs_not_in_course = User.current.organizations.where("organizations.id not in #{course_org_ids} and organizations.name like ?", condition).page((params[:page].to_i || 1)).per(limit)
+ @org_count = @orgs_not_in_course.empty? ? 0 : @orgs_not_in_course.count
+ end
+ @course_count = Project.course_entities.visible.like(params[:name]).page(params[:page]).count
+ @orgs_page = Paginator.new @org_count, limit,params[:page]
@hint_flag = params[:hint_flag]
#render :json => {:orgs => @orgs_not_in_course, :count => @org_count}.to_json
respond_to do |format|
@@ -55,11 +60,11 @@ class CoursesController < ApplicationController
def join
if User.current.logged?
- cs = CoursesService.new
- @user = User.current
- join = cs.join_course params,@user
- @state = join[:state]
- @course = join[:course]
+ cs = CoursesService.new
+ @user = User.current
+ join = cs.join_course params,@user
+ @state = join[:state]
+ @course = join[:course]
# else
# @course = Course.find_by_id params[:object_id]
# CourseMessage.create(:user_id => @course.tea_id, :course_id => @course.id, :viewed => false,:content=> params[:role],:course_message_id=>User.current.id,:course_message_type=>'JoinCourseRequest')
@@ -70,7 +75,7 @@ class CoursesController < ApplicationController
end
@object_id = params[:object_id]
respond_to do |format|
- format.js #{ render :partial => 'set_join', :locals => {:user => @user, :course => @course, :object_id => params[:object_id]} }
+ format.js #{ render :partial => 'set_join', :locals => {:user => @user, :course => @course, :object_id => params[:object_id]} }
end
end
@@ -85,7 +90,7 @@ class CoursesController < ApplicationController
end
end
-
+
def join_private_courses
respond_to do |format|
format.js
@@ -98,14 +103,14 @@ class CoursesController < ApplicationController
c = cs.edit_course params,@course,User.current
@course = c[:course]
if @course.errors.full_messages.count <= 0
- respond_to do |format|
- format.html {
- # render :layout => 'base_courses'
- flash[:notice] = l(:notice_successful_update)
- redirect_to settings_course_url(@course)
- }
- format.api { render_api_ok }
- end
+ respond_to do |format|
+ format.html {
+ # render :layout => 'base_courses'
+ flash[:notice] = l(:notice_successful_update)
+ redirect_to settings_course_url(@course)
+ }
+ format.api { render_api_ok }
+ end
else
respond_to do |format|
format.html {
@@ -134,8 +139,8 @@ class CoursesController < ApplicationController
courses = Course.visible.where("LOWER(name) like '%#{params[:name].to_s.downcase}%'").order("time desc, created_at desc")
@courses = paginateHelper courses,10
end
- @name = params[:name]
- @type = 'courses'
+ @name = params[:name]
+ @type = 'courses'
respond_to do |format|
format.html {
render :layout => 'course_base'
@@ -277,7 +282,7 @@ class CoursesController < ApplicationController
def member
## 有角色参数的才是课程,没有的就是项目
if (User.current.admin? || @course.is_public == 1 || (@course.is_public == 0 && User.current.member_of_course?(@course)))
-
+
@render_file = 'new_member_list'
@score_sort_by = "desc"
@canShowCode = User.current.allowed_to?(:as_teacher,@course) && params[:role] != '1'
@@ -291,10 +296,15 @@ class CoursesController < ApplicationController
@all_members = searchTeacherAndAssistant(@course)
@members = @all_members
when '2'
- @subPage_title = l :label_student_list
- page = params[:page].nil? ? 0 : (params['page'].to_i - 1)
- @all_members = student_homework_score(0,page, 10,"desc")
- @members = @all_members
+ if @course.open_student == 1 || User.current.member_of_course?(@course)
+ @subPage_title = l :label_student_list
+ page = params[:page].nil? ? 0 : (params['page'].to_i - 1)
+ @all_members = student_homework_score(0,page, 10,"desc")
+ @members = @all_members
+ else
+ render_403
+ return
+ end
end
respond_to do |format|
if params[:page]
@@ -311,11 +321,12 @@ class CoursesController < ApplicationController
def export_course_member_excel
@all_members = student_homework_score(0,0,0,"desc")
+ @homeworks = @course.homework_commons.order("created_at desc")
filename="#{@course.teacher.lastname.to_s + @course.teacher.firstname.to_s }_#{@course.name}_#{@course.time.to_s + @course.term}#{l(:excel_member_list)}";
-
+
respond_to do |format|
format.xls {
- send_data(member_to_xls(@all_members,@course.course_groups), :type => "text/excel;charset=utf-8; header=present",
+ send_data(member_to_xls(@homeworks,@course,@all_members,@course.course_groups), :type => "text/excel;charset=utf-8; header=present",
:filename => filename_for_content_disposition("#{filename}.xls"))
}
end
@@ -374,6 +385,7 @@ class CoursesController < ApplicationController
def settings
if User.current.allowed_to?(:as_teacher,@course)
+ @select_tab = params[:tab]
@issue_custom_fields = IssueCustomField.sorted.all
@issue_category ||= IssueCategory.new
@member ||= @course.members.new
@@ -391,6 +403,25 @@ class CoursesController < ApplicationController
end
+ def private_or_public
+ if @course.is_public == 0
+ @course.update_attributes(:is_public => 1)
+ else
+ @course.update_attributes(:is_public => 0)
+ end
+ if @course.is_public == 0
+ course_status = CourseStatus.find_by_course_id(@course.id)
+ course_status.destroy if course_status
+ elsif @course.is_public == 1
+ course_status = CourseStatus.find_by_course_id(@course.id)
+ course_status.destroy if course_status
+ course_status = CourseStatus.create(:course_id => @course.id, :grade => 0)
+ end
+ respond_to do |format|
+ format.js
+ end
+ end
+
def search_member
if User.current.allowed_to?(:as_teacher,@course) || User.current.admin
q = "#{params[:name].strip}"
@@ -409,10 +440,84 @@ class CoursesController < ApplicationController
def create
cs = CoursesService.new
@course = cs.create_course(params,User.current)[:course]
+ if params[:copy_course]
+ copy_course = Course.find params[:copy_course].to_i
+ @course.is_copy = params[:copy_course].to_i
+ @course.open_student = copy_course.open_student
+ @course.publish_resource = copy_course.publish_resource
+ @course.save
+
+ #copy avatar
+ copy_avatar(@course, copy_course)
+
+ if params[:checkAll]
+ attachments = copy_course.attachments
+ attachments.each do |attachment|
+ 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 = Time.now
+ attach_copied_obj.author_id = User.current.id
+ attach_copied_obj.copy_from = attachment.copy_from.nil? ? attachment.id : attachment.copy_from
+ if attach_copied_obj.attachtype == nil
+ attach_copied_obj.attachtype = 4
+ end
+ attach_copied_obj.save
+ update_quotes attach_copied_obj
+ end
+ elsif params[:course_attachment_type]
+ copy_attachments = []
+ params[:course_attachment_type].each do |type|
+ case type
+ when "1"
+ tag_name = l(:label_courseware)
+ when "2"
+ tag_name = l(:label_software)
+ when "3"
+ tag_name = l(:label_media)
+ when "4"
+ tag_name = l(:label_code)
+ when "6"
+ tag_name = "论文"
+ else
+ tag_name = ""
+ end
+ if tag_name == ""
+ tag_attachments = copy_course.attachments.select{|attachment|
+ !attachment.tag_list.include?('课件') &&
+ !attachment.tag_list.include?('软件') &&
+ !attachment.tag_list.include?('媒体') &&
+ !attachment.tag_list.include?('代码') &&
+ !attachment.tag_list.include?('论文') }
+ else
+ tag_attachments = copy_course.attachments.select{|attachment| attachment.tag_list.include?(tag_name)}
+ end
+ tag_attachments.each do |attach|
+ next if copy_attachments.include?(attach)
+ copy_attachments << attach
+ end
+ end
+ unless copy_attachments.blank?
+ copy_attachments.each do |c_attach|
+ attach_copied_obj = c_attach.copy
+ attach_copied_obj.tag_list.add(c_attach.tag_list) # tag关联
+ attach_copied_obj.container = @course
+ attach_copied_obj.created_on = Time.now
+ attach_copied_obj.author_id = User.current.id
+ attach_copied_obj.copy_from = c_attach.copy_from.nil? ? c_attach.id : c_attach.copy_from
+ if attach_copied_obj.attachtype == nil
+ attach_copied_obj.attachtype = 4
+ end
+ attach_copied_obj.save
+ update_quotes attach_copied_obj
+ end
+ end
+ end
+ end
if @course
respond_to do |format|
flash[:notice] = l(:notice_successful_create)
- format.html {redirect_to settings_course_url(@course, :course_type => 1)}
+ format.html {redirect_to course_url(@course)}
format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'courses', :action => 'show', :id => @course.id) }
end
else
@@ -426,7 +531,7 @@ class CoursesController < ApplicationController
end
def course
- @school_id = params[:school_id]
+ @school_id = params[:school_id]
per_page_option = 10
if @school_id == "0" or @school_id.nil?
@courses_all = Course.active.visible.
@@ -514,8 +619,8 @@ class CoursesController < ApplicationController
def index
if !User.current.admin?
- render_404
- return
+ render_404
+ return
end
@course_type = params[:course_type]
@school_id = params[:school_id]
@@ -564,7 +669,7 @@ class CoursesController < ApplicationController
respond_to do |format|
format.html {
- render :layout => 'base'
+ render :layout => 'base'
}
format.atom {
courses = Course.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all
@@ -580,6 +685,13 @@ class CoursesController < ApplicationController
end
end
+ def is_deleted
+ if @course.is_delete == 1 and !User.current.admin?
+ render_404
+ return
+ end
+ end
+
def get_courses
@user = User.current
membership = @user.coursememberships.all
@@ -633,6 +745,11 @@ class CoursesController < ApplicationController
end
def show
+ # 被删除的课程只有超级管理员才能看到,is_delete为1的时候,标记课程被删除
+ # if @course.is_delete == 1 && !User.current.admin?
+ # render_403
+ # return
+ # end
#更新创建课程消息状态
create_course_messages = @course.course_messages.where("user_id =? and course_message_type =? and course_id =? and viewed =?", User.current.id, 'Course', @course.id, 0)
create_course_messages.update_all(:viewed => true)
@@ -683,10 +800,10 @@ class CoursesController < ApplicationController
end
def feedback
- @course.journals_for_messages.each do |messages|
- query = messages.course_messages.where("user_id = ?", User.current.id)
- query.update_all(:viewed => true);
- end
+ @course.journals_for_messages.each do |messages|
+ query = messages.course_messages.where("user_id = ?", User.current.id)
+ query.update_all(:viewed => true);
+ end
if (User.current.admin? || @course.is_public == 1 || (@course.is_public == 0 && User.current.member_of_course?(@course)))
page = params[:page]
@@ -712,6 +829,11 @@ class CoursesController < ApplicationController
#param id:已有课程ID
def copy_course
if @course
+ @new_course = Course.new
+ respond_to do |format|
+ format.js
+ end
+=begin
@new_course = Course.new @course.attributes
@new_course.tea_id = User.current.id
@new_course.created_at = DateTime.now
@@ -730,6 +852,7 @@ class CoursesController < ApplicationController
@new_course.course_infos << course
redirect_to settings_course_url @new_course
end
+=end
else
render_404
end
@@ -737,20 +860,20 @@ class CoursesController < ApplicationController
#从课程创建的老师那里选择课程大纲
def course_outline
- @teacher = User.find(@course.tea_id)
- @blog_articles = @teacher.blog.articles
- @is_in_show_outline_page = params[:is_in_show_outline_page]
- respond_to do |format|
- format.js
- end
+ @teacher = User.find(@course.tea_id)
+ @blog_articles = @teacher.blog.articles
+ @is_in_show_outline_page = params[:is_in_show_outline_page]
+ respond_to do |format|
+ format.js
+ end
end
#根据关键字搜索,查找方法一样的,但返回内容不一样
def search_course_outline
- @article_title = params[:title]
- @teacher = User.find(@course.tea_id)
- @blog_articles = @teacher.blog.articles.like(@article_title)
- render :json=>@blog_articles.to_json
+ @article_title = params[:title]
+ @teacher = User.find(@course.tea_id)
+ @blog_articles = @teacher.blog.articles.like(@article_title)
+ render :json=>@blog_articles.to_json
end
#设置或者更改课程的大纲
@@ -770,13 +893,64 @@ class CoursesController < ApplicationController
format.html {render :layout => 'base_courses'}
end
end
+
#删除课程
- #删除课程只是将课程的is_delete状态改为false,is_delete为false状态的课程只有管理员可以看到
+ #删除课程只是将课程的is_deleted状态改为false,is_deleted为false状态的课程只有管理员可以看到
def destroy
+ @course.update_attributes(:is_delete => true)
+ @course = nil
+ redirect_to user_url(User.current)
+ end
+ # 恢复已删除的课程
+ def renew
+ if User.current.admin?
+ @course.update_attributes(:is_delete => false)
+ redirect_to course_path(@course)
+ else
+ return 404
+ end
+ end
+ #搜索作业
+ def homework_search
+ @search = "%#{params[:search].strip.downcase}%"
+ @is_teacher = User.current.logged? && (User.current.admin? || User.current.allowed_to?(:as_teacher,@course))
+ @page = params[:page] ? params[:page].to_i + 1 : 0
+ if @is_teacher
+ @homeworks = @course.homework_commons.where("name like '%#{@search}%'").order("created_at desc").limit(10).offset(@page * 10)
+ else
+ @homeworks = @course.homework_commons.where("name like '%#{@search}%' and publish_time <= '#{Date.today}'").order("created_at desc").limit(10).offset(@page * 10)
+ end
end
private
+ def update_quotes attachment
+ if attachment.copy_from
+ attachments = Attachment.find_by_sql("select * from attachments where copy_from = #{attachment.copy_from} or id = #{attachment.copy_from}")
+ else
+ attachments = Attachment.find_by_sql("select * from attachments where copy_from = #{attachment.id} or id = #{attachment.copy_from}")
+ end
+ attachment.quotes = get_qute_number attachment
+ attachment.save
+ attachments.each do |att|
+ att.quotes = attachment.quotes
+ att.save
+ end
+ end
+
+ def get_qute_number attachment
+ if attachment.copy_from
+ result = Attachment.find_by_sql("select count(*) as number from attachments where copy_from = #{attachment.copy_from}")
+ else
+ result = Attachment.find_by_sql("select count(*) as number from attachments where copy_from = #{attachment.id}")
+ end
+ if result.nil? || result.count <= 0
+ return 0
+ else
+ return result[0].number
+ end
+ end
+
def allow_join course
if course_endTime_timeout? course
respond_to do |format|
@@ -796,7 +970,7 @@ class CoursesController < ApplicationController
def can_show_course
@first_page = FirstPage.find_by_page_type('project')
if @first_page.try(:show_course) == 2
- render_404
+ render_404
end
end
@@ -805,7 +979,7 @@ class CoursesController < ApplicationController
sql_select = ""
if groupid == 0
sql_select = "SELECT members.*,(
- SELECT SUM(student_works.final_score)
+ SELECT SUM(IF(student_works.final_score is null,null,student_works.final_score - student_works.absence_penalty - student_works.late_penalty))
FROM student_works,homework_commons
WHERE student_works.homework_common_id = homework_commons.id
AND homework_commons.course_id = #{@course.id}
@@ -817,7 +991,7 @@ class CoursesController < ApplicationController
WHERE members.course_id = #{@course.id} ORDER BY score #{score_sort_by}"
else
sql_select = "SELECT members.*,(
- SELECT SUM(student_works.final_score)
+ SELECT SUM(IF(student_works.final_score is null,null,student_works.final_score - student_works.absence_penalty - student_works.late_penalty))
FROM student_works,homework_commons
WHERE student_works.homework_common_id = homework_commons.id
AND homework_commons.course_id = #{@course.id}
@@ -851,16 +1025,54 @@ class CoursesController < ApplicationController
end
- def member_to_xls members,groups
+ def member_to_xls homeworks, course, members,groups
xls_report = StringIO.new
book = Spreadsheet::Workbook.new
sheet1 = book.create_worksheet :name => "student"
-
blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10
- sheet1.row(0).default_format = blue
+ #sheet1.row(0).default_format = blue
+ #sheet1.row(0).concat([l(:excel_user_id),l(:excel_user_name),l(:excel_nickname),l(:excel_student_id),l(:excel_mail),l(:excel_class),l(:excel_f_score),l(:excel_commit_time)])
+ sheet1[0,0] = "课程编号"
+ sheet1[0,1] = course.id
+ sheet1[1,0] = "课程学期"
+ sheet1[1,1] = course.time.to_s+"年"+course.term
+ sheet1[2,0] = "课程名称"
+ sheet1[2,1] = course.name
+ sheet1[3,0] = "教师团队"
+ sheet1[3,1] = (searchTeacherAndAssistant course).map{|member| member.user.show_name}.join('、')
+ sheet1[4,0] = "主讲教师"
+ sheet1[4,1] = course.teacher.show_name
+ sheet1[5,0] = "排名"
+ sheet1[5,1] = "学生姓名"
+ sheet1[5,2] = "昵称"
+ sheet1[5,3] = "学号"
+ for i in 0 ... homeworks.count
+ sheet1[5,i+4] = "第"+(i+1).to_s+"次"
+ end
+ sheet1[5,homeworks.count+4] = "总成绩"
+ sheet1[5,0] = "排名"
+ sheet1[5,0] = "排名"
+ count_row = 6
+ members.each_with_index do |member, i|
+ sheet1[count_row,0]= i+1
+ sheet1[count_row,1] = member.user.lastname.to_s + member.user.firstname.to_s
+ sheet1[count_row,2] = member.user.login
+ sheet1[count_row,3] = member.user.user_extensions.student_id
+ homeworks.each_with_index do |homework, j|
+ student_works = homework.student_works.where("user_id = #{member.user.id}")
+ if student_works.empty?
+ sheet1[count_row,j+4] = format("%0.2f",0)
+ else
+ final_score = student_works.first.final_score.nil? ? 0 : student_works.first.final_score
+ score = final_score - student_works.first.absence_penalty - student_works.first.late_penalty
+ sheet1[count_row,j+4] = format("%0.2f",score <0 ? 0:score)
+ end
+ end
+ sheet1[count_row,homeworks.count+4] = format("%0.2f",member.score.nil? ? 0:member.score.to_s)
+ count_row += 1
+ end
- sheet1.row(0).concat([l(:excel_user_id),l(:excel_user_name),l(:excel_nickname),l(:excel_student_id),l(:excel_mail),l(:excel_homework_score)])
- count_row = 1
+=begin
group0 = CourseGroup.new();
group0.id = 0;
group0.name = l(:excel_member_with_out_class)
@@ -882,6 +1094,7 @@ class CoursesController < ApplicationController
end
end
end
+=end
book.write xls_report
xls_report.string
end
diff --git a/app/controllers/exercise_controller.rb b/app/controllers/exercise_controller.rb
index d53dc067c..b1e5456c5 100644
--- a/app/controllers/exercise_controller.rb
+++ b/app/controllers/exercise_controller.rb
@@ -1,612 +1,709 @@
-class ExerciseController < ApplicationController
- layout "base_courses"
-
- before_filter :find_exercise_and_course, :only => [:create_exercise_question, :edit, :update, :show, :destroy, :commit_exercise, :commit_answer,:publish_exercise,:republish_exercise]
- before_filter :find_course, :only => [:index,:new,:create,:student_exercise_list]
- include ExerciseHelper
-
- include ExerciseHelper
- def index
- remove_invalid_exercise(@course)
- @is_teacher = User.current.allowed_to?(:as_teacher,@course)
- if @is_teacher
- exercises = @course.exercises
- else
- exercises = @course.exercises.where(:exercise_status => 2)
- end
- @exercises = paginateHelper exercises,20 #分页
- respond_to do |format|
- format.html
- end
- end
-
- def show
- unless User.current.member_of_course?(@course)
- render_403
- return
- end
- @exercise = Exercise.find params[:id]
- @is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin?
- if @exercise.exercise_status != 2 && (!User.current.allowed_to?(:as_teacher,@course) || User.current.admin?)
- render_403
- return
- end
- @can_edit_excercise = (!has_commit_exercise?(@exercise.id,User.current.id)) || User.current.admin?
- @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first
- # 学生点击的时候即创建关联,自动保存
- #eu = ExerciseUser.create(:user_id => User.current, :exercise_id => @exercise.id, :start_at => Time.now, :status => false)
-
- # 已提交问卷的用户不能再访问该界面
- if has_commit_exercise?(@exercise.id, User.current.id) && (!User.current.admin?)
- respond_to do |format|
- format.html {render :layout => 'base_courses'}
- end
- else
- if !@is_teacher && !has_click_exercise?(@exercise.id, User.current.id)
- eu = ExerciseUser.create(:user_id => User.current.id, :exercise_id => @exercise.id, :start_at => Time.now, :status => false)
- @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first
- end
- # @percent = get_percent(@exercise,User.current)
- exercise_questions = @exercise.exercise_questions
- @exercise_questions = paginateHelper exercise_questions,5 #分页
- respond_to do |format|
- format.html {render :layout => 'base_courses'}
- end
- end
- end
-
- def new
- option = {
- :exercise_name => "",
- :course_id => @course.id,
- :exercise_status => 1,
- :user_id => User.current.id,
- :time => "",
- :end_time => "",
- :publish_time => "",
- :exercise_description => "",
- :show_result => "",
- :show_result => 1
- }
- @exercise = Exercise.create option
- if @exercise
- redirect_to edit_exercise_url @exercise.id
- end
- end
-
- def create
- if params[:exercise]
- exercise = Exercise.find(params[:exercise_id]) if params[:exercise_id]
- exercise ||= Exercise.new
- exercise.exercise_name = params[:exercise][:exercise_name]
- exercise.exercise_description = params[:exercise][:exercise_description]
- exercise.end_time = params[:exercise][:end_time]
- exercise.publish_time = params[:exercise][:publish_time]
- exercise.user_id = User.current.id
- exercise.time = params[:exercise][:time]
- exercise.course_id = params[:course_id]
- exercise.exercise_status = 1
- if exercise.save
- @exercise = exercise
- respond_to do |format|
- format.js
- end
- end
- end
- end
-
- def edit
- respond_to do |format|
- format.html{render :layout => 'base_courses'}
- end
- end
-
- def update
- @exercise.exercise_name = params[:exercise][:exercise_name]
- @exercise.exercise_description = params[:exercise][:exercise_description]
- @exercise.time = params[:exercise][:time]
- @exercise.end_time = params[:exercise][:end_time]
- @exercise.publish_time = params[:exercise][:publish_time]
- @exercise.show_result = params[:exercise][:show_result]
- if @exercise.save
- respond_to do |format|
- format.js
- end
- else
- render_404
- end
- end
-
- def destroy
- @is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin?
- if @exercise && @exercise.destroy
- if @is_teacher
- exercises = Exercise.where("course_id =?", @course.id)
- else
- exercises = Exercise.where("course_id =? and exercise_status =?", @course.id, 2)
- end
- @exercises = paginateHelper exercises,20 #分页
- respond_to do |format|
- format.js
- end
- end
- end
-
- # 统计结果
- def statistics_result
- @exercise = Exercise.find(params[:id])
- exercise_questions = @exercise.exercise_questions
- @exercise_questions = paginateHelper exercise_questions, 5
- respond_to do |format|
- format.html{render :layout => 'base_courses'}
- end
- end
-
- # 添加题目
- # question_type 1:单选 2:多选 3:填空题
- def create_exercise_question
- question_title = params[:question_title].nil? || params[:question_title].empty? ? l(:label_enter_single_title) : params[:question_title]
- option = {
- :question_title => question_title,
- :question_type => params[:question_type] || 1,
- :question_number => params[:question_type] == "1"? @exercise.exercise_questions.where("question_type = 1").count + 1 :
- (params[:question_type] == "2" ? (@exercise.exercise_questions.where("question_type = 2").count + 1) :
- @exercise.exercise_questions.where("question_type = 3").count + 1),
- :question_score => params[:question_score]
- }
- @exercise_questions = @exercise.exercise_questions.new option
- # params[:question_answer] 题目选项
- if params[:question_answer]
- for i in 1..params[:question_answer].count
- answer = (params[:question_answer].values[i-1].nil? || params[:question_answer].values[i-1].empty?) ? l(:label_new_answer) : params[:question_answer].values[i-1]
- question_option = {
- :choice_position => i,
- :choice_text => answer
- }
- @exercise_questions.exercise_choices.new question_option
- end
- end
- # 如果是插入的话,那么从插入的这个id以后的question_num都将要+1
- if params[:quest_id]
- @is_insert = true
- if @exercise_questions.question_type == 1
- ExerciseQuestion.where("question_number>? and question_type=?",params[:quest_num].to_i, 1).update_all(" question_number = question_number + 1")
- #@exercise.exercise_questions.where("question_number > #{params[:quest_num].to_i} and question_type == 1").update_all(" question_number = question_number + 1")
- elsif @exercise_questions.question_type == 2
- ExerciseQuestion.where("question_number>? and question_type=?",params[:quest_num].to_i, 2).update_all(" question_number = question_number + 1")
- else
- ExerciseQuestion.where("question_number>? and question_type=?",params[:quest_num].to_i, 3).update_all(" question_number = question_number + 1")
- end
- # @exercise_question_num = params[:quest_num].to_i
- @exercise_questions.question_number = params[:quest_num].to_i + 1
- end
- if @exercise_questions.save
- # params[:exercise_choice] 标准答案参数
- # 问答题标准答案有三个,单独处理
- if @exercise_questions.question_type == 3
- for i in 1..params[:exercise_choice].count
- standart_answer = ExerciseStandardAnswer.new
- standart_answer.exercise_question_id = @exercise_questions.id
- standart_answer.answer_text = params[:exercise_choice].values[i-1]
- standart_answer.save
- end
- else
- standart_answer = ExerciseStandardAnswer.new
- standart_answer.exercise_question_id = @exercise_questions.id
- if @exercise_questions.question_type == 1
- standart_answer.exercise_choice_id = sigle_selection_standard_answer(params[:exercise_choice])
- else
- standart_answer.exercise_choice_id = multiselect_standard_answer(params[:exercise_choice])
- end
- standart_answer.save
- end
- respond_to do |format|
- format.js
- end
- end
-
- end
-
- # 修改题目
- # params[:exercise_question] The id of exercise_question
- # params[:question_answer] eg:A、B、C选项
- def update_exercise_question
- @exercise_question = ExerciseQuestion.find params[:exercise_question]
- @exercise_question.question_title = params[:question_title].nil? || params[:question_title].empty? ? l(:label_enter_single_title) : params[:question_title]
- @exercise_question.question_score = params[:question_score]
- # 处理选项:如果选了某个选项,那么则要删除之前的选项
- if params[:question_answer]
- @exercise_question.exercise_choices.each do |answer|
- answer.destroy unless params[:question_answer].keys.include? answer.id.to_s
- end
- for i in 1..params[:question_answer].count
- question = @exercise_question.exercise_choices.find_by_id params[:question_answer].keys[i-1]
- answer = (params[:question_answer].values[i-1].nil? || params[:question_answer].values[i-1].empty?) ? l(:label_new_answer) : params[:question_answer].values[i-1]
- if question
- question.choice_position = i
- question.choice_text = answer
- question.save
- else
- question_option = {
- :choice_position => i,
- :choice_text => answer
- }
- @exercise_question.exercise_choices.new question_option
- end
- end
- end
- # 更新标准答案
- if params[:exercise_choice]
- if @exercise_question.question_type == 3
- # 删除不合理的选项
- @exercise_question.exercise_standard_answers.each do |answer|
- answer.destroy unless params[:exercise_choice].keys.include? answer.id.to_s
- end
- for i in 1..params[:exercise_choice].count
- # 找到对应的标准答案
- question_standart = @exercise_question.exercise_standard_answers.find_by_id params[:exercise_choice].keys[i-1]
- # 标准答案值
- answer_standart = (params[:exercise_choice].values[i-1].nil? || params[:exercise_choice].values[i-1].empty?) ? l(:label_new_answer) : params[:exercise_choice].values[i-1]
- if question_standart
- question_standart.answer_text = answer_standart
- question_standart.save
- else
- standart_answer_option = {
- :answer_text => answer_standart
- }
- @exercise_question.exercise_standard_answers.new standart_answer_option
- end
- end
- else
- answer_standart = @exercise_question.exercise_standard_answers.first
- answer_standart.exercise_choice_id = @exercise_question.question_type == 1 ? sigle_selection_standard_answer(params[:exercise_choice]) : multiselect_standard_answer(params[:exercise_choice])
- answer_standart.save
- end
- @exercise_question.save
- respond_to do |format|
- format.js
- end
- end
- end
-
- # 删除题目
- def delete_exercise_question
- @exercise_question = ExerciseQuestion.find params[:exercise_question]
- @exercise = @exercise_question.exercise
-
- if @exercise_question.question_type == 1
- ExerciseQuestion.where("question_number>? and question_type=?",params[:quest_num].to_i, 1).update_all(" question_number = question_number - 1")
- #@exercise.exercise_questions.where("question_number > #{params[:quest_num].to_i} and question_type == 1").update_all(" question_number = question_number + 1")
- elsif @exercise_question.question_type == 2
- ExerciseQuestion.where("question_number>? and question_type=?",params[:quest_num].to_i, 2).update_all(" question_number = question_number - 1")
- else
- ExerciseQuestion.where("question_number>? and question_type=?",params[:quest_num].to_i, 3).update_all(" question_number = question_number - 1")
- end
- # @exercise_question_num = params[:quest_num].to_i
- # @exercise_questions.question_number = params[:quest_num].to_i - 1
- #
- # exercise_questions = @exercise.exercise_questions.where("question_number > #{@exercise_question.question_number}")
- # exercise_questions.each do |question|
- # question.question_number -= 1
- # question.save
- # end
- if @exercise_question && @exercise_question.destroy
- respond_to do |format|
- format.js
- end
- end
- end
-
- # 发布试卷
- def publish_exercise
- @is_teacher = User.current.allowed_to?(:as_teacher,@course)
- @exercise.exercise_status = 2
- @exercise.publish_time = Time.now
- if @exercise.save
- #redirect_to exercise_index_url(:course_id=> @course.id)
- respond_to do |format|
- format.js
- end
- end
- end
-
- # 重新发布试卷
- # 重新发布的时候会删除所有的答题
- def republish_exercise
- @is_teacher = User.current.allowed_to?(:as_teacher,@course)
- @exercise.exercise_questions.each do |exercise_question|
- exercise_question.exercise_answers.destroy_all
- end
- @exercise.exercise_users.destroy_all
- @exercise.exercise_status = 1
- @exercise.save
- respond_to do |format|
- format.js
- end
- end
-
- def student_exercise_list
- @is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin?
- @exercise = Exercise.find params[:id]
- @all_exercises = @course.exercises.order("created_at desc")
- @exercise_count = @exercise.exercise_users.where('score is not NULL').count
- if @is_teacher || (!@exercise.exercise_users.where(:user_id => User.current.id).empty? && Time.parse(@exercise.end_time.to_s).strftime("%Y-%m-%d-%H-%M-%S") <= Time.now.strftime("%Y-%m-%d-%H-%M-%S"))
- @exercise_users_list = @exercise.exercise_users.where('score is not NULL')
- @show_all = true;
- elsif !@exercise.exercise_users.where(:user_id => User.current.id).empty? && Time.parse(@exercise.end_time.to_s).strftime("%Y-%m-%d-%H-%M-%S") > Time.now.strftime("%Y-%m-%d-%H-%M-%S")
- @exercise_users_list = @exercise.exercise_users.where("user_id = ? and score is not NULL",User.current.id)
- else
- @exercise_users_list = []
- end
- respond_to do |format|
- format.html
- end
- end
-
- # 学生提交答卷,选中答案的过程中提交
- def commit_answer
- eq = ExerciseQuestion.find(params[:exercise_question_id])
- # 已提交过的则不允许答题
- if has_commit_exercise?(@exercise.id,User.current.id) && (!User.current.admin?)
- render :json => {:text => "failure"}
- return
- end
- if eq.question_type == 1
- # 单选题
- ea = ExerciseAnswer.find_by_exercise_question_id_and_user_id(params[:exercise_question_id],User.current.id)
- if ea.nil?
- # 尚未答该题,添加答案
- ea = ExerciseAnswer.new
- ea.user_id = User.current.id
- ea.exercise_question_id = params[:exercise_question_id]
- end
- #修改该题对应答案
- ea.exercise_choice_id = params[:exercise_choice_id]
- if ea.save
- # 保存成功返回成功信息及当前以答题百分比
- @percent = get_percent(@exercise,User.current)
- render :json => {:text => "ok" ,:percent => format("%.2f" ,@percent)}
- else
- #返回失败信息
- render :json => {:text => "failure"}
- end
- elsif eq.question_type == 2
- #多选题
- ea = ExerciseAnswer.find_by_exercise_choice_id_and_user_id(params[:exercise_choice_id],User.current.id)
- if ea.nil?
- #尚未答该题,添加答案
- ea = ExerciseAnswer.new
- ea.user_id = User.current.id
- ea.exercise_question_id = params[:exercise_question_id]
- ea.exercise_choice_id = params[:exercise_choice_id]
- if ea.save
- @percent = get_percent(@exercise,User.current)
- render :json => {:text => "ok",:percent => format("%.2f" ,@percent)}
- else
- render :json => {:text => "failure"}
- end
- else
- #pv不为空,则当前选项之前已被选择,再次点击则是不再选择该项,故删除该答案
- if ea.delete
- @percent = get_percent(@exercise, User.current)
- render :json => {:text => "false" ,:percent => format("%.2f" , @percent)}
- else
- render :json => {:text => "failure"}
- end
- end
- elsif eq.question_type == 3
- #单行文本,多行文本题
- ea = ExerciseAnswer.find_by_exercise_question_id_and_user_id(params[:exercise_question_id], User.current.id)
- if ea.nil?
- # ea为空之前尚未答题,添加答案
- if params[:answer_text].nil? || params[:answer_text].blank?
- #用户提交空答案,视作不作答
- @percent = get_percent(@exercise,User.current)
- render :json => {:text => ea.answer_text,:percent => format("%.2f", @percent)}
- else
- #添加答案
- ea = ExerciseAnswer.new
- ea.user_id = User.current.id
- ea.exercise_question_id = params[:exercise_question_id]
- ea.answer_text = params[:answer_text]
- if ea.save
- @percent = get_percent(@exercise,User.current)
- render :json => {:text => ea.answer_text,:percent => format("%.2f",@percent)}
- else
- render :json => {:text => "failure"}
- end
- end
- else
- # ea不为空说明用户之前已作答
- if params[:answer_text].nil? || params[:answer_text].blank?
- # 用户提交空答案,视为删除答案
- if ea.delete
- @percent = get_percent(@exercise,User.current)
- render :json => {:text => ea.answer_text,:percent => format("%.2f", @percent)}
- else
- render :json => {:text => "failure"}
- end
- else
- #用户修改答案
- ea.answer_text = params[:answer_text]
- if ea.save
- @percent = get_percent(@exercise,User.current)
- render :json => {:text => pv.vote_text,:percent => format("%.2f", @percent)}
- else
- render :json => {:text => "failure"}
- end
- end
- end
-
- else
- render :json => {:text => "failure"}
- end
- end
-
- # 提交问卷
- def commit_exercise
- # 老师不需要提交
- if User.current.allowed_to?(:as_teacher,@course)
- @exercise.update_attributes(:show_result => params[:show_result])
- redirect_to exercise_url(@exercise)
- # REDO: 提示提交成功
- else
- # 更新提交状态
- cur_exercise_user = ExerciseUser.where("user_id =? and exercise_id=?", User.current, @exercise.id).first
- cur_exercise_user.update_attributes(:status => 1)
- # 答题过程中需要统计完成量
- @uncomplete_question = get_uncomplete_question(@exercise, User.current)
- # 获取改学生的考试得分
- @score = calculate_student_score(@exercise, User.current)
- # @score = 100
- if @uncomplete_question.count < 1
- # 查看是否有已提交记录
- eu = get_exercise_user(@exercise.id, User.current.id)
- eu.user_id = User.current.id
- eu.exercise_id = @exercise.id
- eu.score = @score
- if eu.save
- #redirect_to poll_index_path(:polls_group_id => @course.id,:polls_type => 'Course')
- @status = 0 #提交成功
- else
- @status = 2 #未知错误
- end
- else
- @status = 1 #有未做得必答题
- end
- respond_to do |format|
- format.js
- end
- end
- end
-
- # 计算学生得分
- def calculate_student_score(exercise, user)
- score = 0
- score1 = 0
- score2 = 0
- score3 = 0
- exercise_qustions = exercise.exercise_questions
- exercise_qustions.each do |question|
- answer = get_user_answer(question, user)
- standard_answer = get_user_standard_answer(question, user)
- unless answer.nil?
- # 问答题有多个答案
- if question.question_type == 3
- if standard_answer.include?(answer.first.answer_text)
- score1 = score1+ question.question_score unless question.question_score.nil?
- end
- elsif question.question_type == 1
- if answer.first.exercise_choice.choice_position == standard_answer.exercise_choice_id
- score2 = score2 + question.question_score unless question.question_score.nil?
- end
- else
- arr = get_mulscore(question, user)
- if arr.to_i == standard_answer.exercise_choice_id
- score3 = score3 + question.question_score unless question.question_score.nil?
- end
- # ecs = ExerciseAnswer.where("user_id =? and exercise_question_id =?", user.id, question.id)
- # arr = []
- # ecs.each do |ec|
- # arr << ec.exercise_choice.choice_position
- # end
- # arr.sort
- # arr = arr.join("")
- # if arr.to_i == standard_answer.exercise_choice_id
- # score3 = score + question.question_score unless question.question_score.nil?
- # end
- end
- end
- end
- score = score1 + score2 + score3
- end
-
- private
-
- # ExerciseUser记录用户是否已提交问卷有对应的记录则已提交,没有则新建一个
- def get_exercise_user exercise_id,user_id
- eu = ExerciseUser.find_by_exercise_id_and_user_id(exercise_id,user_id)
- if eu.nil?
- eu = ExerciseUser.new
- end
- eu
- end
-
- #获取未完成的题目
- def get_uncomplete_question exercise,user
- all_questions = exercise.exercise_questions
- uncomplete_question = []
- all_questions.each do |question|
- answers = get_user_answer(question, user)
- if answers.nil?
- uncomplete_question << question
- end
- end
- uncomplete_question
- end
-
- # 获取当前学生回答问题的答案
- def get_user_answer(question,user)
- # user_answer = ExerciseAnswer.where("user_id=? and exercise_question_id=?", user.id, question.id).first
- user_answer = question.exercise_answers.where("#{ExerciseAnswer.table_name}.user_id = #{user.id}")
- user_answer
- end
-
- # 获取问题的标准答案
- def get_user_standard_answer(question,user)
- if question.question_type == 3
- standard_answer =[]
- question.exercise_standard_answers.each do |answer|
- standard_answer << answer.answer_text
- end
- else
- standard_answer = question.exercise_standard_answers.first
- end
- standard_answer
- end # 是否完成了答题
- def get_complete_question(exercise,user)
- questions = exercise.exercise_questions
- complete_question = []
- questions.each do |question|
- answers = get_user_answer(question,user)
- if !(answers.nil? || answers.count < 1)
- complete_question << question
- end
- end
- complete_question
- end
-
- # 获取答题百分比
- def get_percent exercise,user
- complete_count = get_complete_question(exercise,user).count
- if exercise.exercise_questions.count == 0
- return 0
- else
- return (complete_count.to_f / exercise.exercise_questions.count.to_f)*100
- end
- end
-
- def remove_invalid_exercise(course)
- exercises = course.exercises.where("exercise_name=?","")
- unless exercises.empty?
- exercises.each do |exercise|
- if exercise.exercise_questions.empty?
- exercise.destroy
- end
- end
- end
- end
-
- def find_exercise_and_course
- @exercise = Exercise.find params[:id]
- @course = Course.find @exercise.course_id
- rescue Exception => e
- render_404
- end
-
- def find_course
- @course = Course.find params[:course_id]
- rescue Exception => e
- render_404
- end
+class ExerciseController < ApplicationController
+ layout "base_courses"
+
+ before_filter :find_exercise_and_course, :only => [:create_exercise_question, :edit, :update, :show, :destroy,
+ :commit_exercise, :commit_answer,:publish_exercise,:republish_exercise,
+ :show_student_result,:student_exercise_list]
+ before_filter :find_course, :only => [:index,:new,:create]
+ include ExerciseHelper
+
+ def index
+ publish_exercises = Exercise.where("publish_time is not null and exercise_status = 1 and publish_time <=?",Time.now)
+ publish_exercises.each do |exercise|
+ exercise.update_column('exercise_status', 2)
+ course = exercise.course
+ course.members.each do |m|
+ exercise.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => false, :status => 2)
+ end
+ end
+
+ if @course.is_public == 0 && !(User.current.member_of_course?(@course)||User.current.admin?)
+ render_403
+ return
+ end
+ remove_invalid_exercise(@course)
+ @is_teacher = User.current.allowed_to?(:as_teacher,@course)
+ if @is_teacher || User.current.admin?
+ exercises = @course.exercises.order("created_at asc")
+ else
+ exercises = @course.exercises.where("exercise_status <> 1").order("created_at asc")
+ end
+ @exercises = paginateHelper exercises,20 #分页
+ respond_to do |format|
+ format.html
+ end
+ end
+
+ def show
+ publish_exercises = Exercise.where("publish_time is not null and exercise_status = 1 and publish_time <=?",Time.now)
+ publish_exercises.each do |exercise|
+ exercise.update_column('exercise_status', 2)
+ course = exercise.course
+ course.members.each do |m|
+ exercise.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => false, :status => 2)
+ end
+ end
+
+ unless User.current.member_of_course?(@course) || User.current.admin?
+ render_403
+ return
+ end
+ @exercise = Exercise.find params[:id]
+ @is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin?
+ exercise_end = @exercise.end_time > Time.now
+ if @exercise.time == -1
+ @can_edit_excercise = exercise_end
+ else
+ @can_edit_excercise = !has_commit_exercise?(@exercise.id,User.current.id)&& exercise_end
+ end
+ unless @is_teacher
+ @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first
+ if @exercise_user.nil?
+ eu = ExerciseUser.create(:user_id => User.current.id, :exercise_id => @exercise.id, :start_at => Time.now, :status => false)
+ @exercise_user = ExerciseUser.where("user_id=? and exercise_id=?", User.current.id, @exercise.id).first
+ end
+ score = calculate_student_score(@exercise, User.current)
+ @exercise_user.update_attributes(:score => score)
+ end
+ # @percent = get_percent(@exercise,User.current)
+ @exercise_questions = @exercise.exercise_questions
+ respond_to do |format|
+ format.html {render :layout => 'base_courses'}
+ end
+ #end
+ end
+
+ def new
+ option = {
+ :exercise_name => "",
+ :course_id => @course.id,
+ :exercise_status => 1,
+ :user_id => User.current.id,
+ :time => "",
+ :end_time => "",
+ :publish_time => "",
+ :exercise_description => "",
+ :show_result => 1
+ }
+ @exercise = Exercise.create option
+ if @exercise
+ redirect_to edit_exercise_url @exercise.id
+ end
+ end
+
+ def create
+ if params[:exercise]
+ exercise = Exercise.find(params[:exercise_id]) if params[:exercise_id]
+ exercise ||= Exercise.new
+ exercise.exercise_name = params[:exercise][:exercise_name]
+ exercise.exercise_description = params[:exercise][:exercise_description]
+ exercise.end_time = Time.at(params[:exercise][:end_time].to_time.to_i + 16*60*60 -1)
+ exercise.publish_time = params[:exercise][:publish_time]
+ exercise.user_id = User.current.id
+ exercise.time = params[:exercise][:time]
+ exercise.course_id = params[:course_id]
+ exercise.exercise_status = 1
+ if exercise.save
+ @exercise = exercise
+ respond_to do |format|
+ format.js
+ end
+ end
+ end
+ end
+
+ def edit
+ respond_to do |format|
+ format.html{render :layout => 'base_courses'}
+ end
+ end
+
+ def update
+ @exercise.exercise_name = params[:exercise][:exercise_name]
+ @exercise.exercise_description = params[:exercise][:exercise_description]
+ @exercise.time = params[:exercise][:time].blank? ? -1 : params[:exercise][:time]
+ @exercise.end_time = Time.at(params[:exercise][:end_time].to_time.to_i + 16*60*60 -1)
+ @exercise.publish_time = params[:exercise][:publish_time]
+ @exercise.show_result = params[:exercise][:show_result].blank? ? 1 : params[:exercise][:show_result]
+ if @exercise.save
+ respond_to do |format|
+ format.js
+ end
+ else
+ render_404
+ end
+ end
+
+ def destroy
+ @is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin?
+ if @exercise && @exercise.destroy
+ if @is_teacher
+ exercises = Exercise.where("course_id =?", @course.id)
+ else
+ exercises = Exercise.where("course_id =? and exercise_status =?", @course.id, 2)
+ end
+ @exercises = paginateHelper exercises,20 #分页
+ respond_to do |format|
+ format.js
+ end
+ end
+ end
+
+ # 统计结果
+ def statistics_result
+ @exercise = Exercise.find(params[:id])
+ exercise_questions = @exercise.exercise_questions
+ @exercise_questions = paginateHelper exercise_questions, 5
+ respond_to do |format|
+ format.html{render :layout => 'base_courses'}
+ end
+ end
+
+ # 添加题目
+ # question_type 1:单选 2:多选 3:填空题
+ def create_exercise_question
+ question_title = params[:question_title].nil? || params[:question_title].empty? ? l(:label_enter_single_title) : params[:question_title]
+ option = {
+ :question_title => question_title,
+ :question_type => params[:question_type] || 1,
+ :question_number => params[:question_type] == "1"? @exercise.exercise_questions.where("question_type = 1").count + 1 :
+ (params[:question_type] == "2" ? (@exercise.exercise_questions.where("question_type = 2").count + 1) :
+ @exercise.exercise_questions.where("question_type = 3").count + 1),
+ :question_score => params[:question_score]
+ }
+ @exercise_questions = @exercise.exercise_questions.new option
+ # params[:question_answer] 题目选项
+ if params[:question_answer]
+ for i in 1..params[:question_answer].count
+ answer = (params[:question_answer].values[i-1].nil? || params[:question_answer].values[i-1].empty?) ? l(:label_new_answer) : params[:question_answer].values[i-1]
+ question_option = {
+ :choice_position => i,
+ :choice_text => answer
+ }
+ @exercise_questions.exercise_choices.new question_option
+ end
+ end
+ # 如果是插入的话,那么从插入的这个id以后的question_num都将要+1
+ if params[:quest_id]
+ @is_insert = true
+ if @exercise_questions.question_type == 1
+ ExerciseQuestion.where("question_number>? and question_type=?",params[:quest_num].to_i, 1).update_all(" question_number = question_number + 1")
+ #@exercise.exercise_questions.where("question_number > #{params[:quest_num].to_i} and question_type == 1").update_all(" question_number = question_number + 1")
+ elsif @exercise_questions.question_type == 2
+ ExerciseQuestion.where("question_number>? and question_type=?",params[:quest_num].to_i, 2).update_all(" question_number = question_number + 1")
+ else
+ ExerciseQuestion.where("question_number>? and question_type=?",params[:quest_num].to_i, 3).update_all(" question_number = question_number + 1")
+ end
+ # @exercise_question_num = params[:quest_num].to_i
+ @exercise_questions.question_number = params[:quest_num].to_i + 1
+ end
+ if @exercise_questions.save
+ # params[:exercise_choice] 标准答案参数
+ # 问答题标准答案有三个,单独处理
+ if @exercise_questions.question_type == 3
+ for i in 1..params[:exercise_choice].count
+ standart_answer = ExerciseStandardAnswer.new
+ standart_answer.exercise_question_id = @exercise_questions.id
+ standart_answer.answer_text = params[:exercise_choice].values[i-1]
+ standart_answer.save
+ end
+ else
+ standart_answer = ExerciseStandardAnswer.new
+ standart_answer.exercise_question_id = @exercise_questions.id
+ if @exercise_questions.question_type == 1
+ standart_answer.exercise_choice_id = sigle_selection_standard_answer(params[:exercise_choice])
+ else
+ standart_answer.exercise_choice_id = multiselect_standard_answer(params[:exercise_choice])
+ end
+ standart_answer.save
+ end
+ respond_to do |format|
+ format.js
+ end
+ end
+
+ end
+
+ # 修改题目
+ # params[:exercise_question] The id of exercise_question
+ # params[:question_answer] eg:A、B、C选项
+ def update_exercise_question
+ @exercise_question = ExerciseQuestion.find params[:exercise_question]
+ @exercise_question.question_title = params[:question_title].nil? || params[:question_title].empty? ? l(:label_enter_single_title) : params[:question_title]
+ @exercise_question.question_score = params[:question_score]
+ # 处理选项:如果选了某个选项,那么则要删除之前的选项
+ if params[:question_answer]
+ @exercise_question.exercise_choices.each do |answer|
+ answer.destroy unless params[:question_answer].keys.include? answer.id.to_s
+ end
+ for i in 1..params[:question_answer].count
+ question = @exercise_question.exercise_choices.find_by_id params[:question_answer].keys[i-1]
+ answer = (params[:question_answer].values[i-1].nil? || params[:question_answer].values[i-1].empty?) ? l(:label_new_answer) : params[:question_answer].values[i-1]
+ if question
+ question.choice_position = i
+ question.choice_text = answer
+ question.save
+ else
+ question_option = {
+ :choice_position => i,
+ :choice_text => answer
+ }
+ @exercise_question.exercise_choices.new question_option
+ end
+ end
+ end
+ # 更新标准答案
+ if params[:exercise_choice]
+ if @exercise_question.question_type == 3
+ # 删除不合理的选项
+ @exercise_question.exercise_standard_answers.each do |answer|
+ answer.destroy unless params[:exercise_choice].keys.include? answer.id.to_s
+ end
+ for i in 1..params[:exercise_choice].count
+ # 找到对应的标准答案
+ question_standart = @exercise_question.exercise_standard_answers.find_by_id params[:exercise_choice].keys[i-1]
+ # 标准答案值
+ answer_standart = (params[:exercise_choice].values[i-1].nil? || params[:exercise_choice].values[i-1].empty?) ? l(:label_new_answer) : params[:exercise_choice].values[i-1]
+ if question_standart
+ question_standart.answer_text = answer_standart
+ question_standart.save
+ else
+ standart_answer_option = {
+ :answer_text => answer_standart
+ }
+ @exercise_question.exercise_standard_answers.new standart_answer_option
+ end
+ end
+ else
+ answer_standart = @exercise_question.exercise_standard_answers.first
+ answer_standart.exercise_choice_id = @exercise_question.question_type == 1 ? sigle_selection_standard_answer(params[:exercise_choice]) : multiselect_standard_answer(params[:exercise_choice])
+ answer_standart.save
+ end
+ @exercise_question.save
+ respond_to do |format|
+ format.js
+ end
+ end
+ end
+
+ # 删除题目
+ def delete_exercise_question
+ @exercise_question = ExerciseQuestion.find params[:exercise_question]
+ @exercise = @exercise_question.exercise
+
+ if @exercise_question.question_type == 1
+ ExerciseQuestion.where("question_number>? and question_type=?",params[:quest_num].to_i, 1).update_all(" question_number = question_number - 1")
+ #@exercise.exercise_questions.where("question_number > #{params[:quest_num].to_i} and question_type == 1").update_all(" question_number = question_number + 1")
+ elsif @exercise_question.question_type == 2
+ ExerciseQuestion.where("question_number>? and question_type=?",params[:quest_num].to_i, 2).update_all(" question_number = question_number - 1")
+ else
+ ExerciseQuestion.where("question_number>? and question_type=?",params[:quest_num].to_i, 3).update_all(" question_number = question_number - 1")
+ end
+ # @exercise_question_num = params[:quest_num].to_i
+ # @exercise_questions.question_number = params[:quest_num].to_i - 1
+ #
+ # exercise_questions = @exercise.exercise_questions.where("question_number > #{@exercise_question.question_number}")
+ # exercise_questions.each do |question|
+ # question.question_number -= 1
+ # question.save
+ # end
+ if @exercise_question && @exercise_question.destroy
+ respond_to do |format|
+ format.js
+ end
+ end
+ end
+
+ # 发布试卷
+ def publish_exercise
+ @is_teacher = User.current.allowed_to?(:as_teacher,@course)
+ @index = params[:index]
+ @exercise.exercise_status = 2
+ @exercise.publish_time = Time.now
+ if @exercise.save
+ @exercise.course.members.each do |m|
+ @exercise.course_messages << CourseMessage.create(:user_id =>m.user_id, :course_id => @exercise.course.id, :viewed => false,:status=>2)
+ end
+ #redirect_to exercise_index_url(:course_id=> @course.id)
+ respond_to do |format|
+ format.js
+ end
+ end
+ end
+
+ # 重新发布试卷
+ # 重新发布的时候会删除所有的答题
+ def republish_exercise
+ @is_teacher = User.current.allowed_to?(:as_teacher,@course)
+ @index = params[:index]
+ @exercise.exercise_questions.each do |exercise_question|
+ exercise_question.exercise_answers.destroy_all
+ end
+ @exercise.course_messages.destroy_all
+ @exercise.exercise_users.destroy_all
+ @exercise.exercise_status = 1
+ @exercise.publish_time = nil
+ @exercise.save
+ respond_to do |format|
+ format.js
+ end
+ end
+
+ def student_exercise_list
+=begin
+ if @exercise.end_time <= Time.now
+ @course.student.each do |student|
+ if ExerciseUser.where("user_id = ? && exercise_id = ?",student.student_id,@exercise.id).empty?
+ ExerciseUser.create(:user_id => student.student_id, :exercise_id => @exercise.id, :start_at => @exercise.end_time, :status => true,:score=>0)
+ end
+
+ s_score = calculate_student_score(@exercise, student.student)
+ exercise_user = ExerciseUser.where("user_id =? and exercise_id=?", student.student_id, @exercise.id).first
+ exercise_user.update_attributes(:score => s_score)
+ end
+ end
+=end
+ @is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin?
+ @all_exercises = @course.exercises.where("exercise_status > 1").order("created_at desc")
+ @exercise_count = @exercise.exercise_users.where('score is not NULL').count
+ if @is_teacher || (!@exercise.exercise_users.where(:user_id => User.current.id).empty? && @exercise.end_time <= Time.now)
+ @exercise_users_list = @exercise.exercise_users.where('score is not NULL')
+ @show_all = true;
+ elsif !@exercise.exercise_users.where(:user_id => User.current.id).empty? && @exercise.end_time > Time.now
+ @exercise_users_list = @exercise.exercise_users.where("user_id = ? and score is not NULL",User.current.id)
+ else
+ @exercise_users_list = []
+ end
+ respond_to do |format|
+ format.html
+ format.xls {
+ filename = "#{@course.teacher.lastname.to_s + @course.teacher.firstname}_#{@course.name}_#{@course.time.to_s + @course.term}_#{@exercise.exercise_name}#{l(:excel_exercise_list)}.xls"
+ send_data(exercise_to_xls(@exercise_users_list), :type => "text/excel;charset=utf-8; header=present",
+ :filename => filename_for_content_disposition(filename))
+ }
+ end
+ end
+
+ # 学生提交答卷,选中答案的过程中提交
+ def commit_answer
+ eq = ExerciseQuestion.find(params[:exercise_question_id])
+ # 已提交过的且是限时的则不允许答题
+ if (has_commit_exercise?(@exercise.id,User.current.id) && (!User.current.admin?) && @exercise.time != -1) || @exercise.end_time < Time.now
+ render :json => {:text => "failure"}
+ return
+ end
+ if eq.question_type == 1
+ # 单选题
+ ea = ExerciseAnswer.find_by_exercise_question_id_and_user_id(params[:exercise_question_id],User.current.id)
+ if ea.nil?
+ # 尚未答该题,添加答案
+ ea = ExerciseAnswer.new
+ ea.user_id = User.current.id
+ ea.exercise_question_id = params[:exercise_question_id]
+ end
+ #修改该题对应答案
+ ea.exercise_choice_id = params[:exercise_choice_id]
+ if ea.save
+ # 保存成功返回成功信息及当前以答题百分比
+ uncomplete_question = get_uncomplete_question(@exercise, User.current)
+ if uncomplete_question.count < 1
+ complete = 1;
+ else
+ complete = 0;
+ end
+ @percent = get_percent(@exercise,User.current)
+ render :json => {:text => "ok" ,:complete => complete,:percent => format("%.2f" ,@percent)}
+ else
+ #返回失败信息
+ render :json => {:text => "failure"}
+ end
+ elsif eq.question_type == 2
+ #多选题
+ ea = ExerciseAnswer.find_by_exercise_choice_id_and_user_id(params[:exercise_choice_id],User.current.id)
+ if ea.nil?
+ #尚未答该题,添加答案
+ ea = ExerciseAnswer.new
+ ea.user_id = User.current.id
+ ea.exercise_question_id = params[:exercise_question_id]
+ ea.exercise_choice_id = params[:exercise_choice_id]
+ if ea.save
+ uncomplete_question = get_uncomplete_question(@exercise, User.current)
+ if uncomplete_question.count < 1
+ complete = 1;
+ else
+ complete = 0;
+ end
+ @percent = get_percent(@exercise,User.current)
+ render :json => {:text => "ok",:complete => complete,:percent => format("%.2f" ,@percent)}
+ else
+ render :json => {:text => "failure"}
+ end
+ else
+ #pv不为空,则当前选项之前已被选择,再次点击则是不再选择该项,故删除该答案
+ if ea.delete
+ @percent = get_percent(@exercise, User.current)
+ render :json => {:text => "false" ,:percent => format("%.2f" , @percent)}
+ else
+ render :json => {:text => "failure"}
+ end
+ end
+ elsif eq.question_type == 3
+ #单行文本,多行文本题
+ ea = ExerciseAnswer.find_by_exercise_question_id_and_user_id(params[:exercise_question_id], User.current.id)
+ if ea.nil?
+ # ea为空之前尚未答题,添加答案
+ if params[:answer_text].nil? || params[:answer_text].blank?
+ #用户提交空答案,视作不作答
+ @percent = get_percent(@exercise,User.current)
+ render :json => {:text => "",:percent => format("%.2f", @percent)}
+ else
+ #添加答案
+ ea = ExerciseAnswer.new
+ ea.user_id = User.current.id
+ ea.exercise_question_id = params[:exercise_question_id]
+ ea.answer_text = params[:answer_text]
+ if ea.save
+ uncomplete_question = get_uncomplete_question(@exercise, User.current)
+ if uncomplete_question.count < 1
+ complete = 1;
+ else
+ complete = 0;
+ end
+ @percent = get_percent(@exercise,User.current)
+ render :json => {:text => ea.answer_text,:complete => complete,:percent => format("%.2f",@percent)}
+ else
+ render :json => {:text => "failure"}
+ end
+ end
+ else
+ # ea不为空说明用户之前已作答
+ if params[:answer_text].nil? || params[:answer_text].blank?
+ # 用户提交空答案,视为删除答案
+ if ea.delete
+ @percent = get_percent(@exercise,User.current)
+ render :json => {:text => "",:percent => format("%.2f", @percent)}
+ else
+ render :json => {:text => "failure"}
+ end
+ else
+ #用户修改答案
+ ea.answer_text = params[:answer_text]
+ if ea.save
+ @percent = get_percent(@exercise,User.current)
+ render :json => {:text => ea.answer_text,:percent => format("%.2f", @percent)}
+ else
+ render :json => {:text => "failure"}
+ end
+ end
+ end
+
+ else
+ render :json => {:text => "failure"}
+ end
+ end
+
+ # 提交问卷
+ def commit_exercise
+ # 老师不需要提交
+ if User.current.allowed_to?(:as_teacher,@course)
+ if @exercise.publish_time.nil?
+ @exercise.update_attributes(:show_result => params[:show_result])
+ @exercise.update_attributes(:exercise_status => 2)
+ @exercise.update_attributes(:publish_time => Time.now)
+ course = @exercise.course
+ course.members.each do |m|
+ @exercise.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => course.id, :viewed => false, :status => 2)
+ end
+ redirect_to exercise_url(@exercise)
+ return
+ elsif @exercise.publish_time > Time.now
+ @exercise.update_attributes(:show_result => params[:show_result])
+ redirect_to exercise_url(@exercise)
+ return
+ end
+ @exercise.update_attributes(:show_result => params[:show_result])
+ redirect_to exercise_url(@exercise)
+ # REDO: 提示提交成功
+ else
+ # 更新提交状态
+ cur_exercise_user = ExerciseUser.where("user_id =? and exercise_id=?", User.current, @exercise.id).first
+ cur_exercise_user.update_attributes(:status => 1)
+ # 答题过程中需要统计完成量
+ @uncomplete_question = get_uncomplete_question(@exercise, User.current)
+ # 获取改学生的考试得分
+ @score = calculate_student_score(@exercise, User.current)
+ # @score = 100
+ if @uncomplete_question.count < 1
+ # 查看是否有已提交记录
+ cur_exercise_user.score = @score
+ if cur_exercise_user.save
+ #redirect_to poll_index_path(:polls_group_id => @course.id,:polls_type => 'Course')
+ @status = 0 #提交成功
+ else
+ @status = 2 #未知错误
+ end
+ else
+ @status = 1 #有未做得必答题
+ end
+ @save = params[:save].to_i if params[:save]
+ respond_to do |format|
+ format.js
+ end
+ end
+ end
+
+ #查看学生的答卷情况
+ def show_student_result
+ @user = User.find params[:user_id]
+ @can_edit_excercise = false
+ @exercise_questions = @exercise.exercise_questions
+ score = calculate_student_score(@exercise, @user)
+ eu = get_exercise_user(@exercise.id, @user.id)
+ eu.update_attributes(:score => score)
+ @exercise_user = ExerciseUser.where("user_id =? and exercise_id=?", @user.id, @exercise.id).first
+ respond_to do |format|
+ format.html {render :layout => 'base_courses'}
+ end
+ end
+
+ # 计算学生得分
+ def calculate_student_score(exercise, user)
+ score = 0
+ score1 = 0
+ score2 = 0
+ score3 = 0
+ exercise_qustions = exercise.exercise_questions
+ exercise_qustions.each do |question|
+ answer = get_user_answer(question, user)
+ standard_answer = get_user_standard_answer(question, user)
+ unless answer.empty?
+ # 问答题有多个答案
+ if question.question_type == 3 && !standard_answer.empty?
+ if standard_answer.include?(answer.first.answer_text)
+ score1 = score1+ question.question_score unless question.question_score.nil?
+ end
+ elsif question.question_type == 1 && !standard_answer.nil?
+ if answer.first.exercise_choice.choice_position == standard_answer.exercise_choice_id
+ score2 = score2 + question.question_score unless question.question_score.nil?
+ end
+ elsif question.question_type == 2 && !standard_answer.nil?
+ arr = get_mulscore(question, user)
+ if arr.to_i == standard_answer.exercise_choice_id
+ score3 = score3 + question.question_score unless question.question_score.nil?
+ end
+ # ecs = ExerciseAnswer.where("user_id =? and exercise_question_id =?", user.id, question.id)
+ # arr = []
+ # ecs.each do |ec|
+ # arr << ec.exercise_choice.choice_position
+ # end
+ # arr.sort
+ # arr = arr.join("")
+ # if arr.to_i == standard_answer.exercise_choice_id
+ # score3 = score + question.question_score unless question.question_score.nil?
+ # end
+ end
+ end
+ end
+ score = score1 + score2 + score3
+ end
+
+ private
+ #测验列表转换为excel
+ def exercise_to_xls items
+ xls_report = StringIO.new
+ book = Spreadsheet::Workbook.new
+ sheet1 = book.create_worksheet :name => "exercise"
+ blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10
+ sheet1.row(0).default_format = blue
+ sheet1.row(0).concat([l(:excel_user_id),l(:excel_user_name),l(:excel_nickname),l(:excel_student_id),l(:excel_mail),l(:excel_class),l(:excel_f_score),l(:excel_commit_time)])
+ count_row = 1
+ items.each do |exercise|
+ sheet1[count_row,0]=exercise.user.id
+ sheet1[count_row,1] = exercise.user.lastname.to_s + exercise.user.firstname.to_s
+ sheet1[count_row,2] = exercise.user.login
+ sheet1[count_row,3] = exercise.user.user_extensions.student_id
+ sheet1[count_row,4] = exercise.user.mail
+ sheet1[count_row,5] = ""
+ sheet1[count_row,6] = exercise.score
+ sheet1[count_row,7] = format_time(exercise.created_at)
+ count_row += 1
+ end
+ book.write xls_report
+ xls_report.string
+ end
+
+ # ExerciseUser记录用户是否已提交问卷有对应的记录则已提交,没有则新建一个
+ def get_exercise_user exercise_id,user_id
+ eu = ExerciseUser.find_by_exercise_id_and_user_id(exercise_id,user_id)
+ if eu.nil?
+ eu = ExerciseUser.new
+ end
+ eu
+ end
+
+ # 获取当前学生回答问题的答案
+ def get_user_answer(question,user)
+ # user_answer = ExerciseAnswer.where("user_id=? and exercise_question_id=?", user.id, question.id).first
+ user_answer = question.exercise_answers.where("#{ExerciseAnswer.table_name}.user_id = #{user.id}")
+ user_answer
+ end
+
+ # 获取问题的标准答案
+ def get_user_standard_answer(question,user)
+ if question.question_type == 3
+ standard_answer =[]
+ question.exercise_standard_answers.each do |answer|
+ standard_answer << answer.answer_text
+ end
+ else
+ standard_answer = question.exercise_standard_answers.first
+ end
+ standard_answer
+ end
+ # 是否完成了答题
+ def get_complete_question(exercise,user)
+ questions = exercise.exercise_questions
+ complete_question = []
+ questions.each do |question|
+ answers = get_user_answer(question,user)
+ if !(answers.nil? || answers.count < 1)
+ complete_question << question
+ end
+ end
+ complete_question
+ end
+
+ # 获取答题百分比
+ def get_percent exercise,user
+ complete_count = get_complete_question(exercise,user).count
+ if exercise.exercise_questions.count == 0
+ return 0
+ else
+ return (complete_count.to_f / exercise.exercise_questions.count.to_f)*100
+ end
+ end
+
+ def remove_invalid_exercise(course)
+ exercises = course.exercises.where("exercise_name=?","")
+ unless exercises.empty?
+ exercises.each do |exercise|
+ if exercise.exercise_questions.empty?
+ exercise.destroy
+ end
+ end
+ end
+ end
+
+ def find_exercise_and_course
+ @exercise = Exercise.find params[:id]
+ @course = Course.find @exercise.course_id
+ rescue Exception => e
+ render_404
+ end
+
+ def find_course
+ @course = Course.find params[:course_id]
+ rescue Exception => e
+ render_404
+ end
end
\ No newline at end of file
diff --git a/app/controllers/files_controller.rb b/app/controllers/files_controller.rb
index a2902e3fc..c4932b1b2 100644
--- a/app/controllers/files_controller.rb
+++ b/app/controllers/files_controller.rb
@@ -17,14 +17,16 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class FilesController < ApplicationController
-
- layout 'base_projects' #by young
+ layout 'base_projects' #by young
menu_item :files
before_filter :auth_login1, :only => [:index]
before_filter :logged_user_by_apptoken,:only => [:index]
before_filter :find_project_by_project_id#, :except => [:getattachtype]
- before_filter :authorize, :except => [:create,:getattachtype,:quote_resource_show,:search,:searchone4reload,:search_project,:quote_resource_show_project,:search_tag_attachment]
+ before_filter :authorize, :except => [:create,:getattachtype,:quote_resource_show,:search,:searchone4reload,:search_project,:quote_resource_show_project,
+ :search_tag_attachment,:subfield_upload_file,:search_org_subfield_tag_attachment,
+ :search_tag_attachment,:quote_resource_show_org_subfield,:find_org_subfield_attache,
+ :search_files_in_subfield,:upload_files_menu]
helper :sort
include SortHelper
@@ -99,6 +101,7 @@ class FilesController < ApplicationController
@sort = ""
@order = ""
@is_remote = true
+ @q = params[:name].strip
if params[:sort]
order_by = params[:sort].split(":")
@sort = order_by[0]
@@ -107,30 +110,71 @@ class FilesController < ApplicationController
end
sort = "#{@sort} #{@order}"
end
+ # show_attachments [@course]
begin
q = "%#{params[:name].strip}%"
#(redirect_to stores_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank?
if params[:insite]
if q == "%%"
- @result = []
- @searched_attach = paginateHelper @result,10
+ @project_attachment_result = []
+ @searched_attach = paginateHelper @project_attachment_result, 10
else
- @result = find_public_attache q,sort
- @result = visable_attachemnts_insite @result,@project
- @searched_attach = paginateHelper @result,10
+ @project_attachment_result = find_public_attache q,sort
+ @project_attachment_result = visable_attachemnts_insite @project_attachment_result, @project
+ @searched_attach = paginateHelper @project_attachment_result, 10
end
else
- @result = find_project_attache q,@project,sort
- @result = visable_attachemnts @result
- @searched_attach = paginateHelper @result,10
+ @project_attachment_result = find_project_attache q, @project,sort
+ @project_attachment_result = visable_attachemnts @project_attachment_result
+ @searched_attach = paginateHelper @project_attachment_result,10
+ @tag_list = get_project_tag_list @project
end
-
#rescue Exception => e
# #render 'stores'
# redirect_to search_course_files_url
end
end
+ def search_files_in_subfield
+ sort = ""
+ @sort = ""
+ @order = ""
+ @is_remote = true
+ @q = params[:name].strip
+ if params[:sort]
+ order_by = params[:sort].split(":")
+ @sort = order_by[0]
+ if order_by.count > 1
+ @order = order_by[1]
+ end
+ sort = "#{@sort} #{@order}"
+ end
+ # show_attachments [@course]
+ begin
+ q = "%#{params[:name].strip}%"
+ #(redirect_to stores_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank?
+ if params[:insite]
+ if q == "%%"
+ @result = []
+ @searched_attach = paginateHelper @result,10
+ else
+ @result = find_public_attache q,sort
+ @result = visable_attachemnts_insite @result,@org_subfield
+ @searched_attach = paginateHelper @result,10
+ end
+ else
+ @result = find_org_subfield_attache q,@org_subfield,sort
+ @result = visable_attachemnts @result
+ @searched_attach = paginateHelper @result,10
+ @tag_list = attachment_tag_list @result
+ end
+ #rescue Exception => e
+ # #render 'stores'
+ # redirect_to search_course_files_url
+ end
+ @page = params[:page] || 1
+ end
+
def find_course_attache keywords,course,sort = ""
if sort == ""
sort = "created_on DESC"
@@ -144,27 +188,27 @@ class FilesController < ApplicationController
#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
- def find_project_attache keywords,project,sort = ""
+ def find_org_subfield_attache keywords,org_subfield,sort = ""
if sort == ""
sort = "created_on DESC"
end
- ids = ""
- len = 0
- count = project.versions.count
- project.versions.each do |version|
- len = len + 1
- if len != count
- ids += version.id.to_s + ','
- else
- ids += version.id.to_s
- end
+ if keywords != "%%"
+ resultSet = Attachment.where("attachments.container_type = 'OrgSubfield' And attachments.container_id = '#{org_subfield.id}' AND filename LIKE :like ", like: "%#{keywords}%").
+ reorder(sort)
+ else
+ resultSet = Attachment.where("attachments.container_type = 'OrgSubfield' And attachments.container_id = '#{org_subfield.id}' "). reorder(sort)
end
- if ids.blank?
- resultSet = Attachment.where("attachments.container_type = 'Project' And attachments.container_id = '#{project.id}' AND filename LIKE :like ", like: "%#{keywords}%").
- reorder(sort)
+ #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
+
+ def find_project_attache keywords,project,sort = ""
+ if sort == ""
+ sort = "created_on DESC"
+ end
+ if keywords != "%%"
+ resultSet = Attachment.where("attachments.container_type = 'Project' And attachments.container_id = '#{project.id}' AND filename LIKE :like ", like: "%#{keywords}%").reorder(sort)
else
- resultSet = Attachment.where("((attachments.container_type = 'Project' And attachments.container_id = '#{project.id}') OR (container_type = 'Version' AND container_id IN (#{ids}))) AND filename LIKE :like ", like: "%#{keywords}%").
- reorder(sort)
+ resultSet = Attachment.where("attachments.container_type = 'Project' And attachments.container_id = '#{project.id}' "). reorder(sort)
end
end
@@ -186,12 +230,11 @@ class FilesController < ApplicationController
@order = ""
@is_remote = false
if params[:project_id]
- @isproject = true
-
+ @page = params[:page] ? params[:page].to_i + 1 : 2
+ @container_type = 0
if params[:sort]
params[:sort].split(",").each do |sort_type|
order_by = sort_type.split(":")
-
case order_by[0]
when "filename"
attribute = "filename"
@@ -229,25 +272,19 @@ class FilesController < ApplicationController
else
sort = "#{Attachment.table_name}.created_on desc"
end
+ # @containers = [ Project.includes(:attachments).find(@project.id)]
+ # @containers += @project.versions.includes(:attachments).all
- @containers = [ Project.includes(:attachments).find(@project.id)]
- @containers += @project.versions.includes(:attachments).all
-
- ids = []
- @containers.each do |c|
- ids += c.attachments.pluck(:id)
- end
- @containers = [Struct.new(:attachments).new(Attachment.where('id in (?)',ids).reorder(sort))]
+ @containers = [ Project.includes(:attachments).reorder(sort).find(@project.id)]
show_attachments @containers
- respond_to do |format|
- format.html
- format.js
- end
- elsif params[:course_id]
- @isproject = false
+ @tag_list = attachment_tag_list @all_attachments
+ render :layout => 'base_projects'
+ elsif params[:course_id]
+ @container_type = 1
+ @page = params[:page] ? params[:page].to_i + 1 : 2
if params[:sort]
params[:sort].split(",").each do |sort_type|
order_by = sort_type.split(":")
@@ -297,6 +334,56 @@ class FilesController < ApplicationController
@tag_list = attachment_tag_list @all_attachments
render :layout => 'base_courses'
+ elsif params[:org_subfield_id]
+ if params[:sort]
+ params[:sort].split(",").each do |sort_type|
+ order_by = sort_type.split(":")
+
+ case order_by[0]
+ when "filename"
+ attribute = "filename"
+ when "size"
+ attribute = "filesize"
+ when "attach_type"
+ attribute = "attachtype"
+ when "content_type"
+ attribute = "created_on"
+ when "field_file_dense"
+ attribute = "is_public"
+ when "downloads"
+ attribute = "downloads"
+ when "created_on"
+ attribute = "created_on"
+ when "quotes"
+ attribute = "quotes"
+ else
+ attribute = "created_on"
+ end
+ @sort = order_by[0]
+ @order = order_by[1]
+ if order_by.count == 1 && attribute
+ sort += "#{Attachment.table_name}.#{attribute} asc "
+ if sort_type != params[:sort].split(",").last
+ sort += ","
+ end
+ elsif order_by.count == 2 && order_by[1]
+ sort += "#{Attachment.table_name}.#{attribute} #{order_by[1]} "
+ if sort_type != params[:sort].split(",").last
+ sort += ","
+ end
+ end
+ end
+ else
+ sort = "#{Attachment.table_name}.created_on desc"
+ end
+ @container_type = 2
+ @containers = [ OrgSubfield.includes(:attachments).reorder(sort).find(@org_subfield.id)]
+ @organization = Organization.find(@containers.first.organization_id)
+ show_attachments @containers
+ @tag_list = attachment_tag_list @all_attachments
+ @page = params[:page] || 1
+ render :layout => 'base_org'
+ # @subfield = params[:org_subfield_id]
end
end
@@ -311,6 +398,12 @@ class FilesController < ApplicationController
@can_quote = attachment_candown @file
end
+ def quote_resource_show_org_subfield
+ @file = Attachment.find(params[:id])
+ @org_subfield = OrgSubfield.find(params[:org_subfield_id])
+ @can_quote = attachment_candown @file
+ end
+
def new
@versions = @project.versions.sort
@course_tag = @project.project_type
@@ -333,16 +426,40 @@ class FilesController < ApplicationController
if @project
@addTag=false
if params[:in_project_toolbar]
- @in_project_toolbar = params[:in_project_toolbar]
+ @in_project_toolbar = params[:in_project_toolbar]
end
- container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id]))
- attachments = Attachment.attach_filesex(container, params[:attachments], params[:attachment_type])
- render_attachment_warning_if_needed(container)
-
+ # 发送邮件
+ attachments = Attachment.attach_filesex(@project, params[:attachments], params[:attachment_type])
if !attachments.empty? && !attachments[:files].blank? && Setting.notified_events.include?('file_added')
Mailer.run.attachments_added(attachments[:files])
end
-
+ # end
+ if params[:project_attachment_type] && params[:project_attachment_type].is_a?(Array)
+ params[:project_attachment_type].each do |type|
+ tag_name = get_project_tag_name_by_type_nmuber type
+ if !attachments.empty? && attachments[:files] && tag_name != ""
+ attachments[:files].each do |attachment|
+ attachment.tag_list.add(tag_name)
+ attachment.save
+ end
+ end
+ end
+ else
+ if params[:project_attachment_type] && params[:project_attachment_type] != "6"
+ tag_name = get_project_tag_name_by_type_nmuber params[:project_attachment_type]
+ if !attachments.empty? && attachments[:files] && tag_name != ""
+ attachments[:files].each do |attachment|
+ attachment.tag_list.add(tag_name)
+ attachment.save
+ end
+ end
+ end
+ end
+ # 更新资源总数, 根据上传的附件数累加
+ unless @project.project_score.nil?
+ @project.project_score.update_attribute(:attach_num, @project.project_score.attach_num + attachments[:files].count)
+ end
+ # end
# TODO: 临时用 nyan
sort_init 'created_on', 'desc'
sort_update 'created_on' => "#{Attachment.table_name}.created_on",
@@ -350,14 +467,11 @@ class FilesController < ApplicationController
'size' => "#{Attachment.table_name}.filesize",
'downloads' => "#{Attachment.table_name}.downloads"
- @containers = [Project.includes(:attachments).reorder("#{Attachment.table_name}.created_on DESC").find(@project.id)] #modify by Long Jun
- @containers += @project.versions.includes(:attachments).reorder("#{Attachment.table_name}.created_on DESC").all.sort
-
+ @containers = [Project.includes(:attachments).reorder("#{Attachment.table_name}.created_on DESC").find(@project.id)]
show_attachments @containers
-
+ @tag_list = attachment_tag_list @all_attachments
@attachtype = 0
@contenttype = 0
-
respond_to do |format|
format.js
format.html {
@@ -395,8 +509,9 @@ class FilesController < ApplicationController
end
end
end
-
-
+ # 更新课程英雄榜得分
+ update_contributor_score(@course, attachments[:files].first)
+ # end
# TODO: 临时用 nyan
sort_init 'created_on', 'desc'
sort_update 'created_on' => "#{Attachment.table_name}.created_on",
@@ -418,11 +533,89 @@ class FilesController < ApplicationController
redirect_to course_files_url(@course)
}
end
- end
+ elsif @org_subfield
+ @addTag=false
+ attachments = Attachment.attach_filesex(@org_subfield, params[:attachments], params[:org_subfield_attachment_type])
+ if params[:org_subfield_attachment_type] && params[:org_subfield_attachment_type].is_a?(Array)
+ params[:org_subfield_attachment_type].each do |type|
+ tag_name = get_tag_name_by_type_number type
+ if !attachments.empty? && attachments[:files] && tag_name != ""
+ attachments[:files].each do |attachment|
+ attachment.tag_list.add(tag_name)
+ attachment.save
+ end
+ end
+ end
+ else
+ if params[:org_subfield_attachment_type] && params[:org_subfield_attachment_type] != "5"
+ tag_name = get_tag_name_by_type_number params[:org_subfield_attachment_type]
+ if !attachments.empty? && attachments[:files] && tag_name != ""
+ attachments[:files].each do |attachment|
+ attachment.tag_list.add(tag_name)
+ attachment.save
+ end
+ end
+ end
+ end
+
+ # TODO: 临时用 nyan
+ sort_init 'created_on', 'desc'
+ sort_update 'created_on' => "#{Attachment.table_name}.created_on",
+ 'filename' => "#{Attachment.table_name}.filename",
+ 'size' => "#{Attachment.table_name}.filesize",
+ 'downloads' => "#{Attachment.table_name}.downloads"
+
+ @containers = [OrgSubfield.includes(:attachments).reorder("#{Attachment.table_name}.created_on DESC").find(@org_subfield.id)]
+
+ show_attachments @containers
+ @tag_list = attachment_tag_list @all_attachments
+ @attachtype = 0
+ @contenttype = 0
+
+ respond_to do |format|
+ format.js
+ # format.html {
+ # redirect_to org_subfield_files_url(@org_subfield)
+ # }
+ end
+ end
end
end
+ def update_contributor_score(course, file )
+ unless file.author.allowed_to?(:as_teacher, course)
+ course_contributor_score = CourseContributorScore.where("course_id =? and user_id =?", course.id, file.author.id).first
+ if course_contributor_score.nil?
+ CourseContributorScore.create(:course_id => course.id, :user_id => file.author.id, :message_num => 0, :message_reply_num => 0,
+ :news_reply_num => 0, :resource_num => 5, :journal_num => 0, :journal_reply_num => 0, :total_score => 5)
+ else
+ score = course_contributor_score.resource_num + 5
+ total_score = course_contributor_score.total_score + 5
+ course_contributor_score.update_attributes(:resource_num => score, :total_score => total_score)
+ end
+ end
+ end
+
+ def get_project_tag_name_by_type_nmuber type
+ case type
+ when "1"
+ tag_name = "软件版本"
+ when "2"
+ tag_name = "文档"
+ when "3"
+ tag_name = "代码"
+ when "4"
+ tag_name = "媒体"
+ when "5"
+ tag_name = "论文"
+ when "6"
+ tag_name = "其它"
+ else
+ tag_name = ""
+ end
+ end
+
def get_tag_name_by_type_number type
case type
when "1"
@@ -574,15 +767,70 @@ class FilesController < ApplicationController
end
q = "%#{@q.strip}%"
- @result = find_course_attache q,@course,sort
+ if @project
+ @result = find_project_attache q, @project,sort
+ else
+ @result = find_course_attache q,@course,sort
+ end
+ @result = visable_attachemnts @result
+ if params[:other]
+ if @project
+ @result = @result.select{|attachment| !attachment.tag_list.include?('软件版本') && !attachment.tag_list.include?('文档') && !attachment.tag_list.include?('代码') && !attachment.tag_list.include?('媒体') && !attachment.tag_list.include?('论文') }
+ else
+ @result = @result.select{|attachment|
+ !attachment.tag_list.include?('课件') &&
+ !attachment.tag_list.include?('软件') &&
+ !attachment.tag_list.include?('媒体') &&
+ !attachment.tag_list.include?('代码') &&
+ !attachment.tag_list.include?('论文') }
+ end
+ else
+ @result = @result.select{|attachment| attachment.tag_list.include?(@tag_name)} unless @tag_name.blank?
+ end
+ @searched_attach = paginateHelper @result,10
+ if @project
+ @tag_list = get_project_tag_list @project
+ @result_search_project = @result
+ else
+ @tag_list = get_course_tag_list @course
+ end
+ respond_to do |format|
+ format.js
+ # format.html
+ end
+ end
+
+ #搜索资源栏目的指定TAG的资源列表
+ def search_org_subfield_tag_attachment
+ @q,@tag_name,@order = params[:q],params[:tag_name]
+ @is_remote = true
+ if params[:sort]
+ order_by = params[:sort].split(":")
+ @sort = order_by[0]
+ if order_by.count > 1
+ @order = order_by[1]
+ end
+ sort = "#{@sort} #{@order}"
+ end
+
+ q = "%#{@q.strip}%"
+ @result = find_org_subfield_attache q,@org_subfield,sort
@result = visable_attachemnts @result
@result = @result.select{|attachment| attachment.tag_list.include?(@tag_name)} unless @tag_name.blank?
@searched_attach = paginateHelper @result,10
- @tag_list = get_course_tag_list @course
+ @tag_list = get_org_subfield_tag_list @org_subfield
respond_to do |format|
format.js
# format.html
end
end
+
+ def subfield_upload_file
+ @org_subfield = OrgSubfield.find(params[:org_subfield_id])
+ end
+
+ def upload_files_menu
+
+end
end
diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb
index efbb4ffab..3332f1207 100644
--- a/app/controllers/forums_controller.rb
+++ b/app/controllers/forums_controller.rb
@@ -147,8 +147,8 @@ class ForumsController < ApplicationController
order = "#{Memo.table_name}.updated_at #{params[:reorder_time]}"
@order_str = "reorder_time="+params[:reorder_time]
else
- order = "last_replies_memos.created_at desc, #{Memo.table_name}.created_at desc"
- @order_str = "reorder_complex=desc"
+ order = "#{Memo.table_name}.updated_at desc"
+ @order_str = "reorder_time=desc"
end
@memo = Memo.new(:forum => @forum)
@topic_count = @forum.topics.count
diff --git a/app/controllers/homework_common_controller.rb b/app/controllers/homework_common_controller.rb
index 687ef9503..a891d3354 100644
--- a/app/controllers/homework_common_controller.rb
+++ b/app/controllers/homework_common_controller.rb
@@ -15,10 +15,22 @@ class HomeworkCommonController < ApplicationController
@new_homework.homework_detail_manual = HomeworkDetailManual.new
@new_homework.course = @course
@page = params[:page] ? params[:page].to_i + 1 : 0
- @homeworks = @course.homework_commons.order("created_at desc").limit(10).offset(@page * 10)
@is_teacher = User.current.logged? && (User.current.admin? || User.current.allowed_to?(:as_teacher,@course))
+ if @is_teacher
+ @homeworks = @course.homework_commons.order("created_at desc").limit(10).offset(@page * 10)
+ else
+ @homeworks = @course.homework_commons.where("publish_time <= '#{Date.today}'").order("created_at desc").limit(10).offset(@page * 10)
+ end
@is_student = User.current.logged? && (User.current.admin? || (User.current.member_of_course?(@course) && !@is_teacher))
@is_new = params[:is_new]
+
+ #设置at已读
+ @homeworks.each do |homework|
+ homework.journals_for_messages.each do |j|
+ User.current.at_messages.unviewed('JournalsForMessage', j.id).each {|x| x.viewed!}
+ end
+ end
+
respond_to do |format|
format.js
format.html
@@ -55,6 +67,7 @@ class HomeworkCommonController < ApplicationController
end
@homework.end_time = params[:homework_common][:end_time] || Time.now
@homework.course_id = params[:course_id]
+ @homework.anonymous_comment = params[:homework_common][:anonymous_comment] ? params[:homework_common][:anonymous_comment] : 0
homework_detail_manual = @homework.homework_detail_manual || HomeworkDetailManual.new
if @homework.publish_time <= Date.today && homework_detail_manual.comment_status == 0
@@ -84,9 +97,20 @@ class HomeworkCommonController < ApplicationController
end
end
+ #分组作业
+ if @homework.homework_type == 3
+ @homework.homework_detail_group ||= HomeworkDetailGroup.new
+ @homework_detail_group = @homework.homework_detail_group
+ @homework_detail_group.min_num = params[:min_num].to_i
+ @homework_detail_group.max_num = params[:max_num].to_i
+ @homework_detail_group.base_on_project = params[:base_on_project].to_i
+ end
+
if @homework.save
@homework_detail_manual.save if @homework_detail_manual
@homework_detail_programing.save if @homework_detail_programing
+ @homework_detail_group.save if @homework_detail_group
+
if params[:is_in_course] == "1"
redirect_to homework_common_index_path(:course => @course.id)
elsif params[:is_in_course] == "0"
@@ -126,24 +150,45 @@ class HomeworkCommonController < ApplicationController
if @homework_detail_manual.comment_status == 1
student_works = @homework.student_works
if student_works && student_works.size >= 2
- student_works.each_with_index do |work, index|
- user = work.user
- n = @homework_detail_manual.evaluation_num
- n = n < student_works.size ? n : student_works.size - 1
- assigned_homeworks = get_assigned_homeworks(student_works, n, index)
- assigned_homeworks.each do |h|
- student_works_evaluation_distributions = StudentWorksEvaluationDistribution.new(user_id: user.id, student_work_id: h.id)
- student_works_evaluation_distributions.save
+ if @homework.homework_type == 3
+ student_work_projects = @homework.student_work_projects.where("student_work_id is not null")
+ student_work_projects.each_with_index do |pro_work, pro_index|
+ n = @homework_detail_manual.evaluation_num
+ n = n < student_works.size ? n : student_works.size - 1
+ work_index = -1
+ student_works.each_with_index do |stu_work, stu_index|
+ if stu_work.id.to_i == pro_work.student_work_id.to_i
+ work_index = stu_index
+ end
+ end
+ assigned_homeworks = get_assigned_homeworks(student_works, n, work_index)
+ assigned_homeworks.each do |h|
+ student_works_evaluation_distributions = StudentWorksEvaluationDistribution.new(user_id: pro_work.user_id, student_work_id: h.id)
+ student_works_evaluation_distributions.save
+ end
+ end
+ else
+ student_works.each_with_index do |work, index|
+ user = work.user
+ n = @homework_detail_manual.evaluation_num
+ n = n < student_works.size ? n : student_works.size - 1
+ assigned_homeworks = get_assigned_homeworks(student_works, n, index)
+ assigned_homeworks.each do |h|
+ student_works_evaluation_distributions = StudentWorksEvaluationDistribution.new(user_id: user.id, student_work_id: h.id)
+ student_works_evaluation_distributions.save
+ end
end
end
@homework_detail_manual.update_column('comment_status', 2)
+ @homework_detail_manual.update_column('evaluation_start', Date.today)
@statue = 1
# 匿评开启消息邮件通知
send_message_anonymous_comment(@homework, m_status = 2)
Mailer.send_mail_anonymous_comment_open(@homework).deliver
else
@statue = 2
- end
+
+ end
else
@statue = 3
end
@@ -155,6 +200,7 @@ class HomeworkCommonController < ApplicationController
#关闭匿评
def stop_anonymous_comment
@homework_detail_manual.update_column('comment_status', 3)
+ @homework_detail_manual.update_column('evaluation_end', Date.today)
#计算缺评扣分
work_ids = "(" + @homework.student_works.map(&:id).join(",") + ")"
@homework.student_works.each do |student_work|
@@ -235,7 +281,13 @@ class HomeworkCommonController < ApplicationController
#启动匿评参数设置
def start_evaluation_set
-
+ if params[:user_activity_id]
+ @user_activity_id = params[:user_activity_id]
+ else
+ @user_activity_id = -1
+ end
+ @is_in_course = params[:is_in_course]
+ @course_activity = params[:course_activity].to_i
end
#设置匿评参数
@@ -251,6 +303,9 @@ class HomeworkCommonController < ApplicationController
@homework_detail_manual.evaluation_num = params[:evaluation_num]
@homework_detail_manual.save
+ @user_activity_id = params[:user_activity_id].to_i
+ @is_in_course = params[:is_in_course].to_i
+ @course_activity = params[:course_activity].to_i
end
end
@@ -262,6 +317,7 @@ class HomeworkCommonController < ApplicationController
@user_activity_id = -1
end
@is_in_course = params[:is_in_course]
+ @course_activity = params[:course_activity].to_i
end
private
@@ -276,6 +332,7 @@ class HomeworkCommonController < ApplicationController
@homework = HomeworkCommon.find params[:id]
@homework_detail_manual = @homework.homework_detail_manual
@homework_detail_programing = @homework.homework_detail_programing
+ @homework_detail_group = @homework.homework_detail_group
@course = @homework.course
rescue
render_404
@@ -287,7 +344,7 @@ class HomeworkCommonController < ApplicationController
#当前用户是不是课程的成员
def member_of_course
- render_403 unless @course.is_public || User.current.member_of_course?(@course) || User.current.admin?
+ render_403 unless @course.is_public==1 || User.current.member_of_course?(@course) || User.current.admin?
end
def get_assigned_homeworks(student_works, n, index)
diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb
index edf055370..eb78c4c5b 100644
--- a/app/controllers/issues_controller.rb
+++ b/app/controllers/issues_controller.rb
@@ -24,7 +24,7 @@ class IssuesController < ApplicationController
before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
before_filter :find_project, :only => [:new, :create, :update_form]
#before_filter :authorize, :except => [:index, :show]
- before_filter :authorize, :except => [:index,:add_journal, :add_journal_in_org]
+ before_filter :authorize, :except => [:index,:add_journal, :add_journal_in_org,:delete_journal,:reply,:add_reply]
before_filter :find_optional_project, :only => [:index]
before_filter :check_for_default_issue_status, :only => [:new, :create]
@@ -55,6 +55,7 @@ class IssuesController < ApplicationController
helper :timelog
include Redmine::Export::PDF
helper :project_score
+ include ApplicationHelper
def index
retrieve_query
@@ -80,13 +81,18 @@ class IssuesController < ApplicationController
@priority_id = params[:priority_id]
@status_id = params[:status_id]
@subject = params[:subject]
+ @done_ratio = params[:done_ratio]
@issue_count = @query.issue_count
@issue_pages = Paginator.new @issue_count, @limit, params['page']
+ params[:page] = (params[:page] || 1).to_i + 1 #页码需要加1
@offset ||= @issue_pages.offset
@issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
- :order => sort_clause,
+ :order => 'issues.updated_on desc',
:offset => @offset,
:limit => @limit)
+ if params[:set_filter]
+ @set_filter = params[:set_filter]
+ end
@issue_count_by_group = @query.issue_count_by_group
respond_to do |format|
format.js
@@ -115,6 +121,14 @@ class IssuesController < ApplicationController
# 当前用户查看指派给他的缺陷消息,则设置消息为已读
query = ForgeMessage.where("forge_message_type =? and user_id =? and forge_message_id =?", "Issue", User.current, @issue).first
query.update_attribute(:viewed, true) unless query.nil?
+
+ # issue 新建的at消息
+ User.current.at_messages.unviewed('Issue', @issue.id).each {|x| x.viewed!}
+ # 回复的at消息
+ @issue.journals.each do |j|
+ User.current.at_messages.unviewed('Journal', j.id).each {|x| x.viewed!}
+ end
+
# 缺陷状态更新
query_journals = @issue.journals
query_journals.each do |query_journal|
@@ -125,6 +139,7 @@ class IssuesController < ApplicationController
end
end
# end
+ @jour_reply = Journal.new
@journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all
@journals.each_with_index {|j,i| j.indice = i+1}
@journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
@@ -141,18 +156,19 @@ class IssuesController < ApplicationController
@project_base_tag = (params[:project_id] || @issue.project) ? 'base_projects':'base'#by young
@available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq
+ @journal = Journal.new(:journalized => @issue)
- respond_to do |format|``
- format.html {
- retrieve_previous_and_next_issue_ids
- render :template => 'issues/show', :layout => @project_base_tag#by young
- }
- format.api
- format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
- format.pdf {
- pdf = issue_to_pdf(@issue, :journals => @journals)
- send_data(pdf, :type => 'application/pdf', :filename => filename_for_content_disposition("#{@project.identifier}-#{@issue.id}.pdf") )
- }
+ respond_to do |format|
+ format.html {
+ retrieve_previous_and_next_issue_ids
+ render :template => 'issues/show', :layout => @project_base_tag#by young
+ }
+ format.api
+ format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
+ format.pdf {
+ pdf = issue_to_pdf(@issue, :journals => @journals)
+ send_data(pdf, :type => 'application/pdf', :filename => filename_for_content_disposition("#{@project.identifier}-#{@issue.id}.pdf") )
+ }
end
end
@@ -174,7 +190,7 @@ class IssuesController < ApplicationController
respond_to do |format|
format.html {
render_attachment_warning_if_needed(@issue)
- flash[:notice] = l(:label_successful_create)
+ #flash[:notice] = l(:label_successful_create)
#flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("#{@issue.subject}", issue_path(@issue), :title => @issue.subject))
#flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
if params[:continue]
@@ -205,20 +221,28 @@ class IssuesController < ApplicationController
end
def update
- return unless update_issue_from_params
- @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
- saved = false
- begin
- saved = @issue.save_issue_with_child_records(params, @time_entry)
- rescue ActiveRecord::StaleObjectError
- @conflict = true
- if params[:last_journal_id]
- @conflict_journals = @issue.journals_after(params[:last_journal_id]).all
- @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
+ if params[:issue_detail]
+ issue = Issue.find(params[:id])
+ issue = update_user_issue_detail(issue, params)
+ @saved = update_user_issue_detail(issue, params)
+ return
+ else
+ return unless update_issue_from_params
+ @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
+ saved = false
+ begin
+ @saved = @issue.save_issue_with_child_records(params, @time_entry)
+ rescue ActiveRecord::StaleObjectError
+ @conflict = true
+ if params[:last_journal_id]
+ @conflict_journals = @issue.journals_after(params[:last_journal_id]).all
+ @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
+ end
end
end
- if saved
+
+ if @saved
#修改界面增加跟踪者
watcherlist = @issue.watcher_users
select_users = []
@@ -247,19 +271,35 @@ class IssuesController < ApplicationController
if reply_id > 0
JournalReply.add_reply(@issue.current_journal.id, reply_id, User.current.id)
end
- flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
+ #flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? 去掉这个notice,因为现在更新都是ajax操作
respond_to do |format|
+ format.js
format.html { redirect_to issue_url(@issue.id) }
format.api { render_api_ok }
end
else
respond_to do |format|
+
+ format.js
format.html { render :action => 'edit' }
format.api { render_validation_errors(@issue) }
end
end
end
+ def update_user_issue_detail(issue, params)
+ case params[:type]
+ when "status"
+ issue.update_attribute(:status_id, params[:status_id])
+ when "assigned"
+ issue.update_attribute(:assigned_to_id, params[:assigned_to_id])
+ when "ratio"
+ issue.update_attribute(:done_ratio, params[:done_ratio])
+ when "prior"
+ issue.update_attribute(:priority_id, params[:priority_id])
+ end
+ end
+
# Updates the issue form when changing the project, status or tracker
# on issue creation/update
def update_form
@@ -386,11 +426,16 @@ class IssuesController < ApplicationController
jour.user_id = User.current.id
jour.notes = params[:notes]
jour.journalized = @issue
+ jour.save_attachments(params[:attachments])
jour.save
- user_activity = UserActivity.where("act_type='Issue' and act_id =#{@issue.id}").first
- user_activity.updated_at = jour.created_on
- user_activity.save
+ update_user_activity(@issue.class,@issue.id)
+ update_forge_activity(@issue.class,@issue.id)
+
@user_activity_id = params[:user_activity_id]
+ if params[:issue_id]
+ @issue_id = params[:issue_id]
+ end
+ @priorities = IssuePriority.active
respond_to do |format|
format.js
end
@@ -414,6 +459,43 @@ class IssuesController < ApplicationController
end
end
+ #对某个journ回复,显示回复框
+ def reply
+ @issue = Issue.find(params[:id])
+ @jour = Journal.find(params[:journal_id])
+ @tempContent = "
#{ll(Setting.default_language, :text_user_wrote, @jour.user.realname.blank? ? @jour.user.login: @jour.user.realname)}".html_safe + respond_to do |format| + format.js + end + end + + #给issue添加journ。回复内容包含 对某个被回复的journ的内容 + def add_reply + if User.current.logged? + jour = Journal.new + jour.user_id = User.current.id + jour.notes = params[:quote]+params[:notes] + @issue = Issue.find params[:id] + jour.journalized = @issue + jour.save + update_user_activity(@issue.class,@issue.id) + update_forge_activity(@issue.class,@issue.id) + + respond_to do |format| + format.js + end + end + end + + # + def delete_journal + @issue = Issue.find(params[:id]) + Journal.destroy(params[:journal_id]) + respond_to do |format| + format.js + end + end + private def find_project diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb index 4181090a5..a47ddf787 100644 --- a/app/controllers/memos_controller.rb +++ b/app/controllers/memos_controller.rb @@ -73,6 +73,7 @@ class MemosController < ApplicationController end end #end + format.js format.html { redirect_to back_memo_url, notice: "#{l :label_memo_create_succ}" } format.json { render json: @memo, status: :created, location: @memo } else @@ -152,17 +153,21 @@ class MemosController < ApplicationController end def update + @flag = false respond_to do |format| if( #@memo.update_column(:subject, params[:memo][:subject]) && @memo.update_column(:content, params[:memo][:content]) && @memo.update_column(:sticky, params[:memo][:sticky]) && @memo.update_column(:lock, params[:memo][:lock]) && - @memo.update_column(:subject,params[:memo][:subject])) + @memo.update_column(:subject,params[:memo][:subject]) && + @memo.update_column(:updated_at,Time.now)) @memo.save_attachments(params[:attachments] || (params[:memo] && params[:memo][:uploads])) - @memo.save + @flag = @memo.save # @memo.root.update_attribute(:updated_at, @memo.updated_at) + format.js format.html {redirect_to back_memo_url, notice: "#{l :label_memo_create_succ}"} else + format.js format.html { render action: "edit" } format.json { render json: @person.errors, status: :unprocessable_entity } end diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 9b090de9a..17111b3f3 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -22,8 +22,8 @@ class MessagesController < ApplicationController default_search_scope :messages before_filter :find_board, :only => [:new, :preview,:edit] before_filter :find_attachments, :only => [:preview] - before_filter :find_message, :except => [:new, :preview] - before_filter :authorize, :except => [:preview, :edit, :destroy, :new] + before_filter :find_message, :except => [:new, :preview, :join_org_subfield, :get_subfield_on_click_org, :join_org_subfields] + before_filter :authorize, :except => [:preview, :edit, :destroy, :new,:join_org_subfield, :get_subfield_on_click_org, :join_org_subfields] helper :boards helper :watchers @@ -75,7 +75,7 @@ class MessagesController < ApplicationController @replies = paginateHelper messages_replies,10 @reply = Message.new(:subject => "RE: #{@message.subject}") render :action => "show", :layout => "base_courses"#by young - else + elsif @project @reply_pages = Paginator.new @reply_count, REPLIES_PER_PAGE, page @replies = @topic.children. includes(:author, :attachments, {:board => :project}). @@ -86,6 +86,18 @@ class MessagesController < ApplicationController @reply = Message.new(:subject => "RE: #{@message.subject}") render :action => "show", :layout => "base_projects"#by young + else + @reply_pages = Paginator.new @reply_count, REPLIES_PER_PAGE, page + @replies = @topic.children. + includes(:author, :attachments, {:board => :project}). + reorder("#{Message.table_name}.created_on DESC"). + limit(@reply_pages.per_page). + offset(@reply_pages.offset). + all + + @reply = Message.new(:subject => "RE: #{@message.subject}") + @organization = @org_subfield.organization + render :action => "show", :layout => "base_org"#by young end end @@ -164,21 +176,11 @@ class MessagesController < ApplicationController @reply.subject = "RE: #{@topic.subject}" unless params[:reply][:subject] # @reply.reply_id = params[:id] @topic.children << @reply - course_activity = CourseActivity.where("course_act_type='Message' and course_act_id =#{@topic.id}").first - if course_activity - course_activity.updated_at = Time.now - course_activity.save - end - user_activity = UserActivity.where("act_type='Message' and act_id =#{@topic.id}").first - if user_activity - user_activity.updated_at = Time.now - user_activity.save - end - org_activity = OrgActivity.where("org_act_type='Message' and org_act_id =#{@topic.id}").first - if org_activity - org_activity.updated_at = Time.now - org_activity.save - end + update_course_activity(@topic.class,@topic.id) + update_user_activity(@topic.class,@topic.id) + update_forge_activity(@topic.class,@topic.id) + update_org_activity(@topic.class,@topic.id) + #@topic.update_attribute(:updated_on, Time.now) if !@reply.new_record? if params[:asset_id] @@ -194,6 +196,8 @@ class MessagesController < ApplicationController end if params[:user_activity_id] @user_activity_id = params[:user_activity_id] + @is_course = params[:is_course] + @is_board = params[:is_board] respond_to do |format| format.js end @@ -214,39 +218,52 @@ class MessagesController < ApplicationController @isReply = false if @project (render_403; return false) unless @message.editable_by?(User.current) - else + elsif @course (render_403; return false) unless @message.course_editable_by?(User.current) + else + (render_403; return false) unless @message.org_subfield_editable_by?(User.current) end @message.safe_attributes = params[:message] if request.post? && @message.save attachments = Attachment.attach_files(@message, params[:attachments]) render_attachment_warning_if_needed(@message) - flash[:notice] = l(:notice_successful_update) + #flash[:notice] = l(:notice_successful_update) @message.reload - if params[:is_board] + if params[:is_course] && params[:is_course].to_i == 0 + redirect_to user_activities_path(User.current.id) + elsif params[:is_course] && params[:is_course].to_i == 1 && params[:is_board] && params[:is_board].to_i == 0 if @project - redirect_to project_boards_path(@project) + redirect_to project_path(@project) elsif @course - redirect_to course_boards_path(@course) + redirect_to course_activity_path(@course) end - else - redirect_to board_message_url(@message.board, @message.root, :r => (@message.parent_id && @message.id)) - end - elsif request.get? || request.post? - if params[:is_board] + elsif params[:is_course] && params[:is_course].to_i == 1 && params[:is_board] && params[:is_board].to_i == 1 if @project redirect_to project_boards_path(@project) elsif @course redirect_to course_boards_path(@course) end else - respond_to do |format| - format.html { - layout_file = @project ? 'base_projects' : 'base_courses' - render :layout => layout_file - } - end + redirect_to board_message_url(@message.board, @message.root, :r => (@message.parent_id && @message.id)) + end + elsif request.get? || request.post? + if params[:is_course] && params[:is_board] + @is_course = params[:is_course] + @is_board = params[:is_board] + end + respond_to do |format| + format.html { + if @project + layout_file = 'base_projects' + elsif @course + layout_file = 'base_courses' + elsif @org_subfield + @organization = @org_subfield.organization + layout_file = 'base_org' + end + render :layout => layout_file + } end end end @@ -255,26 +272,46 @@ class MessagesController < ApplicationController def destroy if @project (render_403; return false) unless @message.destroyable_by?(User.current) - else + elsif @course (render_403; return false) unless @message.course_destroyable_by?(User.current) + else + (render_403; return false) unless @message.org_subfield_editable_by?(User.current) end r = @message.to_param @message.destroy # modify by nwb if @project - if params[:is_board] - redirect_to project_boards_url(@project) + if params[:is_course] && params[:is_course].to_i == 0 + redirect_to user_activities_path(User.current.id) + elsif params[:is_course] && params[:is_course].to_i == 1 && params[:is_board] && params[:is_board].to_i == 0 + redirect_to project_path(@project) + elsif params[:is_course] && params[:is_course].to_i == 1 && params[:is_board] && params[:is_board].to_i == 1 + redirect_to project_boards_path(@project) + elsif @message.parent + redirect_to board_message_url(@board, @message.parent, :r => r) else - redirect_to board_message_url(@board, @topic, :r => @reply) + redirect_to project_board_url(@project, @board) end elsif @course + if params[:is_course] && params[:is_course].to_i == 0 + redirect_to user_activities_path(User.current.id) + elsif params[:is_course] && params[:is_course].to_i == 1 && params[:is_board] && params[:is_board].to_i == 0 + redirect_to course_activity_path(@course) + elsif params[:is_course] && params[:is_course].to_i == 1 && params[:is_board] && params[:is_board].to_i == 1 + redirect_to course_boards_path(@course) + elsif @message.parent + redirect_to board_message_url(@board, @message.parent, :r => r) + else + redirect_to course_boards_path(@course) + end + elsif @org_subfield if params[:is_board] - redirect_to course_boards_url(@course) + redirect_to organization_path(:id => @org_subfield.organization_id, :org_subfield_id => @org_subfield.id) else if @message.parent redirect_to board_message_url(@board, @message.parent, :r => r) else - redirect_to course_board_url(@course, @board) + redirect_to organization_path(:id => @org_subfield.organization_id, :org_subfield_id => @org_subfield.id) end end end @@ -296,6 +333,41 @@ class MessagesController < ApplicationController render :partial => 'common/preview' end + def join_org_subfield + @message = Message.find(params[:message_id]) + @organizations = User.current.organizations + end + + def get_subfield_on_click_org + @org = Organization.find(params[:organization_id]) + end + + def join_org_subfields + org_subfield_ids = params[:org_subfields] + @message = Message.find(params[:id]) + # @message.update_attribute(:updated_on, Time.now) + # type = @message.board.course_id.nil? ? "Project":"Course" + org_subfield_ids.each do |field_id| + @message.quotes = @message.quotes.nil? ? 1 : (@message.quotes + 1) + @message.save + board = OrgSubfield.find(field_id).boards.first + mes = Message.create(:board_id => board.id, :subject => @message.subject, :content => @message.content, :author_id => User.current.id, + :created_on => Time.now, :updated_on => Time.now, :locked => @message.locked, :sticky => @message.sticky) + @message.attachments.each do |attach| + mes.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) + end + # OrgSubfieldMessage.create(:org_subfield_id => field_id.to_i, :message_id => mes.id, :message_type => type) + # org_acts = OrgActivity.where("container_type='OrgSubfield' and container_id=? and org_act_type='Message' and org_act_id=?", field_id.to_i, @message.id) + # if org_acts.all.size() > 0 + # org_acts.first.update_attribute(:updated_at, Time.now) + # else + OrgActivity.create(:container_type => 'OrgSubfield', :container_id => field_id.to_i, :org_act_type=>'Message', :org_act_id => mes.id, :user_id => User.current.id) + # end + end + end + private def find_message return unless find_board @@ -312,6 +384,9 @@ class MessagesController < ApplicationController @project = @board.project elsif @board.course_id @course = @board.course + else + @org_subfield = @board.org_subfield + @organization = @org_subfield end rescue ActiveRecord::RecordNotFound diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rb index 16d516f82..0883b3799 100644 --- a/app/controllers/my_controller.rb +++ b/app/controllers/my_controller.rb @@ -85,7 +85,7 @@ class MyController < ApplicationController flash[:notice] = l(:notice_mail_notification_updated) redirect_to my_account_url else - redirect_to signin_url + redirect_to signin_url end end @@ -109,16 +109,27 @@ class MyController < ApplicationController # Edit user's account def account @user = User.current + lg=@user.login @pref = @user.pref diskfile = disk_filename('User', @user.id) diskfile1 = diskfile + 'temp' begin if request.post? + # 修改邮箱的时候同步修改到gitlab + if @user.mail != params[:user][:mail] + g = Gitlab.client + begin + g.edit_user(@user.gid, :email => params[:user][:mail]) + rescue + logger.error "sync user's email of gitlab failed!" + end + end + @user.safe_attributes = params[:user] @user.pref.attributes = params[:pref] @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') - @user.login = params[:login] + #@user.login = params[:login] unless @user.user_extensions.nil? if @user.user_extensions.identity == 2 @user.firstname = params[:enterprise_name] @@ -212,7 +223,7 @@ class MyController < ApplicationController end flash.now[:error] = l(:notice_account_old_wrong_password) end - render :template => 'my/account',:layout=>'base_users_new' + render :template => 'my/account',:layout=>'new_base_user' end # Create a new feeds key diff --git a/app/controllers/news_controller.rb b/app/controllers/news_controller.rb index be2b44c06..0962deb10 100644 --- a/app/controllers/news_controller.rb +++ b/app/controllers/news_controller.rb @@ -46,15 +46,16 @@ class NewsController < ApplicationController @course = Course.find(params[:course_id]) end if @project + @page = params[:page] ? params[:page].to_i + 1 : 0 scope = @project ? @project.news.visible : News.visible @news_count = scope.count - @news_pages = Paginator.new @news_count, @limit, params['page'] - @offset ||= @news_pages.offset + #@news_pages = Paginator.new @news_count, @limit, params['page'] + #@offset ||= @news_pages.offset @newss = scope.all(:include => [:author, :project], :order => "#{News.table_name}.created_on DESC", - :offset => @offset, - :limit => @limit) + :offset => @page * 10, + :limit => 10) respond_to do |format| format.html { @@ -63,26 +64,32 @@ class NewsController < ApplicationController render :layout => false if request.xhr? } + format.js format.api format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") } end elsif @course if (User.current.admin? || @course.is_public == 1 || (@course.is_public == 0 && User.current.member_of_course?(@course))) scope = @course ? @course.news.course_visible : News.course_visible + news_arr = scope.map{|news| news.id} + + @page = params[:page] ? params[:page].to_i + 1 : 0 + news_page = @page *10 @news_count = scope.count + @is_new = params[:is_new] @q = params[:subject] if params[:subject].nil? || params[:subject].blank? - scope_order = scope.all(:include => [:author, :course], - :order => "#{News.table_name}.sticky DESC, #{News.table_name}.created_on DESC") + scope_order = scope.reorder("#{News.table_name}.sticky DESC, #{News.table_name}.created_on DESC").limit(10).offset(news_page).includes(:author,:course).all() + #all(:include => [:author, :course], + #:order => "#{News.table_name}.sticky DESC, #{News.table_name}.created_on DESC").limit(10).offset(news_page) else - scope_order = scope.where("#{News.table_name}.title like '#{'%' << params[:subject].to_s << '%'}'").all(:include => [:author, :course], - :order => "#{News.table_name}.sticky DESC, #{News.table_name}.created_on DESC") + scope_order = scope.where("#{News.table_name}.title like '#{'%' << params[:subject].to_s << '%'}'").reorder("#{News.table_name}.sticky DESC, #{News.table_name}.created_on DESC").limit(10).offset(news_page).includes(:author,:course).all() + #.all(:include => [:author, :course],:order => "#{News.table_name}.sticky DESC, #{News.table_name}.created_on DESC") end - # :offset => @offset, - # :limit => @limit) - @newss = paginateHelper scope_order,10 + @newss = scope_order + #@newss = paginateHelper scope_order,10 respond_to do |format| format.html { @news = News.new @@ -130,6 +137,7 @@ class NewsController < ApplicationController result = cs.show_course_news params,User.current @news = result[:news] @comments = result[:comments] + @comment = Comment.new #@comments = @news.comments #@comments.reverse! if User.current.wants_comments_in_reverse_order? #modify by nwb @@ -138,6 +146,10 @@ class NewsController < ApplicationController if @course render :layout => 'base_courses' end + elsif @news.org_subfield_id + @org_subfield = OrgSubfield.find(@news.org_subfield_id) + @organization = @org_subfield.organization + render :layout => 'base_org' end end @@ -145,9 +157,39 @@ class NewsController < ApplicationController #modify by nwb if @project @news = News.new(:project => @project, :author => User.current) + @news.safe_attributes = params[:news] + @news.save_attachments(params[:attachments]) + if @news.save + if params[:asset_id] + ids = params[:asset_id].split(',') + update_kindeditor_assets_owner ids,@news.id,OwnerTypeHelper::NEWS + end + render_attachment_warning_if_needed(@news) + #flash[:notice] = l(:notice_successful_create) + redirect_to project_news_index_url(@project) + else + redirect_to project_news_index_url(@project) + #layout_file = 'base_courses' + #render :action => 'new', :layout => layout_file + end elsif @course @news = News.new(:course => @course, :author => User.current) - render :layout => 'base_courses' + #render :layout => 'base_courses' + @news.safe_attributes = params[:news] + @news.save_attachments(params[:attachments]) + if @news.save + if params[:asset_id] + ids = params[:asset_id].split(',') + update_kindeditor_assets_owner ids,@news.id,OwnerTypeHelper::NEWS + end + render_attachment_warning_if_needed(@news) + #flash[:notice] = l(:notice_successful_create) + redirect_to course_news_index_url(@course) + else + redirect_to course_news_index_url(@course) + #layout_file = 'base_courses' + #render :action => 'new', :layout => layout_file + end end end @@ -200,8 +242,14 @@ class NewsController < ApplicationController end def edit + if @news.org_subfield_id + @org_subfield = OrgSubfield.find(@news.org_subfield_id) + @organization = @org_subfield.organization + end if @course render :layout => "base_courses" + elsif @org_subfield + render :layout => 'base_org' end end @@ -210,7 +258,7 @@ class NewsController < ApplicationController @news.save_attachments(params[:attachments]) if @news.save render_attachment_warning_if_needed(@news) - flash[:notice] = l(:notice_successful_update) + #flash[:notice] = l(:notice_successful_update) redirect_to news_url(@news) else #flash[:error] = l(:notice_successful_update) @@ -219,12 +267,17 @@ class NewsController < ApplicationController end def destroy + if @news.org_subfield_id + @org_subfield = OrgSubfield.find(@news.org_subfield_id) + end @news.destroy # modify by nwb if @project redirect_to project_news_index_url(@project) elsif @course redirect_to course_news_index_url(@course) + elsif @org_subfield + redirect_to organization_path(@org_subfield.organization, :org_subfield_id => @org_subfield.id) end end diff --git a/app/controllers/org_courses_controller.rb b/app/controllers/org_courses_controller.rb index 3d1d313c5..d054e41de 100644 --- a/app/controllers/org_courses_controller.rb +++ b/app/controllers/org_courses_controller.rb @@ -3,7 +3,9 @@ class OrgCoursesController < ApplicationController org_ids = params[:orgNames] @course = Course.find(params[:course_id]) org_ids.each do |org_id| - OrgCourse.create(:organization_id => org_id.to_i, :course_id => params[:course_id].to_i, :created_at => Time.now) + if OrgCourse.where("organization_id =? and course_id =?", org_id.to_i, params[:course_id].to_i).count == 0 + OrgCourse.create(:organization_id => org_id.to_i, :course_id => params[:course_id].to_i, :created_at => Time.now) + end end respond_to do |format| format.js diff --git a/app/controllers/org_document_comments_controller.rb b/app/controllers/org_document_comments_controller.rb index 61a27c433..f250b46de 100644 --- a/app/controllers/org_document_comments_controller.rb +++ b/app/controllers/org_document_comments_controller.rb @@ -1,24 +1,39 @@ class OrgDocumentCommentsController < ApplicationController before_filter :find_organization, :only => [:new, :create, :show, :index] - + helper :attachments layout 'base_org' def new @org_document_comment = OrgDocumentComment.new end + def create @org_document_comment = OrgDocumentComment.new(:organization_id => @organization.id, :creator_id => User.current.id) @org_document_comment.title = params[:org_document_comment][:title] @org_document_comment.content = params[:org_document_comment][:content] + @org_document_comment.save_attachments(params[:attachments]) + if params[:field_id] + @org_document_comment.org_subfield_id = params[:field_id].to_i + end if @org_document_comment.save flash.keep[:notice] = l(:notice_successful_create) - OrgActivity - redirect_to organization_org_document_comments_path(@organization) + EditorOfDocument.create(:editor_id => User.current.id, :org_document_comment_id => @org_document_comment.id, :created_at => @org_document_comment.updated_at) + if params[:field_id] + @org_subfield = OrgSubfield.find(params[:field_id]) + if @org_subfield.subfield_subdomain_dir.nil? + redirect_to organization_path(@organization, :org_subfield_id => params[:field_id]) + else + redirect_to show_org_subfield_organization_path(:id => @organization.id, :sub_dir_name => @org_subfield.subfield_subdomain_dir.name) + end + else + redirect_to organization_org_document_comments_path(@organization) + end else redirect_to new_org_document_comment_path(:organization_id => @organization.id) end end + def show @document = OrgDocumentComment.find(params[:id]) end @@ -30,20 +45,35 @@ class OrgDocumentCommentsController < ApplicationController render_403 end end + def update @org_document = OrgDocumentComment.find(params[:id]) @org_document.update_attributes(:title => params[:org_document_comment][:title], :content => params[:org_document_comment][:content]) + Attachment.attach_files(@org_document, params[:attachments]) + # @org_document.save_attachments(params[:attachments]) if @org_document.parent.nil? act = OrgActivity.where("org_act_type='OrgDocumentComment' and org_act_id =?", @org_document.id).first act.update_attributes(:updated_at => @org_document.updated_at) + EditorOfDocument.create(:editor_id => User.current.id, :org_document_comment_id => @org_document.id, :created_at => Time.now) end respond_to do |format| - format.html {redirect_to organization_org_document_comments_path(:organization_id => @org_document.organization.id)} + format.html { + if params[:flag].to_i == 0 + redirect_to organization_org_document_comments_path(:organization_id => @org_document.organization.id) + else + if params[:flag].to_i == 1 + redirect_to org_document_comment_path(@org_document.root.id, :organization_id => @org_document.organization.id) + else + redirect_to organization_path(@org_document.organization.id, :org_subfield_id => params[:org_subfield_id]) + end + end + } end end def edit @org_document = OrgDocumentComment.find(params[:id]) + @flag = params[:flag] @organization = Organization.find(params[:organization_id]) end @@ -74,10 +104,10 @@ class OrgDocumentCommentsController < ApplicationController def destroy @org_document_comment = OrgDocumentComment.find(params[:id]) org = @org_document_comment.organization + if @org_document_comment.id == org.home_id + org.update_attributes(:home_id => nil) + end if @org_document_comment.destroy - if @org_document_comment.id == org.id - org.home_id == nil - end end respond_to do |format| format.js diff --git a/app/controllers/org_projects_controller.rb b/app/controllers/org_projects_controller.rb index 29e60a62a..a455ce408 100644 --- a/app/controllers/org_projects_controller.rb +++ b/app/controllers/org_projects_controller.rb @@ -3,7 +3,9 @@ class OrgProjectsController < ApplicationController org_ids = params[:orgNames] @project = Project.find(params[:project_id]) org_ids.each do |org_id| - OrgProject.create(:organization_id => org_id.to_i, :project_id => params[:project_id].to_i, :created_at => Time.now) + if OrgProject.where("organization_id =? and project_id =?", org_id.to_i, @project.id).count == 0 + OrgProject.create(:organization_id => org_id.to_i, :project_id => params[:project_id].to_i, :created_at => Time.now) + end end respond_to do |format| format.js diff --git a/app/controllers/org_subfields_controller.rb b/app/controllers/org_subfields_controller.rb new file mode 100644 index 000000000..bae0232a5 --- /dev/null +++ b/app/controllers/org_subfields_controller.rb @@ -0,0 +1,145 @@ +class OrgSubfieldsController < ApplicationController + helper :attachments + include FilesHelper + layout 'base_org' + def create + if OrgSubfield.where("organization_id=#{params[:organization_id]} and name=?",params[:name]).count == 0 + @res = true + @subfield = OrgSubfield.create(:name => params[:name]) + @organization = Organization.find(params[:organization_id]) + @organization.org_subfields << @subfield + if !params[:sub_dir].blank? + sql = "select subfield_subdomain_dirs.* from subfield_subdomain_dirs, org_subfields where subfield_subdomain_dirs.org_subfield_id = org_subfields.id "+ + "and org_subfields.organization_id=#{@organization.id} and subfield_subdomain_dirs.name='#{params[:sub_dir]}'" + if SubfieldSubdomainDir.find_by_sql(sql).count == 0 + SubfieldSubdomainDir.create(:org_subfield_id => @subfield.id, :name => params[:sub_dir]) + end + end + @subfield.update_attributes(:priority => @subfield.id, :field_type => params[:field_type]) + else + @res = false + end + end + + def show + @flag = params[:flag] || false + sort = "" + @sort = "" + @order = "" + @is_remote = false + if params[:id] + @organization = Organization.find(params[:id]) + else + @organization = Organization.where("domain=?",request.subdomain).first + end + @org_subfield = OrgSubfield.find_by_sql("select distinct org_subfields.* from org_subfields,"+ + "subfield_subdomain_dirs where org_subfields.id = subfield_subdomain_dirs.org_subfield_id and "+ + " org_subfields.organization_id=#{@organization.id} and subfield_subdomain_dirs.name='#{params[:sub_dir_name]}'").first + if @org_subfield.field_type == 'Post' + @org_subfield_ids = @org_subfield.org_document_comments.map(&:id) << 0 + @org_activities = OrgActivity.where("(org_act_type='OrgDocumentComment'and org_act_id in (#{@org_subfield_ids.join(",")})) || (container_type='OrgSubfield' and container_id=#{@org_subfield.id})").order('updated_at desc').page(params[:page] || 1).per(10) + #redirect_to organization_path(@organization, :org_subfield_id => @org_subfield.id) + else + if params[:sort] + params[:sort].split(",").each do |sort_type| + order_by = sort_type.split(":") + case order_by[0] + when "filename" + attribute = "filename" + when "size" + attribute = "filesize" + when "attach_type" + attribute = "attachtype" + when "content_type" + attribute = "created_on" + when "field_file_dense" + attribute = "is_public" + when "downloads" + attribute = "downloads" + when "created_on" + attribute = "created_on" + when "quotes" + attribute = "quotes" + else + attribute = "created_on" + end + @sort = order_by[0] + @order = order_by[1] + if order_by.count == 1 && attribute + sort += "#{Attachment.table_name}.#{attribute} asc " + if sort_type != params[:sort].split(",").last + sort += "," + end + elsif order_by.count == 2 && order_by[1] + sort += "#{Attachment.table_name}.#{attribute} #{order_by[1]} " + if sort_type != params[:sort].split(",").last + sort += "," + end + end + end + else + sort = "#{Attachment.table_name}.created_on desc" + end + @container_type = 2 + @containers = [ OrgSubfield.includes(:attachments).reorder(sort).find(@org_subfield.id)] + @organization = Organization.find(@containers.first.organization_id) + show_attachments @containers + @tag_list = attachment_tag_list @all_attachments + end + @page = params[:page] || 1 + #render :layout => 'base_org' + end + + def destroy + @subfield = OrgSubfield.find(params[:id]) + @organization = Organization.find(@subfield.organization_id) + @subfield.destroy + end + + def update + @subfield = OrgSubfield.find(params[:id]) + @organization = Organization.find(@subfield.organization_id) + @subfield.update_attributes(:name => params[:name]) + end + + def update_sub_dir + @org_subfield = OrgSubfield.find(params[:id]) + sql = "select subfield_subdomain_dirs.* from subfield_subdomain_dirs, org_subfields where subfield_subdomain_dirs.org_subfield_id = org_subfields.id "+ + "and org_subfields.organization_id=#{@org_subfield.organization.id} and subfield_subdomain_dirs.name='#{params[:sub_dir_name]}'" + if SubfieldSubdomainDir.find_by_sql(sql).count == 0 + if @org_subfield.subfield_subdomain_dir + @sub_dir = @org_subfield.subfield_subdomain_dir + @sub_dir = SubfieldSubdomainDir.update(@sub_dir.id, :name => params[:sub_dir_name]) + #@sub_dir.update_attribute(:name, params[:sub_dir_name]) + else + @sub_dir = SubfieldSubdomainDir.create(:org_subfield_id => @org_subfield.id, :name => params[:sub_dir_name]) + end + @exist = false + else + @exist = true + end + end + + def show_attachments obj + @attachments = [] + obj.each do |container| + @attachments += container.attachments + end + @all_attachments = User.current.admin? ? @attachments : visable_attachemnts(@attachments) + @limit = 10 + @feedback_count = @all_attachments.count + @feedback_pages = Paginator.new @feedback_count, @limit, params['page'] + @offset ||= @feedback_pages.offset + #@curse_attachments_all = @all_attachments[@offset, @limit] + @obj_attachments = paginateHelper @all_attachments,10 + end + + #获取指定资源列表的TAG的集合以及每个TAG的数量,降序排序 + def attachment_tag_list attachments + tag_list = Hash.new + attachments.each do |attachment| + attachment.tag_list.map{|tag| tag_list.has_key?(tag) ? tag_list[tag] = tag_list[tag] + 1 : tag_list[tag] = 1} + end + tag_list.sort {|a,b| b[1]<=>a[1]} + end +end diff --git a/app/controllers/organizations_controller.rb b/app/controllers/organizations_controller.rb index 0d22ab461..6e2da311f 100644 --- a/app/controllers/organizations_controller.rb +++ b/app/controllers/organizations_controller.rb @@ -1,178 +1,330 @@ -# encoding: utf-8 -class OrganizationsController < ApplicationController - - - helper :sort - include SortHelper - helper :custom_fields - include CustomFieldsHelper - include AvatarHelper - include WordsHelper - include GitlabHelper - include UserScoreHelper - - include PollHelper - helper :user_score - helper :journals - - # added by liuping 关注 - - helper :watchers - helper :activities - - ### added by william - include ActsAsTaggableOn::TagsHelper - - # fq - helper :words - helper :project_score - helper :issues - include UsersHelper - before_filter :find_organization, :only => [:show, :members] - layout 'base_org' - def index - - end - def new - @organization = Organization.new - render :layout => 'new_base' - end - def create - @organization = Organization.new - @organization.name = params[:organization][:name] - @organization.description = params[:organization][:description] - @organization.is_public = params[:organization][:is_public] - @organization.creator_id = User.current.id - member = OrgMember.new(:user_id => User.current.id) - - @organization.org_members << member - if @organization.save - OrgMemberRole.create(:org_member_id => member.id, :role_id => 11) - redirect_to organization_path(@organization) - end - end - - def show - if @organization.is_public? || User.current.admin? || User.current.member_of_org?(@organization) - @organization = Organization.find(params[:id]) - project_ids = @organization.projects.map(&:id) << 0 - course_ids = @organization.courses.map(&:id) << 0 - course_types = "('Message','News','HomeworkCommon','Poll','Course')" - case params[:type] - when nil - @org_activities = OrgActivity.where("(container_id =? and container_type =?) " + - "or (container_type ='Project' and org_act_type in ('Issue','Message','ProjectCreateInfo') and container_id in (#{project_ids.join(',')})) "+ - "or (container_type ='Course' and org_act_type in #{course_types} and container_id in (#{course_ids.join(',')}))", - @organization.id, 'Organization').order('updated_at desc').page(params[:page] || 1).per(10) - when 'project_issue' - @org_activities = OrgActivity.where("container_type = 'Project' and org_act_type = 'Issue' and container_id in (#{project_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10) - when 'project_message' - @org_activities = OrgActivity.where("container_type = 'Project' and org_act_type = 'Message' and container_id in (#{project_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10) - when 'org' - @org_activities = OrgActivity.where("container_id =? and container_type =?",@organization.id, 'Organization').order('updated_at desc').page(params[:page] || 1).per(10) - when 'course_homework' - @org_activities = OrgActivity.where("container_type = 'Course' and org_act_type = 'HomeworkCommon' and container_id in (#{course_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10) - when 'course_news' - @org_activities = OrgActivity.where("container_type = 'Course' and org_act_type = 'News' and container_id in (#{course_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10) - when 'course_message' - @org_activities = OrgActivity.where("container_type = 'Course' and org_act_type = 'Message' and container_id in (#{course_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10) - when 'course_poll' - @org_activities = OrgActivity.where("container_type = 'Course' and org_act_type = 'Poll' and container_id in (#{course_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10) - end - @page = params[:page] - respond_to do |format| - format.html - format.js - end - else - render_403 - end - end - - def update - @organization = Organization.find(params[:id]) - @organization.name = params[:organization][:name] - @organization.description = params[:organization][:description] - @organization.domain = params[:organization][:domain] - @organization.is_public = params[:organization][:is_public] == 'on' ? 1 : 0 - #@organization.name = params[:organization][:name] - @organization.save - respond_to do |format| - format.html { redirect_to setting_organization_path(@organization)} - end - end - - def check_uniq - @check = false; - @org_name = params[:org_name].strip - @config_page = params[:config_page] - sameName = @config_page ? Organization.where('name = ? and id != ?',params[:org_name],params[:org_id].to_i).count == 0 : Organization.where('name = ?',params[:org_name]).count == 0 - if sameName == true - @check = true - end - respond_to do |format| - format.js - end - end - - def find_organization - @organization = Organization.find(params[:id]) - end - - def setting - @organization = Organization.find(params[:id]) - - if User.current.admin? || User.current.admin_of_org?(@organization) - else - render_403 - end - end - - - def clear_org_avatar_temp - - end - - def set_homepage - @org = Organization.find(params[:id]) - @org.home_id = params[:home_id] - @org.save - # respond_to do |format| - # format.html {redirect_to organization_path(org)} - # end - end - - def autocomplete_search - @project = Project.find(params[:project_id]) - #@flag = params[:flag] || false - respond_to do |format| - format.js - end - end - - def members - if @organization.is_public? || User.current.admin? || User.current.member_of_org?(@organization) - @members = OrgMember.where("organization_id =?", @organization.id) - else - render_403 - end - end - - def more_org_projects - @organization = Organization.find params[:id] - @page = params[:page] - @org_projects = @organization.projects.reorder('created_at').page((params[:page].to_i || 1) +1).per(5) - respond_to do |format| - format.js - end - end - - def more_org_courses - @organization = Organization.find(params[:id]) - @page = params[:page] - @org_courses = @organization.courses.reorder('created_at').page((params[:page].to_i || 1) + 1 ).per(5) - respond_to do |format| - format.js - end - end -end +# encoding: utf-8 +class OrganizationsController < ApplicationController + + + helper :sort + include SortHelper + helper :custom_fields + include CustomFieldsHelper + include AvatarHelper + include WordsHelper + include GitlabHelper + include UserScoreHelper + + include PollHelper + helper :user_score + helper :journals + helper :attachments + # added by liuping 关注 + + helper :watchers + helper :activities + + ### added by william + include ActsAsTaggableOn::TagsHelper + + # fq + helper :words + helper :project_score + helper :issues + include UsersHelper + before_filter :find_organization, :only => [:show, :members, :apply_subdomain] + layout 'base_org' + def index + + end + def new + @organization = Organization.new + render :layout => 'new_base' + end + + def edit + @organization = Organization.find(params[:id]) + end + + def destroy + @organization = Organization.find(params[:id]) + @organization.destroy + respond_to do |format| + format.html{ redirect_to admin_organization_path } + end + end + + def create + @organization = Organization.new + @organization.name = params[:organization][:name] + @organization.description = params[:organization][:description] + @organization.is_public = params[:organization][:is_public] + @organization.allow_guest_download = params[:organization][:allow_guest_download] == '1' ? 1 : 0 + @organization.creator_id = User.current.id + member = OrgMember.new(:user_id => User.current.id) + + @organization.org_members << member + if @organization.save + OrgMemberRole.create(:org_member_id => member.id, :role_id => 11) + redirect_to organization_path(@organization) + end + end + + def show + if @organization.is_public? || User.current.admin? || User.current.member_of_org?(@organization) + @organization = Organization.find(params[:id]) + if params[:org_subfield_id] + @org_subfield = OrgSubfield.find(params[:org_subfield_id]) + @org_subfield_ids = @org_subfield.org_document_comments.map(&:id) << 0 + @org_activities = OrgActivity.where("(org_act_type='OrgDocumentComment'and org_act_id in (#{@org_subfield_ids.join(",")})) || (container_type='OrgSubfield' and container_id=#{@org_subfield.id})").order('updated_at desc').page(params[:page] || 1).per(10) + else + shield_project_ids = ShieldActivity.where("container_type='Organization' and container_id=#{@organization.id} and shield_type='Project'").map(&:shield_id) + shield_course_ids = ShieldActivity.where("container_type='Organization' and container_id=#{@organization.id} and shield_type='Course'").map(&:shield_id) + project_ids = (@organization.projects.map(&:id)-shield_project_ids) << 0 + course_ids = (@organization.courses.map(&:id)-shield_course_ids) << 0 + course_types = "('Message','News','HomeworkCommon','Poll','Course')" + case params[:type] + when nil + @org_activities = OrgActivity.where("(container_id =? and container_type =?) " + + "or (container_type ='Project' and org_act_type in ('Issue','Message','ProjectCreateInfo') and container_id in (#{project_ids.join(',')})) "+ + "or (container_type ='Course' and org_act_type in #{course_types} and container_id in (#{course_ids.join(',')}))", + @organization.id, 'Organization').order('updated_at desc').page(params[:page] || 1).per(10) + when 'project_issue' + @org_activities = OrgActivity.where("container_type = 'Project' and org_act_type = 'Issue' and container_id in (#{project_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10) + when 'project_message' + @org_activities = OrgActivity.where("container_type = 'Project' and org_act_type = 'Message' and container_id in (#{project_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10) + when 'org' + @org_activities = OrgActivity.where("container_id =? and container_type =?",@organization.id, 'Organization').order('updated_at desc').page(params[:page] || 1).per(10) + when 'course_homework' + @org_activities = OrgActivity.where("container_type = 'Course' and org_act_type = 'HomeworkCommon' and container_id in (#{course_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10) + when 'course_news' + @org_activities = OrgActivity.where("container_type = 'Course' and org_act_type = 'News' and container_id in (#{course_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10) + when 'course_message' + @org_activities = OrgActivity.where("container_type = 'Course' and org_act_type = 'Message' and container_id in (#{course_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10) + when 'course_poll' + @org_activities = OrgActivity.where("container_type = 'Course' and org_act_type = 'Poll' and container_id in (#{course_ids.join(',')})").order('updated_at desc').page(params[:page] || 1).per(10) + end + end + @page = params[:page] ? params[:page].to_i : 1 + respond_to do |format| + format.html + format.js + end + else + render_403 + end + end + + def update + @organization = Organization.find(params[:id]) + @organization.name = params[:organization][:name] + @organization.description = params[:organization][:description] + # @organization.domain = params[:organization][:domain] + @organization.is_public = params[:organization][:is_public] == 'on' ? 1 : 0 + @organization.allow_guest_download = params[:organization][:allow_guest_download] == 'on' ? 1 : 0 + #@organization.name = params[:organization][:name] + @organization.save + respond_to do |format| + format.html { redirect_to setting_organization_path(@organization)} + end + end + + def check_uniq + @check = false; + @org_name = params[:org_name].strip + @config_page = params[:config_page] + sameName = @config_page ? Organization.where('name = ? and id != ?',params[:org_name],params[:org_id].to_i).count == 0 : Organization.where('name = ?',params[:org_name]).count == 0 + if sameName == true + @check = true + end + respond_to do |format| + format.js + end + end + + def check_uniq_domain + @is_exist = (Organization.where("domain=?", params[:org_domain]).count > 0) + end + + def find_organization + @organization = Organization.find(params[:id]) + end + + def setting + @organization = Organization.find(params[:id]) + + if User.current.admin? || User.current.admin_of_org?(@organization) + else + render_403 + end + end + + + def clear_org_avatar_temp + + end + + def set_homepage + @org = Organization.find(params[:id]) + @org.home_id = params[:home_id] + @org.save + # respond_to do |format| + # format.html {redirect_to organization_path(org)} + # end + end + + def cancel_homepage + @org = Organization.find(params[:id]) + @org.home_id = nil + @org.save + end + + def autocomplete_search + @project = Project.find(params[:project_id]) + #@flag = params[:flag] || false + respond_to do |format| + format.js + end + end + + def members + if @organization.is_public? || User.current.admin? || User.current.member_of_org?(@organization) + @members = OrgMember.where("organization_id =?", @organization.id) + else + render_403 + end + end + + def more_org_projects + @organization = Organization.find params[:id] + @page = params[:page] + @org_projects = @organization.projects.reorder('created_at').uniq.page((params[:page].to_i || 1) +1).per(5) + respond_to do |format| + format.js + end + end + + def more_org_courses + @organization = Organization.find(params[:id]) + @page = params[:page] + @org_courses = @organization.courses.reorder('created_at').uniq.page((params[:page].to_i || 1) + 1 ).per(5) + respond_to do |format| + format.js + end + end + + def join_course_menu + @organization = Organization.find(params[:id]) + respond_to do |format| + format.js + end + end + + def search_courses + @organization = Organization.find(params[:id]) + condition = '%%' + if !params[:name].nil? + condition = "%#{params[:name].strip}%".gsub(" ","") + end + sql = "select courses.* from courses inner join members on courses.id = members.course_id where members.user_id = #{User.current.id} and courses.name like '#{condition}'"+ + "and courses.id not in (select distinct org_courses.course_id from org_courses where org_courses.organization_id = #{@organization.id})" + #user_courses = Course.find_by_sql(sql) + @courses = Course.find_by_sql(sql) + # @added_course_ids = @organization.courses.map(&:id) + # @courses = [] + # user_courses.each do |course| + # if !@added_course_ids.include?(course.id) + # @courses << course + # end + # end + end + + def join_courses + @organization = Organization.find(params[:id]) + course_ids = params[:courseNames] + course_ids.each do |id| + OrgCourse.create(:organization_id => @organization.id, :course_id => id.to_i, :created_at => Time.now) + end + respond_to do |format| + format.js + end + end + + def join_project_menu + @organization = Organization.find(params[:id]) + respond_to do |format| + format.js + end + end + + def logout + logout_user + respond_to do |format| + format.html {redirect_to organization_path(params[:id])} + end + end + + def search_projects + @organization = Organization.find(params[:id]) + condition = '%%' + if !params[:name].nil? + condition = "%#{params[:name].strip}%".gsub(" ","") + end + sql = "select projects.* from projects inner join members on projects.id = members.project_id where members.user_id = #{User.current.id} and projects.status != 9 and projects.name like '#{condition}'" + + " and projects.id not in (select org_projects.project_id from org_projects where organization_id = #{@organization.id})" + #user_projects = Course.find_by_sql(sql) + @projects = Course.find_by_sql(sql) + # @added_course_ids = @organization.projects.map(&:id) + # @projects = [] + # user_projects.each do |project| + # if !@added_course_ids.include?(project.id) + # @projects << project + # end + # end + end + + def join_projects + @organization = Organization.find(params[:id]) + project_ids = params[:projectNames] + project_ids.each do |id| + OrgProject.create(:organization_id => @organization.id, :project_id => id.to_i, :created_at => Time.now) + end + respond_to do |format| + format.js + end + end + + def hide_org_subfield + @org_subfield = OrgSubfield.find(params[:org_subfield_id]) + @org_subfield.update_attribute(:hide, 1) + end + + def show_org_subfield + @org_subfield = OrgSubfield.find(params[:org_subfield_id]) + @org_subfield.update_attribute(:hide, 0) + end + + def org_resources_subfield + @org = Organization.find(params[:id]) + if params[:send_type].present? and (params[:send_type] == 'news' or params[:send_type] == 'message') + @subfield = @org.org_subfields.where("field_type = 'Post'") + else + @subfield = @org.org_subfields.where('field_type = "Resource" ') + end + respond_to do | format| + format.js + end + end + def apply_subdomain + @organization = Organization.find(params[:id]) + admins = User.where("admin=1") + admins.each do |admin| + OrgMessage.create(:user_id => admin.id, :organization_id => @organization.id, :message_type => 'ApplySubdomain', :message_id => @organization.id, :sender_id => User.current.id, :viewed => 0, :content => params[:domain]) + end + end + + def agree_apply_subdomain + @organization = Organization.find(params[:organization_id]) + OrgMessage.find(params[:act_id]).update_attribute(:viewed, 1) + @organization.update_attribute(:domain, params[:org_domain]) + if OrgMessage.where("message_type='AgreeApplySubdomain' and organization_id=#{@organization.id} and content=?",params[:org_domain]).count == 0 + OrgMessage.create(:user_id => params[:user_id], :organization_id => @organization.id, :message_type => 'AgreeApplySubdomain', :message_id => @organization.id, :sender_id => User.current.id, :viewed => 0, :content => params[:org_domain]) + end + end +end diff --git a/app/controllers/poll_controller.rb b/app/controllers/poll_controller.rb index ec54ae8a4..1d7b4117d 100644 --- a/app/controllers/poll_controller.rb +++ b/app/controllers/poll_controller.rb @@ -2,6 +2,7 @@ class PollController < ApplicationController before_filter :find_poll_and_course, :only => [:edit,:update,:destroy,:show,:statistics_result,:create_poll_question,:commit_poll,:commit_answer,:publish_poll,:republish_poll,:poll_result,:close_poll,:export_poll] before_filter :find_container, :only => [:new,:create, :index] + before_filter :is_logged, :only => [:index, :show, :poll_result,:new,:create,:edit,:update,:destroy,:publish_poll,:republish_poll,:close_poll,:export_poll,:commit_answer,:commit_poll,:statistics_result] before_filter :is_member_of_course, :only => [:index,:show,:poll_result] before_filter :is_course_teacher, :only => [:new,:create,:edit,:update,:destroy,:publish_poll,:republish_poll,:close_poll,:export_poll] include PollHelper @@ -213,6 +214,7 @@ class PollController < ApplicationController def publish_poll @poll.polls_status = 2 @poll.published_at = Time.now + @poll.show_result = params[:show_result] if @poll.save if params[:is_remote] redirect_to poll_index_url(:polls_type => "Course", :polls_group_id => @course.id) @@ -503,6 +505,10 @@ class PollController < ApplicationController end end + def is_logged + redirect_to signin_path unless User.current.logged? + end + def is_member_of_course render_403 unless(@course && (User.current.member_of_course?(@course) || User.current.admin?)) end diff --git a/app/controllers/praise_tread_controller.rb b/app/controllers/praise_tread_controller.rb index acfd785d9..96eeab884 100644 --- a/app/controllers/praise_tread_controller.rb +++ b/app/controllers/praise_tread_controller.rb @@ -5,39 +5,73 @@ class PraiseTreadController < ApplicationController def praise_plus @obj = nil - # @is_in_list = nil + @activity = false + if request.get? @obj_id = params[:obj_id] @obj_type = params[:obj_type] - @horizontal = params[:horizontal].downcase == "false" ? false:true + if !params[:user_activity_id].nil? && !params[:type].nil? + @user_activity_id = params[:user_activity_id] + @type = params[:type] + @activity = true + end + # @is_in_list = nil @obj = find_object_by_type_and_id(@obj_type,@obj_id) - unless @obj.author_id == User.current.id + pts = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",@obj_id,@obj_type.to_s,User.current.id) + unless pts.empty? + respond_to do |format| + format.js + end + return + end + @horizontal = params[:horizontal].downcase == "false" ? false:true if params[:horizontal] + if @obj.respond_to?("author_id") + author_id = @obj.author_id + elsif @obj.respond_to?("user_id") + author_id = @obj.user_id + end + unless author_id == User.current.id praise_tread_plus(@obj_type,@obj_id,1) end - + respond_to do |format| + format.js + end end end def praise_minus @obj = nil + @activity = false if request.get? - @obj = params[:obj] # 传的是对象,最后变成id了 - + #@obj = params[:obj] # 传的是对象,最后变成id了 #首先更新praise_tread 表 删除关注记录 - @pt = PraiseTread.find_by_user_id_and_praise_tread_object_id_and_praise_tread_object_type(User.current.id,@obj,"user") - @pt.delete - + #@pt = PraiseTread.find_by_user_id_and_praise_tread_object_id_and_praise_tread_object_type(User.current.id,@obj,"user") + @obj_id = params[:obj_id] + @obj_type = params[:obj_type] + if !params[:user_activity_id].nil? && !params[:type].nil? + @user_activity_id = params[:user_activity_id] + @type = params[:type] + @activity = true + end + @obj = find_object_by_type_and_id(@obj_type,@obj_id) + @pt = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",@obj_id,@obj_type.to_s,User.current.id).first + if @pt.nil? + respond_to do |format| + format.js + end + return + end + @pt.delete if !@pt.nil? #再更新praise_tread_cache表 使相应的记录减1 当为0时删除 - @ptc = PraiseTreadCache.find_by_object_id(@obj) - @ptc.minus(1) + @ptc = PraiseTreadCache.where("object_id=? and object_type=?",@obj_id,@obj_type.to_s).first + @ptc.minus(1) if !@ptc.nil? if @ptc.praise_num == 0 - @ptc.delete + @ptc.delete end end - @obj = User.find_by_id(@obj) + #@obj = User.find_by_id(@obj) respond_to do |format| - format.html format.js end end @@ -48,7 +82,7 @@ class PraiseTreadController < ApplicationController if request.get? @obj_id = params[:obj_id] @obj_type = params[:obj_type] - @horizontal = params[:horizontal].downcase == "false" ? false:true + #@horizontal = params[:horizontal].downcase == "false" ? false:true @obj = find_object_by_type_and_id(@obj_type,@obj_id) unless @obj.author_id == User.current.id praise_tread_plus(@obj_type,@obj_id,0) @@ -69,20 +103,36 @@ class PraiseTreadController < ApplicationController def find_object_by_type_and_id(type,id) @obj = nil case type - when 'User' - @obj = User.find_by_id(id) - when 'Issue' - @obj = Issue.find_by_id(id) - when 'Project' - @obj = Project.find_by_id(id) - when 'Bid' - @obj = Bid.find_by_id(id) - when 'Contest' - @obj = Contest.find_by_id(id) - when 'Memo' - @obj = Memo.find_by_id(id) - when 'Message' - @obj = Message.find_by_id(id) + when 'Memo' + @obj = Memo.find_by_id(id) + when 'Message' + @obj = Message.find_by_id(id) + when 'HomeworkCommon' + @obj = HomeworkCommon.find_by_id(id) + when 'JournalsForMessage' + @obj = JournalsForMessage.find_by_id(id) + when 'News' + @obj = News.find_by_id(id) + when 'Comment' + @obj = Comment.find_by_id(id) + when 'Journal' + @obj = Journal.find_by_id(id) + when 'BlogComment' + @obj = BlogComment.find_by_id(id) + when 'OrgDocumentComment' + @obj = OrgDocumentComment.find_by_id(id) + when 'User' + @obj = User.find_by_id(id) + when 'Issue' + @obj = Issue.find_by_id(id) + when 'Project' + @obj = Project.find_by_id(id) + when 'Bid' + @obj = Bid.find_by_id(id) + when 'Contest' + @obj = Contest.find_by_id(id) + else + @obj = nil end return @obj end @@ -99,17 +149,14 @@ class PraiseTreadController < ApplicationController # end #再创建或更新praise_tread_cache表 - @ptc = PraiseTreadCache.find_by_object_id_and_object_type(id,type) + #@ptc = PraiseTreadCache.find_by_object_id_and_object_type(id,type) + @ptc = PraiseTreadCache.where("object_id = ? and object_type = ?",id.to_i,type).first @ptc = @ptc.nil? ? PraiseTreadCache.new : @ptc @ptc.object_id = id.to_i @ptc.object_type = type @ptc.save @ptc.plus(flag,1) end - respond_to do |format| - format.html - format.js - end end end diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 715f56d3d..2d1f8d771 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -30,7 +30,7 @@ class ProjectsController < ApplicationController before_filter :find_project, :except => [ :index, :search,:list, :new, :create, :copy, :statistics, :new_join, :course, :enterprise_course, :course_enterprise,:view_homework_attaches,:join_project] before_filter :authorize, :only => [:show, :settings, :edit, :sort_project_members, :update, :modules, :close, :reopen,:view_homework_attaches,:course] before_filter :authorize_global, :only => [:new, :create,:view_homework_attaches] - before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy, :calendar] + before_filter :require_admin, :only => [ :copy, :unarchive, :destroy, :calendar] before_filter :file, :statistics #:watcherlist # 除非项目内人员,不可查看成员, TODO: 完了写报表里去 # before_filter :memberAccess, only: :member @@ -75,17 +75,19 @@ class ProjectsController < ApplicationController if !params[:name].nil? condition = "%#{params[:name].strip}%".gsub(" ","") end - project_org_ids = OrgProject.find_by_sql("select distinct organization_id from org_projects where project_id = #{params[:id]}").map(&:organization_id) - if project_org_ids.empty? - @orgs_not_in_project = Organization.where("(is_public or creator_id =?) = 1 and name like ?",User.current.id, condition).page((params[:page].to_i || 1)).per(10) - @org_count = Organization.where("is_public = 1 or creator_id =?", User.current.id).where("name like ?", condition).count - else - project_org_ids = "(" + project_org_ids.join(',') + ")" - @orgs_not_in_project = Organization.where("id not in #{project_org_ids} and (is_public = 1 or creator_id =?) and name like ?", User.current.id, condition).page((params[:page].to_i || 1)).per(10) - @org_count = Organization.where("id not in #{project_org_ids} and (is_public = 1 or creator_id =?)", User.current.id).where("name like ?", condition).count - end - # @project_count = Project.project_entities.visible.like(params[:name]).page(params[:page]).count - @orgs_page = Paginator.new @org_count, 10,params[:page] + limit = 15 + project_org_ids = OrgProject.find_by_sql("select distinct organization_id from org_projects where project_id = #{params[:id]}").map(&:organization_id) << 0 + @orgs_not_in_project = User.current.organizations.where("organizations.id not in (#{project_org_ids.join(',')}) and organizations.name like ?", condition).page(params[:page].to_i || 1).per(limit) + @org_count = User.current.organizations.where("organizations.id not in (#{project_org_ids.join(',')}) and organizations.name like '#{condition}'").count + # if project_org_ids.empty? + # @orgs_not_in_project = Organization.where("(is_public or creator_id =?) = 1 and name like ?",User.current.id, condition).page((params[:page].to_i || 1)).per(limit) + # @org_count = Organization.where("is_public = 1 or creator_id =?", User.current.id).where("name like ?", condition).count + # else + # project_org_ids = "(" + project_org_ids.join(',') + ")" + # @orgs_not_in_project = Organization.where("id not in #{project_org_ids} and (is_public = 1 or creator_id =?) and name like ?", User.current.id, condition).page((params[:page].to_i || 1)).per(limit) + # @org_count = Organization.where("id not in #{project_org_ids} and (is_public = 1 or creator_id =?)", User.current.id).where("name like ?", condition).count + # end + @orgs_page = Paginator.new @org_count, limit,params[:page] @no_roll_hint = params[:hint_flag] #render :json => {:orgs => @orgs_not_in_project, :count => @org_count}.to_json respond_to do |format| @@ -110,7 +112,6 @@ class ProjectsController < ApplicationController @project_pages = Project.project_entities.visible.like(params[:name]).page(params[:page]).per(10) else @project_pages = Project.project_entities.visible.page(params[:page] ).per(10) - @project_pages = Project.project_entities.visible.page(params[:page] ).per(10) end @projects = @project_pages.order("created_on desc") @limit = 10#per_page_option @@ -201,6 +202,8 @@ class ProjectsController < ApplicationController end end + # 注意:修改该方法的时候注意同步修改forked方法 + # forked方法也会创建项目 def create unless User.current.login? redirect_to signin_url @@ -220,6 +223,11 @@ class ProjectsController < ApplicationController #unless User.current.admin? r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first m = Member.new(:user => User.current, :roles => [r]) + # project's score + if ProjectScore.where("project_id=?", @project.id).first.nil? + ProjectScore.create(:project_id => @project.id, :score => false) + end + # end project_info = ProjectInfo.new(:user_id => User.current.id, :project_id => @project.id) user_grades = UserGrade.create(:user_id => User.current.id, :project_id => @project.id) Rails.logger.debug "UserGrade created: #{user_grades.to_json}" @@ -320,9 +328,20 @@ class ProjectsController < ApplicationController @activity.scope_select {|t| !has["show_#{t}"].nil?} =end + @page = params[:page] ? params[:page].to_i + 1 : 0 # 根据私密性,取出符合条件的所有数据 if User.current.member_of?(@project) || User.current.admin? - @events_pages = ForgeActivity.where("project_id = ? and forge_act_type != ?",@project, "Document" ).order("created_at desc").page(params['page'|| 1]).per(20); + case params[:type] + when nil + @events_pages = ForgeActivity.where("project_id = ? and forge_act_type in ('Issue', 'Message','News', 'ProjectCreateInfo')",@project).order("updated_at desc").limit(10).offset(@page * 10) + when 'issue' + @events_pages = ForgeActivity.where("project_id = ? and forge_act_type = 'Issue'",@project).order("updated_at desc").limit(10).offset(@page * 10) + when 'news' + @events_pages = ForgeActivity.where("project_id = ? and forge_act_type = 'News'",@project).order("updated_at desc").limit(10).offset(@page * 10) + when 'message' + @events_pages = ForgeActivity.where("project_id = ? and forge_act_type = 'Message'",@project).order("updated_at desc").limit(10).offset(@page * 10) + end + #events = @activity.events(@date_from, @date_to) else @events_pages = ForgeActivity.includes(:project).where("forge_activities.project_id = ? and projects.is_public @@ -499,6 +518,14 @@ class ProjectsController < ApplicationController def edit end + def set_public_or_private + @project = Project.find(params[:id]) + if @project.is_public? + @project.update_attribute(:is_public, 0) + else + @project.update_attribute(:is_public, 1) + end + end # by young # include CoursesHelper def member @@ -686,7 +713,11 @@ class ProjectsController < ApplicationController flash[:error] = l(:error_can_not_archive_project) end end - redirect_to admin_projects_url(:status => params[:status]) + if params[:type] == "project" + redirect_to user_path(User.current) + else + redirect_to admin_projects_url(:status => params[:status]) + end end def unarchive diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 7f7b24a34..707e87fd1 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -32,7 +32,7 @@ class RepositoriesController < ApplicationController before_filter :find_project_by_project_id, :only => [:new, :create, :newrepo] before_filter :find_repository, :only => [:edit, :update, :destroy, :committers] - before_filter :find_project_repository, :except => [:new, :create, :newcreate, :edit, :update, :destroy, :committers, :newrepo,:to_gitlab] + before_filter :find_project_repository, :except => [:new, :create, :newcreate, :edit, :update, :destroy, :committers, :newrepo, :to_gitlab, :forked] before_filter :find_changeset, :only => [:revision, :add_related_issue, :remove_related_issue] before_filter :authorize , :except => [:newrepo,:newcreate,:fork, :to_gitlab, :forked] accept_rss_auth :revisions @@ -64,35 +64,67 @@ class RepositoriesController < ApplicationController end def forked - # 被forked的标识如果不满足单个用户唯一性,则不执行fork - if is_sigle_identifier?(User.current, @repository.identifier) - # REDO: 那些人有权限forked项目 - g = Gitlab.client - gproject = g.post ("/projects/fork/#{@project.gpid}?user_id=#{User.current.gid}") - if gproject - copy_project(@project, gproject) - end + @project = Project.find(params[:id]) + @repository = Repository.where("project_id =? and type =?", @project.id, "Repository::Gitlab") + # 如果当前用户已经fork过该项目,不会新fork项目,则跳至已fork的项 + unless has_forked?(@project, User.current) + project = project_from_current_project(@project.id, User.current.id) + redirect_to project_path(project) else - flash[:notice] = l(:project_gitlab_fork_double_message) - redirect_to settings_project_url(@project, :tab => 'repositories') + # 自己不能fork自己的项目 + if User.current.id == @project.user_id + flash[:notice] = l(:project_gitlab_fork_own) + redirect_to repository_url(@repository) + else + g = Gitlab.client + if User.current.gid.nil? + begin + g.sync_user(User.current) + ensure + logger.error "Synv user failed ==>#{User.current.id}" + end + end + gproject = g.fork(@project.gpid, User.current.gid) + if gproject + copy_project(@project, gproject) + forked_count = @project.forked_count.to_i + 1 + @project.update_attributes(:forked_count => forked_count) + end + end end + + end + + # 判断用户是否已经fork过该项目 + def has_forked?(project, user) + projects = Project.where("user_id =?", user) + projects.map(&:forked_from_project_id).detect{|s| s == @project.id}.nil? ? true : false + end + + # 获取当前用户fork过的项目 + def project_from_current_project(project, user) + project = Project.where("user_id =? and forked_from_project_id =?",user, project).first end # copy a project for fork - def copy_project(project, gproject) + def copy_project(tproject, gproject) project = Project.new - project.name = @project.name - project.is_public = @project.is_public - project.status = @project.status - project.description = @project.description - project.hidden_repo = @project.hidden_repo + project.name = tproject.name + project.is_public = tproject.is_public + project.status = tproject.status + project.description = tproject.description + project.hidden_repo = tproject.hidden_repo project.user_id = User.current.id project.project_type = 0 - project.project_new_type = @project.project_new_type + project.project_new_type = tproject.project_new_type project.gpid = gproject.id + project.forked_from_project_id = tproject.id if project.save r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first m = Member.new(:user => User.current, :roles => [r]) + if ProjectScore.where("project_id=?", project.id).first.nil? + ProjectScore.create(:project_id => project.id, :score => false) + end project_info = ProjectInfo.new(:user_id => User.current.id, :project_id => project.id) user_grades = UserGrade.create(:user_id => User.current.id, :project_id => project.id) Rails.logger.debug "UserGrade created: #{user_grades.to_json}" @@ -124,16 +156,16 @@ class RepositoriesController < ApplicationController def copy_repository(project, gproject) # 避免 - if is_sigle_identifier?(project.user_id, gproject.name) + # if is_sigle_identifier?(project.user_id, gproject.name) repository = Repository.factory('Git') repository.project_id = project.id repository.type = 'Repository::Gitlab' repository.url = gproject.name repository.identifier = gproject.name repository = repository.save - else - flash[:notice] = l(:project_gitlab_create_double_message) - end + # else + # flash[:notice] = l(:project_gitlab_create_double_message) + # end end def newrepo @@ -315,38 +347,22 @@ update #Modified by young # (show_error_not_found; return) unless @entries g = Gitlab.client - # count = 0 - # (0..100).each do |page| - # if g.commits(@project.gpid,:page => page).count == 0 - # break - # else - # count = count + g.commits(@project.gpid,:page => page).count - # end - # end - - - #add by hx - if g.commits(@project.gpid , :page=>25).count==0 - count = count_commits(@project.gpid , 0 , 25) - elsif g.commits(@project.gpid , :page=>50).count ==0 - count = count_commits(@project.gpid , 25 , 50)+ 25 * 20 - elsif g.commits(@project.gpid , :page=>75).count ==0 - count = count_commits(@project.gpid , 50 , 75)+ 50 * 20 - elsif g.commits(@project.gpid , :page=>100).count== 0 - count = count_commits(@project.gpid , 75 , 100) + 75 * 20 - elsif g.commits(@project.gpid , :page=>125).count==0 - count = count_commits(@project.gpid , 100 , 125) + 100 * 20 - elsif g.commits(@project.gpid , :page=>150).count==0 - count = count_commits(@project.gpid , 125 , 150) + 125 * 20 - else - count = count_commits(@project.gpid , 150 ,200) + 150 * 20 - end - - @changesets = g.commits(@project.gpid) + @changesets = g.commits(@project.gpid, :ref_name => @rev) # @changesets = @repository.latest_changesets(@path, @rev) # @changesets_count = @repository.latest_changesets(@path, @rev).count - @changesets_all_count = count + @changesets_all_count = @project.gpid.nil? ? 0 : g.project(@project.gpid).commit_count + # 访问该页面的是会后则刷新 + if @project.project_score.nil? + ProjectScore.create(:project_id => @project.id, :score => false) + end + if @changesets_all_count != @project.project_score.changeset_num && @changesets_all_count != 0 + update_commits_count(@project, @changesets_all_count) + end + # end @changesets_latest_coimmit = @changesets[0] + unless @changesets[0].blank? + update_commits_date(@project, @changesets_latest_coimmit) + end @properties = @repository.properties(@path, @rev) @repositories = @project.repositories @course_tag = params[:course] @@ -368,19 +384,6 @@ update alias_method :browse, :show - #add by hx - def count_commits(project_id , left , right) - count = 0 - (left..right).each do |page| - if $g.commits(project_id,:page => page).count == 0 - break - else - count = count + $g.commits(project_id,:page => page).count - end - end - return count - end - def changes @entry = @repository.entry(@path, @rev) (show_error_not_found; return) unless @entry @@ -388,6 +391,10 @@ update limit = 20 #每次页面的换回值从1开始,但是gitlab的页面查询是从0开始,所以先改变page的类型减一在改回来 @commits = g.commits(@project.gpid, page:(params[:page].to_i - 1).to_s) + + #add by hx + #rep_count = commit_count(@project) + #页面传递必须要str类型,但是Paginator的初始化必须要num类型,需要类型转化 @commits_count = params[:commit_count].to_i @commits_pages = Redmine::Pagination::Paginator.new @commits_count,limit,params[:page] @@ -580,6 +587,15 @@ update end private + # 更新项目统计数 + def update_commits_count project, count + project.project_score.update_attribute(:changeset_num, count) + end + + # 更新项目提交次数时间 + def update_commits_date project, date + project.project_score.update_attribute(:commit_time, date.created_at) + end def find_repository @repository = Repository.find(params[:id]) diff --git a/app/controllers/school_controller.rb b/app/controllers/school_controller.rb index d20a58b75..e0aff6254 100644 --- a/app/controllers/school_controller.rb +++ b/app/controllers/school_controller.rb @@ -120,15 +120,16 @@ class SchoolController < ApplicationController condition.scan(/./).each_with_index do |char,index| if char =~ /[a-zA-Z0-9]/ pinyin << char + elsif char =~ /\'/ else chinese << char end end if(condition == '') - @school = School.page((params[:page].to_i || 1) - 1).per(100) + @school = School.reorder('pinyin').page((params[:page].to_i || 1) - 1).per(100) @school_count = School.count else - @school = School.where("name like '%#{chinese.join("")}%' and pinyin like '%#{pinyin.join("")}%'").page((params[:page].to_i || 1) - 1).per(100) + @school = School.where("name like '%#{chinese.join("")}%' and pinyin like '%#{pinyin.join("")}%'").reorder('pinyin').page((params[:page].to_i || 1) - 1).per(100) @school_count = School.where("name like '%#{chinese.join("")}%' and pinyin like '%#{pinyin.join("")}%'").count end diff --git a/app/controllers/shield_activities_controller.rb b/app/controllers/shield_activities_controller.rb new file mode 100644 index 000000000..b2d5191b3 --- /dev/null +++ b/app/controllers/shield_activities_controller.rb @@ -0,0 +1,43 @@ +class ShieldActivitiesController < ApplicationController + def create + if params[:org_id] + if params[:project_id] + ShieldActivity.create(:container_type => 'Organization', :container_id => params[:org_id].to_i, :shield_type => 'Project', :shield_id => params[:project_id].to_i) + elsif params[:course_id] + ShieldActivity.create(:container_type => 'Organization', :container_id => params[:org_id].to_i, :shield_type => 'Course', :shield_id => params[:course_id].to_i) + end + elsif params[:user_id] + if params[:project_id] + ShieldActivity.create(:container_type => 'User', :container_id => params[:user_id].to_i, :shield_type => 'Project', :shield_id => params[:project_id].to_i) + elsif params[:course_id] + ShieldActivity.create(:container_type => 'User', :container_id => params[:user_id].to_i, :shield_type => 'Course', :shield_id => params[:course_id].to_i) + end + end + end + + def show_acts + if params[:org_id] + if params[:project_id] + ShieldActivity.where("container_type='Organization' and container_id=#{params[:org_id].to_i} and shield_type='Project' and shield_id=#{params[:project_id]}").each do |act| + act.destroy + end + # ShieldActivity.create(:container_type => 'Organization', :container_id => params[:org_id].to_i, :shield_type => 'Project', :shield_id => params[:project_id].to_i) + elsif params[:course_id] + ShieldActivity.where("container_type='Organization' and container_id=#{params[:org_id].to_i} and shield_type='Course' and shield_id=#{params[:course_id]}").each do |act| + act.destroy + end + end + elsif params[:user_id] + if params[:project_id] + ShieldActivity.where("container_type='User' and container_id=#{params[:user_id].to_i} and shield_type='Project' and shield_id=#{params[:project_id]}").each do |act| + act.destroy + end + # ShieldActivity.create(:container_type => 'Organization', :container_id => params[:org_id].to_i, :shield_type => 'Project', :shield_id => params[:project_id].to_i) + elsif params[:course_id] + ShieldActivity.where("container_type='User' and container_id=#{params[:user_id].to_i} and shield_type='Course' and shield_id=#{params[:course_id]}").each do |act| + act.destroy + end + end + end + end +end diff --git a/app/controllers/student_work_controller.rb b/app/controllers/student_work_controller.rb index 87d13e169..e0afc38ae 100644 --- a/app/controllers/student_work_controller.rb +++ b/app/controllers/student_work_controller.rb @@ -1,10 +1,11 @@ class StudentWorkController < ApplicationController layout "base_courses" include StudentWorkHelper + include ApplicationHelper require 'bigdecimal' require "base64" - before_filter :find_homework, :only => [:new, :index, :create, :student_work_absence_penalty, :absence_penalty_list, :evaluation_list, :program_test,:set_score_rule,:forbidden_anonymous_comment] - before_filter :find_work, :only => [:edit, :update, :show, :destroy, :add_score, :praise_student_work] + before_filter :find_homework, :only => [:new, :index, :create, :student_work_absence_penalty, :absence_penalty_list, :evaluation_list, :program_test,:set_score_rule,:forbidden_anonymous_comment,:delete_work,:new_student_work_project,:student_work_project,:cancel_relate_project,:search_course_students] + before_filter :find_work, :only => [:edit, :update, :show, :destroy, :add_score, :praise_student_work,:retry_work,:revise_attachment] before_filter :member_of_course, :only => [:index, :new, :create, :show, :add_score, :praise_student_work] before_filter :author_of_work, :only => [:edit, :update, :destroy] before_filter :teacher_of_course, :only => [:student_work_absence_penalty, :absence_penalty_list, :evaluation_list, :set_score_rule, :forbidden_anonymous_comment] @@ -76,7 +77,7 @@ class StudentWorkController < ApplicationController end ################################################################################################################## @order,@b_sort,@name,@group = params[:order] || "score",params[:sort] || "desc",params[:name] || "",params[:group] - @homework_commons = @course.homework_commons.order("created_at desc") + @homework_commons = @course.homework_commons.where("publish_time <= ?",Time.now.strftime("%Y-%m-%d")).order("created_at desc") @is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin? @is_evaluation = @homework.homework_detail_manual && @homework.homework_detail_manual.comment_status == 2 && !@is_teacher #是不是匿评 @show_all = false @@ -95,12 +96,27 @@ class StudentWorkController < ApplicationController @stundet_works = search_homework_member @homework.student_works.select("student_works.*,IF(final_score is null,null,final_score - absence_penalty - late_penalty) as score").joins(:user).where("users.id in #{student_in_group}").order("#{@order} #{@b_sort}"),@name @show_all = true elsif @homework.homework_detail_manual.comment_status == 1 #学生 && 未开启匿评 只看到自己的 - @stundet_works = @homework.student_works.select("student_works.*,IF(final_score is null,null,final_score - absence_penalty - late_penalty) as score").where(:user_id => User.current.id) + if @homework.homework_type == 3 + pro = @homework.student_work_projects.where(:user_id => User.current.id).first + @stundet_works = @homework.student_works.select("student_works.*,IF(final_score is null,null,final_score - absence_penalty - late_penalty) as score").where(:id => pro.student_work_id) + else + @stundet_works = @homework.student_works.select("student_works.*,IF(final_score is null,null,final_score - absence_penalty - late_penalty) as score").where(:user_id => User.current.id) + end elsif @homework.homework_detail_manual.comment_status == 2 #学生 && 开启匿评 看到匿评列表 - my_work = @homework.student_works.where(:user_id => User.current.id) + if @homework.homework_type == 3 + pro = @homework.student_work_projects.where(:user_id => User.current.id).first + my_work = @homework.student_works.where(:id => pro.student_work_id) + else + my_work = @homework.student_works.where(:user_id => User.current.id) + end @stundet_works = my_work + User.current.student_works_evaluation_distributions.map(&:student_work).select { |work| work.homework_common_id == @homework.id} elsif @homework.homework_detail_manual.comment_status == 3 #学生 && 关闭匿评 未提交作品之前列表为空,提交了作品看到所有的 - my_work = @homework.student_works.select("student_works.*,IF(final_score is null,null,final_score - absence_penalty - late_penalty) as score").where(:user_id => User.current.id) + if @homework.homework_type == 3 + pro = @homework.student_work_projects.where(:user_id => User.current.id).first + my_work = @homework.student_works.select("student_works.*,IF(final_score is null,null,final_score - absence_penalty - late_penalty) as score").where(:id => pro.student_work_id) + else + my_work = @homework.student_works.select("student_works.*,IF(final_score is null,null,final_score - absence_penalty - late_penalty) as score").where(:user_id => User.current.id) + end if my_work.empty? @stundet_works = [] else @@ -116,12 +132,27 @@ class StudentWorkController < ApplicationController @stundet_works = search_homework_member @homework.student_works.select("student_works.*,IF(final_score is null,null,final_score - absence_penalty - late_penalty) as score").order("#{@order} #{@b_sort}"),@name @show_all = true elsif @homework.homework_detail_manual.comment_status == 1 #学生 && 未开启匿评 只看到自己的 - @stundet_works = @homework.student_works.select("student_works.*,IF(final_score is null,null,final_score - absence_penalty - late_penalty) as score").where(:user_id => User.current.id) + if @homework.homework_type == 3 + pro = @homework.student_work_projects.where(:user_id => User.current.id).first + @stundet_works = @homework.student_works.select("student_works.*,IF(final_score is null,null,final_score - absence_penalty - late_penalty) as score").where(:id => pro.student_work_id) + else + @stundet_works = @homework.student_works.select("student_works.*,IF(final_score is null,null,final_score - absence_penalty - late_penalty) as score").where(:user_id => User.current.id) + end elsif @homework.homework_detail_manual.comment_status == 2 #学生 && 开启匿评 看到匿评列表 - my_work = @homework.student_works.where(:user_id => User.current.id) + if @homework.homework_type == 3 + pro = @homework.student_work_projects.where(:user_id => User.current.id).first + my_work = @homework.student_works.where(:id => pro.student_work_id) + else + my_work = @homework.student_works.where(:user_id => User.current.id) + end @stundet_works = my_work + User.current.student_works_evaluation_distributions.map(&:student_work).select { |work| work.homework_common_id == @homework.id} elsif @homework.homework_detail_manual.comment_status == 3 #学生 && 关闭匿评 未提交作品之前列表为空,提交了作品看到所有的 - my_work = @homework.student_works.select("student_works.*,IF(final_score is null,null,final_score - absence_penalty - late_penalty) as score").where(:user_id => User.current.id) + if @homework.homework_type == 3 + pro = @homework.student_work_projects.where(:user_id => User.current.id).first + my_work = @homework.student_works.select("student_works.*,IF(final_score is null,null,final_score - absence_penalty - late_penalty) as score").where(:id => pro.student_work_id) + else + my_work = @homework.student_works.select("student_works.*,IF(final_score is null,null,final_score - absence_penalty - late_penalty) as score").where(:user_id => User.current.id) + end if my_work.empty? @stundet_works = [] else @@ -168,9 +199,15 @@ class StudentWorkController < ApplicationController def create # 提交作品前先判断是否已经提交 + @has_commit = false; if hsd_committed_work?(User.current.id, @homework.id) - flash[:notice] = l(:notice_successful_create) - redirect_to edit_student_work_url(params[:student_work]) + @work = StudentWork.where("user_id =? and homework_common_id =?", User.current.id, @homework.id).first + @has_commit = true; + #flash[:notice] = l(:notice_successful_create) + #redirect_to edit_student_work_url(params[:student_work]) + respond_to do |format| + format.js + end return end if params[:student_work] @@ -179,11 +216,14 @@ class StudentWorkController < ApplicationController student_work ||= StudentWork.new student_work.name = params[:student_work][:name] student_work.description = params[:student_work][:description] - student_work.project_id = params[:student_work][:project_id] student_work.homework_common_id = @homework.id student_work.user_id = User.current.id student_work.save_attachments(params[:attachments]) render_attachment_warning_if_needed(student_work) + if @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 1 + @student_work_project = @homework.student_work_projects.where("user_id = #{User.current.id}").first + student_work.project_id = @student_work_project.project_id + end #提交作品时,计算是否迟交 if Time.parse(@homework.end_time.to_s).strftime("%Y-%m-%d") < Time.parse(Time.now.to_s).strftime("%Y-%m-%d") student_work.late_penalty = @homework.late_penalty @@ -191,16 +231,42 @@ class StudentWorkController < ApplicationController student_work.late_penalty = 0 end if student_work.save - course_activity = CourseActivity.where("course_act_type='HomeworkCommon' and course_act_id =#{@homework.id}").first - if course_activity - course_activity.updated_at = Time.now - course_activity.save - end - user_activity = UserActivity.where("act_type='HomeworkCommon' and act_id =#{@homework.id}").first - if user_activity - user_activity.updated_at = Time.now - user_activity.save + if @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 1 + @student_work_project.student_work_id = student_work.id + @student_work_project.save + members = params[:group_member_ids].split(',') + for i in 1 .. members.count-1 + stu_project = StudentWorkProject.new + stu_project.homework_common_id = @homework.id + stu_project.student_work_id = student_work.id + stu_project.project_id = @student_work_project.project_id + stu_project.user_id = members[i].to_i + stu_project.is_leader = 0 + stu_project.save + end + elsif @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 0 + members = params[:group_member_ids].split(',') + for i in 0 .. members.count-1 + stu_project = StudentWorkProject.new + stu_project.homework_common_id = @homework.id + stu_project.student_work_id = student_work.id + stu_project.project_id = -1 + stu_project.user_id = members[i].to_i + if i == 0 + stu_project.is_leader = 1 + else + stu_project.is_leader = 0 + end + stu_project.save + end end + update_course_activity(@homework.class,@homework.id) + update_user_activity(@homework.class,@homework.id) + update_org_activity(@homework.class,@homework.id) + + course_message = CourseMessage.new(:user_id =>User.current.id,:content=>"new",:course_message_id=>student_work.id,:course_id => @course.id,:course_message_type=>"StudentWork", :status => 9) #作品提交记录 + course_message.save + @student_work = StudentWork.where(homework_common_id: @homework.id, user_id: User.current.id).first respond_to do |format| format.js @@ -216,7 +282,7 @@ class StudentWorkController < ApplicationController def edit @user = User.current - if !User.current.admin? && @homework.homework_type == 2 #编程作业不能修改作业 + if (!User.current.admin? && @homework.homework_type == 2) || Time.parse(@homework.end_time.to_s).strftime("%Y-%m-%d") < Time.now.strftime("%Y-%m-%d") #编程作业不能修改作业|| 截止日期已到不能修改作业 render_403 else respond_to do |format| @@ -230,17 +296,31 @@ class StudentWorkController < ApplicationController @submit_result = true @work.name = params[:student_work][:name] @work.description = params[:student_work][:description] - @work.project_id = params[:student_work][:project] @work.save_attachments(params[:attachments]) render_attachment_warning_if_needed(@work) if @work.save -=begin - respond_to do |format| - format.html { - flash[:notice] = l(:notice_successful_edit) - redirect_to student_work_index_url(:homework => @homework.id) - } -=end + if @homework.homework_type == 3 + @student_work_project = @homework.student_work_projects.where("user_id=?",User.current.id).first + student_work_projects = @homework.student_work_projects.where("student_work_id=? and is_leader =?",@work.id,0) + student_work_projects.delete_all + members = params[:group_member_ids].split(',') + for i in 1 .. members.count-1 + stu_project = StudentWorkProject.new + stu_project.homework_common_id = @homework.id + stu_project.student_work_id = @work.id + if @homework.homework_detail_group.base_on_project == 1 + stu_project.project_id = @student_work_project.project_id + else @homework.homework_detail_group.base_on_project == 0 + stu_project.project_id = -1 + end + stu_project.user_id = members[i].to_i + stu_project.is_leader = 0 + stu_project.save + end + end + + course_message = CourseMessage.new(:user_id =>User.current.id,:content=>"edit",:course_message_id=>@work.id,:course_id => @course.id,:course_message_type=>"StudentWork", :status => 9) #作品提交记录 + course_message.save @student_work = @work respond_to do |format| format.js @@ -270,6 +350,18 @@ class StudentWorkController < ApplicationController def destroy if @work.destroy + if @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 1 + pros = @work.student_work_projects.where("is_leader = 0") + pros.each do |pro| + pro.destroy + end + project = @work.student_work_projects.where("is_leader = 1").first + project.update_attributes(:student_work_id => nil) + elsif @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 0 + @work.student_work_projects.each do |pro2| + pro2.destroy + end + end respond_to do |format| format.html { redirect_to student_work_index_url(:homework => @homework.id) @@ -278,6 +370,47 @@ class StudentWorkController < ApplicationController end end + def delete_work + @work = StudentWork.where("user_id =? and homework_common_id =?", User.current.id, @homework.id).first + if @work + @work.destroy + if @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 1 + pros = @work.student_work_projects.where("is_leader = 0") + pros.each do |pro| + pro.destroy + end + project = @work.student_work_projects.where("is_leader = 1").first + project.update_attributes(:student_work_id => nil) + elsif @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 0 + @work.student_work_projects.each do |pro2| + pro2.destroy + end + end + end + redirect_to user_homeworks_user_path(User.current.id) + end + + def retry_work + if @work.destroy + if @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 1 + pros = @work.student_work_projects.where("is_leader = 0") + pros.each do |pro| + pro.destroy + end + project = @work.student_work_projects.where("is_leader = 1").first + project.update_attributes(:student_work_id => nil) + elsif @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 0 + @work.student_work_projects.each do |pro2| + pro2.destroy + end + end + @student_work = StudentWork.new + respond_to do |format| + format.js + end + end + end + #添加评分,已评分则为修改评分 def add_score @is_last = params[:is_last] == "true" @@ -477,8 +610,9 @@ class StudentWorkController < ApplicationController if params[:student_path] redirect_to student_work_index_url(:homework => @homework.id) else - @user_activity_id = params[:user_activity_id] - @is_in_course = params[:is_in_course] + @user_activity_id = params[:user_activity_id].to_i + @is_in_course = params[:is_in_course].to_i + @course_activity = params[:course_activity].to_i respond_to do |format| format.js end @@ -504,7 +638,108 @@ class StudentWorkController < ApplicationController @course_activity = params[:course_activity].to_i end + def revise_attachment + Attachment.attach_filesex(@work, params[:attachments], params[:attachment_type]) + revise_attachments = @work.attachments.where("attachtype = 7").reorder("created_on desc") + if revise_attachments.count == 2 + revise_attachments.last.destroy + end + attachment = @work.attachments.where("attachtype = 7").first + attachment.update_attributes(:description => params[:description]) + respond_to do |format| + format.js + end + end + + def new_student_work_project + @user_activity_id = params[:user_activity_id].to_i + @is_in_course = params[:is_in_course].to_i + @course_activity = params[:course_activity].to_i + respond_to do |format| + format.js + end + end + + #创建作业的关联项目 + def student_work_project + @project = StudentWorkProject.new + @project.homework_common_id = @homework.id + @project.project_id = (Project.find params[:projectName].to_i).id + @project.user_id = User.current.id + @project.is_leader = 1 + if @project.save + @user_activity_id = params[:user_activity_id].to_i + @is_in_course = params[:is_in_course].to_i + @course_activity = params[:course_activity].to_i + respond_to do |format| + format.js + end + end + + end + + #查找学生创建的项目列表 + def search_user_projects + condition = '%%' + if !params[:name].nil? + condition = "%#{params[:name].strip}%".gsub(" ","") + end + @project_ids = Project.visible.where("user_id = #{User.current.id} and name like '#{condition}'") + @first = params[:first].to_i + respond_to do |format| + format.js + end + end + + #查找课程的学生 + def search_course_students + name = "" + unless params[:name].nil? + name = params[:name] + end + if @homework.homework_detail_group.base_on_project == 0 + all_student_ids = "(" + @homework.course.student.map{|student| student.student_id}.join(",") + ")" + else + pro = Project.find @homework.student_work_projects.where("user_id=?",User.current.id).first.project_id + all_student_ids = "(" + pro.members.map{|member| member.user_id}.join(",") + ")" + end + all_students = User.where("id in #{all_student_ids}") + @commit_student_ids = @homework.student_work_projects.map{|student| student.user_id} + @users = searchstudent_by_name all_students,name + respond_to do |format| + format.js + end + end + + def cancel_relate_project + relate_pro = StudentWorkProject.where("user_id = #{User.current.id} and homework_common_id = #{@homework.id}").first + if relate_pro.destroy + @user_activity_id = params[:user_activity_id].to_i + @is_in_course = params[:is_in_course].to_i + @course_activity = params[:course_activity].to_i + respond_to do |format| + format.js + end + end + end + private + def searchstudent_by_name users, name + mems = [] + if name != "" + name = name.to_s.downcase + users.each do |m| + username = m.lastname.to_s.downcase + m.firstname.to_s.downcase + if(m.login.to_s.downcase.include?(name) || m.user_extensions[:student_id].to_s.downcase.include?(name) || username.include?(name)) + mems << m + end + end + else + mems = users + end + mems + end + def hsd_committed_work?(user, homework) sw = StudentWork.where("user_id =? and homework_common_id =?", user, homework).first sw.nil? ? result = false : result = true @@ -701,7 +936,7 @@ class StudentWorkController < ApplicationController #成绩计算 def set_final_score homework,student_work if homework && homework.homework_detail_manual - if homework.homework_type == 1 #匿评作业 + if homework.homework_type != 2 #匿评作业 if homework.teacher_priority == 1 #教师优先 if student_work.teacher_score student_work.final_score = student_work.teacher_score diff --git a/app/controllers/subfield_subdomain_dirs_controller.rb b/app/controllers/subfield_subdomain_dirs_controller.rb new file mode 100644 index 000000000..90c6a7a92 --- /dev/null +++ b/app/controllers/subfield_subdomain_dirs_controller.rb @@ -0,0 +1,10 @@ +class SubfieldSubdomainDirsController < ApplicationController + def update + @org_subfield = OrgSubfield.find(params[:org_subfield_id]) + if @org_subfield.subfield_subdomain_dir + @org_subfield.subfield_subdomain_dir.update_attribute(:name, :params[:sub_dir_name]) + else + SubfieldSubdomainDir.create(:org_subfield_id => @org_subfield.id, :name => params[:sub_dir_name]) + end + end +end diff --git a/app/controllers/system_messages_controller.rb b/app/controllers/system_messages_controller.rb index 684378e8a..df17ca3e2 100644 --- a/app/controllers/system_messages_controller.rb +++ b/app/controllers/system_messages_controller.rb @@ -1,4 +1,5 @@ class SystemMessagesController < ApplicationController + # before_filter :message_author, :only => [:show] # # def message_author @@ -41,6 +42,7 @@ class SystemMessagesController < ApplicationController @system_messages.description = params[:system_message][:description] @system_messages.subject = params[:system_message][:subject] @system_messages.user_id = User.current.id + # @system_messages.save_attachments(params[:attachments]) respond_to do |format| if @system_messages.save format.html {redirect_to user_system_messages_path(User.current)} diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index cb61177f5..507dada24 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -227,6 +227,18 @@ class TagsController < ApplicationController @tag_list = get_course_tag_list @course @select_tag_name = params[:select_tag_name] end + + if @obj && @object_flag == '6' && @obj.container.kind_of?(Project) + @project = @obj.container + @tag_list = get_course_tag_list @project + @select_tag_name = params[:select_tag_name] + end + + if @obj && @object_flag == '6' && @obj.container.kind_of?(OrgSubfield) + @org_subfield = @obj.container + @tag_list = get_org_subfield_tag_list @org_subfield + @select_tag_name = params[:select_tag_name] + end # end end end @@ -314,6 +326,168 @@ class TagsController < ApplicationController end end + def update_project_tag_name + @tag_name = params[:tagName] + @rename_tag_name = params[:renameName] + @taggable_id = params[:taggableId] + @taggable_type = numbers_to_object_type(params[:taggableType]) + @project_id = params[:projectId] + + @rename_tag = (ActsAsTaggableOn::Tag.find_by_name(@rename_tag_name)) #查找重命名后的tag + @tag_id = (ActsAsTaggableOn::Tag.find_by_name(@tag_name)).id #重命名前的tag_id + @taggings = ActsAsTaggableOn::Tagging.find_by_tag_id_and_taggable_id_and_taggable_type(@tag_id,@taggable_id,@taggable_type) unless @taggable_id.blank? + @obj = get_object(@taggable_id,params[:taggableType]) unless @taggable_id.blank? + if @taggable_id.blank? #如果没有传tag_id,那么直接更新tag_name就好了。但是要防止 重命名后的tag存在。 + if @project_id + project = Project.find @project_id + if project + project.attachments.each do |attachment| + taggings = ActsAsTaggableOn::Tagging.find_by_tag_id_and_taggable_id_and_taggable_type(@tag_id,attachment.id,attachment.class) + if taggings + taggings.delete + attachment.tag_list.add(@rename_tag_name.split(",")) + attachment.save + end + end + end + end + else + if(@rename_tag.nil?) #这次命名的是新的tag + + # 是否还有其他记录 引用了 tag_id + @tagging = ActsAsTaggableOn::Tagging.where("tag_id = #{@tag_id}") + # 如果taggings表中记录为 1 ,那么改变@tag_id对应的tag的名字 + if @tagging.count == 1 + @tag = ActsAsTaggableOn::Tag.find_by_id(@tag_id) + @tag.update_attributes({:name=>@rename_tag_name}) + else #如果tagging表中的记录大于1,那么就要新增tag记录 + + unless @obj.nil? + @obj.tag_list.add(@rename_tag_name.split(",")) + @obj.save + end + #删除原来的对应的taggings的记录 + unless @taggings.nil? + @taggings.delete + end + end + else #这是已有的tag + # 更改taggings记录里的tag_id + unless @taggings.nil? + @taggings.update_attributes({:tag_id=>@rename_tag.id}) + end + end + end + + @obj_flag = params[:taggableType] + if @obj && @obj_flag == '6' && @obj.container.kind_of?(Project) + @project = @obj.container + @tag_list = @tag_list = get_course_tag_list @project + elsif @project_id + @project = Project.find(@project_id) + @tag_list = get_project_tag_list @project + + #这里要引用FilesController里的逻辑了。将资源库当前的文件列表刷新一遍。 + @flag = params[:flag] || false + sort = "" + @sort = "" + @order = "" + @is_remote = false + @isproject = false + + sort = "#{Attachment.table_name}.created_on desc" + + @containers = [ Project.includes(:attachments).reorder(sort).find(@project.id)] + + show_attachments @containers + elsif @obj && @obj_flag == '5' + @forum = @obj + end + respond_to do |format| + format.js + end + end + + def update_org_subfield_tag_name + @tag_name = params[:tagName] + @rename_tag_name = params[:renameName] + @taggable_id = params[:taggableId] + @taggable_type = numbers_to_object_type(params[:taggableType]) + @rename_tag = (ActsAsTaggableOn::Tag.find_by_name(@rename_tag_name)) #查找重命名后的tag + @tag_id = (ActsAsTaggableOn::Tag.find_by_name(@tag_name)).id #重命名前的tag_id + @taggings = ActsAsTaggableOn::Tagging.find_by_tag_id_and_taggable_id_and_taggable_type(@tag_id,@taggable_id,@taggable_type) unless @taggable_id.blank? + @obj = get_object(@taggable_id,params[:taggableType]) unless @taggable_id.blank? + if @taggable_id.blank? #如果没有传tag_id,那么直接更新tag_name就好了。但是要防止 重命名后的tag存在。 + if params[:org_subfield_id] + org_subfield = OrgSubfield.find params[:org_subfield_id] + if org_subfield + org_subfield.attachments.each do |attachment| + taggings = ActsAsTaggableOn::Tagging.find_by_tag_id_and_taggable_id_and_taggable_type(@tag_id,attachment.id,attachment.class) + if taggings + taggings.delete + attachment.tag_list.add(@rename_tag_name.split(",")) + attachment.save + end + end + end + end + else + if(@rename_tag.nil?) #这次命名的是新的tag + + # 是否还有其他记录 引用了 tag_id + @tagging = ActsAsTaggableOn::Tagging.where("tag_id = #{@tag_id}") + # 如果taggings表中记录为 1 ,那么改变@tag_id对应的tag的名字 + if @tagging.count == 1 + @tag = ActsAsTaggableOn::Tag.find_by_id(@tag_id) + @tag.update_attributes({:name=>@rename_tag_name}) + else #如果tagging表中的记录大于1,那么就要新增tag记录 + + unless @obj.nil? + @obj.tag_list.add(@rename_tag_name.split(",")) + @obj.save + end + #删除原来的对应的taggings的记录 + unless @taggings.nil? + @taggings.delete + end + end + else #这是已有的tag + # 更改taggings记录里的tag_id + unless @taggings.nil? + @taggings.update_attributes({:tag_id=>@rename_tag.id}) + end + end + end + + @obj_flag = params[:taggableType] + if @obj && @obj_flag == '6' && @obj.container.kind_of?(OrgSubfield) + @org_subfield = @obj.container + @tag_list = @tag_list = get_org_subfield_tag_list @org_subfield + elsif params[:org_subfield_id] + @org_subfield = OrgSubfield.find(params[:org_subfield_id]) + @tag_list = get_org_subfield_tag_list @org_subfield + + #这里要引用FilesController里的逻辑了。将资源库当前的文件列表刷新一遍。 + @flag = params[:flag] || false + sort = "" + @sort = "" + @order = "" + @is_remote = false + @isproject = false + + sort = "#{Attachment.table_name}.created_on desc" + + @containers = [ OrgSubfield.includes(:attachments).reorder(sort).find(@org_subfield.id)] + + show_attachments @containers + elsif @obj && @obj_flag == '5' + @forum = @obj + end + respond_to do |format| + format.js + end + end + def show_attachments obj @attachments = [] obj.each do |container| @@ -372,6 +546,14 @@ class TagsController < ApplicationController @course = @obj.container @tag_list = @tag_list = get_course_tag_list @course end + if @obj && @obj_flag == '6' && @obj.container.kind_of?(Project) + @project = @obj.container + @tag_list = @tag_list = get_project_tag_list @project + end + if @obj && @obj_flag == '6' && @obj.container.kind_of?(OrgSubfield) + @org_subfield = @obj.container + @tag_list = @tag_list = get_org_subfield_tag_list @org_subfield + end respond_to do |format| format.js format.html diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 776e6b7d5..625a1de74 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -55,6 +55,7 @@ class UsersController < ApplicationController before_filter :recorded_visitor, :only => [:show,:user_fanslist,:user_watchlist,:user_visitorlist] helper :sort + helper :attachments include SortHelper helper :custom_fields include CustomFieldsHelper @@ -63,6 +64,7 @@ class UsersController < ApplicationController include GitlabHelper include UserScoreHelper + include PollHelper helper :user_score helper :journals @@ -91,8 +93,13 @@ class UsersController < ApplicationController end end + # 更新issue状态 + def user_act_issue_assign_to + + end + # 用户消息 - # 说明: homework 发布作业;message:讨论区; news:新闻; poll:问卷;works_reviewers:作品评阅;works_reply:作品回复 + # 说明: homework 发布作业;message:讨论区; news:新闻; poll:问卷;works_reviewers:作品评阅;works_reply:作品回复,exercise:课程测验 # issue:问题;journal:缺陷状态更新; forum:公共贴吧: user_feedback: 用户留言; new_reply:新闻回复(comment) def user_messages if !User.current.logged? @@ -128,7 +135,7 @@ class UsersController < ApplicationController #课程相关消息 when 'homework' - @message_alls = CourseMessage.where("course_message_type in ('HomeworkCommon','StudentWorksScore','JournalsForMessage','StudentWork') and user_id =?", @user).order("created_at desc") + @message_alls = CourseMessage.where("course_message_type in ('HomeworkCommon','StudentWorksScore','JournalsForMessage','StudentWork','Exercise') and user_id =?", @user).order("created_at desc") when 'course_message' @message_alls = CourseMessage.where("course_message_type =? and user_id =?", "Message", @user).order("created_at desc") when 'course_news' @@ -185,11 +192,13 @@ class UsersController < ApplicationController forge_querys = ForgeMessage.where("user_id =? and viewed =?", user, 0) user_querys = UserFeedbackMessage.where("user_id =? and viewed =?", user, 0) forum_querys = MemoMessage.where("user_id =? and viewed =?", user, 0) + org_querys = OrgMessage.where("user_id=? and viewed=0", user) if User.current.id == @user.id course_querys.update_all(:viewed => true) forge_querys.update_all(:viewed => true) user_querys.update_all(:viewed => true) forum_querys.update_all(:viewed => true) + org_querys.update_all(:viewed => true) end end @@ -396,8 +405,47 @@ class UsersController < ApplicationController #导入作业 def user_import_homeworks + @user = User.current @select_course = params[:select_course] ? 1 : 0 - @user_homeworks = HomeworkCommon.where(:user_id => @user.id).order("created_at desc") + #@user_homeworks = HomeworkCommon.where(:user_id => @user.id).order("created_at desc") + visible_course = Course.where("is_public = 1 && is_delete = 0") + visible_course_ids = visible_course.empty? ? "(-1)" : "(" + visible_course.map{|course| course.id}.join(",") + ")" + @homeworks = HomeworkCommon.where("course_id in #{visible_course_ids} and publish_time <= '#{Date.today}'").order("created_at desc") + @type = params[:type] + @limit = 15 + @is_remote = true + @hw_count = @homeworks.count + @hw_pages = Paginator.new @hw_count, @limit, params['page'] || 1 + @offset ||= @hw_pages.offset + @homeworks = paginateHelper @homeworks,15 + respond_to do |format| + format.js + end + end + + def user_homework_type + @user = User.current + if(params[:type].blank? || params[:type] == "1") #公共题库 + visible_course = Course.where("is_public = 1 && is_delete = 0") + visible_course_ids = visible_course.empty? ? "(-1)" : "(" + visible_course.map{|course| course.id}.join(",") + ")" + @homeworks = HomeworkCommon.where("course_id in #{visible_course_ids} and publish_time <= '#{Date.today}'").order("created_at desc") + elsif params[:type] == "2" #我的题库 + @homeworks = HomeworkCommon.where("user_id = #{@user.id} and publish_time <= '#{Date.today}'").order("created_at desc") + end + @type = params[:type] + @limit = 15 + @is_remote = true + @hw_count = @homeworks.count + @hw_pages = Paginator.new @hw_count, @limit, params['page'] || 1 + @offset ||= @hw_pages.offset + @homeworks = paginateHelper @homeworks,15 + respond_to do |format| + format.js + end + end + + def show_homework_detail + @homework = HomeworkCommon.find params[:homework].to_i respond_to do |format| format.js end @@ -405,7 +453,22 @@ class UsersController < ApplicationController #用户主页过滤作业 def user_search_homeworks - @user_homeworks = HomeworkCommon.where("user_id = '#{@user.id}' and lower(name) like '%#{params[:name].to_s.downcase}%'").order("created_at desc") + @user = User.current + search = params[:name].to_s.strip.downcase + if(params[:type].blank? || params[:type] == "1") #全部 + visible_course = Course.where("is_public = 1 && is_delete = 0") + visible_course_ids = visible_course.empty? ? "(-1)" : "(" + visible_course.map{|course| course.id}.join(",") + ")" + @homeworks = HomeworkCommon.where("course_id in #{visible_course_ids} and publish_time <= '#{Date.today}' and (name like '%#{search}%')").order("created_at desc") + 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 + @type = params[:type] + @limit = 15 + @is_remote = true + @hw_count = @homeworks.count + @hw_pages = Paginator.new @hw_count, @limit, params['page'] || 1 + @offset ||= @hw_pages.offset + @homeworks = paginateHelper @homeworks,15 respond_to do |format| format.js end @@ -415,9 +478,11 @@ class UsersController < ApplicationController def user_select_homework homework = HomeworkCommon.find_by_id params[:checkMenu] homework_detail_programing = homework.homework_detail_programing + homework_detail_group = homework.homework_detail_group @homework = HomeworkCommon.new @select_course = params[:select_course] || 0 if homework + @ref_homework = homework @homework.name = homework.name @homework.description = homework.description @homework.end_time = homework.end_time @@ -444,6 +509,14 @@ class UsersController < ApplicationController ) end end + + if homework_detail_group + @homework.homework_detail_group = HomeworkDetailGroup.new + @homework_detail_group = @homework.homework_detail_group + @homework_detail_group.min_num = homework_detail_group.min_num + @homework_detail_group.max_num = homework_detail_group.max_num + @homework_detail_group.base_on_project = homework_detail_group.base_on_project + end end respond_to do |format| format.js @@ -488,7 +561,7 @@ class UsersController < ApplicationController #提交作品时,计算是否迟交 if Time.parse(homework.end_time.to_s).strftime("%Y-%m-%d") < Time.parse(Time.now.to_s).strftime("%Y-%m-%d") - student_work.late_penalty = 1 + student_work.late_penalty = homework.late_penalty else student_work.late_penalty = 0 end @@ -513,6 +586,7 @@ class UsersController < ApplicationController else homework.publish_time = params[:homework_common][:publish_time] end + homework.anonymous_comment = params[:homework_common][:anonymous_comment] ? params[:homework_common][:anonymous_comment] : 0 homework.homework_type = params[:homework_type].to_i || 1 homework.late_penalty = 10 homework.teacher_priority = 1 @@ -551,19 +625,30 @@ class UsersController < ApplicationController ) end end + end + #分组作业 + if homework.homework_type == 3 + homework_detail_group = HomeworkDetailGroup.new + homework.homework_detail_group = homework_detail_group + homework_detail_group.min_num = params[:min_num].to_i + homework_detail_group.max_num = params[:max_num].to_i + homework_detail_group.base_on_project = params[:base_on_project].to_i end if homework.save homework_detail_manual.save if homework_detail_manual homework_detail_programing.save if homework_detail_programing - + homework_detail_group.save if homework_detail_group + if params[:quotes] && !params[:quotes].blank? + homework = HomeworkCommon.find params[:quotes].to_i + homework.update_attribute(:quotes, homework.quotes+1) + end if params[:is_in_course] == "1" redirect_to homework_common_index_path(:course => homework.course_id) else redirect_to user_homeworks_user_path(User.current.id) end - end end else @@ -600,7 +685,7 @@ class UsersController < ApplicationController end 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','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) "+ + @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} @@ -725,14 +810,28 @@ class UsersController < ApplicationController end end # end - jours = @user.journals_for_messages.where('m_parent_id IS NULL').order('created_on DESC') + @page = params[:page] ? params[:page].to_i + 1 : 0 + if params[:type].present? + case params[:type] + when "public" + jours = @user.journals_for_messages.where('m_parent_id IS NULL and private = 0').order('updated_on DESC') + when "private" + jours = @user.journals_for_messages.where('m_parent_id IS NULL and private = 1').order('updated_on DESC') + else + jours = @user.journals_for_messages.where('m_parent_id IS NULL').order('updated_on DESC') + end + else + jours = @user.journals_for_messages.where('m_parent_id IS NULL').order('updated_on DESC') + end + @jour_count = jours.count + @jour = jours.limit(10).offset(@page * 10) + @type = params[:type] if User.current == @user jours.update_all(:is_readed => true, :status => false) jours.each do |journal| fetch_user_leaveWord_reply(journal).update_all(:is_readed => true, :status => false) end end - @jour = paginateHelper jours,10 @state = false render :layout=>'new_base_user' end @@ -856,7 +955,7 @@ class UsersController < ApplicationController #显示更多用户课程 def user_courses4show @page = params[:page].to_i + 1 - @courses = @user.courses.visible.select("courses.*,(SELECT MAX(created_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS a").order("a desc").limit(5).offset(@page * 5) + @courses = @user.courses.visible.where("is_delete =?", 0).select("courses.*,(SELECT MAX(created_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS a").order("a desc").limit(5).offset(@page * 5) end #显示更多用户项目 @@ -946,9 +1045,11 @@ class UsersController < ApplicationController params[:course_id], 'JoinCourseRequest', User.current.id, @user.id, false) join_course_messages.update_all(:viewed => true) end + shield_project_ids = ShieldActivity.where("container_type='User' and container_id=#{@user.id} and shield_type='Project'").map(&:shield_id) + shield_course_ids = ShieldActivity.where("container_type='User' and container_id=#{@user.id} and shield_type='Course'").map(&:shield_id) @page = params[:page] ? params[:page].to_i + 1 : 0 - user_project_ids = @user.projects.visible.empty? ? "(-1)" : "(" + @user.projects.visible.map{|project| project.id}.join(",") + ")" - user_course_ids = @user.courses.visible.empty? ? "(-1)" : "(" + @user.courses.visible.map{|course| course.id}.join(",") + ")" + user_project_ids = (@user.projects.visible.map{|project| project.id}-shield_project_ids).empty? ? "(-1)" : "(" + (@user.projects.visible.map{|project| project.id}-shield_project_ids).join(",") + ")" + user_course_ids = (@user.courses.visible.map{|course| course.id}-shield_course_ids).empty? ? "(-1)" : "(" + (@user.courses.visible.map{|course| course.id}-shield_course_ids).join(",") + ")" course_types = "('Message','News','HomeworkCommon','Poll','Course')" project_types = "('Message','Issue','ProjectCreateInfo')" principal_types = "JournalsForMessage" @@ -1308,12 +1409,12 @@ class UsersController < ApplicationController 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','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) "+ + @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','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) " + + "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" #课程资源 @@ -1367,15 +1468,15 @@ class UsersController < ApplicationController end end - if(params[:type].blank? || params[:type] == "1") #全部 + 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','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) "+ + @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','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) " + + "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" #课程资源 @@ -1423,12 +1524,14 @@ class UsersController < ApplicationController def search_user_course @user = User.current if !params[:search].nil? - @course = @user.courses.where(" #{Course.table_name}.id = #{params[:search].to_i } or #{Course.table_name}.name like '%#{params[:search.to_s]}%'") + 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)} else @course = @user.courses .select { |course| @user.allowed_to?(:as_teacher,course)} end + @search = params[:search] #这里仅仅是传递需要发送的资源id @send_id = params[:send_id] @send_ids = params[:checkbox1] || params[:send_ids] @@ -1441,10 +1544,12 @@ class UsersController < ApplicationController def search_user_project @user = User.current if !params[:search].nil? - @projects = @user.projects.where(" #{Project.table_name}.id = #{params[:search].to_i } or #{Project.table_name}.name like '%#{params[:search.to_s]}%'") + search = "%#{params[:search].to_s.strip.downcase}%" + @projects = @user.projects.where(" #{Project.table_name}.id = #{params[:search].to_i } or #{Project.table_name}.name like :p",:p=>search) else @projects = @user.projects end + @search = params[:search] #这里仅仅是传递需要发送的资源id @send_id = params[:send_id] @send_ids = params[:checkbox1] || params[:send_ids] #搜索的时候 和 直接 用表格提交的时候的send_ids @@ -1466,11 +1571,23 @@ class UsersController < ApplicationController unless course_ids.nil? course_ids.each do |id| 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 + 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 = 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 if attach_copied_obj.attachtype == nil attach_copied_obj.attachtype = 4 end @@ -1490,11 +1607,23 @@ class UsersController < ApplicationController unless course_ids.nil? course_ids.each do |id| 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 + 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 = 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 if attach_copied_obj.attachtype == nil attach_copied_obj.attachtype = 4 end @@ -1507,7 +1636,7 @@ class UsersController < ApplicationController @flag = false end user_course_ids = User.current.courses.map { |c| c.id} #我的资源库的话,那么应该是我上传的所有资源 加上 我加入的课程的所有资源 - @attachments = Attachment.where("(author_id = #{params[:id]} and container_type in('Project','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) "+ + @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") @type = params[:type] @limit = 25 @@ -1536,15 +1665,31 @@ class UsersController < ApplicationController unless project_ids.nil? project_ids.each do |project_id| next if ori.blank? + @exist = false + Project.find(project_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 = Project.find(project_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 attach_copied_obj.save + unless Project.find(project_id).project_score.nil? + Project.find(project_id).project_score.update_attribute(:attach_num, + Project.find(project_id).project_score.attach_num + 1) + end end end elsif params[:send_ids].present? @@ -1559,15 +1704,30 @@ class UsersController < ApplicationController unless project_ids.nil? project_ids.each do |project_id| next if ori.blank? + @exist = false + Project.find(project_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 = Project.find(project_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 attach_copied_obj.save + unless Project.find(project_id).project_score.nil? + Project.find(project_id).project_score.update_attribute(:attach_num, Project.find(project_id).project_score.attach_num + 1) + end end end end @@ -1575,7 +1735,7 @@ class UsersController < ApplicationController @flag=true end user_course_ids = User.current.courses.map { |c| c.id} #我的资源库的话,那么应该是我上传的所有资源 加上 我加入的课程的所有资源 - @attachments = Attachment.where("(author_id = #{params[:id]} and container_type in('Project','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) "+ + @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") @type = params[:type] @limit = 25 @@ -1591,6 +1751,187 @@ class UsersController < ApplicationController end end + def add_exist_file_to_org + @flag = true + if params[:send_id].present? + send_id = params[:send_id] + subfield_id = params[:subfield] + if subfield_id.nil? + @flag = false + 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 + 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 + attach_copied_obj.save + end + end + elsif params[:send_ids].present? + send_ids = params[:send_ids].split(" ") + subfield_id = params[:subfield] + if subfield_id.nil? + @flag = false + end + send_ids.each do |send_id| + + 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 + attach_copied_obj.save + end + end + else + @flag=true + end + 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") + @type = params[:type] + @limit = 25 + @user = User.current + @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,25 + respond_to do |format| + format.js + end + end + + def share_news_to_course + news = News.find(params[:send_id]) + course_ids = params[:course_ids] + course_ids.each do |course_id| + if Course.find(course_id).news.map(&:id).exclude?(news.id) + course_news = News.create(:course_id => course_id.to_i, :title => news.title, :summary => news.summary, :description => news.description,:author_id => User.current.id, :created_on => Time.now,:project_id => -1) + 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) + end + end + end + end + + def share_news_to_project + news = News.find(params[:send_id]) + project_ids = params[:project_ids] + project_ids.each do |project_id| + project = Project.find(project_id) + if project.news.map(&:id).exclude?(news.id) + message = Message.create(:board_id => project.boards.first.id, :subject => news.title, :content => news.description, :author_id => User.current.id) + 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) + end + end + end + end + + def share_news_to_org + news = News.find(params[:send_id]) + field_id = params[:subfield] + org_news = News.create(:org_subfield_id => field_id.to_i, :title => news.title, :summary => news.summary, :description => news.description,:author_id => User.current.id, :created_on => Time.now,:project_id => -1) + 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) + 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 + + def share_message_to_course + @message = Message.find(params[:send_id]) + course_ids = params[:course_ids] + course_ids.each do |course_id| + course = Course.find(course_id) + if course.news.map(&:id).exclude?(@message.id) + message = Message.create(:board_id => course.boards.first.id, :subject => @message.subject, :content => @message.content, :author_id => User.current.id) + @message.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) + end + end + end + end + + def share_message_to_project + @message = Message.find(params[:send_id]) + project_ids = params[:project_ids] + project_ids.each do |project_id| + project = Project.find(project_id) + if project.news.map(&:id).exclude?(@message.id) + message = Message.create(:board_id => project.boards.first.id, :subject => @message.subject, :content => @message.content, :author_id => User.current.id) + @message.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) + end + end + end + end + + def share_message_to_org + field_id = params[:subfield] + @message = Message.find(params[:send_id]) + @message.quotes = @message.quotes.nil? ? 1 : (@message.quotes + 1) + @message.save + board = OrgSubfield.find(field_id).boards.first + mes = Message.create(:board_id => board.id, :subject => @message.subject, :content => @message.content, :author_id => User.current.id) + @message.attachments.each do |attach| + mes.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) + end + OrgActivity.create(:container_type => 'OrgSubfield', :container_id => field_id.to_i, :org_act_type=>'Message', :org_act_id => mes.id, :user_id => User.current.id) + end + + def change_org_subfield + + end + # 资源预览 def resource_preview preview_id = params[:resource_id] @@ -1803,12 +2144,12 @@ class UsersController < ApplicationController 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','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) "+ + @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','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) " + + "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" #课程资源 @@ -1828,9 +2169,9 @@ class UsersController < ApplicationController end 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") + @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')").order("created_on desc") + @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 elsif params[:type] == "5" #用户资源 if User.current.id.to_i == params[:id].to_i @@ -1855,46 +2196,46 @@ class UsersController < ApplicationController # 根据资源关键字进行搜索 def resource_search - search = params[:search].to_s.strip.downcase - if(params[:type].nil? || params[:type] == "1") #全部 + search = "%#{params[:search].strip.downcase}%" + 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','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 (filename like '%#{search}%') ").order("created_on desc") + @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(',')}))) and (filename like :p) ",:p=>search).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 in" + " ('Project','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 (filename like '%#{search}%') ").order("created_on desc") + " and (filename like :p) ",:p=>search).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(',')})) and (filename like '%#{search}%') ").order("created_on desc") + @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(',')})) and (filename like :p) ",:p=>search).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(',')})) )"+ - " and (filename like '%#{search}%') ").order("created_on desc") + " and (filename like :p) ",:p=>search).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' and (filename like '%#{search}%')").order("created_on desc") + @attachments = Attachment.where("author_id = #{params[:id]} and container_type = 'Project' and (filename like :p)",:p=>search).order("created_on desc") else - @attachments = Attachment.where("author_id = #{params[:id]} and is_public = 1 and container_type = 'Project' and (filename like '%#{search}%') ").order("created_on desc") + @attachments = Attachment.where("author_id = #{params[:id]} and is_public = 1 and container_type = 'Project' and (filename like :p) ",:p=>search).order("created_on desc") end 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('Project','Issue','Document','Message','News','StudentWorkScore','HomewCommon') and (filename like '%#{search}%')").order("created_on desc") + @attachments = Attachment.where("author_id = #{params[:id]} and container_type in('Project','Issue','Document','Message','News','StudentWorkScore','HomewCommon') and (filename like :p)",:p=>search).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') and (filename like '%#{search}%')").order("created_on desc") + @attachments = Attachment.where("author_id = #{params[:id]} and is_public = 1 and container_type in('Issue','Document','Message','News','StudentWorkScore','HomewCommon') and (filename like :p)",:p=>search).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' and (filename like '%#{search}%')").order("created_on desc") + @attachments = Attachment.where("author_id = #{params[:id]} and container_type = 'Principal' and (filename like :p)",:p=>search).order("created_on desc") else - @attachments = Attachment.where("author_id = #{params[:id]} and is_public = 1 and container_type = 'Principal' and (filename like '%#{search}%')").order("created_on desc") + @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 end @type = params[:type] @@ -1932,6 +2273,31 @@ class UsersController < ApplicationController end end + def search_user_org + @user = User.current + if !params[:search].nil? #发送到有栏目类型为资源的组织中 + search = "%#{params[:search].to_s.strip.downcase}%" + if params[:send_type].present? and (params[:send_type] == 'news' or params[:send_type] == 'message') + @orgs = @user.organizations.where("name like ?", search).select{|org| OrgSubfield.where("organization_id = #{org.id} and field_type='Post'").count > 0} + else + @orgs = @user.organizations.where("name like ?", search).select{|org| OrgSubfield.where("organization_id = #{org.id} and field_type='Resource'").count > 0} + end + else + if params[:send_type].present? and (params[:send_type] == 'news' or params[:send_type] == 'message') + @orgs = @user.organizations.select{|org| OrgSubfield.where("organization_id = #{org.id} and field_type='Post'").count > 0} + else + @orgs = @user.organizations.select{|org| OrgSubfield.where("organization_id = #{org.id} and field_type='Resource'").count > 0} + end + end + @search = params[:search] + #这里仅仅是传递需要发送的资源id + @send_id = params[:send_id] + @send_ids = params[:checkbox1] || params[:send_ids] + respond_to do |format| + format.js + end + end + private def find_user diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 784066378..1508fc871 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -151,36 +151,113 @@ class WelcomeController < ApplicationController end def search - search_condition = params[:q] - search_type = params[:search_type].to_sym unless search_condition.blank? - search_by = params[:search_by] + @name = params[:q] + @search_type = params[:search_type] + case params[:search_type] + when 'all' + @alls = Elasticsearch::Model.search({ + query: { + multi_match: { + query: @name, + type:"most_fields", + operator: "or", + fields: ['login', 'firstname','lastname','name','description^0.5','filename','subject','content^0.5'] + } + }, + highlight: { + pre_tags: [''], + post_tags: [''], + fields: { + login: {}, + firstname: {}, + lastname: {}, + name:{}, + description:{}, + filename:{}, + subject:{}, + content:{} + } + } + },[User,Course,Attachment,Project,Memo] ).page(params[:page] || 1).per(20).results + when 'user' + @users = User.search(@name).page(params[:page] || 1).per(20) + when 'project' + @projects = Project.search(@name).page(params[:page] || 1).per(20).results + when 'course' + @courses = Course.search(@name).page(params[:page] || 1).per(20).results + when 'attachment' + @attachments = Attachment.search(@name).page(params[:page] || 1).per(20).results + when 'memo' + @memos = Memo.search(@name).page(params[:page] || 1).per(20).results + else + @alls = Elasticsearch::Model.search({ + query: { + multi_match: { + query: @name, + type:"most_fields", + operator: "or", + fields: ['login', 'firstname','lastname','name','description^0.5','filename','subject','content^0.5'] + } + }, + highlight: { + pre_tags: [''], + post_tags: [''], + fields: { + login: {}, + firstname: {}, + lastname: {}, + name:{}, + description:{}, + filename:{}, + subject:{}, + content:{} + } + } + },[User,Course,Attachment,Project,Memo] ).page(params[:page] || 1).per(20).results - if search_type.nil? && params[:contests_search] && params[:name] != "" - search_type = :contests - search_condition = params[:name] end + @users_count = User.search(@name).results.total + + @course_count = Course.search(@name).results.total + @attach_count = Attachment.search(@name).results.total + @project_count = Project.search(@name).results.total + @memo_count = Memo.search(@name).results.total + @total_count = Elasticsearch::Model.search({ + query: { + multi_match: { + query: @name, + type:"most_fields", + operator: "or", + fields: ['login', 'firstname','lastname','name','description^0.5','filename','subject','content^0.5'] + } + }, + highlight: { + pre_tags: [''], + post_tags: [''], + fields: { + login: {}, + firstname: {}, + lastname: {}, + name:{}, + description:{}, + filename:{}, + subject:{}, + content:{} + } + } + },[User,Course,Attachment,Project,Memo] ).results.total + # search_type = params[:search_type].to_sym unless search_condition.blank? + # search_by = params[:search_by] + # + # if search_type.nil? && params[:contests_search] && params[:name] != "" + # search_type = :contests + # search_condition = params[:name] + # end + respond_to do |format| - format.html{ - case search_type - when :projects - redirect_to projects_search_url(:name => search_condition, - :project_type => Project::ProjectType_project) - when :courses - redirect_to courses_search_url(:name => search_condition) - when :contests - redirect_to contests_url(:name => search_condition) - when :users - redirect_to users_search_url(:name => search_condition,:search_by => search_by) - when :users_teacher - redirect_to users_search_url(:name => search_condition, :search_by => search_by, :role => :teacher) - when :users_student - redirect_to users_search_url(:name => search_condition, :search_by => search_by, :role => :student) - else - #redirect_to home_path, :alert => l(:label_sumbit_empty) - (redirect_to signin_path, :notice => l(:label_sumbit_empty);return) #if params[:name].blank? - end - } + format.js + format.html{ render :layout=>'users_base'} end end diff --git a/app/controllers/words_controller.rb b/app/controllers/words_controller.rb index 6cc622c49..351be4e64 100644 --- a/app/controllers/words_controller.rb +++ b/app/controllers/words_controller.rb @@ -3,6 +3,8 @@ class WordsController < ApplicationController include ApplicationHelper before_filter :find_user, :only => [:new, :create, :destroy, :more, :back] + before_filter :require_login, :only => [:create_reply] + def create if params[:new_form][:user_message].size>0 && User.current.logged? unless params[:user_id].nil? @@ -55,21 +57,12 @@ class WordsController < ApplicationController @jfm = add_reply_adapter options @save_succ = true if @jfm.errors.empty? if @save_succ - course_activity = CourseActivity.where("course_act_type='JournalsForMessage' and course_act_id =#{parent_id}").first - if course_activity - course_activity.updated_at = Time.now - course_activity.save - end - principal_activity = PrincipalActivity.where("principal_act_type='JournalsForMessage' and principal_act_id =#{parent_id}").first - if principal_activity - principal_activity.updated_at = Time.now - principal_activity.save - end - user_activity = UserActivity.where("act_type='JournalsForMessage' and act_id =#{parent_id}").first - if user_activity - user_activity.updated_at = Time.now - user_activity.save - end + update_course_activity('JournalsForMessage',parent_id) + update_user_activity('JournalsForMessage',parent_id) + update_forge_activity('JournalsForMessage',parent_id) + update_org_activity('JournalsForMessage',parent_id) + update_principal_activity('JournalsForMessage',parent_id) + (JournalsForMessage.find parent_id).update_attribute(:updated_on,Time.now) end respond_to do |format| # format.html { @@ -81,9 +74,9 @@ class WordsController < ApplicationController # render 'test/index' # } format.js { - @reply_type = params[:reply_type] @user_activity_id = params[:user_activity_id] @activity = JournalsForMessage.find(parent_id) + @is_activity = params[:is_activity] } end @@ -102,6 +95,18 @@ class WordsController < ApplicationController @user = User.find(@journal_destroyed.jour_id) @jours_count = @user.journals_for_messages.where('m_parent_id IS NULL').count @is_user = true + @user_activity_id = params[:user_activity_id] if params[:user_activity_id] + @is_activity = params[:is_activity].to_i if params[:is_activity] + @activity = @journal_destroyed.parent if @journal_destroyed.parent + elsif @journal_destroyed.jour_type == 'HomeworkCommon' + @homework = HomeworkCommon.find @journal_destroyed.jour_id + if params[:user_activity_id] + @user_activity_id = params[:user_activity_id] + else + @user_activity_id = -1 + end + @is_in_course = params[:is_in_course].to_i + @course_activity = params[:course_activity].to_i end respond_to do |format| format.js @@ -224,8 +229,12 @@ class WordsController < ApplicationController def leave_user_message if User.current.logged? @user = User.find(params[:id]) - if params[:new_form][:user_message].size>0 && User.current.logged? && @user - @user.add_jour(User.current, params[:new_form][:user_message]) + if params[:new_form][:content].size>0 && User.current.logged? && @user + if params[:private] && params[:private] == '1' + @user.journals_for_messages << JournalsForMessage.new(:user_id => User.current.id, :notes => params[:new_form][:content], :reply_id => 0, :status => true, :is_readed => false, :private => 1) + else + @user.add_jour(User.current, params[:new_form][:content]) + end end redirect_to feedback_path(@user) else @@ -263,16 +272,9 @@ class WordsController < ApplicationController update_kindeditor_assets_owner ids,feedback[:id],OwnerTypeHelper::JOURNALSFORMESSAGE end - course_activity = CourseActivity.where("course_act_type='HomeworkCommon' and course_act_id =#{@homework_common.id}").first - if course_activity - course_activity.updated_at = Time.now - course_activity.save - end - user_activity = UserActivity.where("act_type='HomeworkCommon' and act_id =#{@homework_common.id}").first - if user_activity - user_activity.updated_at = Time.now - user_activity.save - end + update_course_activity(@homework_common.class,@homework_common.id) + update_user_activity(@homework_common.class,@homework_common.id) + update_org_activity(@homework_common.class,@homework_common.id) respond_to do |format| format.js{ @user_activity_id = params[:user_activity_id] diff --git a/app/controllers/zipdown_controller.rb b/app/controllers/zipdown_controller.rb index 1a01ac97b..d66c6a30d 100644 --- a/app/controllers/zipdown_controller.rb +++ b/app/controllers/zipdown_controller.rb @@ -1,4 +1,8 @@ -require 'zip' +#coding=utf-8 + +require "base64" +require 'zip' + class ZipdownController < ApplicationController #查找项目(课程) before_filter :find_project_by_bid_id, :only => [:assort] @@ -11,7 +15,13 @@ class ZipdownController < ApplicationController def download if User.current.logged? begin - send_file "#{OUTPUT_FOLDER}/#{params[:file]}", :filename => filename_for_content_disposition(params[:filename]), :type => detect_content_type(params[:file]) + if params[:base64file] + file = Base64.decode64(params[:base64file]) + file = file.sub('*', '+') + send_file "#{OUTPUT_FOLDER}/#{file}", :filename => filename_for_content_disposition(file), :type => detect_content_type(file) + else + send_file "#{OUTPUT_FOLDER}/#{params[:file]}", :filename => filename_for_content_disposition(params[:filename]), :type => detect_content_type(params[:file]) + end rescue => e render file: 'public/no_file_found.html' end @@ -107,11 +117,17 @@ class ZipdownController < ApplicationController bid_homework_path, OUTPUT_FOLDER) } [{files:[out_file.file_path], count: 1, index: 1, - real_file: out_file.file_path, file: File.basename(out_file.file_path), + real_file: out_file.file_path, + file: File.basename(out_file.file_path), + base64file: encode64(File.basename(out_file.file_path)), size:(out_file.pack_size / 1024.0 / 1024.0).round(2) }] end + def encode64(str) + Base64.encode64(str).sub('+', '*') + end + def zip_homework_common homework_common bid_homework_path = [] digests = [] @@ -129,7 +145,9 @@ class ZipdownController < ApplicationController bid_homework_path, OUTPUT_FOLDER) } [{files:[out_file.file_path], count: 1, index: 1, - real_file: out_file.file_path, file: File.basename(out_file.file_path), + real_file: out_file.file_path, + file: File.basename(out_file.file_path), + base64file: encode64(File.basename(out_file.file_path)), size:(out_file.pack_size / 1024.0 / 1024.0).round(2) }] end diff --git a/app/helpers/api_helper.rb b/app/helpers/api_helper.rb index f73cc87e6..8324951f9 100644 --- a/app/helpers/api_helper.rb +++ b/app/helpers/api_helper.rb @@ -180,7 +180,7 @@ module ApiHelper if timePassed < 0 updateAtValue = "时间有问题" elsif timePassed < ONE_MINUTE - updateAtValue = "一分钟前" + updateAtValue = "1分钟前" elsif timePassed < ONE_HOUR timeIntoFormat = timePassed / ONE_MINUTE updateAtValue = timeIntoFormat.to_s + "分钟前" diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index f4e0f88ff..9d08be378 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -48,6 +48,28 @@ module ApplicationHelper end end end + + def link_to_user_version(version, options = {}) + return '' unless version && version.is_a?(Version) + link_to_if version.visible?, format_version_name(version), { :controller => 'versions', :action => 'show', :id => version }, :class => "linkBlue" + end + + # 判断课程是否为精品课程 + def is_excellent_course course + (course.is_excellent? or course.excellent_option?) ? true : false + end + + # 判断课程对成员是否可见 + def visible_course?(course) + (course.is_delete? or (!course.is_public? && !User.current.member_of_course?(course))) ? false : true + end + + # 获取项目/课程总分 + # 发布缺陷 4分 回复缺陷 1分 提交一次 4分 讨论帖子 2分 回复帖子 1分 发布新闻 1分 + def static_project_score obj + score = obj.issue_num * 4 + obj.issue_journal_num + (obj.changeset_num||0) * 4 + obj.board_num * 2 + obj.board_message_num + obj.news_num + obj.attach_num * 5 + end + # 获取组织成员中文名字 def get_org_member_role_name member case member.roles[0].name @@ -58,6 +80,19 @@ module ApplicationHelper end end + # 判断组织左侧展开或者隐藏 + def is_hide_org_left obj + if obj.nil? + return true + else + if obj.hide == 0 + return true + else + return false + end + end + end + # Time 2015-03-24 16:38:05 # Author lizanle # Description after save后需要进行资源记录的更新 @@ -75,6 +110,50 @@ module ApplicationHelper end end + # 更新课程英雄榜得分 + # user传过来必须是学生 + 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 "JournalForMessage" + 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 => 1, :journal_reply_num => 0, :total_score => 1) + else + score = course_contributor_score.journal_num + 1 + total_score = course_contributor_score.total_score + 1 + course_contributor_score.update_attributes(:journal_num => score, :total_score => total_score) + end + when "Message" + if course_contributor_score.nil? + CourseContributorScore.create(:course_id => course_id, :user_id => user_id, :message_num => 2, :message_reply_num => 0, + :news_reply_num => 0, :resource_num => 0, :journal_num => 0, :journal_reply_num => 0, :total_score => 2) + else + score = course_contributor_score.message_num + 2 + total_score = course_contributor_score.total_score + 2 + course_contributor_score.update_attributes(:message_num => score, :total_score => total_score) + end + when "MessageReply" + if course_contributor_score.nil? + CourseContributorScore.create(:course_id => course_id, :user_id => user_id, :message_num => 0, :message_reply_num => 1, + :news_reply_num => 0, :resource_num => 0, :journal_num => 0, :journal_reply_num => 0, :total_score => 1) + else + score = course_contributor_score.message_reply_num + 1 + total_score = course_contributor_score.total_score + 1 + course_contributor_score.update_attributes(:message_reply_num => score, :total_score => total_score) + end + when "NewReply" + 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 => 1, :resource_num => 0, :journal_num => 0, :journal_reply_num => 0, :total_score => 1) + else + score = course_contributor_score.news_reply_num + 1 + total_score = course_contributor_score.total_score + 1 + course_contributor_score.update_attributes(:news_reply_num => score, :total_score => total_score) + end + end + end + # Added by young # Define the course menu's link class # 不是数组的转化成数组,然后判断当前menu_item是否在给定的列表 @@ -209,8 +288,10 @@ module ApplicationHelper subject = truncate(subject, :length => 60) end end - if issue.status_id == 5 + if issue.status_id == 3 s = link_to text, issue_path(issue), :class => "text_line_s", :title => title + elsif issue.status_id == 5 + s = link_to text, issue_path(issue), :class => "text_line_s del_line", :title => title else s = link_to text, issue_path(issue), :class => "c_blue", :title => title end @@ -624,6 +705,42 @@ module ApplicationHelper return rep.blank? ? true :false end + # 获取Gitlab版本库提交总数 + def commit_count(project) + g = Gitlab.client + #add by hx + if g.commits(project.gpid , :page=>200).count > 0 + count = 4020 + elsif g.commits(project.gpid , :page=>25).count==0 + count = count_commits(project.gpid , 0 , 25) + elsif g.commits(project.gpid , :page=>50).count ==0 + count = count_commits(project.gpid , 25 , 50)+ 25 * 20 + elsif g.commits(project.gpid , :page=>75).count ==0 + count = count_commits(project.gpid , 50 , 75)+ 50 * 20 + elsif g.commits(project.gpid , :page=>100).count== 0 + count = count_commits(project.gpid , 75 , 100) + 75 * 20 + elsif g.commits(project.gpid , :page=>125).count==0 + count = count_commits(project.gpid , 100 , 125) + 100 * 20 + elsif g.commits(project.gpid , :page=>150).count==0 + count = count_commits(project.gpid , 125 , 150) + 125 * 20 + else + count = count_commits(project.gpid , 150 ,200) + 150 * 20 + end + end + + #add by hx + def count_commits(project_id , left , right) + count = 0 + (left..right).each do |page| + if $g.commits(project_id,:page => page).count == 0 + break + else + count = count + $g.commits(project_id,:page => page).count + end + end + return count + end + # 获取单一gitlab项目 def gitlab_repository(project) rep = Repository.where("project_id =? and type =?", project.id,"Repository::Gitlab" ).first @@ -857,7 +974,17 @@ module ApplicationHelper first_page = FirstPage.find_by_page_type('project') if args.empty? title = @html_title || [] - title << @project.name if @project + if @project + title << @project.name + elsif @course + title << @course.name + elsif @organization + title << @organization.name + elsif @user + title << @user.login + else + title << User.current.login + end if first_page.nil? || first_page.web_title.nil? title << Setting.app_title unless Setting.app_title == title.last else @@ -1672,6 +1799,13 @@ module ApplicationHelper # def javascript_include_tag(*sources) options = sources.last.is_a?(Hash) ? sources.pop : {} + + @sources ||= [] + sources = sources.delete_if do|source| + @sources.include?(source) + end + @sources += sources + if plugin = options.delete(:plugin) sources = sources.map do |source| if plugin @@ -1681,7 +1815,12 @@ module ApplicationHelper end end end - super sources, options + + if sources && !sources.empty? + super(sources, options) + else + '' + end end def content_for(name, content = nil, &block) @@ -1825,6 +1964,23 @@ module ApplicationHelper s end + def get_user_identity identity + s = "" + case identity + when 0 + s = '教师' + when 1 + s = '学生' + when 2 + s = '组织' + when 3 + s= '开发者' + else + s = '学生' + end + s + end + def get_memo @new_memo = Memo.new @public_forum = Forum.find(1) rescue ActiveRecord::RecordNotFound @@ -1841,6 +1997,18 @@ module ApplicationHelper courses_doing end + def attachment_history_candown attachment_history + if attachment_history.container_type == "Course" + course = Course.find(attachment_history.container_id) + candown = User.current.member_of?(course) || (course.is_public && attachment_history.is_public == 1) + elsif attachment_history.container_type == "Project" + project = Project.find(attachment_history.container_id) + candown = User.current.member_of?(project) || (project.is_public && attachment_history.is_public == 1) + elsif attachment_history.container_type == "OrgSubfield" + org = OrgSubfield.find(attachment_history.container_id) + candown = User.current.member_of_org?(org) || (org.organization.is_public && attachment_history.is_public == 1) + end + end def attachment_candown attachment candown = false @@ -1863,6 +2031,10 @@ module ApplicationHelper elsif attachment.container.is_a?(Course) course = attachment.container candown= User.current.member_of_course?(course) || (course.is_public==1 && attachment.is_public == 1) + elsif attachment.container.is_a?(OrgSubfield) + candown = true + elsif attachment.container.is_a?(OrgDocumentComment) + candown = true elsif (attachment.container.has_attribute?(:board) || attachment.container.has_attribute?(:board_id)) && attachment.container.board && attachment.container.board.course course = attachment.container.board.course @@ -1873,6 +2045,10 @@ module ApplicationHelper candown = true elsif attachment.container.class.to_s=="StudentWork" candown = true + elsif attachment.container.class.to_s=="BlogComment" #博客资源允许下载 + candown = true + elsif attachment.container.class.to_s=="Memo" #论坛资源允许下载 + candown = true elsif attachment.container.class.to_s == "User" candown = (attachment.is_public == 1 || attachment.is_public == true || attachment.author_id == User.current.id) elsif attachment.container_type == "Bid" && attachment.container && attachment.container.courses @@ -2315,6 +2491,25 @@ module ApplicationHelper tag_list end + # 获取项目资源的Tag云 + def get_project_tag_list project + all_attachments = project.attachments.select{|attachment| attachment.is_public? || + (attachment.container_type == "Project" && User.current.member_of?(project))|| + attachment.author_id == User.current.id + } + tag_list = attachment_tag_list all_attachments + tag_list + end + + def get_org_subfield_tag_list org_subfield + all_attachments = org_subfield.attachments.select{|attachment| attachment.is_public? || + (attachment.container_type == "OrgSubfield" && User.current.member_of_org?(org_subfield.organization))|| + attachment.author_id == User.current.id + } + tag_list = attachment_tag_list all_attachments + tag_list + end + #获取匿评相关连接代码 def homework_anonymous_comment (homework, is_in_course, user_activity_id = -1, course_activity = -1) if Time.parse(homework.end_time.to_s).strftime("%Y-%m-%d") >= Time.now.strftime("%Y-%m-%d") @@ -2374,18 +2569,27 @@ module ApplicationHelper link_to "作品(#{homework.student_works.count})",student_work_index_path(:homework => homework.id),:class => "c_blue" else #学生显示提交作品、修改作品等按钮 work = cur_user_works_for_homework homework + project = cur_user_projects_for_homework homework if work.nil? && Time.parse(homework.end_time.to_s).strftime("%Y-%m-%d") >= Time.now.strftime("%Y-%m-%d") - link_to "提交作品(#{homework.student_works.count})", new_student_work_path(:homework => homework.id),:class => 'c_blue' + if homework.homework_type ==3 && project.nil? && homework.homework_detail_group.base_on_project == 1 + link_to "提交作品(#{homework.student_works.count})","javascript:void(0)", :class => 'c_grey',:style=>"cursor:not-allowed",:title => '请先关联项目再提交作品' + else + link_to "提交作品(#{homework.student_works.count})", new_student_work_path(:homework => homework.id),:class => 'c_blue' + end elsif work.nil? && Time.parse(homework.end_time.to_s).strftime("%Y-%m-%d") < Time.now.strftime("%Y-%m-%d") - link_to "补交作品(#{homework.student_works.count})", new_student_work_path(:homework => homework.id),:class => 'c_red' + if homework.homework_type ==3 && project.nil? && homework.homework_detail_group.base_on_project == 1 + link_to "补交作品(#{homework.student_works.count})","javascript:void(0)", :class => 'c_grey',:style=>"cursor:not-allowed",:title => '请先关联项目再补交作品' + else + link_to "补交作品(#{homework.student_works.count})", new_student_work_path(:homework => homework.id),:class => 'c_red' + end else if homework.homework_detail_manual && homework.homework_detail_manual.comment_status == 2 #匿评作业,且作业状态不是在开启匿评之前 link_to "作品匿评", student_work_index_path(:homework => homework.id), :class => 'c_blue', :title => "开启匿评后不可修改作品" elsif homework.homework_detail_manual && homework.homework_detail_manual.comment_status == 3 - link_to "匿评结束", student_work_index_path(:homework => homework.id), :class => 'c_blue', :title => "匿评已结束" + link_to "查看作品(#{homework.student_works.count})", student_work_index_path(:homework => homework.id), :class => 'c_blue', :title => "匿评已结束" elsif homework.homework_type == 2 && Time.parse(homework.end_time.to_s).strftime("%Y-%m-%d") >= Time.now.strftime("%Y-%m-%d")#编程作业不能修改作品 link_to "修改作品(#{homework.student_works.count})", new_student_work_path(:homework => homework.id),:class => 'c_blue' - elsif Time.parse(homework.end_time.to_s).strftime("%Y-%m-%d") >= Time.now.strftime("%Y-%m-%d") + elsif Time.parse(homework.end_time.to_s).strftime("%Y-%m-%d") >= Time.now.strftime("%Y-%m-%d") && work.user_id == User.current.id link_to "修改作品(#{homework.student_works.count})", edit_student_work_path(work.id),:class => 'c_blue' else link_to "查看作品(#{homework.student_works.count})", student_work_index_path(:homework => homework.id), :class => 'c_blue', :title => "作业截止后不可修改作品" @@ -2398,6 +2602,24 @@ module ApplicationHelper end + def relate_project homework,is_teacher,is_in_course,user_activity_id,course_activity + if User.current.member_of_course?(homework.course) + if is_teacher + #link_to "已关联(#{homework.student_work_projects.count})",student_work_index_path(:homework => homework.id),:class => "c_blue" + else + projects = cur_user_projects_for_homework homework + works = cur_user_works_for_homework homework + if works.nil? && projects.nil? + link_to "关联项目",new_student_work_project_student_work_index_path(:homework => homework.id,:is_in_course=>is_in_course,:user_activity_id=>user_activity_id,:course_activity=>course_activity),remote: true,:class=> 'c_blue', :title=> '请选择分组作业关联的项目' + elsif works.nil? + link_to "取消关联",cancel_relate_project_student_work_index_path(:homework => homework.id,:is_in_course=>is_in_course,:user_activity_id=>user_activity_id,:course_activity=>course_activity), :confirm => "您确定要取消关联吗?", remote: true,:class => "c_blue", :title=> '取消关联项目' + else + #link_to "已关联(#{homework.student_work_projects.count})",student_work_index_path(:homework => homework.id),:class => "c_blue" + end + end + end + end + def student_anonymous_comment homework if homework.homework_detail_manual case homework.homework_detail_manual.comment_status @@ -2413,7 +2635,20 @@ module ApplicationHelper #获取当前用户在指定作业下提交的作业的集合 def cur_user_works_for_homework homework - homework.student_works.where("user_id = ?",User.current).first + work = homework.student_works.where("user_id = ?",User.current).first + if homework.homework_type == 3 + pro = homework.student_work_projects.where("user_id = #{User.current.id}").first + if pro.nil? || pro.student_work_id == "" || pro.student_work_id.nil? + work = nil + else + work = StudentWork.find pro.student_work_id + end + end + work + end + #获取当前用户在指定作业下关联的项目的集合 + def cur_user_projects_for_homework homework + homework.student_work_projects.where("user_id = ?",User.current).first end def file_preview_tag(file, html_options={}) @@ -2422,6 +2657,12 @@ module ApplicationHelper end end + def file_preview_eye(file, html_options={}) + if %w(pdf pptx doc docx xls xlsx).any?{|x| file.filename.downcase.end_with?(x)} + link_to '', download_named_attachment_path(file.id, file.filename, preview: true),html_options + end + end + #将文本内的/n转换为
#{@jour.notes.html_safe}