libdir.'/formslib.php'); require_once($CFG->libdir . '/questionlib.php'); class delete_category_form extends moodleform { var $_category; function definition() { global $CFG; $mform =& $this->_form; $category = $this->_customdata; ensure_context_subobj_present($category, CONTEXT_COURSECAT); $this->_category = $category; /// Check permissions, to see if it OK to give the option to delete /// the contents, rather than move elsewhere. /// Are there any subcategories of this one, can they be deleted? $candeletecontent = true; $tocheck = get_child_categories($category->id); $containscategories = !empty($tocheck); $categoryids = array($category->id); while (!empty($tocheck)) { $checkcat = array_pop($tocheck); $childcategoryids[] = $checkcat->id; $tocheck = $tocheck + get_child_categories($checkcat->id); if ($candeletecontent && !has_capability('moodle/category:manage', $checkcat->context)) { $candeletecontent = false; } } /// Are there any courses in here, can they be deleted? $containedcourses = get_records_sql(" SELECT id,1 FROM {$CFG->prefix}course c WHERE c.category IN (" . implode(',', $categoryids) . ")"); $containscourses = false; if ($containedcourses) { $containscourses = true; foreach ($containedcourses as $courseid => $notused) { if ($candeletecontent && !can_delete_course($courseid)) { $candeletecontent = false; break; } } } /// Are there any questions in the question bank here? $containsquestions = question_context_has_any_questions($category->context); /// Get the list of categories we might be able to move to. $testcaps = array(); if ($containscourses) { $testcaps[] = 'moodle/course:create'; } if ($containscategories || $containsquestions) { $testcaps[] = 'moodle/category:manage'; } $displaylist = array(); $notused = array(); if (!empty($testcaps)) { make_categories_list($displaylist, $notused, $testcaps, $category->id); } /// Now build the options. $options = array(); if ($displaylist) { $options[0] = get_string('movecontentstoanothercategory'); } if ($candeletecontent) { $options[1] = get_string('deleteallcannotundo'); } /// Now build the form. $mform->addElement('header','general', get_string('categorycurrentcontents', '', format_string($category->name))); if ($containscourses || $containscategories || $containsquestions) { if (empty($options)) { print_error('youcannotdeletecategory', 'error', 'index.php', format_string($category->name)); } /// Describe the contents of this category. $contents = '