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