scorm.class.php 46 KB

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