CourseRestorer.class.php 91 KB

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