See Release Notes
Long Term Support Release
Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]
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 * Base class for conditional availability information (for module or section). 19 * 20 * @package core_availability 21 * @copyright 2014 The Open University 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace core_availability; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 /** 30 * Base class for conditional availability information (for module or section). 31 * 32 * @package core_availability 33 * @copyright 2014 The Open University 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 abstract class info { 37 /** @var \stdClass Course */ 38 protected $course; 39 40 /** @var \course_modinfo Modinfo (available only during some functions) */ 41 protected $modinfo = null; 42 43 /** @var bool Visibility flag (eye icon) */ 44 protected $visible; 45 46 /** @var string Availability data as JSON string */ 47 protected $availability; 48 49 /** @var tree Availability configuration, decoded from JSON; null if unset */ 50 protected $availabilitytree; 51 52 /** @var array|null Array of information about current restore if any */ 53 protected static $restoreinfo = null; 54 55 /** 56 * Constructs with item details. 57 * 58 * @param \stdClass $course Course object 59 * @param int $visible Value of visible flag (eye icon) 60 * @param string $availability Availability definition (JSON format) or null 61 * @throws \coding_exception If data is not valid JSON format 62 */ 63 public function __construct($course, $visible, $availability) { 64 // Set basic values. 65 $this->course = $course; 66 $this->visible = (bool)$visible; 67 $this->availability = $availability; 68 } 69 70 /** 71 * Obtains the course associated with this availability information. 72 * 73 * @return \stdClass Moodle course object 74 */ 75 public function get_course() { 76 return $this->course; 77 } 78 79 /** 80 * Gets context used for checking capabilities for this item. 81 * 82 * @return \context Context for this item 83 */ 84 public abstract function get_context(); 85 86 /** 87 * Obtains the modinfo associated with this availability information. 88 * 89 * Note: This field is available ONLY for use by conditions when calculating 90 * availability or information. 91 * 92 * @return \course_modinfo Modinfo 93 * @throws \coding_exception If called at incorrect times 94 */ 95 public function get_modinfo() { 96 if (!$this->modinfo) { 97 throw new \coding_exception( 98 'info::get_modinfo available only during condition checking'); 99 } 100 return $this->modinfo; 101 } 102 103 /** 104 * Gets the availability tree, decoding it if not already done. 105 * 106 * @return tree Availability tree 107 */ 108 public function get_availability_tree() { 109 if (is_null($this->availabilitytree)) { 110 if (is_null($this->availability)) { 111 throw new \coding_exception( 112 'Cannot call get_availability_tree with null availability'); 113 } 114 $this->availabilitytree = $this->decode_availability($this->availability, true); 115 } 116 return $this->availabilitytree; 117 } 118 119 /** 120 * Decodes availability data from JSON format. 121 * 122 * This function also validates the retrieved data as follows: 123 * 1. Data that does not meet the API-defined structure causes a 124 * coding_exception (this should be impossible unless there is 125 * a system bug or somebody manually hacks the database). 126 * 2. Data that meets the structure but cannot be implemented (e.g. 127 * reference to missing plugin or to module that doesn't exist) is 128 * either silently discarded (if $lax is true) or causes a 129 * coding_exception (if $lax is false). 130 * 131 * @param string $availability Availability string in JSON format 132 * @param boolean $lax If true, throw exceptions only for invalid structure 133 * @return tree Availability tree 134 * @throws \coding_exception If data is not valid JSON format 135 */ 136 protected function decode_availability($availability, $lax) { 137 // Decode JSON data. 138 $structure = json_decode($availability); 139 if (is_null($structure)) { 140 throw new \coding_exception('Invalid availability text', $availability); 141 } 142 143 // Recursively decode tree. 144 return new tree($structure, $lax); 145 } 146 147 /** 148 * Determines whether this particular item is currently available 149 * according to the availability criteria. 150 * 151 * - This does not include the 'visible' setting (i.e. this might return 152 * true even if visible is false); visible is handled independently. 153 * - This does not take account of the viewhiddenactivities capability. 154 * That should apply later. 155 * 156 * Depending on options selected, a description of the restrictions which 157 * mean the student can't view it (in HTML format) may be stored in 158 * $information. If there is nothing in $information and this function 159 * returns false, then the activity should not be displayed at all. 160 * 161 * This function displays debugging() messages if the availability 162 * information is invalid. 163 * 164 * @param string $information String describing restrictions in HTML format 165 * @param bool $grabthelot Performance hint: if true, caches information 166 * required for all course-modules, to make the front page and similar 167 * pages work more quickly (works only for current user) 168 * @param int $userid If set, specifies a different user ID to check availability for 169 * @param \course_modinfo $modinfo Usually leave as null for default. Specify when 170 * calling recursively from inside get_fast_modinfo() 171 * @return bool True if this item is available to the user, false otherwise 172 */ 173 public function is_available(&$information, $grabthelot = false, $userid = 0, 174 \course_modinfo $modinfo = null) { 175 global $USER; 176 177 // Default to no information. 178 $information = ''; 179 180 // Do nothing if there are no availability restrictions. 181 if (is_null($this->availability)) { 182 return true; 183 } 184 185 // Resolve optional parameters. 186 if (!$userid) { 187 $userid = $USER->id; 188 } 189 if (!$modinfo) { 190 $modinfo = get_fast_modinfo($this->course, $userid); 191 } 192 $this->modinfo = $modinfo; 193 194 // Get availability from tree. 195 try { 196 $tree = $this->get_availability_tree(); 197 $result = $tree->check_available(false, $this, $grabthelot, $userid); 198 } catch (\coding_exception $e) { 199 $this->warn_about_invalid_availability($e); 200 $this->modinfo = null; 201 return false; 202 } 203 204 // See if there are any messages. 205 if ($result->is_available()) { 206 $this->modinfo = null; 207 return true; 208 } else { 209 // If the item is marked as 'not visible' then we don't change the available 210 // flag (visible/available are treated distinctly), but we remove any 211 // availability info. If the item is hidden with the eye icon, it doesn't 212 // make sense to show 'Available from <date>' or similar, because even 213 // when that date arrives it will still not be available unless somebody 214 // toggles the eye icon. 215 if ($this->visible) { 216 $information = $tree->get_result_information($this, $result); 217 } 218 219 $this->modinfo = null; 220 return false; 221 } 222 } 223 224 /** 225 * Checks whether this activity is going to be available for all users. 226 * 227 * Normally, if there are any conditions, then it may be hidden depending 228 * on the user. However in the case of date conditions there are some 229 * conditions which will definitely not result in it being hidden for 230 * anyone. 231 * 232 * @return bool True if activity is available for all 233 */ 234 public function is_available_for_all() { 235 global $CFG; 236 if (is_null($this->availability) || empty($CFG->enableavailability)) { 237 return true; 238 } else { 239 try { 240 return $this->get_availability_tree()->is_available_for_all(); 241 } catch (\coding_exception $e) { 242 $this->warn_about_invalid_availability($e); 243 return false; 244 } 245 } 246 } 247 248 /** 249 * Obtains a string describing all availability restrictions (even if 250 * they do not apply any more). Used to display information for staff 251 * editing the website. 252 * 253 * The modinfo parameter must be specified when it is called from inside 254 * get_fast_modinfo, to avoid infinite recursion. 255 * 256 * This function displays debugging() messages if the availability 257 * information is invalid. 258 * 259 * @param \course_modinfo $modinfo Usually leave as null for default 260 * @return string Information string (for admin) about all restrictions on 261 * this item 262 */ 263 public function get_full_information(\course_modinfo $modinfo = null) { 264 // Do nothing if there are no availability restrictions. 265 if (is_null($this->availability)) { 266 return ''; 267 } 268 269 // Resolve optional parameter. 270 if (!$modinfo) { 271 $modinfo = get_fast_modinfo($this->course); 272 } 273 $this->modinfo = $modinfo; 274 275 try { 276 $result = $this->get_availability_tree()->get_full_information($this); 277 $this->modinfo = null; 278 return $result; 279 } catch (\coding_exception $e) { 280 $this->warn_about_invalid_availability($e); 281 return false; 282 } 283 } 284 285 /** 286 * In some places we catch coding_exception because if a bug happens, it 287 * would be fatal for the course page GUI; instead we just show a developer 288 * debug message. 289 * 290 * @param \coding_exception $e Exception that occurred 291 */ 292 protected function warn_about_invalid_availability(\coding_exception $e) { 293 $name = $this->get_thing_name(); 294 $htmlname = $this->format_info($name, $this->course); 295 // Because we call format_info here, likely in the middle of building dynamic data for the 296 // activity, there could be a chance that the name might not be available. 297 if ($htmlname === '') { 298 // So instead use the numbers (cmid) from the tag. 299 $htmlname = preg_replace('~[^0-9]~', '', $name); 300 } 301 $info = 'Error processing availability data for ‘' . $htmlname 302 . '’: ' . s($e->a); 303 debugging($info, DEBUG_DEVELOPER); 304 } 305 306 /** 307 * Called during restore (near end of restore). Updates any necessary ids 308 * and writes the updated tree to the database. May output warnings if 309 * necessary (e.g. if a course-module cannot be found after restore). 310 * 311 * @param string $restoreid Restore identifier 312 * @param int $courseid Target course id 313 * @param \base_logger $logger Logger for any warnings 314 * @param int $dateoffset Date offset to be added to any dates (0 = none) 315 * @param \base_task $task Restore task 316 */ 317 public function update_after_restore($restoreid, $courseid, \base_logger $logger, 318 $dateoffset, \base_task $task) { 319 $tree = $this->get_availability_tree(); 320 // Set static data for use by get_restore_date_offset function. 321 self::$restoreinfo = array('restoreid' => $restoreid, 'dateoffset' => $dateoffset, 322 'task' => $task); 323 $changed = $tree->update_after_restore($restoreid, $courseid, $logger, 324 $this->get_thing_name()); 325 if ($changed) { 326 // Save modified data. 327 if ($tree->is_empty()) { 328 // If the tree is empty, but the tree has changed, remove this condition. 329 $this->set_in_database(null); 330 } else { 331 $structure = $tree->save(); 332 $this->set_in_database(json_encode($structure)); 333 } 334 } 335 } 336 337 /** 338 * Gets the date offset (amount by which any date values should be 339 * adjusted) for the current restore. 340 * 341 * @param string $restoreid Restore identifier 342 * @return int Date offset (0 if none) 343 * @throws coding_exception If not in a restore (or not in that restore) 344 */ 345 public static function get_restore_date_offset($restoreid) { 346 if (!self::$restoreinfo) { 347 throw new coding_exception('Only valid during restore'); 348 } 349 if (self::$restoreinfo['restoreid'] !== $restoreid) { 350 throw new coding_exception('Data not available for that restore id'); 351 } 352 return self::$restoreinfo['dateoffset']; 353 } 354 355 /** 356 * Gets the restore task (specifically, the task that calls the 357 * update_after_restore method) for the current restore. 358 * 359 * @param string $restoreid Restore identifier 360 * @return \base_task Restore task 361 * @throws coding_exception If not in a restore (or not in that restore) 362 */ 363 public static function get_restore_task($restoreid) { 364 if (!self::$restoreinfo) { 365 throw new coding_exception('Only valid during restore'); 366 } 367 if (self::$restoreinfo['restoreid'] !== $restoreid) { 368 throw new coding_exception('Data not available for that restore id'); 369 } 370 return self::$restoreinfo['task']; 371 } 372 373 /** 374 * Obtains the name of the item (cm_info or section_info, at present) that 375 * this is controlling availability of. Name should be formatted ready 376 * for on-screen display. 377 * 378 * @return string Name of item 379 */ 380 protected abstract function get_thing_name(); 381 382 /** 383 * Stores an updated availability tree JSON structure into the relevant 384 * database table. 385 * 386 * @param string $availabilty New JSON value 387 */ 388 protected abstract function set_in_database($availabilty); 389 390 /** 391 * In rare cases the system may want to change all references to one ID 392 * (e.g. one course-module ID) to another one, within a course. This 393 * function does that for the conditional availability data for all 394 * modules and sections on the course. 395 * 396 * @param int|\stdClass $courseorid Course id or object 397 * @param string $table Table name e.g. 'course_modules' 398 * @param int $oldid Previous ID 399 * @param int $newid New ID 400 * @return bool True if anything changed, otherwise false 401 */ 402 public static function update_dependency_id_across_course( 403 $courseorid, $table, $oldid, $newid) { 404 global $DB; 405 $transaction = $DB->start_delegated_transaction(); 406 $modinfo = get_fast_modinfo($courseorid); 407 $anychanged = false; 408 foreach ($modinfo->get_cms() as $cm) { 409 $info = new info_module($cm); 410 $changed = $info->update_dependency_id($table, $oldid, $newid); 411 $anychanged = $anychanged || $changed; 412 } 413 foreach ($modinfo->get_section_info_all() as $section) { 414 $info = new info_section($section); 415 $changed = $info->update_dependency_id($table, $oldid, $newid); 416 $anychanged = $anychanged || $changed; 417 } 418 $transaction->allow_commit(); 419 if ($anychanged) { 420 get_fast_modinfo($courseorid, 0, true); 421 } 422 return $anychanged; 423 } 424 425 /** 426 * Called on a single item. If necessary, updates availability data where 427 * it has a dependency on an item with a particular id. 428 * 429 * @param string $table Table name e.g. 'course_modules' 430 * @param int $oldid Previous ID 431 * @param int $newid New ID 432 * @return bool True if it changed, otherwise false 433 */ 434 protected function update_dependency_id($table, $oldid, $newid) { 435 // Do nothing if there are no availability restrictions. 436 if (is_null($this->availability)) { 437 return false; 438 } 439 // Pass requirement on to tree object. 440 $tree = $this->get_availability_tree(); 441 $changed = $tree->update_dependency_id($table, $oldid, $newid); 442 if ($changed) { 443 // Save modified data. 444 $structure = $tree->save(); 445 $this->set_in_database(json_encode($structure)); 446 } 447 return $changed; 448 } 449 450 /** 451 * Converts legacy data from fields (if provided) into the new availability 452 * syntax. 453 * 454 * Supported fields: availablefrom, availableuntil, showavailability 455 * (and groupingid for sections). 456 * 457 * It also supports the groupmembersonly field for modules. This part was 458 * optional in 2.7 but now always runs (because groupmembersonly has been 459 * removed). 460 * 461 * @param \stdClass $rec Object possibly containing legacy fields 462 * @param bool $section True if this is a section 463 * @param bool $modgroupmembersonlyignored Ignored option, previously used 464 * @return string|null New availability value or null if none 465 */ 466 public static function convert_legacy_fields($rec, $section, $modgroupmembersonlyignored = false) { 467 // Do nothing if the fields are not set. 468 if (empty($rec->availablefrom) && empty($rec->availableuntil) && 469 (empty($rec->groupmembersonly)) && 470 (!$section || empty($rec->groupingid))) { 471 return null; 472 } 473 474 // Handle legacy availability data. 475 $conditions = array(); 476 $shows = array(); 477 478 // Groupmembersonly condition (if enabled) for modules, groupingid for 479 // sections. 480 if (!empty($rec->groupmembersonly) || 481 (!empty($rec->groupingid) && $section)) { 482 if (!empty($rec->groupingid)) { 483 $conditions[] = '{"type":"grouping"' . 484 ($rec->groupingid ? ',"id":' . $rec->groupingid : '') . '}'; 485 } else { 486 // No grouping specified, so allow any group. 487 $conditions[] = '{"type":"group"}'; 488 } 489 // Group members only condition was not displayed to students. 490 $shows[] = 'false'; 491 } 492 493 // Date conditions. 494 if (!empty($rec->availablefrom)) { 495 $conditions[] = '{"type":"date","d":">=","t":' . $rec->availablefrom . '}'; 496 $shows[] = !empty($rec->showavailability) ? 'true' : 'false'; 497 } 498 if (!empty($rec->availableuntil)) { 499 $conditions[] = '{"type":"date","d":"<","t":' . $rec->availableuntil . '}'; 500 // Until dates never showed to students. 501 $shows[] = 'false'; 502 } 503 504 // If there are some conditions, return them. 505 if ($conditions) { 506 return '{"op":"&","showc":[' . implode(',', $shows) . '],' . 507 '"c":[' . implode(',', $conditions) . ']}'; 508 } else { 509 return null; 510 } 511 } 512 513 /** 514 * Adds a condition from the legacy availability condition. 515 * 516 * (For use during restore only.) 517 * 518 * This function assumes that the activity either has no conditions, or 519 * that it has an AND tree with one or more conditions. 520 * 521 * @param string|null $availability Current availability conditions 522 * @param \stdClass $rec Object containing information from old table 523 * @param bool $show True if 'show' option should be enabled 524 * @return string New availability conditions 525 */ 526 public static function add_legacy_availability_condition($availability, $rec, $show) { 527 if (!empty($rec->sourcecmid)) { 528 // Completion condition. 529 $condition = '{"type":"completion","cm":' . $rec->sourcecmid . 530 ',"e":' . $rec->requiredcompletion . '}'; 531 } else { 532 // Grade condition. 533 $minmax = ''; 534 if (!empty($rec->grademin)) { 535 $minmax .= ',"min":' . sprintf('%.5f', $rec->grademin); 536 } 537 if (!empty($rec->grademax)) { 538 $minmax .= ',"max":' . sprintf('%.5f', $rec->grademax); 539 } 540 $condition = '{"type":"grade","id":' . $rec->gradeitemid . $minmax . '}'; 541 } 542 543 return self::add_legacy_condition($availability, $condition, $show); 544 } 545 546 /** 547 * Adds a condition from the legacy availability field condition. 548 * 549 * (For use during restore only.) 550 * 551 * This function assumes that the activity either has no conditions, or 552 * that it has an AND tree with one or more conditions. 553 * 554 * @param string|null $availability Current availability conditions 555 * @param \stdClass $rec Object containing information from old table 556 * @param bool $show True if 'show' option should be enabled 557 * @return string New availability conditions 558 */ 559 public static function add_legacy_availability_field_condition($availability, $rec, $show) { 560 if (isset($rec->userfield)) { 561 // Standard field. 562 $fieldbit = ',"sf":' . json_encode($rec->userfield); 563 } else { 564 // Custom field. 565 $fieldbit = ',"cf":' . json_encode($rec->shortname); 566 } 567 // Value is not included for certain operators. 568 switch($rec->operator) { 569 case 'isempty': 570 case 'isnotempty': 571 $valuebit = ''; 572 break; 573 574 default: 575 $valuebit = ',"v":' . json_encode($rec->value); 576 break; 577 } 578 $condition = '{"type":"profile","op":"' . $rec->operator . '"' . 579 $fieldbit . $valuebit . '}'; 580 581 return self::add_legacy_condition($availability, $condition, $show); 582 } 583 584 /** 585 * Adds a condition to an AND group. 586 * 587 * (For use during restore only.) 588 * 589 * This function assumes that the activity either has no conditions, or 590 * that it has only conditions added by this function. 591 * 592 * @param string|null $availability Current availability conditions 593 * @param string $condition Condition text '{...}' 594 * @param bool $show True if 'show' option should be enabled 595 * @return string New availability conditions 596 */ 597 protected static function add_legacy_condition($availability, $condition, $show) { 598 $showtext = ($show ? 'true' : 'false'); 599 if (is_null($availability)) { 600 $availability = '{"op":"&","showc":[' . $showtext . 601 '],"c":[' . $condition . ']}'; 602 } else { 603 $matches = array(); 604 if (!preg_match('~^({"op":"&","showc":\[(?:true|false)(?:,(?:true|false))*)' . 605 '(\],"c":\[.*)(\]})$~', $availability, $matches)) { 606 throw new \coding_exception('Unexpected availability value'); 607 } 608 $availability = $matches[1] . ',' . $showtext . $matches[2] . 609 ',' . $condition . $matches[3]; 610 } 611 return $availability; 612 } 613 614 /** 615 * Tests against a user list. Users who cannot access the activity due to 616 * availability restrictions will be removed from the list. 617 * 618 * Note this only includes availability restrictions (those handled within 619 * this API) and not other ways of restricting access. 620 * 621 * This test ONLY includes conditions which are marked as being applied to 622 * user lists. For example, group conditions are included but date 623 * conditions are not included. 624 * 625 * The function operates reasonably efficiently i.e. should not do per-user 626 * database queries. It is however likely to be fairly slow. 627 * 628 * @param array $users Array of userid => object 629 * @return array Filtered version of input array 630 */ 631 public function filter_user_list(array $users) { 632 global $CFG; 633 if (is_null($this->availability) || !$CFG->enableavailability) { 634 return $users; 635 } 636 $tree = $this->get_availability_tree(); 637 $checker = new capability_checker($this->get_context()); 638 639 // Filter using availability tree. 640 $this->modinfo = get_fast_modinfo($this->get_course()); 641 $filtered = $tree->filter_user_list($users, false, $this, $checker); 642 $this->modinfo = null; 643 644 // Include users in the result if they're either in the filtered list, 645 // or they have viewhidden. This logic preserves ordering of the 646 // passed users array. 647 $result = array(); 648 $canviewhidden = $checker->get_users_by_capability($this->get_view_hidden_capability()); 649 foreach ($users as $userid => $data) { 650 if (array_key_exists($userid, $filtered) || array_key_exists($userid, $canviewhidden)) { 651 $result[$userid] = $users[$userid]; 652 } 653 } 654 655 return $result; 656 } 657 658 /** 659 * Gets the capability used to view hidden activities/sections (as 660 * appropriate). 661 * 662 * @return string Name of capability used to view hidden items of this type 663 */ 664 protected abstract function get_view_hidden_capability(); 665 666 /** 667 * Obtains SQL that returns a list of enrolled users that has been filtered 668 * by the conditions applied in the availability API, similar to calling 669 * get_enrolled_users and then filter_user_list. As for filter_user_list, 670 * this ONLY filters out users with conditions that are marked as applying 671 * to user lists. For example, group conditions are included but date 672 * conditions are not included. 673 * 674 * The returned SQL is a query that returns a list of user IDs. It does not 675 * include brackets, so you neeed to add these to make it into a subquery. 676 * You would normally use it in an SQL phrase like "WHERE u.id IN ($sql)". 677 * 678 * The function returns an array with '' and an empty array, if there are 679 * no restrictions on users from these conditions. 680 * 681 * The SQL will be complex and may be slow. It uses named parameters (sorry, 682 * I know they are annoying, but it was unavoidable here). 683 * 684 * @param bool $onlyactive True if including only active enrolments 685 * @return array Array of SQL code (may be empty) and params 686 */ 687 public function get_user_list_sql($onlyactive) { 688 global $CFG; 689 if (is_null($this->availability) || !$CFG->enableavailability) { 690 return array('', array()); 691 } 692 693 // Get SQL for the availability filter. 694 $tree = $this->get_availability_tree(); 695 list ($filtersql, $filterparams) = $tree->get_user_list_sql(false, $this, $onlyactive); 696 if ($filtersql === '') { 697 // No restrictions, so return empty query. 698 return array('', array()); 699 } 700 701 // Get SQL for the view hidden list. 702 list ($viewhiddensql, $viewhiddenparams) = get_enrolled_sql( 703 $this->get_context(), $this->get_view_hidden_capability(), 0, $onlyactive); 704 705 // Result is a union of the two. 706 return array('(' . $filtersql . ') UNION (' . $viewhiddensql . ')', 707 array_merge($filterparams, $viewhiddenparams)); 708 } 709 710 /** 711 * Formats the $cm->availableinfo string for display. This includes 712 * filling in the names of any course-modules that might be mentioned. 713 * Should be called immediately prior to display, or at least somewhere 714 * that we can guarantee does not happen from within building the modinfo 715 * object. 716 * 717 * @param \renderable|string $inforenderable Info string or renderable 718 * @param int|\stdClass $courseorid 719 * @return string Correctly formatted info string 720 */ 721 public static function format_info($inforenderable, $courseorid) { 722 global $PAGE, $OUTPUT; 723 724 // Use renderer if required. 725 if (is_string($inforenderable)) { 726 $info = $inforenderable; 727 } else { 728 $renderable = new \core_availability\output\availability_info($inforenderable); 729 $info = $OUTPUT->render($renderable); 730 } 731 732 // Don't waste time if there are no special tags. 733 if (strpos($info, '<AVAILABILITY_') === false) { 734 return $info; 735 } 736 737 // Handle CMNAME tags. 738 $modinfo = get_fast_modinfo($courseorid); 739 $context = \context_course::instance($modinfo->courseid); 740 $info = preg_replace_callback('~<AVAILABILITY_CMNAME_([0-9]+)/>~', 741 function($matches) use($modinfo, $context) { 742 $cm = $modinfo->get_cm($matches[1]); 743 if ($cm->has_view() and $cm->get_user_visible()) { 744 // Help student by providing a link to the module which is preventing availability. 745 return \html_writer::link($cm->get_url(), format_string($cm->get_name(), true, ['context' => $context])); 746 } else { 747 return format_string($cm->get_name(), true, ['context' => $context]); 748 } 749 }, $info); 750 $info = preg_replace_callback('~<AVAILABILITY_FORMAT_STRING>(.*?)</AVAILABILITY_FORMAT_STRING>~s', 751 function($matches) use ($context) { 752 $decoded = htmlspecialchars_decode($matches[1], ENT_NOQUOTES); 753 return format_string($decoded, true, ['context' => $context]); 754 }, $info); 755 $info = preg_replace_callback('~<AVAILABILITY_CALLBACK type="([a-z0-9_]+)">(.*?)</AVAILABILITY_CALLBACK>~s', 756 function($matches) use ($modinfo, $context) { 757 // Find the class, it must have already been loaded by now. 758 $fullclassname = 'availability_' . $matches[1] . '\condition'; 759 if (!class_exists($fullclassname, false)) { 760 return '<!-- Error finding class ' . $fullclassname .' -->'; 761 } 762 // Load the parameters. 763 $params = []; 764 $encodedparams = preg_split('~<P/>~', $matches[2], 0); 765 foreach ($encodedparams as $encodedparam) { 766 $params[] = htmlspecialchars_decode($encodedparam, ENT_NOQUOTES); 767 } 768 return $fullclassname::get_description_callback_value($modinfo, $context, $params); 769 }, $info); 770 771 return $info; 772 } 773 774 /** 775 * Used in course/lib.php because we need to disable the completion tickbox 776 * JS (using the non-JS version instead, which causes a page reload) if a 777 * completion tickbox value may affect a conditional activity. 778 * 779 * @param \stdClass $course Moodle course object 780 * @param int $cmid Course-module id 781 * @return bool True if this is used in a condition, false otherwise 782 */ 783 public static function completion_value_used($course, $cmid) { 784 // Access all plugins. Normally only the completion plugin is going 785 // to affect this value, but it's potentially possible that some other 786 // plugin could also rely on the completion plugin. 787 $pluginmanager = \core_plugin_manager::instance(); 788 $enabled = $pluginmanager->get_enabled_plugins('availability'); 789 $componentparams = new \stdClass(); 790 foreach ($enabled as $plugin => $info) { 791 // Use the static method. 792 $class = '\availability_' . $plugin . '\condition'; 793 if ($class::completion_value_used($course, $cmid)) { 794 return true; 795 } 796 } 797 return false; 798 } 799 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body