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.
This commit is contained in:
Eric Merrill 2014-05-29 17:19:37 -04:00 committed by Frederic Massart
parent 6a74e76fb8
commit 54640aba8a
2 changed files with 39 additions and 15 deletions

View File

@ -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';

View File

@ -81,21 +81,45 @@ if ($param->moveupcontext || $param->movedowncontext) {
// The previous line does a redirect().
}
if ($param->delete && ($questionstomove = $DB->count_records("question", array("category" => $param->delete)))) {
if (!$category = $DB->get_record("question_categories", array("id" => $param->delete))) { // security
print_error('nocate', 'question', $thispageurl->out(), $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();
}
$categorycontext = context::instance_by_id($category->contextid);
$qcobject->moveform = new question_move_form($thispageurl,
array('contexts'=>array($categorycontext), 'currentcat'=>$param->delete));
if ($qcobject->moveform->is_cancelled()){
redirect($thispageurl);
} elseif ($formdata = $qcobject->moveform->get_data()) {
/// 'confirm' is the category to move existing questions to
list($tocategoryid, $tocontextid) = explode(',', $formdata->category);
$qcobject->move_questions_and_delete_category($formdata->delete, $tocategoryid);
$thispageurl->remove_params('cat', 'category');
redirect($thispageurl);
// 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));
if ($qcobject->moveform->is_cancelled()){
redirect($thispageurl);
} elseif ($formdata = $qcobject->moveform->get_data()) {
/// 'confirm' is the category to move existing questions to
list($tocategoryid, $tocontextid) = explode(',', $formdata->category);
$qcobject->move_questions_and_delete_category($formdata->delete, $tocategoryid);
$thispageurl->remove_params('cat', 'category');
redirect($thispageurl);
}
} else {
$questionstomove = 0;
}
} else {
$questionstomove = 0;