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  /**
  18   * Mock condition.
  19   *
  20   * @package core_availability
  21   * @copyright 2014 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace availability_mock;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Mock condition.
  31   *
  32   * @package core_availability
  33   * @copyright 2014 The Open University
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class condition extends \core_availability\condition {
  37      /** @var bool True if available */
  38      protected $available;
  39      /** @var string Message if not available */
  40      protected $message;
  41      /** @var bool True if available for all (normal state) */
  42      protected $forall;
  43      /** @var bool True if available for all (NOT state) */
  44      protected $forallnot;
  45      /** @var string Dependency table (empty if none) */
  46      protected $dependtable;
  47      /** @var id Dependency id (0 if none) */
  48      protected $dependid;
  49      /** @var array Array of user ids for filter results, empty if no filter support */
  50      protected $filter;
  51  
  52      /**
  53       * Constructs a mock condition with given structure.
  54       *
  55       * @param \stdClass $structure Structure object
  56       */
  57      public function __construct($structure) {
  58          $this->available = isset($structure->a) ? $structure->a : false;
  59          $this->message = isset($structure->m) ? $structure->m : '';
  60          $this->forall = isset($structure->all) ? $structure->all : false;
  61          $this->forallnot = isset($structure->allnot) ? $structure->allnot : false;
  62          $this->dependtable = isset($structure->table) ? $structure->table : '';
  63          $this->dependid = isset($structure->id) ? $structure->id : 0;
  64          $this->filter = isset($structure->filter) ? $structure->filter : array();
  65      }
  66  
  67      public function save() {
  68          return (object)array('a' => $this->available, 'm' => $this->message,
  69                  'all' => $this->forall, 'allnot' => $this->forallnot,
  70                  'table' => $this->dependtable, 'id' => $this->dependid,
  71                  'filter' => $this->filter);
  72      }
  73  
  74      public function is_available($not, \core_availability\info $info, $grabthelot, $userid) {
  75          return $not ? !$this->available : $this->available;
  76      }
  77  
  78      public function is_available_for_all($not = false) {
  79          return $not ? $this->forallnot : $this->forall;
  80      }
  81  
  82      public function get_description($full, $not, \core_availability\info $info) {
  83          $fulltext = $full ? '[FULL]' : '';
  84          $nottext = $not ? '!' : '';
  85          return $nottext . $fulltext . $this->message;
  86      }
  87  
  88      public function get_standalone_description(
  89              $full, $not, \core_availability\info $info) {
  90          // Override so that we can spot that this function is used.
  91          return 'SA: ' . $this->get_description($full, $not, $info);
  92      }
  93  
  94      public function update_dependency_id($table, $oldid, $newid) {
  95          if ($table === $this->dependtable && (int)$oldid === (int)$this->dependid) {
  96              $this->dependid = $newid;
  97              return true;
  98          } else {
  99              return false;
 100          }
 101      }
 102  
 103      protected function get_debug_string() {
 104          return ($this->available ? 'y' : 'n') . ',' . $this->message;
 105      }
 106  
 107      public function is_applied_to_user_lists() {
 108          return $this->filter;
 109      }
 110  
 111      public function filter_user_list(array $users, $not, \core_availability\info $info,
 112              \core_availability\capability_checker $checker) {
 113          $result = array();
 114          foreach ($users as $id => $user) {
 115              $match = in_array($id, $this->filter);
 116              if ($not) {
 117                  $match = !$match;
 118              }
 119              if ($match) {
 120                  $result[$id] = $user;
 121              }
 122          }
 123          return $result;
 124      }
 125  
 126      public function get_user_list_sql($not, \core_availability\info $info, $onlyactive) {
 127          global $DB;
 128          // The data for this condition is not really stored in the database,
 129          // so we return SQL that contains the hard-coded user list.
 130          list ($enrolsql, $enrolparams) =
 131                  get_enrolled_sql($info->get_context(), '', 0, $onlyactive);
 132          $condition = $not ? 'NOT' : '';
 133          list ($matchsql, $matchparams) = $DB->get_in_or_equal($this->filter, SQL_PARAMS_NAMED);
 134          $sql = "SELECT userids.id
 135                    FROM ($enrolsql) userids
 136                   WHERE $condition (userids.id $matchsql)";
 137          return array($sql, array_merge($enrolparams, $matchparams));
 138      }
 139  }