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 |
* Data provider tests.
|
|
|
19 |
*
|
|
|
20 |
* @package core_badges
|
|
|
21 |
* @category test
|
|
|
22 |
* @copyright 2018 Frédéric Massart
|
|
|
23 |
* @author Frédéric Massart <fred@branchup.tech>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
namespace core_badges\privacy;
|
|
|
27 |
|
|
|
28 |
defined('MOODLE_INTERNAL') || die();
|
|
|
29 |
global $CFG;
|
|
|
30 |
|
|
|
31 |
use core_privacy\tests\provider_testcase;
|
|
|
32 |
use core_privacy\local\request\approved_contextlist;
|
|
|
33 |
use core_privacy\local\request\transform;
|
|
|
34 |
use core_privacy\local\request\writer;
|
|
|
35 |
use core_badges\privacy\provider;
|
|
|
36 |
use core_privacy\local\request\approved_userlist;
|
|
|
37 |
use core_badges\helper;
|
|
|
38 |
|
|
|
39 |
require_once($CFG->libdir . '/badgeslib.php');
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Data provider testcase class.
|
|
|
43 |
*
|
|
|
44 |
* @package core_badges
|
|
|
45 |
* @category test
|
|
|
46 |
* @copyright 2018 Frédéric Massart
|
|
|
47 |
* @author Frédéric Massart <fred@branchup.tech>
|
|
|
48 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
49 |
*/
|
|
|
50 |
class provider_test extends provider_testcase {
|
|
|
51 |
|
|
|
52 |
public function setUp(): void {
|
|
|
53 |
$this->resetAfterTest();
|
|
|
54 |
}
|
|
|
55 |
|
11 |
efrain |
56 |
public function test_get_contexts_for_userid_for_badge_editing(): void {
|
1 |
efrain |
57 |
$dg = $this->getDataGenerator();
|
|
|
58 |
$u1 = $dg->create_user();
|
|
|
59 |
$u2 = $dg->create_user();
|
|
|
60 |
$u3 = $dg->create_user();
|
|
|
61 |
$u4 = $dg->create_user();
|
|
|
62 |
$u5 = $dg->create_user();
|
|
|
63 |
$c1 = $dg->create_course();
|
|
|
64 |
$c2 = $dg->create_course();
|
|
|
65 |
$sysctx = \context_system::instance();
|
|
|
66 |
$c1ctx = \context_course::instance($c1->id);
|
|
|
67 |
$c2ctx = \context_course::instance($c2->id);
|
|
|
68 |
|
|
|
69 |
// Assert that we find contexts where we created/modified a badge.
|
|
|
70 |
$this->create_badge(['usercreated' => $u1->id, 'usermodified' => $u5->id]);
|
|
|
71 |
$this->create_badge(['usercreated' => $u2->id, 'type' => BADGE_TYPE_COURSE, 'courseid' => $c1->id]);
|
|
|
72 |
$this->create_badge(['usermodified' => $u3->id]);
|
|
|
73 |
$this->create_badge(['usermodified' => $u4->id, 'type' => BADGE_TYPE_COURSE, 'courseid' => $c2->id,
|
|
|
74 |
'usercreated' => $u5->id]);
|
|
|
75 |
|
|
|
76 |
$contexts = provider::get_contexts_for_userid($u1->id)->get_contextids();
|
|
|
77 |
$this->assertCount(1, $contexts);
|
|
|
78 |
$this->assertEquals($sysctx->id, $contexts[0]);
|
|
|
79 |
|
|
|
80 |
$contexts = provider::get_contexts_for_userid($u2->id)->get_contextids();
|
|
|
81 |
$this->assertCount(1, $contexts);
|
|
|
82 |
$this->assertEquals($c1ctx->id, $contexts[0]);
|
|
|
83 |
|
|
|
84 |
$contexts = provider::get_contexts_for_userid($u3->id)->get_contextids();
|
|
|
85 |
$this->assertCount(1, $contexts);
|
|
|
86 |
$this->assertEquals($sysctx->id, $contexts[0]);
|
|
|
87 |
|
|
|
88 |
$contexts = provider::get_contexts_for_userid($u4->id)->get_contextids();
|
|
|
89 |
$this->assertCount(1, $contexts);
|
|
|
90 |
$this->assertEquals($c2ctx->id, $contexts[0]);
|
|
|
91 |
|
|
|
92 |
$contexts = provider::get_contexts_for_userid($u5->id)->get_contextids();
|
|
|
93 |
$this->assertCount(2, $contexts);
|
|
|
94 |
$this->assertTrue(in_array($sysctx->id, $contexts));
|
|
|
95 |
$this->assertTrue(in_array($c2ctx->id, $contexts));
|
|
|
96 |
}
|
|
|
97 |
|
11 |
efrain |
98 |
public function test_get_contexts_for_userid_for_manual_award(): void {
|
1 |
efrain |
99 |
global $DB;
|
|
|
100 |
|
|
|
101 |
$dg = $this->getDataGenerator();
|
|
|
102 |
$u1 = $dg->create_user();
|
|
|
103 |
$u2 = $dg->create_user();
|
|
|
104 |
$u3 = $dg->create_user();
|
|
|
105 |
$u4 = $dg->create_user();
|
|
|
106 |
$c1 = $dg->create_course();
|
|
|
107 |
$sysctx = \context_system::instance();
|
|
|
108 |
$c1ctx = \context_course::instance($c1->id);
|
|
|
109 |
$u1ctx = \context_user::instance($u1->id);
|
|
|
110 |
$u2ctx = \context_user::instance($u2->id);
|
|
|
111 |
$u3ctx = \context_user::instance($u3->id);
|
|
|
112 |
$u4ctx = \context_user::instance($u4->id);
|
|
|
113 |
$b1 = $this->create_badge();
|
|
|
114 |
$b2 = $this->create_badge(['type' => BADGE_TYPE_COURSE, 'courseid' => $c1->id]);
|
|
|
115 |
|
|
|
116 |
$this->create_manual_award(['recipientid' => $u4->id, 'issuerid' => $u1->id, 'badgeid' => $b1->id]);
|
|
|
117 |
$this->create_manual_award(['recipientid' => $u3->id, 'issuerid' => $u2->id, 'badgeid' => $b1->id]);
|
|
|
118 |
$this->create_manual_award(['recipientid' => $u3->id, 'issuerid' => $u2->id, 'badgeid' => $b2->id]);
|
|
|
119 |
|
|
|
120 |
$contexts = provider::get_contexts_for_userid($u1->id)->get_contextids();
|
|
|
121 |
$this->assertCount(1, $contexts);
|
|
|
122 |
$this->assertEquals($u4ctx->id, $contexts[0]);
|
|
|
123 |
|
|
|
124 |
$contexts = provider::get_contexts_for_userid($u2->id)->get_contextids();
|
|
|
125 |
$this->assertCount(1, $contexts);
|
|
|
126 |
$this->assertEquals($u3ctx->id, $contexts[0]);
|
|
|
127 |
}
|
|
|
128 |
|
11 |
efrain |
129 |
public function test_get_contexts_for_userid_for_my_stuff(): void {
|
1 |
efrain |
130 |
global $DB;
|
|
|
131 |
|
|
|
132 |
$dg = $this->getDataGenerator();
|
|
|
133 |
$u1 = $dg->create_user();
|
|
|
134 |
$u2 = $dg->create_user();
|
|
|
135 |
$u3 = $dg->create_user();
|
|
|
136 |
$u4 = $dg->create_user();
|
|
|
137 |
$c1 = $dg->create_course();
|
|
|
138 |
$sysctx = \context_system::instance();
|
|
|
139 |
$c1ctx = \context_course::instance($c1->id);
|
|
|
140 |
$u1ctx = \context_user::instance($u1->id);
|
|
|
141 |
$u2ctx = \context_user::instance($u2->id);
|
|
|
142 |
$u3ctx = \context_user::instance($u3->id);
|
|
|
143 |
$u4ctx = \context_user::instance($u4->id);
|
|
|
144 |
$b1 = $this->create_badge();
|
|
|
145 |
$b2 = $this->create_badge(['type' => BADGE_TYPE_COURSE, 'courseid' => $c1->id]);
|
|
|
146 |
|
|
|
147 |
helper::create_fake_backpack(['userid' => $u1->id]);
|
|
|
148 |
$this->create_manual_award(['recipientid' => $u2->id, 'badgeid' => $b1->id]);
|
|
|
149 |
$this->create_issued(['badgeid' => $b2->id, 'userid' => $u3->id]);
|
|
|
150 |
|
|
|
151 |
$crit = $this->create_criteria_manual($b1->id);
|
|
|
152 |
$crit->mark_complete($u4->id);
|
|
|
153 |
|
|
|
154 |
$contexts = provider::get_contexts_for_userid($u1->id)->get_contextids();
|
|
|
155 |
$this->assertCount(1, $contexts);
|
|
|
156 |
$this->assertEquals($u1ctx->id, $contexts[0]);
|
|
|
157 |
|
|
|
158 |
$contexts = provider::get_contexts_for_userid($u2->id)->get_contextids();
|
|
|
159 |
$this->assertCount(1, $contexts);
|
|
|
160 |
$this->assertEquals($u2ctx->id, $contexts[0]);
|
|
|
161 |
|
|
|
162 |
$contexts = provider::get_contexts_for_userid($u3->id)->get_contextids();
|
|
|
163 |
$this->assertCount(1, $contexts);
|
|
|
164 |
$this->assertEquals($u3ctx->id, $contexts[0]);
|
|
|
165 |
|
|
|
166 |
$contexts = provider::get_contexts_for_userid($u4->id)->get_contextids();
|
|
|
167 |
$this->assertCount(1, $contexts);
|
|
|
168 |
$this->assertEquals($u4ctx->id, $contexts[0]);
|
|
|
169 |
}
|
|
|
170 |
|
11 |
efrain |
171 |
public function test_delete_data_for_user(): void {
|
1 |
efrain |
172 |
global $DB;
|
|
|
173 |
|
|
|
174 |
$dg = $this->getDataGenerator();
|
|
|
175 |
$u1 = $dg->create_user();
|
|
|
176 |
$u2 = $dg->create_user();
|
|
|
177 |
$c1 = $dg->create_course();
|
|
|
178 |
$sysctx = \context_system::instance();
|
|
|
179 |
$c1ctx = \context_course::instance($c1->id);
|
|
|
180 |
$u1ctx = \context_user::instance($u1->id);
|
|
|
181 |
$u2ctx = \context_user::instance($u2->id);
|
|
|
182 |
|
|
|
183 |
$b1 = $this->create_badge(['usercreated' => $u1->id, 'usermodified' => $u2->id]);
|
|
|
184 |
$b2 = $this->create_badge(['usercreated' => $u2->id, 'usermodified' => $u1->id,
|
|
|
185 |
'type' => BADGE_TYPE_COURSE, 'courseid' => $c1->id]);
|
|
|
186 |
|
|
|
187 |
helper::create_fake_backpack(['userid' => $u1->id]);
|
|
|
188 |
helper::create_fake_backpack(['userid' => $u2->id]);
|
|
|
189 |
$this->create_manual_award(['recipientid' => $u1->id, 'badgeid' => $b1->id]);
|
|
|
190 |
$this->create_manual_award(['recipientid' => $u2->id, 'badgeid' => $b1->id, 'issuerid' => $u1->id]);
|
|
|
191 |
$this->create_issued(['badgeid' => $b2->id, 'userid' => $u1->id]);
|
|
|
192 |
$this->create_issued(['badgeid' => $b2->id, 'userid' => $u2->id]);
|
|
|
193 |
|
|
|
194 |
$crit = $this->create_criteria_manual($b1->id);
|
|
|
195 |
$crit->mark_complete($u2->id);
|
|
|
196 |
$crit = $this->create_criteria_manual($b2->id);
|
|
|
197 |
$crit->mark_complete($u1->id);
|
|
|
198 |
|
|
|
199 |
$this->assertTrue($DB->record_exists('badge_backpack', ['userid' => $u1->id]));
|
|
|
200 |
$this->assertTrue($DB->record_exists('badge_backpack', ['userid' => $u2->id]));
|
|
|
201 |
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u1->id]));
|
|
|
202 |
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u1->id]));
|
|
|
203 |
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u2->id]));
|
|
|
204 |
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u2->id]));
|
|
|
205 |
$this->assertTrue($DB->record_exists('badge_manual_award', ['recipientid' => $u1->id]));
|
|
|
206 |
$this->assertTrue($DB->record_exists('badge_manual_award', ['recipientid' => $u2->id]));
|
|
|
207 |
$this->assertTrue($DB->record_exists('badge_issued', ['userid' => $u1->id]));
|
|
|
208 |
$this->assertTrue($DB->record_exists('badge_issued', ['userid' => $u2->id]));
|
|
|
209 |
$this->assertTrue($DB->record_exists('badge_criteria_met', ['userid' => $u1->id]));
|
|
|
210 |
$this->assertTrue($DB->record_exists('badge_criteria_met', ['userid' => $u2->id]));
|
|
|
211 |
|
|
|
212 |
provider::delete_data_for_user(new approved_contextlist($u1, 'core_badges', [$sysctx->id, $c1ctx->id,
|
|
|
213 |
$u1ctx->id, $u2ctx->id]));
|
|
|
214 |
|
|
|
215 |
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u1->id]));
|
|
|
216 |
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u1->id]));
|
|
|
217 |
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u2->id]));
|
|
|
218 |
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u2->id]));
|
|
|
219 |
$this->assertFalse($DB->record_exists('badge_backpack', ['userid' => $u1->id]));
|
|
|
220 |
$this->assertTrue($DB->record_exists('badge_backpack', ['userid' => $u2->id]));
|
|
|
221 |
$this->assertFalse($DB->record_exists('badge_manual_award', ['recipientid' => $u1->id]));
|
|
|
222 |
$this->assertTrue($DB->record_exists('badge_manual_award', ['recipientid' => $u2->id]));
|
|
|
223 |
$this->assertFalse($DB->record_exists('badge_issued', ['userid' => $u1->id]));
|
|
|
224 |
$this->assertTrue($DB->record_exists('badge_issued', ['userid' => $u2->id]));
|
|
|
225 |
$this->assertFalse($DB->record_exists('badge_criteria_met', ['userid' => $u1->id]));
|
|
|
226 |
$this->assertTrue($DB->record_exists('badge_criteria_met', ['userid' => $u2->id]));
|
|
|
227 |
}
|
|
|
228 |
|
11 |
efrain |
229 |
public function test_delete_data_for_all_users_in_context(): void {
|
1 |
efrain |
230 |
global $DB;
|
|
|
231 |
|
|
|
232 |
$dg = $this->getDataGenerator();
|
|
|
233 |
$u1 = $dg->create_user();
|
|
|
234 |
$u2 = $dg->create_user();
|
|
|
235 |
$c1 = $dg->create_course();
|
|
|
236 |
$sysctx = \context_system::instance();
|
|
|
237 |
$c1ctx = \context_course::instance($c1->id);
|
|
|
238 |
$u1ctx = \context_user::instance($u1->id);
|
|
|
239 |
$u2ctx = \context_user::instance($u2->id);
|
|
|
240 |
|
|
|
241 |
$b1 = $this->create_badge(['usercreated' => $u1->id, 'usermodified' => $u2->id]);
|
|
|
242 |
$b2 = $this->create_badge(['usercreated' => $u2->id, 'usermodified' => $u1->id,
|
|
|
243 |
'type' => BADGE_TYPE_COURSE, 'courseid' => $c1->id]);
|
|
|
244 |
|
|
|
245 |
helper::create_fake_backpack(['userid' => $u1->id]);
|
|
|
246 |
helper::create_fake_backpack(['userid' => $u2->id]);
|
|
|
247 |
$this->create_manual_award(['recipientid' => $u1->id, 'badgeid' => $b1->id]);
|
|
|
248 |
$this->create_manual_award(['recipientid' => $u2->id, 'badgeid' => $b1->id, 'issuerid' => $u1->id]);
|
|
|
249 |
$this->create_issued(['badgeid' => $b2->id, 'userid' => $u1->id]);
|
|
|
250 |
$this->create_issued(['badgeid' => $b2->id, 'userid' => $u2->id]);
|
|
|
251 |
|
|
|
252 |
$crit = $this->create_criteria_manual($b1->id);
|
|
|
253 |
$crit->mark_complete($u2->id);
|
|
|
254 |
$crit = $this->create_criteria_manual($b2->id);
|
|
|
255 |
$crit->mark_complete($u1->id);
|
|
|
256 |
|
|
|
257 |
$assertnochange = function() use ($DB, $u1, $u2) {
|
|
|
258 |
$this->assertTrue($DB->record_exists('badge_backpack', ['userid' => $u1->id]));
|
|
|
259 |
$this->assertTrue($DB->record_exists('badge_backpack', ['userid' => $u2->id]));
|
|
|
260 |
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u1->id]));
|
|
|
261 |
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u1->id]));
|
|
|
262 |
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u2->id]));
|
|
|
263 |
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u2->id]));
|
|
|
264 |
$this->assertTrue($DB->record_exists('badge_manual_award', ['recipientid' => $u1->id]));
|
|
|
265 |
$this->assertTrue($DB->record_exists('badge_manual_award', ['recipientid' => $u2->id]));
|
|
|
266 |
$this->assertTrue($DB->record_exists('badge_issued', ['userid' => $u1->id]));
|
|
|
267 |
$this->assertTrue($DB->record_exists('badge_issued', ['userid' => $u2->id]));
|
|
|
268 |
$this->assertTrue($DB->record_exists('badge_criteria_met', ['userid' => $u1->id]));
|
|
|
269 |
$this->assertTrue($DB->record_exists('badge_criteria_met', ['userid' => $u2->id]));
|
|
|
270 |
};
|
|
|
271 |
$assertnochange();
|
|
|
272 |
|
|
|
273 |
provider::delete_data_for_all_users_in_context($sysctx);
|
|
|
274 |
$assertnochange();
|
|
|
275 |
|
|
|
276 |
provider::delete_data_for_all_users_in_context($c1ctx);
|
|
|
277 |
$assertnochange();
|
|
|
278 |
|
|
|
279 |
provider::delete_data_for_all_users_in_context($u1ctx);
|
|
|
280 |
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u1->id]));
|
|
|
281 |
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u1->id]));
|
|
|
282 |
$this->assertTrue($DB->record_exists('badge', ['usercreated' => $u2->id]));
|
|
|
283 |
$this->assertTrue($DB->record_exists('badge', ['usermodified' => $u2->id]));
|
|
|
284 |
$this->assertFalse($DB->record_exists('badge_backpack', ['userid' => $u1->id]));
|
|
|
285 |
$this->assertTrue($DB->record_exists('badge_backpack', ['userid' => $u2->id]));
|
|
|
286 |
$this->assertFalse($DB->record_exists('badge_manual_award', ['recipientid' => $u1->id]));
|
|
|
287 |
$this->assertTrue($DB->record_exists('badge_manual_award', ['recipientid' => $u2->id]));
|
|
|
288 |
$this->assertFalse($DB->record_exists('badge_issued', ['userid' => $u1->id]));
|
|
|
289 |
$this->assertTrue($DB->record_exists('badge_issued', ['userid' => $u2->id]));
|
|
|
290 |
$this->assertFalse($DB->record_exists('badge_criteria_met', ['userid' => $u1->id]));
|
|
|
291 |
$this->assertTrue($DB->record_exists('badge_criteria_met', ['userid' => $u2->id]));
|
|
|
292 |
}
|
|
|
293 |
|
11 |
efrain |
294 |
public function test_export_data_for_user(): void {
|
1 |
efrain |
295 |
global $DB;
|
|
|
296 |
|
|
|
297 |
$yes = transform::yesno(true);
|
|
|
298 |
$no = transform::yesno(false);
|
|
|
299 |
|
|
|
300 |
$dg = $this->getDataGenerator();
|
|
|
301 |
$u1 = $dg->create_user();
|
|
|
302 |
$u2 = $dg->create_user();
|
|
|
303 |
$u3 = $dg->create_user();
|
|
|
304 |
$c1 = $dg->create_course();
|
|
|
305 |
$sysctx = \context_system::instance();
|
|
|
306 |
$c1ctx = \context_course::instance($c1->id);
|
|
|
307 |
$u1ctx = \context_user::instance($u1->id);
|
|
|
308 |
$u2ctx = \context_user::instance($u2->id);
|
|
|
309 |
|
|
|
310 |
$b1 = $this->create_badge(['usercreated' => $u3->id]);
|
|
|
311 |
$this->endorse_badge(['badgeid' => $b1->id]);
|
|
|
312 |
$this->align_badge(['badgeid' => $b1->id], ' (1)');
|
|
|
313 |
$this->align_badge(['badgeid' => $b1->id], ' (2)');
|
|
|
314 |
$b2 = $this->create_badge(['type' => BADGE_TYPE_COURSE, 'courseid' => $c1->id, 'usermodified' => $u3->id]);
|
|
|
315 |
$this->relate_badge($b1->id, $b2->id);
|
|
|
316 |
$b3 = $this->create_badge();
|
|
|
317 |
$b3crit = $this->create_criteria_manual($b3->id);
|
|
|
318 |
$b4 = $this->create_badge();
|
|
|
319 |
|
|
|
320 |
// Create things for user 2, to check it's not exported it.
|
|
|
321 |
$this->create_issued(['badgeid' => $b4->id, 'userid' => $u2->id]);
|
|
|
322 |
helper::create_fake_backpack(['userid' => $u2->id, 'email' => $u2->email]);
|
|
|
323 |
$this->create_manual_award(['badgeid' => $b1->id, 'recipientid' => $u2->id, 'issuerid' => $u3->id]);
|
|
|
324 |
|
|
|
325 |
// Create a set of stuff for u1.
|
|
|
326 |
$this->create_issued(['badgeid' => $b1->id, 'userid' => $u1->id, 'uniquehash' => 'yoohoo']);
|
|
|
327 |
$this->create_manual_award(['badgeid' => $b2->id, 'recipientid' => $u1->id, 'issuerid' => $u3->id]);
|
|
|
328 |
$b3crit->mark_complete($u1->id);
|
|
|
329 |
helper::create_fake_backpack(['userid' => $u1->id, 'email' => $u1->email]);
|
|
|
330 |
|
|
|
331 |
// Check u1.
|
|
|
332 |
writer::reset();
|
|
|
333 |
provider::export_user_data(new approved_contextlist($u1, 'core_badges', [$u1ctx->id, $u2ctx->id,
|
|
|
334 |
$sysctx->id, $c1ctx->id]));
|
|
|
335 |
$this->assertFalse(writer::with_context($u2ctx)->has_any_data());
|
|
|
336 |
$this->assertFalse(writer::with_context($sysctx)->has_any_data());
|
|
|
337 |
$this->assertFalse(writer::with_context($c1ctx)->has_any_data());
|
|
|
338 |
|
|
|
339 |
$path = [get_string('badges', 'core_badges'), "{$b1->name} ({$b1->id})"];
|
|
|
340 |
$data = writer::with_context($u1ctx)->get_data($path);
|
|
|
341 |
$this->assertEquals($b1->name, $data->name);
|
|
|
342 |
$this->assertEquals($b1->version, $data->version);
|
|
|
343 |
$this->assertEquals($b1->language, $data->language);
|
|
|
344 |
$this->assertEquals($b1->imageauthorname, $data->imageauthorname);
|
|
|
345 |
$this->assertEquals($b1->imageauthoremail, $data->imageauthoremail);
|
|
|
346 |
$this->assertEquals($b1->imageauthorurl, $data->imageauthorurl);
|
|
|
347 |
$this->assertEquals($b1->imagecaption, $data->imagecaption);
|
|
|
348 |
$this->assertNotEmpty($data->issued);
|
|
|
349 |
$this->assertEmpty($data->manual_award);
|
|
|
350 |
$this->assertEmpty($data->criteria_met);
|
|
|
351 |
$this->assertFalse(isset($data->course));
|
|
|
352 |
$this->assertEquals('yoohoo', $data->issued['unique_hash']);
|
|
|
353 |
$this->assertNull($data->issued['expires_on']);
|
|
|
354 |
|
|
|
355 |
$this->assertNotEmpty($data->endorsement);
|
|
|
356 |
$this->assertNotEmpty($data->endorsement['issuername']);
|
|
|
357 |
$this->assertNotEmpty($data->endorsement['issuerurl']);
|
|
|
358 |
$this->assertNotEmpty($data->endorsement['issueremail']);
|
|
|
359 |
$this->assertNotEmpty($data->endorsement['claimid']);
|
|
|
360 |
$this->assertNotEmpty($data->endorsement['claimcomment']);
|
|
|
361 |
$this->assertNotEmpty($data->endorsement['dateissued']);
|
|
|
362 |
|
|
|
363 |
$this->assertNotEmpty($data->related_badge);
|
|
|
364 |
$this->assertNotEmpty($data->related_badge[0]);
|
|
|
365 |
$this->assertEquals($data->related_badge[0]['badgeid'], $b2->id);
|
|
|
366 |
$this->assertEquals($data->related_badge[0]['badgename'], $b2->name);
|
|
|
367 |
|
|
|
368 |
$this->assertNotEmpty($data->alignment);
|
|
|
369 |
$this->assertNotEmpty($data->alignment[0]);
|
|
|
370 |
$this->assertNotEmpty($data->alignment[0]['targetname']);
|
|
|
371 |
$this->assertNotEmpty($data->alignment[0]['targeturl']);
|
|
|
372 |
$this->assertNotEmpty($data->alignment[0]['targetdescription']);
|
|
|
373 |
$this->assertNotEmpty($data->alignment[0]['targetframework']);
|
|
|
374 |
$this->assertNotEmpty($data->alignment[0]['targetcode']);
|
|
|
375 |
$this->assertNotEmpty($data->alignment[1]);
|
|
|
376 |
$this->assertNotEmpty($data->alignment[1]['targetname']);
|
|
|
377 |
$this->assertNotEmpty($data->alignment[1]['targeturl']);
|
|
|
378 |
$this->assertNotEmpty($data->alignment[1]['targetdescription']);
|
|
|
379 |
$this->assertNotEmpty($data->alignment[1]['targetframework']);
|
|
|
380 |
$this->assertNotEmpty($data->alignment[1]['targetcode']);
|
|
|
381 |
|
|
|
382 |
$path = [get_string('badges', 'core_badges'), "{$b2->name} ({$b2->id})"];
|
|
|
383 |
$data = writer::with_context($u1ctx)->get_data($path);
|
|
|
384 |
$this->assertEquals($b2->name, $data->name);
|
|
|
385 |
$this->assertEmpty($data->issued);
|
|
|
386 |
$this->assertNotEmpty($data->manual_award);
|
|
|
387 |
$this->assertEmpty($data->criteria_met);
|
|
|
388 |
$this->assertEquals($c1->fullname, $data->course);
|
|
|
389 |
$this->assertEquals($u3->id, $data->manual_award['issuer']);
|
|
|
390 |
|
|
|
391 |
$path = [get_string('badges', 'core_badges'), "{$b3->name} ({$b3->id})"];
|
|
|
392 |
$data = writer::with_context($u1ctx)->get_data($path);
|
|
|
393 |
$this->assertEquals($b3->name, $data->name);
|
|
|
394 |
$this->assertEmpty($data->issued);
|
|
|
395 |
$this->assertEmpty($data->manual_award);
|
|
|
396 |
$this->assertNotEmpty($data->criteria_met);
|
|
|
397 |
$this->assertNotFalse(strpos($data->criteria_met[0], get_string('criteria_descr_2', 'core_badges', 'ALL')));
|
|
|
398 |
|
|
|
399 |
$path = [get_string('badges', 'core_badges')];
|
|
|
400 |
$data = writer::with_context($u1ctx)->get_related_data($path, 'backpacks');
|
|
|
401 |
$this->assertCount(1, $data->backpacks);
|
|
|
402 |
$this->assertEquals($u1->email, $data->backpacks[0]['email']);
|
|
|
403 |
|
|
|
404 |
// Confirm we do not have u2.
|
|
|
405 |
$path = [get_string('badges', 'core_badges'), "{$b4->name} ({$b4->id})"];
|
|
|
406 |
$data = writer::with_context($u1ctx)->get_data($path);
|
|
|
407 |
$this->assertEmpty($data);
|
|
|
408 |
$data = writer::with_context($u2ctx)->get_data($path);
|
|
|
409 |
$this->assertEmpty($data);
|
|
|
410 |
|
|
|
411 |
// Export for u3.
|
|
|
412 |
writer::reset();
|
|
|
413 |
$path = [get_string('badges', 'core_badges')];
|
|
|
414 |
provider::export_user_data(new approved_contextlist($u3, 'core_badges', [$u1ctx->id, $u2ctx->id,
|
|
|
415 |
$sysctx->id, $c1ctx->id]));
|
|
|
416 |
|
|
|
417 |
$data = writer::with_context($u2ctx)->get_related_data($path, 'manual_awards');
|
|
|
418 |
$this->assertCount(1, $data->badges);
|
|
|
419 |
$this->assertEquals($b1->name, $data->badges[0]['name']);
|
|
|
420 |
$this->assertEquals($yes, $data->badges[0]['issued_by_you']);
|
|
|
421 |
$this->assertEquals('Manager', $data->badges[0]['issuer_role']);
|
|
|
422 |
|
|
|
423 |
$data = writer::with_context($sysctx)->get_data($path);
|
|
|
424 |
$this->assertCount(1, $data->badges);
|
|
|
425 |
$this->assertEquals($b1->name, $data->badges[0]['name']);
|
|
|
426 |
$this->assertEquals($yes, $data->badges[0]['created_by_you']);
|
|
|
427 |
$this->assertEquals($no, $data->badges[0]['modified_by_you']);
|
|
|
428 |
|
|
|
429 |
$data = writer::with_context($c1ctx)->get_data($path);
|
|
|
430 |
$this->assertCount(1, $data->badges);
|
|
|
431 |
$this->assertEquals($b2->name, $data->badges[0]['name']);
|
|
|
432 |
$this->assertEquals($no, $data->badges[0]['created_by_you']);
|
|
|
433 |
$this->assertEquals($yes, $data->badges[0]['modified_by_you']);
|
|
|
434 |
|
|
|
435 |
$data = writer::with_context($u1ctx)->get_related_data($path, 'manual_awards');
|
|
|
436 |
$this->assertCount(1, $data->badges);
|
|
|
437 |
$this->assertEquals($b3->name, $data->badges[0]['name']);
|
|
|
438 |
$this->assertEquals($yes, $data->badges[0]['issued_by_you']);
|
|
|
439 |
$this->assertEquals('Manager', $data->badges[0]['issuer_role']);
|
|
|
440 |
}
|
|
|
441 |
|
|
|
442 |
/**
|
|
|
443 |
* Test that only users within a user, system and course context are fetched.
|
|
|
444 |
*/
|
11 |
efrain |
445 |
public function test_get_users_in_context(): void {
|
1 |
efrain |
446 |
$component = 'core_badges';
|
|
|
447 |
|
|
|
448 |
// Create course1.
|
|
|
449 |
$course1 = $this->getDataGenerator()->create_course();
|
|
|
450 |
$coursecontext1 = \context_course::instance($course1->id);
|
|
|
451 |
// Create course2.
|
|
|
452 |
$course2 = $this->getDataGenerator()->create_course();
|
|
|
453 |
$coursecontext2 = \context_course::instance($course2->id);
|
|
|
454 |
// Create user1.
|
|
|
455 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
456 |
$usercontext1 = \context_user::instance($user1->id);
|
|
|
457 |
// Create user2.
|
|
|
458 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
459 |
$usercontext2 = \context_user::instance($user2->id);
|
|
|
460 |
// Create user3.
|
|
|
461 |
$user3 = $this->getDataGenerator()->create_user();
|
|
|
462 |
$usercontext3 = \context_user::instance($user3->id);
|
|
|
463 |
|
|
|
464 |
// The list of users in usercontext1 should not return anything yet (related data still haven't been created).
|
|
|
465 |
$userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
|
|
|
466 |
provider::get_users_in_context($userlist1);
|
|
|
467 |
$this->assertCount(0, $userlist1);
|
|
|
468 |
// The list of users in coursecontext1 should not return anything yet (related data still haven't been created).
|
|
|
469 |
$userlist2 = new \core_privacy\local\request\userlist($coursecontext1, $component);
|
|
|
470 |
provider::get_users_in_context($userlist2);
|
|
|
471 |
$this->assertCount(0, $userlist2);
|
|
|
472 |
// The list of users in systemcontext should not return anything yet (related data still haven't been created).
|
|
|
473 |
$systemcontext = \context_system::instance();
|
|
|
474 |
$userlist3 = new \core_privacy\local\request\userlist($systemcontext, $component);
|
|
|
475 |
provider::get_users_in_context($userlist3);
|
|
|
476 |
$this->assertCount(0, $userlist3);
|
|
|
477 |
|
|
|
478 |
// Assert that we find contexts where we created/modified a badge.
|
|
|
479 |
$this->create_badge(['usercreated' => $user1->id, 'usermodified' => $user2->id]);
|
|
|
480 |
$badge1 = $this->create_badge(['usercreated' => $user2->id, 'type' => BADGE_TYPE_COURSE, 'courseid' => $course1->id]);
|
|
|
481 |
$badge2 = $this->create_badge(['usercreated' => $user3->id, 'usermodified' => $user1->id]);
|
|
|
482 |
|
|
|
483 |
$this->create_manual_award(['recipientid' => $user2->id, 'issuerid' => $user1->id, 'badgeid' => $badge1->id]);
|
|
|
484 |
$this->create_manual_award(['recipientid' => $user3->id, 'issuerid' => $user2->id, 'badgeid' => $badge1->id]);
|
|
|
485 |
$this->create_manual_award(['recipientid' => $user1->id, 'issuerid' => $user2->id, 'badgeid' => $badge2->id]);
|
|
|
486 |
|
|
|
487 |
helper::create_fake_backpack(['userid' => $user2->id]);
|
|
|
488 |
$this->create_issued(['badgeid' => $badge2->id, 'userid' => $user3->id]);
|
|
|
489 |
|
|
|
490 |
$crit = $this->create_criteria_manual($badge1->id);
|
|
|
491 |
$crit->mark_complete($user3->id);
|
|
|
492 |
|
|
|
493 |
// The list of users for user context should return user1 and user2.
|
|
|
494 |
provider::get_users_in_context($userlist1);
|
|
|
495 |
$this->assertCount(2, $userlist1);
|
|
|
496 |
$this->assertTrue(in_array($user1->id, $userlist1->get_userids()));
|
|
|
497 |
$this->assertTrue(in_array($user2->id, $userlist1->get_userids()));
|
|
|
498 |
|
|
|
499 |
// The list of users for course context should return user2.
|
|
|
500 |
provider::get_users_in_context($userlist2);
|
|
|
501 |
$this->assertCount(1, $userlist2);
|
|
|
502 |
$this->assertTrue(in_array($user2->id, $userlist2->get_userids()));
|
|
|
503 |
|
|
|
504 |
// The list of users for system context should return user1, user2 and user3.
|
|
|
505 |
provider::get_users_in_context($userlist3);
|
|
|
506 |
$this->assertCount(3, $userlist3);
|
|
|
507 |
$this->assertTrue(in_array($user1->id, $userlist3->get_userids()));
|
|
|
508 |
$this->assertTrue(in_array($user2->id, $userlist3->get_userids()));
|
|
|
509 |
$this->assertTrue(in_array($user3->id, $userlist3->get_userids()));
|
|
|
510 |
}
|
|
|
511 |
|
|
|
512 |
/**
|
|
|
513 |
* Test that data for users in approved userlist is deleted.
|
|
|
514 |
*/
|
11 |
efrain |
515 |
public function test_delete_data_for_users(): void {
|
1 |
efrain |
516 |
$component = 'core_badges';
|
|
|
517 |
|
|
|
518 |
// Create course1.
|
|
|
519 |
$course1 = $this->getDataGenerator()->create_course();
|
|
|
520 |
$coursecontext1 = \context_course::instance($course1->id);
|
|
|
521 |
// Create course2.
|
|
|
522 |
$course2 = $this->getDataGenerator()->create_course();
|
|
|
523 |
$coursecontext2 = \context_course::instance($course2->id);
|
|
|
524 |
// Create user1.
|
|
|
525 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
526 |
$usercontext1 = \context_user::instance($user1->id);
|
|
|
527 |
// Create user2.
|
|
|
528 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
529 |
$usercontext2 = \context_user::instance($user2->id);
|
|
|
530 |
// Create user3.
|
|
|
531 |
$user3 = $this->getDataGenerator()->create_user();
|
|
|
532 |
$usercontext3 = \context_user::instance($user3->id);
|
|
|
533 |
|
|
|
534 |
$this->create_badge(['usercreated' => $user1->id, 'usermodified' => $user2->id]);
|
|
|
535 |
$badge1 = $this->create_badge(['usercreated' => $user2->id, 'type' => BADGE_TYPE_COURSE, 'courseid' => $course1->id]);
|
|
|
536 |
$badge2 = $this->create_badge(['usercreated' => $user3->id, 'type' => BADGE_TYPE_COURSE, 'courseid' => $course2->id,
|
|
|
537 |
'usermodified' => $user1->id]);
|
|
|
538 |
|
|
|
539 |
$this->create_manual_award(['recipientid' => $user2->id, 'issuerid' => $user1->id, 'badgeid' => $badge1->id]);
|
|
|
540 |
$this->create_manual_award(['recipientid' => $user3->id, 'issuerid' => $user2->id, 'badgeid' => $badge1->id]);
|
|
|
541 |
$this->create_manual_award(['recipientid' => $user1->id, 'issuerid' => $user2->id, 'badgeid' => $badge2->id]);
|
|
|
542 |
|
|
|
543 |
helper::create_fake_backpack(['userid' => $user2->id]);
|
|
|
544 |
$this->create_issued(['badgeid' => $badge2->id, 'userid' => $user3->id]);
|
|
|
545 |
|
|
|
546 |
$crit = $this->create_criteria_manual($badge1->id);
|
|
|
547 |
$crit->mark_complete($user3->id);
|
|
|
548 |
|
|
|
549 |
// The list of users for usercontext2 context should return users.
|
|
|
550 |
$userlist1 = new \core_privacy\local\request\userlist($usercontext2, $component);
|
|
|
551 |
provider::get_users_in_context($userlist1);
|
|
|
552 |
$this->assertCount(2, $userlist1);
|
|
|
553 |
$this->assertTrue(in_array($user1->id, $userlist1->get_userids()));
|
|
|
554 |
$this->assertTrue(in_array($user2->id, $userlist1->get_userids()));
|
|
|
555 |
|
|
|
556 |
// The list of users for coursecontext2 context should return users.
|
|
|
557 |
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
|
|
|
558 |
provider::get_users_in_context($userlist2);
|
|
|
559 |
$this->assertCount(2, $userlist2);
|
|
|
560 |
$this->assertTrue(in_array($user1->id, $userlist2->get_userids()));
|
|
|
561 |
$this->assertTrue(in_array($user3->id, $userlist2->get_userids()));
|
|
|
562 |
|
|
|
563 |
// The list of users for system context should return users.
|
|
|
564 |
$systemcontext = \context_system::instance();
|
|
|
565 |
$userlist3 = new \core_privacy\local\request\userlist($systemcontext, $component);
|
|
|
566 |
provider::get_users_in_context($userlist3);
|
|
|
567 |
$this->assertCount(2, $userlist3);
|
|
|
568 |
$this->assertTrue(in_array($user1->id, $userlist3->get_userids()));
|
|
|
569 |
$this->assertTrue(in_array($user2->id, $userlist3->get_userids()));
|
|
|
570 |
|
|
|
571 |
// Delete the data for user1 in usercontext2.
|
|
|
572 |
$approvedlist = new approved_userlist($usercontext2, $component, [$user1->id]);
|
|
|
573 |
// Delete using delete_data_for_user. No data for users in usercontext2 should be removed.
|
|
|
574 |
provider::delete_data_for_users($approvedlist);
|
|
|
575 |
// The list of users for usercontext2 context should still return user1, user2.
|
|
|
576 |
$userlist1 = new \core_privacy\local\request\userlist($usercontext2, $component);
|
|
|
577 |
provider::get_users_in_context($userlist1);
|
|
|
578 |
$this->assertCount(2, $userlist1);
|
|
|
579 |
$this->assertTrue(in_array($user1->id, $userlist1->get_userids()));
|
|
|
580 |
$this->assertTrue(in_array($user2->id, $userlist1->get_userids()));
|
|
|
581 |
|
|
|
582 |
// Delete the data for user2 in usercontext2.
|
|
|
583 |
$approvedlist = new approved_userlist($usercontext2, $component, [$user2->id]);
|
|
|
584 |
// Delete using delete_data_for_user. The user data in usercontext2 should be removed.
|
|
|
585 |
provider::delete_data_for_users($approvedlist);
|
|
|
586 |
// The list of users for usercontext2 context should not return any users.
|
|
|
587 |
$userlist1 = new \core_privacy\local\request\userlist($usercontext2, $component);
|
|
|
588 |
provider::get_users_in_context($userlist1);
|
|
|
589 |
$this->assertCount(0, $userlist1);
|
|
|
590 |
|
|
|
591 |
// The list of users for coursecontext2 context should return the previous users.
|
|
|
592 |
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
|
|
|
593 |
provider::get_users_in_context($userlist2);
|
|
|
594 |
$this->assertCount(2, $userlist2);
|
|
|
595 |
|
|
|
596 |
// The list of users for system context should return the previous users.
|
|
|
597 |
$systemcontext = \context_system::instance();
|
|
|
598 |
$userlist3 = new \core_privacy\local\request\userlist($systemcontext, $component);
|
|
|
599 |
provider::get_users_in_context($userlist3);
|
|
|
600 |
$this->assertCount(2, $userlist3);
|
|
|
601 |
|
|
|
602 |
// Make sure data is only deleted in the user context, nothing in course or system.
|
|
|
603 |
// Convert $userlist2 into an approved_contextlist.
|
|
|
604 |
$approvedlist = new approved_userlist($coursecontext2, $component, $userlist2->get_userids());
|
|
|
605 |
provider::delete_data_for_users($approvedlist);
|
|
|
606 |
|
|
|
607 |
// The list of users for coursecontext2 context should still return the user data.
|
|
|
608 |
$userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
|
|
|
609 |
provider::get_users_in_context($userlist2);
|
|
|
610 |
$this->assertCount(2, $userlist2);
|
|
|
611 |
}
|
|
|
612 |
|
|
|
613 |
/**
|
|
|
614 |
* Create a badge.
|
|
|
615 |
*
|
|
|
616 |
* @param array $params Parameters.
|
|
|
617 |
* @return object
|
|
|
618 |
*/
|
|
|
619 |
protected function create_badge(array $params = []) {
|
|
|
620 |
global $DB, $USER;
|
|
|
621 |
$record = (object) array_merge([
|
|
|
622 |
'name' => "Test badge with 'apostrophe' and other friends (<>&@#)",
|
|
|
623 |
'description' => "Testing badges",
|
|
|
624 |
'timecreated' => time(),
|
|
|
625 |
'timemodified' => time(),
|
|
|
626 |
'usercreated' => $USER->id,
|
|
|
627 |
'usermodified' => $USER->id,
|
|
|
628 |
'issuername' => "Test issuer",
|
|
|
629 |
'issuerurl' => "http://issuer-url.domain.co.nz",
|
|
|
630 |
'issuercontact' => "issuer@example.com",
|
|
|
631 |
'expiredate' => null,
|
|
|
632 |
'expireperiod' => null,
|
|
|
633 |
'type' => BADGE_TYPE_SITE,
|
|
|
634 |
'courseid' => null,
|
|
|
635 |
'messagesubject' => "Test message subject",
|
|
|
636 |
'message' => "Test message body",
|
|
|
637 |
'attachment' => 1,
|
|
|
638 |
'notification' => 0,
|
|
|
639 |
'status' => BADGE_STATUS_ACTIVE,
|
|
|
640 |
'version' => OPEN_BADGES_V2,
|
|
|
641 |
'language' => 'en',
|
|
|
642 |
'imageauthorname' => 'Image author',
|
|
|
643 |
'imageauthoremail' => 'author@example.com',
|
|
|
644 |
'imageauthorurl' => 'http://image.example.com/',
|
|
|
645 |
'imagecaption' => 'Image caption'
|
|
|
646 |
], $params);
|
|
|
647 |
$record->id = $DB->insert_record('badge', $record);
|
|
|
648 |
|
|
|
649 |
return $record;
|
|
|
650 |
}
|
|
|
651 |
|
|
|
652 |
/**
|
|
|
653 |
* Relate a badge.
|
|
|
654 |
*
|
|
|
655 |
* @param int $badgeid The badge ID.
|
|
|
656 |
* @param int $relatedbadgeid The related badge ID.
|
|
|
657 |
* @return object
|
|
|
658 |
*/
|
|
|
659 |
protected function relate_badge(int $badgeid, int $relatedbadgeid) {
|
|
|
660 |
global $DB;
|
|
|
661 |
$record = (object) [
|
|
|
662 |
'badgeid' => $badgeid,
|
|
|
663 |
'relatedbadgeid' => $relatedbadgeid
|
|
|
664 |
];
|
|
|
665 |
$record->id = $DB->insert_record('badge_related', $record);
|
|
|
666 |
|
|
|
667 |
return $record;
|
|
|
668 |
}
|
|
|
669 |
|
|
|
670 |
/**
|
|
|
671 |
* Align a badge.
|
|
|
672 |
*
|
|
|
673 |
* @param array $params Parameters.
|
|
|
674 |
* @return object
|
|
|
675 |
*/
|
|
|
676 |
protected function align_badge(array $params = [], $suffix = '') {
|
|
|
677 |
global $DB;
|
|
|
678 |
$record = (object) array_merge([
|
|
|
679 |
'badgeid' => null,
|
|
|
680 |
'targetname' => "Alignment name" . $suffix,
|
|
|
681 |
'targeturl' => "http://issuer-url.domain.co.nz",
|
|
|
682 |
'targetdescription' => "Description" . $suffix,
|
|
|
683 |
'targetframework' => "Framework" . $suffix,
|
|
|
684 |
'targetcode' => "Code . $suffix"
|
|
|
685 |
], $params);
|
|
|
686 |
$record->id = $DB->insert_record('badge_alignment', $record);
|
|
|
687 |
|
|
|
688 |
return $record;
|
|
|
689 |
}
|
|
|
690 |
|
|
|
691 |
/**
|
|
|
692 |
* Endorse a badge.
|
|
|
693 |
*
|
|
|
694 |
* @param array $params Parameters.
|
|
|
695 |
* @return object
|
|
|
696 |
*/
|
|
|
697 |
protected function endorse_badge(array $params = []) {
|
|
|
698 |
global $DB;
|
|
|
699 |
$record = (object) array_merge([
|
|
|
700 |
'badgeid' => null,
|
|
|
701 |
'issuername' => "External issuer name",
|
|
|
702 |
'issuerurl' => "http://issuer-url.domain.co.nz",
|
|
|
703 |
'issueremail' => "issuer@example.com",
|
|
|
704 |
'claimid' => "Claim ID",
|
|
|
705 |
'claimcomment' => "Claim comment",
|
|
|
706 |
'dateissued' => time()
|
|
|
707 |
], $params);
|
|
|
708 |
$record->id = $DB->insert_record('badge_endorsement', $record);
|
|
|
709 |
|
|
|
710 |
return $record;
|
|
|
711 |
}
|
|
|
712 |
|
|
|
713 |
/**
|
|
|
714 |
* Create a criteria of type badge.
|
|
|
715 |
*
|
|
|
716 |
* @param int $badgeid The badge ID.
|
|
|
717 |
* @param array $params Parameters.
|
|
|
718 |
* @return object
|
|
|
719 |
*/
|
|
|
720 |
protected function create_criteria_badge($badgeid, array $params = []) {
|
|
|
721 |
$badge = new \badge($badgeid);
|
|
|
722 |
if (empty($badge->criteria)) {
|
|
|
723 |
$overall = \award_criteria::build(['criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id]);
|
|
|
724 |
$overall->save(['agg' => BADGE_CRITERIA_AGGREGATION_ALL]);
|
|
|
725 |
}
|
|
|
726 |
|
|
|
727 |
$criteria = \award_criteria::build([
|
|
|
728 |
'badgeid' => $badge->id,
|
|
|
729 |
'criteriatype' => BADGE_CRITERIA_TYPE_BADGE,
|
|
|
730 |
]);
|
|
|
731 |
|
|
|
732 |
if (isset($params['badgeid'])) {
|
|
|
733 |
$params['badge_' . $params['badgeid']] = $params['badgeid'];
|
|
|
734 |
unset($params['badgeid']);
|
|
|
735 |
}
|
|
|
736 |
|
|
|
737 |
$criteria->save($params);
|
|
|
738 |
$badge = new \badge($badgeid);
|
|
|
739 |
return $badge->criteria[BADGE_CRITERIA_TYPE_BADGE];
|
|
|
740 |
}
|
|
|
741 |
|
|
|
742 |
/**
|
|
|
743 |
* Create a criteria of type manual.
|
|
|
744 |
*
|
|
|
745 |
* @param int $badgeid The badge ID.
|
|
|
746 |
* @param array $params Parameters.
|
|
|
747 |
* @return object
|
|
|
748 |
*/
|
|
|
749 |
protected function create_criteria_manual($badgeid, array $params = []) {
|
|
|
750 |
global $DB;
|
|
|
751 |
|
|
|
752 |
$badge = new \badge($badgeid);
|
|
|
753 |
if (empty($badge->criteria)) {
|
|
|
754 |
$overall = \award_criteria::build(['criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id]);
|
|
|
755 |
$overall->save(['agg' => BADGE_CRITERIA_AGGREGATION_ALL]);
|
|
|
756 |
}
|
|
|
757 |
|
|
|
758 |
$criteria = \award_criteria::build([
|
|
|
759 |
'badgeid' => $badge->id,
|
|
|
760 |
'criteriatype' => BADGE_CRITERIA_TYPE_MANUAL,
|
|
|
761 |
]);
|
|
|
762 |
|
|
|
763 |
$managerroleid = $DB->get_field_select('role', 'id', 'shortname = ?', ['manager'], IGNORE_MULTIPLE);
|
|
|
764 |
if (empty($params)) {
|
|
|
765 |
$params = [
|
|
|
766 |
'role_' . $managerroleid = $managerroleid
|
|
|
767 |
];
|
|
|
768 |
}
|
|
|
769 |
|
|
|
770 |
$criteria->save($params);
|
|
|
771 |
$badge = new \badge($badgeid);
|
|
|
772 |
return $badge->criteria[BADGE_CRITERIA_TYPE_MANUAL];
|
|
|
773 |
}
|
|
|
774 |
|
|
|
775 |
/**
|
|
|
776 |
* Create a badge issued.
|
|
|
777 |
*
|
|
|
778 |
* @param array $params Parameters.
|
|
|
779 |
* @return object
|
|
|
780 |
*/
|
|
|
781 |
protected function create_issued(array $params = []) {
|
|
|
782 |
global $DB, $USER;
|
|
|
783 |
$record = (object) array_merge([
|
|
|
784 |
'badgeid' => null,
|
|
|
785 |
'userid' => null,
|
|
|
786 |
'uniquehash' => random_string(40),
|
|
|
787 |
'dateissued' => time(),
|
|
|
788 |
'dateexpire' => null,
|
|
|
789 |
'visible' => 1,
|
|
|
790 |
'issuernotified' => null,
|
|
|
791 |
], $params);
|
|
|
792 |
$record->id = $DB->insert_record('badge_issued', $record);
|
|
|
793 |
return $record;
|
|
|
794 |
}
|
|
|
795 |
|
|
|
796 |
/**
|
|
|
797 |
* Create a manual award.
|
|
|
798 |
*
|
|
|
799 |
* @param array $params Parameters.
|
|
|
800 |
* @return object
|
|
|
801 |
*/
|
|
|
802 |
protected function create_manual_award(array $params = []) {
|
|
|
803 |
global $DB, $USER;
|
|
|
804 |
$record = (object) array_merge([
|
|
|
805 |
'badgeid' => null,
|
|
|
806 |
'recipientid' => null,
|
|
|
807 |
'issuerid' => $USER->id,
|
|
|
808 |
'issuerrole' => $DB->get_field_select('role', 'id', 'shortname = ?', ['manager'], IGNORE_MULTIPLE),
|
|
|
809 |
'datemet' => time()
|
|
|
810 |
], $params);
|
|
|
811 |
$record->id = $DB->insert_record('badge_manual_award', $record);
|
|
|
812 |
return $record;
|
|
|
813 |
}
|
|
|
814 |
|
|
|
815 |
}
|