| Total Complexity | 161 |
| Total Lines | 915 |
| 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) |
||
| 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 = '') |
||
| 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 = '') |
||
| 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 = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/temp/'.$LPnamesafe; |
||
| 610 | $scormfoldername = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/scorm/'.$LPnamesafe; |
||
| 611 | $zipfilename = $zipfoldername.'/'.$LPnamesafe.'.zip'; |
||
| 612 | |||
| 613 | // Get a temporary dir for creating the zip file. |
||
| 614 | removeDir($zipfoldername); //make sure the temp dir is cleared |
||
| 615 | mkdir($zipfoldername, api_get_permissions_for_new_directories()); |
||
| 616 | |||
| 617 | // @todo use ZipFile |
||
| 618 | // Create zipfile of given directory. |
||
| 619 | $zip_folder = new PclZip($zipfilename); |
||
| 620 | $zip_folder->create($scormfoldername.'/', PCLZIP_OPT_REMOVE_PATH, $scormfoldername.'/'); |
||
| 621 | |||
| 622 | //this file sending implies removing the default mime-type from php.ini |
||
| 623 | DocumentManager::file_send_for_download($zipfilename, true); |
||
| 624 | |||
| 625 | return true; |
||
| 626 | } |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Gets a resource's path if available, otherwise return empty string. |
||
| 630 | * |
||
| 631 | * @param string Resource ID as used in resource array |
||
| 632 | * |
||
| 633 | * @return string The resource's path as declared in config file course.crs |
||
| 634 | */ |
||
| 635 | public function get_res_path($id) |
||
| 636 | { |
||
| 637 | if ($this->debug > 0) { |
||
| 638 | error_log('In aicc::get_res_path('.$id.') method', 0); |
||
| 639 | } |
||
| 640 | $path = ''; |
||
| 641 | if (isset($this->resources[$id])) { |
||
| 642 | $oRes = &$this->resources[$id]; |
||
| 643 | $path = @$oRes->get_path(); |
||
| 644 | } |
||
| 645 | |||
| 646 | return $path; |
||
| 647 | } |
||
| 648 | |||
| 649 | /** |
||
| 650 | * Gets a resource's type if available, otherwise return empty string. |
||
| 651 | * |
||
| 652 | * @param string Resource ID as used in resource array |
||
| 653 | * |
||
| 654 | * @return string The resource's type as declared in the assignable unit (.au) file |
||
| 655 | */ |
||
| 656 | public function get_res_type($id) |
||
| 657 | { |
||
| 658 | if ($this->debug > 0) { |
||
| 659 | error_log('In aicc::get_res_type('.$id.') method', 0); |
||
| 660 | } |
||
| 661 | $type = ''; |
||
| 662 | if (isset($this->resources[$id])) { |
||
| 663 | $oRes = &$this->resources[$id]; |
||
| 664 | $temptype = $oRes->get_scorm_type(); |
||
| 665 | if (!empty($temptype)) { |
||
| 666 | $type = $temptype; |
||
| 667 | } |
||
| 668 | } |
||
| 669 | |||
| 670 | return $type; |
||
| 671 | } |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Gets the default organisation's title. |
||
| 675 | * |
||
| 676 | * @return string The organization's title |
||
| 677 | */ |
||
| 678 | public function get_title() |
||
| 679 | { |
||
| 680 | if ($this->debug > 0) { |
||
| 681 | error_log('In aicc::get_title() method', 0); |
||
| 682 | } |
||
| 683 | $title = ''; |
||
| 684 | if (isset($this->config['organizations']['default'])) { |
||
| 685 | $title = $this->organizations[$this->config['organizations']['default']]->get_name(); |
||
| 686 | } elseif (1 == count($this->organizations)) { |
||
| 687 | // This will only get one title but so we don't need to know the index. |
||
| 688 | foreach ($this->organizations as $id => $value) { |
||
| 689 | $title = $this->organizations[$id]->get_name(); |
||
| 690 | break; |
||
| 691 | } |
||
| 692 | } |
||
| 693 | |||
| 694 | return $title; |
||
| 695 | } |
||
| 696 | |||
| 697 | /** |
||
| 698 | * TODO: Implement this function to restore items data from a set of AICC config files, |
||
| 699 | * updating the existing table... This will prove very useful in case initial data |
||
| 700 | * from config files were not imported well enough. |
||
| 701 | */ |
||
| 702 | public function reimport_aicc() |
||
| 703 | { |
||
| 704 | if ($this->debug > 0) { |
||
| 705 | error_log('In aicc::reimport_aicc() method', 0); |
||
| 706 | } |
||
| 707 | //query current items list |
||
| 708 | //get the identifiers |
||
| 709 | //parse the config files |
||
| 710 | //match both |
||
| 711 | //update DB accordingly |
||
| 712 | return true; |
||
| 713 | } |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Static function to parse AICC ini files. |
||
| 717 | * Based on work by sinedeo at gmail dot com published on php.net (parse_ini_file()). |
||
| 718 | * |
||
| 719 | * @param string File path |
||
| 720 | * |
||
| 721 | * @return array Structured array |
||
| 722 | */ |
||
| 723 | public function parse_ini_file_quotes_safe($f) |
||
| 724 | { |
||
| 725 | $null = ''; |
||
| 726 | $r = $null; |
||
| 727 | $sec = $null; |
||
| 728 | $f = @file_get_contents($f); |
||
| 729 | $f = api_convert_encoding($f, api_get_system_encoding(), $this->config_encoding); |
||
| 730 | $f = preg_split('/\r?\n/', $f); |
||
| 731 | for ($i = 0; $i < @count($f); $i++) { |
||
| 732 | $newsec = 0; |
||
| 733 | $w = @trim($f[$i]); |
||
| 734 | if (';' == substr($w, 0, 1)) { |
||
| 735 | // Ignore comment lines |
||
| 736 | continue; |
||
| 737 | } |
||
| 738 | if ($w) { |
||
| 739 | if ((!$r) or ($sec)) { |
||
| 740 | if (('[' == @substr($w, 0, 1)) and ']' == (@substr($w, -1, 1))) { |
||
| 741 | $sec = @substr($w, 1, @strlen($w) - 2); |
||
| 742 | $newsec = 1; |
||
| 743 | } |
||
| 744 | } |
||
| 745 | if (!$newsec) { |
||
| 746 | $w = @explode('=', $w); |
||
| 747 | $k = @trim($w[0]); |
||
| 748 | unset($w[0]); |
||
| 749 | $v = @trim(@implode('=', $w)); |
||
| 750 | if (("\"" == @substr($v, 0, 1)) and ("\"" == @substr($v, -1, 1))) { |
||
| 751 | $v = @substr($v, 1, @strlen($v) - 2); |
||
| 752 | } |
||
| 753 | if ($sec) { |
||
| 754 | if ('course_description' == api_strtolower($sec)) { // A special case. |
||
| 755 | $r[api_strtolower($sec)] = $k; |
||
| 756 | } else { |
||
| 757 | $r[api_strtolower($sec)][api_strtolower($k)] = $v; |
||
| 758 | } |
||
| 759 | } else { |
||
| 760 | $r[api_strtolower($k)] = $v; |
||
| 761 | } |
||
| 762 | } |
||
| 763 | } |
||
| 764 | } |
||
| 765 | |||
| 766 | return $r; |
||
| 767 | } |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Static function to parse AICC ini strings. |
||
| 771 | * Based on work by sinedeo at gmail dot com published on php.net (parse_ini_file()). |
||
| 772 | * |
||
| 773 | * @param string INI File string |
||
| 774 | * @param array List of names of sections that should be considered |
||
| 775 | * as containing only hard string data (no variables), provided in lower case |
||
| 776 | * |
||
| 777 | * @return array Structured array |
||
| 778 | */ |
||
| 779 | public function parse_ini_string_quotes_safe($s, $pure_strings = []) |
||
| 780 | { |
||
| 781 | $null = ''; |
||
| 782 | $r = $null; |
||
| 783 | $sec = $null; |
||
| 784 | $s = api_convert_encoding($s, api_get_system_encoding(), $this->config_encoding); |
||
| 785 | //$f = split("\r\n", $s); |
||
| 786 | $f = preg_split('/\r?\n/', $s); |
||
| 787 | for ($i = 0; $i < @count($f); $i++) { |
||
| 788 | $newsec = 0; |
||
| 789 | $w = @trim($f[$i]); |
||
| 790 | if (';' == substr($w, 0, 1)) { |
||
| 791 | // Ignore comment lines |
||
| 792 | continue; |
||
| 793 | } |
||
| 794 | if ($w) { |
||
| 795 | if ((!$r) or ($sec)) { |
||
| 796 | if (('[' == @substr($w, 0, 1)) and ']' == (@substr($w, -1, 1))) { |
||
| 797 | $sec = @substr($w, 1, @strlen($w) - 2); |
||
| 798 | $pure_data = 0; |
||
| 799 | if (in_array(api_strtolower($sec), $pure_strings)) { |
||
| 800 | // This section can only be considered as pure string data (until the next section). |
||
| 801 | $pure_data = 1; |
||
| 802 | $r[api_strtolower($sec)] = ''; |
||
| 803 | } |
||
| 804 | $newsec = 1; |
||
| 805 | } |
||
| 806 | } |
||
| 807 | if (!$newsec) { |
||
| 808 | $w = @explode('=', $w); |
||
| 809 | $k = @trim($w[0]); |
||
| 810 | unset($w[0]); |
||
| 811 | $v = @trim(@implode('=', $w)); |
||
| 812 | if (("\"" == @substr($v, 0, 1)) and ("\"" == @substr($v, -1, 1))) { |
||
| 813 | $v = @substr($v, 1, @strlen($v) - 2); |
||
| 814 | } |
||
| 815 | if ($sec) { |
||
| 816 | if ($pure_data) { |
||
| 817 | $r[api_strtolower($sec)] .= $f[$i]; |
||
| 818 | } else { |
||
| 819 | if ('course_description' == api_strtolower($sec)) { // A special case. |
||
| 820 | $r[api_strtolower($sec)] = $k; |
||
| 821 | } else { |
||
| 822 | $r[api_strtolower($sec)][api_strtolower($k)] = $v; |
||
| 823 | } |
||
| 824 | } |
||
| 825 | } else { |
||
| 826 | $r[api_strtolower($k)] = $v; |
||
| 827 | } |
||
| 828 | } |
||
| 829 | } |
||
| 830 | } |
||
| 831 | |||
| 832 | return $r; |
||
| 833 | } |
||
| 834 | |||
| 835 | /** |
||
| 836 | * Static function that parses CSV files into simple arrays, based on a function |
||
| 837 | * by spam at cyber-space dot nl published on php.net (fgetcsv()). |
||
| 838 | * |
||
| 839 | * @param string Filepath |
||
| 840 | * @param string CSV delimiter |
||
| 841 | * @param string CSV enclosure |
||
| 842 | * @param bool Might one field name happen more than once on the same line? (then split by comma in the values) |
||
| 843 | * |
||
| 844 | * @return array Simple structured array |
||
| 845 | */ |
||
| 846 | public function parse_csv_file($f, $delim = ',', $enclosure = '"', $multiples = false) |
||
| 927 | } |
||
| 928 | } |
||
| 929 |