Questions that are in use are now only deleted from the category list but not from quizzes or old attempts.

This commit is contained in:
gustav_delius 2005-03-05 16:03:59 +00:00
parent 14ca3084a0
commit 1997f36731
3 changed files with 42 additions and 66 deletions

View file

@ -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: <br />$a->quiznames<br />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';

View file

@ -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 "<table border=\"0\" cellpadding=\"5\" cellspacing=\"2\" width=\"100%\">";
echo "<tr>";
if ($canedit) {
echo "<th width=\"105\" nowrap=\"nowrap\">$straction</th>";
echo "<th width=\"100\" nowrap=\"nowrap\">$straction</th>";
}
echo "<th width=\"100%\" align=\"left\" nowrap=\"nowrap\">$strquestionname</th><th width=\"*\" nowrap=\"nowrap\">$strtype</th>";
echo "</tr>\n";
@ -1436,25 +1437,22 @@ function quiz_print_cat_question_list($categoryid, $quizselected=true, $recurse=
src=\"../../pix/t/edit.gif\" border=\"0\" alt=\"$stredit\" /></a>&nbsp;";
// hide-feature
if($question->hidden) {
$strhideshow = get_string("show");
$imghideshow = "show.gif";
$hideshow = 0;
echo "<a title=\"$strrestore\" href=\"question.php?id=$question->id&amp;hide=0&amp;sesskey=$USER->sesskey\"><img
src=\"../../pix/t/restore.gif\" border=\"0\" alt=\"$strrestore\" /></a>";
} else {
$strhideshow = get_string("hide");
$imghideshow = "hide.gif";
$hideshow = 1;
echo "<a title=\"$strdelete\" href=\"question.php?id=$question->id&amp;delete=$question->id\"><img
src=\"../../pix/t/delete.gif\" border=\"0\" alt=\"$strdelete\" /></a>";
}
echo "<a title=\"$strhideshow\" href=\"question.php?id=$question->id&amp;hide=$hideshow&amp;sesskey=$USER->sesskey\"><img
src=\"../../pix/t/$imghideshow\" border=\"0\" alt=\"$strhideshow\" /></a>";
echo "<a title=\"$strdelete\" href=\"question.php?id=$question->id&amp;delete=$question->id\">\n<img
src=\"../../pix/t/delete.gif\" border=\"0\" alt=\"$strdelete\" /></a>";
if ($quizselected) {
echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
}
echo "</td>\n";
}
echo "<td>".$question->name."</td>\n";
if ($question->hidden) {
echo '<td class="dimmed_text">'.$question->name."</td>\n";
} else {
echo "<td>".$question->name."</td>\n";
}
echo "<td align=\"center\">\n";
quiz_print_question_icon($question, $canedit);
echo "</td>\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') {

View file

@ -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", "<i>$question->name</i>")."<p>".$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&amp;id=$question->id&amp;delete=$delete&amp;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&amp;id=$question->id&amp;delete=$delete&amp;confirm=".md5($delete), "edit.php");
}
print_footer($course);
exit;
}
if ($form = data_submitted() and confirm_sesskey()) {