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 |
* The core_xapi test class for xAPI statements.
|
|
|
19 |
*
|
|
|
20 |
* @package core_xapi
|
|
|
21 |
* @since Moodle 3.9
|
|
|
22 |
* @copyright 2020 Ferran Recio
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
namespace fake_component\xapi;
|
|
|
27 |
|
|
|
28 |
use core_xapi\local\statement;
|
|
|
29 |
use core_xapi\handler as handler_base;
|
|
|
30 |
use core_xapi\event\xapi_test_statement_post;
|
|
|
31 |
use context_system;
|
|
|
32 |
use core\event\base;
|
|
|
33 |
use core_xapi\local\state;
|
|
|
34 |
|
|
|
35 |
defined('MOODLE_INTERNAL') || die();
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Class xapi_handler testing dummie class.
|
|
|
39 |
*
|
|
|
40 |
* @package core_xapi
|
|
|
41 |
* @since Moodle 3.9
|
|
|
42 |
* @copyright 2020 Ferran Recio
|
|
|
43 |
*/
|
|
|
44 |
class handler extends handler_base {
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Convert statements to event.
|
|
|
48 |
*
|
|
|
49 |
* Convert a statement object into a Moodle xAPI Event. If a statement is accepted
|
|
|
50 |
* by validate_statement the component must provide a event to handle that statement,
|
|
|
51 |
* otherwise the statement will be rejected.
|
|
|
52 |
*
|
|
|
53 |
* @param statement $statement the statement object
|
|
|
54 |
* @return \core\event\base|null a Moodle event to trigger
|
|
|
55 |
*/
|
|
|
56 |
public function statement_to_event(statement $statement): ?base {
|
|
|
57 |
global $USER;
|
|
|
58 |
|
|
|
59 |
// Validate verb.
|
|
|
60 |
$validvalues = [
|
|
|
61 |
'cook',
|
|
|
62 |
'http://adlnet.gov/expapi/verbs/answered'
|
|
|
63 |
];
|
|
|
64 |
$verbid = $statement->get_verb_id();
|
|
|
65 |
if (!in_array($verbid, $validvalues)) {
|
|
|
66 |
return null;
|
|
|
67 |
}
|
|
|
68 |
// Validate object.
|
|
|
69 |
$validvalues = [
|
|
|
70 |
'paella',
|
|
|
71 |
'http://adlnet.gov/expapi/activities/example'
|
|
|
72 |
];
|
|
|
73 |
$activityid = $statement->get_activity_id();
|
|
|
74 |
if (!in_array($activityid, $validvalues)) {
|
|
|
75 |
return null;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
if ($this->supports_group_actors()) {
|
|
|
79 |
$users = $statement->get_all_users();
|
|
|
80 |
// In most cases we can use $USER->id as the event userid but because
|
|
|
81 |
// this is just a test class it checks first for $USER and, if not
|
|
|
82 |
// present just pick the first one.
|
|
|
83 |
$user = $users[$USER->id] ?? array_shift($users);
|
|
|
84 |
} else {
|
|
|
85 |
$user = $statement->get_user();
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
// Convert into a Moodle event.
|
|
|
89 |
$minstatement = $statement->minify();
|
|
|
90 |
$params = array(
|
|
|
91 |
'other' => $minstatement,
|
|
|
92 |
'context' => context_system::instance(),
|
|
|
93 |
'userid' => $user->id,
|
|
|
94 |
);
|
|
|
95 |
return xapi_test_statement_post::create($params);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Return true if group actor is enabled.
|
|
|
100 |
*
|
|
|
101 |
* NOTE: the use of a global is only for testing. We need to change
|
|
|
102 |
* the behaviour from the PHPUnitTest to test all possible scenarios.
|
|
|
103 |
*
|
|
|
104 |
* Note: this method must be overridden by the plugins which want to
|
|
|
105 |
* use groups in statements.
|
|
|
106 |
*
|
|
|
107 |
* @return bool
|
|
|
108 |
*/
|
|
|
109 |
public function supports_group_actors(): bool {
|
|
|
110 |
global $CFG;
|
|
|
111 |
if (isset($CFG->xapitestforcegroupactors)) {
|
|
|
112 |
return $CFG->xapitestforcegroupactors;
|
|
|
113 |
}
|
|
|
114 |
return parent::supports_group_actors();
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Validate a xAPI state.
|
|
|
119 |
*
|
|
|
120 |
* Check if the state is valid for this handler.
|
|
|
121 |
*
|
|
|
122 |
* This method is used also for the state get requests so the validation
|
|
|
123 |
* cannot rely on having state data.
|
|
|
124 |
*
|
|
|
125 |
* @param state $state
|
|
|
126 |
* @return bool if the state is valid or not
|
|
|
127 |
*/
|
|
|
128 |
protected function validate_state(state $state): bool {
|
|
|
129 |
// For testing purposes, the state will be considered NOT valid when stateid is set to 'paella'.
|
|
|
130 |
if ($state->get_state_id() === 'paella') {
|
|
|
131 |
return false;
|
|
|
132 |
}
|
|
|
133 |
return true;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
}
|