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.
   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 format_topics\courseformat;
  18  
  19  use core_courseformat\stateupdates;
  20  use core_courseformat\stateactions as stateactions_base;
  21  use core\event\course_module_updated;
  22  use cm_info;
  23  use section_info;
  24  use stdClass;
  25  use course_modinfo;
  26  use moodle_exception;
  27  use context_module;
  28  use context_course;
  29  
  30  /**
  31   * Contains the core course state actions specific to topics format.
  32   *
  33   * @package    format_topics
  34   * @copyright  2022 Ferran Recio <ferran@moodle.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class stateactions extends stateactions_base {
  38  
  39      /**
  40       * Highlight course section.
  41       *
  42       * @param stateupdates $updates the affected course elements track
  43       * @param stdClass $course the course object
  44       * @param int[] $ids section ids (only ther first one will be highlighted)
  45       * @param int $targetsectionid not used
  46       * @param int $targetcmid not used
  47       */
  48      public function section_highlight(
  49          stateupdates $updates,
  50          stdClass $course,
  51          array $ids = [],
  52          ?int $targetsectionid = null,
  53          ?int $targetcmid = null
  54      ): void {
  55          global $DB;
  56  
  57          $this->validate_sections($course, $ids, __FUNCTION__);
  58          $coursecontext = context_course::instance($course->id);
  59          require_capability('moodle/course:setcurrentsection', $coursecontext);
  60  
  61          // Get the previous marked section.
  62          $modinfo = get_fast_modinfo($course);
  63          $previousmarker = $DB->get_field("course", "marker", ['id' => $course->id]);
  64  
  65          $section = $modinfo->get_section_info_by_id(reset($ids), MUST_EXIST);
  66          if ($section->section == $previousmarker) {
  67              return;
  68          }
  69  
  70          // Mark the new one.
  71          course_set_marker($course->id, $section->section);
  72          $updates->add_section_put($section->id);
  73          if ($previousmarker) {
  74              $section = $modinfo->get_section_info($previousmarker);
  75              $updates->add_section_put($section->id);
  76          }
  77      }
  78  
  79      /**
  80       * Remove highlight from a course sections.
  81       *
  82       * @param stateupdates $updates the affected course elements track
  83       * @param stdClass $course the course object
  84       * @param int[] $ids optional extra section ids to refresh
  85       * @param int $targetsectionid not used
  86       * @param int $targetcmid not used
  87       */
  88      public function section_unhighlight(
  89          stateupdates $updates,
  90          stdClass $course,
  91          array $ids = [],
  92          ?int $targetsectionid = null,
  93          ?int $targetcmid = null
  94      ): void {
  95          global $DB;
  96  
  97          $this->validate_sections($course, $ids, __FUNCTION__);
  98          $coursecontext = context_course::instance($course->id);
  99          require_capability('moodle/course:setcurrentsection', $coursecontext);
 100  
 101          $affectedsections = [];
 102  
 103          // Get the previous marked section and unmark it.
 104          $modinfo = get_fast_modinfo($course);
 105          $previousmarker = $DB->get_field("course", "marker", ['id' => $course->id]);
 106          course_set_marker($course->id, 0);
 107          $section = $modinfo->get_section_info($previousmarker, MUST_EXIST);
 108          $updates->add_section_put($section->id);
 109  
 110          foreach ($ids as $sectionid) {
 111              $section = $modinfo->get_section_info_by_id($sectionid, MUST_EXIST);
 112              if ($section->section != $previousmarker) {
 113                  $updates->add_section_put($section->id);
 114              }
 115          }
 116      }
 117  }