1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\ServiceHelper; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Repository\AccessUrlRelPluginRepository; |
10
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
11
|
|
|
|
12
|
|
|
class PluginServiceHelper |
13
|
|
|
{ |
14
|
|
|
public function __construct( |
15
|
|
|
private ParameterBagInterface $parameterBag, |
16
|
|
|
private AccessUrlRelPluginRepository $pluginRepo, |
17
|
|
|
private AccessUrlHelper $accessUrlHelper, |
18
|
|
|
) {} |
19
|
|
|
|
20
|
|
|
public function loadLegacyPlugin(string $pluginName): ?object |
21
|
|
|
{ |
22
|
|
|
$projectDir = $this->parameterBag->get('kernel.project_dir'); |
23
|
|
|
$pluginPath = $projectDir . '/public/plugin/' . $pluginName . '/src/' . $pluginName . '.php'; |
24
|
|
|
$pluginClass = $pluginName; |
25
|
|
|
|
26
|
|
|
if (!file_exists($pluginPath)) { |
27
|
|
|
return null; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (!class_exists($pluginClass)) { |
31
|
|
|
require_once $pluginPath; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (class_exists($pluginClass) && method_exists($pluginClass, 'create')) { |
35
|
|
|
return $pluginClass::create(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getPluginSetting(string $pluginName, string $settingKey): mixed |
42
|
|
|
{ |
43
|
|
|
$plugin = $this->loadLegacyPlugin($pluginName); |
44
|
|
|
|
45
|
|
|
if (!$plugin || !method_exists($plugin, 'get')) { |
46
|
|
|
return null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $plugin->get($settingKey); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function isPluginEnabled(string $pluginName): bool |
53
|
|
|
{ |
54
|
|
|
$accessUrl = $this->accessUrlHelper->getCurrent(); |
55
|
|
|
if (null === $accessUrl) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$pluginSetting = $this->pluginRepo->findOneByPluginName($pluginName, $accessUrl->getId()); |
60
|
|
|
|
61
|
|
|
return $pluginSetting && $pluginSetting->isActive(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function shouldBlockAccessByPositioning(?int $userId, int $courseId, ?int $sessionId): bool |
65
|
|
|
{ |
66
|
|
|
if (!$this->isPluginEnabled('Positioning') || !$userId) { |
|
|
|
|
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$plugin = $this->loadLegacyPlugin('Positioning'); |
71
|
|
|
|
72
|
|
|
if (!$plugin || $plugin->get('block_course_if_initial_exercise_not_attempted') !== 'true') { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$initialData = $plugin->getInitialExercise($courseId, $sessionId); |
77
|
|
|
|
78
|
|
|
if (empty($initialData['exercise_id'])) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$results = \Event::getExerciseResultsByUser( |
|
|
|
|
83
|
|
|
$userId, |
84
|
|
|
(int) $initialData['exercise_id'], |
85
|
|
|
$courseId, |
86
|
|
|
$sessionId |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
return empty($results); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: