update-db-scorm-1.6.x-1.8.0.inc.php 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Chamilo LMS
  5. * Script handling the migration between an old Dokeos platform (<1.8.0) to
  6. * setup the new database system (4 scorm tables inside the course's database)
  7. * @package chamilo.scorm
  8. * @author Yannick Warnier <ywarnier@beeznest.org>
  9. */
  10. /**
  11. * Include mandatory libraries
  12. */
  13. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  14. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php'; //check_name_exists()
  15. require_once api_get_path(SYS_CODE_PATH).'newscorm/learnpath.class.php';
  16. require_once api_get_path(SYS_CODE_PATH).'newscorm/scorm.class.php';
  17. $loglevel = 0;
  18. // Get table prefix from $prefix variable declared in update-db-....inc.php
  19. $table_prefix = $prefix;
  20. $sys_course_path = $pathForm.'courses/';
  21. $upd_course_path = $proposedUpdatePath.'courses/';
  22. function my_get_time($time) {
  23. $matches = array();
  24. if (preg_match('/(\d{1,4}):(\d{2})(:(\d{2})(\.\d*)?)?/', $time, $matches)) {
  25. if (count($matches) == 3) {
  26. return ($matches[1] * 60) + ($matches[2]);
  27. } else {
  28. return ($matches[1] * 3600) + ($matches[2] * 60) + ($matches[4]);
  29. }
  30. }
  31. else return 0;
  32. }
  33. // Open log file
  34. $fh = fopen('../../archive/newscorm_'.time().'.log', 'w');
  35. $fh_revert = fopen('../../archive/newscorm_'.time().'_revert.log', 'w');
  36. $fh_res = fopen('../../archive/newscorm_'.time().'_res.log', 'w');
  37. fwrite($fh, "-- Recording course homepages links changes to enable reverting\n");
  38. fwrite($fh_revert, "-- Recording reverted course homepages links changes to enable reverting\n");
  39. fwrite($fh_res, "-- Recording resulting course homepages links changes\n");
  40. //echo "<html><body>";
  41. /**
  42. * New tables definition:
  43. */
  44. $new_lp = 'lp';
  45. $new_lp_view = 'lp_view';
  46. $new_lp_item = 'lp_item';
  47. $new_lp_item_view = 'lp_item_view';
  48. $new_lp_type = 'lp_type';
  49. $max_dsp_lp = 0;
  50. $courses_list = array();
  51. $courses_id_list = array();
  52. $courses_id_full_table_prefix_list = array();
  53. $courses_dir_list = array();
  54. Database::select_db($dbNameForm);
  55. $sql = "SELECT * FROM course";
  56. $res = Database::query($sql);
  57. while ($row = Database::fetch_array($res)) {
  58. $course_pref = $table_prefix;
  59. if ($singleDbForm) {
  60. $dbname = $_configuration['main_database'].'.'.$course_pref.$row['db_name'].'_';
  61. } else {
  62. $dbname = $row['db_name'].'.'.$course_pref;
  63. }
  64. $courses_list[] = $row['db_name'];
  65. $courses_id_list[$row['code']] = $row['db_name'];
  66. $courses_id_full_table_prefix_list[$row['code']] = $dbname;
  67. $courses_dir_list[$row['code']] = $row['directory'];
  68. }
  69. if ($loglevel > 0) { error_log("Tables created/deleted for all courses", 0); }
  70. /**
  71. * The migration needs to take all data from the original learnpath tables and add them to the new
  72. * lp, lp_view, lp_item and lp_item_view tables
  73. */
  74. // MIGRATING LEARNPATHS
  75. // Test only one course
  76. foreach ($courses_id_full_table_prefix_list as $course_code => $db) {
  77. if (strlen($courses_id_list[$course_code]) > 40) {
  78. error_log('Database '.$courses_id_list[$course_code].' is too long, skipping', 0);
  79. continue;
  80. }
  81. $incoherences = 0;
  82. if ($loglevel > 0) { error_log("Now starting migration of learnpath tables from $db database...", 0); }
  83. $lp_doc = $db.TABLE_DOCUMENT;
  84. $lp_main = $db.TABLE_LEARNPATH_MAIN;
  85. $lp_ids = array();
  86. $lp_user = $db.TABLE_LEARNPATH_USER;
  87. $lp_users = array();
  88. $lp_chap = $db.TABLE_LEARNPATH_CHAPTER;
  89. $parent_chaps = array();
  90. $lp_chap_items = array();
  91. $ordered_chaps = array();
  92. $lp_item = $db.TABLE_LEARNPATH_ITEM;
  93. $lp_items = array();
  94. $lp_ordered_items = array();
  95. $parent_lps = array(); //keeps a track of chapter's learnpath ids
  96. $my_new_lp = $db.$new_lp;
  97. $my_new_lp_item = $db.$new_lp_item;
  98. $my_new_lp_view = $db.$new_lp_view;
  99. $my_new_lp_item_view = $db.$new_lp_item_view;
  100. // Migrate learnpaths
  101. $sql_test = "SELECT * FROM $my_new_lp";
  102. $res_test = Database::query($sql_test);
  103. $sql_lp = "SELECT * FROM $lp_main";
  104. if ($loglevel > 1) { error_log("$sql_lp", 0); }
  105. $res_lp = Database::query($sql_lp);
  106. if (!$res_lp or !$res_test) {
  107. if ($loglevel > 1) {
  108. error_log("+++Problem querying DB $lp_main+++ skipping (".Database::error().")", 0);
  109. if (!$res_test) {
  110. error_log("This might be due to no existing table in the destination course", 0);
  111. }
  112. }
  113. continue;
  114. }
  115. $dsp_ord = 1;
  116. while ($row = Database::fetch_array($res_lp)) {
  117. //echo "Treating lp id : ".$row['learnpath_id']."<br />\n";
  118. $ins_lp_sql = "INSERT INTO $my_new_lp (lp_type,name,description,display_order,content_maker) " .
  119. "VALUES (1," .
  120. "'".Database::escape_string($row['learnpath_name'])."'," .
  121. "'".Database::escape_string($row['learnpath_description'])."',$dsp_ord,'Dokeos')";
  122. $ins_lp_res = Database::query($ins_lp_sql);
  123. $in_id = Database::insert_id();
  124. if (empty($in_id) or $in_id == false) die('Could not insert lp: '.$ins_lp_sql);
  125. $lp_ids[$row['learnpath_id']] = $in_id;
  126. $dsp_ord++;
  127. $max_dsp_lp = $dsp_ord;
  128. }
  129. //echo "<pre>lp_ids:".print_r($lp_ids,true)."</pre>\n";
  130. // MIGRATING LEARNPATH CHAPTERS
  131. $sql_lp_chap = "ALTER TABLE $lp_chap ADD INDEX ( parent_chapter_id, display_order )";
  132. $res_lp_chap = Database::query($sql_lp_chap);
  133. $sql_lp_chap = "SELECT * FROM $lp_chap ORDER BY parent_chapter_id, display_order";
  134. //echo "$sql_lp_chap<br />\n";
  135. $res_lp_chap = Database::query($sql_lp_chap);
  136. while ($row = Database::fetch_array($res_lp_chap)) {
  137. //echo "Treating chapter id : ".$row['id']."<br />\n";
  138. //TODO: Build path for this chapter (although there is no real path for any chapter)
  139. //TODO: Find out how to calculate the "next_item_id" with the "ordre" field
  140. $my_lp_item = $my_new_lp_item;
  141. $myname = Database::escape_string($row['chapter_name']);
  142. $mydesc = Database::escape_string($row['chapter_description']);
  143. $ins_lp_sql = "INSERT INTO $my_new_lp_item (" .
  144. "lp_id," .
  145. "item_type," .
  146. "title," .
  147. "description," .
  148. "path, " .
  149. "display_order, " .
  150. "next_item_id) " .
  151. "VALUES (" .
  152. "'".$lp_ids[$row['learnpath_id']]."'," . //insert new learnpath ID
  153. "'dokeos_chapter'," .
  154. "'".$myname."'," .
  155. "'".$mydesc."'," .
  156. "''," .
  157. $row['display_order'].", " .
  158. "123456" .
  159. ")";
  160. //echo $ins_lp_sql."<br />\n";
  161. $ins_res = Database::query($ins_lp_sql);
  162. $in_id = Database::insert_id();
  163. //echo "&nbsp;&nbsp;Inserted item $in_id<br />\n";
  164. if (empty($in_id) OR $in_id == false) die('Could not insert lp: '.$ins_sql);
  165. $parent_chaps[$row['id']] = $row['parent_chapter_id'];
  166. $lp_chap_items[$row['id']] = $in_id;
  167. $parent_lps[$row['id']] = $row['learnpath_id'];
  168. $ordered_chaps[$row['parent_chapter_id']][$row['display_order']] = $in_id;
  169. $lp_chaps_list[$row['learnpath_id']][] = $in_id;
  170. }
  171. //echo "<pre>parent_lps:".print_r($parent_lps,true)."</pre>\n";
  172. // Now one loop to update the parent_chapter_ids
  173. foreach ($parent_chaps as $old_chap => $old_parent_chap) {
  174. if ($old_parent_chap != 0) {
  175. $new_chap = $lp_chap_items[$old_chap];
  176. $new_parent = $lp_chap_items[$old_parent_chap];
  177. if (isset($new_chap) && $new_chap != '' && isset($new_parent) && $new_parent != '') {
  178. $sql_par_chap = "UPDATE $my_new_lp_item " .
  179. "SET parent_item_id = $new_parent " .
  180. "WHERE id = $new_chap";
  181. $res_par_chap = Database::query($sql_par_chap);
  182. }
  183. }
  184. }
  185. unset($parent_chaps);
  186. // Now one loop to set the next_item_id and the previous_item_id
  187. foreach ($ordered_chaps as $parent_chap) {
  188. $last = 0;
  189. foreach ($ordered_chaps[$parent_chap] as $order => $new_id) {
  190. $sql_upd_chaps = "UPDATE $my_new_lp_item " .
  191. "SET previous_item_id = $last " .
  192. "WHERE id = $new_id";
  193. $res_upd_chaps = Database::query($sql_upd_chaps);
  194. $next = 0;
  195. if (!empty($ordered_chaps[$parent_chap][$order + 1])) {
  196. $next = $ordered_chaps[$parent_chap][$order + 1];
  197. }
  198. $sql_upd_chaps = "UPDATE $my_new_lp_item " .
  199. "SET next_item_id = $next " .
  200. "WHERE id = $new_id";
  201. $res_upd_chaps = Database::query($sql_upd_chaps);
  202. $last = $new_id;
  203. }
  204. }
  205. unset($ordered_chaps);
  206. // Migrate learnpath_items
  207. // TODO: Define this array thanks to types defined in the learnpath_building scripts
  208. // TODO: Set order correctly
  209. $type_trans = array(
  210. 'document' => TOOL_DOCUMENT,
  211. 'exercise' => TOOL_QUIZ,
  212. 'forum' => TOOL_FORUM,
  213. 'Agenda' => TOOL_CALENDAR_EVENT,
  214. 'Ad_Valvas' => TOOL_ANNOUNCEMENT,
  215. 'Link' => TOOL_LINK,
  216. 'Link _blank' => TOOL_LINK,
  217. 'Exercise' => TOOL_QUIZ,
  218. 'HotPotatoes'=> 'HotPotatoes',
  219. 'Forum' => TOOL_FORUM,
  220. 'Thread' => TOOL_THREAD,
  221. 'Topic' => TOOL_THREAD,
  222. 'Post' => TOOL_POST,
  223. 'Document' => TOOL_DOCUMENT,
  224. 'Assignments'=> 'Assignments',
  225. 'Dropbox' => TOOL_DROPBOX,
  226. 'Introduction_text'=> 'Introduction_text',
  227. 'Course_description' => TOOL_COURSE_DESCRIPTION,
  228. 'Groups' => TOOL_GROUP,
  229. 'Users' => TOOL_USER,
  230. //'chapter' => 'dokeos_chapter', Chapters should all be in learnpath_chapter, no matter the nesting level
  231. );
  232. // MIGRATING LEARNPATH ITEMS
  233. $sql_lp_item = "ALTER TABLE $lp_item ADD INDEX ( chapter_id, display_order)";
  234. $res_lp_item = Database::query($sql_lp_item);
  235. $sql_lp_item = "SELECT * FROM $lp_item ORDER BY chapter_id, display_order";
  236. //echo "$sql_lp_item<br />\n";
  237. $res_lp_item = Database::query($sql_lp_item);
  238. while ($row = Database::fetch_array($res_lp_item)) {
  239. //echo "Treating chapter ".$row['chapter_id'].", item ".$row['id']."<br />\n";
  240. $type = $type_trans[$row['item_type']];
  241. $ref = $row['item_id'];
  242. //TODO: Build item path
  243. //TODO: Calculate "next_item_id" with the "ordre" field
  244. // Prepare prereqs
  245. // Prerequisites in Dokeos 1.6 is only authorised on previous items, so
  246. // We know that we are gonna talk about an item that has already been passed
  247. // through here - if none found, print message
  248. $prereq_id = '';
  249. if (!empty($row['prereq_id'])) {
  250. switch ($row['prereq_type']) {
  251. case 'c':
  252. // chapter-type prereq
  253. $prereq_id = $lp_chap_items[$row['prereq_id']];
  254. if (empty($prereq_id) && $loglevel > 1) { error_log("Could not find prereq chapter ".$row['prereq_id'], 0); }
  255. break;
  256. case 'i':
  257. default:
  258. // item type prereq
  259. $prereq_id = $lp_items[$parent_lps[$row['chapter_id']]][$row['prereq_id']];
  260. if (empty($prereq_id) && $loglevel > 1) { error_log("Could not find prereq item ".$row['prereq_id'], 0); }
  261. break;
  262. }
  263. }
  264. $my_parent_id = 0;
  265. if (isset($lp_chap_items[$row['chapter_id']])) {
  266. $my_parent_id = $lp_chap_items[$row['chapter_id']];
  267. }
  268. $title = '';
  269. if (empty($row['title']) && $type == 'Document' && !empty($row['item_id'])) {
  270. $my_sql_doctitle = "SELECT title FROM $lp_doc WHERE id = ".$row['item_id'];
  271. $my_res_doctitle = Database::query($my_sql_doctitle);
  272. if ($row_doctitle = Database::fetch_array($my_res_doctitle)) {
  273. $title = $row_doctitle['title'];
  274. } else {
  275. $title = '-';
  276. }
  277. } else {
  278. $title = $row['title'];
  279. }
  280. if (isset($lp_ids[$parent_lps[$row['chapter_id']]])) {
  281. // If there is a parent learnpath
  282. $ins_lp_sql = "INSERT INTO $my_new_lp_item (" .
  283. "lp_id," .
  284. "item_type," .
  285. "ref, " .
  286. "title," .
  287. "description," .
  288. "path, " .
  289. "parent_item_id," .
  290. "prerequisite," .
  291. "display_order" .
  292. ") VALUES (" .
  293. "'".$lp_ids[$parent_lps[$row['chapter_id']]]."'," . // Insert new learnpath ID
  294. "'$type'," .
  295. "'$ref', " .
  296. "'".Database::escape_string($row['title'])."'," .
  297. "'".Database::escape_string($row['description'])."'," .
  298. "'$ref'," .
  299. "".$my_parent_id."," .
  300. "'$prereq_id'," .
  301. $row['display_order']." " .
  302. ")";
  303. $ins_res = Database::query($ins_lp_sql);
  304. $in_id = Database::insert_id();
  305. //echo "&nbsp;&nbsp;Inserted item $in_id (".$row['title'].")<br />\n";
  306. if (empty($in_id) OR $in_id == false) die('Could not insert lp_item: '.$ins_sql);
  307. $lp_items[$parent_lps[$row['chapter_id']]][$row['id']] = $in_id;
  308. $lp_ordered_items[$parent_lps[$row['chapter_id']]][$row['chapter_id']][] = $in_id;
  309. }
  310. }
  311. //echo "<pre>lp_items:".print_r($lp_items,true)."</pre>\n";
  312. // Complete next_item_id field by going through the new table and looking at parent_id and display_order
  313. $order_sql = "ALTER TABLE $my_new_lp_item ADD INDEX (lp_id, parent_item_id, display_order)";
  314. $order_res = Database::query($order_sql);
  315. $order_sql = "SELECT * FROM $my_new_lp_item ORDER by lp_id ASC, parent_item_id ASC, display_order ASC";
  316. //echo "$order_sql<br />\n";
  317. $order_res = Database::query($order_sql);
  318. $order_item = array(); //this will contain a sequential list of item_id's, thus allowing to give a simple way to get next id...
  319. $lp_id = 0;
  320. //echo "<pre>";
  321. while ($row = Database::fetch_array($order_res)) {
  322. //print_r($row);
  323. if ($row['lp_id'] != $lp_id) {
  324. // Apply changes to the database and clean tool arrays
  325. $last = 0;
  326. foreach ($order_item as $order_id => $item_id) {
  327. $next = 0;
  328. if (!empty($order_item[$order_id+1])) {
  329. $next = $order_item[$order_id+1];
  330. }
  331. $upd = "UPDATE $my_new_lp_item " .
  332. "SET next_item_id = ".$next."," .
  333. " previous_item_id = ".$last." " .
  334. "WHERE id = ".$item_id;
  335. //echo "$upd<br />\n";
  336. Database::query($upd);
  337. $last = $item_id;
  338. }
  339. $order_item = array();
  340. $lp_id = $row['lp_id'];
  341. $order_item[] = $row['id'];
  342. } else {
  343. $order_item[] = $row['id'];
  344. }
  345. }
  346. // Process the last LP stack
  347. $last = 0;
  348. foreach ($order_item as $order_id => $item_id) {
  349. $next = 0;
  350. if (!empty($order_item[$order_id + 1])) {
  351. $next = $order_item[$order_id + 1];
  352. }
  353. $upd = "UPDATE $my_new_lp_item " .
  354. "SET next_item_id = ".$next."," .
  355. " previous_item_id = ".$last." " .
  356. "WHERE id = ".$item_id;
  357. //echo "$upd<br />\n";
  358. Database::query($upd);
  359. $last = $item_id;
  360. }
  361. //echo "</pre>\n";
  362. // MIGRATING THE learnpath_user TABLE (results)
  363. $mysql = "ALTER TABLE $my_new_lp_item_view ADD INDEX (lp_view_id)";
  364. $myres = Database::query($mysql);
  365. $sql_lp_user = "ALTER TABLE $lp_user ADD INDEX (user_id, learnpath_id, learnpath_item_id)";
  366. $res_lp_user = Database::query($sql_lp_user);
  367. $sql_lp_user = "SELECT * FROM $lp_user ORDER BY user_id, learnpath_id, learnpath_item_id";
  368. //echo "$sql_lp_user<br />\n";
  369. $res_lp_user = Database::query($sql_lp_user);
  370. $user_id = 0;
  371. $learnpath_id = 0;
  372. $lp_view = 0;
  373. while ($row = Database::fetch_array($res_lp_user)) {
  374. if ($row['user_id']!=$user_id OR $row['learnpath_id']!=$learnpath_id) { //the user has changed or this is the first
  375. // Insert a new lp_view
  376. $last = 0;
  377. if (!empty($lp_chaps_list[$row['learnpath_id']][0])) {
  378. $last = $lp_chaps_list[$row['learnpath_id']][0];
  379. }
  380. if (empty($lp_ids[$row['learnpath_id']])) {
  381. // This can be ignored as it means there was an LP before, this user
  382. // used it, but now it's been removed
  383. //echo "Somehow we also miss a lp_ids[".$row['learnpath_id']."] here<br />\n";
  384. $incoherences ++;
  385. } else {
  386. $mylpid = $lp_ids[$row['learnpath_id']];
  387. $sql_ins_view = "INSERT INTO $my_new_lp_view(" .
  388. "lp_id," .
  389. "user_id," .
  390. "view_count," .
  391. "last_item" .
  392. ")VALUES(" .
  393. "".$mylpid."," . // new learnpath id
  394. "".$row['user_id']."," . // user IDs stay the same
  395. "1," .
  396. "".$last."" . // Use the first chapter from this learnpath
  397. ")";
  398. //echo $sql_ins_view;
  399. $res_ins_view = Database::query($sql_ins_view);
  400. $in_id = Database::insert_id();
  401. $user_id = $row['user_id'];
  402. $learnpath_id = $row['learnpath_id'];
  403. $lp_view = $in_id;
  404. }
  405. }
  406. // Insert the record into lp_item_view
  407. // TODO: fix the whole in here (missing one item at least)
  408. $my_new_lp_item_id = $lp_items[$learnpath_id][$row['learnpath_item_id']];
  409. if (empty($my_new_lp_item_id)) {
  410. // This can be ignored safely as it just means a user used a learnpath_item
  411. // before it was removed from items - maybe fix that in Dokeos?
  412. //echo "Somehow we miss lp_items[".$learnpath_id."][".$row['learnpath_item_id']."] here...<br />";
  413. $incoherences ++;
  414. } else {
  415. $start_time = 0;
  416. $my_time = my_get_time($row['time']);
  417. if ($my_time > 0) {
  418. $start_time = time() - $my_time;
  419. }
  420. $sql_ins_iv = "INSERT INTO $my_new_lp_item_view(" .
  421. "lp_item_id," .
  422. "lp_view_id," .
  423. "view_count," .
  424. "start_time," .
  425. "total_time," .
  426. "score," .
  427. "status" .
  428. ")VALUES(" .
  429. "".$lp_items[$learnpath_id][$row['learnpath_item_id']]."," .
  430. "".$lp_view."," .
  431. "1," .
  432. "$start_time," .
  433. "".$my_time."," .
  434. "".$row['score']."," .
  435. "'".$row['status']."'" .
  436. ")";
  437. //echo $sql_ins_iv;
  438. $res_ins_iv = Database::query($sql_ins_iv);
  439. }
  440. // UPDATE THE LP_VIEW progress
  441. $mysql = "SELECT count(distinct(lp_item_id)) FROM $my_new_lp_item_view WHERE lp_view_id = ".$lp_view." AND status IN ('passed','completed','succeeded','browsed','failed')";
  442. $myres = Database::query($mysql);
  443. $myrow = Database::fetch_array($myres);
  444. $completed = $myrow[0];
  445. $mylpid = $lp_ids[$row['learnpath_id']];
  446. $sql = "SELECT count(*) FROM $my_new_lp_item WHERE lp_id = '".$mylpid."'";
  447. $myres = Database::query($sql);
  448. $myrow = Database::fetch_array($myres);
  449. $total = $myrow[0];
  450. $progress = ((float)$completed / (float)$total) * 100;
  451. $progress = number_format($progress, 0);
  452. $sql = "UPDATE $my_new_lp_view SET progress = '$progress' WHERE id = '$lp_view'";
  453. $myres = Database::query($sql);
  454. }
  455. /**
  456. * Move prerequisites
  457. * TODO: Integrate prerequisites migration into learnpath_item migration
  458. */
  459. $msg = '';
  460. if ($incoherences > 0) {
  461. $msg = "(found $incoherences incoherences between views and items - ignored)";
  462. }
  463. /**
  464. * Migrate links on the homepage as well now (look into the TABLE_TOOL_LIST table and
  465. * update the links to newscorm/lp_controller.php?action=view&lp_id=x)
  466. * Only normal learnpaths were visible from the homepage so we only need to update here
  467. */
  468. // MIGRATING LEARNPATH LINKS ON COURSES HOMEPAGES
  469. $tbl_tool = $db.TABLE_TOOL_LIST;
  470. $sql_tool = "SELECT * FROM $tbl_tool WHERE image='scormbuilder.gif' AND link LIKE '%learnpath_handler%'";
  471. $res_tool = Database::query($sql_tool);
  472. while ($row_tool = Database::fetch_array($res_tool)) {
  473. $name = $row_tool['name'];
  474. $link = $row_tool['link'];
  475. // Get old lp_id from there
  476. $matches = array();
  477. if (preg_match('/learnpath_id=(\d+)$/', $link,$matches)) {
  478. $old_lp_id = $matches[1];
  479. $new_lp_id = $lp_ids[$old_lp_id];
  480. $sql_tool_upd = "UPDATE $tbl_tool " .
  481. "SET link='newscorm/lp_controller.php?action=view&lp_id=$new_lp_id' " .
  482. "WHERE id = ".$row_tool['id'];
  483. error_log('New LP - Migration - Updating tool table: '.$sql_tool_upd, 0);
  484. // Make sure there is a way of retrieving which links were updated (to revert)
  485. fwrite($fh,$sql_tool_upd." AND link ='$link'");
  486. fwrite($fh_revert, "UPDATE $tbl_tool SET link='$link' WHERE id=".$row_tool['id']." AND link ='newscorm/lp_controller.php?action=view&lp_id=$new_lp_id';\n");
  487. //echo $sql_tool_upd." (and link='$link')<br />\n";
  488. $res_tool_upd = Database::query($sql_tool_upd);
  489. }
  490. }
  491. $tbl_tool = $db.TABLE_TOOL_LIST;
  492. $sql_tool = "SELECT * FROM $tbl_tool";
  493. $res_tool = Database::query($sql_tool);
  494. while ($row_tool = Database::fetch_array($res_tool)) {
  495. $link = $row_tool['link'];
  496. $matches = array();
  497. $pattern = '@scorm/showinframes\.php([^\s"\'&]*)(&|&amp;)file=([^\s"\'&]*)@';
  498. if (preg_match($pattern, $link, $matches)) {
  499. //echo "Found match for scorm link in $tbl_tool: $link<br />";
  500. if (!empty($matches[3]) && (strtolower(substr($matches[3], -15)) == 'imsmanifest.xml') && !is_file(realpath(urldecode($matches[3])))) {
  501. //echo "Removing link $link from tools<br />";
  502. $sql_tool_upd = "DELETE FROM $tbl_tool WHERE id = ".$row_tool['id'];
  503. error_log('New LP - Migration - Updating tool table (dead link): '.$sql_tool_upd, 0);
  504. // Make sure there is a way of retrieving which links were updated (to revert)
  505. fwrite($fh, $sql_tool_upd." AND link ='$link'");
  506. fwrite($fh_revert, "INSERT INTO $tbl_tool (link) VALUES ('$link');\n");
  507. //echo $sql_tool_upd." (and link='$link')<br />\n";
  508. $res_tool_upd = Database::query($sql_tool_upd);
  509. }
  510. } else {
  511. //echo "No scorm link found in tool link $link<br />";
  512. }
  513. //flush();
  514. }
  515. /**
  516. * Update course description (intro page) to use new links instead of learnpath/learnpath_handler.php
  517. */
  518. $tbl_intro = $db.TABLE_TOOL_INTRO;
  519. $sql_i = "SELECT * FROM $tbl_intro WHERE id='course_homepage'";
  520. $res_i = Database::query($sql_i);
  521. //$link_to_course1 = 'scorm/scormdocument.php';
  522. while ($row_i = Database::fetch_array($res_i)) {
  523. $intro = $row_i['intro_text'];
  524. $update = 0;
  525. $out = array();
  526. if (preg_match_all('/claroline\/learnpath\/showinframes\.php([^\s"\']*)learnpath_id=(\d*)/', $intro, $out, PREG_SET_ORDER)) {
  527. foreach ($out as $results) {
  528. //echo "---> replace ".'/learnpath\/showinframes\.php([^\s"\']*)learnpath_id='.$results[2].'/ by newscorm/lp_controller.php'.$results[1].'action=view&lp_id='.$lp_ids[$results[2]];
  529. $intro = preg_replace('/claroline\/learnpath\/showinframes\.php([^\s"\']*)learnpath_id='.$results[2].'/','main/newscorm/lp_controller.php'.$results[1].'action=view&lp_id='.$lp_ids[$results[2]], $intro);
  530. }
  531. }
  532. if (preg_match_all('/claroline\/phpbb\/index.php/', $intro, $out, PREG_SET_ORDER)) {
  533. foreach ($out as $results) {
  534. $intro = preg_replace('/claroline\/phpbb\/index\.php([^\s"\']*)/','main/forum/index.php'.$results[1], $intro);
  535. }
  536. }
  537. if ($intrp != $row_i['intro_text']) {
  538. //echo "<pre>Replacing ".$row_i['intro_text']."\n by \n ".$intro."</pre><br />\n";
  539. $sql_upd = "update $tbl_intro set intro_text = '".Database::escape_string($intro)."' WHERE id = 'course_homepage' AND intro_text = '".Database::escape_string($row_i['intro_text'])."'";
  540. //echo $sql_upd."<br />\n";
  541. fwrite($fh,"$sql_upd\n");
  542. fwrite($fh_revert,"UPDATE $tbl_intro set intro_text = '".$row_i['intro_text']."' WHERE id = 'course_homepage' AND intro_text = '$intro';\n");
  543. fwrite($fh_res,"$intro\n");
  544. Database::query($sql_upd);
  545. }
  546. }
  547. if ($loglevel > 0) { error_log("Done!".$msg, 0); }
  548. //flush();
  549. //ob_flush();
  550. }
  551. unset($lp_ids);
  552. unset($lp_users);
  553. unset($parent_chaps);
  554. unset($lp_chap_items);
  555. unset($ordered_chaps);
  556. unset($lp_items);
  557. unset($lp_ordered_items);
  558. unset($parent_lps);
  559. fwrite($fh, "-- Recording course homepages links changes for SCORM to enable reverting\n");
  560. fwrite($fh_revert, "-- Recording reverted course homepages links changes for SCORM to enable reverting\n");
  561. fwrite($fh_res, "-- Recording resulting course homepages links changes for SCORM\n");
  562. /**
  563. * SCORM
  564. * The migration needs to take all data from the scorm.scorm_main and scorm.scorm_sco_data tables
  565. * and add them to the new lp, lp_view, lp_item and lp_item_view tables.
  566. */
  567. if ($loglevel > 0) { error_log("Now starting migration of scorm tables from global SCORM database", 0); }
  568. $scorm_main = $dbScormForm.'.'.TABLE_SCORM_MAIN;
  569. $scorm_item = $dbScormForm.'.'.TABLE_SCORM_SCO_DATA;
  570. //$lp_main = Database::get_course_table(TABLE_LEARNPATH_MAIN,$db);
  571. $course_pref = $table_prefix;
  572. $lp_ids = array();
  573. $lp_item_ids = array();
  574. $lp_item_refs = array();
  575. $lp_course = array();
  576. $lp_course_code = array();
  577. $scorm_lp_paths = array();
  578. // Avoid empty dokeosCourse fields as they potentially break the rest
  579. Database::select_db($dbNameForm);
  580. $course_main = TABLE_MAIN_COURSE;
  581. $sql_crs = "SELECT * FROM $course_main WHERE target_course_code IS NULL";
  582. if ($loglevel > 0) { error_log("$sql_crs", 0); }
  583. $res_crs = Database::query($sql_crs);
  584. $num = Database::num_rows($res_crs);
  585. // Prepare an array that will contain course codes and for each course code a list of lps [by path prefixed by '/']
  586. $scorms = array();
  587. $course_code_swap = '';
  588. $scormdocuments_lps = array();
  589. while ($course_row = Database::fetch_array($res_crs)) {
  590. if ($loglevel > 0) { error_log("Now dealing with course ".$course_row['code']."...", 0); }
  591. // Check the validity of this new course
  592. $my_course_code = $course_row['code'];
  593. // Reinit the scormdocuments list
  594. //$scormdocuments_lps = array();
  595. $db_name = $courses_id_full_table_prefix_list[$my_course_code];
  596. if (strlen($courses_id_list[$course_code]) > 40) {
  597. error_log('Database '.$courses_id_list[$course_code].' is too long, skipping', 0);
  598. continue;
  599. }
  600. //echo "Now processing database $db_name<br />";
  601. $tblscodoc = $db_name.TABLE_SCORMDOC;
  602. $sql_scodoc = "SELECT path FROM $tblscodoc WHERE path IS NOT NULL AND path != ''";
  603. if ($loglevel > 1) { error_log("$sql_scodoc", 0); }
  604. $res_scodoc = Database::query($sql_scodoc);
  605. while ($row_scodoc = Database::fetch_array($res_scodoc)) {
  606. // Check if there's more than one slash in total
  607. if (strpos($row_scodoc['path'], '/', 1) === false) {
  608. $tmp_path = $row_scodoc['path'];
  609. if ($loglevel > 1) { error_log("++Now opening $tmp_path", 0); }
  610. // Add a prefixing slash if there is none
  611. if (substr($tmp_path, 0, 1) != '/') {
  612. $tmp_path = '/'.$tmp_path;
  613. }
  614. // If the path is just a slash, empty it
  615. if ($tmp_path == '/') {
  616. $tmp_path = '';
  617. }
  618. // There is only one 'slash' sign at the beginning, or none at all, so we assume
  619. // it is a main directory that should be taken as path.
  620. $courses_dir = $sys_course_path.''.$courses_dir_list[$my_course_code].'/scorm'.$tmp_path;
  621. if (!is_dir($courses_dir)) {
  622. //echo "Scormdocument path $my_content_id: $tmp_path doesn't exist in ".$sys_course_path.$courses_dir_list[$my_course_code]."/scorm, skipping<br />\n";
  623. continue;
  624. // Avoid if contentTitle is not the name of an existing directory
  625. } elseif (!is_file($courses_dir."/imsmanifest.xml")) {
  626. // If the imsmanifest file was not found there
  627. if ($loglevel > 2) { error_log(" !!imsmanifest.xml not found at scormdocument's $courses_dir/imsmanifest.xml, skipping", 0); }
  628. // Try subdirectories on one level depth
  629. if ($loglevel > 2) { error_log(" Trying subdirectories...", 0); }
  630. $dh = opendir($courses_dir);
  631. while ($entry = readdir($dh)) {
  632. if (substr($entry, 0, 1) != '.') {
  633. if (is_dir($courses_dir."/".$entry)) {
  634. if (is_file($courses_dir."/".$entry."/imsmanifest.xml")) {
  635. if ($loglevel > 2) { error_log(". .. and found $courses_dir/$entry/imsmanifest.xml!", 0); }
  636. if (!in_array($tmp_path."/".$entry."/imsmanifest.xml",$scormdocuments_lps)) {
  637. if ($loglevel > 2){ error_log(" Recording.<br />", 0); }
  638. $scormdocuments_lps[] = $tmp_path."/".$entry;
  639. }
  640. }
  641. }
  642. }
  643. }
  644. } else {
  645. if ($loglevel > 2) { error_log(" Found scormdocument $tmp_path in ".$sys_course_path.$courses_dir_list[$my_course_code]."/scorm, treating it.", 0); }
  646. $scormdocuments_lps[] = $tmp_path;
  647. }
  648. }
  649. }
  650. // Because certain people with no admin skills had fun adding direct links to SCORM
  651. // from the courses introductions, we have to check for SCORM packages from there too...
  652. $tbl_intro = $db_name.TABLE_TOOL_INTRO;
  653. $sql_i = "SELECT * FROM $tbl_intro WHERE id='course_homepage'";
  654. //echo $sql_i;
  655. $res_i = Database::query($sql_i);
  656. //$link_to_course1 = 'scorm/scormdocument.php';
  657. while ($row_scodoc = Database::fetch_array($res_i)) {
  658. $intro = $row_scodoc['intro_text'];
  659. //echo $intro."<br />\n";
  660. $matches = array();
  661. $pattern = '@scorm/showinframes\.php([^\s"\']*)file=([^\s"\'&]*)@';
  662. if (preg_match_all($pattern, $intro, $matches, PREG_SET_ORDER)) {
  663. if (count($matches) < 1) {
  664. // Skip
  665. } else {
  666. //echo "Found matches in $tbl_intro<br />";
  667. foreach ($matches as $match) {
  668. //echo "Found match ".print_r($match,true)."<br />";
  669. $mymatch = urldecode($match[2]);
  670. $mymatch = str_replace($sys_course_path, $upd_course_path, $mymatch);
  671. if (!empty($mymatch) && (strtolower(substr($mymatch, -15)) == 'imsmanifest.xml') && is_file(realpath(urldecode($mymatch)))) {
  672. //echo $mymatch." seems ok<br />";
  673. // Found a new scorm course in the old directory
  674. $courses_dir = $upd_course_path.''.$courses_dir_list[$my_course_code].'/scorm';
  675. // Check if the file is in the current course path, otherwise just forget about it
  676. // as it would be too difficult to migrate
  677. //echo "Comparing $mymatch with $courses_dir<br />";
  678. if (strpos($mymatch, $courses_dir) !== false) {
  679. // Remove the course dir up to /scorm from this path
  680. $entry = substr($mymatch, strlen($courses_dir));
  681. // Remove the /imsmanifest.xml from the end of the path
  682. $entry = substr($entry, 0, -16);
  683. // If $entry was /var/www/dokeos/courses/ABC/scorm/tralala/imsmanifest.xml,
  684. // $entry is now /tralala
  685. //echo "Checking if manifest exists in ".$courses_dir.$entry."/imsmanifest.xml<br />";
  686. if (is_file($courses_dir.$entry."/imsmanifest.xml")) {
  687. //echo "found $courses_dir/$entry/imsmanifest.xml!<br />";
  688. if ($loglevel > 2) { error_log(". .. and found $courses_dir/$entry/imsmanifest.xml!", 0); }
  689. if (!in_array($entry."/imsmanifest.xml", $scormdocuments_lps)) {
  690. if ($loglevel > 2) { error_log(" Recording.<br />", 0); }
  691. //echo "Recording $entry<br />";
  692. $scormdocuments_lps[] = $entry;
  693. }
  694. } else {
  695. //echo "Manifest does not exist in ".$courses_dir.$entry."/imsmanifest.xml<br />";
  696. }
  697. }
  698. }
  699. }
  700. }
  701. }
  702. }
  703. // Prepare the new course's space in the scorms array
  704. $scorms[$my_course_code] = array();
  705. $sql_paths = "SELECT * FROM $scorm_main WHERE dokeosCourse = '".$my_course_code."'";
  706. if ($loglevel > 0) { error_log("$sql_paths", 0); }
  707. $res_paths = Database::query($sql_paths);
  708. $num = Database::num_rows($res_paths);
  709. while ($scorm_row = Database::fetch_array($res_paths)) {
  710. // Check if this is a new course
  711. $my_content_id = $scorm_row['contentId'];
  712. $my_path = $scorm_row['contentTitle'];
  713. if (substr($my_path, 0, 1) != '/') {
  714. $my_path = '/'.$my_path;
  715. }
  716. if ($my_path == '/') {
  717. $my_path = '';
  718. }
  719. if ($loglevel > 1) { error_log("++++Now opening $my_path", 0); }
  720. if (!is_dir($courses_dir = $sys_course_path.''.$courses_dir_list[$my_course_code].'/scorm'.$my_path)) {
  721. if ($loglevel > 1) { error_log("Path $my_content_id: $my_path doesn't exist in ".$sys_course_path.$courses_dir_list[$my_course_code]."/scorm, skipping", 0); }
  722. continue;
  723. // Avoid if contentTitle is not the name of an existing directory
  724. } elseif (!is_file($sys_course_path.$courses_dir_list[$my_course_code].'/scorm'.$my_path."/imsmanifest.xml")) {
  725. if ($loglevel > 1) { error_log("!!imsmanifest.xml not found at ".$sys_course_path.$courses_dir_list[$my_course_code].'/scorm'.$my_path."/imsmanifest.xml, skipping", 0); }
  726. continue;
  727. } else {
  728. if ($loglevel > 1) { error_log("Found $my_path in ".$sys_course_path.$courses_dir_list[$my_course_code]."/scorm".$mypath."/imsmanifest.xml, keeping it.", 0); }
  729. $scorms[$my_course_code][$my_path] = $my_content_id;
  730. }
  731. }
  732. // Check if all the lps from scormdocuments_lps are already in the course array,
  733. // otherwise add them (and set ID of 0 so no tracking will be available)
  734. foreach ($scormdocuments_lps as $path) {
  735. if (!in_array($path,array_keys($scorms[$my_course_code]))) {
  736. // Add it (-1 means no ID)
  737. if ($loglevel > 1) { error_log("** Scormdocument path $path wasn't recorded yet. Added.", 0); }
  738. $scorms[$my_course_code][$path] = -1;
  739. }
  740. }
  741. $course_code_swap = $my_course_code;
  742. unset($scormdocuments_lps);
  743. }
  744. //echo "<pre>courses_id_list: ".print_r($courses_id_list,true)."</pre>\n";
  745. $my_count = 0;
  746. foreach ($scorms as $mycourse => $my_paths) {
  747. $my_count += count($my_paths);
  748. }
  749. if ($loglevel > 0) { error_log("---- Scorms array now contains ".$mycount." paths to migrate. Starting migration...", 0); }
  750. /**
  751. * Looping through the SCO_MAIN table for SCORM learnpath attached to courses
  752. * Order by course to try and reuse the maximum data
  753. */
  754. $i_count = 0;
  755. foreach ($scorms as $my_course_code => $paths_list) {
  756. $max_dsp_lp = 0;
  757. $course_lp_done = array();
  758. $db_name = $courses_id_full_table_prefix_list[$my_course_code];
  759. foreach ($paths_list as $my_path => $old_id) {
  760. if ($loglevel > 1) { error_log("Migrating lp $my_path from course $my_course_code...", 0); }
  761. $i_count ++;
  762. //error_log('New LP - Migration script - Content '.$i_count.' on '.$num.' (course '.$scorm['dokeosCourse'].')',0);
  763. // Check whether there is no embedded learnpaths into other learnpaths (one root-level and another embedded)
  764. $embedded = false;
  765. foreach ($course_lp_done as $tmp_lp) {
  766. if (empty($tmp_lp)) {
  767. $tmp_lp = '/'; // Allows finding the lp as a subitem, otherwise strstr returns false
  768. }
  769. if (strstr($my_path, $tmp_lp) === false) {
  770. // Let it be
  771. } else {
  772. // This lp is embedded inside another lp who's imsmanifest exists, so prevent from parsing
  773. if ($loglevel > 1) { error_log("LP $my_path is embedded into $tmp_lp, ignoring...", 0); }
  774. $embedded = true;
  775. continue;
  776. }
  777. }
  778. if ($embedded) {
  779. continue;
  780. }
  781. $course_lp_done[] = $my_path;
  782. //echo "<pre>scorm row: ".print_r($scorm,true)."</pre>\n";
  783. $my_content_id = $old_id;
  784. $my_path = $my_path;
  785. $my_name = basename($my_path);
  786. if ($loglevel > 1) { error_log("Try importing LP $my_path from imsmanifest first as it is more reliable", 0); }
  787. // Setup the ims path (path to the imsmanifest.xml file)
  788. //echo "Looking for course with code ".$lp_course_code[$my_content_id]." (using $my_content_id)<br />\n";
  789. //$courses_dir = $sys_course_path.$courses_dir_list[$my_course_code];
  790. $courses_dir = $upd_course_path.$courses_dir_list[$my_course_code];
  791. $sco_path_temp = ($my_path == '/') ? '' : $my_path;
  792. $sco_middle_path = (empty($sco_path_temp) ? '' : (substr($sco_path_temp, 0, 1) == '/') ? substr($sco_path_temp, 1).'/' : $sco_path_temp.'/'); //same thing as sco_path_temp but with reversed slashes
  793. $ims = $courses_dir.'/scorm'.$sco_path_temp.'/imsmanifest.xml';
  794. if (is_file($ims)){
  795. //echo "Path $ims exists, importing...(line ".__LINE__.")<br />";
  796. $oScorm = new scorm();
  797. // Check whether imsmanifest.xml exists at this location. If not, ignore the imsmanifest.
  798. // That should have been done before already, now.
  799. if ($loglevel > 1) { error_log("Found imsmanifest ($ims), importing...", 0); }
  800. if (!empty($sco_middle_path)) { $oScorm->subdir = $sco_middle_path; } //this sets the subdir for the scorm package inside the scorm dir
  801. // Parse manifest file
  802. $manifest = $oScorm->parse_manifest($ims);
  803. // The title is already escaped in the method
  804. $oScorm->import_manifest($my_course_code);
  805. //TODO: Add code to update the path in that new lp created, as it probably uses / where
  806. // $sco_path_temp should be used...
  807. $lp_ids[$my_content_id] = $oScorm->lp_id; // Contains the old LP ID => the new LP ID
  808. if ($loglevel > 1) { error_log(" @@@ Created scorm lp ".$oScorm->lp_id." from imsmanifest [".$ims."] in course $my_course_code", 0); }
  809. $lp_course[$my_content_id] = $courses_id_list[$my_course_code]; // Contains the old learnpath ID => the course DB name
  810. $lp_course_code[$my_content_id] = $my_course_code;
  811. $max_dsp_lp++;
  812. /*
  813. * QUERY SCORM ITEMS FROM SCORM_SCO_DATA
  814. * The danger here is that we might have several users for the same data, and so
  815. * we have to avoid entering the same elements twice
  816. */
  817. $sql_items = "SELECT * FROM $scorm_item WHERE contentId = '".$my_content_id."' ORDER BY scoId";
  818. //echo "$sql_items<br />\n";
  819. $res_items = Database::query($sql_items);
  820. while ($scormItem = Database::fetch_array($res_items)) {
  821. $my_sco_id = $scormItem['scoId']; //the index for display??? (check that)
  822. $my_identifier = $scormItem['scoIdentifier']; //the scorm item path/name
  823. $my_title = Database::escape_string($scormItem['scoTitle']);
  824. $my_status = $scormItem['status'];
  825. $my_student = $scormItem['studentId'];
  826. $my_score = $scormItem['score'];
  827. $my_time = my_get_time($scormItem['time']);
  828. $my_type = 'sco';
  829. //$my_item_path = $scorm_lp_paths[$my_content_id]['path'];
  830. $my_item_path = '';
  831. //echo "&nbsp;&nbsp;FOUND item belonging to old learnpath num $my_content_id so belongs to course ".$lp_course[$my_content_id]."<br />\n";
  832. $my_new_lp_item = $db_name.$new_lp_item;
  833. $my_new_lp_view = $db_name.$new_lp_view;
  834. $my_new_lp_item_view = $db_name.$new_lp_item_view;
  835. /*
  836. * Check if a view is needed
  837. */
  838. if ($my_score != '' and $my_status != 'not attempted') {
  839. // It is worth creating an lp_view and an lp_item_view - otherwise not
  840. $sel_sqlb = "SELECT * FROM $my_new_lp_view " .
  841. "WHERE lp_id = ".$lp_ids[$my_content_id]." AND user_id = $my_student";
  842. $sel_resb = Database::query($sel_sqlb);
  843. if (Database::num_rows($sel_resb) > 0) {
  844. // Don't insert
  845. $rowb = Database::fetch_array($sel_resb);
  846. $view_insert_id = $rowb['id'];
  847. } else {
  848. $ins_sql = "INSERT INTO $my_new_lp_view (" .
  849. "lp_id," .
  850. "user_id," .
  851. "view_count" .
  852. ") VALUES (" .
  853. $lp_ids[$my_content_id].", " .
  854. $my_student.", " .
  855. "1" .
  856. ")";
  857. //echo "$ins_sql<br />";
  858. $ins_res = Database::query($ins_sql);
  859. $view_insert_id = Database::insert_id();
  860. }
  861. $sel_sqlc = "SELECT * FROM $my_new_lp_item " .
  862. "WHERE lp_id = ".$lp_ids[$my_content_id]." AND ref = '$my_identifier'";
  863. $sel_resc = Database::query($sel_sqlc);
  864. if (Database::num_rows($sel_resc) > 0) {
  865. $my_item_id_row = Database::fetch_array($sel_resc);
  866. $item_insert_id = $my_item_id_row['id'];
  867. $ins_sql = "INSERT INTO $my_new_lp_item_view (" .
  868. "lp_item_id, lp_view_id, view_count," .
  869. "start_time, total_time, score," .
  870. "status" .
  871. ") VALUES (" .
  872. "$item_insert_id, $view_insert_id, 1," .
  873. "0, $my_time, $my_score," .
  874. "'$my_status'" .
  875. ")";
  876. //echo "$ins_sql<br />";
  877. $ins_res = Database::query($ins_sql);
  878. } else {
  879. //echo " Didn't find corresponding item for $my_identifier in new tables<br />\n";
  880. }
  881. }
  882. }
  883. } else {
  884. //echo "Could not find $ims... Proceeding from database...(line ".__LINE__.")<br />";
  885. if ($loglevel > 1) { error_log("This is a normal SCORM path", 0); }
  886. $scorm_lp_paths[$my_content_id]['path'] = $my_path;
  887. //$scorm_lp_paths[$my_content_id]['ims'] = '';
  888. $table_name = $db_name.$new_lp;
  889. $sql_ins = "INSERT INTO $table_name (" .
  890. "lp_type," .
  891. "name," .
  892. "description," .
  893. "path," .
  894. "force_commit, " .
  895. "default_encoding," .
  896. "display_order," .
  897. "content_maker," .
  898. "content_local," .
  899. "js_lib" .
  900. ") VALUES (" .
  901. "2," .
  902. "'$my_name'," .
  903. "''," .
  904. "'$my_path'," .
  905. "0," .
  906. "'UTF-8'," .
  907. "".$max_dsp_lp."," .
  908. "'Unknown'," .
  909. "'Unknown'," .
  910. "'scorm_api.php'" .
  911. ")";
  912. if ($loglevel > 1) { error_log("$sql_ins", 0); }
  913. $sql_res = Database::query($sql_ins);
  914. $in_id = Database::insert_id();
  915. if (empty($in_id) or $in_id == false) die('Could not insert scorm lp: '.$sql_ins);
  916. //echo "&nbsp;&nbsp;Inserted item $in_id<br />\n";
  917. $lp_ids[$my_content_id] = $in_id; //contains the old LP ID => the new LP ID
  918. $lp_course[$my_content_id] = $courses_id_list[$my_course_code]; // Contains the old learnpath ID => the course DB name
  919. $lp_course_code[$my_content_id] = $my_course_code;
  920. $max_dsp_lp++;
  921. // Setup the ims path (path to the imsmanifest.xml file)
  922. //echo "Looking for course with code ".$lp_course_code[$my_content_id]." (using $my_content_id)<br />\n";
  923. $courses_dir = $sys_course_path.$courses_dir_list[$lp_course_code[$my_content_id]];
  924. //$scorm_lp_paths[$my_content_id]['path'] = str_replace(' ', '\\ ', $scorm_lp_paths[$my_content_id]['path']);
  925. $sco_path_temp = ($scorm_lp_paths[$my_content_id]['path'] == '/') ? '' : $scorm_lp_paths[$my_content_id]['path'];
  926. $scorm_lp_paths[$my_content_id]['ims'] = $courses_dir.'/scorm'.$sco_path_temp.'/imsmanifest.xml';
  927. // Generate an imsmanifest object to get more info about the learnpath from the file
  928. $oScorm = new scorm();
  929. // Check whether imsmanifest.xml exists at this location. If not, ignore the imsmanifest.
  930. // That should have been done before already, now.
  931. if (!is_file($scorm_lp_paths[$my_content_id]['ims'])) {
  932. if ($loglevel > 1) { error_log("!!! imsmanifest file not found at ".$scorm_lp_paths[$my_content_id]['ims'].' for old lp '.$my_content_id.' and new '.$lp_ids[$my_content_id], 0); }
  933. $manifest = false;
  934. } else {
  935. //echo "Parsing ".$scorm_lp_paths[$my_content_id]['ims']."<br />\n";
  936. // Parse manifest file
  937. $manifest = $oScorm->parse_manifest($scorm_lp_paths[$my_content_id]['ims']);
  938. // The title is already escaped in the method
  939. //$my_lp_title = api_convert_encoding($oScorm->get_title(),'ISO-8859-1',$oScorm->manifest_encoding);
  940. $my_lp_title = api_convert_encoding($oScorm->get_title(), 'ISO-8859-1', 'UTF-8'); // TODO: This "magic" conversion to be checked.
  941. if (!empty($my_lp_title)) {
  942. $my_new_lp = $db_name.$new_lp;
  943. $my_sql = "UPDATE $my_new_lp " .
  944. "SET name = '$my_lp_title', " .
  945. "default_encoding = '".strtoupper($oScorm->manifest_encoding)."' " .
  946. "WHERE id = ".$lp_ids[$my_content_id];
  947. if ($loglevel > 1) { error_log("Updating title and encoding: ".$my_sql, 0); }
  948. $my_res = Database::query($my_sql);
  949. }
  950. }
  951. /*
  952. * QUERY SCORM ITEMS FROM SCORM_SCO_DATA
  953. * The danger here is that we might have several users for the same data, and so
  954. * we have to avoid entering the same elements twice
  955. */
  956. $sql_items = "SELECT * FROM $scorm_item WHERE contentId = '".$my_content_id."' ORDER BY scoId";
  957. //echo "$sql_items<br />\n";
  958. $res_items = Database::query($sql_items);
  959. while ($scormItem = Database::fetch_array($res_items)) {
  960. $my_sco_id = $scormItem['scoId']; // The index for display??? (check that)
  961. $my_identifier = $scormItem['scoIdentifier']; //the scorm item path/name
  962. $my_title = Database::escape_string($scormItem['scoTitle']);
  963. $my_status = $scormItem['status'];
  964. $my_student = $scormItem['studentId'];
  965. $my_score = $scormItem['score'];
  966. $my_time = my_get_time($scormItem['time']);
  967. $my_type = 'sco';
  968. //$my_item_path = $scorm_lp_paths[$my_content_id]['path'];
  969. $my_item_path = '';
  970. //echo "&nbsp;&nbsp;FOUND item belonging to old learnpath num $my_content_id so belongs to course ".$lp_course[$my_content_id]."<br />\n";
  971. $my_new_lp_item = $db_name.$new_lp_item;
  972. $my_new_lp_view = $db_name.$new_lp_view;
  973. $my_new_lp_item_view = $db_name.$new_lp_item_view;
  974. /*
  975. * Query items from the new table to check if it doesn't exist already
  976. * Otherwise insert it
  977. */
  978. $sel_sql = "SELECT * FROM $my_new_lp_item " .
  979. "WHERE ref = '$my_identifier' " .
  980. "AND lp_id = ".$lp_ids[$my_content_id]."";
  981. //echo $sel_sql."<br />\n";
  982. $sel_res = Database::query($sel_sql);
  983. if (Database::num_rows($sel_res) > 0) {
  984. // This item already exists, reuse
  985. $row = Database::fetch_array($sel_res);
  986. $item_insert_id = $row['lp_id'];
  987. } else {
  988. $ins_sql = "INSERT INTO $my_new_lp_item (" .
  989. "lp_id," .
  990. "item_type," .
  991. "ref," .
  992. "title," .
  993. "path" .
  994. ") " .
  995. "VALUES (" .
  996. "'".$lp_ids[$my_content_id]."'," . //insert new learnpath ID
  997. "'$my_type'," .
  998. "'".$my_identifier."'," .
  999. "'".$my_title."'," .
  1000. "'$my_item_path'" .
  1001. ")";
  1002. $ins_res = Database::query($ins_sql);
  1003. $item_insert_id = Database::insert_id();
  1004. $lp_item_ids[$lp_ids[$my_content_id]][$my_sco_id] = $item_insert_id;
  1005. $lp_item_refs[$lp_ids[$my_content_id]][$my_identifier] = $item_insert_id;
  1006. }
  1007. /*
  1008. * Check if a view is needed
  1009. */
  1010. if ($my_score != '' and $my_status != 'not attempted') {
  1011. // It is worth creating an lp_view and an lp_item_view - otherwise not
  1012. $sel_sqlb = "SELECT * FROM $my_new_lp_view " .
  1013. "WHERE lp_id = ".$lp_ids[$my_content_id]." AND user_id = $my_student";
  1014. $sel_resb = Database::query($sel_sqlb);
  1015. if (Database::num_rows($sel_resb) > 0) {
  1016. // Don't insert
  1017. $rowb = Database::fetch_array($sel_resb);
  1018. $view_insert_id = $rowb['id'];
  1019. } else {
  1020. $ins_sql = "INSERT INTO $my_new_lp_view (" .
  1021. "lp_id," .
  1022. "user_id," .
  1023. "view_count" .
  1024. ") VALUES (" .
  1025. $lp_ids[$my_content_id].", " .
  1026. $my_student.", " .
  1027. "1" .
  1028. ")";
  1029. $ins_res = Database::query($ins_sql);
  1030. $view_insert_id = Database::insert_id();
  1031. }
  1032. $ins_sql = "INSERT INTO $my_new_lp_item_view (" .
  1033. "lp_item_id, lp_view_id, view_count," .
  1034. "start_time, total_time, score," .
  1035. "status" .
  1036. ") VALUES (" .
  1037. "$item_insert_id, $view_insert_id, 1," .
  1038. "0, $my_time, $my_score," .
  1039. "'$my_status'" .
  1040. ")";
  1041. $ins_res = Database::query($ins_sql);
  1042. }
  1043. }
  1044. // UPDATE THE LP_VIEW progress
  1045. if (!empty($view_insert_id)) {
  1046. $sql = "SELECT count(distinct(lp_item_id)) FROM $my_new_lp_item_view WHERE lp_view_id = ".$view_insert_id." AND status IN ('passed','completed','succeeded','browsed','failed')";
  1047. $myres = Database::query($sql);
  1048. $myrow = Database::fetch_array($myres);
  1049. $completed = $myrow[0];
  1050. $mylpid = $lp_ids[$my_content_id];
  1051. $sql = "SELECT count(*) FROM $my_new_lp_item WHERE lp_id = '".$mylpid."'";
  1052. $myres = Database::query($sql);
  1053. $myrow = Database::fetch_array($myres);
  1054. $total = $myrow[0];
  1055. $progress = ((float)$completed / (float)$total) * 100;
  1056. $progress = number_format($progress, 0);
  1057. $sql = "UPDATE $my_new_lp_view SET progress = '$progress' WHERE id = '$view_insert_id'";
  1058. $myres = Database::query($sql);
  1059. }
  1060. /*
  1061. * Set all information that might be more correct coming from imsmanifest
  1062. */
  1063. //$my_new_lp = $db_name.$new_lp;
  1064. //$my_new_lp_item = $db_name.$new_lp_item;
  1065. //$my_new_lp_view = $db_name.$new_lp_view;
  1066. //$my_new_lp_item_view = $db_name.$new_lp_item_view;
  1067. //$sel_sql = "SELECT * FROM $my_new_lp WHERE id = $in_id";
  1068. //$res = @Database::query($sel_sql);
  1069. //if (!$res) {
  1070. // echo "Error selecting lp: $sel_sql - ".Database::error()."<br />\n";
  1071. //}
  1072. $lp_details = array();
  1073. //while ($row = Database::fetch_array($res)) {
  1074. $ordered_list = array();
  1075. $mylist = array();
  1076. foreach ($oScorm->organizations as $org) {
  1077. // There should be only one organization (generally)
  1078. // and if there are more, we are not supposed to have been
  1079. // able to manage them before the new tool, so ignore
  1080. if (count($ordered_list) > 0) {
  1081. break;
  1082. }
  1083. $ordered_list = $org->get_flat_items_list();
  1084. }
  1085. $previous = 0;
  1086. $stock = array(0);
  1087. $level = 0;
  1088. $parent_id = 0;
  1089. foreach ($ordered_list as $index => $subarray) {
  1090. // $subarray is an array representing one item and that contains info like
  1091. // identifier, level, rel_order, prerequisites, title, masteryscore, etc.
  1092. //echo "<pre>Lookin for ".$subarray['identifier']." ".print_r($lp_item_refs,true)."</pre>\n";
  1093. if (!empty($lp_item_refs[$in_id][$subarray['identifier']])) {
  1094. $new_id = $lp_item_refs[$in_id][$subarray['identifier']];
  1095. $next = 0;
  1096. $dsp = $subarray['rel_order'];
  1097. if ($subarray['level'] > $level) {
  1098. // Getting one level deeper, just consult
  1099. $parent_id = $previous;
  1100. array_push($stock,$previous);
  1101. $level = $subarray['level'];
  1102. } elseif ($subarray['level'] == $level) {
  1103. // We are on the same level, going to the next id
  1104. //array_pop($stock);
  1105. //array_push($stock, $new_id);
  1106. } else {
  1107. // Getting back from one level deeper
  1108. array_pop($stock);
  1109. $parent_id = array_pop($stock);
  1110. array_push($stock, $parent_id);
  1111. $level = $subarray['level'];
  1112. }
  1113. if (!empty($ordered_list[$index + 1]['identifier']) && !empty($lp_item_refs[$in_id][$ordered_list[$index + 1]['identifier']])){
  1114. $next = $lp_item_refs[$in_id][$ordered_list[$index + 1]['identifier']];
  1115. }
  1116. $path = $oScorm->get_res_path($subarray['identifierref']);
  1117. $update_path = '';
  1118. if (!empty($path)) {
  1119. // If new path is not empty, update
  1120. $update_path = "path = '$path', ";
  1121. }
  1122. $type = $oScorm->get_res_type($subarray['identifierref']);
  1123. $update_type = '';
  1124. if (!empty($type)) {
  1125. // If type is defined, update
  1126. $update_type = "item_type = '$type', ";
  1127. }
  1128. if (empty($path)) {
  1129. // If path is empty, it is a dir anyway
  1130. $update_type = "item_type = 'dir', ";
  1131. }
  1132. $prereq = $subarray['prerequisites'];
  1133. $update_prereq = '';
  1134. if (!empty($prereq)) {
  1135. $update_prereq = "prerequisite = '$prereq', ";
  1136. }
  1137. // We had previous data about this element, update
  1138. $sql2 = "UPDATE $my_new_lp_item " .
  1139. "SET parent_item_id = $parent_id, " .
  1140. "previous_item_id = $previous, " .
  1141. "next_item_id = $next, " .
  1142. $update_path.
  1143. $update_type.
  1144. $update_prereq.
  1145. "display_order = $dsp " .
  1146. "WHERE lp_id = ".$in_id." AND id = ".$new_id;
  1147. //echo "$sql2<br />\n";
  1148. $res2 = Database::query($sql2);
  1149. $previous = $new_id;
  1150. }
  1151. }
  1152. /**
  1153. * Migrate links on the homepage as well now (look into the TABLE_TOOL_LIST table and
  1154. * update the links to newscorm/lp_controller.php?action=view&lp_id=x)
  1155. * See scorm_migrate_hometools.php
  1156. */
  1157. //}
  1158. // end of case where $my_content_id != -1
  1159. }
  1160. /**
  1161. * Update course description (intro page) to use new links instead of learnpath/learnpath_handler.php
  1162. */
  1163. $tbl_intro = $db_name.TABLE_TOOL_INTRO;
  1164. $sql_i = "SELECT * FROM $tbl_intro WHERE id='course_homepage'";
  1165. $res_i = Database::query($sql_i);
  1166. //$link_to_course1 = 'scorm/scormdocument.php';
  1167. while ($row_i = Database::fetch_array($res_i)) {
  1168. $intro = $row_i['intro_text'];
  1169. $out = array();
  1170. $enc_path = str_replace('/', '%2F', $my_path);
  1171. $enc_path = str_replace(' ', '\+', $enc_path);
  1172. //echo "Looking for path ".$enc_path."<br />\n";
  1173. $pattern = '@claroline/scorm/scormdocument\.php([^\s"\']*)openDir='.$enc_path.'([\\"\'\s&]*)@';
  1174. if (preg_match_all($pattern, $intro, $out, PREG_SET_ORDER)) {
  1175. foreach ($out as $results) {
  1176. //echo "---> replace ".'/'.$results[0].'/ by newscorm/lp_controller.php'.$results[1].'action=view&lp_id='.$lp_ids[$my_content_id];
  1177. //$intro = preg_replace('/scorm\/scormdocument\.php([^\s"\']*)openDir='.$enc_path.'([\\"\'\s&])/', 'newscorm/lp_controller.php'.$results[1].'action=view&lp_id='.$lp_ids[$my_content_id], $intro);
  1178. $intro = preg_replace('@claroline/scorm/scormdocument\.php([^\s"\']*)openDir='.$enc_path.'([^\s"\']*)@','main/newscorm/lp_controller.php'.$results[1].'action=view&lp_id='.$lp_ids[$my_content_id], $intro);
  1179. }
  1180. } else {
  1181. //echo "No scorm link found in intro text<br />";
  1182. }
  1183. $pattern = '@claroline/scorm/showinframes\.php([^\s"\']*)file=([^\s"\'&]*)'.$enc_path.'@';
  1184. if (preg_match_all($pattern, $intro, $out, PREG_SET_ORDER)) {
  1185. foreach ($out as $results) {
  1186. //echo "---> replace ".'/'.$results[0].'/ by newscorm/lp_controller.php'.$results[1].'action=view&lp_id='.$lp_ids[$my_content_id];
  1187. //$intro = preg_replace('/scorm\/showinframes\.php([^\s"\']*)file=([^\s"\']*)'.$enc_path.'/', 'newscorm/lp_controller.php'.$results[1].'action=view&lp_id='.$lp_ids[$my_content_id], $intro);
  1188. $intro = preg_replace('@claroline/scorm/showinframes\.php([^\s"\']*)file=([^\s"\'&]*)'.$enc_path.'([^\s"\']*)@','main/newscorm/lp_controller.php'.$results[1].'action=view&lp_id='.$lp_ids[$my_content_id], $intro);
  1189. }
  1190. } else {
  1191. //echo "No scorm link found in intro text<br />";
  1192. }
  1193. if ($intro != $row_i['intro_text']) {
  1194. //echo "<pre>Replacing ".$row_i['intro_text']."\n by \n ".$intro."</pre><br />\n";
  1195. $sql_upd = "update $tbl_intro set intro_text = '".Database::escape_string($intro)."' WHERE id = 'course_homepage' AND intro_text = '".Database::escape_string($row_i['intro_text'])."'";
  1196. //echo $sql_upd."<br />\n";
  1197. fwrite($fh, $sql_upd."\n");
  1198. fwrite($fh_revert, "UPDATE $tbl_intro set intro_text = '".$row_i['intro_text']."' WHERE id = 'course_homepage' AND intro_text = '$intro';\n");
  1199. fwrite($fh_res, $intro."\n");
  1200. Database::query($sql_upd);
  1201. }
  1202. }
  1203. flush();
  1204. }
  1205. }
  1206. fclose($fh);
  1207. fclose($fh_revert);
  1208. fclose($fh_res);
  1209. if ($loglevel > 0) { error_log("All done!", 0); }
  1210. //echo "</body></html>";