Proyectos de Subversion Moodle

Rev

| 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
namespace core_xapi;
18
 
19
use core_xapi\local\state;
20
use core_xapi\local\statement;
21
use core_xapi\xapi_exception;
22
 
23
/**
24
 * Class handler handles basic xAPI statements and states.
25
 *
26
 * @package    core_xapi
27
 * @since      Moodle 3.9
28
 * @copyright  2020 Ferran Recio
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
abstract class handler {
32
 
33
    /** @var string component name in frankenstyle. */
34
    protected $component;
35
 
36
    /** @var state_store the state_store instance. */
37
    protected $statestore;
38
 
39
    /**
40
     * Constructor for a xAPI handler base class.
41
     *
42
     * @param string $component the component name
43
     */
44
    final protected function __construct(string $component) {
45
        $this->component = $component;
46
        $this->statestore = $this->get_state_store();
47
    }
48
 
49
    /**
50
     * Returns the xAPI handler of a specific component.
51
     *
52
     * @param string $component the component name in frankenstyle.
53
     * @return handler|null a handler object or null if none found.
54
     * @throws xapi_exception
55
     */
56
    final public static function create(string $component): self {
57
        if (self::supports_xapi($component)) {
58
            $classname = "\\$component\\xapi\\handler";
59
            return new $classname($component);
60
        }
61
        throw new xapi_exception('Unknown handler');
62
    }
63
 
64
    /**
65
     * Whether a component supports (and implements) xAPI.
66
     *
67
     * @param string $component the component name in frankenstyle.
68
     * @return bool true if the given component implements xAPI handler; false otherwise.
69
     */
70
    final public static function supports_xapi(string $component): bool {
71
        $classname = "\\$component\\xapi\\handler";
72
        return class_exists($classname);
73
    }
74
 
75
    /**
76
     * Convert a statement object into a Moodle xAPI Event.
77
     *
78
     * If a statement is accepted by validate_statement the component must provide a event
79
     * to handle that statement, otherwise the statement will be rejected.
80
     *
81
     * Note: this method must be overridden by the plugins which want to use xAPI.
82
     *
83
     * @param statement $statement
84
     * @return \core\event\base|null a Moodle event to trigger
85
     */
86
    abstract public function statement_to_event(statement $statement): ?\core\event\base;
87
 
88
    /**
89
     * Return true if group actor is enabled.
90
     *
91
     * Note: this method must be overridden by the plugins which want to
92
     * use groups in statements.
93
     *
94
     * @return bool
95
     */
96
    public function supports_group_actors(): bool {
97
        return false;
98
    }
99
 
100
    /**
101
     * Process a bunch of statements sended to a specific component.
102
     *
103
     * @param statement[] $statements an array with all statement to process.
104
     * @return int[] return an specifying what statements are being stored.
105
     */
106
    public function process_statements(array $statements): array {
107
        $result = [];
108
        foreach ($statements as $key => $statement) {
109
            try {
110
                // Ask the plugin to convert into an event.
111
                $event = $this->statement_to_event($statement);
112
                if ($event) {
113
                    $event->trigger();
114
                    $result[$key] = true;
115
                } else {
116
                    $result[$key] = false;
117
                }
118
            } catch (\Exception $e) {
119
                $result[$key] = false;
120
            }
121
        }
122
        return $result;
123
    }
124
 
125
    /**
126
     * Validate a xAPI state.
127
     *
128
     * Check if the state is valid for this handler.
129
     *
130
     * This method is used also for the state get requests so the validation
131
     * cannot rely on having state data.
132
     *
133
     * Note: this method must be overridden by the plugins which want to use xAPI states.
134
     *
135
     * @param state $state
136
     * @return bool if the state is valid or not
137
     */
138
    abstract protected function validate_state(state $state): bool;
139
 
140
    /**
141
     * Process a state save request.
142
     *
143
     * @param state $state the state object
144
     * @return bool if the state can be saved
145
     */
146
    public function save_state(state $state): bool {
147
        if (!$this->validate_state($state)) {
148
            throw new xapi_exception('The state is not accepted, so it cannot be saved');
149
        }
150
        return $this->statestore->put($state);
151
    }
152
 
153
    /**
154
     * Process a state save request.
155
     *
156
     * @param state $state the state object
157
     * @return state|null the resulting loaded state
158
     */
159
    public function load_state(state $state): ?state {
160
        if (!$this->validate_state($state)) {
161
            throw new xapi_exception('The state is not accepted, so it cannot be loaded');
162
        }
163
        $state = $this->statestore->get($state);
164
        return $state;
165
    }
166
 
167
    /**
168
     * Process a state delete request.
169
     *
170
     * @param state $state the state object
171
     * @return bool if the deletion is successful
172
     */
173
    public function delete_state(state $state): bool {
174
        if (!$this->validate_state($state)) {
175
            throw new xapi_exception('The state is not accepted, so it cannot be deleted');
176
        }
177
        return $this->statestore->delete($state);
178
    }
179
 
180
    /**
181
     * Delete all states from this component.
182
     *
183
     * @param string|null $itemid
184
     * @param int|null $userid
185
     * @param string|null $stateid
186
     * @param string|null $registration
187
     */
188
    public function wipe_states(
189
        ?string $itemid = null,
190
        ?int $userid = null,
191
        ?string $stateid = null,
192
        ?string $registration = null
193
    ): void {
194
        $this->statestore->wipe($itemid, $userid, $stateid, $registration);
195
    }
196
 
197
    /**
198
     * Reset all states from this component.
199
     *
200
     * @param string|null $itemid
201
     * @param int|null $userid
202
     * @param string|null $stateid
203
     * @param string|null $registration
204
     */
205
    public function reset_states(
206
        ?string $itemid = null,
207
        ?int $userid = null,
208
        ?string $stateid = null,
209
        ?string $registration = null
210
    ): void {
211
        $this->statestore->reset($itemid, $userid, $stateid, $registration);
212
    }
213
 
214
    /**
215
     * Return a valor state store for this component.
216
     *
217
     * Plugins may override this method is they want to use a different
218
     * state store class.
219
     * @return state_store the store to use to get/put/delete states.
220
     */
221
    public function get_state_store(): state_store {
222
        return new state_store($this->component);
223
    }
224
}