Index: course/delete_category_form.php =================================================================== RCS file: /cvsroot/moodle/moodle/course/delete_category_form.php,v retrieving revision 1.1.2.1 diff -u -r1.1.2.1 delete_category_form.php --- course/delete_category_form.php 13 May 2008 21:51:46 -0000 1.1.2.1 +++ course/delete_category_form.php 3 Dec 2008 09:50:47 -0000 @@ -11,36 +11,30 @@ $mform =& $this->_form; $category = $this->_customdata; + ensure_context_subobj_present($category, CONTEXT_COURSECAT); $this->_category = $category; $mform->addElement('header','general', get_string('categorycurrentcontents', '', format_string($category->name))); $displaylist = array(); - $parentlist = array(); - $children = array(); - make_categories_list($displaylist, $parentlist); - unset($displaylist[$category->id]); - foreach ($displaylist as $catid=>$unused) { - // remove all children of $category - if (isset($parentlist[$catid]) and in_array($category->id, $parentlist[$catid])) { - $children[] = $catid; - unset($displaylist[$catid]); - continue; - } - if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $catid))) { - unset($displaylist[$catid]); - } - } + $notused = array(); + make_categories_list($displaylist, $notused, 'moodle/course:create', $category->id); + // Check permissions, to see if it OK to give the option to delete + // the contents, rather than move elsewhere. $candeletecontent = true; - foreach ($children as $catid) { - $context = get_context_instance(CONTEXT_COURSECAT, $catid); - if (!has_capability('moodle/category:delete', $context)) { + $tocheck = array($category); + while (!empty($tocheck)) { + $checkcat = array_pop($tocheck); + $tocheck = $tocheck + get_child_categories($checkcat->id); + if (!has_capability('moodle/category:manage', $checkcat->context)) { $candeletecontent = false; break; } } + // TODO check that the user is allowed to delete all the courses MDL-17502! + $options = array(); if ($displaylist) { @@ -48,7 +42,7 @@ } if ($candeletecontent) { - $options[1] =get_string('delete'); + $options[1] = get_string('delete'); } if (empty($options)) { Index: course/category.php =================================================================== RCS file: /cvsroot/moodle/moodle/course/category.php,v retrieving revision 1.119.2.9 diff -u -r1.119.2.9 category.php --- course/category.php 6 Oct 2008 22:29:00 -0000 1.119.2.9 +++ course/category.php 3 Dec 2008 09:50:47 -0000 @@ -6,25 +6,23 @@ require_once("../config.php"); require_once("lib.php"); - $id = required_param('id', PARAM_INT); // Category id - $page = optional_param('page', 0, PARAM_INT); // which page to show - $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT); // how many per page + $id = required_param('id', PARAM_INT); // Category id + $page = optional_param('page', 0, PARAM_INT); // which page to show + $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT); // how many per page $categoryedit = optional_param('categoryedit', -1, PARAM_BOOL); - $hide = optional_param('hide', 0, PARAM_INT); - $show = optional_param('show', 0, PARAM_INT); - $moveup = optional_param('moveup', 0, PARAM_INT); - $movedown = optional_param('movedown', 0, PARAM_INT); - $moveto = optional_param('moveto', 0, PARAM_INT); - $rename = optional_param('rename', '', PARAM_TEXT); - $resort = optional_param('resort', 0, PARAM_BOOL); - $categorytheme= optional_param('categorytheme', false, PARAM_SAFEDIR); + $hide = optional_param('hide', 0, PARAM_INT); + $show = optional_param('show', 0, PARAM_INT); + $moveup = optional_param('moveup', 0, PARAM_INT); + $movedown = optional_param('movedown', 0, PARAM_INT); + $moveto = optional_param('moveto', 0, PARAM_INT); + $resort = optional_param('resort', 0, PARAM_BOOL); if ($CFG->forcelogin) { require_login(); } if (!$site = get_site()) { - error("Site isn't defined!"); + error('Site isn\'t defined!'); } if (empty($id)) { @@ -38,45 +36,24 @@ if (!$category = get_record("course_categories", "id", $id)) { error("Category not known!"); } + if (!$category->visible) { + require_capability('moodle/category:viewhiddencategories', $context); + } - if (has_capability('moodle/course:create', $context)) { + if (update_category_button($category->id)) { if ($categoryedit !== -1) { $USER->categoryediting = $categoryedit; } - $navbaritem = update_category_button($category->id); - $creatorediting = !empty($USER->categoryediting); - $adminediting = (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)) and $creatorediting); - + $editingon = !empty($USER->categoryediting); + $navbaritem = update_category_button($category->id); // Must call this again after updating the state. } else { - if (!$category->visible) { - print_error('notavailable', 'error'); - } $navbaritem = print_course_search("", true, "navbar"); - $adminediting = false; - $creatorediting = false; + $editingon = false; } - if (has_capability('moodle/category:update', $context)) { - /// Rename the category if requested - if (!empty($rename) and confirm_sesskey()) { - if (! set_field("course_categories", "name", $rename, "id", $category->id)) { - notify("An error occurred while renaming the category"); - } - $category->name = stripslashes($rename); - //trigger events - events_trigger('course_category_updated', $category); - } - - /// Set the category theme if requested - if (($categorytheme !== false) and confirm_sesskey()) { - $category->theme = $categorytheme; - if (! set_field('course_categories', 'theme', $category->theme, 'id', $category->id)) { - notify('An error occurred while setting the theme'); - } - } - + // Process any category actions. + if (has_capability('moodle/category:manage', $context)) { /// Resort the category if requested - if ($resort and confirm_sesskey()) { if ($courses = get_courses($category->id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) { // move it off the range @@ -94,88 +71,76 @@ } } - if(! empty($CFG->allowcategorythemes) ){ - if(isset($category->theme)){ - // specifying theme here saves us some dbqs - theme_setup($category->theme); - } + if(!empty($CFG->allowcategorythemes) && isset($category->theme)) { + // specifying theme here saves us some dbqs + theme_setup($category->theme); } /// Print headings + $numcategories = count_records('course_categories'); - $numcategories = count_records("course_categories"); - - $stradministration = get_string("administration"); - $strcategories = get_string("categories"); - $strcategory = get_string("category"); - $strcourses = get_string("courses"); + $stradministration = get_string('administration'); + $strcategories = get_string('categories'); + $strcategory = get_string('category'); + $strcourses = get_string('courses'); $navlinks = array(); $navlinks[] = array('name' => $strcategories, 'link' => 'index.php', 'type' => 'misc'); $navlinks[] = array('name' => format_string($category->name), 'link' => null, 'type' => 'misc'); $navigation = build_navigation($navlinks); - if ($creatorediting) { - if ($adminediting) { - // modify this to treat this as an admin page - - require_once($CFG->libdir.'/adminlib.php'); - admin_externalpage_setup('coursemgmt'); - admin_externalpage_print_header(); - } else { - print_header("$site->shortname: $category->name", "$site->fullname: $strcourses", $navigation, "", "", true, $navbaritem); - } + if ($editingon && update_category_button()) { + // Integrate into the admin tree only if the user can edit categories at the top level, + // otherwise the admin block does not appear to this user, and you get an error. + require_once($CFG->libdir.'/adminlib.php'); + admin_externalpage_setup('coursemgmt'); + admin_externalpage_print_header(); } else { - print_header("$site->shortname: $category->name", "$site->fullname: $strcourses", $navigation, "", "", true, $navbaritem); + print_header("$site->shortname: $category->name", "$site->fullname: $strcourses", $navigation, '', '', true, $navbaritem); } + print_heading(format_string($category->name)); /// Print button to turn editing off - if ($adminediting) { + if ($editingon) { echo '
'; } /// Print link to roles - if (has_capability('moodle/role:assign', $context)) { echo ''; } -/// Print the category selector +/// Print the category selector $displaylist = array(); - $parentlist = array(); - - make_categories_list($displaylist, $parentlist, ""); + $notused = array(); + make_categories_list($displaylist, $notused); echo '