/** * Delete categories * * @param array $categories A list of category ids * @return array * @since Moodle 2.3 */ public static function delete_categories($categories) { global $CFG, $DB; require_once $CFG->dirroot . "/course/lib.php"; // Validate parameters. $params = self::validate_parameters(self::delete_categories_parameters(), array('categories' => $categories)); $transaction = $DB->start_delegated_transaction(); foreach ($params['categories'] as $category) { if (!($deletecat = $DB->get_record('course_categories', array('id' => $category['id'])))) { throw new moodle_exception('unknowcategory'); } $context = context_coursecat::instance($deletecat->id); require_capability('moodle/category:manage', $context); self::validate_context($context); self::validate_context(get_category_or_system_context($deletecat->parent)); if ($category['recursive']) { // If recursive was specified, then we recursively delete the category's contents. category_delete_full($deletecat, false); } else { // In this situation, we don't delete the category's contents, we either move it to newparent or parent. // If the parent is the root, moving is not supported (because a course must always be inside a category). // We must move to an existing category. if (!empty($category['newparent'])) { if (!$DB->record_exists('course_categories', array('id' => $category['newparent']))) { throw new moodle_exception('unknowcategory'); } $newparent = $category['newparent']; } else { $newparent = $deletecat->parent; } // This operation is not allowed. We must move contents to an existing category. if ($newparent == 0) { throw new moodle_exception('movecatcontentstoroot'); } $parentcontext = get_category_or_system_context($newparent); require_capability('moodle/category:manage', $parentcontext); self::validate_context($parentcontext); category_delete_move($deletecat, $newparent, false); } } $transaction->allow_commit(); }
echo $OUTPUT->heading($heading); $mform->display(); echo $OUTPUT->footer(); exit; } } echo $OUTPUT->header(); echo $OUTPUT->heading($heading); if ($data->fulldelete) { $deletedcourses = category_delete_full($deletecat, true); foreach ($deletedcourses as $course) { echo $OUTPUT->notification(get_string('coursedeleted', '', $course->shortname), 'notifysuccess'); } echo $OUTPUT->notification(get_string('coursecategorydeleted', '', format_string($deletecat->name)), 'notifysuccess'); } else { category_delete_move($deletecat, $data->newparent, true); } // If we deleted $CFG->defaultrequestcategory, make it point somewhere else. if ($delete == $CFG->defaultrequestcategory) { set_config('defaultrequestcategory', $DB->get_field('course_categories', 'MIN(id)', array('parent' => 0))); } echo $OUTPUT->continue_button('index.php'); echo $OUTPUT->footer(); die; } /// Create a default category if necessary if (!($categories = get_categories())) { /// No category yet! // Try and make one $tempcat = new stdClass(); $tempcat->name = get_string('miscellaneous');
redirect(new moodle_url('/course/manage.php')); } // Start output. echo $OUTPUT->header(); echo $OUTPUT->heading($heading); if ($data = $mform->get_data()) { // The form has been submit handle it. if ($data->fulldelete) { $deletedcourses = category_delete_full($cattodelete, true); foreach ($deletedcourses as $course) { echo $OUTPUT->notification(get_string('coursedeleted', '', $course->shortname), 'notifysuccess'); } $cattodeletename = format_string($cattodelete->name, true, array('context' => $context)); echo $OUTPUT->notification(get_string('coursecategorydeleted', '', $catetodeletename), 'notifysuccess'); } else { category_delete_move($cattodelete, $data->newparent, true); } if ($deletecat == $CFG->defaultrequestcategory) { // If we deleted $CFG->defaultrequestcategory, make it point somewhere else. set_config('defaultrequestcategory', $DB->get_field('course_categories', 'MIN(id)', array('parent' => 0))); } echo $OUTPUT->continue_button(new moodle_url('/course/manage.php')); } else { // Display the form. require_once $CFG->libdir . '/questionlib.php'; $mform->display(); } // Finish output and exit. echo $OUTPUT->footer(); exit; }