CourseRestorer.class.php 122 KB

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