MDL-78962 core/loadingicon: remove jQuery requirement in the API
[moodle.git] / cohort / lib.php
blob48c7c87f645408dd6f3c49f9b36990fb45dc4cc4
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);
104 $cohort->timemodified = time();
106 // Update custom fields if there are any of them in the form.
107 $handler = core_cohort\customfield\cohort_handler::create();
108 $handler->instance_form_save($cohort);
110 $DB->update_record('cohort', $cohort);
112 $event = \core\event\cohort_updated::create(array(
113 'context' => context::instance_by_id($cohort->contextid),
114 'objectid' => $cohort->id,
116 $event->trigger();
120 * Delete cohort.
121 * @param stdClass $cohort
122 * @return void
124 function cohort_delete_cohort($cohort) {
125 global $DB;
127 if ($cohort->component) {
128 // TODO: add component delete callback
131 $handler = core_cohort\customfield\cohort_handler::create();
132 $handler->delete_instance($cohort->id);
134 $DB->delete_records('cohort_members', array('cohortid'=>$cohort->id));
135 $DB->delete_records('cohort', array('id'=>$cohort->id));
137 // Notify the competency subsystem.
138 \core_competency\api::hook_cohort_deleted($cohort);
140 $event = \core\event\cohort_deleted::create(array(
141 'context' => context::instance_by_id($cohort->contextid),
142 'objectid' => $cohort->id,
144 $event->add_record_snapshot('cohort', $cohort);
145 $event->trigger();
149 * Somehow deal with cohorts when deleting course category,
150 * we can not just delete them because they might be used in enrol
151 * plugins or referenced in external systems.
152 * @param stdClass|core_course_category $category
153 * @return void
155 function cohort_delete_category($category) {
156 global $DB;
157 // TODO: make sure that cohorts are really, really not used anywhere and delete, for now just move to parent or system context
159 $oldcontext = context_coursecat::instance($category->id);
161 if ($category->parent and $parent = $DB->get_record('course_categories', array('id'=>$category->parent))) {
162 $parentcontext = context_coursecat::instance($parent->id);
163 $sql = "UPDATE {cohort} SET contextid = :newcontext WHERE contextid = :oldcontext";
164 $params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$parentcontext->id);
165 } else {
166 $syscontext = context_system::instance();
167 $sql = "UPDATE {cohort} SET contextid = :newcontext WHERE contextid = :oldcontext";
168 $params = array('oldcontext'=>$oldcontext->id, 'newcontext'=>$syscontext->id);
171 $DB->execute($sql, $params);
175 * Add cohort member
176 * @param int $cohortid
177 * @param int $userid
178 * @return void
180 function cohort_add_member($cohortid, $userid) {
181 global $DB;
182 if ($DB->record_exists('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid))) {
183 // No duplicates!
184 return;
186 $record = new stdClass();
187 $record->cohortid = $cohortid;
188 $record->userid = $userid;
189 $record->timeadded = time();
190 $DB->insert_record('cohort_members', $record);
192 $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST);
194 $event = \core\event\cohort_member_added::create(array(
195 'context' => context::instance_by_id($cohort->contextid),
196 'objectid' => $cohortid,
197 'relateduserid' => $userid,
199 $event->add_record_snapshot('cohort', $cohort);
200 $event->trigger();
204 * Remove cohort member
205 * @param int $cohortid
206 * @param int $userid
207 * @return void
209 function cohort_remove_member($cohortid, $userid) {
210 global $DB;
211 $DB->delete_records('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid));
213 $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST);
215 $event = \core\event\cohort_member_removed::create(array(
216 'context' => context::instance_by_id($cohort->contextid),
217 'objectid' => $cohortid,
218 'relateduserid' => $userid,
220 $event->add_record_snapshot('cohort', $cohort);
221 $event->trigger();
225 * Is this user a cohort member?
226 * @param int $cohortid
227 * @param int $userid
228 * @return bool
230 function cohort_is_member($cohortid, $userid) {
231 global $DB;
233 return $DB->record_exists('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid));
237 * Returns the list of cohorts visible to the current user in the given course.
239 * The following fields are returned in each record: id, name, contextid, idnumber, visible
240 * Fields memberscnt and enrolledcnt will be also returned if requested
242 * @param context $currentcontext
243 * @param int $withmembers one of the COHORT_XXX constants that allows to return non empty cohorts only
244 * or cohorts with enroled/not enroled users, or just return members count
245 * @param int $offset
246 * @param int $limit
247 * @param string $search
248 * @param bool $withcustomfields if set to yes, then cohort custom fields will be included in the results.
249 * @return array
251 function cohort_get_available_cohorts($currentcontext, $withmembers = 0, $offset = 0, $limit = 25,
252 $search = '', $withcustomfields = false) {
253 global $DB;
255 $params = array();
257 // Build context subquery. Find the list of parent context where user is able to see any or visible-only cohorts.
258 // Since this method is normally called for the current course all parent contexts are already preloaded.
259 $contextsany = array_filter($currentcontext->get_parent_context_ids(),
260 function($a) {
261 return has_capability("moodle/cohort:view", context::instance_by_id($a));
263 $contextsvisible = array_diff($currentcontext->get_parent_context_ids(), $contextsany);
264 if (empty($contextsany) && empty($contextsvisible)) {
265 // User does not have any permissions to view cohorts.
266 return array();
268 $subqueries = array();
269 if (!empty($contextsany)) {
270 list($parentsql, $params1) = $DB->get_in_or_equal($contextsany, SQL_PARAMS_NAMED, 'ctxa');
271 $subqueries[] = 'c.contextid ' . $parentsql;
272 $params = array_merge($params, $params1);
274 if (!empty($contextsvisible)) {
275 list($parentsql, $params1) = $DB->get_in_or_equal($contextsvisible, SQL_PARAMS_NAMED, 'ctxv');
276 $subqueries[] = '(c.visible = 1 AND c.contextid ' . $parentsql. ')';
277 $params = array_merge($params, $params1);
279 $wheresql = '(' . implode(' OR ', $subqueries) . ')';
281 // Build the rest of the query.
282 $fromsql = "";
283 $fieldssql = 'c.id, c.name, c.contextid, c.idnumber, c.visible';
284 $groupbysql = '';
285 $havingsql = '';
286 if ($withmembers) {
287 $fieldssql .= ', s.memberscnt';
288 $subfields = "c.id, COUNT(DISTINCT cm.userid) AS memberscnt";
289 $groupbysql = " GROUP BY c.id";
290 $fromsql = " LEFT JOIN {cohort_members} cm ON cm.cohortid = c.id ";
291 if (in_array($withmembers,
292 array(COHORT_COUNT_ENROLLED_MEMBERS, COHORT_WITH_ENROLLED_MEMBERS_ONLY, COHORT_WITH_NOTENROLLED_MEMBERS_ONLY))) {
293 list($esql, $params2) = get_enrolled_sql($currentcontext);
294 $fromsql .= " LEFT JOIN ($esql) u ON u.id = cm.userid ";
295 $params = array_merge($params2, $params);
296 $fieldssql .= ', s.enrolledcnt';
297 $subfields .= ', COUNT(DISTINCT u.id) AS enrolledcnt';
299 if ($withmembers == COHORT_WITH_MEMBERS_ONLY) {
300 $havingsql = " HAVING COUNT(DISTINCT cm.userid) > 0";
301 } else if ($withmembers == COHORT_WITH_ENROLLED_MEMBERS_ONLY) {
302 $havingsql = " HAVING COUNT(DISTINCT u.id) > 0";
303 } else if ($withmembers == COHORT_WITH_NOTENROLLED_MEMBERS_ONLY) {
304 $havingsql = " HAVING COUNT(DISTINCT cm.userid) > COUNT(DISTINCT u.id)";
307 if ($search) {
308 list($searchsql, $searchparams) = cohort_get_search_query($search);
309 $wheresql .= ' AND ' . $searchsql;
310 $params = array_merge($params, $searchparams);
313 if ($withmembers) {
314 $sql = "SELECT " . str_replace('c.', 'cohort.', $fieldssql) . "
315 FROM {cohort} cohort
316 JOIN (SELECT $subfields
317 FROM {cohort} c $fromsql
318 WHERE $wheresql $groupbysql $havingsql
319 ) s ON cohort.id = s.id
320 ORDER BY cohort.name, cohort.idnumber";
321 } else {
322 $sql = "SELECT $fieldssql
323 FROM {cohort} c $fromsql
324 WHERE $wheresql
325 ORDER BY c.name, c.idnumber";
328 $cohorts = $DB->get_records_sql($sql, $params, $offset, $limit);
330 if ($withcustomfields) {
331 $cohortids = array_keys($cohorts);
332 $customfieldsdata = cohort_get_custom_fields_data($cohortids);
334 foreach ($cohorts as $cohort) {
335 $cohort->customfields = !empty($customfieldsdata[$cohort->id]) ? $customfieldsdata[$cohort->id] : [];
339 return $cohorts;
343 * Check if cohort exists and user is allowed to access it from the given context.
345 * @param stdClass|int $cohortorid cohort object or id
346 * @param context $currentcontext current context (course) where visibility is checked
347 * @return boolean
349 function cohort_can_view_cohort($cohortorid, $currentcontext) {
350 global $DB;
351 if (is_numeric($cohortorid)) {
352 $cohort = $DB->get_record('cohort', array('id' => $cohortorid), 'id, contextid, visible');
353 } else {
354 $cohort = $cohortorid;
357 if ($cohort && in_array($cohort->contextid, $currentcontext->get_parent_context_ids())) {
358 if ($cohort->visible) {
359 return true;
361 $cohortcontext = context::instance_by_id($cohort->contextid);
362 if (has_capability('moodle/cohort:view', $cohortcontext)) {
363 return true;
366 return false;
370 * Get a cohort by id. Also does a visibility check and returns false if the user cannot see this cohort.
372 * @param stdClass|int $cohortorid cohort object or id
373 * @param context $currentcontext current context (course) where visibility is checked
374 * @param bool $withcustomfields if set to yes, then cohort custom fields will be included in the results.
375 * @return stdClass|boolean
377 function cohort_get_cohort($cohortorid, $currentcontext, $withcustomfields = false) {
378 global $DB;
379 if (is_numeric($cohortorid)) {
380 $cohort = $DB->get_record('cohort', array('id' => $cohortorid), 'id, contextid, visible');
381 } else {
382 $cohort = $cohortorid;
385 if ($cohort && in_array($cohort->contextid, $currentcontext->get_parent_context_ids())) {
386 if (!$cohort->visible) {
387 $cohortcontext = context::instance_by_id($cohort->contextid);
388 if (!has_capability('moodle/cohort:view', $cohortcontext)) {
389 return false;
392 } else {
393 return false;
396 if ($cohort && $withcustomfields) {
397 $customfieldsdata = cohort_get_custom_fields_data([$cohort->id]);
398 $cohort->customfields = !empty($customfieldsdata[$cohort->id]) ? $customfieldsdata[$cohort->id] : [];
401 return $cohort;
405 * Produces a part of SQL query to filter cohorts by the search string
407 * Called from {@link cohort_get_cohorts()}, {@link cohort_get_all_cohorts()} and {@link cohort_get_available_cohorts()}
409 * @access private
411 * @param string $search search string
412 * @param string $tablealias alias of cohort table in the SQL query (highly recommended if other tables are used in query)
413 * @return array of two elements - SQL condition and array of named parameters
415 function cohort_get_search_query($search, $tablealias = '') {
416 global $DB;
417 $params = array();
418 if (empty($search)) {
419 // This function should not be called if there is no search string, just in case return dummy query.
420 return array('1=1', $params);
422 if ($tablealias && substr($tablealias, -1) !== '.') {
423 $tablealias .= '.';
425 $searchparam = '%' . $DB->sql_like_escape($search) . '%';
426 $conditions = array();
427 $fields = array('name', 'idnumber', 'description');
428 $cnt = 0;
429 foreach ($fields as $field) {
430 $conditions[] = $DB->sql_like($tablealias . $field, ':csearch' . $cnt, false);
431 $params['csearch' . $cnt] = $searchparam;
432 $cnt++;
434 $sql = '(' . implode(' OR ', $conditions) . ')';
435 return array($sql, $params);
439 * Get all the cohorts defined in given context.
441 * The function does not check user capability to view/manage cohorts in the given context
442 * assuming that it has been already verified.
444 * @param int $contextid
445 * @param int $page number of the current page
446 * @param int $perpage items per page
447 * @param string $search search string
448 * @param bool $withcustomfields if set to yes, then cohort custom fields will be included in the results.
449 * @return array Array(totalcohorts => int, cohorts => array, allcohorts => int)
451 function cohort_get_cohorts($contextid, $page = 0, $perpage = 25, $search = '', $withcustomfields = false) {
452 global $DB;
454 $fields = "SELECT *";
455 $countfields = "SELECT COUNT(1)";
456 $sql = " FROM {cohort}
457 WHERE contextid = :contextid";
458 $params = array('contextid' => $contextid);
459 $order = " ORDER BY name ASC, idnumber ASC";
461 if (!empty($search)) {
462 list($searchcondition, $searchparams) = cohort_get_search_query($search);
463 $sql .= ' AND ' . $searchcondition;
464 $params = array_merge($params, $searchparams);
467 $totalcohorts = $allcohorts = $DB->count_records('cohort', array('contextid' => $contextid));
468 if (!empty($search)) {
469 $totalcohorts = $DB->count_records_sql($countfields . $sql, $params);
471 $cohorts = $DB->get_records_sql($fields . $sql . $order, $params, $page*$perpage, $perpage);
473 if ($withcustomfields) {
474 $cohortids = array_keys($cohorts);
475 $customfieldsdata = cohort_get_custom_fields_data($cohortids);
477 foreach ($cohorts as $cohort) {
478 $cohort->customfields = !empty($customfieldsdata[$cohort->id]) ? $customfieldsdata[$cohort->id] : [];
482 return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts, 'allcohorts' => $allcohorts);
486 * Get all the cohorts defined anywhere in system.
488 * The function assumes that user capability to view/manage cohorts on system level
489 * has already been verified. This function only checks if such capabilities have been
490 * revoked in child (categories) contexts.
492 * @param int $page number of the current page
493 * @param int $perpage items per page
494 * @param string $search search string
495 * @param bool $withcustomfields if set to yes, then cohort custom fields will be included in the results.
496 * @return array Array(totalcohorts => int, cohorts => array, allcohorts => int)
498 function cohort_get_all_cohorts($page = 0, $perpage = 25, $search = '', $withcustomfields = false) {
499 global $DB;
501 $fields = "SELECT c.*, ".context_helper::get_preload_record_columns_sql('ctx');
502 $countfields = "SELECT COUNT(*)";
503 $sql = " FROM {cohort} c
504 JOIN {context} ctx ON ctx.id = c.contextid ";
505 $params = array();
506 $wheresql = '';
508 if ($excludedcontexts = cohort_get_invisible_contexts()) {
509 list($excludedsql, $excludedparams) = $DB->get_in_or_equal($excludedcontexts, SQL_PARAMS_NAMED, 'excl', false);
510 $wheresql = ' WHERE c.contextid '.$excludedsql;
511 $params = array_merge($params, $excludedparams);
514 $totalcohorts = $allcohorts = $DB->count_records_sql($countfields . $sql . $wheresql, $params);
516 if (!empty($search)) {
517 list($searchcondition, $searchparams) = cohort_get_search_query($search, 'c');
518 $wheresql .= ($wheresql ? ' AND ' : ' WHERE ') . $searchcondition;
519 $params = array_merge($params, $searchparams);
520 $totalcohorts = $DB->count_records_sql($countfields . $sql . $wheresql, $params);
523 $order = " ORDER BY c.name ASC, c.idnumber ASC";
524 $cohorts = $DB->get_records_sql($fields . $sql . $wheresql . $order, $params, $page*$perpage, $perpage);
526 if ($withcustomfields) {
527 $cohortids = array_keys($cohorts);
528 $customfieldsdata = cohort_get_custom_fields_data($cohortids);
531 foreach ($cohorts as $cohort) {
532 // Preload used contexts, they will be used to check view/manage/assign capabilities and display categories names.
533 context_helper::preload_from_record($cohort);
534 if ($withcustomfields) {
535 $cohort->customfields = !empty($customfieldsdata[$cohort->id]) ? $customfieldsdata[$cohort->id] : [];
539 return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts, 'allcohorts' => $allcohorts);
543 * Get all the cohorts where the given user is member of.
545 * @param int $userid
546 * @param bool $withcustomfields if set to yes, then cohort custom fields will be included in the results.
547 * @return array Array
549 function cohort_get_user_cohorts($userid, $withcustomfields = false) {
550 global $DB;
552 $sql = 'SELECT c.*
553 FROM {cohort} c
554 JOIN {cohort_members} cm ON c.id = cm.cohortid
555 WHERE cm.userid = ? AND c.visible = 1';
556 $cohorts = $DB->get_records_sql($sql, array($userid));
558 if ($withcustomfields) {
559 $cohortids = array_keys($cohorts);
560 $customfieldsdata = cohort_get_custom_fields_data($cohortids);
562 foreach ($cohorts as $cohort) {
563 $cohort->customfields = !empty($customfieldsdata[$cohort->id]) ? $customfieldsdata[$cohort->id] : [];
567 return $cohorts;
571 * Get the user cohort theme.
573 * If the user is member of one cohort, will return this cohort theme (if defined).
574 * If the user is member of 2 or more cohorts, will return the theme if all them have the same
575 * theme (null themes are ignored).
577 * @param int $userid
578 * @return string|null
580 function cohort_get_user_cohort_theme($userid) {
581 $cohorts = cohort_get_user_cohorts($userid);
582 $theme = null;
583 foreach ($cohorts as $cohort) {
584 if (!empty($cohort->theme)) {
585 if (null === $theme) {
586 $theme = $cohort->theme;
587 } else if ($theme != $cohort->theme) {
588 return null;
592 return $theme;
596 * Returns list of contexts where cohorts are present but current user does not have capability to view/manage them.
598 * This function is called from {@link cohort_get_all_cohorts()} to ensure correct pagination in rare cases when user
599 * is revoked capability in child contexts. It assumes that user's capability to view/manage cohorts on system
600 * level has already been verified.
602 * @access private
604 * @return array array of context ids
606 function cohort_get_invisible_contexts() {
607 global $DB;
608 if (is_siteadmin()) {
609 // Shortcut, admin can do anything and can not be prohibited from any context.
610 return array();
612 $records = $DB->get_recordset_sql("SELECT DISTINCT ctx.id, ".context_helper::get_preload_record_columns_sql('ctx')." ".
613 "FROM {context} ctx JOIN {cohort} c ON ctx.id = c.contextid ");
614 $excludedcontexts = array();
615 foreach ($records as $ctx) {
616 context_helper::preload_from_record($ctx);
617 if (context::instance_by_id($ctx->id) == context_system::instance()) {
618 continue; // System context cohorts should be available and permissions already checked.
620 if (!has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), context::instance_by_id($ctx->id))) {
621 $excludedcontexts[] = $ctx->id;
624 $records->close();
625 return $excludedcontexts;
629 * Returns navigation controls (tabtree) to be displayed on cohort management pages
631 * @param context $context system or category context where cohorts controls are about to be displayed
632 * @param moodle_url $currenturl
633 * @return null|renderable
635 function cohort_edit_controls(context $context, moodle_url $currenturl) {
636 $tabs = array();
637 $currenttab = 'view';
638 $viewurl = new moodle_url('/cohort/index.php', array('contextid' => $context->id));
639 if (($searchquery = $currenturl->get_param('search'))) {
640 $viewurl->param('search', $searchquery);
642 if ($context->contextlevel == CONTEXT_SYSTEM) {
643 $tabs[] = new tabobject('view', new moodle_url($viewurl, array('showall' => 0)), get_string('systemcohorts', 'cohort'));
644 $tabs[] = new tabobject('viewall', new moodle_url($viewurl, array('showall' => 1)), get_string('allcohorts', 'cohort'));
645 if ($currenturl->get_param('showall')) {
646 $currenttab = 'viewall';
648 } else {
649 $tabs[] = new tabobject('view', $viewurl, get_string('cohorts', 'cohort'));
651 if (has_capability('moodle/cohort:manage', $context)) {
652 $addurl = new moodle_url('/cohort/edit.php', array('contextid' => $context->id));
653 $tabs[] = new tabobject('addcohort', $addurl, get_string('addcohort', 'cohort'));
654 if ($currenturl->get_path() === $addurl->get_path() && !$currenturl->param('id')) {
655 $currenttab = 'addcohort';
658 $uploadurl = new moodle_url('/cohort/upload.php', array('contextid' => $context->id));
659 $tabs[] = new tabobject('uploadcohorts', $uploadurl, get_string('uploadcohorts', 'cohort'));
660 if ($currenturl->get_path() === $uploadurl->get_path()) {
661 $currenttab = 'uploadcohorts';
664 if (count($tabs) > 1) {
665 return new tabtree($tabs, $currenttab);
667 return null;
671 * Implements callback inplace_editable() allowing to edit values in-place
673 * @param string $itemtype
674 * @param int $itemid
675 * @param mixed $newvalue
676 * @return \core\output\inplace_editable
678 function core_cohort_inplace_editable($itemtype, $itemid, $newvalue) {
679 if ($itemtype === 'cohortname') {
680 return \core_cohort\output\cohortname::update($itemid, $newvalue);
681 } else if ($itemtype === 'cohortidnumber') {
682 return \core_cohort\output\cohortidnumber::update($itemid, $newvalue);
687 * Returns a list of valid themes which can be displayed in a selector.
689 * @return array as (string)themename => (string)get_string_theme
691 function cohort_get_list_of_themes() {
692 $themes = array();
693 $allthemes = get_list_of_themes();
694 foreach ($allthemes as $key => $theme) {
695 if (empty($theme->hidefromselector)) {
696 $themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
699 return $themes;
703 * Returns custom fields data for provided cohorts.
705 * @param array $cohortids a list of cohort IDs to provide data for.
706 * @return \core_customfield\data_controller[]
708 function cohort_get_custom_fields_data(array $cohortids): array {
709 $result = [];
711 if (!empty($cohortids)) {
712 $handler = core_cohort\customfield\cohort_handler::create();
713 $result = $handler->get_instances_data($cohortids, true);
716 return $result;