CourseRestorer.class.php 94 KB

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