CourseRestorer.class.php 147 KB

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