Passed
Pull Request — 1.11.x (#4900)
by Angel Fernando Quiroz
09:12
created

ExerciseMonitoringPlugin::isAdult()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 21
rs 9.5555
c 1
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\PluginBundle\ExerciseMonitoring\Entity\Log;
6
use Doctrine\ORM\Tools\SchemaTool;
7
use Doctrine\ORM\Tools\ToolsException;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
class ExerciseMonitoringPlugin extends Plugin
11
{
12
    public const SETTING_TOOL_ENABLE = 'tool_enable';
13
    public const SETTING_INSTRUCTIONS = 'intructions';
14
    public const SETTING_INSTRUCTION_AGE_DISTINCTION_ENABLE = 'age_distinction_enable';
15
    public const SETTING_INSTRUCTION_LEGAL_AGE = 'legal_age';
16
    public const SETTING_EXTRAFIELD_BIRTHDATE = 'extrafield_birtdate';
17
    public const SETTING_INSTRUCTIONS_ADULTS = 'instructions_adults';
18
    public const SETTING_INSTRUCTIONS_MINORS = 'instructions_minors';
19
    public const SETTING_SNAPSHOTS_LIFETIME = 'snapshots_lifetime';
20
21
    public const FIELD_SELECTED = 'exercisemonitoring_selected';
22
23
    private const TABLE_LOG = 'plugin_exercisemonitoring_log';
24
25
    protected function __construct()
26
    {
27
        $version = '0.0.1';
28
29
        $settings = [
30
            self::SETTING_TOOL_ENABLE => 'boolean',
31
            self::SETTING_INSTRUCTIONS => 'wysiwyg',
32
            self::SETTING_INSTRUCTION_AGE_DISTINCTION_ENABLE => 'boolean',
33
            self::SETTING_INSTRUCTION_LEGAL_AGE => 'text',
34
            self::SETTING_EXTRAFIELD_BIRTHDATE => 'text',
35
            self::SETTING_INSTRUCTIONS_ADULTS => 'wysiwyg',
36
            self::SETTING_INSTRUCTIONS_MINORS => 'wysiwyg',
37
            self::SETTING_SNAPSHOTS_LIFETIME => 'text',
38
        ];
39
40
        parent::__construct(
41
            $version,
42
            "Angel Fernando Quiroz Campos <[email protected]>",
43
            $settings
44
        );
45
    }
46
47
    public static function create(): self
48
    {
49
        static $result = null;
50
51
        return $result ?: $result = new self();
52
    }
53
54
    /**
55
     * @throws ToolsException
56
     */
57
    public function install()
58
    {
59
        $em = Database::getManager();
60
61
        if ($em->getConnection()->getSchemaManager()->tablesExist([self::TABLE_LOG])) {
62
            return;
63
        }
64
65
        $schemaTool = new SchemaTool($em);
66
        $schemaTool->createSchema(
67
            [
68
                $em->getClassMetadata(Log::class),
69
            ]
70
        );
71
72
        $pluginDirName = api_get_path(SYS_UPLOAD_PATH).'plugins/exercisemonitoring';
73
74
        $fs = new Filesystem();
75
        $fs->mkdir(
76
            $pluginDirName,
77
            api_get_permissions_for_new_directories()
78
        );
79
80
        $objField = new ExtraField('exercise');
81
        $objField->save([
82
            'variable' => self::FIELD_SELECTED,
83
            'field_type' => ExtraField::FIELD_TYPE_CHECKBOX,
84
            'display_text' => $this->get_title(),
85
            'visible_to_self' => true,
86
            'changeable' => true,
87
            'filter' => false,
88
        ]);
89
    }
90
91
    public function uninstall()
92
    {
93
        $em = Database::getManager();
94
95
        if (!$em->getConnection()->getSchemaManager()->tablesExist([self::TABLE_LOG])) {
96
            return;
97
        }
98
99
        $schemaTool = new SchemaTool($em);
100
        $schemaTool->dropSchema(
101
            [
102
                $em->getClassMetadata(Log::class),
103
            ]
104
        );
105
106
        $objField = new ExtraField('exercise');
107
        $extraFieldInfo = $objField->get_handler_field_info_by_field_variable(self::FIELD_SELECTED);
108
109
        if ($extraFieldInfo) {
110
            $objField->delete($extraFieldInfo['id']);
111
        }
112
    }
113
114
    public function getAdminUrl(): string
115
    {
116
        $name = $this->get_name();
117
        $webPath = api_get_path(WEB_PLUGIN_PATH).$name;
118
119
        return "$webPath/admin.php";
120
    }
121
122
    public function generateDetailLink(int $exeId, int $userId): string
123
    {
124
        $title = $this->get_lang('ExerciseMonitored');
125
        $webcamIcon = Display::return_icon('webcam.png', $title);
126
        $webcamNaIcon = Display::return_icon('webcam_na.png', $this->get_lang('ExerciseUnmonitored'));
127
128
        $monitoringDetailUrl = api_get_path(WEB_PLUGIN_PATH).'exercisemonitoring/pages/detail.php?'.api_get_cidreq()
129
            .'&'.http_build_query(['id' => $exeId]);
130
131
        $url =  Display::url(
132
            $webcamIcon,
133
            $monitoringDetailUrl,
134
            [
135
                'class' => 'ajax',
136
                'data-title' => $title,
137
                'data-size' => 'lg',
138
            ]
139
        );
140
141
        $showLink = true;
142
143
        if ('true' === $this->get(self::SETTING_INSTRUCTION_AGE_DISTINCTION_ENABLE) && !$this->isAdult($userId)) {
144
            $showLink = false;
145
        }
146
147
        return $showLink ? $url : $webcamNaIcon;
148
    }
149
150
    public static function generateSnapshotUrl(
151
        int $userId,
152
        string $imageFileName,
153
        string $path = WEB_UPLOAD_PATH
154
    ): string {
155
        $pluginDirName = api_get_path($path).'plugins/exercisemonitoring';
156
157
        return $pluginDirName.'/'.$userId.'/'.$imageFileName;
158
    }
159
160
    /**
161
     * @throws Exception
162
     */
163
    public function isAdult(int $userId = 0): bool
164
    {
165
        $userId = $userId ?: api_get_user_id();
166
        $fieldVariable = $this->get(self::SETTING_EXTRAFIELD_BIRTHDATE);
167
        $legalAge = (int) $this->get(self::SETTING_INSTRUCTION_LEGAL_AGE);
168
169
        $value = UserManager::get_extra_user_data_by_field($userId, $fieldVariable);
170
171
        if (empty($value)) {
172
            return false;
173
        }
174
175
        if (empty($value[$fieldVariable])) {
176
            return false;
177
        }
178
179
        $birthdate = new DateTime($value[$fieldVariable]);
180
        $now = new DateTime();
181
        $diff = $birthdate->diff($now);
182
183
        return !$diff->invert && $diff->y >= $legalAge;
184
    }
185
}
186