aicc.class.php 35 KB

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