aicc.class.php 42 KB

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