copy_course_session.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Copy resources from one course in a session to another one.
  5. *
  6. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  7. * @author Julio Montoya <gugli100@gmail.com> Lots of bug fixes/improvements
  8. * @package chamilo.backup
  9. */
  10. /**
  11. * Code
  12. */
  13. /* INIT SECTION */
  14. // Language files that need to be included
  15. $language_file = array('coursebackup', 'admin');
  16. $cidReset = true;
  17. ////require_once '../inc/global.inc.php';
  18. $current_course_tool = TOOL_COURSE_MAINTENANCE;
  19. api_protect_course_script(true, true);
  20. require_once api_get_path(LIBRARY_PATH).'xajax/xajax.inc.php';
  21. require_once 'classes/CourseBuilder.class.php';
  22. require_once 'classes/CourseRestorer.class.php';
  23. require_once 'classes/CourseSelectForm.class.php';
  24. $xajax = new xajax();
  25. $xajax->registerFunction('search_courses');
  26. if (!api_is_allowed_to_edit() && !api_is_session_admin()) {
  27. api_not_allowed(true);
  28. }
  29. // Remove memory and time limits as much as possible as this might be a long process...
  30. if (function_exists('ini_set')) {
  31. api_set_memory_limit('256M');
  32. ini_set('max_execution_time', 1800);
  33. }
  34. $this_section = SECTION_PLATFORM_ADMIN;
  35. $nameTools = get_lang('CopyCourse');
  36. $interbreadcrumb[] = array('url' => '../admin/index.php', 'name' => get_lang('PlatformAdmin'));
  37. // Database Table Definitions
  38. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  39. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  40. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  41. /* FUNCTIONS */
  42. function make_select_session_list($name, $sessions, $attr = array())
  43. {
  44. $attrs = '';
  45. if (count($attr) > 0) {
  46. foreach ($attr as $key => $value) {
  47. $attrs .= ' '.$key.'="'.$value.'"';
  48. }
  49. }
  50. $output = '<select name="'.$name.'" '.$attrs.'>';
  51. if (count($sessions) == 0) {
  52. $output .= '<option value = "0">'.get_lang('ThereIsNotStillASession').'</option>';
  53. } else {
  54. $output .= '<option value = "0">'.get_lang('SelectASession').'</option>';
  55. }
  56. if (is_array($sessions)) {
  57. foreach ($sessions as $session) {
  58. $categoryName = '';
  59. if (!empty($session['category_name'])) {
  60. $categoryName = ' ('.$session['category_name'].')';
  61. }
  62. $output .= '<option value="'.$session['id'].'">'.
  63. $session['name'].' '.$categoryName.
  64. '</option>';
  65. }
  66. }
  67. $output .= '</select>';
  68. return $output;
  69. }
  70. function display_form()
  71. {
  72. $html = '';
  73. $sessions = SessionManager::get_sessions_list(null, array('name ASC'));
  74. // Actions
  75. $html .= '<div class="actions">';
  76. // Link back to the documents overview
  77. $html .= '<a href="../admin/index.php">'.Display::return_icon(
  78. 'back.png',
  79. get_lang('BackTo').' '.get_lang('PlatformAdmin'),
  80. '',
  81. ICON_SIZE_MEDIUM
  82. ).'</a>';
  83. $html .= '</div>';
  84. $html .= Display::return_message(get_lang('CopyCourseFromSessionToSessionExplanation'));
  85. $html .= '<form name="formulaire" method="post" action="'.api_get_self().'" >';
  86. $html .= '<table border="0" cellpadding="5" cellspacing="0" width="100%">';
  87. // origin
  88. $html .= '<tr><td width="15%"><b>'.get_lang('OriginCoursesFromSession').':</b></td>';
  89. $html .= '<td width="10%" align="left">'.make_select_session_list(
  90. 'sessions_list_origin',
  91. $sessions,
  92. array('onchange' => 'javascript: xajax_search_courses(this.value,\'origin\');')
  93. ).'</td>';
  94. $html .= '<td width="50%"><div id="ajax_list_courses_origin">';
  95. $html .= '<select id="origin" name="SessionCoursesListOrigin[]" style="width:380px;"></select></div></td></tr>';
  96. //destination
  97. $html .= '<tr><td width="15%"><b>'.get_lang('DestinationCoursesFromSession').':</b></td>';
  98. $html .= '<td width="10%" align="left"><div id="ajax_sessions_list_destination">';
  99. $html .= '<select name="sessions_list_destination" onchange="javascript: xajax_search_courses(this.value,\'destination\');">';
  100. $html .= '<option value = "0">'.get_lang('ThereIsNotStillASession').'</option></select ></div></td>';
  101. $html .= '<td width="50%">';
  102. $html .= '<div id="ajax_list_courses_destination">';
  103. $html .= '<select id="destination" name="SessionCoursesListDestination[]" style="width:380px;" ></select></div></td>';
  104. $html .= '</tr></table>';
  105. $html .= '<h3>'.get_lang('TypeOfCopy').'</h3>';
  106. $html .= '<label class="radio"><input type="radio" id="copy_option_1" name="copy_option" value="full_copy" checked="checked"/>';
  107. $html .= get_lang('FullCopy').'</label><br/>';
  108. $html .= '<label class="radio"><input type="radio" id="copy_option_2" name="copy_option" value="select_items" disabled="disabled"/>';
  109. $html .= ' '.get_lang('LetMeSelectItems').'</label><br/>';
  110. $html .= '<label class="checkbox"><input type="checkbox" id="copy_base_content_id" name="copy_only_session_items" />'.get_lang(
  111. 'CopyOnlySessionItems'
  112. ).'</label><br /><br/>';
  113. $html .= '<button class="save" type="submit" onclick="javascript:if(!confirm('."'".addslashes(
  114. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES)
  115. )."'".')) return false;">'.get_lang('CopyCourse').'</button>';
  116. $html .= '</form>';
  117. echo $html;
  118. }
  119. function search_courses($id_session, $type)
  120. {
  121. global $tbl_course, $tbl_session_rel_course, $course_list;
  122. $xajax_response = new XajaxResponse();
  123. $select_destination = '';
  124. if (!empty($type)) {
  125. $id_session = intval($id_session);
  126. if ($type == 'origin') {
  127. $course_list = SessionManager::get_course_list_by_session_id($id_session);
  128. $temp_course_list = array();
  129. $return = '<select id="origin" name="SessionCoursesListOrigin[]" style="width:380px;" onclick="javascript: checkSelected(this.id,\'copy_option_2\',\'title_option2\',\'destination\');">';
  130. foreach ($course_list as $course) {
  131. $temp_course_list[] = "'{$course['code']}'";
  132. $course_title = str_replace("'", "\'", $course_title);
  133. $return .= '<option value="'.$course['code'].'" title="'.@htmlspecialchars(
  134. $course['title'].' ('.$course['visual_code'].')',
  135. ENT_QUOTES,
  136. api_get_system_encoding()
  137. ).'">'.$course['title'].' ('.$course['visual_code'].')</option>';
  138. }
  139. $return .= '</select>';
  140. $_SESSION['course_list'] = $temp_course_list;
  141. $_SESSION['session_origin'] = $id_session;
  142. // Build select for destination sessions where is not included current session from select origin
  143. if (!empty($id_session)) {
  144. $sessions = SessionManager::get_sessions_list(null, array('name ASC'));
  145. $select_destination .= '<select name="sessions_list_destination" width="380px" onchange = "javascript: xajax_search_courses(this.value,\'destination\');">';
  146. $select_destination .= '<option value = "0">-- '.get_lang('SelectASession').' --</option>';
  147. foreach ($sessions as $session) {
  148. if ($id_session == $session['id']) {
  149. continue;
  150. }
  151. ;
  152. if (!empty($session['category_name'])) {
  153. $session['category_name'] = ' ('.$session['category_name'].') ';
  154. }
  155. $select_destination .= '<option value="'.$session['id'].'">'.$session['name'].' '.$session['category_name'].'</option>';
  156. }
  157. $select_destination .= '</select>';
  158. $xajax_response->addAssign(
  159. 'ajax_sessions_list_destination',
  160. 'innerHTML',
  161. api_utf8_encode($select_destination)
  162. );
  163. } else {
  164. $select_destination .= '<select name="sessions_list_destination" width="380px" onchange = "javascript: xajax_search_courses(this.value,\'destination\');">';
  165. $select_destination .= '<option value = "0">'.get_lang('ThereIsNotStillASession').'</option>';
  166. $select_destination .= '</select>';
  167. $xajax_response->addAssign(
  168. 'ajax_sessions_list_destination',
  169. 'innerHTML',
  170. api_utf8_encode($select_destination)
  171. );
  172. }
  173. // Select multiple destination empty
  174. $select_multiple_empty = '<select id="destination" name="SessionCoursesListDestination[]" style="width:380px;"></select>';
  175. // Send response by ajax
  176. $xajax_response->addAssign('ajax_list_courses_origin', 'innerHTML', api_utf8_encode($return));
  177. $xajax_response->addAssign(
  178. 'ajax_list_courses_destination',
  179. 'innerHTML',
  180. api_utf8_encode($select_multiple_empty)
  181. );
  182. } else {
  183. //Left Select - Destination
  184. // Search courses by id_session where course codes is include en courses list destination
  185. $sql = "SELECT c.code, c.visual_code, c.title, src.id_session
  186. FROM $tbl_course c, $tbl_session_rel_course src
  187. WHERE src.c_id = c.id
  188. AND src.id_session = '".intval($id_session)."'";
  189. //AND c.code IN ($list_courses_origin)";
  190. $rs = Database::query($sql);
  191. $course_list_destination = array();
  192. $return = '<select id="destination" name="SessionCoursesListDestination[]" style="width:380px;" >';
  193. while ($course = Database :: fetch_array($rs)) {
  194. $course_list_destination[] = $course['c_id'];
  195. $course_title = str_replace("'", "\'", $course_title);
  196. $return .= '<option value="'.$course['c_id'].'" title="'.@htmlspecialchars($course['title'].' ('.$course['visual_code'].')',ENT_QUOTES,api_get_system_encoding()).'">'.$course['title'].' ('.$course['visual_code'].')</option>';
  197. }
  198. $return .= '</select>';
  199. $_SESSION['course_list_destination'] = $course_list_destination;
  200. // Send response by ajax
  201. $xajax_response->addAssign('ajax_list_courses_destination', 'innerHTML', api_utf8_encode($return));
  202. }
  203. }
  204. return $xajax_response;
  205. }
  206. $xajax->processRequests();
  207. /* HTML head extra */
  208. $htmlHeadXtra[] = $xajax->getJavascript(api_get_path(WEB_LIBRARY_PATH).'xajax/');
  209. $htmlHeadXtra[] = '<script>
  210. function checkSelected(id_select,id_radio,id_title,id_destination) {
  211. var num=0;
  212. obj_origin = document.getElementById(id_select);
  213. obj_destination = document.getElementById(id_destination);
  214. for (x=0;x<obj_origin.options.length;x++) {
  215. if (obj_origin.options[x].selected) {
  216. if (obj_destination.options.length > 0) {
  217. for (y=0;y<obj_destination.options.length;y++) {
  218. if (obj_origin.options[x].value == obj_destination.options[y].value) {
  219. obj_destination.options[y].selected = true;
  220. }
  221. }
  222. }
  223. num++;
  224. } else {
  225. if (obj_destination.options.length > 0) {
  226. for (y=0;y<obj_destination.options.length;y++) {
  227. if (obj_origin.options[x].value == obj_destination.options[y].value) {
  228. obj_destination.options[y].selected = false;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. if (num == 1) {
  235. document.getElementById(id_radio).disabled = false;
  236. document.getElementById(id_title).style.color = \'#000\';
  237. } else {
  238. document.getElementById(id_radio).disabled = true;
  239. document.getElementById(id_title).style.color = \'#aaa\';
  240. }
  241. }
  242. </script>';
  243. Display::display_header($nameTools);
  244. $with_base_content = true;
  245. if (isset($_POST['copy_only_session_items']) && $_POST['copy_only_session_items']) {
  246. $with_base_content = false;
  247. }
  248. /* MAIN CODE */
  249. if ((isset($_POST['action']) && $_POST['action'] == 'course_select_form') || (isset($_POST['copy_option']) && $_POST['copy_option'] == 'full_copy')) {
  250. $destination_course = $origin_course = $destination_session = $origin_session = '';
  251. if (isset ($_POST['action']) && $_POST['action'] == 'course_select_form') {
  252. $destination_course = $_POST['destination_course'];
  253. $origin_course = $_POST['origin_course'];
  254. $destination_session = $_POST['destination_session'];
  255. $origin_session = $_POST['origin_session'];
  256. $course = CourseSelectForm :: get_posted_course('copy_course', $origin_session, $origin_course);
  257. $cr = new CourseRestorer($course);
  258. $cr->restore($destination_course, $destination_session);
  259. Display::display_confirmation_message(get_lang('CopyFinished'));
  260. display_form();
  261. } else {
  262. $arr_course_origin = array();
  263. $arr_course_destination = array();
  264. $destination_session = '';
  265. $origin_session = '';
  266. if (isset($_POST['SessionCoursesListOrigin'])) {
  267. $arr_course_origin = $_POST['SessionCoursesListOrigin'];
  268. }
  269. if (isset($_POST['SessionCoursesListDestination'])) {
  270. $arr_course_destination = $_POST['SessionCoursesListDestination'];
  271. }
  272. if (isset($_POST['sessions_list_destination'])) {
  273. $destination_session = $_POST['sessions_list_destination'];
  274. }
  275. if (isset($_POST['sessions_list_origin'])) {
  276. $origin_session = $_POST['sessions_list_origin'];
  277. }
  278. if ((is_array($arr_course_origin) && count($arr_course_origin) > 0) && !empty($destination_session)) {
  279. //We need only one value
  280. if (count($arr_course_origin) > 1 || count($arr_course_destination) > 1) {
  281. Display::display_error_message(get_lang('YouMustSelectACourseFromOriginalSession'));
  282. } else {
  283. //foreach ($arr_course_origin as $course_origin) {
  284. //first element of the array
  285. $course_code = $arr_course_origin[0];
  286. $course_destinatination = $arr_course_destination[0];
  287. $courseDestinationInfo = api_get_course_info_by_id($course_destinatination);
  288. $course_destinatination = $courseDestinationInfo['code'];
  289. $course_origin = api_get_course_info($course_code);
  290. $cb = new CourseBuilder('', $course_origin);
  291. $course = $cb->build($origin_session, $course_code, $with_base_content);
  292. $cr = new CourseRestorer($course);
  293. $cr->restore($course_destinatination, $destination_session);
  294. }
  295. Display::display_confirmation_message(get_lang('CopyFinished'));
  296. display_form();
  297. } else {
  298. Display::display_error_message(get_lang('YouMustSelectACourseFromOriginalSession'));
  299. display_form();
  300. }
  301. }
  302. } elseif (isset($_POST['copy_option']) && $_POST['copy_option'] == 'select_items') {
  303. // Else, if a CourseSelectForm is requested, show it
  304. if (api_get_setting('show_glossary_in_documents') != 'none') {
  305. Display::display_normal_message(get_lang('ToExportDocumentsWithGlossaryYouHaveToSelectGlossary'));
  306. }
  307. $arr_course_origin = array();
  308. $arr_course_destination = array();
  309. $destination_session = '';
  310. $origin_session = '';
  311. if (isset($_POST['SessionCoursesListOrigin'])) {
  312. $arr_course_origin = $_POST['SessionCoursesListOrigin'];
  313. }
  314. if (isset($_POST['SessionCoursesListDestination'])) {
  315. $arr_course_destination = $_POST['SessionCoursesListDestination'];
  316. }
  317. if (isset($_POST['sessions_list_destination'])) {
  318. $destination_session = $_POST['sessions_list_destination'];
  319. }
  320. if (isset($_POST['sessions_list_origin'])) {
  321. $origin_session = $_POST['sessions_list_origin'];
  322. }
  323. if ((is_array($arr_course_origin) && count($arr_course_origin) > 0) && !empty($destination_session)) {
  324. Display::display_normal_message(get_lang('ToExportLearnpathWithQuizYouHaveToSelectQuiz'));
  325. $course_origin = api_get_course_info($arr_course_origin[0]);
  326. $cb = new CourseBuilder('', $course_origin);
  327. $course = $cb->build($origin_session, $arr_course_origin[0], $with_base_content);
  328. //$hidden_fields['same_file_name_option'] = $_POST['same_file_name_option'];
  329. $hidden_fields['destination_course'] = $arr_course_destination[0];
  330. $hidden_fields['origin_course'] = $arr_course_origin[0];
  331. $hidden_fields['destination_session'] = $destination_session;
  332. $hidden_fields['origin_session'] = $origin_session;
  333. CourseSelectForm :: display_form($course, $hidden_fields, true);
  334. echo '<div style="float:right"><a href="javascript:window.back();">'.Display::return_icon(
  335. 'back.png',
  336. get_lang('Back').' '.get_lang('To').' '.get_lang('PlatformAdmin'),
  337. array('style' => 'vertical-align:middle')
  338. ).get_lang('Back').'</a></div>';
  339. } else {
  340. Display::display_error_message(
  341. get_lang('You must select a course from original session and select a destination session')
  342. );
  343. display_form();
  344. }
  345. } else {
  346. display_form();
  347. }
  348. Display::display_footer();