CourseRestorer.class.php 119 KB

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