function question_delete_course_category in Quiz 6.5
Same name and namespace in other branches
- 6.6 includes/moodle/lib/questionlib.php \question_delete_course_category()
Category is about to be deleted, 1/ All question categories and their questions are deleted for this course category. 2/ All questions are moved to new category
Parameters
object $category course category object:
object $newcategory empty means everything deleted, otherwise id of category where content moved:
boolean $feedback to specify if the process must output a summary of its work:
Return value
boolean
File
- includes/
moodle/ lib/ questionlib.php, line 565
Code
function question_delete_course_category($category, $newcategory, $feedback = true) {
$context = get_context_instance(CONTEXT_COURSECAT, $category->id);
if (empty($newcategory)) {
$feedbackdata = array();
// To store feedback to be showed at the end of the process
$rescueqcategory = null;
// See the code around the call to question_save_from_deletion.
$strcatdeleted = get_string('unusedcategorydeleted', 'quiz');
// Loop over question categories.
if ($categories = get_records('question_categories', 'contextid', $context->id, 'parent', 'id, parent, name')) {
foreach ($categories as $category) {
// Deal with any questions in the category.
if ($questions = get_records('question', 'category', $category->id)) {
// Try to delete each question.
foreach ($questions as $question) {
delete_question($question->id);
}
// Check to see if there were any questions that were kept because they are
// still in use somehow, even though quizzes in courses in this category will
// already have been deteted. This could happen, for example, if questions are
// added to a course, and then that course is moved to another category (MDL-14802).
$questionids = get_records_select_menu('question', 'category = ' . $category->id, '', 'id,1');
if (!empty($questionids)) {
if (!($rescueqcategory = question_save_from_deletion(implode(',', array_keys($questionids)), get_parent_contextid($context), print_context_name($context), $rescueqcategory))) {
return false;
}
$feedbackdata[] = array(
$category->name,
get_string('questionsmovedto', 'question', $rescueqcategory->name),
);
}
}
// Now delete the category.
if (!delete_records('question_categories', 'id', $category->id)) {
return false;
}
$feedbackdata[] = array(
$category->name,
$strcatdeleted,
);
}
// End loop over categories.
}
// Output feedback if requested.
if ($feedback and $feedbackdata) {
$table = new stdClass();
$table->head = array(
get_string('questioncategory', 'question'),
get_string('action'),
);
$table->data = $feedbackdata;
print_table($table);
}
}
else {
// Move question categories ot the new context.
if (!($newcontext = get_context_instance(CONTEXT_COURSECAT, $newcategory->id))) {
return false;
}
if (!set_field('question_categories', 'contextid', $newcontext->id, 'contextid', $context->id)) {
return false;
}
if ($feedback) {
$a = new stdClass();
$a->oldplace = print_context_name($context);
$a->newplace = print_context_name($newcontext);
notify(get_string('movedquestionsandcategories', 'question', $a), 'notifysuccess');
}
}
return true;
}