CourseRestorer.class.php 120 KB

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