aicc.class.php 44 KB

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