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 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 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          $format = $this->format;
  54          $course = $format->get_course();
  55          // State must represent always the most updated version of the course.
  56          $modinfo = course_modinfo::instance($course);
  57  
  58          $url = new moodle_url('/course/view.php', ['id' => $course->id]);
  59  
  60          $data = (object)[
  61              'id' => $course->id,
  62              'numsections' => $format->get_last_section_number(),
  63              'sectionlist' => [],
  64              'editmode' => $format->show_editor(),
  65              'highlighted' => $format->get_section_highlighted_name(),
  66              'maxsections' => $format->get_max_sections(),
  67              'baseurl' => $url->out(),
  68              'statekey' => course_format::session_cache($course),
  69          ];
  70  
  71          $sections = $modinfo->get_section_info_all();
  72          foreach ($sections as $section) {
  73              if ($format->is_section_visible($section)) {
  74                  $data->sectionlist[] = $section->id;
  75              }
  76          }
  77  
  78          return $data;
  79      }
  80  }