Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]
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. 13 // 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/>. 16 17 /** 18 * Cohort related management functions, this file needs to be included manually. 19 * 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 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 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); 33 34 /** 35 * Add new cohort. 36 * 37 * @param stdClass $cohort 38 * @return int new cohort id 39 */ 40 function cohort_add_cohort($cohort) { 41 global $DB, $CFG; 42 43 if (!isset($cohort->name)) { 44 throw new coding_exception('Missing cohort name in cohort_add_cohort().'); 45 } 46 if (!isset($cohort->idnumber)) { 47 $cohort->idnumber = NULL; 48 } 49 if (!isset($cohort->description)) { 50 $cohort->description = ''; 51 } 52 if (!isset($cohort->descriptionformat)) { 53 $cohort->descriptionformat = FORMAT_HTML; 54 } 55 if (!isset($cohort->visible)) { 56 $cohort->visible = 1; 57 } 58 if (empty($cohort->component)) { 59 $cohort->component = ''; 60 } 61 if (empty($CFG->allowcohortthemes) && isset($cohort->theme)) { 62 unset($cohort->theme); 63 } 64 if (empty($cohort->theme) || empty($CFG->allowcohortthemes)) { 65 $cohort->theme = ''; 66 } 67 if (!isset($cohort->timecreated)) { 68 $cohort->timecreated = time(); 69 } 70 if (!isset($cohort->timemodified)) { 71 $cohort->timemodified = $cohort->timecreated; 72 } 73 74 $cohort->id = $DB->insert_record('cohort', $cohort); 75 76 $handler = core_cohort\customfield\cohort_handler::create(); 77 $handler->instance_form_save($cohort, true); 78 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(); 85 86 return $cohort->id; 87 } 88 89 /** 90 * Update existing cohort. 91 * @param stdClass $cohort 92 * @return void 93 */ 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 = ''; 99 } 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); 103 } 104 $cohort->timemodified = time(); 105 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); 109 110 $DB->update_record('cohort', $cohort); 111 112 $event = \core\event\cohort_updated::create(array( 113 'context' => context::instance_by_id($cohort->contextid), 114 'objectid' => $cohort->id, 115 )); 116 $event->trigger(); 117 } 118 119 /** 120 * Delete cohort. 121 * @param stdClass $cohort 122 * @return void 123 */ 124 function cohort_delete_cohort($cohort) { 125 global $DB; 126 127 if ($cohort->component) { 128 // TODO: add component delete callback 129 } 130 131 $handler = core_cohort\customfield\cohort_handler::create(); 132 $handler->delete_instance($cohort->id); 133 134 $DB->delete_records('cohort_members', array('cohortid'=>$cohort->id)); 135 $DB->delete_records('cohort', array('id'=>$cohort->id)); 136 137 // Notify the competency subsystem. 138 \core_competency\api::hook_cohort_deleted($cohort); 139 140 $event = \core\event\cohort_deleted::create(array( 141 'context' => context::instance_by_id($cohort->contextid), 142 'objectid' => $cohort->id, 143 )); 144 $event->add_record_snapshot('cohort', $cohort); 145 $event->trigger(); 146 } 147 148 /** 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 154 */ 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 158 159 $oldcontext = context_coursecat::instance($category->id); 160 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); 169 } 170 171 $DB->execute($sql, $params); 172 } 173 174 /** 175 * Add cohort member 176 * @param int $cohortid 177 * @param int $userid 178 * @return void 179 */ 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; 185 } 186 $record = new stdClass(); 187 $record->cohortid = $cohortid; 188 $record->userid = $userid; 189 $record->timeadded = time(); 190 $DB->insert_record('cohort_members', $record); 191 192 $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST); 193 194 $event = \core\event\cohort_member_added::create(array( 195 'context' => context::instance_by_id($cohort->contextid), 196 'objectid' => $cohortid, 197 'relateduserid' => $userid, 198 )); 199 $event->add_record_snapshot('cohort', $cohort); 200 $event->trigger(); 201 } 202 203 /** 204 * Remove cohort member 205 * @param int $cohortid 206 * @param int $userid 207 * @return void 208 */ 209 function cohort_remove_member($cohortid, $userid) { 210 global $DB; 211 $DB->delete_records('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid)); 212 213 $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST); 214 215 $event = \core\event\cohort_member_removed::create(array( 216 'context' => context::instance_by_id($cohort->contextid), 217 'objectid' => $cohortid, 218 'relateduserid' => $userid, 219 )); 220 $event->add_record_snapshot('cohort', $cohort); 221 $event->trigger(); 222 } 223 224 /** 225 * Is this user a cohort member? 226 * @param int $cohortid 227 * @param int $userid 228 * @return bool 229 */ 230 function cohort_is_member($cohortid, $userid) { 231 global $DB; 232 233 return $DB->record_exists('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid)); 234 } 235 236 /** 237 * Returns the list of cohorts visible to the current user in the given course. 238 * 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 241 * 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 250 */ 251 function cohort_get_available_cohorts($currentcontext, $withmembers = 0, $offset = 0, $limit = 25, 252 $search = '', $withcustomfields = false) { 253 global $DB; 254 255 $params = array(); 256 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)); 262 }); 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(); 267 } 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); 273 } 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); 278 } 279 $wheresql = '(' . implode(' OR ', $subqueries) . ')'; 280 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'; 298 } 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)"; 305 } 306 } 307 if ($search) { 308 list($searchsql, $searchparams) = cohort_get_search_query($search); 309 $wheresql .= ' AND ' . $searchsql; 310 $params = array_merge($params, $searchparams); 311 } 312 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"; 326 } 327 328 $cohorts = $DB->get_records_sql($sql, $params, $offset, $limit); 329 330 if ($withcustomfields) { 331 $cohortids = array_keys($cohorts); 332 $customfieldsdata = cohort_get_custom_fields_data($cohortids); 333 334 foreach ($cohorts as $cohort) { 335 $cohort->customfields = !empty($customfieldsdata[$cohort->id]) ? $customfieldsdata[$cohort->id] : []; 336 } 337 } 338 339 return $cohorts; 340 } 341 342 /** 343 * Check if cohort exists and user is allowed to access it from the given context. 344 * 345 * @param stdClass|int $cohortorid cohort object or id 346 * @param context $currentcontext current context (course) where visibility is checked 347 * @return boolean 348 */ 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; 355 } 356 357 if ($cohort && in_array($cohort->contextid, $currentcontext->get_parent_context_ids())) { 358 if ($cohort->visible) { 359 return true; 360 } 361 $cohortcontext = context::instance_by_id($cohort->contextid); 362 if (has_capability('moodle/cohort:view', $cohortcontext)) { 363 return true; 364 } 365 } 366 return false; 367 } 368 369 /** 370 * Get a cohort by id. Also does a visibility check and returns false if the user cannot see this cohort. 371 * 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 376 */ 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; 383 } 384 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; 390 } 391 } 392 } else { 393 return false; 394 } 395 396 if ($cohort && $withcustomfields) { 397 $customfieldsdata = cohort_get_custom_fields_data([$cohort->id]); 398 $cohort->customfields = !empty($customfieldsdata[$cohort->id]) ? $customfieldsdata[$cohort->id] : []; 399 } 400 401 return $cohort; 402 } 403 404 /** 405 * Produces a part of SQL query to filter cohorts by the search string 406 * 407 * Called from {@link cohort_get_cohorts()}, {@link cohort_get_all_cohorts()} and {@link cohort_get_available_cohorts()} 408 * 409 * @access private 410 * 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 414 */ 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); 421 } 422 if ($tablealias && substr($tablealias, -1) !== '.') { 423 $tablealias .= '.'; 424 } 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++; 433 } 434 $sql = '(' . implode(' OR ', $conditions) . ')'; 435 return array($sql, $params); 436 } 437 438 /** 439 * Get all the cohorts defined in given context. 440 * 441 * The function does not check user capability to view/manage cohorts in the given context 442 * assuming that it has been already verified. 443 * 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) 450 */ 451 function cohort_get_cohorts($contextid, $page = 0, $perpage = 25, $search = '', $withcustomfields = false) { 452 global $DB; 453 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"; 460 461 if (!empty($search)) { 462 list($searchcondition, $searchparams) = cohort_get_search_query($search); 463 $sql .= ' AND ' . $searchcondition; 464 $params = array_merge($params, $searchparams); 465 } 466 467 $totalcohorts = $allcohorts = $DB->count_records('cohort', array('contextid' => $contextid)); 468 if (!empty($search)) { 469 $totalcohorts = $DB->count_records_sql($countfields . $sql, $params); 470 } 471 $cohorts = $DB->get_records_sql($fields . $sql . $order, $params, $page*$perpage, $perpage); 472 473 if ($withcustomfields) { 474 $cohortids = array_keys($cohorts); 475 $customfieldsdata = cohort_get_custom_fields_data($cohortids); 476 477 foreach ($cohorts as $cohort) { 478 $cohort->customfields = !empty($customfieldsdata[$cohort->id]) ? $customfieldsdata[$cohort->id] : []; 479 } 480 } 481 482 return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts, 'allcohorts' => $allcohorts); 483 } 484 485 /** 486 * Get all the cohorts defined anywhere in system. 487 * 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. 491 * 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) 497 */ 498 function cohort_get_all_cohorts($page = 0, $perpage = 25, $search = '', $withcustomfields = false) { 499 global $DB; 500 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 = ''; 507 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); 512 } 513 514 $totalcohorts = $allcohorts = $DB->count_records_sql($countfields . $sql . $wheresql, $params); 515 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); 521 } 522 523 $order = " ORDER BY c.name ASC, c.idnumber ASC"; 524 $cohorts = $DB->get_records_sql($fields . $sql . $wheresql . $order, $params, $page*$perpage, $perpage); 525 526 if ($withcustomfields) { 527 $cohortids = array_keys($cohorts); 528 $customfieldsdata = cohort_get_custom_fields_data($cohortids); 529 } 530 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] : []; 536 } 537 } 538 539 return array('totalcohorts' => $totalcohorts, 'cohorts' => $cohorts, 'allcohorts' => $allcohorts); 540 } 541 542 /** 543 * Get all the cohorts where the given user is member of. 544 * 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 548 */ 549 function cohort_get_user_cohorts($userid, $withcustomfields = false) { 550 global $DB; 551 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)); 557 558 if ($withcustomfields) { 559 $cohortids = array_keys($cohorts); 560 $customfieldsdata = cohort_get_custom_fields_data($cohortids); 561 562 foreach ($cohorts as $cohort) { 563 $cohort->customfields = !empty($customfieldsdata[$cohort->id]) ? $customfieldsdata[$cohort->id] : []; 564 } 565 } 566 567 return $cohorts; 568 } 569 570 /** 571 * Get the user cohort theme. 572 * 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). 576 * 577 * @param int $userid 578 * @return string|null 579 */ 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; 589 } 590 } 591 } 592 return $theme; 593 } 594 595 /** 596 * Returns list of contexts where cohorts are present but current user does not have capability to view/manage them. 597 * 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. 601 * 602 * @access private 603 * 604 * @return array array of context ids 605 */ 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(); 611 } 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. 619 } 620 if (!has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), context::instance_by_id($ctx->id))) { 621 $excludedcontexts[] = $ctx->id; 622 } 623 } 624 $records->close(); 625 return $excludedcontexts; 626 } 627 628 /** 629 * Returns navigation controls (tabtree) to be displayed on cohort management pages 630 * 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 634 */ 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); 641 } 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'; 647 } 648 } else { 649 $tabs[] = new tabobject('view', $viewurl, get_string('cohorts', 'cohort')); 650 } 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'; 656 } 657 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'; 662 } 663 } 664 if (count($tabs) > 1) { 665 return new tabtree($tabs, $currenttab); 666 } 667 return null; 668 } 669 670 /** 671 * Implements callback inplace_editable() allowing to edit values in-place 672 * 673 * @param string $itemtype 674 * @param int $itemid 675 * @param mixed $newvalue 676 * @return \core\output\inplace_editable 677 */ 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); 683 } 684 } 685 686 /** 687 * Returns a list of valid themes which can be displayed in a selector. 688 * 689 * @return array as (string)themename => (string)get_string_theme 690 */ 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); 697 } 698 } 699 return $themes; 700 } 701 702 /** 703 * Returns custom fields data for provided cohorts. 704 * 705 * @param array $cohortids a list of cohort IDs to provide data for. 706 * @return \core_customfield\data_controller[] 707 */ 708 function cohort_get_custom_fields_data(array $cohortids): array { 709 $result = []; 710 711 if (!empty($cohortids)) { 712 $handler = core_cohort\customfield\cohort_handler::create(); 713 $result = $handler->get_instances_data($cohortids, true); 714 } 715 716 return $result; 717 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body