Commit 54640aba authored by Eric Merrill's avatar Eric Merrill Committed by Frederic Massart
Browse files

MDL-29905 question: Try to delete unused hidden/random questions

Before going to display the warning about moving in use questions,
try to delete unused Random qtype questions, as well as 'hidden'
questions (these are questions that were deleted from the category),
but were in use at the time.
parent 6a74e76f
......@@ -65,7 +65,7 @@ $string['categorycurrent'] = 'Current category';
$string['categorycurrentuse'] = 'Use this category';
$string['categorydoesnotexist'] = 'This category does not exist';
$string['categoryinfo'] = 'Category info';
$string['categorymove'] = 'The category \'{$a->name}\' contains {$a->count} questions (some of them may be old, hidden, questions that are still in use in some existing quizzes). Please choose another category to move them to.';
$string['categorymove'] = 'The category \'{$a->name}\' contains {$a->count} questions (some of them may be old, hidden, questions, or Random questions that are still in use in some existing quizzes). Please choose another category to move them to.';
$string['categorymoveto'] = 'Save in category';
$string['categorynamecantbeblank'] = 'The category name cannot be blank.';
$string['clickflag'] = 'Flag question';
......
......@@ -81,10 +81,31 @@ if ($param->moveupcontext || $param->movedowncontext) {
// The previous line does a redirect().
}
if ($param->delete && ($questionstomove = $DB->count_records("question", array("category" => $param->delete)))) {
if ($param->delete) {
$questionstomove = $DB->count_records("question", array("category" => $param->delete));
// First pass, try and remove unused random or hidden questions in the category.
if ($questionstomove) {
if (!$category = $DB->get_record("question_categories", array("id" => $param->delete))) { // security
print_error('nocate', 'question', $thispageurl->out(), $param->delete);
}
$select = "category = ? AND (qtype = 'random' OR hidden = 1)";
$questions = $DB->get_recordset_select("question", $select, array("category" => $param->delete), '', 'id');
if ($questions->valid()) {
$question = $questions->current();
if (question_has_capability_on($question->id, 'edit')) {
foreach ($questions as $question) {
question_delete_question($question->id);
}
$questionstomove = $DB->count_records("question", array("category" => $param->delete));
}
}
$questions->close();
}
// Second pass, if we still have questions to move, setup the form .
if ($questionstomove) {
$categorycontext = context::instance_by_id($category->contextid);
$qcobject->moveform = new question_move_form($thispageurl,
array('contexts'=>array($categorycontext), 'currentcat'=>$param->delete));
......@@ -97,6 +118,9 @@ if ($param->delete && ($questionstomove = $DB->count_records("question", array("
$thispageurl->remove_params('cat', 'category');
redirect($thispageurl);
}
} else {
$questionstomove = 0;
}
} else {
$questionstomove = 0;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment