Merge branch 'MDL-81457-main' of https://github.com/andrewnicols/moodle
[moodle.git] / cohort / lib.php
blob3d7ef7195acf4266f3510cabf6426c8a22cab710
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Cohort related management functions, this file needs to be included manually.
20 * @package core_cohort
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 define('COHORT_ALL', 0);
28 define('COHORT_COUNT_MEMBERS', 1);
29 define('COHORT_COUNT_ENROLLED_MEMBERS', 3);
30 define('COHORT_WITH_MEMBERS_ONLY', 5);
31 define('COHORT_WITH_ENROLLED_MEMBERS_ONLY', 17);
32 define('COHORT_WITH_NOTENROLLED_MEMBERS_ONLY', 23);
34 /**
35 * Add new cohort.
37 * @param stdClass $cohort
38 * @return int new cohort id
40 function cohort_add_cohort($cohort) {
41 global $DB, $CFG;
43 if (!isset($cohort->name)) {
44 throw new coding_exception('Missing cohort name in cohort_add_cohort().');
46 if (!isset($cohort->idnumber)) {
47 $cohort->idnumber = NULL;
49 if (!isset($cohort->description)) {
50 $cohort->description = '';
52 if (!isset($cohort->descriptionformat)) {
53 $cohort->descriptionformat = FORMAT_HTML;
55 if (!isset($cohort->visible)) {
56 $cohort->visible = 1;
58 if (empty($cohort->component)) {
59 $cohort->component = '';
61 if (empty($CFG->allowcohortthemes) && isset($cohort->theme)) {
62 unset($cohort->theme);
64 if (empty($cohort->theme) || empty($CFG->allowcohortthemes)) {
65 $cohort->theme = '';
67 if (!isset($cohort->timecreated)) {
68 $cohort->timecreated = time();
70 if (!isset($cohort->timemodified)) {
71 $cohort->timemodified = $cohort->timecreated;
74 $cohort->id = $DB->insert_record('cohort', $cohort);
76 $handler = core_cohort\customfield\cohort_handler::create();
77 $handler->instance_form_save($cohort, true);
79 $event = \core\event\cohort_created::create(array(
80 'context' => context::instance_by_id($cohort->contextid),
81 'objectid' => $cohort->id,
82 ));
83 $event->add_record_snapshot('cohort', $cohort);
84 $event->trigger();
86 return $cohort->id;
89 /**
90 * Update existing cohort.
91 * @param stdClass $cohort
92 * @return void
94 function cohort_update_cohort($cohort) {
95 global $DB, $CFG;
96 if (property_exists($cohort, 'component') and empty($cohort->component)) {
97 // prevent NULLs
98 $cohort->component = '';
100 // Only unset the cohort theme if allowcohortthemes is enabled to prevent the value from being overwritten.
101 if (empty($CFG->allowcohortthemes) && isset($cohort->theme)) {
102 unset($cohort->theme);
105 // Delete theme usage cache if the theme has been changed.
106 if (isset($cohort->theme)) {
107 $oldcohort = $DB->get_record('cohort', ['id' => $cohort->id]);
108 if ($cohort->theme != $oldcohort->theme) {
109 theme_delete_used_in_context_cache($cohort->theme, $oldcohort->theme);
113 $cohort->timemodified = time();
115 // Update custom fields if there are any of them in the form.
116 $handler = core_cohort\customfield\cohort_handler::create();
117 $handler->instance_form_save($cohort);
119 $DB->update_record('cohort', $cohort);
121 $event = \core\event\cohort_updated::create(array(
122 'context' => context::instance_by_id($cohort->contextid),
123 'objectid' => $cohort->id,
125 $event->trigger();
129 * Delete cohort.
130 * @param stdClass $cohort
131 * @return void
133 function cohort_delete_cohort($cohort) {
134 global $DB;
136 if ($cohort->component) {
137 // TODO: add component delete callback
140 $handler = core_cohort\customfield\cohort_handler::create();
141 $handler->delete_instance($cohort->id);
143 $DB->delete_records('cohort_members', array('cohortid'=>$cohort->id));
144 $DB->delete_records('cohort', array('id'=>$cohort->id));
146 // Notify the competency subsystem.
147 \core_competency\api::hook_cohort_deleted($cohort);
149 $event = \core\event\cohort_deleted::create(array(
150 'context' => context::instance_by_id($cohort->contextid),
151 'objectid' => $cohort->id,
153 $event->add_record_snapshot('cohort', $cohort);
154 $event->trigger();
158 * Somehow deal with cohorts when deleting course category,
159 * we can not just delete them because they might be used in enrol
160 * plugins or referenced in external systems.
161 * @param stdClass|core_course_category $category
162 * @return void
164 function cohort_delete_category($category) {
165 global $DB;
166 // TODO: make sure that cohorts are really, really not used anywhere and delete, for now just move to parent or system context
168 $oldcontext = context_coursecat::instance($category->id);
170 if ($category->parent and $parent = $DB->get_record('course_categories', array('id'=>$category->parent))) {
171 $parentcontext = context_coursecat::instance($parent->id);
172 $sql = "UPDATE {cohort} SET contextid = :newcontext WHERE contextid = :oldcontext";
173 $params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$parentcontext->id);
174 } else {
175 $syscontext = context_system::instance();
176 $sql = "UPDATE {cohort} SET contextid = :newcontext WHERE contextid = :oldcontext";
177 $params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$syscontext->id);
180 $DB->execute($sql, $params);
184 * Add cohort member
185 * @param int $cohortid
186 * @param int $userid
187 * @return void
189 function cohort_add_member($cohortid, $userid) {
190 global $DB;
191 if ($DB->record_exists('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid))) {
192 // No duplicates!
193 return;
195 $record = new stdClass();
196 $record->cohortid = $cohortid;
197 $record->userid = $userid;
198 $record->timeadded = time();
199 $DB->insert_record('cohort_members', $record);
201 $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST);
203 $event = \core\event\cohort_member_added::create(array(
204 'context' => context::instance_by_id($cohort->contextid),
205 'objectid' => $cohortid,
206 'relateduserid' => $userid,
208 $event->add_record_snapshot('cohort', $cohort);
209 $event->trigger();
213 * Remove cohort member
214 * @param int $cohortid
215 * @param int $userid
216 * @return void
218 function cohort_remove_member($cohortid, $userid) {
219 global $DB;
220 $DB->delete_records('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid));
222 $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST);
224 $event = \core\event\cohort_member_removed::create(array(
225 'context' => context::instance_by_id($cohort->contextid),
226 'objectid' => $cohortid,
227 'relateduserid' => $userid,
229 $event->add_record_snapshot('cohort', $cohort);
230 $event->trigger();
234 * Is this user a cohort member?
235 * @param int $cohortid
236 * @param int $userid
237 * @return bool
239 function cohort_is_member($cohortid, $userid) {
240 global $DB;
242 return $DB->record_exists('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid));
246 * Returns the list of cohorts visible to the current user in the given course.
248 * The following fields are returned in each record: id, name, contextid, idnumber, visible
249 * Fields memberscnt and enrolledcnt will be also returned if requested
251 * @param context $currentcontext
252 * @param int $withmembers one of the COHORT_XXX constants that allows to return non empty cohorts only
253 * or cohorts with enroled/not enroled users, or just return members count
254 * @param int $offset
255 * @param int $limit
256 * @param string $search
257 * @param bool $withcustomfields if set to yes, then cohort custom fields will be included in the results.
258 * @return array
260 function cohort_get_available_cohorts($currentcontext, $withmembers = 0, $offset = 0, $limit = 25,
261 $search = '', $withcustomfields = false) {
262 global $DB;
264 $params = array();
266 // Build context subquery. Find the list of parent context where user is able to see any or visible-only cohorts.
267 // Since this method is normally called for the current course all parent contexts are already preloaded.
268 $contextsany = array_filter($currentcontext->get_parent_context_ids(),
269 function($a) {
270 return has_capability("moodle/cohort:view", context::instance_by_id($a));
272 $contextsvisible = array_diff($currentcontext->get_parent_context_ids(), $contextsany);
273 if (empty($contextsany) && empty($contextsvisible)) {
274 // User does not have any permissions to view cohorts.
275 return array();
277 $subqueries = array();
278 if (!empty($contextsany)) {
279 list($parentsql, $params1) = $DB->get_in_or_equal($contextsany, SQL_PARAMS_NAMED, 'ctxa');
280 $subqueries[] = 'c.contextid ' . $parentsql;
281 $params = array_merge($params, $params1);
283 if (!empty($contextsvisible)) {
284 list($parentsql, $params1) = $DB->get_in_or_equal($contextsvisible, SQL_PARAMS_NAMED, 'ctxv');
285 $subqueries[] = '(c.visible = 1 AND c.contextid ' . $parentsql. ')';
286 $params = array_merge($params, $params1);
288 $wheresql = '(' . implode(' OR ', $subqueries) . ')';
290 // Build the rest of the query.
291 $fromsql = "";
292 $fieldssql = 'c.id, c.name, c.contextid, c.idnumber, c.visible';
293 $groupbysql = '';
294 $havingsql = '';
295 if ($withmembers) {
296 $fieldssql .= ', s.memberscnt';
297 $subfields = "c.id, COUNT(DISTINCT cm.userid) AS memberscnt";
298 $groupbysql = " GROUP BY c.id";
299 $fromsql = " LEFT JOIN {cohort_members} cm ON cm.cohortid = c.id ";
300 if (in_array($withmembers,
301 array(COHORT_COUNT_ENROLLED_MEMBERS, COHORT_WITH_ENROLLED_MEMBERS_ONLY, COHORT_WITH_NOTENROLLED_MEMBERS_ONLY))) {
302 list($esql, $params2) = get_enrolled_sql($currentcontext);
303 $fromsql .= " LEFT JOIN ($esql) u ON u.id = cm.userid ";
304 $params = array_merge($params2, $params);
305 $fieldssql .= ', s.enrolledcnt';
306 $subfields .= ', COUNT(DISTINCT u.id) AS enrolledcnt';
308 if ($withmembers == COHORT_WITH_MEMBERS_ONLY) {
309 $havingsql = " HAVING COUNT(DISTINCT cm.userid) > 0";
310 } else if ($withmembers == COHORT_WITH_ENROLLED_MEMBERS_ONLY) {
311 $havingsql = " HAVING COUNT(DISTINCT u.id) > 0";
312 } else if ($withmembers == COHORT_WITH_NOTENROLLED_MEMBERS_ONLY) {
313 $havingsql = " HAVING COUNT(DISTINCT cm.userid) > COUNT(DISTINCT u.id)";
316 if ($search) {
317 list($searchsql, $searchparams) = cohort_get_search_query($search);
318 $wheresql .= ' AND ' . $searchsql;
319 $params = array_merge($params, $searchparams);
322 if ($withmembers) {
323 $sql = "SELECT " . str_replace('c.', 'cohort.', $fieldssql) . "
324 FROM {cohort} cohort
325 JOIN (SELECT $subfields
326 FROM {cohort} c $fromsql
327 WHERE $wheresql $groupbysql $havingsql
328 ) s ON cohort.id = s.id
329 ORDER BY cohort.name, cohort.idnumber";
330 } else {
331 $sql = "SELECT $fieldssql
332 FROM {cohort} c $fromsql
333 WHERE $wheresql
334 ORDER BY c.name, c.idnumber";
337 $cohorts = $DB->get_records_sql($sql, $params, $offset, $limit);
339 if ($withcustomfields) {
340 $cohortids = array_keys($cohorts);
341 $customfieldsdata = cohort_get_custom_fields_data($cohortids);
343 foreach ($cohorts as $cohort) {
344 $cohort->customfields = !empty($customfieldsdata[$cohort->id]) ? $customfieldsdata[$cohort->id] : [];
348 return $cohorts;
352 * Check if cohort exists and user is allowed to access it from the given context.
354 * @param stdClass|int $cohortorid cohort object or id
355 * @param context $currentcontext current context (course) where visibility is checked
356 * @return boolean
358 function cohort_can_view_cohort($cohortorid, $currentcontext) {
359 global $DB;
360 if (is_numeric($cohortorid)) {
361 $cohort = $DB->get_record('cohort', array('id' => $cohortorid), 'id, contextid, visible');
362 } else {
363 $cohort = $cohortorid;
366 if ($cohort && in_array($cohort->contextid, $currentcontext->get_parent_context_ids())) {
367 if ($cohort->visible) {
368 return true;
370 $cohortcontext = context::instance_by_id($cohort->contextid);
371 if (has_capability('moodle/cohort:view', $cohortcontext)) {
372 return true;
375 return false;
379 * Get a cohort by id. Also does a visibility check and returns false if the user cannot see this cohort.
381 * @param stdClass|int $cohortorid cohort object or id
382 * @param context $currentcontext current context (course) where visibility is checked
383 * @param bool $withcustomfields if set to yes, then cohort custom fields will be included in the results.
384 * @return stdClass|boolean
386 function cohort_get_cohort($cohortorid, $currentcontext, $withcustomfields = false) {
387 global $DB;
388 if (is_numeric($cohortorid)) {
389 $cohort = $DB->get_record('cohort', array('id' => $cohortorid), 'id, contextid, visible');
390 } else {
391 $cohort = $cohortorid;
394 if ($cohort && in_array($cohort->contextid, $currentcontext->get_parent_context_ids())) {
395 if (!$cohort->visible) {
396 $cohortcontext = context::instance_by_id($cohort->contextid);
397 if (!has_capability('moodle/cohort:view', $cohortcontext)) {
398 return false;
401 } else {
402 return false;
405 if ($cohort && $withcustomfields) {
406 $customfieldsdata = cohort_get_custom_fields_data([$cohort->id]);
407 $cohort->customfields = !empty($customfieldsdata[$cohort->id]) ? $customfieldsdata[$cohort->id] : [];
410 return $cohort;
414 * Produces a part of SQL query to filter cohorts by the search string
416 * Called from {@link cohort_get_cohorts()}, {@link cohort_get_all_cohorts()} and {@link cohort_get_available_cohorts()}
418 * @access private
420 * @param string $search search string
421 * @param string $tablealias alias of cohort table in the SQL query (highly recommended if other tables are used in query)
422 * @return array of two elements - SQL condition and array of named parameters
424 function cohort_get_search_query($search, $tablealias = '') {
425 global $DB;
426 $params = array();
427 if (empty($search)) {
428 // This function should not be called if there is no search string, just in case return dummy query.
429 return array('1=1', $params);
431 if ($tablealias && substr($tablealias, -1) !== '.') {
432 $tablealias .= '.';
434 $searchparam = '%' . $DB->sql_like_escape($search) . '%';
435 $conditions = array();
436 $fields = array('name', 'idnumber', 'description');
437 $cnt = 0;
438 foreach ($fields as $field) {
439 $conditions[] = $DB->sql_like($tablealias . $field, ':csearch' . $cnt, false);
440 $params['csearch' . $cnt] = $searchparam;
441 $cnt++;
443 $sql = '(' . implode(' OR ', $conditions) . ')';
444 return array($sql, $params);
448 * Get all the cohorts defined in given context.
450 * The function does not check user capability to view/manage cohorts in the given context
451 * assuming that it has been already verified.
453 * @param int $contextid
454 * @param int $page number of the current page
455 * @param int $perpage items per page
456 * @param string $search search string
457 * @param bool $withcustomfields if set to yes, then cohort custom fields will be included in the results.
458 * @return array Array(totalcohorts => int, cohorts => array, allcohorts => int)
460 function cohort_get_cohorts($contextid, $page = 0, $perpage = 25, $search = '', $withcustomfields = false) {
461 global $DB;
463 $fields = "SELECT *";
464 $countfields = "SELECT COUNT(1)";
465 $sql = " FROM {cohort}
466 WHERE contextid = :contextid";
467 $params = array('contextid' => $contextid);
468 $order = " ORDER BY name ASC, idnumber ASC";
470 if (!empty($search)) {
471 list($searchcondition, $searchparams) = cohort_get_search_query($search);
472 $sql .= ' AND ' . $searchcondition;
473 $params = array_merge($params, $searchparams);
476 $totalcohorts = $allcohorts = $DB->count_records('cohort', array('contextid' => $contextid));
477 if (!empty($search)) {
478 $totalcohorts = $DB->count_records_sql($countfields . $sql, $params);
480 $cohorts = $DB->get_records_sql($fields . $sql . $order, $params, $page*$perpage, $perpage);
482 if ($withcustomfields) {
483 $cohortids = array_keys($cohorts);
484 $customfieldsdata = cohort_get_custom_fields_data($cohortids);
486 foreach ($cohorts as $cohort) {
487 $cohort->customfields = !empty($customfieldsdata[$cohort->id]) ? $customfieldsdata[$cohort->id] : [];
491 return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts, 'allcohorts' => $allcohorts);
495 * Get all the cohorts defined anywhere in system.
497 * The function assumes that user capability to view/manage cohorts on system level
498 * has already been verified. This function only checks if such capabilities have been
499 * revoked in child (categories) contexts.
501 * @param int $page number of the current page
502 * @param int $perpage items per page
503 * @param string $search search string
504 * @param bool $withcustomfields if set to yes, then cohort custom fields will be included in the results.
505 * @return array Array(totalcohorts => int, cohorts => array, allcohorts => int)
507 function cohort_get_all_cohorts($page = 0, $perpage = 25, $search = '', $withcustomfields = false) {
508 global $DB;
510 $fields = "SELECT c.*, ".context_helper::get_preload_record_columns_sql('ctx');
511 $countfields = "SELECT COUNT(*)";
512 $sql = " FROM {cohort} c
513 JOIN {context} ctx ON ctx.id = c.contextid ";
514 $params = array();
515 $wheresql = '';
517 if ($excludedcontexts = cohort_get_invisible_contexts()) {
518 list($excludedsql, $excludedparams) = $DB->get_in_or_equal($excludedcontexts, SQL_PARAMS_NAMED, 'excl', false);
519 $wheresql = ' WHERE c.contextid '.$excludedsql;
520 $params = array_merge($params, $excludedparams);
523 $totalcohorts = $allcohorts = $DB->count_records_sql($countfields . $sql . $wheresql, $params);
525 if (!empty($search)) {
526 list($searchcondition, $searchparams) = cohort_get_search_query($search, 'c');
527 $wheresql .= ($wheresql ? ' AND ' : ' WHERE ') . $searchcondition;
528 $params = array_merge($params, $searchparams);
529 $totalcohorts = $DB->count_records_sql($countfields . $sql . $wheresql, $params);
532 $order = " ORDER BY c.name ASC, c.idnumber ASC";
533 $cohorts = $DB->get_records_sql($fields . $sql . $wheresql . $order, $params, $page*$perpage, $perpage);
535 if ($withcustomfields) {
536 $cohortids = array_keys($cohorts);
537 $customfieldsdata = cohort_get_custom_fields_data($cohortids);
540 foreach ($cohorts as $cohort) {
541 // Preload used contexts, they will be used to check view/manage/assign capabilities and display categories names.
542 context_helper::preload_from_record($cohort);
543 if ($withcustomfields) {
544 $cohort->customfields = !empty($customfieldsdata[$cohort->id]) ? $customfieldsdata[$cohort->id] : [];
548 return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts, 'allcohorts' => $allcohorts);
552 * Get all the cohorts where the given user is member of.
554 * @param int $userid
555 * @param bool $withcustomfields if set to yes, then cohort custom fields will be included in the results.
556 * @return array Array
558 function cohort_get_user_cohorts($userid, $withcustomfields = false) {
559 global $DB;
561 $sql = 'SELECT c.*
562 FROM {cohort} c
563 JOIN {cohort_members} cm ON c.id = cm.cohortid
564 WHERE cm.userid = ? AND c.visible = 1';
565 $cohorts = $DB->get_records_sql($sql, array($userid));
567 if ($withcustomfields) {
568 $cohortids = array_keys($cohorts);
569 $customfieldsdata = cohort_get_custom_fields_data($cohortids);
571 foreach ($cohorts as $cohort) {
572 $cohort->customfields = !empty($customfieldsdata[$cohort->id]) ? $customfieldsdata[$cohort->id] : [];
576 return $cohorts;
580 * Get the user cohort theme.
582 * If the user is member of one cohort, will return this cohort theme (if defined).
583 * If the user is member of 2 or more cohorts, will return the theme if all them have the same
584 * theme (null themes are ignored).
586 * @param int $userid
587 * @return string|null
589 function cohort_get_user_cohort_theme($userid) {
590 $cohorts = cohort_get_user_cohorts($userid);
591 $theme = null;
592 foreach ($cohorts as $cohort) {
593 if (!empty($cohort->theme)) {
594 if (null === $theme) {
595 $theme = $cohort->theme;
596 } else if ($theme != $cohort->theme) {
597 return null;
601 return $theme;
605 * Returns list of contexts where cohorts are present but current user does not have capability to view/manage them.
607 * This function is called from {@link cohort_get_all_cohorts()} to ensure correct pagination in rare cases when user
608 * is revoked capability in child contexts. It assumes that user's capability to view/manage cohorts on system
609 * level has already been verified.
611 * @access private
613 * @return array array of context ids
615 function cohort_get_invisible_contexts() {
616 global $DB;
617 if (is_siteadmin()) {
618 // Shortcut, admin can do anything and can not be prohibited from any context.
619 return array();
621 $records = $DB->get_recordset_sql("SELECT DISTINCT ctx.id, ".context_helper::get_preload_record_columns_sql('ctx')." ".
622 "FROM {context} ctx JOIN {cohort} c ON ctx.id = c.contextid ");
623 $excludedcontexts = array();
624 foreach ($records as $ctx) {
625 context_helper::preload_from_record($ctx);
626 if (context::instance_by_id($ctx->id) == context_system::instance()) {
627 continue; // System context cohorts should be available and permissions already checked.
629 if (!has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), context::instance_by_id($ctx->id))) {
630 $excludedcontexts[] = $ctx->id;
633 $records->close();
634 return $excludedcontexts;
638 * Returns navigation controls (tabtree) to be displayed on cohort management pages
640 * @param context $context system or category context where cohorts controls are about to be displayed
641 * @param moodle_url $currenturl
642 * @return null|renderable
644 function cohort_edit_controls(context $context, moodle_url $currenturl) {
645 $tabs = array();
646 $currenttab = 'view';
647 $viewurl = new moodle_url('/cohort/index.php', array('contextid' => $context->id));
648 if (($searchquery = $currenturl->get_param('search'))) {
649 $viewurl->param('search', $searchquery);
651 if ($context->contextlevel == CONTEXT_SYSTEM) {
652 $tabs[] = new tabobject('view', new moodle_url($viewurl, array('showall' => 0)), get_string('systemcohorts', 'cohort'));
653 $tabs[] = new tabobject('viewall', new moodle_url($viewurl, array('showall' => 1)), get_string('allcohorts', 'cohort'));
654 if ($currenturl->get_param('showall')) {
655 $currenttab = 'viewall';
657 } else {
658 $tabs[] = new tabobject('view', $viewurl, get_string('cohorts', 'cohort'));
660 if (has_capability('moodle/cohort:manage', $context)) {
661 $addurl = new moodle_url('/cohort/edit.php', array('contextid' => $context->id));
662 $tabs[] = new tabobject('addcohort', $addurl, get_string('addcohort', 'cohort'));
663 if ($currenturl->get_path() === $addurl->get_path() && !$currenturl->param('id')) {
664 $currenttab = 'addcohort';
667 $uploadurl = new moodle_url('/cohort/upload.php', array('contextid' => $context->id));
668 $tabs[] = new tabobject('uploadcohorts', $uploadurl, get_string('uploadcohorts', 'cohort'));
669 if ($currenturl->get_path() === $uploadurl->get_path()) {
670 $currenttab = 'uploadcohorts';
673 if (count($tabs) > 1) {
674 return new tabtree($tabs, $currenttab);
676 return null;
680 * Implements callback inplace_editable() allowing to edit values in-place
682 * @param string $itemtype
683 * @param int $itemid
684 * @param mixed $newvalue
685 * @return \core\output\inplace_editable
687 function core_cohort_inplace_editable($itemtype, $itemid, $newvalue) {
688 if ($itemtype === 'cohortname') {
689 return \core_cohort\output\cohortname::update($itemid, $newvalue);
690 } else if ($itemtype === 'cohortidnumber') {
691 return \core_cohort\output\cohortidnumber::update($itemid, $newvalue);
696 * Returns a list of valid themes which can be displayed in a selector.
698 * @return array as (string)themename => (string)get_string_theme
700 function cohort_get_list_of_themes() {
701 $themes = array();
702 $allthemes = get_list_of_themes();
703 foreach ($allthemes as $key => $theme) {
704 if (empty($theme->hidefromselector)) {
705 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
708 return $themes;
712 * Returns custom fields data for provided cohorts.
714 * @param array $cohortids a list of cohort IDs to provide data for.
715 * @return \core_customfield\data_controller[]
717 function cohort_get_custom_fields_data(array $cohortids): array {
718 $result = [];
720 if (!empty($cohortids)) {
721 $handler = core_cohort\customfield\cohort_handler::create();
722 $result = $handler->get_instances_data($cohortids, true);
725 return $result;