Differences Between: [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace core_courseformat\output\local\state; 18 19 use core_courseformat\base as course_format; 20 use completion_info; 21 use renderer_base; 22 use section_info; 23 use cm_info; 24 use renderable; 25 use stdClass; 26 use core_availability\info_module; 27 28 defined('MOODLE_INTERNAL') || die(); 29 30 require_once($CFG->libdir . '/completionlib.php'); 31 32 /** 33 * Contains the ajax update course module structure. 34 * 35 * @package core_course 36 * @copyright 2021 Ferran Recio <ferran@moodle.com> 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 39 class cm implements renderable { 40 41 /** @var course_format the course format class */ 42 protected $format; 43 44 /** @var section_info the course section class */ 45 protected $section; 46 47 /** @var bool if cmitem HTML content must be exported as well */ 48 protected $exportcontent; 49 50 /** @var cm_info the course module to display */ 51 protected $cm; 52 53 /** 54 * Constructor. 55 * 56 * @param course_format $format the course format 57 * @param section_info $section the section data 58 * @param cm_info $cm the course module data 59 * @param bool $exportcontent = false if pre-rendered cmitem must be exported. 60 */ 61 public function __construct(course_format $format, section_info $section, cm_info $cm, bool $exportcontent = false) { 62 $this->format = $format; 63 $this->section = $section; 64 $this->cm = $cm; 65 $this->exportcontent = $exportcontent; 66 } 67 68 /** 69 * Export this data so it can be used as state object in the course editor. 70 * 71 * @param renderer_base $output typically, the renderer that's calling this function 72 * @return stdClass data context for a mustache template 73 */ 74 public function export_for_template(renderer_base $output): stdClass { 75 global $CFG, $USER; 76 77 $format = $this->format; 78 $section = $this->section; 79 $cm = $this->cm; 80 $course = $format->get_course(); 81 82 $data = (object)[ 83 'id' => $cm->id, 84 'anchor' => "module-{$cm->id}", 85 'name' => \core_external\util::format_string($cm->name, $cm->context, true), 86 'visible' => !empty($cm->visible), 87 'stealth' => $cm->is_stealth(), 88 'sectionid' => $section->id, 89 'sectionnumber' => $section->section, 90 'uservisible' => $cm->uservisible, 91 'hascmrestrictions' => $this->get_has_restrictions(), 92 'modname' => get_string('pluginname', 'mod_' . $cm->modname), 93 'indent' => ($format->uses_indentation()) ? $cm->indent : 0, 94 'groupmode' => $cm->groupmode, 95 'module' => $cm->modname, 96 'plugin' => 'mod_' . $cm->modname, 97 ]; 98 99 // Check the user access type to this cm. 100 $info = new info_module($cm); 101 $data->accessvisible = ($data->visible && $info->is_available_for_all()); 102 103 // Add url if the activity is compatible. 104 $url = $cm->url; 105 if ($url) { 106 $data->url = $url->out(); 107 } 108 109 if ($this->exportcontent) { 110 $data->content = $output->course_section_updated_cm_item($format, $section, $cm); 111 } 112 113 // Completion status. 114 $completioninfo = new completion_info($course); 115 $data->istrackeduser = $completioninfo->is_tracked_user($USER->id); 116 if ($data->istrackeduser && $completioninfo->is_enabled($cm)) { 117 $completiondata = $completioninfo->get_data($cm); 118 $data->completionstate = $completiondata->completionstate; 119 } 120 121 $data->allowstealth = !empty($CFG->allowstealth) && $format->allow_stealth_module_visibility($cm, $section); 122 123 return $data; 124 } 125 126 /** 127 * Return if the activity has a restrictions icon displayed or not. 128 * 129 * @return bool if the activity has visible restrictions for the user. 130 */ 131 protected function get_has_restrictions(): bool { 132 global $CFG; 133 $cm = $this->cm; 134 135 if (empty($cm->visible) || empty($CFG->enableavailability)) { 136 return false; 137 } 138 // Nothing to be displayed to the user. 139 if (!$cm->is_visible_on_course_page()) { 140 return false; 141 } 142 // Not allowed to see the module but might be allowed to see some availability. 143 if (!$cm->uservisible) { 144 return !empty($cm->availableinfo); 145 } 146 // Content editors can see all restrictions if the activity is visible. 147 if (has_capability('moodle/course:viewhiddenactivities', $cm->context)) { 148 $ci = new info_module($cm); 149 return !empty($ci->get_full_information()); 150 } 151 // Regular users can only see restrictions if apply to them. 152 return false; 153 } 154 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body