MDL-47787 course: Added question delete to cm delete.

This commit is contained in:
Tyler Bannister
2015-06-25 16:36:50 -04:00
parent 47cbd1b42d
commit d45b318a5d
5 changed files with 473 additions and 149 deletions
+95 -127
View File
@@ -261,6 +261,52 @@ function question_category_isused($categoryid, $recursive = false) {
'Please use question_category_in_use instead.');
}
/**
* 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));
}
/**
* Tests whether any question in a category is used by any part of Moodle.
*
@@ -346,6 +392,36 @@ function question_delete_question($questionid) {
question_bank::notify_question_edited($questionid);
}
/**
* All question categories and their questions are deleted for this context id.
*
* @param object $contextid The contextid to delete question categories from
* @return array Feedback from deletes (if any)
*/
function question_delete_context($contextid) {
global $DB;
//To store feedback to be showed at the end of the process
$feedbackdata = array();
//Cache some strings
$strcatdeleted = get_string('unusedcategorydeleted', 'question');
$fields = 'id, parent, name, contextid';
if ($categories = $DB->get_records('question_categories', array('contextid' => $contextid), 'parent', $fields)) {
//Sort categories following their tree (parent-child) relationships
//this will make the feedback more readable
$categories = sort_categories_by_tree($categories);
foreach ($categories as $category) {
question_category_delete_safe($category);
//Fill feedback
$feedbackdata[] = array($category->name, $strcatdeleted);
}
}
return $feedbackdata;
}
/**
* All question categories and their questions are deleted for this course.
*
@@ -354,47 +430,15 @@ function question_delete_question($questionid) {
* @return boolean
*/
function question_delete_course($course, $feedback=true) {
global $DB, $OUTPUT;
//To store feedback to be showed at the end of the process
$feedbackdata = array();
//Cache some strings
$strcatdeleted = get_string('unusedcategorydeleted', 'question');
$coursecontext = context_course::instance($course->id);
$categoriescourse = $DB->get_records('question_categories',
array('contextid' => $coursecontext->id), 'parent', 'id, parent, name, contextid');
$feedbackdata = question_delete_context($coursecontext->id, $feedback);
if ($categoriescourse) {
//Sort categories following their tree (parent-child) relationships
//this will make the feedback more readable
$categoriescourse = sort_categories_by_tree($categoriescourse);
foreach ($categoriescourse as $category) {
//Delete it completely (questions and category itself)
//deleting questions
if ($questions = $DB->get_records('question',
array('category' => $category->id), '', 'id,qtype')) {
foreach ($questions as $question) {
question_delete_question($question->id);
}
$DB->delete_records("question", array("category" => $category->id));
}
//delete the category
$DB->delete_records('question_categories', array('id' => $category->id));
//Fill feedback
$feedbackdata[] = array($category->name, $strcatdeleted);
}
//Inform about changes performed if feedback is enabled
if ($feedback) {
$table = new html_table();
$table->head = array(get_string('category', 'question'), get_string('action'));
$table->data = $feedbackdata;
echo html_writer::table($table);
}
// Inform about changes performed if feedback is enabled.
if ($feedback && $feedbackdata) {
$table = new html_table();
$table->head = array(get_string('category', 'question'), get_string('action'));
$table->data = $feedbackdata;
echo html_writer::table($table);
}
return true;
}
@@ -415,58 +459,10 @@ function question_delete_course_category($category, $newcategory, $feedback=true
$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', 'question');
// 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 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',
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.
}
$feedbackdata = question_delete_context($context->id, $feedback);
// Output feedback if requested.
if ($feedback and $feedbackdata) {
if ($feedback && $feedbackdata) {
$table = new html_table();
$table->head = array(get_string('questioncategory', 'question'), get_string('action'));
$table->data = $feedbackdata;
@@ -501,7 +497,7 @@ function question_delete_course_category($category, $newcategory, $feedback=true
* Enter description here...
*
* @param array $questionids of question ids
* @param object $newcontext the context to create the saved category in.
* @param object $newcontextid the context to create the saved category in.
* @param string $oldplace a textual description of the think being deleted,
* e.g. from get_context_name
* @param object $newcategory
@@ -538,44 +534,16 @@ function question_save_from_deletion($questionids, $newcontextid, $oldplace,
* @return boolean
*/
function question_delete_activity($cm, $feedback=true) {
global $DB, $OUTPUT;
global $DB;
//To store feedback to be showed at the end of the process
$feedbackdata = array();
//Cache some strings
$strcatdeleted = get_string('unusedcategorydeleted', 'question');
$modcontext = context_module::instance($cm->id);
if ($categoriesmods = $DB->get_records('question_categories',
array('contextid' => $modcontext->id), 'parent', 'id, parent, name, contextid')) {
//Sort categories following their tree (parent-child) relationships
//this will make the feedback more readable
$categoriesmods = sort_categories_by_tree($categoriesmods);
foreach ($categoriesmods as $category) {
//Delete it completely (questions and category itself)
//deleting questions
if ($questions = $DB->get_records('question',
array('category' => $category->id), '', 'id,qtype')) {
foreach ($questions as $question) {
question_delete_question($question->id);
}
$DB->delete_records("question", array("category"=>$category->id));
}
//delete the category
$DB->delete_records('question_categories', array('id'=>$category->id));
//Fill feedback
$feedbackdata[] = array($category->name, $strcatdeleted);
}
//Inform about changes performed if feedback is enabled
if ($feedback) {
$table = new html_table();
$table->head = array(get_string('category', 'question'), get_string('action'));
$table->data = $feedbackdata;
echo html_writer::table($table);
}
$feedbackdata = question_delete_context($modcontext->id, $feedback);
// Inform about changes performed if feedback is enabled.
if ($feedback && $feedbackdata) {
$table = new html_table();
$table->head = array(get_string('category', 'question'), get_string('action'));
$table->data = $feedbackdata;
echo html_writer::table($table);
}
return true;
}
@@ -1397,7 +1365,7 @@ function question_has_capability_on($question, $cap, $cachecat = -1) {
if (!isset($categories[$question->category])) {
if (!$categories[$question->category] = $DB->get_record('question_categories',
array('id'=>$question->category))) {
print_error('invalidcategory', 'quiz');
print_error('invalidcategory', 'question');
}
}
$category = $categories[$question->category];