aicc.class.php 44 KB

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