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
26namespace quizaccess_seb\event;
27
28use core\event\base;
29use quizaccess_seb\access_manager;
30
31defined('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 */
39class 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     * @return base
50     */
51    public static function create_strict(access_manager $accessmanager, string $reason) : base {
52        global $USER;
53
54        $other = [];
55        $other['reason'] = $reason;
56        $other['savedconfigkey'] = $accessmanager->get_valid_config_key();
57        $other['receivedconfigkey'] = $accessmanager->get_received_config_key();
58        $other['receivedbrowserexamkey'] = $accessmanager->get_received_browser_exam_key();
59
60        return self::create([
61            'userid' => $USER->id,
62            'objectid' => $accessmanager->get_quiz()->get_quizid(),
63            'courseid' => $accessmanager->get_quiz()->get_courseid(),
64            'context' => $accessmanager->get_quiz()->get_context(),
65            'other' => $other,
66        ]);
67    }
68
69    /**
70     * Initialize the event data.
71     */
72    protected function init() {
73        $this->data['objecttable'] = 'quiz';
74        $this->data['crud'] = 'r';
75        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
76    }
77
78    /**
79     * Get the name of the event.
80     *
81     * @return string Name of event.
82     */
83    public static function get_name() {
84        return get_string('event:accessprevented', 'quizaccess_seb');
85    }
86
87    /**
88     * Returns non-localised event description with id's for admin use only.
89     *
90     * @return string Description.
91     */
92    public function get_description() {
93        $description = "The user with id '$this->userid' has been prevented from accessing quiz with id '$this->objectid' by the "
94                . "Safe Exam Browser access plugin. The reason was '{$this->other['reason']}'. "
95            . "Expected config key: '{$this->other['savedconfigkey']}'. "
96            . "Received config key: '{$this->other['receivedconfigkey']}'. "
97            . "Received browser exam key: '{$this->other['receivedbrowserexamkey']}'.";
98
99        return $description;
100    }
101
102    /**
103     * This is used when restoring course logs where it is required that we
104     * map the objectid to it's new value in the new course.
105     *
106     * @return array Mapping of object id.
107     */
108    public static function get_objectid_mapping() : array {
109        return array('db' => 'quiz', 'restore' => 'quiz');
110    }
111
112    /**
113     * This is used when restoring course logs where it is required that we
114     * map the information in 'other' to it's new value in the new course.
115     *
116     * @return array List of mapping of other ids.
117     */
118    public static function get_other_mapping() : array {
119        return [
120            'cmid' => ['db' => 'course_modules', 'restore' => 'course_modules']
121        ];
122    }
123}