/** * 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 * * @param object|coursecat $category course category object * @param object|coursecat $newcategory empty means everything deleted, otherwise id of * category where content moved * @param boolean $feedback to specify if the process must output a summary of its work * @return boolean */ function question_delete_course_category($category, $newcategory, $feedback = true) { global $DB, $OUTPUT; $context = context_coursecat::instance($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 = $DB->get_records('question_categories', array('contextid' => $context->id), 'parent', 'id, parent, name')) { foreach ($categories as $category) { // Deal with any questions in the category. if ($questions = $DB->get_records('question', array('category' => $category->id), '', 'id,qtype')) { // Try to delete each question. foreach ($questions as $question) { 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 = $DB->get_records_menu('question', array('category' => $category->id), '', 'id, 1'); if (!empty($questionids)) { $parentcontextid = false; $parentcontext = $context->get_parent_context(); if ($parentcontext) { $parentcontextid = $parentcontext->id; } if (!($rescueqcategory = question_save_from_deletion(array_keys($questionids), $parentcontextid, $context->get_context_name(), $rescueqcategory))) { return false; } $feedbackdata[] = array($category->name, get_string('questionsmovedto', 'question', $rescueqcategory->name)); } } // Now delete the category. if (!$DB->delete_records('question_categories', array('id' => $category->id))) { return false; } $feedbackdata[] = array($category->name, $strcatdeleted); } // End loop over categories. } // Output feedback if requested. if ($feedback and $feedbackdata) { $table = new html_table(); $table->head = array(get_string('questioncategory', 'question'), get_string('action')); $table->data = $feedbackdata; echo html_writer::table($table); } } else { // Move question categories ot the new context. if (!($newcontext = context_coursecat::instance($newcategory->id))) { return false; } $DB->set_field('question_categories', 'contextid', $newcontext->id, array('contextid' => $context->id)); if ($feedback) { $a = new stdClass(); $a->oldplace = $context->get_context_name(); $a->newplace = $newcontext->get_context_name(); echo $OUTPUT->notification(get_string('movedquestionsandcategories', 'question', $a), 'notifysuccess'); } } return true; }
/** * 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 * * @param object $category course category object * @param object $newcategory empty means everything deleted, otherwise id of category where content moved * @param boolean $feedback to specify if the process must output a summary of its work * @return boolean */ 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; }
/** * Category is about to be deleted, * 1/ All questions are deleted for this question category. * 2/ Any questions that can't be deleted are moved to a new category * NOTE: this function is called from lib/db/upgrade.php * * @param object|coursecat $category course category object */ function question_category_delete_safe($category) { global $DB; $criteria = array('category' => $category->id); $context = context::instance_by_id($category->contextid, IGNORE_MISSING); $rescue = null; // See the code around the call to question_save_from_deletion. // Deal with any questions in the category. if ($questions = $DB->get_records('question', $criteria, '', 'id,qtype')) { // Try to delete each question. foreach ($questions as $question) { 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 deleted. This could // happen, for example, if questions are added to a course, // and then that course is moved to another category (MDL-14802). $questionids = $DB->get_records_menu('question', $criteria, '', 'id, 1'); if (!empty($questionids)) { $parentcontextid = SYSCONTEXTID; $name = get_string('unknown', 'question'); if ($context !== false) { $name = $context->get_context_name(); $parentcontext = $context->get_parent_context(); if ($parentcontext) { $parentcontextid = $parentcontext->id; } } question_save_from_deletion(array_keys($questionids), $parentcontextid, $name, $rescue); } } // Now delete the category. $DB->delete_records('question_categories', array('id' => $category->id)); }