MDL-35669 gravatar Provide default image URL to Gravatar
[moodle.git] / course / category.php
blobc148d66c333a9cdf253eb81571d129bcd8786999
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 * Displays the top level category or all courses
19 * In editing mode, allows the admin to edit a category,
20 * and rearrange courses
22 * @package core
23 * @subpackage course
24 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once("../config.php");
29 require_once($CFG->dirroot.'/course/lib.php');
30 require_once($CFG->libdir.'/textlib.class.php');
32 $id = required_param('id', PARAM_INT); // Category id
33 $page = optional_param('page', 0, PARAM_INT); // which page to show
34 $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT); // how many per page
35 $categoryedit = optional_param('categoryedit', -1, PARAM_BOOL);
36 $hide = optional_param('hide', 0, PARAM_INT);
37 $show = optional_param('show', 0, PARAM_INT);
38 $moveup = optional_param('moveup', 0, PARAM_INT);
39 $movedown = optional_param('movedown', 0, PARAM_INT);
40 $moveto = optional_param('moveto', 0, PARAM_INT);
41 $resort = optional_param('resort', 0, PARAM_BOOL);
42 $sesskey = optional_param('sesskey', '', PARAM_RAW);
44 if (empty($id)) {
45 print_error("unknowcategory");
48 $PAGE->set_category_by_id($id);
49 $PAGE->set_url(new moodle_url('/course/category.php', array('id' => $id)));
50 // This is sure to be the category context
51 $context = $PAGE->context;
52 // And the object has been loaded for us no need for another DB call
53 $category = $PAGE->category;
55 $canedit = can_edit_in_category($category->id);
56 if ($canedit) {
57 if ($categoryedit !== -1) {
58 $USER->editing = $categoryedit;
60 require_login();
61 $editingon = $PAGE->user_is_editing();
62 } else {
63 if ($CFG->forcelogin) {
64 require_login();
66 $editingon = false;
69 if (!$category->visible) {
70 require_capability('moodle/category:viewhiddencategories', $context);
73 $canmanage = has_capability('moodle/category:manage', $context);
74 $sesskeyprovided = !empty($sesskey) && confirm_sesskey($sesskey);
76 // Process any category actions.
77 if ($canmanage && $resort && $sesskeyprovided) {
78 // Resort the category if requested
79 if ($courses = get_courses($category->id, '', 'c.id,c.fullname,c.sortorder')) {
80 collatorlib::asort_objects_by_property($courses, 'fullname');
81 $i = 1;
82 foreach ($courses as $course) {
83 $DB->set_field('course', 'sortorder', $category->sortorder+$i, array('id'=>$course->id));
84 $i++;
86 fix_course_sortorder(); // should not be needed
90 // Process any course actions.
91 if ($editingon && $sesskeyprovided) {
93 // Move a specified course to a new category
94 if (!empty($moveto) and $data = data_submitted()) {
95 // Some courses are being moved
96 // user must have category update in both cats to perform this
97 require_capability('moodle/category:manage', $context);
98 require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveto));
100 if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) {
101 print_error('cannotfindcategory', '', '', $data->moveto);
104 $courses = array();
105 foreach ($data as $key => $value) {
106 if (preg_match('/^c\d+$/', $key)) {
107 $courseid = substr($key, 1);
108 array_push($courses, $courseid);
110 // check this course's category
111 if ($movingcourse = $DB->get_record('course', array('id'=>$courseid))) {
112 if ($movingcourse->category != $id ) {
113 print_error('coursedoesnotbelongtocategory');
115 } else {
116 print_error('cannotfindcourse');
120 move_courses($courses, $data->moveto);
123 // Hide or show a course
124 if (!empty($hide) or !empty($show)) {
125 if (!empty($hide)) {
126 $course = $DB->get_record('course', array('id' => $hide));
127 $visible = 0;
128 } else {
129 $course = $DB->get_record('course', array('id' => $show));
130 $visible = 1;
133 if ($course) {
134 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
135 require_capability('moodle/course:visibility', $coursecontext);
136 // Set the visibility of the course. we set the old flag when user manually changes visibility of course.
137 $DB->update_record('course', array('id' => $course->id, 'visible' => $visible, 'visibleold' => $visible, 'timemodified' => time()));
142 // Move a course up or down
143 if (!empty($moveup) or !empty($movedown)) {
144 require_capability('moodle/category:manage', $context);
146 // Ensure the course order has continuous ordering
147 fix_course_sortorder();
148 $swapcourse = NULL;
150 if (!empty($moveup)) {
151 if ($movecourse = $DB->get_record('course', array('id' => $moveup))) {
152 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder - 1));
154 } else {
155 if ($movecourse = $DB->get_record('course', array('id' => $movedown))) {
156 $swapcourse = $DB->get_record('course', array('sortorder' => $movecourse->sortorder + 1));
159 if ($swapcourse and $movecourse) {
160 // check course's category
161 if ($movecourse->category != $id) {
162 print_error('coursedoesnotbelongtocategory');
164 $DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $movecourse->id));
165 $DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id' => $swapcourse->id));
169 } // End of editing stuff
171 // Prepare the standard URL params for this page. We'll need them later.
172 $urlparams = array('id' => $id);
173 if ($page) {
174 $urlparams['page'] = $page;
176 if ($perpage) {
177 $urlparams['perpage'] = $perpage;
180 // Begin output
181 if ($editingon && can_edit_in_category()) {
182 // Integrate into the admin tree only if the user can edit categories at the top level,
183 // otherwise the admin block does not appear to this user, and you get an error.
184 require_once($CFG->libdir . '/adminlib.php');
185 admin_externalpage_setup('coursemgmt', '', $urlparams, $CFG->wwwroot . '/course/category.php');
186 $PAGE->set_context($context); // Ensure that we are actually showing blocks etc for the cat context
188 $settingsnode = $PAGE->settingsnav->find_active_node();
189 if ($settingsnode) {
190 $settingsnode->make_inactive();
191 $settingsnode->force_open();
192 $PAGE->navbar->add($settingsnode->text, $settingsnode->action);
194 echo $OUTPUT->header();
195 } else {
196 $site = get_site();
197 $PAGE->set_title("$site->shortname: $category->name");
198 $PAGE->set_heading($site->fullname);
199 $PAGE->set_button(print_course_search('', true, 'navbar'));
200 $PAGE->set_pagelayout('coursecategory');
201 echo $OUTPUT->header();
204 /// Print the category selector
205 $displaylist = array();
206 $notused = array();
207 make_categories_list($displaylist, $notused);
209 echo '<div class="categorypicker">';
210 $select = new single_select(new moodle_url('/course/category.php'), 'id', $displaylist, $category->id, null, 'switchcategory');
211 $select->set_label(get_string('categories').':');
212 echo $OUTPUT->render($select);
213 echo '</div>';
215 /// Print current category description
216 if (!$editingon && $category->description) {
217 echo $OUTPUT->box_start();
218 $options = new stdClass;
219 $options->noclean = true;
220 $options->para = false;
221 $options->overflowdiv = true;
222 if (!isset($category->descriptionformat)) {
223 $category->descriptionformat = FORMAT_MOODLE;
225 $text = file_rewrite_pluginfile_urls($category->description, 'pluginfile.php', $context->id, 'coursecat', 'description', null);
226 echo format_text($text, $category->descriptionformat, $options);
227 echo $OUTPUT->box_end();
230 if ($editingon && $canmanage) {
231 echo $OUTPUT->container_start('buttons');
233 // Print button to update this category
234 $url = new moodle_url('/course/editcategory.php', array('id' => $category->id));
235 echo $OUTPUT->single_button($url, get_string('editcategorythis'), 'get');
237 // Print button for creating new categories
238 $url = new moodle_url('/course/editcategory.php', array('parent' => $category->id));
239 echo $OUTPUT->single_button($url, get_string('addsubcategory'), 'get');
241 echo $OUTPUT->container_end();
244 // Print out all the sub-categories
245 // In order to view hidden subcategories the user must have the viewhiddencategories
246 // capability in the current category.
247 if (has_capability('moodle/category:viewhiddencategories', $context)) {
248 $categorywhere = '';
249 } else {
250 $categorywhere = 'AND cc.visible = 1';
252 // We're going to preload the context for the subcategory as we know that we
253 // need it later on for formatting.
254 list($ctxselect, $ctxjoin) = context_instance_preload_sql('cc.id', CONTEXT_COURSECAT, 'ctx');
255 $sql = "SELECT cc.* $ctxselect
256 FROM {course_categories} cc
257 $ctxjoin
258 WHERE cc.parent = :parentid
259 $categorywhere
260 ORDER BY cc.sortorder ASC";
261 $subcategories = $DB->get_recordset_sql($sql, array('parentid' => $category->id));
262 // Prepare a table to display the sub categories.
263 $table = new html_table;
264 $table->attributes = array('border' => '0', 'cellspacing' => '2', 'cellpadding' => '4', 'class' => 'generalbox boxaligncenter category_subcategories');
265 $table->head = array(get_string('subcategories'));
266 $table->data = array();
267 $baseurl = new moodle_url('/course/category.php');
268 foreach ($subcategories as $subcategory) {
269 // Preload the context we will need it to format the category name shortly.
270 context_instance_preload($subcategory);
271 $context = get_context_instance(CONTEXT_COURSECAT, $subcategory->id);
272 // Prepare the things we need to create a link to the subcategory
273 $attributes = $subcategory->visible ? array() : array('class' => 'dimmed');
274 $text = format_string($subcategory->name, true, array('context' => $context));
275 // Add the subcategory to the table
276 $baseurl->param('id', $subcategory->id);
277 $table->data[] = array(html_writer::link($baseurl, $text, $attributes));
280 $subcategorieswereshown = (count($table->data) > 0);
281 if ($subcategorieswereshown) {
282 echo html_writer::table($table);
285 // Print out all the courses
286 $courses = get_courses_page($category->id, 'c.sortorder ASC',
287 'c.id,c.sortorder,c.shortname,c.fullname,c.summary,c.visible',
288 $totalcount, $page*$perpage, $perpage);
289 $numcourses = count($courses);
291 if (!$courses) {
292 if (empty($subcategorieswereshown)) {
293 echo $OUTPUT->heading(get_string("nocoursesyet"));
296 } else if ($numcourses <= COURSE_MAX_SUMMARIES_PER_PAGE and !$page and !$editingon) {
297 echo $OUTPUT->box_start('courseboxes');
298 print_courses($category);
299 echo $OUTPUT->box_end();
301 } else {
302 echo $OUTPUT->paging_bar($totalcount, $page, $perpage, "/course/category.php?id=$category->id&perpage=$perpage");
304 echo '<form id="movecourses" action="category.php" method="post"><div>';
305 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
306 echo '<table border="0" cellspacing="2" cellpadding="4" class="generalbox boxaligncenter"><tr>';
307 echo '<th class="header" scope="col">'.get_string('courses').'</th>';
308 if ($editingon) {
309 echo '<th class="header" scope="col">'.get_string('edit').'</th>';
310 echo '<th class="header" scope="col">'.get_string('select').'</th>';
311 } else {
312 echo '<th class="header" scope="col">&nbsp;</th>';
314 echo '</tr>';
316 $count = 0;
317 $abletomovecourses = false; // for now
319 // Checking if we are at the first or at the last page, to allow courses to
320 // be moved up and down beyond the paging border
321 if ($totalcount > $perpage) {
322 $atfirstpage = ($page == 0);
323 if ($perpage > 0) {
324 $atlastpage = (($page + 1) == ceil($totalcount / $perpage));
325 } else {
326 $atlastpage = true;
328 } else {
329 $atfirstpage = true;
330 $atlastpage = true;
333 $baseurl = new moodle_url('/course/category.php', $urlparams + array('sesskey' => sesskey()));
334 foreach ($courses as $acourse) {
335 $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
337 $count++;
338 $up = ($count > 1 || !$atfirstpage);
339 $down = ($count < $numcourses || !$atlastpage);
341 $linkcss = $acourse->visible ? '' : ' class="dimmed" ';
342 echo '<tr>';
343 $coursename = get_course_display_name_for_list($acourse);
344 echo '<td><a '.$linkcss.' href="view.php?id='.$acourse->id.'">'. format_string($coursename) .'</a></td>';
345 if ($editingon) {
346 echo '<td>';
347 if (has_capability('moodle/course:update', $coursecontext)) {
348 $url = new moodle_url('/course/edit.php', array('id' => $acourse->id, 'category' => $id, 'returnto' => 'category'));
349 echo $OUTPUT->action_icon($url, new pix_icon('t/edit', get_string('settings')));
352 // role assignment link
353 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
354 $url = new moodle_url('/enrol/users.php', array('id' => $acourse->id));
355 echo $OUTPUT->action_icon($url, new pix_icon('i/users', get_string('enrolledusers', 'enrol')));
358 if (can_delete_course($acourse->id)) {
359 $url = new moodle_url('/course/delete.php', array('id' => $acourse->id));
360 echo $OUTPUT->action_icon($url, new pix_icon('t/delete', get_string('delete')));
363 // MDL-8885, users with no capability to view hidden courses, should not be able to lock themselves out
364 if (has_capability('moodle/course:visibility', $coursecontext) && has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
365 if (!empty($acourse->visible)) {
366 $url = new moodle_url($baseurl, array('hide' => $acourse->id));
367 echo $OUTPUT->action_icon($url, new pix_icon('t/hide', get_string('hide')));
368 } else {
369 $url = new moodle_url($baseurl, array('show' => $acourse->id));
370 echo $OUTPUT->action_icon($url, new pix_icon('t/show', get_string('show')));
374 if (has_capability('moodle/backup:backupcourse', $coursecontext)) {
375 $url = new moodle_url('/backup/backup.php', array('id' => $acourse->id));
376 echo $OUTPUT->action_icon($url, new pix_icon('t/backup', get_string('backup')));
379 if (has_capability('moodle/restore:restorecourse', $coursecontext)) {
380 $url = new moodle_url('/backup/restorefile.php', array('contextid' => $coursecontext->id));
381 echo $OUTPUT->action_icon($url, new pix_icon('t/restore', get_string('restore')));
384 if ($canmanage) {
385 if ($up) {
386 $url = new moodle_url($baseurl, array('moveup' => $acourse->id));
387 echo $OUTPUT->action_icon($url, new pix_icon('t/up', get_string('moveup')));
390 if ($down) {
391 $url = new moodle_url($baseurl, array('movedown' => $acourse->id));
392 echo $OUTPUT->action_icon($url, new pix_icon('t/down', get_string('movedown')));
394 $abletomovecourses = true;
397 echo '</td>';
398 echo '<td align="center">';
399 echo '<input type="checkbox" name="c'.$acourse->id.'" />';
400 echo '</td>';
401 } else {
402 echo '<td align="right">';
403 // print enrol info
404 if ($icons = enrol_get_course_info_icons($acourse)) {
405 foreach ($icons as $pix_icon) {
406 echo $OUTPUT->render($pix_icon);
409 if (!empty($acourse->summary)) {
410 $url = new moodle_url("/course/info.php?id=$acourse->id");
411 echo $OUTPUT->action_link($url, '<img alt="'.get_string('info').'" class="icon" src="'.$OUTPUT->pix_url('i/info') . '" />',
412 new popup_action('click', $url, 'courseinfo'), array('title'=>get_string('summary')));
414 echo "</td>";
416 echo "</tr>";
419 if ($abletomovecourses) {
420 $movetocategories = array();
421 $notused = array();
422 make_categories_list($movetocategories, $notused, 'moodle/category:manage');
423 $movetocategories[$category->id] = get_string('moveselectedcoursesto');
424 echo '<tr><td colspan="3" align="right">';
425 echo html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
426 echo html_writer::select($movetocategories, 'moveto', $category->id, null, array('id'=>'movetoid'));
427 $PAGE->requires->js_init_call('M.util.init_select_autosubmit', array('movecourses', 'movetoid', false));
428 echo '<input type="hidden" name="id" value="'.$category->id.'" />';
429 echo '</td></tr>';
432 echo '</table>';
433 echo '</div></form>';
434 echo '<br />';
437 echo '<div class="buttons">';
438 if ($canmanage and $numcourses > 1) {
439 // Print button to re-sort courses by name
440 $url = new moodle_url('/course/category.php', array('id' => $category->id, 'resort' => 'name', 'sesskey' => sesskey()));
441 echo $OUTPUT->single_button($url, get_string('resortcoursesbyname'), 'get');
444 if (has_capability('moodle/course:create', $context)) {
445 // Print button to create a new course
446 $url = new moodle_url('/course/edit.php', array('category' => $category->id, 'returnto' => 'category'));
447 echo $OUTPUT->single_button($url, get_string('addnewcourse'), 'get');
450 if (!empty($CFG->enablecourserequests) && $category->id == $CFG->defaultrequestcategory) {
451 print_course_request_buttons(get_context_instance(CONTEXT_SYSTEM));
453 echo '</div>';
455 print_course_search();
457 echo $OUTPUT->footer();