CourseRestorer.class.php 122 KB

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