aicc.class.php 44 KB

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