From 1997f3673197b401e5bbbd5a995ad4f448b1a3a7 Mon Sep 17 00:00:00 2001 From: gustav_delius Date: Sat, 5 Mar 2005 16:03:59 +0000 Subject: [PATCH] Questions that are in use are now only deleted from the category list but not from quizzes or old attempts. --- lang/en/quiz.php | 4 ++-- mod/quiz/locallib.php | 56 ++++++++++++++++--------------------------- mod/quiz/question.php | 48 +++++++++++++++---------------------- 3 files changed, 42 insertions(+), 66 deletions(-) diff --git a/lang/en/quiz.php b/lang/en/quiz.php index 5ba553dd64d..f57aad37680 100644 --- a/lang/en/quiz.php +++ b/lang/en/quiz.php @@ -205,7 +205,7 @@ $string['publish'] = 'Publish'; $string['publishedit'] = 'You must have permission in the publishing course to add or edit questions in this category'; $string['qti'] = 'IMS QTI format'; $string['question'] = 'Question'; -$string['questioninuse'] = 'The question \'$a\' is currently being used:'; +$string['questioninuse'] = 'The question \'$a->questionname\' is currently being used in:
$a->quiznames
The question will not be deleted from these quizzes but only from the category list.'; $string['questionname'] = 'Question name'; $string['questionnametoolong'] = 'Question name too long at line $a (255 char. max). It has been truncated.'; $string['questions'] = 'Questions'; @@ -268,7 +268,7 @@ $string['shortanswer'] = 'Short Answer'; $string['show'] = 'Show'; $string['showcorrectanswer'] = 'In feedback, show correct answers?'; $string['showfeedback'] = 'After answering, show feedback?'; -$string['showhidden'] = 'Also show hidden questions'; +$string['showhidden'] = 'Also show old questions'; $string['shuffleanswers'] = 'Shuffle answers'; $string['shufflequestions'] = 'Shuffle questions'; $string['significantfiguresformat'] = 'significant figures'; diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 41c70ed9d89..ad4ced72f60 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -1339,6 +1339,7 @@ function quiz_print_cat_question_list($categoryid, $quizselected=true, $recurse= $strdelete = get_string("delete"); $stredit = get_string("edit"); $straction = get_string("action"); + $strrestore = get_string('restore'); $straddselectedtoquiz = get_string("addselectedtoquiz", "quiz"); $straddtoquiz = get_string("addtoquiz", "quiz"); @@ -1415,7 +1416,7 @@ function quiz_print_cat_question_list($categoryid, $quizselected=true, $recurse= echo ""; echo ""; if ($canedit) { - echo ""; + echo ""; } echo ""; echo "\n"; @@ -1436,25 +1437,22 @@ function quiz_print_cat_question_list($categoryid, $quizselected=true, $recurse= src=\"../../pix/t/edit.gif\" border=\"0\" alt=\"$stredit\" /> "; // hide-feature if($question->hidden) { - $strhideshow = get_string("show"); - $imghideshow = "show.gif"; - $hideshow = 0; + echo "id&hide=0&sesskey=$USER->sesskey\">\"$strrestore\""; } else { - $strhideshow = get_string("hide"); - $imghideshow = "hide.gif"; - $hideshow = 1; + echo "id&delete=$question->id\">\"$strdelete\""; } - echo "id&hide=$hideshow&sesskey=$USER->sesskey\">\"$strhideshow\""; - echo "id&delete=$question->id\">\n\"$strdelete\""; if ($quizselected) { echo " id\" value=\"1\" />"; } echo "\n"; } - - echo "\n"; + if ($question->hidden) { + echo '\n"; + } else { + echo "\n"; + } echo "\n"; @@ -1985,30 +1983,18 @@ function quiz_categorylist($categoryid) { return $categorylist; } -// function to determine where question is in use -function quizzes_question_used( $id, $published=false, $courseid=0 ) { - // $id = question id - // $published = is category published - // $courseid = course id, required only if $published=true - // returns array of names of quizzes it appears in - if ($published) { - $quizzes = get_records("quiz"); - } - else { - $quizzes = get_records("quiz","course",$courseid); - } - $beingused = array(); - if ($quizzes) { - foreach ($quizzes as $quiz) { - $questions = explode(',', $quiz->questions); - foreach ($questions as $question) { - if ($question==$id) { - $beingused[] = $quiz->name; +function quizzes_question_used($id) { +// $id = question id +// returns array of names of quizzes a question appears in + + $quizlist = array(); + if ($grades = get_records('quiz_question_grades', 'question', $id)) { + foreach($grades as $grade) { + $quizlist[$grade->quiz] = get_field('quiz', 'name', 'id', $grade->quiz); } - } } - } - return $beingused; + + return $quizlist; } function quiz_parse_fieldname($name, $nameprefix='question') { diff --git a/mod/quiz/question.php b/mod/quiz/question.php index ed893079e4b..3b4941c8697 100644 --- a/mod/quiz/question.php +++ b/mod/quiz/question.php @@ -93,8 +93,16 @@ if (isset($_REQUEST['delete'])) { if (isset($confirm) and confirm_sesskey()) { if ($confirm == md5($delete)) { - if (!delete_records("quiz_questions", "id", $question->id)) { - error("An error occurred trying to delete question (id $question->id)"); + if (record_exists('quiz_responses', 'question', $question->id) or + record_exists('quiz_responses', 'originalquestion', $question->id) or + record_exists('quiz_question_grades', 'question', $question->id)) { + if (!set_field('quiz_questions', 'hidden', 1, 'id', $delete)) { + error('Was not able to hide question'); + } + } else { + if (!delete_records("quiz_questions", "id", $question->id)) { + error("An error occurred trying to delete question (id $question->id)"); + } } redirect("edit.php"); } else { @@ -102,35 +110,17 @@ } } else { - // determine if the question is being used in any quiz - $beingused = quizzes_question_used( $delete, $category->publish, $course->id ); - if ($beingused) { - $beingused = implode(", ", $beingused); - $beingused = get_string("questioninuse", "quiz", "$question->name")."

".$beingused; - notice($beingused, "edit.php"); - - } else { // the question is not used in any of the existing quizzes - - // we also have to check if the question is being used in the quiz - // which is currently being set up - if (isset($SESSION->modform)) { - if ($qus = explode(",", $SESSION->modform->questions)) { - foreach ($qus as $key => $qu) { - if ($qu == $delete) { - unset($qus[$key]); - unset($SESSION->modform->grades[$qu]); - } - } - } - $SESSION->modform->questions = implode(",", $qus); - } - - notice_yesno(get_string("deletequestioncheck", "quiz", $question->name), - "question.php?sesskey=$USER->sesskey&id=$question->id&delete=$delete&confirm=".md5($delete), "edit.php"); + if ($quiznames = quizzes_question_used($id)) { + $a->questionname = $question->name; + $a->quiznames = implode(', ', $quiznames); + notify(get_string('questioninuse', 'quiz', $a)); } - print_footer($course); - exit; + + notice_yesno(get_string("deletequestioncheck", "quiz", $question->name), + "question.php?sesskey=$USER->sesskey&id=$question->id&delete=$delete&confirm=".md5($delete), "edit.php"); } + print_footer($course); + exit; } if ($form = data_submitted() and confirm_sesskey()) {

$straction$straction$strquestionname$strtype
".$question->name."'.$question->name."".$question->name."\n"; quiz_print_question_icon($question, $canedit); echo "