Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * Category filter.
  19   *
  20   * @package    tool_usertours
  21   * @copyright  2017 The Open University
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_usertours\local\filter;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use tool_usertours\tour;
  30  use context;
  31  
  32  /**
  33   * Category filter.
  34   *
  35   * @copyright  2017 The Open University
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class category extends base {
  39      /**
  40       * The name of the filter.
  41       *
  42       * @return  string
  43       */
  44      public static function get_filter_name() {
  45          return 'category';
  46      }
  47  
  48      /**
  49       * Retrieve the list of available filter options.
  50       *
  51       * @return  array                   An array whose keys are the valid options
  52       *                                  And whose values are the values to display
  53       */
  54      public static function get_filter_options() {
  55          $options = \core_course_category::make_categories_list();
  56          return $options;
  57      }
  58  
  59      /**
  60       * Check whether the filter matches the specified tour and/or context.
  61       *
  62       * @param   tour        $tour       The tour to check
  63       * @param   context     $context    The context to check
  64       * @return  boolean
  65       */
  66      public static function filter_matches(tour $tour, context $context) {
  67          $values = $tour->get_filter_values(self::get_filter_name());
  68          if (empty($values) || empty($values[0])) {
  69              // There are no values configured, meaning all.
  70              return true;
  71          }
  72          if ($context->contextlevel < CONTEXT_COURSECAT) {
  73              return false;
  74          }
  75          return self::check_contexts($context, $values);
  76      }
  77  
  78      /**
  79       * Recursive function allows checking of parent categories.
  80       *
  81       * @param context $context
  82       * @param array $values
  83       * @return boolean
  84       */
  85      private static function check_contexts(context $context, $values) {
  86          if ($context->contextlevel > CONTEXT_COURSECAT) {
  87              return self::check_contexts($context->get_parent_context(), $values);
  88          } else if ($context->contextlevel == CONTEXT_COURSECAT) {
  89              if (in_array($context->instanceid, $values)) {
  90                  return true;
  91              } else {
  92                  return self::check_contexts($context->get_parent_context(), $values);
  93              }
  94          } else {
  95              return false;
  96          }
  97      }
  98  }