From 4a5a602a6ed8fb3e70e5b682aae62e7c3929e3e4 Mon Sep 17 00:00:00 2001 From: cxt Date: Thu, 28 Apr 2016 14:46:27 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/schema.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index 70fb756fb..0ec4fab77 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20160427075457) do +ActiveRecord::Schema.define(:version => 20160427061847) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false @@ -334,6 +334,17 @@ ActiveRecord::Schema.define(:version => 20160427075457) do t.boolean "diff_all" end + create_table "code_tests", :force => true do |t| + t.integer "homework_id" + t.integer "wait_time", :default => 0 + t.integer "language" + t.integer "status" + t.integer "time_used", :default => 0 + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + t.integer "student_work_id", :default => 0 + end + create_table "comments", :force => true do |t| t.string "commented_type", :limit => 30, :default => "", :null => false t.integer "commented_id", :default => 0, :null => false From 53bf0dcd7acc32478c63cb3e4973c48e0a1681c0 Mon Sep 17 00:00:00 2001 From: cxt Date: Fri, 29 Apr 2016 15:46:26 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E9=A2=98=E5=BA=93=E5=8A=A0=E5=85=A5?= =?UTF-8?q?=E6=89=80=E6=9C=89=E7=A7=81=E6=9C=89=E4=BD=9C=E4=B8=9A=E3=80=81?= =?UTF-8?q?=E5=B9=B6=E5=8F=AF=E9=80=9A=E8=BF=87=E6=8F=90=E4=BA=A4=E7=94=B3?= =?UTF-8?q?=E8=AF=B7=E6=9D=A5=E5=AF=BC=E5=85=A5=E7=A7=81=E6=9C=89=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 85 +++++++++++++++++-- app/models/apply_homework.rb | 6 ++ app/models/course_message.rb | 5 +- app/models/mailer.rb | 8 +- app/views/users/_homework_repository.html.erb | 19 ++++- app/views/users/_user_message_course.html.erb | 29 +++++++ app/views/users/apply_for_homework.js.erb | 8 ++ app/views/users/choose_user_course.js.erb | 24 ++++-- .../users/dealwith_apply_homework.js.erb | 11 +++ app/views/users/user_homeworks.html.erb | 4 +- config/locales/courses/zh.yml | 1 + config/routes.rb | 2 + .../20160429015956_create_apply_homeworks.rb | 13 +++ ...429030819_add_column_to_course_messages.rb | 6 ++ db/schema.rb | 32 ++++++- spec/factories/apply_homeworks.rb | 9 ++ spec/models/apply_homework_spec.rb | 5 ++ 17 files changed, 243 insertions(+), 24 deletions(-) create mode 100644 app/models/apply_homework.rb create mode 100644 app/views/users/apply_for_homework.js.erb create mode 100644 app/views/users/dealwith_apply_homework.js.erb create mode 100644 db/migrate/20160429015956_create_apply_homeworks.rb create mode 100644 db/migrate/20160429030819_add_column_to_course_messages.rb create mode 100644 spec/factories/apply_homeworks.rb create mode 100644 spec/models/apply_homework_spec.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 8e08508ed..d8e9a0f62 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -288,6 +288,27 @@ class UsersController < ApplicationController end end + #处理引用作业的请求 + #status 1 同意 2 拒绝 + def dealwith_apply_homework + @msg = CourseMessage.find(params[:msg_id]) + case params[:agree] + when 'Y' + ah = ApplyHomework.where("user_id = ? and homework_common_id = ?",@msg.apply_user_id, @msg.course_message_id) + unless ah.empty? + ah.first.update_column('status', 2) + CourseMessage.create(:user_id => @msg.apply_user_id, :course_id => @msg.course_id, :viewed => false,:content=> @msg.content,:course_message_id=>@msg.course_message_id,:content=>@msg.content,:course_message_type=>'HomeworkCommon',:status=>5,:apply_result=>1) + @msg.update_attributes(:apply_result=>1,:viewed=>1) + end + when 'N' + CourseMessage.create(:user_id => @msg.apply_user_id, :course_id => @msg.course_id, :viewed => false,:content=> @msg.content,:course_message_id=>@msg.course_message_id,:content=>@msg.content,:course_message_type=>'HomeworkCommon',:status=>5,:apply_result=>2) + @msg.update_attributes(:apply_result=>2,:viewed=>1) + end + respond_to do |format| + format.js + end + end + # added by bai def show_score @@ -372,13 +393,38 @@ class UsersController < ApplicationController end # end + #申请引用非公开作业 + def apply_for_homework + if User.current.logged? + homework = HomeworkCommon.find params[:homework_id] + unless homework.nil? + ah = ApplyHomework.where("user_id = ? and homework_common_id = ?", User.current.id, params[:homework_id].to_i) + if ah.empty? + ApplyHomework.create(:user_id => params[:id].to_i, :homework_common_id => params[:homework_id].to_i, :status => 1) + Mailer.run.apply_for_homework_request(homework, User.current) + CourseMessage.create(:user_id => homework.user_id, :course_id => homework.course.id, :viewed => false,:course_message_id=>params[:homework_id].to_i,:course_message_type=>'HomeworkCommon',:status=>5,:apply_user_id=>params[:id].to_i) + @state = 2 + @ah = ApplyHomework.where("user_id = ? and homework_common_id = ?", params[:id].to_i, params[:homework_id].to_i).first + @homework = homework + else + @state = 3 + end + end + else + @state = 1 + end + respond_to do |format| + format.js + end + end + #用户作业列表 def user_homeworks @order,@b_sort = params[:order] || "created_at",params[:sort] || "desc" @user = User.current @r_sort = @b_sort == "desc" ? "asc" : "desc" - if(params[:type].blank? || params[:type] == "1") #公共题库 - visible_course = Course.where("is_public = 1 && is_delete = 0") + if(params[:type].blank? || params[:type] == "1") #题库 + visible_course = Course.where("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("#{@order} #{@b_sort}") elsif params[:type] == "2" #我的题库 @@ -424,6 +470,14 @@ class UsersController < ApplicationController def choose_user_course homework = HomeworkCommon.find params[:send_id].to_i + if homework.course.is_public == 0 && homework.user_id != User.current.id + ah = ApplyHomework.where("user_id = ? and homework_common_id = ?", User.current.id, params[:send_id].to_i) + if ah.empty? + @status = 2 + elsif ah.first.status == 1 + @status = 1 + end + end if !params[:search].nil? search = "%#{params[:search].to_s.strip.downcase}%" @course = @user.courses.where("is_delete = 0 and #{Course.table_name}.id != #{homework.course_id} and #{Course.table_name}.id = #{params[:search].to_i } or #{Course.table_name}.name like :p",:p=>search).select { |course| @user.allowed_to?(:as_teacher,course)} @@ -528,12 +582,18 @@ class UsersController < ApplicationController @order,@b_sort = params[:order] || "created_at",params[:sort] || "desc" @r_sort = @b_sort == "desc" ? "asc" : "desc" @user = User.current - if(params[:type].blank? || params[:type] == "1") #公共题库 - visible_course = Course.where("is_public = 1 && is_delete = 0") + if(params[:type].blank? || params[:type] == "1") #题库 + visible_course = Course.where("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("#{@order} #{@b_sort}") elsif params[:type] == "2" #我的题库 @homeworks = HomeworkCommon.where("user_id = #{@user.id} and publish_time <= '#{Date.today}'").order("#{@order} #{@b_sort}") + elsif params[:type] == "3" #申请题库 + #apply_homeworks = ApplyHomework.where("user_id = ?",@user.id) + #homework_ids = apply_homeworks.empty? ? "(-1)" : "(" + apply_homeworks.map{|ah| ah.homework_common_id}.join(",") + ")" + #@homeworks = HomeworkCommon.where("id in #{homework_ids}") + sql = "SELECT homework_commons.* FROM homework_commons INNER JOIN apply_homeworks as ah ON homework_commons.id = ah.homework_common_id where ah.user_id = #{@user.id} order by ah.created_at desc" + @homeworks = HomeworkCommon.find_by_sql(sql) end if params[:property] && params[:property] == "1" @homeworks = @homeworks.where("homework_type = 1").reorder("#{@order} #{@b_sort}") @@ -571,8 +631,8 @@ class UsersController < ApplicationController @user = User.current search = params[:name].to_s.strip.downcase type_ids = params[:property]=="" || params[:property].nil? ? "(1, 2, 3)" : "(" + params[:property] + ")" - if(params[:type].blank? || params[:type] == "1") #全部 - visible_course = Course.where("is_public = 1 && is_delete = 0") + if(params[:type].blank? || params[:type] == "1") #全部 + visible_course = Course.where("is_delete = 0") visible_course_ids = visible_course.empty? ? "(-1)" : "(" + visible_course.map{|course| course.id}.join(",") + ")" all_homeworks = HomeworkCommon.where("course_id in #{visible_course_ids} and publish_time <= '#{Date.today}'") all_user_ids = all_homeworks.map{|hw| hw.user_id} @@ -586,7 +646,7 @@ class UsersController < ApplicationController else @homeworks = HomeworkCommon.where("course_id in #{visible_course_ids} and publish_time <= '#{Date.today}' and homework_type in #{type_ids} and (name like '%#{search}%' or user_id in #{user_ids})").order("#{@order} #{@b_sort}") end - elsif params[:type] == "2" #课程资源 + elsif params[:type] == "2" #我的题库 if @order == "course_name" sql = "SELECT homework_commons.* FROM homework_commons INNER JOIN courses ON homework_commons.course_id = courses.id where homework_commons.user_id = #{@user.id} and homework_type in #{type_ids} and publish_time <= '#{Date.today}' and (homework_commons.name like '%#{search}%') order by CONVERT (courses.name USING gbk) COLLATE gbk_chinese_ci #{@b_sort}" @homeworks = HomeworkCommon.find_by_sql(sql) @@ -595,6 +655,17 @@ class UsersController < ApplicationController else @homeworks = HomeworkCommon.where("user_id = #{@user.id} and publish_time <= '#{Date.today}' and (name like '%#{search}%') and homework_type in #{type_ids}").order("#{@order} #{@b_sort}") end + elsif params[:type] == "3" #申请题库 + apply_homeworks = ApplyHomework.where("user_id = ?",@user.id) + homework_ids = apply_homeworks.empty? ? "(-1)" : "(" + apply_homeworks.map{|ah| ah.homework_common_id}.join(",") + ")" + if @order == "course_name" + sql = "SELECT homework_commons.* FROM homework_commons INNER JOIN courses ON homework_commons.course_id = courses.id where homework_commons.id in #{homework_ids} and homework_type in #{type_ids} and publish_time <= '#{Date.today}' and (homework_commons.name like '%#{search}%') order by CONVERT (courses.name USING gbk) COLLATE gbk_chinese_ci #{@b_sort}" + @homeworks = HomeworkCommon.find_by_sql(sql) + elsif @order == "user_name" + @homeworks = HomeworkCommon.where("homework_commons.id in #{homework_ids} and (name like '%#{search}%') and homework_type in #{type_ids}").joins(:user).order("CONVERT (lastname USING gbk) COLLATE gbk_chinese_ci #{@b_sort}, CONVERT (firstname USING gbk) COLLATE gbk_chinese_ci #{@b_sort},login #{@b_sort}") + else + @homeworks = HomeworkCommon.where("id in #{homework_ids} and (name like '%#{search}%') and homework_type in #{type_ids}").order("#{@order} #{@b_sort}") + end end =begin if params[:property] && params[:property] == "1" diff --git a/app/models/apply_homework.rb b/app/models/apply_homework.rb new file mode 100644 index 000000000..c92eccca6 --- /dev/null +++ b/app/models/apply_homework.rb @@ -0,0 +1,6 @@ +class ApplyHomework < ActiveRecord::Base + #status:1. 待审核 2.审核通过 + belongs_to :user + belongs_to :homework_common + attr_accessible :status, :user_id, :homework_common_id +end diff --git a/app/models/course_message.rb b/app/models/course_message.rb index 79d27f3c5..3375aa5ab 100644 --- a/app/models/course_message.rb +++ b/app/models/course_message.rb @@ -1,8 +1,9 @@ class CourseMessage < ActiveRecord::Base # status说明: status在课程不同的类型,区分不同的功能 status = 9 作品的提交记录 # HomeworkCommon:status: - # nil:发布了作业; 1:作业截止时间到了提醒!;2:开启匿评; 3:关闭匿评; 4:匿评开始失败 - attr_accessible :course_id, :course_message_id, :course_message_type, :user_id, :viewed, :content, :status + # nil:发布了作业; 1:作业截止时间到了提醒!;2:开启匿评; 3:关闭匿评; 4:匿评开始失败; 5:申请引用作业 + # apply_user_id: 申请者的用户id + attr_accessible :course_id, :course_message_id, :course_message_type, :user_id, :viewed, :content, :status, :apply_user_id, :apply_result # 多态 虚拟关联 belongs_to :course_message ,:polymorphic => true diff --git a/app/models/mailer.rb b/app/models/mailer.rb index 05b0349ec..7f6f5abb9 100644 --- a/app/models/mailer.rb +++ b/app/models/mailer.rb @@ -1039,7 +1039,13 @@ class Mailer < ActionMailer::Base :subject => @subject end - + def apply_for_homework_request(homework, user) + @receive = User.find(homework.user_id) + @user = user + @subject = "#{@user.show_name} #{l(:label_apply_for_homework)} #{homework.name} " + mail :to => @receive.mail, + :subject => @subject + end private diff --git a/app/views/users/_homework_repository.html.erb b/app/views/users/_homework_repository.html.erb index 1ed8ae688..72211a0d9 100644 --- a/app/views/users/_homework_repository.html.erb +++ b/app/views/users/_homework_repository.html.erb @@ -19,7 +19,9 @@ <%= homework.name%> - 私有 + <% if homework.course.is_public == 0 %> + 私有 + <% end %>
  • @@ -35,7 +37,20 @@
  • <%= homework.quotes %>
  • <%=format_date homework.publish_time %>
  • -
  • 提交申请
  • + <% if homework.course.is_public == 0 && homework.user_id != User.current.id %> +
  • + <% ah = ApplyHomework.where("user_id = ? and homework_common_id = ?", User.current.id, homework.id).first %> + <% if ah.nil? %> + <%= link_to("提交申请", apply_for_homework_user_path(User.current.id,:homework_id => homework.id), :remote => true) %> + <% elsif ah.status == 1 %> + 待审核 + <% elsif ah.status == 2 %> + 可引用 + <% end %> +
  • + <% else %> +
  • --
  • + <% end %> <% end %> \ No newline at end of file diff --git a/app/views/users/_user_message_course.html.erb b/app/views/users/_user_message_course.html.erb index ccd329852..cfde9d4bd 100644 --- a/app/views/users/_user_message_course.html.erb +++ b/app/views/users/_user_message_course.html.erb @@ -229,7 +229,36 @@
  • <%= time_tag(ma.created_at).html_safe %>
  • <% end %> + <% if ma.course_message_type == "HomeworkCommon" && ma.status == 5 %> + + <% end %> <% if ma.course_message_type == "Poll" %>