2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Contains class coursecat reponsible for course category operations
22 * @copyright 2013 Marina Glancy
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
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
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.
83 protected $idnumber = null;
86 protected $description = false;
89 protected $descriptionformat = false;
92 protected $parent = 0;
95 protected $sortorder = 0;
98 protected $coursecount = false;
101 protected $visible = 1;
104 protected $visibleold = false;
107 protected $timemodified = false;
110 protected $depth = 0;
113 protected $path = '';
116 protected $theme = false;
119 protected $fromcache;
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
140 public function __get($name) {
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;
154 debugging('Invalid coursecat property accessed! '.$name, DEBUG_DEVELOPER
);
159 * Full support for isset on our magic read only properties.
161 * @param string $name
164 public function __isset($name) {
165 if (array_key_exists($name, self
::$coursecatfields)) {
166 return isset($this->$name);
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() {
189 foreach (self
::$coursecatfields as $property => $unused) {
190 if ($this->$property !== false) {
191 $ret[$property] = $this->$property;
194 return new ArrayIterator($ret);
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)) {
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) {
237 if (!isset(self
::$coursecat0)) {
238 $record = new stdClass();
240 $record->visible
= 1;
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);
254 $coursecatrecordcache->set($id, $coursecat);
257 if ($coursecat && ($alwaysreturnhidden ||
$coursecat->is_uservisible())) {
260 if ($strictness == MUST_EXIST
) {
261 throw new moodle_exception('unknowcategory');
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) {
276 $coursecatrecordcache = cache
::make('core', 'coursecatrecords');
277 $categories = $coursecatrecordcache->get_many($ids);
279 foreach ($categories as $id => $result) {
280 if ($result === false) {
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);
288 foreach ($records as $record) {
289 $categories[$record->id
] = new coursecat($record);
290 $toset[$record->id
] = $categories[$record->id
];
292 $coursecatrecordcache->set_many($toset);
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
305 public static function get_default() {
306 if ($visiblechildren = self
::get(0)->get_children()) {
307 $defcategory = reset($visiblechildren);
309 $toplevelcategories = self
::get_tree(0);
310 $defcategoryid = $toplevelcategories[0];
311 $defcategory = self
::get($defcategoryid, MUST_EXIST
, true);
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.
342 * @throws moodle_exception
344 public static function create($data, $editoroptions = null) {
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);
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;
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
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) {
464 // There is no actual DB record associated with root category.
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
;
505 if (isset($data->visible
)) {
506 if ($data->visible
) {
507 $changes = $this->show_raw();
509 $changes = $this->hide_raw(0);
513 if (isset($data->parent
) && $data->parent
!= $this->parent
) {
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
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.
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
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() {
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];
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));
588 foreach ($ids as $id) {
589 if ($coursecat = self
::get($id, IGNORE_MISSING
)) {
590 $rv[$id] = $coursecat;
598 * Returns the complete corresponding record from DB table course_categories
600 * Mostly used in deprecated functions
604 public function get_db_record() {
606 if ($record = $DB->get_record('course_categories', array('id' => $this->id
))) {
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
628 protected static function get_tree($id) {
630 $coursecattreecache = cache
::make('core', 'coursecattree');
631 $rv = $coursecattreecache->get($id);
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());
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
;
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
;
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();
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)) {
678 // Requested non-existing category.
683 * Returns number of ALL categories in the system regardless if
684 * they are visible to current user or not
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) {
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,
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
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) {
744 if (empty($courses) ||
empty($CFG->coursecontact
)) {
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
) {
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];
767 // Array $courseids now stores list of ids of courses for which we still need to retrieve contacts.
768 if (empty($courseids)) {
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
;
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]);
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) {
845 // If the input array is too big, split it into chunks.
846 $maxcoursesinquery = 20;
847 if (count($courseusers) > $maxcoursesinquery) {
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);
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);
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;
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
;
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) {
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';
912 $fields[] = $DB->sql_substr('c.summary', 1, 1). ' as hassummary';
914 $sql = "SELECT ". join(',', $fields). ", $ctxselect
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);
945 * Returns array of ids of children categories that current user can not see
947 * This data is cached in user session cache
951 protected function get_not_visible_children_ids() {
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();
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
987 protected static function sort_records(&$records, $sortfields) {
988 if (empty($records)) {
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
;
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);
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!
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()) {
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'];
1039 $sortfields = array('sortorder' => 1);
1042 if (!empty($options['limit']) && (int)$options['limit']) {
1043 $limit = (int)$options['limit'];
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);
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);
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)) {
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);
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);
1098 foreach ($sortedids as $id) {
1099 if (isset($records[$id])) {
1100 $rv[$id] = new coursecat($records[$id]);
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.
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.
1124 public static function has_capability_on_any($capabilities) {
1126 if (!isloggedin() ||
isguestuser()) {
1130 if (!is_array($capabilities)) {
1131 $capabilities = array($capabilities);
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') {
1145 } else if ($capability === false) {
1149 if ($needtoload === false) {
1150 // All capabilities were retrieved and the user didn't have any.
1155 $fields = context_helper
::get_preload_record_columns_sql('ctx');
1156 $sql = "SELECT ctx.instanceid AS categoryid, $fields
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;
1172 $recordset->close();
1173 if ($haskey === null) {
1175 foreach ($keys as $key) {
1178 $cache->set_many($data);
1181 $cache->set($haskey, '1');
1187 * Returns true if the user can resort any category.
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.
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
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
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)
1229 public function has_courses() {
1231 return $DB->record_exists_sql("select 1 from {course} where category = ?",
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()) {
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);
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]);
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);
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)";
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');
1315 debugging('No criteria is specified while searching courses', DEBUG_DEVELOPER
);
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.
1335 foreach ($records as $record) {
1336 $courses[$record->id
] = new course_in_list($record);
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
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);
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!
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()) {
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)) {
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);
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]);
1449 // Retrieve list of courses in category.
1450 $where = 'c.id <> :siteid';
1451 $params = array('siteid' => SITEID
);
1454 $context = context_coursecat
::instance($this->id
);
1455 $where .= ' AND ctx.path like :path';
1456 $params['path'] = $context->path
. '/%';
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.
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);
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.)
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);
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.
1523 public function can_delete() {
1524 if (!$this->has_manage_capability()) {
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
1539 public function can_delete_full() {
1546 $context = $this->get_context();
1547 if (!$this->is_uservisible() ||
1548 !has_capability('moodle/category:manage', $context)) {
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)) {
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
)) {
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) {
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);
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
1663 public function move_content_targets_list() {
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.
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.
1693 * Checks if user has capability to move all category content to the new parent before
1694 * removing this category
1696 * @param int $newcatid
1699 public function can_move_content_to($newcatid) {
1701 require_once($CFG->libdir
. '/questionlib.php');
1702 $context = $this->get_context();
1703 if (!$this->is_uservisible() ||
1704 !has_capability('moodle/category:manage', $context)) {
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.
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
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();
1751 foreach ($children as $childcat) {
1752 $childcat->change_parent_raw($newparentcat);
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
,
1762 fix_course_sortorder();
1766 if (!move_courses($coursesids, $newparentid)) {
1767 if ($showfeedback) {
1768 echo $OUTPUT->notification("Error moving courses");
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');
1789 // Finally delete the category and it's context.
1790 $DB->delete_records('course_categories', array('id' => $this->id
));
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);
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)));
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
1825 public function can_change_parent($newparentcat) {
1826 if (!has_capability('moodle/category:manage', $this->get_context())) {
1829 if (is_object($newparentcat)) {
1830 $newparentcat = self
::get($newparentcat->id
, IGNORE_MISSING
);
1832 $newparentcat = self
::get((int)$newparentcat, IGNORE_MISSING
);
1834 if (!$newparentcat) {
1837 if ($newparentcat->id
== $this->id ||
in_array($this->id
, $newparentcat->get_parents())) {
1838 // Can not move to itself or it's own child.
1841 if ($newparentcat->id
) {
1842 return has_capability('moodle/category:manage', context_coursecat
::instance($newparentcat->id
));
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) {
1862 $context = $this->get_context();
1865 if (empty($newparentcat->id
)) {
1866 $DB->set_field('course_categories', 'parent', 0, array('id' => $this->id
));
1867 $newparent = context_system
::instance();
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.
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
));
1890 fix_course_sortorder();
1892 // Hide object but store 1 in visibleold, because when parent category visibility changes this category must
1893 // become visible again.
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.
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);
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');
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
));
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) {
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
));
1966 if (!$this->visible ||
!$this->id
) {
1967 // Already hidden or can not be hidden.
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
));
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
));
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() {
2025 if ($this->visible
) {
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
));
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
));
2070 * Returns name of the category formatted as a string
2072 * @param array $options formatting options other than context
2075 public function get_formatted_name($options = array()) {
2077 $context = $this->get_context();
2078 return format_string($this->name
, true, array('context' => $context) +
$options);
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);
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 = ' / ') {
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 $basecachekey = 'catlist';
2150 $baselist = $coursecatcache->get($basecachekey);
2152 $thiscachekey = null;
2153 if (!empty($requiredcapability)) {
2154 $requiredcapability = (array)$requiredcapability;
2155 $thiscachekey = 'catlist:'. serialize($requiredcapability);
2156 if ($baselist !== false && ($thislist = $coursecatcache->get($thiscachekey)) !== false) {
2157 $thislist = preg_split('|,|', $thislist, -1, PREG_SPLIT_NO_EMPTY
);
2159 } else if ($baselist !== false) {
2160 $thislist = array_keys($baselist);
2163 if ($baselist === false) {
2164 // We don't have $baselist cached, retrieve it. Retrieve $thislist again in any case.
2165 $ctxselect = context_helper
::get_preload_record_columns_sql('ctx');
2166 $sql = "SELECT cc.id, cc.sortorder, cc.name, cc.visible, cc.parent, cc.path, $ctxselect
2167 FROM {course_categories} cc
2168 JOIN {context} ctx ON cc.id = ctx.instanceid AND ctx.contextlevel = :contextcoursecat
2169 ORDER BY cc.sortorder";
2170 $rs = $DB->get_recordset_sql($sql, array('contextcoursecat' => CONTEXT_COURSECAT
));
2171 $baselist = array();
2172 $thislist = array();
2173 foreach ($rs as $record) {
2174 // If the category's parent is not visible to the user, it is not visible as well.
2175 if (!$record->parent ||
isset($baselist[$record->parent
])) {
2176 context_helper
::preload_from_record($record);
2177 $context = context_coursecat
::instance($record->id
);
2178 if (!$record->visible
&& !has_capability('moodle/category:viewhiddencategories', $context)) {
2179 // No cap to view category, added to neither $baselist nor $thislist.
2182 $baselist[$record->id
] = array(
2183 'name' => format_string($record->name
, true, array('context' => $context)),
2184 'path' => $record->path
2186 if (!empty($requiredcapability) && !has_all_capabilities($requiredcapability, $context)) {
2187 // No required capability, added to $baselist but not to $thislist.
2190 $thislist[] = $record->id
;
2194 $coursecatcache->set($basecachekey, $baselist);
2195 if (!empty($requiredcapability)) {
2196 $coursecatcache->set($thiscachekey, join(',', $thislist));
2198 } else if ($thislist === false) {
2199 // We have $baselist cached but not $thislist. Simplier query is used to retrieve.
2200 $ctxselect = context_helper
::get_preload_record_columns_sql('ctx');
2201 $sql = "SELECT ctx.instanceid AS id, $ctxselect
2202 FROM {context} ctx WHERE ctx.contextlevel = :contextcoursecat";
2203 $contexts = $DB->get_records_sql($sql, array('contextcoursecat' => CONTEXT_COURSECAT
));
2204 $thislist = array();
2205 foreach (array_keys($baselist) as $id) {
2206 context_helper
::preload_from_record($contexts[$id]);
2207 if (has_all_capabilities($requiredcapability, context_coursecat
::instance($id))) {
2211 $coursecatcache->set($thiscachekey, join(',', $thislist));
2214 // Now build the array of strings to return, mind $separator and $excludeid.
2216 foreach ($thislist as $id) {
2217 $path = preg_split('|/|', $baselist[$id]['path'], -1, PREG_SPLIT_NO_EMPTY
);
2218 if (!$excludeid ||
!in_array($excludeid, $path)) {
2219 $namechunks = array();
2220 foreach ($path as $parentid) {
2221 $namechunks[] = $baselist[$parentid]['name'];
2223 $names[$id] = join($separator, $namechunks);
2230 * Prepares the object for caching. Works like the __sleep method.
2232 * implementing method from interface cacheable_object
2234 * @return array ready to be cached
2236 public function prepare_to_cache() {
2238 foreach (self
::$coursecatfields as $property => $cachedirectives) {
2239 if ($cachedirectives !== null) {
2240 list($shortname, $defaultvalue) = $cachedirectives;
2241 if ($this->$property !== $defaultvalue) {
2242 $a[$shortname] = $this->$property;
2246 $context = $this->get_context();
2247 $a['xi'] = $context->id
;
2248 $a['xp'] = $context->path
;
2253 * Takes the data provided by prepare_to_cache and reinitialises an instance of the associated from it.
2255 * implementing method from interface cacheable_object
2260 public static function wake_from_cache($a) {
2261 $record = new stdClass
;
2262 foreach (self
::$coursecatfields as $property => $cachedirectives) {
2263 if ($cachedirectives !== null) {
2264 list($shortname, $defaultvalue) = $cachedirectives;
2265 if (array_key_exists($shortname, $a)) {
2266 $record->$property = $a[$shortname];
2268 $record->$property = $defaultvalue;
2272 $record->ctxid
= $a['xi'];
2273 $record->ctxpath
= $a['xp'];
2274 $record->ctxdepth
= $record->depth +
1;
2275 $record->ctxlevel
= CONTEXT_COURSECAT
;
2276 $record->ctxinstance
= $record->id
;
2277 return new coursecat($record, true);
2281 * Returns true if the user is able to create a top level category.
2284 public static function can_create_top_level_category() {
2285 return has_capability('moodle/category:manage', context_system
::instance());
2289 * Returns the category context.
2290 * @return context_coursecat
2292 public function get_context() {
2293 if ($this->id
=== 0) {
2294 // This is the special top level category object.
2295 return context_system
::instance();
2297 return context_coursecat
::instance($this->id
);
2302 * Returns true if the user is able to manage this category.
2305 public function has_manage_capability() {
2306 if ($this->hasmanagecapability
=== null) {
2307 $this->hasmanagecapability
= has_capability('moodle/category:manage', $this->get_context());
2309 return $this->hasmanagecapability
;
2313 * Returns true if the user has the manage capability on the parent category.
2316 public function parent_has_manage_capability() {
2317 return has_capability('moodle/category:manage', get_category_or_system_context($this->parent
));
2321 * Returns true if the current user can create subcategories of this category.
2324 public function can_create_subcategory() {
2325 return $this->has_manage_capability();
2329 * Returns true if the user can resort this categories sub categories and courses.
2330 * Must have manage capability and be able to see all subcategories.
2333 public function can_resort_subcategories() {
2334 return $this->has_manage_capability() && !$this->get_not_visible_children_ids();
2338 * Returns true if the user can resort the courses within this category.
2339 * Must have manage capability and be able to see all courses.
2342 public function can_resort_courses() {
2343 return $this->has_manage_capability() && $this->coursecount
== $this->get_courses_count();
2347 * Returns true of the user can change the sortorder of this category (resort in the parent category)
2350 public function can_change_sortorder() {
2351 return $this->id
&& $this->get_parent_coursecat()->can_resort_subcategories();
2355 * Returns true if the current user can create a course within this category.
2358 public function can_create_course() {
2359 return has_capability('moodle/course:create', $this->get_context());
2363 * Returns true if the current user can edit this categories settings.
2366 public function can_edit() {
2367 return $this->has_manage_capability();
2371 * Returns true if the current user can review role assignments for this category.
2374 public function can_review_roles() {
2375 return has_capability('moodle/role:assign', $this->get_context());
2379 * Returns true if the current user can review permissions for this category.
2382 public function can_review_permissions() {
2383 return has_any_capability(array(
2384 'moodle/role:assign',
2385 'moodle/role:safeoverride',
2386 'moodle/role:override',
2387 'moodle/role:assign'
2388 ), $this->get_context());
2392 * Returns true if the current user can review cohorts for this category.
2395 public function can_review_cohorts() {
2396 return has_any_capability(array('moodle/cohort:view', 'moodle/cohort:manage'), $this->get_context());
2400 * Returns true if the current user can review filter settings for this category.
2403 public function can_review_filters() {
2404 return has_capability('moodle/filter:manage', $this->get_context()) &&
2405 count(filter_get_available_in_context($this->get_context()))>0;
2409 * Returns true if the current user is able to change the visbility of this category.
2412 public function can_change_visibility() {
2413 return $this->parent_has_manage_capability();
2417 * Returns true if the user can move courses out of this category.
2420 public function can_move_courses_out_of() {
2421 return $this->has_manage_capability();
2425 * Returns true if the user can move courses into this category.
2428 public function can_move_courses_into() {
2429 return $this->has_manage_capability();
2433 * Returns true if the user is able to restore a course into this category as a new course.
2436 public function can_restore_courses_into() {
2437 return has_capability('moodle/course:create', $this->get_context());
2441 * Resorts the sub categories of this category by the given field.
2443 * @param string $field
2444 * @param bool $cleanup If true cleanup will be done, if false you will need to do it manually later.
2445 * @return bool True on success.
2446 * @throws coding_exception
2448 public function resort_subcategories($field, $cleanup = true) {
2450 if ($field !== 'name' && $field !== 'idnumber') {
2451 throw new coding_exception('Invalid field requested');
2453 $children = $this->get_children();
2454 core_collator
::asort_objects_by_property($children, $field, core_collator
::SORT_NATURAL
);
2456 foreach ($children as $cat) {
2458 $DB->set_field('course_categories', 'sortorder', $i, array('id' => $cat->id
));
2459 $i +
= $cat->coursecount
;
2462 self
::resort_categories_cleanup();
2468 * Cleans things up after categories have been resorted.
2469 * @param bool $includecourses If set to true we know courses have been resorted as well.
2471 public static function resort_categories_cleanup($includecourses = false) {
2472 // This should not be needed but we do it just to be safe.
2473 fix_course_sortorder();
2474 cache_helper
::purge_by_event('changesincoursecat');
2475 if ($includecourses) {
2476 cache_helper
::purge_by_event('changesincourse');
2481 * Resort the courses within this category by the given field.
2483 * @param string $field One of fullname, shortname or idnumber
2484 * @param bool $cleanup
2485 * @return bool True for success.
2486 * @throws coding_exception
2488 public function resort_courses($field, $cleanup = true) {
2490 if ($field !== 'fullname' && $field !== 'shortname' && $field !== 'idnumber') {
2491 // This is ultra important as we use $field in an SQL statement below this.
2492 throw new coding_exception('Invalid field requested');
2494 $ctxfields = context_helper
::get_preload_record_columns_sql('ctx');
2495 $sql = "SELECT c.id, c.sortorder, c.{$field}, $ctxfields
2497 LEFT JOIN {context} ctx ON ctx.instanceid = c.id
2498 WHERE ctx.contextlevel = :ctxlevel AND
2499 c.category = :categoryid
2500 ORDER BY c.{$field}, c.sortorder";
2502 'ctxlevel' => CONTEXT_COURSE
,
2503 'categoryid' => $this->id
2505 $courses = $DB->get_records_sql($sql, $params);
2506 if (count($courses) > 0) {
2507 foreach ($courses as $courseid => $course) {
2508 context_helper
::preload_from_record($course);
2509 if ($field === 'idnumber') {
2510 $course->sortby
= $course->idnumber
;
2512 // It'll require formatting.
2514 'context' => context_course
::instance($course->id
)
2516 // We format the string first so that it appears as the user would see it.
2517 // This ensures the sorting makes sense to them. However it won't necessarily make
2518 // sense to everyone if things like multilang filters are enabled.
2519 // We then strip any tags as we don't want things such as image tags skewing the
2521 $course->sortby
= strip_tags(format_string($course->$field, true, $options));
2523 // We set it back here rather than using references as there is a bug with using
2524 // references in a foreach before passing as an arg by reference.
2525 $courses[$courseid] = $course;
2527 // Sort the courses.
2528 core_collator
::asort_objects_by_property($courses, 'sortby', core_collator
::SORT_NATURAL
);
2530 foreach ($courses as $course) {
2531 $DB->set_field('course', 'sortorder', $this->sortorder +
$i, array('id' => $course->id
));
2535 // This should not be needed but we do it just to be safe.
2536 fix_course_sortorder();
2537 cache_helper
::purge_by_event('changesincourse');
2544 * Changes the sort order of this categories parent shifting this category up or down one.
2546 * @global \moodle_database $DB
2547 * @param bool $up If set to true the category is shifted up one spot, else its moved down.
2548 * @return bool True on success, false otherwise.
2550 public function change_sortorder_by_one($up) {
2552 $params = array($this->sortorder
, $this->parent
);
2554 $select = 'sortorder < ? AND parent = ?';
2555 $sort = 'sortorder DESC';
2557 $select = 'sortorder > ? AND parent = ?';
2558 $sort = 'sortorder ASC';
2560 fix_course_sortorder();
2561 $swapcategory = $DB->get_records_select('course_categories', $select, $params, $sort, '*', 0, 1);
2562 $swapcategory = reset($swapcategory);
2563 if ($swapcategory) {
2564 $DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder
, array('id' => $this->id
));
2565 $DB->set_field('course_categories', 'sortorder', $this->sortorder
, array('id' => $swapcategory->id
));
2566 $this->sortorder
= $swapcategory->sortorder
;
2568 $event = \core\event\course_category_updated
::create(array(
2569 'objectid' => $this->id
,
2570 'context' => $this->get_context()
2572 $event->set_legacy_logdata(array(SITEID
, 'category', 'move', 'management.php?categoryid=' . $this->id
,
2576 // Finally reorder courses.
2577 fix_course_sortorder();
2578 cache_helper
::purge_by_event('changesincoursecat');
2585 * Returns the parent coursecat object for this category.
2589 public function get_parent_coursecat() {
2590 return self
::get($this->parent
);
2595 * Returns true if the user is able to request a new course be created.
2598 public function can_request_course() {
2600 if (empty($CFG->enablecourserequests
) ||
$this->id
!= $CFG->defaultrequestcategory
) {
2603 return !$this->can_create_course() && has_capability('moodle/course:request', $this->get_context());
2607 * Returns true if the user can approve course requests.
2610 public static function can_approve_course_requests() {
2612 if (empty($CFG->enablecourserequests
)) {
2615 $context = context_system
::instance();
2616 if (!has_capability('moodle/site:approvecourse', $context)) {
2619 if (!$DB->record_exists('course_request', array())) {
2627 * Class to store information about one course in a list of courses
2629 * Not all information may be retrieved when object is created but
2630 * it will be retrieved on demand when appropriate property or method is
2633 * Instances of this class are usually returned by functions
2634 * {@link coursecat::search_courses()}
2636 * {@link coursecat::get_courses()}
2638 * @property-read int $id
2639 * @property-read int $category Category ID
2640 * @property-read int $sortorder
2641 * @property-read string $fullname
2642 * @property-read string $shortname
2643 * @property-read string $idnumber
2644 * @property-read string $summary Course summary. Field is present if coursecat::get_courses()
2645 * was called with option 'summary'. Otherwise will be retrieved from DB on first request
2646 * @property-read int $summaryformat Summary format. Field is present if coursecat::get_courses()
2647 * was called with option 'summary'. Otherwise will be retrieved from DB on first request
2648 * @property-read string $format Course format. Retrieved from DB on first request
2649 * @property-read int $showgrades Retrieved from DB on first request
2650 * @property-read int $newsitems Retrieved from DB on first request
2651 * @property-read int $startdate
2652 * @property-read int $marker Retrieved from DB on first request
2653 * @property-read int $maxbytes Retrieved from DB on first request
2654 * @property-read int $legacyfiles Retrieved from DB on first request
2655 * @property-read int $showreports Retrieved from DB on first request
2656 * @property-read int $visible
2657 * @property-read int $visibleold Retrieved from DB on first request
2658 * @property-read int $groupmode Retrieved from DB on first request
2659 * @property-read int $groupmodeforce Retrieved from DB on first request
2660 * @property-read int $defaultgroupingid Retrieved from DB on first request
2661 * @property-read string $lang Retrieved from DB on first request
2662 * @property-read string $theme Retrieved from DB on first request
2663 * @property-read int $timecreated Retrieved from DB on first request
2664 * @property-read int $timemodified Retrieved from DB on first request
2665 * @property-read int $requested Retrieved from DB on first request
2666 * @property-read int $enablecompletion Retrieved from DB on first request
2667 * @property-read int $completionnotify Retrieved from DB on first request
2668 * @property-read int $cacherev
2671 * @subpackage course
2672 * @copyright 2013 Marina Glancy
2673 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2675 class course_in_list
implements IteratorAggregate
{
2677 /** @var stdClass record retrieved from DB, may have additional calculated property such as managers and hassummary */
2680 /** @var array array of course contacts - stores result of call to get_course_contacts() */
2681 protected $coursecontacts;
2683 /** @var bool true if the current user can access the course, false otherwise. */
2684 protected $canaccess = null;
2687 * Creates an instance of the class from record
2689 * @param stdClass $record except fields from course table it may contain
2690 * field hassummary indicating that summary field is not empty.
2691 * Also it is recommended to have context fields here ready for
2692 * context preloading
2694 public function __construct(stdClass
$record) {
2695 context_helper
::preload_from_record($record);
2696 $this->record
= new stdClass();
2697 foreach ($record as $key => $value) {
2698 $this->record
->$key = $value;
2703 * Indicates if the course has non-empty summary field
2707 public function has_summary() {
2708 if (isset($this->record
->hassummary
)) {
2709 return !empty($this->record
->hassummary
);
2711 if (!isset($this->record
->summary
)) {
2712 // We need to retrieve summary.
2713 $this->__get('summary');
2715 return !empty($this->record
->summary
);
2719 * Indicates if the course have course contacts to display
2723 public function has_course_contacts() {
2724 if (!isset($this->record
->managers
)) {
2725 $courses = array($this->id
=> &$this->record
);
2726 coursecat
::preload_course_contacts($courses);
2728 return !empty($this->record
->managers
);
2732 * Returns list of course contacts (usually teachers) to display in course link
2734 * Roles to display are set up in $CFG->coursecontact
2736 * The result is the list of users where user id is the key and the value
2737 * is an array with elements:
2738 * - 'user' - object containing basic user information
2739 * - 'role' - object containing basic role information (id, name, shortname, coursealias)
2740 * - 'rolename' => role_get_name($role, $context, ROLENAME_ALIAS)
2741 * - 'username' => fullname($user, $canviewfullnames)
2745 public function get_course_contacts() {
2747 if (empty($CFG->coursecontact
)) {
2748 // No roles are configured to be displayed as course contacts.
2751 if ($this->coursecontacts
=== null) {
2752 $this->coursecontacts
= array();
2753 $context = context_course
::instance($this->id
);
2755 if (!isset($this->record
->managers
)) {
2756 // Preload course contacts from DB.
2757 $courses = array($this->id
=> &$this->record
);
2758 coursecat
::preload_course_contacts($courses);
2761 // Build return array with full roles names (for this course context) and users names.
2762 $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
2763 foreach ($this->record
->managers
as $ruser) {
2764 if (isset($this->coursecontacts
[$ruser->id
])) {
2765 // Only display a user once with the highest sortorder role.
2768 $user = new stdClass();
2769 $user = username_load_fields_from_object($user, $ruser, null, array('id', 'username'));
2770 $role = new stdClass();
2771 $role->id
= $ruser->roleid
;
2772 $role->name
= $ruser->rolename
;
2773 $role->shortname
= $ruser->roleshortname
;
2774 $role->coursealias
= $ruser->rolecoursealias
;
2776 $this->coursecontacts
[$user->id
] = array(
2779 'rolename' => role_get_name($role, $context, ROLENAME_ALIAS
),
2780 'username' => fullname($user, $canviewfullnames)
2784 return $this->coursecontacts
;
2788 * Checks if course has any associated overview files
2792 public function has_course_overviewfiles() {
2794 if (empty($CFG->courseoverviewfileslimit
)) {
2797 $fs = get_file_storage();
2798 $context = context_course
::instance($this->id
);
2799 return !$fs->is_area_empty($context->id
, 'course', 'overviewfiles');
2803 * Returns all course overview files
2805 * @return array array of stored_file objects
2807 public function get_course_overviewfiles() {
2809 if (empty($CFG->courseoverviewfileslimit
)) {
2812 require_once($CFG->libdir
. '/filestorage/file_storage.php');
2813 require_once($CFG->dirroot
. '/course/lib.php');
2814 $fs = get_file_storage();
2815 $context = context_course
::instance($this->id
);
2816 $files = $fs->get_area_files($context->id
, 'course', 'overviewfiles', false, 'filename', false);
2817 if (count($files)) {
2818 $overviewfilesoptions = course_overviewfiles_options($this->id
);
2819 $acceptedtypes = $overviewfilesoptions['accepted_types'];
2820 if ($acceptedtypes !== '*') {
2821 // Filter only files with allowed extensions.
2822 require_once($CFG->libdir
. '/filelib.php');
2823 foreach ($files as $key => $file) {
2824 if (!file_extension_in_typegroup($file->get_filename(), $acceptedtypes)) {
2825 unset($files[$key]);
2829 if (count($files) > $CFG->courseoverviewfileslimit
) {
2830 // Return no more than $CFG->courseoverviewfileslimit files.
2831 $files = array_slice($files, 0, $CFG->courseoverviewfileslimit
, true);
2838 * Magic method to check if property is set
2840 * @param string $name
2843 public function __isset($name) {
2844 return isset($this->record
->$name);
2848 * Magic method to get a course property
2850 * Returns any field from table course (retrieves it from DB if it was not retrieved before)
2852 * @param string $name
2855 public function __get($name) {
2857 if (property_exists($this->record
, $name)) {
2858 return $this->record
->$name;
2859 } else if ($name === 'summary' ||
$name === 'summaryformat') {
2860 // Retrieve fields summary and summaryformat together because they are most likely to be used together.
2861 $record = $DB->get_record('course', array('id' => $this->record
->id
), 'summary, summaryformat', MUST_EXIST
);
2862 $this->record
->summary
= $record->summary
;
2863 $this->record
->summaryformat
= $record->summaryformat
;
2864 return $this->record
->$name;
2865 } else if (array_key_exists($name, $DB->get_columns('course'))) {
2866 // Another field from table 'course' that was not retrieved.
2867 $this->record
->$name = $DB->get_field('course', $name, array('id' => $this->record
->id
), MUST_EXIST
);
2868 return $this->record
->$name;
2870 debugging('Invalid course property accessed! '.$name);
2875 * All properties are read only, sorry.
2877 * @param string $name
2879 public function __unset($name) {
2880 debugging('Can not unset '.get_class($this).' instance properties!');
2884 * Magic setter method, we do not want anybody to modify properties from the outside
2886 * @param string $name
2887 * @param mixed $value
2889 public function __set($name, $value) {
2890 debugging('Can not change '.get_class($this).' instance properties!');
2894 * Create an iterator because magic vars can't be seen by 'foreach'.
2895 * Exclude context fields
2897 * Implementing method from interface IteratorAggregate
2899 * @return ArrayIterator
2901 public function getIterator() {
2902 $ret = array('id' => $this->record
->id
);
2903 foreach ($this->record
as $property => $value) {
2904 $ret[$property] = $value;
2906 return new ArrayIterator($ret);
2910 * Returns the name of this course as it should be displayed within a list.
2913 public function get_formatted_name() {
2914 return format_string(get_course_display_name_for_list($this), true, $this->get_context());
2918 * Returns the formatted fullname for this course.
2921 public function get_formatted_fullname() {
2922 return format_string($this->__get('fullname'), true, $this->get_context());
2926 * Returns the formatted shortname for this course.
2929 public function get_formatted_shortname() {
2930 return format_string($this->__get('shortname'), true, $this->get_context());
2934 * Returns true if the current user can access this course.
2937 public function can_access() {
2938 if ($this->canaccess
=== null) {
2939 $this->canaccess
= can_access_course($this->record
);
2941 return $this->canaccess
;
2945 * Returns true if the user can edit this courses settings.
2947 * Note: this function does not check that the current user can access the course.
2948 * To do that please call require_login with the course, or if not possible call {@see course_in_list::can_access()}
2952 public function can_edit() {
2953 return has_capability('moodle/course:update', $this->get_context());
2957 * Returns true if the user can change the visibility of this course.
2959 * Note: this function does not check that the current user can access the course.
2960 * To do that please call require_login with the course, or if not possible call {@see course_in_list::can_access()}
2964 public function can_change_visibility() {
2965 // You must be able to both hide a course and view the hidden course.
2966 return has_all_capabilities(array('moodle/course:visibility', 'moodle/course:viewhiddencourses'), $this->get_context());
2970 * Returns the context for this course.
2971 * @return context_course
2973 public function get_context() {
2974 return context_course
::instance($this->__get('id'));
2978 * Returns true if this course is visible to the current user.
2981 public function is_uservisible() {
2982 return $this->visible ||
has_capability('moodle/course:viewhiddencourses', $this->get_context());
2986 * Returns true if the current user can review enrolments for this course.
2988 * Note: this function does not check that the current user can access the course.
2989 * To do that please call require_login with the course, or if not possible call {@see course_in_list::can_access()}
2993 public function can_review_enrolments() {
2994 return has_capability('moodle/course:enrolreview', $this->get_context());
2998 * Returns true if the current user can delete this course.
3000 * Note: this function does not check that the current user can access the course.
3001 * To do that please call require_login with the course, or if not possible call {@see course_in_list::can_access()}
3005 public function can_delete() {
3006 return can_delete_course($this->id
);
3010 * Returns true if the current user can backup this course.
3012 * Note: this function does not check that the current user can access the course.
3013 * To do that please call require_login with the course, or if not possible call {@see course_in_list::can_access()}
3017 public function can_backup() {
3018 return has_capability('moodle/backup:backupcourse', $this->get_context());
3022 * Returns true if the current user can restore this course.
3024 * Note: this function does not check that the current user can access the course.
3025 * To do that please call require_login with the course, or if not possible call {@see course_in_list::can_access()}
3029 public function can_restore() {
3030 return has_capability('moodle/restore:restorecourse', $this->get_context());
3035 * An array of records that is sortable by many fields.
3037 * For more info on the ArrayObject class have a look at php.net.
3040 * @subpackage course
3041 * @copyright 2013 Sam Hemelryk
3042 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3044 class coursecat_sortable_records
extends ArrayObject
{
3047 * An array of sortable fields.
3048 * Gets set temporarily when sort is called.
3051 protected $sortfields = array();
3054 * Sorts this array using the given fields.
3056 * @param array $records
3057 * @param array $fields
3060 public static function sort(array $records, array $fields) {
3061 $records = new coursecat_sortable_records($records);
3062 $records->sortfields
= $fields;
3063 $records->uasort(array($records, 'sort_by_many_fields'));
3064 return $records->getArrayCopy();
3068 * Sorts the two records based upon many fields.
3070 * This method should not be called itself, please call $sort instead.
3071 * It has been marked as access private as such.
3074 * @param stdClass $a
3075 * @param stdClass $b
3078 public function sort_by_many_fields($a, $b) {
3079 foreach ($this->sortfields
as $field => $mult) {
3081 if (is_null($a->$field) && !is_null($b->$field)) {
3084 if (is_null($b->$field) && !is_null($a->$field)) {
3088 if (is_string($a->$field) ||
is_string($b->$field)) {
3090 if ($cmp = strcoll($a->$field, $b->$field)) {
3091 return $mult * $cmp;
3095 if ($a->$field > $b->$field) {
3098 if ($a->$field < $b->$field) {