scorm.class.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. // scorm dir web path starting from /courses
  489. $course_rel_dir = api_get_course_path().'/scorm';
  490. $course_sys_dir = api_get_path(SYS_COURSE_PATH).$course_rel_dir; // Absolute system path for this course.
  491. if (!is_dir($course_sys_dir)) {
  492. mkdir($course_sys_dir, api_get_permissions_for_new_directories());
  493. }
  494. $current_dir = api_replace_dangerous_char(trim($current_dir), 'strict'); // Current dir we are in, inside scorm/
  495. if ($this->debug > 1) {
  496. error_log('New LP - import_package() - current_dir = '.$current_dir, 0);
  497. }
  498. //$uploaded_filename = $_FILES['userFile']['name'];
  499. // Get name of the zip file without the extension.
  500. if ($this->debug > 1) {
  501. error_log('New LP - Received zip file name: '.$zip_file_path, 0);
  502. }
  503. $file_info = pathinfo($zip_file_name);
  504. $filename = $file_info['basename'];
  505. $extension = $file_info['extension'];
  506. $file_base_name = str_replace('.'.$extension, '', $filename); // Filename without its extension.
  507. $this->zipname = $file_base_name; // Save for later in case we don't have a title.
  508. if ($this->debug > 1) {
  509. error_log("New LP - base file name is : ".$file_base_name, 0);
  510. }
  511. $new_dir = api_replace_dangerous_char(trim($file_base_name), 'strict');
  512. $this->subdir = $new_dir;
  513. if ($this->debug > 1) {
  514. error_log("New LP - subdir is first set to : ".$this->subdir, 0);
  515. }
  516. $zipFile = new PclZip($zip_file_path);
  517. // Check the zip content (real size and file extension).
  518. $zipContentArray = $zipFile->listContent();
  519. $package_type = '';
  520. $at_root = false;
  521. $manifest = '';
  522. $realFileSize = 0;
  523. $manifest_list = array();
  524. // The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
  525. foreach ($zipContentArray as $thisContent) {
  526. $file = $thisContent['filename'];
  527. //error_log('Looking at '.$thisContent['filename'], 0);
  528. if (preg_match('~.(php.*|phtml)$~i', $file)) {
  529. $this->set_error_msg("File $file contains a PHP script");
  530. //return api_failure::set_failure('php_file_in_zip_file');
  531. } elseif (stristr($thisContent['filename'], 'imsmanifest.xml')) {
  532. //error_log('Found imsmanifest at '.$thisContent['filename'], 0);
  533. if ($thisContent['filename'] == basename($thisContent['filename'])) {
  534. $at_root = true;
  535. } else {
  536. //$this->subdir .= '/'.dirname($thisContent['filename']);
  537. if ($this->debug > 2) {
  538. error_log("New LP - subdir is now ".$this->subdir, 0);
  539. }
  540. }
  541. $package_type = 'scorm';
  542. $manifest_list[] = $thisContent['filename'];
  543. $manifest = $thisContent['filename']; //just the relative directory inside scorm/
  544. } else {
  545. // Do nothing, if it has not been set as scorm somewhere else, it stays as '' default.
  546. }
  547. $realFileSize += $thisContent['size'];
  548. }
  549. // Now get the shortest path (basically, the imsmanifest that is the closest to the root).
  550. $shortest_path = $manifest_list[0];
  551. $slash_count = substr_count($shortest_path, '/');
  552. foreach ($manifest_list as $manifest_path) {
  553. $tmp_slash_count = substr_count($manifest_path, '/');
  554. if ($tmp_slash_count < $slash_count) {
  555. $shortest_path = $manifest_path;
  556. $slash_count = $tmp_slash_count;
  557. }
  558. }
  559. $this->subdir .= '/'.dirname($shortest_path); // Do not concatenate because already done above.
  560. $manifest = $shortest_path;
  561. if ($this->debug > 1) {
  562. error_log('New LP - Package type is now '.$package_type, 0);
  563. }
  564. // && defined('CHECK_FOR_SCORM') && CHECK_FOR_SCORM)
  565. if ($package_type == '') {
  566. if ($this->debug > 1) {
  567. error_log('New LP - Package type is empty', 0);
  568. }
  569. return api_failure::set_failure('not_scorm_content');
  570. }
  571. // It happens on Linux that $new_dir sometimes doesn't start with '/'
  572. if ($new_dir[0] != '/') {
  573. $new_dir = '/'.$new_dir;
  574. }
  575. if ($new_dir[strlen($new_dir) - 1] == '/') {
  576. $new_dir = substr($new_dir, 0, -1);
  577. }
  578. $isDir = is_dir($course_sys_dir.$new_dir);
  579. if ($isDir == false) {
  580. mkdir($course_sys_dir.$new_dir, api_get_permissions_for_new_directories());
  581. $isDir = is_dir($course_sys_dir.$new_dir);
  582. }
  583. /* Uncompressing phase */
  584. /*
  585. We need to process each individual file in the zip archive to
  586. - add it to the database
  587. - parse & change relative html links
  588. - make sure the filenames are secure (filter funny characters or php extensions)
  589. */
  590. if ($isDir) {
  591. if (!FileManager::enough_size($realFileSize, $course_sys_dir, $maxFilledSpace)) {
  592. if ($this->debug > 1) {
  593. error_log('New LP - Not enough space to store package', 0);
  594. }
  595. return api_failure::set_failure('not_enough_space');
  596. }
  597. // PHP method - slower...
  598. if ($this->debug >= 1) {
  599. error_log('New LP - Changing dir to '.$course_sys_dir.$new_dir, 0);
  600. }
  601. $saved_dir = getcwd();
  602. chdir($course_sys_dir.$new_dir);
  603. $unzippingState = $zipFile->extract();
  604. for ($j = 0; $j < count($unzippingState); $j++) {
  605. $state = $unzippingState[$j];
  606. // TODO: Fix relative links in html files (?)
  607. $extension = strrchr($state['stored_filename'], '.');
  608. if ($this->debug >= 1) {
  609. error_log('New LP - found extension '.$extension.' in '.$state['stored_filename'], 0);
  610. }
  611. }
  612. if (!empty($new_dir)) {
  613. $new_dir = $new_dir.'/';
  614. }
  615. // Rename files, for example with \\ in it.
  616. if ($this->debug >= 1) {
  617. error_log('New LP - try to open: '.$course_sys_dir.$new_dir, 0);
  618. }
  619. if ($dir = @opendir($course_sys_dir.$new_dir)) {
  620. if ($this->debug >= 1) {
  621. error_log('New LP - Opened dir '.$course_sys_dir.$new_dir, 0);
  622. }
  623. while ($file = readdir($dir)) {
  624. if ($file != '.' && $file != '..') {
  625. $filetype = 'file';
  626. if (is_dir($course_sys_dir.$new_dir.$file)) {
  627. $filetype = 'folder';
  628. }
  629. // TODO: RENAMING FILES CAN BE VERY DANGEROUS SCORM-WISE, avoid that as much as possible!
  630. //$safe_file = replace_dangerous_char($file, 'strict');
  631. $find_str = array('\\', '.php', '.phtml');
  632. $repl_str = array('/', '.txt', '.txt');
  633. $safe_file = str_replace($find_str, $repl_str, $file);
  634. if ($this->debug >= 1) {
  635. error_log('Comparing: '.$safe_file, 0);
  636. }
  637. if ($this->debug >= 1) {
  638. error_log('and: '.$file, 0);
  639. }
  640. if ($safe_file != $file) {
  641. $mydir = dirname($course_sys_dir.$new_dir.$safe_file);
  642. if (!is_dir($mydir)) {
  643. $mysubdirs = split('/', $mydir);
  644. $mybasedir = '/';
  645. foreach ($mysubdirs as $mysubdir) {
  646. if (!empty($mysubdir)) {
  647. $mybasedir = $mybasedir.$mysubdir.'/';
  648. if (!is_dir($mybasedir)) {
  649. @mkdir($mybasedir, api_get_permissions_for_new_directories());
  650. if ($this->debug >= 1) {
  651. error_log('New LP - Dir '.$mybasedir.' doesnt exist. Creating.', 0);
  652. }
  653. }
  654. }
  655. }
  656. }
  657. @rename($course_sys_dir.$new_dir.$file, $course_sys_dir.$new_dir.$safe_file);
  658. if ($this->debug >= 1) {
  659. error_log(
  660. 'New LP - Renaming '.$course_sys_dir.$new_dir.$file.' to '.$course_sys_dir.$new_dir.$safe_file,
  661. 0
  662. );
  663. }
  664. }
  665. }
  666. }
  667. closedir($dir);
  668. chdir($saved_dir);
  669. api_chmod_R($course_sys_dir.$new_dir, api_get_permissions_for_new_directories());
  670. if ($this->debug > 1) {
  671. error_log('New LP - changed back to init dir: '.$course_sys_dir.$new_dir, 0);
  672. }
  673. }
  674. } else {
  675. return '';
  676. }
  677. return $course_sys_dir.$new_dir.$manifest;
  678. }
  679. /**
  680. * Sets the proximity setting in the database
  681. * @param string Proximity setting
  682. */
  683. function set_proximity($proxy = '')
  684. {
  685. $course_id = api_get_course_int_id();
  686. if ($this->debug > 0) {
  687. error_log('In scorm::set_proximity('.$proxy.') method', 0);
  688. }
  689. $lp = $this->get_id();
  690. if ($lp != 0) {
  691. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  692. $sql = "UPDATE $tbl_lp SET content_local = '$proxy' WHERE c_id = ".$course_id." AND id = ".$lp;
  693. $res = Database::query($sql);
  694. return $res;
  695. } else {
  696. return false;
  697. }
  698. }
  699. /**
  700. * Sets the theme setting in the database
  701. * @param string theme setting
  702. */
  703. function set_theme($theme = '')
  704. {
  705. $course_id = api_get_course_int_id();
  706. if ($this->debug > 0) {
  707. error_log('In scorm::set_theme('.$theme.') method', 0);
  708. }
  709. $lp = $this->get_id();
  710. if ($lp != 0) {
  711. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  712. $sql = "UPDATE $tbl_lp SET theme = '$theme' WHERE c_id = ".$course_id." AND id = ".$lp;
  713. $res = Database::query($sql);
  714. return $res;
  715. } else {
  716. return false;
  717. }
  718. }
  719. /**
  720. * Sets the image setting in the database
  721. * @param string preview_image setting
  722. */
  723. function set_preview_image($preview_image = '')
  724. {
  725. $course_id = api_get_course_int_id();
  726. if ($this->debug > 0) {
  727. error_log('In scorm::set_theme('.$preview_image.') method', 0);
  728. }
  729. $lp = $this->get_id();
  730. if ($lp != 0) {
  731. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  732. $sql = "UPDATE $tbl_lp SET preview_image = '$preview_image' WHERE c_id = ".$course_id." AND id = ".$lp;
  733. $res = Database::query($sql);
  734. return $res;
  735. } else {
  736. return false;
  737. }
  738. }
  739. /**
  740. * Sets the author setting in the database
  741. * @param string preview_image setting
  742. */
  743. function set_author($author = '')
  744. {
  745. $course_id = api_get_course_int_id();
  746. if ($this->debug > 0) {
  747. error_log('In scorm::set_author('.$author.') method', 0);
  748. }
  749. $lp = $this->get_id();
  750. if ($lp != 0) {
  751. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  752. $sql = "UPDATE $tbl_lp SET author = '$author' WHERE c_id = ".$course_id." AND id = ".$lp;
  753. $res = Database::query($sql);
  754. return $res;
  755. } else {
  756. return false;
  757. }
  758. }
  759. /**
  760. * Sets the content maker setting in the database
  761. * @param string Proximity setting
  762. */
  763. function set_maker($maker = '')
  764. {
  765. $course_id = api_get_course_int_id();
  766. if ($this->debug > 0) {
  767. error_log('In scorm::set_maker method('.$maker.')', 0);
  768. }
  769. $lp = $this->get_id();
  770. if ($lp != 0) {
  771. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  772. $sql = "UPDATE $tbl_lp SET content_maker = '$maker' WHERE c_id = ".$course_id." AND id = ".$lp;
  773. $res = Database::query($sql);
  774. return $res;
  775. } else {
  776. return false;
  777. }
  778. }
  779. /**
  780. * Exports the current SCORM object's files as a zip. Excerpts taken from learnpath_functions.inc.php::exportpath()
  781. * @param integer Learnpath ID (optional, taken from object context if not defined)
  782. */
  783. function export_zip($lp_id = null)
  784. {
  785. if ($this->debug > 0) {
  786. error_log('In scorm::export_zip method('.$lp_id.')', 0);
  787. }
  788. if (empty($lp_id)) {
  789. if (!is_object($this)) {
  790. return false;
  791. } else {
  792. $id = $this->get_id();
  793. if (empty($id)) {
  794. return false;
  795. } else {
  796. $lp_id = $this->get_id();
  797. }
  798. }
  799. }
  800. //error_log('New LP - in export_zip()',0);
  801. //zip everything that is in the corresponding scorm dir
  802. //write the zip file somewhere (might be too big to return)
  803. require_once 'learnpath_functions.inc.php';
  804. $course_id = api_get_course_int_id();
  805. $tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
  806. $_course = api_get_course_info(api_get_course_id());
  807. $sql = "SELECT * FROM $tbl_lp WHERE c_id = ".$course_id." AND id=".$lp_id;
  808. $result = Database::query($sql);
  809. $row = Database::fetch_array($result);
  810. $LPname = $row['path'];
  811. $list = split('/', $LPname);
  812. $LPnamesafe = $list[0];
  813. //$zipfoldername = '/tmp';
  814. //$zipfoldername = '../../courses/'.$_course['directory'].'/temp/'.$LPnamesafe;
  815. $zipfoldername = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/temp/'.$LPnamesafe;
  816. $scormfoldername = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/scorm/'.$LPnamesafe;
  817. $zipfilename = $zipfoldername.'/'.$LPnamesafe.'.zip';
  818. // Get a temporary dir for creating the zip file.
  819. //error_log('New LP - cleaning dir '.$zipfoldername, 0);
  820. deldir($zipfoldername); // Make sure the temp dir is cleared.
  821. $res = mkdir($zipfoldername, api_get_permissions_for_new_directories());
  822. //error_log('New LP - made dir '.$zipfoldername, 0);
  823. // Create zipfile of given directory.
  824. $zip_folder = new PclZip($zipfilename);
  825. $zip_folder->create($scormfoldername.'/', PCLZIP_OPT_REMOVE_PATH, $scormfoldername.'/');
  826. //$zipfilename = '/var/www/chamilo/courses/TEST2/scorm/example_document.html';
  827. //This file sending implies removing the default mime-type from php.ini
  828. //DocumentManager :: file_send_for_download($zipfilename, true, $LPnamesafe.'.zip');
  829. DocumentManager :: file_send_for_download($zipfilename, true);
  830. // Delete the temporary zip file and directory in fileManager.lib.php
  831. FileManager::my_delete($zipfilename);
  832. FileManager::my_delete($zipfoldername);
  833. return true;
  834. }
  835. /**
  836. * Gets a resource's path if available, otherwise return empty string
  837. * @param string Resource ID as used in resource array
  838. * @return string The resource's path as declared in imsmanifest.xml
  839. */
  840. function get_res_path($id)
  841. {
  842. if ($this->debug > 0) {
  843. error_log('In scorm::get_res_path('.$id.') method', 0);
  844. }
  845. $path = '';
  846. if (isset($this->resources[$id])) {
  847. $oRes =& $this->resources[$id];
  848. $path = @$oRes->get_path();
  849. }
  850. return $path;
  851. }
  852. /**
  853. * Gets a resource's type if available, otherwise return empty string
  854. * @param string Resource ID as used in resource array
  855. * @return string The resource's type as declared in imsmanifest.xml
  856. */
  857. function get_res_type($id)
  858. {
  859. if ($this->debug > 0) {
  860. error_log('In scorm::get_res_type('.$id.') method', 0);
  861. }
  862. $type = '';
  863. if (isset($this->resources[$id])) {
  864. $oRes =& $this->resources[$id];
  865. $temptype = $oRes->get_scorm_type();
  866. if (!empty($temptype)) {
  867. $type = $temptype;
  868. }
  869. }
  870. return $type;
  871. }
  872. /**
  873. * Gets the default organisation's title
  874. * @return string The organization's title
  875. */
  876. function get_title()
  877. {
  878. if ($this->debug > 0) {
  879. error_log('In scorm::get_title() method', 0);
  880. }
  881. $title = '';
  882. if (isset($this->manifest['organizations']['default'])) {
  883. $title = $this->organizations[$this->manifest['organizations']['default']]->get_name();
  884. } elseif (count($this->organizations) == 1) {
  885. // This will only get one title but so we don't need to know the index.
  886. foreach ($this->organizations as $id => $value) {
  887. $title = $this->organizations[$id]->get_name();
  888. break;
  889. }
  890. }
  891. return $title;
  892. }
  893. /**
  894. * // TODO @TODO Implement this function to restore items data from an imsmanifest,
  895. * updating the existing table... This will prove very useful in case initial data
  896. * from imsmanifest were not imported well enough
  897. * @param string course Code
  898. * @param string LP ID (in database)
  899. * @param string Manifest file path (optional if lp_id defined)
  900. * @return integer New LP ID or false on failure
  901. * TODO @TODO Implement imsmanifest_path parameter
  902. */
  903. function reimport_manifest($course, $lp_id = null, $imsmanifest_path = '')
  904. {
  905. if ($this->debug > 0) {
  906. error_log('In scorm::reimport_manifest() method', 0);
  907. }
  908. $_course = api_get_course_info();
  909. // RECOVERING PATH FROM DB
  910. $main_table = Database::get_main_table(TABLE_MAIN_COURSE);
  911. $course = Datbase::escape_string($course);
  912. $sql = "SELECT * FROM $main_table WHERE code = '$course'";
  913. if ($this->debug > 2) {
  914. error_log('New LP - scorm::reimport_manifest() '.__LINE__.' - Querying course: '.$sql, 0);
  915. }
  916. //$res = Database::query($sql);
  917. $res = Database::query($sql);
  918. if (Database::num_rows($res) > 0) {
  919. $this->cc = $course;
  920. } else {
  921. $this->error = 'Course code does not exist in database ('.$sql.')';
  922. return false;
  923. }
  924. // TODO: Make it flexible to use any course_code (still using env course code here)
  925. //$lp_table = Database::get_course_table(LEARNPATH_TABLE);
  926. $course_id = api_get_course_int_id();
  927. $lp_table = Database::get_course_table(TABLE_LP_MAIN);
  928. $lp_id = intval($lp_id);
  929. $sql = "SELECT * FROM $lp_table WHERE c_id = ".$course_id." AND id = '$lp_id'";
  930. if ($this->debug > 2) {
  931. error_log('New LP - scorm::reimport_manifest() '.__LINE__.' - Querying lp: '.$sql, 0);
  932. }
  933. $res = Database::query($sql);
  934. if (Database::num_rows($res) > 0) {
  935. $this->lp_id = $lp_id;
  936. $row = Database::fetch_array($res);
  937. $this->type = $row['lp_type'];
  938. $this->name = stripslashes($row['name']);
  939. $this->encoding = $row['default_encoding'];
  940. $this->proximity = $row['content_local'];
  941. $this->maker = $row['content_maker'];
  942. $this->prevent_reinit = $row['prevent_reinit'];
  943. $this->license = $row['content_license'];
  944. $this->scorm_debug = $row['debug'];
  945. $this->js_lib = $row['js_lib'];
  946. $this->path = $row['path'];
  947. if ($this->type == 2) {
  948. if ($row['force_commit'] == 1) {
  949. $this->force_commit = true;
  950. }
  951. }
  952. $this->mode = $row['default_view_mod'];
  953. $this->subdir = $row['path'];
  954. }
  955. // Parse the manifest (it is already in this lp's details).
  956. $manifest_file = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/scorm/'.$this->subdir.'/imsmanifest.xml';
  957. if ($this->subdir == '') {
  958. $manifest_file = api_get_path(SYS_COURSE_PATH).$_course['directory'].'/scorm/imsmanifest.xml';
  959. }
  960. echo $manifest_file;
  961. if (is_file($manifest_file) && is_readable($manifest_file)) {
  962. // Re-parse the manifest file.
  963. if ($this->debug > 1) {
  964. error_log('New LP - In scorm::reimport_manifest() - Parsing manifest '.$manifest_file, 0);
  965. }
  966. $manifest = $this->parse_manifest($manifest_file);
  967. // Import new LP in DB (ignore the current one).
  968. if ($this->debug > 1) {
  969. error_log('New LP - In scorm::reimport_manifest() - Importing manifest '.$manifest_file, 0);
  970. }
  971. $this->import_manifest(api_get_course_id());
  972. } else {
  973. if ($this->debug > 0) {
  974. error_log(
  975. 'New LP - In scorm::reimport_manifest() - Could not find manifest file at '.$manifest_file,
  976. 0
  977. );
  978. }
  979. }
  980. return false;
  981. }
  982. }