aicc.class.php 44 KB

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