Merge branch 'wip-mdl-48190-m27' of https://github.com/rajeshtaneja/moodle into MOODL...
[moodle.git] / lib / coursecatlib.php
blob49dd69442a8fe66f3e4b8136ed47f6a0ddea7b45
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 * Contains class coursecat reponsible for course category operations
20 * @package core
21 * @subpackage course
22 * @copyright 2013 Marina Glancy
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Class to store, cache, render and manage course category
31 * @property-read int $id
32 * @property-read string $name
33 * @property-read string $idnumber
34 * @property-read string $description
35 * @property-read int $descriptionformat
36 * @property-read int $parent
37 * @property-read int $sortorder
38 * @property-read int $coursecount
39 * @property-read int $visible
40 * @property-read int $visibleold
41 * @property-read int $timemodified
42 * @property-read int $depth
43 * @property-read string $path
44 * @property-read string $theme
46 * @package core
47 * @subpackage course
48 * @copyright 2013 Marina Glancy
49 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
51 class coursecat implements renderable, cacheable_object, IteratorAggregate {
52 /** @var coursecat stores pseudo category with id=0. Use coursecat::get(0) to retrieve */
53 protected static $coursecat0;
55 /** Do not fetch course contacts more often than once per hour. */
56 const CACHE_COURSE_CONTACTS_TTL = 3600;
58 /** @var array list of all fields and their short name and default value for caching */
59 protected static $coursecatfields = array(
60 'id' => array('id', 0),
61 'name' => array('na', ''),
62 'idnumber' => array('in', null),
63 'description' => null, // Not cached.
64 'descriptionformat' => null, // Not cached.
65 'parent' => array('pa', 0),
66 'sortorder' => array('so', 0),
67 'coursecount' => array('cc', 0),
68 'visible' => array('vi', 1),
69 'visibleold' => null, // Not cached.
70 'timemodified' => null, // Not cached.
71 'depth' => array('dh', 1),
72 'path' => array('ph', null),
73 'theme' => null, // Not cached.
76 /** @var int */
77 protected $id;
79 /** @var string */
80 protected $name = '';
82 /** @var string */
83 protected $idnumber = null;
85 /** @var string */
86 protected $description = false;
88 /** @var int */
89 protected $descriptionformat = false;
91 /** @var int */
92 protected $parent = 0;
94 /** @var int */
95 protected $sortorder = 0;
97 /** @var int */
98 protected $coursecount = false;
100 /** @var int */
101 protected $visible = 1;
103 /** @var int */
104 protected $visibleold = false;
106 /** @var int */
107 protected $timemodified = false;
109 /** @var int */
110 protected $depth = 0;
112 /** @var string */
113 protected $path = '';
115 /** @var string */
116 protected $theme = false;
118 /** @var bool */
119 protected $fromcache;
121 /** @var bool */
122 protected $hasmanagecapability = null;
125 * Magic setter method, we do not want anybody to modify properties from the outside
127 * @param string $name
128 * @param mixed $value
130 public function __set($name, $value) {
131 debugging('Can not change coursecat instance properties!', DEBUG_DEVELOPER);
135 * Magic method getter, redirects to read only values. Queries from DB the fields that were not cached
137 * @param string $name
138 * @return mixed
140 public function __get($name) {
141 global $DB;
142 if (array_key_exists($name, self::$coursecatfields)) {
143 if ($this->$name === false) {
144 // Property was not retrieved from DB, retrieve all not retrieved fields.
145 $notretrievedfields = array_diff_key(self::$coursecatfields, array_filter(self::$coursecatfields));
146 $record = $DB->get_record('course_categories', array('id' => $this->id),
147 join(',', array_keys($notretrievedfields)), MUST_EXIST);
148 foreach ($record as $key => $value) {
149 $this->$key = $value;
152 return $this->$name;
154 debugging('Invalid coursecat property accessed! '.$name, DEBUG_DEVELOPER);
155 return null;
159 * Full support for isset on our magic read only properties.
161 * @param string $name
162 * @return bool
164 public function __isset($name) {
165 if (array_key_exists($name, self::$coursecatfields)) {
166 return isset($this->$name);
168 return false;
172 * All properties are read only, sorry.
174 * @param string $name
176 public function __unset($name) {
177 debugging('Can not unset coursecat instance properties!', DEBUG_DEVELOPER);
181 * Create an iterator because magic vars can't be seen by 'foreach'.
183 * implementing method from interface IteratorAggregate
185 * @return ArrayIterator
187 public function getIterator() {
188 $ret = array();
189 foreach (self::$coursecatfields as $property => $unused) {
190 if ($this->$property !== false) {
191 $ret[$property] = $this->$property;
194 return new ArrayIterator($ret);
198 * Constructor
200 * Constructor is protected, use coursecat::get($id) to retrieve category
202 * @param stdClass $record record from DB (may not contain all fields)
203 * @param bool $fromcache whether it is being restored from cache
205 protected function __construct(stdClass $record, $fromcache = false) {
206 context_helper::preload_from_record($record);
207 foreach ($record as $key => $val) {
208 if (array_key_exists($key, self::$coursecatfields)) {
209 $this->$key = $val;
212 $this->fromcache = $fromcache;
216 * Returns coursecat object for requested category
218 * If category is not visible to user it is treated as non existing
219 * unless $alwaysreturnhidden is set to true
221 * If id is 0, the pseudo object for root category is returned (convenient
222 * for calling other functions such as get_children())
224 * @param int $id category id
225 * @param int $strictness whether to throw an exception (MUST_EXIST) or
226 * return null (IGNORE_MISSING) in case the category is not found or
227 * not visible to current user
228 * @param bool $alwaysreturnhidden set to true if you want an object to be
229 * returned even if this category is not visible to the current user
230 * (category is hidden and user does not have
231 * 'moodle/category:viewhiddencategories' capability). Use with care!
232 * @return null|coursecat
233 * @throws moodle_exception
235 public static function get($id, $strictness = MUST_EXIST, $alwaysreturnhidden = false) {
236 if (!$id) {
237 if (!isset(self::$coursecat0)) {
238 $record = new stdClass();
239 $record->id = 0;
240 $record->visible = 1;
241 $record->depth = 0;
242 $record->path = '';
243 self::$coursecat0 = new coursecat($record);
245 return self::$coursecat0;
247 $coursecatrecordcache = cache::make('core', 'coursecatrecords');
248 $coursecat = $coursecatrecordcache->get($id);
249 if ($coursecat === false) {
250 if ($records = self::get_records('cc.id = :id', array('id' => $id))) {
251 $record = reset($records);
252 $coursecat = new coursecat($record);
253 // Store in cache.
254 $coursecatrecordcache->set($id, $coursecat);
257 if ($coursecat && ($alwaysreturnhidden || $coursecat->is_uservisible())) {
258 return $coursecat;
259 } else {
260 if ($strictness == MUST_EXIST) {
261 throw new moodle_exception('unknowcategory');
264 return null;
268 * Load many coursecat objects.
270 * @global moodle_database $DB
271 * @param array $ids An array of category ID's to load.
272 * @return coursecat[]
274 public static function get_many(array $ids) {
275 global $DB;
276 $coursecatrecordcache = cache::make('core', 'coursecatrecords');
277 $categories = $coursecatrecordcache->get_many($ids);
278 $toload = array();
279 foreach ($categories as $id => $result) {
280 if ($result === false) {
281 $toload[] = $id;
284 if (!empty($toload)) {
285 list($where, $params) = $DB->get_in_or_equal($toload, SQL_PARAMS_NAMED);
286 $records = self::get_records('cc.id '.$where, $params);
287 $toset = array();
288 foreach ($records as $record) {
289 $categories[$record->id] = new coursecat($record);
290 $toset[$record->id] = $categories[$record->id];
292 $coursecatrecordcache->set_many($toset);
294 return $categories;
298 * Returns the first found category
300 * Note that if there are no categories visible to the current user on the first level,
301 * the invisible category may be returned
303 * @return coursecat
305 public static function get_default() {
306 if ($visiblechildren = self::get(0)->get_children()) {
307 $defcategory = reset($visiblechildren);
308 } else {
309 $toplevelcategories = self::get_tree(0);
310 $defcategoryid = $toplevelcategories[0];
311 $defcategory = self::get($defcategoryid, MUST_EXIST, true);
313 return $defcategory;
317 * Restores the object after it has been externally modified in DB for example
318 * during {@link fix_course_sortorder()}
320 protected function restore() {
321 // Update all fields in the current object.
322 $newrecord = self::get($this->id, MUST_EXIST, true);
323 foreach (self::$coursecatfields as $key => $unused) {
324 $this->$key = $newrecord->$key;
329 * Creates a new category either from form data or from raw data
331 * Please note that this function does not verify access control.
333 * Exception is thrown if name is missing or idnumber is duplicating another one in the system.
335 * Category visibility is inherited from parent unless $data->visible = 0 is specified
337 * @param array|stdClass $data
338 * @param array $editoroptions if specified, the data is considered to be
339 * form data and file_postupdate_standard_editor() is being called to
340 * process images in description.
341 * @return coursecat
342 * @throws moodle_exception
344 public static function create($data, $editoroptions = null) {
345 global $DB, $CFG;
346 $data = (object)$data;
347 $newcategory = new stdClass();
349 $newcategory->descriptionformat = FORMAT_MOODLE;
350 $newcategory->description = '';
351 // Copy all description* fields regardless of whether this is form data or direct field update.
352 foreach ($data as $key => $value) {
353 if (preg_match("/^description/", $key)) {
354 $newcategory->$key = $value;
358 if (empty($data->name)) {
359 throw new moodle_exception('categorynamerequired');
361 if (core_text::strlen($data->name) > 255) {
362 throw new moodle_exception('categorytoolong');
364 $newcategory->name = $data->name;
366 // Validate and set idnumber.
367 if (!empty($data->idnumber)) {
368 if (core_text::strlen($data->idnumber) > 100) {
369 throw new moodle_exception('idnumbertoolong');
371 if ($DB->record_exists('course_categories', array('idnumber' => $data->idnumber))) {
372 throw new moodle_exception('categoryidnumbertaken');
375 if (isset($data->idnumber)) {
376 $newcategory->idnumber = $data->idnumber;
379 if (isset($data->theme) && !empty($CFG->allowcategorythemes)) {
380 $newcategory->theme = $data->theme;
383 if (empty($data->parent)) {
384 $parent = self::get(0);
385 } else {
386 $parent = self::get($data->parent, MUST_EXIST, true);
388 $newcategory->parent = $parent->id;
389 $newcategory->depth = $parent->depth + 1;
391 // By default category is visible, unless visible = 0 is specified or parent category is hidden.
392 if (isset($data->visible) && !$data->visible) {
393 // Create a hidden category.
394 $newcategory->visible = $newcategory->visibleold = 0;
395 } else {
396 // Create a category that inherits visibility from parent.
397 $newcategory->visible = $parent->visible;
398 // In case parent is hidden, when it changes visibility this new subcategory will automatically become visible too.
399 $newcategory->visibleold = 1;
402 $newcategory->sortorder = 0;
403 $newcategory->timemodified = time();
405 $newcategory->id = $DB->insert_record('course_categories', $newcategory);
407 // Update path (only possible after we know the category id.
408 $path = $parent->path . '/' . $newcategory->id;
409 $DB->set_field('course_categories', 'path', $path, array('id' => $newcategory->id));
411 // We should mark the context as dirty.
412 context_coursecat::instance($newcategory->id)->mark_dirty();
414 fix_course_sortorder();
416 // If this is data from form results, save embedded files and update description.
417 $categorycontext = context_coursecat::instance($newcategory->id);
418 if ($editoroptions) {
419 $newcategory = file_postupdate_standard_editor($newcategory, 'description', $editoroptions, $categorycontext,
420 'coursecat', 'description', 0);
422 // Update only fields description and descriptionformat.
423 $updatedata = new stdClass();
424 $updatedata->id = $newcategory->id;
425 $updatedata->description = $newcategory->description;
426 $updatedata->descriptionformat = $newcategory->descriptionformat;
427 $DB->update_record('course_categories', $updatedata);
430 $event = \core\event\course_category_created::create(array(
431 'objectid' => $newcategory->id,
432 'context' => $categorycontext
434 $event->trigger();
436 cache_helper::purge_by_event('changesincoursecat');
438 return self::get($newcategory->id, MUST_EXIST, true);
442 * Updates the record with either form data or raw data
444 * Please note that this function does not verify access control.
446 * This function calls coursecat::change_parent_raw if field 'parent' is updated.
447 * It also calls coursecat::hide_raw or coursecat::show_raw if 'visible' is updated.
448 * Visibility is changed first and then parent is changed. This means that
449 * if parent category is hidden, the current category will become hidden
450 * too and it may overwrite whatever was set in field 'visible'.
452 * Note that fields 'path' and 'depth' can not be updated manually
453 * Also coursecat::update() can not directly update the field 'sortoder'
455 * @param array|stdClass $data
456 * @param array $editoroptions if specified, the data is considered to be
457 * form data and file_postupdate_standard_editor() is being called to
458 * process images in description.
459 * @throws moodle_exception
461 public function update($data, $editoroptions = null) {
462 global $DB, $CFG;
463 if (!$this->id) {
464 // There is no actual DB record associated with root category.
465 return;
468 $data = (object)$data;
469 $newcategory = new stdClass();
470 $newcategory->id = $this->id;
472 // Copy all description* fields regardless of whether this is form data or direct field update.
473 foreach ($data as $key => $value) {
474 if (preg_match("/^description/", $key)) {
475 $newcategory->$key = $value;
479 if (isset($data->name) && empty($data->name)) {
480 throw new moodle_exception('categorynamerequired');
483 if (!empty($data->name) && $data->name !== $this->name) {
484 if (core_text::strlen($data->name) > 255) {
485 throw new moodle_exception('categorytoolong');
487 $newcategory->name = $data->name;
490 if (isset($data->idnumber) && $data->idnumber != $this->idnumber) {
491 if (core_text::strlen($data->idnumber) > 100) {
492 throw new moodle_exception('idnumbertoolong');
494 if ($DB->record_exists('course_categories', array('idnumber' => $data->idnumber))) {
495 throw new moodle_exception('categoryidnumbertaken');
497 $newcategory->idnumber = $data->idnumber;
500 if (isset($data->theme) && !empty($CFG->allowcategorythemes)) {
501 $newcategory->theme = $data->theme;
504 $changes = false;
505 if (isset($data->visible)) {
506 if ($data->visible) {
507 $changes = $this->show_raw();
508 } else {
509 $changes = $this->hide_raw(0);
513 if (isset($data->parent) && $data->parent != $this->parent) {
514 if ($changes) {
515 cache_helper::purge_by_event('changesincoursecat');
517 $parentcat = self::get($data->parent, MUST_EXIST, true);
518 $this->change_parent_raw($parentcat);
519 fix_course_sortorder();
522 $newcategory->timemodified = time();
524 $categorycontext = $this->get_context();
525 if ($editoroptions) {
526 $newcategory = file_postupdate_standard_editor($newcategory, 'description', $editoroptions, $categorycontext,
527 'coursecat', 'description', 0);
529 $DB->update_record('course_categories', $newcategory);
531 $event = \core\event\course_category_updated::create(array(
532 'objectid' => $newcategory->id,
533 'context' => $categorycontext
535 $event->trigger();
537 fix_course_sortorder();
538 // Purge cache even if fix_course_sortorder() did not do it.
539 cache_helper::purge_by_event('changesincoursecat');
541 // Update all fields in the current object.
542 $this->restore();
546 * Checks if this course category is visible to current user
548 * Please note that methods coursecat::get (without 3rd argumet),
549 * coursecat::get_children(), etc. return only visible categories so it is
550 * usually not needed to call this function outside of this class
552 * @return bool
554 public function is_uservisible() {
555 return !$this->id || $this->visible ||
556 has_capability('moodle/category:viewhiddencategories', $this->get_context());
560 * Returns all categories visible to the current user
562 * This is a generic function that returns an array of
563 * (category id => coursecat object) sorted by sortorder
565 * @see coursecat::get_children()
567 * @return cacheable_object_array array of coursecat objects
569 public static function get_all_visible() {
570 global $USER;
571 $coursecatcache = cache::make('core', 'coursecat');
572 $ids = $coursecatcache->get('user'. $USER->id);
573 if ($ids === false) {
574 $all = self::get_all_ids();
575 $parentvisible = $all[0];
576 $rv = array();
577 foreach ($all as $id => $children) {
578 if ($id && in_array($id, $parentvisible) &&
579 ($coursecat = self::get($id, IGNORE_MISSING)) &&
580 (!$coursecat->parent || isset($rv[$coursecat->parent]))) {
581 $rv[$id] = $coursecat;
582 $parentvisible += $children;
585 $coursecatcache->set('user'. $USER->id, array_keys($rv));
586 } else {
587 $rv = array();
588 foreach ($ids as $id) {
589 if ($coursecat = self::get($id, IGNORE_MISSING)) {
590 $rv[$id] = $coursecat;
594 return $rv;
598 * Returns the complete corresponding record from DB table course_categories
600 * Mostly used in deprecated functions
602 * @return stdClass
604 public function get_db_record() {
605 global $DB;
606 if ($record = $DB->get_record('course_categories', array('id' => $this->id))) {
607 return $record;
608 } else {
609 return (object)convert_to_array($this);
614 * Returns the entry from categories tree and makes sure the application-level tree cache is built
616 * The following keys can be requested:
618 * 'countall' - total number of categories in the system (always present)
619 * 0 - array of ids of top-level categories (always present)
620 * '0i' - array of ids of top-level categories that have visible=0 (always present but may be empty array)
621 * $id (int) - array of ids of categories that are direct children of category with id $id. If
622 * category with id $id does not exist returns false. If category has no children returns empty array
623 * $id.'i' - array of ids of children categories that have visible=0
625 * @param int|string $id
626 * @return mixed
628 protected static function get_tree($id) {
629 global $DB;
630 $coursecattreecache = cache::make('core', 'coursecattree');
631 $rv = $coursecattreecache->get($id);
632 if ($rv !== false) {
633 return $rv;
635 // Re-build the tree.
636 $sql = "SELECT cc.id, cc.parent, cc.visible
637 FROM {course_categories} cc
638 ORDER BY cc.sortorder";
639 $rs = $DB->get_recordset_sql($sql, array());
640 $all = array(0 => array(), '0i' => array());
641 $count = 0;
642 foreach ($rs as $record) {
643 $all[$record->id] = array();
644 $all[$record->id. 'i'] = array();
645 if (array_key_exists($record->parent, $all)) {
646 $all[$record->parent][] = $record->id;
647 if (!$record->visible) {
648 $all[$record->parent. 'i'][] = $record->id;
650 } else {
651 // Parent not found. This is data consistency error but next fix_course_sortorder() should fix it.
652 $all[0][] = $record->id;
653 if (!$record->visible) {
654 $all['0i'][] = $record->id;
657 $count++;
659 $rs->close();
660 if (!$count) {
661 // No categories found.
662 // This may happen after upgrade of a very old moodle version.
663 // In new versions the default category is created on install.
664 $defcoursecat = self::create(array('name' => get_string('miscellaneous')));
665 set_config('defaultrequestcategory', $defcoursecat->id);
666 $all[0] = array($defcoursecat->id);
667 $all[$defcoursecat->id] = array();
668 $count++;
670 // We must add countall to all in case it was the requested ID.
671 $all['countall'] = $count;
672 foreach ($all as $key => $children) {
673 $coursecattreecache->set($key, $children);
675 if (array_key_exists($id, $all)) {
676 return $all[$id];
678 // Requested non-existing category.
679 return array();
683 * Returns number of ALL categories in the system regardless if
684 * they are visible to current user or not
686 * @return int
688 public static function count_all() {
689 return self::get_tree('countall');
693 * Retrieves number of records from course_categories table
695 * Only cached fields are retrieved. Records are ready for preloading context
697 * @param string $whereclause
698 * @param array $params
699 * @return array array of stdClass objects
701 protected static function get_records($whereclause, $params) {
702 global $DB;
703 // Retrieve from DB only the fields that need to be stored in cache.
704 $fields = array_keys(array_filter(self::$coursecatfields));
705 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
706 $sql = "SELECT cc.". join(',cc.', $fields). ", $ctxselect
707 FROM {course_categories} cc
708 JOIN {context} ctx ON cc.id = ctx.instanceid AND ctx.contextlevel = :contextcoursecat
709 WHERE ". $whereclause." ORDER BY cc.sortorder";
710 return $DB->get_records_sql($sql,
711 array('contextcoursecat' => CONTEXT_COURSECAT) + $params);
715 * Given list of DB records from table course populates each record with list of users with course contact roles
717 * This function fills the courses with raw information as {@link get_role_users()} would do.
718 * See also {@link course_in_list::get_course_contacts()} for more readable return
720 * $courses[$i]->managers = array(
721 * $roleassignmentid => $roleuser,
722 * ...
723 * );
725 * where $roleuser is an stdClass with the following properties:
727 * $roleuser->raid - role assignment id
728 * $roleuser->id - user id
729 * $roleuser->username
730 * $roleuser->firstname
731 * $roleuser->lastname
732 * $roleuser->rolecoursealias
733 * $roleuser->rolename
734 * $roleuser->sortorder - role sortorder
735 * $roleuser->roleid
736 * $roleuser->roleshortname
738 * @todo MDL-38596 minimize number of queries to preload contacts for the list of courses
740 * @param array $courses
742 public static function preload_course_contacts(&$courses) {
743 global $CFG, $DB;
744 if (empty($courses) || empty($CFG->coursecontact)) {
745 return;
747 $managerroles = explode(',', $CFG->coursecontact);
748 $cache = cache::make('core', 'coursecontacts');
749 $cacheddata = $cache->get_many(array_merge(array('basic'), array_keys($courses)));
750 // Check if cache was set for the current course contacts and it is not yet expired.
751 if (empty($cacheddata['basic']) || $cacheddata['basic']['roles'] !== $CFG->coursecontact ||
752 $cacheddata['basic']['lastreset'] < time() - self::CACHE_COURSE_CONTACTS_TTL) {
753 // Reset cache.
754 $cache->purge();
755 $cache->set('basic', array('roles' => $CFG->coursecontact, 'lastreset' => time()));
756 $cacheddata = $cache->get_many(array_merge(array('basic'), array_keys($courses)));
758 $courseids = array();
759 foreach (array_keys($courses) as $id) {
760 if ($cacheddata[$id] !== false) {
761 $courses[$id]->managers = $cacheddata[$id];
762 } else {
763 $courseids[] = $id;
767 // Array $courseids now stores list of ids of courses for which we still need to retrieve contacts.
768 if (empty($courseids)) {
769 return;
772 // First build the array of all context ids of the courses and their categories.
773 $allcontexts = array();
774 foreach ($courseids as $id) {
775 $context = context_course::instance($id);
776 $courses[$id]->managers = array();
777 foreach (preg_split('|/|', $context->path, 0, PREG_SPLIT_NO_EMPTY) as $ctxid) {
778 if (!isset($allcontexts[$ctxid])) {
779 $allcontexts[$ctxid] = array();
781 $allcontexts[$ctxid][] = $id;
785 // Fetch list of all users with course contact roles in any of the courses contexts or parent contexts.
786 list($sql1, $params1) = $DB->get_in_or_equal(array_keys($allcontexts), SQL_PARAMS_NAMED, 'ctxid');
787 list($sql2, $params2) = $DB->get_in_or_equal($managerroles, SQL_PARAMS_NAMED, 'rid');
788 list($sort, $sortparams) = users_order_by_sql('u');
789 $notdeleted = array('notdeleted'=>0);
790 $allnames = get_all_user_name_fields(true, 'u');
791 $sql = "SELECT ra.contextid, ra.id AS raid,
792 r.id AS roleid, r.name AS rolename, r.shortname AS roleshortname,
793 rn.name AS rolecoursealias, u.id, u.username, $allnames
794 FROM {role_assignments} ra
795 JOIN {user} u ON ra.userid = u.id
796 JOIN {role} r ON ra.roleid = r.id
797 LEFT JOIN {role_names} rn ON (rn.contextid = ra.contextid AND rn.roleid = r.id)
798 WHERE ra.contextid ". $sql1." AND ra.roleid ". $sql2." AND u.deleted = :notdeleted
799 ORDER BY r.sortorder, $sort";
800 $rs = $DB->get_recordset_sql($sql, $params1 + $params2 + $notdeleted + $sortparams);
801 $checkenrolments = array();
802 foreach ($rs as $ra) {
803 foreach ($allcontexts[$ra->contextid] as $id) {
804 $courses[$id]->managers[$ra->raid] = $ra;
805 if (!isset($checkenrolments[$id])) {
806 $checkenrolments[$id] = array();
808 $checkenrolments[$id][] = $ra->id;
811 $rs->close();
813 // Remove from course contacts users who are not enrolled in the course.
814 $enrolleduserids = self::ensure_users_enrolled($checkenrolments);
815 foreach ($checkenrolments as $id => $userids) {
816 if (empty($enrolleduserids[$id])) {
817 $courses[$id]->managers = array();
818 } else if ($notenrolled = array_diff($userids, $enrolleduserids[$id])) {
819 foreach ($courses[$id]->managers as $raid => $ra) {
820 if (in_array($ra->id, $notenrolled)) {
821 unset($courses[$id]->managers[$raid]);
827 // Set the cache.
828 $values = array();
829 foreach ($courseids as $id) {
830 $values[$id] = $courses[$id]->managers;
832 $cache->set_many($values);
836 * Verify user enrollments for multiple course-user combinations
838 * @param array $courseusers array where keys are course ids and values are array
839 * of users in this course whose enrolment we wish to verify
840 * @return array same structure as input array but values list only users from input
841 * who are enrolled in the course
843 protected static function ensure_users_enrolled($courseusers) {
844 global $DB;
845 // If the input array is too big, split it into chunks.
846 $maxcoursesinquery = 20;
847 if (count($courseusers) > $maxcoursesinquery) {
848 $rv = array();
849 for ($offset = 0; $offset < count($courseusers); $offset += $maxcoursesinquery) {
850 $chunk = array_slice($courseusers, $offset, $maxcoursesinquery, true);
851 $rv = $rv + self::ensure_users_enrolled($chunk);
853 return $rv;
856 // Create a query verifying valid user enrolments for the number of courses.
857 $sql = "SELECT DISTINCT e.courseid, ue.userid
858 FROM {user_enrolments} ue
859 JOIN {enrol} e ON e.id = ue.enrolid
860 WHERE ue.status = :active
861 AND e.status = :enabled
862 AND ue.timestart < :now1 AND (ue.timeend = 0 OR ue.timeend > :now2)";
863 $now = round(time(), -2); // Rounding helps caching in DB.
864 $params = array('enabled' => ENROL_INSTANCE_ENABLED,
865 'active' => ENROL_USER_ACTIVE,
866 'now1' => $now, 'now2' => $now);
867 $cnt = 0;
868 $subsqls = array();
869 $enrolled = array();
870 foreach ($courseusers as $id => $userids) {
871 $enrolled[$id] = array();
872 if (count($userids)) {
873 list($sql2, $params2) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'userid'.$cnt.'_');
874 $subsqls[] = "(e.courseid = :courseid$cnt AND ue.userid ".$sql2.")";
875 $params = $params + array('courseid'.$cnt => $id) + $params2;
876 $cnt++;
879 if (count($subsqls)) {
880 $sql .= "AND (". join(' OR ', $subsqls).")";
881 $rs = $DB->get_recordset_sql($sql, $params);
882 foreach ($rs as $record) {
883 $enrolled[$record->courseid][] = $record->userid;
885 $rs->close();
887 return $enrolled;
891 * Retrieves number of records from course table
893 * Not all fields are retrieved. Records are ready for preloading context
895 * @param string $whereclause
896 * @param array $params
897 * @param array $options may indicate that summary and/or coursecontacts need to be retrieved
898 * @param bool $checkvisibility if true, capability 'moodle/course:viewhiddencourses' will be checked
899 * on not visible courses
900 * @return array array of stdClass objects
902 protected static function get_course_records($whereclause, $params, $options, $checkvisibility = false) {
903 global $DB;
904 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
905 $fields = array('c.id', 'c.category', 'c.sortorder',
906 'c.shortname', 'c.fullname', 'c.idnumber',
907 'c.startdate', 'c.visible', 'c.cacherev');
908 if (!empty($options['summary'])) {
909 $fields[] = 'c.summary';
910 $fields[] = 'c.summaryformat';
911 } else {
912 $fields[] = $DB->sql_substr('c.summary', 1, 1). ' as hassummary';
914 $sql = "SELECT ". join(',', $fields). ", $ctxselect
915 FROM {course} c
916 JOIN {context} ctx ON c.id = ctx.instanceid AND ctx.contextlevel = :contextcourse
917 WHERE ". $whereclause." ORDER BY c.sortorder";
918 $list = $DB->get_records_sql($sql,
919 array('contextcourse' => CONTEXT_COURSE) + $params);
921 if ($checkvisibility) {
922 // Loop through all records and make sure we only return the courses accessible by user.
923 foreach ($list as $course) {
924 if (isset($list[$course->id]->hassummary)) {
925 $list[$course->id]->hassummary = strlen($list[$course->id]->hassummary) > 0;
927 if (empty($course->visible)) {
928 // Load context only if we need to check capability.
929 context_helper::preload_from_record($course);
930 if (!has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) {
931 unset($list[$course->id]);
937 // Preload course contacts if necessary.
938 if (!empty($options['coursecontacts'])) {
939 self::preload_course_contacts($list);
941 return $list;
945 * Returns array of ids of children categories that current user can not see
947 * This data is cached in user session cache
949 * @return array
951 protected function get_not_visible_children_ids() {
952 global $DB;
953 $coursecatcache = cache::make('core', 'coursecat');
954 if (($invisibleids = $coursecatcache->get('ic'. $this->id)) === false) {
955 // We never checked visible children before.
956 $hidden = self::get_tree($this->id.'i');
957 $invisibleids = array();
958 if ($hidden) {
959 // Preload categories contexts.
960 list($sql, $params) = $DB->get_in_or_equal($hidden, SQL_PARAMS_NAMED, 'id');
961 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
962 $contexts = $DB->get_records_sql("SELECT $ctxselect FROM {context} ctx
963 WHERE ctx.contextlevel = :contextcoursecat AND ctx.instanceid ".$sql,
964 array('contextcoursecat' => CONTEXT_COURSECAT) + $params);
965 foreach ($contexts as $record) {
966 context_helper::preload_from_record($record);
968 // Check that user has 'viewhiddencategories' capability for each hidden category.
969 foreach ($hidden as $id) {
970 if (!has_capability('moodle/category:viewhiddencategories', context_coursecat::instance($id))) {
971 $invisibleids[] = $id;
975 $coursecatcache->set('ic'. $this->id, $invisibleids);
977 return $invisibleids;
981 * Sorts list of records by several fields
983 * @param array $records array of stdClass objects
984 * @param array $sortfields assoc array where key is the field to sort and value is 1 for asc or -1 for desc
985 * @return int
987 protected static function sort_records(&$records, $sortfields) {
988 if (empty($records)) {
989 return;
991 // If sorting by course display name, calculate it (it may be fullname or shortname+fullname).
992 if (array_key_exists('displayname', $sortfields)) {
993 foreach ($records as $key => $record) {
994 if (!isset($record->displayname)) {
995 $records[$key]->displayname = get_course_display_name_for_list($record);
999 // Sorting by one field - use core_collator.
1000 if (count($sortfields) == 1) {
1001 $property = key($sortfields);
1002 if (in_array($property, array('sortorder', 'id', 'visible', 'parent', 'depth'))) {
1003 $sortflag = core_collator::SORT_NUMERIC;
1004 } else if (in_array($property, array('idnumber', 'displayname', 'name', 'shortname', 'fullname'))) {
1005 $sortflag = core_collator::SORT_STRING;
1006 } else {
1007 $sortflag = core_collator::SORT_REGULAR;
1009 core_collator::asort_objects_by_property($records, $property, $sortflag);
1010 if ($sortfields[$property] < 0) {
1011 $records = array_reverse($records, true);
1013 return;
1015 $records = coursecat_sortable_records::sort($records, $sortfields);
1019 * Returns array of children categories visible to the current user
1021 * @param array $options options for retrieving children
1022 * - sort - list of fields to sort. Example
1023 * array('idnumber' => 1, 'name' => 1, 'id' => -1)
1024 * will sort by idnumber asc, name asc and id desc.
1025 * Default: array('sortorder' => 1)
1026 * Only cached fields may be used for sorting!
1027 * - offset
1028 * - limit - maximum number of children to return, 0 or null for no limit
1029 * @return coursecat[] Array of coursecat objects indexed by category id
1031 public function get_children($options = array()) {
1032 global $DB;
1033 $coursecatcache = cache::make('core', 'coursecat');
1035 // Get default values for options.
1036 if (!empty($options['sort']) && is_array($options['sort'])) {
1037 $sortfields = $options['sort'];
1038 } else {
1039 $sortfields = array('sortorder' => 1);
1041 $limit = null;
1042 if (!empty($options['limit']) && (int)$options['limit']) {
1043 $limit = (int)$options['limit'];
1045 $offset = 0;
1046 if (!empty($options['offset']) && (int)$options['offset']) {
1047 $offset = (int)$options['offset'];
1050 // First retrieve list of user-visible and sorted children ids from cache.
1051 $sortedids = $coursecatcache->get('c'. $this->id. ':'. serialize($sortfields));
1052 if ($sortedids === false) {
1053 $sortfieldskeys = array_keys($sortfields);
1054 if ($sortfieldskeys[0] === 'sortorder') {
1055 // No DB requests required to build the list of ids sorted by sortorder.
1056 // We can easily ignore other sort fields because sortorder is always different.
1057 $sortedids = self::get_tree($this->id);
1058 if ($sortedids && ($invisibleids = $this->get_not_visible_children_ids())) {
1059 $sortedids = array_diff($sortedids, $invisibleids);
1060 if ($sortfields['sortorder'] == -1) {
1061 $sortedids = array_reverse($sortedids, true);
1064 } else {
1065 // We need to retrieve and sort all children. Good thing that it is done only on first request.
1066 if ($invisibleids = $this->get_not_visible_children_ids()) {
1067 list($sql, $params) = $DB->get_in_or_equal($invisibleids, SQL_PARAMS_NAMED, 'id', false);
1068 $records = self::get_records('cc.parent = :parent AND cc.id '. $sql,
1069 array('parent' => $this->id) + $params);
1070 } else {
1071 $records = self::get_records('cc.parent = :parent', array('parent' => $this->id));
1073 self::sort_records($records, $sortfields);
1074 $sortedids = array_keys($records);
1076 $coursecatcache->set('c'. $this->id. ':'.serialize($sortfields), $sortedids);
1079 if (empty($sortedids)) {
1080 return array();
1083 // Now retrieive and return categories.
1084 if ($offset || $limit) {
1085 $sortedids = array_slice($sortedids, $offset, $limit);
1087 if (isset($records)) {
1088 // Easy, we have already retrieved records.
1089 if ($offset || $limit) {
1090 $records = array_slice($records, $offset, $limit, true);
1092 } else {
1093 list($sql, $params) = $DB->get_in_or_equal($sortedids, SQL_PARAMS_NAMED, 'id');
1094 $records = self::get_records('cc.id '. $sql, array('parent' => $this->id) + $params);
1097 $rv = array();
1098 foreach ($sortedids as $id) {
1099 if (isset($records[$id])) {
1100 $rv[$id] = new coursecat($records[$id]);
1103 return $rv;
1107 * Returns true if the user has the manage capability on any category.
1109 * This method uses the coursecat cache and an entry `has_manage_capability` to speed up
1110 * calls to this method.
1112 * @return bool
1114 public static function has_manage_capability_on_any() {
1115 return self::has_capability_on_any('moodle/category:manage');
1119 * Checks if the user has at least one of the given capabilities on any category.
1121 * @param array|string $capabilities One or more capabilities to check. Check made is an OR.
1122 * @return bool
1124 public static function has_capability_on_any($capabilities) {
1125 global $DB;
1126 if (!isloggedin() || isguestuser()) {
1127 return false;
1130 if (!is_array($capabilities)) {
1131 $capabilities = array($capabilities);
1133 $keys = array();
1134 foreach ($capabilities as $capability) {
1135 $keys[$capability] = sha1($capability);
1138 /* @var cache_session $cache */
1139 $cache = cache::make('core', 'coursecat');
1140 $hascapability = $cache->get_many($keys);
1141 $needtoload = false;
1142 foreach ($hascapability as $capability) {
1143 if ($capability === '1') {
1144 return true;
1145 } else if ($capability === false) {
1146 $needtoload = true;
1149 if ($needtoload === false) {
1150 // All capabilities were retrieved and the user didn't have any.
1151 return false;
1154 $haskey = null;
1155 $fields = context_helper::get_preload_record_columns_sql('ctx');
1156 $sql = "SELECT ctx.instanceid AS categoryid, $fields
1157 FROM {context} ctx
1158 WHERE contextlevel = :contextlevel
1159 ORDER BY depth ASC";
1160 $params = array('contextlevel' => CONTEXT_COURSECAT);
1161 $recordset = $DB->get_recordset_sql($sql, $params);
1162 foreach ($recordset as $context) {
1163 context_helper::preload_from_record($context);
1164 $context = context_coursecat::instance($context->categoryid);
1165 foreach ($capabilities as $capability) {
1166 if (has_capability($capability, $context)) {
1167 $haskey = $capability;
1168 break 2;
1172 $recordset->close();
1173 if ($haskey === null) {
1174 $data = array();
1175 foreach ($keys as $key) {
1176 $data[$key] = '0';
1178 $cache->set_many($data);
1179 return false;
1180 } else {
1181 $cache->set($haskey, '1');
1182 return true;
1187 * Returns true if the user can resort any category.
1188 * @return bool
1190 public static function can_resort_any() {
1191 return self::has_manage_capability_on_any();
1195 * Returns true if the user can change the parent of any category.
1196 * @return bool
1198 public static function can_change_parent_any() {
1199 return self::has_manage_capability_on_any();
1203 * Returns number of subcategories visible to the current user
1205 * @return int
1207 public function get_children_count() {
1208 $sortedids = self::get_tree($this->id);
1209 $invisibleids = $this->get_not_visible_children_ids();
1210 return count($sortedids) - count($invisibleids);
1214 * Returns true if the category has ANY children, including those not visible to the user
1216 * @return boolean
1218 public function has_children() {
1219 $allchildren = self::get_tree($this->id);
1220 return !empty($allchildren);
1224 * Returns true if the category has courses in it (count does not include courses
1225 * in child categories)
1227 * @return bool
1229 public function has_courses() {
1230 global $DB;
1231 return $DB->record_exists_sql("select 1 from {course} where category = ?",
1232 array($this->id));
1236 * Searches courses
1238 * List of found course ids is cached for 10 minutes. Cache may be purged prior
1239 * to this when somebody edits courses or categories, however it is very
1240 * difficult to keep track of all possible changes that may affect list of courses.
1242 * @param array $search contains search criterias, such as:
1243 * - search - search string
1244 * - blocklist - id of block (if we are searching for courses containing specific block0
1245 * - modulelist - name of module (if we are searching for courses containing specific module
1246 * - tagid - id of tag
1247 * @param array $options display options, same as in get_courses() except 'recursive' is ignored -
1248 * search is always category-independent
1249 * @return course_in_list[]
1251 public static function search_courses($search, $options = array()) {
1252 global $DB;
1253 $offset = !empty($options['offset']) ? $options['offset'] : 0;
1254 $limit = !empty($options['limit']) ? $options['limit'] : null;
1255 $sortfields = !empty($options['sort']) ? $options['sort'] : array('sortorder' => 1);
1257 $coursecatcache = cache::make('core', 'coursecat');
1258 $cachekey = 's-'. serialize($search + array('sort' => $sortfields));
1259 $cntcachekey = 'scnt-'. serialize($search);
1261 $ids = $coursecatcache->get($cachekey);
1262 if ($ids !== false) {
1263 // We already cached last search result.
1264 $ids = array_slice($ids, $offset, $limit);
1265 $courses = array();
1266 if (!empty($ids)) {
1267 list($sql, $params) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED, 'id');
1268 $records = self::get_course_records("c.id ". $sql, $params, $options);
1269 // Preload course contacts if necessary - saves DB queries later to do it for each course separately.
1270 if (!empty($options['coursecontacts'])) {
1271 self::preload_course_contacts($records);
1273 // If option 'idonly' is specified no further action is needed, just return list of ids.
1274 if (!empty($options['idonly'])) {
1275 return array_keys($records);
1277 // Prepare the list of course_in_list objects.
1278 foreach ($ids as $id) {
1279 $courses[$id] = new course_in_list($records[$id]);
1282 return $courses;
1285 $preloadcoursecontacts = !empty($options['coursecontacts']);
1286 unset($options['coursecontacts']);
1288 if (!empty($search['search'])) {
1289 // Search courses that have specified words in their names/summaries.
1290 $searchterms = preg_split('|\s+|', trim($search['search']), 0, PREG_SPLIT_NO_EMPTY);
1291 $searchterms = array_filter($searchterms, create_function('$v', 'return strlen($v) > 1;'));
1292 $courselist = get_courses_search($searchterms, 'c.sortorder ASC', 0, 9999999, $totalcount);
1293 self::sort_records($courselist, $sortfields);
1294 $coursecatcache->set($cachekey, array_keys($courselist));
1295 $coursecatcache->set($cntcachekey, $totalcount);
1296 $records = array_slice($courselist, $offset, $limit, true);
1297 } else {
1298 if (!empty($search['blocklist'])) {
1299 // Search courses that have block with specified id.
1300 $blockname = $DB->get_field('block', 'name', array('id' => $search['blocklist']));
1301 $where = 'ctx.id in (SELECT distinct bi.parentcontextid FROM {block_instances} bi
1302 WHERE bi.blockname = :blockname)';
1303 $params = array('blockname' => $blockname);
1304 } else if (!empty($search['modulelist'])) {
1305 // Search courses that have module with specified name.
1306 $where = "c.id IN (SELECT DISTINCT module.course ".
1307 "FROM {".$search['modulelist']."} module)";
1308 $params = array();
1309 } else if (!empty($search['tagid'])) {
1310 // Search courses that are tagged with the specified tag.
1311 $where = "c.id IN (SELECT t.itemid ".
1312 "FROM {tag_instance} t WHERE t.tagid = :tagid AND t.itemtype = :itemtype)";
1313 $params = array('tagid' => $search['tagid'], 'itemtype' => 'course');
1314 } else {
1315 debugging('No criteria is specified while searching courses', DEBUG_DEVELOPER);
1316 return array();
1318 $courselist = self::get_course_records($where, $params, $options, true);
1319 self::sort_records($courselist, $sortfields);
1320 $coursecatcache->set($cachekey, array_keys($courselist));
1321 $coursecatcache->set($cntcachekey, count($courselist));
1322 $records = array_slice($courselist, $offset, $limit, true);
1325 // Preload course contacts if necessary - saves DB queries later to do it for each course separately.
1326 if (!empty($preloadcoursecontacts)) {
1327 self::preload_course_contacts($records);
1329 // If option 'idonly' is specified no further action is needed, just return list of ids.
1330 if (!empty($options['idonly'])) {
1331 return array_keys($records);
1333 // Prepare the list of course_in_list objects.
1334 $courses = array();
1335 foreach ($records as $record) {
1336 $courses[$record->id] = new course_in_list($record);
1338 return $courses;
1342 * Returns number of courses in the search results
1344 * It is recommended to call this function after {@link coursecat::search_courses()}
1345 * and not before because only course ids are cached. Otherwise search_courses() may
1346 * perform extra DB queries.
1348 * @param array $search search criteria, see method search_courses() for more details
1349 * @param array $options display options. They do not affect the result but
1350 * the 'sort' property is used in cache key for storing list of course ids
1351 * @return int
1353 public static function search_courses_count($search, $options = array()) {
1354 $coursecatcache = cache::make('core', 'coursecat');
1355 $cntcachekey = 'scnt-'. serialize($search);
1356 if (($cnt = $coursecatcache->get($cntcachekey)) === false) {
1357 // Cached value not found. Retrieve ALL courses and return their count.
1358 unset($options['offset']);
1359 unset($options['limit']);
1360 unset($options['summary']);
1361 unset($options['coursecontacts']);
1362 $options['idonly'] = true;
1363 $courses = self::search_courses($search, $options);
1364 $cnt = count($courses);
1366 return $cnt;
1370 * Retrieves the list of courses accessible by user
1372 * Not all information is cached, try to avoid calling this method
1373 * twice in the same request.
1375 * The following fields are always retrieved:
1376 * - id, visible, fullname, shortname, idnumber, category, sortorder
1378 * If you plan to use properties/methods course_in_list::$summary and/or
1379 * course_in_list::get_course_contacts()
1380 * you can preload this information using appropriate 'options'. Otherwise
1381 * they will be retrieved from DB on demand and it may end with bigger DB load.
1383 * Note that method course_in_list::has_summary() will not perform additional
1384 * DB queries even if $options['summary'] is not specified
1386 * List of found course ids is cached for 10 minutes. Cache may be purged prior
1387 * to this when somebody edits courses or categories, however it is very
1388 * difficult to keep track of all possible changes that may affect list of courses.
1390 * @param array $options options for retrieving children
1391 * - recursive - return courses from subcategories as well. Use with care,
1392 * this may be a huge list!
1393 * - summary - preloads fields 'summary' and 'summaryformat'
1394 * - coursecontacts - preloads course contacts
1395 * - sort - list of fields to sort. Example
1396 * array('idnumber' => 1, 'shortname' => 1, 'id' => -1)
1397 * will sort by idnumber asc, shortname asc and id desc.
1398 * Default: array('sortorder' => 1)
1399 * Only cached fields may be used for sorting!
1400 * - offset
1401 * - limit - maximum number of children to return, 0 or null for no limit
1402 * - idonly - returns the array or course ids instead of array of objects
1403 * used only in get_courses_count()
1404 * @return course_in_list[]
1406 public function get_courses($options = array()) {
1407 global $DB;
1408 $recursive = !empty($options['recursive']);
1409 $offset = !empty($options['offset']) ? $options['offset'] : 0;
1410 $limit = !empty($options['limit']) ? $options['limit'] : null;
1411 $sortfields = !empty($options['sort']) ? $options['sort'] : array('sortorder' => 1);
1413 // Check if this category is hidden.
1414 // Also 0-category never has courses unless this is recursive call.
1415 if (!$this->is_uservisible() || (!$this->id && !$recursive)) {
1416 return array();
1419 $coursecatcache = cache::make('core', 'coursecat');
1420 $cachekey = 'l-'. $this->id. '-'. (!empty($options['recursive']) ? 'r' : '').
1421 '-'. serialize($sortfields);
1422 $cntcachekey = 'lcnt-'. $this->id. '-'. (!empty($options['recursive']) ? 'r' : '');
1424 // Check if we have already cached results.
1425 $ids = $coursecatcache->get($cachekey);
1426 if ($ids !== false) {
1427 // We already cached last search result and it did not expire yet.
1428 $ids = array_slice($ids, $offset, $limit);
1429 $courses = array();
1430 if (!empty($ids)) {
1431 list($sql, $params) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED, 'id');
1432 $records = self::get_course_records("c.id ". $sql, $params, $options);
1433 // Preload course contacts if necessary - saves DB queries later to do it for each course separately.
1434 if (!empty($options['coursecontacts'])) {
1435 self::preload_course_contacts($records);
1437 // If option 'idonly' is specified no further action is needed, just return list of ids.
1438 if (!empty($options['idonly'])) {
1439 return array_keys($records);
1441 // Prepare the list of course_in_list objects.
1442 foreach ($ids as $id) {
1443 $courses[$id] = new course_in_list($records[$id]);
1446 return $courses;
1449 // Retrieve list of courses in category.
1450 $where = 'c.id <> :siteid';
1451 $params = array('siteid' => SITEID);
1452 if ($recursive) {
1453 if ($this->id) {
1454 $context = context_coursecat::instance($this->id);
1455 $where .= ' AND ctx.path like :path';
1456 $params['path'] = $context->path. '/%';
1458 } else {
1459 $where .= ' AND c.category = :categoryid';
1460 $params['categoryid'] = $this->id;
1462 // Get list of courses without preloaded coursecontacts because we don't need them for every course.
1463 $list = $this->get_course_records($where, $params, array_diff_key($options, array('coursecontacts' => 1)), true);
1465 // Sort and cache list.
1466 self::sort_records($list, $sortfields);
1467 $coursecatcache->set($cachekey, array_keys($list));
1468 $coursecatcache->set($cntcachekey, count($list));
1470 // Apply offset/limit, convert to course_in_list and return.
1471 $courses = array();
1472 if (isset($list)) {
1473 if ($offset || $limit) {
1474 $list = array_slice($list, $offset, $limit, true);
1476 // Preload course contacts if necessary - saves DB queries later to do it for each course separately.
1477 if (!empty($options['coursecontacts'])) {
1478 self::preload_course_contacts($list);
1480 // If option 'idonly' is specified no further action is needed, just return list of ids.
1481 if (!empty($options['idonly'])) {
1482 return array_keys($list);
1484 // Prepare the list of course_in_list objects.
1485 foreach ($list as $record) {
1486 $courses[$record->id] = new course_in_list($record);
1489 return $courses;
1493 * Returns number of courses visible to the user
1495 * @param array $options similar to get_courses() except some options do not affect
1496 * number of courses (i.e. sort, summary, offset, limit etc.)
1497 * @return int
1499 public function get_courses_count($options = array()) {
1500 $cntcachekey = 'lcnt-'. $this->id. '-'. (!empty($options['recursive']) ? 'r' : '');
1501 $coursecatcache = cache::make('core', 'coursecat');
1502 if (($cnt = $coursecatcache->get($cntcachekey)) === false) {
1503 // Cached value not found. Retrieve ALL courses and return their count.
1504 unset($options['offset']);
1505 unset($options['limit']);
1506 unset($options['summary']);
1507 unset($options['coursecontacts']);
1508 $options['idonly'] = true;
1509 $courses = $this->get_courses($options);
1510 $cnt = count($courses);
1512 return $cnt;
1516 * Returns true if the user is able to delete this category.
1518 * Note if this category contains any courses this isn't a full check, it will need to be accompanied by a call to either
1519 * {@link coursecat::can_delete_full()} or {@link coursecat::can_move_content_to()} depending upon what the user wished to do.
1521 * @return boolean
1523 public function can_delete() {
1524 if (!$this->has_manage_capability()) {
1525 return false;
1527 return $this->parent_has_manage_capability();
1531 * Returns true if user can delete current category and all its contents
1533 * To be able to delete course category the user must have permission
1534 * 'moodle/category:manage' in ALL child course categories AND
1535 * be able to delete all courses
1537 * @return bool
1539 public function can_delete_full() {
1540 global $DB;
1541 if (!$this->id) {
1542 // Fool-proof.
1543 return false;
1546 $context = $this->get_context();
1547 if (!$this->is_uservisible() ||
1548 !has_capability('moodle/category:manage', $context)) {
1549 return false;
1552 // Check all child categories (not only direct children).
1553 $sql = context_helper::get_preload_record_columns_sql('ctx');
1554 $childcategories = $DB->get_records_sql('SELECT c.id, c.visible, '. $sql.
1555 ' FROM {context} ctx '.
1556 ' JOIN {course_categories} c ON c.id = ctx.instanceid'.
1557 ' WHERE ctx.path like ? AND ctx.contextlevel = ?',
1558 array($context->path. '/%', CONTEXT_COURSECAT));
1559 foreach ($childcategories as $childcat) {
1560 context_helper::preload_from_record($childcat);
1561 $childcontext = context_coursecat::instance($childcat->id);
1562 if ((!$childcat->visible && !has_capability('moodle/category:viewhiddencategories', $childcontext)) ||
1563 !has_capability('moodle/category:manage', $childcontext)) {
1564 return false;
1568 // Check courses.
1569 $sql = context_helper::get_preload_record_columns_sql('ctx');
1570 $coursescontexts = $DB->get_records_sql('SELECT ctx.instanceid AS courseid, '.
1571 $sql. ' FROM {context} ctx '.
1572 'WHERE ctx.path like :pathmask and ctx.contextlevel = :courselevel',
1573 array('pathmask' => $context->path. '/%',
1574 'courselevel' => CONTEXT_COURSE));
1575 foreach ($coursescontexts as $ctxrecord) {
1576 context_helper::preload_from_record($ctxrecord);
1577 if (!can_delete_course($ctxrecord->courseid)) {
1578 return false;
1582 return true;
1586 * Recursively delete category including all subcategories and courses
1588 * Function {@link coursecat::can_delete_full()} MUST be called prior
1589 * to calling this function because there is no capability check
1590 * inside this function
1592 * @param boolean $showfeedback display some notices
1593 * @return array return deleted courses
1594 * @throws moodle_exception
1596 public function delete_full($showfeedback = true) {
1597 global $CFG, $DB;
1599 require_once($CFG->libdir.'/gradelib.php');
1600 require_once($CFG->libdir.'/questionlib.php');
1601 require_once($CFG->dirroot.'/cohort/lib.php');
1603 $deletedcourses = array();
1605 // Get children. Note, we don't want to use cache here because it would be rebuilt too often.
1606 $children = $DB->get_records('course_categories', array('parent' => $this->id), 'sortorder ASC');
1607 foreach ($children as $record) {
1608 $coursecat = new coursecat($record);
1609 $deletedcourses += $coursecat->delete_full($showfeedback);
1612 if ($courses = $DB->get_records('course', array('category' => $this->id), 'sortorder ASC')) {
1613 foreach ($courses as $course) {
1614 if (!delete_course($course, false)) {
1615 throw new moodle_exception('cannotdeletecategorycourse', '', '', $course->shortname);
1617 $deletedcourses[] = $course;
1621 // Move or delete cohorts in this context.
1622 cohort_delete_category($this);
1624 // Now delete anything that may depend on course category context.
1625 grade_course_category_delete($this->id, 0, $showfeedback);
1626 if (!question_delete_course_category($this, 0, $showfeedback)) {
1627 throw new moodle_exception('cannotdeletecategoryquestions', '', '', $this->get_formatted_name());
1630 // Finally delete the category and it's context.
1631 $DB->delete_records('course_categories', array('id' => $this->id));
1633 $coursecatcontext = context_coursecat::instance($this->id);
1634 $coursecatcontext->delete();
1636 cache_helper::purge_by_event('changesincoursecat');
1638 // Trigger a course category deleted event.
1639 /* @var \core\event\course_category_deleted $event */
1640 $event = \core\event\course_category_deleted::create(array(
1641 'objectid' => $this->id,
1642 'context' => $coursecatcontext,
1643 'other' => array('name' => $this->name)
1645 $event->set_coursecat($this);
1646 $event->trigger();
1648 // If we deleted $CFG->defaultrequestcategory, make it point somewhere else.
1649 if ($this->id == $CFG->defaultrequestcategory) {
1650 set_config('defaultrequestcategory', $DB->get_field('course_categories', 'MIN(id)', array('parent' => 0)));
1652 return $deletedcourses;
1656 * Checks if user can delete this category and move content (courses, subcategories and questions)
1657 * to another category. If yes returns the array of possible target categories names
1659 * If user can not manage this category or it is completely empty - empty array will be returned
1661 * @return array
1663 public function move_content_targets_list() {
1664 global $CFG;
1665 require_once($CFG->libdir . '/questionlib.php');
1666 $context = $this->get_context();
1667 if (!$this->is_uservisible() ||
1668 !has_capability('moodle/category:manage', $context)) {
1669 // User is not able to manage current category, he is not able to delete it.
1670 // No possible target categories.
1671 return array();
1674 $testcaps = array();
1675 // If this category has courses in it, user must have 'course:create' capability in target category.
1676 if ($this->has_courses()) {
1677 $testcaps[] = 'moodle/course:create';
1679 // If this category has subcategories or questions, user must have 'category:manage' capability in target category.
1680 if ($this->has_children() || question_context_has_any_questions($context)) {
1681 $testcaps[] = 'moodle/category:manage';
1683 if (!empty($testcaps)) {
1684 // Return list of categories excluding this one and it's children.
1685 return self::make_categories_list($testcaps, $this->id);
1688 // Category is completely empty, no need in target for contents.
1689 return array();
1693 * Checks if user has capability to move all category content to the new parent before
1694 * removing this category
1696 * @param int $newcatid
1697 * @return bool
1699 public function can_move_content_to($newcatid) {
1700 global $CFG;
1701 require_once($CFG->libdir . '/questionlib.php');
1702 $context = $this->get_context();
1703 if (!$this->is_uservisible() ||
1704 !has_capability('moodle/category:manage', $context)) {
1705 return false;
1707 $testcaps = array();
1708 // If this category has courses in it, user must have 'course:create' capability in target category.
1709 if ($this->has_courses()) {
1710 $testcaps[] = 'moodle/course:create';
1712 // If this category has subcategories or questions, user must have 'category:manage' capability in target category.
1713 if ($this->has_children() || question_context_has_any_questions($context)) {
1714 $testcaps[] = 'moodle/category:manage';
1716 if (!empty($testcaps)) {
1717 return has_all_capabilities($testcaps, context_coursecat::instance($newcatid));
1720 // There is no content but still return true.
1721 return true;
1725 * Deletes a category and moves all content (children, courses and questions) to the new parent
1727 * Note that this function does not check capabilities, {@link coursecat::can_move_content_to()}
1728 * must be called prior
1730 * @param int $newparentid
1731 * @param bool $showfeedback
1732 * @return bool
1734 public function delete_move($newparentid, $showfeedback = false) {
1735 global $CFG, $DB, $OUTPUT;
1737 require_once($CFG->libdir.'/gradelib.php');
1738 require_once($CFG->libdir.'/questionlib.php');
1739 require_once($CFG->dirroot.'/cohort/lib.php');
1741 // Get all objects and lists because later the caches will be reset so.
1742 // We don't need to make extra queries.
1743 $newparentcat = self::get($newparentid, MUST_EXIST, true);
1744 $catname = $this->get_formatted_name();
1745 $children = $this->get_children();
1746 $params = array('category' => $this->id);
1747 $coursesids = $DB->get_fieldset_select('course', 'id', 'category = :category ORDER BY sortorder ASC', $params);
1748 $context = $this->get_context();
1750 if ($children) {
1751 foreach ($children as $childcat) {
1752 $childcat->change_parent_raw($newparentcat);
1753 // Log action.
1754 $event = \core\event\course_category_updated::create(array(
1755 'objectid' => $childcat->id,
1756 'context' => $childcat->get_context()
1758 $event->set_legacy_logdata(array(SITEID, 'category', 'move', 'editcategory.php?id=' . $childcat->id,
1759 $childcat->id));
1760 $event->trigger();
1762 fix_course_sortorder();
1765 if ($coursesids) {
1766 if (!move_courses($coursesids, $newparentid)) {
1767 if ($showfeedback) {
1768 echo $OUTPUT->notification("Error moving courses");
1770 return false;
1772 if ($showfeedback) {
1773 echo $OUTPUT->notification(get_string('coursesmovedout', '', $catname), 'notifysuccess');
1777 // Move or delete cohorts in this context.
1778 cohort_delete_category($this);
1780 // Now delete anything that may depend on course category context.
1781 grade_course_category_delete($this->id, $newparentid, $showfeedback);
1782 if (!question_delete_course_category($this, $newparentcat, $showfeedback)) {
1783 if ($showfeedback) {
1784 echo $OUTPUT->notification(get_string('errordeletingquestionsfromcategory', 'question', $catname), 'notifysuccess');
1786 return false;
1789 // Finally delete the category and it's context.
1790 $DB->delete_records('course_categories', array('id' => $this->id));
1791 $context->delete();
1793 // Trigger a course category deleted event.
1794 /* @var \core\event\course_category_deleted $event */
1795 $event = \core\event\course_category_deleted::create(array(
1796 'objectid' => $this->id,
1797 'context' => $context,
1798 'other' => array('name' => $this->name)
1800 $event->set_coursecat($this);
1801 $event->trigger();
1803 cache_helper::purge_by_event('changesincoursecat');
1805 if ($showfeedback) {
1806 echo $OUTPUT->notification(get_string('coursecategorydeleted', '', $catname), 'notifysuccess');
1809 // If we deleted $CFG->defaultrequestcategory, make it point somewhere else.
1810 if ($this->id == $CFG->defaultrequestcategory) {
1811 set_config('defaultrequestcategory', $DB->get_field('course_categories', 'MIN(id)', array('parent' => 0)));
1813 return true;
1817 * Checks if user can move current category to the new parent
1819 * This checks if new parent category exists, user has manage cap there
1820 * and new parent is not a child of this category
1822 * @param int|stdClass|coursecat $newparentcat
1823 * @return bool
1825 public function can_change_parent($newparentcat) {
1826 if (!has_capability('moodle/category:manage', $this->get_context())) {
1827 return false;
1829 if (is_object($newparentcat)) {
1830 $newparentcat = self::get($newparentcat->id, IGNORE_MISSING);
1831 } else {
1832 $newparentcat = self::get((int)$newparentcat, IGNORE_MISSING);
1834 if (!$newparentcat) {
1835 return false;
1837 if ($newparentcat->id == $this->id || in_array($this->id, $newparentcat->get_parents())) {
1838 // Can not move to itself or it's own child.
1839 return false;
1841 if ($newparentcat->id) {
1842 return has_capability('moodle/category:manage', context_coursecat::instance($newparentcat->id));
1843 } else {
1844 return has_capability('moodle/category:manage', context_system::instance());
1849 * Moves the category under another parent category. All associated contexts are moved as well
1851 * This is protected function, use change_parent() or update() from outside of this class
1853 * @see coursecat::change_parent()
1854 * @see coursecat::update()
1856 * @param coursecat $newparentcat
1857 * @throws moodle_exception
1859 protected function change_parent_raw(coursecat $newparentcat) {
1860 global $DB;
1862 $context = $this->get_context();
1864 $hidecat = false;
1865 if (empty($newparentcat->id)) {
1866 $DB->set_field('course_categories', 'parent', 0, array('id' => $this->id));
1867 $newparent = context_system::instance();
1868 } else {
1869 if ($newparentcat->id == $this->id || in_array($this->id, $newparentcat->get_parents())) {
1870 // Can not move to itself or it's own child.
1871 throw new moodle_exception('cannotmovecategory');
1873 $DB->set_field('course_categories', 'parent', $newparentcat->id, array('id' => $this->id));
1874 $newparent = context_coursecat::instance($newparentcat->id);
1876 if (!$newparentcat->visible and $this->visible) {
1877 // Better hide category when moving into hidden category, teachers may unhide afterwards and the hidden children
1878 // will be restored properly.
1879 $hidecat = true;
1882 $this->parent = $newparentcat->id;
1884 $context->update_moved($newparent);
1886 // Now make it last in new category.
1887 $DB->set_field('course_categories', 'sortorder', MAX_COURSES_IN_CATEGORY*MAX_COURSE_CATEGORIES, array('id' => $this->id));
1889 if ($hidecat) {
1890 fix_course_sortorder();
1891 $this->restore();
1892 // Hide object but store 1 in visibleold, because when parent category visibility changes this category must
1893 // become visible again.
1894 $this->hide_raw(1);
1899 * Efficiently moves a category - NOTE that this can have
1900 * a huge impact access-control-wise...
1902 * Note that this function does not check capabilities.
1904 * Example of usage:
1905 * $coursecat = coursecat::get($categoryid);
1906 * if ($coursecat->can_change_parent($newparentcatid)) {
1907 * $coursecat->change_parent($newparentcatid);
1910 * This function does not update field course_categories.timemodified
1911 * If you want to update timemodified, use
1912 * $coursecat->update(array('parent' => $newparentcat));
1914 * @param int|stdClass|coursecat $newparentcat
1916 public function change_parent($newparentcat) {
1917 // Make sure parent category exists but do not check capabilities here that it is visible to current user.
1918 if (is_object($newparentcat)) {
1919 $newparentcat = self::get($newparentcat->id, MUST_EXIST, true);
1920 } else {
1921 $newparentcat = self::get((int)$newparentcat, MUST_EXIST, true);
1923 if ($newparentcat->id != $this->parent) {
1924 $this->change_parent_raw($newparentcat);
1925 fix_course_sortorder();
1926 cache_helper::purge_by_event('changesincoursecat');
1927 $this->restore();
1929 $event = \core\event\course_category_updated::create(array(
1930 'objectid' => $this->id,
1931 'context' => $this->get_context()
1933 $event->set_legacy_logdata(array(SITEID, 'category', 'move', 'editcategory.php?id=' . $this->id, $this->id));
1934 $event->trigger();
1939 * Hide course category and child course and subcategories
1941 * If this category has changed the parent and is moved under hidden
1942 * category we will want to store it's current visibility state in
1943 * the field 'visibleold'. If admin clicked 'hide' for this particular
1944 * category, the field 'visibleold' should become 0.
1946 * All subcategories and courses will have their current visibility in the field visibleold
1948 * This is protected function, use hide() or update() from outside of this class
1950 * @see coursecat::hide()
1951 * @see coursecat::update()
1953 * @param int $visibleold value to set in field $visibleold for this category
1954 * @return bool whether changes have been made and caches need to be purged afterwards
1956 protected function hide_raw($visibleold = 0) {
1957 global $DB;
1958 $changes = false;
1960 // Note that field 'visibleold' is not cached so we must retrieve it from DB if it is missing.
1961 if ($this->id && $this->__get('visibleold') != $visibleold) {
1962 $this->visibleold = $visibleold;
1963 $DB->set_field('course_categories', 'visibleold', $visibleold, array('id' => $this->id));
1964 $changes = true;
1966 if (!$this->visible || !$this->id) {
1967 // Already hidden or can not be hidden.
1968 return $changes;
1971 $this->visible = 0;
1972 $DB->set_field('course_categories', 'visible', 0, array('id'=>$this->id));
1973 // Store visible flag so that we can return to it if we immediately unhide.
1974 $DB->execute("UPDATE {course} SET visibleold = visible WHERE category = ?", array($this->id));
1975 $DB->set_field('course', 'visible', 0, array('category' => $this->id));
1976 // Get all child categories and hide too.
1977 if ($subcats = $DB->get_records_select('course_categories', "path LIKE ?", array("$this->path/%"), 'id, visible')) {
1978 foreach ($subcats as $cat) {
1979 $DB->set_field('course_categories', 'visibleold', $cat->visible, array('id' => $cat->id));
1980 $DB->set_field('course_categories', 'visible', 0, array('id' => $cat->id));
1981 $DB->execute("UPDATE {course} SET visibleold = visible WHERE category = ?", array($cat->id));
1982 $DB->set_field('course', 'visible', 0, array('category' => $cat->id));
1985 return true;
1989 * Hide course category and child course and subcategories
1991 * Note that there is no capability check inside this function
1993 * This function does not update field course_categories.timemodified
1994 * If you want to update timemodified, use
1995 * $coursecat->update(array('visible' => 0));
1997 public function hide() {
1998 if ($this->hide_raw(0)) {
1999 cache_helper::purge_by_event('changesincoursecat');
2001 $event = \core\event\course_category_updated::create(array(
2002 'objectid' => $this->id,
2003 'context' => $this->get_context()
2005 $event->set_legacy_logdata(array(SITEID, 'category', 'hide', 'editcategory.php?id=' . $this->id, $this->id));
2006 $event->trigger();
2011 * Show course category and restores visibility for child course and subcategories
2013 * Note that there is no capability check inside this function
2015 * This is protected function, use show() or update() from outside of this class
2017 * @see coursecat::show()
2018 * @see coursecat::update()
2020 * @return bool whether changes have been made and caches need to be purged afterwards
2022 protected function show_raw() {
2023 global $DB;
2025 if ($this->visible) {
2026 // Already visible.
2027 return false;
2030 $this->visible = 1;
2031 $this->visibleold = 1;
2032 $DB->set_field('course_categories', 'visible', 1, array('id' => $this->id));
2033 $DB->set_field('course_categories', 'visibleold', 1, array('id' => $this->id));
2034 $DB->execute("UPDATE {course} SET visible = visibleold WHERE category = ?", array($this->id));
2035 // Get all child categories and unhide too.
2036 if ($subcats = $DB->get_records_select('course_categories', "path LIKE ?", array("$this->path/%"), 'id, visibleold')) {
2037 foreach ($subcats as $cat) {
2038 if ($cat->visibleold) {
2039 $DB->set_field('course_categories', 'visible', 1, array('id' => $cat->id));
2041 $DB->execute("UPDATE {course} SET visible = visibleold WHERE category = ?", array($cat->id));
2044 return true;
2048 * Show course category and restores visibility for child course and subcategories
2050 * Note that there is no capability check inside this function
2052 * This function does not update field course_categories.timemodified
2053 * If you want to update timemodified, use
2054 * $coursecat->update(array('visible' => 1));
2056 public function show() {
2057 if ($this->show_raw()) {
2058 cache_helper::purge_by_event('changesincoursecat');
2060 $event = \core\event\course_category_updated::create(array(
2061 'objectid' => $this->id,
2062 'context' => $this->get_context()
2064 $event->set_legacy_logdata(array(SITEID, 'category', 'show', 'editcategory.php?id=' . $this->id, $this->id));
2065 $event->trigger();
2070 * Returns name of the category formatted as a string
2072 * @param array $options formatting options other than context
2073 * @return string
2075 public function get_formatted_name($options = array()) {
2076 if ($this->id) {
2077 $context = $this->get_context();
2078 return format_string($this->name, true, array('context' => $context) + $options);
2079 } else {
2080 return get_string('top');
2085 * Returns ids of all parents of the category. Last element in the return array is the direct parent
2087 * For example, if you have a tree of categories like:
2088 * Miscellaneous (id = 1)
2089 * Subcategory (id = 2)
2090 * Sub-subcategory (id = 4)
2091 * Other category (id = 3)
2093 * coursecat::get(1)->get_parents() == array()
2094 * coursecat::get(2)->get_parents() == array(1)
2095 * coursecat::get(4)->get_parents() == array(1, 2);
2097 * Note that this method does not check if all parents are accessible by current user
2099 * @return array of category ids
2101 public function get_parents() {
2102 $parents = preg_split('|/|', $this->path, 0, PREG_SPLIT_NO_EMPTY);
2103 array_pop($parents);
2104 return $parents;
2108 * This function returns a nice list representing category tree
2109 * for display or to use in a form <select> element
2111 * List is cached for 10 minutes
2113 * For example, if you have a tree of categories like:
2114 * Miscellaneous (id = 1)
2115 * Subcategory (id = 2)
2116 * Sub-subcategory (id = 4)
2117 * Other category (id = 3)
2118 * Then after calling this function you will have
2119 * array(1 => 'Miscellaneous',
2120 * 2 => 'Miscellaneous / Subcategory',
2121 * 4 => 'Miscellaneous / Subcategory / Sub-subcategory',
2122 * 3 => 'Other category');
2124 * If you specify $requiredcapability, then only categories where the current
2125 * user has that capability will be added to $list.
2126 * If you only have $requiredcapability in a child category, not the parent,
2127 * then the child catgegory will still be included.
2129 * If you specify the option $excludeid, then that category, and all its children,
2130 * are omitted from the tree. This is useful when you are doing something like
2131 * moving categories, where you do not want to allow people to move a category
2132 * to be the child of itself.
2134 * See also {@link make_categories_options()}
2136 * @param string/array $requiredcapability if given, only categories where the current
2137 * user has this capability will be returned. Can also be an array of capabilities,
2138 * in which case they are all required.
2139 * @param integer $excludeid Exclude this category and its children from the lists built.
2140 * @param string $separator string to use as a separator between parent and child category. Default ' / '
2141 * @return array of strings
2143 public static function make_categories_list($requiredcapability = '', $excludeid = 0, $separator = ' / ') {
2144 global $DB;
2145 $coursecatcache = cache::make('core', 'coursecat');
2147 // Check if we cached the complete list of user-accessible category names ($baselist) or list of ids
2148 // with requried cap ($thislist).
2149 $currentlang = current_language();
2150 $basecachekey = $currentlang . '_catlist';
2151 $baselist = $coursecatcache->get($basecachekey);
2152 $thislist = false;
2153 $thiscachekey = null;
2154 if (!empty($requiredcapability)) {
2155 $requiredcapability = (array)$requiredcapability;
2156 $thiscachekey = 'catlist:'. serialize($requiredcapability);
2157 if ($baselist !== false && ($thislist = $coursecatcache->get($thiscachekey)) !== false) {
2158 $thislist = preg_split('|,|', $thislist, -1, PREG_SPLIT_NO_EMPTY);
2160 } else if ($baselist !== false) {
2161 $thislist = array_keys($baselist);
2164 if ($baselist === false) {
2165 // We don't have $baselist cached, retrieve it. Retrieve $thislist again in any case.
2166 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
2167 $sql = "SELECT cc.id, cc.sortorder, cc.name, cc.visible, cc.parent, cc.path, $ctxselect
2168 FROM {course_categories} cc
2169 JOIN {context} ctx ON cc.id = ctx.instanceid AND ctx.contextlevel = :contextcoursecat
2170 ORDER BY cc.sortorder";
2171 $rs = $DB->get_recordset_sql($sql, array('contextcoursecat' => CONTEXT_COURSECAT));
2172 $baselist = array();
2173 $thislist = array();
2174 foreach ($rs as $record) {
2175 // If the category's parent is not visible to the user, it is not visible as well.
2176 if (!$record->parent || isset($baselist[$record->parent])) {
2177 context_helper::preload_from_record($record);
2178 $context = context_coursecat::instance($record->id);
2179 if (!$record->visible && !has_capability('moodle/category:viewhiddencategories', $context)) {
2180 // No cap to view category, added to neither $baselist nor $thislist.
2181 continue;
2183 $baselist[$record->id] = array(
2184 'name' => format_string($record->name, true, array('context' => $context)),
2185 'path' => $record->path
2187 if (!empty($requiredcapability) && !has_all_capabilities($requiredcapability, $context)) {
2188 // No required capability, added to $baselist but not to $thislist.
2189 continue;
2191 $thislist[] = $record->id;
2194 $rs->close();
2195 $coursecatcache->set($basecachekey, $baselist);
2196 if (!empty($requiredcapability)) {
2197 $coursecatcache->set($thiscachekey, join(',', $thislist));
2199 } else if ($thislist === false) {
2200 // We have $baselist cached but not $thislist. Simplier query is used to retrieve.
2201 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
2202 $sql = "SELECT ctx.instanceid AS id, $ctxselect
2203 FROM {context} ctx WHERE ctx.contextlevel = :contextcoursecat";
2204 $contexts = $DB->get_records_sql($sql, array('contextcoursecat' => CONTEXT_COURSECAT));
2205 $thislist = array();
2206 foreach (array_keys($baselist) as $id) {
2207 context_helper::preload_from_record($contexts[$id]);
2208 if (has_all_capabilities($requiredcapability, context_coursecat::instance($id))) {
2209 $thislist[] = $id;
2212 $coursecatcache->set($thiscachekey, join(',', $thislist));
2215 // Now build the array of strings to return, mind $separator and $excludeid.
2216 $names = array();
2217 foreach ($thislist as $id) {
2218 $path = preg_split('|/|', $baselist[$id]['path'], -1, PREG_SPLIT_NO_EMPTY);
2219 if (!$excludeid || !in_array($excludeid, $path)) {
2220 $namechunks = array();
2221 foreach ($path as $parentid) {
2222 $namechunks[] = $baselist[$parentid]['name'];
2224 $names[$id] = join($separator, $namechunks);
2227 return $names;
2231 * Prepares the object for caching. Works like the __sleep method.
2233 * implementing method from interface cacheable_object
2235 * @return array ready to be cached
2237 public function prepare_to_cache() {
2238 $a = array();
2239 foreach (self::$coursecatfields as $property => $cachedirectives) {
2240 if ($cachedirectives !== null) {
2241 list($shortname, $defaultvalue) = $cachedirectives;
2242 if ($this->$property !== $defaultvalue) {
2243 $a[$shortname] = $this->$property;
2247 $context = $this->get_context();
2248 $a['xi'] = $context->id;
2249 $a['xp'] = $context->path;
2250 return $a;
2254 * Takes the data provided by prepare_to_cache and reinitialises an instance of the associated from it.
2256 * implementing method from interface cacheable_object
2258 * @param array $a
2259 * @return coursecat
2261 public static function wake_from_cache($a) {
2262 $record = new stdClass;
2263 foreach (self::$coursecatfields as $property => $cachedirectives) {
2264 if ($cachedirectives !== null) {
2265 list($shortname, $defaultvalue) = $cachedirectives;
2266 if (array_key_exists($shortname, $a)) {
2267 $record->$property = $a[$shortname];
2268 } else {
2269 $record->$property = $defaultvalue;
2273 $record->ctxid = $a['xi'];
2274 $record->ctxpath = $a['xp'];
2275 $record->ctxdepth = $record->depth + 1;
2276 $record->ctxlevel = CONTEXT_COURSECAT;
2277 $record->ctxinstance = $record->id;
2278 return new coursecat($record, true);
2282 * Returns true if the user is able to create a top level category.
2283 * @return bool
2285 public static function can_create_top_level_category() {
2286 return has_capability('moodle/category:manage', context_system::instance());
2290 * Returns the category context.
2291 * @return context_coursecat
2293 public function get_context() {
2294 if ($this->id === 0) {
2295 // This is the special top level category object.
2296 return context_system::instance();
2297 } else {
2298 return context_coursecat::instance($this->id);
2303 * Returns true if the user is able to manage this category.
2304 * @return bool
2306 public function has_manage_capability() {
2307 if ($this->hasmanagecapability === null) {
2308 $this->hasmanagecapability = has_capability('moodle/category:manage', $this->get_context());
2310 return $this->hasmanagecapability;
2314 * Returns true if the user has the manage capability on the parent category.
2315 * @return bool
2317 public function parent_has_manage_capability() {
2318 return has_capability('moodle/category:manage', get_category_or_system_context($this->parent));
2322 * Returns true if the current user can create subcategories of this category.
2323 * @return bool
2325 public function can_create_subcategory() {
2326 return $this->has_manage_capability();
2330 * Returns true if the user can resort this categories sub categories and courses.
2331 * Must have manage capability and be able to see all subcategories.
2332 * @return bool
2334 public function can_resort_subcategories() {
2335 return $this->has_manage_capability() && !$this->get_not_visible_children_ids();
2339 * Returns true if the user can resort the courses within this category.
2340 * Must have manage capability and be able to see all courses.
2341 * @return bool
2343 public function can_resort_courses() {
2344 return $this->has_manage_capability() && $this->coursecount == $this->get_courses_count();
2348 * Returns true of the user can change the sortorder of this category (resort in the parent category)
2349 * @return bool
2351 public function can_change_sortorder() {
2352 return $this->id && $this->get_parent_coursecat()->can_resort_subcategories();
2356 * Returns true if the current user can create a course within this category.
2357 * @return bool
2359 public function can_create_course() {
2360 return has_capability('moodle/course:create', $this->get_context());
2364 * Returns true if the current user can edit this categories settings.
2365 * @return bool
2367 public function can_edit() {
2368 return $this->has_manage_capability();
2372 * Returns true if the current user can review role assignments for this category.
2373 * @return bool
2375 public function can_review_roles() {
2376 return has_capability('moodle/role:assign', $this->get_context());
2380 * Returns true if the current user can review permissions for this category.
2381 * @return bool
2383 public function can_review_permissions() {
2384 return has_any_capability(array(
2385 'moodle/role:assign',
2386 'moodle/role:safeoverride',
2387 'moodle/role:override',
2388 'moodle/role:assign'
2389 ), $this->get_context());
2393 * Returns true if the current user can review cohorts for this category.
2394 * @return bool
2396 public function can_review_cohorts() {
2397 return has_any_capability(array('moodle/cohort:view', 'moodle/cohort:manage'), $this->get_context());
2401 * Returns true if the current user can review filter settings for this category.
2402 * @return bool
2404 public function can_review_filters() {
2405 return has_capability('moodle/filter:manage', $this->get_context()) &&
2406 count(filter_get_available_in_context($this->get_context()))>0;
2410 * Returns true if the current user is able to change the visbility of this category.
2411 * @return bool
2413 public function can_change_visibility() {
2414 return $this->parent_has_manage_capability();
2418 * Returns true if the user can move courses out of this category.
2419 * @return bool
2421 public function can_move_courses_out_of() {
2422 return $this->has_manage_capability();
2426 * Returns true if the user can move courses into this category.
2427 * @return bool
2429 public function can_move_courses_into() {
2430 return $this->has_manage_capability();
2434 * Returns true if the user is able to restore a course into this category as a new course.
2435 * @return bool
2437 public function can_restore_courses_into() {
2438 return has_capability('moodle/course:create', $this->get_context());
2442 * Resorts the sub categories of this category by the given field.
2444 * @param string $field
2445 * @param bool $cleanup If true cleanup will be done, if false you will need to do it manually later.
2446 * @return bool True on success.
2447 * @throws coding_exception
2449 public function resort_subcategories($field, $cleanup = true) {
2450 global $DB;
2451 if ($field !== 'name' && $field !== 'idnumber') {
2452 throw new coding_exception('Invalid field requested');
2454 $children = $this->get_children();
2455 core_collator::asort_objects_by_property($children, $field, core_collator::SORT_NATURAL);
2456 $i = 1;
2457 foreach ($children as $cat) {
2458 $i++;
2459 $DB->set_field('course_categories', 'sortorder', $i, array('id' => $cat->id));
2460 $i += $cat->coursecount;
2462 if ($cleanup) {
2463 self::resort_categories_cleanup();
2465 return true;
2469 * Cleans things up after categories have been resorted.
2470 * @param bool $includecourses If set to true we know courses have been resorted as well.
2472 public static function resort_categories_cleanup($includecourses = false) {
2473 // This should not be needed but we do it just to be safe.
2474 fix_course_sortorder();
2475 cache_helper::purge_by_event('changesincoursecat');
2476 if ($includecourses) {
2477 cache_helper::purge_by_event('changesincourse');
2482 * Resort the courses within this category by the given field.
2484 * @param string $field One of fullname, shortname or idnumber
2485 * @param bool $cleanup
2486 * @return bool True for success.
2487 * @throws coding_exception
2489 public function resort_courses($field, $cleanup = true) {
2490 global $DB;
2491 if ($field !== 'fullname' && $field !== 'shortname' && $field !== 'idnumber') {
2492 // This is ultra important as we use $field in an SQL statement below this.
2493 throw new coding_exception('Invalid field requested');
2495 $ctxfields = context_helper::get_preload_record_columns_sql('ctx');
2496 $sql = "SELECT c.id, c.sortorder, c.{$field}, $ctxfields
2497 FROM {course} c
2498 LEFT JOIN {context} ctx ON ctx.instanceid = c.id
2499 WHERE ctx.contextlevel = :ctxlevel AND
2500 c.category = :categoryid
2501 ORDER BY c.{$field}, c.sortorder";
2502 $params = array(
2503 'ctxlevel' => CONTEXT_COURSE,
2504 'categoryid' => $this->id
2506 $courses = $DB->get_records_sql($sql, $params);
2507 if (count($courses) > 0) {
2508 foreach ($courses as $courseid => $course) {
2509 context_helper::preload_from_record($course);
2510 if ($field === 'idnumber') {
2511 $course->sortby = $course->idnumber;
2512 } else {
2513 // It'll require formatting.
2514 $options = array(
2515 'context' => context_course::instance($course->id)
2517 // We format the string first so that it appears as the user would see it.
2518 // This ensures the sorting makes sense to them. However it won't necessarily make
2519 // sense to everyone if things like multilang filters are enabled.
2520 // We then strip any tags as we don't want things such as image tags skewing the
2521 // sort results.
2522 $course->sortby = strip_tags(format_string($course->$field, true, $options));
2524 // We set it back here rather than using references as there is a bug with using
2525 // references in a foreach before passing as an arg by reference.
2526 $courses[$courseid] = $course;
2528 // Sort the courses.
2529 core_collator::asort_objects_by_property($courses, 'sortby', core_collator::SORT_NATURAL);
2530 $i = 1;
2531 foreach ($courses as $course) {
2532 $DB->set_field('course', 'sortorder', $this->sortorder + $i, array('id' => $course->id));
2533 $i++;
2535 if ($cleanup) {
2536 // This should not be needed but we do it just to be safe.
2537 fix_course_sortorder();
2538 cache_helper::purge_by_event('changesincourse');
2541 return true;
2545 * Changes the sort order of this categories parent shifting this category up or down one.
2547 * @global \moodle_database $DB
2548 * @param bool $up If set to true the category is shifted up one spot, else its moved down.
2549 * @return bool True on success, false otherwise.
2551 public function change_sortorder_by_one($up) {
2552 global $DB;
2553 $params = array($this->sortorder, $this->parent);
2554 if ($up) {
2555 $select = 'sortorder < ? AND parent = ?';
2556 $sort = 'sortorder DESC';
2557 } else {
2558 $select = 'sortorder > ? AND parent = ?';
2559 $sort = 'sortorder ASC';
2561 fix_course_sortorder();
2562 $swapcategory = $DB->get_records_select('course_categories', $select, $params, $sort, '*', 0, 1);
2563 $swapcategory = reset($swapcategory);
2564 if ($swapcategory) {
2565 $DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder, array('id' => $this->id));
2566 $DB->set_field('course_categories', 'sortorder', $this->sortorder, array('id' => $swapcategory->id));
2567 $this->sortorder = $swapcategory->sortorder;
2569 $event = \core\event\course_category_updated::create(array(
2570 'objectid' => $this->id,
2571 'context' => $this->get_context()
2573 $event->set_legacy_logdata(array(SITEID, 'category', 'move', 'management.php?categoryid=' . $this->id,
2574 $this->id));
2575 $event->trigger();
2577 // Finally reorder courses.
2578 fix_course_sortorder();
2579 cache_helper::purge_by_event('changesincoursecat');
2580 return true;
2582 return false;
2586 * Returns the parent coursecat object for this category.
2588 * @return coursecat
2590 public function get_parent_coursecat() {
2591 return self::get($this->parent);
2596 * Returns true if the user is able to request a new course be created.
2597 * @return bool
2599 public function can_request_course() {
2600 global $CFG;
2601 if (empty($CFG->enablecourserequests) || $this->id != $CFG->defaultrequestcategory) {
2602 return false;
2604 return !$this->can_create_course() && has_capability('moodle/course:request', $this->get_context());
2608 * Returns true if the user can approve course requests.
2609 * @return bool
2611 public static function can_approve_course_requests() {
2612 global $CFG, $DB;
2613 if (empty($CFG->enablecourserequests)) {
2614 return false;
2616 $context = context_system::instance();
2617 if (!has_capability('moodle/site:approvecourse', $context)) {
2618 return false;
2620 if (!$DB->record_exists('course_request', array())) {
2621 return false;
2623 return true;
2628 * Class to store information about one course in a list of courses
2630 * Not all information may be retrieved when object is created but
2631 * it will be retrieved on demand when appropriate property or method is
2632 * called.
2634 * Instances of this class are usually returned by functions
2635 * {@link coursecat::search_courses()}
2636 * and
2637 * {@link coursecat::get_courses()}
2639 * @property-read int $id
2640 * @property-read int $category Category ID
2641 * @property-read int $sortorder
2642 * @property-read string $fullname
2643 * @property-read string $shortname
2644 * @property-read string $idnumber
2645 * @property-read string $summary Course summary. Field is present if coursecat::get_courses()
2646 * was called with option 'summary'. Otherwise will be retrieved from DB on first request
2647 * @property-read int $summaryformat Summary format. Field is present if coursecat::get_courses()
2648 * was called with option 'summary'. Otherwise will be retrieved from DB on first request
2649 * @property-read string $format Course format. Retrieved from DB on first request
2650 * @property-read int $showgrades Retrieved from DB on first request
2651 * @property-read int $newsitems Retrieved from DB on first request
2652 * @property-read int $startdate
2653 * @property-read int $marker Retrieved from DB on first request
2654 * @property-read int $maxbytes Retrieved from DB on first request
2655 * @property-read int $legacyfiles Retrieved from DB on first request
2656 * @property-read int $showreports Retrieved from DB on first request
2657 * @property-read int $visible
2658 * @property-read int $visibleold Retrieved from DB on first request
2659 * @property-read int $groupmode Retrieved from DB on first request
2660 * @property-read int $groupmodeforce Retrieved from DB on first request
2661 * @property-read int $defaultgroupingid Retrieved from DB on first request
2662 * @property-read string $lang Retrieved from DB on first request
2663 * @property-read string $theme Retrieved from DB on first request
2664 * @property-read int $timecreated Retrieved from DB on first request
2665 * @property-read int $timemodified Retrieved from DB on first request
2666 * @property-read int $requested Retrieved from DB on first request
2667 * @property-read int $enablecompletion Retrieved from DB on first request
2668 * @property-read int $completionnotify Retrieved from DB on first request
2669 * @property-read int $cacherev
2671 * @package core
2672 * @subpackage course
2673 * @copyright 2013 Marina Glancy
2674 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2676 class course_in_list implements IteratorAggregate {
2678 /** @var stdClass record retrieved from DB, may have additional calculated property such as managers and hassummary */
2679 protected $record;
2681 /** @var array array of course contacts - stores result of call to get_course_contacts() */
2682 protected $coursecontacts;
2684 /** @var bool true if the current user can access the course, false otherwise. */
2685 protected $canaccess = null;
2688 * Creates an instance of the class from record
2690 * @param stdClass $record except fields from course table it may contain
2691 * field hassummary indicating that summary field is not empty.
2692 * Also it is recommended to have context fields here ready for
2693 * context preloading
2695 public function __construct(stdClass $record) {
2696 context_helper::preload_from_record($record);
2697 $this->record = new stdClass();
2698 foreach ($record as $key => $value) {
2699 $this->record->$key = $value;
2704 * Indicates if the course has non-empty summary field
2706 * @return bool
2708 public function has_summary() {
2709 if (isset($this->record->hassummary)) {
2710 return !empty($this->record->hassummary);
2712 if (!isset($this->record->summary)) {
2713 // We need to retrieve summary.
2714 $this->__get('summary');
2716 return !empty($this->record->summary);
2720 * Indicates if the course have course contacts to display
2722 * @return bool
2724 public function has_course_contacts() {
2725 if (!isset($this->record->managers)) {
2726 $courses = array($this->id => &$this->record);
2727 coursecat::preload_course_contacts($courses);
2729 return !empty($this->record->managers);
2733 * Returns list of course contacts (usually teachers) to display in course link
2735 * Roles to display are set up in $CFG->coursecontact
2737 * The result is the list of users where user id is the key and the value
2738 * is an array with elements:
2739 * - 'user' - object containing basic user information
2740 * - 'role' - object containing basic role information (id, name, shortname, coursealias)
2741 * - 'rolename' => role_get_name($role, $context, ROLENAME_ALIAS)
2742 * - 'username' => fullname($user, $canviewfullnames)
2744 * @return array
2746 public function get_course_contacts() {
2747 global $CFG;
2748 if (empty($CFG->coursecontact)) {
2749 // No roles are configured to be displayed as course contacts.
2750 return array();
2752 if ($this->coursecontacts === null) {
2753 $this->coursecontacts = array();
2754 $context = context_course::instance($this->id);
2756 if (!isset($this->record->managers)) {
2757 // Preload course contacts from DB.
2758 $courses = array($this->id => &$this->record);
2759 coursecat::preload_course_contacts($courses);
2762 // Build return array with full roles names (for this course context) and users names.
2763 $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
2764 foreach ($this->record->managers as $ruser) {
2765 if (isset($this->coursecontacts[$ruser->id])) {
2766 // Only display a user once with the highest sortorder role.
2767 continue;
2769 $user = new stdClass();
2770 $user = username_load_fields_from_object($user, $ruser, null, array('id', 'username'));
2771 $role = new stdClass();
2772 $role->id = $ruser->roleid;
2773 $role->name = $ruser->rolename;
2774 $role->shortname = $ruser->roleshortname;
2775 $role->coursealias = $ruser->rolecoursealias;
2777 $this->coursecontacts[$user->id] = array(
2778 'user' => $user,
2779 'role' => $role,
2780 'rolename' => role_get_name($role, $context, ROLENAME_ALIAS),
2781 'username' => fullname($user, $canviewfullnames)
2785 return $this->coursecontacts;
2789 * Checks if course has any associated overview files
2791 * @return bool
2793 public function has_course_overviewfiles() {
2794 global $CFG;
2795 if (empty($CFG->courseoverviewfileslimit)) {
2796 return false;
2798 $fs = get_file_storage();
2799 $context = context_course::instance($this->id);
2800 return !$fs->is_area_empty($context->id, 'course', 'overviewfiles');
2804 * Returns all course overview files
2806 * @return array array of stored_file objects
2808 public function get_course_overviewfiles() {
2809 global $CFG;
2810 if (empty($CFG->courseoverviewfileslimit)) {
2811 return array();
2813 require_once($CFG->libdir. '/filestorage/file_storage.php');
2814 require_once($CFG->dirroot. '/course/lib.php');
2815 $fs = get_file_storage();
2816 $context = context_course::instance($this->id);
2817 $files = $fs->get_area_files($context->id, 'course', 'overviewfiles', false, 'filename', false);
2818 if (count($files)) {
2819 $overviewfilesoptions = course_overviewfiles_options($this->id);
2820 $acceptedtypes = $overviewfilesoptions['accepted_types'];
2821 if ($acceptedtypes !== '*') {
2822 // Filter only files with allowed extensions.
2823 require_once($CFG->libdir. '/filelib.php');
2824 foreach ($files as $key => $file) {
2825 if (!file_extension_in_typegroup($file->get_filename(), $acceptedtypes)) {
2826 unset($files[$key]);
2830 if (count($files) > $CFG->courseoverviewfileslimit) {
2831 // Return no more than $CFG->courseoverviewfileslimit files.
2832 $files = array_slice($files, 0, $CFG->courseoverviewfileslimit, true);
2835 return $files;
2839 * Magic method to check if property is set
2841 * @param string $name
2842 * @return bool
2844 public function __isset($name) {
2845 return isset($this->record->$name);
2849 * Magic method to get a course property
2851 * Returns any field from table course (retrieves it from DB if it was not retrieved before)
2853 * @param string $name
2854 * @return mixed
2856 public function __get($name) {
2857 global $DB;
2858 if (property_exists($this->record, $name)) {
2859 return $this->record->$name;
2860 } else if ($name === 'summary' || $name === 'summaryformat') {
2861 // Retrieve fields summary and summaryformat together because they are most likely to be used together.
2862 $record = $DB->get_record('course', array('id' => $this->record->id), 'summary, summaryformat', MUST_EXIST);
2863 $this->record->summary = $record->summary;
2864 $this->record->summaryformat = $record->summaryformat;
2865 return $this->record->$name;
2866 } else if (array_key_exists($name, $DB->get_columns('course'))) {
2867 // Another field from table 'course' that was not retrieved.
2868 $this->record->$name = $DB->get_field('course', $name, array('id' => $this->record->id), MUST_EXIST);
2869 return $this->record->$name;
2871 debugging('Invalid course property accessed! '.$name);
2872 return null;
2876 * All properties are read only, sorry.
2878 * @param string $name
2880 public function __unset($name) {
2881 debugging('Can not unset '.get_class($this).' instance properties!');
2885 * Magic setter method, we do not want anybody to modify properties from the outside
2887 * @param string $name
2888 * @param mixed $value
2890 public function __set($name, $value) {
2891 debugging('Can not change '.get_class($this).' instance properties!');
2895 * Create an iterator because magic vars can't be seen by 'foreach'.
2896 * Exclude context fields
2898 * Implementing method from interface IteratorAggregate
2900 * @return ArrayIterator
2902 public function getIterator() {
2903 $ret = array('id' => $this->record->id);
2904 foreach ($this->record as $property => $value) {
2905 $ret[$property] = $value;
2907 return new ArrayIterator($ret);
2911 * Returns the name of this course as it should be displayed within a list.
2912 * @return string
2914 public function get_formatted_name() {
2915 return format_string(get_course_display_name_for_list($this), true, $this->get_context());
2919 * Returns the formatted fullname for this course.
2920 * @return string
2922 public function get_formatted_fullname() {
2923 return format_string($this->__get('fullname'), true, $this->get_context());
2927 * Returns the formatted shortname for this course.
2928 * @return string
2930 public function get_formatted_shortname() {
2931 return format_string($this->__get('shortname'), true, $this->get_context());
2935 * Returns true if the current user can access this course.
2936 * @return bool
2938 public function can_access() {
2939 if ($this->canaccess === null) {
2940 $this->canaccess = can_access_course($this->record);
2942 return $this->canaccess;
2946 * Returns true if the user can edit this courses settings.
2948 * Note: this function does not check that the current user can access the course.
2949 * To do that please call require_login with the course, or if not possible call {@see course_in_list::can_access()}
2951 * @return bool
2953 public function can_edit() {
2954 return has_capability('moodle/course:update', $this->get_context());
2958 * Returns true if the user can change the visibility of this course.
2960 * Note: this function does not check that the current user can access the course.
2961 * To do that please call require_login with the course, or if not possible call {@see course_in_list::can_access()}
2963 * @return bool
2965 public function can_change_visibility() {
2966 // You must be able to both hide a course and view the hidden course.
2967 return has_all_capabilities(array('moodle/course:visibility', 'moodle/course:viewhiddencourses'), $this->get_context());
2971 * Returns the context for this course.
2972 * @return context_course
2974 public function get_context() {
2975 return context_course::instance($this->__get('id'));
2979 * Returns true if this course is visible to the current user.
2980 * @return bool
2982 public function is_uservisible() {
2983 return $this->visible || has_capability('moodle/course:viewhiddencourses', $this->get_context());
2987 * Returns true if the current user can review enrolments for this course.
2989 * Note: this function does not check that the current user can access the course.
2990 * To do that please call require_login with the course, or if not possible call {@see course_in_list::can_access()}
2992 * @return bool
2994 public function can_review_enrolments() {
2995 return has_capability('moodle/course:enrolreview', $this->get_context());
2999 * Returns true if the current user can delete this course.
3001 * Note: this function does not check that the current user can access the course.
3002 * To do that please call require_login with the course, or if not possible call {@see course_in_list::can_access()}
3004 * @return bool
3006 public function can_delete() {
3007 return can_delete_course($this->id);
3011 * Returns true if the current user can backup this course.
3013 * Note: this function does not check that the current user can access the course.
3014 * To do that please call require_login with the course, or if not possible call {@see course_in_list::can_access()}
3016 * @return bool
3018 public function can_backup() {
3019 return has_capability('moodle/backup:backupcourse', $this->get_context());
3023 * Returns true if the current user can restore this course.
3025 * Note: this function does not check that the current user can access the course.
3026 * To do that please call require_login with the course, or if not possible call {@see course_in_list::can_access()}
3028 * @return bool
3030 public function can_restore() {
3031 return has_capability('moodle/restore:restorecourse', $this->get_context());
3036 * An array of records that is sortable by many fields.
3038 * For more info on the ArrayObject class have a look at php.net.
3040 * @package core
3041 * @subpackage course
3042 * @copyright 2013 Sam Hemelryk
3043 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3045 class coursecat_sortable_records extends ArrayObject {
3048 * An array of sortable fields.
3049 * Gets set temporarily when sort is called.
3050 * @var array
3052 protected $sortfields = array();
3055 * Sorts this array using the given fields.
3057 * @param array $records
3058 * @param array $fields
3059 * @return array
3061 public static function sort(array $records, array $fields) {
3062 $records = new coursecat_sortable_records($records);
3063 $records->sortfields = $fields;
3064 $records->uasort(array($records, 'sort_by_many_fields'));
3065 return $records->getArrayCopy();
3069 * Sorts the two records based upon many fields.
3071 * This method should not be called itself, please call $sort instead.
3072 * It has been marked as access private as such.
3074 * @access private
3075 * @param stdClass $a
3076 * @param stdClass $b
3077 * @return int
3079 public function sort_by_many_fields($a, $b) {
3080 foreach ($this->sortfields as $field => $mult) {
3081 // Nulls first.
3082 if (is_null($a->$field) && !is_null($b->$field)) {
3083 return -$mult;
3085 if (is_null($b->$field) && !is_null($a->$field)) {
3086 return $mult;
3089 if (is_string($a->$field) || is_string($b->$field)) {
3090 // String fields.
3091 if ($cmp = strcoll($a->$field, $b->$field)) {
3092 return $mult * $cmp;
3094 } else {
3095 // Int fields.
3096 if ($a->$field > $b->$field) {
3097 return $mult;
3099 if ($a->$field < $b->$field) {
3100 return -$mult;
3104 return 0;