CourseRestorer.class.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. <?php
  2. // $Id: CourseRestorer.class.php 10190 2006-11-24 00:23:20Z pcool $
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2004 Dokeos S.A.
  7. Copyright (c) 2003 Ghent University (UGent)
  8. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  9. Copyright (c) Bart Mollet (bart.mollet@hogent.be)
  10. For a full list of contributors, see "credits.txt".
  11. The full license can be read in "license.txt".
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  16. See the GNU General Public License for more details.
  17. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  18. Mail: info@dokeos.com
  19. ==============================================================================
  20. */
  21. require_once ('Course.class.php');
  22. require_once ('Event.class.php');
  23. require_once ('Link.class.php');
  24. require_once ('ToolIntro.class.php');
  25. require_once ('LinkCategory.class.php');
  26. require_once ('ForumCategory.class.php');
  27. require_once ('Forum.class.php');
  28. require_once ('ForumTopic.class.php');
  29. require_once ('ForumPost.class.php');
  30. require_once ('CourseDescription.class.php');
  31. require_once ('Learnpath.class.php');
  32. require_once ('mkdirr.php');
  33. require_once ('rmdirr.php');
  34. define('FILE_SKIP', 1);
  35. define('FILE_RENAME', 2);
  36. define('FILE_OVERWRITE', 3);
  37. /**
  38. * Class to restore items from a course object to a Dokeos-course
  39. * @author Bart Mollet <bart.mollet@hogent.be>
  40. * @package dokeos.backup
  41. */
  42. class CourseRestorer
  43. {
  44. /**
  45. * The course-object
  46. */
  47. var $course;
  48. /**
  49. * What to do with files with same name (FILE_SKIP, FILE_RENAME or
  50. * FILE_OVERWRITE)
  51. */
  52. var $file_option;
  53. /**
  54. * Create a new CourseRestorer
  55. */
  56. function CourseRestorer($course)
  57. {
  58. $this->course = $course;
  59. $this->file_option = FILE_RENAME;
  60. }
  61. /**
  62. * Set the file-option
  63. * @param constant $options What to do with files with same name (FILE_SKIP,
  64. * FILE_RENAME or FILE_OVERWRITE)
  65. */
  66. function set_file_option($option)
  67. {
  68. $this->file_option = $option;
  69. }
  70. /**
  71. * Restore a course.
  72. * @param string $destination_course_code The code of the Dokeos-course in
  73. * which the resources should be stored. Default: Current Dokeos-course.
  74. */
  75. function restore($destination_course_code = '')
  76. {
  77. if ($destination_course_code == '')
  78. {
  79. $course_info = api_get_course_info();
  80. $this->course->destination_db = $course_info['dbName'];
  81. $this->course->destination_path = $course_info['path'];
  82. }
  83. else
  84. {
  85. $course_info = Database :: get_course_info($destination_course_code);
  86. $this->course->destination_db = $course_info['database'];
  87. $this->course->destination_path = $course_info['directory'];
  88. }
  89. $this->restore_links();
  90. $this->restore_tool_intro();
  91. $this->restore_events();
  92. $this->restore_announcements();
  93. $this->restore_documents();
  94. $this->restore_scorm_documents();
  95. $this->restore_course_descriptions();
  96. //$this->restore_forums();
  97. $this->restore_quizzes(); // after restore_documents! (for correct import of sound/video)
  98. $this->restore_learnpaths();
  99. // Restore the item properties
  100. $table = Database :: get_course_table(ITEM_PROPERTY_TABLE, $this->course->destination_db);
  101. foreach ($this->course->resources as $type => $resources)
  102. {
  103. foreach ($resources as $id => $resource)
  104. {
  105. foreach ($resource->item_properties as $property)
  106. {
  107. // First check if there isn't allready a record for this resource
  108. $sql = "SELECT * FROM $table WHERE tool = '".$property['tool']."' AND ref = '".$resource->destination_id."'";
  109. $res = api_sql_query($sql,__FILE__,__LINE__);
  110. if( mysql_num_rows($res) == 0)
  111. {
  112. // The to_group_id and to_user_id are set to default values as users/groups possibly not exist in the target course
  113. $sql = "INSERT INTO $table SET
  114. tool = '".$property['tool']."',
  115. insert_user_id = '".$property['insert_user_id']."',
  116. insert_date = '".$property['insert_date']."',
  117. lastedit_date = '".$property['lastedit_date']."',
  118. ref = '".$resource->destination_id."',
  119. lastedit_type = '".$property['lastedit_type']."',
  120. lastedit_user_id = '".$property['lastedit_user_id']."',
  121. visibility = '".$property['visibility']."',
  122. start_visible = '".$property['start_visible']."',
  123. end_visible = '".$property['end_visible']."',
  124. to_group_id = '0'";
  125. ;
  126. api_sql_query($sql, __FILE__, __LINE__);
  127. }
  128. }
  129. }
  130. }
  131. // Restore the linked-resources
  132. $table = Database :: get_course_table(LINKED_RESOURCES_TABLE, $this->course->destination_db);
  133. foreach ($this->course->resources as $type => $resources)
  134. {
  135. foreach ($resources as $id => $resource)
  136. {
  137. $linked_resources = $resource->get_linked_resources();
  138. foreach ($linked_resources as $to_type => $to_ids)
  139. {
  140. foreach ($to_ids as $index => $to_id)
  141. {
  142. $to_resource = $this->course->resources[$to_type][$to_id];
  143. $sql = "INSERT INTO ".$table." SET source_type = '".$type."', source_id = '".$resource->destination_id."', resource_type='".$to_type."', resource_id='".$to_resource->destination_id."' ";
  144. api_sql_query($sql, __FILE__, __LINE__);
  145. }
  146. }
  147. }
  148. }
  149. }
  150. /**
  151. * Restore documents
  152. */
  153. function restore_documents()
  154. {
  155. if ($this->course->has_resources(RESOURCE_DOCUMENT))
  156. {
  157. $table = Database :: get_course_table(DOCUMENT_TABLE, $this->course->destination_db);
  158. $resources = $this->course->resources;
  159. foreach ($resources[RESOURCE_DOCUMENT] as $id => $document)
  160. {
  161. $path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/';
  162. mkdirr(dirname($path.$document->path), 0755);
  163. if ($document->file_type == DOCUMENT)
  164. {
  165. if (file_exists($path.$document->path))
  166. {
  167. switch ($this->file_option)
  168. {
  169. case FILE_OVERWRITE :
  170. copy($this->course->backup_path.'/'.$document->path, $path.$document->path);
  171. $sql = "SELECT id FROM ".$table." WHERE path='/".substr($document->path, 9)."'";
  172. $res = api_sql_query($sql, __FILE__, __LINE__);
  173. $obj = mysql_fetch_object($res);
  174. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id;
  175. $sql = "UPDATE ".$table." SET comment = '".mysql_real_escape_string($document->comment)."', title='".mysql_real_escape_string($document->title)."', size='".$document->size."' WHERE id = '".$obj->id."'";
  176. api_sql_query($sql, __FILE__, __LINE__);
  177. break;
  178. case FILE_SKIP :
  179. $sql = "SELECT id FROM ".$table." WHERE path='/".mysql_real_escape_string(substr($document->path, 9))."'";
  180. $res = api_sql_query($sql, __FILE__, __LINE__);
  181. $obj = mysql_fetch_object($res);
  182. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id;
  183. break;
  184. case FILE_RENAME :
  185. $i = 1;
  186. $ext = explode('.', basename($document->path));
  187. if (count($ext) > 1)
  188. {
  189. $ext = array_pop($ext);
  190. $file_name_no_ext = substr($document->path, 0, - (strlen($ext) + 1));
  191. $ext = '.'.$ext;
  192. }
  193. else
  194. {
  195. $ext = '';
  196. $file_name_no_ext = $document->path;
  197. }
  198. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  199. $file_exists = file_exists($path.$new_file_name);
  200. while ($file_exists)
  201. {
  202. $i ++;
  203. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  204. $file_exists = file_exists($path.$new_file_name);
  205. }
  206. copy($this->course->backup_path.'/'.$document->path, $path.$new_file_name);
  207. $sql = "INSERT INTO ".$table." SET path = '/".mysql_real_escape_string(substr($new_file_name, 9))."', comment = '".mysql_real_escape_string($document->comment)."', title = '".mysql_real_escape_string($document->title)."' ,filetype='".$document->file_type."', size= '".$document->size."'";
  208. api_sql_query($sql, __FILE__, __LINE__);
  209. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = mysql_insert_id();
  210. break;
  211. } // end switch
  212. } // end if file exists
  213. else
  214. {
  215. copy($this->course->backup_path.'/'.$document->path, $path.$document->path);
  216. $sql = "INSERT INTO ".$table." SET path = '/".substr($document->path, 9)."', comment = '".mysql_real_escape_string($document->comment)."', title = '".mysql_real_escape_string($document->title)."' ,filetype='".$document->file_type."', size= '".$document->size."'";
  217. api_sql_query($sql, __FILE__, __LINE__);
  218. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = mysql_insert_id();
  219. } // end file doesn't exist
  220. }
  221. else
  222. {
  223. $sql = "SELECT id FROM ".$table." WHERE path = '/".mysql_real_escape_string(substr($document->path, 9))."'";
  224. $res = api_sql_query($sql,__FILE__,__LINE__);
  225. if( mysql_num_rows($res)> 0)
  226. {
  227. $obj = mysql_fetch_object($res);
  228. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id;
  229. }
  230. else
  231. {
  232. $sql = "INSERT INTO ".$table." SET path = '/".mysql_real_escape_string(substr($document->path, 9))."', comment = '".mysql_real_escape_string($document->comment)."', title = '".mysql_real_escape_string($document->title)."' ,filetype='".$document->file_type."', size= '".$document->size."'";
  233. api_sql_query($sql, __FILE__, __LINE__);
  234. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = mysql_insert_id();
  235. }
  236. } // end folder
  237. } // end for each
  238. }
  239. }
  240. /**
  241. * Restore scorm documents
  242. */
  243. function restore_scorm_documents()
  244. {
  245. if ($this->course->has_resources(RESOURCE_SCORM))
  246. {
  247. $resources = $this->course->resources;
  248. foreach ($resources[RESOURCE_SCORM] as $id => $document)
  249. {
  250. $path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/';
  251. mkdirr(dirname($path.$document->path), 0755);
  252. if (file_exists($path.$document->path))
  253. {
  254. switch ($this->file_option)
  255. {
  256. case FILE_OVERWRITE :
  257. rmdirr($path.$document->path);
  258. copyDirTo($this->course->backup_path.'/'.$document->path, $path.dirname($document->path), false);
  259. break;
  260. case FILE_SKIP :
  261. break;
  262. case FILE_RENAME :
  263. $i = 1;
  264. $ext = explode('.', basename($document->path));
  265. if (count($ext) > 1)
  266. {
  267. $ext = array_pop($ext);
  268. $file_name_no_ext = substr($document->path, 0, - (strlen($ext) + 1));
  269. $ext = '.'.$ext;
  270. }
  271. else
  272. {
  273. $ext = '';
  274. $file_name_no_ext = $document->path;
  275. }
  276. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  277. $file_exists = file_exists($path.$new_file_name);
  278. while ($file_exists)
  279. {
  280. $i ++;
  281. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  282. $file_exists = file_exists($path.$new_file_name);
  283. }
  284. rename($this->course->backup_path.'/'.$document->path,$this->course->backup_path.'/'.$new_file_name);
  285. copyDirTo($this->course->backup_path.'/'.$new_file_name, $path.dirname($new_file_name), false);
  286. rename($this->course->backup_path.'/'.$new_file_name,$this->course->backup_path.'/'.$document->path);
  287. break;
  288. } // end switch
  289. } // end if file exists
  290. else
  291. {
  292. copyDirTo($this->course->backup_path.'/'.$document->path, $path.dirname($document->path), false);
  293. }
  294. } // end for each
  295. }
  296. }
  297. /**
  298. * Restore forums
  299. */
  300. function restore_forums()
  301. {
  302. if ($this->course->has_resources(RESOURCE_FORUM))
  303. {
  304. $table_forum = Database :: get_course_table(TABLE_FORUM, $this->course->destination_db);
  305. $table_topic = Database :: get_course_table(TABLE_FORUM_POST, $this->course->destination_db);
  306. $table_post = Database :: get_course_table(TABLE_FORUM_POST, $this->course->destination_db);
  307. $resources = $this->course->resources;
  308. foreach ($resources[RESOURCE_FORUM] as $id => $forum)
  309. {
  310. $cat_id = $this->restore_forum_category($forum->category_id);
  311. $sql = "INSERT INTO ".$table_forum." SET forum_name = '".mysql_real_escape_string($forum->title)."', forum_desc = '".mysql_real_escape_string($forum->description)."', cat_id = '".$cat_id."', forum_access='2'";
  312. api_sql_query($sql, __FILE__, __LINE__);
  313. $new_id = mysql_insert_id();
  314. $this->course->resources[RESOURCE_FORUM][$id]->destination_id = $new_id;
  315. $forum_topics = 0;
  316. if (is_array($this->course->resources[RESOURCE_FORUMTOPIC]))
  317. {
  318. foreach ($this->course->resources[RESOURCE_FORUMTOPIC] as $topic_id => $topic)
  319. {
  320. if ($topic->forum_id == $id)
  321. {
  322. $this->restore_topic($topic_id, $new_id);
  323. $forum_topics ++;
  324. }
  325. }
  326. }
  327. if ($forum_topics > 0)
  328. {
  329. $last_post = $this->course->resources[RESOURCE_FORUMPOST][$forum->last_post];
  330. $sql = "UPDATE ".$table_forum." SET forum_topics = ".$forum_topics.", forum_last_post_id = ".$last_post->destination_id." WHERE forum_id = '".$new_id."' ";
  331. api_sql_query($sql, __FILE__, __LINE__);
  332. }
  333. }
  334. }
  335. }
  336. /**
  337. * Restore forum-categories
  338. */
  339. function restore_forum_category($id)
  340. {
  341. $forum_cat_table = Database :: get_course_table(TABLE_FORUM_CATEGORY, $this->course->destination_db);
  342. $resources = $this->course->resources;
  343. $forum_cat = $resources[RESOURCE_FORUMCATEGORY][$id];
  344. if (!$forum_cat->is_restored())
  345. {
  346. $sql = "INSERT INTO ".$forum_cat_table." SET cat_title = '".mysql_real_escape_string($forum_cat->title.' ('.$this->course->code.')')."'";
  347. api_sql_query($sql, __FILE__, __LINE__);
  348. $new_id = mysql_insert_id();
  349. $this->course->resources[RESOURCE_FORUMCATEGORY][$id]->destination_id = $new_id;
  350. return $new_id;
  351. }
  352. return $this->course->resources[RESOURCE_FORUMCATEGORY][$id]->destination_id;
  353. }
  354. /**
  355. * Restore a forum-topic
  356. */
  357. function restore_topic($id, $forum_id)
  358. {
  359. $table = Database :: get_course_table(TABLE_FORUM_POST, $this->course->destination_db);
  360. $resources = $this->course->resources;
  361. $topic = $resources[RESOURCE_FORUMTOPIC][$id];
  362. $sql = "INSERT INTO ".$table." SET topic_title = '".mysql_real_escape_string($topic->title)."', topic_time = '".$topic->time."', nom = '".mysql_real_escape_string($topic->lastname)."', prenom = '".mysql_real_escape_string($topic->firstname)."', topic_notify = '".$topic->topic_notify."', forum_id = '".$forum_id."'";
  363. api_sql_query($sql, __FILE__, __LINE__);
  364. $new_id = mysql_insert_id();
  365. $this->course->resources[RESOURCE_FORUMTOPIC][$id]->destination_id = $new_id;
  366. $topic_replies = -1;
  367. foreach ($this->course->resources[RESOURCE_FORUMPOST] as $post_id => $post)
  368. {
  369. if ($post->topic_id == $id)
  370. {
  371. $topic_replies ++;
  372. $this->restore_post($post_id, $new_id, $forum_id);
  373. }
  374. }
  375. if ($topic_replies >= 0)
  376. {
  377. $last_post = $this->course->resources[RESOURCE_FORUMPOST][$topic->last_post];
  378. $sql = "UPDATE ".$table." SET topic_replies = '".$topic_replies."', topic_last_post_id = ".$last_post->destination_id;
  379. api_sql_query($sql, __FILE__, __LINE__);
  380. }
  381. return $new_id;
  382. }
  383. /**
  384. * restore a forum-post
  385. * @todo restore tree-structure of posts.
  386. */
  387. function restore_post($id, $topic_id, $forum_id)
  388. {
  389. $table_post = Database :: get_course_table(TABLE_FORUM_POST, $this->course->destination_db);
  390. $table_posttext = Database :: get_course_table(TOOL_FORUM_POST_TEXT_TABLE, $this->course->destination_db);
  391. $resources = $this->course->resources;
  392. $post = $resources[RESOURCE_FORUMPOST][$id];
  393. $sql = "INSERT INTO ".$table_post." SET topic_id = '".$topic_id."', post_time = '".$post->post_time."', forum_id = '".$forum_id."', nom = '".mysql_real_escape_string($post->lastname)."', prenom = '".mysql_real_escape_string($post->firstname)."', topic_notify = '".$post->topic_notify."', poster_ip = '".$post->poster_ip."'";
  394. api_sql_query($sql, __FILE__, __LINE__);
  395. $new_id = mysql_insert_id();
  396. $this->course->resources[RESOURCE_FORUMPOST][$id]->destination_id = $new_id;
  397. $sql = "INSERT INTO ".$table_posttext." SET post_text = '".mysql_real_escape_string($post->text)."', post_title = '".mysql_real_escape_string($post->title)."', post_id = '".$new_id."'";
  398. api_sql_query($sql, __FILE__, __LINE__);
  399. return $new_id;
  400. }
  401. /**
  402. * Restore links
  403. */
  404. function restore_links()
  405. {
  406. if ($this->course->has_resources(RESOURCE_LINK))
  407. {
  408. $link_table = Database :: get_course_table(LINK_TABLE, $this->course->destination_db);
  409. $resources = $this->course->resources;
  410. foreach ($resources[RESOURCE_LINK] as $id => $link)
  411. {
  412. $cat_id = $this->restore_link_category($link->category_id);
  413. $sql = "SELECT MAX(display_order) FROM $link_table WHERE category_id='" . mysql_real_escape_string($cat_id). "'";
  414. $result = api_sql_query($sql, __FILE__, __LINE__);
  415. list($max_order) = mysql_fetch_row($result);
  416. $sql = "INSERT INTO ".$link_table." SET url = '".mysql_real_escape_string($link->url)."', title = '".mysql_real_escape_string($link->title)."', description = '".mysql_real_escape_string($link->description)."', category_id='".$cat_id."', on_homepage = '".$link->on_homepage."', display_order='".($max_order+1)."'";
  417. api_sql_query($sql, __FILE__, __LINE__);
  418. $this->course->resources[RESOURCE_LINK][$id]->destination_id = mysql_insert_id();
  419. }
  420. }
  421. }
  422. /**
  423. * Restore tool intro
  424. */
  425. function restore_tool_intro()
  426. {
  427. if ($this->course->has_resources(RESOURCE_TOOL_INTRO))
  428. {
  429. $tool_intro_table = Database :: get_course_table(TOOL_INTRO_TABLE, $this->course->destination_db);
  430. $resources = $this->course->resources;
  431. foreach ($resources[RESOURCE_TOOL_INTRO] as $id => $tool_intro)
  432. {
  433. $sql = "DELETE FROM ".$tool_intro_table." WHERE id='".mysql_real_escape_string($tool_intro->id)."'";
  434. api_sql_query($sql, __FILE__, __LINE__);
  435. $sql = "INSERT INTO ".$tool_intro_table." SET id='".mysql_real_escape_string($tool_intro->id)."', intro_text = '".mysql_real_escape_string($tool_intro->intro_text)."'";
  436. api_sql_query($sql, __FILE__, __LINE__);
  437. $this->course->resources[RESOURCE_TOOL_INTRO][$id]->destination_id = mysql_insert_id();
  438. }
  439. }
  440. }
  441. /**
  442. * Restore a link-category
  443. */
  444. function restore_link_category($id)
  445. {
  446. if ($id == 0)
  447. return 0;
  448. $link_cat_table = Database :: get_course_table(LINK_CATEGORY_TABLE, $this->course->destination_db);
  449. $resources = $this->course->resources;
  450. $link_cat = $resources[RESOURCE_LINKCATEGORY][$id];
  451. if (!$link_cat->is_restored())
  452. {
  453. $sql = "SELECT MAX(display_order) FROM $link_cat_table";
  454. $result=api_sql_query($sql,__FILE__,__LINE__);
  455. list($orderMax)=mysql_fetch_row($result);
  456. $display_order=$orderMax+1;
  457. $sql = "INSERT INTO ".$link_cat_table." SET category_title = '".mysql_real_escape_string($link_cat->title)."', description='".mysql_real_escape_string($link_cat->description)."', display_order='".$display_order."' ";
  458. api_sql_query($sql, __FILE__, __LINE__);
  459. $new_id = mysql_insert_id();
  460. $this->course->resources[RESOURCE_LINKCATEGORY][$id]->destination_id = $new_id;
  461. return $new_id;
  462. }
  463. return $this->course->resources[RESOURCE_LINKCATEGORY][$id]->destination_id;
  464. }
  465. /**
  466. * Restore events
  467. */
  468. function restore_events()
  469. {
  470. if ($this->course->has_resources(RESOURCE_EVENT))
  471. {
  472. $table = Database :: get_course_table(AGENDA_TABLE, $this->course->destination_db);
  473. $resources = $this->course->resources;
  474. foreach ($resources[RESOURCE_EVENT] as $id => $event)
  475. {
  476. $sql = "INSERT INTO ".$table." SET title = '".mysql_real_escape_string($event->title)."', content = '".mysql_real_escape_string($event->content)."', start_date = '".$event->start_date."', end_date = '".$event->end_date."'";
  477. api_sql_query($sql, __FILE__, __LINE__);
  478. $this->course->resources[RESOURCE_EVENT][$id]->destination_id = mysql_insert_id();
  479. }
  480. }
  481. }
  482. /**
  483. * Restore course-description
  484. */
  485. function restore_course_descriptions()
  486. {
  487. if ($this->course->has_resources(RESOURCE_COURSEDESCRIPTION))
  488. {
  489. $table = Database :: get_course_table(COURSE_DESCRIPTION_TABLE, $this->course->destination_db);
  490. $resources = $this->course->resources;
  491. foreach ($resources[RESOURCE_COURSEDESCRIPTION] as $id => $cd)
  492. {
  493. $sql = "INSERT INTO ".$table." SET title = '".mysql_real_escape_string($cd->title)."', content = '".mysql_real_escape_string($cd->content)."'";
  494. api_sql_query($sql, __FILE__, __LINE__);
  495. $this->course->resources[RESOURCE_COURSEDESCRIPTION][$id]->destination_id = mysql_insert_id();
  496. }
  497. }
  498. }
  499. /**
  500. * Restore announcements
  501. */
  502. function restore_announcements()
  503. {
  504. if ($this->course->has_resources(RESOURCE_ANNOUNCEMENT))
  505. {
  506. $table = Database :: get_course_table(ANNOUNCEMENT_TABLE, $this->course->destination_db);
  507. $resources = $this->course->resources;
  508. foreach ($resources[RESOURCE_ANNOUNCEMENT] as $id => $announcement)
  509. {
  510. $sql = "INSERT INTO ".$table." SET title = '".mysql_real_escape_string($announcement->title)."',content = '".mysql_real_escape_string($announcement->content)."', end_date = '".$announcement->date."', display_order = '".$announcement->display_order."'";
  511. api_sql_query($sql, __FILE__, __LINE__);
  512. $this->course->resources[RESOURCE_ANNOUNCEMENT][$id]->destination_id = mysql_insert_id();
  513. }
  514. }
  515. }
  516. /**
  517. * Restore Quiz
  518. */
  519. function restore_quizzes()
  520. {
  521. if ($this->course->has_resources(RESOURCE_QUIZ))
  522. {
  523. $table_qui = Database :: get_course_table(QUIZ_TEST_TABLE, $this->course->destination_db);
  524. $table_rel = Database :: get_course_table(QUIZ_TEST_QUESTION_TABLE, $this->course->destination_db);
  525. $table_doc = Database :: get_course_table(DOCUMENT_TABLE, $this->course->destination_db);
  526. $resources = $this->course->resources;
  527. foreach ($resources[RESOURCE_QUIZ] as $id => $quiz)
  528. {
  529. $doc = '';
  530. if (strlen($quiz->media) > 0)
  531. {
  532. if ($this->course->resources[RESOURCE_DOCUMENT][$quiz->media]->is_restored())
  533. {
  534. $sql = "SELECT path FROM ".$table_doc." WHERE id = ".$resources[RESOURCE_DOCUMENT][$quiz->media]->destination_id;
  535. $doc = api_sql_query($sql, __FILE__, __LINE__);
  536. $doc = mysql_fetch_object($doc);
  537. $doc = str_replace('/audio/', '', $doc->path);
  538. }
  539. }
  540. $sql = "INSERT INTO ".$table_qui." SET title = '".mysql_real_escape_string($quiz->title)."', description = '".mysql_real_escape_string($quiz->description)."', type = '".$quiz->quiz_type."', random = '".$quiz->random."', active = '".$quiz->active."', sound = '".mysql_real_escape_string($doc)."' ";
  541. api_sql_query($sql, __FILE__, __LINE__);
  542. $new_id = mysql_insert_id();
  543. $this->course->resources[RESOURCE_QUIZ][$id]->destination_id = $new_id;
  544. foreach ($quiz->question_ids as $index => $question_id)
  545. {
  546. $qid = $this->restore_quiz_question($question_id);
  547. $sql = "INSERT IGNORE INTO ".$table_rel." SET question_id = ".$qid.", exercice_id = ".$new_id."";
  548. api_sql_query($sql, __FILE__, __LINE__);
  549. }
  550. }
  551. }
  552. }
  553. /**
  554. * Restore quiz-questions
  555. */
  556. function restore_quiz_question($id)
  557. {
  558. $resources = $this->course->resources;
  559. $question = $resources[RESOURCE_QUIZQUESTION][$id];
  560. $new_id=0;
  561. if(is_object($question))
  562. {
  563. if ($question->is_restored())
  564. {
  565. return $question->destination_id;
  566. }
  567. $table_que = Database :: get_course_table(QUIZ_QUESTION_TABLE, $this->course->destination_db);
  568. $table_ans = Database :: get_course_table(QUIZ_ANSWER_TABLE, $this->course->destination_db);
  569. $sql = "INSERT INTO ".$table_que." SET question = '".addslashes($question->question)."', description = '".addslashes($question->description)."', ponderation = '".addslashes($question->ponderation)."', position = '".addslashes($question->position)."', type='".addslashes($question->quiz_type)."'";
  570. api_sql_query($sql, __FILE__, __LINE__);
  571. $new_id = mysql_insert_id();
  572. foreach ($question->answers as $index => $answer)
  573. {
  574. $sql = "INSERT INTO ".$table_ans." SET id= '". ($index +1)."',question_id = '".$new_id."', answer = '".mysql_real_escape_string($answer['answer'])."', correct = '".$answer['correct']."', comment = '".mysql_real_escape_string($answer['comment'])."', ponderation='".$answer['ponderation']."', position = '".$answer['position']."'";
  575. api_sql_query($sql, __FILE__, __LINE__);
  576. }
  577. $this->course->resources[RESOURCE_QUIZQUESTION][$id]->destination_id = $new_id;
  578. }
  579. return $new_id;
  580. }
  581. /**
  582. * Restore learnpaths
  583. */
  584. function restore_learnpaths()
  585. {
  586. if ($this->course->has_resources(RESOURCE_LEARNPATH))
  587. {
  588. $table_main = Database :: get_course_table(LEARNPATH_MAIN_TABLE, $this->course->destination_db);
  589. $table_chapter = Database :: get_course_table(LEARNPATH_CHAPTER_TABLE, $this->course->destination_db);
  590. $table_item = Database :: get_course_table(LEARNPATH_ITEM_TABLE, $this->course->destination_db);
  591. $table_tool = Database::get_course_table(TOOL_LIST_TABLE, $this->course->destination_db);
  592. $resources = $this->course->resources;
  593. $prereq_old = array ();
  594. $item_old_id = array ();
  595. foreach ($resources[RESOURCE_LEARNPATH] as $id => $lp)
  596. {
  597. $sql = "INSERT INTO ".$table_main." SET learnpath_name = '".mysql_real_escape_string($lp->name)."', learnpath_description = '".mysql_real_escape_string($lp->description)."'";
  598. api_sql_query($sql, __FILE__, __LINE__);
  599. $new_lp_id = mysql_insert_id();
  600. if($lp->visibility)
  601. {
  602. $sql = "INSERT INTO $table_tool SET name='".mysql_real_escape_string($lp->name)."', link='learnpath/learnpath_handler.php?learnpath_id=$new_lp_id', image='scormbuilder.gif', visibility='1', admin='0', address='squaregrey.gif'";
  603. api_sql_query($sql, __FILE__, __LINE__);
  604. }
  605. foreach ($lp->get_chapters() as $index => $chapter)
  606. {
  607. $sql = "INSERT INTO ".$table_chapter." SET learnpath_id ='".$new_lp_id."' ,chapter_name='".mysql_real_escape_string($chapter['name'])."', chapter_description='".mysql_real_escape_string($chapter['description'])."',display_order='".$chapter['display_order']."' ";
  608. api_sql_query($sql, __FILE__, __LINE__);
  609. $new_chap_id = mysql_insert_id();
  610. foreach ($chapter['items'] as $index => $item)
  611. {
  612. if ($item['id'] != 0)
  613. {
  614. // Link in learnpath have types 'Link _self' or 'Link _blank'. We only need 'Link' here.
  615. $type_parts = explode(' ',$item['type']);
  616. $item['id'] = $this->course->resources[$type_parts[0]][$item['id']]->destination_id;
  617. }
  618. $sql = "INSERT INTO ".$table_item." SET chapter_id='".$new_chap_id."', item_type='".$item['type']."', item_id='".$item['id']."', display_order = '".$item['display_order']."', title = '".mysql_real_escape_string($item['title'])."', description ='".mysql_real_escape_string($item['description'])."', prereq_id='".$item['prereq']."', prereq_type = '".$item['prereq_type']."', prereq_completion_limit = '".$item['prereq_completion_limit']."' ";
  619. api_sql_query($sql, __FILE__, __LINE__);
  620. $new_item_id = mysql_insert_id();
  621. if ($item['prereq'] != '')
  622. {
  623. $prereq_old[$new_item_id] = $item['prereq'];
  624. }
  625. $item_id_old[$item['ref_id']] = $new_item_id;
  626. }
  627. }
  628. foreach ($prereq_old as $new_item_id => $prereq_old_id)
  629. {
  630. $prereq_new_id = $item_id_old[$prereq_old_id];
  631. $sql = "UPDATE ".$table_item." SET prereq_id = '".$prereq_new_id."' WHERE id = '".$new_item_id."'";
  632. api_sql_query($sql, __FILE__, __LINE__);
  633. }
  634. $this->course->resources[RESOURCE_LEARNPATH][$id]->destination_id = $new_lp_id;
  635. }
  636. }
  637. }
  638. }
  639. ?>