Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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   * Access Date filter.
  19   *
  20   * @package    tool_usertours
  21   * @copyright  2019 Tom Dickman <tomdickman@catalyst-au.net>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_usertours\local\filter;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use context;
  29  use tool_usertours\tour;
  30  
  31  /**
  32   * Access date filter. Used to determine if USER should see a tour based on a particular access date.
  33   *
  34   * @copyright  2019 Tom Dickman <tomdickman@catalyst-au.net>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class accessdate extends base {
  38  
  39      /**
  40       * Access date filtering constant for setting base date as account creation date.
  41       */
  42      const FILTER_ACCOUNT_CREATION = 'tool_usertours_accountcreation';
  43  
  44      /**
  45       * Access date filtering constant for setting base date as account first login date.
  46       */
  47      const FILTER_FIRST_LOGIN = 'tool_usertours_firstlogin';
  48  
  49      /**
  50       * Access date filtering constant for setting base date as account last login date.
  51       */
  52      const FILTER_LAST_LOGIN = 'tool_usertours_lastlogin';
  53  
  54      /**
  55       * Default this filter to not be enabled.
  56       */
  57      const FILTER_ENABLED_DEFAULT = 0;
  58  
  59      /**
  60       * The name of the filter.
  61       *
  62       * @return  string
  63       */
  64      public static function get_filter_name() {
  65          return 'accessdate';
  66      }
  67  
  68      /**
  69       * Retrieve the list of available filter options.
  70       *
  71       * @return  array  An array whose keys are the valid options
  72       *                 And whose values are the values to display
  73       * @throws \coding_exception
  74       */
  75      public static function get_filter_options() {
  76  
  77          return array(
  78              self::FILTER_ACCOUNT_CREATION => get_string('filter_date_account_creation', 'tool_usertours'),
  79              self::FILTER_FIRST_LOGIN => get_string('filter_date_first_login', 'tool_usertours'),
  80              self::FILTER_LAST_LOGIN => get_string('filter_date_last_login', 'tool_usertours'),
  81          );
  82  
  83      }
  84  
  85      /**
  86       * Add the form elements for the filter to the supplied form.
  87       *
  88       * @param \MoodleQuickForm $mform The form to add filter settings to.
  89       *
  90       * @throws \coding_exception
  91       */
  92      public static function add_filter_to_form(\MoodleQuickForm &$mform) {
  93  
  94          $filtername = static::get_filter_name();
  95          $key = "filter_{$filtername}";
  96          $range = "{$key}_range";
  97          $enabled = "{$key}_enabled";
  98  
  99          $mform->addElement('advcheckbox', $enabled, get_string($key, 'tool_usertours'),
 100              get_string('filter_accessdate_enabled', 'tool_usertours'), null, array(0, 1));
 101          $mform->addHelpButton($enabled, $enabled, 'tool_usertours');
 102  
 103          $mform->addElement('select', $key, ' ', self::get_filter_options());
 104          $mform->setDefault($key, self::FILTER_ACCOUNT_CREATION);
 105          $mform->hideIf($key, $enabled, 'notchecked');
 106  
 107          $mform->addElement('duration', $range, null, [
 108              'optional' => false,
 109              'defaultunit' => DAYSECS,
 110          ]);
 111          $mform->setDefault($range, 90 * DAYSECS);
 112          $mform->hideIf($range, $enabled, 'notchecked');
 113  
 114      }
 115  
 116      /**
 117       * Prepare the filter values for the form.
 118       *
 119       * @param   tour            $tour       The tour to prepare values from
 120       * @param   stdClass        $data       The data value
 121       * @return  stdClass
 122       */
 123      public static function prepare_filter_values_for_form(tour $tour, \stdClass $data) {
 124          $filtername = static::get_filter_name();
 125  
 126          $key = "filter_{$filtername}";
 127          $range = "{$key}_range";
 128          $enabled = "{$key}_enabled";
 129  
 130          $values = $tour->get_filter_values($filtername);
 131  
 132          // Prepare the advanced checkbox value and prepare filter values based on previously set values.
 133          if (!empty($values)) {
 134              $data->$enabled = $values->$enabled ? $values->$enabled : self::FILTER_ENABLED_DEFAULT;
 135              if ($data->$enabled) {
 136                  if (isset($values->$key)) {
 137                      $data->$key = $values->$key;
 138                  }
 139                  if (isset($values->$range)) {
 140                      $data->$range = $values->$range;
 141                  }
 142              }
 143          } else {
 144              $data->$enabled = self::FILTER_ENABLED_DEFAULT;
 145          }
 146          return $data;
 147      }
 148  
 149      /**
 150       * Save the filter values from the form to the tour.
 151       *
 152       * @param   tour            $tour       The tour to save values to
 153       * @param   \stdClass        $data       The data submitted in the form
 154       */
 155      public static function save_filter_values_from_form(tour $tour, \stdClass $data) {
 156          $filtername = static::get_filter_name();
 157          $key = "filter_{$filtername}";
 158          $range = "{$key}_range";
 159          $enabled = "{$key}_enabled";
 160  
 161          $savedata = [];
 162          $savedata[$key] = $data->$key;
 163          $savedata[$range] = $data->$range;
 164          $savedata[$enabled] = $data->$enabled;
 165  
 166          $tour->set_filter_values($filtername, $savedata);
 167      }
 168  
 169      /**
 170       * Check whether the filter matches the specified tour and/or context.
 171       *
 172       * @param   tour        $tour       The tour to check
 173       * @param   context     $context    The context to check
 174       * @return  boolean
 175       */
 176      public static function filter_matches(tour $tour, context $context) {
 177          global $USER;
 178  
 179          $filtername = static::get_filter_name();
 180          $key = "filter_{$filtername}";
 181          $range = "{$key}_range";
 182          $enabled = "{$key}_enabled";
 183  
 184          // Default behaviour is to match filter.
 185          $result = true;
 186          $values = (array) $tour->get_filter_values(self::get_filter_name());
 187  
 188          // If the access date filter is not enabled, end here.
 189          if (empty($values[$enabled])) {
 190              return $result;
 191          }
 192  
 193          if (!empty($values[$key])) {
 194              switch ($values[$key]) {
 195                  case (self::FILTER_ACCOUNT_CREATION):
 196                      $filterbasedate = (int) $USER->timecreated;
 197                      break;
 198                  case (self::FILTER_FIRST_LOGIN):
 199                      $filterbasedate = (int) $USER->firstaccess;
 200                      break;
 201                  case (self::FILTER_LAST_LOGIN):
 202                      $filterbasedate = (int) $USER->lastlogin;
 203                      break;
 204                  default:
 205                      // Use account creation as default.
 206                      $filterbasedate = (int) $USER->timecreated;
 207                      break;
 208              }
 209              // If the base date has no value because a user hasn't accessed Moodle yet, default to account creation.
 210              if (empty($filterbasedate)) {
 211                  $filterbasedate = (int) $USER->timecreated;
 212              }
 213  
 214              if (!empty($values[$range])) {
 215                  $filterrange = (int) $values[$range];
 216              } else {
 217                  $filterrange = 90 * DAYSECS;
 218              }
 219  
 220              // If we're outside the set range from the set base date, filter out tour.
 221              if ((time() > ($filterbasedate + $filterrange))) {
 222                  $result = false;
 223              }
 224          }
 225          return $result;
 226      }
 227  }