diff --git a/app/controllers/quiz_groups_controller.rb b/app/controllers/quiz_groups_controller.rb index a37bdc76eda..c62a2ccfea8 100644 --- a/app/controllers/quiz_groups_controller.rb +++ b/app/controllers/quiz_groups_controller.rb @@ -62,8 +62,8 @@ class QuizGroupsController < ApplicationController if authorized_action(@quiz, @current_user, :update) @group = @quiz.quiz_groups.find(params[:quiz_group_id]) items = [] - group_questions = @group.quiz_questions - questions = @quiz.quiz_questions + group_questions = @group.quiz_questions.active + questions = @quiz.quiz_questions.active order = params[:order].split(",") order.each do |name| id = name.gsub(/\Aquestion_/, "").to_i diff --git a/app/controllers/quiz_questions_controller.rb b/app/controllers/quiz_questions_controller.rb index 89e86d35c55..7265bd5de28 100644 --- a/app/controllers/quiz_questions_controller.rb +++ b/app/controllers/quiz_questions_controller.rb @@ -21,7 +21,7 @@ class QuizQuestionsController < ApplicationController def show if authorized_action(@quiz, @current_user, :update) - @question = @quiz.quiz_questions.find(params[:id]) + @question = @quiz.quiz_questions.active.find(params[:id]) render :json => @question.to_json(:include => :assessment_question) end end @@ -55,7 +55,7 @@ class QuizQuestionsController < ApplicationController def update if authorized_action(@quiz, @current_user, :update) - @question = @quiz.quiz_questions.find(params[:id]) + @question = @quiz.quiz_questions.active.find(params[:id]) question_data = params[:question] question_data[:regrade_user] = @current_user question_data ||= {} @@ -64,7 +64,7 @@ class QuizQuestionsController < ApplicationController @group = @quiz.quiz_groups.find(question_data[:quiz_group_id]) if question_data[:quiz_group_id] != @question.quiz_group_id @question.quiz_group_id = question_data[:quiz_group_id] - @question.position = @group.quiz_questions.length + @question.position = @group.quiz_questions.active.length end end @@ -78,7 +78,7 @@ class QuizQuestionsController < ApplicationController def destroy if authorized_action(@quiz, @current_user, :update) - @question = @quiz.quiz_questions.find(params[:id]) + @question = @quiz.quiz_questions.active.find(params[:id]) @question.destroy render :json => @question.to_json end diff --git a/app/controllers/quizzes_controller.rb b/app/controllers/quizzes_controller.rb index 7e67d99248d..7c252656ab3 100644 --- a/app/controllers/quizzes_controller.rb +++ b/app/controllers/quizzes_controller.rb @@ -376,7 +376,7 @@ class QuizzesController < ApplicationController if authorized_action(@quiz, @current_user, :update) items = [] groups = @quiz.quiz_groups - questions = @quiz.quiz_questions + questions = @quiz.quiz_questions.active order = params[:order].split(",") order.each_index do |idx| name = order[idx] @@ -387,7 +387,7 @@ class QuizzesController < ApplicationController obj = groups.detect{|g| g.id == id.to_i} if id != 0 && name.match(/\Agroup/) items << obj if obj end - root_questions = @quiz.quiz_questions.where("quiz_group_id IS NULL").all + root_questions = @quiz.quiz_questions.active.where("quiz_group_id IS NULL").all items += root_questions items.uniq! question_updates = [] diff --git a/app/models/quiz.rb b/app/models/quiz.rb index 15b683cc676..6d4d332e851 100644 --- a/app/models/quiz.rb +++ b/app/models/quiz.rb @@ -368,19 +368,34 @@ class Quiz < ActiveRecord::Base end def root_entries_max_position - question_max = self.quiz_questions.maximum(:position, :conditions => 'quiz_group_id is null') + question_max = self.active_quiz_questions.maximum(:position, :conditions => 'quiz_group_id is null') group_max = self.quiz_groups.maximum(:position) [question_max, group_max, 0].compact.max end + def active_quiz_questions_without_group + if self.quiz_questions.loaded? + active_quiz_questions.select { |q| !q.quiz_group_id } + else + active_quiz_questions.where(quiz_group_id: nil).all + end + end + + def active_quiz_questions + if self.quiz_questions.loaded? + quiz_questions.select(&:active?) + else + quiz_questions.active + end + end + # Returns the list of all "root" entries, either questions or question # groups for this quiz. This is PRE-SAVED data. Once the quiz has # been saved, all the data can be found in Quiz.quiz_data def root_entries(force_check=false) return @root_entries if @root_entries && !force_check result = [] - all_questions = self.quiz_questions - result.concat all_questions.select{|q| !q.quiz_group_id } + result.concat self.active_quiz_questions_without_group result.concat self.quiz_groups result = result.sort_by{|e| e.position || 99999}.map do |e| res = nil @@ -393,7 +408,7 @@ class Quiz < ActiveRecord::Base data[:assessment_question_bank_id] = e.assessment_question_bank_id data[:questions] = [] else - data[:questions] = e.quiz_questions.sort_by{|q| q.position || 99999}.map(&:data) + data[:questions] = e.quiz_questions.active.sort_by{|q| q.position || 99999}.map(&:data) end data[:actual_pick_count] = e.actual_pick_count res = data @@ -756,7 +771,7 @@ class Quiz < ActiveRecord::Base end def migrate_content_links_by_hand(user) - self.quiz_questions.each do |question| + self.quiz_questions.active.each do |question| data = QuizQuestion.migrate_question_hash(question.question_data, :context => self.context, :user => user) question.write_attribute(:question_data, data) question.save @@ -866,7 +881,7 @@ class Quiz < ActiveRecord::Base raise e if retrying return self.clone_for(context, original_dup, options, true) end - entities = self.quiz_groups + self.quiz_questions + entities = self.quiz_groups + self.active_quiz_questions entities.each do |entity| entity_dup = entity.clone_for(dup, nil, :old_context => self.context, :new_context => context) entity_dup.quiz_id = dup.id @@ -951,7 +966,7 @@ class Quiz < ActiveRecord::Base return false if has_student_submissions? self.question_count = 0 - self.quiz_questions.destroy_all + self.quiz_questions.active.map(&:destroy) self.quiz_groups.destroy_all self.quiz_data = nil true @@ -1031,7 +1046,7 @@ class Quiz < ActiveRecord::Base hash[:questions] ||= [] if question_data[:qq_data] || question_data[:aq_data] - existing_questions = item.quiz_questions.where("migration_id IS NOT NULL").select([:id, :migration_id]).index_by(&:migration_id) + existing_questions = item.quiz_questions.active.where("migration_id IS NOT NULL").select([:id, :migration_id]).index_by(&:migration_id) end if question_data[:qq_data] diff --git a/app/models/quiz_group.rb b/app/models/quiz_group.rb index d67d56fc8d9..67f9c1d29b8 100644 --- a/app/models/quiz_group.rb +++ b/app/models/quiz_group.rb @@ -40,7 +40,7 @@ class QuizGroup < ActiveRecord::Base # don't do a valid question check because we don't want to instantiate all the bank's questions count = self.assessment_question_bank.assessment_question_count else - count = self.quiz_questions.select{|q| q.unsupported != true}.length rescue self.quiz_questions.length + count = self.quiz_questions.active.count end [self.pick_count.to_i, count].min @@ -66,7 +66,7 @@ class QuizGroup < ActiveRecord::Base "name" => self.name, "pick_count" => self.pick_count, "question_points" => self.question_points, - "questions" => self.assessment_question_bank_id ? [] : self.quiz_questions.map{|q| q.data}, + "questions" => self.assessment_question_bank_id ? [] : self.quiz_questions.active.map{|q| q.data}, "assessment_question_bank_id" => self.assessment_question_bank_id }.with_indifferent_access end diff --git a/app/models/quiz_question.rb b/app/models/quiz_question.rb index 4759aefd833..2b94898c34f 100644 --- a/app/models/quiz_question.rb +++ b/app/models/quiz_question.rb @@ -19,6 +19,8 @@ require 'quiz_question_link_migrator' class QuizQuestion < ActiveRecord::Base + include Workflow + attr_accessible :quiz, :quiz_group, :assessment_question, :question_data, :assessment_question_version attr_readonly :quiz_id belongs_to :quiz @@ -31,11 +33,18 @@ class QuizQuestion < ActiveRecord::Base validates_presence_of :quiz_id serialize :question_data after_save :update_quiz + + workflow do + state :active + state :deleted + end + + scope :active, where("workflow_state='active' OR workflow_state IS NULL") def infer_defaults if !self.position && self.quiz if self.quiz_group - self.position = (self.quiz_group.quiz_questions.map(&:position).compact.max || 0) + 1 + self.position = (self.quiz_group.quiz_questions.active.map(&:position).compact.max || 0) + 1 else self.position = self.quiz.root_entries_max_position + 1 end @@ -180,6 +189,12 @@ class QuizQuestion < ActiveRecord::Base end end + alias_method :destroy!, :destroy + def destroy + self.workflow_state = 'deleted' + self.save + end + private def update_question_regrade(regrade_option, regrade_user) diff --git a/app/views/quizzes/history.html.erb b/app/views/quizzes/history.html.erb index 08964cdb842..c903a818412 100644 --- a/app/views/quizzes/history.html.erb +++ b/app/views/quizzes/history.html.erb @@ -19,7 +19,7 @@