CourseRestorer.class.php 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. <?php // $Id: CourseRestorer.class.php 22200 2009-07-17 19:47:58Z iflorespaz $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2008 Dokeos SPRL
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Bart Mollet (bart.mollet@hogent.be)
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  17. Mail: info@dokeos.com
  18. ==============================================================================
  19. */
  20. require_once 'Course.class.php';
  21. require_once 'Event.class.php';
  22. require_once 'Link.class.php';
  23. require_once 'ToolIntro.class.php';
  24. require_once 'LinkCategory.class.php';
  25. require_once 'ForumCategory.class.php';
  26. require_once 'Forum.class.php';
  27. require_once 'ForumTopic.class.php';
  28. require_once 'ForumPost.class.php';
  29. require_once 'CourseDescription.class.php';
  30. require_once 'Learnpath.class.php';
  31. require_once 'Survey.class.php';
  32. require_once 'SurveyQuestion.class.php';
  33. //require_once 'mkdirr.php';
  34. //require_once 'rmdirr.php';
  35. require_once 'Glossary.class.php';
  36. require_once 'wiki.class.php';
  37. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  38. define('FILE_SKIP', 1);
  39. define('FILE_RENAME', 2);
  40. define('FILE_OVERWRITE', 3);
  41. /**
  42. * Class to restore items from a course object to a Dokeos-course
  43. * @author Bart Mollet <bart.mollet@hogent.be>
  44. * @package dokeos.backup
  45. */
  46. class CourseRestorer
  47. {
  48. /**
  49. * The course-object
  50. */
  51. var $course;
  52. /**
  53. * What to do with files with same name (FILE_SKIP, FILE_RENAME or
  54. * FILE_OVERWRITE)
  55. */
  56. var $file_option;
  57. /**
  58. * Create a new CourseRestorer
  59. */
  60. function CourseRestorer($course)
  61. {
  62. $this->course = $course;
  63. $this->file_option = FILE_RENAME;
  64. }
  65. /**
  66. * Set the file-option
  67. * @param constant $options What to do with files with same name (FILE_SKIP,
  68. * FILE_RENAME or FILE_OVERWRITE)
  69. */
  70. function set_file_option($option)
  71. {
  72. $this->file_option = $option;
  73. }
  74. /**
  75. * Restore a course.
  76. * @param string $destination_course_code The code of the Dokeos-course in
  77. * which the resources should be stored. Default: Current Dokeos-course.
  78. */
  79. function restore($destination_course_code = '',$session_id = 0)
  80. {
  81. if ($destination_course_code == '') {
  82. $course_info = api_get_course_info();
  83. $this->course->destination_db = $course_info['dbName'];
  84. $this->course->destination_path = $course_info['path'];
  85. } else {
  86. $course_info = Database :: get_course_info($destination_course_code);
  87. $this->course->destination_db = $course_info['database'];
  88. $this->course->destination_path = $course_info['directory'];
  89. }
  90. // platform encoding
  91. $course_charset = $this->course->encoding;
  92. if (!empty($session_id)) {
  93. $this->restore_documents($session_id,$destination_course_code);
  94. $this->restore_quizzes($session_id);
  95. $this->restore_glossary($session_id);
  96. $this->restore_learnpaths($session_id);
  97. $this->restore_links($session_id);
  98. $this->restore_course_descriptions($session_id);
  99. $this->restore_wiki($session_id);
  100. } else {
  101. $this->restore_links();
  102. $this->restore_tool_intro();
  103. $this->restore_events();
  104. $this->restore_announcements();
  105. $this->restore_documents();
  106. $this->restore_scorm_documents();
  107. $this->restore_course_descriptions();
  108. $this->restore_quizzes(); // after restore_documents! (for correct import of sound/video)
  109. $this->restore_learnpaths();
  110. $this->restore_surveys();
  111. $this->restore_student_publication();
  112. $this->restore_glossary();
  113. $this->restore_wiki();
  114. }
  115. // Restore the item properties
  116. $table = Database :: get_course_table(TABLE_ITEM_PROPERTY, $this->course->destination_db);
  117. $condition_session = "";
  118. if (!empty($session_id)) {
  119. $condition_session = " , id_session='".intval($session_id)."'";
  120. }
  121. foreach ($this->course->resources as $type => $resources) {
  122. if (is_array($resources)) {
  123. foreach ($resources as $id => $resource) {
  124. foreach ($resource->item_properties as $property)
  125. {
  126. // First check if there isn't allready a record for this resource
  127. $sql = "SELECT * FROM $table WHERE tool = '".$property['tool']."' AND ref = '".$resource->destination_id."'";
  128. $res = Database::query($sql,__FILE__,__LINE__);
  129. if( Database::num_rows($res) == 0) {
  130. // The to_group_id and to_user_id are set to default values as users/groups possibly not exist in the target course
  131. $sql = "INSERT INTO $table SET
  132. tool = '".Database::escape_string($property['tool'])."',
  133. insert_user_id = '".Database::escape_string($property['insert_user_id'])."',
  134. insert_date = '".Database::escape_string($property['insert_date'])."',
  135. lastedit_date = '".Database::escape_string($property['lastedit_date'])."',
  136. ref = '".Database::escape_string($resource->destination_id)."',
  137. lastedit_type = '".Database::escape_string($property['lastedit_type'])."',
  138. lastedit_user_id = '".Database::escape_string($property['lastedit_user_id'])."',
  139. visibility = '".Database::escape_string($property['visibility'])."',
  140. start_visible = '".Database::escape_string($property['start_visible'])."',
  141. end_visible = '".Database::escape_string($property['end_visible'])."',
  142. to_user_id = '".Database::escape_string($property['to_user_id'])."',
  143. to_group_id = '0' $condition_session" ;
  144. ;
  145. Database::query($sql, __FILE__, __LINE__);
  146. }
  147. }
  148. }
  149. }
  150. }
  151. // Restore the linked-resources
  152. $table = Database :: get_course_table(TABLE_LINKED_RESOURCES, $this->course->destination_db);
  153. foreach ($this->course->resources as $type => $resources)
  154. {
  155. if (is_array($resources))
  156. foreach ($resources as $id => $resource)
  157. {
  158. $linked_resources = $resource->get_linked_resources();
  159. foreach ($linked_resources as $to_type => $to_ids)
  160. {
  161. foreach ($to_ids as $index => $to_id)
  162. {
  163. $to_resource = $this->course->resources[$to_type][$to_id];
  164. $sql = "INSERT INTO ".$table." SET source_type = '".$type."', source_id = '".$resource->destination_id."', resource_type='".$to_type."', resource_id='".$to_resource->destination_id."' ";
  165. Database::query($sql, __FILE__, __LINE__);
  166. }
  167. }
  168. }
  169. }
  170. }
  171. /**
  172. * Restore documents
  173. */
  174. function restore_documents($session_id = 0,$destination_course_code = '')
  175. {
  176. if ($this->course->has_resources(RESOURCE_DOCUMENT)) {
  177. $table = Database :: get_course_table(TABLE_DOCUMENT, $this->course->destination_db);
  178. $resources = $this->course->resources;
  179. $destination_course['dbName']= $this->course->destination_db;
  180. /* echo '<pre>'; echo $this->course->backup_path; echo '<br>'; */
  181. foreach ($resources[RESOURCE_DOCUMENT] as $id => $document) {
  182. $path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/';
  183. $perm = api_get_setting('permissions_for_new_directories');
  184. $perm = octdec(!empty($perm)?$perm:0770);
  185. $dirs = explode('/', dirname($document->path));
  186. //if (count($dirs)==1) {
  187. if ($document->file_type==FOLDER) {
  188. $visibility = $document->item_properties[0]['visibility'];
  189. $new = substr($document->path, 8);
  190. if (!is_dir($path.'document/'.$new)) {
  191. $sql = "SELECT id FROM ".$table." WHERE path='/".Database::escape_string($new)."'";
  192. $res = Database::query($sql, __FILE__, __LINE__);
  193. $num_result = Database::num_rows($res);
  194. if ($num_result==0) {
  195. $created_dir = create_unexisting_directory($destination_course,api_get_user_id(),0, 0 ,$path.'document',$new,basename($new),$visibility);
  196. }
  197. }
  198. }
  199. //}
  200. /*
  201. else {
  202. $my_temp = '';
  203. for ($i=1; $i<=count($dirs); $i++) {
  204. $my_temp .= $dirs[$i];
  205. if (!is_dir($path.'document/'.$my_temp)) {
  206. $sql = "SELECT id FROM ".$table." WHERE path='/".Database::escape_string($my_temp)."'";
  207. $res = Database::query($sql, __FILE__, __LINE__);
  208. $num_result = Database::num_rows($res);
  209. if ($num_result==0) {
  210. $created_dir = create_unexisting_directory($destination_course,api_get_user_id(),0, 0 ,$path.'document','/'.$my_temp,basename($my_temp));
  211. }
  212. }
  213. $my_temp .= '/';
  214. }
  215. }
  216. */
  217. /*
  218. echo '<br>';
  219. echo '------------------------';
  220. echo '<br>';
  221. echo '$doculent:';echo '<br>';
  222. echo print_r($document); echo '<br>';
  223. echo 'documlent->path'.$path.$document->path;echo '<br>';
  224. echo 'file option:'.$this->file_option; echo '<br>';
  225. echo 'filetype:'.$document->file_type ;
  226. echo '<br>';
  227. */
  228. if ($document->file_type == DOCUMENT) {
  229. if (file_exists($path.$document->path)) {
  230. switch ($this->file_option) {
  231. case FILE_OVERWRITE :
  232. $origin_path = $this->course->backup_path.'/'.$document->path;
  233. if (file_exists($origin_path)) {
  234. copy($origin_path, $path.$document->path);
  235. }
  236. $sql = "SELECT id FROM ".$table." WHERE path='/".substr($document->path, 9)."'";
  237. $res = Database::query($sql, __FILE__, __LINE__);
  238. $obj = Database::fetch_object($res);
  239. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id;
  240. $sql = "UPDATE ".$table." SET comment = '".Database::escape_string($document->comment)."', title='".Database::escape_string($document->title)."', size='".$document->size."' WHERE id = '".$obj->id."'";
  241. Database::query($sql, __FILE__, __LINE__);
  242. break;
  243. case FILE_SKIP :
  244. $sql = "SELECT id FROM ".$table." WHERE path='/".Database::escape_string(substr($document->path, 9))."'";
  245. $res = Database::query($sql, __FILE__, __LINE__);
  246. $obj = Database::fetch_object($res);
  247. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id;
  248. break;
  249. case FILE_RENAME :
  250. $i = 1;
  251. $ext = explode('.', basename($document->path));
  252. if (count($ext) > 1) {
  253. $ext = array_pop($ext);
  254. $file_name_no_ext = substr($document->path, 0, - (strlen($ext) + 1));
  255. $ext = '.'.$ext;
  256. } else {
  257. $ext = '';
  258. $file_name_no_ext = $document->path;
  259. }
  260. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  261. $file_exists = file_exists($path.$new_file_name);
  262. while ($file_exists) {
  263. $i ++;
  264. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  265. $file_exists = file_exists($path.$new_file_name);
  266. }
  267. if (!empty($session_id)) {
  268. $document_path = explode('/',$document->path,3);
  269. $course_path = $path; // "/var/www/wiener/courses/"
  270. $orig_base_folder = $document_path[1];
  271. $orig_base_path = $course_path.$document_path[0].'/'.$document_path[1];
  272. if (is_dir($orig_base_path)) {
  273. $new_base_foldername = $orig_base_folder; // e.g: "carpeta1"
  274. $new_base_path = $orig_base_path; // e.g: "/var/www/wiener/courses/CURSO4/document/carpeta1"
  275. if ($_SESSION['orig_base_foldername'] != $new_base_foldername) {
  276. unset($_SESSION['new_base_foldername']);
  277. unset($_SESSION['orig_base_foldername']);
  278. unset($_SESSION['new_base_path']);
  279. }
  280. $folder_exists = file_exists($new_base_path);
  281. if ($folder_exists) {
  282. $_SESSION['orig_base_foldername'] = $new_base_foldername; // e.g: carpeta1 in session
  283. $x = '';
  284. while ($folder_exists) {
  285. $x = $x + 1;
  286. $new_base_foldername = $document_path[1].'_'.$x;
  287. $new_base_path = $orig_base_path.'_'.$x;
  288. if ($_SESSION['new_base_foldername'] == $new_base_foldername) break;
  289. $folder_exists = file_exists($new_base_path);
  290. }
  291. $_SESSION['new_base_foldername'] = $new_base_foldername;
  292. $_SESSION['new_base_path'] = $new_base_path;
  293. }
  294. if (isset($_SESSION['new_base_foldername']) && isset($_SESSION['new_base_path'])) {
  295. $new_base_foldername = $_SESSION['new_base_foldername'];
  296. $new_base_path = $_SESSION['new_base_path'];
  297. }
  298. $dest_document_path = $new_base_path.'/'.$document_path[2]; // e.g: "/var/www/wiener/courses/CURSO4/document/carpeta1_1/subcarpeta1/collaborative.png"
  299. $basedir_dest_path = dirname($dest_document_path); // e.g: "/var/www/wiener/courses/CURSO4/document/carpeta1_1/subcarpeta1"
  300. $dest_filename = basename($dest_document_path); // e.g: "collaborative.png"
  301. $base_path_document = $course_path.$document_path[0]; // e.g: "/var/www/wiener/courses/CURSO4/document"
  302. $course_info = api_get_course_info($destination_course_code);
  303. $path_title = '/'.$new_base_foldername.'/'.$document_path[2];
  304. copy_folder_course_session($basedir_dest_path, $base_path_document,$session_id,$course_info, $document);
  305. copy($course_path.$document->path, $dest_document_path);
  306. $sql = "INSERT INTO $table SET path = '$path_title', comment = '".Database::escape_string($document->comment)."', title = '".Database::escape_string(basename($path_title))."' ,filetype='".$document->file_type."', size= '".$document->size."', session_id = '$session_id'";
  307. Database::query($sql, __FILE__, __LINE__);
  308. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = Database::insert_id();
  309. } else {
  310. copy($path.$document->path, $path.$new_file_name);
  311. $sql = "INSERT INTO ".$table." SET path = '/".Database::escape_string(substr($new_file_name, 9))."', comment = '".Database::escape_string($document->comment)."', title = '".Database::escape_string($document->title)."' ,filetype='".$document->file_type."', size= '".$document->size."', session_id = '$session_id'";
  312. Database::query($sql, __FILE__, __LINE__);
  313. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = Database::insert_id();
  314. }
  315. } else {
  316. copy($this->course->backup_path.'/'.$document->path, $path.$new_file_name);
  317. $sql = "INSERT INTO ".$table." SET path = '/".Database::escape_string(substr($new_file_name, 9))."', comment = '".Database::escape_string($document->comment)."', title = '".Database::escape_string($document->title)."' ,filetype='".$document->file_type."', size= '".$document->size."'";
  318. Database::query($sql, __FILE__, __LINE__);
  319. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = Database::insert_id();
  320. }
  321. break;
  322. } // end switch
  323. } // end if file exists
  324. else
  325. {
  326. //make sure the source file actually exists
  327. //echo $this->course->backup_path.'/'.$document->path;
  328. if(is_file($this->course->backup_path.'/'.$document->path) && is_readable($this->course->backup_path.'/'.$document->path) && is_dir(dirname($path.$document->path)) && is_writeable(dirname($path.$document->path)))
  329. {
  330. copy($this->course->backup_path.'/'.$document->path, $path.$document->path);
  331. $sql = "INSERT INTO ".$table." SET path = '/".substr($document->path, 9)."', comment = '".Database::escape_string($document->comment)."', title = '".Database::escape_string($document->title)."' ,filetype='".$document->file_type."', size= '".$document->size."'";
  332. Database::query($sql, __FILE__, __LINE__);
  333. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = Database::insert_id();
  334. }
  335. else
  336. {
  337. if(is_file($this->course->backup_path.'/'.$document->path) && is_readable($this->course->backup_path.'/'.$document->path))
  338. {
  339. error_log('Course copy generated an ignoreable error while trying to copy '.$this->course->backup_path.'/'.$document->path.': file not found');
  340. }
  341. if(!is_dir(dirname($path.$document->path)))
  342. {
  343. error_log('Course copy generated an ignoreable error while trying to copy to '.dirname($path.$document->path).': directory not found');
  344. }
  345. if(!is_writeable(dirname($path.$document->path)))
  346. {
  347. error_log('Course copy generated an ignoreable error while trying to copy to '.dirname($path.$document->path).': directory not writeable');
  348. }
  349. }
  350. } // end file doesn't exist
  351. }
  352. else
  353. {
  354. /*$sql = "SELECT id FROM ".$table." WHERE path = '/".Database::escape_string(substr($document->path, 9))."'";
  355. $res = Database::query($sql,__FILE__,__LINE__);
  356. if( Database::num_rows($res)> 0)
  357. {
  358. $obj = Database::fetch_object($res);
  359. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id;
  360. }
  361. else
  362. {
  363. $sql = "INSERT INTO ".$table." SET path = '/".Database::escape_string(substr($document->path, 9))."', comment = '".Database::escape_string($document->comment)."', title = '".Database::escape_string($document->title)."' ,filetype='".$document->file_type."', size= '".$document->size."'";
  364. Database::query($sql, __FILE__, __LINE__);
  365. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = Database::insert_id();
  366. }*/
  367. } // end folder
  368. } // end for each
  369. // Delete sessions for the copy the new folder in session
  370. unset($_SESSION['new_base_foldername']);
  371. unset($_SESSION['orig_base_foldername']);
  372. unset($_SESSION['new_base_path']);
  373. }
  374. }
  375. /**
  376. * Restore scorm documents
  377. * TODO @TODO check that the restore function with renaming doesn't break the scorm structure!
  378. */
  379. function restore_scorm_documents()
  380. {
  381. if ($this->course->has_resources(RESOURCE_SCORM))
  382. {
  383. $resources = $this->course->resources;
  384. foreach ($resources[RESOURCE_SCORM] as $id => $document)
  385. {
  386. $path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/';
  387. $perm = api_get_setting('permissions_for_new_directories');
  388. $perm = octdec(!empty($perm)?$perm:'0770');
  389. @mkdir(dirname($path.$document->path), $perm, true);
  390. if (file_exists($path.$document->path))
  391. {
  392. switch ($this->file_option)
  393. {
  394. case FILE_OVERWRITE :
  395. rmdirr($path.$document->path);
  396. copyDirTo($this->course->backup_path.'/'.$document->path, $path.dirname($document->path), false);
  397. break;
  398. case FILE_SKIP :
  399. break;
  400. case FILE_RENAME :
  401. $i = 1;
  402. $ext = explode('.', basename($document->path));
  403. if (count($ext) > 1)
  404. {
  405. $ext = array_pop($ext);
  406. $file_name_no_ext = substr($document->path, 0, - (strlen($ext) + 1));
  407. $ext = '.'.$ext;
  408. }
  409. else
  410. {
  411. $ext = '';
  412. $file_name_no_ext = $document->path;
  413. }
  414. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  415. $file_exists = file_exists($path.$new_file_name);
  416. while ($file_exists)
  417. {
  418. $i ++;
  419. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  420. $file_exists = file_exists($path.$new_file_name);
  421. }
  422. rename($this->course->backup_path.'/'.$document->path,$this->course->backup_path.'/'.$new_file_name);
  423. copyDirTo($this->course->backup_path.'/'.$new_file_name, $path.dirname($new_file_name), false);
  424. rename($this->course->backup_path.'/'.$new_file_name,$this->course->backup_path.'/'.$document->path);
  425. break;
  426. } // end switch
  427. } // end if file exists
  428. else
  429. {
  430. copyDirTo($this->course->backup_path.'/'.$document->path, $path.dirname($document->path), false);
  431. }
  432. } // end for each
  433. }
  434. }
  435. /**
  436. * Restore forums
  437. */
  438. function restore_forums()
  439. {
  440. if ($this->course->has_resources(RESOURCE_FORUM))
  441. {
  442. $table_forum = Database :: get_course_table(TABLE_FORUM, $this->course->destination_db);
  443. $table_topic = Database :: get_course_table(TABLE_FORUM_THREAD, $this->course->destination_db);
  444. $table_post = Database :: get_course_table(TABLE_FORUM_POST, $this->course->destination_db);
  445. $resources = $this->course->resources;
  446. foreach ($resources[RESOURCE_FORUM] as $id => $forum)
  447. {
  448. $cat_id = $this->restore_forum_category($forum->category_id);
  449. $sql = "INSERT INTO ".$table_forum.
  450. " SET forum_title = '".Database::escape_string($forum->title).
  451. "', forum_comment = '".Database::escape_string($forum->description).
  452. "', forum_category = ".(int)Database::escape_string($cat_id).
  453. ", forum_last_post = ".(int)Database::escape_string($forum->last_post).
  454. ", forum_threads = ".(int)Database::escape_string($forum->topics).
  455. ", forum_posts = ".(int)Database::escape_string($forum->posts).
  456. ", allow_anonymous = ".(int)Database::escape_string($forum->allow_anonymous).
  457. ", allow_edit = ".(int)Database::escape_string($forum->allow_edit).
  458. ", approval_direct_post = '".Database::escape_string($forum->approval_direct_post).
  459. "', allow_attachments = ".(int)Database::escape_string($forum->allow_attachments).
  460. ", allow_new_threads = ".(int)Database::escape_string($forum->allow_new_topics).
  461. ", default_view = '".Database::escape_string($forum->default_view).
  462. "', forum_of_group = '".Database::escape_string($forum->of_group).
  463. "', forum_group_public_private = '".Database::escape_string($forum->group_public_private).
  464. "', forum_order = ".(int)Database::escape_string($forum->order).
  465. ", locked = ".(int)Database::escape_string($forum->locked).
  466. ", session_id = ".(int)Database::escape_string($forum->session_id).
  467. ", forum_image = '".Database::escape_string($forum->image)."'";
  468. Database::query($sql, __FILE__, __LINE__);
  469. $new_id = Database::insert_id();
  470. $this->course->resources[RESOURCE_FORUM][$id]->destination_id = $new_id;
  471. $forum_topics = 0;
  472. if (is_array($this->course->resources[RESOURCE_FORUMTOPIC]))
  473. {
  474. foreach ($this->course->resources[RESOURCE_FORUMTOPIC] as $topic_id => $topic)
  475. {
  476. if ($topic->forum_id == $id)
  477. {
  478. $this->restore_topic($topic_id, $new_id);
  479. $forum_topics ++;
  480. }
  481. }
  482. }
  483. if ($forum_topics > 0)
  484. {
  485. $last_post = $this->course->resources[RESOURCE_FORUMPOST][$forum->last_post];
  486. $sql = "UPDATE ".$table_forum." SET forum_threads = ".$forum_topics.", forum_last_post = ".(int)$last_post->destination_id." WHERE forum_id = ".(int)$new_id;
  487. Database::query($sql, __FILE__, __LINE__);
  488. }
  489. }
  490. }
  491. }
  492. /**
  493. * Restore forum-categories
  494. */
  495. function restore_forum_category($id)
  496. {
  497. $forum_cat_table = Database :: get_course_table(TABLE_FORUM_CATEGORY, $this->course->destination_db);
  498. $resources = $this->course->resources;
  499. $forum_cat = $resources[RESOURCE_FORUMCATEGORY][$id];
  500. if (!$forum_cat->is_restored())
  501. {
  502. $title = $forum_cat->title;
  503. if (!preg_match('/.*\((.+)\)$/', $title, $matches)) // This is for avoiding repetitive adding of training code after several backup/restore cycles.
  504. {
  505. if ($matches[1] != $this->course->code)
  506. {
  507. $title = $title.' ('.$this->course->code.')';
  508. }
  509. }
  510. $sql = "INSERT INTO ".$forum_cat_table.
  511. " SET cat_title = '".Database::escape_string($title).
  512. "', cat_comment = '".Database::escape_string($forum_cat->description).
  513. "', cat_order = ".(int)Database::escape_string($forum_cat->order).
  514. ", locked = ".(int)Database::escape_string($forum_cat->locked).
  515. ", session_id = ".(int)Database::escape_string($forum_cat->session_id);
  516. Database::query($sql, __FILE__, __LINE__);
  517. $new_id = Database::insert_id();
  518. $this->course->resources[RESOURCE_FORUMCATEGORY][$id]->destination_id = $new_id;
  519. return $new_id;
  520. }
  521. return $this->course->resources[RESOURCE_FORUMCATEGORY][$id]->destination_id;
  522. }
  523. /**
  524. * Restore a forum-topic
  525. */
  526. function restore_topic($id, $forum_id)
  527. {
  528. $table = Database :: get_course_table(TABLE_FORUM_THREAD, $this->course->destination_db);
  529. $resources = $this->course->resources;
  530. $topic = $resources[RESOURCE_FORUMTOPIC][$id];
  531. $sql = "INSERT INTO ".$table.
  532. " SET thread_title = '".Database::escape_string($topic->title).
  533. "', forum_id = ".(int)Database::escape_string($forum_id).
  534. ", thread_date = '".Database::escape_string($topic->time).
  535. "', thread_poster_id = ".(int)Database::escape_string($topic->topic_poster_id).
  536. ", thread_poster_name = '".Database::escape_string($topic->topic_poster_name).
  537. "', thread_views = ".(int)Database::escape_string($topic->views).
  538. ", thread_sticky = ".(int)Database::escape_string($topic->sticky).
  539. ", locked = ".(int)Database::escape_string($topic->locked).
  540. ", thread_close_date = '".Database::escape_string($topic->time_closed).
  541. "', thread_weight = ".(float)Database::escape_string($topic->weight).
  542. ", thread_title_qualify = '".Database::escape_string($topic->title_qualify).
  543. "', thread_qualify_max = ".(float)Database::escape_string($topic->qualify_max);
  544. Database::query($sql, __FILE__, __LINE__);
  545. $new_id = Database::insert_id();
  546. $this->course->resources[RESOURCE_FORUMTOPIC][$id]->destination_id = $new_id;
  547. $topic_replies = -1;
  548. foreach ($this->course->resources[RESOURCE_FORUMPOST] as $post_id => $post)
  549. {
  550. if ($post->topic_id == $id)
  551. {
  552. $topic_replies ++;
  553. $this->restore_post($post_id, $new_id, $forum_id);
  554. }
  555. }
  556. $last_post = $this->course->resources[RESOURCE_FORUMPOST][$topic->last_post];
  557. if (is_object($last_post))
  558. {
  559. $sql = "UPDATE ".$table." SET thread_last_post = ".(int)$last_post->destination_id;
  560. Database::query($sql, __FILE__, __LINE__);
  561. }
  562. if ($topic_replies >= 0)
  563. {
  564. $sql = "UPDATE ".$table." SET thread_replies = ".$topic_replies;
  565. Database::query($sql, __FILE__, __LINE__);
  566. }
  567. return $new_id;
  568. }
  569. /**
  570. * Restore a forum-post
  571. * @TODO Restore tree-structure of posts. For example: attachments to posts.
  572. */
  573. function restore_post($id, $topic_id, $forum_id)
  574. {
  575. $table_post = Database :: get_course_table(TABLE_FORUM_POST, $this->course->destination_db);
  576. $resources = $this->course->resources;
  577. $post = $resources[RESOURCE_FORUMPOST][$id];
  578. $sql = "INSERT INTO ".$table_post.
  579. " SET post_title = '".Database::escape_string($post->title).
  580. "', post_text = '".Database::escape_string($post->text).
  581. "', thread_id = ".(int)Database::escape_string($topic_id).
  582. ", post_date = '".Database::escape_string($post->post_time).
  583. "', forum_id = ".(int)Database::escape_string($forum_id).
  584. ", poster_id = ".(int)Database::escape_string($post->poster_id).
  585. ", poster_name = '".Database::escape_string($post->poster_name).
  586. "', post_notification = ".(int)Database::escape_string($post->topic_notify).
  587. ", post_parent_id = ".(int)Database::escape_string($post->parent_post_id).
  588. ", visible = ".(int)Database::escape_string($post->visible);
  589. Database::query($sql, __FILE__, __LINE__);
  590. $new_id = Database::insert_id();
  591. $this->course->resources[RESOURCE_FORUMPOST][$id]->destination_id = $new_id;
  592. return $new_id;
  593. }
  594. /**
  595. * Restore links
  596. */
  597. function restore_links($session_id = 0)
  598. {
  599. if ($this->course->has_resources(RESOURCE_LINK))
  600. {
  601. $link_table = Database :: get_course_table(TABLE_LINK, $this->course->destination_db);
  602. $resources = $this->course->resources;
  603. foreach ($resources[RESOURCE_LINK] as $id => $link)
  604. {
  605. $cat_id = $this->restore_link_category($link->category_id,$session_id);
  606. $sql = "SELECT MAX(display_order) FROM $link_table WHERE category_id='" . Database::escape_string($cat_id). "'";
  607. $result = Database::query($sql, __FILE__, __LINE__);
  608. list($max_order) = Database::fetch_array($result);
  609. $condition_session = "";
  610. if (!empty($session_id)) {
  611. $condition_session = " , session_id = '$session_id' ";
  612. }
  613. $sql = "INSERT INTO ".$link_table." SET url = '".Database::escape_string($link->url)."', title = '".Database::escape_string($link->title)."', description = '".Database::escape_string($link->description)."', category_id='".$cat_id."', on_homepage = '".$link->on_homepage."', display_order='".($max_order+1)."' $condition_session";
  614. Database::query($sql, __FILE__, __LINE__);
  615. $this->course->resources[RESOURCE_LINK][$id]->destination_id = Database::insert_id();
  616. }
  617. }
  618. }
  619. /**
  620. * Restore tool intro
  621. */
  622. function restore_tool_intro()
  623. {
  624. if ($this->course->has_resources(RESOURCE_TOOL_INTRO))
  625. {
  626. $tool_intro_table = Database :: get_course_table(TABLE_TOOL_INTRO, $this->course->destination_db);
  627. $resources = $this->course->resources;
  628. foreach ($resources[RESOURCE_TOOL_INTRO] as $id => $tool_intro)
  629. {
  630. $sql = "DELETE FROM ".$tool_intro_table." WHERE id='".Database::escape_string($tool_intro->id)."'";
  631. Database::query($sql, __FILE__, __LINE__);
  632. $sql = "INSERT INTO ".$tool_intro_table." SET id='".Database::escape_string($tool_intro->id)."', intro_text = '".Database::escape_string($tool_intro->intro_text)."'";
  633. Database::query($sql, __FILE__, __LINE__);
  634. $this->course->resources[RESOURCE_TOOL_INTRO][$id]->destination_id = Database::insert_id();
  635. }
  636. }
  637. }
  638. /**
  639. * Restore a link-category
  640. */
  641. function restore_link_category($id,$session_id = 0)
  642. {
  643. $condition_session = "";
  644. if (!empty($session_id)) {
  645. $condition_session = " , session_id = '$session_id' ";
  646. }
  647. if ($id == 0)
  648. return 0;
  649. $link_cat_table = Database :: get_course_table(TABLE_LINK_CATEGORY, $this->course->destination_db);
  650. $resources = $this->course->resources;
  651. $link_cat = $resources[RESOURCE_LINKCATEGORY][$id];
  652. if (is_object($link_cat) && !$link_cat->is_restored())
  653. {
  654. $sql = "SELECT MAX(display_order) FROM $link_cat_table";
  655. $result=Database::query($sql,__FILE__,__LINE__);
  656. list($orderMax)=Database::fetch_array($result,'NUM');
  657. $display_order=$orderMax+1;
  658. $sql = "INSERT INTO ".$link_cat_table." SET category_title = '".Database::escape_string($link_cat->title)."', description='".Database::escape_string($link_cat->description)."', display_order='".$display_order."' $condition_session ";
  659. Database::query($sql, __FILE__, __LINE__);
  660. $new_id = Database::insert_id();
  661. $this->course->resources[RESOURCE_LINKCATEGORY][$id]->destination_id = $new_id;
  662. return $new_id;
  663. }
  664. return $this->course->resources[RESOURCE_LINKCATEGORY][$id]->destination_id;
  665. }
  666. /**
  667. * Restore events
  668. */
  669. function restore_events()
  670. {
  671. if ($this->course->has_resources(RESOURCE_EVENT))
  672. {
  673. $table = Database :: get_course_table(TABLE_AGENDA, $this->course->destination_db);
  674. $resources = $this->course->resources;
  675. foreach ($resources[RESOURCE_EVENT] as $id => $event)
  676. {
  677. $sql = "INSERT INTO ".$table." SET title = '".Database::escape_string($event->title)."', content = '".Database::escape_string($event->content)."', start_date = '".$event->start_date."', end_date = '".$event->end_date."'";
  678. Database::query($sql, __FILE__, __LINE__);
  679. $this->course->resources[RESOURCE_EVENT][$id]->destination_id = Database::insert_id();
  680. }
  681. }
  682. }
  683. /**
  684. * Restore course-description
  685. */
  686. function restore_course_descriptions($session_id = 0)
  687. {
  688. if ($this->course->has_resources(RESOURCE_COURSEDESCRIPTION))
  689. {
  690. $table = Database :: get_course_table(TABLE_COURSE_DESCRIPTION, $this->course->destination_db);
  691. $resources = $this->course->resources;
  692. foreach ($resources[RESOURCE_COURSEDESCRIPTION] as $id => $cd)
  693. {
  694. if (isset($_POST['destination_course'])) {
  695. $course_destination=Security::remove_XSS($_POST['destination_course']);
  696. $course_destination=api_get_course_info($course_destination);
  697. $course_destination=$course_destination['path'];
  698. } else {
  699. $course_destination=$this->course->destination_path;
  700. }
  701. $course_info=api_get_course_info(api_get_course_id());
  702. $search='../courses/'.$course_info['path'].'/document';
  703. $replace_search_by='../courses/'.$course_destination.'/document';
  704. $description_content=str_replace($search,$replace_search_by,$cd->content);
  705. $condition_session = "";
  706. if (!empty($session_id)) {
  707. $session_id = intval($session_id);
  708. $condition_session = " , session_id = '$session_id' ";
  709. }
  710. $sql = "INSERT INTO ".$table." SET description_type = '".Database::escape_string($cd->description_type)."',title = '".Database::escape_string($cd->title)."', content = '".Database::escape_string($description_content)."' $condition_session";
  711. Database::query($sql, __FILE__, __LINE__);
  712. $this->course->resources[RESOURCE_COURSEDESCRIPTION][$id]->destination_id = Database::insert_id();
  713. }
  714. }
  715. }
  716. /**
  717. * Restore announcements
  718. */
  719. function restore_announcements()
  720. {
  721. if ($this->course->has_resources(RESOURCE_ANNOUNCEMENT))
  722. {
  723. $table = Database :: get_course_table(TABLE_ANNOUNCEMENT, $this->course->destination_db);
  724. $resources = $this->course->resources;
  725. foreach ($resources[RESOURCE_ANNOUNCEMENT] as $id => $announcement)
  726. {
  727. $sql = "INSERT INTO ".$table." " .
  728. "SET title = '".Database::escape_string($announcement->title)."'," .
  729. "content = '".Database::escape_string($announcement->content)."', " .
  730. "end_date = '".$announcement->date."', " .
  731. "display_order = '".$announcement->display_order."', " .
  732. "email_sent = '".$announcement->email_sent."'";
  733. Database::query($sql, __FILE__, __LINE__);
  734. $this->course->resources[RESOURCE_ANNOUNCEMENT][$id]->destination_id = Database::insert_id();
  735. }
  736. }
  737. }
  738. /**
  739. * Restore Quiz
  740. */
  741. function restore_quizzes($session_id = 0)
  742. {
  743. if ($this->course->has_resources(RESOURCE_QUIZ))
  744. {
  745. $table_qui = Database :: get_course_table(TABLE_QUIZ_TEST, $this->course->destination_db);
  746. $table_rel = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION, $this->course->destination_db);
  747. $table_doc = Database :: get_course_table(TABLE_DOCUMENT, $this->course->destination_db);
  748. $resources = $this->course->resources;
  749. foreach ($resources[RESOURCE_QUIZ] as $id => $quiz)
  750. {
  751. $doc = '';
  752. if (strlen($quiz->media) > 0)
  753. {
  754. if ($this->course->resources[RESOURCE_DOCUMENT][$quiz->media]->is_restored())
  755. {
  756. $sql = "SELECT path FROM ".$table_doc." WHERE id = ".$resources[RESOURCE_DOCUMENT][$quiz->media]->destination_id;
  757. $doc = Database::query($sql, __FILE__, __LINE__);
  758. $doc = Database::fetch_object($doc);
  759. $doc = str_replace('/audio/', '', $doc->path);
  760. }
  761. }
  762. if ($id != -1)
  763. {
  764. $condition_session = "";
  765. if (!empty($session_id)) {
  766. $session_id = intval($session_id);
  767. $condition_session = " , session_id = '$session_id' ";
  768. }
  769. // Normal tests are stored in the database.
  770. $sql = "INSERT INTO ".$table_qui.
  771. " SET title = '".Database::escape_string($quiz->title).
  772. "', description = '".Database::escape_string($quiz->description).
  773. "', type = '".$quiz->quiz_type.
  774. "', random = '".$quiz->random.
  775. "', active = '".$quiz->active.
  776. "', sound = '".Database::escape_string($doc).
  777. "', max_attempt = ".(int)$quiz->attempts.
  778. ", results_disabled = ".(int)$quiz->results_disabled.
  779. ", access_condition = '".$quiz->access_condition.
  780. "', start_time = '".$quiz->start_time.
  781. "', end_time = '".$quiz->end_time.
  782. "', feedback_type = ".(int)$quiz->feedback_type.
  783. ", random_answers = ".(int)$quiz->random_answers.
  784. ", expired_time = ".(int)$quiz->expired_time.
  785. $condition_session;
  786. Database::query($sql, __FILE__, __LINE__);
  787. $new_id = Database::insert_id();
  788. } else {
  789. // $id = -1 identifies the fictionary test for collecting orphan questions. We do not store it in the database.
  790. $new_id = -1;
  791. }
  792. $this->course->resources[RESOURCE_QUIZ][$id]->destination_id = $new_id;
  793. foreach ($quiz->question_ids as $index => $question_id) {
  794. $qid = $this->restore_quiz_question($question_id);
  795. $sql = "INSERT IGNORE INTO ".$table_rel." SET question_id = ".$qid.", exercice_id = ".$new_id.", question_order = ".$quiz->question_orders[$index]."";
  796. Database::query($sql, __FILE__, __LINE__);
  797. }
  798. }
  799. }
  800. }
  801. /**
  802. * Restore quiz-questions
  803. */
  804. function restore_quiz_question($id)
  805. {
  806. $resources = $this->course->resources;
  807. $question = $resources[RESOURCE_QUIZQUESTION][$id];
  808. $new_id=0;
  809. if(is_object($question))
  810. {
  811. if ($question->is_restored())
  812. {
  813. return $question->destination_id;
  814. }
  815. $table_que = Database :: get_course_table(TABLE_QUIZ_QUESTION, $this->course->destination_db);
  816. $table_ans = Database :: get_course_table(TABLE_QUIZ_ANSWER, $this->course->destination_db);
  817. $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)."', picture='".addslashes($question->picture)."', level='".addslashes($question->level)."'";
  818. Database::query($sql, __FILE__, __LINE__);
  819. $new_id = Database::insert_id();
  820. if ($question->quiz_type == 4) { // for answer type matching
  821. foreach ($question->answers as $index => $answer) {
  822. $sql = "INSERT INTO ".$table_ans." SET id= '".$answer['id']."',question_id = '".$new_id."', answer = '".Database::escape_string($answer['answer'])."', correct = '".$answer['correct']."', comment = '".Database::escape_string($answer['comment'])."', ponderation='".$answer['ponderation']."', position = '".$answer['position']."', hotspot_coordinates = '".$answer['hotspot_coordinates']."', hotspot_type = '".$answer['hotspot_type']."'";
  823. Database::query($sql, __FILE__, __LINE__);
  824. }
  825. } else {
  826. foreach ($question->answers as $index => $answer) {
  827. $sql = "INSERT INTO ".$table_ans." SET id= '". ($index +1)."',question_id = '".$new_id."', answer = '".Database::escape_string($answer['answer'])."', correct = '".$answer['correct']."', comment = '".Database::escape_string($answer['comment'])."', ponderation='".$answer['ponderation']."', position = '".$answer['position']."', hotspot_coordinates = '".$answer['hotspot_coordinates']."', hotspot_type = '".$answer['hotspot_type']."'";
  828. Database::query($sql, __FILE__, __LINE__);
  829. }
  830. }
  831. $this->course->resources[RESOURCE_QUIZQUESTION][$id]->destination_id = $new_id;
  832. }
  833. return $new_id;
  834. }
  835. /**
  836. * Restore surveys
  837. */
  838. function restore_surveys()
  839. {
  840. if ($this->course->has_resources(RESOURCE_SURVEY))
  841. {
  842. $table_sur = Database :: get_course_table(TABLE_SURVEY, $this->course->destination_db);
  843. $table_que = Database :: get_course_table(TABLE_SURVEY_QUESTION, $this->course->destination_db);
  844. $table_ans = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION, $this->course->destination_db);
  845. $resources = $this->course->resources;
  846. foreach ($resources[RESOURCE_SURVEY] as $id => $survey)
  847. {
  848. $sql_check = 'SELECT survey_id FROM '.$table_sur.'
  849. WHERE code = "'.Database::escape_string($survey->code).'"
  850. AND lang="'.Database::escape_string($survey->lang).'"
  851. ';
  852. $result_check = Database::query($sql_check, __FILE__, __LINE__);
  853. $doc = '';
  854. $sql = "INSERT INTO ".$table_sur." " .
  855. "SET code = '".Database::escape_string($survey->code)."', " .
  856. "title = '".Database::escape_string($survey->title)."', " .
  857. "subtitle = '".Database::escape_string($survey->subtitle)."', " .
  858. "author = '".Database::escape_string($survey->author)."', " .
  859. "lang = '".Database::escape_string($survey->lang)."', " .
  860. "avail_from = '".Database::escape_string($survey->avail_from)."', " .
  861. "avail_till = '".Database::escape_string($survey->avail_till)."', " .
  862. "is_shared = '".Database::escape_string($survey->is_shared)."', " .
  863. "template = '".Database::escape_string($survey->template)."', " .
  864. "intro = '".Database::escape_string($survey->intro)."', " .
  865. "surveythanks = '".Database::escape_string($survey->surveythanks)."', " .
  866. "creation_date = '".Database::escape_string($survey->creation_date)."', " .
  867. "invited = '0', " .
  868. "answered = '0', " .
  869. "invite_mail = '".Database::escape_string($survey->invite_mail)."', " .
  870. "reminder_mail = '".Database::escape_string($survey->reminder_mail)."'";
  871. //An existing survey exists with the same code and the same language
  872. if(Database::num_rows($result_check) == 1)
  873. {
  874. switch ($this->file_option) {
  875. case FILE_SKIP:
  876. //Do nothing
  877. break;
  878. case FILE_RENAME:
  879. $survey_code = $survey->code.'_';
  880. $i=1;
  881. $temp_survey_code = $survey_code.$i;
  882. while (!$this->is_survey_code_available($temp_survey_code))
  883. {
  884. $temp_survey_code = $survey_code.++$i;
  885. }
  886. $survey_code = $temp_survey_code;
  887. $sql = "INSERT INTO ".$table_sur." " .
  888. "SET code = '".Database::escape_string($survey_code)."', " .
  889. "title = '".Database::escape_string($survey->title)."', " .
  890. "subtitle = '".Database::escape_string($survey->subtitle)."', " .
  891. "author = '".Database::escape_string($survey->author)."', " .
  892. "lang = '".Database::escape_string($survey->lang)."', " .
  893. "avail_from = '".Database::escape_string($survey->avail_from)."', " .
  894. "avail_till = '".Database::escape_string($survey->avail_till)."', " .
  895. "is_shared = '".Database::escape_string($survey->is_shared)."', " .
  896. "template = '".Database::escape_string($survey->template)."', " .
  897. "intro = '".Database::escape_string($survey->intro)."', " .
  898. "surveythanks = '".Database::escape_string($survey->surveythanks)."', " .
  899. "creation_date = '".Database::escape_string($survey->creation_date)."', " .
  900. "invited = '0', " .
  901. "answered = '0', " .
  902. "invite_mail = '".Database::escape_string($survey->invite_mail)."', " .
  903. "reminder_mail = '".Database::escape_string($survey->reminder_mail)."'";
  904. //Insert the new source survey
  905. Database::query($sql, __FILE__, __LINE__);
  906. $new_id = Database::insert_id();
  907. $this->course->resources[RESOURCE_SURVEY][$id]->destination_id = $new_id;
  908. foreach ($survey->question_ids as $index => $question_id)
  909. {
  910. $qid = $this->restore_survey_question($question_id);
  911. $sql = "UPDATE ".$table_que." " .
  912. "SET survey_id = ".$new_id." WHERE " .
  913. "question_id = ".$qid."";
  914. Database::query($sql, __FILE__, __LINE__);
  915. $sql = "UPDATE ".$table_ans." ".
  916. "SET survey_id = ".$new_id." WHERE " .
  917. "question_id = ".$qid."";
  918. Database::query($sql, __FILE__, __LINE__);
  919. }
  920. break;
  921. case FILE_OVERWRITE:
  922. // Delete the existing survey with the same code and language and import the one of the source course
  923. // getting the information of the survey (used for when the survey is shared)
  924. require_once(api_get_path(SYS_CODE_PATH).'survey/survey.lib.php');
  925. $sql_select_existing_survey = "SELECT * FROM $table_sur WHERE survey_id='".Database::escape_string(Database::result($result_check,0,0))."'";
  926. $result = Database::query($sql_select_existing_survey, __FILE__, __LINE__);
  927. $survey_data = Database::fetch_array($result,'ASSOC');
  928. // if the survey is shared => also delete the shared content
  929. if (is_numeric($survey_data['survey_share']))
  930. {
  931. survey_manager::delete_survey($survey_data['survey_share'], true,$this->course->destination_db);
  932. }
  933. $return = survey_manager :: delete_survey($survey_data['survey_id'],false,$this->course->destination_db);
  934. //Insert the new source survey
  935. Database::query($sql, __FILE__, __LINE__);
  936. $new_id = Database::insert_id();
  937. $this->course->resources[RESOURCE_SURVEY][$id]->destination_id = $new_id;
  938. foreach ($survey->question_ids as $index => $question_id)
  939. {
  940. $qid = $this->restore_survey_question($question_id);
  941. $sql = "UPDATE ".$table_que." " .
  942. "SET survey_id = ".$new_id." WHERE " .
  943. "question_id = ".$qid."";
  944. Database::query($sql, __FILE__, __LINE__);
  945. $sql = "UPDATE ".$table_ans." ".
  946. "SET survey_id = ".$new_id." WHERE " .
  947. "question_id = ".$qid."";
  948. Database::query($sql, __FILE__, __LINE__);
  949. }
  950. break;
  951. default:
  952. break;
  953. }
  954. }
  955. //No existing survey with the same language and the same code, we just copy the survey
  956. else
  957. {
  958. Database::query($sql, __FILE__, __LINE__);
  959. $new_id = Database::insert_id();
  960. $this->course->resources[RESOURCE_SURVEY][$id]->destination_id = $new_id;
  961. foreach ($survey->question_ids as $index => $question_id)
  962. {
  963. $qid = $this->restore_survey_question($question_id);
  964. $sql = "UPDATE ".$table_que." " .
  965. "SET survey_id = ".$new_id." WHERE " .
  966. "question_id = ".$qid."";
  967. Database::query($sql, __FILE__, __LINE__);
  968. $sql = "UPDATE ".$table_ans." ".
  969. "SET survey_id = ".$new_id." WHERE " .
  970. "question_id = ".$qid."";
  971. Database::query($sql, __FILE__, __LINE__);
  972. }
  973. }
  974. }
  975. }
  976. }
  977. /**
  978. * Check availability of a survey code
  979. */
  980. function is_survey_code_available($survey_code)
  981. {
  982. $table_sur = Database :: get_course_table(TABLE_SURVEY, $this->course->destination_db);
  983. $sql = "SELECT * FROM $table_sur WHERE code='".Database::escape_string($survey_code)."'";
  984. $result = Database::query($sql, __FILE__, __LINE__);
  985. if(Database::num_rows($result) > 0) return false; else return true;
  986. }
  987. /**
  988. * Restore survey-questions
  989. */
  990. function restore_survey_question($id)
  991. {
  992. $resources = $this->course->resources;
  993. $question = $resources[RESOURCE_SURVEYQUESTION][$id];
  994. $new_id=0;
  995. if(is_object($question))
  996. {
  997. if ($question->is_restored())
  998. {
  999. return $question->destination_id;
  1000. }
  1001. $table_que = Database :: get_course_table(TABLE_SURVEY_QUESTION, $this->course->destination_db);
  1002. $table_ans = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION, $this->course->destination_db);
  1003. $sql = "INSERT INTO ".$table_que." " .
  1004. "SET survey_id = '".Database::escape_string($question->survey_id)."', " .
  1005. "survey_question = '".Database::escape_string($question->survey_question)."', " .
  1006. "survey_question_comment = '".Database::escape_string($question->survey_question_comment)."', " .
  1007. "type = '".Database::escape_string($question->survey_question_type)."', " .
  1008. "display = '".Database::escape_string($question->display)."', " .
  1009. "sort = '".Database::escape_string($question->sort)."', " .
  1010. "shared_question_id = '".Database::escape_string($question->shared_question_id)."', " .
  1011. "max_value = '".Database::escape_string($question->max_value)."' ";
  1012. Database::query($sql, __FILE__, __LINE__);
  1013. $new_id = Database::insert_id();
  1014. foreach ($question->answers as $index => $answer) {
  1015. $sql = "INSERT INTO ".$table_ans." " .
  1016. "SET " .
  1017. "question_id = '".Database::escape_string($new_id)."', " .
  1018. "option_text = '".Database::escape_string($answer['option_text'])."', " .
  1019. "sort = '".Database::escape_string($answer['sort'])."', " .
  1020. "survey_id = '".Database::escape_string($question->survey_id)."'";
  1021. Database::query($sql, __FILE__, __LINE__);
  1022. }
  1023. $this->course->resources[RESOURCE_SURVEYQUESTION][$id]->destination_id = $new_id;
  1024. }
  1025. return $new_id;
  1026. }
  1027. /**
  1028. * Restore learnpaths
  1029. */
  1030. function restore_learnpaths($session_id = 0)
  1031. {
  1032. if ($this->course->has_resources(RESOURCE_LEARNPATH)) {
  1033. $table_main = Database :: get_course_table(TABLE_LP_MAIN, $this->course->destination_db);
  1034. $table_item = Database :: get_course_table(TABLE_LP_ITEM, $this->course->destination_db);
  1035. $table_tool = Database::get_course_table(TABLE_TOOL_LIST, $this->course->destination_db);
  1036. $resources = $this->course->resources;
  1037. foreach ($resources[RESOURCE_LEARNPATH] as $id => $lp) {
  1038. $condition_session = "";
  1039. if (!empty($session_id)) {
  1040. $session_id = intval($session_id);
  1041. $condition_session = " , session_id = '$session_id' ";
  1042. }
  1043. $sql = "INSERT INTO ".$table_main." " .
  1044. "SET lp_type = '".$lp->lp_type."', " .
  1045. "name = '".Database::escape_string($lp->name)."', " .
  1046. "path = '".Database::escape_string($lp->path)."', " .
  1047. "ref = '".$lp->ref."', " .
  1048. "description = '".Database::escape_string($lp->description)."', " .
  1049. "content_local = '".Database::escape_string($lp->content_local)."', " .
  1050. "default_encoding = '".Database::escape_string($lp->default_encoding)."', " .
  1051. "default_view_mod = '".Database::escape_string($lp->default_view_mod)."', " .
  1052. "prevent_reinit = '".Database::escape_string($lp->prevent_reinit)."', " .
  1053. "force_commit = '".Database::escape_string($lp->force_commit)."', " .
  1054. "content_maker = '".Database::escape_string($lp->content_maker)."', " .
  1055. "display_order = '".Database::escape_string($lp->display_order)."', " .
  1056. "js_lib= '".Database::escape_string($lp->js_lib)."', " .
  1057. "content_license= '".Database::escape_string($lp->content_license)."', " .
  1058. "debug= '".Database::escape_string($lp->debug)."' $condition_session ";
  1059. Database::query($sql, __FILE__, __LINE__);
  1060. $new_lp_id = Database::insert_id();
  1061. if($lp->visibility) {
  1062. $sql = "INSERT INTO $table_tool SET name='".Database::escape_string($lp->name)."', link='newscorm/lp_controller.php?action=view&lp_id=$new_lp_id', image='scormbuilder.gif', visibility='1', admin='0', address='squaregrey.gif'";
  1063. Database::query($sql, __FILE__, __LINE__);
  1064. }
  1065. $new_item_ids = array();
  1066. $parent_item_ids = array();
  1067. $previous_item_ids = array();
  1068. $next_item_ids = array();
  1069. $old_prerequisite = array();
  1070. $old_refs = array();
  1071. $prerequisite_ids = array();
  1072. foreach ($lp->get_items() as $index => $item) {
  1073. /*
  1074. if ($item['id'] != 0)
  1075. {
  1076. // Links in learnpath have types 'Link _self' or 'Link _blank'. We only need 'Link' here.
  1077. $type_parts = explode(' ',$item['type']);
  1078. $item['id'] = $this->course->resources[$type_parts[0]][$item['id']]->destination_id;
  1079. }
  1080. */
  1081. /*
  1082. //Get the new ref ID for all items that are not sco (dokeos quizzes, documents, etc)
  1083. $ref = '';
  1084. if(!empty($item['ref']) && $lp->lp_type!='2'){
  1085. $ref = $this->get_new_id($item['item_type'],$item['ref']);
  1086. } else {
  1087. $ref = $item['ref'];
  1088. }*/
  1089. // we set the ref code here and then we update in a for loop
  1090. $ref = $item['ref'];
  1091. //Dealing with path the same way as ref as some data has been put into path when it's a
  1092. //local resource
  1093. $path = Database::escape_string($item['path']);
  1094. if(strval(intval($path)) === $path) {
  1095. if (!empty($session_id)) {
  1096. $path = intval($path);
  1097. } else {
  1098. $path = $this->get_new_id($item['item_type'],$path);
  1099. }
  1100. }
  1101. $sql = "INSERT INTO ".$table_item." SET " .
  1102. "lp_id = '".Database::escape_string($new_lp_id)."', " .
  1103. "item_type='".Database::escape_string($item['item_type'])."', " .
  1104. "ref = '".Database::escape_string($ref)."', " .
  1105. "title = '".Database::escape_string($item['title'])."', " .
  1106. "description ='".Database::escape_string($item['description'])."', " .
  1107. "path = '".Database::escape_string($path)."', " .
  1108. "min_score = '".Database::escape_string($item['min_score'])."', " .
  1109. "max_score = '".Database::escape_string($item['max_score'])."', " .
  1110. "mastery_score = '".Database::escape_string($item['mastery_score'])."', " .
  1111. "parent_item_id = '".Database::escape_string($item['parent_item_id'])."', " .
  1112. "previous_item_id = '".Database::escape_string($item['previous_item_id'])."', " .
  1113. "next_item_id = '".Database::escape_string($item['next_item_id'])."', " .
  1114. "display_order = '".Database::escape_string($item['display_order'])."', " .
  1115. "prerequisite = '".Database::escape_string($item['prerequisite'])."', " .
  1116. "parameters='".Database::escape_string($item['parameters'])."', " .
  1117. "launch_data = '".Database::escape_string($item['launch_dataprereq_type'])."'";
  1118. Database::query($sql, __FILE__, __LINE__);
  1119. $new_item_id = Database::insert_id();
  1120. //save a link between old and new item IDs
  1121. $new_item_ids[$item['id']] = $new_item_id;
  1122. //save a reference of items that need a parent_item_id refresh
  1123. $parent_item_ids[$new_item_id] = $item['parent_item_id'];
  1124. //save a reference of items that need a previous_item_id refresh
  1125. $previous_item_ids[$new_item_id] = $item['previous_item_id'];
  1126. //save a reference of items that need a next_item_id refresh
  1127. $next_item_ids[$new_item_id] = $item['next_item_id'];
  1128. if (!empty($item['prerequisite'])) {
  1129. if ($lp->lp_type =='2') {
  1130. // if is an sco
  1131. $old_prerequisite[$new_item_id]= $item['prerequisite'];
  1132. } else {
  1133. $old_prerequisite[$new_item_id]= $new_item_ids[$item['prerequisite']];
  1134. }
  1135. }
  1136. if (!empty($ref)) {
  1137. if ($lp->lp_type =='2') {
  1138. // if is an sco
  1139. $old_refs[$new_item_id]= $ref;
  1140. } else {
  1141. $old_refs[$new_item_id]= $new_item_ids[$ref];
  1142. }
  1143. }
  1144. $prerequisite_ids[$new_item_id] = $item['prerequisite'];
  1145. }
  1146. // updating prerequisites
  1147. foreach ($old_prerequisite as $key=>$my_old_prerequisite) {
  1148. if($my_old_prerequisite != ''){
  1149. $sql = "UPDATE ".$table_item." SET prerequisite = '".$my_old_prerequisite."' WHERE id = '".$key."' ";
  1150. Database::query($sql, __FILE__, __LINE__);
  1151. }
  1152. }
  1153. //updating refs
  1154. foreach ($old_refs as $key=>$my_old_ref) {
  1155. if ($my_old_ref != '') {
  1156. $sql = "UPDATE ".$table_item." SET ref = '".$my_old_ref."' WHERE id = '".$key."' ";
  1157. Database::query($sql, __FILE__, __LINE__);
  1158. }
  1159. }
  1160. foreach ($parent_item_ids as $new_item_id => $parent_item_old_id) {
  1161. $parent_new_id = 0;
  1162. if($parent_item_old_id != 0){
  1163. $parent_new_id = $new_item_ids[$parent_item_old_id];
  1164. }
  1165. $sql = "UPDATE ".$table_item." SET parent_item_id = '".$parent_new_id."' WHERE id = '".$new_item_id."'";
  1166. Database::query($sql, __FILE__, __LINE__);
  1167. }
  1168. foreach ($previous_item_ids as $new_item_id => $previous_item_old_id) {
  1169. $previous_new_id = 0;
  1170. if($previous_item_old_id != 0){
  1171. $previous_new_id = $new_item_ids[$previous_item_old_id];
  1172. }
  1173. $sql = "UPDATE ".$table_item." SET previous_item_id = '".$previous_new_id."' WHERE id = '".$new_item_id."'";
  1174. Database::query($sql, __FILE__, __LINE__);
  1175. }
  1176. foreach ($next_item_ids as $new_item_id => $next_item_old_id) {
  1177. $next_new_id = 0;
  1178. if($next_item_old_id != 0){
  1179. $next_new_id = $new_item_ids[$next_item_old_id];
  1180. }
  1181. $sql = "UPDATE ".$table_item." SET next_item_id = '".$next_new_id."' WHERE id = '".$new_item_id."'";
  1182. Database::query($sql, __FILE__, __LINE__);
  1183. }
  1184. foreach ($prerequisite_ids as $new_item_id => $prerequisite_old_id)
  1185. {
  1186. $prerequisite_new_id = 0;
  1187. if($prerequisite_old_id != 0){
  1188. $prerequisite_new_id = $new_item_ids[$prerequisite_old_id];
  1189. }
  1190. $sql = "UPDATE ".$table_item." SET prerequisite = '".$prerequisite_new_id."' WHERE id = '".$new_item_id."'";
  1191. Database::query($sql, __FILE__, __LINE__);
  1192. }
  1193. $this->course->resources[RESOURCE_LEARNPATH][$id]->destination_id = $new_lp_id;
  1194. }
  1195. }
  1196. }
  1197. /**
  1198. * restore works
  1199. */
  1200. function restore_student_publication ()
  1201. {
  1202. $my_course_id=api_get_course_id();
  1203. $my_course_info=api_get_course_info($my_course_id);//student_publication_assignment
  1204. $my_tbl_db_spa_origin=Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT,$my_course_info['dbName']);
  1205. $my_tbl_db_spa_destination = Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT, $this->course->destination_db);
  1206. $my_tbl_db_origin=Database :: get_course_table(TABLE_STUDENT_PUBLICATION,$my_course_info['dbName']);
  1207. $my_tbl_db_destination = Database :: get_course_table(TABLE_STUDENT_PUBLICATION, $this->course->destination_db);
  1208. $my_tbl_db_item_property_origin=Database :: get_course_table(TABLE_ITEM_PROPERTY, $my_course_info['dbName']);
  1209. $my_tbl_db_item_property_destination=Database :: get_course_table(TABLE_ITEM_PROPERTY, $this->course->destination_db);
  1210. //query in student publication
  1211. $query_sql_fin_sp='INSERT IGNORE INTO '.$my_tbl_db_destination.'' .
  1212. '(id,url,title,description,author,active,accepted,post_group_id,sent_date,' .
  1213. 'filetype,has_properties,view_properties,qualification,date_of_qualification,' .
  1214. 'parent_id,qualificator_id,session_id) ';
  1215. $query_sql_ini_sp='SELECT id,url,title,description,author,active,accepted,post_group_id,' .
  1216. 'sent_date,filetype,has_properties,view_properties,qualification,date_of_qualification,' .
  1217. 'parent_id,qualificator_id,session_id FROM '.$my_tbl_db_origin.' WHERE filetype="folder" ';
  1218. //var_dump($query_sql_ini_sp);
  1219. $destination='../../courses/'.$this->course->destination_path.'/work/';
  1220. $course_info=api_get_course_info(api_get_course_id());
  1221. $origin='../../courses/'.$course_info['path'].'/work/';
  1222. self::allow_create_all_directory($origin,$destination,false);
  1223. //query in item property
  1224. $query_sql_fin_ip='INSERT IGNORE INTO '.$my_tbl_db_item_property_destination.'' .
  1225. '(tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,' .
  1226. 'to_user_id,visibility,start_visible,end_visible) ';
  1227. $query_sql_ini_ip='SELECT tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,' .
  1228. 'lastedit_user_id,to_group_id,to_user_id,visibility,start_visible,
  1229. end_visible FROM '.$my_tbl_db_item_property_origin.' ip INNER JOIN '.$my_tbl_db_origin.' sp' .
  1230. ' ON ip.ref=sp.id WHERE tool="work" ';
  1231. $query_sql_fin_sa='INSERT IGNORE INTO '.$my_tbl_db_spa_destination.'' .
  1232. '(id,expires_on,ends_on,add_to_calendar,enable_qualification,publication_id) ';
  1233. $query_sql_ini_sa='SELECT sa.id,sa.expires_on,sa.ends_on,sa.add_to_calendar,sa.enable_qualification,sa.publication_id FROM '.$my_tbl_db_spa_origin.' sa INNER JOIN '.$my_tbl_db_origin.' sp
  1234. ON sa.publication_id=sp.id WHERE filetype="folder" ';
  1235. $query_sql_sp = $query_sql_fin_sp.$query_sql_ini_sp;
  1236. $query_sql_ip = $query_sql_fin_ip.$query_sql_ini_ip;
  1237. $query_sql_sa = $query_sql_fin_sa.$query_sql_ini_sa;
  1238. Database::query($query_sql_sp,__FILE__,__LINE__);
  1239. Database::query($query_sql_ip,__FILE__,__LINE__);
  1240. Database::query($query_sql_sa,__FILE__,__LINE__);
  1241. }
  1242. /**
  1243. * copy all directory and sub directory
  1244. * @param string The path origin
  1245. * @param string The path destination
  1246. * @param boolean Option Overwrite
  1247. * @return void()
  1248. */
  1249. function allow_create_all_directory($source, $dest, $overwrite = false){
  1250. if(!is_dir($dest)) {
  1251. mkdir($dest);
  1252. }
  1253. if ($handle = opendir($source)) { // if the folder exploration is sucsessful, continue
  1254. while (false !== ($file = readdir($handle))) { // as long as storing the next file to $file is successful, continue
  1255. if ($file != '.' && $file != '..') {
  1256. $path = $source . '/' . $file;
  1257. if (is_file($path)) {
  1258. /* if (!is_file($dest . '/' . $file) || $overwrite)
  1259. if (!@copy($path, $dest . '/' . $file)) {
  1260. echo '<font color="red">File ('.$path.') '.get_lang('NotHavePermission').'</font>';
  1261. }*/
  1262. } elseif(is_dir($path)) {
  1263. if (!is_dir($dest . '/' . $file))
  1264. mkdir($dest . '/' . $file);
  1265. self:: allow_create_all_directory($path, $dest . '/' . $file, $overwrite);
  1266. }
  1267. }
  1268. }
  1269. closedir($handle);
  1270. }
  1271. }
  1272. /**
  1273. * Gets the new ID of one specific tool item from the tool name and the old ID
  1274. * @param string Tool name
  1275. * @param integer Old ID
  1276. * @return integer New ID
  1277. */
  1278. function get_new_id($tool,$ref)
  1279. {
  1280. //transform $tool into one backup/restore constant
  1281. if($tool == 'hotpotatoes'){$tool = 'document';}
  1282. if(!empty($this->course->resources[$tool][$ref]->destination_id)){
  1283. return $this->course->resources[$tool][$ref]->destination_id;
  1284. }
  1285. return '';
  1286. }
  1287. /**
  1288. * Restore glossary
  1289. */
  1290. function restore_glossary($session_id = 0)
  1291. {
  1292. if ($this->course->has_resources(RESOURCE_GLOSSARY))
  1293. {
  1294. $table_glossary = Database :: get_course_table(TABLE_GLOSSARY, $this->course->destination_db);
  1295. $t_item_propery = Database :: get_course_table(TABLE_ITEM_PROPERTY, $this->course->destination_db);
  1296. $resources = $this->course->resources;
  1297. foreach ($resources[RESOURCE_GLOSSARY] as $id => $glossary) {
  1298. $condition_session = "";
  1299. if (!empty($session_id)) {
  1300. $session_id = intval($session_id);
  1301. $condition_session = " , session_id = '$session_id' ";
  1302. }
  1303. $sql = "INSERT INTO ".$table_glossary." SET name = '".Database::escape_string($glossary->name)."', description = '".Database::escape_string($glossary->description)."', display_order='".Database::escape_string($glossary->display_order)."' $condition_session ";
  1304. Database::query($sql, __FILE__, __LINE__);
  1305. $this->course->resources[RESOURCE_GLOSSARY][$id]->destination_id = Database::insert_id();
  1306. }
  1307. }
  1308. }
  1309. function restore_wiki($session_id = 0)
  1310. {
  1311. if ($this->course->has_resources(RESOURCE_WIKI))
  1312. {
  1313. // wiki table of the target course
  1314. $table_wiki = Database :: get_course_table('wiki', $this->course->destination_db);
  1315. $table_wiki_conf = Database :: get_course_table('wiki_conf', $this->course->destination_db);
  1316. // storing all the resources that have to be copied in an array
  1317. $resources = $this->course->resources;
  1318. foreach ($resources[RESOURCE_WIKI] as $id => $wiki)
  1319. {
  1320. //$wiki = new Wiki($obj->page_id, $obj->reflink, $obj->title, $obj->content, $obj->user_id, $obj->group_id, $obj->dtime);
  1321. // the sql statement to insert the groups from the old course to the new course
  1322. $sql = "INSERT INTO $table_wiki (page_id, reflink, title, content, user_id, group_id, dtime, progress, version, session_id)
  1323. VALUES (
  1324. '".Database::escape_string($wiki->page_id)."',
  1325. '".Database::escape_string($wiki->reflink)."',
  1326. '".Database::escape_string($wiki->title)."',
  1327. '".Database::escape_string($wiki->content)."',
  1328. '".intval($wiki->user_id)."',
  1329. '".intval($wiki->group_id)."',
  1330. '".Database::escape_string($wiki->dtime)."',
  1331. '".Database::escape_string($wiki->progress)."',
  1332. '".intval($wiki->version)."',
  1333. '".(!empty($session_id)?intval($session_id):0)."')";
  1334. $rs2 = Database::query($sql, __FILE__, __LINE__);
  1335. $new_id = Database::insert_id();
  1336. $this->course->resources[RESOURCE_WIKI][$id]->destination_id = $new_id;
  1337. $sql = "UPDATE $table_wiki set page_id = '$new_id' WHERE id = '$new_id'";
  1338. Database::query($sql, __FILE__, __LINE__);
  1339. // we also add an entry in wiki_conf
  1340. $sql = "INSERT INTO $table_wiki_conf
  1341. (page_id, task, feedback1, feedback2, feedback3, fprogress1, fprogress2, fprogress3, max_size, max_text, max_version, startdate_assig, enddate_assig, delayedsubmit)
  1342. VALUES
  1343. ('".intval($new_id)."', '', '', '', '', '', '', '', NULL, 0, 0, '0000-00-00 00:00:00', '0000-00-00 00:00:00', 0)";
  1344. $rs1 = Database::query($sql, __FILE__, __LINE__);
  1345. }
  1346. }
  1347. }
  1348. }
  1349. ?>