CourseRestorer.class.php 131 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661
  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. define('FILE_SKIP', 1);
  28. define('FILE_RENAME', 2);
  29. define('FILE_OVERWRITE', 3);
  30. define('UTF8_CONVERT', false); //false by default
  31. /**
  32. * Class to restore items from a course object to a Chamilo-course
  33. * @author Bart Mollet <bart.mollet@hogent.be>
  34. * @author Julio Montoya <gugli100@gmail.com> Several fixes/improvements
  35. * @package chamilo.backup
  36. */
  37. class CourseRestorer
  38. {
  39. /**
  40. * The course-object
  41. */
  42. public $course;
  43. public $destination_course_info;
  44. /**
  45. * What to do with files with same name (FILE_SKIP, FILE_RENAME or
  46. * FILE_OVERWRITE)
  47. */
  48. public $file_option;
  49. public $set_tools_invisible_by_default;
  50. public $skip_content;
  51. public $tools_to_restore = array(
  52. 'announcements',
  53. 'attendance',
  54. 'course_descriptions',
  55. 'documents',
  56. 'events',
  57. 'forum_category',
  58. 'forums',
  59. // 'forum_topics',
  60. 'glossary',
  61. 'quizzes',
  62. 'links',
  63. 'learnpaths',
  64. 'surveys',
  65. //'scorm_documents', ??
  66. 'tool_intro',
  67. 'thematic',
  68. 'wiki'
  69. );
  70. /** Setting per tool */
  71. public $tool_copy_settings = array();
  72. /**
  73. * If true adds the text "copy" in the title of an item (only for LPs right now)
  74. *
  75. * */
  76. public $add_text_in_items = false;
  77. /**
  78. * Create a new CourseRestorer
  79. */
  80. public function __construct($course)
  81. {
  82. $this->course = $course;
  83. $course_info = api_get_course_info($this->course->code);
  84. if (!empty($course_info)) {
  85. $this->course_origin_id = $course_info['real_id'];
  86. } else {
  87. $this->course_origin_id = null;
  88. }
  89. $this->file_option = FILE_RENAME;
  90. $this->set_tools_invisible_by_default = false;
  91. $this->skip_content = array();
  92. }
  93. /**
  94. * Set the file-option
  95. * @param constant $options What to do with files with same name (FILE_SKIP,
  96. * FILE_RENAME or FILE_OVERWRITE)
  97. */
  98. function set_file_option($option)
  99. {
  100. $this->file_option = $option;
  101. }
  102. function set_add_text_in_items($status)
  103. {
  104. $this->add_text_in_items = $status;
  105. }
  106. function set_tool_copy_settings($array)
  107. {
  108. $this->tool_copy_settings = $array;
  109. }
  110. /**
  111. * Restore a course.
  112. * @param string The code of the Chamilo-course in
  113. * @param int The session id
  114. * @param bool Course settings are going to be restore?
  115. */
  116. function restore(
  117. $destination_course_code = '',
  118. $session_id = 0,
  119. $update_course_settings = false,
  120. $respect_base_content = false
  121. ) {
  122. if ($destination_course_code == '') {
  123. $course_info = api_get_course_info();
  124. $this->destination_course_info = $course_info;
  125. $this->course->destination_path = $course_info['path'];
  126. } else {
  127. $course_info = api_get_course_info($destination_course_code);
  128. $this->destination_course_info = $course_info;
  129. $this->course->destination_path = $course_info['path'];
  130. }
  131. $this->destination_course_id = $course_info['real_id'];
  132. // Getting first teacher (for the forums)
  133. $teacher_list = CourseManager::get_teacher_list_from_course_code($course_info['real_id']);
  134. $this->first_teacher_id = api_get_user_id();
  135. if (!empty($teacher_list)) {
  136. foreach ($teacher_list as $teacher) {
  137. $this->first_teacher_id = $teacher['user_id'];
  138. break;
  139. }
  140. }
  141. if (empty($this->course)) {
  142. return false;
  143. }
  144. // Source platform encoding - reading/detection
  145. // The correspondent data field has been added as of version 1.8.6.1
  146. if (empty($this->course->encoding)) {
  147. // The archive has been created by a system which is prior to 1.8.6.1 version.
  148. // In this case we have to detect the encoding.
  149. $sample_text = $this->course->get_sample_text()."\n";
  150. // Let us exclude ASCII lines, probably they are English texts.
  151. $sample_text = explode("\n", $sample_text);
  152. foreach ($sample_text as $key => &$line) {
  153. if (api_is_valid_ascii($line)) {
  154. unset($sample_text[$key]);
  155. }
  156. }
  157. $sample_text = join("\n", $sample_text);
  158. $this->course->encoding = api_detect_encoding($sample_text, $course_info['language']);
  159. }
  160. // Encoding conversion of the course, if it is needed.
  161. $this->course->to_system_encoding();
  162. foreach ($this->tools_to_restore as $tool) {
  163. $function_build = 'restore_'.$tool;
  164. $this->$function_build($session_id, $respect_base_content, $destination_course_code);
  165. }
  166. /*
  167. $this->restore_links($session_id);
  168. $this->restore_documents($session_id, $destination_course_code);
  169. $this->restore_quizzes($session_id, $respect_base_content);
  170. $this->restore_glossary($session_id);
  171. $this->restore_learnpaths($session_id, $respect_base_content);
  172. $this->restore_course_descriptions($session_id);
  173. $this->restore_wiki($session_id);
  174. $this->restore_thematic($session_id);
  175. $this->restore_attendance($session_id);
  176. if (!empty($session_id)) {
  177. } else {
  178. //$this->restore_links();
  179. $this->restore_tool_intro();
  180. $this->restore_events();
  181. $this->restore_announcements();
  182. //$this->restore_documents();
  183. $this->restore_scorm_documents();
  184. //$this->restore_course_descriptions();
  185. //$this->restore_quizzes(); // after restore_documents! (for correct import of sound/video)
  186. //$this->restore_learnpaths();
  187. $this->restore_surveys();
  188. $this->restore_student_publication();
  189. //$this->restore_glossary();
  190. //$this->restore_wiki();
  191. //$this->restore_thematic();
  192. //$this->restore_attendance();
  193. } */
  194. if ($update_course_settings) {
  195. $this->restore_course_settings($destination_course_code);
  196. }
  197. // Restore the item properties
  198. $table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  199. $condition_session = "";
  200. if (!empty($session_id)) {
  201. $condition_session = " , id_session='".intval($session_id)."'";
  202. }
  203. foreach ($this->course->resources as $type => $resources) {
  204. if (is_array($resources)) {
  205. foreach ($resources as $id => $resource) {
  206. foreach ($resource->item_properties as $property) {
  207. // First check if there isn't allready a record for this resource
  208. $sql = "SELECT * FROM $table WHERE c_id = ".$this->destination_course_id." AND tool = '".$property['tool']."' AND ref = '".$resource->destination_id."'";
  209. $res = Database::query($sql);
  210. if (Database::num_rows($res) == 0) {
  211. // The to_group_id and to_user_id are set to default values as users/groups possibly not exist in the target course
  212. $sql = "INSERT INTO $table SET
  213. c_id = '".$this->destination_course_id."',
  214. tool = '".self::DBUTF8escapestring($property['tool'])."',
  215. insert_user_id = '".self::DBUTF8escapestring($property['insert_user_id'])."',
  216. insert_date = '".self::DBUTF8escapestring($property['insert_date'])."',
  217. lastedit_date = '".self::DBUTF8escapestring($property['lastedit_date'])."',
  218. ref = '".self::DBUTF8escapestring($resource->destination_id)."',
  219. lastedit_type = '".self::DBUTF8escapestring($property['lastedit_type'])."',
  220. lastedit_user_id = '".self::DBUTF8escapestring($property['lastedit_user_id'])."',
  221. visibility = '".self::DBUTF8escapestring($property['visibility'])."',
  222. start_visible = '".self::DBUTF8escapestring($property['start_visible'])."',
  223. end_visible = '".self::DBUTF8escapestring($property['end_visible'])."',
  224. to_user_id = '".self::DBUTF8escapestring($property['to_user_id'])."',
  225. to_group_id = '0' $condition_session";
  226. ;
  227. Database::query($sql);
  228. }
  229. }
  230. }
  231. }
  232. }
  233. /*
  234. // Restore the linked-resources
  235. $table = Database :: get_course_table(TABLE_LINKED_RESOURCES);
  236. foreach ($this->course->resources as $type => $resources) {
  237. if (is_array($resources))
  238. foreach ($resources as $id => $resource) {
  239. $linked_resources = $resource->get_linked_resources();
  240. foreach ($linked_resources as $to_type => $to_ids) {
  241. foreach ($to_ids as $index => $to_id) {
  242. $to_resource = $this->course->resources[$to_type][$to_id];
  243. $sql = "INSERT INTO ".$table." SET
  244. source_type = '".$type."',
  245. source_id = '".$resource->destination_id."',
  246. resource_type='".$to_type."',
  247. resource_id='".$to_resource->destination_id."' ";
  248. Database::query($sql);
  249. }
  250. }
  251. }
  252. } */
  253. }
  254. /**
  255. * Restore only harmless course settings: course_language, visibility, department_name,department_url, subscribe, unsubscribe ,category_code
  256. *
  257. * @return unknown_type
  258. */
  259. function restore_course_settings($destination_course_code)
  260. {
  261. $origin_course_info = api_get_course_info($destination_course_code);
  262. $course_info = $this->course->info;
  263. $params['course_language'] = $course_info['language'];
  264. $params['visibility'] = $course_info['visibility'];
  265. $params['department_name'] = $course_info['department_name'];
  266. $params['department_url'] = $course_info['department_url'];
  267. $params['category_code'] = $course_info['categoryCode'];
  268. $params['subscribe'] = $course_info['subscribe_allowed'];
  269. $params['unsubscribe'] = $course_info['unsubscribe'];
  270. CourseManager::update_attributes($origin_course_info['real_id'], $params);
  271. }
  272. /**
  273. * Restore documents
  274. * @param int session id
  275. *
  276. */
  277. function restore_documents($session_id = 0, $respect_base_content = false, $destination_course_code = '')
  278. {
  279. $perm = api_get_permissions_for_new_directories();
  280. $course_info = api_get_course_info($destination_course_code);
  281. if ($this->course->has_resources(RESOURCE_DOCUMENT)) {
  282. $table = Database :: get_course_table(TABLE_DOCUMENT);
  283. $resources = $this->course->resources;
  284. foreach ($resources[RESOURCE_DOCUMENT] as $id => $document) {
  285. $path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/';
  286. //$dirs = explode('/', dirname($document->path));
  287. if (empty($document->item_properties[0]['id_session'])) {
  288. $my_session_id = 0;
  289. } else {
  290. $my_session_id = $session_id;
  291. }
  292. if ($document->file_type == FOLDER) {
  293. $visibility = $document->item_properties[0]['visibility'];
  294. if (!empty($document->title)) {
  295. $title = $document->title;
  296. } else {
  297. $title = basename($document->path);
  298. }
  299. $new = substr($document->path, 8);
  300. if (!is_dir($path.'document'.$new)) {
  301. $created_dir = FileManager::create_unexisting_directory(
  302. $course_info,
  303. api_get_user_id(),
  304. $my_session_id,
  305. 0,
  306. 0,
  307. $path.'document',
  308. $new,
  309. $title,
  310. $visibility
  311. );
  312. }
  313. } elseif ($document->file_type == DOCUMENT) {
  314. //Checking if folder exists in the database otherwise we created it
  315. $dir_to_create = dirname($document->path);
  316. if (!empty($dir_to_create) && $dir_to_create != 'document' && $dir_to_create != '/') {
  317. if (is_dir($path.dirname($document->path))) {
  318. $sql = "SELECT id FROM ".$table." WHERE c_id = ".$this->destination_course_id." AND path = '/".self::DBUTF8escapestring(
  319. substr(dirname($document->path), 9)
  320. )."'";
  321. $res = Database::query($sql);
  322. if (Database::num_rows($res) == 0) {
  323. //continue;
  324. $visibility = $document->item_properties[0]['visibility'];
  325. $new = '/'.substr(dirname($document->path), 9);
  326. $title = str_replace('/', '', $new);
  327. // This code fixes the possibility for a file without a directory entry to be
  328. $document_id = FileManager::add_document(
  329. $course_info,
  330. $new,
  331. 'folder',
  332. 0,
  333. $title,
  334. null,
  335. null,
  336. false
  337. );
  338. api_item_property_update(
  339. $course_info,
  340. TOOL_DOCUMENT,
  341. $document_id,
  342. 'FolderCreated',
  343. $document->item_properties[0]['insert_user_id'],
  344. $document->item_properties[0]['to_group_id'],
  345. $document->item_properties[0]['to_user_id'],
  346. null,
  347. null,
  348. $my_session_id
  349. );
  350. }
  351. } else {
  352. //$created_dir = FileManager::create_unexisting_directory($course_info, api_get_user_id(), $my_session_id, 0, 0 , $path.'document', $new, $title, $visibility, true);
  353. }
  354. }
  355. if (file_exists($path.$document->path)) {
  356. switch ($this->file_option) {
  357. case FILE_OVERWRITE :
  358. $origin_path = $this->course->backup_path.'/'.$document->path;
  359. if (file_exists($origin_path)) {
  360. copy($origin_path, $path.$document->path);
  361. $sql = "SELECT id FROM ".$table." WHERE c_id = ".$this->destination_course_id." AND path = '/".self::DBUTF8escapestring(
  362. substr($document->path, 9)
  363. )."'";
  364. $res = Database::query($sql);
  365. $count = Database::num_rows($res);
  366. if ($count == 0) {
  367. $sql = "INSERT INTO $table SET
  368. path = '/".self::DBUTF8escapestring(
  369. substr($document->path, 9)
  370. )."',
  371. c_id = ".$this->destination_course_id.",
  372. comment = '".self::DBUTF8escapestring($document->comment)."',
  373. title = '".self::DBUTF8escapestring($document->title)."' ,
  374. filetype ='".$document->file_type."',
  375. size = '".$document->size."',
  376. session_id = '$my_session_id'";
  377. Database::query($sql);
  378. $document_id = Database::insert_id();
  379. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id;
  380. api_item_property_update(
  381. $course_info,
  382. TOOL_DOCUMENT,
  383. $document_id,
  384. 'DocumentAdded',
  385. $document->item_properties[0]['insert_user_id'],
  386. $document->item_properties[0]['to_group_id'],
  387. $document->item_properties[0]['to_user_id'],
  388. null,
  389. null,
  390. $my_session_id
  391. );
  392. } else {
  393. $obj = Database::fetch_object($res);
  394. $sql = "UPDATE ".$table." SET
  395. path = '/".self::DBUTF8escapestring(substr($document->path, 9))."',
  396. c_id = ".$this->destination_course_id.",
  397. comment = '".self::DBUTF8escapestring($document->comment)."',
  398. title = '".self::DBUTF8escapestring($document->title)."' ,
  399. filetype ='".$document->file_type."',
  400. size = '".$document->size."',
  401. session_id = '$my_session_id'
  402. WHERE c_id = ".$this->destination_course_id." AND path = '/".self::DBUTF8escapestring(
  403. substr($document->path, 9)
  404. )."'";
  405. Database::query($sql);
  406. api_item_property_update(
  407. $course_info,
  408. TOOL_DOCUMENT,
  409. $obj->id,
  410. 'default',
  411. $document->item_properties[0]['insert_user_id'],
  412. $document->item_properties[0]['to_group_id'],
  413. $document->item_properties[0]['to_user_id'],
  414. null,
  415. null,
  416. $my_session_id
  417. );
  418. }
  419. }
  420. //Replace old course code with the new destination code
  421. $file_info = pathinfo($path.$document->path);
  422. if (in_array($file_info['extension'], array('html', 'htm'))) {
  423. $content = file_get_contents($path.$document->path);
  424. if (UTF8_CONVERT) {
  425. $content = utf8_encode($content);
  426. }
  427. $content = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  428. $content,
  429. $this->course->code,
  430. $this->course->destination_path,
  431. $this->course->backup_path,
  432. $this->course->info['path']
  433. );
  434. $result = file_put_contents($path.$document->path, $content);
  435. }
  436. $sql = "SELECT id FROM ".$table." WHERE c_id = ".$this->destination_course_id." AND path='/".substr(
  437. $document->path,
  438. 9
  439. )."'";
  440. $res = Database::query($sql);
  441. $obj = Database::fetch_object($res);
  442. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id;
  443. $sql = "UPDATE ".$table." SET comment = '".self::DBUTF8escapestring(
  444. $document->comment
  445. )."', title='".self::DBUTF8escapestring($document->title)."', size='".$document->size."'
  446. WHERE c_id = ".$this->destination_course_id." AND id = '".$obj->id."'";
  447. Database::query($sql);
  448. break;
  449. case FILE_SKIP :
  450. $sql = "SELECT id FROM ".$table." WHERE c_id = ".$this->destination_course_id." AND path='/".self::DBUTF8escapestring(
  451. substr($document->path, 9)
  452. )."'";
  453. $res = Database::query($sql);
  454. $obj = Database::fetch_object($res);
  455. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id;
  456. break;
  457. case FILE_RENAME :
  458. $i = 1;
  459. $ext = explode('.', basename($document->path));
  460. if (count($ext) > 1) {
  461. $ext = array_pop($ext);
  462. $file_name_no_ext = substr($document->path, 0, -(strlen($ext) + 1));
  463. $ext = '.'.$ext;
  464. } else {
  465. $ext = '';
  466. $file_name_no_ext = $document->path;
  467. }
  468. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  469. $file_exists = file_exists($path.$new_file_name);
  470. while ($file_exists) {
  471. $i++;
  472. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  473. $file_exists = file_exists($path.$new_file_name);
  474. }
  475. if (!empty($session_id)) {
  476. $document_path = explode('/', $document->path, 3);
  477. $course_path = $path; // "/var/www/wiener/courses/"
  478. $orig_base_folder = $document_path[1];
  479. $orig_base_path = $course_path.$document_path[0].'/'.$document_path[1];
  480. if (is_dir($orig_base_path)) {
  481. $new_base_foldername = $orig_base_folder; // e.g: "carpeta1"
  482. $new_base_path = $orig_base_path; // e.g: "/var/www/wiener/courses/CURSO4/document/carpeta1"
  483. if ($_SESSION['orig_base_foldername'] != $new_base_foldername) {
  484. unset($_SESSION['new_base_foldername']);
  485. unset($_SESSION['orig_base_foldername']);
  486. unset($_SESSION['new_base_path']);
  487. }
  488. $folder_exists = file_exists($new_base_path);
  489. if ($folder_exists) {
  490. $_SESSION['orig_base_foldername'] = $new_base_foldername; // e.g: carpeta1 in session
  491. $x = '';
  492. while ($folder_exists) {
  493. $x = $x + 1;
  494. $new_base_foldername = $document_path[1].'_'.$x;
  495. $new_base_path = $orig_base_path.'_'.$x;
  496. if ($_SESSION['new_base_foldername'] == $new_base_foldername) {
  497. break;
  498. }
  499. $folder_exists = file_exists($new_base_path);
  500. }
  501. $_SESSION['new_base_foldername'] = $new_base_foldername;
  502. $_SESSION['new_base_path'] = $new_base_path;
  503. }
  504. if (isset($_SESSION['new_base_foldername']) && isset($_SESSION['new_base_path'])) {
  505. $new_base_foldername = $_SESSION['new_base_foldername'];
  506. $new_base_path = $_SESSION['new_base_path'];
  507. }
  508. $dest_document_path = $new_base_path.'/'.$document_path[2]; // e.g: "/var/www/wiener/courses/CURSO4/document/carpeta1_1/subcarpeta1/collaborative.png"
  509. $basedir_dest_path = dirname(
  510. $dest_document_path
  511. ); // e.g: "/var/www/wiener/courses/CURSO4/document/carpeta1_1/subcarpeta1"
  512. //$dest_filename = basename($dest_document_path); // e.g: "collaborative.png"
  513. $base_path_document = $course_path.$document_path[0]; // e.g: "/var/www/wiener/courses/CURSO4/document"
  514. $path_title = '/'.$new_base_foldername.'/'.$document_path[2];
  515. api_copy_folder_course_session(
  516. $basedir_dest_path,
  517. $base_path_document,
  518. $session_id,
  519. $course_info,
  520. $document,
  521. $this->course_origin_id
  522. );
  523. if (file_exists($course_path.$document->path)) {
  524. copy($course_path.$document->path, $dest_document_path);
  525. }
  526. //Replace old course code with the new destination code see BT#1985
  527. if (file_exists($dest_document_path)) {
  528. $file_info = pathinfo($dest_document_path);
  529. if (in_array($file_info['extension'], array('html', 'htm'))) {
  530. $content = file_get_contents($dest_document_path);
  531. if (UTF8_CONVERT) {
  532. $content = utf8_encode($content);
  533. }
  534. $content = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  535. $content,
  536. $this->course->code,
  537. $this->course->destination_path,
  538. $this->course->backup_path,
  539. $this->course->info['path']
  540. );
  541. $result = file_put_contents($dest_document_path, $content);
  542. }
  543. }
  544. $sql = "INSERT INTO $table SET
  545. path = '$path_title',
  546. c_id = ".$this->destination_course_id.",
  547. comment = '".self::DBUTF8escapestring($document->comment)."',
  548. title = '".self::DBUTF8escapestring(basename($path_title))."' ,
  549. filetype ='".$document->file_type."',
  550. size = '".$document->size."',
  551. session_id = '$my_session_id'";
  552. Database::query($sql);
  553. $document_id = Database::insert_id();
  554. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id;
  555. api_item_property_update(
  556. $course_info,
  557. TOOL_DOCUMENT,
  558. $document_id,
  559. 'DocumentAdded',
  560. $document->item_properties[0]['insert_user_id'],
  561. $document->item_properties[0]['to_group_id'],
  562. $document->item_properties[0]['to_user_id'],
  563. null,
  564. null,
  565. $my_session_id
  566. );
  567. } else {
  568. if (file_exists($path.$document->path)) {
  569. copy($path.$document->path, $path.$new_file_name);
  570. }
  571. //Replace old course code with the new destination code see BT#1985
  572. if (file_exists($path.$new_file_name)) {
  573. $file_info = pathinfo($path.$new_file_name);
  574. if (in_array($file_info['extension'], array('html', 'htm'))) {
  575. $content = file_get_contents($path.$new_file_name);
  576. if (UTF8_CONVERT) {
  577. $content = utf8_encode($content);
  578. }
  579. $content = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  580. $content,
  581. $this->course->code,
  582. $this->course->destination_path,
  583. $this->course->backup_path,
  584. $this->course->info['path']
  585. );
  586. $result = file_put_contents($path.$new_file_name, $content);
  587. }
  588. }
  589. $sql = "INSERT INTO ".$table." SET
  590. c_id = ".$this->destination_course_id.",
  591. path = '/".self::DBUTF8escapestring(
  592. substr($new_file_name, 9)
  593. )."',
  594. comment = '".self::DBUTF8escapestring($document->comment)."',
  595. title = '".self::DBUTF8escapestring($document->title)."' ,
  596. filetype ='".$document->file_type."',
  597. size = '".$document->size."',
  598. session_id = '$my_session_id'";
  599. Database::query($sql);
  600. $document_id = Database::insert_id();
  601. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id;
  602. api_item_property_update(
  603. $course_info,
  604. TOOL_DOCUMENT,
  605. $document_id,
  606. 'DocumentAdded',
  607. $document->item_properties[0]['insert_user_id'],
  608. $document->item_properties[0]['to_group_id'],
  609. $document->item_properties[0]['to_user_id'],
  610. null,
  611. null,
  612. $my_session_id
  613. );
  614. }
  615. } else {
  616. copy($this->course->backup_path.'/'.$document->path, $path.$new_file_name);
  617. //Replace old course code with the new destination code see BT#1985
  618. if (file_exists($path.$new_file_name)) {
  619. $file_info = pathinfo($path.$new_file_name);
  620. if (in_array($file_info['extension'], array('html', 'htm'))) {
  621. $content = file_get_contents($path.$new_file_name);
  622. if (UTF8_CONVERT) {
  623. $content = utf8_encode($content);
  624. }
  625. $content = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  626. $content,
  627. $this->course->code,
  628. $this->course->destination_path,
  629. $this->course->backup_path,
  630. $this->course->info['path']
  631. );
  632. $result = file_put_contents($path.$new_file_name, $content);
  633. }
  634. }
  635. $sql = "INSERT INTO ".$table." SET
  636. c_id = ".$this->destination_course_id.",
  637. path = '/".self::DBUTF8escapestring(substr($new_file_name, 9))."',
  638. comment = '".self::DBUTF8escapestring($document->comment)."',
  639. title = '".self::DBUTF8escapestring($document->title)."' ,
  640. filetype ='".$document->file_type."',
  641. size = '".$document->size."',
  642. session_id = '$my_session_id'";
  643. Database::query($sql);
  644. $document_id = Database::insert_id();
  645. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id;
  646. api_item_property_update(
  647. $course_info,
  648. TOOL_DOCUMENT,
  649. $document_id,
  650. 'DocumentAdded',
  651. $document->item_properties[0]['insert_user_id'],
  652. $document->item_properties[0]['to_group_id'],
  653. $document->item_properties[0]['to_user_id'],
  654. null,
  655. null,
  656. $my_session_id
  657. );
  658. }
  659. break;
  660. } // end switch
  661. } else { // end if file exists
  662. //make sure the source file actually exists
  663. if (is_file($this->course->backup_path.'/'.$document->path) && is_readable(
  664. $this->course->backup_path.'/'.$document->path
  665. ) && is_dir(dirname($path.$document->path)) && is_writeable(dirname($path.$document->path))
  666. ) {
  667. //echo 'Copying';
  668. copy($this->course->backup_path.'/'.$document->path, $path.$document->path);
  669. //Replace old course code with the new destination code see BT#1985
  670. if (file_exists($path.$document->path)) {
  671. $file_info = pathinfo($path.$document->path);
  672. if (in_array($file_info['extension'], array('html', 'htm'))) {
  673. $content = file_get_contents($path.$document->path);
  674. if (UTF8_CONVERT) {
  675. $content = utf8_encode($content);
  676. }
  677. $content = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  678. $content,
  679. $this->course->code,
  680. $this->course->destination_path,
  681. $this->course->backup_path,
  682. $this->course->info['path']
  683. );
  684. $result = file_put_contents($path.$document->path, $content);
  685. }
  686. }
  687. $sql = "INSERT INTO ".$table." SET
  688. c_id = ".$this->destination_course_id.",
  689. path = '/".substr($document->path, 9)."',
  690. comment = '".self::DBUTF8escapestring($document->comment)."',
  691. title = '".self::DBUTF8escapestring($document->title)."' ,
  692. filetype='".$document->file_type."',
  693. size= '".$document->size."',
  694. session_id = '$my_session_id'";
  695. Database::query($sql);
  696. $document_id = Database::insert_id();
  697. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $document_id;
  698. api_item_property_update(
  699. $course_info,
  700. TOOL_DOCUMENT,
  701. $document_id,
  702. 'DocumentAdded',
  703. $document->item_properties[0]['insert_user_id'],
  704. $document->item_properties[0]['to_group_id'],
  705. $document->item_properties[0]['to_user_id'],
  706. null,
  707. null,
  708. $my_session_id
  709. );
  710. } else {
  711. //echo 'not Copying';
  712. if (is_file($this->course->backup_path.'/'.$document->path) && is_readable(
  713. $this->course->backup_path.'/'.$document->path
  714. )
  715. ) {
  716. error_log(
  717. 'Course copy generated an ignoreable error while trying to copy '.$this->course->backup_path.'/'.$document->path.': file not found'
  718. );
  719. }
  720. if (!is_dir(dirname($path.$document->path))) {
  721. error_log(
  722. 'Course copy generated an ignoreable error while trying to copy to '.dirname(
  723. $path.$document->path
  724. ).': directory not found'
  725. );
  726. }
  727. if (!is_writeable(dirname($path.$document->path))) {
  728. error_log(
  729. 'Course copy generated an ignoreable error while trying to copy to '.dirname(
  730. $path.$document->path
  731. ).': directory not writeable'
  732. );
  733. }
  734. }
  735. } // end file doesn't exist
  736. } else {
  737. /* $sql = "SELECT id FROM ".$table." WHERE path = '/".self::DBUTF8escapestring(substr($document->path, 9))."'";
  738. $res = Database::query($sql);
  739. if( Database::num_rows($res)> 0)
  740. {
  741. $obj = Database::fetch_object($res);
  742. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = $obj->id;
  743. }
  744. else
  745. {
  746. $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."'";
  747. Database::query($sql);
  748. $this->course->resources[RESOURCE_DOCUMENT][$id]->destination_id = Database::insert_id();
  749. } */
  750. } // end folder
  751. } // end for each
  752. // Delete sessions for the copy the new folder in session
  753. unset($_SESSION['new_base_foldername']);
  754. unset($_SESSION['orig_base_foldername']);
  755. unset($_SESSION['new_base_path']);
  756. }
  757. }
  758. /**
  759. * Restore scorm documents
  760. * TODO @TODO check that the restore function with renaming doesn't break the scorm structure!
  761. */
  762. function restore_scorm_documents()
  763. {
  764. $perm = api_get_permissions_for_new_directories();
  765. if ($this->course->has_resources(RESOURCE_SCORM)) {
  766. $resources = $this->course->resources;
  767. foreach ($resources[RESOURCE_SCORM] as $id => $document) {
  768. $path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/';
  769. @mkdir(dirname($path.$document->path), $perm, true);
  770. if (file_exists($path.$document->path)) {
  771. switch ($this->file_option) {
  772. case FILE_OVERWRITE :
  773. api_rmdirr($path.$document->path);
  774. FileManager::copyDirTo(
  775. $this->course->backup_path.'/'.$document->path,
  776. $path.dirname($document->path),
  777. false
  778. );
  779. break;
  780. case FILE_SKIP :
  781. break;
  782. case FILE_RENAME :
  783. $i = 1;
  784. $ext = explode('.', basename($document->path));
  785. if (count($ext) > 1) {
  786. $ext = array_pop($ext);
  787. $file_name_no_ext = substr($document->path, 0, -(strlen($ext) + 1));
  788. $ext = '.'.$ext;
  789. } else {
  790. $ext = '';
  791. $file_name_no_ext = $document->path;
  792. }
  793. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  794. $file_exists = file_exists($path.$new_file_name);
  795. while ($file_exists) {
  796. $i++;
  797. $new_file_name = $file_name_no_ext.'_'.$i.$ext;
  798. $file_exists = file_exists($path.$new_file_name);
  799. }
  800. rename(
  801. $this->course->backup_path.'/'.$document->path,
  802. $this->course->backup_path.'/'.$new_file_name
  803. );
  804. FileManager::copyDirTo(
  805. $this->course->backup_path.'/'.$new_file_name,
  806. $path.dirname($new_file_name),
  807. false
  808. );
  809. rename(
  810. $this->course->backup_path.'/'.$new_file_name,
  811. $this->course->backup_path.'/'.$document->path
  812. );
  813. break;
  814. } // end switch
  815. } // end if file exists
  816. else {
  817. FileManager::copyDirTo(
  818. $this->course->backup_path.'/'.$document->path,
  819. $path.dirname($document->path),
  820. false
  821. );
  822. }
  823. } // end for each
  824. }
  825. }
  826. /**
  827. * Restore forums
  828. */
  829. function restore_forums()
  830. {
  831. if ($this->course->has_resources(RESOURCE_FORUM)) {
  832. $table_forum = Database::get_course_table(TABLE_FORUM);
  833. $resources = $this->course->resources;
  834. foreach ($resources[RESOURCE_FORUM] as $id => $forum) {
  835. $params = (array)$forum->obj;
  836. if ($this->course->resources[RESOURCE_FORUMCATEGORY][$params['forum_category']]->destination_id == -1) {
  837. $cat_id = $this->restore_forum_category($params['forum_category']);
  838. } else {
  839. $cat_id = $this->course->resources[RESOURCE_FORUMCATEGORY][$params['forum_category']]->destination_id;
  840. }
  841. $params = self::DBUTF8_array($params);
  842. $params['c_id'] = $this->destination_course_id;
  843. $params['forum_category'] = $cat_id;
  844. unset($params['forum_id']);
  845. $params['forum_comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  846. $params['forum_comment'],
  847. $this->course->code,
  848. $this->course->destination_path,
  849. $this->course->backup_path,
  850. $this->course->info['path']
  851. );
  852. if (!empty($params['forum_image'])) {
  853. $original_forum_image = $this->course->path.'upload/forum/images/'.$params['forum_image'];
  854. if (file_exists($original_forum_image)) {
  855. $new_forum_image = api_get_path(
  856. SYS_COURSE_PATH
  857. ).$this->destination_course_info['path'].'/upload/forum/images/'.$params['forum_image'];
  858. @copy($original_forum_image, $new_forum_image);
  859. }
  860. }
  861. $new_id = Database::insert($table_forum, $params);
  862. $this->course->resources[RESOURCE_FORUM][$id]->destination_id = $new_id;
  863. $forum_topics = 0;
  864. if (is_array($this->course->resources[RESOURCE_FORUMTOPIC])) {
  865. foreach ($this->course->resources[RESOURCE_FORUMTOPIC] as $topic_id => $topic) {
  866. if ($topic->obj->forum_id == $id) {
  867. $this->restore_topic($topic_id, $new_id);
  868. $forum_topics++;
  869. }
  870. }
  871. }
  872. if ($forum_topics > 0) {
  873. $sql = "UPDATE ".$table_forum." SET forum_threads = ".$forum_topics."
  874. WHERE c_id = {$this->destination_course_id} AND forum_id = ".(int)$new_id;
  875. Database::query($sql);
  876. }
  877. }
  878. }
  879. }
  880. /**
  881. * Restore forum-categories
  882. */
  883. function restore_forum_category($my_id = null)
  884. {
  885. $forum_cat_table = Database :: get_course_table(TABLE_FORUM_CATEGORY);
  886. $resources = $this->course->resources;
  887. if (!empty($resources[RESOURCE_FORUMCATEGORY])) {
  888. foreach ($resources[RESOURCE_FORUMCATEGORY] as $id => $forum_cat) {
  889. if (!empty($my_id)) {
  890. if ($my_id != $id) {
  891. continue;
  892. }
  893. }
  894. if ($forum_cat && !$forum_cat->is_restored()) {
  895. $title = $forum_cat->obj->cat_title;
  896. if (!empty($title)) {
  897. if (!preg_match('/.*\((.+)\)$/', $title, $matches)) {
  898. // This is for avoiding repetitive adding of training code after several backup/restore cycles.
  899. if ($matches[1] != $this->course->code) {
  900. $title = $title.' ('.$this->course->code.')';
  901. }
  902. }
  903. }
  904. $params = (array)$forum_cat->obj;
  905. $params['c_id'] = $this->destination_course_id;
  906. $params['cat_comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  907. $params['cat_comment'],
  908. $this->course->code,
  909. $this->course->destination_path,
  910. $this->course->backup_path,
  911. $this->course->info['path']
  912. );
  913. unset($params['cat_id']);
  914. $params = self::DBUTF8_array($params);
  915. $new_id = Database::insert($forum_cat_table, $params);
  916. $this->course->resources[RESOURCE_FORUMCATEGORY][$id]->destination_id = $new_id;
  917. if (!empty($my_id)) {
  918. return $new_id;
  919. }
  920. }
  921. }
  922. }
  923. }
  924. /**
  925. * Restore a forum-topic
  926. */
  927. function restore_topic($thread_id, $forum_id)
  928. {
  929. $table = Database :: get_course_table(TABLE_FORUM_THREAD);
  930. $topic = $this->course->resources[RESOURCE_FORUMTOPIC][$thread_id];
  931. $params = (array)$topic->obj;
  932. $params = self::DBUTF8_array($params);
  933. $params['c_id'] = $this->destination_course_id;
  934. $params['forum_id'] = $forum_id;
  935. $params['thread_poster_id'] = $this->first_teacher_id;
  936. $params['thread_date'] = api_get_utc_datetime();
  937. $params['thread_close_date'] = '0000-00-00 00:00:00';
  938. $params['thread_last_post'] = 0;
  939. $params['thread_replies'] = 0;
  940. $params['thread_views'] = 0;
  941. unset($params['thread_id']);
  942. $new_id = Database::insert($table, $params);
  943. api_item_property_update(
  944. $this->destination_course_info,
  945. TOOL_FORUM_THREAD,
  946. $new_id,
  947. 'ThreadAdded',
  948. api_get_user_id(),
  949. 0,
  950. 0,
  951. null,
  952. null
  953. );
  954. $this->course->resources[RESOURCE_FORUMTOPIC][$thread_id]->destination_id = $new_id;
  955. $topic_replies = -1;
  956. foreach ($this->course->resources[RESOURCE_FORUMPOST] as $post_id => $post) {
  957. if ($post->obj->thread_id == $thread_id) {
  958. $topic_replies++;
  959. $this->restore_post($post_id, $new_id, $forum_id);
  960. }
  961. }
  962. return $new_id;
  963. }
  964. /**
  965. * Restore a forum-post
  966. * @TODO Restore tree-structure of posts. For example: attachments to posts.
  967. */
  968. function restore_post($id, $topic_id, $forum_id)
  969. {
  970. $table_post = Database :: get_course_table(TABLE_FORUM_POST);
  971. $post = $this->course->resources[RESOURCE_FORUMPOST][$id];
  972. $params = (array)$post->obj;
  973. $params['c_id'] = $this->destination_course_id;
  974. $params['forum_id'] = $forum_id;
  975. $params['thread_id'] = $topic_id;
  976. $params['poster_id'] = $this->first_teacher_id;
  977. $params['post_date'] = api_get_utc_datetime();
  978. unset($params['post_id']);
  979. $params['post_text'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  980. $params['post_text'],
  981. $this->course->code,
  982. $this->course->destination_path,
  983. $this->course->backup_path,
  984. $this->course->info['path']
  985. );
  986. $new_id = Database::insert($table_post, $params);
  987. api_item_property_update(
  988. $this->destination_course_info,
  989. TOOL_FORUM_POST,
  990. $new_id,
  991. 'PostAdded',
  992. api_get_user_id(),
  993. 0,
  994. 0,
  995. null,
  996. null
  997. );
  998. $this->course->resources[RESOURCE_FORUMPOST][$id]->destination_id = $new_id;
  999. return $new_id;
  1000. }
  1001. /**
  1002. * Restore links
  1003. */
  1004. function restore_links($session_id = 0)
  1005. {
  1006. if ($this->course->has_resources(RESOURCE_LINK)) {
  1007. $link_table = Database :: get_course_table(TABLE_LINK);
  1008. $resources = $this->course->resources;
  1009. foreach ($resources[RESOURCE_LINK] as $id => $link) {
  1010. $cat_id = $this->restore_link_category($link->category_id, $session_id);
  1011. $sql = "SELECT MAX(display_order) FROM $link_table WHERE c_id = ".$this->destination_course_id." AND category_id='".self::DBUTF8escapestring(
  1012. $cat_id
  1013. )."'";
  1014. $result = Database::query($sql);
  1015. list($max_order) = Database::fetch_array($result);
  1016. $condition_session = "";
  1017. if (!empty($session_id)) {
  1018. $condition_session = " , session_id = '$session_id' ";
  1019. }
  1020. $sql = "INSERT INTO ".$link_table." SET
  1021. c_id = ".$this->destination_course_id." ,
  1022. url = '".self::DBUTF8escapestring($link->url)."',
  1023. title = '".self::DBUTF8escapestring($link->title)."',
  1024. description = '".self::DBUTF8escapestring($link->description)."',
  1025. category_id = '".$cat_id."',
  1026. on_homepage = '".$link->on_homepage."',
  1027. display_order = '".($max_order + 1)."' $condition_session";
  1028. Database::query($sql);
  1029. $this->course->resources[RESOURCE_LINK][$id]->destination_id = Database::insert_id();
  1030. }
  1031. }
  1032. }
  1033. /**
  1034. * Restore tool intro
  1035. */
  1036. function restore_tool_intro()
  1037. {
  1038. if ($this->course->has_resources(RESOURCE_TOOL_INTRO)) {
  1039. $tool_intro_table = Database :: get_course_table(TABLE_TOOL_INTRO);
  1040. $resources = $this->course->resources;
  1041. foreach ($resources[RESOURCE_TOOL_INTRO] as $id => $tool_intro) {
  1042. $sql = "DELETE FROM ".$tool_intro_table." WHERE c_id = ".$this->destination_course_id." AND id='".self::DBUTF8escapestring(
  1043. $tool_intro->id
  1044. )."'";
  1045. Database::query($sql);
  1046. $tool_intro->intro_text = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  1047. $tool_intro->intro_text,
  1048. $this->course->code,
  1049. $this->course->destination_path,
  1050. $this->course->backup_path,
  1051. $this->course->info['path']
  1052. );
  1053. $sql = "INSERT INTO ".$tool_intro_table." SET c_id = ".$this->destination_course_id." , id='".self::DBUTF8escapestring(
  1054. $tool_intro->id
  1055. )."', intro_text = '".self::DBUTF8escapestring($tool_intro->intro_text)."'";
  1056. Database::query($sql);
  1057. $this->course->resources[RESOURCE_TOOL_INTRO][$id]->destination_id = Database::insert_id();
  1058. }
  1059. }
  1060. }
  1061. /**
  1062. * Restore a link-category
  1063. */
  1064. function restore_link_category($id, $session_id = 0)
  1065. {
  1066. $condition_session = "";
  1067. if (!empty($session_id)) {
  1068. $condition_session = " , session_id = '$session_id' ";
  1069. }
  1070. if ($id == 0) {
  1071. return 0;
  1072. }
  1073. $link_cat_table = Database :: get_course_table(TABLE_LINK_CATEGORY);
  1074. $resources = $this->course->resources;
  1075. $link_cat = $resources[RESOURCE_LINKCATEGORY][$id];
  1076. if (is_object($link_cat) && !$link_cat->is_restored()) {
  1077. $sql = "SELECT MAX(display_order) FROM $link_cat_table WHERE c_id = ".$this->destination_course_id." ";
  1078. $result = Database::query($sql);
  1079. list($orderMax) = Database::fetch_array($result, 'NUM');
  1080. $display_order = $orderMax + 1;
  1081. $sql = "INSERT INTO ".$link_cat_table." SET c_id = ".$this->destination_course_id." , category_title = '".self::DBUTF8escapestring(
  1082. $link_cat->title
  1083. )."', description='".self::DBUTF8escapestring(
  1084. $link_cat->description
  1085. )."', display_order='".$display_order."' $condition_session ";
  1086. Database::query($sql);
  1087. $new_id = Database::insert_id();
  1088. $this->course->resources[RESOURCE_LINKCATEGORY][$id]->destination_id = $new_id;
  1089. return $new_id;
  1090. }
  1091. return $this->course->resources[RESOURCE_LINKCATEGORY][$id]->destination_id;
  1092. }
  1093. /**
  1094. * Restore events
  1095. */
  1096. function restore_events()
  1097. {
  1098. if ($this->course->has_resources(RESOURCE_EVENT)) {
  1099. $table = Database :: get_course_table(TABLE_AGENDA);
  1100. $resources = $this->course->resources;
  1101. foreach ($resources[RESOURCE_EVENT] as $id => $event) {
  1102. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1103. $event->content = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  1104. $event->content,
  1105. $this->course->code,
  1106. $this->course->destination_path,
  1107. $this->course->backup_path,
  1108. $this->course->info['path']
  1109. );
  1110. $sql = "INSERT INTO ".$table." SET
  1111. c_id = ".$this->destination_course_id." ,
  1112. title = '".self::DBUTF8escapestring($event->title)."',
  1113. content = '".self::DBUTF8escapestring($event->content)."',
  1114. all_day = '".$event->all_day."',
  1115. start_date = '".$event->start_date."',
  1116. end_date = '".$event->end_date."'";
  1117. Database::query($sql);
  1118. $new_event_id = Database::insert_id();
  1119. $this->course->resources[RESOURCE_EVENT][$id]->destination_id = $new_event_id;
  1120. //Copy event attachment
  1121. $origin_path = $this->course->backup_path.'/upload/calendar/';
  1122. $destination_path = api_get_path(SYS_COURSE_PATH).$this->course->destination_path.'/upload/calendar/';
  1123. if (!empty($this->course->orig)) {
  1124. $table_attachment = Database :: get_course_table(TABLE_AGENDA_ATTACHMENT);
  1125. $sql = 'SELECT path, comment, size, filename FROM '.$table_attachment.' WHERE c_id = '.$this->destination_course_id.' AND agenda_id = '.$id;
  1126. $attachment_event = Database::query($sql);
  1127. $attachment_event = Database::fetch_object($attachment_event);
  1128. if (file_exists($origin_path.$attachment_event->path) && !is_dir(
  1129. $origin_path.$attachment_event->path
  1130. )
  1131. ) {
  1132. $new_filename = uniqid(
  1133. ''
  1134. ); //ass seen in the add_agenda_attachment_file() function in agenda.inc.php
  1135. $copy_result = copy($origin_path.$attachment_event->path, $destination_path.$new_filename);
  1136. //$copy_result = true;
  1137. if ($copy_result) {
  1138. $table_attachment = Database :: get_course_table(TABLE_AGENDA_ATTACHMENT);
  1139. $sql = "INSERT INTO ".$table_attachment." SET c_id = ".$this->destination_course_id." , path = '".self::DBUTF8escapestring(
  1140. $new_filename
  1141. )."', comment = '".self::DBUTF8escapestring(
  1142. $attachment_event->comment
  1143. )."', size = '".$attachment_event->size."', filename = '".$attachment_event->filename."' , agenda_id = '".$new_event_id."' ";
  1144. Database::query($sql);
  1145. }
  1146. }
  1147. } else {
  1148. // get the info of the file
  1149. if (!empty($event->attachment_path) && is_file($origin_path.$event->attachment_path) && is_readable(
  1150. $origin_path.$event->attachment_path
  1151. )
  1152. ) {
  1153. $new_filename = uniqid(
  1154. ''
  1155. ); //ass seen in the add_agenda_attachment_file() function in agenda.inc.php
  1156. $copy_result = copy($origin_path.$event->attachment_path, $destination_path.$new_filename);
  1157. if ($copy_result) {
  1158. $table_attachment = Database :: get_course_table(TABLE_AGENDA_ATTACHMENT);
  1159. $sql = "INSERT INTO ".$table_attachment." SET c_id = ".$this->destination_course_id." , path = '".self::DBUTF8escapestring(
  1160. $new_filename
  1161. )."', comment = '".self::DBUTF8escapestring(
  1162. $event->attachment_comment
  1163. )."', size = '".$event->attachment_size."', filename = '".$event->attachment_filename."' , agenda_id = '".$new_event_id."' ";
  1164. Database::query($sql);
  1165. }
  1166. }
  1167. }
  1168. }
  1169. }
  1170. }
  1171. /**
  1172. * Restore course-description
  1173. */
  1174. function restore_course_descriptions($session_id = 0)
  1175. {
  1176. if ($this->course->has_resources(RESOURCE_COURSEDESCRIPTION)) {
  1177. $table = Database :: get_course_table(TABLE_COURSE_DESCRIPTION);
  1178. $resources = $this->course->resources;
  1179. foreach ($resources[RESOURCE_COURSEDESCRIPTION] as $id => $cd) {
  1180. if (isset($_POST['destination_course'])) {
  1181. $course_destination = Security::remove_XSS($_POST['destination_course']);
  1182. $course_destination = api_get_course_info($course_destination);
  1183. $course_destination = $course_destination['path'];
  1184. } else {
  1185. $course_destination = $this->course->destination_path;
  1186. }
  1187. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1188. $description_content = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  1189. $cd->content,
  1190. $this->course->code,
  1191. $this->course->destination_path,
  1192. $this->course->backup_path,
  1193. $this->course->info['path']
  1194. );
  1195. $condition_session = "";
  1196. if (!empty($session_id)) {
  1197. $session_id = intval($session_id);
  1198. $condition_session = " , session_id = '$session_id' ";
  1199. }
  1200. $sql = "INSERT INTO ".$table." SET c_id = ".$this->destination_course_id." , description_type = '".self::DBUTF8escapestring(
  1201. $cd->description_type
  1202. )."',title = '".self::DBUTF8escapestring($cd->title)."', content = '".self::DBUTF8escapestring(
  1203. $description_content
  1204. )."' $condition_session";
  1205. Database::query($sql);
  1206. $this->course->resources[RESOURCE_COURSEDESCRIPTION][$id]->destination_id = Database::insert_id();
  1207. }
  1208. }
  1209. }
  1210. /**
  1211. * Restore announcements
  1212. */
  1213. function restore_announcements()
  1214. {
  1215. if ($this->course->has_resources(RESOURCE_ANNOUNCEMENT)) {
  1216. $table = Database :: get_course_table(TABLE_ANNOUNCEMENT);
  1217. $resources = $this->course->resources;
  1218. foreach ($resources[RESOURCE_ANNOUNCEMENT] as $id => $announcement) {
  1219. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1220. $announcement->content = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  1221. $announcement->content,
  1222. $this->course->code,
  1223. $this->course->destination_path,
  1224. $this->course->backup_path,
  1225. $this->course->info['path']
  1226. );
  1227. $sql = "INSERT INTO ".$table." "."SET
  1228. c_id = ".$this->destination_course_id." ,
  1229. title = '".self::DBUTF8escapestring($announcement->title)."',".
  1230. "content = '".self::DBUTF8escapestring($announcement->content)."', ".
  1231. "end_date = '".$announcement->date."', ".
  1232. "display_order = '".$announcement->display_order."', ".
  1233. "email_sent = '".$announcement->email_sent."'";
  1234. Database::query($sql);
  1235. $new_announcement_id = Database::insert_id();
  1236. $this->course->resources[RESOURCE_ANNOUNCEMENT][$id]->destination_id = $new_announcement_id;
  1237. $origin_path = $this->course->backup_path.'/upload/announcements/';
  1238. $destination_path = api_get_path(
  1239. SYS_COURSE_PATH
  1240. ).$this->course->destination_path.'/upload/announcements/';
  1241. //Copy announcement attachment file
  1242. if (!empty($this->course->orig)) {
  1243. $table_attachment = Database :: get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1244. $sql = 'SELECT path, comment, size, filename FROM '.$table_attachment.' WHERE c_id = '.$this->destination_course_id.' AND announcement_id = '.$id;
  1245. $attachment_event = Database::query($sql);
  1246. $attachment_event = Database::fetch_object($attachment_event);
  1247. if (file_exists($origin_path.$attachment_event->path) && !is_dir(
  1248. $origin_path.$attachment_event->path
  1249. )
  1250. ) {
  1251. $new_filename = uniqid(
  1252. ''
  1253. ); //ass seen in the add_agenda_attachment_file() function in agenda.inc.php
  1254. $copy_result = copy($origin_path.$attachment_event->path, $destination_path.$new_filename);
  1255. //error_log($destination_path.$new_filename); error_log($copy_result);
  1256. //$copy_result = true;
  1257. if ($copy_result) {
  1258. $table_attachment = Database :: get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1259. $sql = "INSERT INTO ".$table_attachment." SET c_id = ".$this->destination_course_id." , path = '".self::DBUTF8escapestring(
  1260. $new_filename
  1261. )."', comment = '".self::DBUTF8escapestring(
  1262. $attachment_event->comment
  1263. )."', size = '".$attachment_event->size."', filename = '".$attachment_event->filename."' , announcement_id = '".$new_announcement_id."' ";
  1264. Database::query($sql);
  1265. }
  1266. }
  1267. } else {
  1268. // get the info of the file
  1269. if (!empty($announcement->attachment_path) && is_file(
  1270. $origin_path.$announcement->attachment_path
  1271. ) && is_readable($origin_path.$announcement->attachment_path)
  1272. ) {
  1273. $new_filename = uniqid(
  1274. ''
  1275. ); //ass seen in the add_agenda_attachment_file() function in agenda.inc.php
  1276. $copy_result = copy(
  1277. $origin_path.$announcement->attachment_path,
  1278. $destination_path.$new_filename
  1279. );
  1280. if ($copy_result) {
  1281. $table_attachment = Database :: get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1282. $sql = "INSERT INTO ".$table_attachment." SET c_id = ".$this->destination_course_id." , path = '".self::DBUTF8escapestring(
  1283. $new_filename
  1284. )."', comment = '".self::DBUTF8escapestring(
  1285. $announcement->attachment_comment
  1286. )."', size = '".$announcement->attachment_size."', filename = '".$announcement->attachment_filename."' , announcement_id = '".$new_announcement_id."' ";
  1287. Database::query($sql);
  1288. }
  1289. }
  1290. }
  1291. }
  1292. }
  1293. }
  1294. /**
  1295. * Restore Quiz
  1296. */
  1297. function restore_quizzes($session_id = 0, $respect_base_content = false)
  1298. {
  1299. if ($this->course->has_resources(RESOURCE_QUIZ)) {
  1300. $table_qui = Database :: get_course_table(TABLE_QUIZ_TEST);
  1301. $table_rel = Database :: get_course_table(TABLE_QUIZ_TEST_QUESTION);
  1302. $table_doc = Database :: get_course_table(TABLE_DOCUMENT);
  1303. $resources = $this->course->resources;
  1304. foreach ($resources[RESOURCE_QUIZ] as $id => $quiz) {
  1305. if (isset($quiz->obj)) {
  1306. //For new imports
  1307. $quiz = $quiz->obj;
  1308. } else {
  1309. //For backward compatibility
  1310. $quiz->obj = $quiz;
  1311. }
  1312. $doc = '';
  1313. if (!empty($quiz->sound)) {
  1314. if (isset($this->course->resources[RESOURCE_DOCUMENT][$quiz->sound]) &&
  1315. $this->course->resources[RESOURCE_DOCUMENT][$quiz->sound]->is_restored()) {
  1316. $sql = "SELECT path FROM ".$table_doc." WHERE c_id = ".$this->destination_course_id." AND id = ".$resources[RESOURCE_DOCUMENT][$quiz->sound]->destination_id;
  1317. $doc = Database::query($sql);
  1318. $doc = Database::fetch_object($doc);
  1319. $doc = str_replace('/audio/', '', $doc->path);
  1320. }
  1321. }
  1322. if ($id != -1) {
  1323. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1324. $quiz->description = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  1325. $quiz->description,
  1326. $this->course->code,
  1327. $this->course->destination_path,
  1328. $this->course->backup_path,
  1329. $this->course->info['path']
  1330. );
  1331. global $_custom;
  1332. if (isset($_custom['exercises_clean_dates_when_restoring']) && $_custom['exercises_clean_dates_when_restoring']) {
  1333. $quiz->start_time = null;
  1334. $quiz->end_time = null;
  1335. }
  1336. $params = array(
  1337. 'c_id' => $this->destination_course_id,
  1338. 'title' => self::DBUTF8($quiz->title),
  1339. 'description' => self::DBUTF8($quiz->description),
  1340. 'type' => isset($quiz->quiz_type) ? $quiz->quiz_type : $quiz->type,
  1341. 'random' => $quiz->random,
  1342. 'active' => $quiz->active,
  1343. 'sound' => self::DBUTF8($doc),
  1344. 'max_attempt' => (int)$quiz->max_attempt,
  1345. 'results_disabled' => (int)$quiz->results_disabled,
  1346. 'access_condition' => $quiz->access_condition,
  1347. 'start_time' => $quiz->start_time,
  1348. 'pass_percentage' => $quiz->pass_percentage,
  1349. 'end_time' => $quiz->end_time,
  1350. 'feedback_type' => (int)$quiz->feedback_type,
  1351. 'random_answers' => (int)$quiz->random_answers,
  1352. 'random_by_category' => $quiz->random_by_category,
  1353. 'review_answers' => $quiz->review_answers,
  1354. 'propagate_neg' => $quiz->propagate_neg,
  1355. 'text_when_finished' => $quiz->text_when_finished,
  1356. 'expired_time' => (int)$quiz->expired_time,
  1357. 'end_button' => (int)$quiz->end_button
  1358. );
  1359. if ($respect_base_content) {
  1360. $my_session_id = $quiz->session_id;
  1361. if (!empty($quiz->session_id)) {
  1362. $my_session_id = $session_id;
  1363. }
  1364. $params['session_id'] = $my_session_id;
  1365. } else {
  1366. if (!empty($session_id)) {
  1367. $session_id = intval($session_id);
  1368. $params['session_id'] = $session_id;
  1369. }
  1370. }
  1371. $new_id = Database::insert($table_qui, $params);
  1372. } else {
  1373. // $id = -1 identifies the fictionary test for collecting orphan questions. We do not store it in the database.
  1374. $new_id = -1;
  1375. }
  1376. if ($new_id && $new_id != -1) {
  1377. // Updates the question position
  1378. $exercise = new Exercise($this->destination_course_id);
  1379. $exercise->read($new_id);
  1380. $exercise->addExerciseToOrderTable();
  1381. $this->course->resources[RESOURCE_QUIZ][$id]->obj->destination_id = $new_id;
  1382. $order = 0;
  1383. if (!empty($quiz->question_ids)) {
  1384. foreach ($quiz->question_ids as $index => $question_id) {
  1385. $qid = $this->restore_quiz_question($question_id);
  1386. $question_order = $quiz->question_orders[$index] ? $quiz->question_orders[$index] : ++$order;
  1387. $sql = "INSERT IGNORE INTO ".$table_rel." SET c_id = ".$this->destination_course_id.", question_id = ".$qid.", exercice_id = ".$new_id.", question_order = ".$question_order;
  1388. Database::query($sql);
  1389. }
  1390. }
  1391. if (isset($quiz->categorie) && $quiz->categories) {
  1392. $exercise_obj = new Exercise($this->destination_course_id);
  1393. $exercise_obj->read($new_id);
  1394. $cats = array();
  1395. foreach ($quiz->categories as $cat) {
  1396. $cat_from_db = new Testcategory($cat['category_id']);
  1397. /*$cat_from_db = new Testcategory($cat['category_id']);
  1398. if ($cat_from_db && $cat_from_db->title == $cat['title']) {
  1399. echo '1';
  1400. //use the same id
  1401. $cats[$cat_from_db->id] = $cat['count_questions'];
  1402. } else {*/
  1403. $cat_from_db = $cat_from_db->get_category_by_title($cat['title'], $this->destination_course_id);
  1404. if (empty($cat_from_db)) {
  1405. //Create a new category in this portal
  1406. if ($cat['category_id'] == 0) {
  1407. $category_c_id = 0;
  1408. } else {
  1409. $category_c_id = $this->destination_course_id;
  1410. }
  1411. $new_cat = new Testcategory(null, $cat['title'], $cat['description'], null, 'simple', $category_c_id);
  1412. $new_cat_id = $new_cat->addCategoryInBDD();
  1413. $cats[$new_cat_id] = $cat['count_questions'];
  1414. } else {
  1415. $cats[$cat_from_db['iid']] = $cat['count_questions'];
  1416. }
  1417. //}
  1418. }
  1419. $exercise_obj->save_categories_in_exercise($cats);
  1420. }
  1421. }
  1422. }
  1423. }
  1424. }
  1425. /**
  1426. * Restore quiz-questions
  1427. * @params int question id
  1428. */
  1429. function restore_quiz_question($id)
  1430. {
  1431. $resources = $this->course->resources;
  1432. $question = isset($resources[RESOURCE_QUIZQUESTION][$id]) ? $resources[RESOURCE_QUIZQUESTION][$id] : null;
  1433. $new_id = 0;
  1434. if (is_object($question)) {
  1435. if ($question->is_restored()) {
  1436. return $question->destination_id;
  1437. }
  1438. $table_que = Database::get_course_table(TABLE_QUIZ_QUESTION);
  1439. $table_ans = Database::get_course_table(TABLE_QUIZ_ANSWER);
  1440. $table_options = Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION);
  1441. // check resources inside html from ckeditor tool and copy correct urls into recipient course
  1442. $question->description = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  1443. $question->description,
  1444. $this->course->code,
  1445. $this->course->destination_path
  1446. );
  1447. $parent_id = 0;
  1448. if (isset($question->parent_info) && !empty($question->parent_info)) {
  1449. $question_obj = Question::readByTitle($question->parent_info['question'], $this->destination_course_id);
  1450. if ($question_obj) {
  1451. // Reuse media.
  1452. $parent_id = $question_obj->selectId();
  1453. } else {
  1454. // Create media.
  1455. $question_obj = new MediaQuestion();
  1456. $question_obj->updateCourse($this->destination_course_id);
  1457. $parent_id = $question_obj->saveMedia(array(
  1458. 'questionName' => $question->parent_info['question'],
  1459. 'questionDescription' => $question->parent_info['description']
  1460. ));
  1461. }
  1462. }
  1463. $sql = "INSERT INTO ".$table_que." SET
  1464. c_id = ".$this->destination_course_id." ,
  1465. question = '".self::DBUTF8escapestring($question->question)."',
  1466. description = '".self::DBUTF8escapestring($question->description)."',
  1467. ponderation = '".self::DBUTF8escapestring($question->ponderation)."',
  1468. position = '".self::DBUTF8escapestring($question->position)."',
  1469. type='".self::DBUTF8escapestring($question->quiz_type)."',
  1470. picture='".self::DBUTF8escapestring($question->picture)."',
  1471. level='".self::DBUTF8escapestring($question->level)."',
  1472. parent_id = '".$parent_id."',
  1473. extra='".self::DBUTF8escapestring($question->extra)."'";
  1474. Database::query($sql);
  1475. $new_id = Database::insert_id();
  1476. if ($new_id) {
  1477. if (!empty($question->picture)) {
  1478. $question_temp = Question::read($new_id, $this->destination_course_info['real_id']);
  1479. $documentPath = api_get_path(SYS_COURSE_PATH).$this->destination_course_info['path'].'/document';
  1480. // picture path
  1481. $picturePath = $documentPath.'/images';
  1482. $old_picture = api_get_path(SYS_COURSE_PATH).$this->course->info['path'].'/document/images/'.$question->picture;
  1483. if (file_exists($old_picture)) {
  1484. $picture_name = 'quiz-'.$new_id.'.jpg';
  1485. $result = $question_temp->uploadPicture($old_picture, $picture_name, $picturePath);
  1486. if ($result) {
  1487. $sql = "UPDATE $table_que SET picture = '$picture_name' WHERE c_id = ".$this->destination_course_id." AND id = $new_id ";
  1488. Database::query($sql);
  1489. }
  1490. }
  1491. }
  1492. }
  1493. if ($question->type == MATCHING) {
  1494. $temp = array();
  1495. $matching_list = array();
  1496. $matching_to_update = array();
  1497. foreach ($question->answers as $index => $answer) {
  1498. $temp[$answer['position']] = $answer;
  1499. if (!empty($answer['correct'])) {
  1500. $matching_to_update[$answer['iid']] = $answer['correct'];
  1501. }
  1502. }
  1503. foreach ($temp as $index => $answer) {
  1504. $sql = "INSERT INTO ".$table_ans." SET
  1505. c_id = ".$this->destination_course_id." ,
  1506. question_id = '".$new_id."',
  1507. answer = '".self::DBUTF8escapestring($answer['answer'])."',
  1508. correct = '".$answer['correct']."',
  1509. comment = '".self::DBUTF8escapestring($answer['comment'])."',
  1510. ponderation = '".$answer['ponderation']."',
  1511. position = '".$answer['position']."',
  1512. hotspot_coordinates = '".$answer['hotspot_coordinates']."',
  1513. hotspot_type = '".$answer['hotspot_type']."'";
  1514. Database::query($sql);
  1515. $new_answer_id = Database::insert_id();
  1516. $matching_list[$answer['iid']] = $new_answer_id;
  1517. }
  1518. foreach ($matching_to_update as $old_answer_id => $old_correct_id) {
  1519. $new_correct = $matching_list[$old_correct_id];
  1520. $new_fixed_id = $matching_list[$old_answer_id];
  1521. $sql = "UPDATE $table_ans SET correct = '$new_correct' WHERE iid = $new_fixed_id ";
  1522. Database::query($sql);
  1523. }
  1524. } else {
  1525. $correct_answers = array();
  1526. foreach ($question->answers as $index => $answer) {
  1527. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1528. $answer['answer'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($answer['answer'], $this->course->code, $this->course->destination_path);
  1529. $answer['comment'] = DocumentManager::replace_urls_inside_content_html_from_copy_course($answer['comment'], $this->course->code, $this->course->destination_path);
  1530. $sql = "INSERT INTO ".$table_ans." SET
  1531. c_id = ".$this->destination_course_id." ,
  1532. question_id = '".$new_id."',
  1533. answer = '".self::DBUTF8escapestring($answer['answer'])."',
  1534. correct = '".$answer['correct']."',
  1535. comment = '".self::DBUTF8escapestring($answer['comment'])."',
  1536. ponderation = '".$answer['ponderation']."',
  1537. position = '".$answer['position']."',
  1538. hotspot_coordinates = '".$answer['hotspot_coordinates']."',
  1539. hotspot_type = '".$answer['hotspot_type']."'";
  1540. Database::query($sql);
  1541. $new_answer_id = Database::insert_id();
  1542. $correct_answers[$new_answer_id] = $answer['correct'];
  1543. }
  1544. }
  1545. //Current course id
  1546. $course_id = api_get_course_int_id();
  1547. //Moving quiz_question_options
  1548. if ($question->type == MULTIPLE_ANSWER_TRUE_FALSE) {
  1549. $question_option_list = Question::readQuestionOption($id, $course_id);
  1550. //Question copied from the current platform
  1551. if ($question_option_list) {
  1552. $old_option_ids = array();
  1553. foreach ($question_option_list as $item) {
  1554. $old_id = $item['iid'];
  1555. unset($item['iid']);
  1556. $item['question_id'] = $new_id;
  1557. $item['c_id'] = $this->destination_course_id;
  1558. $question_option_id = Database::insert($table_options, $item);
  1559. $old_option_ids[$old_id] = $question_option_id;
  1560. }
  1561. if ($old_option_ids) {
  1562. $new_answers = Database::select('iid, correct', $table_ans, array('WHERE' => array('question_id = ? AND c_id = ? '=> array($new_id, $this->destination_course_id))));
  1563. foreach ($new_answers as $answer_item) {
  1564. $params = array();
  1565. $params['correct'] = $old_option_ids[$answer_item['correct']];
  1566. $question_option_id = Database::update($table_ans, $params, array('iid = ? AND c_id = ? AND question_id = ? '=> array($answer_item['iid'], $this->destination_course_id, $new_id)), false);
  1567. }
  1568. }
  1569. } else {
  1570. $new_options = array();
  1571. if (isset($question->question_options)) {
  1572. foreach ($question->question_options as $obj) {
  1573. $item = array();
  1574. $item['question_id'] = $new_id;
  1575. $item['c_id'] = $this->destination_course_id;
  1576. $item['name'] = $obj->obj->name;
  1577. $item['position'] = $obj->obj->position;
  1578. $question_option_id = Database::insert($table_options, $item);
  1579. $new_options[$obj->obj->iid] = $question_option_id;
  1580. }
  1581. foreach ($correct_answers as $answer_id => $correct_answer) {
  1582. $params = array();
  1583. $params['correct'] = $new_options[$correct_answer];
  1584. Database::update($table_ans, $params, array('iid = ? AND c_id = ? AND question_id = ? '=> array($answer_id, $this->destination_course_id, $new_id)), false);
  1585. }
  1586. }
  1587. }
  1588. }
  1589. if ($question->categories) {
  1590. $cats = array();
  1591. foreach ($question->categories as $cat) {
  1592. $new_category = new Testcategory($cat['category_id']);
  1593. $new_category = $new_category->get_category_by_title($cat['title'], $this->destination_course_id);
  1594. if (empty($new_category)) {
  1595. //Create a new category in this portal
  1596. if ($cat['category_id'] == 0) {
  1597. $category_c_id = 0;
  1598. } else {
  1599. $category_c_id = $this->destination_course_id;
  1600. }
  1601. $new_cat = new Testcategory(null, $cat['title'], $cat['description'], null, 'simple', $category_c_id);
  1602. $new_cat_id = $new_cat->addCategoryInBDD();
  1603. $cats[] = $new_cat_id;
  1604. } else {
  1605. $cats[] = $new_category['iid'];
  1606. }
  1607. }
  1608. $question = Question::read($new_id, $this->destination_course_id);
  1609. if (!empty($cats)) {
  1610. $question->saveCategories($cats);
  1611. }
  1612. }
  1613. $this->course->resources[RESOURCE_QUIZQUESTION][$id]->destination_id = $new_id;
  1614. }
  1615. return $new_id;
  1616. }
  1617. /**
  1618. * Restore surveys
  1619. */
  1620. function restore_surveys()
  1621. {
  1622. if ($this->course->has_resources(RESOURCE_SURVEY)) {
  1623. $table_sur = Database :: get_course_table(TABLE_SURVEY);
  1624. $table_que = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  1625. $table_ans = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  1626. $resources = $this->course->resources;
  1627. foreach ($resources[RESOURCE_SURVEY] as $id => $survey) {
  1628. $sql_check = 'SELECT survey_id FROM '.$table_sur.'
  1629. WHERE c_id = '.$this->destination_course_id.' AND
  1630. code = "'.self::DBUTF8escapestring($survey->code).'" AND
  1631. lang = "'.self::DBUTF8escapestring($survey->lang).'" ';
  1632. $result_check = Database::query($sql_check);
  1633. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1634. $survey->title = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  1635. $survey->title,
  1636. $this->course->code,
  1637. $this->course->destination_path,
  1638. $this->course->backup_path,
  1639. $this->course->info['path']
  1640. );
  1641. $survey->subtitle = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  1642. $survey->subtitle,
  1643. $this->course->code,
  1644. $this->course->destination_path,
  1645. $this->course->backup_path,
  1646. $this->course->info['path']
  1647. );
  1648. $survey->intro = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  1649. $survey->intro,
  1650. $this->course->code,
  1651. $this->course->destination_path,
  1652. $this->course->backup_path,
  1653. $this->course->info['path']
  1654. );
  1655. $survey->surveythanks = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  1656. $survey->surveythanks,
  1657. $this->course->code,
  1658. $this->course->destination_path,
  1659. $this->course->backup_path,
  1660. $this->course->info['path']
  1661. );
  1662. $sql = "INSERT INTO ".$table_sur." "."SET
  1663. c_id = ".$this->destination_course_id." ,
  1664. code = '".self::DBUTF8escapestring($survey->code)."', ".
  1665. "title = '".self::DBUTF8escapestring($survey->title)."', ".
  1666. "subtitle = '".self::DBUTF8escapestring($survey->subtitle)."', ".
  1667. "author = '".self::DBUTF8escapestring($survey->author)."', ".
  1668. "lang = '".self::DBUTF8escapestring($survey->lang)."', ".
  1669. "avail_from = '".self::DBUTF8escapestring($survey->avail_from)."', ".
  1670. "avail_till = '".self::DBUTF8escapestring($survey->avail_till)."', ".
  1671. "is_shared = '".self::DBUTF8escapestring($survey->is_shared)."', ".
  1672. "template = '".self::DBUTF8escapestring($survey->template)."', ".
  1673. "intro = '".self::DBUTF8escapestring($survey->intro)."', ".
  1674. "surveythanks = '".self::DBUTF8escapestring($survey->surveythanks)."', ".
  1675. "creation_date = '".self::DBUTF8escapestring($survey->creation_date)."', ".
  1676. "invited = '0', ".
  1677. "answered = '0', ".
  1678. "invite_mail = '".self::DBUTF8escapestring($survey->invite_mail)."', ".
  1679. "reminder_mail = '".self::DBUTF8escapestring($survey->reminder_mail)."'";
  1680. //An existing survey exists with the same code and the same language
  1681. if (Database::num_rows($result_check) == 1) {
  1682. switch ($this->file_option) {
  1683. case FILE_SKIP:
  1684. //Do nothing
  1685. break;
  1686. case FILE_RENAME:
  1687. $survey_code = $survey->code.'_';
  1688. $i = 1;
  1689. $temp_survey_code = $survey_code.$i;
  1690. while (!$this->is_survey_code_available($temp_survey_code)) {
  1691. $temp_survey_code = $survey_code.++$i;
  1692. }
  1693. $survey_code = $temp_survey_code;
  1694. $sql = "INSERT INTO ".$table_sur." "."SET
  1695. c_id = ".$this->destination_course_id." ,
  1696. code = '".self::DBUTF8escapestring($survey_code)."', ".
  1697. "title = '".self::DBUTF8escapestring($survey->title)."', ".
  1698. "subtitle = '".self::DBUTF8escapestring($survey->subtitle)."', ".
  1699. "author = '".self::DBUTF8escapestring($survey->author)."', ".
  1700. "lang = '".self::DBUTF8escapestring($survey->lang)."', ".
  1701. "avail_from = '".self::DBUTF8escapestring($survey->avail_from)."', ".
  1702. "avail_till = '".self::DBUTF8escapestring($survey->avail_till)."', ".
  1703. "is_shared = '".self::DBUTF8escapestring($survey->is_shared)."', ".
  1704. "template = '".self::DBUTF8escapestring($survey->template)."', ".
  1705. "intro = '".self::DBUTF8escapestring($survey->intro)."', ".
  1706. "surveythanks = '".self::DBUTF8escapestring($survey->surveythanks)."', ".
  1707. "creation_date = '".self::DBUTF8escapestring($survey->creation_date)."', ".
  1708. "invited = '0', ".
  1709. "answered = '0', ".
  1710. "invite_mail = '".self::DBUTF8escapestring($survey->invite_mail)."', ".
  1711. "reminder_mail = '".self::DBUTF8escapestring($survey->reminder_mail)."'";
  1712. //Insert the new source survey
  1713. Database::query($sql);
  1714. $new_id = Database::insert_id();
  1715. $this->course->resources[RESOURCE_SURVEY][$id]->destination_id = $new_id;
  1716. foreach ($survey->question_ids as $index => $question_id) {
  1717. $qid = $this->restore_survey_question($question_id, $new_id);
  1718. $sql = "UPDATE ".$table_que." SET survey_id = ".$new_id." WHERE c_id = ".$this->destination_course_id." AND question_id = $qid";
  1719. Database::query($sql);
  1720. $sql = "UPDATE ".$table_ans." SET survey_id = ".$new_id." WHERE c_id = ".$this->destination_course_id." AND question_id = $qid";
  1721. Database::query($sql);
  1722. }
  1723. break;
  1724. case FILE_OVERWRITE:
  1725. // Delete the existing survey with the same code and language and import the one of the source course
  1726. // getting the information of the survey (used for when the survey is shared)
  1727. require_once(api_get_path(SYS_CODE_PATH).'survey/survey.lib.php');
  1728. $sql_select_existing_survey = "SELECT * FROM $table_sur WHERE c_id = ".$this->destination_course_id." AND survey_id='".self::DBUTF8escapestring(
  1729. Database::result($result_check, 0, 0)
  1730. )."'";
  1731. $result = Database::query($sql_select_existing_survey);
  1732. $survey_data = Database::fetch_array($result, 'ASSOC');
  1733. // if the survey is shared => also delete the shared content
  1734. if (is_numeric($survey_data['survey_share'])) {
  1735. survey_manager::delete_survey(
  1736. $survey_data['survey_share'],
  1737. true,
  1738. $this->destination_course_id
  1739. );
  1740. }
  1741. $return = survey_manager :: delete_survey(
  1742. $survey_data['survey_id'],
  1743. false,
  1744. $this->destination_course_id
  1745. );
  1746. //Insert the new source survey
  1747. Database::query($sql);
  1748. $new_id = Database::insert_id();
  1749. $this->course->resources[RESOURCE_SURVEY][$id]->destination_id = $new_id;
  1750. foreach ($survey->question_ids as $index => $question_id) {
  1751. $qid = $this->restore_survey_question($question_id, $new_id);
  1752. $sql = "UPDATE $table_que SET survey_id = $new_id WHERE c_id = ".$this->destination_course_id." AND question_id = $qid";
  1753. Database::query($sql);
  1754. $sql = "UPDATE $table_ans SET survey_id = $new_id WHERE c_id = ".$this->destination_course_id." AND question_id = $qid";
  1755. Database::query($sql);
  1756. }
  1757. break;
  1758. default:
  1759. break;
  1760. }
  1761. } //No existing survey with the same language and the same code, we just copy the survey
  1762. else {
  1763. Database::query($sql);
  1764. $new_id = Database::insert_id();
  1765. $this->course->resources[RESOURCE_SURVEY][$id]->destination_id = $new_id;
  1766. foreach ($survey->question_ids as $index => $question_id) {
  1767. $qid = $this->restore_survey_question($question_id, $new_id);
  1768. $sql = "UPDATE $table_que SET survey_id = $new_id WHERE c_id = ".$this->destination_course_id." AND question_id = $qid";
  1769. Database::query($sql);
  1770. $sql = "UPDATE $table_ans SET survey_id = $new_id WHERE c_id = ".$this->destination_course_id." AND question_id = $qid";
  1771. Database::query($sql);
  1772. }
  1773. }
  1774. }
  1775. }
  1776. }
  1777. /**
  1778. * Check availability of a survey code
  1779. */
  1780. function is_survey_code_available($survey_code)
  1781. {
  1782. $table_sur = Database :: get_course_table(TABLE_SURVEY);
  1783. $sql = "SELECT * FROM $table_sur WHERE c_id = ".$this->destination_course_id." AND code='".self::DBUTF8escapestring(
  1784. $survey_code
  1785. )."'";
  1786. $result = Database::query($sql);
  1787. if (Database::num_rows($result) > 0) {
  1788. return false;
  1789. } else {
  1790. return true;
  1791. }
  1792. }
  1793. /**
  1794. * Restore survey-questions
  1795. */
  1796. function restore_survey_question($id, $survey_id)
  1797. {
  1798. $resources = $this->course->resources;
  1799. $question = $resources[RESOURCE_SURVEYQUESTION][$id];
  1800. $new_id = 0;
  1801. if (is_object($question)) {
  1802. if ($question->is_restored()) {
  1803. return $question->destination_id;
  1804. }
  1805. $table_que = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  1806. $table_ans = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  1807. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1808. $question->survey_question = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  1809. $question->survey_question,
  1810. $this->course->code,
  1811. $this->course->destination_path,
  1812. $this->course->backup_path,
  1813. $this->course->info['path']
  1814. );
  1815. $sql = "INSERT INTO ".$table_que." SET
  1816. c_id = ".$this->destination_course_id." ,
  1817. survey_id = '".self::DBUTF8escapestring($survey_id)."', ".
  1818. "survey_question = '".self::DBUTF8escapestring($question->survey_question)."', ".
  1819. "survey_question_comment = '".self::DBUTF8escapestring($question->survey_question_comment)."', ".
  1820. "type = '".self::DBUTF8escapestring($question->survey_question_type)."', ".
  1821. "display = '".self::DBUTF8escapestring($question->display)."', ".
  1822. "sort = '".self::DBUTF8escapestring($question->sort)."', ".
  1823. "shared_question_id = '".self::DBUTF8escapestring($question->shared_question_id)."', ".
  1824. "max_value = '".self::DBUTF8escapestring($question->max_value)."' ";
  1825. Database::query($sql);
  1826. $new_id = Database::insert_id();
  1827. foreach ($question->answers as $index => $answer) {
  1828. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  1829. $answer['option_text'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  1830. $answer['option_text'],
  1831. $this->course->code,
  1832. $this->course->destination_path,
  1833. $this->course->backup_path,
  1834. $this->course->info['path']
  1835. );
  1836. $sql = "INSERT INTO ".$table_ans." SET
  1837. c_id = ".$this->destination_course_id." ,
  1838. question_id = '".self::DBUTF8escapestring($new_id)."', ".
  1839. "option_text = '".self::DBUTF8escapestring($answer['option_text'])."', ".
  1840. "sort = '".self::DBUTF8escapestring($answer['sort'])."', ".
  1841. "survey_id = '".self::DBUTF8escapestring($survey_id)."'";
  1842. Database::query($sql);
  1843. }
  1844. $this->course->resources[RESOURCE_SURVEYQUESTION][$id]->destination_id = $new_id;
  1845. }
  1846. return $new_id;
  1847. }
  1848. /**
  1849. * Restore learnpaths
  1850. */
  1851. function restore_learnpaths($session_id = 0, $respect_base_content = false)
  1852. {
  1853. if ($this->course->has_resources(RESOURCE_LEARNPATH)) {
  1854. $table_main = Database::get_course_table(TABLE_LP_MAIN);
  1855. $table_item = Database::get_course_table(TABLE_LP_ITEM);
  1856. $table_tool = Database::get_course_table(TABLE_TOOL_LIST);
  1857. $resources = $this->course->resources;
  1858. $origin_path = $this->course->backup_path.'/upload/learning_path/images/';
  1859. $destination_path = api_get_path(
  1860. SYS_COURSE_PATH
  1861. ).$this->course->destination_path.'/upload/learning_path/images/';
  1862. foreach ($resources[RESOURCE_LEARNPATH] as $id => $lp) {
  1863. $condition_session = "";
  1864. if (!empty($session_id)) {
  1865. if ($respect_base_content) {
  1866. $my_session_id = $lp->session_id;
  1867. if (!empty($lp->session_id)) {
  1868. $my_session_id = $session_id;
  1869. }
  1870. $condition_session = " , session_id = '$my_session_id' ";
  1871. } else {
  1872. $session_id = intval($session_id);
  1873. $condition_session = " , session_id = '$session_id' ";
  1874. }
  1875. }
  1876. //Adding the author's image
  1877. if (!empty($lp->preview_image)) {
  1878. $new_filename = uniqid('').$new_filename.substr(
  1879. $lp->preview_image,
  1880. strlen($lp->preview_image) - 7,
  1881. strlen($lp->preview_image)
  1882. );
  1883. if (file_exists($origin_path.$lp->preview_image) && !is_dir($origin_path.$lp->preview_image)) {
  1884. $copy_result = copy($origin_path.$lp->preview_image, $destination_path.$new_filename);
  1885. //$copy_result = true;
  1886. if ($copy_result) {
  1887. $lp->preview_image = $new_filename;
  1888. } else {
  1889. $lp->preview_image = '';
  1890. }
  1891. }
  1892. }
  1893. if ($this->add_text_in_items) {
  1894. $lp->name = $lp->name.' '.get_lang('Copy');
  1895. }
  1896. if (isset($this->tool_copy_settings['learnpaths'])) {
  1897. if (isset($this->tool_copy_settings['learnpaths']['reset_dates']) && $this->tool_copy_settings['learnpaths']['reset_dates']) {
  1898. $lp->created_on = api_get_utc_datetime();
  1899. $lp->modified_on = api_get_utc_datetime();
  1900. $lp->publicated_on = null;
  1901. }
  1902. }
  1903. $sql = "INSERT INTO ".$table_main." SET
  1904. c_id = ".$this->destination_course_id." ,
  1905. lp_type = '".$lp->lp_type."', ".
  1906. "name = '".self::DBUTF8escapestring($lp->name)."', ".
  1907. "path = '".self::DBUTF8escapestring($lp->path)."', ".
  1908. "ref = '".$lp->ref."', ".
  1909. "description = '".self::DBUTF8escapestring($lp->description)."', ".
  1910. "content_local = '".self::DBUTF8escapestring($lp->content_local)."', ".
  1911. "default_encoding = '".self::DBUTF8escapestring($lp->default_encoding)."', ".
  1912. "default_view_mod = '".self::DBUTF8escapestring($lp->default_view_mod)."', ".
  1913. "prevent_reinit = '".self::DBUTF8escapestring($lp->prevent_reinit)."', ".
  1914. "force_commit = '".self::DBUTF8escapestring($lp->force_commit)."', ".
  1915. "content_maker = '".self::DBUTF8escapestring($lp->content_maker)."', ".
  1916. "display_order = '".self::DBUTF8escapestring($lp->display_order)."', ".
  1917. "js_lib = '".self::DBUTF8escapestring($lp->js_lib)."', ".
  1918. "content_license = '".self::DBUTF8escapestring($lp->content_license)."', ".
  1919. "author = '".self::DBUTF8escapestring($lp->author)."', ".
  1920. "preview_image = '".self::DBUTF8escapestring($lp->preview_image)."', ".
  1921. "use_max_score = '".self::DBUTF8escapestring($lp->use_max_score)."', ".
  1922. "autolunch = '".self::DBUTF8escapestring($lp->autolunch)."', ".
  1923. "created_on = '".self::DBUTF8escapestring($lp->created_on)."', ".
  1924. "modified_on = '".self::DBUTF8escapestring($lp->modified_on)."', ".
  1925. "publicated_on = '".self::DBUTF8escapestring($lp->publicated_on)."', ".
  1926. "expired_on = '".self::DBUTF8escapestring($lp->expired_on)."', ".
  1927. "debug = '".self::DBUTF8escapestring($lp->debug)."' $condition_session ";
  1928. Database::query($sql);
  1929. $new_lp_id = Database::insert_id();
  1930. if ($lp->visibility) {
  1931. $sql = "INSERT INTO $table_tool SET c_id = ".$this->destination_course_id." , name='".self::DBUTF8escapestring(
  1932. $lp->name
  1933. )."', link='newscorm/lp_controller.php?action=view&lp_id=$new_lp_id', image='scormbuilder.gif', visibility='1', admin='0', address='squaregrey.gif'";
  1934. Database::query($sql);
  1935. }
  1936. if ($new_lp_id) {
  1937. api_item_property_update(
  1938. $this->destination_course_info,
  1939. TOOL_LEARNPATH,
  1940. $new_lp_id,
  1941. 'LearnpathAdded',
  1942. api_get_user_id(),
  1943. 0,
  1944. 0,
  1945. 0,
  1946. 0,
  1947. $session_id
  1948. );
  1949. //Set the new LP to visible
  1950. api_item_property_update(
  1951. $this->destination_course_info,
  1952. TOOL_LEARNPATH,
  1953. $new_lp_id,
  1954. 'invisible',
  1955. api_get_user_id(),
  1956. 0,
  1957. 0,
  1958. 0,
  1959. 0,
  1960. $session_id
  1961. );
  1962. }
  1963. $new_item_ids = array();
  1964. $parent_item_ids = array();
  1965. $previous_item_ids = array();
  1966. $next_item_ids = array();
  1967. $old_prerequisite = array();
  1968. $old_refs = array();
  1969. $prerequisite_ids = array();
  1970. foreach ($lp->get_items() as $index => $item) {
  1971. // we set the ref code here and then we update in a for loop
  1972. $ref = $item['ref'];
  1973. //Dealing with path the same way as ref as some data has been put into path when it's a local resource
  1974. //Only fix the path for no scos
  1975. if ($item['item_type'] == 'sco') {
  1976. $path = self::DBUTF8escapestring($item['path']);
  1977. } else {
  1978. $path = self::DBUTF8escapestring($item['path']);
  1979. $path = $this->get_new_id($item['item_type'], $path);
  1980. }
  1981. $item['launch_dataprereq_type'] = isset($item['launch_dataprereq_type']) ? $item['launch_dataprereq_type'] : null;
  1982. $sql = "INSERT INTO ".$table_item." SET
  1983. c_id = ".$this->destination_course_id." ,
  1984. lp_id = '".self::DBUTF8escapestring($new_lp_id)."', ".
  1985. "item_type='".self::DBUTF8escapestring($item['item_type'])."', ".
  1986. "ref = '".self::DBUTF8escapestring($ref)."', ".
  1987. "title = '".self::DBUTF8escapestring($item['title'])."', ".
  1988. "description ='".self::DBUTF8escapestring($item['description'])."', ".
  1989. "path = '".self::DBUTF8escapestring($path)."', ".
  1990. "min_score = '".self::DBUTF8escapestring($item['min_score'])."', ".
  1991. "max_score = '".self::DBUTF8escapestring($item['max_score'])."', ".
  1992. "mastery_score = '".self::DBUTF8escapestring($item['mastery_score'])."', ".
  1993. "parent_item_id = '".self::DBUTF8escapestring($item['parent_item_id'])."', ".
  1994. "previous_item_id = '".self::DBUTF8escapestring($item['previous_item_id'])."', ".
  1995. "next_item_id = '".self::DBUTF8escapestring($item['next_item_id'])."', ".
  1996. "display_order = '".self::DBUTF8escapestring($item['display_order'])."', ".
  1997. "prerequisite = '".self::DBUTF8escapestring($item['prerequisite'])."', ".
  1998. "parameters='".self::DBUTF8escapestring($item['parameters'])."', ".
  1999. "audio='".self::DBUTF8escapestring($item['audio'])."', ".
  2000. "launch_data = '".self::DBUTF8escapestring($item['launch_dataprereq_type'])."'";
  2001. Database::query($sql);
  2002. $new_item_id = Database::insert_id();
  2003. //save a link between old and new item IDs
  2004. $new_item_ids[$item['id']] = $new_item_id;
  2005. //save a reference of items that need a parent_item_id refresh
  2006. $parent_item_ids[$new_item_id] = $item['parent_item_id'];
  2007. //save a reference of items that need a previous_item_id refresh
  2008. $previous_item_ids[$new_item_id] = $item['previous_item_id'];
  2009. //save a reference of items that need a next_item_id refresh
  2010. $next_item_ids[$new_item_id] = $item['next_item_id'];
  2011. if (!empty($item['prerequisite'])) {
  2012. if ($lp->lp_type == '2') {
  2013. // if is an sco
  2014. $old_prerequisite[$new_item_id] = $item['prerequisite'];
  2015. } else {
  2016. $old_prerequisite[$new_item_id] = $new_item_ids[$item['prerequisite']];
  2017. }
  2018. }
  2019. if (!empty($ref)) {
  2020. if ($lp->lp_type == '2') {
  2021. // if is an sco
  2022. $old_refs[$new_item_id] = $ref;
  2023. } else {
  2024. $old_refs[$new_item_id] = $new_item_ids[$ref];
  2025. }
  2026. }
  2027. $prerequisite_ids[$new_item_id] = $item['prerequisite'];
  2028. }
  2029. // updating prerequisites
  2030. foreach ($old_prerequisite as $key => $my_old_prerequisite) {
  2031. if ($my_old_prerequisite != '') {
  2032. $sql = "UPDATE ".$table_item." SET prerequisite = '".$my_old_prerequisite."' WHERE c_id = ".$this->destination_course_id." AND id = '".$key."' ";
  2033. Database::query($sql);
  2034. }
  2035. }
  2036. //updating refs
  2037. foreach ($old_refs as $key => $my_old_ref) {
  2038. if ($my_old_ref != '') {
  2039. $sql = "UPDATE ".$table_item." SET ref = '".$my_old_ref."' WHERE c_id = ".$this->destination_course_id." AND id = '".$key."' ";
  2040. Database::query($sql);
  2041. }
  2042. }
  2043. foreach ($parent_item_ids as $new_item_id => $parent_item_old_id) {
  2044. $parent_new_id = 0;
  2045. if ($parent_item_old_id != 0) {
  2046. $parent_new_id = $new_item_ids[$parent_item_old_id];
  2047. }
  2048. $sql = "UPDATE ".$table_item." SET parent_item_id = '".$parent_new_id."' WHERE c_id = ".$this->destination_course_id." AND id = '".$new_item_id."'";
  2049. Database::query($sql);
  2050. }
  2051. foreach ($previous_item_ids as $new_item_id => $previous_item_old_id) {
  2052. $previous_new_id = 0;
  2053. if ($previous_item_old_id != 0) {
  2054. $previous_new_id = $new_item_ids[$previous_item_old_id];
  2055. }
  2056. $sql = "UPDATE ".$table_item." SET previous_item_id = '".$previous_new_id."' WHERE c_id = ".$this->destination_course_id." AND id = '".$new_item_id."'";
  2057. Database::query($sql);
  2058. }
  2059. foreach ($next_item_ids as $new_item_id => $next_item_old_id) {
  2060. $next_new_id = 0;
  2061. if ($next_item_old_id != 0) {
  2062. $next_new_id = $new_item_ids[$next_item_old_id];
  2063. }
  2064. $sql = "UPDATE ".$table_item." SET next_item_id = '".$next_new_id."' WHERE c_id = ".$this->destination_course_id." AND id = '".$new_item_id."'";
  2065. Database::query($sql);
  2066. }
  2067. foreach ($prerequisite_ids as $new_item_id => $prerequisite_old_id) {
  2068. $prerequisite_new_id = 0;
  2069. if ($prerequisite_old_id != 0) {
  2070. $prerequisite_new_id = $new_item_ids[$prerequisite_old_id];
  2071. }
  2072. $sql = "UPDATE ".$table_item." SET prerequisite = '".$prerequisite_new_id."' WHERE c_id = ".$this->destination_course_id." AND id = '".$new_item_id."'";
  2073. Database::query($sql);
  2074. }
  2075. $this->course->resources[RESOURCE_LEARNPATH][$id]->destination_id = $new_lp_id;
  2076. }
  2077. }
  2078. }
  2079. /**
  2080. * restore works
  2081. * @todo I fixed this crappy function jmontoya
  2082. */
  2083. function restore_student_publication()
  2084. {
  2085. $work_assignment_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  2086. $work_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  2087. $item_property_table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  2088. //query in student publication
  2089. $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
  2090. FROM '.$work_table.'
  2091. WHERE c_id = '.$this->course_origin_id.' AND filetype="folder" AND active IN (0, 1) ';
  2092. $result = Database::query($sql);
  2093. $folders = Database::store_result($result, 'ASSOC');
  2094. foreach ($folders as $folder) {
  2095. $old_id = $folder['id'];
  2096. unset($folder['id']);
  2097. $folder['c_id'] = $this->destination_course_id;
  2098. $folder['parent_id'] = 0;
  2099. $new_id = Database::insert($work_table, $folder);
  2100. if ($new_id) {
  2101. //query in item property
  2102. $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
  2103. FROM '.$item_property_table.' ip INNER JOIN '.$work_table.' sp ON ip.ref=sp.id
  2104. WHERE sp.c_id = '.$this->course_origin_id.' AND
  2105. ip.c_id = '.$this->course_origin_id.' AND
  2106. tool="work" AND sp.id = '.$old_id.'';
  2107. $result = Database::query($sql);
  2108. $sub_folders = Database::store_result($result, 'ASSOC');
  2109. foreach ($sub_folders as $sub_folder) {
  2110. $sub_folder['c_id'] = $this->destination_course_id;
  2111. $sub_folder['ref'] = $new_id;
  2112. $new_item_id = Database::insert($item_property_table, $sub_folder);
  2113. }
  2114. $sql = 'SELECT sa.id, sa.expires_on,sa.ends_on,sa.add_to_calendar, sa.enable_qualification, sa.publication_id
  2115. FROM '.$work_assignment_table.' sa INNER JOIN '.$work_table.' sp ON sa.publication_id=sp.id
  2116. WHERE sp.c_id = '.$this->course_origin_id.' AND
  2117. sa.c_id = '.$this->course_origin_id.' AND
  2118. filetype="folder" AND sp.id = '.$old_id.'';
  2119. $result = Database::query($sql);
  2120. $assing_list = Database::store_result($result, 'ASSOC');
  2121. foreach ($assing_list as $assign) {
  2122. $assign['c_id'] = $this->destination_course_id;
  2123. $assign['id'] = $new_id;
  2124. Database::insert($work_assignment_table, $assign);
  2125. }
  2126. }
  2127. }
  2128. $destination = '../../courses/'.$this->course->destination_path.'/work/';
  2129. $origin = '../../courses/'.$this->course->info['path'].'/work/';
  2130. self::allow_create_all_directory($origin, $destination, false);
  2131. }
  2132. /**
  2133. * copy all directory and sub directory
  2134. * @param string The path origin
  2135. * @param string The path destination
  2136. * @param boolean Option Overwrite
  2137. * @return void()
  2138. */
  2139. function allow_create_all_directory($source, $dest, $overwrite = false)
  2140. {
  2141. if (!is_dir($dest)) {
  2142. mkdir($dest, api_get_permissions_for_new_directories());
  2143. }
  2144. if ($handle = opendir($source)) { // if the folder exploration is sucsessful, continue
  2145. while (false !== ($file = readdir(
  2146. $handle
  2147. ))) { // as long as storing the next file to $file is successful, continue
  2148. if ($file != '.' && $file != '..') {
  2149. $path = $source.'/'.$file;
  2150. if (is_file($path)) {
  2151. /* if (!is_file($dest . '/' . $file) || $overwrite)
  2152. if (!@copy($path, $dest . '/' . $file)) {
  2153. echo '<font color="red">File ('.$path.') '.get_lang('NotHavePermission').'</font>';
  2154. } */
  2155. } elseif (is_dir($path)) {
  2156. if (!is_dir($dest.'/'.$file)) {
  2157. mkdir($dest.'/'.$file);
  2158. }
  2159. self:: allow_create_all_directory($path, $dest.'/'.$file, $overwrite);
  2160. }
  2161. }
  2162. }
  2163. closedir($handle);
  2164. }
  2165. }
  2166. /**
  2167. * Gets the new ID of one specific tool item from the tool name and the old ID
  2168. * @param string Tool name
  2169. * @param integer Old ID
  2170. * @return integer New ID
  2171. */
  2172. function get_new_id($tool, $ref)
  2173. {
  2174. //transform $tool into one backup/restore constant
  2175. //just in case you copy the tool in the same course
  2176. //error_log($this->course_origin_id .' - '.$this->destination_course_id);
  2177. if ($this->course_origin_id == $this->destination_course_id) {
  2178. return $ref;
  2179. }
  2180. if ($tool == 'hotpotatoes') {
  2181. $tool = 'document';
  2182. }
  2183. if (!empty($this->course->resources[$tool][$ref]->destination_id)) {
  2184. return $this->course->resources[$tool][$ref]->destination_id;
  2185. }
  2186. return '';
  2187. }
  2188. /**
  2189. * Restore glossary
  2190. */
  2191. function restore_glossary($session_id = 0)
  2192. {
  2193. if ($this->course->has_resources(RESOURCE_GLOSSARY)) {
  2194. $table_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  2195. $t_item_propery = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  2196. $resources = $this->course->resources;
  2197. foreach ($resources[RESOURCE_GLOSSARY] as $id => $glossary) {
  2198. $condition_session = "";
  2199. if (!empty($session_id)) {
  2200. $session_id = intval($session_id);
  2201. $condition_session = " , session_id = '$session_id' ";
  2202. }
  2203. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  2204. $glossary->description = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  2205. $glossary->description,
  2206. $this->course->code,
  2207. $this->course->destination_path,
  2208. $this->course->backup_path,
  2209. $this->course->info['path']
  2210. );
  2211. $sql = "INSERT INTO ".$table_glossary." SET c_id = ".$this->destination_course_id." , name = '".self::DBUTF8escapestring(
  2212. $glossary->name
  2213. )."', description = '".self::DBUTF8escapestring(
  2214. $glossary->description
  2215. )."', display_order='".self::DBUTF8escapestring($glossary->display_order)."' $condition_session ";
  2216. Database::query($sql);
  2217. $my_id = Database::insert_id();
  2218. api_item_property_update(
  2219. $this->destination_course_info,
  2220. TOOL_GLOSSARY,
  2221. $my_id,
  2222. "GlossaryAdded",
  2223. api_get_user_id()
  2224. );
  2225. $this->course->resources[RESOURCE_GLOSSARY][$id]->destination_id = Database::insert_id();
  2226. }
  2227. }
  2228. }
  2229. function restore_wiki($session_id = 0)
  2230. {
  2231. if ($this->course->has_resources(RESOURCE_WIKI)) {
  2232. // wiki table of the target course
  2233. $table_wiki = Database :: get_course_table('wiki');
  2234. $table_wiki_conf = Database :: get_course_table('wiki_conf');
  2235. // storing all the resources that have to be copied in an array
  2236. $resources = $this->course->resources;
  2237. foreach ($resources[RESOURCE_WIKI] as $id => $wiki) {
  2238. //$wiki = new Wiki($obj->page_id, $obj->reflink, $obj->title, $obj->content, $obj->user_id, $obj->group_id, $obj->dtime);
  2239. // the sql statement to insert the groups from the old course to the new course
  2240. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  2241. $wiki->content = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  2242. $wiki->content,
  2243. $this->course->code,
  2244. $this->course->destination_path,
  2245. $this->course->backup_path,
  2246. $this->course->info['path']
  2247. );
  2248. $sql = "INSERT INTO $table_wiki (c_id, page_id, reflink, title, content, user_id, group_id, dtime, progress, version, session_id) VALUES (
  2249. ".$this->destination_course_id." ,
  2250. '".self::DBUTF8escapestring($wiki->page_id)."',
  2251. '".self::DBUTF8escapestring($wiki->reflink)."',
  2252. '".self::DBUTF8escapestring($wiki->title)."',
  2253. '".self::DBUTF8escapestring($wiki->content)."',
  2254. '".intval($wiki->user_id)."',
  2255. '".intval($wiki->group_id)."',
  2256. '".self::DBUTF8escapestring($wiki->dtime)."',
  2257. '".self::DBUTF8escapestring($wiki->progress)."',
  2258. '".intval($wiki->version)."',
  2259. '".(!empty($session_id) ? intval($session_id) : 0)."')";
  2260. $rs2 = Database::query($sql);
  2261. $new_id = Database::insert_id();
  2262. $this->course->resources[RESOURCE_WIKI][$id]->destination_id = $new_id;
  2263. $sql = "UPDATE $table_wiki set page_id = '$new_id' WHERE c_id = ".$this->destination_course_id." AND id = '$new_id'";
  2264. Database::query($sql);
  2265. // we also add an entry in wiki_conf
  2266. $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
  2267. (".$this->destination_course_id." , '".intval(
  2268. $new_id
  2269. )."', '', '', '', '', '', '', '', NULL, 0, 0, '0000-00-00 00:00:00', '0000-00-00 00:00:00', 0)";
  2270. $rs1 = Database::query($sql);
  2271. }
  2272. }
  2273. }
  2274. /**
  2275. * Restore Thematics
  2276. */
  2277. function restore_thematic($session_id = 0)
  2278. {
  2279. if ($this->course->has_resources(RESOURCE_THEMATIC)) {
  2280. $table_thematic = Database :: get_course_table(TABLE_THEMATIC);
  2281. $table_thematic_advance = Database :: get_course_table(TABLE_THEMATIC_ADVANCE);
  2282. $table_thematic_plan = Database :: get_course_table(TABLE_THEMATIC_PLAN);
  2283. $resources = $this->course->resources;
  2284. foreach ($resources[RESOURCE_THEMATIC] as $id => $thematic) {
  2285. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  2286. $thematic->content = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  2287. $thematic->content,
  2288. $this->course->code,
  2289. $this->course->destination_path,
  2290. $this->course->backup_path,
  2291. $this->course->info['path']
  2292. );
  2293. $thematic->params['c_id'] = $this->destination_course_id;
  2294. $thematic->params['id'] = null;
  2295. $last_id = Database::insert($table_thematic, $thematic->params, false);
  2296. if (is_numeric($last_id)) {
  2297. api_item_property_update(
  2298. $this->destination_course_info,
  2299. 'thematic',
  2300. $last_id,
  2301. "ThematicAdded",
  2302. api_get_user_id()
  2303. );
  2304. foreach ($thematic->thematic_advance_list as $thematic_advance) {
  2305. unset($thematic_advance['id']);
  2306. $thematic_advance['attendance_id'] = 0;
  2307. $thematic_advance['thematic_id'] = $last_id;
  2308. $thematic_advance['c_id'] = $this->destination_course_id;
  2309. $my_id = Database::insert($table_thematic_advance, $thematic_advance, false);
  2310. if (is_numeric($my_id)) {
  2311. api_item_property_update(
  2312. $this->destination_course_info,
  2313. 'thematic_advance',
  2314. $my_id,
  2315. "ThematicAdvanceAdded",
  2316. api_get_user_id()
  2317. );
  2318. }
  2319. }
  2320. foreach ($thematic->thematic_plan_list as $thematic_plan) {
  2321. unset($thematic_plan['id']);
  2322. $thematic_plan['thematic_id'] = $last_id;
  2323. $thematic_plan['c_id'] = $this->destination_course_id;
  2324. $my_id = Database::insert($table_thematic_plan, $thematic_plan, false);
  2325. if (is_numeric($my_id)) {
  2326. api_item_property_update(
  2327. $this->destination_course_info,
  2328. 'thematic_plan',
  2329. $my_id,
  2330. "ThematicPlanAdded",
  2331. api_get_user_id()
  2332. );
  2333. }
  2334. }
  2335. }
  2336. }
  2337. }
  2338. }
  2339. /**
  2340. * Restore Attendance
  2341. */
  2342. function restore_attendance($session_id = 0)
  2343. {
  2344. if ($this->course->has_resources(RESOURCE_ATTENDANCE)) {
  2345. $table_attendance = Database :: get_course_table(TABLE_ATTENDANCE);
  2346. $table_attendance_calendar = Database :: get_course_table(TABLE_ATTENDANCE_CALENDAR);
  2347. $resources = $this->course->resources;
  2348. foreach ($resources[RESOURCE_ATTENDANCE] as $id => $obj) {
  2349. // check resources inside html from fckeditor tool and copy correct urls into recipient course
  2350. $obj->params['description'] = DocumentManager::replace_urls_inside_content_html_from_copy_course(
  2351. $obj->params['description'],
  2352. $this->course->code,
  2353. $this->course->destination_path,
  2354. $this->course->backup_path,
  2355. $this->course->info['path']
  2356. );
  2357. $obj->params['id'] = null;
  2358. $obj->params['c_id'] = $this->destination_course_id;
  2359. $last_id = Database::insert($table_attendance, $obj->params);
  2360. if (is_numeric($last_id)) {
  2361. api_item_property_update(
  2362. $this->destination_course_info,
  2363. TOOL_ATTENDANCE,
  2364. $last_id,
  2365. "AttendanceAdded",
  2366. api_get_user_id()
  2367. );
  2368. foreach ($obj->attendance_calendar as $attendance_calendar) {
  2369. unset($attendance_calendar['id']);
  2370. $attendance_calendar['attendance_id'] = $last_id;
  2371. $attendance_calendar['c_id'] = $this->destination_course_id;
  2372. $my_id = Database::insert($table_attendance_calendar, $attendance_calendar);
  2373. }
  2374. }
  2375. }
  2376. }
  2377. }
  2378. function DBUTF8($str)
  2379. {
  2380. if (UTF8_CONVERT) {
  2381. $str = utf8_encode($str);
  2382. }
  2383. return $str;
  2384. }
  2385. function DBUTF8escapestring($str)
  2386. {
  2387. if (UTF8_CONVERT) {
  2388. $str = utf8_encode($str);
  2389. }
  2390. return Database::escape_string($str);
  2391. }
  2392. function DBUTF8_array($array)
  2393. {
  2394. if (UTF8_CONVERT) {
  2395. foreach ($array as &$item) {
  2396. $item = utf8_encode($item);
  2397. }
  2398. return $array;
  2399. } else {
  2400. return $array;
  2401. }
  2402. }
  2403. }