Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 400 and 403] [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 course_modinfo;
  21  use moodle_url;
  22  use renderable;
  23  use stdClass;
  24  
  25  /**
  26   * Contains the ajax update course structure.
  27   *
  28   * @package   core_course
  29   * @copyright 2021 Ferran Recio <ferran@moodle.com>
  30   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class course implements renderable {
  33  
  34      /** @var course_format the course format class */
  35      protected $format;
  36  
  37      /**
  38       * Constructor.
  39       *
  40       * @param course_format $format the course format
  41       */
  42      public function __construct(course_format $format) {
  43          $this->format = $format;
  44      }
  45  
  46      /**
  47       * Export this data so it can be used as state object in the course editor.
  48       *
  49       * @param renderer_base $output typically, the renderer that's calling this function
  50       * @return stdClass data context for a mustache template
  51       */
  52      public function export_for_template(\renderer_base $output): stdClass {
  53          global $CFG;
  54  
  55          $format = $this->format;
  56          $course = $format->get_course();
  57          $context = $format->get_context();
  58          // State must represent always the most updated version of the course.
  59          $modinfo = course_modinfo::instance($course);
  60  
  61          $url = new moodle_url('/course/view.php', ['id' => $course->id]);
  62          $maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes, $course->maxbytes);
  63  
  64          $data = (object)[
  65              'id' => $course->id,
  66              'numsections' => $format->get_last_section_number(),
  67              'sectionlist' => [],
  68              'editmode' => $format->show_editor(),
  69              'highlighted' => $format->get_section_highlighted_name(),
  70              'maxsections' => $format->get_max_sections(),
  71              'baseurl' => $url->out(),
  72              'statekey' => course_format::session_cache($course),
  73              'maxbytes' => $maxbytes,
  74              'maxbytestext' => display_size($maxbytes),
  75          ];
  76  
  77  
  78          $sections = $modinfo->get_section_info_all();
  79          foreach ($sections as $section) {
  80              if ($format->is_section_visible($section)) {
  81                  $data->sectionlist[] = $section->id;
  82              }
  83          }
  84  
  85          return $data;
  86      }
  87  }