copy_course_session.php 17 KB

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