Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 400 and 402] [Versions 401 and 402] [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              'module' => $cm->modname,
  95              'plugin' => 'mod_' . $cm->modname,
  96          ];
  97  
  98          // Check the user access type to this cm.
  99          $info = new info_module($cm);
 100          $data->accessvisible = ($data->visible && $info->is_available_for_all());
 101  
 102          // Add url if the activity is compatible.
 103          $url = $cm->url;
 104          if ($url) {
 105              $data->url = $url->out();
 106          }
 107  
 108          if ($this->exportcontent) {
 109              $data->content = $output->course_section_updated_cm_item($format, $section, $cm);
 110          }
 111  
 112          // Completion status.
 113          $completioninfo = new completion_info($course);
 114          $data->istrackeduser = $completioninfo->is_tracked_user($USER->id);
 115          if ($data->istrackeduser && $completioninfo->is_enabled($cm)) {
 116              $completiondata = $completioninfo->get_data($cm);
 117              $data->completionstate = $completiondata->completionstate;
 118          }
 119  
 120          $data->allowstealth = !empty($CFG->allowstealth) && $format->allow_stealth_module_visibility($cm, $section);
 121  
 122          return $data;
 123      }
 124  
 125      /**
 126       * Return if the activity has a restrictions icon displayed or not.
 127       *
 128       * @return bool if the activity has visible restrictions for the user.
 129       */
 130      protected function get_has_restrictions(): bool {
 131          global $CFG;
 132          $cm = $this->cm;
 133  
 134          if (empty($cm->visible) || empty($CFG->enableavailability)) {
 135              return false;
 136          }
 137          // Nothing to be displayed to the user.
 138          if (!$cm->is_visible_on_course_page()) {
 139              return false;
 140          }
 141          // Not allowed to see the module but might be allowed to see some availability.
 142          if (!$cm->uservisible) {
 143              return !empty($cm->availableinfo);
 144          }
 145          // Content editors can see all restrictions if the activity is visible.
 146          if (has_capability('moodle/course:viewhiddenactivities', $cm->context)) {
 147              $ci = new info_module($cm);
 148              return !empty($ci->get_full_information());
 149          }
 150          // Regular users can only see restrictions if apply to them.
 151          return false;
 152      }
 153  }