Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
1 <?php 2 3 // This file is part of Moodle - http://moodle.org/ 4 // 5 // Moodle is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // Moodle is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 17 18 /** 19 * Renderer for use with the course section and all the goodness that falls 20 * within it. 21 * 22 * This renderer should contain methods useful to courses, and categories. 23 * 24 * @package moodlecore 25 * @copyright 2010 Sam Hemelryk 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 */ 28 29 /** 30 * The core course renderer 31 * 32 * Can be retrieved with the following: 33 * $renderer = $PAGE->get_renderer('core','course'); 34 */ 35 class core_course_renderer extends plugin_renderer_base { 36 const COURSECAT_SHOW_COURSES_NONE = 0; /* do not show courses at all */ 37 const COURSECAT_SHOW_COURSES_COUNT = 5; /* do not show courses but show number of courses next to category name */ 38 const COURSECAT_SHOW_COURSES_COLLAPSED = 10; 39 const COURSECAT_SHOW_COURSES_AUTO = 15; /* will choose between collapsed and expanded automatically */ 40 const COURSECAT_SHOW_COURSES_EXPANDED = 20; 41 const COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT = 30; 42 43 const COURSECAT_TYPE_CATEGORY = 0; 44 const COURSECAT_TYPE_COURSE = 1; 45 46 /** 47 * A cache of strings 48 * @var stdClass 49 */ 50 protected $strings; 51 52 /** 53 * Whether a category content is being initially rendered with children. This is mainly used by the 54 * core_course_renderer::corsecat_tree() to render the appropriate action for the Expand/Collapse all link on 55 * page load. 56 * @var bool 57 */ 58 protected $categoryexpandedonload = false; 59 60 /** 61 * Override the constructor so that we can initialise the string cache 62 * 63 * @param moodle_page $page 64 * @param string $target 65 */ 66 public function __construct(moodle_page $page, $target) { 67 $this->strings = new stdClass; 68 parent::__construct($page, $target); 69 } 70 71 /** 72 * @deprecated since 3.2 73 */ 74 protected function add_modchoosertoggle() { 75 throw new coding_exception('core_course_renderer::add_modchoosertoggle() can not be used anymore.'); 76 } 77 78 /** 79 * Renders course info box. 80 * 81 * @param stdClass $course 82 * @return string 83 */ 84 public function course_info_box(stdClass $course) { 85 $content = ''; 86 $content .= $this->output->box_start('generalbox info'); 87 $chelper = new coursecat_helper(); 88 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED); 89 $content .= $this->coursecat_coursebox($chelper, $course); 90 $content .= $this->output->box_end(); 91 return $content; 92 } 93 94 /** 95 * Renderers a structured array of courses and categories into a nice XHTML tree structure. 96 * 97 * @deprecated since 2.5 98 * 99 * Please see http://docs.moodle.org/dev/Courses_lists_upgrade_to_2.5 100 * 101 * @param array $ignored argument ignored 102 * @return string 103 */ 104 public final function course_category_tree(array $ignored) { 105 debugging('Function core_course_renderer::course_category_tree() is deprecated, please use frontpage_combo_list()', DEBUG_DEVELOPER); 106 return $this->frontpage_combo_list(); 107 } 108 109 /** 110 * Renderers a category for use with course_category_tree 111 * 112 * @deprecated since 2.5 113 * 114 * Please see http://docs.moodle.org/dev/Courses_lists_upgrade_to_2.5 115 * 116 * @param array $category 117 * @param int $depth 118 * @return string 119 */ 120 protected final function course_category_tree_category(stdClass $category, $depth=1) { 121 debugging('Function core_course_renderer::course_category_tree_category() is deprecated', DEBUG_DEVELOPER); 122 return ''; 123 } 124 125 /** 126 * Render a modchooser. 127 * 128 * @param renderable $modchooser The chooser. 129 * @return string 130 */ 131 public function render_modchooser(renderable $modchooser) { 132 return $this->render_from_template('core_course/modchooser', $modchooser->export_for_template($this)); 133 } 134 135 /** 136 * Build the HTML for the module chooser javascript popup 137 * 138 * @param array $modules A set of modules as returned form @see 139 * get_module_metadata 140 * @param object $course The course that will be displayed 141 * @return string The composed HTML for the module 142 */ 143 public function course_modchooser($modules, $course) { 144 debugging('course_modchooser() is deprecated. Please use course_activitychooser() instead.', DEBUG_DEVELOPER); 145 146 return $this->course_activitychooser($course->id); 147 } 148 149 /** 150 * Build the HTML for the module chooser javascript popup. 151 * 152 * @param int $courseid The course id to fetch modules for. 153 * @return string 154 */ 155 public function course_activitychooser($courseid) { 156 157 if (!$this->page->requires->should_create_one_time_item_now('core_course_modchooser')) { 158 return ''; 159 } 160 161 // Build an object of config settings that we can then hook into in the Activity Chooser. 162 $chooserconfig = (object) [ 163 'tabmode' => get_config('core', 'activitychoosertabmode'), 164 ]; 165 $this->page->requires->js_call_amd('core_course/activitychooser', 'init', [$courseid, $chooserconfig]); 166 167 return ''; 168 } 169 170 /** 171 * Build the HTML for a specified set of modules 172 * 173 * @param array $modules A set of modules as used by the 174 * course_modchooser_module function 175 * @return string The composed HTML for the module 176 */ 177 protected function course_modchooser_module_types($modules) { 178 debugging('Method core_course_renderer::course_modchooser_module_types() is deprecated, ' . 179 'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER); 180 return ''; 181 } 182 183 /** 184 * Return the HTML for the specified module adding any required classes 185 * 186 * @param object $module An object containing the title, and link. An 187 * icon, and help text may optionally be specified. If the module 188 * contains subtypes in the types option, then these will also be 189 * displayed. 190 * @param array $classes Additional classes to add to the encompassing 191 * div element 192 * @return string The composed HTML for the module 193 */ 194 protected function course_modchooser_module($module, $classes = array('option')) { 195 debugging('Method core_course_renderer::course_modchooser_module() is deprecated, ' . 196 'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER); 197 return ''; 198 } 199 200 protected function course_modchooser_title($title, $identifier = null) { 201 debugging('Method core_course_renderer::course_modchooser_title() is deprecated, ' . 202 'see core_course_renderer::render_modchooser().', DEBUG_DEVELOPER); 203 return ''; 204 } 205 206 /** 207 * Renders HTML for displaying the sequence of course module editing buttons 208 * 209 * @see course_get_cm_edit_actions() 210 * 211 * @param action_link[] $actions Array of action_link objects 212 * @param cm_info $mod The module we are displaying actions for. 213 * @param array $displayoptions additional display options: 214 * ownerselector => A JS/CSS selector that can be used to find an cm node. 215 * If specified the owning node will be given the class 'action-menu-shown' when the action 216 * menu is being displayed. 217 * constraintselector => A JS/CSS selector that can be used to find the parent node for which to constrain 218 * the action menu to when it is being displayed. 219 * donotenhance => If set to true the action menu that gets displayed won't be enhanced by JS. 220 * @return string 221 */ 222 public function course_section_cm_edit_actions($actions, cm_info $mod = null, $displayoptions = array()) { 223 global $CFG; 224 225 if (empty($actions)) { 226 return ''; 227 } 228 229 if (isset($displayoptions['ownerselector'])) { 230 $ownerselector = $displayoptions['ownerselector']; 231 } else if ($mod) { 232 $ownerselector = '#module-'.$mod->id; 233 } else { 234 debugging('You should upgrade your call to '.__FUNCTION__.' and provide $mod', DEBUG_DEVELOPER); 235 $ownerselector = 'li.activity'; 236 } 237 238 if (isset($displayoptions['constraintselector'])) { 239 $constraint = $displayoptions['constraintselector']; 240 } else { 241 $constraint = '.course-content'; 242 } 243 244 $menu = new action_menu(); 245 $menu->set_owner_selector($ownerselector); 246 $menu->set_constraint($constraint); 247 $menu->set_alignment(action_menu::TR, action_menu::BR); 248 $menu->set_menu_trigger(get_string('edit')); 249 250 foreach ($actions as $action) { 251 if ($action instanceof action_menu_link) { 252 $action->add_class('cm-edit-action'); 253 } 254 $menu->add($action); 255 } 256 $menu->attributes['class'] .= ' section-cm-edit-actions commands'; 257 258 // Prioritise the menu ahead of all other actions. 259 $menu->prioritise = true; 260 261 return $this->render($menu); 262 } 263 264 /** 265 * Renders HTML for the menus to add activities and resources to the current course 266 * 267 * @param stdClass $course 268 * @param int $section relative section number (field course_sections.section) 269 * @param int $sectionreturn The section to link back to 270 * @param array $displayoptions additional display options, for example blocks add 271 * option 'inblock' => true, suggesting to display controls vertically 272 * @return string 273 */ 274 function course_section_add_cm_control($course, $section, $sectionreturn = null, $displayoptions = array()) { 275 global $CFG, $USER; 276 277 // The returned control HTML can be one of the following: 278 // - Only the non-ajax control (select menus of activities and resources) with a noscript fallback for non js clients. 279 // - Only the ajax control (the link which when clicked produces the activity chooser modal). No noscript fallback. 280 // - [Behat only]: The non-ajax control and optionally the ajax control (depending on site settings). If included, the link 281 // takes priority and the non-ajax control is wrapped in a <noscript>. 282 // Behat requires the third case because some features run with JS, some do not. We must include the noscript fallback. 283 $behatsite = defined('BEHAT_SITE_RUNNING'); 284 $nonajaxcontrol = ''; 285 $ajaxcontrol = ''; 286 $courseajaxenabled = course_ajax_enabled($course); 287 $userchooserenabled = get_user_preferences('usemodchooser', $CFG->modchooserdefault); 288 289 // Decide what combination of controls to output: 290 // During behat runs, both controls can be used in conjunction to provide non-js fallback. 291 // During normal use only one control or the other will be output. No non-js fallback is needed. 292 $rendernonajaxcontrol = $behatsite || !$courseajaxenabled || !$userchooserenabled || $course->id != $this->page->course->id; 293 $renderajaxcontrol = $courseajaxenabled && $userchooserenabled && $course->id == $this->page->course->id; 294 295 // The non-ajax control, which includes an entirely non-js (<noscript>) fallback too. 296 if ($rendernonajaxcontrol) { 297 $vertical = !empty($displayoptions['inblock']); 298 299 // Check to see if user can add menus. 300 if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id)) 301 || !$this->page->user_is_editing()) { 302 return ''; 303 } 304 305 // Retrieve all modules with associated metadata. 306 $contentitemservice = \core_course\local\factory\content_item_service_factory::get_content_item_service(); 307 $urlparams = ['section' => $section]; 308 if (!is_null($sectionreturn)) { 309 $urlparams['sr'] = $sectionreturn; 310 } 311 $modules = $contentitemservice->get_content_items_for_user_in_course($USER, $course, $urlparams); 312 313 // Return if there are no content items to add. 314 if (empty($modules)) { 315 return ''; 316 } 317 318 // We'll sort resources and activities into two lists. 319 $activities = array(MOD_CLASS_ACTIVITY => array(), MOD_CLASS_RESOURCE => array()); 320 321 foreach ($modules as $module) { 322 $activityclass = MOD_CLASS_ACTIVITY; 323 if ($module->archetype == MOD_ARCHETYPE_RESOURCE) { 324 $activityclass = MOD_CLASS_RESOURCE; 325 } else if ($module->archetype === MOD_ARCHETYPE_SYSTEM) { 326 // System modules cannot be added by user, do not add to dropdown. 327 continue; 328 } 329 $link = $module->link; 330 $activities[$activityclass][$link] = $module->title; 331 } 332 333 $straddactivity = get_string('addactivity'); 334 $straddresource = get_string('addresource'); 335 $sectionname = get_section_name($course, $section); 336 $strresourcelabel = get_string('addresourcetosection', null, $sectionname); 337 $stractivitylabel = get_string('addactivitytosection', null, $sectionname); 338 339 $nonajaxcontrol = html_writer::start_tag('div', array('class' => 'section_add_menus', 'id' => 'add_menus-section-' 340 . $section)); 341 342 if (!$vertical) { 343 $nonajaxcontrol .= html_writer::start_tag('div', array('class' => 'horizontal')); 344 } 345 346 if (!empty($activities[MOD_CLASS_RESOURCE])) { 347 $select = new url_select($activities[MOD_CLASS_RESOURCE], '', array('' => $straddresource), "ressection$section"); 348 $select->set_help_icon('resources'); 349 $select->set_label($strresourcelabel, array('class' => 'accesshide')); 350 $nonajaxcontrol .= $this->output->render($select); 351 } 352 353 if (!empty($activities[MOD_CLASS_ACTIVITY])) { 354 $select = new url_select($activities[MOD_CLASS_ACTIVITY], '', array('' => $straddactivity), "section$section"); 355 $select->set_help_icon('activities'); 356 $select->set_label($stractivitylabel, array('class' => 'accesshide')); 357 $nonajaxcontrol .= $this->output->render($select); 358 } 359 360 if (!$vertical) { 361 $nonajaxcontrol .= html_writer::end_tag('div'); 362 } 363 364 $nonajaxcontrol .= html_writer::end_tag('div'); 365 } 366 367 // The ajax control - the 'Add an activity or resource' link. 368 if ($renderajaxcontrol) { 369 // The module chooser link. 370 $straddeither = get_string('addresourceoractivity'); 371 $ajaxcontrol = html_writer::start_tag('div', array('class' => 'mdl-right')); 372 $ajaxcontrol .= html_writer::start_tag('div', array('class' => 'section-modchooser')); 373 $icon = $this->output->pix_icon('t/add', ''); 374 $span = html_writer::tag('span', $straddeither, array('class' => 'section-modchooser-text')); 375 $ajaxcontrol .= html_writer::tag('button', $icon . $span, [ 376 'class' => 'section-modchooser-link btn btn-link', 377 'data-action' => 'open-chooser', 378 'data-sectionid' => $section, 379 'data-sectionreturnid' => $sectionreturn, 380 ] 381 ); 382 $ajaxcontrol .= html_writer::end_tag('div'); 383 $ajaxcontrol .= html_writer::end_tag('div'); 384 385 // Load the JS for the modal. 386 $this->course_activitychooser($course->id); 387 } 388 389 // Behat only: If both controls are being included in the HTML, 390 // show the link by default and only fall back to the selects if js is disabled. 391 if ($behatsite && $renderajaxcontrol) { 392 $nonajaxcontrol = html_writer::tag('div', $nonajaxcontrol, array('class' => 'hiddenifjs addresourcedropdown')); 393 $ajaxcontrol = html_writer::tag('div', $ajaxcontrol, array('class' => 'visibleifjs addresourcemodchooser')); 394 } 395 396 // If behat is running, we should have the non-ajax control + the ajax control. 397 // Otherwise, we'll have one or the other. 398 return $ajaxcontrol . $nonajaxcontrol; 399 } 400 401 /** 402 * Renders html to display a course search form 403 * 404 * @param string $value default value to populate the search field 405 * @return string 406 */ 407 public function course_search_form($value = '') { 408 409 $data = [ 410 'action' => \core_search\manager::get_course_search_url(), 411 'btnclass' => 'btn-primary', 412 'inputname' => 'q', 413 'searchstring' => get_string('searchcourses'), 414 'hiddenfields' => (object) ['name' => 'areaids', 'value' => 'core_course-course'], 415 'query' => $value 416 ]; 417 return $this->render_from_template('core/search_input', $data); 418 } 419 420 /** 421 * Renders html for completion box on course page 422 * 423 * If completion is disabled, returns empty string 424 * If completion is automatic, returns an icon of the current completion state 425 * If completion is manual, returns a form (with an icon inside) that allows user to 426 * toggle completion 427 * 428 * @param stdClass $course course object 429 * @param completion_info $completioninfo completion info for the course, it is recommended 430 * to fetch once for all modules in course/section for performance 431 * @param cm_info $mod module to show completion for 432 * @param array $displayoptions display options, not used in core 433 * @return string 434 */ 435 public function course_section_cm_completion($course, &$completioninfo, cm_info $mod, $displayoptions = array()) { 436 global $CFG, $DB, $USER; 437 $output = ''; 438 439 $istrackeduser = $completioninfo->is_tracked_user($USER->id); 440 $isediting = $this->page->user_is_editing(); 441 442 if (!empty($displayoptions['hidecompletion']) || !isloggedin() || isguestuser() || !$mod->uservisible) { 443 return $output; 444 } 445 if ($completioninfo === null) { 446 $completioninfo = new completion_info($course); 447 } 448 $completion = $completioninfo->is_enabled($mod); 449 450 if ($completion == COMPLETION_TRACKING_NONE) { 451 if ($isediting) { 452 $output .= html_writer::span(' ', 'filler'); 453 } 454 return $output; 455 } 456 457 $completionicon = ''; 458 459 if ($isediting || !$istrackeduser) { 460 switch ($completion) { 461 case COMPLETION_TRACKING_MANUAL : 462 $completionicon = 'manual-enabled'; break; 463 case COMPLETION_TRACKING_AUTOMATIC : 464 $completionicon = 'auto-enabled'; break; 465 } 466 } else { 467 $completiondata = $completioninfo->get_data($mod, true); 468 if ($completion == COMPLETION_TRACKING_MANUAL) { 469 switch($completiondata->completionstate) { 470 case COMPLETION_INCOMPLETE: 471 $completionicon = 'manual-n' . ($completiondata->overrideby ? '-override' : ''); 472 break; 473 case COMPLETION_COMPLETE: 474 $completionicon = 'manual-y' . ($completiondata->overrideby ? '-override' : ''); 475 break; 476 } 477 } else { // Automatic 478 switch($completiondata->completionstate) { 479 case COMPLETION_INCOMPLETE: 480 $completionicon = 'auto-n' . ($completiondata->overrideby ? '-override' : ''); 481 break; 482 case COMPLETION_COMPLETE: 483 $completionicon = 'auto-y' . ($completiondata->overrideby ? '-override' : ''); 484 break; 485 case COMPLETION_COMPLETE_PASS: 486 $completionicon = 'auto-pass'; break; 487 case COMPLETION_COMPLETE_FAIL: 488 $completionicon = 'auto-fail'; break; 489 } 490 } 491 } 492 if ($completionicon) { 493 $formattedname = html_entity_decode($mod->get_formatted_name(), ENT_QUOTES, 'UTF-8'); 494 if (!$isediting && $istrackeduser && $completiondata->overrideby) { 495 $args = new stdClass(); 496 $args->modname = $formattedname; 497 $overridebyuser = \core_user::get_user($completiondata->overrideby, '*', MUST_EXIST); 498 $args->overrideuser = fullname($overridebyuser); 499 $imgalt = get_string('completion-alt-' . $completionicon, 'completion', $args); 500 } else { 501 $imgalt = get_string('completion-alt-' . $completionicon, 'completion', $formattedname); 502 } 503 504 if ($isediting || !$istrackeduser || !has_capability('moodle/course:togglecompletion', $mod->context)) { 505 // When editing, the icon is just an image. 506 $completionpixicon = new pix_icon('i/completion-'.$completionicon, $imgalt, '', 507 array('title' => $imgalt, 'class' => 'iconsmall')); 508 $output .= html_writer::tag('span', $this->output->render($completionpixicon), 509 array('class' => 'autocompletion')); 510 } else if ($completion == COMPLETION_TRACKING_MANUAL) { 511 $newstate = 512 $completiondata->completionstate == COMPLETION_COMPLETE 513 ? COMPLETION_INCOMPLETE 514 : COMPLETION_COMPLETE; 515 // In manual mode the icon is a toggle form... 516 517 // If this completion state is used by the 518 // conditional activities system, we need to turn 519 // off the JS. 520 $extraclass = ''; 521 if (!empty($CFG->enableavailability) && 522 core_availability\info::completion_value_used($course, $mod->id)) { 523 $extraclass = ' preventjs'; 524 } 525 $output .= html_writer::start_tag('form', array('method' => 'post', 526 'action' => new moodle_url('/course/togglecompletion.php'), 527 'class' => 'togglecompletion'. $extraclass)); 528 $output .= html_writer::start_tag('div'); 529 $output .= html_writer::empty_tag('input', array( 530 'type' => 'hidden', 'name' => 'id', 'value' => $mod->id)); 531 $output .= html_writer::empty_tag('input', array( 532 'type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())); 533 $output .= html_writer::empty_tag('input', array( 534 'type' => 'hidden', 'name' => 'modulename', 'value' => $formattedname)); 535 $output .= html_writer::empty_tag('input', array( 536 'type' => 'hidden', 'name' => 'completionstate', 'value' => $newstate)); 537 $output .= html_writer::tag('button', 538 $this->output->pix_icon('i/completion-' . $completionicon, $imgalt), 539 array('class' => 'btn btn-link', 'aria-live' => 'assertive')); 540 $output .= html_writer::end_tag('div'); 541 $output .= html_writer::end_tag('form'); 542 } else { 543 // In auto mode, the icon is just an image. 544 $completionpixicon = new pix_icon('i/completion-'.$completionicon, $imgalt, '', 545 array('title' => $imgalt)); 546 $output .= html_writer::tag('span', $this->output->render($completionpixicon), 547 array('class' => 'autocompletion')); 548 } 549 } 550 return $output; 551 } 552 553 /** 554 * Checks if course module has any conditions that may make it unavailable for 555 * all or some of the students 556 * 557 * This function is internal and is only used to create CSS classes for the module name/text 558 * 559 * @param cm_info $mod 560 * @return bool 561 */ 562 protected function is_cm_conditionally_hidden(cm_info $mod) { 563 global $CFG; 564 $conditionalhidden = false; 565 if (!empty($CFG->enableavailability)) { 566 $info = new \core_availability\info_module($mod); 567 $conditionalhidden = !$info->is_available_for_all(); 568 } 569 return $conditionalhidden; 570 } 571 572 /** 573 * Renders html to display a name with the link to the course module on a course page 574 * 575 * If module is unavailable for user but still needs to be displayed 576 * in the list, just the name is returned without a link 577 * 578 * Note, that for course modules that never have separate pages (i.e. labels) 579 * this function return an empty string 580 * 581 * @param cm_info $mod 582 * @param array $displayoptions 583 * @return string 584 */ 585 public function course_section_cm_name(cm_info $mod, $displayoptions = array()) { 586 if (!$mod->is_visible_on_course_page() || !$mod->url) { 587 // Nothing to be displayed to the user. 588 return ''; 589 } 590 591 list($linkclasses, $textclasses) = $this->course_section_cm_classes($mod); 592 $groupinglabel = $mod->get_grouping_label($textclasses); 593 594 // Render element that allows to edit activity name inline. It calls {@link course_section_cm_name_title()} 595 // to get the display title of the activity. 596 $tmpl = new \core_course\output\course_module_name($mod, $this->page->user_is_editing(), $displayoptions); 597 return $this->output->render_from_template('core/inplace_editable', $tmpl->export_for_template($this->output)) . 598 $groupinglabel; 599 } 600 601 /** 602 * Returns the CSS classes for the activity name/content 603 * 604 * For items which are hidden, unavailable or stealth but should be displayed 605 * to current user ($mod->is_visible_on_course_page()), we show those as dimmed. 606 * Students will also see as dimmed activities names that are not yet available 607 * but should still be displayed (without link) with availability info. 608 * 609 * @param cm_info $mod 610 * @return array array of two elements ($linkclasses, $textclasses) 611 */ 612 protected function course_section_cm_classes(cm_info $mod) { 613 $linkclasses = ''; 614 $textclasses = ''; 615 if ($mod->uservisible) { 616 $conditionalhidden = $this->is_cm_conditionally_hidden($mod); 617 $accessiblebutdim = (!$mod->visible || $conditionalhidden) && 618 has_capability('moodle/course:viewhiddenactivities', $mod->context); 619 if ($accessiblebutdim) { 620 $linkclasses .= ' dimmed'; 621 $textclasses .= ' dimmed_text'; 622 if ($conditionalhidden) { 623 $linkclasses .= ' conditionalhidden'; 624 $textclasses .= ' conditionalhidden'; 625 } 626 } 627 if ($mod->is_stealth()) { 628 // Stealth activity is the one that is not visible on course page. 629 // It still may be displayed to the users who can manage it. 630 $linkclasses .= ' stealth'; 631 $textclasses .= ' stealth'; 632 } 633 } else { 634 $linkclasses .= ' dimmed'; 635 $textclasses .= ' dimmed dimmed_text'; 636 } 637 return array($linkclasses, $textclasses); 638 } 639 640 /** 641 * Renders html to display a name with the link to the course module on a course page 642 * 643 * If module is unavailable for user but still needs to be displayed 644 * in the list, just the name is returned without a link 645 * 646 * Note, that for course modules that never have separate pages (i.e. labels) 647 * this function return an empty string 648 * 649 * @param cm_info $mod 650 * @param array $displayoptions 651 * @return string 652 */ 653 public function course_section_cm_name_title(cm_info $mod, $displayoptions = array()) { 654 $output = ''; 655 $url = $mod->url; 656 if (!$mod->is_visible_on_course_page() || !$url) { 657 // Nothing to be displayed to the user. 658 return $output; 659 } 660 661 //Accessibility: for files get description via icon, this is very ugly hack! 662 $instancename = $mod->get_formatted_name(); 663 $altname = $mod->modfullname; 664 // Avoid unnecessary duplication: if e.g. a forum name already 665 // includes the word forum (or Forum, etc) then it is unhelpful 666 // to include that in the accessible description that is added. 667 if (false !== strpos(core_text::strtolower($instancename), 668 core_text::strtolower($altname))) { 669 $altname = ''; 670 } 671 // File type after name, for alphabetic lists (screen reader). 672 if ($altname) { 673 $altname = get_accesshide(' '.$altname); 674 } 675 676 list($linkclasses, $textclasses) = $this->course_section_cm_classes($mod); 677 678 // Get on-click attribute value if specified and decode the onclick - it 679 // has already been encoded for display (puke). 680 $onclick = htmlspecialchars_decode($mod->onclick, ENT_QUOTES); 681 682 // Display link itself. 683 $activitylink = html_writer::empty_tag('img', array('src' => $mod->get_icon_url(), 684 'class' => 'iconlarge activityicon', 'alt' => '', 'role' => 'presentation', 'aria-hidden' => 'true')) . 685 html_writer::tag('span', $instancename . $altname, array('class' => 'instancename')); 686 if ($mod->uservisible) { 687 $output .= html_writer::link($url, $activitylink, array('class' => 'aalink' . $linkclasses, 'onclick' => $onclick)); 688 } else { 689 // We may be displaying this just in order to show information 690 // about visibility, without the actual link ($mod->is_visible_on_course_page()). 691 $output .= html_writer::tag('div', $activitylink, array('class' => $textclasses)); 692 } 693 return $output; 694 } 695 696 /** 697 * Renders html to display the module content on the course page (i.e. text of the labels) 698 * 699 * @param cm_info $mod 700 * @param array $displayoptions 701 * @return string 702 */ 703 public function course_section_cm_text(cm_info $mod, $displayoptions = array()) { 704 $output = ''; 705 if (!$mod->is_visible_on_course_page()) { 706 // nothing to be displayed to the user 707 return $output; 708 } 709 $content = $mod->get_formatted_content(array('overflowdiv' => true, 'noclean' => true)); 710 list($linkclasses, $textclasses) = $this->course_section_cm_classes($mod); 711 if ($mod->url && $mod->uservisible) { 712 if ($content) { 713 // If specified, display extra content after link. 714 $output = html_writer::tag('div', $content, array('class' => 715 trim('contentafterlink ' . $textclasses))); 716 } 717 } else { 718 $groupinglabel = $mod->get_grouping_label($textclasses); 719 720 // No link, so display only content. 721 $output = html_writer::tag('div', $content . $groupinglabel, 722 array('class' => 'contentwithoutlink ' . $textclasses)); 723 } 724 return $output; 725 } 726 727 /** 728 * Displays availability info for a course section or course module 729 * 730 * @param string $text 731 * @param string $additionalclasses 732 * @return string 733 */ 734 public function availability_info($text, $additionalclasses = '') { 735 736 $data = ['text' => $text, 'classes' => $additionalclasses]; 737 $additionalclasses = array_filter(explode(' ', $additionalclasses)); 738 739 if (in_array('ishidden', $additionalclasses)) { 740 $data['ishidden'] = 1; 741 742 } else if (in_array('isstealth', $additionalclasses)) { 743 $data['isstealth'] = 1; 744 745 } else if (in_array('isrestricted', $additionalclasses)) { 746 $data['isrestricted'] = 1; 747 748 if (in_array('isfullinfo', $additionalclasses)) { 749 $data['isfullinfo'] = 1; 750 } 751 } 752 753 return $this->render_from_template('core/availability_info', $data); 754 } 755 756 /** 757 * Renders HTML to show course module availability information (for someone who isn't allowed 758 * to see the activity itself, or for staff) 759 * 760 * @param cm_info $mod 761 * @param array $displayoptions 762 * @return string 763 */ 764 public function course_section_cm_availability(cm_info $mod, $displayoptions = array()) { 765 global $CFG; 766 $output = ''; 767 if (!$mod->is_visible_on_course_page()) { 768 return $output; 769 } 770 if (!$mod->uservisible) { 771 // this is a student who is not allowed to see the module but might be allowed 772 // to see availability info (i.e. "Available from ...") 773 if (!empty($mod->availableinfo)) { 774 $formattedinfo = \core_availability\info::format_info( 775 $mod->availableinfo, $mod->get_course()); 776 $output = $this->availability_info($formattedinfo, 'isrestricted'); 777 } 778 return $output; 779 } 780 // this is a teacher who is allowed to see module but still should see the 781 // information that module is not available to all/some students 782 $modcontext = context_module::instance($mod->id); 783 $canviewhidden = has_capability('moodle/course:viewhiddenactivities', $modcontext); 784 if ($canviewhidden && !$mod->visible) { 785 // This module is hidden but current user has capability to see it. 786 // Do not display the availability info if the whole section is hidden. 787 if ($mod->get_section_info()->visible) { 788 $output .= $this->availability_info(get_string('hiddenfromstudents'), 'ishidden'); 789 } 790 } else if ($mod->is_stealth()) { 791 // This module is available but is normally not displayed on the course page 792 // (this user can see it because they can manage it). 793 $output .= $this->availability_info(get_string('hiddenoncoursepage'), 'isstealth'); 794 } 795 if ($canviewhidden && !empty($CFG->enableavailability)) { 796 // Display information about conditional availability. 797 // Don't add availability information if user is not editing and activity is hidden. 798 if ($mod->visible || $this->page->user_is_editing()) { 799 $hidinfoclass = 'isrestricted isfullinfo'; 800 if (!$mod->visible) { 801 $hidinfoclass .= ' hide'; 802 } 803 $ci = new \core_availability\info_module($mod); 804 $fullinfo = $ci->get_full_information(); 805 if ($fullinfo) { 806 $formattedinfo = \core_availability\info::format_info( 807 $fullinfo, $mod->get_course()); 808 $output .= $this->availability_info($formattedinfo, $hidinfoclass); 809 } 810 } 811 } 812 return $output; 813 } 814 815 /** 816 * Renders HTML to display one course module for display within a section. 817 * 818 * This function calls: 819 * {@link core_course_renderer::course_section_cm()} 820 * 821 * @param stdClass $course 822 * @param completion_info $completioninfo 823 * @param cm_info $mod 824 * @param int|null $sectionreturn 825 * @param array $displayoptions 826 * @return String 827 */ 828 public function course_section_cm_list_item($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = array()) { 829 $output = ''; 830 if ($modulehtml = $this->course_section_cm($course, $completioninfo, $mod, $sectionreturn, $displayoptions)) { 831 $modclasses = 'activity ' . $mod->modname . ' modtype_' . $mod->modname . ' ' . $mod->extraclasses; 832 $output .= html_writer::tag('li', $modulehtml, array('class' => $modclasses, 'id' => 'module-' . $mod->id)); 833 } 834 return $output; 835 } 836 837 /** 838 * Renders HTML to display one course module in a course section 839 * 840 * This includes link, content, availability, completion info and additional information 841 * that module type wants to display (i.e. number of unread forum posts) 842 * 843 * This function calls: 844 * {@link core_course_renderer::course_section_cm_name()} 845 * {@link core_course_renderer::course_section_cm_text()} 846 * {@link core_course_renderer::course_section_cm_availability()} 847 * {@link core_course_renderer::course_section_cm_completion()} 848 * {@link course_get_cm_edit_actions()} 849 * {@link core_course_renderer::course_section_cm_edit_actions()} 850 * 851 * @param stdClass $course 852 * @param completion_info $completioninfo 853 * @param cm_info $mod 854 * @param int|null $sectionreturn 855 * @param array $displayoptions 856 * @return string 857 */ 858 public function course_section_cm($course, &$completioninfo, cm_info $mod, $sectionreturn, $displayoptions = array()) { 859 $output = ''; 860 // We return empty string (because course module will not be displayed at all) 861 // if: 862 // 1) The activity is not visible to users 863 // and 864 // 2) The 'availableinfo' is empty, i.e. the activity was 865 // hidden in a way that leaves no info, such as using the 866 // eye icon. 867 if (!$mod->is_visible_on_course_page()) { 868 return $output; 869 } 870 871 $indentclasses = 'mod-indent'; 872 if (!empty($mod->indent)) { 873 $indentclasses .= ' mod-indent-'.$mod->indent; 874 if ($mod->indent > 15) { 875 $indentclasses .= ' mod-indent-huge'; 876 } 877 } 878 879 $output .= html_writer::start_tag('div'); 880 881 if ($this->page->user_is_editing()) { 882 $output .= course_get_cm_move($mod, $sectionreturn); 883 } 884 885 $output .= html_writer::start_tag('div', array('class' => 'mod-indent-outer w-100')); 886 887 // This div is used to indent the content. 888 $output .= html_writer::div('', $indentclasses); 889 890 // Start a wrapper for the actual content to keep the indentation consistent 891 $output .= html_writer::start_tag('div'); 892 893 // Display the link to the module (or do nothing if module has no url) 894 $cmname = $this->course_section_cm_name($mod, $displayoptions); 895 896 if (!empty($cmname)) { 897 // Start the div for the activity title, excluding the edit icons. 898 $output .= html_writer::start_tag('div', array('class' => 'activityinstance')); 899 $output .= $cmname; 900 901 902 // Module can put text after the link (e.g. forum unread) 903 $output .= $mod->afterlink; 904 905 // Closing the tag which contains everything but edit icons. Content part of the module should not be part of this. 906 $output .= html_writer::end_tag('div'); // .activityinstance 907 } 908 909 // If there is content but NO link (eg label), then display the 910 // content here (BEFORE any icons). In this case cons must be 911 // displayed after the content so that it makes more sense visually 912 // and for accessibility reasons, e.g. if you have a one-line label 913 // it should work similarly (at least in terms of ordering) to an 914 // activity. 915 $contentpart = $this->course_section_cm_text($mod, $displayoptions); 916 $url = $mod->url; 917 if (empty($url)) { 918 $output .= $contentpart; 919 } 920 921 $modicons = ''; 922 if ($this->page->user_is_editing()) { 923 $editactions = course_get_cm_edit_actions($mod, $mod->indent, $sectionreturn); 924 $modicons .= ' '. $this->course_section_cm_edit_actions($editactions, $mod, $displayoptions); 925 $modicons .= $mod->afterediticons; 926 } 927 928 $modicons .= $this->course_section_cm_completion($course, $completioninfo, $mod, $displayoptions); 929 930 if (!empty($modicons)) { 931 $output .= html_writer::div($modicons, 'actions'); 932 } 933 934 // Show availability info (if module is not available). 935 $output .= $this->course_section_cm_availability($mod, $displayoptions); 936 937 // If there is content AND a link, then display the content here 938 // (AFTER any icons). Otherwise it was displayed before 939 if (!empty($url)) { 940 $output .= $contentpart; 941 } 942 943 $output .= html_writer::end_tag('div'); // $indentclasses 944 945 // End of indentation div. 946 $output .= html_writer::end_tag('div'); 947 948 $output .= html_writer::end_tag('div'); 949 return $output; 950 } 951 952 /** 953 * Message displayed to the user when they try to access unavailable activity following URL 954 * 955 * This method is a very simplified version of {@link course_section_cm()} to be part of the error 956 * notification only. It also does not check if module is visible on course page or not. 957 * 958 * The message will be displayed inside notification! 959 * 960 * @param cm_info $cm 961 * @return string 962 */ 963 public function course_section_cm_unavailable_error_message(cm_info $cm) { 964 if ($cm->uservisible) { 965 return null; 966 } 967 if (!$cm->availableinfo) { 968 return get_string('activityiscurrentlyhidden'); 969 } 970 971 $altname = get_accesshide(' ' . $cm->modfullname); 972 $name = html_writer::empty_tag('img', array('src' => $cm->get_icon_url(), 973 'class' => 'iconlarge activityicon', 'alt' => ' ', 'role' => 'presentation')) . 974 html_writer::tag('span', ' '.$cm->get_formatted_name() . $altname, array('class' => 'instancename')); 975 $formattedinfo = \core_availability\info::format_info($cm->availableinfo, $cm->get_course()); 976 return html_writer::div($name, 'activityinstance-error') . 977 html_writer::div($formattedinfo, 'availabilityinfo-error'); 978 } 979 980 /** 981 * Renders HTML to display a list of course modules in a course section 982 * Also displays "move here" controls in Javascript-disabled mode 983 * 984 * This function calls {@link core_course_renderer::course_section_cm()} 985 * 986 * @param stdClass $course course object 987 * @param int|stdClass|section_info $section relative section number or section object 988 * @param int $sectionreturn section number to return to 989 * @param int $displayoptions 990 * @return void 991 */ 992 public function course_section_cm_list($course, $section, $sectionreturn = null, $displayoptions = array()) { 993 global $USER; 994 995 $output = ''; 996 $modinfo = get_fast_modinfo($course); 997 if (is_object($section)) { 998 $section = $modinfo->get_section_info($section->section); 999 } else { 1000 $section = $modinfo->get_section_info($section); 1001 } 1002 $completioninfo = new completion_info($course); 1003 1004 // check if we are currently in the process of moving a module with JavaScript disabled 1005 $ismoving = $this->page->user_is_editing() && ismoving($course->id); 1006 if ($ismoving) { 1007 $strmovefull = strip_tags(get_string("movefull", "", "'$USER->activitycopyname'")); 1008 } 1009 1010 // Get the list of modules visible to user (excluding the module being moved if there is one) 1011 $moduleshtml = array(); 1012 if (!empty($modinfo->sections[$section->section])) { 1013 foreach ($modinfo->sections[$section->section] as $modnumber) { 1014 $mod = $modinfo->cms[$modnumber]; 1015 1016 if ($ismoving and $mod->id == $USER->activitycopy) { 1017 // do not display moving mod 1018 continue; 1019 } 1020 1021 if ($modulehtml = $this->course_section_cm_list_item($course, 1022 $completioninfo, $mod, $sectionreturn, $displayoptions)) { 1023 $moduleshtml[$modnumber] = $modulehtml; 1024 } 1025 } 1026 } 1027 1028 $sectionoutput = ''; 1029 if (!empty($moduleshtml) || $ismoving) { 1030 foreach ($moduleshtml as $modnumber => $modulehtml) { 1031 if ($ismoving) { 1032 $movingurl = new moodle_url('/course/mod.php', array('moveto' => $modnumber, 'sesskey' => sesskey())); 1033 $sectionoutput .= html_writer::tag('li', 1034 html_writer::link($movingurl, '', array('title' => $strmovefull, 'class' => 'movehere')), 1035 array('class' => 'movehere')); 1036 } 1037 1038 $sectionoutput .= $modulehtml; 1039 } 1040 1041 if ($ismoving) { 1042 $movingurl = new moodle_url('/course/mod.php', array('movetosection' => $section->id, 'sesskey' => sesskey())); 1043 $sectionoutput .= html_writer::tag('li', 1044 html_writer::link($movingurl, '', array('title' => $strmovefull, 'class' => 'movehere')), 1045 array('class' => 'movehere')); 1046 } 1047 } 1048 1049 // Always output the section module list. 1050 $output .= html_writer::tag('ul', $sectionoutput, array('class' => 'section img-text')); 1051 1052 return $output; 1053 } 1054 1055 /** 1056 * Displays a custom list of courses with paging bar if necessary 1057 * 1058 * If $paginationurl is specified but $totalcount is not, the link 'View more' 1059 * appears under the list. 1060 * 1061 * If both $paginationurl and $totalcount are specified, and $totalcount is 1062 * bigger than count($courses), a paging bar is displayed above and under the 1063 * courses list. 1064 * 1065 * @param array $courses array of course records (or instances of core_course_list_element) to show on this page 1066 * @param bool $showcategoryname whether to add category name to the course description 1067 * @param string $additionalclasses additional CSS classes to add to the div.courses 1068 * @param moodle_url $paginationurl url to view more or url to form links to the other pages in paging bar 1069 * @param int $totalcount total number of courses on all pages, if omitted $paginationurl will be displayed as 'View more' link 1070 * @param int $page current page number (defaults to 0 referring to the first page) 1071 * @param int $perpage number of records per page (defaults to $CFG->coursesperpage) 1072 * @return string 1073 */ 1074 public function courses_list($courses, $showcategoryname = false, $additionalclasses = null, $paginationurl = null, $totalcount = null, $page = 0, $perpage = null) { 1075 global $CFG; 1076 // create instance of coursecat_helper to pass display options to function rendering courses list 1077 $chelper = new coursecat_helper(); 1078 if ($showcategoryname) { 1079 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT); 1080 } else { 1081 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED); 1082 } 1083 if ($totalcount !== null && $paginationurl !== null) { 1084 // add options to display pagination 1085 if ($perpage === null) { 1086 $perpage = $CFG->coursesperpage; 1087 } 1088 $chelper->set_courses_display_options(array( 1089 'limit' => $perpage, 1090 'offset' => ((int)$page) * $perpage, 1091 'paginationurl' => $paginationurl, 1092 )); 1093 } else if ($paginationurl !== null) { 1094 // add options to display 'View more' link 1095 $chelper->set_courses_display_options(array('viewmoreurl' => $paginationurl)); 1096 $totalcount = count($courses) + 1; // has to be bigger than count($courses) otherwise link will not be displayed 1097 } 1098 $chelper->set_attributes(array('class' => $additionalclasses)); 1099 $content = $this->coursecat_courses($chelper, $courses, $totalcount); 1100 return $content; 1101 } 1102 1103 /** 1104 * Returns HTML to display course name. 1105 * 1106 * @param coursecat_helper $chelper 1107 * @param core_course_list_element $course 1108 * @return string 1109 */ 1110 protected function course_name(coursecat_helper $chelper, core_course_list_element $course): string { 1111 $content = ''; 1112 if ($chelper->get_show_courses() >= self::COURSECAT_SHOW_COURSES_EXPANDED) { 1113 $nametag = 'h3'; 1114 } else { 1115 $nametag = 'div'; 1116 } 1117 $coursename = $chelper->get_course_formatted_name($course); 1118 $coursenamelink = html_writer::link(new moodle_url('/course/view.php', ['id' => $course->id]), 1119 $coursename, ['class' => $course->visible ? 'aalink' : 'aalink dimmed']); 1120 $content .= html_writer::tag($nametag, $coursenamelink, ['class' => 'coursename']); 1121 // If we display course in collapsed form but the course has summary or course contacts, display the link to the info page. 1122 $content .= html_writer::start_tag('div', ['class' => 'moreinfo']); 1123 if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) { 1124 if ($course->has_summary() || $course->has_course_contacts() || $course->has_course_overviewfiles() 1125 || $course->has_custom_fields()) { 1126 $url = new moodle_url('/course/info.php', ['id' => $course->id]); 1127 $image = $this->output->pix_icon('i/info', $this->strings->summary); 1128 $content .= html_writer::link($url, $image, ['title' => $this->strings->summary]); 1129 // Make sure JS file to expand course content is included. 1130 $this->coursecat_include_js(); 1131 } 1132 } 1133 $content .= html_writer::end_tag('div'); 1134 return $content; 1135 } 1136 1137 /** 1138 * Returns HTML to display course enrolment icons. 1139 * 1140 * @param core_course_list_element $course 1141 * @return string 1142 */ 1143 protected function course_enrolment_icons(core_course_list_element $course): string { 1144 $content = ''; 1145 if ($icons = enrol_get_course_info_icons($course)) { 1146 $content .= html_writer::start_tag('div', ['class' => 'enrolmenticons']); 1147 foreach ($icons as $icon) { 1148 $content .= $this->render($icon); 1149 } 1150 $content .= html_writer::end_tag('div'); 1151 } 1152 return $content; 1153 } 1154 1155 /** 1156 * Displays one course in the list of courses. 1157 * 1158 * This is an internal function, to display an information about just one course 1159 * please use {@link core_course_renderer::course_info_box()} 1160 * 1161 * @param coursecat_helper $chelper various display options 1162 * @param core_course_list_element|stdClass $course 1163 * @param string $additionalclasses additional classes to add to the main <div> tag (usually 1164 * depend on the course position in list - first/last/even/odd) 1165 * @return string 1166 */ 1167 protected function coursecat_coursebox(coursecat_helper $chelper, $course, $additionalclasses = '') { 1168 if (!isset($this->strings->summary)) { 1169 $this->strings->summary = get_string('summary'); 1170 } 1171 if ($chelper->get_show_courses() <= self::COURSECAT_SHOW_COURSES_COUNT) { 1172 return ''; 1173 } 1174 if ($course instanceof stdClass) { 1175 $course = new core_course_list_element($course); 1176 } 1177 $content = ''; 1178 $classes = trim('coursebox clearfix '. $additionalclasses); 1179 if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) { 1180 $classes .= ' collapsed'; 1181 } 1182 1183 // .coursebox 1184 $content .= html_writer::start_tag('div', array( 1185 'class' => $classes, 1186 'data-courseid' => $course->id, 1187 'data-type' => self::COURSECAT_TYPE_COURSE, 1188 )); 1189 1190 $content .= html_writer::start_tag('div', array('class' => 'info')); 1191 $content .= $this->course_name($chelper, $course); 1192 $content .= $this->course_enrolment_icons($course); 1193 $content .= html_writer::end_tag('div'); 1194 1195 $content .= html_writer::start_tag('div', array('class' => 'content')); 1196 $content .= $this->coursecat_coursebox_content($chelper, $course); 1197 $content .= html_writer::end_tag('div'); 1198 1199 $content .= html_writer::end_tag('div'); // .coursebox 1200 return $content; 1201 } 1202 1203 /** 1204 * Returns HTML to display course summary. 1205 * 1206 * @param coursecat_helper $chelper 1207 * @param core_course_list_element $course 1208 * @return string 1209 */ 1210 protected function course_summary(coursecat_helper $chelper, core_course_list_element $course): string { 1211 $content = ''; 1212 if ($course->has_summary()) { 1213 $content .= html_writer::start_tag('div', ['class' => 'summary']); 1214 $content .= $chelper->get_course_formatted_summary($course, 1215 array('overflowdiv' => true, 'noclean' => true, 'para' => false)); 1216 $content .= html_writer::end_tag('div'); 1217 } 1218 return $content; 1219 } 1220 1221 /** 1222 * Returns HTML to display course contacts. 1223 * 1224 * @param core_course_list_element $course 1225 * @return string 1226 */ 1227 protected function course_contacts(core_course_list_element $course) { 1228 $content = ''; 1229 if ($course->has_course_contacts()) { 1230 $content .= html_writer::start_tag('ul', ['class' => 'teachers']); 1231 foreach ($course->get_course_contacts() as $coursecontact) { 1232 $rolenames = array_map(function ($role) { 1233 return $role->displayname; 1234 }, $coursecontact['roles']); 1235 $name = implode(", ", $rolenames).': '. 1236 html_writer::link(new moodle_url('/user/view.php', 1237 ['id' => $coursecontact['user']->id, 'course' => SITEID]), 1238 $coursecontact['username']); 1239 $content .= html_writer::tag('li', $name); 1240 } 1241 $content .= html_writer::end_tag('ul'); 1242 } 1243 return $content; 1244 } 1245 1246 /** 1247 * Returns HTML to display course overview files. 1248 * 1249 * @param core_course_list_element $course 1250 * @return string 1251 */ 1252 protected function course_overview_files(core_course_list_element $course): string { 1253 global $CFG; 1254 1255 $contentimages = $contentfiles = ''; 1256 foreach ($course->get_course_overviewfiles() as $file) { 1257 $isimage = $file->is_valid_image(); 1258 $url = moodle_url::make_file_url("$CFG->wwwroot/pluginfile.php", 1259 '/' . $file->get_contextid() . '/' . $file->get_component() . '/' . 1260 $file->get_filearea() . $file->get_filepath() . $file->get_filename(), !$isimage); 1261 if ($isimage) { 1262 $contentimages .= html_writer::tag('div', 1263 html_writer::empty_tag('img', ['src' => $url]), 1264 ['class' => 'courseimage']); 1265 } else { 1266 $image = $this->output->pix_icon(file_file_icon($file, 24), $file->get_filename(), 'moodle'); 1267 $filename = html_writer::tag('span', $image, ['class' => 'fp-icon']). 1268 html_writer::tag('span', $file->get_filename(), ['class' => 'fp-filename']); 1269 $contentfiles .= html_writer::tag('span', 1270 html_writer::link($url, $filename), 1271 ['class' => 'coursefile fp-filename-icon']); 1272 } 1273 } 1274 return $contentimages . $contentfiles; 1275 } 1276 1277 /** 1278 * Returns HTML to display course category name. 1279 * 1280 * @param coursecat_helper $chelper 1281 * @param core_course_list_element $course 1282 * @return string 1283 */ 1284 protected function course_category_name(coursecat_helper $chelper, core_course_list_element $course): string { 1285 $content = ''; 1286 // Display course category if necessary (for example in search results). 1287 if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT) { 1288 if ($cat = core_course_category::get($course->category, IGNORE_MISSING)) { 1289 $content .= html_writer::start_tag('div', ['class' => 'coursecat']); 1290 $content .= get_string('category').': '. 1291 html_writer::link(new moodle_url('/course/index.php', ['categoryid' => $cat->id]), 1292 $cat->get_formatted_name(), ['class' => $cat->visible ? '' : 'dimmed']); 1293 $content .= html_writer::end_tag('div'); 1294 } 1295 } 1296 return $content; 1297 } 1298 1299 /** 1300 * Returns HTML to display course custom fields. 1301 * 1302 * @param core_course_list_element $course 1303 * @return string 1304 */ 1305 protected function course_custom_fields(core_course_list_element $course): string { 1306 $content = ''; 1307 if ($course->has_custom_fields()) { 1308 $handler = core_course\customfield\course_handler::create(); 1309 $customfields = $handler->display_custom_fields_data($course->get_custom_fields()); 1310 $content .= \html_writer::tag('div', $customfields, ['class' => 'customfields-container']); 1311 } 1312 return $content; 1313 } 1314 1315 /** 1316 * Returns HTML to display course content (summary, course contacts and optionally category name) 1317 * 1318 * This method is called from coursecat_coursebox() and may be re-used in AJAX 1319 * 1320 * @param coursecat_helper $chelper various display options 1321 * @param stdClass|core_course_list_element $course 1322 * @return string 1323 */ 1324 protected function coursecat_coursebox_content(coursecat_helper $chelper, $course) { 1325 if ($chelper->get_show_courses() < self::COURSECAT_SHOW_COURSES_EXPANDED) { 1326 return ''; 1327 } 1328 if ($course instanceof stdClass) { 1329 $course = new core_course_list_element($course); 1330 } 1331 $content = $this->course_summary($chelper, $course); 1332 $content .= $this->course_overview_files($course); 1333 $content .= $this->course_contacts($course); 1334 $content .= $this->course_category_name($chelper, $course); 1335 $content .= $this->course_custom_fields($course); 1336 return $content; 1337 } 1338 1339 /** 1340 * Renders the list of courses 1341 * 1342 * This is internal function, please use {@link core_course_renderer::courses_list()} or another public 1343 * method from outside of the class 1344 * 1345 * If list of courses is specified in $courses; the argument $chelper is only used 1346 * to retrieve display options and attributes, only methods get_show_courses(), 1347 * get_courses_display_option() and get_and_erase_attributes() are called. 1348 * 1349 * @param coursecat_helper $chelper various display options 1350 * @param array $courses the list of courses to display 1351 * @param int|null $totalcount total number of courses (affects display mode if it is AUTO or pagination if applicable), 1352 * defaulted to count($courses) 1353 * @return string 1354 */ 1355 protected function coursecat_courses(coursecat_helper $chelper, $courses, $totalcount = null) { 1356 global $CFG; 1357 if ($totalcount === null) { 1358 $totalcount = count($courses); 1359 } 1360 if (!$totalcount) { 1361 // Courses count is cached during courses retrieval. 1362 return ''; 1363 } 1364 1365 if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_AUTO) { 1366 // In 'auto' course display mode we analyse if number of courses is more or less than $CFG->courseswithsummarieslimit 1367 if ($totalcount <= $CFG->courseswithsummarieslimit) { 1368 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED); 1369 } else { 1370 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_COLLAPSED); 1371 } 1372 } 1373 1374 // prepare content of paging bar if it is needed 1375 $paginationurl = $chelper->get_courses_display_option('paginationurl'); 1376 $paginationallowall = $chelper->get_courses_display_option('paginationallowall'); 1377 if ($totalcount > count($courses)) { 1378 // there are more results that can fit on one page 1379 if ($paginationurl) { 1380 // the option paginationurl was specified, display pagingbar 1381 $perpage = $chelper->get_courses_display_option('limit', $CFG->coursesperpage); 1382 $page = $chelper->get_courses_display_option('offset') / $perpage; 1383 $pagingbar = $this->paging_bar($totalcount, $page, $perpage, 1384 $paginationurl->out(false, array('perpage' => $perpage))); 1385 if ($paginationallowall) { 1386 $pagingbar .= html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => 'all')), 1387 get_string('showall', '', $totalcount)), array('class' => 'paging paging-showall')); 1388 } 1389 } else if ($viewmoreurl = $chelper->get_courses_display_option('viewmoreurl')) { 1390 // the option for 'View more' link was specified, display more link 1391 $viewmoretext = $chelper->get_courses_display_option('viewmoretext', new lang_string('viewmore')); 1392 $morelink = html_writer::tag('div', html_writer::link($viewmoreurl, $viewmoretext), 1393 array('class' => 'paging paging-morelink')); 1394 } 1395 } else if (($totalcount > $CFG->coursesperpage) && $paginationurl && $paginationallowall) { 1396 // there are more than one page of results and we are in 'view all' mode, suggest to go back to paginated view mode 1397 $pagingbar = html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => $CFG->coursesperpage)), 1398 get_string('showperpage', '', $CFG->coursesperpage)), array('class' => 'paging paging-showperpage')); 1399 } 1400 1401 // display list of courses 1402 $attributes = $chelper->get_and_erase_attributes('courses'); 1403 $content = html_writer::start_tag('div', $attributes); 1404 1405 if (!empty($pagingbar)) { 1406 $content .= $pagingbar; 1407 } 1408 1409 $coursecount = 0; 1410 foreach ($courses as $course) { 1411 $coursecount ++; 1412 $classes = ($coursecount%2) ? 'odd' : 'even'; 1413 if ($coursecount == 1) { 1414 $classes .= ' first'; 1415 } 1416 if ($coursecount >= count($courses)) { 1417 $classes .= ' last'; 1418 } 1419 $content .= $this->coursecat_coursebox($chelper, $course, $classes); 1420 } 1421 1422 if (!empty($pagingbar)) { 1423 $content .= $pagingbar; 1424 } 1425 if (!empty($morelink)) { 1426 $content .= $morelink; 1427 } 1428 1429 $content .= html_writer::end_tag('div'); // .courses 1430 return $content; 1431 } 1432 1433 /** 1434 * Renders the list of subcategories in a category 1435 * 1436 * @param coursecat_helper $chelper various display options 1437 * @param core_course_category $coursecat 1438 * @param int $depth depth of the category in the current tree 1439 * @return string 1440 */ 1441 protected function coursecat_subcategories(coursecat_helper $chelper, $coursecat, $depth) { 1442 global $CFG; 1443 $subcategories = array(); 1444 if (!$chelper->get_categories_display_option('nodisplay')) { 1445 $subcategories = $coursecat->get_children($chelper->get_categories_display_options()); 1446 } 1447 $totalcount = $coursecat->get_children_count(); 1448 if (!$totalcount) { 1449 // Note that we call core_course_category::get_children_count() AFTER core_course_category::get_children() 1450 // to avoid extra DB requests. 1451 // Categories count is cached during children categories retrieval. 1452 return ''; 1453 } 1454 1455 // prepare content of paging bar or more link if it is needed 1456 $paginationurl = $chelper->get_categories_display_option('paginationurl'); 1457 $paginationallowall = $chelper->get_categories_display_option('paginationallowall'); 1458 if ($totalcount > count($subcategories)) { 1459 if ($paginationurl) { 1460 // the option 'paginationurl was specified, display pagingbar 1461 $perpage = $chelper->get_categories_display_option('limit', $CFG->coursesperpage); 1462 $page = $chelper->get_categories_display_option('offset') / $perpage; 1463 $pagingbar = $this->paging_bar($totalcount, $page, $perpage, 1464 $paginationurl->out(false, array('perpage' => $perpage))); 1465 if ($paginationallowall) { 1466 $pagingbar .= html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => 'all')), 1467 get_string('showall', '', $totalcount)), array('class' => 'paging paging-showall')); 1468 } 1469 } else if ($viewmoreurl = $chelper->get_categories_display_option('viewmoreurl')) { 1470 // the option 'viewmoreurl' was specified, display more link (if it is link to category view page, add category id) 1471 if ($viewmoreurl->compare(new moodle_url('/course/index.php'), URL_MATCH_BASE)) { 1472 $viewmoreurl->param('categoryid', $coursecat->id); 1473 } 1474 $viewmoretext = $chelper->get_categories_display_option('viewmoretext', new lang_string('viewmore')); 1475 $morelink = html_writer::tag('div', html_writer::link($viewmoreurl, $viewmoretext), 1476 array('class' => 'paging paging-morelink')); 1477 } 1478 } else if (($totalcount > $CFG->coursesperpage) && $paginationurl && $paginationallowall) { 1479 // there are more than one page of results and we are in 'view all' mode, suggest to go back to paginated view mode 1480 $pagingbar = html_writer::tag('div', html_writer::link($paginationurl->out(false, array('perpage' => $CFG->coursesperpage)), 1481 get_string('showperpage', '', $CFG->coursesperpage)), array('class' => 'paging paging-showperpage')); 1482 } 1483 1484 // display list of subcategories 1485 $content = html_writer::start_tag('div', array('class' => 'subcategories')); 1486 1487 if (!empty($pagingbar)) { 1488 $content .= $pagingbar; 1489 } 1490 1491 foreach ($subcategories as $subcategory) { 1492 $content .= $this->coursecat_category($chelper, $subcategory, $depth + 1); 1493 } 1494 1495 if (!empty($pagingbar)) { 1496 $content .= $pagingbar; 1497 } 1498 if (!empty($morelink)) { 1499 $content .= $morelink; 1500 } 1501 1502 $content .= html_writer::end_tag('div'); 1503 return $content; 1504 } 1505 1506 /** 1507 * Make sure that javascript file for AJAX expanding of courses and categories content is included 1508 */ 1509 protected function coursecat_include_js() { 1510 if (!$this->page->requires->should_create_one_time_item_now('core_course_categoryexpanderjsinit')) { 1511 return; 1512 } 1513 1514 // We must only load this module once. 1515 $this->page->requires->yui_module('moodle-course-categoryexpander', 1516 'Y.Moodle.course.categoryexpander.init'); 1517 } 1518 1519 /** 1520 * Returns HTML to display the subcategories and courses in the given category 1521 * 1522 * This method is re-used by AJAX to expand content of not loaded category 1523 * 1524 * @param coursecat_helper $chelper various display options 1525 * @param core_course_category $coursecat 1526 * @param int $depth depth of the category in the current tree 1527 * @return string 1528 */ 1529 protected function coursecat_category_content(coursecat_helper $chelper, $coursecat, $depth) { 1530 $content = ''; 1531 // Subcategories 1532 $content .= $this->coursecat_subcategories($chelper, $coursecat, $depth); 1533 1534 // AUTO show courses: Courses will be shown expanded if this is not nested category, 1535 // and number of courses no bigger than $CFG->courseswithsummarieslimit. 1536 $showcoursesauto = $chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_AUTO; 1537 if ($showcoursesauto && $depth) { 1538 // this is definitely collapsed mode 1539 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_COLLAPSED); 1540 } 1541 1542 // Courses 1543 if ($chelper->get_show_courses() > core_course_renderer::COURSECAT_SHOW_COURSES_COUNT) { 1544 $courses = array(); 1545 if (!$chelper->get_courses_display_option('nodisplay')) { 1546 $courses = $coursecat->get_courses($chelper->get_courses_display_options()); 1547 } 1548 if ($viewmoreurl = $chelper->get_courses_display_option('viewmoreurl')) { 1549 // the option for 'View more' link was specified, display more link (if it is link to category view page, add category id) 1550 if ($viewmoreurl->compare(new moodle_url('/course/index.php'), URL_MATCH_BASE)) { 1551 $chelper->set_courses_display_option('viewmoreurl', new moodle_url($viewmoreurl, array('categoryid' => $coursecat->id))); 1552 } 1553 } 1554 $content .= $this->coursecat_courses($chelper, $courses, $coursecat->get_courses_count()); 1555 } 1556 1557 if ($showcoursesauto) { 1558 // restore the show_courses back to AUTO 1559 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_AUTO); 1560 } 1561 1562 return $content; 1563 } 1564 1565 /** 1566 * Returns HTML to display a course category as a part of a tree 1567 * 1568 * This is an internal function, to display a particular category and all its contents 1569 * use {@link core_course_renderer::course_category()} 1570 * 1571 * @param coursecat_helper $chelper various display options 1572 * @param core_course_category $coursecat 1573 * @param int $depth depth of this category in the current tree 1574 * @return string 1575 */ 1576 protected function coursecat_category(coursecat_helper $chelper, $coursecat, $depth) { 1577 // open category tag 1578 $classes = array('category'); 1579 if (empty($coursecat->visible)) { 1580 $classes[] = 'dimmed_category'; 1581 } 1582 if ($chelper->get_subcat_depth() > 0 && $depth >= $chelper->get_subcat_depth()) { 1583 // do not load content 1584 $categorycontent = ''; 1585 $classes[] = 'notloaded'; 1586 if ($coursecat->get_children_count() || 1587 ($chelper->get_show_courses() >= self::COURSECAT_SHOW_COURSES_COLLAPSED && $coursecat->get_courses_count())) { 1588 $classes[] = 'with_children'; 1589 $classes[] = 'collapsed'; 1590 } 1591 } else { 1592 // load category content 1593 $categorycontent = $this->coursecat_category_content($chelper, $coursecat, $depth); 1594 $classes[] = 'loaded'; 1595 if (!empty($categorycontent)) { 1596 $classes[] = 'with_children'; 1597 // Category content loaded with children. 1598 $this->categoryexpandedonload = true; 1599 } 1600 } 1601 1602 // Make sure JS file to expand category content is included. 1603 $this->coursecat_include_js(); 1604 1605 $content = html_writer::start_tag('div', array( 1606 'class' => join(' ', $classes), 1607 'data-categoryid' => $coursecat->id, 1608 'data-depth' => $depth, 1609 'data-showcourses' => $chelper->get_show_courses(), 1610 'data-type' => self::COURSECAT_TYPE_CATEGORY, 1611 )); 1612 1613 // category name 1614 $categoryname = $coursecat->get_formatted_name(); 1615 $categoryname = html_writer::link(new moodle_url('/course/index.php', 1616 array('categoryid' => $coursecat->id)), 1617 $categoryname); 1618 if ($chelper->get_show_courses() == self::COURSECAT_SHOW_COURSES_COUNT 1619 && ($coursescount = $coursecat->get_courses_count())) { 1620 $categoryname .= html_writer::tag('span', ' ('. $coursescount.')', 1621 array('title' => get_string('numberofcourses'), 'class' => 'numberofcourse')); 1622 } 1623 $content .= html_writer::start_tag('div', array('class' => 'info')); 1624 1625 $content .= html_writer::tag(($depth > 1) ? 'h4' : 'h3', $categoryname, array('class' => 'categoryname aabtn')); 1626 $content .= html_writer::end_tag('div'); // .info 1627 1628 // add category content to the output 1629 $content .= html_writer::tag('div', $categorycontent, array('class' => 'content')); 1630 1631 $content .= html_writer::end_tag('div'); // .category 1632 1633 // Return the course category tree HTML 1634 return $content; 1635 } 1636 1637 /** 1638 * Returns HTML to display a tree of subcategories and courses in the given category 1639 * 1640 * @param coursecat_helper $chelper various display options 1641 * @param core_course_category $coursecat top category (this category's name and description will NOT be added to the tree) 1642 * @return string 1643 */ 1644 protected function coursecat_tree(coursecat_helper $chelper, $coursecat) { 1645 // Reset the category expanded flag for this course category tree first. 1646 $this->categoryexpandedonload = false; 1647 $categorycontent = $this->coursecat_category_content($chelper, $coursecat, 0); 1648 if (empty($categorycontent)) { 1649 return ''; 1650 } 1651 1652 // Start content generation 1653 $content = ''; 1654 $attributes = $chelper->get_and_erase_attributes('course_category_tree clearfix'); 1655 $content .= html_writer::start_tag('div', $attributes); 1656 1657 if ($coursecat->get_children_count()) { 1658 $classes = array( 1659 'collapseexpand', 'aabtn' 1660 ); 1661 1662 // Check if the category content contains subcategories with children's content loaded. 1663 if ($this->categoryexpandedonload) { 1664 $classes[] = 'collapse-all'; 1665 $linkname = get_string('collapseall'); 1666 } else { 1667 $linkname = get_string('expandall'); 1668 } 1669 1670 // Only show the collapse/expand if there are children to expand. 1671 $content .= html_writer::start_tag('div', array('class' => 'collapsible-actions')); 1672 $content .= html_writer::link('#', $linkname, array('class' => implode(' ', $classes))); 1673 $content .= html_writer::end_tag('div'); 1674 $this->page->requires->strings_for_js(array('collapseall', 'expandall'), 'moodle'); 1675 } 1676 1677 $content .= html_writer::tag('div', $categorycontent, array('class' => 'content')); 1678 1679 $content .= html_writer::end_tag('div'); // .course_category_tree 1680 1681 return $content; 1682 } 1683 1684 /** 1685 * Renders HTML to display particular course category - list of it's subcategories and courses 1686 * 1687 * Invoked from /course/index.php 1688 * 1689 * @param int|stdClass|core_course_category $category 1690 */ 1691 public function course_category($category) { 1692 global $CFG; 1693 $usertop = core_course_category::user_top(); 1694 if (empty($category)) { 1695 $coursecat = $usertop; 1696 } else if (is_object($category) && $category instanceof core_course_category) { 1697 $coursecat = $category; 1698 } else { 1699 $coursecat = core_course_category::get(is_object($category) ? $category->id : $category); 1700 } 1701 $site = get_site(); 1702 $output = ''; 1703 1704 if ($coursecat->can_create_course() || $coursecat->has_manage_capability()) { 1705 // Add 'Manage' button if user has permissions to edit this category. 1706 $managebutton = $this->single_button(new moodle_url('/course/management.php', 1707 array('categoryid' => $coursecat->id)), get_string('managecourses'), 'get'); 1708 $this->page->set_button($managebutton); 1709 } 1710 1711 if (core_course_category::is_simple_site()) { 1712 // There is only one category in the system, do not display link to it. 1713 $strfulllistofcourses = get_string('fulllistofcourses'); 1714 $this->page->set_title("$site->shortname: $strfulllistofcourses"); 1715 } else if (!$coursecat->id || !$coursecat->is_uservisible()) { 1716 $strcategories = get_string('categories'); 1717 $this->page->set_title("$site->shortname: $strcategories"); 1718 } else { 1719 $strfulllistofcourses = get_string('fulllistofcourses'); 1720 $this->page->set_title("$site->shortname: $strfulllistofcourses"); 1721 1722 // Print the category selector 1723 $categorieslist = core_course_category::make_categories_list(); 1724 if (count($categorieslist) > 1) { 1725 $output .= html_writer::start_tag('div', array('class' => 'categorypicker')); 1726 $select = new single_select(new moodle_url('/course/index.php'), 'categoryid', 1727 core_course_category::make_categories_list(), $coursecat->id, null, 'switchcategory'); 1728 $select->set_label(get_string('categories').':'); 1729 $output .= $this->render($select); 1730 $output .= html_writer::end_tag('div'); // .categorypicker 1731 } 1732 } 1733 1734 // Print current category description 1735 $chelper = new coursecat_helper(); 1736 if ($description = $chelper->get_category_formatted_description($coursecat)) { 1737 $output .= $this->box($description, array('class' => 'generalbox info')); 1738 } 1739 1740 // Prepare parameters for courses and categories lists in the tree 1741 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_AUTO) 1742 ->set_attributes(array('class' => 'category-browse category-browse-'.$coursecat->id)); 1743 1744 $coursedisplayoptions = array(); 1745 $catdisplayoptions = array(); 1746 $browse = optional_param('browse', null, PARAM_ALPHA); 1747 $perpage = optional_param('perpage', $CFG->coursesperpage, PARAM_INT); 1748 $page = optional_param('page', 0, PARAM_INT); 1749 $baseurl = new moodle_url('/course/index.php'); 1750 if ($coursecat->id) { 1751 $baseurl->param('categoryid', $coursecat->id); 1752 } 1753 if ($perpage != $CFG->coursesperpage) { 1754 $baseurl->param('perpage', $perpage); 1755 } 1756 $coursedisplayoptions['limit'] = $perpage; 1757 $catdisplayoptions['limit'] = $perpage; 1758 if ($browse === 'courses' || !$coursecat->get_children_count()) { 1759 $coursedisplayoptions['offset'] = $page * $perpage; 1760 $coursedisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'courses')); 1761 $catdisplayoptions['nodisplay'] = true; 1762 $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories')); 1763 $catdisplayoptions['viewmoretext'] = new lang_string('viewallsubcategories'); 1764 } else if ($browse === 'categories' || !$coursecat->get_courses_count()) { 1765 $coursedisplayoptions['nodisplay'] = true; 1766 $catdisplayoptions['offset'] = $page * $perpage; 1767 $catdisplayoptions['paginationurl'] = new moodle_url($baseurl, array('browse' => 'categories')); 1768 $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses')); 1769 $coursedisplayoptions['viewmoretext'] = new lang_string('viewallcourses'); 1770 } else { 1771 // we have a category that has both subcategories and courses, display pagination separately 1772 $coursedisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1)); 1773 $catdisplayoptions['viewmoreurl'] = new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1)); 1774 } 1775 $chelper->set_courses_display_options($coursedisplayoptions)->set_categories_display_options($catdisplayoptions); 1776 // Add course search form. 1777 $output .= $this->course_search_form(); 1778 1779 // Display course category tree. 1780 $output .= $this->coursecat_tree($chelper, $coursecat); 1781 1782 // Add action buttons 1783 $output .= $this->container_start('buttons'); 1784 if ($coursecat->is_uservisible()) { 1785 $context = get_category_or_system_context($coursecat->id); 1786 if (has_capability('moodle/course:create', $context)) { 1787 // Print link to create a new course, for the 1st available category. 1788 if ($coursecat->id) { 1789 $url = new moodle_url('/course/edit.php', array('category' => $coursecat->id, 'returnto' => 'category')); 1790 } else { 1791 $url = new moodle_url('/course/edit.php', 1792 array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcat')); 1793 } 1794 $output .= $this->single_button($url, get_string('addnewcourse'), 'get'); 1795 } 1796 ob_start(); 1797 print_course_request_buttons($context); 1798 $output .= ob_get_contents(); 1799 ob_end_clean(); 1800 } 1801 $output .= $this->container_end(); 1802 1803 return $output; 1804 } 1805 1806 /** 1807 * Serves requests to /course/category.ajax.php 1808 * 1809 * In this renderer implementation it may expand the category content or 1810 * course content. 1811 * 1812 * @return string 1813 * @throws coding_exception 1814 */ 1815 public function coursecat_ajax() { 1816 global $DB, $CFG; 1817 1818 $type = required_param('type', PARAM_INT); 1819 1820 if ($type === self::COURSECAT_TYPE_CATEGORY) { 1821 // This is a request for a category list of some kind. 1822 $categoryid = required_param('categoryid', PARAM_INT); 1823 $showcourses = required_param('showcourses', PARAM_INT); 1824 $depth = required_param('depth', PARAM_INT); 1825 1826 $category = core_course_category::get($categoryid); 1827 1828 $chelper = new coursecat_helper(); 1829 $baseurl = new moodle_url('/course/index.php', array('categoryid' => $categoryid)); 1830 $coursedisplayoptions = array( 1831 'limit' => $CFG->coursesperpage, 1832 'viewmoreurl' => new moodle_url($baseurl, array('browse' => 'courses', 'page' => 1)) 1833 ); 1834 $catdisplayoptions = array( 1835 'limit' => $CFG->coursesperpage, 1836 'viewmoreurl' => new moodle_url($baseurl, array('browse' => 'categories', 'page' => 1)) 1837 ); 1838 $chelper->set_show_courses($showcourses)-> 1839 set_courses_display_options($coursedisplayoptions)-> 1840 set_categories_display_options($catdisplayoptions); 1841 1842 return $this->coursecat_category_content($chelper, $category, $depth); 1843 } else if ($type === self::COURSECAT_TYPE_COURSE) { 1844 // This is a request for the course information. 1845 $courseid = required_param('courseid', PARAM_INT); 1846 1847 $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); 1848 1849 $chelper = new coursecat_helper(); 1850 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED); 1851 return $this->coursecat_coursebox_content($chelper, $course); 1852 } else { 1853 throw new coding_exception('Invalid request type'); 1854 } 1855 } 1856 1857 /** 1858 * Renders html to display search result page 1859 * 1860 * @param array $searchcriteria may contain elements: search, blocklist, modulelist, tagid 1861 * @return string 1862 */ 1863 public function search_courses($searchcriteria) { 1864 global $CFG; 1865 $content = ''; 1866 1867 $search = ''; 1868 if (!empty($searchcriteria['search'])) { 1869 $search = $searchcriteria['search']; 1870 } 1871 $content .= $this->course_search_form($search); 1872 1873 if (!empty($searchcriteria)) { 1874 // print search results 1875 1876 $displayoptions = array('sort' => array('displayname' => 1)); 1877 // take the current page and number of results per page from query 1878 $perpage = optional_param('perpage', 0, PARAM_RAW); 1879 if ($perpage !== 'all') { 1880 $displayoptions['limit'] = ((int)$perpage <= 0) ? $CFG->coursesperpage : (int)$perpage; 1881 $page = optional_param('page', 0, PARAM_INT); 1882 $displayoptions['offset'] = $displayoptions['limit'] * $page; 1883 } 1884 // options 'paginationurl' and 'paginationallowall' are only used in method coursecat_courses() 1885 $displayoptions['paginationurl'] = new moodle_url('/course/search.php', $searchcriteria); 1886 $displayoptions['paginationallowall'] = true; // allow adding link 'View all' 1887 1888 $class = 'course-search-result'; 1889 foreach ($searchcriteria as $key => $value) { 1890 if (!empty($value)) { 1891 $class .= ' course-search-result-'. $key; 1892 } 1893 } 1894 $chelper = new coursecat_helper(); 1895 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT)-> 1896 set_courses_display_options($displayoptions)-> 1897 set_search_criteria($searchcriteria)-> 1898 set_attributes(array('class' => $class)); 1899 1900 $courses = core_course_category::search_courses($searchcriteria, $chelper->get_courses_display_options()); 1901 $totalcount = core_course_category::search_courses_count($searchcriteria); 1902 $courseslist = $this->coursecat_courses($chelper, $courses, $totalcount); 1903 1904 if (!$totalcount) { 1905 if (!empty($searchcriteria['search'])) { 1906 $content .= $this->heading(get_string('nocoursesfound', '', $searchcriteria['search'])); 1907 } else { 1908 $content .= $this->heading(get_string('novalidcourses')); 1909 } 1910 } else { 1911 $content .= $this->heading(get_string('searchresults'). ": $totalcount"); 1912 $content .= $courseslist; 1913 } 1914 } 1915 return $content; 1916 } 1917 1918 /** 1919 * Renders html to print list of courses tagged with particular tag 1920 * 1921 * @param int $tagid id of the tag 1922 * @param bool $exclusivemode if set to true it means that no other entities tagged with this tag 1923 * are displayed on the page and the per-page limit may be bigger 1924 * @param int $fromctx context id where the link was displayed, may be used by callbacks 1925 * to display items in the same context first 1926 * @param int $ctx context id where to search for records 1927 * @param bool $rec search in subcontexts as well 1928 * @param array $displayoptions 1929 * @return string empty string if no courses are marked with this tag or rendered list of courses 1930 */ 1931 public function tagged_courses($tagid, $exclusivemode = true, $ctx = 0, $rec = true, $displayoptions = null) { 1932 global $CFG; 1933 if (empty($displayoptions)) { 1934 $displayoptions = array(); 1935 } 1936 $showcategories = !core_course_category::is_simple_site(); 1937 $displayoptions += array('limit' => $CFG->coursesperpage, 'offset' => 0); 1938 $chelper = new coursecat_helper(); 1939 $searchcriteria = array('tagid' => $tagid, 'ctx' => $ctx, 'rec' => $rec); 1940 $chelper->set_show_courses($showcategories ? self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT : 1941 self::COURSECAT_SHOW_COURSES_EXPANDED)-> 1942 set_search_criteria($searchcriteria)-> 1943 set_courses_display_options($displayoptions)-> 1944 set_attributes(array('class' => 'course-search-result course-search-result-tagid')); 1945 // (we set the same css class as in search results by tagid) 1946 if ($totalcount = core_course_category::search_courses_count($searchcriteria)) { 1947 $courses = core_course_category::search_courses($searchcriteria, $chelper->get_courses_display_options()); 1948 if ($exclusivemode) { 1949 return $this->coursecat_courses($chelper, $courses, $totalcount); 1950 } else { 1951 $tagfeed = new core_tag\output\tagfeed(); 1952 $img = $this->output->pix_icon('i/course', ''); 1953 foreach ($courses as $course) { 1954 $url = course_get_url($course); 1955 $imgwithlink = html_writer::link($url, $img); 1956 $coursename = html_writer::link($url, $course->get_formatted_name()); 1957 $details = ''; 1958 if ($showcategories && ($cat = core_course_category::get($course->category, IGNORE_MISSING))) { 1959 $details = get_string('category').': '. 1960 html_writer::link(new moodle_url('/course/index.php', array('categoryid' => $cat->id)), 1961 $cat->get_formatted_name(), array('class' => $cat->visible ? '' : 'dimmed')); 1962 } 1963 $tagfeed->add($imgwithlink, $coursename, $details); 1964 } 1965 return $this->output->render_from_template('core_tag/tagfeed', $tagfeed->export_for_template($this->output)); 1966 } 1967 } 1968 return ''; 1969 } 1970 1971 /** 1972 * Returns HTML to display one remote course 1973 * 1974 * @param stdClass $course remote course information, contains properties: 1975 id, remoteid, shortname, fullname, hostid, summary, summaryformat, cat_name, hostname 1976 * @return string 1977 */ 1978 protected function frontpage_remote_course(stdClass $course) { 1979 $url = new moodle_url('/auth/mnet/jump.php', array( 1980 'hostid' => $course->hostid, 1981 'wantsurl' => '/course/view.php?id='. $course->remoteid 1982 )); 1983 1984 $output = ''; 1985 $output .= html_writer::start_tag('div', array('class' => 'coursebox remotecoursebox clearfix')); 1986 $output .= html_writer::start_tag('div', array('class' => 'info')); 1987 $output .= html_writer::start_tag('h3', array('class' => 'name')); 1988 $output .= html_writer::link($url, format_string($course->fullname), array('title' => get_string('entercourse'))); 1989 $output .= html_writer::end_tag('h3'); // .name 1990 $output .= html_writer::tag('div', '', array('class' => 'moreinfo')); 1991 $output .= html_writer::end_tag('div'); // .info 1992 $output .= html_writer::start_tag('div', array('class' => 'content')); 1993 $output .= html_writer::start_tag('div', array('class' => 'summary')); 1994 $options = new stdClass(); 1995 $options->noclean = true; 1996 $options->para = false; 1997 $options->overflowdiv = true; 1998 $output .= format_text($course->summary, $course->summaryformat, $options); 1999 $output .= html_writer::end_tag('div'); // .summary 2000 $addinfo = format_string($course->hostname) . ' : ' 2001 . format_string($course->cat_name) . ' : ' 2002 . format_string($course->shortname); 2003 $output .= html_writer::tag('div', $addinfo, array('class' => 'remotecourseinfo')); 2004 $output .= html_writer::end_tag('div'); // .content 2005 $output .= html_writer::end_tag('div'); // .coursebox 2006 return $output; 2007 } 2008 2009 /** 2010 * Returns HTML to display one remote host 2011 * 2012 * @param array $host host information, contains properties: name, url, count 2013 * @return string 2014 */ 2015 protected function frontpage_remote_host($host) { 2016 $output = ''; 2017 $output .= html_writer::start_tag('div', array('class' => 'coursebox remotehost clearfix')); 2018 $output .= html_writer::start_tag('div', array('class' => 'info')); 2019 $output .= html_writer::start_tag('h3', array('class' => 'name')); 2020 $output .= html_writer::link($host['url'], s($host['name']), array('title' => s($host['name']))); 2021 $output .= html_writer::end_tag('h3'); // .name 2022 $output .= html_writer::tag('div', '', array('class' => 'moreinfo')); 2023 $output .= html_writer::end_tag('div'); // .info 2024 $output .= html_writer::start_tag('div', array('class' => 'content')); 2025 $output .= html_writer::start_tag('div', array('class' => 'summary')); 2026 $output .= $host['count'] . ' ' . get_string('courses'); 2027 $output .= html_writer::end_tag('div'); // .content 2028 $output .= html_writer::end_tag('div'); // .coursebox 2029 return $output; 2030 } 2031 2032 /** 2033 * Returns HTML to print list of courses user is enrolled to for the frontpage 2034 * 2035 * Also lists remote courses or remote hosts if MNET authorisation is used 2036 * 2037 * @return string 2038 */ 2039 public function frontpage_my_courses() { 2040 global $USER, $CFG, $DB; 2041 2042 if (!isloggedin() or isguestuser()) { 2043 return ''; 2044 } 2045 2046 $output = ''; 2047 $courses = enrol_get_my_courses('summary, summaryformat'); 2048 $rhosts = array(); 2049 $rcourses = array(); 2050 if (!empty($CFG->mnet_dispatcher_mode) && $CFG->mnet_dispatcher_mode==='strict') { 2051 $rcourses = get_my_remotecourses($USER->id); 2052 $rhosts = get_my_remotehosts(); 2053 } 2054 2055 if (!empty($courses) || !empty($rcourses) || !empty($rhosts)) { 2056 2057 $chelper = new coursecat_helper(); 2058 $totalcount = count($courses); 2059 if (count($courses) > $CFG->frontpagecourselimit) { 2060 // There are more enrolled courses than we can display, display link to 'My courses'. 2061 $courses = array_slice($courses, 0, $CFG->frontpagecourselimit, true); 2062 $chelper->set_courses_display_options(array( 2063 'viewmoreurl' => new moodle_url('/my/'), 2064 'viewmoretext' => new lang_string('mycourses') 2065 )); 2066 } else if (core_course_category::top()->is_uservisible()) { 2067 // All enrolled courses are displayed, display link to 'All courses' if there are more courses in system. 2068 $chelper->set_courses_display_options(array( 2069 'viewmoreurl' => new moodle_url('/course/index.php'), 2070 'viewmoretext' => new lang_string('fulllistofcourses') 2071 )); 2072 $totalcount = $DB->count_records('course') - 1; 2073 } 2074 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)-> 2075 set_attributes(array('class' => 'frontpage-course-list-enrolled')); 2076 $output .= $this->coursecat_courses($chelper, $courses, $totalcount); 2077 2078 // MNET 2079 if (!empty($rcourses)) { 2080 // at the IDP, we know of all the remote courses 2081 $output .= html_writer::start_tag('div', array('class' => 'courses')); 2082 foreach ($rcourses as $course) { 2083 $output .= $this->frontpage_remote_course($course); 2084 } 2085 $output .= html_writer::end_tag('div'); // .courses 2086 } elseif (!empty($rhosts)) { 2087 // non-IDP, we know of all the remote servers, but not courses 2088 $output .= html_writer::start_tag('div', array('class' => 'courses')); 2089 foreach ($rhosts as $host) { 2090 $output .= $this->frontpage_remote_host($host); 2091 } 2092 $output .= html_writer::end_tag('div'); // .courses 2093 } 2094 } 2095 return $output; 2096 } 2097 2098 /** 2099 * Returns HTML to print list of available courses for the frontpage 2100 * 2101 * @return string 2102 */ 2103 public function frontpage_available_courses() { 2104 global $CFG; 2105 2106 $chelper = new coursecat_helper(); 2107 $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED)-> 2108 set_courses_display_options(array( 2109 'recursive' => true, 2110 'limit' => $CFG->frontpagecourselimit, 2111 'viewmoreurl' => new moodle_url('/course/index.php'), 2112 'viewmoretext' => new lang_string('fulllistofcourses'))); 2113 2114 $chelper->set_attributes(array('class' => 'frontpage-course-list-all')); 2115 $courses = core_course_category::top()->get_courses($chelper->get_courses_display_options()); 2116 $totalcount = core_course_category::top()->get_courses_count($chelper->get_courses_display_options()); 2117 if (!$totalcount && !$this->page->user_is_editing() && has_capability('moodle/course:create', context_system::instance())) { 2118 // Print link to create a new course, for the 1st available category. 2119 return $this->add_new_course_button(); 2120 } 2121 return $this->coursecat_courses($chelper, $courses, $totalcount); 2122 } 2123 2124 /** 2125 * Returns HTML to the "add new course" button for the page 2126 * 2127 * @return string 2128 */ 2129 public function add_new_course_button() { 2130 global $CFG; 2131 // Print link to create a new course, for the 1st available category. 2132 $output = $this->container_start('buttons'); 2133 $url = new moodle_url('/course/edit.php', array('category' => $CFG->defaultrequestcategory, 'returnto' => 'topcat')); 2134 $output .= $this->single_button($url, get_string('addnewcourse'), 'get'); 2135 $output .= $this->container_end('buttons'); 2136 return $output; 2137 } 2138 2139 /** 2140 * Returns HTML to print tree with course categories and courses for the frontpage 2141 * 2142 * @return string 2143 */ 2144 public function frontpage_combo_list() { 2145 global $CFG; 2146 // TODO MDL-10965 improve. 2147 $tree = core_course_category::top(); 2148 if (!$tree->get_children_count()) { 2149 return ''; 2150 } 2151 $chelper = new coursecat_helper(); 2152 $chelper->set_subcat_depth($CFG->maxcategorydepth)-> 2153 set_categories_display_options(array( 2154 'limit' => $CFG->coursesperpage, 2155 'viewmoreurl' => new moodle_url('/course/index.php', 2156 array('browse' => 'categories', 'page' => 1)) 2157 ))-> 2158 set_courses_display_options(array( 2159 'limit' => $CFG->coursesperpage, 2160 'viewmoreurl' => new moodle_url('/course/index.php', 2161 array('browse' => 'courses', 'page' => 1)) 2162 ))-> 2163 set_attributes(array('class' => 'frontpage-category-combo')); 2164 return $this->coursecat_tree($chelper, $tree); 2165 } 2166 2167 /** 2168 * Returns HTML to print tree of course categories (with number of courses) for the frontpage 2169 * 2170 * @return string 2171 */ 2172 public function frontpage_categories_list() { 2173 global $CFG; 2174 // TODO MDL-10965 improve. 2175 $tree = core_course_category::top(); 2176 if (!$tree->get_children_count()) { 2177 return ''; 2178 } 2179 $chelper = new coursecat_helper(); 2180 $chelper->set_subcat_depth($CFG->maxcategorydepth)-> 2181 set_show_courses(self::COURSECAT_SHOW_COURSES_COUNT)-> 2182 set_categories_display_options(array( 2183 'limit' => $CFG->coursesperpage, 2184 'viewmoreurl' => new moodle_url('/course/index.php', 2185 array('browse' => 'categories', 'page' => 1)) 2186 ))-> 2187 set_attributes(array('class' => 'frontpage-category-names')); 2188 return $this->coursecat_tree($chelper, $tree); 2189 } 2190 2191 /** 2192 * Renders the activity navigation. 2193 * 2194 * Defer to template. 2195 * 2196 * @param \core_course\output\activity_navigation $page 2197 * @return string html for the page 2198 */ 2199 public function render_activity_navigation(\core_course\output\activity_navigation $page) { 2200 $data = $page->export_for_template($this->output); 2201 return $this->output->render_from_template('core_course/activity_navigation', $data); 2202 } 2203 2204 /** 2205 * Display waiting information about backup size during uploading backup process 2206 * @param object $backupfile the backup stored_file 2207 * @return $html string 2208 */ 2209 public function sendingbackupinfo($backupfile) { 2210 $sizeinfo = new stdClass(); 2211 $sizeinfo->total = number_format($backupfile->get_filesize() / 1000000, 2); 2212 $html = html_writer::tag('div', get_string('sendingsize', 'hub', $sizeinfo), 2213 array('class' => 'courseuploadtextinfo')); 2214 return $html; 2215 } 2216 2217 /** 2218 * Hub information (logo - name - description - link) 2219 * @param object $hubinfo 2220 * @return string html code 2221 */ 2222 public function hubinfo($hubinfo) { 2223 $screenshothtml = html_writer::empty_tag('img', 2224 array('src' => $hubinfo['imgurl'], 'alt' => $hubinfo['name'])); 2225 $hubdescription = html_writer::tag('div', $screenshothtml, 2226 array('class' => 'hubscreenshot')); 2227 2228 $hubdescription .= html_writer::tag('a', $hubinfo['name'], 2229 array('class' => 'hublink', 'href' => $hubinfo['url'], 2230 'onclick' => 'this.target="_blank"')); 2231 2232 $hubdescription .= html_writer::tag('div', format_text($hubinfo['description'], FORMAT_PLAIN), 2233 array('class' => 'hubdescription')); 2234 $hubdescription = html_writer::tag('div', $hubdescription, array('class' => 'hubinfo clearfix')); 2235 2236 return $hubdescription; 2237 } 2238 2239 /** 2240 * Output frontpage summary text and frontpage modules (stored as section 1 in site course) 2241 * 2242 * This may be disabled in settings 2243 * 2244 * @return string 2245 */ 2246 public function frontpage_section1() { 2247 global $SITE, $USER; 2248 2249 $output = ''; 2250 $editing = $this->page->user_is_editing(); 2251 2252 if ($editing) { 2253 // Make sure section with number 1 exists. 2254 course_create_sections_if_missing($SITE, 1); 2255 } 2256 2257 $modinfo = get_fast_modinfo($SITE); 2258 $section = $modinfo->get_section_info(1); 2259 if (($section && (!empty($modinfo->sections[1]) or !empty($section->summary))) or $editing) { 2260 $output .= $this->box_start('generalbox sitetopic'); 2261 2262 // If currently moving a file then show the current clipboard. 2263 if (ismoving($SITE->id)) { 2264 $stractivityclipboard = strip_tags(get_string('activityclipboard', '', $USER->activitycopyname)); 2265 $output .= '<p><font size="2">'; 2266 $cancelcopyurl = new moodle_url('/course/mod.php', ['cancelcopy' => 'true', 'sesskey' => sesskey()]); 2267 $output .= "$stractivityclipboard (" . html_writer::link($cancelcopyurl, get_string('cancel')) .')'; 2268 $output .= '</font></p>'; 2269 } 2270 2271 $context = context_course::instance(SITEID); 2272 2273 // If the section name is set we show it. 2274 if (trim($section->name) !== '') { 2275 $output .= $this->heading( 2276 format_string($section->name, true, array('context' => $context)), 2277 2, 2278 'sectionname' 2279 ); 2280 } 2281 2282 $summarytext = file_rewrite_pluginfile_urls($section->summary, 2283 'pluginfile.php', 2284 $context->id, 2285 'course', 2286 'section', 2287 $section->id); 2288 $summaryformatoptions = new stdClass(); 2289 $summaryformatoptions->noclean = true; 2290 $summaryformatoptions->overflowdiv = true; 2291 2292 $output .= format_text($summarytext, $section->summaryformat, $summaryformatoptions); 2293 2294 if ($editing && has_capability('moodle/course:update', $context)) { 2295 $streditsummary = get_string('editsummary'); 2296 $editsectionurl = new moodle_url('/course/editsection.php', ['id' => $section->id]); 2297 $attributes = ['title' => $streditsummary, 'aria-label' => $streditsummary]; 2298 $output .= html_writer::link($editsectionurl, $this->pix_icon('t/edit', ''), $attributes) . 2299 "<br /><br />"; 2300 } 2301 2302 $output .= $this->course_section_cm_list($SITE, $section); 2303 2304 $output .= $this->course_section_add_cm_control($SITE, $section->section); 2305 $output .= $this->box_end(); 2306 } 2307 2308 return $output; 2309 } 2310 2311 /** 2312 * Output news for the frontpage (extract from site-wide news forum) 2313 * 2314 * @param stdClass $forum record from db table 'forum' that represents the site news forum 2315 * @return string 2316 */ 2317 protected function frontpage_news($forum) { 2318 global $CFG, $SITE, $SESSION, $USER; 2319 require_once($CFG->dirroot .'/mod/forum/lib.php'); 2320 2321 $output = ''; 2322 2323 if (isloggedin()) { 2324 $SESSION->fromdiscussion = $CFG->wwwroot; 2325 $subtext = ''; 2326 if (\mod_forum\subscriptions::is_subscribed($USER->id, $forum)) { 2327 if (!\mod_forum\subscriptions::is_forcesubscribed($forum)) { 2328 $subtext = get_string('unsubscribe', 'forum'); 2329 } 2330 } else { 2331 $subtext = get_string('subscribe', 'forum'); 2332 } 2333 $suburl = new moodle_url('/mod/forum/subscribe.php', array('id' => $forum->id, 'sesskey' => sesskey())); 2334 $output .= html_writer::tag('div', html_writer::link($suburl, $subtext), array('class' => 'subscribelink')); 2335 } 2336 2337 $coursemodule = get_coursemodule_from_instance('forum', $forum->id); 2338 $context = context_module::instance($coursemodule->id); 2339 2340 $entityfactory = mod_forum\local\container::get_entity_factory(); 2341 $forumentity = $entityfactory->get_forum_from_stdclass($forum, $context, $coursemodule, $SITE); 2342 2343 $rendererfactory = mod_forum\local\container::get_renderer_factory(); 2344 $discussionsrenderer = $rendererfactory->get_frontpage_news_discussion_list_renderer($forumentity); 2345 $cm = \cm_info::create($coursemodule); 2346 return $output . $discussionsrenderer->render($USER, $cm, null, null, 0, $SITE->newsitems); 2347 } 2348 2349 /** 2350 * Renders part of frontpage with a skip link (i.e. "My courses", "Site news", etc.) 2351 * 2352 * @param string $skipdivid 2353 * @param string $contentsdivid 2354 * @param string $header Header of the part 2355 * @param string $contents Contents of the part 2356 * @return string 2357 */ 2358 protected function frontpage_part($skipdivid, $contentsdivid, $header, $contents) { 2359 if (strval($contents) === '') { 2360 return ''; 2361 } 2362 $output = html_writer::link('#' . $skipdivid, 2363 get_string('skipa', 'access', core_text::strtolower(strip_tags($header))), 2364 array('class' => 'skip-block skip aabtn')); 2365 2366 // Wrap frontpage part in div container. 2367 $output .= html_writer::start_tag('div', array('id' => $contentsdivid)); 2368 $output .= $this->heading($header); 2369 2370 $output .= $contents; 2371 2372 // End frontpage part div container. 2373 $output .= html_writer::end_tag('div'); 2374 2375 $output .= html_writer::tag('span', '', array('class' => 'skip-block-to', 'id' => $skipdivid)); 2376 return $output; 2377 } 2378 2379 /** 2380 * Outputs contents for frontpage as configured in $CFG->frontpage or $CFG->frontpageloggedin 2381 * 2382 * @return string 2383 */ 2384 public function frontpage() { 2385 global $CFG, $SITE; 2386 2387 $output = ''; 2388 2389 if (isloggedin() and !isguestuser() and isset($CFG->frontpageloggedin)) { 2390 $frontpagelayout = $CFG->frontpageloggedin; 2391 } else { 2392 $frontpagelayout = $CFG->frontpage; 2393 } 2394 2395 foreach (explode(',', $frontpagelayout) as $v) { 2396 switch ($v) { 2397 // Display the main part of the front page. 2398 case FRONTPAGENEWS: 2399 if ($SITE->newsitems) { 2400 // Print forums only when needed. 2401 require_once($CFG->dirroot .'/mod/forum/lib.php'); 2402 if (($newsforum = forum_get_course_forum($SITE->id, 'news')) && 2403 ($forumcontents = $this->frontpage_news($newsforum))) { 2404 $newsforumcm = get_fast_modinfo($SITE)->instances['forum'][$newsforum->id]; 2405 $output .= $this->frontpage_part('skipsitenews', 'site-news-forum', 2406 $newsforumcm->get_formatted_name(), $forumcontents); 2407 } 2408 } 2409 break; 2410 2411 case FRONTPAGEENROLLEDCOURSELIST: 2412 $mycourseshtml = $this->frontpage_my_courses(); 2413 if (!empty($mycourseshtml)) { 2414 $output .= $this->frontpage_part('skipmycourses', 'frontpage-course-list', 2415 get_string('mycourses'), $mycourseshtml); 2416 } 2417 break; 2418 2419 case FRONTPAGEALLCOURSELIST: 2420 $availablecourseshtml = $this->frontpage_available_courses(); 2421 $output .= $this->frontpage_part('skipavailablecourses', 'frontpage-available-course-list', 2422 get_string('availablecourses'), $availablecourseshtml); 2423 break; 2424 2425 case FRONTPAGECATEGORYNAMES: 2426 $output .= $this->frontpage_part('skipcategories', 'frontpage-category-names', 2427 get_string('categories'), $this->frontpage_categories_list()); 2428 break; 2429 2430 case FRONTPAGECATEGORYCOMBO: 2431 $output .= $this->frontpage_part('skipcourses', 'frontpage-category-combo', 2432 get_string('courses'), $this->frontpage_combo_list()); 2433 break; 2434 2435 case FRONTPAGECOURSESEARCH: 2436 $output .= $this->box($this->course_search_form(''), 'd-flex justify-content-center'); 2437 break; 2438 2439 } 2440 $output .= '<br />'; 2441 } 2442 2443 return $output; 2444 } 2445 } 2446 2447 /** 2448 * Class storing display options and functions to help display course category and/or courses lists 2449 * 2450 * This is a wrapper for core_course_category objects that also stores display options 2451 * and functions to retrieve sorted and paginated lists of categories/courses. 2452 * 2453 * If theme overrides methods in core_course_renderers that access this class 2454 * it may as well not use this class at all or extend it. 2455 * 2456 * @package core 2457 * @copyright 2013 Marina Glancy 2458 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 2459 */ 2460 class coursecat_helper { 2461 /** @var string [none, collapsed, expanded] how (if) display courses list */ 2462 protected $showcourses = 10; /* core_course_renderer::COURSECAT_SHOW_COURSES_COLLAPSED */ 2463 /** @var int depth to expand subcategories in the tree (deeper subcategories will be loaded by AJAX or proceed to category page by clicking on category name) */ 2464 protected $subcatdepth = 1; 2465 /** @var array options to display courses list */ 2466 protected $coursesdisplayoptions = array(); 2467 /** @var array options to display subcategories list */ 2468 protected $categoriesdisplayoptions = array(); 2469 /** @var array additional HTML attributes */ 2470 protected $attributes = array(); 2471 /** @var array search criteria if the list is a search result */ 2472 protected $searchcriteria = null; 2473 2474 /** 2475 * Sets how (if) to show the courses - none, collapsed, expanded, etc. 2476 * 2477 * @param int $showcourses SHOW_COURSES_NONE, SHOW_COURSES_COLLAPSED, SHOW_COURSES_EXPANDED, etc. 2478 * @return coursecat_helper 2479 */ 2480 public function set_show_courses($showcourses) { 2481 $this->showcourses = $showcourses; 2482 // Automatically set the options to preload summary and coursecontacts for core_course_category::get_courses() 2483 // and core_course_category::search_courses(). 2484 $this->coursesdisplayoptions['summary'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_AUTO; 2485 $this->coursesdisplayoptions['coursecontacts'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_EXPANDED; 2486 $this->coursesdisplayoptions['customfields'] = $showcourses >= core_course_renderer::COURSECAT_SHOW_COURSES_COLLAPSED; 2487 return $this; 2488 } 2489 2490 /** 2491 * Returns how (if) to show the courses - none, collapsed, expanded, etc. 2492 * 2493 * @return int - COURSECAT_SHOW_COURSES_NONE, COURSECAT_SHOW_COURSES_COLLAPSED, COURSECAT_SHOW_COURSES_EXPANDED, etc. 2494 */ 2495 public function get_show_courses() { 2496 return $this->showcourses; 2497 } 2498 2499 /** 2500 * Sets the maximum depth to expand subcategories in the tree 2501 * 2502 * deeper subcategories may be loaded by AJAX or proceed to category page by clicking on category name 2503 * 2504 * @param int $subcatdepth 2505 * @return coursecat_helper 2506 */ 2507 public function set_subcat_depth($subcatdepth) { 2508 $this->subcatdepth = $subcatdepth; 2509 return $this; 2510 } 2511 2512 /** 2513 * Returns the maximum depth to expand subcategories in the tree 2514 * 2515 * deeper subcategories may be loaded by AJAX or proceed to category page by clicking on category name 2516 * 2517 * @return int 2518 */ 2519 public function get_subcat_depth() { 2520 return $this->subcatdepth; 2521 } 2522 2523 /** 2524 * Sets options to display list of courses 2525 * 2526 * Options are later submitted as argument to core_course_category::get_courses() and/or core_course_category::search_courses() 2527 * 2528 * Options that core_course_category::get_courses() accept: 2529 * - recursive - return courses from subcategories as well. Use with care, 2530 * this may be a huge list! 2531 * - summary - preloads fields 'summary' and 'summaryformat' 2532 * - coursecontacts - preloads course contacts 2533 * - customfields - preloads custom fields data 2534 * - isenrolled - preloads indication whether this user is enrolled in the course 2535 * - sort - list of fields to sort. Example 2536 * array('idnumber' => 1, 'shortname' => 1, 'id' => -1) 2537 * will sort by idnumber asc, shortname asc and id desc. 2538 * Default: array('sortorder' => 1) 2539 * Only cached fields may be used for sorting! 2540 * - offset 2541 * - limit - maximum number of children to return, 0 or null for no limit 2542 * 2543 * Options summary and coursecontacts are filled automatically in the set_show_courses() 2544 * 2545 * Also renderer can set here any additional options it wants to pass between renderer functions. 2546 * 2547 * @param array $options 2548 * @return coursecat_helper 2549 */ 2550 public function set_courses_display_options($options) { 2551 $this->coursesdisplayoptions = $options; 2552 $this->set_show_courses($this->showcourses); // this will calculate special display options 2553 return $this; 2554 } 2555 2556 /** 2557 * Sets one option to display list of courses 2558 * 2559 * @see coursecat_helper::set_courses_display_options() 2560 * 2561 * @param string $key 2562 * @param mixed $value 2563 * @return coursecat_helper 2564 */ 2565 public function set_courses_display_option($key, $value) { 2566 $this->coursesdisplayoptions[$key] = $value; 2567 return $this; 2568 } 2569 2570 /** 2571 * Return the specified option to display list of courses 2572 * 2573 * @param string $optionname option name 2574 * @param mixed $defaultvalue default value for option if it is not specified 2575 * @return mixed 2576 */ 2577 public function get_courses_display_option($optionname, $defaultvalue = null) { 2578 if (array_key_exists($optionname, $this->coursesdisplayoptions)) { 2579 return $this->coursesdisplayoptions[$optionname]; 2580 } else { 2581 return $defaultvalue; 2582 } 2583 } 2584 2585 /** 2586 * Returns all options to display the courses 2587 * 2588 * This array is usually passed to {@link core_course_category::get_courses()} or 2589 * {@link core_course_category::search_courses()} 2590 * 2591 * @return array 2592 */ 2593 public function get_courses_display_options() { 2594 return $this->coursesdisplayoptions; 2595 } 2596 2597 /** 2598 * Sets options to display list of subcategories 2599 * 2600 * Options 'sort', 'offset' and 'limit' are passed to core_course_category::get_children(). 2601 * Any other options may be used by renderer functions 2602 * 2603 * @param array $options 2604 * @return coursecat_helper 2605 */ 2606 public function set_categories_display_options($options) { 2607 $this->categoriesdisplayoptions = $options; 2608 return $this; 2609 } 2610 2611 /** 2612 * Return the specified option to display list of subcategories 2613 * 2614 * @param string $optionname option name 2615 * @param mixed $defaultvalue default value for option if it is not specified 2616 * @return mixed 2617 */ 2618 public function get_categories_display_option($optionname, $defaultvalue = null) { 2619 if (array_key_exists($optionname, $this->categoriesdisplayoptions)) { 2620 return $this->categoriesdisplayoptions[$optionname]; 2621 } else { 2622 return $defaultvalue; 2623 } 2624 } 2625 2626 /** 2627 * Returns all options to display list of subcategories 2628 * 2629 * This array is usually passed to {@link core_course_category::get_children()} 2630 * 2631 * @return array 2632 */ 2633 public function get_categories_display_options() { 2634 return $this->categoriesdisplayoptions; 2635 } 2636 2637 /** 2638 * Sets additional general options to pass between renderer functions, usually HTML attributes 2639 * 2640 * @param array $attributes 2641 * @return coursecat_helper 2642 */ 2643 public function set_attributes($attributes) { 2644 $this->attributes = $attributes; 2645 return $this; 2646 } 2647 2648 /** 2649 * Return all attributes and erases them so they are not applied again 2650 * 2651 * @param string $classname adds additional class name to the beginning of $attributes['class'] 2652 * @return array 2653 */ 2654 public function get_and_erase_attributes($classname) { 2655 $attributes = $this->attributes; 2656 $this->attributes = array(); 2657 if (empty($attributes['class'])) { 2658 $attributes['class'] = ''; 2659 } 2660 $attributes['class'] = $classname . ' '. $attributes['class']; 2661 return $attributes; 2662 } 2663 2664 /** 2665 * Sets the search criteria if the course is a search result 2666 * 2667 * Search string will be used to highlight terms in course name and description 2668 * 2669 * @param array $searchcriteria 2670 * @return coursecat_helper 2671 */ 2672 public function set_search_criteria($searchcriteria) { 2673 $this->searchcriteria = $searchcriteria; 2674 return $this; 2675 } 2676 2677 /** 2678 * Returns formatted and filtered description of the given category 2679 * 2680 * @param core_course_category $coursecat category 2681 * @param stdClass|array $options format options, by default [noclean,overflowdiv], 2682 * if context is not specified it will be added automatically 2683 * @return string|null 2684 */ 2685 public function get_category_formatted_description($coursecat, $options = null) { 2686 if ($coursecat->id && $coursecat->is_uservisible() && !empty($coursecat->description)) { 2687 if (!isset($coursecat->descriptionformat)) { 2688 $descriptionformat = FORMAT_MOODLE; 2689 } else { 2690 $descriptionformat = $coursecat->descriptionformat; 2691 } 2692 if ($options === null) { 2693 $options = array('noclean' => true, 'overflowdiv' => true); 2694 } else { 2695 $options = (array)$options; 2696 } 2697 $context = context_coursecat::instance($coursecat->id); 2698 if (!isset($options['context'])) { 2699 $options['context'] = $context; 2700 } 2701 $text = file_rewrite_pluginfile_urls($coursecat->description, 2702 'pluginfile.php', $context->id, 'coursecat', 'description', null); 2703 return format_text($text, $descriptionformat, $options); 2704 } 2705 return null; 2706 } 2707 2708 /** 2709 * Returns given course's summary with proper embedded files urls and formatted 2710 * 2711 * @param core_course_list_element $course 2712 * @param array|stdClass $options additional formatting options 2713 * @return string 2714 */ 2715 public function get_course_formatted_summary($course, $options = array()) { 2716 global $CFG; 2717 require_once($CFG->libdir. '/filelib.php'); 2718 if (!$course->has_summary()) { 2719 return ''; 2720 } 2721 $options = (array)$options; 2722 $context = context_course::instance($course->id); 2723 if (!isset($options['context'])) { 2724 // TODO see MDL-38521 2725 // option 1 (current), page context - no code required 2726 // option 2, system context 2727 // $options['context'] = context_system::instance(); 2728 // option 3, course context: 2729 // $options['context'] = $context; 2730 // option 4, course category context: 2731 // $options['context'] = $context->get_parent_context(); 2732 } 2733 $summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course', 'summary', null); 2734 $summary = format_text($summary, $course->summaryformat, $options, $course->id); 2735 if (!empty($this->searchcriteria['search'])) { 2736 $summary = highlight($this->searchcriteria['search'], $summary); 2737 } 2738 return $summary; 2739 } 2740 2741 /** 2742 * Returns course name as it is configured to appear in courses lists formatted to course context 2743 * 2744 * @param core_course_list_element $course 2745 * @param array|stdClass $options additional formatting options 2746 * @return string 2747 */ 2748 public function get_course_formatted_name($course, $options = array()) { 2749 $options = (array)$options; 2750 if (!isset($options['context'])) { 2751 $options['context'] = context_course::instance($course->id); 2752 } 2753 $name = format_string(get_course_display_name_for_list($course), true, $options); 2754 if (!empty($this->searchcriteria['search'])) { 2755 $name = highlight($this->searchcriteria['search'], $name); 2756 } 2757 return $name; 2758 } 2759 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body