Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Base class for question events.
19
 *
20
 * @package    core
21
 * @copyright  2019 Stephen Bourget
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core\event;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
/**
30
 * Base class for question events.
31
 *
32
 * @package    core
33
 * @since      Moodle 3.7
34
 * @copyright  2019 Stephen Bourget
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
abstract class question_base extends base {
38
 
39
    /**
40
     * Init method.
41
     */
42
    protected function init() {
43
        $this->data['objecttable'] = 'question';
44
        $this->data['edulevel'] = self::LEVEL_TEACHING;
45
    }
46
 
47
    /**
48
     * Returns relevant URL.
49
     *
50
     * @return \moodle_url
51
     */
52
    public function get_url() {
53
        $cat = $this->other['categoryid'] . ',' . $this->contextid;
1441 ariadna 54
 
55
        return new \moodle_url(
56
            '/question/edit.php',
57
            ['cmid' => $this->contextinstanceid, 'cat' => $cat, 'lastchanged' => $this->objectid]
58
        );
1 efrain 59
    }
60
 
61
    /**
62
     * Custom validations.
63
     *
64
     * @throws \coding_exception
65
     * @return void
66
     */
67
    protected function validate_data() {
68
        parent::validate_data();
69
 
70
        if (!isset($this->other['categoryid'])) {
71
            throw new \coding_exception('The \'categoryid\' must be set in \'other\'.');
72
        }
73
    }
74
 
75
    /**
76
     * Returns DB mappings used with backup / restore.
77
     *
78
     * @return array
79
     */
80
    public static function get_objectid_mapping() {
81
        return ['db' => 'question', 'restore' => 'question'];
82
    }
83
 
84
    /**
85
     * Used for maping events on restore
86
     *
87
     * @return array
88
     */
89
    public static function get_other_mapping() {
90
 
91
        $othermapped = [];
92
        $othermapped['categoryid'] = ['db' => 'question_categories', 'restore' => 'question_categories'];
93
        return $othermapped;
94
    }
95
 
96
    /**
97
     * Create a event from question object
98
     *
99
     * @param object $question
100
     * @param object|null $context
101
     * @param array|null $other will override the categoryid pre-filled out on the first line.
102
     * @return base
103
     * @throws \coding_exception
104
     */
105
    public static function create_from_question_instance($question, $context = null, $other = null) {
106
 
107
        $params = ['objectid' => $question->id, 'other' => ['categoryid' => $question->category]];
108
 
109
        if (!empty($question->contextid)) {
110
            $params['contextid'] = $question->contextid;
111
        }
112
 
113
        $params['context'] = $context;
114
 
115
        if (!empty($other)) {
116
            $params['other'] = $other;
117
        }
118
 
119
        $event = self::create($params);
120
        return $event;
121
    }
122
}
123