diff --git a/lang/en/question.php b/lang/en/question.php index 766167f04e0..d245288a0e6 100644 --- a/lang/en/question.php +++ b/lang/en/question.php @@ -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'; diff --git a/question/category.php b/question/category.php index 0d6288710e8..39d5de99332 100644 --- a/question/category.php +++ b/question/category.php @@ -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;