copy_course_session.php 17 KB

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