1
|
|
|
<?php |
2
|
|
|
/* For licensing terms, see /license.txt */ |
3
|
|
|
|
4
|
|
|
use Chamilo\CoreBundle\Entity\ExtraField; |
|
|
|
|
5
|
|
|
use Chamilo\CoreBundle\Entity\ExtraFieldValues; |
6
|
|
|
use Chamilo\PluginBundle\Entity\WhispeakAuth\LogEvent; |
7
|
|
|
use Chamilo\PluginBundle\Entity\WhispeakAuth\LogEventLp; |
8
|
|
|
use Chamilo\PluginBundle\Entity\WhispeakAuth\LogEventQuiz; |
9
|
|
|
use Chamilo\UserBundle\Entity\User; |
10
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class WhispeakAuthPlugin. |
14
|
|
|
*/ |
15
|
|
|
class WhispeakAuthPlugin extends Plugin implements HookPluginInterface |
16
|
|
|
{ |
17
|
|
|
const SETTING_ENABLE = 'enable'; |
18
|
|
|
const SETTING_MAX_ATTEMPTS = 'max_attempts'; |
19
|
|
|
const SETTING_2FA = '2fa'; |
20
|
|
|
const SETTING_API_URL = 'api_url'; |
21
|
|
|
const SETTING_TOKEN = 'token'; |
22
|
|
|
|
23
|
|
|
const EXTRAFIELD_AUTH_UID = 'whispeak_auth_uid'; |
24
|
|
|
const EXTRAFIELD_LP_ITEM = 'whispeak_lp_item'; |
25
|
|
|
const EXTRAFIELD_QUIZ_QUESTION = 'whispeak_quiz_question'; |
26
|
|
|
|
27
|
|
|
const SESSION_FAILED_LOGINS = 'whispeak_failed_logins'; |
28
|
|
|
const SESSION_2FA_USER = 'whispeak_user_id'; |
29
|
|
|
const SESSION_LP_ITEM = 'whispeak_lp_item'; |
30
|
|
|
const SESSION_QUIZ_QUESTION = 'whispeak_quiz_question'; |
31
|
|
|
const SESSION_AUTH_PASSWORD = 'whispeak_auth_password'; |
32
|
|
|
const SESSION_SENTENCE_TEXT = 'whispeak_sentence_text'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* StudentFollowUpPlugin constructor. |
36
|
|
|
*/ |
37
|
|
|
protected function __construct() |
38
|
|
|
{ |
39
|
|
|
parent::__construct( |
40
|
|
|
'0.1', |
41
|
|
|
'Angel Fernando Quiroz', |
42
|
|
|
[ |
43
|
|
|
self::SETTING_ENABLE => 'boolean', |
44
|
|
|
self::SETTING_API_URL => 'text', |
45
|
|
|
self::SETTING_TOKEN => 'text', |
46
|
|
|
self::SETTING_MAX_ATTEMPTS => 'text', |
47
|
|
|
self::SETTING_2FA => 'boolean', |
48
|
|
|
] |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the admin URL for the plugin if Plugin::isAdminPlugin is true. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getAdminUrl() |
58
|
|
|
{ |
59
|
|
|
$webPath = api_get_path(WEB_PLUGIN_PATH).$this->get_name(); |
60
|
|
|
|
61
|
|
|
return "$webPath/admin.php"; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return WhispeakAuthPlugin |
66
|
|
|
*/ |
67
|
|
|
public static function create() |
68
|
|
|
{ |
69
|
|
|
static $result = null; |
70
|
|
|
|
71
|
|
|
return $result ? $result : $result = new self(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @throws \Doctrine\ORM\Tools\ToolsException |
76
|
|
|
*/ |
77
|
|
|
public function install() |
78
|
|
|
{ |
79
|
|
|
$this->installExtraFields(); |
80
|
|
|
$this->installEntities(); |
81
|
|
|
$this->installHook(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function uninstall() |
85
|
|
|
{ |
86
|
|
|
$this->uninstallHook(); |
87
|
|
|
$this->uninstallExtraFields(); |
88
|
|
|
$this->uninstallEntities(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return ExtraField |
93
|
|
|
*/ |
94
|
|
|
public static function getAuthUidExtraField() |
95
|
|
|
{ |
96
|
|
|
$em = Database::getManager(); |
97
|
|
|
$efRepo = $em->getRepository('ChamiloCoreBundle:ExtraField'); |
98
|
|
|
|
99
|
|
|
/** @var ExtraField $extraField */ |
100
|
|
|
$extraField = $efRepo->findOneBy( |
101
|
|
|
[ |
102
|
|
|
'variable' => self::EXTRAFIELD_AUTH_UID, |
103
|
|
|
'extraFieldType' => ExtraField::USER_FIELD_TYPE, |
104
|
|
|
] |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
return $extraField; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return ExtraField |
112
|
|
|
*/ |
113
|
|
|
public static function getLpItemExtraField() |
114
|
|
|
{ |
115
|
|
|
$efRepo = Database::getManager()->getRepository('ChamiloCoreBundle:ExtraField'); |
116
|
|
|
|
117
|
|
|
/** @var ExtraField $extraField */ |
118
|
|
|
$extraField = $efRepo->findOneBy( |
119
|
|
|
[ |
120
|
|
|
'variable' => self::EXTRAFIELD_LP_ITEM, |
121
|
|
|
'extraFieldType' => ExtraField::LP_ITEM_FIELD_TYPE, |
122
|
|
|
] |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return $extraField; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return ExtraField |
130
|
|
|
*/ |
131
|
|
|
public static function getQuizQuestionExtraField() |
132
|
|
|
{ |
133
|
|
|
$efRepo = Database::getManager()->getRepository('ChamiloCoreBundle:ExtraField'); |
134
|
|
|
|
135
|
|
|
/** @var ExtraField $extraField */ |
136
|
|
|
$extraField = $efRepo->findOneBy( |
137
|
|
|
[ |
138
|
|
|
'variable' => self::EXTRAFIELD_QUIZ_QUESTION, |
139
|
|
|
'extraFieldType' => ExtraField::QUESTION_FIELD_TYPE, |
140
|
|
|
] |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
return $extraField; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param int $userId |
148
|
|
|
* |
149
|
|
|
* @return ExtraFieldValues |
150
|
|
|
*/ |
151
|
|
|
public static function getAuthUidValue($userId) |
152
|
|
|
{ |
153
|
|
|
$extraField = self::getAuthUidExtraField(); |
154
|
|
|
$em = Database::getManager(); |
155
|
|
|
$efvRepo = $em->getRepository('ChamiloCoreBundle:ExtraFieldValues'); |
156
|
|
|
|
157
|
|
|
/** @var ExtraFieldValues $value */ |
158
|
|
|
$value = $efvRepo->findOneBy(['field' => $extraField, 'itemId' => $userId]); |
159
|
|
|
|
160
|
|
|
return $value; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the whispeak_lp_item value for a LP item ID. |
165
|
|
|
* |
166
|
|
|
* @param int $lpItemId |
167
|
|
|
* |
168
|
|
|
* @return array|false |
169
|
|
|
*/ |
170
|
|
|
public static function getLpItemValue($lpItemId) |
171
|
|
|
{ |
172
|
|
|
$efv = new ExtraFieldValue('lp_item'); |
173
|
|
|
$value = $efv->get_values_by_handler_and_field_variable($lpItemId, self::EXTRAFIELD_LP_ITEM); |
174
|
|
|
|
175
|
|
|
return $value; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param int $lpItemId |
180
|
|
|
* |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
|
|
public static function isLpItemMarked($lpItemId) |
184
|
|
|
{ |
185
|
|
|
if (!self::create()->isEnabled()) { |
186
|
|
|
return false; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$value = self::getLpItemValue($lpItemId); |
190
|
|
|
|
191
|
|
|
return !empty($value) && !empty($value['value']); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get the whispeak_quiz_question value for a quiz question ID. |
196
|
|
|
* |
197
|
|
|
* @param int $questionId |
198
|
|
|
* |
199
|
|
|
* @return array|false |
200
|
|
|
*/ |
201
|
|
|
public static function getQuizQuestionValue($questionId) |
202
|
|
|
{ |
203
|
|
|
$efv = new ExtraFieldValue('question'); |
204
|
|
|
$value = $efv->get_values_by_handler_and_field_variable($questionId, self::EXTRAFIELD_QUIZ_QUESTION); |
205
|
|
|
|
206
|
|
|
return $value; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param int $questionId |
211
|
|
|
* |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
|
|
public static function isQuizQuestionMarked($questionId) |
215
|
|
|
{ |
216
|
|
|
if (!self::create()->isEnabled()) { |
217
|
|
|
return false; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$value = self::getQuizQuestionValue($questionId); |
221
|
|
|
|
222
|
|
|
return !empty($value) && !empty($value['value']); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param int $questionId |
227
|
|
|
* |
228
|
|
|
* @return bool |
229
|
|
|
*/ |
230
|
|
|
public static function questionRequireAuthentify($questionId) |
231
|
|
|
{ |
232
|
|
|
$isMarked = self::isQuizQuestionMarked($questionId); |
233
|
|
|
|
234
|
|
|
if (!$isMarked) { |
235
|
|
|
return false; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$questionInfo = ChamiloSession::read(self::SESSION_QUIZ_QUESTION, []); |
239
|
|
|
|
240
|
|
|
if (empty($questionInfo)) { |
241
|
|
|
return true; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if ((int) $questionId !== $questionInfo['question']) { |
245
|
|
|
return true; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
if (false === $questionInfo['passed']) { |
249
|
|
|
return true; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return false; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param int $userId |
257
|
|
|
* |
258
|
|
|
* @return bool |
259
|
|
|
*/ |
260
|
|
|
public static function checkUserIsEnrolled($userId) |
261
|
|
|
{ |
262
|
|
|
$value = self::getAuthUidValue($userId); |
263
|
|
|
|
264
|
|
|
if (empty($value)) { |
265
|
|
|
return false; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return !empty($value->getValue()); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @return string |
273
|
|
|
*/ |
274
|
|
|
public static function getEnrollmentUrl() |
275
|
|
|
{ |
276
|
|
|
return api_get_path(WEB_PLUGIN_PATH).'whispeakauth/enrollment.php'; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param string $uid |
281
|
|
|
* |
282
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
283
|
|
|
*/ |
284
|
|
|
public function saveEnrollment(User $user, $uid) |
285
|
|
|
{ |
286
|
|
|
$em = Database::getManager(); |
287
|
|
|
$extraFieldValue = self::getAuthUidValue($user->getId()); |
288
|
|
|
|
289
|
|
|
if (empty($extraFieldValue)) { |
290
|
|
|
$extraField = self::getAuthUidExtraField(); |
291
|
|
|
$now = new DateTime('now', new DateTimeZone('UTC')); |
292
|
|
|
|
293
|
|
|
$extraFieldValue = new ExtraFieldValues(); |
294
|
|
|
$extraFieldValue |
295
|
|
|
->setField($extraField) |
296
|
|
|
->setItemId($user->getId()) |
297
|
|
|
->setUpdatedAt($now); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$extraFieldValue->setValue($uid); |
301
|
|
|
|
302
|
|
|
$em->persist($extraFieldValue); |
303
|
|
|
$em->flush(); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @return bool |
308
|
|
|
*/ |
309
|
|
|
public function toolIsEnabled() |
310
|
|
|
{ |
311
|
|
|
return 'true' === $this->get(self::SETTING_ENABLE); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Access not allowed when tool is not enabled. |
316
|
|
|
* |
317
|
|
|
* @param bool $printHeaders Optional. Print headers. |
318
|
|
|
*/ |
319
|
|
|
public function protectTool($printHeaders = true) |
320
|
|
|
{ |
321
|
|
|
if ($this->toolIsEnabled()) { |
322
|
|
|
return; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
api_not_allowed($printHeaders); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Get the max_attemtps option. |
330
|
|
|
* |
331
|
|
|
* @return int |
332
|
|
|
*/ |
333
|
|
|
public function getMaxAttempts() |
334
|
|
|
{ |
335
|
|
|
return (int) $this->get(self::SETTING_MAX_ATTEMPTS); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Install hook when saving the plugin configuration. |
340
|
|
|
* |
341
|
|
|
* @return WhispeakAuthPlugin |
342
|
|
|
*/ |
343
|
|
|
public function performActionsAfterConfigure() |
344
|
|
|
{ |
345
|
|
|
$observer = WhispeakConditionalLoginHook::create(); |
346
|
|
|
|
347
|
|
|
if ('true' === $this->get(self::SETTING_2FA)) { |
348
|
|
|
HookConditionalLogin::create()->attach($observer); |
349
|
|
|
} else { |
350
|
|
|
HookConditionalLogin::create()->detach($observer); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
return $this; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* This method will call the Hook management insertHook to add Hook observer from this plugin. |
358
|
|
|
*/ |
359
|
|
|
public function installHook() |
360
|
|
|
{ |
361
|
|
|
$observer = WhispeakMyStudentsLpTrackingHook::create(); |
362
|
|
|
HookMyStudentsLpTracking::create()->attach($observer); |
363
|
|
|
|
364
|
|
|
$observer = WhispeakMyStudentsQuizTrackingHook::create(); |
365
|
|
|
HookMyStudentsQuizTracking::create()->attach($observer); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* This method will call the Hook management deleteHook to disable Hook observer from this plugin. |
370
|
|
|
*/ |
371
|
|
|
public function uninstallHook() |
372
|
|
|
{ |
373
|
|
|
$observer = WhispeakConditionalLoginHook::create(); |
374
|
|
|
HookConditionalLogin::create()->detach($observer); |
375
|
|
|
|
376
|
|
|
$observer = WhispeakMyStudentsLpTrackingHook::create(); |
377
|
|
|
HookMyStudentsLpTracking::create()->detach($observer); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* @param int $userId |
382
|
|
|
* |
383
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
384
|
|
|
* |
385
|
|
|
* @return bool |
386
|
|
|
*/ |
387
|
|
|
public static function deleteEnrollment($userId) |
388
|
|
|
{ |
389
|
|
|
$extraFieldValue = self::getAuthUidValue($userId); |
390
|
|
|
|
391
|
|
|
if (empty($extraFieldValue)) { |
392
|
|
|
return false; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
$em = Database::getManager(); |
396
|
|
|
$em->remove($extraFieldValue); |
397
|
|
|
$em->flush(); |
398
|
|
|
|
399
|
|
|
return true; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Check if the WhispeakAuth plugin is installed and enabled. |
404
|
|
|
* |
405
|
|
|
* @param bool $checkEnabled Check if, additionnally to being installed, the plugin is enabled |
406
|
|
|
* |
407
|
|
|
* @return bool |
408
|
|
|
*/ |
409
|
|
|
public function isEnabled($checkEnabled = false) |
410
|
|
|
{ |
411
|
|
|
return parent::isEnabled() && 'true' === api_get_plugin_setting('whispeakauth', self::SETTING_ENABLE); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* @param int $lpItemId |
416
|
|
|
* |
417
|
|
|
* @return bool |
418
|
|
|
*/ |
419
|
|
|
public static function isAllowedToSaveLpItem($lpItemId) |
420
|
|
|
{ |
421
|
|
|
if (!self::isLpItemMarked($lpItemId)) { |
422
|
|
|
return true; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
$markedItem = ChamiloSession::read(self::SESSION_LP_ITEM, []); |
426
|
|
|
|
427
|
|
|
if (empty($markedItem)) { |
428
|
|
|
return true; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
if ((int) $lpItemId !== (int) $markedItem['lp_item']) { |
432
|
|
|
return true; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
return false; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Display a error message. |
440
|
|
|
* |
441
|
|
|
* @param string|null $error Optional. The message text |
442
|
|
|
*/ |
443
|
|
|
public static function displayNotAllowedMessage($error = null) |
444
|
|
|
{ |
445
|
|
|
$error = empty($error) ? get_lang('NotAllowed') : $error; |
446
|
|
|
|
447
|
|
|
echo Display::return_message($error, 'error', false); |
448
|
|
|
|
449
|
|
|
exit; |
|
|
|
|
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* @param int $questionId |
454
|
|
|
* |
455
|
|
|
* @throws Exception |
456
|
|
|
* |
457
|
|
|
* @return string |
458
|
|
|
*/ |
459
|
|
|
public static function quizQuestionAuthentify($questionId, Exercise $exercise) |
460
|
|
|
{ |
461
|
|
|
ChamiloSession::write( |
462
|
|
|
self::SESSION_QUIZ_QUESTION, |
463
|
|
|
[ |
464
|
|
|
'quiz' => (int) $exercise->iId, |
|
|
|
|
465
|
|
|
'question' => (int) $questionId, |
466
|
|
|
'url_params' => $_SERVER['QUERY_STRING'], |
467
|
|
|
'passed' => false, |
468
|
|
|
] |
469
|
|
|
); |
470
|
|
|
|
471
|
|
|
$template = new Template('', false, false, false, true, false, false); |
472
|
|
|
$template->assign('question', $questionId); |
473
|
|
|
$template->assign('exercise', $exercise->iId); |
474
|
|
|
$content = $template->fetch('whispeakauth/view/quiz_question.html.twig'); |
475
|
|
|
|
476
|
|
|
echo $content; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* @param int $status |
481
|
|
|
* @param int $userId |
482
|
|
|
* @param int $lpItemId |
483
|
|
|
* @param int $lpId |
484
|
|
|
* |
485
|
|
|
* @throws \Doctrine\ORM\ORMException |
486
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
487
|
|
|
* @throws \Doctrine\ORM\TransactionRequiredException |
488
|
|
|
* |
489
|
|
|
* @return LogEventLp|null |
490
|
|
|
*/ |
491
|
|
|
public function addAttemptInLearningPath($status, $userId, $lpItemId, $lpId) |
492
|
|
|
{ |
493
|
|
|
$em = Database::getManager(); |
494
|
|
|
|
495
|
|
|
$user = api_get_user_entity($userId); |
496
|
|
|
$lpItem = $em->find('ChamiloCourseBundle:CLpItem', $lpItemId); |
497
|
|
|
$lp = $em->find('ChamiloCourseBundle:CLp', $lpId); |
498
|
|
|
|
499
|
|
|
if (empty($lp) || empty($lpItem)) { |
500
|
|
|
return null; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
$logEvent = new LogEventLp(); |
504
|
|
|
$logEvent |
505
|
|
|
->setLpItem($lpItem) |
506
|
|
|
->setLp($lp) |
507
|
|
|
->setUser($user) |
508
|
|
|
->setDatetime( |
509
|
|
|
api_get_utc_datetime(null, false, true) |
510
|
|
|
) |
511
|
|
|
->setActionStatus($status); |
512
|
|
|
|
513
|
|
|
$em->persist($logEvent); |
514
|
|
|
$em->flush(); |
515
|
|
|
|
516
|
|
|
return $logEvent; |
517
|
|
|
} |
518
|
|
|
|
519
|
|
|
/** |
520
|
|
|
* @param int $status |
521
|
|
|
* @param int $userId |
522
|
|
|
* @param int $questionId |
523
|
|
|
* @param int $quizId |
524
|
|
|
* |
525
|
|
|
* @throws \Doctrine\ORM\ORMException |
526
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
527
|
|
|
* @throws \Doctrine\ORM\TransactionRequiredException |
528
|
|
|
* |
529
|
|
|
* @return LogEventQuiz|null |
530
|
|
|
*/ |
531
|
|
|
public function addAttemptInQuiz($status, $userId, $questionId, $quizId) |
532
|
|
|
{ |
533
|
|
|
$em = Database::getManager(); |
534
|
|
|
|
535
|
|
|
$user = api_get_user_entity($userId); |
536
|
|
|
$question = $em->find('ChamiloCourseBundle:CQuizQuestion', $questionId); |
537
|
|
|
$quiz = $em->find('ChamiloCourseBundle:CQuiz', $quizId); |
538
|
|
|
|
539
|
|
|
if (empty($quiz) || empty($question)) { |
540
|
|
|
return null; |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
$logEvent = new LogEventQuiz(); |
544
|
|
|
$logEvent |
545
|
|
|
->setQuestion($question) |
546
|
|
|
->setQuiz($quiz) |
547
|
|
|
->setUser($user) |
548
|
|
|
->setDatetime( |
549
|
|
|
api_get_utc_datetime(null, false, true) |
550
|
|
|
) |
551
|
|
|
->setActionStatus($status); |
552
|
|
|
|
553
|
|
|
$em->persist($logEvent); |
554
|
|
|
$em->flush(); |
555
|
|
|
|
556
|
|
|
return $logEvent; |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* @param int $status |
561
|
|
|
* @param int $userId |
562
|
|
|
* |
563
|
|
|
* @throws \Doctrine\ORM\ORMException |
564
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
565
|
|
|
* @throws \Doctrine\ORM\TransactionRequiredException |
566
|
|
|
* |
567
|
|
|
* @return LogEvent|null |
568
|
|
|
*/ |
569
|
|
|
public function addAuthenticationAttempt($status, $userId) |
570
|
|
|
{ |
571
|
|
|
$em = Database::getManager(); |
572
|
|
|
|
573
|
|
|
$user = api_get_user_entity($userId); |
574
|
|
|
|
575
|
|
|
$logEvent = new LogEvent(); |
576
|
|
|
$logEvent |
577
|
|
|
->setUser($user) |
578
|
|
|
->setDatetime( |
579
|
|
|
api_get_utc_datetime(null, false, true) |
580
|
|
|
) |
581
|
|
|
->setActionStatus($status); |
582
|
|
|
|
583
|
|
|
$em->persist($logEvent); |
584
|
|
|
$em->flush(); |
585
|
|
|
|
586
|
|
|
return $logEvent; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* @param int $lpId |
591
|
|
|
* @param int $userId |
592
|
|
|
* |
593
|
|
|
* @throws \Doctrine\ORM\Query\QueryException |
594
|
|
|
* |
595
|
|
|
* @return string |
596
|
|
|
*/ |
597
|
|
|
public static function countAllAttemptsInLearningPath($lpId, $userId) |
598
|
|
|
{ |
599
|
|
|
$query = Database::getManager() |
600
|
|
|
->createQuery( |
601
|
|
|
'SELECT COUNT(log) AS c FROM ChamiloPluginBundle:WhispeakAuth\LogEventLp log |
602
|
|
|
WHERE log.lp = :lp AND log.user = :user' |
603
|
|
|
) |
604
|
|
|
->setParameters(['lp' => $lpId, 'user' => $userId]); |
605
|
|
|
|
606
|
|
|
$totalCount = (int) $query->getSingleScalarResult(); |
607
|
|
|
|
608
|
|
|
return $totalCount; |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
/** |
612
|
|
|
* @param int $lpId |
613
|
|
|
* @param int $userId |
614
|
|
|
* |
615
|
|
|
* @throws \Doctrine\ORM\Query\QueryException |
616
|
|
|
* |
617
|
|
|
* @return string |
618
|
|
|
*/ |
619
|
|
|
public static function countSuccessAttemptsInLearningPath($lpId, $userId) |
620
|
|
|
{ |
621
|
|
|
$query = Database::getManager() |
622
|
|
|
->createQuery( |
623
|
|
|
'SELECT COUNT(log) AS c FROM ChamiloPluginBundle:WhispeakAuth\LogEventLp log |
624
|
|
|
WHERE log.lp = :lp AND log.user = :user AND log.actionStatus = :status' |
625
|
|
|
) |
626
|
|
|
->setParameters(['lp' => $lpId, 'user' => $userId, 'status' => LogEvent::STATUS_SUCCESS]); |
627
|
|
|
|
628
|
|
|
$totalCount = (int) $query->getSingleScalarResult(); |
629
|
|
|
|
630
|
|
|
return $totalCount; |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
/** |
634
|
|
|
* @param int $quizId |
635
|
|
|
* @param int $userId |
636
|
|
|
* |
637
|
|
|
* @throws \Doctrine\ORM\Query\QueryException |
638
|
|
|
* |
639
|
|
|
* @return string |
640
|
|
|
*/ |
641
|
|
|
public static function countAllAttemptsInQuiz($quizId, $userId) |
642
|
|
|
{ |
643
|
|
|
$query = Database::getManager() |
644
|
|
|
->createQuery( |
645
|
|
|
'SELECT COUNT(log) AS c FROM ChamiloPluginBundle:WhispeakAuth\LogEventQuiz log |
646
|
|
|
WHERE log.quiz = :quiz AND log.user = :user' |
647
|
|
|
) |
648
|
|
|
->setParameters(['quiz' => $quizId, 'user' => $userId]); |
649
|
|
|
|
650
|
|
|
$totalCount = (int) $query->getSingleScalarResult(); |
651
|
|
|
|
652
|
|
|
return $totalCount; |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** |
656
|
|
|
* @param int $quizId |
657
|
|
|
* @param int $userId |
658
|
|
|
* |
659
|
|
|
* @throws \Doctrine\ORM\Query\QueryException |
660
|
|
|
* |
661
|
|
|
* @return string |
662
|
|
|
*/ |
663
|
|
|
public static function countSuccessAttemptsInQuiz($quizId, $userId) |
664
|
|
|
{ |
665
|
|
|
$query = Database::getManager() |
666
|
|
|
->createQuery( |
667
|
|
|
'SELECT COUNT(log) AS c FROM ChamiloPluginBundle:WhispeakAuth\LogEventQuiz log |
668
|
|
|
WHERE log.quiz = :quiz AND log.user = :user AND log.actionStatus = :status' |
669
|
|
|
) |
670
|
|
|
->setParameters(['quiz' => $quizId, 'user' => $userId, 'status' => LogEvent::STATUS_SUCCESS]); |
671
|
|
|
|
672
|
|
|
$totalCount = (int) $query->getSingleScalarResult(); |
673
|
|
|
|
674
|
|
|
return $totalCount; |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* @return string |
679
|
|
|
*/ |
680
|
|
|
public function getApiUrl() |
681
|
|
|
{ |
682
|
|
|
$url = $this->get(self::SETTING_API_URL); |
683
|
|
|
|
684
|
|
|
return trim($url, " \t\n\r \v/").'/'; |
685
|
|
|
} |
686
|
|
|
|
687
|
|
|
/** |
688
|
|
|
* Install extra fields for user, learning path and quiz question. |
689
|
|
|
*/ |
690
|
|
|
private function installExtraFields() |
691
|
|
|
{ |
692
|
|
|
UserManager::create_extra_field( |
693
|
|
|
self::EXTRAFIELD_AUTH_UID, |
694
|
|
|
\ExtraField::FIELD_TYPE_TEXT, |
695
|
|
|
$this->get_lang('Whispeak uid'), |
696
|
|
|
'' |
697
|
|
|
); |
698
|
|
|
|
699
|
|
|
LpItem::createExtraField( |
700
|
|
|
self::EXTRAFIELD_LP_ITEM, |
701
|
|
|
\ExtraField::FIELD_TYPE_CHECKBOX, |
702
|
|
|
$this->get_lang('MarkForSpeechAuthentication'), |
703
|
|
|
'0', |
704
|
|
|
true, |
705
|
|
|
true |
706
|
|
|
); |
707
|
|
|
|
708
|
|
|
$extraField = new \ExtraField('question'); |
709
|
|
|
$params = [ |
710
|
|
|
'variable' => self::EXTRAFIELD_QUIZ_QUESTION, |
711
|
|
|
'field_type' => \ExtraField::FIELD_TYPE_CHECKBOX, |
712
|
|
|
'display_text' => $this->get_lang('MarkForSpeechAuthentication'), |
713
|
|
|
'default_value' => '0', |
714
|
|
|
'changeable' => true, |
715
|
|
|
'visible_to_self' => true, |
716
|
|
|
'visible_to_others' => false, |
717
|
|
|
]; |
718
|
|
|
|
719
|
|
|
$extraField->save($params); |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
/** |
723
|
|
|
* Install the Doctrine's entities. |
724
|
|
|
* @throws \Doctrine\ORM\Tools\ToolsException |
725
|
|
|
*/ |
726
|
|
|
private function installEntities() |
727
|
|
|
{ |
728
|
|
|
$em = Database::getManager(); |
729
|
|
|
|
730
|
|
|
if ($em->getConnection()->getSchemaManager()->tablesExist(['whispeak_log_event'])) { |
731
|
|
|
return; |
732
|
|
|
} |
733
|
|
|
|
734
|
|
|
$schemaTool = new SchemaTool($em); |
735
|
|
|
$schemaTool->createSchema( |
736
|
|
|
[ |
737
|
|
|
$em->getClassMetadata(LogEvent::class), |
738
|
|
|
$em->getClassMetadata(LogEventLp::class), |
739
|
|
|
$em->getClassMetadata(LogEventQuiz::class), |
740
|
|
|
] |
741
|
|
|
); |
742
|
|
|
} |
743
|
|
|
|
744
|
|
|
/** |
745
|
|
|
* Uninstall extra fields for user, learning path and quiz question. |
746
|
|
|
*/ |
747
|
|
|
private function uninstallExtraFields() |
748
|
|
|
{ |
749
|
|
|
$em = Database::getManager(); |
750
|
|
|
|
751
|
|
|
$authIdExtrafield = self::getAuthUidExtraField(); |
752
|
|
|
|
753
|
|
|
if (!empty($authIdExtrafield)) { |
754
|
|
|
$em |
755
|
|
|
->createQuery('DELETE FROM ChamiloCoreBundle:ExtraFieldValues efv WHERE efv.field = :field') |
756
|
|
|
->execute(['field' => $authIdExtrafield]); |
757
|
|
|
|
758
|
|
|
$em->remove($authIdExtrafield); |
759
|
|
|
$em->flush(); |
760
|
|
|
} |
761
|
|
|
|
762
|
|
|
$lpItemExtrafield = self::getLpItemExtraField(); |
763
|
|
|
|
764
|
|
|
if (!empty($lpItemExtrafield)) { |
765
|
|
|
$em |
766
|
|
|
->createQuery('DELETE FROM ChamiloCoreBundle:ExtraFieldValues efv WHERE efv.field = :field') |
767
|
|
|
->execute(['field' => $lpItemExtrafield]); |
768
|
|
|
|
769
|
|
|
$em->remove($lpItemExtrafield); |
770
|
|
|
$em->flush(); |
771
|
|
|
} |
772
|
|
|
|
773
|
|
|
$quizQuestionExtrafield = self::getQuizQuestionExtraField(); |
774
|
|
|
|
775
|
|
|
if (!empty($quizQuestionExtrafield)) { |
776
|
|
|
$em |
777
|
|
|
->createQuery('DELETE FROM ChamiloCoreBundle:ExtraFieldValues efv WHERE efv.field = :field') |
778
|
|
|
->execute(['field' => $quizQuestionExtrafield]); |
779
|
|
|
|
780
|
|
|
$em->remove($quizQuestionExtrafield); |
781
|
|
|
$em->flush(); |
782
|
|
|
} |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
/** |
786
|
|
|
* Uninstall the Doctrine's entities. |
787
|
|
|
*/ |
788
|
|
|
private function uninstallEntities() |
789
|
|
|
{ |
790
|
|
|
$em = Database::getManager(); |
791
|
|
|
|
792
|
|
|
if (!$em->getConnection()->getSchemaManager()->tablesExist(['whispeak_log_event'])) { |
793
|
|
|
return; |
794
|
|
|
} |
795
|
|
|
|
796
|
|
|
$schemaTool = new SchemaTool($em); |
797
|
|
|
$schemaTool->dropSchema( |
798
|
|
|
[ |
799
|
|
|
$em->getClassMetadata(LogEvent::class), |
800
|
|
|
$em->getClassMetadata(LogEventLp::class), |
801
|
|
|
$em->getClassMetadata(LogEventQuiz::class), |
802
|
|
|
] |
803
|
|
|
); |
804
|
|
|
} |
805
|
|
|
} |
806
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: