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.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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   * Event for when access to a quiz is prevented by this subplugin.
  19   *
  20   * @package    quizaccess_seb
  21   * @author     Andrew Madden <andrewmadden@catalyst-au.net>
  22   * @copyright  2020 Catalyst IT
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace quizaccess_seb\event;
  27  
  28  use core\event\base;
  29  use quizaccess_seb\access_manager;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  /**
  34   * Event for when access to a quiz is prevented by this subplugin.
  35   *
  36   * @copyright  2020 Catalyst IT
  37   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class access_prevented extends base {
  40  
  41      /**
  42       * Create event with strict parameters.
  43       *
  44       * Define strict parameters to create event with instead of relying on internal validation of array. Better code practice.
  45       * Easier for consumers of this class to know what data must be supplied and observers can have more trust in event data.
  46       *
  47       * @param access_manager $accessmanager Access manager.
  48       * @param string $reason Reason that access was prevented.
  49       * @param string|null $configkey A Safe Exam Browser config key.
  50       * @param string|null $browserexamkey A Safe Exam Browser browser exam key.
  51       * @return base
  52       */
  53      public static function create_strict(access_manager $accessmanager, string $reason,
  54              ?string $configkey = null, ?string $browserexamkey = null) : base {
  55          global $USER;
  56  
  57          $other = [];
  58          $other['reason'] = $reason;
  59          $other['savedconfigkey'] = $accessmanager->get_valid_config_key();
  60          $other['receivedconfigkey'] = !empty($configkey) ? $configkey : $accessmanager->get_received_config_key();
  61          $other['receivedbrowserexamkey'] = !empty($browserexamkey) ? $browserexamkey
  62                  : $accessmanager->get_received_browser_exam_key();
  63  
  64          return self::create([
  65              'userid' => $USER->id,
  66              'objectid' => $accessmanager->get_quiz()->get_quizid(),
  67              'courseid' => $accessmanager->get_quiz()->get_courseid(),
  68              'context' => $accessmanager->get_quiz()->get_context(),
  69              'other' => $other,
  70          ]);
  71      }
  72  
  73      /**
  74       * Initialize the event data.
  75       */
  76      protected function init() {
  77          $this->data['objecttable'] = 'quiz';
  78          $this->data['crud'] = 'r';
  79          $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
  80      }
  81  
  82      /**
  83       * Get the name of the event.
  84       *
  85       * @return string Name of event.
  86       */
  87      public static function get_name() {
  88          return get_string('event:accessprevented', 'quizaccess_seb');
  89      }
  90  
  91      /**
  92       * Returns non-localised event description with id's for admin use only.
  93       *
  94       * @return string Description.
  95       */
  96      public function get_description() {
  97          $description = "The user with id '$this->userid' has been prevented from accessing quiz with id '$this->objectid' by the "
  98                  . "Safe Exam Browser access plugin. The reason was '{$this->other['reason']}'. "
  99              . "Expected config key: '{$this->other['savedconfigkey']}'. "
 100              . "Received config key: '{$this->other['receivedconfigkey']}'. "
 101              . "Received browser exam key: '{$this->other['receivedbrowserexamkey']}'.";
 102  
 103          return $description;
 104      }
 105  
 106      /**
 107       * This is used when restoring course logs where it is required that we
 108       * map the objectid to it's new value in the new course.
 109       *
 110       * @return array Mapping of object id.
 111       */
 112      public static function get_objectid_mapping() : array {
 113          return array('db' => 'quiz', 'restore' => 'quiz');
 114      }
 115  
 116      /**
 117       * This is used when restoring course logs where it is required that we
 118       * map the information in 'other' to it's new value in the new course.
 119       *
 120       * @return array List of mapping of other ids.
 121       */
 122      public static function get_other_mapping() : array {
 123          return [
 124              'cmid' => ['db' => 'course_modules', 'restore' => 'course_modules']
 125          ];
 126      }
 127  }