CourseRestorer.class.php 109 KB

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