scorm.class.php 46 KB

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