. /** * Displays the top level category or all courses * In editing mode, allows the admin to edit a category, * and rearrange courses * * @package core * @subpackage course * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once("../config.php"); require_once($CFG->dirroot.'/course/lib.php'); require_once($CFG->libdir.'/textlib.class.php'); $id = required_param('id', PARAM_INT); // Category id $page = optional_param('page', 0, PARAM_INT); // which page to show $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); $resort = optional_param('resort', 0, PARAM_BOOL); $sesskey = optional_param('sesskey', '', PARAM_RAW); // MDL-27824 - This is a temporary fix until we have the proper // way to check/initialize $CFG value. // @todo MDL-35138 remove this temporary solution if (!empty($CFG->coursesperpage)) { $defaultperpage = $CFG->coursesperpage; } else { $defaultperpage = 20; } $perpage = optional_param('perpage', $defaultperpage, PARAM_INT); // how many per page if (empty($id)) { print_error("unknowcategory"); } $PAGE->set_category_by_id($id); $PAGE->set_url(new moodle_url('/course/category.php', array('id' => $id))); // This is sure to be the category context $context = $PAGE->context; // And the object has been loaded for us no need for another DB call $category = $PAGE->category; $canedit = can_edit_in_category($category->id); if ($canedit) { if ($categoryedit !== -1) { $USER->editing = $categoryedit; } require_login(); $editingon = $PAGE->user_is_editing(); } else { if ($CFG->forcelogin) { require_login(); } $editingon = false; } if (!$category->visible) { require_capability('moodle/category:viewhiddencategories', $context); } $canmanage = has_capability('moodle/category:manage', $context); $sesskeyprovided = !empty($sesskey) && confirm_sesskey($sesskey); // Process any category actions. if ($canmanage && $resort && $sesskeyprovided) { // Resort the category if requested if ($courses = get_courses($category->id, '', 'c.id,c.fullname,c.sortorder')) { collatorlib::asort_objects_by_property($courses, 'fullname'); $i = 1; foreach ($courses as $course) { $DB->set_field('course', 'sortorder', $category->sortorder+$i, array('id'=>$course->id)); $i++; } fix_course_sortorder(); // should not be needed } } // Process any course actions. if ($editingon && $sesskeyprovided) { // Move a specified course to a new category if (!empty($moveto) and $data = data_submitted()) { // Some courses are being moved // user must have category update in both cats to perform this require_capability('moodle/category:manage', $context); require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveto)); if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) { print_error('cannotfindcategory', '', '', $data->moveto); } $courses = array(); foreach ($data as $key => $value) { if (preg_match('/^c\d+$/', $key)) { $courseid = substr($key, 1); array_push($courses, $courseid); // check this course's category if ($movingcourse = $DB->get_record('course', array('id'=>$courseid))) { if ($movingcourse->category != $id ) { print_error('coursedoesnotbelongtocategory'); } } else { print_error('cannotfindcourse'); } } } move_courses($courses, $data->moveto); } // Hide or show a course if (!empty($hide) or !empty($show)) { if (!empty($hide)) { $course = $DB->get_record('course', array('id' => $hide)); $visible = 0; } else { $course = $DB->get_record('course', array('id' => $show)); $visible = 1; } if ($course) { $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); require_capability('moodle/course:visibility', $coursecontext); // Set the visibility of the course. we set the old flag when user manually changes visibility of course. $DB->update_record('course', array('id' => $course->id, 'visible' => $visible, 'visibleold' => $visible, 'timemodified' => time())); } } // Move a course up or down if (!empty($moveup) or !empty($movedown)) { require_capability('moodle/category:manage', $context); // Ensure the course order has continuous ordering fix_course_sortorder(); $swapcourse = NULL; if (!empty($moveup)) { if ($movecourse = $DB->get_record('course', array('id' => $moveup))) { $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder - 1)); } } else { if ($movecourse = $DB->get_record('course', array('id' => $movedown))) { $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1)); } } if ($swapcourse and $movecourse) { // check course's category if ($movecourse->category != $id) { print_error('coursedoesnotbelongtocategory'); } $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id)); $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id)); } } } // End of editing stuff // Prepare the standard URL params for this page. We'll need them later. $urlparams = array('id' => $id); if ($page) { $urlparams['page'] = $page; } if ($perpage) { $urlparams['perpage'] = $perpage; } // Begin output if ($editingon && can_edit_in_category()) { // 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', '', $urlparams, $CFG->wwwroot . '/course/category.php'); $PAGE->set_context($context); // Ensure that we are actually showing blocks etc for the cat context $settingsnode = $PAGE->settingsnav->find_active_node(); if ($settingsnode) { $settingsnode->make_inactive(); $settingsnode->force_open(); $PAGE->navbar->add($settingsnode->text, $settingsnode->action); } echo $OUTPUT->header(); } else { $site = get_site(); $PAGE->set_title("$site->shortname: $category->name"); $PAGE->set_heading($site->fullname); $PAGE->set_button(print_course_search('', true, 'navbar')); $PAGE->set_pagelayout('coursecategory'); echo $OUTPUT->header(); } /// Print the category selector $displaylist = array(); $notused = array(); make_categories_list($displaylist, $notused); echo '