Merge branch 'MDL-38821_master' of git://github.com/dmonllao/moodle
[moodle.git] / course / manage.php
blob128e7a7a18a9a3e80b06a2762698d47534a05f1a
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Allows the admin to create, delete and rename course categories rearrange courses
20 * @package core
21 * @copyright 2013 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once("../config.php");
26 require_once($CFG->dirroot.'/course/lib.php');
27 require_once($CFG->libdir.'/coursecatlib.php');
29 // Category id.
30 $id = optional_param('id', 0, PARAM_INT);
31 // Which page to show.
32 $page = optional_param('page', 0, PARAM_INT);
33 // How many per page.
34 $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT);
36 // Actions to manage courses.
37 $hide = optional_param('hide', 0, PARAM_INT);
38 $show = optional_param('show', 0, PARAM_INT);
39 $moveup = optional_param('moveup', 0, PARAM_INT);
40 $movedown = optional_param('movedown', 0, PARAM_INT);
41 $moveto = optional_param('moveto', 0, PARAM_INT);
42 $resort = optional_param('resort', 0, PARAM_BOOL);
44 // Actions to manage categories.
45 $deletecat = optional_param('deletecat', 0, PARAM_INT);
46 $hidecat = optional_param('hidecat', 0, PARAM_INT);
47 $showcat = optional_param('showcat', 0, PARAM_INT);
48 $movecat = optional_param('movecat', 0, PARAM_INT);
49 $movetocat = optional_param('movetocat', -1, PARAM_INT);
50 $moveupcat = optional_param('moveupcat', 0, PARAM_INT);
51 $movedowncat = optional_param('movedowncat', 0, PARAM_INT);
53 require_login();
55 // Retrieve coursecat object
56 // This will also make sure that category is accessible and create default category if missing
57 $coursecat = coursecat::get($id);
59 if ($id) {
60 $PAGE->set_category_by_id($id);
61 $PAGE->set_url(new moodle_url('/course/manage.php', array('id' => $id)));
62 // This is sure to be the category context.
63 $context = $PAGE->context;
64 if (!can_edit_in_category($coursecat->id)) {
65 redirect(new moodle_url('/course/category.php', array('id' => $coursecat->id)));
67 } else {
68 $context = context_system::instance();
69 $PAGE->set_context($context);
70 $PAGE->set_url(new moodle_url('/course/manage.php'));
71 if (!can_edit_in_category()) {
72 redirect(new moodle_url('/course/index.php'));
76 $canmanage = has_capability('moodle/category:manage', $context);
78 // Process any category actions.
79 if (!empty($deletecat) and confirm_sesskey()) {
80 // Delete a category.
81 $cattodelete = coursecat::get($deletecat);
82 $context = context_coursecat::instance($deletecat);
83 require_capability('moodle/category:manage', $context);
84 require_capability('moodle/category:manage', get_category_or_system_context($cattodelete->parent));
86 $heading = get_string('deletecategory', 'moodle', format_string($cattodelete->name, true, array('context' => $context)));
88 require_once($CFG->dirroot.'/course/delete_category_form.php');
89 $mform = new delete_category_form(null, $cattodelete);
90 if ($mform->is_cancelled()) {
91 redirect(new moodle_url('/course/manage.php'));
94 // Start output.
95 echo $OUTPUT->header();
96 echo $OUTPUT->heading($heading);
98 if ($data = $mform->get_data()) {
99 // The form has been submit handle it.
100 if ($data->fulldelete == 1 && $cattodelete->can_delete_full()) {
101 $cattodeletename = $cattodelete->get_formatted_name();
102 $deletedcourses = $cattodelete->delete_full(true);
103 foreach ($deletedcourses as $course) {
104 echo $OUTPUT->notification(get_string('coursedeleted', '', $course->shortname), 'notifysuccess');
106 echo $OUTPUT->notification(get_string('coursecategorydeleted', '', $cattodeletename), 'notifysuccess');
107 echo $OUTPUT->continue_button(new moodle_url('/course/manage.php'));
109 } else if ($data->fulldelete == 0 && $cattodelete->can_move_content_to($data->newparent)) {
110 $cattodelete->delete_move($data->newparent, true);
111 echo $OUTPUT->continue_button(new moodle_url('/course/manage.php'));
112 } else {
113 // Some error in parameters (user is cheating?)
114 $mform->display();
116 } else {
117 // Display the form.
118 $mform->display();
120 // Finish output and exit.
121 echo $OUTPUT->footer();
122 exit();
125 if (!empty($movecat) and ($movetocat >= 0) and confirm_sesskey()) {
126 // Move a category to a new parent if required.
127 $cattomove = coursecat::get($movecat);
128 if ($cattomove->parent != $movetocat) {
129 if ($cattomove->can_change_parent($movetocat)) {
130 $cattomove->change_parent($movetocat);
131 } else {
132 print_error('cannotmovecategory');
137 // Hide or show a category.
138 if ($hidecat and confirm_sesskey()) {
139 $cattohide = coursecat::get($hidecat);
140 require_capability('moodle/category:manage', get_category_or_system_context($cattohide->parent));
141 $cattohide->hide();
142 } else if ($showcat and confirm_sesskey()) {
143 $cattoshow = coursecat::get($showcat);
144 require_capability('moodle/category:manage', get_category_or_system_context($cattoshow->parent));
145 $cattoshow->show();
148 if ((!empty($moveupcat) or !empty($movedowncat)) and confirm_sesskey()) {
149 // Move a category up or down.
150 fix_course_sortorder();
151 $swapcategory = null;
153 if (!empty($moveupcat)) {
154 require_capability('moodle/category:manage', context_coursecat::instance($moveupcat));
155 if ($movecategory = $DB->get_record('course_categories', array('id' => $moveupcat))) {
156 $params = array($movecategory->sortorder, $movecategory->parent);
157 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder<? AND parent=?", $params, 'sortorder DESC', '*', 0, 1)) {
158 $swapcategory = reset($swapcategory);
161 } else {
162 require_capability('moodle/category:manage', context_coursecat::instance($movedowncat));
163 if ($movecategory = $DB->get_record('course_categories', array('id' => $movedowncat))) {
164 $params = array($movecategory->sortorder, $movecategory->parent);
165 if ($swapcategory = $DB->get_records_select('course_categories', "sortorder>? AND parent=?", $params, 'sortorder ASC', '*', 0, 1)) {
166 $swapcategory = reset($swapcategory);
170 if ($swapcategory and $movecategory) {
171 $DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder, array('id' => $movecategory->id));
172 $DB->set_field('course_categories', 'sortorder', $movecategory->sortorder, array('id' => $swapcategory->id));
173 add_to_log(SITEID, "category", "move", "editcategory.php?id=$movecategory->id", $movecategory->id);
176 // Finally reorder courses.
177 fix_course_sortorder();
180 if ($coursecat->id && $canmanage && $resort && confirm_sesskey()) {
181 // Resort the category.
182 if ($courses = get_courses($coursecat->id, '', 'c.id,c.fullname,c.sortorder')) {
183 collatorlib::asort_objects_by_property($courses, 'fullname', collatorlib::SORT_NATURAL);
184 $i = 1;
185 foreach ($courses as $course) {
186 $DB->set_field('course', 'sortorder', $coursecat->sortorder + $i, array('id' => $course->id));
187 $i++;
189 // This should not be needed but we do it just to be safe.
190 fix_course_sortorder();
191 cache_helper::purge_by_event('changesincourse');
195 if (!empty($moveto) && ($data = data_submitted()) && confirm_sesskey()) {
196 // Move a specified course to a new category.
197 // User must have category update in both cats to perform this.
198 require_capability('moodle/category:manage', $context);
199 require_capability('moodle/category:manage', context_coursecat::instance($moveto));
201 if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) {
202 print_error('cannotfindcategory', '', '', $data->moveto);
205 $courses = array();
206 foreach ($data as $key => $value) {
207 if (preg_match('/^c\d+$/', $key)) {
208 $courseid = substr($key, 1);
209 array_push($courses, $courseid);
210 // Check this course's category.
211 if ($movingcourse = $DB->get_record('course', array('id' => $courseid))) {
212 if ($movingcourse->category != $id ) {
213 print_error('coursedoesnotbelongtocategory');
215 } else {
216 print_error('cannotfindcourse');
220 move_courses($courses, $data->moveto);
223 if ((!empty($hide) or !empty($show)) && confirm_sesskey()) {
224 // Hide or show a course.
225 if (!empty($hide)) {
226 $course = $DB->get_record('course', array('id' => $hide), '*', MUST_EXIST);
227 $visible = 0;
228 } else {
229 $course = $DB->get_record('course', array('id' => $show), '*', MUST_EXIST);
230 $visible = 1;
232 $coursecontext = context_course::instance($course->id);
233 require_capability('moodle/course:visibility', $coursecontext);
234 // Set the visibility of the course. we set the old flag when user manually changes visibility of course.
235 $params = array('id' => $course->id, 'visible' => $visible, 'visibleold' => $visible, 'timemodified' => time());
236 $DB->update_record('course', $params);
237 cache_helper::purge_by_event('changesincourse');
238 add_to_log($course->id, "course", ($visible ? 'show' : 'hide'), "edit.php?id=$course->id", $course->id);
241 if ((!empty($moveup) or !empty($movedown)) && confirm_sesskey()) {
242 // Move a course up or down.
243 require_capability('moodle/category:manage', $context);
245 // Ensure the course order has continuous ordering.
246 fix_course_sortorder();
247 $swapcourse = null;
249 if (!empty($moveup)) {
250 if ($movecourse = $DB->get_record('course', array('id' => $moveup))) {
251 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder - 1));
253 } else {
254 if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
255 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
258 if ($swapcourse and $movecourse) {
259 // Check course's category.
260 if ($movecourse->category != $id) {
261 print_error('coursedoesnotbelongtocategory');
263 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
264 $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
265 cache_helper::purge_by_event('changesincourse');
266 add_to_log($movecourse->id, "course", "move", "edit.php?id=$movecourse->id", $movecourse->id);
270 // Prepare the standard URL params for this page. We'll need them later.
271 $urlparams = array('id' => $id);
272 if ($page) {
273 $urlparams['page'] = $page;
275 if ($perpage) {
276 $urlparams['perpage'] = $perpage;
279 $PAGE->set_pagelayout('coursecategory');
281 if (can_edit_in_category()) {
282 // Integrate into the admin tree only if the user can edit categories at the top level,
283 // otherwise the admin block does not appear to this user, and you get an error.
284 require_once($CFG->libdir . '/adminlib.php');
285 if ($id) {
286 navigation_node::override_active_url(new moodle_url('/course/category.php', array('id' => $id)));
288 admin_externalpage_setup('coursemgmt', '', $urlparams, $CFG->wwwroot . '/course/manage.php');
289 $settingsnode = $PAGE->settingsnav->find_active_node();
290 if ($id && $settingsnode) {
291 $settingsnode->make_inactive();
292 $settingsnode->force_open();
293 $PAGE->navbar->add($settingsnode->text, $settingsnode->action);
295 } else {
296 $site = get_site();
297 $PAGE->set_title("$site->shortname: $coursecat->name");
298 $PAGE->set_heading($site->fullname);
299 $PAGE->set_button(print_course_search('', true, 'navbar'));
302 $displaylist[0] = get_string('top');
304 // Start output.
305 echo $OUTPUT->header();
307 if (!$coursecat->id) {
308 // Print out the categories with all the knobs.
309 $table = new html_table;
310 $table->id = 'coursecategories';
311 $table->attributes['class'] = 'admintable generaltable editcourse';
312 $table->head = array(
313 get_string('categories'),
314 get_string('courses'),
315 get_string('edit'),
316 get_string('movecategoryto'),
318 $table->colclasses = array(
319 'leftalign name',
320 'centeralign count',
321 'centeralign icons',
322 'leftalign actions'
324 $table->data = array();
326 print_category_edit($table, $coursecat);
328 echo html_writer::table($table);
329 } else {
330 // Print the category selector.
331 $select = new single_select(new moodle_url('/course/manage.php'), 'id', $displaylist, $coursecat->id, null, 'switchcategory');
332 $select->set_label(get_string('categories').':');
334 echo html_writer::start_tag('div', array('class' => 'categorypicker'));
335 echo $OUTPUT->render($select);
336 echo html_writer::end_tag('div');
339 if ($canmanage) {
340 echo $OUTPUT->container_start('buttons');
341 // Print button to update this category.
342 if ($id) {
343 $url = new moodle_url('/course/editcategory.php', array('id' => $id));
344 echo $OUTPUT->single_button($url, get_string('editcategorythis'), 'get');
347 // Print button for creating new categories.
348 $url = new moodle_url('/course/editcategory.php', array('parent' => $id));
349 if ($id) {
350 $title = get_string('addsubcategory');
351 } else {
352 $title = get_string('addnewcategory');
354 echo $OUTPUT->single_button($url, $title, 'get');
355 echo $OUTPUT->container_end();
358 if ($coursecat->id) {
359 // Print out all the sub-categories (plain mode).
360 // In order to view hidden subcategories the user must have the viewhiddencategories.
361 // capability in the current category..
362 if (has_capability('moodle/category:viewhiddencategories', $context)) {
363 $categorywhere = '';
364 } else {
365 $categorywhere = 'AND cc.visible = 1';
367 // We're going to preload the context for the subcategory as we know that we
368 // need it later on for formatting.
369 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
370 $sql = "SELECT cc.*, $ctxselect
371 FROM {course_categories} cc
372 JOIN {context} ctx ON cc.id = ctx.instanceid
373 WHERE cc.parent = :parentid AND
374 ctx.contextlevel = :contextlevel
375 $categorywhere
376 ORDER BY cc.sortorder ASC";
377 $subcategories = $DB->get_recordset_sql($sql, array('parentid' => $coursecat->id, 'contextlevel' => CONTEXT_COURSECAT));
378 // Prepare a table to display the sub categories.
379 $table = new html_table;
380 $table->attributes = array(
381 'border' => '0',
382 'cellspacing' => '2',
383 'cellpadding' => '4',
384 'class' => 'generalbox boxaligncenter category_subcategories'
386 $table->head = array(new lang_string('subcategories'));
387 $table->data = array();
388 $baseurl = new moodle_url('/course/manage.php');
389 foreach ($subcategories as $subcategory) {
390 // Preload the context we will need it to format the category name shortly.
391 context_helper::preload_from_record($subcategory);
392 $context = context_coursecat::instance($subcategory->id);
393 // Prepare the things we need to create a link to the subcategory.
394 $attributes = $subcategory->visible ? array() : array('class' => 'dimmed');
395 $text = format_string($subcategory->name, true, array('context' => $context));
396 // Add the subcategory to the table.
397 $baseurl->param('id', $subcategory->id);
398 $table->data[] = array(html_writer::link($baseurl, $text, $attributes));
401 $subcategorieswereshown = (count($table->data) > 0);
402 if ($subcategorieswereshown) {
403 echo html_writer::table($table);
406 $courses = get_courses_page($coursecat->id, 'c.sortorder ASC',
407 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
408 $totalcount, $page*$perpage, $perpage);
409 $numcourses = count($courses);
410 } else {
411 $subcategorieswereshown = true;
412 $courses = array();
413 $numcourses = $totalcount = 0;
416 if (!$courses) {
417 // There is no course to display.
418 if (empty($subcategorieswereshown)) {
419 echo $OUTPUT->heading(get_string("nocoursesyet"));
421 } else {
422 // Display a basic list of courses with paging/editing options.
423 $table = new html_table;
424 $table->attributes = array('border' => 0, 'cellspacing' => 0, 'cellpadding' => '4', 'class' => 'generalbox boxaligncenter');
425 $table->head = array(
426 get_string('courses'),
427 get_string('edit'),
428 get_string('select')
430 $table->colclasses = array(null, null, 'mdl-align');
431 $table->data = array();
433 $count = 0;
434 $abletomovecourses = false;
436 // Checking if we are at the first or at the last page, to allow courses to
437 // be moved up and down beyond the paging border.
438 if ($totalcount > $perpage) {
439 $atfirstpage = ($page == 0);
440 if ($perpage > 0) {
441 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
442 } else {
443 $atlastpage = true;
445 } else {
446 $atfirstpage = true;
447 $atlastpage = true;
450 $baseurl = new moodle_url('/course/manage.php', $urlparams + array('sesskey' => sesskey()));
451 foreach ($courses as $acourse) {
452 $coursecontext = context_course::instance($acourse->id);
454 $count++;
455 $up = ($count > 1 || !$atfirstpage);
456 $down = ($count < $numcourses || !$atlastpage);
458 $courseurl = new moodle_url('/course/view.php', array('id' => $acourse->id));
459 $attributes = array();
460 $attributes['class'] = $acourse->visible ? '' : 'dimmed';
461 $coursename = get_course_display_name_for_list($acourse);
462 $coursename = format_string($coursename, true, array('context' => $coursecontext));
463 $coursename = html_writer::link($courseurl, $coursename, $attributes);
465 $icons = array();
466 // Update course icon.
467 if (has_capability('moodle/course:update', $coursecontext)) {
468 $url = new moodle_url('/course/edit.php', array('id' => $acourse->id, 'category' => $id, 'returnto' => 'catmanage'));
469 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('settings')));
472 // Role assignment icon.
473 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
474 $url = new moodle_url('/enrol/users.php', array('id' => $acourse->id));
475 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/enrolusers', get_string('enrolledusers', 'enrol')));
478 // Delete course icon.
479 if (can_delete_course($acourse->id)) {
480 $url = new moodle_url('/course/delete.php', array('id' => $acourse->id));
481 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete')));
484 // Change visibility.
485 // Users with no capability to view hidden courses, should not be able to lock themselves out.
486 if (has_any_capability(array('moodle/course:visibility', 'moodle/course:viewhiddencourses'), $coursecontext)) {
487 if (!empty($acourse->visible)) {
488 $url = new moodle_url($baseurl, array('hide' => $acourse->id));
489 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/hide', get_string('hide')));
490 } else {
491 $url = new moodle_url($baseurl, array('show' => $acourse->id));
492 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/show', get_string('show')));
496 // Backup course icon.
497 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
498 $url = new moodle_url('/backup/backup.php', array('id' => $acourse->id));
499 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/backup', get_string('backup')));
502 // Restore course icon.
503 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
504 $url = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
505 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/restore', get_string('restore')));
508 if ($canmanage) {
509 if ($up) {
510 $url = new moodle_url($baseurl, array('moveup' => $acourse->id));
511 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup')));
513 if ($down) {
514 $url = new moodle_url($baseurl, array('movedown' => $acourse->id));
515 $icons[] = $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown')));
517 $abletomovecourses = true;
520 $table->data[] = new html_table_row(array(
521 new html_table_cell($coursename),
522 new html_table_cell(join('', $icons)),
523 new html_table_cell(html_writer::empty_tag('input', array('type' => 'checkbox', 'name' => 'c'.$acourse->id)))
527 if ($abletomovecourses) {
528 $movetocategories = coursecat::make_categories_list('moodle/category:manage');
529 $movetocategories[$id] = get_string('moveselectedcoursesto');
531 $cell = new html_table_cell();
532 $cell->colspan = 3;
533 $cell->attributes['class'] = 'mdl-right';
534 $cell->text = html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
535 $cell->text .= html_writer::select($movetocategories, 'moveto', $id, null, array('id'=>'movetoid', 'class' => 'autosubmit'));
536 $cell->text .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'id', 'value' => $id));
537 $PAGE->requires->yui_module('moodle-core-formautosubmit',
538 'M.core.init_formautosubmit',
539 array(array('selectid' => 'movetoid', 'nothing' => $id))
541 $table->data[] = new html_table_row(array($cell));
544 $actionurl = new moodle_url('/course/manage.php');
545 $pagingurl = new moodle_url('/course/manage.php', array('id' => $id, 'perpage' => $perpage));
547 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, $pagingurl);
548 echo html_writer::start_tag('form', array('id' => 'movecourses', 'action' => $actionurl, 'method' => 'post'));
549 echo html_writer::start_tag('div');
550 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
551 echo html_writer::table($table);
552 echo html_writer::end_tag('div');
553 echo html_writer::end_tag('form');
554 echo html_writer::empty_tag('br');
557 echo html_writer::start_tag('div', array('class' => 'buttons'));
558 if ($canmanage and $numcourses > 1) {
559 // Print button to re-sort courses by name.
560 $url = new moodle_url('/course/manage.php', array('id' => $id, 'resort' => 'name', 'sesskey' => sesskey()));
561 echo $OUTPUT->single_button($url, get_string('resortcoursesbyname'), 'get');
564 if (has_capability('moodle/course:create', $context)) {
565 // Print button to create a new course.
566 $url = new moodle_url('/course/edit.php');
567 if ($coursecat->id) {
568 $url->params(array('category' => $coursecat->id, 'returnto' => 'catmanage'));
569 } else {
570 $url->params(array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcatmanage'));
572 echo $OUTPUT->single_button($url, get_string('addnewcourse'), 'get');
575 if (!empty($CFG->enablecourserequests) && $id == $CFG->defaultrequestcategory) {
576 print_course_request_buttons(context_system::instance());
578 echo html_writer::end_tag('div');
580 print_course_search();
582 echo $OUTPUT->footer();
585 * Recursive function to print all the categories ready for editing.
587 * @param html_table $table The table to add data to.
588 * @param coursecat $category The category to render
589 * @param int $depth The depth of the category.
590 * @param bool $up True if this category can be moved up.
591 * @param bool $down True if this category can be moved down.
593 function print_category_edit(html_table $table, coursecat $category, $depth = -1, $up = false, $down = false) {
594 global $OUTPUT;
596 static $str = null;
598 if (is_null($str)) {
599 $str = new stdClass;
600 $str->edit = new lang_string('edit');
601 $str->delete = new lang_string('delete');
602 $str->moveup = new lang_string('moveup');
603 $str->movedown = new lang_string('movedown');
604 $str->edit = new lang_string('editthiscategory');
605 $str->hide = new lang_string('hide');
606 $str->show = new lang_string('show');
607 $str->cohorts = new lang_string('cohorts', 'cohort');
608 $str->spacer = $OUTPUT->spacer().' ';
611 if ($category->id) {
613 $categorycontext = context_coursecat::instance($category->id);
615 $attributes = array();
616 $attributes['class'] = $category->visible ? '' : 'dimmed';
617 $attributes['title'] = $str->edit;
618 $categoryurl = new moodle_url('/course/manage.php', array('id' => $category->id, 'sesskey' => sesskey()));
619 $categoryname = $category->get_formatted_name();
620 $categorypadding = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $depth);
621 $categoryname = $categorypadding . html_writer::link($categoryurl, $categoryname, $attributes);
623 $icons = array();
624 if (has_capability('moodle/category:manage', $categorycontext)) {
625 // Edit category.
626 $icons[] = $OUTPUT->action_icon(
627 new moodle_url('/course/editcategory.php', array('id' => $category->id)),
628 new pix_icon('t/edit', $str->edit, 'moodle', array('class' => 'iconsmall')),
629 null, array('title' => $str->edit)
631 // Delete category.
632 $icons[] = $OUTPUT->action_icon(
633 new moodle_url('/course/manage.php', array('deletecat' => $category->id, 'sesskey' => sesskey())),
634 new pix_icon('t/delete', $str->delete, 'moodle', array('class' => 'iconsmall')),
635 null, array('title' => $str->delete)
637 // Change visibility.
638 if (!empty($category->visible)) {
639 $icons[] = $OUTPUT->action_icon(
640 new moodle_url('/course/manage.php', array('hidecat' => $category->id, 'sesskey' => sesskey())),
641 new pix_icon('t/hide', $str->hide, 'moodle', array('class' => 'iconsmall')),
642 null, array('title' => $str->hide)
644 } else {
645 $icons[] = $OUTPUT->action_icon(
646 new moodle_url('/course/manage.php', array('showcat' => $category->id, 'sesskey' => sesskey())),
647 new pix_icon('t/show', $str->show, 'moodle', array('class' => 'iconsmall')),
648 null, array('title' => $str->show)
651 // Cohorts.
652 if (has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), $categorycontext)) {
653 $icons[] = $OUTPUT->action_icon(
654 new moodle_url('/cohort/index.php', array('contextid' => $categorycontext->id)),
655 new pix_icon('t/cohort', $str->cohorts, 'moodle', array('class' => 'iconsmall')),
656 null, array('title' => $str->cohorts)
659 // Move up/down.
660 if ($up) {
661 $icons[] = $OUTPUT->action_icon(
662 new moodle_url('/course/manage.php', array('moveupcat' => $category->id, 'sesskey' => sesskey())),
663 new pix_icon('t/up', $str->moveup, 'moodle', array('class' => 'iconsmall')),
664 null, array('title' => $str->moveup)
666 } else {
667 $icons[] = $str->spacer;
669 if ($down) {
670 $icons[] = $OUTPUT->action_icon(
671 new moodle_url('/course/manage.php', array('movedowncat' => $category->id, 'sesskey' => sesskey())),
672 new pix_icon('t/down', $str->movedown, 'moodle', array('class' => 'iconsmall')),
673 null, array('title' => $str->movedown)
675 } else {
676 $icons[] = $str->spacer;
680 $actions = '';
681 if (has_capability('moodle/category:manage', $categorycontext)) {
682 $popupurl = new moodle_url("manage.php?movecat=$category->id&sesskey=".sesskey());
683 $tempdisplaylist = array(0 => get_string('top')) + coursecat::make_categories_list('moodle/category:manage', $category->id);
684 $select = new single_select($popupurl, 'movetocat', $tempdisplaylist, $category->parent, null, "moveform$category->id");
685 $select->set_label(get_string('frontpagecategorynames'), array('class' => 'accesshide'));
686 $actions = $OUTPUT->render($select);
689 $table->data[] = new html_table_row(array(
690 // Category name.
691 new html_table_cell($categoryname),
692 // Course count.
693 new html_table_cell($category->coursecount),
694 // Icons.
695 new html_table_cell(join(' ', $icons)),
696 // Actions.
697 new html_table_cell($actions)
701 if ($categories = $category->get_children()) {
702 // Print all the children recursively.
703 $countcats = count($categories);
704 $count = 0;
705 $first = true;
706 $last = false;
707 foreach ($categories as $cat) {
708 $count++;
709 if ($count == $countcats) {
710 $last = true;
712 $up = $first ? false : true;
713 $down = $last ? false : true;
714 $first = false;
716 print_category_edit($table, $cat, $depth+1, $up, $down);