Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

customcertificate/src/CustomCertificatePlugin.php (1 issue)

1
<?php
2
/* For license terms, see /license.txt */
3
4
/**
5
 * Plugin class for the CustomCertificate plugin.
6
 *
7
 * @package chamilo.plugin.customcertificate
8
 *
9
 * @author Jose Angel Ruiz <[email protected]>
10
 */
11
class CustomCertificatePlugin extends Plugin
12
{
13
    const TABLE_CUSTOMCERTIFICATE = 'plugin_customcertificate';
14
    public $isCoursePlugin = true;
15
16
    // When creating a new course this settings are added to the course
17
    public $course_settings = [
18
        [
19
            'name' => 'customcertificate_course_enable',
20
            'type' => 'checkbox',
21
        ],
22
        [
23
            'name' => 'use_certificate_default',
24
            'type' => 'checkbox',
25
        ],
26
    ];
27
28
    /**
29
     * Constructor.
30
     */
31
    protected function __construct()
32
    {
33
        parent::__construct(
34
            '1.0',
35
            'Jose Angel Ruiz - NoSoloRed (original author), Julio Montoya',
36
            [
37
                'enable_plugin_customcertificate' => 'boolean',
38
            ]
39
        );
40
41
        $this->isAdminPlugin = true;
42
    }
43
44
    /**
45
     * Instance the plugin.
46
     *
47
     * @staticvar null $result
48
     *
49
     * @return CustomCertificatePlugin
50
     */
51
    public static function create()
52
    {
53
        static $result = null;
54
55
        return $result ? $result : $result = new self();
56
    }
57
58
    /**
59
     * This method creates the tables required to this plugin.
60
     */
61
    public function install()
62
    {
63
        //Installing course settings
64
        $this->install_course_fields_in_all_courses();
65
66
        $tablesToBeCompared = [self::TABLE_CUSTOMCERTIFICATE];
67
        $em = Database::getManager();
68
        $cn = $em->getConnection();
69
        $sm = $cn->getSchemaManager();
70
        $tables = $sm->tablesExist($tablesToBeCompared);
71
72
        if ($tables) {
73
            return false;
74
        }
75
76
        require_once api_get_path(SYS_PLUGIN_PATH).'customcertificate/database.php';
77
    }
78
79
    /**
80
     * This method drops the plugin tables.
81
     */
82
    public function uninstall()
83
    {
84
        // Deleting course settings.
85
        $this->uninstall_course_fields_in_all_courses();
86
87
        $tablesToBeDeleted = [self::TABLE_CUSTOMCERTIFICATE];
88
        foreach ($tablesToBeDeleted as $tableToBeDeleted) {
89
            $table = Database::get_main_table($tableToBeDeleted);
90
            $sql = "DROP TABLE IF EXISTS $table";
91
            Database::query($sql);
92
        }
93
        $this->manageTab(false);
94
    }
95
96
    /**
97
     * This method update the previous plugin tables.
98
     */
99
    public function update()
100
    {
101
        $oldCertificateTable = 'gradebook_certificate_alternative';
102
        $base = api_get_path(WEB_UPLOAD_PATH);
103
        if (Database::num_rows(Database::query("SHOW TABLES LIKE '$oldCertificateTable'")) == 1) {
104
            $sql = "SELECT * FROM $oldCertificateTable";
105
            $res = Database::query($sql);
106
            while ($row = Database::fetch_assoc($res)) {
107
                $pathOrigin = $base.'certificates/'.$row['id'].'/';
108
                $params = [
109
                    'access_url_id' => api_get_current_access_url_id(),
110
                    'c_id' => $row['c_id'],
111
                    'session_id' => $row['session_id'],
112
                    'content_course' => $row['content_course'],
113
                    'contents_type' => intval($row['contents_type']),
114
                    'contents' => $row['contents'],
115
                    'date_change' => intval($row['date_change']),
116
                    'date_start' => $row['date_start'],
117
                    'date_end' => $row['date_end'],
118
                    'place' => $row['place'],
119
                    'type_date_expediction' => intval($row['type_date_expediction']),
120
                    'day' => $row['day'],
121
                    'month' => $row['month'],
122
                    'year' => $row['year'],
123
                    'logo_left' => $row['logo_left'],
124
                    'logo_center' => $row['logo_center'],
125
                    'logo_right' => $row['logo_right'],
126
                    'seal' => $row['seal'],
127
                    'signature1' => $row['signature1'],
128
                    'signature2' => $row['signature2'],
129
                    'signature3' => $row['signature3'],
130
                    'signature4' => $row['signature4'],
131
                    'signature_text1' => $row['signature_text1'],
132
                    'signature_text2' => $row['signature_text2'],
133
                    'signature_text3' => $row['signature_text3'],
134
                    'signature_text4' => $row['signature_text4'],
135
                    'background' => $row['background'],
136
                    'margin_left' => intval($row['margin']),
137
                    'margin_right' => 0,
138
                    'certificate_default' => 0,
139
                ];
140
141
                $certificateId = Database::insert(self::TABLE_CUSTOMCERTIFICATE, $params);
142
143
                // Image manager
144
                $pathDestiny = $base.'certificates/'.$certificateId.'/';
0 ignored issues
show
Are you sure $certificateId of type false|integer can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
                $pathDestiny = $base.'certificates/'./** @scrutinizer ignore-type */ $certificateId.'/';
Loading history...
145
146
                if (!file_exists($pathDestiny)) {
147
                    mkdir($pathDestiny, api_get_permissions_for_new_directories(), true);
148
                }
149
150
                $imgList = [
151
                    'logo_left',
152
                    'logo_center',
153
                    'logo_right',
154
                    'seal',
155
                    'signature1',
156
                    'signature2',
157
                    'signature3',
158
                    'signature4',
159
                    'background',
160
                ];
161
                foreach ($imgList as $value) {
162
                    if (!empty($row[$value])) {
163
                        copy(
164
                            $pathOrigin.$row[$value],
165
                            $pathDestiny.$row[$value]
166
                        );
167
                    }
168
                }
169
170
                if ($row['certificate_default'] == 1) {
171
                    $params['c_id'] = 0;
172
                    $params['session_id'] = 0;
173
                    $params['certificate_default'] = 1;
174
                    $certificateId = Database::insert(self::TABLE_CUSTOMCERTIFICATE, $params);
175
                    $pathOrigin = $base.'certificates/default/';
176
                    $pathDestiny = $base.'certificates/'.$certificateId.'/';
177
                    foreach ($imgList as $value) {
178
                        if (!empty($row[$value])) {
179
                            copy(
180
                                $pathOrigin.$row[$value],
181
                                $pathDestiny.$row[$value]
182
                            );
183
                        }
184
                    }
185
                }
186
            }
187
188
            $sql = "DROP TABLE IF EXISTS $oldCertificateTable";
189
            Database::query($sql);
190
191
            echo get_lang('MessageUpdate');
192
        }
193
    }
194
195
    /**
196
     * By default new icon is invisible.
197
     *
198
     * @return bool
199
     */
200
    public function isIconVisibleByDefault()
201
    {
202
        return false;
203
    }
204
205
    /**
206
     * Get certificate data.
207
     *
208
     * @param int $id     The certificate
209
     * @param int $userId
210
     *
211
     * @return array
212
     */
213
    public static function getCertificateData($id, $userId)
214
    {
215
        $id = (int) $id;
216
        $userId = (int) $userId;
217
218
        if (empty($id) || empty($userId)) {
219
            return [];
220
        }
221
222
        $certificateTable = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
223
        $categoryTable = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
224
        $sql = "SELECT cer.user_id AS user_id, cat.session_id AS session_id, cat.course_code AS course_code
225
                FROM $certificateTable cer
226
                INNER JOIN $categoryTable cat
227
                ON (cer.cat_id = cat.id AND cer.user_id = $userId)
228
                WHERE cer.id = $id";
229
230
        $rs = Database::query($sql);
231
        if (Database::num_rows($rs) > 0) {
232
            $row = Database::fetch_assoc($rs);
233
            $courseCode = $row['course_code'];
234
            $sessionId = $row['session_id'];
235
            $userId = $row['user_id'];
236
            if (api_get_course_setting('customcertificate_course_enable', api_get_course_info($courseCode)) == 1) {
237
                return [
238
                    'course_code' => $courseCode,
239
                    'session_id' => $sessionId,
240
                    'user_id' => $userId,
241
                ];
242
            }
243
        }
244
245
        return [];
246
    }
247
248
    /**
249
     * Check if it redirects.
250
     *
251
     * @param certificate $certificate
252
     * @param int         $certId
253
     * @param int         $userId
254
     */
255
    public static function redirectCheck($certificate, $certId, $userId)
256
    {
257
        $certId = (int) $certId;
258
        $userId = !empty($userId) ? $userId : api_get_user_id();
259
260
        if (api_get_plugin_setting('customcertificate', 'enable_plugin_customcertificate') === 'true') {
261
            $infoCertificate = self::getCertificateData($certId, $userId);
262
            if (!empty($infoCertificate)) {
263
                if ($certificate->user_id == api_get_user_id() && !empty($certificate->certificate_data)) {
264
                    $certificateId = $certificate->certificate_data['id'];
265
                    $extraFieldValue = new ExtraFieldValue('user_certificate');
266
                    $value = $extraFieldValue->get_values_by_handler_and_field_variable(
267
                        $certificateId,
268
                        'downloaded_at'
269
                    );
270
                    if (empty($value)) {
271
                        $params = [
272
                            'item_id' => $certificate->certificate_data['id'],
273
                            'extra_downloaded_at' => api_get_utc_datetime(),
274
                        ];
275
                        $extraFieldValue->saveFieldValues($params);
276
                    }
277
                }
278
279
                $url = api_get_path(WEB_PLUGIN_PATH).'customcertificate/src/print_certificate.php'.
280
                    '?student_id='.$infoCertificate['user_id'].
281
                    '&course_code='.$infoCertificate['course_code'].
282
                    '&session_id='.$infoCertificate['session_id'];
283
                header('Location: '.$url);
284
                exit;
285
            }
286
        }
287
    }
288
289
    /**
290
     * Get certificate info.
291
     *
292
     * @param int $courseId
293
     * @param int $sessionId
294
     * @param int $accessUrlId
295
     *
296
     * @return array
297
     */
298
    public static function getInfoCertificate($courseId, $sessionId, $accessUrlId)
299
    {
300
        $courseId = (int) $courseId;
301
        $sessionId = (int) $sessionId;
302
        $accessUrlId = !empty($accessUrlId) ? (int) $accessUrlId : 1;
303
304
        $table = Database::get_main_table(self::TABLE_CUSTOMCERTIFICATE);
305
        $sql = "SELECT * FROM $table
306
                WHERE 
307
                    c_id = $courseId AND 
308
                    session_id = $sessionId AND
309
                    access_url_id = $accessUrlId";
310
        $result = Database::query($sql);
311
        $resultArray = [];
312
        if (Database::num_rows($result) > 0) {
313
            $resultArray = Database::fetch_array($result);
314
        }
315
316
        return $resultArray;
317
    }
318
319
    /**
320
     * Get default certificate info.
321
     *
322
     * @param int $accessUrlId
323
     *
324
     * @return array
325
     */
326
    public static function getInfoCertificateDefault($accessUrlId)
327
    {
328
        $accessUrlId = !empty($accessUrlId) ? (int) $accessUrlId : 1;
329
330
        $table = Database::get_main_table(self::TABLE_CUSTOMCERTIFICATE);
331
        $sql = "SELECT * FROM $table
332
                WHERE certificate_default = 1 AND access_url_id = $accessUrlId";
333
        $result = Database::query($sql);
334
        $resultArray = [];
335
        if (Database::num_rows($result) > 0) {
336
            $resultArray = Database::fetch_array($result);
337
        }
338
339
        return $resultArray;
340
    }
341
}
342