scorm.class.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. <?php
  2. /**
  3. * Defines the scorm class, which is meant to contain the scorm items (nuclear elements)
  4. * @package dokeos.learnpath.scorm
  5. * @author Yannick Warnier <ywarnier@beeznest.org>
  6. */
  7. /**
  8. * Defines the "scorm" child of class "learnpath"
  9. * @package dokeos.learnpath
  10. */
  11. require_once('scormItem.class.php');
  12. require_once('scormMetadata.class.php');
  13. require_once('scormOrganization.class.php');
  14. require_once('scormResource.class.php');
  15. class scorm extends learnpath {
  16. var $manifest = array();
  17. var $resources = array();
  18. var $resources_att = array();
  19. var $organizations = array();
  20. var $organizations_att = array();
  21. var $metadata = array();
  22. var $idrefs = array(); //will hold the references to resources for each item ID found
  23. var $refurls = array(); //for each resource found, stores the file url/uri
  24. var $subdir = ''; //path between the scorm/ directory and the imsmanifest.xml e.g. maritime_nav/maritime_nav. This is the path that will be used in the lp_path when importing a package
  25. var $items = array();
  26. var $zipname = ''; //keeps the zipfile safe for the object's life so that we can use it if no title avail
  27. var $lastzipnameindex = 0; //keeps an index of the number of uses of the zipname so far
  28. var $manifest_encoding = 'UTF-8';
  29. var $debug = 0;
  30. /**
  31. * Class constructor. Based on the parent constructor.
  32. * @param string Course code
  33. * @param integer Learnpath ID in DB
  34. * @param integer User ID
  35. */
  36. function scorm($course_code=null,$resource_id=null,$user_id=null) {
  37. if($this->debug>0){error_log('New LP - scorm::scorm('.$course_code.','.$resource_id.','.$user_id.') - In scorm constructor',0);}
  38. if(!empty($course_code) and !empty($resource_id) and !empty($user_id))
  39. {
  40. parent::learnpath($course_code, $resource_id, $user_id);
  41. }else{
  42. //do nothing but still build the scorm object
  43. }
  44. }
  45. /**
  46. * Opens a resource
  47. * @param integer Database ID of the resource
  48. */
  49. function open($id)
  50. {
  51. if($this->debug>0){error_log('New LP - scorm::open() - In scorm::open method',0);}
  52. // redefine parent method
  53. }
  54. /**
  55. * Possible SCO status: see CAM doc 2.3.2.5.1: passed, completed, browsed, failed, not attempted, incomplete
  56. */
  57. /**
  58. * Prerequisites: see CAM doc 2.3.2.5.1 for pseudo-code
  59. */
  60. /**
  61. * Parses an imsmanifest.xml file and puts everything into the $manifest array
  62. * @param string Path to the imsmanifest.xml file on the system. If not defined, uses the base path of the course's scorm dir
  63. * @return array Structured array representing the imsmanifest's contents
  64. */
  65. function parse_manifest($file='')
  66. {
  67. if($this->debug>0){error_log('In scorm::parse_manifest('.$file.')',0);}
  68. if(empty($file)){
  69. //get the path of the imsmanifest file
  70. }
  71. if(is_file($file) and is_readable($file))
  72. {
  73. $v = substr(phpversion(),0,1);
  74. if($v == 4){
  75. if($this->debug>0){error_log('In scorm::parse_manifest() - Parsing using PHP4 method',0);}
  76. $var = file_get_contents($file);
  77. //quick ugly hack to remove xml:id attributes from the file (break the system if xslt not installed)
  78. $var = preg_replace('/xml:id=["\']id_\d{1,4}["\']/i','',$var);
  79. $doc = xmldoc($var);
  80. //error_reporting(E_ALL);
  81. //$doc = xmldocfile($file);
  82. if(!empty($doc->encoding)){
  83. $this->manifest_encoding = strtoupper($doc->encoding);
  84. }
  85. if($this->debug>1){error_log('New LP - Called xmldoc() (encoding:'.strtoupper($doc->encoding).' - saved: '.$this->manifest_encoding.')',0);}
  86. if(!$doc)
  87. {
  88. if($this->debug>1){error_log('New LP - File '.$file.' is not an XML file',0);}
  89. //$this->set_error_msg("File $file is not an XML file");
  90. return null;
  91. }else{
  92. if($this->debug>1){error_log('New LP - File '.$file.' is XML',0);}
  93. $root = $doc->root();
  94. $attributes = $root->attributes();
  95. for($a=0; $a<sizeof($attributes);$a++)
  96. {//<manifest> element attributes
  97. if($attributes[$a]->type == XML_ATTRIBUTE_NODE){
  98. $this->manifest[$attributes[$a]->name] = $attributes[$a]->value;
  99. }
  100. }
  101. $this->manifest['name'] = $root->name;
  102. $children = $root->children();
  103. for($b=0; $b<sizeof($children);$b++)
  104. {
  105. //<manifest> element children (can be <metadata>, <organizations> or <resources> )
  106. if($children[$b]->type == XML_ELEMENT_NODE){
  107. switch($children[$b]->tagname){
  108. case 'metadata':
  109. //parse items from inside the <metadata> element
  110. $this->metadata = new scormMetadata('manifest',$children[$b]);
  111. break;
  112. case 'organizations':
  113. //contains the course structure - this element appears 1 and only 1 time in a package imsmanifest. It contains at least one 'organization' sub-element
  114. $orgs_attribs = $children[$b]->attributes();
  115. for($c=0; $c<sizeof($orgs_attribs);$c++)
  116. {//attributes of the <organizations> element
  117. if($orgs_attribs[$c]->type == XML_ATTRIBUTE_NODE){
  118. $this->manifest['organizations'][$orgs_attribs[$c]->name] = $orgs_attribs[$c]->value;
  119. }
  120. }
  121. $orgs_nodes = $children[$b]->children();
  122. $i = 0;
  123. $found_an_org = false;
  124. foreach($orgs_nodes as $c => $dummy)
  125. {
  126. //<organization> elements - can contain <item>, <metadata> and <title>
  127. //Here we are at the 'organization' level. There might be several organization tags but
  128. //there is generally only one.
  129. //There are generally three children nodes we are looking for inside and organization:
  130. //-title
  131. //-item (may contain other item tags or may appear several times inside organization)
  132. //-metadata (relative to the organization)
  133. $found_an_org = false;
  134. $orgnode =& $orgs_nodes[$c];
  135. switch($orgnode->type){
  136. case XML_TEXT_NODE:
  137. //ignore here
  138. break;
  139. case XML_ATTRIBUTE_NODE:
  140. //just in case ther would be interesting attributes inside the organization tag. There shouldn't
  141. //as this is a node-level, not a data level
  142. //$manifest['organizations'][$i][$orgnode->name] = $orgnode->value;
  143. //$found_an_org = true;
  144. break;
  145. case XML_ELEMENT_NODE:
  146. //<item>,<metadata> or <title> (or attributes)
  147. $organizations_attributes = $orgnode->attributes();
  148. foreach($organizations_attributes as $d1 => $dummy)
  149. {
  150. //$manifest['organizations'][$i]['attributes'][$organizations_attributes[$d1]->name] = $organizations_attributes[$d1]->value;
  151. //$found_an_org = true;
  152. $this->organizations_att[$organizations_attributes[$d1]->name] = $organizations_attributes[$d1]->value;
  153. }
  154. $oOrganization = new scormOrganization('manifest',$orgnode,$this->manifest_encoding);
  155. if($oOrganization->identifier != ''){
  156. $name = $oOrganization->get_name();
  157. if(empty($name)){
  158. //if the org title is empty, use zip file name
  159. $myname = $this->zipname;
  160. if($this->lastzipnameindex != 0){
  161. $myname = $myname + $this->lastzipnameindex;
  162. $this->lastzipnameindex++;
  163. }
  164. $oOrganization->set_name($this->zipname);
  165. }
  166. $this->organizations[$oOrganization->identifier] = $oOrganization;
  167. }
  168. break;
  169. }
  170. }
  171. break;
  172. case 'resources':
  173. $resources_attribs = $children[$b]->attributes();
  174. for($c=0; $c<sizeof($resources_attribs);$c++)
  175. {
  176. if($resources_attribs[$c]->type == XML_ATTRIBUTE_NODE){
  177. $this->manifest['resources'][$resources_attribs[$c]->name] = $resources_attribs[$c]->value;
  178. }
  179. }
  180. $resources_nodes = $children[$b]->children();
  181. $i = 0;
  182. foreach($resources_nodes as $c => $dummy)
  183. {
  184. /*
  185. $my_res = scorm::parse_items($resources_nodes[$c]);
  186. scorm::parse_items_get_refurls($resources_nodes[$c],$refurls);
  187. if(count($my_res)>0)
  188. {
  189. $manifest['resources'][$i] = $my_res;
  190. }
  191. */
  192. $oResource = new scormResource('manifest',$resources_nodes[$c]);
  193. if($oResource->identifier != ''){
  194. $this->resources[$oResource->identifier] = $oResource;
  195. $i++;
  196. }
  197. }
  198. //contains links to physical resources
  199. break;
  200. case 'manifest':
  201. //only for sub-manifests
  202. break;
  203. }
  204. }
  205. }
  206. }
  207. unset($doc);
  208. }elseif($v==5){
  209. if($this->debug>0){error_log('In scorm::parse_manifest() - Parsing using PHP5 method',0);}
  210. $doc = new DOMDocument();
  211. $res = $doc->load($file);
  212. if($res===false){
  213. if($this->debug>0){error_log('New LP - In scorm::parse_manifest() - Exception thrown when loading '.$file.' in DOMDocument',0);}
  214. //throw exception?
  215. return null;
  216. }
  217. if(!empty($doc->xmlEncoding)){
  218. $this->manifest_encoding = strtoupper($doc->xmlEncoding);
  219. }
  220. if($this->debug>1){error_log('New LP - Called (encoding:'.$doc->xmlEncoding.' - saved: '.$this->manifest_encoding.')',0);}
  221. $root = $doc->documentElement;
  222. if($root->hasAttributes()){
  223. $attributes = $root->attributes;
  224. if($attributes->length !== 0){
  225. foreach($attributes as $attrib)
  226. {//<manifest> element attributes
  227. $this->manifest[$attrib->name] = $attrib->value;
  228. }
  229. }
  230. }
  231. $this->manifest['name'] = $root->tagName;
  232. if($root->hasChildNodes()){
  233. $children = $root->childNodes;
  234. if($children->length !== 0){
  235. foreach($children as $child)
  236. {
  237. //<manifest> element children (can be <metadata>, <organizations> or <resources> )
  238. if($child->nodeType == XML_ELEMENT_NODE){
  239. switch($child->tagName){
  240. case 'metadata':
  241. //parse items from inside the <metadata> element
  242. $this->metadata = new scormMetadata('manifest',$child);
  243. break;
  244. case 'organizations':
  245. //contains the course structure - this element appears 1 and only 1 time in a package imsmanifest. It contains at least one 'organization' sub-element
  246. $orgs_attribs = $child->attributes;
  247. foreach($orgs_attribs as $orgs_attrib)
  248. {//attributes of the <organizations> element
  249. if($orgs_attrib->nodeType == XML_ATTRIBUTE_NODE){
  250. $this->manifest['organizations'][$orgs_attrib->name] = $orgs_attrib->value;
  251. }
  252. }
  253. $orgs_nodes = $child->childNodes;
  254. $i = 0;
  255. $found_an_org = false;
  256. foreach($orgs_nodes as $orgnode)
  257. {
  258. //<organization> elements - can contain <item>, <metadata> and <title>
  259. //Here we are at the 'organization' level. There might be several organization tags but
  260. //there is generally only one.
  261. //There are generally three children nodes we are looking for inside and organization:
  262. //-title
  263. //-item (may contain other item tags or may appear several times inside organization)
  264. //-metadata (relative to the organization)
  265. $found_an_org = false;
  266. switch($orgnode->nodeType){
  267. case XML_TEXT_NODE:
  268. //ignore here
  269. break;
  270. case XML_ATTRIBUTE_NODE:
  271. //just in case there would be interesting attributes inside the organization tag. There shouldn't
  272. //as this is a node-level, not a data level
  273. //$manifest['organizations'][$i][$orgnode->name] = $orgnode->value;
  274. //$found_an_org = true;
  275. break;
  276. case XML_ELEMENT_NODE:
  277. //<item>,<metadata> or <title> (or attributes)
  278. $organizations_attributes = $orgnode->attributes;
  279. foreach($organizations_attributes as $orgs_attr)
  280. {
  281. $this->organizations_att[$orgs_attr->name] = $orgs_attr->value;
  282. }
  283. $oOrganization = new scormOrganization('manifest',$orgnode,$this->manifest_encoding);
  284. if($oOrganization->identifier != ''){
  285. $name = $oOrganization->get_name();
  286. if(empty($name)){
  287. //if the org title is empty, use zip file name
  288. $myname = $this->zipname;
  289. if($this->lastzipnameindex != 0){
  290. $myname = $myname + $this->lastzipnameindex;
  291. $this->lastzipnameindex++;
  292. }
  293. $oOrganization->set_name($this->zipname);
  294. }
  295. $this->organizations[$oOrganization->identifier] = $oOrganization;
  296. }
  297. break;
  298. }
  299. }
  300. break;
  301. case 'resources':
  302. if($child->hasAttributes()){
  303. $resources_attribs = $child->attributes;
  304. foreach($resources_attribs as $res_attr)
  305. {
  306. if($res_attr->type == XML_ATTRIBUTE_NODE){
  307. $this->manifest['resources'][$res_attr->name] = $res_attr->value;
  308. }
  309. }
  310. }
  311. if($child->hasChildNodes()){
  312. $resources_nodes = $child->childNodes;
  313. $i = 0;
  314. foreach($resources_nodes as $res_node)
  315. {
  316. $oResource = new scormResource('manifest',$res_node);
  317. if($oResource->identifier != ''){
  318. $this->resources[$oResource->identifier] = $oResource;
  319. $i++;
  320. }
  321. }
  322. }
  323. //contains links to physical resources
  324. break;
  325. case 'manifest':
  326. //only for sub-manifests
  327. break;
  328. }
  329. }
  330. }
  331. }
  332. }
  333. unset($doc);
  334. }else{
  335. if($this->debug>0){error_log('In scorm::parse_manifest() - PHP version is not 4 nor 5, cannot parse',0);}
  336. $this->set_error_msg("Parsing impossible because PHP version is not 4 nor 5");
  337. return null;
  338. }
  339. }else{
  340. if($this->debug>1){error_log('New LP - Could not open/read file '.$file,0);}
  341. $this->set_error_msg("File $file could not be read");
  342. return null;
  343. }
  344. //TODO close the DOM handler
  345. return $this->manifest;
  346. }
  347. /**
  348. * Import the scorm object (as a result from the parse_manifest function) into the database structure
  349. * @param string Unique course code
  350. * @return bool Returns -1 on error
  351. */
  352. function import_manifest($course_code){
  353. if($this->debug>0){error_log('New LP - Entered import_manifest('.$course_code.')',0);}
  354. $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE)." WHERE code='$course_code'";
  355. $res = api_sql_query($sql,__FILE__,__LINE__);
  356. if(Database::num_rows($res)<1){ error_log('Database for '.$course_code.' not found '.__FILE__.' '.__LINE__,0);return -1;}
  357. $row = Database::fetch_array($res);
  358. $dbname = $row['db_name'];
  359. //get table names
  360. $new_lp = Database::get_course_table('lp',$dbname);
  361. $new_lp_item = Database::get_course_table('lp_item',$dbname);
  362. foreach($this->organizations as $id => $dummy)
  363. {
  364. $oOrganization =& $this->organizations[$id];
  365. //prepare and execute insert queries
  366. //-for learnpath
  367. //-for items
  368. //-for views?
  369. $get_max = "SELECT MAX(display_order) FROM $new_lp";
  370. $res_max = api_sql_query($get_max);
  371. $dsp = 1;
  372. if(Database::num_rows($res_max)>0){
  373. $row = Database::fetch_array($res_max);
  374. $dsp = $row[0]+1;
  375. }
  376. $myname = $oOrganization->get_name();
  377. //$this->manifest_encoding = 'UTF-8';
  378. global $charset;
  379. if(!empty($charset) && !empty($this->manifest_encoding) && $this->manifest_encoding != $charset){
  380. $myname = mb_convert_encoding($myname,$charset,$this->manifest_encoding);
  381. //error_log('New LP - Converting name from '.$this->manifest_encoding.' to ISO-8859-1',0);
  382. }
  383. $sql = "INSERT INTO $new_lp (lp_type, name, ref, description, path, force_commit, default_view_mod, default_encoding, js_lib,display_order)" .
  384. "VALUES (2,'".$myname."', '".$oOrganization->get_ref()."','','".$this->subdir."', 0, 'embedded', '".$this->manifest_encoding."','scorm_api.php',$dsp)";
  385. if($this->debug>1){error_log('New LP - In import_manifest(), inserting path: '. $sql,0);}
  386. $res = api_sql_query($sql,__FILE__,__LINE__);
  387. $lp_id = Database::get_last_insert_id();
  388. $this->lp_id = $lp_id;
  389. //insert into item_property
  390. api_item_property_update(api_get_course_info($course_code),TOOL_LEARNPATH,$this->lp_id,'LearnpathAdded',api_get_user_id());
  391. api_item_property_update(api_get_course_info($course_code),TOOL_LEARNPATH,$this->lp_id,'visible',api_get_user_id());
  392. //now insert all elements from inside that learning path
  393. //make sure we also get the href and sco/asset from the resources
  394. $list = $oOrganization->get_flat_items_list();
  395. $parents_stack = array(0);
  396. $parent = 0;
  397. $previous = 0;
  398. $level = 0;
  399. foreach($list as $item){
  400. if($item['level'] > $level){
  401. //push something into the parents array
  402. array_push($parents_stack,$previous);
  403. $parent = $previous;
  404. }elseif($item['level'] < $level){
  405. $diff = $level - $item['level'];
  406. //pop something out of the parents array
  407. for($j=1;$j<=$diff;$j++){
  408. $outdated_parent = array_pop($parents_stack);
  409. }
  410. $parent = array_pop($parents_stack); //just save that value, then add it back
  411. array_push($parents_stack,$parent);
  412. }
  413. $path = '';
  414. $type = 'dir';
  415. if(isset($this->resources[$item['identifierref']])){
  416. $oRes =& $this->resources[$item['identifierref']];
  417. $path = @$oRes->get_path();
  418. if(!empty($path)){
  419. $temptype = $oRes->get_scorm_type();
  420. if(!empty($temptype)){
  421. $type = $temptype;
  422. }
  423. }
  424. }
  425. $level = $item['level'];
  426. $field_add = '';
  427. $value_add = '';
  428. if(!empty($item['masteryscore'])){
  429. $field_add .= 'mastery_score, ';
  430. $value_add .= $item['masteryscore'].',';
  431. }
  432. if(!empty($item['maxtimeallowed']))
  433. {
  434. $field_add .= 'max_time_allowed, ';
  435. $value_add .= "'".$item['maxtimeallowed']."',";
  436. }
  437. $title = mysql_real_escape_string($item['title']);
  438. //DOM in PHP5 is always recovering data as UTF-8, somehow, no matter what
  439. //the XML document encoding is. This means that we have to convert
  440. //the data to the declared encoding when it is not UTF-8
  441. if($this->manifest_encoding != 'UTF-8'){
  442. $title = mb_convert_encoding($title,$this->manifest_encoding,'UTF-8');
  443. }
  444. //if($this->manifest_encoding != $charset){
  445. // $title = mb_convert_encoding($title,$charset,$this->manifest_encoding);
  446. //}
  447. $identifier = mysql_real_escape_string($item['identifier']);
  448. $prereq = mysql_real_escape_string($item['prerequisites']);
  449. $sql_item = "INSERT INTO $new_lp_item " .
  450. "(lp_id,item_type,ref,title," .
  451. "path,min_score,max_score, $field_add" .
  452. "parent_item_id,previous_item_id,next_item_id," .
  453. "prerequisite,display_order,launch_data," .
  454. "parameters) " .
  455. "VALUES " .
  456. "($lp_id, '$type','".$identifier."','".$title."'," .
  457. "'$path',0,100, $value_add" .
  458. "$parent, $previous, 0, " .
  459. "'$prereq', ".$item['rel_order'] .", '".$item['datafromlms']."'," .
  460. "'".$item['parameters']."'" .
  461. ")";
  462. $res_item = api_sql_query($sql_item);
  463. if($this->debug>1){error_log('New LP - In import_manifest(), inserting item : '.$sql_item.' : '.mysql_error(),0);}
  464. $item_id = Database::get_last_insert_id();
  465. //now update previous item to change next_item_id
  466. $upd = "UPDATE $new_lp_item SET next_item_id = $item_id WHERE id = $previous";
  467. $upd_res = api_sql_query($upd);
  468. //update previous item id
  469. $previous = $item_id;
  470. // code for indexing, now only index specific fields like terms and the title
  471. if (!empty($_POST['index_document'])) {
  472. require_once(api_get_path(LIBRARY_PATH).'search/DokeosIndexer.class.php');
  473. require_once(api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php');
  474. require_once(api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php');
  475. $di = new DokeosIndexer();
  476. isset($_POST['language'])? $lang=Database::escape_string($_POST['language']): $lang = 'english';
  477. $di->connectDb(NULL, NULL, $lang);
  478. $ic_slide = new IndexableChunk();
  479. $ic_slide->addValue("title", $title);
  480. $specific_fields = get_specific_field_list();
  481. $all_specific_terms = '';
  482. foreach ($specific_fields as $specific_field) {
  483. if (isset($_REQUEST[$specific_field['code']])) {
  484. $sterms = trim($_REQUEST[$specific_field['code']]);
  485. $all_specific_terms .= ' '. $sterms;
  486. if (!empty($sterms)) {
  487. $sterms = explode(',', $sterms);
  488. foreach ($sterms as $sterm) {
  489. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  490. }
  491. }
  492. }
  493. }
  494. $body_to_index = $all_specific_terms .' '. $title;
  495. $ic_slide->addValue("content", $body_to_index);
  496. //TODO: add a comment to say terms separated by commas
  497. $courseid = api_get_course_id();
  498. $ic_slide->addCourseId($courseid);
  499. $ic_slide->addToolId(TOOL_LEARNPATH);
  500. $xapian_data = array(
  501. SE_COURSE_ID => $courseid,
  502. SE_TOOL_ID => TOOL_LEARNPATH,
  503. SE_DATA => array('lp_id' => $lp_id, 'lp_item'=> $previous, 'document_id' => ''), //TODO unify with other lp types
  504. SE_USER => (int)api_get_user_id(),
  505. );
  506. $ic_slide->xapian_data = serialize($xapian_data);
  507. $di->addChunk($ic_slide);
  508. //index and return search engine document id
  509. $did = $di->index();
  510. if ($did) {
  511. // save it to db
  512. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  513. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did)
  514. VALUES (NULL , \'%s\', \'%s\', %s, %s, %s)';
  515. $sql = sprintf($sql, $tbl_se_ref, api_get_course_id(), TOOL_LEARNPATH, $lp_id, $previous, $did);
  516. api_sql_query($sql,__FILE__,__LINE__);
  517. }
  518. }
  519. }
  520. }
  521. }
  522. /**
  523. * Intermediate to import_package only to allow import from local zip files
  524. * @param string Path to the zip file, from the dokeos sys root
  525. * @param string Current path (optional)
  526. * @return string Absolute path to the imsmanifest.xml file or empty string on error
  527. */
  528. function import_local_package($file_path,$current_dir='')
  529. {
  530. //todo prepare info as given by the $_FILES[''] vector
  531. $file_info = array();
  532. $file_info['tmp_name'] = $file_path;
  533. $file_info['name'] = basename($file_path);
  534. //call the normal import_package function
  535. return $this->import_package($file_info,$current_dir);
  536. }
  537. /**
  538. * Imports a zip file into the Dokeos structure
  539. * @param string Zip file info as given by $_FILES['userFile']
  540. * @return string Absolute path to the imsmanifest.xml file or empty string on error
  541. */
  542. function import_package($zip_file_info,$current_dir = '')
  543. {
  544. if($this->debug>0){error_log('In scorm::import_package('.print_r($zip_file_info,true).',"'.$current_dir.'") method',0);}
  545. $maxFilledSpace = 1000000000;
  546. $zip_file_path = $zip_file_info['tmp_name'];
  547. $zip_file_name = $zip_file_info['name'];
  548. if($this->debug>1){error_log('New LP - import_package() - zip file path = '.$zip_file_path.', zip file name = '.$zip_file_name,0);}
  549. $course_rel_dir = api_get_course_path().'/scorm'; //scorm dir web path starting from /courses
  550. $course_sys_dir = api_get_path(SYS_COURSE_PATH).$course_rel_dir; //absolute system path for this course
  551. $current_dir = replace_dangerous_char(trim($current_dir),'strict'); //current dir we are in, inside scorm/
  552. if($this->debug>1){error_log('New LP - import_package() - current_dir = '.$current_dir,0);}
  553. //$uploaded_filename = $_FILES['userFile']['name'];
  554. //get name of the zip file without the extension
  555. if($this->debug>1){error_log('New LP - Received zip file name: '.$zip_file_path,0);}
  556. $file_info = pathinfo($zip_file_name);
  557. $filename = $file_info['basename'];
  558. $extension = $file_info['extension'];
  559. $file_base_name = str_replace('.'.$extension,'',$filename); //filename without its extension
  560. $this->zipname = $file_base_name; //save for later in case we don't have a title
  561. if($this->debug>1){error_log("New LP - base file name is : ".$file_base_name,0);}
  562. $new_dir = replace_dangerous_char(trim($file_base_name),'strict');
  563. $this->subdir = $new_dir;
  564. if($this->debug>1){error_log("New LP - subdir is first set to : ".$this->subdir,0);}
  565. $zipFile = new pclZip($zip_file_path);
  566. // Check the zip content (real size and file extension)
  567. $zipContentArray = $zipFile->listContent();
  568. $package_type='';
  569. $at_root = false;
  570. $manifest = '';
  571. $manifest_list = array();
  572. //the following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?)
  573. foreach($zipContentArray as $thisContent)
  574. {
  575. //error_log('Looking at '.$thisContent['filename'],0);
  576. if ( preg_match('~.(php.*|phtml)$~i', $thisContent['filename']) )
  577. {
  578. $this->set_error_msg("File $file contains a PHP script");
  579. //return api_failure::set_failure('php_file_in_zip_file');
  580. }
  581. elseif(stristr($thisContent['filename'],'imsmanifest.xml'))
  582. {
  583. //error_log('Found imsmanifest at '.$thisContent['filename'],0);
  584. if($thisContent['filename'] == basename($thisContent['filename'])){
  585. $at_root = true;
  586. }else{
  587. //$this->subdir .= '/'.dirname($thisContent['filename']);
  588. if($this->debug>2){error_log("New LP - subdir is now ".$this->subdir,0);}
  589. }
  590. $package_type = 'scorm';
  591. $manifest_list[] = $thisContent['filename'];
  592. $manifest = $thisContent['filename']; //just the relative directory inside scorm/
  593. }
  594. else
  595. {
  596. //do nothing, if it has not been set as scorm somewhere else, it stays as '' default
  597. }
  598. $realFileSize += $thisContent['size'];
  599. }
  600. //now get the shortest path (basically, the imsmanifest that is the closest to the root)
  601. $shortest_path = $manifest_list[0];
  602. $slash_count = substr_count($shortest_path,'/');
  603. foreach($manifest_list as $manifest_path){
  604. $tmp_slash_count = substr_count($manifest_path,'/');
  605. if($tmp_slash_count<$slash_count){
  606. $shortest_path = $manifest_path;
  607. $slash_count = $tmp_slash_count;
  608. }
  609. }
  610. $this->subdir .= '/'.dirname($shortest_path); //do not concatenate because already done above
  611. $manifest = $shortest_path;
  612. if($this->debug>1){error_log('New LP - Package type is now '.$package_type,0);}
  613. if($package_type== '')
  614. // && defined('CHECK_FOR_SCORM') && CHECK_FOR_SCORM)
  615. {
  616. return api_failure::set_failure('not_scorm_content');
  617. }
  618. if (! enough_size($realFileSize, $course_sys_dir, $maxFilledSpace) )
  619. {
  620. return api_failure::set_failure('not_enough_space');
  621. }
  622. // it happens on Linux that $new_dir sometimes doesn't start with '/'
  623. if($new_dir[0] != '/')
  624. {
  625. $new_dir='/'.$new_dir;
  626. }
  627. if($new_dir[strlen($new_dir)-1] == '/')
  628. {
  629. $new_dir=substr($new_dir,0,-1);
  630. }
  631. /*
  632. --------------------------------------
  633. Uncompressing phase
  634. --------------------------------------
  635. */
  636. /*
  637. We need to process each individual file in the zip archive to
  638. - add it to the database
  639. - parse & change relative html links
  640. - make sure the filenames are secure (filter funny characters or php extensions)
  641. */
  642. if(is_dir($course_sys_dir.$new_dir) OR @mkdir($course_sys_dir.$new_dir))
  643. {
  644. // PHP method - slower...
  645. if($this->debug>=1){error_log('New LP - Changing dir to '.$course_sys_dir.$new_dir,0);}
  646. $saved_dir = getcwd();
  647. chdir($course_sys_dir.$new_dir);
  648. $unzippingState = $zipFile->extract();
  649. for($j=0;$j<count($unzippingState);$j++)
  650. {
  651. $state=$unzippingState[$j];
  652. //TODO fix relative links in html files (?)
  653. $extension = strrchr($state["stored_filename"], ".");
  654. if($this->debug>=1){error_log('New LP - found extension '.$extension.' in '.$state['stored_filename'],0);}
  655. }
  656. if(!empty($new_dir))
  657. {
  658. $new_dir = $new_dir.'/';
  659. }
  660. //rename files, for example with \\ in it
  661. if($dir=@opendir($course_sys_dir.$new_dir))
  662. {
  663. if($this->debug==1){error_log('New LP - Opened dir '.$course_sys_dir.$new_dir,0);}
  664. while($file=readdir($dir))
  665. {
  666. if($file != '.' && $file != '..')
  667. {
  668. $filetype="file";
  669. if(is_dir($course_sys_dir.$new_dir.$file)) $filetype="folder";
  670. //TODO RENAMING FILES CAN BE VERY DANGEROUS SCORM-WISE, avoid that as much as possible!
  671. //$safe_file=replace_dangerous_char($file,'strict');
  672. $find_str = array('\\','.php','.phtml');
  673. $repl_str = array('/', '.txt','.txt');
  674. $safe_file = str_replace($find_str,$repl_str,$file);
  675. if($safe_file != $file){
  676. //@rename($course_sys_dir.$new_dir,$course_sys_dir.'/'.$safe_file);
  677. $mydir = dirname($course_sys_dir.$new_dir.$safe_file);
  678. if(!is_dir($mydir)){
  679. $mysubdirs = split('/',$mydir);
  680. $mybasedir = '/';
  681. foreach($mysubdirs as $mysubdir){
  682. if(!empty($mysubdir)){
  683. $mybasedir = $mybasedir.$mysubdir.'/';
  684. if(!is_dir($mybasedir)){
  685. @mkdir($mybasedir);
  686. if($this->debug==1){error_log('New LP - Dir '.$mybasedir.' doesnt exist. Creating.',0);}
  687. }
  688. }
  689. }
  690. }
  691. @rename($course_sys_dir.$new_dir.$file,$course_sys_dir.$new_dir.$safe_file);
  692. if($this->debug==1){error_log('New LP - Renaming '.$course_sys_dir.$new_dir.$file.' to '.$course_sys_dir.$new_dir.$safe_file,0);}
  693. }
  694. //set_default_settings($course_sys_dir,$safe_file,$filetype);
  695. }
  696. }
  697. closedir($dir);
  698. chdir($saved_dir);
  699. $perm = api_get_setting('permissions_for_new_directories');
  700. $perm = octdec(!empty($perm)?$perm:'0770');
  701. api_chmod_R($course_sys_dir.$new_dir , $perm);
  702. }
  703. }else{
  704. return '';
  705. }
  706. return $course_sys_dir.$new_dir.$manifest;
  707. }
  708. /**
  709. * Sets the proximity setting in the database
  710. * @param string Proximity setting
  711. */
  712. function set_proximity($proxy=''){
  713. if($this->debug>0){error_log('In scorm::set_proximity('.$proxy.') method',0);}
  714. $lp = $this->get_id();
  715. if($lp!=0){
  716. $tbl_lp = Database::get_course_table('lp');
  717. $sql = "UPDATE $tbl_lp SET content_local = '$proxy' WHERE id = ".$lp;
  718. $res = api_sql_query($sql);
  719. return $res;
  720. }else{
  721. return false;
  722. }
  723. }
  724. /**
  725. * Sets the theme setting in the database
  726. * @param string theme setting
  727. */
  728. function set_theme($theme=''){
  729. if($this->debug>0){error_log('In scorm::set_theme('.$theme.') method',0);}
  730. $lp = $this->get_id();
  731. if($lp!=0){
  732. $tbl_lp = Database::get_course_table('lp');
  733. $sql = "UPDATE $tbl_lp SET theme = '$theme' WHERE id = ".$lp;
  734. $res = api_sql_query($sql);
  735. return $res;
  736. }else{
  737. return false;
  738. }
  739. }
  740. /**
  741. * Sets the image setting in the database
  742. * @param string preview_image setting
  743. */
  744. function set_preview_image($preview_image=''){
  745. if($this->debug>0){error_log('In scorm::set_theme('.$preview_image.') method',0);}
  746. $lp = $this->get_id();
  747. if($lp!=0){
  748. $tbl_lp = Database::get_course_table('lp');
  749. $sql = "UPDATE $tbl_lp SET preview_image = '$preview_image' WHERE id = ".$lp;
  750. $res = api_sql_query($sql);
  751. return $res;
  752. }else{
  753. return false;
  754. }
  755. }
  756. /**
  757. * Sets the author setting in the database
  758. * @param string preview_image setting
  759. */
  760. function set_author($author=''){
  761. if($this->debug>0){error_log('In scorm::set_author('.$author.') method',0);}
  762. $lp = $this->get_id();
  763. if($lp!=0){
  764. $tbl_lp = Database::get_course_table('lp');
  765. $sql = "UPDATE $tbl_lp SET author = '$author' WHERE id = ".$lp;
  766. $res = api_sql_query($sql);
  767. return $res;
  768. }else{
  769. return false;
  770. }
  771. }
  772. /**
  773. * Sets the content maker setting in the database
  774. * @param string Proximity setting
  775. */
  776. function set_maker($maker=''){
  777. if($this->debug>0){error_log('In scorm::set_maker method('.$maker.')',0);}
  778. $lp = $this->get_id();
  779. if($lp!=0){
  780. $tbl_lp = Database::get_course_table('lp');
  781. $sql = "UPDATE $tbl_lp SET content_maker = '$maker' WHERE id = ".$lp;
  782. $res = api_sql_query($sql);
  783. return $res;
  784. }else{
  785. return false;
  786. }
  787. }
  788. /**
  789. * Exports the current SCORM object's files as a zip. Excerpts taken from learnpath_functions.inc.php::exportpath()
  790. * @param integer Learnpath ID (optional, taken from object context if not defined)
  791. */
  792. function export_zip($lp_id=null){
  793. if($this->debug>0){error_log('In scorm::export_zip method('.$lp_id.')',0);}
  794. if(empty($lp_id)){
  795. if(!is_object($this))
  796. {
  797. return false;
  798. }
  799. else{
  800. $id = $this->get_id();
  801. if(empty($id)){
  802. return false;
  803. }
  804. else{
  805. $lp_id = $this->get_id();
  806. }
  807. }
  808. }
  809. //error_log('New LP - in export_zip()',0);
  810. //zip everything that is in the corresponding scorm dir
  811. //write the zip file somewhere (might be too big to return)
  812. require_once (api_get_path(LIBRARY_PATH)."fileUpload.lib.php");
  813. require_once (api_get_path(LIBRARY_PATH)."fileManage.lib.php");
  814. require_once (api_get_path(LIBRARY_PATH)."document.lib.php");
  815. require_once (api_get_path(LIBRARY_PATH)."pclzip/pclzip.lib.php");
  816. require_once ("learnpath_functions.inc.php");
  817. $tbl_lp = Database::get_course_table('lp');
  818. $_course = Database::get_course_info(api_get_course_id());
  819. $sql = "SELECT * FROM $tbl_lp WHERE id=".$lp_id;
  820. $result = api_sql_query($sql, __FILE__, __LINE__);
  821. $row = mysql_fetch_array($result);
  822. $LPname = $row['path'];
  823. $list = split('/',$LPname);
  824. $LPnamesafe = $list[0];
  825. //$zipfoldername = '/tmp';
  826. //$zipfoldername = '../../courses/'.$_course['directory']."/temp/".$LPnamesafe;
  827. $zipfoldername = api_get_path('SYS_COURSE_PATH').$_course['directory']."/temp/".$LPnamesafe;
  828. $scormfoldername = api_get_path('SYS_COURSE_PATH').$_course['directory']."/scorm/".$LPnamesafe;
  829. $zipfilename = $zipfoldername."/".$LPnamesafe.".zip";
  830. //Get a temporary dir for creating the zip file
  831. //error_log('New LP - cleaning dir '.$zipfoldername,0);
  832. deldir($zipfoldername); //make sure the temp dir is cleared
  833. $res = mkdir($zipfoldername);
  834. //error_log('New LP - made dir '.$zipfoldername,0);
  835. //create zipfile of given directory
  836. $zip_folder = new PclZip($zipfilename);
  837. $zip_folder->create($scormfoldername.'/', PCLZIP_OPT_REMOVE_PATH, $scormfoldername.'/');
  838. //$zipfilename = '/var/www/dokeos-comp/courses/TEST2/scorm/example_document.html';
  839. //this file sending implies removing the default mime-type from php.ini
  840. //DocumentManager :: file_send_for_download($zipfilename, true, $LPnamesafe.".zip");
  841. DocumentManager :: file_send_for_download($zipfilename, true);
  842. // Delete the temporary zip file and directory in fileManage.lib.php
  843. my_delete($zipfilename);
  844. my_delete($zipfoldername);
  845. return true;
  846. }
  847. /**
  848. * Gets a resource's path if available, otherwise return empty string
  849. * @param string Resource ID as used in resource array
  850. * @return string The resource's path as declared in imsmanifest.xml
  851. */
  852. function get_res_path($id){
  853. if($this->debug>0){error_log('In scorm::get_res_path('.$id.') method',0);}
  854. $path = '';
  855. if(isset($this->resources[$id])){
  856. $oRes =& $this->resources[$id];
  857. $path = @$oRes->get_path();
  858. }
  859. return $path;
  860. }
  861. /**
  862. * Gets a resource's type if available, otherwise return empty string
  863. * @param string Resource ID as used in resource array
  864. * @return string The resource's type as declared in imsmanifest.xml
  865. */
  866. function get_res_type($id){
  867. if($this->debug>0){error_log('In scorm::get_res_type('.$id.') method',0);}
  868. $type = '';
  869. if(isset($this->resources[$id])){
  870. $oRes =& $this->resources[$id];
  871. $temptype = $oRes->get_scorm_type();
  872. if(!empty($temptype)){
  873. $type = $temptype;
  874. }
  875. }
  876. return $type;
  877. }
  878. /**
  879. * Gets the default organisation's title
  880. * @return string The organization's title
  881. */
  882. function get_title(){
  883. if($this->debug>0){error_log('In scorm::get_title() method',0);}
  884. $title = '';
  885. if(isset($this->manifest['organizations']['default'])){
  886. $title = $this->organizations[$this->manifest['organizations']['default']]->get_name();
  887. }elseif(count($this->organizations)==1){
  888. //this will only get one title but so we don't need to know the index
  889. foreach($this->organizations as $id => $value){
  890. $title = $this->organizations[$id]->get_name();
  891. break;
  892. }
  893. }
  894. return $title;
  895. }
  896. /**
  897. * //TODO @TODO implement this function to restore items data from an imsmanifest,
  898. * updating the existing table... This will prove very useful in case initial data
  899. * from imsmanifest were not imported well enough
  900. * @param string course Code
  901. * @param string LP ID (in database)
  902. * @param string Manifest file path (optional if lp_id defined)
  903. * @return integer New LP ID or false on failure
  904. * TODO @TODO Implement imsmanifest_path parameter
  905. */
  906. function reimport_manifest($course,$lp_id=null,$imsmanifest_path=''){
  907. if($this->debug>0){error_log('In scorm::reimport_manifest() method',0);}
  908. global $_course;
  909. //RECOVERING PATH FROM DB
  910. $main_table = Database::get_main_table(TABLE_MAIN_COURSE);
  911. //$course = Database::escape_string($course);
  912. $course = $this->escape_string($course);
  913. $sql = "SELECT * FROM $main_table WHERE code = '$course'";
  914. if($this->debug>2){error_log('New LP - scorm::reimport_manifest() '.__LINE__.' - Querying course: '.$sql,0);}
  915. //$res = Database::query($sql);
  916. $res = api_sql_query($sql);
  917. if(Database::num_rows($res)>0)
  918. {
  919. $this->cc = $course;
  920. }
  921. else
  922. {
  923. $this->error = 'Course code does not exist in database ('.$sql.')';
  924. return false;
  925. }
  926. //TODO make it flexible to use any course_code (still using env course code here)
  927. //$lp_table = Database::get_course_table(LEARNPATH_TABLE);
  928. $lp_table = Database::get_course_table('lp');
  929. //$id = Database::escape_integer($id);
  930. $lp_id = $this->escape_string($lp_id);
  931. $sql = "SELECT * FROM $lp_table WHERE id = '$lp_id'";
  932. if($this->debug>2){error_log('New LP - scorm::reimport_manifest() '.__LINE__.' - Querying lp: '.$sql,0);}
  933. //$res = Database::query($sql);
  934. $res = api_sql_query($sql);
  935. if(Database::num_rows($res)>0)
  936. {
  937. $this->lp_id = $lp_id;
  938. $row = Database::fetch_array($res);
  939. $this->type = $row['lp_type'];
  940. $this->name = stripslashes($row['name']);
  941. $this->encoding = $row['default_encoding'];
  942. $this->proximity = $row['content_local'];
  943. $this->maker = $row['content_maker'];
  944. $this->prevent_reinit = $row['prevent_reinit'];
  945. $this->license = $row['content_license'];
  946. $this->scorm_debug = $row['debug'];
  947. $this->js_lib = $row['js_lib'];
  948. $this->path = $row['path'];
  949. if($this->type == 2){
  950. if($row['force_commit'] == 1){
  951. $this->force_commit = true;
  952. }
  953. }
  954. $this->mode = $row['default_view_mod'];
  955. $this->subdir = $row['path'];
  956. }
  957. //parse the manifest (it is already in this lp's details)
  958. $manifest_file = api_get_path('SYS_COURSE_PATH').$_course['directory'].'/scorm/'.$this->subdir.'/imsmanifest.xml';
  959. if($this->subdir == ''){
  960. $manifest_file = api_get_path('SYS_COURSE_PATH').$_course['directory'].'/scorm/imsmanifest.xml';
  961. }
  962. echo $manifest_file;
  963. if(is_file($manifest_file) && is_readable($manifest_file)){
  964. //re-parse the manifest file
  965. if($this->debug>1){error_log('New LP - In scorm::reimport_manifest() - Parsing manifest '.$manifest_file,0);}
  966. $manifest = $this->parse_manifest($manifest_file);
  967. //import new LP in DB (ignore the current one)
  968. if($this->debug>1){error_log('New LP - In scorm::reimport_manifest() - Importing manifest '.$manifest_file,0);}
  969. $this->import_manifest(api_get_course_id());
  970. }else{
  971. if($this->debug>0){error_log('New LP - In scorm::reimport_manifest() - Could not find manifest file at '.$manifest_file,0);}
  972. }
  973. return false;
  974. }
  975. }
  976. ?>