CourseRestorer.class.php 141 KB

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