scorm.class.php 45 KB

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