if ($mform->is_cancelled()) {
        redirect('index.php');
    } else {
        if (!($data = $mform->get_data())) {
            require_once $CFG->libdir . '/questionlib.php';
            echo $OUTPUT->header();
            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;
}
Example #2
0
 /**
  * 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();
 }
Example #3
0
/**
 * Recursively delete category including all subcategories and courses.
 * @param stdClass $category
 * @param boolean $showfeedback display some notices
 * @return array return deleted courses
 */
function category_delete_full($category, $showfeedback = true)
{
    global $CFG, $DB;
    require_once $CFG->libdir . '/gradelib.php';
    require_once $CFG->libdir . '/questionlib.php';
    require_once $CFG->dirroot . '/cohort/lib.php';
    if ($children = $DB->get_records('course_categories', array('parent' => $category->id), 'sortorder ASC')) {
        foreach ($children as $childcat) {
            category_delete_full($childcat, $showfeedback);
        }
    }
    $deletedcourses = array();
    if ($courses = $DB->get_records('course', array('category' => $category->id), 'sortorder ASC')) {
        foreach ($courses as $course) {
            if (!delete_course($course, false)) {
                throw new moodle_exception('cannotdeletecategorycourse', '', '', $course->shortname);
            }
            $deletedcourses[] = $course;
        }
    }
    // move or delete cohorts in this context
    cohort_delete_category($category);
    // now delete anything that may depend on course category context
    grade_course_category_delete($category->id, 0, $showfeedback);
    if (!question_delete_course_category($category, 0, $showfeedback)) {
        throw new moodle_exception('cannotdeletecategoryquestions', '', '', $category->name);
    }
    // finally delete the category and it's context
    $DB->delete_records('course_categories', array('id' => $category->id));
    delete_context(CONTEXT_COURSECAT, $category->id);
    events_trigger('course_category_deleted', $category);
    return $deletedcourses;
}
Example #4
0
/**
 * Recursively delete category including all subcategories and courses.
 * @param object $ccategory
 * @return bool status
 */
function category_delete_full($category, $showfeedback = true)
{
    global $CFG;
    require_once $CFG->libdir . '/gradelib.php';
    require_once $CFG->libdir . '/questionlib.php';
    if ($children = get_records('course_categories', 'parent', $category->id, 'sortorder ASC')) {
        foreach ($children as $childcat) {
            if (!category_delete_full($childcat, $showfeedback)) {
                notify("Error deleting category {$childcat->name}");
                return false;
            }
        }
    }
    if ($courses = get_records('course', 'category', $category->id, 'sortorder ASC')) {
        foreach ($courses as $course) {
            if (!delete_course($course->id, false)) {
                notify("Error deleting course {$course->shortname}");
                return false;
            }
            notify(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
        }
    }
    // now delete anything that may depend on course category context
    grade_course_category_delete($category->id, 0, $showfeedback);
    if (!question_delete_course_category($category, 0, $showfeedback)) {
        notify(get_string('errordeletingquestionsfromcategory', 'question', $category), 'notifysuccess');
        return false;
    }
    // finally delete the category and it's context
    delete_records('course_categories', 'id', $category->id);
    delete_context(CONTEXT_COURSECAT, $category->id);
    events_trigger('course_category_deleted', $category);
    notify(get_string('coursecategorydeleted', '', format_string($category->name)), 'notifysuccess');
    return true;
}
Example #5
0
    if ($mform->is_cancelled()) {
        redirect('index.php');
    } else {
        if (!($data = $mform->get_data(false))) {
            require_once $CFG->libdir . '/questionlib.php';
            print_category_edit_header();
            print_heading($heading);
            $mform->display();
            admin_externalpage_print_footer();
            exit;
        }
    }
    print_category_edit_header();
    print_heading($heading);
    if ($data->fulldelete) {
        category_delete_full($deletecat, true);
    } 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', get_field('course_categories', 'MIN(id)', 'parent', 0));
    }
    print_continue('index.php');
    admin_externalpage_print_footer();
    die;
}
/// Print headings
print_category_edit_header();
print_heading($strcategories);
/// Create a default category if necessary
Example #6
0
 require_capability('moodle/category:manage', $context);
 require_capability('moodle/category:manage', get_category_or_system_context($cattodelete->parent));
 $heading = get_string('deletecategory', 'moodle', format_string($cattodelete->name, true, array('context' => $context)));
 require_once $CFG->dirroot . '/course/delete_category_form.php';
 $mform = new delete_category_form(null, $cattodelete);
 $mform->set_data(array('deletecat' => $deletecat));
 if ($mform->is_cancelled()) {
     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.