CourseRestorer.class.php 93 KB

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