aicc.class.php 44 KB

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