CourseRestorer.class.php 95 KB

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