CourseRestorer.class.php 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Course Restorer script
  5. * @package chamilo.backup
  6. */
  7. /**
  8. * Code
  9. */
  10. require_once 'Course.class.php';
  11. require_once 'Event.class.php';
  12. require_once 'Link.class.php';
  13. require_once 'ToolIntro.class.php';
  14. require_once 'LinkCategory.class.php';
  15. require_once 'ForumCategory.class.php';
  16. require_once 'Forum.class.php';
  17. require_once 'ForumTopic.class.php';
  18. require_once 'ForumPost.class.php';
  19. require_once 'CourseDescription.class.php';
  20. require_once 'CourseCopyLearnpath.class.php';
  21. require_once 'Survey.class.php';
  22. require_once 'SurveyQuestion.class.php';
  23. require_once api_get_path(SYS_CODE_PATH).'exercice/question.class.php';
  24. require_once 'Glossary.class.php';
  25. require_once 'wiki.class.php';
  26. require_once 'Thematic.class.php';
  27. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  28. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  29. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  30. define('FILE_SKIP', 1);
  31. define('FILE_RENAME', 2);
  32. define('FILE_OVERWRITE', 3);
  33. define('UTF8_CONVERT', false);
  34. /**
  35. * Class to restore items from a course object to a Chamilo-course
  36. * @author Bart Mollet <bart.mollet@hogent.be>
  37. * @author Julio Montoya <gugli100@gmail.com> Several fixes/improvements
  38. * @package chamilo.backup
  39. */
  40. class CourseRestorer
  41. {
  42. /**
  43. * The course-object
  44. */
  45. var $course;
  46. var $destination_course_info;
  47. /**
  48. * What to do with files with same name (FILE_SKIP, FILE_RENAME or
  49. * FILE_OVERWRITE)
  50. */
  51. var $file_option;
  52. var $set_tools_invisible_by_default;
  53. var $skip_content;
  54. /**
  55. * Create a new CourseRestorer
  56. */
  57. function CourseRestorer($course) {
  58. $this->course = $course;
  59. $course_info = api_get_course_info($this->course->code);
  60. $this->course_origin_id = $course_info['real_id'];
  61. $this->file_option = FILE_RENAME;
  62. $this->set_tools_invisible_by_default = false;
  63. $this->skip_content = array();
  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. $this->file_option = $option;
  72. }
  73. /**
  74. * Restore a course.
  75. * @param string The code of the Chamilo-course in
  76. * @param int The session id
  77. * @param bool Course settings are going to be restore?
  78. */
  79. function restore($destination_course_code = '', $session_id = 0, $update_course_settings = false, $respect_base_content = false) {
  80. if ($destination_course_code == '') {
  81. $course_info = api_get_course_info();
  82. $this->destination_course_info = $course_info;
  83. $this->course->destination_path = $course_info['path'];
  84. } else {
  85. $course_info = api_get_course_info($destination_course_code);
  86. $this->destination_course_info = $course_info;
  87. $this->course->destination_path = $course_info['path'];
  88. }
  89. $this->destination_course_id = $course_info['real_id'];
  90. // Source platform encoding - reading/detection
  91. // The correspondent data field has been added as of version 1.8.6.1.
  92. if (empty($this->course->encoding)) {
  93. // The archive has been created by a system wich is prior to 1.8.6.1 version.
  94. // In this case we have to detect the encoding.
  95. $sample_text = $this->course->get_sample_text()."\n";
  96. // Let us exclude ASCII lines, probably they are English texts.
  97. $sample_text = explode("\n", $sample_text);
  98. foreach ($sample_text as $key => &$line) {
  99. if (api_is_valid_ascii($line)) {
  100. unset($sample_text[$key]);
  101. }
  102. }
  103. $sample_text = join("\n", $sample_text);
  104. $this->course->encoding = api_detect_encoding($sample_text, $course_info['language']);
  105. }
  106. // Encoding conversion of the course, if it is needed.
  107. $this->course->to_system_encoding();
  108. $this->restore_links($session_id);
  109. $this->restore_documents($session_id, $destination_course_code);
  110. $this->restore_quizzes($session_id, $respect_base_content);
  111. $this->restore_glossary($session_id);
  112. $this->restore_learnpaths($session_id, $respect_base_content);
  113. $this->restore_course_descriptions($session_id);
  114. $this->restore_wiki($session_id);
  115. $this->restore_thematic($session_id);
  116. $this->restore_attendance($session_id);
  117. if (!empty($session_id)) {
  118. } else {
  119. //$this->restore_links();
  120. $this->restore_tool_intro();
  121. $this->restore_events();
  122. $this->restore_announcements();
  123. //$this->restore_documents();
  124. $this->restore_scorm_documents();
  125. //$this->restore_course_descriptions();
  126. //$this->restore_quizzes(); // after restore_documents! (for correct import of sound/video)
  127. //$this->restore_learnpaths();
  128. $this->restore_surveys();
  129. $this->restore_student_publication();
  130. //$this->restore_glossary();
  131. //$this->restore_wiki();
  132. //$this->restore_thematic();
  133. //$this->restore_attendance();
  134. }
  135. if ($update_course_settings) {
  136. $this->restore_course_settings($destination_course_code);
  137. }
  138. // Restore the item properties
  139. $table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  140. $condition_session = "";
  141. if (!empty($session_id)) {
  142. $condition_session = " , id_session='".intval($session_id)."'";
  143. }
  144. foreach ($this->course->resources as $type => $resources) {
  145. if (is_array($resources)) {
  146. foreach ($resources as $id => $resource) {
  147. foreach ($resource->item_properties as $property) {
  148. // First check if there isn't allready a record for this resource
  149. $sql = "SELECT * FROM $table WHERE c_id = ".$this->destination_course_id." AND tool = '".$property['tool']."' AND ref = '".$resource->destination_id."'";
  150. $res = Database::query($sql);
  151. if( Database::num_rows($res) == 0) {
  152. // The to_group_id and to_user_id are set to default values as users/groups possibly not exist in the target course
  153. $sql = "INSERT INTO $table SET
  154. c_id = '".$this->destination_course_id."',
  155. tool = '".self::DBUTF8escapestring($property['tool'])."',
  156. insert_user_id = '".self::DBUTF8escapestring($property['insert_user_id'])."',
  157. insert_date = '".self::DBUTF8escapestring($property['insert_date'])."',
  158. lastedit_date = '".self::DBUTF8escapestring($property['lastedit_date'])."',
  159. ref = '".self::DBUTF8escapestring($resource->destination_id)."',
  160. lastedit_type = '".self::DBUTF8escapestring($property['lastedit_type'])."',
  161. lastedit_user_id = '".self::DBUTF8escapestring($property['lastedit_user_id'])."',
  162. visibility = '".self::DBUTF8escapestring($property['visibility'])."',
  163. start_visible = '".self::DBUTF8escapestring($property['start_visible'])."',
  164. end_visible = '".self::DBUTF8escapestring($property['end_visible'])."',
  165. to_user_id = '".self::DBUTF8escapestring($property['to_user_id'])."',
  166. to_group_id = '0' $condition_session" ;
  167. ;
  168. Database::query($sql);
  169. }
  170. }
  171. }
  172. }
  173. }
  174. /*
  175. // Restore the linked-resources
  176. $table = Database :: get_course_table(TABLE_LINKED_RESOURCES);
  177. foreach ($this->course->resources as $type => $resources) {
  178. if (is_array($resources))
  179. foreach ($resources as $id => $resource) {
  180. $linked_resources = $resource->get_linked_resources();
  181. foreach ($linked_resources as $to_type => $to_ids) {
  182. foreach ($to_ids as $index => $to_id) {
  183. $to_resource = $this->course->resources[$to_type][$to_id];
  184. $sql = "INSERT INTO ".$table." SET
  185. source_type = '".$type."',
  186. source_id = '".$resource->destination_id."',
  187. resource_type='".$to_type."',
  188. resource_id='".$to_resource->destination_id."' ";
  189. Database::query($sql);
  190. }
  191. }
  192. }
  193. }*/
  194. }
  195. /**
  196. * Restore only harmless course settings: course_language, visibility, department_name,department_url, subscribe, unsubscribe ,category_code
  197. *
  198. * @return unknown_type
  199. */
  200. function restore_course_settings($destination_course_code) {
  201. $origin_course_info = api_get_course_info($destination_course_code);
  202. $course_info = $this->course->info;
  203. $params['course_language'] = $course_info['language'];
  204. $params['visibility'] = $course_info['visibility'];
  205. $params['department_name'] = $course_info['department_name'];
  206. $params['department_url'] = $course_info['department_url'];
  207. $params['category_code'] = $course_info['categoryCode'];
  208. $params['subscribe'] = $course_info['subscribe_allowed'];
  209. $params['unsubscribe'] = $course_info['unubscribe_allowed'];
  210. CourseManager::update_attributes($origin_course_info['real_id'], $params);
  211. }
  212. /**
  213. * Restore documents
  214. * @param int session id
  215. *
  216. */
  217. function restore_documents($session_id = 0, $destination_course_code = '') {
  218. $perm = api_get_permissions_for_new_directories();
  219. $course_info = api_get_course_info($destination_course_code);
  220. if ($this->course->has_resources(RESOURCE_DOCUMENT)) {
  221. $table = Database :: get_course_table(TABLE_DOCUMENT);
  222. $resources = $this->course->resources;
  223. foreach ($resources[RESOURCE_DOCUMENT] as $id => $document) {
  224. $path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/';
  225. $dirs = explode('/', dirname($document->path));
  226. if (empty($document->item_properties[0]['id_session'])) {
  227. $my_session_id = 0;
  228. } else {
  229. $my_session_id = $session_id;
  230. }
  231. if ($document->file_type == FOLDER) {
  232. $visibility = $document->item_properties[0]['visibility'];
  233. if (!empty($document->title)) {
  234. $title = $document->title;
  235. } else {
  236. $title = basename($document->path);
  237. }
  238. $new = substr($document->path, 8);
  239. if (!is_dir($path.'document'.$new)) {
  240. //var_dump($path.'document'.$new);
  241. $created_dir = create_unexisting_directory($course_info, api_get_user_id(), $my_session_id, 0, 0 ,$path.'document', $new, $title, $visibility);
  242. }
  243. } elseif ($document->file_type == DOCUMENT) {
  244. /*
  245. echo 'option'; var_dump($this->file_option);
  246. echo 'file type'; var_dump($document->file_type);
  247. echo 'session'; var_dump($session_id);
  248. echo 'file _exists'; var_dump($path.$document->path); var_dump(file_exists($path.$document->path));
  249. */
  250. if (!is_dir($path.dirname($document->path))) {
  251. continue;
  252. //echo '! is dir'; var_dump($path.dirname($document->path));
  253. $visibility = $document->item_properties[0]['visibility'];
  254. //$new = $path.dirname($document->path);
  255. //$new = substr($new, 8);
  256. $new = substr($document->path, 8);
  257. if (!empty($document->title)) {
  258. $title = $document->title;
  259. } else {
  260. $title = basename($new);
  261. }
  262. // This code fixes the possibility for a file without a directory entry to be
  263. $created_dir = create_unexisting_directory($destination_course, api_get_user_id(), $my_session_id, 0, 0 , $path.'document', $new, $title, $visibility, true);
  264. }
  265. if (file_exists($path.$document->path)) {
  266. //var_dump ('enter');
  267. switch ($this->file_option) {
  268. case FILE_OVERWRITE :
  269. $origin_path = $this->course->backup_path.'/'.$document->path;
  270. if (file_exists($origin_path)) {
  271. copy($origin_path, $path.$document->path);
  272. }
  273. //Replace old course code with the new destination code
  274. $file_info = pathinfo($path.$document->path);
  275. if (in_array($file_info['extension'], array('html','htm'))) {
  276. $content = file_get_contents($path.$document->path);
  277. if (UTF8_CONVERT) $content = utf8_encode($content);
  278. $content = DocumentManager::replace_urls_inside_content_html_from_copy_course($content ,$this->course->code,$this->course->destination_path);
  279. $result = file_put_contents($path.$document->path,$content);
  280. }
  281. $sql = "SELECT id FROM ".$table." WHERE c_id = ".$this->destination_course_id." AND path='/".substr($document->path, 9)."'";
  282. $res = Database::query($sql);
  283. $obj = Database::fetch_object($res);
  284. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id;
  285. $sql = "UPDATE ".$table." SET comment = '".self::DBUTF8escapestring($document->comment)."', title='".self::DBUTF8escapestring($document->title)."', size='".$document->size."'
  286. WHERE id = '".$obj->id."'";
  287. Database::query($sql);
  288. break;
  289. case FILE_SKIP :
  290. $sql = "SELECT id FROM ".$table." WHERE c_id = ".$this->destination_course_id." AND path='/".self::DBUTF8escapestring(substr($document->path, 9))."'";
  291. $res = Database::query($sql);
  292. $obj = Database::fetch_object($res);
  293. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id;
  294. break;
  295. case FILE_RENAME :
  296. $i = 1;
  297. $ext = explode('.', basename($document->path));
  298. if (count($ext) > 1) {
  299. $ext = array_pop($ext);
  300. $file_name_no_ext = substr($document->path, 0, - (strlen($ext) + 1));
  301. $ext = '.'.$ext;
  302. } else {
  303. $ext = '';
  304. $file_name_no_ext = $document->path;
  305. }
  306. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  307. $file_exists = file_exists($path.$new_file_name);
  308. while ($file_exists) {
  309. $i ++;
  310. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  311. $file_exists = file_exists($path.$new_file_name);
  312. }
  313. if (!empty($session_id)) {
  314. $document_path = explode('/',$document->path,3);
  315. $course_path = $path; // "/var/www/wiener/courses/"
  316. $orig_base_folder = $document_path[1];
  317. $orig_base_path = $course_path.$document_path[0].'/'.$document_path[1];
  318. //echo '$orig_base_path'; var_dump($orig_base_path);
  319. if (is_dir($orig_base_path)) {
  320. $new_base_foldername = $orig_base_folder; // e.g: "carpeta1"
  321. $new_base_path = $orig_base_path; // e.g: "/var/www/wiener/courses/CURSO4/document/carpeta1"
  322. if ($_SESSION['orig_base_foldername'] != $new_base_foldername) {
  323. unset($_SESSION['new_base_foldername']);
  324. unset($_SESSION['orig_base_foldername']);
  325. unset($_SESSION['new_base_path']);
  326. }
  327. $folder_exists = file_exists($new_base_path);
  328. if ($folder_exists) {
  329. $_SESSION['orig_base_foldername'] = $new_base_foldername; // e.g: carpeta1 in session
  330. $x = '';
  331. while ($folder_exists) {
  332. $x = $x + 1;
  333. $new_base_foldername = $document_path[1].'_'.$x;
  334. $new_base_path = $orig_base_path.'_'.$x;
  335. if ($_SESSION['new_base_foldername'] == $new_base_foldername) break;
  336. $folder_exists = file_exists($new_base_path);
  337. }
  338. $_SESSION['new_base_foldername'] = $new_base_foldername;
  339. $_SESSION['new_base_path'] = $new_base_path;
  340. }
  341. if (isset($_SESSION['new_base_foldername']) && isset($_SESSION['new_base_path'])) {
  342. $new_base_foldername = $_SESSION['new_base_foldername'];
  343. $new_base_path = $_SESSION['new_base_path'];
  344. }
  345. $dest_document_path = $new_base_path.'/'.$document_path[2]; // e.g: "/var/www/wiener/courses/CURSO4/document/carpeta1_1/subcarpeta1/collaborative.png"
  346. $basedir_dest_path = dirname($dest_document_path); // e.g: "/var/www/wiener/courses/CURSO4/document/carpeta1_1/subcarpeta1"
  347. $dest_filename = basename($dest_document_path); // e.g: "collaborative.png"
  348. $base_path_document = $course_path.$document_path[0]; // e.g: "/var/www/wiener/courses/CURSO4/document"
  349. $path_title = '/'.$new_base_foldername.'/'.$document_path[2];
  350. copy_folder_course_session($basedir_dest_path, $base_path_document, $session_id, $course_info, $document);
  351. if (file_exists($course_path.$document->path)) {
  352. copy($course_path.$document->path, $dest_document_path);
  353. }
  354. //Replace old course code with the new destination code see BT#1985
  355. if (file_exists($dest_document_path)) {
  356. $file_info = pathinfo($dest_document_path);
  357. if (in_array($file_info['extension'], array('html','htm'))) {
  358. $content = file_get_contents($dest_document_path);
  359. if (UTF8_CONVERT) $content = utf8_encode($content);
  360. $content = DocumentManager::replace_urls_inside_content_html_from_copy_course($content ,$this->course->code,$this->course->destination_path);
  361. $result = file_put_contents($dest_document_path,$content);
  362. }
  363. }
  364. $sql = "INSERT INTO $table SET
  365. path = '$path_title',
  366. c_id = ".$this->destination_course_id.",
  367. comment = '".self::DBUTF8escapestring($document->comment)."',
  368. title = '".self::DBUTF8escapestring(basename($path_title))."' ,
  369. filetype ='".$document->file_type."',
  370. size = '".$document->size."',
  371. session_id = '$my_session_id'";
  372. Database::query($sql);
  373. $document_id = Database::insert_id();
  374. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id;
  375. api_item_property_update($course_info, TOOL_DOCUMENT, $document_id, 'DocumentAdded', $document->item_properties[0]['insert_user_id'], $document->item_properties[0]['to_group_id'], $document->item_properties[0]['to_user_id'], null, null, $my_session_id);
  376. } else {
  377. if (file_exists($path.$document->path)) {
  378. copy($path.$document->path, $path.$new_file_name);
  379. }
  380. //Replace old course code with the new destination code see BT#1985
  381. if (file_exists($path.$new_file_name)) {
  382. $file_info = pathinfo($path.$new_file_name);
  383. if (in_array($file_info['extension'], array('html','htm'))) {
  384. $content = file_get_contents($path.$new_file_name);
  385. if (UTF8_CONVERT) $content = utf8_encode($content);
  386. $content = DocumentManager::replace_urls_inside_content_html_from_copy_course($content ,$this->course->code,$this->course->destination_path);
  387. $result = file_put_contents($path.$new_file_name, $content);
  388. }
  389. }
  390. $sql = "INSERT INTO ".$table." SET
  391. c_id = ".$this->destination_course_id.",
  392. path = '/".self::DBUTF8escapestring(substr($new_file_name, 9))."',
  393. comment = '".self::DBUTF8escapestring($document->comment)."',
  394. title = '".self::DBUTF8escapestring($document->title)."' ,
  395. filetype ='".$document->file_type."',
  396. size = '".$document->size."',
  397. session_id = '$my_session_id'";
  398. Database::query($sql);
  399. $document_id = Database::insert_id();
  400. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id;
  401. api_item_property_update($course_info, TOOL_DOCUMENT, $document_id, 'DocumentAdded', $document->item_properties[0]['insert_user_id'], $document->item_properties[0]['to_group_id'], $document->item_properties[0]['to_user_id'], null, null, $my_session_id);
  402. }
  403. } else {
  404. copy($this->course->backup_path.'/'.$document->path, $path.$new_file_name);
  405. //Replace old course code with the new destination code see BT#1985
  406. if (file_exists($path.$new_file_name)) {
  407. $file_info = pathinfo($path.$new_file_name);
  408. if (in_array($file_info['extension'], array('html','htm'))) {
  409. $content = file_get_contents($path.$new_file_name);
  410. if (UTF8_CONVERT) $content = utf8_encode($content);
  411. $content = DocumentManager::replace_urls_inside_content_html_from_copy_course($content ,$this->course->code,$this->course->destination_path);
  412. $result = file_put_contents($path.$new_file_name, $content);
  413. }
  414. }
  415. $sql = "INSERT INTO ".$table." SET
  416. c_id = ".$this->destination_course_id.",
  417. path = '/".self::DBUTF8escapestring(substr($new_file_name, 9))."',
  418. comment = '".self::DBUTF8escapestring($document->comment)."',
  419. title = '".self::DBUTF8escapestring($document->title)."' ,
  420. filetype ='".$document->file_type."',
  421. size = '".$document->size."',
  422. session_id = '$my_session_id'";
  423. Database::query($sql);
  424. $document_id = Database::insert_id();
  425. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id;
  426. api_item_property_update($course_info, TOOL_DOCUMENT, $document_id, 'DocumentAdded', $document->item_properties[0]['insert_user_id'], $document->item_properties[0]['to_group_id'], $document->item_properties[0]['to_user_id'], null, null, $my_session_id);
  427. }
  428. break;
  429. } // end switch
  430. } else { // end if file exists
  431. //make sure the source file actually exists
  432. 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))) {
  433. //echo 'Copying';
  434. copy($this->course->backup_path.'/'.$document->path, $path.$document->path);
  435. //Replace old course code with the new destination code see BT#1985
  436. if (file_exists($path.$document->path)) {
  437. $file_info = pathinfo($path.$document->path);
  438. if (in_array($file_info['extension'], array('html','htm'))) {
  439. $content = file_get_contents($path.$document->path);
  440. if (UTF8_CONVERT) $content = utf8_encode($content);
  441. $content = DocumentManager::replace_urls_inside_content_html_from_copy_course($content ,$this->course->code,$this->course->destination_path);
  442. $result = file_put_contents($path.$document->path, $content);
  443. }
  444. }
  445. $sql = "INSERT INTO ".$table." SET
  446. c_id = ".$this->destination_course_id.",
  447. path = '/".substr($document->path, 9)."',
  448. comment = '".self::DBUTF8escapestring($document->comment)."',
  449. title = '".self::DBUTF8escapestring($document->title)."' ,
  450. filetype='".$document->file_type."',
  451. size= '".$document->size."',
  452. session_id = '$my_session_id'";
  453. Database::query($sql);
  454. $document_id = Database::insert_id();
  455. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id;
  456. api_item_property_update($course_info, TOOL_DOCUMENT, $document_id, 'DocumentAdded', $document->item_properties[0]['insert_user_id'], $document->item_properties[0]['to_group_id'], $document->item_properties[0]['to_user_id'], null, null, $my_session_id);
  457. } else {
  458. //echo 'not Copying';
  459. if(is_file($this->course->backup_path.'/'.$document->path) && is_readable($this->course->backup_path.'/'.$document->path)) {
  460. error_log('Course copy generated an ignoreable error while trying to copy '.$this->course->backup_path.'/'.$document->path.': file not found');
  461. }
  462. if(!is_dir(dirname($path.$document->path))) {
  463. error_log('Course copy generated an ignoreable error while trying to copy to '.dirname($path.$document->path).': directory not found');
  464. }
  465. if(!is_writeable(dirname($path.$document->path))) {
  466. error_log('Course copy generated an ignoreable error while trying to copy to '.dirname($path.$document->path).': directory not writeable');
  467. }
  468. }
  469. } // end file doesn't exist
  470. } else {
  471. /*$sql = "SELECT id FROM ".$table." WHERE path = '/".self::DBUTF8escapestring(substr($document->path, 9))."'";
  472. $res = Database::query($sql);
  473. if( Database::num_rows($res)> 0)
  474. {
  475. $obj = Database::fetch_object($res);
  476. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id;
  477. }
  478. else
  479. {
  480. $sql = "INSERT INTO ".$table." SET path = '/".self::DBUTF8escapestring(substr($document->path, 9))."', comment = '".self::DBUTF8escapestring($document->comment)."', title = '".self::DBUTF8escapestring($document->title)."' ,filetype='".$document->file_type."', size= '".$document->size."'";
  481. Database::query($sql);
  482. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = Database::insert_id();
  483. }*/
  484. } // end folder
  485. } // end for each
  486. // Delete sessions for the copy the new folder in session
  487. unset($_SESSION['new_base_foldername']);
  488. unset($_SESSION['orig_base_foldername']);
  489. unset($_SESSION['new_base_path']);
  490. }
  491. }
  492. /**
  493. * Restore scorm documents
  494. * TODO @TODO check that the restore function with renaming doesn't break the scorm structure!
  495. */
  496. function restore_scorm_documents()
  497. {
  498. $perm = api_get_permissions_for_new_directories();
  499. if ($this->course->has_resources(RESOURCE_SCORM)) {
  500. $resources = $this->course->resources;
  501. foreach ($resources[RESOURCE_SCORM] as $id => $document) {
  502. $path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/';
  503. @mkdir(dirname($path.$document->path), $perm, true);
  504. if (file_exists($path.$document->path)) {
  505. switch ($this->file_option) {
  506. case FILE_OVERWRITE :
  507. rmdirr($path.$document->path);
  508. copyDirTo($this->course->backup_path.'/'.$document->path, $path.dirname($document->path), false);
  509. break;
  510. case FILE_SKIP :
  511. break;
  512. case FILE_RENAME :
  513. $i = 1;
  514. $ext = explode('.', basename($document->path));
  515. if (count($ext) > 1) {
  516. $ext = array_pop($ext);
  517. $file_name_no_ext = substr($document->path, 0, - (strlen($ext) + 1));
  518. $ext = '.'.$ext;
  519. } else {
  520. $ext = '';
  521. $file_name_no_ext = $document->path;
  522. }
  523. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  524. $file_exists = file_exists($path.$new_file_name);
  525. while ($file_exists) {
  526. $i ++;
  527. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  528. $file_exists = file_exists($path.$new_file_name);
  529. }
  530. rename($this->course->backup_path.'/'.$document->path,$this->course->backup_path.'/'.$new_file_name);
  531. copyDirTo($this->course->backup_path.'/'.$new_file_name, $path.dirname($new_file_name), false);
  532. rename($this->course->backup_path.'/'.$new_file_name,$this->course->backup_path.'/'.$document->path);
  533. break;
  534. } // end switch
  535. } // end if file exists
  536. else {
  537. copyDirTo($this->course->backup_path.'/'.$document->path, $path.dirname($document->path), false);
  538. }
  539. } // end for each
  540. }
  541. }
  542. /**
  543. * Restore forums
  544. */
  545. function restore_forums()
  546. {
  547. if ($this->course->has_resources(RESOURCE_FORUM))
  548. {
  549. $table_forum = Database::get_course_table(TABLE_FORUM);
  550. $table_topic = Database::get_course_table(TABLE_FORUM_THREAD);
  551. $table_post = Database::get_course_table(TABLE_FORUM_POST);
  552. $resources = $this->course->resources;
  553. foreach ($resources[RESOURCE_FORUM] as $id => $forum)
  554. {
  555. $cat_id = $this->restore_forum_category($forum->category_id);
  556. $sql = "INSERT INTO ".$table_forum." SET
  557. forum_title = '".self::DBUTF8escapestring($forum->title).
  558. "', c_id= '".$this->destination_course_id.
  559. "', forum_comment = '".self::DBUTF8escapestring($forum->description).
  560. "', forum_category = ".(int)self::DBUTF8escapestring($cat_id).
  561. ", forum_last_post = ".(int)self::DBUTF8escapestring($forum->last_post).
  562. ", forum_threads = ".(int)self::DBUTF8escapestring($forum->topics).
  563. ", forum_posts = ".(int)self::DBUTF8escapestring($forum->posts).
  564. ", allow_anonymous = ".(int)self::DBUTF8escapestring($forum->allow_anonymous).
  565. ", allow_edit = ".(int)self::DBUTF8escapestring($forum->allow_edit).
  566. ", approval_direct_post = '".self::DBUTF8escapestring($forum->approval_direct_post).
  567. "', allow_attachments = ".(int)self::DBUTF8escapestring($forum->allow_attachments).
  568. ", allow_new_threads = ".(int)self::DBUTF8escapestring($forum->allow_new_topics).
  569. ", default_view = '".self::DBUTF8escapestring($forum->default_view).
  570. "', forum_of_group = '".self::DBUTF8escapestring($forum->of_group).
  571. "', forum_group_public_private = '".self::DBUTF8escapestring($forum->group_public_private).
  572. "', forum_order = ".(int)self::DBUTF8escapestring($forum->order).
  573. ", locked = ".(int)self::DBUTF8escapestring($forum->locked).
  574. ", session_id = ".(int)self::DBUTF8escapestring($forum->session_id).
  575. ", forum_image = '".self::DBUTF8escapestring($forum->image)."'";
  576. Database::query($sql);
  577. $new_id = Database::insert_id();
  578. $this->course->resources[RESOURCE_FORUM][$id]->destination_id = $new_id;
  579. $forum_topics = 0;
  580. if (is_array($this->course->resources[RESOURCE_FORUMTOPIC])) {
  581. foreach ($this->course->resources[RESOURCE_FORUMTOPIC] as $topic_id => $topic) {
  582. if ($topic->forum_id == $id) {
  583. $this->restore_topic($topic_id, $new_id);
  584. $forum_topics ++;
  585. }
  586. }
  587. }
  588. if ($forum_topics > 0) {
  589. $last_post = $this->course->resources[RESOURCE_FORUMPOST][$forum->last_post];
  590. $sql = "UPDATE ".$table_forum." SET forum_threads = ".$forum_topics.", forum_last_post = ".(int)$last_post->destination_id." WHERE forum_id = ".(int)$new_id;
  591. Database::query($sql);
  592. }
  593. }
  594. }
  595. }
  596. /**
  597. * Restore forum-categories
  598. */
  599. function restore_forum_category($id)
  600. {
  601. $forum_cat_table = Database :: get_course_table(TABLE_FORUM_CATEGORY);
  602. $resources = $this->course->resources;
  603. $forum_cat = $resources[RESOURCE_FORUMCATEGORY][$id];
  604. if (!$forum_cat->is_restored()) {
  605. $title = $forum_cat->title;
  606. if (!empty($title)) {
  607. if (!preg_match('/.*\((.+)\)$/', $title, $matches)) {
  608. // This is for avoiding repetitive adding of training code after several backup/restore cycles.
  609. if ($matches[1] != $this->course->code) {
  610. $title = $title.' ('.$this->course->code.')';
  611. }
  612. }
  613. }
  614. $sql = "INSERT INTO ".$forum_cat_table." SET
  615. c_id = ".$this->destination_course_id." ,
  616. cat_title = '".self::DBUTF8escapestring($title).
  617. "', cat_comment = '".self::DBUTF8escapestring($forum_cat->description).
  618. "', cat_order = ".(int)self::DBUTF8escapestring($forum_cat->order).
  619. ", locked = ".(int)self::DBUTF8escapestring($forum_cat->locked).
  620. ", session_id = ".(int)self::DBUTF8escapestring($forum_cat->session_id);
  621. Database::query($sql);
  622. $new_id = Database::insert_id();
  623. $this->course->resources[RESOURCE_FORUMCATEGORY][$id]->destination_id = $new_id;
  624. return $new_id;
  625. }
  626. return $this->course->resources[RESOURCE_FORUMCATEGORY][$id]->destination_id;
  627. }
  628. /**
  629. * Restore a forum-topic
  630. */
  631. function restore_topic($id, $forum_id)
  632. {
  633. $table = Database :: get_course_table(TABLE_FORUM_THREAD);
  634. $resources = $this->course->resources;
  635. $topic = $resources[RESOURCE_FORUMTOPIC][$id];
  636. $sql = "INSERT INTO ".$table." SET
  637. c_id = ".$this->destination_course_id." ,
  638. thread_title = '".self::DBUTF8escapestring($topic->title).
  639. "', forum_id = ".(int)self::DBUTF8escapestring($forum_id).
  640. ", thread_date = '".self::DBUTF8escapestring($topic->time).
  641. "', thread_poster_id = ".(int)self::DBUTF8escapestring($topic->topic_poster_id).
  642. ", thread_poster_name = '".self::DBUTF8escapestring($topic->topic_poster_name).
  643. "', thread_views = ".(int)self::DBUTF8escapestring($topic->views).
  644. ", thread_sticky = ".(int)self::DBUTF8escapestring($topic->sticky).
  645. ", locked = ".(int)self::DBUTF8escapestring($topic->locked).
  646. ", thread_close_date = '".self::DBUTF8escapestring($topic->time_closed).
  647. "', thread_weight = ".(float)self::DBUTF8escapestring($topic->weight).
  648. ", thread_title_qualify = '".self::DBUTF8escapestring($topic->title_qualify).
  649. "', thread_qualify_max = ".(float)self::DBUTF8escapestring($topic->qualify_max);
  650. Database::query($sql);
  651. $new_id = Database::insert_id();
  652. $this->course->resources[RESOURCE_FORUMTOPIC][$id]->destination_id = $new_id;
  653. $topic_replies = -1;
  654. foreach ($this->course->resources[RESOURCE_FORUMPOST] as $post_id => $post)
  655. {
  656. if ($post->topic_id == $id)
  657. {
  658. $topic_replies ++;
  659. $this->restore_post($post_id, $new_id, $forum_id);
  660. }
  661. }
  662. $last_post = $this->course->resources[RESOURCE_FORUMPOST][$topic->last_post];
  663. if (is_object($last_post))
  664. {
  665. $sql = "UPDATE ".$table." SET thread_last_post = ".(int)$last_post->destination_id;
  666. Database::query($sql);
  667. }
  668. if ($topic_replies >= 0)
  669. {
  670. $sql = "UPDATE ".$table." SET thread_replies = ".$topic_replies;
  671. Database::query($sql);
  672. }
  673. return $new_id;
  674. }
  675. /**
  676. * Restore a forum-post
  677. * @TODO Restore tree-structure of posts. For example: attachments to posts.
  678. */
  679. function restore_post($id, $topic_id, $forum_id)
  680. {
  681. $table_post = Database :: get_course_table(TABLE_FORUM_POST);
  682. $resources = $this->course->resources;
  683. $post = $resources[RESOURCE_FORUMPOST][$id];
  684. $sql = "INSERT INTO ".$table_post." SET
  685. c_id = ".$this->destination_course_id." ,
  686. post_title = '".self::DBUTF8escapestring($post->title).
  687. "', post_text = '".self::DBUTF8escapestring($post->text).
  688. "', thread_id = ".(int)self::DBUTF8escapestring($topic_id).
  689. ", post_date = '".self::DBUTF8escapestring($post->post_time).
  690. "', forum_id = ".(int)self::DBUTF8escapestring($forum_id).
  691. ", poster_id = ".(int)self::DBUTF8escapestring($post->poster_id).
  692. ", poster_name = '".self::DBUTF8escapestring($post->poster_name).
  693. "', post_notification = ".(int)self::DBUTF8escapestring($post->topic_notify).
  694. ", post_parent_id = ".(int)self::DBUTF8escapestring($post->parent_post_id).
  695. ", visible = ".(int)self::DBUTF8escapestring($post->visible);
  696. Database::query($sql);
  697. $new_id = Database::insert_id();
  698. $this->course->resources[RESOURCE_FORUMPOST][$id]->destination_id = $new_id;
  699. return $new_id;
  700. }
  701. /**
  702. * Restore links
  703. */
  704. function restore_links($session_id = 0) {
  705. if ($this->course->has_resources(RESOURCE_LINK)) {
  706. $link_table = Database :: get_course_table(TABLE_LINK);
  707. $resources = $this->course->resources;
  708. foreach ($resources[RESOURCE_LINK] as $id => $link) {
  709. $cat_id = $this->restore_link_category($link->category_id,$session_id);
  710. $sql = "SELECT MAX(display_order) FROM $link_table WHERE c_id = ".$this->destination_course_id." AND category_id='" . self::DBUTF8escapestring($cat_id). "'";
  711. $result = Database::query($sql);
  712. list($max_order) = Database::fetch_array($result);
  713. $condition_session = "";
  714. if (!empty($session_id)) {
  715. $condition_session = " , session_id = '$session_id' ";
  716. }
  717. $sql = "INSERT INTO ".$link_table." SET
  718. c_id = ".$this->destination_course_id." ,
  719. url = '".self::DBUTF8escapestring($link->url)."',
  720. title = '".self::DBUTF8escapestring($link->title)."',
  721. description = '".self::DBUTF8escapestring($link->description)."',
  722. category_id='".$cat_id."',
  723. on_homepage = '".$link->on_homepage."',
  724. display_order='".($max_order+1)."' $condition_session";
  725. Database::query($sql);
  726. $this->course->resources[RESOURCE_LINK][$id]->destination_id = Database::insert_id();
  727. }
  728. }
  729. }
  730. /**
  731. * Restore tool intro
  732. */
  733. function restore_tool_intro() {
  734. if ($this->course->has_resources(RESOURCE_TOOL_INTRO)) {
  735. $tool_intro_table = Database :: get_course_table(TABLE_TOOL_INTRO);
  736. $resources = $this->course->resources;
  737. foreach ($resources[RESOURCE_TOOL_INTRO] as $id => $tool_intro) {
  738. $sql = "DELETE FROM ".$tool_intro_table." WHERE c_id = ".$this->destination_course_id." AND id='".self::DBUTF8escapestring($tool_intro->id)."'";
  739. Database::query($sql);
  740. $tool_intro->intro_text = DocumentManager::replace_urls_inside_content_html_from_copy_course($tool_intro->intro_text,$this->course->code,$this->course->destination_path);
  741. $sql = "INSERT INTO ".$tool_intro_table." SET c_id = ".$this->destination_course_id." , id='".self::DBUTF8escapestring($tool_intro->id)."', intro_text = '".self::DBUTF8escapestring($tool_intro->intro_text)."'";
  742. Database::query($sql);
  743. $this->course->resources[RESOURCE_TOOL_INTRO][$id]->destination_id = Database::insert_id();
  744. }
  745. }
  746. }
  747. /**
  748. * Restore a link-category
  749. */
  750. function restore_link_category($id,$session_id = 0) {
  751. $condition_session = "";
  752. if (!empty($session_id)) {
  753. $condition_session = " , session_id = '$session_id' ";
  754. }
  755. if ($id == 0)
  756. return 0;
  757. $link_cat_table = Database :: get_course_table(TABLE_LINK_CATEGORY);
  758. $resources = $this->course->resources;
  759. $link_cat = $resources[RESOURCE_LINKCATEGORY][$id];
  760. if (is_object($link_cat) && !$link_cat->is_restored()) {
  761. $sql = "SELECT MAX(display_order) FROM $link_cat_table WHERE c_id = ".$this->destination_course_id." ";
  762. $result=Database::query($sql);
  763. list($orderMax)=Database::fetch_array($result,'NUM');
  764. $display_order=$orderMax+1;
  765. $sql = "INSERT INTO ".$link_cat_table." SET c_id = ".$this->destination_course_id." , category_title = '".self::DBUTF8escapestring($link_cat->title)."', description='".self::DBUTF8escapestring($link_cat->description)."', display_order='".$display_order."' $condition_session ";
  766. Database::query($sql);
  767. $new_id = Database::insert_id();
  768. $this->course->resources[RESOURCE_LINKCATEGORY][$id]->destination_id = $new_id;
  769. return $new_id;
  770. }
  771. return $this->course->resources[RESOURCE_LINKCATEGORY][$id]->destination_id;
  772. }
  773. /**
  774. * Restore events
  775. */
  776. function restore_events() {
  777. if ($this->course->has_resources(RESOURCE_EVENT)) {
  778. $table = Database :: get_course_table(TABLE_AGENDA);
  779. $resources = $this->course->resources;
  780. foreach ($resources[RESOURCE_EVENT] as $id => $event) {
  781. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  782. $event->content = DocumentManager::replace_urls_inside_content_html_from_copy_course($event->content, $this->course->code, $this->course->destination_path);
  783. $sql = "INSERT INTO ".$table." SET
  784. c_id = ".$this->destination_course_id." ,
  785. title = '".self::DBUTF8escapestring($event->title)."',
  786. content = '".self::DBUTF8escapestring($event->content)."',
  787. start_date = '".$event->start_date."',
  788. end_date = '".$event->end_date."'";
  789. Database::query($sql);
  790. $new_event_id = Database::insert_id();
  791. $this->course->resources[RESOURCE_EVENT][$id]->destination_id = $new_event_id;
  792. //Copy event attachment
  793. $origin_path = $this->course->backup_path.'/upload/calendar/';
  794. $destination_path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/upload/calendar/';
  795. if (!empty($this->course->orig)) {
  796. $table_attachment = Database :: get_course_table(TABLE_AGENDA_ATTACHMENT, $this->course->orig);
  797. $sql = 'SELECT path, comment, size, filename FROM '.$table_attachment.' WHERE c_id = '.$this->destination_course_id.' AND agenda_id = '.$id;
  798. $attachment_event = Database::query($sql);
  799. $attachment_event = Database::fetch_object($attachment_event);
  800. if (file_exists($origin_path.$attachment_event->path) && !is_dir($origin_path.$attachment_event->path) ) {
  801. $new_filename = uniqid(''); //ass seen in the add_agenda_attachment_file() function in agenda.inc.php
  802. $copy_result = copy($origin_path.$attachment_event->path, $destination_path.$new_filename);
  803. //$copy_result = true;
  804. if ($copy_result) {
  805. $table_attachment = Database :: get_course_table(TABLE_AGENDA_ATTACHMENT);
  806. $sql = "INSERT INTO ".$table_attachment." SET c_id = ".$this->destination_course_id." , path = '".self::DBUTF8escapestring($new_filename)."', comment = '".self::DBUTF8escapestring($attachment_event->comment)."', size = '".$attachment_event->size."', filename = '".$attachment_event->filename."' , agenda_id = '".$new_event_id."' ";
  807. Database::query($sql);
  808. }
  809. }
  810. } else {
  811. // get the info of the file
  812. if(!empty($event->attachment_path) && is_file($origin_path.$event->attachment_path) && is_readable($origin_path.$event->attachment_path)) {
  813. $new_filename = uniqid(''); //ass seen in the add_agenda_attachment_file() function in agenda.inc.php
  814. $copy_result = copy($origin_path.$event->attachment_path, $destination_path.$new_filename);
  815. if ($copy_result) {
  816. $table_attachment = Database :: get_course_table(TABLE_AGENDA_ATTACHMENT);
  817. $sql = "INSERT INTO ".$table_attachment." SET c_id = ".$this->destination_course_id." , path = '".self::DBUTF8escapestring($new_filename)."', comment = '".self::DBUTF8escapestring($event->attachment_comment)."', size = '".$event->attachment_size."', filename = '".$event->attachment_filename."' , agenda_id = '".$new_event_id."' ";
  818. Database::query($sql);
  819. }
  820. }
  821. }
  822. }
  823. }
  824. }
  825. /**
  826. * Restore course-description
  827. */
  828. function restore_course_descriptions($session_id = 0) {
  829. if ($this->course->has_resources(RESOURCE_COURSEDESCRIPTION)) {
  830. $table = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  831. $resources = $this->course->resources;
  832. foreach ($resources[RESOURCE_COURSEDESCRIPTION] as $id => $cd) {
  833. if (isset($_POST['destination_course'])) {
  834. $course_destination=Security::remove_XSS($_POST['destination_course']);
  835. $course_destination=api_get_course_info($course_destination);
  836. $course_destination=$course_destination['path'];
  837. } else {
  838. $course_destination=$this->course->destination_path;
  839. }
  840. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  841. $description_content = DocumentManager::replace_urls_inside_content_html_from_copy_course($cd->content, $this->course->code, $this->course->destination_path);
  842. $condition_session = "";
  843. if (!empty($session_id)) {
  844. $session_id = intval($session_id);
  845. $condition_session = " , session_id = '$session_id' ";
  846. }
  847. $sql = "INSERT INTO ".$table." SET c_id = ".$this->destination_course_id." , description_type = '".self::DBUTF8escapestring($cd->description_type)."',title = '".self::DBUTF8escapestring($cd->title)."', content = '".self::DBUTF8escapestring($description_content)."' $condition_session";
  848. Database::query($sql);
  849. $this->course->resources[RESOURCE_COURSEDESCRIPTION][$id]->destination_id = Database::insert_id();
  850. }
  851. }
  852. }
  853. /**
  854. * Restore announcements
  855. */
  856. function restore_announcements() {
  857. if ($this->course->has_resources(RESOURCE_ANNOUNCEMENT)) {
  858. $table = Database :: get_course_table(TABLE_ANNOUNCEMENT);
  859. $resources = $this->course->resources;
  860. foreach ($resources[RESOURCE_ANNOUNCEMENT] as $id => $announcement) {
  861. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  862. $announcement->content = DocumentManager::replace_urls_inside_content_html_from_copy_course($announcement->content, $this->course->code, $this->course->destination_path);
  863. $sql = "INSERT INTO ".$table." " ."SET
  864. c_id = ".$this->destination_course_id." ,
  865. title = '".self::DBUTF8escapestring($announcement->title)."'," .
  866. "content = '".self::DBUTF8escapestring($announcement->content)."', " .
  867. "end_date = '".$announcement->date."', " .
  868. "display_order = '".$announcement->display_order."', " .
  869. "email_sent = '".$announcement->email_sent."'";
  870. Database::query($sql);
  871. $new_announcement_id = Database::insert_id();
  872. $this->course->resources[RESOURCE_ANNOUNCEMENT][$id]->destination_id = $new_announcement_id;
  873. $origin_path = $this->course->backup_path.'/upload/announcements/';
  874. $destination_path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/upload/announcements/';
  875. //Copy announcement attachment file
  876. if (!empty($this->course->orig)) {
  877. $table_attachment = Database :: get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT, $this->course->orig);
  878. $sql = 'SELECT path, comment, size, filename FROM '.$table_attachment.' WHERE c_id = '.$this->destination_course_id.' AND announcement_id = '.$id;
  879. $attachment_event = Database::query($sql);
  880. $attachment_event = Database::fetch_object($attachment_event);
  881. if (file_exists($origin_path.$attachment_event->path) && !is_dir($origin_path.$attachment_event->path) ) {
  882. $new_filename = uniqid(''); //ass seen in the add_agenda_attachment_file() function in agenda.inc.php
  883. $copy_result = copy($origin_path.$attachment_event->path, $destination_path.$new_filename);
  884. //error_log($destination_path.$new_filename); error_log($copy_result);
  885. //$copy_result = true;
  886. if ($copy_result) {
  887. $table_attachment = Database :: get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  888. $sql = "INSERT INTO ".$table_attachment." SET c_id = ".$this->destination_course_id." , path = '".self::DBUTF8escapestring($new_filename)."', comment = '".self::DBUTF8escapestring($attachment_event->comment)."', size = '".$attachment_event->size."', filename = '".$attachment_event->filename."' , announcement_id = '".$new_announcement_id."' ";
  889. Database::query($sql);
  890. }
  891. }
  892. } else {
  893. // get the info of the file
  894. if(!empty($announcement->attachment_path) && is_file($origin_path.$announcement->attachment_path) && is_readable($origin_path.$announcement->attachment_path)) {
  895. $new_filename = uniqid(''); //ass seen in the add_agenda_attachment_file() function in agenda.inc.php
  896. $copy_result = copy($origin_path.$announcement->attachment_path, $destination_path.$new_filename);
  897. if ($copy_result) {
  898. $table_attachment = Database :: get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  899. $sql = "INSERT INTO ".$table_attachment." SET c_id = ".$this->destination_course_id." , path = '".self::DBUTF8escapestring($new_filename)."', comment = '".self::DBUTF8escapestring($announcement->attachment_comment)."', size = '".$announcement->attachment_size."', filename = '".$announcement->attachment_filename."' , announcement_id = '".$new_announcement_id."' ";
  900. Database::query($sql);
  901. }
  902. }
  903. }
  904. }
  905. }
  906. }
  907. /**
  908. * Restore Quiz
  909. */
  910. function restore_quizzes($session_id = 0, $respect_base_content = false) {
  911. if ($this->course->has_resources(RESOURCE_QUIZ)) {
  912. $table_qui = Database :: get_course_table(TABLE_QUIZ_TEST);
  913. $table_rel = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  914. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  915. $resources = $this->course->resources;
  916. foreach ($resources[RESOURCE_QUIZ] as $id => $quiz) {
  917. $doc = '';
  918. if (strlen($quiz->media) > 0) {
  919. if ($this->course->resources[RESOURCE_DOCUMENT][$quiz->media]->is_restored()) {
  920. $sql = "SELECT path FROM ".$table_doc." WHERE c_id = ".$this->destination_course_id." AND id = ".$resources[RESOURCE_DOCUMENT][$quiz->media]->destination_id;
  921. $doc = Database::query($sql);
  922. $doc = Database::fetch_object($doc);
  923. $doc = str_replace('/audio/', '', $doc->path);
  924. }
  925. }
  926. if ($id != -1) {
  927. if ($respect_base_content) {
  928. $my_session_id = $quiz->session_id;
  929. if (!empty($quiz->session_id)) {
  930. $my_session_id = $session_id;
  931. }
  932. $condition_session = " , session_id = '$my_session_id' ";
  933. } else {
  934. $condition_session = "";
  935. if (!empty($session_id)) {
  936. $session_id = intval($session_id);
  937. $condition_session = " , session_id = '$session_id' ";
  938. }
  939. }
  940. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  941. $quiz->description = DocumentManager::replace_urls_inside_content_html_from_copy_course($quiz->description, $this->course->code, $this->course->destination_path);
  942. global $_custom;
  943. if (isset($_custom['exercises_clean_dates_when_restoring']) && $_custom['exercises_clean_dates_when_restoring']) {
  944. $quiz->start_time = null;
  945. $quiz->end_time = null;
  946. }
  947. // Normal tests are stored in the database.
  948. $sql = "INSERT INTO ".$table_qui." SET
  949. c_id = ".$this->destination_course_id." ,
  950. title = '".self::DBUTF8escapestring($quiz->title).
  951. "', description = '".self::DBUTF8escapestring($quiz->description).
  952. "', type = '".$quiz->quiz_type.
  953. "', random = '".$quiz->random.
  954. "', active = '".$quiz->active.
  955. "', sound = '".self::DBUTF8escapestring($doc).
  956. "', max_attempt = ".(int)$quiz->attempts.
  957. ", results_disabled = ".(int)$quiz->results_disabled.
  958. ", access_condition = '".$quiz->access_condition.
  959. "', start_time = '".$quiz->start_time.
  960. "', end_time = '".$quiz->end_time.
  961. "', feedback_type = ".(int)$quiz->feedback_type.
  962. ", random_answers = ".(int)$quiz->random_answers.
  963. ", expired_time = ".(int)$quiz->expired_time.
  964. $condition_session;
  965. Database::query($sql);
  966. $new_id = Database::insert_id();
  967. } else {
  968. // $id = -1 identifies the fictionary test for collecting orphan questions. We do not store it in the database.
  969. $new_id = -1;
  970. }
  971. $this->course->resources[RESOURCE_QUIZ][$id]->destination_id = $new_id;
  972. $order = 0;
  973. foreach ($quiz->question_ids as $index => $question_id) {
  974. $qid = $this->restore_quiz_question($question_id);
  975. $question_order = $quiz->question_orders[$index] ? $quiz->question_orders[$index] : ++$order;
  976. //$question_order = $quiz->question_orders[$index] ? $quiz->question_orders[$index] : 1;
  977. $sql = "INSERT IGNORE INTO ".$table_rel." SET c_id = ".$this->destination_course_id." , question_id = ".$qid.", exercice_id = ".$new_id.", question_order = ".$question_order;
  978. Database::query($sql);
  979. }
  980. }
  981. }
  982. }
  983. /**
  984. * Restore quiz-questions
  985. */
  986. function restore_quiz_question($id) {
  987. $resources = $this->course->resources;
  988. $question = $resources[RESOURCE_QUIZQUESTION][$id];
  989. $new_id=0;
  990. if(is_object($question)) {
  991. if ($question->is_restored()) {
  992. return $question->destination_id;
  993. }
  994. $table_que = Database::get_course_table(TABLE_QUIZ_QUESTION);
  995. $table_ans = Database::get_course_table(TABLE_QUIZ_ANSWER);
  996. $table_options = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  997. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  998. $question->description = DocumentManager::replace_urls_inside_content_html_from_copy_course($question->description, $this->course->code, $this->course->destination_path);
  999. $sql = "INSERT INTO ".$table_que." SET
  1000. c_id = ".$this->destination_course_id." ,
  1001. question = '".self::DBUTF8escapestring($question->question)."',
  1002. description = '".self::DBUTF8escapestring($question->description)."',
  1003. ponderation = '".self::DBUTF8escapestring($question->ponderation)."',
  1004. position = '".self::DBUTF8escapestring($question->position)."',
  1005. type='".self::DBUTF8escapestring($question->quiz_type)."',
  1006. picture='".self::DBUTF8escapestring($question->picture)."',
  1007. level='".self::DBUTF8escapestring($question->level)."',
  1008. extra='".self::DBUTF8escapestring($question->extra)."'";
  1009. Database::query($sql);
  1010. $new_id = Database::insert_id();
  1011. if ($question->quiz_type == MATCHING) { // for answer type matching
  1012. $answerid = 0;
  1013. $t = array();
  1014. foreach ($question->answers as $index => $answer) {
  1015. $t[$answer['position']] = $answer;
  1016. }
  1017. foreach ($t as $index => $answer) {
  1018. $sql = "INSERT INTO ".$table_ans." SET c_id = ".$this->destination_course_id." , id= '".$index."',question_id = '".$new_id."', answer = '".self::DBUTF8escapestring($answer['answer'])."', correct = '".$answer['correct']."', comment = '".self::DBUTF8escapestring($answer['comment'])."', ponderation='".$answer['ponderation']."', position = '".$answer['position']."', hotspot_coordinates = '".$answer['hotspot_coordinates']."', hotspot_type = '".$answer['hotspot_type']."'";
  1019. Database::query($sql);
  1020. }
  1021. } else {
  1022. foreach ($question->answers as $index => $answer) {
  1023. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1024. $answer['answer'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($answer['answer'], $this->course->code, $this->course->destination_path);
  1025. $answer['comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($answer['comment'], $this->course->code, $this->course->destination_path);
  1026. $sql = "INSERT INTO ".$table_ans." SET c_id = ".$this->destination_course_id." , id= '". ($index +1)."',question_id = '".$new_id."', answer = '".self::DBUTF8escapestring($answer['answer'])."', correct = '".$answer['correct']."', comment = '".self::DBUTF8escapestring($answer['comment'])."', ponderation='".$answer['ponderation']."', position = '".$answer['position']."', hotspot_coordinates = '".$answer['hotspot_coordinates']."', hotspot_type = '".$answer['hotspot_type']."'";
  1027. Database::query($sql);
  1028. }
  1029. }
  1030. //@todo check this
  1031. $course_id = api_get_course_int_id();
  1032. //Moving quiz_question_options
  1033. if ($question->quiz_type == MULTIPLE_ANSWER_TRUE_FALSE) {
  1034. $question_option_list = Question::readQuestionOption($id, $course_id);
  1035. $old_option_ids = array();
  1036. foreach ($question_option_list as $item) {
  1037. $old_id = $item['id'];
  1038. unset($item['id']);
  1039. $item['question_id'] = $new_id;
  1040. $question_option_id = Database::insert($table_options, $item);
  1041. $old_option_ids[$old_id] = $question_option_id;
  1042. }
  1043. $new_answers = Database::select('id, correct', $table_ans, array('WHERE' => array('question_id = ? AND c_id = ? '=>array($new_id, $this->destination_course_id))));
  1044. foreach ($new_answers as $answer_item) {
  1045. $params['correct'] = $old_option_ids[$answer_item['correct']];
  1046. $question_option_id = Database::update($table_ans, $params, array('id = ?'=>$answer_item['id']));
  1047. }
  1048. }
  1049. $this->course->resources[RESOURCE_QUIZQUESTION][$id]->destination_id = $new_id;
  1050. }
  1051. return $new_id;
  1052. }
  1053. /**
  1054. * Restore surveys
  1055. */
  1056. function restore_surveys() {
  1057. if ($this->course->has_resources(RESOURCE_SURVEY)) {
  1058. $table_sur = Database :: get_course_table(TABLE_SURVEY);
  1059. $table_que = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  1060. $table_ans = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  1061. $resources = $this->course->resources;
  1062. foreach ($resources[RESOURCE_SURVEY] as $id => $survey) {
  1063. $sql_check = 'SELECT survey_id FROM '.$table_sur.'
  1064. WHERE c_id = '.$this->destination_course_id.' AND
  1065. code = "'.self::DBUTF8escapestring($survey->code).'" AND
  1066. lang = "'.self::DBUTF8escapestring($survey->lang).'" ';
  1067. $result_check = Database::query($sql_check);
  1068. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1069. $survey->title = DocumentManager::replace_urls_inside_content_html_from_copy_course($survey->title, $this->course->code, $this->course->destination_path);
  1070. $survey->subtitle = DocumentManager::replace_urls_inside_content_html_from_copy_course($survey->subtitle, $this->course->code, $this->course->destination_path);
  1071. $survey->intro = DocumentManager::replace_urls_inside_content_html_from_copy_course($survey->intro, $this->course->code, $this->course->destination_path);
  1072. $survey->surveythanks = DocumentManager::replace_urls_inside_content_html_from_copy_course($survey->surveythanks, $this->course->code, $this->course->destination_path);
  1073. $doc = '';
  1074. $sql = "INSERT INTO ".$table_sur." " ."SET
  1075. c_id = ".$this->destination_course_id." ,
  1076. code = '".self::DBUTF8escapestring($survey->code)."', " .
  1077. "title = '".self::DBUTF8escapestring($survey->title)."', " .
  1078. "subtitle = '".self::DBUTF8escapestring($survey->subtitle)."', " .
  1079. "author = '".self::DBUTF8escapestring($survey->author)."', " .
  1080. "lang = '".self::DBUTF8escapestring($survey->lang)."', " .
  1081. "avail_from = '".self::DBUTF8escapestring($survey->avail_from)."', " .
  1082. "avail_till = '".self::DBUTF8escapestring($survey->avail_till)."', " .
  1083. "is_shared = '".self::DBUTF8escapestring($survey->is_shared)."', " .
  1084. "template = '".self::DBUTF8escapestring($survey->template)."', " .
  1085. "intro = '".self::DBUTF8escapestring($survey->intro)."', " .
  1086. "surveythanks = '".self::DBUTF8escapestring($survey->surveythanks)."', " .
  1087. "creation_date = '".self::DBUTF8escapestring($survey->creation_date)."', " .
  1088. "invited = '0', " .
  1089. "answered = '0', " .
  1090. "invite_mail = '".self::DBUTF8escapestring($survey->invite_mail)."', " .
  1091. "reminder_mail = '".self::DBUTF8escapestring($survey->reminder_mail)."'";
  1092. //An existing survey exists with the same code and the same language
  1093. if (Database::num_rows($result_check) == 1) {
  1094. switch ($this->file_option) {
  1095. case FILE_SKIP:
  1096. //Do nothing
  1097. break;
  1098. case FILE_RENAME:
  1099. $survey_code = $survey->code.'_';
  1100. $i=1;
  1101. $temp_survey_code = $survey_code.$i;
  1102. while (!$this->is_survey_code_available($temp_survey_code)) {
  1103. $temp_survey_code = $survey_code.++$i;
  1104. }
  1105. $survey_code = $temp_survey_code;
  1106. $sql = "INSERT INTO ".$table_sur." " ."SET
  1107. c_id = ".$this->destination_course_id." ,
  1108. code = '".self::DBUTF8escapestring($survey_code)."', " .
  1109. "title = '".self::DBUTF8escapestring($survey->title)."', " .
  1110. "subtitle = '".self::DBUTF8escapestring($survey->subtitle)."', " .
  1111. "author = '".self::DBUTF8escapestring($survey->author)."', " .
  1112. "lang = '".self::DBUTF8escapestring($survey->lang)."', " .
  1113. "avail_from = '".self::DBUTF8escapestring($survey->avail_from)."', " .
  1114. "avail_till = '".self::DBUTF8escapestring($survey->avail_till)."', " .
  1115. "is_shared = '".self::DBUTF8escapestring($survey->is_shared)."', " .
  1116. "template = '".self::DBUTF8escapestring($survey->template)."', " .
  1117. "intro = '".self::DBUTF8escapestring($survey->intro)."', " .
  1118. "surveythanks = '".self::DBUTF8escapestring($survey->surveythanks)."', " .
  1119. "creation_date = '".self::DBUTF8escapestring($survey->creation_date)."', " .
  1120. "invited = '0', " .
  1121. "answered = '0', " .
  1122. "invite_mail = '".self::DBUTF8escapestring($survey->invite_mail)."', " .
  1123. "reminder_mail = '".self::DBUTF8escapestring($survey->reminder_mail)."'";
  1124. //Insert the new source survey
  1125. Database::query($sql);
  1126. $new_id = Database::insert_id();
  1127. $this->course->resources[RESOURCE_SURVEY][$id]->destination_id = $new_id;
  1128. foreach ($survey->question_ids as $index => $question_id) {
  1129. $qid = $this->restore_survey_question($question_id, $new_id);
  1130. $sql = "UPDATE ".$table_que." " .
  1131. "SET survey_id = ".$new_id." WHERE c_id = ".$this->destination_course_id." AND question_id = $qid";
  1132. Database::query($sql);
  1133. $sql = "UPDATE ".$table_ans." ".
  1134. "SET survey_id = ".$new_id." WHERE c_id = ".$this->destination_course_id." AND question_id = $qid";
  1135. Database::query($sql);
  1136. }
  1137. break;
  1138. case FILE_OVERWRITE:
  1139. // Delete the existing survey with the same code and language and import the one of the source course
  1140. // getting the information of the survey (used for when the survey is shared)
  1141. require_once(api_get_path(SYS_CODE_PATH).'survey/survey.lib.php');
  1142. $sql_select_existing_survey = "SELECT * FROM $table_sur WHERE c_id = ".$this->destination_course_id." AND survey_id='".self::DBUTF8escapestring(Database::result($result_check,0,0))."'";
  1143. $result = Database::query($sql_select_existing_survey);
  1144. $survey_data = Database::fetch_array($result,'ASSOC');
  1145. // if the survey is shared => also delete the shared content
  1146. if (is_numeric($survey_data['survey_share'])) {
  1147. survey_manager::delete_survey($survey_data['survey_share'], true,$this->destination_course_id);
  1148. }
  1149. $return = survey_manager :: delete_survey($survey_data['survey_id'],false,$this->destination_course_id);
  1150. //Insert the new source survey
  1151. Database::query($sql);
  1152. $new_id = Database::insert_id();
  1153. $this->course->resources[RESOURCE_SURVEY][$id]->destination_id = $new_id;
  1154. foreach ($survey->question_ids as $index => $question_id)
  1155. {
  1156. $qid = $this->restore_survey_question($question_id, $new_id);
  1157. $sql = "UPDATE ".$table_que." " .
  1158. "SET survey_id = ".$new_id." WHERE " .
  1159. "question_id = ".$qid."";
  1160. Database::query($sql);
  1161. $sql = "UPDATE ".$table_ans." ".
  1162. "SET survey_id = ".$new_id." WHERE " .
  1163. "question_id = ".$qid."";
  1164. Database::query($sql);
  1165. }
  1166. break;
  1167. default:
  1168. break;
  1169. }
  1170. }
  1171. //No existing survey with the same language and the same code, we just copy the survey
  1172. else {
  1173. Database::query($sql);
  1174. $new_id = Database::insert_id();
  1175. $this->course->resources[RESOURCE_SURVEY][$id]->destination_id = $new_id;
  1176. foreach ($survey->question_ids as $index => $question_id)
  1177. {
  1178. $qid = $this->restore_survey_question($question_id, $new_id);
  1179. $sql = "UPDATE ".$table_que." " .
  1180. "SET survey_id = ".$new_id." WHERE " .
  1181. "question_id = ".$qid."";
  1182. Database::query($sql);
  1183. $sql = "UPDATE ".$table_ans." ".
  1184. "SET survey_id = ".$new_id." WHERE " .
  1185. "question_id = ".$qid."";
  1186. Database::query($sql);
  1187. }
  1188. }
  1189. }
  1190. }
  1191. }
  1192. /**
  1193. * Check availability of a survey code
  1194. */
  1195. function is_survey_code_available($survey_code)
  1196. {
  1197. $table_sur = Database :: get_course_table(TABLE_SURVEY);
  1198. $sql = "SELECT * FROM $table_sur WHERE c_id = ".$this->destination_course_id." AND code='".self::DBUTF8escapestring($survey_code)."'";
  1199. $result = Database::query($sql);
  1200. if(Database::num_rows($result) > 0) return false; else return true;
  1201. }
  1202. /**
  1203. * Restore survey-questions
  1204. */
  1205. function restore_survey_question($id, $survey_id)
  1206. {
  1207. $resources = $this->course->resources;
  1208. $question = $resources[RESOURCE_SURVEYQUESTION][$id];
  1209. $new_id=0;
  1210. if (is_object($question)) {
  1211. if ($question->is_restored()) {
  1212. return $question->destination_id;
  1213. }
  1214. $table_que = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  1215. $table_ans = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  1216. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1217. $question->survey_question = DocumentManager::replace_urls_inside_content_html_from_copy_course($question->survey_question, $this->course->code, $this->course->destination_path);
  1218. $sql = "INSERT INTO ".$table_que." SET
  1219. c_id = ".$this->destination_course_id." ,
  1220. survey_id = '".self::DBUTF8escapestring($survey_id)."', " .
  1221. "survey_question = '".self::DBUTF8escapestring($question->survey_question)."', " .
  1222. "survey_question_comment = '".self::DBUTF8escapestring($question->survey_question_comment)."', " .
  1223. "type = '".self::DBUTF8escapestring($question->survey_question_type)."', " .
  1224. "display = '".self::DBUTF8escapestring($question->display)."', " .
  1225. "sort = '".self::DBUTF8escapestring($question->sort)."', " .
  1226. "shared_question_id = '".self::DBUTF8escapestring($question->shared_question_id)."', " .
  1227. "max_value = '".self::DBUTF8escapestring($question->max_value)."' ";
  1228. Database::query($sql);
  1229. $new_id = Database::insert_id();
  1230. foreach ($question->answers as $index => $answer) {
  1231. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1232. $answer['option_text'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($answer['option_text'], $this->course->code, $this->course->destination_path);
  1233. $sql = "INSERT INTO ".$table_ans." SET
  1234. c_id = ".$this->destination_course_id." ,
  1235. question_id = '".self::DBUTF8escapestring($new_id)."', " .
  1236. "option_text = '".self::DBUTF8escapestring($answer['option_text'])."', " .
  1237. "sort = '".self::DBUTF8escapestring($answer['sort'])."', " .
  1238. "survey_id = '".self::DBUTF8escapestring($survey_id)."'";
  1239. Database::query($sql);
  1240. }
  1241. $this->course->resources[RESOURCE_SURVEYQUESTION][$id]->destination_id = $new_id;
  1242. }
  1243. return $new_id;
  1244. }
  1245. /**
  1246. * Restore learnpaths
  1247. */
  1248. function restore_learnpaths($session_id = 0, $respect_base_content = false)
  1249. {
  1250. if ($this->course->has_resources(RESOURCE_LEARNPATH)) {
  1251. $table_main = Database::get_course_table(TABLE_LP_MAIN);
  1252. $table_item = Database::get_course_table(TABLE_LP_ITEM);
  1253. $table_tool = Database::get_course_table(TABLE_TOOL_LIST);
  1254. $resources = $this->course->resources;
  1255. $origin_path = $this->course->backup_path.'/upload/learning_path/images/';
  1256. $destination_path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/upload/learning_path/images/';
  1257. foreach ($resources[RESOURCE_LEARNPATH] as $id => $lp) {
  1258. $condition_session = "";
  1259. if (!empty($session_id)) {
  1260. if ($respect_base_content) {
  1261. $my_session_id = $lp->session_id;
  1262. if (!empty($lp->session_id)) {
  1263. $my_session_id = $session_id;
  1264. }
  1265. $condition_session = " , session_id = '$my_session_id' ";
  1266. } else {
  1267. $session_id = intval($session_id);
  1268. $condition_session = " , session_id = '$session_id' ";
  1269. }
  1270. }
  1271. //Adding the author's image
  1272. if (!empty($lp->preview_image)) {
  1273. $new_filename = uniqid('').$new_filename.substr($lp->preview_image,strlen($lp->preview_image)-7, strlen($lp->preview_image));
  1274. if (file_exists($origin_path.$lp->preview_image) && !is_dir($origin_path.$lp->preview_image)) {
  1275. $copy_result = copy($origin_path.$lp->preview_image, $destination_path.$new_filename);
  1276. //$copy_result = true;
  1277. if ($copy_result) {
  1278. $lp->preview_image = $new_filename;
  1279. } else {
  1280. $lp->preview_image ='';
  1281. }
  1282. }
  1283. }
  1284. $sql = "INSERT INTO ".$table_main." SET
  1285. c_id = ".$this->destination_course_id." ,
  1286. lp_type = '".$lp->lp_type."', " .
  1287. "name = '".self::DBUTF8escapestring($lp->name)."', " .
  1288. "path = '".self::DBUTF8escapestring($lp->path)."', " .
  1289. "ref = '".$lp->ref."', " .
  1290. "description = '".self::DBUTF8escapestring($lp->description)."', " .
  1291. "content_local = '".self::DBUTF8escapestring($lp->content_local)."', " .
  1292. "default_encoding = '".self::DBUTF8escapestring($lp->default_encoding)."', " .
  1293. "default_view_mod = '".self::DBUTF8escapestring($lp->default_view_mod)."', " .
  1294. "prevent_reinit = '".self::DBUTF8escapestring($lp->prevent_reinit)."', " .
  1295. "force_commit = '".self::DBUTF8escapestring($lp->force_commit)."', " .
  1296. "content_maker = '".self::DBUTF8escapestring($lp->content_maker)."', " .
  1297. "display_order = '".self::DBUTF8escapestring($lp->display_order)."', " .
  1298. "js_lib = '".self::DBUTF8escapestring($lp->js_lib)."', " .
  1299. "content_license = '".self::DBUTF8escapestring($lp->content_license)."', " .
  1300. "author = '".self::DBUTF8escapestring($lp->author)."', " .
  1301. "preview_image = '".self::DBUTF8escapestring($lp->preview_image)."', " .
  1302. "use_max_score = '".self::DBUTF8escapestring($lp->use_max_score)."', " .
  1303. "autolunch = '".self::DBUTF8escapestring($lp->autolunch)."', " .
  1304. "created_on = '".self::DBUTF8escapestring($lp->created_on)."', " .
  1305. "modified_on = '".self::DBUTF8escapestring($lp->modified_on)."', " .
  1306. "publicated_on = '".self::DBUTF8escapestring($lp->publicated_on)."', " .
  1307. "expired_on = '".self::DBUTF8escapestring($lp->expired_on)."', " .
  1308. "debug = '".self::DBUTF8escapestring($lp->debug)."' $condition_session ";
  1309. Database::query($sql);
  1310. $new_lp_id = Database::insert_id();
  1311. if ($lp->visibility) {
  1312. $sql = "INSERT INTO $table_tool SET c_id = ".$this->destination_course_id." , name='".self::DBUTF8escapestring($lp->name)."', link='newscorm/lp_controller.php?action=view&lp_id=$new_lp_id', image='scormbuilder.gif', visibility='1', admin='0', address='squaregrey.gif'";
  1313. Database::query($sql);
  1314. }
  1315. if ($new_lp_id) {
  1316. api_item_property_update($this->destination_course_info, TOOL_LEARNPATH, $new_lp_id, 'LearnpathAdded', api_get_user_id(), 0, 0, 0, 0, $session_id);
  1317. }
  1318. $new_item_ids = array();
  1319. $parent_item_ids = array();
  1320. $previous_item_ids = array();
  1321. $next_item_ids = array();
  1322. $old_prerequisite = array();
  1323. $old_refs = array();
  1324. $prerequisite_ids = array();
  1325. foreach ($lp->get_items() as $index => $item) {
  1326. /*
  1327. if ($item['id'] != 0)
  1328. {
  1329. // Links in learnpath have types 'Link _self' or 'Link _blank'. We only need 'Link' here.
  1330. $type_parts = explode(' ',$item['type']);
  1331. $item['id'] = $this->course->resources[$type_parts[0]][$item['id']]->destination_id;
  1332. }
  1333. */
  1334. /*
  1335. //Get the new ref ID for all items that are not sco (chamilo quizzes, documents, etc)
  1336. $ref = '';
  1337. if(!empty($item['ref']) && $lp->lp_type!='2'){
  1338. $ref = $this->get_new_id($item['item_type'],$item['ref']);
  1339. } else {
  1340. $ref = $item['ref'];
  1341. }*/
  1342. // we set the ref code here and then we update in a for loop
  1343. $ref = $item['ref'];
  1344. //Dealing with path the same way as ref as some data has been put into path when it's a
  1345. //local resource
  1346. $path = self::DBUTF8escapestring($item['path']);
  1347. if(strval(intval($path)) === $path) {
  1348. if (!empty($session_id)) {
  1349. $path = intval($path);
  1350. } else {
  1351. $path = $this->get_new_id($item['item_type'],$path);
  1352. }
  1353. }
  1354. $sql = "INSERT INTO ".$table_item." SET
  1355. c_id = ".$this->destination_course_id." ,
  1356. lp_id = '". self::DBUTF8escapestring($new_lp_id)."', " .
  1357. "item_type='". self::DBUTF8escapestring($item['item_type'])."', " .
  1358. "ref = '". self::DBUTF8escapestring($ref)."', " .
  1359. "title = '". self::DBUTF8escapestring($item['title'])."', " .
  1360. "description ='". self::DBUTF8escapestring($item['description'])."', " .
  1361. "path = '". self::DBUTF8escapestring($path)."', " .
  1362. "min_score = '". self::DBUTF8escapestring($item['min_score'])."', " .
  1363. "max_score = '". self::DBUTF8escapestring($item['max_score'])."', " .
  1364. "mastery_score = '". self::DBUTF8escapestring($item['mastery_score'])."', " .
  1365. "parent_item_id = '". self::DBUTF8escapestring($item['parent_item_id'])."', " .
  1366. "previous_item_id = '". self::DBUTF8escapestring($item['previous_item_id'])."', " .
  1367. "next_item_id = '". self::DBUTF8escapestring($item['next_item_id'])."', " .
  1368. "display_order = '". self::DBUTF8escapestring($item['display_order'])."', " .
  1369. "prerequisite = '". self::DBUTF8escapestring($item['prerequisite'])."', " .
  1370. "parameters='". self::DBUTF8escapestring($item['parameters'])."', " .
  1371. "audio='". self::DBUTF8escapestring($item['audio'])."', " .
  1372. "launch_data = '". self::DBUTF8escapestring($item['launch_dataprereq_type'])."'";
  1373. Database::query($sql);
  1374. $new_item_id = Database::insert_id();
  1375. //save a link between old and new item IDs
  1376. $new_item_ids[$item['id']] = $new_item_id;
  1377. //save a reference of items that need a parent_item_id refresh
  1378. $parent_item_ids[$new_item_id] = $item['parent_item_id'];
  1379. //save a reference of items that need a previous_item_id refresh
  1380. $previous_item_ids[$new_item_id] = $item['previous_item_id'];
  1381. //save a reference of items that need a next_item_id refresh
  1382. $next_item_ids[$new_item_id] = $item['next_item_id'];
  1383. if (!empty($item['prerequisite'])) {
  1384. if ($lp->lp_type =='2') {
  1385. // if is an sco
  1386. $old_prerequisite[$new_item_id]= $item['prerequisite'];
  1387. } else {
  1388. $old_prerequisite[$new_item_id]= $new_item_ids[$item['prerequisite']];
  1389. }
  1390. }
  1391. if (!empty($ref)) {
  1392. if ($lp->lp_type =='2') {
  1393. // if is an sco
  1394. $old_refs[$new_item_id]= $ref;
  1395. } else {
  1396. $old_refs[$new_item_id]= $new_item_ids[$ref];
  1397. }
  1398. }
  1399. $prerequisite_ids[$new_item_id] = $item['prerequisite'];
  1400. }
  1401. // updating prerequisites
  1402. foreach ($old_prerequisite as $key=>$my_old_prerequisite) {
  1403. if($my_old_prerequisite != ''){
  1404. $sql = "UPDATE ".$table_item." SET prerequisite = '".$my_old_prerequisite."' WHERE id = '".$key."' ";
  1405. Database::query($sql);
  1406. }
  1407. }
  1408. //updating refs
  1409. foreach ($old_refs as $key=>$my_old_ref) {
  1410. if ($my_old_ref != '') {
  1411. $sql = "UPDATE ".$table_item." SET ref = '".$my_old_ref."' WHERE id = '".$key."' ";
  1412. Database::query($sql);
  1413. }
  1414. }
  1415. foreach ($parent_item_ids as $new_item_id => $parent_item_old_id) {
  1416. $parent_new_id = 0;
  1417. if($parent_item_old_id != 0){
  1418. $parent_new_id = $new_item_ids[$parent_item_old_id];
  1419. }
  1420. $sql = "UPDATE ".$table_item." SET parent_item_id = '".$parent_new_id."' WHERE id = '".$new_item_id."'";
  1421. Database::query($sql);
  1422. }
  1423. foreach ($previous_item_ids as $new_item_id => $previous_item_old_id) {
  1424. $previous_new_id = 0;
  1425. if($previous_item_old_id != 0){
  1426. $previous_new_id = $new_item_ids[$previous_item_old_id];
  1427. }
  1428. $sql = "UPDATE ".$table_item." SET previous_item_id = '".$previous_new_id."' WHERE id = '".$new_item_id."'";
  1429. Database::query($sql);
  1430. }
  1431. foreach ($next_item_ids as $new_item_id => $next_item_old_id) {
  1432. $next_new_id = 0;
  1433. if($next_item_old_id != 0){
  1434. $next_new_id = $new_item_ids[$next_item_old_id];
  1435. }
  1436. $sql = "UPDATE ".$table_item." SET next_item_id = '".$next_new_id."' WHERE id = '".$new_item_id."'";
  1437. Database::query($sql);
  1438. }
  1439. foreach ($prerequisite_ids as $new_item_id => $prerequisite_old_id) {
  1440. $prerequisite_new_id = 0;
  1441. if($prerequisite_old_id != 0){
  1442. $prerequisite_new_id = $new_item_ids[$prerequisite_old_id];
  1443. }
  1444. $sql = "UPDATE ".$table_item." SET prerequisite = '".$prerequisite_new_id."' WHERE id = '".$new_item_id."'";
  1445. Database::query($sql);
  1446. }
  1447. $this->course->resources[RESOURCE_LEARNPATH][$id]->destination_id = $new_lp_id;
  1448. }
  1449. }
  1450. }
  1451. /**
  1452. * restore works
  1453. * @todo I fixed this crappy function jmontoya
  1454. */
  1455. function restore_student_publication() {
  1456. $work_assignment_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  1457. $work_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  1458. $item_property_table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  1459. //query in student publication
  1460. /* $query_sql_fin_sp='INSERT IGNORE INTO '.$my_tbl_db_destination.' (c_id, id,url,title,description,author,active,accepted,post_group_id,sent_date,' .
  1461. 'filetype,has_properties,view_properties,qualification,date_of_qualification,' .
  1462. 'parent_id,qualificator_id,session_id) ';
  1463. */
  1464. $sql = 'SELECT c_id, id, url, title,description,author,active,accepted,post_group_id, sent_date,filetype,has_properties,view_properties,qualification,date_of_qualification,qualificator_id,session_id
  1465. FROM '.$work_table.'
  1466. WHERE c_id = '.$this->course_origin_id.' AND filetype="folder" AND active IN (0, 1) ';
  1467. $result = Database::query($sql);
  1468. $folders = Database::store_result($result, 'ASSOC');
  1469. foreach ($folders as $folder) {
  1470. $old_id = $folder['id'];
  1471. unset($folder['id']);
  1472. $folder['c_id'] = $this->destination_course_id;
  1473. $folder['parent_id'] = 0;
  1474. $new_id = Database::insert($work_table, $folder);
  1475. if ($new_id) {
  1476. //query in item property
  1477. $sql = 'SELECT tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type, lastedit_user_id,to_group_id,to_user_id,visibility,start_visible, end_visible
  1478. FROM '.$item_property_table.' ip INNER JOIN '.$work_table.' sp ON ip.ref=sp.id
  1479. WHERE sp.c_id = '.$this->course_origin_id.' AND
  1480. ip.c_id = '.$this->course_origin_id.' AND
  1481. tool="work" AND sp.id = '.$old_id.'';
  1482. $result = Database::query($sql);
  1483. $sub_folders = Database::store_result($result, 'ASSOC');
  1484. foreach ($sub_folders as $sub_folder) {
  1485. $sub_folder['c_id'] = $this->destination_course_id;
  1486. $sub_folder['ref'] = $new_id;
  1487. $new_item_id = Database::insert($item_property_table, $sub_folder);
  1488. }
  1489. $sql = 'SELECT sa.id, sa.expires_on,sa.ends_on,sa.add_to_calendar, sa.enable_qualification, sa.publication_id
  1490. FROM '.$work_assignment_table.' sa INNER JOIN '.$work_table.' sp ON sa.publication_id=sp.id
  1491. WHERE sp.c_id = '.$this->course_origin_id.' AND
  1492. sa.c_id = '.$this->course_origin_id.' AND
  1493. filetype="folder" AND sp.id = '.$old_id.'';
  1494. $result = Database::query($sql);
  1495. $assing_list = Database::store_result($result, 'ASSOC');
  1496. foreach($assing_list as $assign) {
  1497. $assign['c_id'] = $this->destination_course_id;
  1498. $assign['id'] = $new_id;
  1499. Database::insert($work_assignment_table, $assign);
  1500. }
  1501. }
  1502. }
  1503. $destination='../../courses/'.$this->course->destination_path.'/work/';
  1504. $origin='../../courses/'.$this->course->info['path'].'/work/';
  1505. self::allow_create_all_directory($origin,$destination,false);
  1506. }
  1507. /**
  1508. * copy all directory and sub directory
  1509. * @param string The path origin
  1510. * @param string The path destination
  1511. * @param boolean Option Overwrite
  1512. * @return void()
  1513. */
  1514. function allow_create_all_directory($source, $dest, $overwrite = false) {
  1515. if(!is_dir($dest)) {
  1516. mkdir($dest, api_get_permissions_for_new_directories());
  1517. }
  1518. if ($handle = opendir($source)) { // if the folder exploration is sucsessful, continue
  1519. while (false !== ($file = readdir($handle))) { // as long as storing the next file to $file is successful, continue
  1520. if ($file != '.' && $file != '..') {
  1521. $path = $source . '/' . $file;
  1522. if (is_file($path)) {
  1523. /* if (!is_file($dest . '/' . $file) || $overwrite)
  1524. if (!@copy($path, $dest . '/' . $file)) {
  1525. echo '<font color="red">File ('.$path.') '.get_lang('NotHavePermission').'</font>';
  1526. }*/
  1527. } elseif(is_dir($path)) {
  1528. if (!is_dir($dest . '/' . $file))
  1529. mkdir($dest . '/' . $file);
  1530. self:: allow_create_all_directory($path, $dest . '/' . $file, $overwrite);
  1531. }
  1532. }
  1533. }
  1534. closedir($handle);
  1535. }
  1536. }
  1537. /**
  1538. * Gets the new ID of one specific tool item from the tool name and the old ID
  1539. * @param string Tool name
  1540. * @param integer Old ID
  1541. * @return integer New ID
  1542. */
  1543. function get_new_id($tool,$ref)
  1544. {
  1545. //transform $tool into one backup/restore constant
  1546. if($tool == 'hotpotatoes'){$tool = 'document';}
  1547. if(!empty($this->course->resources[$tool][$ref]->destination_id)){
  1548. return $this->course->resources[$tool][$ref]->destination_id;
  1549. }
  1550. return '';
  1551. }
  1552. /**
  1553. * Restore glossary
  1554. */
  1555. function restore_glossary($session_id = 0)
  1556. {
  1557. if ($this->course->has_resources(RESOURCE_GLOSSARY)) {
  1558. $table_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  1559. $t_item_propery = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  1560. $resources = $this->course->resources;
  1561. foreach ($resources[RESOURCE_GLOSSARY] as $id => $glossary) {
  1562. $condition_session = "";
  1563. if (!empty($session_id)) {
  1564. $session_id = intval($session_id);
  1565. $condition_session = " , session_id = '$session_id' ";
  1566. }
  1567. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1568. $glossary->description = DocumentManager::replace_urls_inside_content_html_from_copy_course($glossary->description, $this->course->code, $this->course->destination_path);
  1569. $sql = "INSERT INTO ".$table_glossary." SET c_id = ".$this->destination_course_id." , name = '".self::DBUTF8escapestring($glossary->name)."', description = '".self::DBUTF8escapestring($glossary->description)."', display_order='".self::DBUTF8escapestring($glossary->display_order)."' $condition_session ";
  1570. Database::query($sql);
  1571. $my_id = Database::get_last_insert_id();
  1572. api_item_property_update($this->destination_course_info, TOOL_GLOSSARY, $my_id,"GlossaryAdded", api_get_user_id());
  1573. $this->course->resources[RESOURCE_GLOSSARY][$id]->destination_id = Database::insert_id();
  1574. }
  1575. }
  1576. }
  1577. function restore_wiki($session_id = 0)
  1578. {
  1579. if ($this->course->has_resources(RESOURCE_WIKI))
  1580. {
  1581. // wiki table of the target course
  1582. $table_wiki = Database :: get_course_table('wiki');
  1583. $table_wiki_conf = Database :: get_course_table('wiki_conf');
  1584. // storing all the resources that have to be copied in an array
  1585. $resources = $this->course->resources;
  1586. foreach ($resources[RESOURCE_WIKI] as $id => $wiki)
  1587. {
  1588. //$wiki = new Wiki($obj->page_id, $obj->reflink, $obj->title, $obj->content, $obj->user_id, $obj->group_id, $obj->dtime);
  1589. // the sql statement to insert the groups from the old course to the new course
  1590. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1591. $wiki->content = DocumentManager::replace_urls_inside_content_html_from_copy_course($wiki->content, $this->course->code, $this->course->destination_path);
  1592. $sql = "INSERT INTO $table_wiki (c_id, page_id, reflink, title, content, user_id, group_id, dtime, progress, version, session_id) VALUES (
  1593. ".$this->destination_course_id." ,
  1594. '".self::DBUTF8escapestring($wiki->page_id)."',
  1595. '".self::DBUTF8escapestring($wiki->reflink)."',
  1596. '".self::DBUTF8escapestring($wiki->title)."',
  1597. '".self::DBUTF8escapestring($wiki->content)."',
  1598. '".intval($wiki->user_id)."',
  1599. '".intval($wiki->group_id)."',
  1600. '".self::DBUTF8escapestring($wiki->dtime)."',
  1601. '".self::DBUTF8escapestring($wiki->progress)."',
  1602. '".intval($wiki->version)."',
  1603. '".(!empty($session_id)?intval($session_id):0)."')";
  1604. $rs2 = Database::query($sql);
  1605. $new_id = Database::insert_id();
  1606. $this->course->resources[RESOURCE_WIKI][$id]->destination_id = $new_id;
  1607. $sql = "UPDATE $table_wiki set page_id = '$new_id' WHERE id = '$new_id'";
  1608. Database::query($sql);
  1609. // we also add an entry in wiki_conf
  1610. $sql = "INSERT INTO $table_wiki_conf (c_id, page_id, task, feedback1, feedback2, feedback3, fprogress1, fprogress2, fprogress3, max_size, max_text, max_version, startdate_assig, enddate_assig, delayedsubmit) VALUES
  1611. (".$this->destination_course_id." , '".intval($new_id)."', '', '', '', '', '', '', '', NULL, 0, 0, '0000-00-00 00:00:00', '0000-00-00 00:00:00', 0)";
  1612. $rs1 = Database::query($sql);
  1613. }
  1614. }
  1615. }
  1616. /**
  1617. * Restore Thematics
  1618. */
  1619. function restore_thematic($session_id = 0) {
  1620. if ($this->course->has_resources(RESOURCE_THEMATIC)) {
  1621. $table_thematic = Database :: get_course_table(TABLE_THEMATIC);
  1622. $table_thematic_advance = Database :: get_course_table(TABLE_THEMATIC_ADVANCE);
  1623. $table_thematic_plan = Database :: get_course_table(TABLE_THEMATIC_PLAN);
  1624. $resources = $this->course->resources;
  1625. foreach ($resources[RESOURCE_THEMATIC] as $id => $thematic) {
  1626. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1627. $thematic->content = DocumentManager::replace_urls_inside_content_html_from_copy_course($thematic->content, $this->course->code, $this->course->destination_path);
  1628. $thematic->params['c_id'] = $this->destination_course_id;
  1629. $doc = '';
  1630. $thematic->params['id'] = null;
  1631. $last_id = Database::insert($table_thematic, $thematic->params, false);
  1632. if (is_numeric($last_id)) {
  1633. api_item_property_update($this->destination_course_info, 'thematic', $last_id,"ThematicAdded", api_get_user_id());
  1634. foreach($thematic->thematic_advance_list as $thematic_advance) {
  1635. unset($thematic_advance['id']);
  1636. $thematic_advance['attendance_id'] = 0;
  1637. $thematic_advance['thematic_id'] = $last_id;
  1638. $thematic_advance['c_id'] = $this->destination_course_id;
  1639. $my_id = Database::insert($table_thematic_advance, $thematic_advance, false);
  1640. if (is_numeric($my_id)) {
  1641. api_item_property_update($this->destination_course_info, 'thematic_advance', $my_id,"ThematicAdvanceAdded", api_get_user_id());
  1642. }
  1643. }
  1644. foreach($thematic->thematic_plan_list as $thematic_plan) {
  1645. unset($thematic_advance['id']);
  1646. $thematic_plan['thematic_id'] = $last_id;
  1647. $thematic_plan['c_id'] = $this->destination_course_id;
  1648. $my_id = Database::insert($table_thematic_plan, $thematic_plan, false);
  1649. if (is_numeric($my_id)) {
  1650. api_item_property_update($this->destination_course_info, 'thematic_plan', $my_id, "ThematicPlanAdded", api_get_user_id());
  1651. }
  1652. }
  1653. }
  1654. }
  1655. }
  1656. }
  1657. /**
  1658. * Restore Attendance
  1659. */
  1660. function restore_attendance($session_id = 0) {
  1661. if ($this->course->has_resources(RESOURCE_ATTENDANCE)) {
  1662. $table_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  1663. $table_attendance_calendar = Database :: get_course_table(TABLE_ATTENDANCE_CALENDAR);
  1664. $resources = $this->course->resources;
  1665. foreach ($resources[RESOURCE_ATTENDANCE] as $id => $obj) {
  1666. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1667. $obj->params['description'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($obj->params['description'], $this->course->code, $this->course->destination_path);
  1668. $doc = '';
  1669. $obj->params['id'] = null;
  1670. $obj->params['c_id'] = $this->destination_course_id;
  1671. $last_id = Database::insert($table_attendance, $obj->params);
  1672. if (is_numeric($last_id)) {
  1673. api_item_property_update($this->destination_course_info, TOOL_ATTENDANCE, $last_id,"AttendanceAdded", api_get_user_id());
  1674. foreach($obj->attendance_calendar as $attendance_calendar) {
  1675. unset($attendance_calendar['id']);
  1676. $attendance_calendar['attendance_id'] = $last_id;
  1677. $attendance_calendar['c_id'] = $this->destination_course_id;
  1678. $my_id = Database::insert($table_attendance_calendar, $attendance_calendar);
  1679. /*
  1680. if (is_numeric($my_id)) {
  1681. api_item_property_update($this->destination_course_info, 'thematic_advance', $my_id,"ThematicAdvanceAdded", api_get_user_id());
  1682. }*/
  1683. }
  1684. }
  1685. }
  1686. }
  1687. }
  1688. function DBUTF8escapestring($str)
  1689. {
  1690. if (UTF8_CONVERT) $str = utf8_encode($str);
  1691. return Database::escape_string($str);
  1692. }
  1693. }