From 0316bf698f10087a747988ba193b55a01399c7e6 Mon Sep 17 00:00:00 2001 From: huang Date: Tue, 17 Nov 2015 11:25:17 +0800 Subject: [PATCH 1/3] =?UTF-8?q?exercise=E6=96=B0=E5=A2=9E=E3=80=81update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/exercise_controller.rb | 59 ++++++++++++++++++++++++-- app/helpers/exercise_helper.rb | 11 +++++ 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/app/controllers/exercise_controller.rb b/app/controllers/exercise_controller.rb index 296c744b6..87854619c 100644 --- a/app/controllers/exercise_controller.rb +++ b/app/controllers/exercise_controller.rb @@ -16,11 +16,42 @@ class ExerciseController < ApplicationController end def show - + @exercise = Exercise.find params[:id] + if @exercise.exercise_status != 2 && (!User.current.allowed_to?(:as_teacher,@course) || User.current.admin?) + render_403 + return + end + # 问卷消息状态更新 + # REDO:问卷添加消息 + #已提交问卷的用户不能再访问该界面 + if has_commit_exercise?(@poll.id,User.current.id) && (!User.current.admin?) + redirect_to poll_index_url(:polls_type => "Course", :polls_group_id => @course.id) + else + @can_edit_poll = (!has_commit_poll?(@poll.id,User.current.id)) || User.current.admin? + @percent = get_percent(@poll,User.current) + poll_questions = @poll.poll_questions + @poll_questions = paginateHelper poll_questions,5 #分页 + respond_to do |format| + format.html {render :layout => 'base_courses'} + end + end + end end def new - + option = { + :exercise_name => "", + :exercise_description => "", + :course_id => @course.id, + :exercise_status => 1, + :user_id => User.current.id, + :start_at => "", + :end_at => "" + } + @exercise = Exercise.create option + if @exercise + redirect_to edit_exercise_url @exercise.id + end end def create @@ -32,11 +63,31 @@ class ExerciseController < ApplicationController end def update - + @exercise.exercise_name = params[:exercise_name] + @exercise.exercise_description = params[:exercise_name] + @exercise.start_at = params[:start_at] + @exercise.end_at = params[:end_at] + if @exercise.save + respond_to do |format| + format.js + end + else + render_404 + end end def destroy - + if @exercise && @exercise.destroy + if @is_teacher + polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id}") + else + polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id} and polls_status = 2") + end + @polls = paginateHelper polls,20 #分页 + respond_to do |format| + format.js + end + end end #统计结果 diff --git a/app/helpers/exercise_helper.rb b/app/helpers/exercise_helper.rb index 7fc7b1421..844cff1c1 100644 --- a/app/helpers/exercise_helper.rb +++ b/app/helpers/exercise_helper.rb @@ -1,3 +1,14 @@ # encoding: utf-8 module ExerciseHelper + + #判断用户是否已经提交了问卷 + def has_commit_exercise?(poll_id,user_id) + pu = PollUser.find_by_poll_id_and_user_id(poll_id,user_id) + if pu.nil? + false + else + true + end + end + end \ No newline at end of file From 23ca20692fe6a10358c05422dafaa53e02e5d1c3 Mon Sep 17 00:00:00 2001 From: huang Date: Tue, 17 Nov 2015 15:20:59 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E9=97=AE=E5=8D=B7=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/exercise_controller.rb | 71 ++++++++++++++++++++++---- app/helpers/exercise_helper.rb | 4 +- db/schema.rb | 54 +------------------- 3 files changed, 65 insertions(+), 64 deletions(-) diff --git a/app/controllers/exercise_controller.rb b/app/controllers/exercise_controller.rb index 87854619c..e29ae8176 100644 --- a/app/controllers/exercise_controller.rb +++ b/app/controllers/exercise_controller.rb @@ -21,21 +21,18 @@ class ExerciseController < ApplicationController render_403 return end - # 问卷消息状态更新 - # REDO:问卷添加消息 #已提交问卷的用户不能再访问该界面 - if has_commit_exercise?(@poll.id,User.current.id) && (!User.current.admin?) - redirect_to poll_index_url(:polls_type => "Course", :polls_group_id => @course.id) + if has_commit_exercise?(@exercise.id, User.current.id) && (!User.current.admin?) + redirect_to poll_index_url(:course_id=> @course.id) else - @can_edit_poll = (!has_commit_poll?(@poll.id,User.current.id)) || User.current.admin? - @percent = get_percent(@poll,User.current) + @can_edit_poll = (!has_commit_exercise?(@exercise.id,User.current.id)) || User.current.admin? + @percent = get_percent(@exercise,User.current) poll_questions = @poll.poll_questions @poll_questions = paginateHelper poll_questions,5 #分页 respond_to do |format| format.html {render :layout => 'base_courses'} end end - end end def new @@ -45,8 +42,8 @@ class ExerciseController < ApplicationController :course_id => @course.id, :exercise_status => 1, :user_id => User.current.id, - :start_at => "", - :end_at => "" + :time => "", + :end_time => "" } @exercise = Exercise.create option if @exercise @@ -92,7 +89,63 @@ class ExerciseController < ApplicationController #统计结果 def statistics_result + @exercise = Exercise.find(params[:id]) + exercise_questions = @exercise.poll_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[:exercise_questions_title].nil? || params[:poll_questions_title].empty? ? l(:label_enter_single_title) : params[:poll_questions_title] + option = { + :is_necessary => (params[:is_necessary]=="true" ? 1 : 0), + :question_title => question_title, + :question_type => params[:question_type] || 1, + :question_number => @poll.poll_questions.count + 1 + } + @poll_questions = @poll.poll_questions.new option + 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 = { + :answer_position => i, + :answer_text => answer + } + @poll_questions.poll_answers.new question_option + end + end + # 如果是插入的话,那么从插入的这个id以后的question_num都将要+1 + if params[:quest_id] + @is_insert = true + @poll.poll_questions.where("question_number > #{params[:quest_num].to_i}").update_all(" question_number = question_number + 1") + @poll_question_num = params[:quest_num].to_i + @poll_questions.question_number = params[:quest_num].to_i + 1 + end + if @poll_questions.save + respond_to do |format| + format.js + end + end + + end + + #发布问卷 + def publish_excercise + @exercise.exercise_status = 2 + @exercise.publish_time = Time.now + if @exercise.save + if params[:is_remote] + redirect_to poll_index_url(:course_id => @course.id) + else + respond_to do |format| + format.js + end + end + end end def student_exercise_list diff --git a/app/helpers/exercise_helper.rb b/app/helpers/exercise_helper.rb index 844cff1c1..410a9936b 100644 --- a/app/helpers/exercise_helper.rb +++ b/app/helpers/exercise_helper.rb @@ -2,8 +2,8 @@ module ExerciseHelper #判断用户是否已经提交了问卷 - def has_commit_exercise?(poll_id,user_id) - pu = PollUser.find_by_poll_id_and_user_id(poll_id,user_id) + def has_commit_exercise?(exercise_id, user_id) + pu = PollUser.find_by_poll_id_and_user_id(excercise_id, user_id) if pu.nil? false else diff --git a/db/schema.rb b/db/schema.rb index 3feb43adb..25d561e86 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 => 20151116065904) do +ActiveRecord::Schema.define(:version => 20151116071721) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false @@ -241,58 +241,6 @@ ActiveRecord::Schema.define(:version => 20151116065904) do add_index "changesets_issues", ["changeset_id", "issue_id"], :name => "changesets_issues_ids", :unique => true - create_table "code_review_assignments", :force => true do |t| - t.integer "issue_id" - t.integer "change_id" - t.integer "attachment_id" - t.string "file_path" - t.string "rev" - t.string "rev_to" - t.string "action_type" - t.integer "changeset_id" - end - - create_table "code_review_project_settings", :force => true do |t| - t.integer "project_id" - t.integer "tracker_id" - t.datetime "created_at" - t.datetime "updated_at" - t.integer "updated_by" - t.boolean "hide_code_review_tab", :default => false - t.integer "auto_relation", :default => 1 - t.integer "assignment_tracker_id" - t.text "auto_assign" - t.integer "lock_version", :default => 0, :null => false - t.boolean "tracker_in_review_dialog", :default => false - end - - create_table "code_review_user_settings", :force => true do |t| - t.integer "user_id", :default => 0, :null => false - t.integer "mail_notification", :default => 0, :null => false - t.datetime "created_at" - t.datetime "updated_at" - end - - create_table "code_reviews", :force => true do |t| - t.integer "project_id" - t.integer "change_id" - t.datetime "created_at" - t.datetime "updated_at" - t.integer "line" - t.integer "updated_by_id" - t.integer "lock_version", :default => 0, :null => false - t.integer "status_changed_from" - t.integer "status_changed_to" - t.integer "issue_id" - t.string "action_type" - t.string "file_path" - t.string "rev" - t.string "rev_to" - t.integer "attachment_id" - t.integer "file_count", :default => 0, :null => false - t.boolean "diff_all" - 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 4a0865ab76a55b9aa36da8109b9509a846ec14d8 Mon Sep 17 00:00:00 2001 From: huang Date: Tue, 17 Nov 2015 18:20:47 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=92=8C=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E9=A2=98=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/exercise_controller.rb | 84 +++++++++++++++++++------- 1 file changed, 62 insertions(+), 22 deletions(-) diff --git a/app/controllers/exercise_controller.rb b/app/controllers/exercise_controller.rb index 3870fe9c5..2237a9cfe 100644 --- a/app/controllers/exercise_controller.rb +++ b/app/controllers/exercise_controller.rb @@ -21,7 +21,7 @@ class ExerciseController < ApplicationController render_403 return end - #已提交问卷的用户不能再访问该界面 + # 已提交问卷的用户不能再访问该界面 if has_commit_exercise?(@exercise.id, User.current.id) && (!User.current.admin?) redirect_to poll_index_url(:course_id=> @course.id) else @@ -67,20 +67,10 @@ class ExerciseController < ApplicationController end def destroy - if @exercise && @exercise.destroy - if @is_teacher - polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id}") - else - polls = Poll.where("polls_type = 'Course' and polls_group_id = #{@course.id} and polls_status = 2") - end - @polls = paginateHelper polls,20 #分页 - respond_to do |format| - format.js - end - end + end - #统计结果 + # 统计结果 def statistics_result @exercise = Exercise.find(params[:id]) exercise_questions = @exercise.poll_questions @@ -90,17 +80,16 @@ class ExerciseController < ApplicationController end end - #添加题目 - #question_type 1:单选 2:多选 3:填空题 + # 添加题目 + # question_type 1:单选 2:多选 3:填空题 def create_exercise_question question_title = params[:exercise_questions_title].nil? || params[:poll_questions_title].empty? ? l(:label_enter_single_title) : params[:poll_questions_title] option = { - :is_necessary => (params[:is_necessary]=="true" ? 1 : 0), :question_title => question_title, :question_type => params[:question_type] || 1, - :question_number => @poll.poll_questions.count + 1 + :question_number => @exercise.exercise_questions.count + 1 } - @poll_questions = @poll.poll_questions.new option + @exercise_questions = @exercise.exercise_questions.new option 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] @@ -108,15 +97,15 @@ class ExerciseController < ApplicationController :answer_position => i, :answer_text => answer } - @poll_questions.poll_answers.new question_option + @exercise_questions.poll_answers.new question_option end end # 如果是插入的话,那么从插入的这个id以后的question_num都将要+1 if params[:quest_id] @is_insert = true - @poll.poll_questions.where("question_number > #{params[:quest_num].to_i}").update_all(" question_number = question_number + 1") - @poll_question_num = params[:quest_num].to_i - @poll_questions.question_number = params[:quest_num].to_i + 1 + @exercise.exercise_questions.where("question_number > #{params[:quest_num].to_i}").update_all(" question_number = question_number + 1") + # @exercise_question_num = params[:quest_num].to_i + @exercise_questions.question_number = params[:quest_num].to_i + 1 end if @poll_questions.save respond_to do |format| @@ -126,6 +115,57 @@ class ExerciseController < ApplicationController 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_questions.question_title = params[:exercise_questions_title].nil? || params[:exercise_questions_title].empty? ? l(:label_enter_single_title) : params[:exercise_questions_title] + ################处理选项 + if params[:question_answer] + @exercise_question.exercise_answers.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_answers.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.exercise_choices_id = i + question.answer_text = answer + question.save + else + question_option = { + :exercise_choices_id => i, + :answer_text => answer + } + @exercise_question.exercise_answers.new question_option + end + end + end + @exercise_question.save + respond_to do |format| + format.js + end + end + + # 删除题目 + def delete_exercise_question + @exercise_question = ExerciseQuestion.find params[:exercise_question] + @exercise = @exercise_question.exercise + exercise_questions = @exercise.exercise_questions.where("question_number > #{@exercise_question.question_number}") + poll_questions.each do |question| + question.question_number -= 1 + question.save + end + if @poll_question && @poll_question.destroy + respond_to do |format| + format.js + end + end + end + + #发布问卷 def publish_excercise @exercise.exercise_status = 2