CourseRestorer.class.php 141 KB

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