aicc.class.php 44 KB

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