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.
   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  /**
  18   * For use in unit tests that require an info module which isn't really used.
  19   *
  20   * @package core_availability
  21   * @copyright 2019 Ferran Recio
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_availability;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * For use in unit tests that require an info module which isn't really used.
  31   *
  32   * @package core_availability
  33   * @copyright 2019 Ferran Recio <ferran@moodle.com>
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class mock_info_module extends info_module {
  37      /** @var int User id for modinfo */
  38      protected $userid;
  39  
  40      /** @var \cm_info Activity. */
  41      protected $cm;
  42  
  43      /**
  44       * Constructs with item details.
  45       *
  46       * @param int $userid Userid for modinfo (if used)
  47       * @param \cm_info $cm Course-module object
  48       */
  49      public function __construct($userid = 0, \cm_info $cm = null) {
  50          parent::__construct($cm);
  51          $this->userid = $userid;
  52          $this->cm = $cm;
  53      }
  54  
  55      /**
  56       * Just returns a mock name.
  57       *
  58       * @return string Name of item
  59       */
  60      protected function get_thing_name() {
  61          return 'Mock Module';
  62      }
  63  
  64      /**
  65       * Returns the current context.
  66       *
  67       * @return \context Context for this item
  68       */
  69      public function get_context() {
  70          return \context_course::instance($this->get_course()->id);
  71      }
  72  
  73      /**
  74       * Returns the cappability used to ignore access restrictions.
  75       *
  76       * @return string Name of capability used to view hidden items of this type
  77       */
  78      protected function get_view_hidden_capability() {
  79          return 'moodle/course:ignoreavailabilityrestrictions';
  80      }
  81  
  82      /**
  83       * Mocks don't need to save anything into DB.
  84       *
  85       * @param string $availability New JSON value
  86       */
  87      protected function set_in_database($availability) {
  88      }
  89  
  90      /**
  91       * Obtains the modinfo associated with this availability information.
  92       *
  93       * Note: This field is available ONLY for use by conditions when calculating
  94       * availability or information.
  95       *
  96       * @return \course_modinfo Modinfo
  97       * @throws \coding_exception If called at incorrect times
  98       */
  99      public function get_modinfo() {
 100          // Allow modinfo usage outside is_available etc., so we can use this
 101          // to directly call into condition is_available.
 102          if (!$this->userid) {
 103              throw new \coding_exception('Need to set mock_info userid');
 104          }
 105          return get_fast_modinfo($this->course, $this->userid);
 106      }
 107  
 108      /**
 109       * Override course-module info.
 110       * @param \cm_info $cm
 111       */
 112      public function set_cm (\cm_info $cm) {
 113          $this->cm = $cm;
 114      }
 115  }