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("\n SELECT id,1 FROM {$CFG->prefix}course c\n 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 = '<ul>'; if ($containscategories) { $contents .= '<li>' . get_string('subcategories') . '</li>'; } if ($containscourses) { $contents .= '<li>' . get_string('courses') . '</li>'; } if ($containsquestions) { $contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>'; } $contents .= '</ul>'; $mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), $contents); /// Give the options for what to do. $mform->addElement('select', 'fulldelete', get_string('whattodo'), $options); if (count($options) == 1) { $mform->hardFreeze('fulldelete'); $mform->setConstant('fulldelete', reset(array_keys($options))); } if ($displaylist) { $mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist); if (in_array($category->parent, $displaylist)) { $mform->setDefault('newparent', $category->parent); } $mform->disabledIf('newparent', 'fulldelete', 'eq', '1'); } } else { $mform->addElement('hidden', 'fulldelete', 1); $mform->setType('fulldelete', PARAM_INT); $mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty')); } $mform->addElement('hidden', 'delete'); $mform->setType('delete', PARAM_ALPHANUM); $mform->addElement('hidden', 'sure'); $mform->setType('sure', PARAM_ALPHANUM); $mform->setDefault('sure', md5(serialize($category))); //-------------------------------------------------------------------------------- $this->add_action_buttons(true, get_string('delete')); }
/** * This function recursively travels the categories, building up a nice list * for display. It also makes an array that list all the parents for each * category. * * For example, if you have a tree of categories like: * Miscellaneous (id = 1) * Subcategory (id = 2) * Sub-subcategory (id = 4) * Other category (id = 3) * Then after calling this function you will have * $list = array(1 => 'Miscellaneous', 2 => 'Miscellaneous / Subcategory', * 4 => 'Miscellaneous / Subcategory / Sub-subcategory', * 3 => 'Other category'); * $parents = array(2 => array(1), 4 => array(1, 2)); * * If you specify $requiredcapability, then only categories where the current * user has that capability will be added to $list, although all categories * will still be added to $parents, and if you only have $requiredcapability * in a child category, not the parent, then the child catgegory will still be * included. * * If you specify the option $excluded, then that category, and all its children, * are omitted from the tree. This is useful when you are doing something like * moving categories, where you do not want to allow people to move a category * to be the child of itself. * * @param array $list For output, accumulates an array categoryid => full category path name * @param array $parents For output, accumulates an array categoryid => list of parent category ids. * @param string/array $requiredcapability if given, only categories where the current * user has this capability will be added to $list. Can also be an array of capabilities, * in which case they are all required. * @param integer $excludeid Omit this category and its children from the lists built. * @param object $category Build the tree starting at this category - otherwise starts at the top level. * @param string $path For internal use, as part of recursive calls. */ function make_categories_list(&$list, &$parents, $requiredcapability = '', $excludeid = 0, $category = NULL, $path = "") { // initialize the arrays if needed if (!is_array($list)) { $list = array(); } if (!is_array($parents)) { $parents = array(); } if (empty($category)) { // Start at the top level. $category = new stdClass(); $category->id = 0; } else { // This is the excluded category, don't include it. if ($excludeid > 0 && $excludeid == $category->id) { return; } // Update $path. if ($path) { $path = $path . ' / ' . format_string($category->name); } else { $path = format_string($category->name); } // Add this category to $list, if the permissions check out. if (empty($requiredcapability)) { $list[$category->id] = $path; } else { ensure_context_subobj_present($category, CONTEXT_COURSECAT); $requiredcapability = (array) $requiredcapability; if (has_all_capabilities($requiredcapability, $category->context)) { $list[$category->id] = $path; } } } // Add all the children recursively, while updating the parents array. if ($categories = get_child_categories($category->id)) { foreach ($categories as $cat) { if (!empty($category->id)) { if (isset($parents[$category->id])) { $parents[$cat->id] = $parents[$category->id]; } $parents[$cat->id][] = $category->id; } make_categories_list($list, $parents, $requiredcapability, $excludeid, $cat, $path); } } }