aicc.class.php 46 KB

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