| 1 | 
           efrain | 
           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 section which isn't really used.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package core_availability
  | 
        
        
            | 
            | 
           21 | 
            * @copyright 2019 Ferran Recio <ferran@moodle.com>
  | 
        
        
            | 
            | 
           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 section which isn't really used.
  | 
        
        
            | 
            | 
           31 | 
            *
  | 
        
        
            | 
            | 
           32 | 
            * @package core_availability
  | 
        
        
            | 
            | 
           33 | 
            * @copyright 2019 Ferran Recio
  | 
        
        
            | 
            | 
           34 | 
            * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           35 | 
            */
  | 
        
        
            | 
            | 
           36 | 
           class mock_info_section extends info_section {
  | 
        
        
            | 
            | 
           37 | 
               /** @var int User id for modinfo */
  | 
        
        
            | 
            | 
           38 | 
               protected $userid;
  | 
        
        
            | 
            | 
           39 | 
              | 
        
        
            | 
            | 
           40 | 
               /** @var \section_info Section. */
  | 
        
        
            | 
            | 
           41 | 
               protected $section;
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
               /**
  | 
        
        
            | 
            | 
           44 | 
                * Constructs with item details.
  | 
        
        
            | 
            | 
           45 | 
                *
  | 
        
        
            | 
            | 
           46 | 
                * @param int $userid Userid for modinfo (if used)
  | 
        
        
            | 
            | 
           47 | 
                * @param \section_info $section Section object
  | 
        
        
            | 
            | 
           48 | 
                */
  | 
        
        
           | 1441 | 
           ariadna | 
           49 | 
               public function __construct($userid = 0, ?\section_info $section = null) {
  | 
        
        
           | 1 | 
           efrain | 
           50 | 
                   parent::__construct($section);
  | 
        
        
            | 
            | 
           51 | 
                   $this->userid = $userid;
  | 
        
        
            | 
            | 
           52 | 
                   $this->section = $section;
  | 
        
        
            | 
            | 
           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 Section';
  | 
        
        
            | 
            | 
           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 section info.
  | 
        
        
            | 
            | 
           110 | 
                *
  | 
        
        
            | 
            | 
           111 | 
                * @param \section_info $section
  | 
        
        
            | 
            | 
           112 | 
                */
  | 
        
        
            | 
            | 
           113 | 
               public function set_section(\section_info $section) {
  | 
        
        
            | 
            | 
           114 | 
                   $this->section = $section;
  | 
        
        
            | 
            | 
           115 | 
               }
  | 
        
        
            | 
            | 
           116 | 
           }
  |