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

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