aicc.class.php 44 KB

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