aicc.class.php 43 KB

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