CourseRestorer.class.php 94 KB

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