Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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