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

main/lp/aicc.class.php (6 issues)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Class aicc
6
 * Defines the AICC class, which is meant to contain the aicc items (nuclear elements).
7
 *
8
 * @author Yannick Warnier <[email protected]>
9
 * @license GNU/GPL
10
 */
11
class aicc extends learnpath
12
{
13
    public $config = [];
14
    // The configuration files might be multiple and might have
15
    // funny names. We need to keep the name of that file while we
16
    // install the content.
17
    public $config_basename = '';
18
    public $config_files = [];
19
    public $config_exts = [
20
        'crs' => 0, // Course description file (mandatory)
21
        'au' => 0, // Assignable Unit file (mandatory)
22
        'des' => 0, // Descriptor file (mandatory)
23
        'cst' => 0, // Course structure file (mandatory)
24
        'ore' => 0, // Objectives relationshops file (optional)
25
        'pre' => 0, // Prerequisites file (optional)
26
        'cmp' => 0,  // Completion Requirements file (optional)
27
    ];
28
    public $aulist = [];
29
    public $au_order_list = [];
30
    public $au_order_list_new_id = [];
31
    public $deslist = [];
32
    public $cstlist = [];
33
    public $orelist = [];
34
35
    // Path between the scorm/ directory and the config files
36
    // e.g. maritime_nav/maritime_nav.
37
    // This is the path that will be used in the lp_path when importing a package.
38
    public $subdir = '';
39
    // Keeps the zipfile safe for the object's life
40
    // so that we can use it if there is no title available.
41
    public $zipname = '';
42
    // Keeps an index of the number of uses of the zipname so far.
43
    public $lastzipnameindex = 0;
44
    public $config_encoding = 'ISO-8859-1';
45
    public $debug = 0;
46
47
    /**
48
     * Class constructor. Based on the parent constructor.
49
     *
50
     * @param string $course_code
51
     * @param int    $resource_id Learnpath ID in DB
52
     * @param int    $user_id
53
     */
54
    public function __construct($course_code = null, $resource_id = null, $user_id = null)
55
    {
56
        if ($this->debug > 0) {
57
            error_log('In aicc::aicc()');
58
        }
59
        if (!empty($course_code) && !empty($resource_id) && !empty($user_id)) {
60
            parent::__construct($course_code, $resource_id, $user_id);
61
        }
62
    }
63
64
    /**
65
     * Opens a resource.
66
     *
67
     * @param int Database ID of the resource
68
     */
69
    public function open($id)
70
    {
71
    }
72
73
    /**
74
     * Parses a set of AICC config files and puts everything into the $config array.
75
     *
76
     * @param string Path to the config files dir on the system.
77
     * If not defined, uses the base path of the course's scorm dir
78
     *
79
     * @return array Structured array representing the config files' contents
80
     */
81
    public function parse_config_files($dir = '')
82
    {
83
        if ($this->debug > 0) {
84
            error_log('New LP - In aicc::parse_config_files('.$dir.')', 0);
85
        }
86
        if (empty($dir)) {
87
            // Get the path of the AICC config files dir.
88
            $dir = $this->subdir;
89
        }
90
        if (is_dir($dir) && is_readable($dir)) {
91
            // Now go through all the config files one by one and parse everything into AICC objects.
92
            // The basename for the config files is stored in $this->config_basename.
93
            // Parse the Course Description File (.crs) - ini-type.
94
            $crs_file = $dir.'/'.$this->config_files['crs'];
95
            $crs_params = $this->parse_ini_file_quotes_safe($crs_file);
96
            if ($this->debug > 1) {
97
                error_log('New LP - In aicc::parse_config_files() - '.$crs_file.' has been parsed');
98
            }
99
100
            // CRS distribute crs params into the aicc object.
101
            if (!empty($crs_params['course']['course_creator'])) {
102
                $this->course_creator = Database::escape_string($crs_params['course']['course_creator']);
103
            }
104
            if (!empty($crs_params['course']['course_id'])) {
105
                $this->course_id = Database::escape_string($crs_params['course']['course_id']);
106
            }
107
            if (!empty($crs_params['course']['course_system'])) {
108
                $this->course_system = $crs_params['course']['course_system'];
109
            }
110
            if (!empty($crs_params['course']['course_title'])) {
111
                $this->course_title = Database::escape_string($crs_params['course']['course_title']);
112
            }
113
            if (!empty($crs_params['course']['course_level'])) {
114
                $this->course_level = $crs_params['course']['course_level'];
115
            }
116
            if (!empty($crs_params['course']['max_fields_cst'])) {
117
                $this->course_max_fields_cst = $crs_params['course']['max_fields_cst'];
118
            }
119
            if (!empty($crs_params['course']['max_fields_ort'])) {
120
                $this->course_max_fields_ort = $crs_params['course']['max_fields_ort'];
121
            }
122
            if (!empty($crs_params['course']['total_aus'])) {
123
                $this->course_total_aus = $crs_params['course']['total_aus'];
124
            }
125
            if (!empty($crs_params['course']['total_blocks'])) {
126
                $this->course_total_blocks = $crs_params['course']['total_blocks'];
127
            }
128
            if (!empty($crs_params['course']['total_objectives'])) {
129
                $this->course_total_objectives = $crs_params['course']['total_objectives'];
130
            }
131
            if (!empty($crs_params['course']['total_complex_objectives'])) {
132
                $this->course_total_complex_objectives = $crs_params['course']['total_complex_objectives'];
133
            }
134
            if (!empty($crs_params['course']['version'])) {
135
                $this->course_version = $crs_params['course']['version'];
136
            }
137
            if (!empty($crs_params['course_description'])) {
138
                $this->course_description = Database::escape_string($crs_params['course_description']);
139
            }
140
141
            // Parse the Descriptor File (.des) - csv-type.
142
            $des_file = $dir.'/'.$this->config_files['des'];
143
            $des_params = $this->parse_csv_file($des_file);
144
            if ($this->debug > 1) {
145
                error_log('New LP - In aicc::parse_config_files() - '.$des_file.' has been parsed', 0);
146
            }
147
            // Distribute des params into the aicc object.
148
            foreach ($des_params as $des) {
149
                // One AU in AICC is equivalent to one SCO in SCORM (scormItem class).
150
                $oDes = new aiccResource('config', $des);
151
                $this->deslist[$oDes->identifier] = $oDes;
152
            }
153
154
            // Parse the Assignable Unit File (.au) - csv-type.
155
            $au_file = $dir.'/'.$this->config_files['au'];
156
            $au_params = $this->parse_csv_file($au_file);
157
            if ($this->debug > 1) {
158
                error_log('New LP - In aicc::parse_config_files() - '.$au_file.' has been parsed', 0);
159
            }
160
            // Distribute au params into the aicc object.
161
            foreach ($au_params as $au) {
162
                $oAu = new aiccItem('config', $au);
163
                $this->aulist[$oAu->identifier] = $oAu;
164
                $this->au_order_list[] = $oAu->identifier;
165
            }
166
167
            // Parse the Course Structure File (.cst) - csv-type.
168
            $cst_file = $dir.'/'.$this->config_files['cst'];
169
            $cst_params = $this->parse_csv_file($cst_file, ',', '"', true);
170
            if ($this->debug > 1) {
171
                error_log('New LP - In aicc::parse_config_files() - '.$cst_file.' has been parsed', 0);
172
            }
173
            // Distribute cst params into the aicc object.
174
            foreach ($cst_params as $cst) {
175
                $oCst = new aiccBlock('config', $cst);
176
                $this->cstlist[$oCst->identifier] = $oCst;
177
            }
178
179
            // Parse the Objectives Relationships File (.ore) - csv-type - if exists.
180
            // TODO: Implement these objectives. For now they're just parsed.
181
            if (!empty($this->config_files['ore'])) {
182
                $ore_file = $dir.'/'.$this->config_files['ore'];
183
                $ore_params = $this->parse_csv_file($ore_file, ',', '"', true);
184
                if ($this->debug > 1) {
185
                    error_log('New LP - In aicc::parse_config_files() - '.$ore_file.' has been parsed', 0);
186
                }
187
                // Distribute ore params into the aicc object.
188
                foreach ($ore_params as $ore) {
189
                    $oOre = new aiccObjective('config', $ore);
190
                    $this->orelist[$oOre->identifier] = $oOre;
191
                }
192
            }
193
194
            // Parse the Prerequisites File (.pre) - csv-type - if exists.
195
            if (!empty($this->config_files['pre'])) {
196
                $pre_file = $dir.'/'.$this->config_files['pre'];
197
                $pre_params = $this->parse_csv_file($pre_file);
198
                if ($this->debug > 1) {
199
                    error_log('New LP - In aicc::parse_config_files() - '.$pre_file.' has been parsed', 0);
200
                }
201
                // Distribute pre params into the aicc object.
202
                foreach ($pre_params as $pre) {
203
                    // Place a constraint on the corresponding block or AU.
204
                    if (in_array(api_strtolower($pre['structure_element']), array_keys($this->cstlist))) {
205
                        // If this references a block element:
206
                        $this->cstlist[api_strtolower($pre['structure_element'])]->prereq_string = api_strtolower($pre['prerequisite']);
207
                    }
208
                    if (in_array(api_strtolower($pre['structure_element']), array_keys($this->aulist))) {
209
                        // If this references a block element:
210
                        $this->aulist[api_strtolower($pre['structure_element'])]->prereq_string = api_strtolower($pre['prerequisite']);
211
                    }
212
                }
213
            }
214
215
            // Parse the Completion Requirements File (.cmp) - csv-type - if exists.
216
            // TODO: Implement this set of requirements (needs database changes).
217
            if (!empty($this->config_files['cmp'])) {
218
                $cmp_file = $dir.'/'.$this->config_files['cmp'];
219
                $cmp_params = $this->parse_csv_file($cmp_file);
220
                if ($this->debug > 1) {
221
                    error_log('New LP - In aicc::parse_config_files() - '.$cmp_file.' has been parsed', 0);
222
                }
223
                // Distribute cmp params into the aicc object.
224
                foreach ($cmp_params as $cmp) {
225
                    //$oCmp = new aiccCompletionRequirements('config', $cmp);
226
                    //$this->cmplist[$oCmp->identifier] =& $oCmp;
227
                }
228
            }
229
        }
230
231
        return $this->config;
232
    }
233
234
    /**
235
     * Import the aicc object (as a result from the parse_config_files function) into the database structure.
236
     *
237
     * @param string $course_code
238
     *
239
     * @return bool Returns -1 on error
240
     */
241
    public function import_aicc($course_code)
242
    {
243
        $courseInfo = api_get_course_info($course_code);
244
        $course_id = $courseInfo['real_id'];
245
246
        if (empty($course_id)) {
247
            return false;
248
        }
249
250
        if ($this->debug > 0) {
251
            error_log('New LP - In aicc::import_aicc('.$course_code.')', 0);
252
        }
253
254
        $new_lp = Database::get_course_table(TABLE_LP_MAIN);
255
        $new_lp_item = Database::get_course_table(TABLE_LP_ITEM);
256
        $get_max = "SELECT MAX(display_order) FROM $new_lp WHERE c_id = $course_id";
257
        $res_max = Database::query($get_max);
258
        if (Database::num_rows($res_max) < 1) {
259
            $dsp = 1;
260
        } else {
261
            $row = Database::fetch_array($res_max);
262
            $dsp = $row[0] + 1;
263
        }
264
265
        $this->config_encoding = "ISO-8859-1"; // TODO: We may apply detection for this value, see the function api_detect_encoding().
266
267
        $sql = "INSERT INTO $new_lp (c_id, lp_type, name, ref, description, path, force_commit, default_view_mod, default_encoding, js_lib, content_maker,display_order)".
268
                "VALUES ".
269
                "($course_id, 3, '".$this->course_title."', '".$this->course_id."','".$this->course_description."',".
270
                "'".$this->subdir."', 0, 'embedded', '".$this->config_encoding."',".
271
                "'aicc_api.php','".$this->course_creator."',$dsp)";
272
        if ($this->debug > 2) {
273
            error_log('New LP - In import_aicc(), inserting path: '.$sql, 0);
274
        }
275
        Database::query($sql);
276
        $lp_id = Database::insert_id();
277
278
        if ($lp_id) {
279
            $sql = "UPDATE $new_lp SET id = iid  WHERE iid = $lp_id";
280
            Database::query($sql);
281
282
            $this->lp_id = $lp_id;
283
284
            api_item_property_update(
285
                $courseInfo,
286
                TOOL_LEARNPATH,
287
                $this->lp_id,
288
                'LearnpathAdded',
289
                api_get_user_id()
290
            );
291
292
            api_item_property_update(
293
                $courseInfo,
294
                TOOL_LEARNPATH,
295
                $this->lp_id,
296
                'visible',
297
                api_get_user_id()
298
            );
299
        }
300
301
        $previous = 0;
302
        foreach ($this->aulist as $identifier => $dummy) {
303
            $oAu = &$this->aulist[$identifier];
304
            //echo "Item ".$oAu->identifier;
305
            $field_add = '';
306
            $value_add = '';
307
            if (!empty($oAu->masteryscore)) {
308
                $field_add = 'mastery_score, ';
309
                $value_add = $oAu->masteryscore.',';
310
            }
311
            $title = $oAu->identifier;
312
            if (is_object($this->deslist[$identifier])) {
313
                $title = $this->deslist[$identifier]->title;
314
            }
315
            $path = $oAu->path;
316
            //$max_score = $oAu->max_score // TODO: Check if special constraint exists for this item.
317
            //$min_score = $oAu->min_score // TODO: Check if special constraint exists for this item.
318
            $parent = 0; // TODO: Deal with the parent.
319
            $previous = 0;
320
            $prereq = $oAu->prereq_string;
321
            $sql_item = "INSERT INTO $new_lp_item (c_id, lp_id,item_type,ref,title, path,min_score,max_score, $field_add parent_item_id,previous_item_id,next_item_id, prerequisite,display_order,parameters) ".
322
                    "VALUES ".
323
                    "($course_id, $lp_id, 'au','".$oAu->identifier."','".$title."',".
324
                    "'$path',0,100, $value_add".
325
                    "$parent, $previous, 0, ".
326
                    "'$prereq', 0,'".(!empty($oAu->parameters) ? Database::escape_string($oAu->parameters) : '')."'".
327
                    ")";
328
            Database::query($sql_item);
329
            if ($this->debug > 1) {
330
                error_log('New LP - In aicc::import_aicc() - inserting item : '.$sql_item.' : ', 0);
331
            }
332
            $item_id = Database::insert_id();
333
334
            if ($item_id) {
335
                $sql = "UPDATE $new_lp_item SET id = iid WHERE iid = $lp_id";
336
                Database::query($sql);
337
            }
338
339
            // Now update previous item to change next_item_id.
340
            if (0 != $previous) {
341
                $upd = "UPDATE $new_lp_item SET next_item_id = $item_id WHERE c_id = $course_id AND id = $previous";
342
                Database::query($upd);
343
                // Update the previous item id.
344
            }
345
            $previous = $item_id;
346
        }
347
    }
348
349
    /**
350
     * Intermediate to import_package only to allow import from local zip files.
351
     *
352
     * @param string    Path to the zip file, from the dokeos sys root
353
     * @param string    Current path (optional)
354
     *
355
     * @return string Absolute path to the AICC description files or empty string on error
356
     */
357
    public function import_local_package($file_path, $current_dir = '')
358
    {
359
        // TODO: Prepare info as given by the $_FILES[''] vector.
360
        $file_info = [];
361
        $file_info['tmp_name'] = $file_path;
362
        $file_info['name'] = basename($file_path);
363
        // Call the normal import_package function.
364
        return $this->import_package($file_info, $current_dir);
365
    }
366
367
    /**
368
     * Imports a zip file (presumably AICC) into the Chamilo structure.
369
     *
370
     * @param string    Zip file info as given by $_FILES['userFile']
371
     *
372
     * @return string Absolute path to the AICC config files directory or empty string on error
373
     */
374
    public function import_package($zip_file_info, $current_dir = '')
375
    {
376
        if ($this->debug > 0) {
377
            error_log('In aicc::import_package('.print_r($zip_file_info, true).',"'.$current_dir.'") method', 0);
378
        }
379
        //ini_set('error_log', 'E_ALL');
380
        $maxFilledSpace = 1000000000;
381
        $zip_file_path = $zip_file_info['tmp_name'];
382
        $zip_file_name = $zip_file_info['name'];
383
384
        if ($this->debug > 0) {
385
            error_log(
386
                'New LP - aicc::import_package() - Zip file path = '.$zip_file_path.', zip file name = '.$zip_file_name,
387
                0
388
            );
389
        }
390
        $course_rel_dir = api_get_course_path().'/scorm'; // Scorm dir web path starting from /courses
391
        $course_sys_dir = api_get_path(SYS_COURSE_PATH).$course_rel_dir; // The absolute system path of this course.
392
        $current_dir = api_replace_dangerous_char(trim($current_dir)); // Current dir we are in, inside scorm/
393
        if ($this->debug > 0) {
394
            error_log('New LP - aicc::import_package() - Current_dir = '.$current_dir, 0);
395
        }
396
397
        //$uploaded_filename = $_FILES['userFile']['name'];
398
        // Get the name of the zip file without the extension.
399
        if ($this->debug > 0) {
400
            error_log('New LP - aicc::import_package() - Received zip file name: '.$zip_file_path, 0);
401
        }
402
        $file_info = pathinfo($zip_file_name);
403
        $filename = $file_info['basename'];
404
        $extension = $file_info['extension'];
405
        $file_base_name = str_replace('.'.$extension, '', $filename); // Filename without its extension.
406
        $this->zipname = $file_base_name; // Save for later in case we don't have a title.
407
408
        if ($this->debug > 0) {
409
            error_log('New LP - aicc::import_package() - Base file name is : '.$file_base_name, 0);
410
        }
411
        $new_dir = api_replace_dangerous_char(trim($file_base_name));
412
        $this->subdir = $new_dir;
413
        if ($this->debug > 0) {
414
            error_log('New LP - aicc::import_package() - Subdir is first set to : '.$this->subdir, 0);
415
        }
416
417
        /*
418
        if (check_name_exist($course_sys_dir.$current_dir.'/'.$new_dir)) {
419
            $dialogBox = get_lang('FileExists');
420
            $stopping_error = true;
421
        }
422
        */
423
        $zipFile = new PclZip($zip_file_path);
424
        // Check the zip content (real size and file extension).
425
        $zipContentArray = $zipFile->listContent();
426
427
        $package_type = ''; // The type of the package. Should be 'aicc' after the next few lines.
428
        $package = ''; // The basename of the config files (if 'courses.crs' => 'courses').
429
        $at_root = false; // Check if the config files are at zip root.
430
        $config_dir = ''; // The directory in which the config files are. May remain empty.
431
        $files_found = [];
432
        $subdir_isset = false;
433
        // The following loop should be stopped as soon as we found the right config files (.crs, .au, .des and .cst).
434
        $realFileSize = 0;
435
        foreach ($zipContentArray as $thisContent) {
436
            if (preg_match('~.(php.*|phtml)$~i', $thisContent['filename'])) {
437
                // If a php file is found, do not authorize (security risk).
438
                if ($this->debug > 1) {
439
                    error_log(
440
                        'New LP - aicc::import_package() - Found unauthorized file: '.$thisContent['filename']
441
                    );
442
                }
443
                Display::addFlash(
444
                    Display::return_message(get_lang('ZipNoPhp'))
445
                );
446
447
                return false;
448
            } elseif (preg_match('?.*/aicc/$?', $thisContent['filename'])) {
449
                // If a directory named 'aicc' is found, package type = aicc, but continue,
450
                // because we need to find the right AICC files;
451
                if ($this->debug > 1) {
452
                    error_log('New LP - aicc::import_package() - Found aicc directory: '.$thisContent['filename']);
453
                }
454
                $package_type = 'aicc';
455
            } else {
456
                // else, look for one of the files we're searching for (something.crs case insensitive).
457
                $res = [];
458
                if (preg_match('?^(.*)\.(crs|au|des|cst|ore|pre|cmp)$?i', $thisContent['filename'], $res)) {
459
                    if ($this->debug > 1) {
460
                        error_log(
461
                            'New LP - aicc::import_package() - Found AICC config file: '.$thisContent['filename'].'. Now splitting: '.$res[1].' and '.$res[2]
462
                        );
463
                    }
464
                    if ($thisContent['filename'] == basename($thisContent['filename'])) {
465
                        if ($this->debug > 2) {
466
                            error_log('New LP - aicc::import_package() - '.$thisContent['filename'].' is at root level', 0);
467
                        }
468
                        $at_root = true;
469
                        if (!is_array($files_found[$res[1]])) {
470
                            $files_found[$res[1]] = $this->config_exts; // Initialise list of expected extensions (defined in class definition).
471
                        }
472
                        $files_found[$res[1]][api_strtolower($res[2])] = $thisContent['filename'];
473
                        $subdir_isset = true;
474
                    } else {
475
                        if (!$subdir_isset) {
476
                            if (preg_match('?^.*/aicc$?i', dirname($thisContent['filename']))) {
477
                                //echo "Cutting subdir<br/>";
478
                                $this->subdir .= '/'.substr(dirname($thisContent['filename']), 0, -5);
479
                            } else {
480
                                //echo "Not cutting subdir<br/>";
481
                                $this->subdir .= '/'.dirname($thisContent['filename']);
482
                            }
483
                            $subdir_isset = true;
484
                        }
485
                        if ($this->debug > 2) {
486
                            error_log('New LP - aicc::import_package() - '.$thisContent['filename'].' is not at root level - recording subdir '.$this->subdir, 0);
487
                        }
488
                        $config_dir = dirname($thisContent['filename']); // Just the relative directory inside scorm/
489
                        if (!is_array($files_found[basename($res[1])])) {
490
                            $files_found[basename($res[1])] = $this->config_exts;
491
                        }
492
                        $files_found[basename($res[1])][api_strtolower($res[2])] = basename($thisContent['filename']);
493
                    }
494
                    $package_type = 'aicc';
495
                } else {
496
                    if ($this->debug > 3) {
497
                        error_log('New LP - aicc::import_package() - File '.$thisContent['filename'].' didnt match any check', 0);
498
                    }
499
                }
500
            }
501
            $realFileSize += $thisContent['size'];
502
        }
503
        if ($this->debug > 2) {
504
            error_log('New LP - aicc::import_package() - $files_found: '.print_r($files_found, true), 0);
505
        }
506
        if ($this->debug > 1) {
507
            error_log('New LP - aicc::import_package() - Package type is now '.$package_type, 0);
508
        }
509
        $mandatory = false;
510
        foreach ($files_found as $file_name => $file_exts) {
511
            $temp = (
512
                !empty($files_found[$file_name]['crs'])
513
                && !empty($files_found[$file_name]['au'])
514
                && !empty($files_found[$file_name]['des'])
515
                && !empty($files_found[$file_name]['cst'])
516
            );
517
            if ($temp) {
518
                if ($this->debug > 1) {
519
                    error_log('New LP - aicc::import_package() - Found all config files for '.$file_name, 0);
520
                }
521
                $mandatory = true;
522
                $package = $file_name;
523
                // Store base config file name for reuse in parse_config_files().
524
                $this->config_basename = $file_name;
525
                // Store filenames for reuse in parse_config_files().
526
                $this->config_files = $files_found[$file_name];
527
                // Get out, we only want one config files set.
528
                break;
529
            }
530
        }
531
532
        if ($package_type == '' || !$mandatory) {
533
            Display::addFlash(
534
                Display::return_message(get_lang('FileError'))
535
            );
536
537
            return false;
538
        }
539
540
        if (!enough_size($realFileSize, $course_sys_dir, $maxFilledSpace)) {
541
            Display::addFlash(
542
                Display::return_message(get_lang('NoSpace'))
543
            );
544
545
            return false;
546
        }
547
548
        // It happens on Linux that $new_dir sometimes doesn't start with '/'
549
        if ($new_dir[0] != '/') {
550
            $new_dir = '/'.$new_dir;
551
        }
552
        // Cut trailing slash.
553
        if ($new_dir[strlen($new_dir) - 1] == '/') {
554
            $new_dir = substr($new_dir, 0, -1);
555
        }
556
557
        /* Uncompressing phase */
558
559
        /*
560
            We need to process each individual file in the zip archive to
561
            - add it to the database
562
            - parse & change relative html links
563
            - make sure the filenames are secure (filter funny characters or php extensions)
564
        */
565
        if (is_dir($course_sys_dir.$new_dir) ||
566
            @mkdir($course_sys_dir.$new_dir, api_get_permissions_for_new_directories())
567
        ) {
568
            // PHP method - slower...
569
            if ($this->debug >= 1) {
570
                error_log('New LP - Changing dir to '.$course_sys_dir.$new_dir, 0);
571
            }
572
            chdir($course_sys_dir.$new_dir);
573
            $zipFile->extract(
574
                PCLZIP_CB_PRE_EXTRACT,
575
                'clean_up_files_in_zip'
576
            );
577
        } else {
578
            return '';
579
        }
580
581
        return $course_sys_dir.$new_dir.$config_dir;
582
    }
583
584
    /**
585
     * Sets the proximity setting in the database.
586
     *
587
     * @param string $proxy Proximity setting
588
     *
589
     * @return bool
590
     */
591
    public function set_proximity($proxy = '')
592
    {
593
        $course_id = api_get_course_int_id();
594
        if ($this->debug > 0) {
595
            error_log('In aicc::set_proximity('.$proxy.') method', 0);
596
        }
597
        $lp = $this->get_id();
598
        if ($lp != 0) {
599
            $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
600
            $sql = "UPDATE $tbl_lp SET content_local = '$proxy' WHERE c_id = ".$course_id." id = ".$lp;
601
            Database::query($sql);
602
603
            return true;
604
        } else {
605
            return false;
606
        }
607
    }
608
609
    /**
610
     * Sets the theme setting in the database.
611
     *
612
     * @param    string    Theme setting
613
     *
614
     * @return bool
615
     */
616
    public function set_theme($theme = '')
617
    {
618
        $course_id = api_get_course_int_id();
619
        if ($this->debug > 0) {
620
            error_log('In aicc::set_theme('.$theme.') method', 0);
621
        }
622
        $lp = $this->get_id();
623
        if ($lp != 0) {
624
            $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
625
            $sql = "UPDATE $tbl_lp SET theme = '$theme' WHERE c_id = ".$course_id." id = ".$lp;
626
            $res = Database::query($sql);
627
628
            return true;
629
        } else {
630
            return false;
631
        }
632
    }
633
634
    /**
635
     * Sets the image LP in the database.
636
     *
637
     * @param string $preview_image Theme setting
638
     *
639
     * @return bool
640
     */
641
    public function set_preview_image($preview_image = '')
642
    {
643
        $course_id = api_get_course_int_id();
644
        if ($this->debug > 0) {
645
            error_log('In aicc::set_preview_image('.$preview_image.') method', 0);
646
        }
647
        $lp = $this->get_id();
648
        if ($lp != 0) {
649
            $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
650
            $sql = "UPDATE $tbl_lp SET preview_image = '$preview_image' 
651
                    WHERE c_id = ".$course_id." id = ".$lp;
652
            Database::query($sql);
653
654
            return true;
655
        } else {
656
            return false;
657
        }
658
    }
659
660
    /**
661
     * Sets the Author LP in the database.
662
     *
663
     * @param string $author
664
     *
665
     * @return true
666
     */
667
    public function set_author($author = '')
668
    {
669
        $course_id = api_get_course_int_id();
670
        $lp = $this->get_id();
671
        if ($lp != 0) {
672
            $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
673
            $sql = "UPDATE $tbl_lp SET author = '$author' 
674
                    WHERE c_id = ".$course_id." id = ".$lp;
675
            Database::query($sql);
676
677
            return true;
678
        }
679
680
        return false;
681
    }
682
683
    /**
684
     * Sets the content maker setting in the database.
685
     *
686
     * @param string $maker
687
     *
688
     * @return bool
689
     */
690
    public function set_maker($maker = '')
691
    {
692
        $course_id = api_get_course_int_id();
693
        if ($this->debug > 0) {
694
            error_log('In aicc::set_maker method('.$maker.')', 0);
695
        }
696
        $lp = $this->get_id();
697
        if ($lp != 0) {
698
            $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
699
            $sql = "UPDATE $tbl_lp SET content_maker = '$maker' 
700
                    WHERE c_id = ".$course_id." id = ".$lp;
701
            Database::query($sql);
702
703
            return true;
704
        } else {
705
            return false;
706
        }
707
    }
708
709
    /**
710
     * Exports the current AICC object's files as a zip. Excerpts taken from learnpath_functions.inc.php::exportpath().
711
     *
712
     * @param    int    Learnpath ID (optional, taken from object context if not defined)
713
     *
714
     * @return bool
715
     */
716
    public function export_zip($lp_id = null)
717
    {
718
        if ($this->debug > 0) {
719
            error_log('In aicc::export_zip method('.$lp_id.')', 0);
720
        }
721
        if (empty($lp_id)) {
722
            if (!is_object($this)) {
723
                return false;
724
            } else {
725
                $id = $this->get_id();
726
                if (empty($id)) {
727
                    return false;
728
                } else {
729
                    $lp_id = $this->get_id();
730
                }
731
            }
732
        }
733
        // Zip everything that is in the corresponding scorm dir.
734
        // Write the zip file somewhere (might be too big to return).
735
        $course_id = api_get_course_int_id();
736
        $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
737
        $_course = api_get_course_info(api_get_course_id());
738
739
        $sql = "SELECT * FROM $tbl_lp WHERE iid= $lp_id";
740
        $result = Database::query($sql);
741
        $row = Database::fetch_array($result);
742
        $LPname = $row['path'];
743
        $list = explode('/', $LPname);
744
        $LPnamesafe = $list[0];
745
        //$zipfoldername = '/tmp';
746
        //$zipfoldername = '../../courses/'.$_course['directory'].'/temp/'.$LPnamesafe;
747
        $zipfoldername = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/temp/'.$LPnamesafe;
748
        $scormfoldername = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/scorm/'.$LPnamesafe;
749
        $zipfilename = $zipfoldername.'/'.$LPnamesafe.'.zip';
750
751
        // Get a temporary dir for creating the zip file.
752
        removeDir($zipfoldername); //make sure the temp dir is cleared
753
        mkdir($zipfoldername, api_get_permissions_for_new_directories());
754
755
        // Create zipfile of given directory.
756
        $zip_folder = new PclZip($zipfilename);
757
        $zip_folder->create($scormfoldername.'/', PCLZIP_OPT_REMOVE_PATH, $scormfoldername.'/');
0 ignored issues
show
The constant PCLZIP_OPT_REMOVE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
758
759
        //this file sending implies removing the default mime-type from php.ini
760
        DocumentManager::file_send_for_download($zipfilename, true);
761
762
        // Delete the temporary zip file and directory in fileManage.lib.php
763
        my_delete($zipfilename);
764
        my_delete($zipfoldername);
765
766
        return true;
767
    }
768
769
    /**
770
     * Gets a resource's path if available, otherwise return empty string.
771
     *
772
     * @param	string	Resource ID as used in resource array
773
     *
774
     * @return string The resource's path as declared in config file course.crs
775
     */
776
    public function get_res_path($id)
777
    {
778
        if ($this->debug > 0) {
779
            error_log('In aicc::get_res_path('.$id.') method', 0);
780
        }
781
        $path = '';
782
        if (isset($this->resources[$id])) {
0 ignored issues
show
Bug Best Practice introduced by
The property resources does not exist on aicc. Did you maybe forget to declare it?
Loading history...
783
            $oRes = &$this->resources[$id];
784
            $path = @$oRes->get_path();
785
        }
786
787
        return $path;
788
    }
789
790
    /**
791
     * Gets a resource's type if available, otherwise return empty string.
792
     *
793
     * @param	string	Resource ID as used in resource array
794
     *
795
     * @return string The resource's type as declared in the assignable unit (.au) file
796
     */
797
    public function get_res_type($id)
798
    {
799
        if ($this->debug > 0) {
800
            error_log('In aicc::get_res_type('.$id.') method', 0);
801
        }
802
        $type = '';
803
        if (isset($this->resources[$id])) {
0 ignored issues
show
Bug Best Practice introduced by
The property resources does not exist on aicc. Did you maybe forget to declare it?
Loading history...
804
            $oRes = &$this->resources[$id];
805
            $temptype = $oRes->get_scorm_type();
806
            if (!empty($temptype)) {
807
                $type = $temptype;
808
            }
809
        }
810
811
        return $type;
812
    }
813
814
    /**
815
     * Gets the default organisation's title.
816
     *
817
     * @return string The organization's title
818
     */
819
    public function get_title()
820
    {
821
        if ($this->debug > 0) {
822
            error_log('In aicc::get_title() method', 0);
823
        }
824
        $title = '';
825
        if (isset($this->config['organizations']['default'])) {
826
            $title = $this->organizations[$this->config['organizations']['default']]->get_name();
0 ignored issues
show
Bug Best Practice introduced by
The property organizations does not exist on aicc. Did you maybe forget to declare it?
Loading history...
827
        } elseif (count($this->organizations) == 1) {
828
            // This will only get one title but so we don't need to know the index.
829
            foreach ($this->organizations as $id => $value) {
830
                $title = $this->organizations[$id]->get_name();
831
                break;
832
            }
833
        }
834
835
        return $title;
836
    }
837
838
    /**
839
     * TODO: Implement this function to restore items data from a set of AICC config files,
840
     * updating the existing table... This will prove very useful in case initial data
841
     * from config files were not imported well enough.
842
     */
843
    public function reimport_aicc()
844
    {
845
        if ($this->debug > 0) {
846
            error_log('In aicc::reimport_aicc() method', 0);
847
        }
848
        //query current items list
849
        //get the identifiers
850
        //parse the config files
851
        //match both
852
        //update DB accordingly
853
        return true;
854
    }
855
856
    /**
857
     * Static function to parse AICC ini files.
858
     * Based on work by sinedeo at gmail dot com published on php.net (parse_ini_file()).
859
     *
860
     * @param	string	File path
861
     *
862
     * @return array Structured array
863
     */
864
    public function parse_ini_file_quotes_safe($f)
865
    {
866
        $null = '';
867
        $r = $null;
868
        $sec = $null;
869
        $f = @file_get_contents($f);
870
        $f = api_convert_encoding($f, api_get_system_encoding(), $this->config_encoding);
871
        $f = preg_split('/\r?\n/', $f);
872
        for ($i = 0; $i < @count($f); $i++) {
873
            $newsec = 0;
874
            $w = @trim($f[$i]);
875
            if (substr($w, 0, 1) == ';') {
876
                // Ignore comment lines
877
                continue;
878
            }
879
            if ($w) {
880
                if ((!$r) or ($sec)) {
881
                    if ((@substr($w, 0, 1) == '[') and (@substr($w, -1, 1)) == ']') {
882
                        $sec = @substr($w, 1, @strlen($w) - 2);
883
                        $newsec = 1;
884
                    }
885
                }
886
                if (!$newsec) {
887
                    $w = @explode('=', $w);
888
                    $k = @trim($w[0]);
889
                    unset($w[0]);
890
                    $v = @trim(@implode('=', $w));
891
                    if ((@substr($v, 0, 1) == "\"") and (@substr($v, -1, 1) == "\"")) {
892
                        $v = @substr($v, 1, @strlen($v) - 2);
893
                    }
894
                    if ($sec) {
895
                        if (api_strtolower($sec) == 'course_description') { // A special case.
896
                            $r[api_strtolower($sec)] = $k;
897
                        } else {
898
                            $r[api_strtolower($sec)][api_strtolower($k)] = $v;
899
                        }
900
                    } else {
901
                        $r[api_strtolower($k)] = $v;
902
                    }
903
                }
904
            }
905
        }
906
907
        return $r;
908
    }
909
910
    /**
911
     * Static function to parse AICC ini strings.
912
     * Based on work by sinedeo at gmail dot com published on php.net (parse_ini_file()).
913
     *
914
     * @param		string	INI File string
915
     * @param		array	List of names of sections that should be considered
916
     * as containing only hard string data (no variables), provided in lower case
917
     *
918
     * @return array Structured array
919
     */
920
    public function parse_ini_string_quotes_safe($s, $pure_strings = [])
921
    {
922
        $null = '';
923
        $r = $null;
924
        $sec = $null;
925
        $s = api_convert_encoding($s, api_get_system_encoding(), $this->config_encoding);
926
        //$f = split("\r\n", $s);
927
        $f = preg_split('/\r?\n/', $s);
928
        for ($i = 0; $i < @count($f); $i++) {
929
            $newsec = 0;
930
            $w = @trim($f[$i]);
931
            if (substr($w, 0, 1) == ';') {
932
                // Ignore comment lines
933
                continue;
934
            }
935
            if ($w) {
936
                if ((!$r) or ($sec)) {
937
                    if ((@substr($w, 0, 1) == '[') and (@substr($w, -1, 1)) == ']') {
938
                        $sec = @substr($w, 1, @strlen($w) - 2);
939
                        $pure_data = 0;
940
                        if (in_array(api_strtolower($sec), $pure_strings)) {
941
                            // This section can only be considered as pure string data (until the next section).
942
                            $pure_data = 1;
943
                            $r[api_strtolower($sec)] = '';
944
                        }
945
                        $newsec = 1;
946
                    }
947
                }
948
                if (!$newsec) {
949
                    $w = @explode('=', $w);
950
                    $k = @trim($w[0]);
951
                    unset($w[0]);
952
                    $v = @trim(@implode('=', $w));
953
                    if ((@substr($v, 0, 1) == "\"") and (@substr($v, -1, 1) == "\"")) {
954
                        $v = @substr($v, 1, @strlen($v) - 2);
955
                    }
956
                    if ($sec) {
957
                        if ($pure_data) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pure_data does not seem to be defined for all execution paths leading up to this point.
Loading history...
958
                            $r[api_strtolower($sec)] .= $f[$i];
959
                        } else {
960
                            if (api_strtolower($sec) == 'course_description') { // A special case.
961
                                $r[api_strtolower($sec)] = $k;
962
                            } else {
963
                                $r[api_strtolower($sec)][api_strtolower($k)] = $v;
964
                            }
965
                        }
966
                    } else {
967
                        $r[api_strtolower($k)] = $v;
968
                    }
969
                }
970
            }
971
        }
972
973
        return $r;
974
    }
975
976
    /**
977
     * Static function that parses CSV files into simple arrays, based on a function
978
     * by spam at cyber-space dot nl published on php.net (fgetcsv()).
979
     *
980
     * @param	string	Filepath
981
     * @param	string	CSV delimiter
982
     * @param	string	CSV enclosure
983
     * @param	bool	Might one field name happen more than once on the same line? (then split by comma in the values)
984
     *
985
     * @return array Simple structured array
986
     */
987
    public function parse_csv_file($f, $delim = ',', $enclosure = '"', $multiples = false)
988
    {
989
        $data = @file_get_contents($f);
990
        $data = api_convert_encoding($data, api_get_system_encoding(), $this->config_encoding);
991
        $enclosed = false;
992
        $fldcount = 0;
993
        $linecount = 0;
994
        $fldval = '';
995
        for ($i = 0; $i < strlen($data); $i++) {
996
            $chr = $data[$i];
997
            switch ($chr) {
998
                case $enclosure:
999
                    if ($enclosed && $data[$i + 1] == $enclosure) {
1000
                        $fldval .= $chr;
1001
                        $i++; // Skip the next character.
1002
                    } else {
1003
                        $enclosed = !$enclosed;
1004
                    }
1005
                    break;
1006
                case $delim:
1007
                    if (!$enclosed) {
1008
                        $ret_array[$linecount][$fldcount++] = $fldval;
1009
                        $fldval = '';
1010
                    } else {
1011
                        $fldval .= $chr;
1012
                    }
1013
                    break;
1014
                case "\r":
1015
                    if (!$enclosed && $data[$i + 1] == "\n") {
1016
                        break;
1017
                    }
1018
                    // no break
1019
                case "\n":
1020
                    if (!$enclosed) {
1021
                        $ret_array[$linecount++][$fldcount] = $fldval;
1022
                        $fldcount = 0;
1023
                        $fldval = '';
1024
                    } else {
1025
                        $fldval .= $chr;
1026
                    }
1027
                    break;
1028
                case "\\r":
1029
                    if (!$enclosed && $data[$i + 1] == "\\n") {
1030
                        break;
1031
                    }
1032
                    // no break
1033
                case "\\n":
1034
                    if (!$enclosed) {
1035
                        $ret_array[$linecount++][$fldcount] = $fldval;
1036
                        $fldcount = 0;
1037
                        $fldval = '';
1038
                    } else {
1039
                        $fldval .= $chr;
1040
                    }
1041
                    break;
1042
                default:
1043
                    $fldval .= $chr;
1044
            }
1045
        }
1046
        if ($fldval) {
1047
            $ret_array[$linecount][$fldcount] = $fldval;
1048
        }
1049
        // Transform the array to use the first line as titles.
1050
        $titles = [];
1051
        $ret_ret_array = [];
1052
        foreach ($ret_array as $line_idx => $line) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ret_array does not seem to be defined for all execution paths leading up to this point.
Loading history...
1053
            if ($line_idx == 0) {
1054
                $titles = $line;
1055
            } else {
1056
                $ret_ret_array[$line_idx] = [];
1057
                foreach ($line as $idx => $val) {
1058
                    if ($multiples && !empty($ret_ret_array[$line_idx][api_strtolower($titles[$idx])])) {
1059
                        $ret_ret_array[$line_idx][api_strtolower($titles[$idx])] .= ','.$val;
1060
                    } else {
1061
                        $ret_ret_array[$line_idx][api_strtolower($titles[$idx])] = $val;
1062
                    }
1063
                }
1064
            }
1065
        }
1066
1067
        return $ret_ret_array;
1068
    }
1069
}
1070