Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401] [Versions 401 and 402] [Versions 401 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              'stealth' => $cm->is_stealth(),
  89              'sectionid' => $section->id,
  90              'sectionnumber' => $section->section,
  91              'uservisible' => $cm->uservisible,
  92              'hascmrestrictions' => $this->get_has_restrictions(),
  93              'module' => $cm->modname,
  94              'plugin' => 'mod_' . $cm->modname,
  95              'indent' => ($format->uses_indentation()) ? $cm->indent : 0,
  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          return $data;
 121      }
 122  
 123      /**
 124       * Return if the activity has a restrictions icon displayed or not.
 125       *
 126       * @return bool if the activity has visible restrictions for the user.
 127       */
 128      protected function get_has_restrictions(): bool {
 129          global $CFG;
 130          $cm = $this->cm;
 131  
 132          if (empty($cm->visible) || empty($CFG->enableavailability)) {
 133              return false;
 134          }
 135          // Nothing to be displayed to the user.
 136          if (!$cm->is_visible_on_course_page()) {
 137              return false;
 138          }
 139          // Not allowed to see the module but might be allowed to see some availability.
 140          if (!$cm->uservisible) {
 141              return !empty($cm->availableinfo);
 142          }
 143          // Content editors can see all restrictions if the activity is visible.
 144          if (has_capability('moodle/course:viewhiddenactivities', $cm->context)) {
 145              $ci = new info_module($cm);
 146              return !empty($ci->get_full_information());
 147          }
 148          // Regular users can only see restrictions if apply to them.
 149          return false;
 150      }
 151  }