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); $resort = optional_param('resort', 0, PARAM_BOOL); if (!$site = get_site()) { print_error('siteisnotdefined', 'debug'); } if (empty($id)) { print_error("unknowcategory"); } $PAGE->set_category_by_id($id); $urlparams = array('id' => $id); if ($page) { $urlparams['page'] = $page; } if ($perpage) { $urlparams['perpage'] = $perpage; } $PAGE->set_url('course/category.php', $urlparams); $context = $PAGE->context; $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); } // 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')) { $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) { /// Move a specified course to a new category if (!empty($moveto) and $data = data_submitted() and confirm_sesskey()) { // 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)) and confirm_sesskey()) { 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); if (!$DB->set_field('course', 'visible', $visible, array('id' => $course->id))) { print_error('errorupdatingcoursevisibility'); } } } /// Move a course up or down if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) { 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 // Print headings $numcategories = $DB->count_records('course_categories'); $stradministration = get_string('administration'); $strcategories = get_string('categories'); $strcategory = get_string('category'); $strcourses = get_string('courses'); 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 admin_externalpage_print_header(); } else { $PAGE->set_title("$site->shortname: $category->name"); $PAGE->set_heading("$site->fullname: $strcourses"); $PAGE->set_button(print_course_search('', true, 'navbar')); echo $OUTPUT->header(); } /// Print link to roles if (has_capability('moodle/role:assign', $context)) { echo ''; } /// Print the category selector $displaylist = array(); $notused = array(); make_categories_list($displaylist, $notused); echo '
'; $select = html_select::make_popup_form('category.php', 'id', $displaylist, 'switchcategory', $category->id); $select->set_label($strcategories.':'); $select->nothinglabel = false; echo $OUTPUT->select($select); echo '
'; /// Print current category description if (!$editingon && $category->description) { echo $OUTPUT->box_start(); echo format_text($category->description); // for multilang filter echo $OUTPUT->box_end(); } if ($editingon && has_capability('moodle/category:manage', $context)) { echo $OUTPUT->container_start('buttons'); // Print button to update this category $options = array('id' => $category->id); echo $OUTPUT->button(html_form::make_button($CFG->wwwroot.'/course/editcategory.php', $options, get_string('editcategorythis'), 'get')); // Print button for creating new categories $options = array('parent' => $category->id); echo $OUTPUT->button(html_form::make_button($CFG->wwwroot.'/course/editcategory.php', $options, get_string('addsubcategory'), 'get')); echo $OUTPUT->container_end(); } /// Print out all the sub-categories if ($subcategories = $DB->get_records('course_categories', array('parent' => $category->id), 'sortorder ASC')) { $firstentry = true; foreach ($subcategories as $subcategory) { if ($subcategory->visible || has_capability('moodle/category:viewhiddencategories', $context)) { $subcategorieswereshown = true; if ($firstentry) { echo ''; echo ''; echo '
'.get_string('subcategories').'
'; $firstentry = false; } $catlinkcss = $subcategory->visible ? '' : ' class="dimmed" '; echo ''. format_string($subcategory->name).'
'; } } if (!$firstentry) { echo '
'; echo '
'; } } /// Print out all the courses $courses = get_courses_page($category->id, 'c.sortorder ASC', 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible,c.guest,c.password', $totalcount, $page*$perpage, $perpage); $numcourses = count($courses); if (!$courses) { if (empty($subcategorieswereshown)) { echo $OUTPUT->heading(get_string("nocoursesyet")); } } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$editingon) { echo $OUTPUT->box_start('courseboxes'); print_courses($category); echo $OUTPUT->box_end(); } else { echo $OUTPUT->paging_bar(moodle_paging_bar::make($totalcount, $page, $perpage, "category.php?id=$category->id&perpage=$perpage")); $strcourses = get_string('courses'); $strselect = get_string('select'); $stredit = get_string('edit'); $strdelete = get_string('delete'); $strbackup = get_string('backup'); $strrestore = get_string('restore'); $strmoveup = get_string('moveup'); $strmovedown = get_string('movedown'); $strupdate = get_string('update'); $strhide = get_string('hide'); $strshow = get_string('show'); $strsummary = get_string('summary'); $strsettings = get_string('settings'); $strassignteachers = get_string('assignteachers'); $strallowguests = get_string('allowguests'); $strrequireskey = get_string('requireskey'); echo '
'; echo ''; echo ''; echo ''; if ($editingon) { echo ''; echo ''; } else { echo ''; } echo ''; $count = 0; $abletomovecourses = false; // for now // Checking if we are at the first or at the last page, to allow courses to // be moved up and down beyond the paging border if ($totalcount > $perpage) { $atfirstpage = ($page == 0); if ($perpage > 0) { $atlastpage = (($page + 1) == ceil($totalcount / $perpage)); } else { $atlastpage = true; } } else { $atfirstpage = true; $atlastpage = true; } $spacer = ' '; foreach ($courses as $acourse) { if (isset($acourse->context)) { $coursecontext = $acourse->context; } else { $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id); } $count++; $up = ($count > 1 || !$atfirstpage); $down = ($count < $numcourses || !$atlastpage); $linkcss = $acourse->visible ? '' : ' class="dimmed" '; echo ''; echo ''; if ($editingon) { echo ''; echo ''; } else { echo '"; } echo ""; } if ($abletomovecourses) { $movetocategories = array(); $notused = array(); make_categories_list($movetocategories, $notused, 'moodle/category:manage'); $movetocategories[$category->id] = get_string('moveselectedcoursesto'); echo ''; } echo '
'.$strcourses.''.$stredit.''.$strselect.' 
'. format_string($acourse->fullname) .''; if (has_capability('moodle/course:update', $coursecontext)) { echo ''. ''.$stredit.' '; } else { echo $spacer; } // role assignment link if (has_capability('moodle/role:assign', $coursecontext)) { echo ''. ''.get_string('assignroles', 'role').' '; } else { echo $spacer; } if (can_delete_course($acourse->id)) { echo ''. ''.$strdelete.' '; } else { echo $spacer; } // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) { if (!empty($acourse->visible)) { echo ''. ''.$strhide.' '; } else { echo ''. ''.$strshow.' '; } } else { echo $spacer; } if (has_capability('moodle/site:backup', $coursecontext)) { echo ''. ''.$strbackup.' '; } else { echo $spacer; } if (has_capability('moodle/site:restore', $coursecontext)) { echo ''. ''.$strrestore.' '; } else { echo $spacer; } if (has_capability('moodle/category:manage', $context)) { if ($up) { echo ''. ''.$strmoveup.' '; } else { echo $spacer; } if ($down) { echo ''. ''.$strmovedown.' '; } else { echo $spacer; } $abletomovecourses = true; } else { echo $spacer, $spacer; } echo ''; echo ''; echo ''; if (!empty($acourse->guest)) { echo ''.$strallowguests.''; } if (!empty($acourse->password)) { echo ''.$strrequireskey.''; } if (!empty($acourse->summary)) { $link = html_link::make("/course/info.php?id=$acourse->id", ''.get_string('info').''); $link->add_action(new popup_action('click', $link->url, 'courseinfo')); $link->title = $strsummary; echo $OUTPUT->link($link); } echo "
'; $select = new html_select(); $select->options = $movetocategories; $select->name = 'moveto'; $select->selectedvalue = $category->id; $select->add_action('change', 'submit_form_by_id', array('id' => 'movecourses')); echo $OUTPUT->select($select); echo ''; echo '
'; echo '
'; echo '
'; } echo '
'; if (has_capability('moodle/category:manage', $context) and $numcourses > 1) { /// Print button to re-sort courses by name unset($options); $options['id'] = $category->id; $options['resort'] = 'name'; $options['sesskey'] = sesskey(); echo $OUTPUT->button(html_form::make_button('category.php', $options, get_string('resortcoursesbyname'), 'get')); } if (has_capability('moodle/course:create', $context)) { /// Print button to create a new course unset($options); $options['category'] = $category->id; echo $OUTPUT->button(html_form::make_button('edit.php', $options, get_string('addnewcourse'), 'get')); } if (!empty($CFG->enablecourserequests) && $category->id == $CFG->enablecourserequests) { print_course_request_buttons(get_context_instance(CONTEXT_SYSTEM)); } echo '
'; print_course_search(); echo $OUTPUT->footer();