copy_course_session.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. $attrs = '';
  44. if (count($attr) > 0) {
  45. foreach ($attr as $key => $value) {
  46. $attrs .= ' '.$key.'="'.$value.'"';
  47. }
  48. }
  49. $output = '<select name="'.$name.'" '.$attrs.'>';
  50. if (count($sessions) == 0) {
  51. $output .= '<option value = "0">'.get_lang('ThereIsNotStillASession').'</option>';
  52. } else {
  53. $output .= '<option value = "0">'.get_lang('SelectASession').'</option>';
  54. }
  55. if (is_array($sessions)) {
  56. foreach ($sessions as $session) {
  57. $category_name = '';
  58. if (!empty($session['category_name'])) {
  59. $category_name = ' ('.$session['category_name'].')';
  60. }
  61. $output .= '<option value="'.$session['id'].'">'.$session['name'].' '.$category_name.'</option>';
  62. }
  63. }
  64. $output .= '</select>';
  65. return $output;
  66. }
  67. function display_form() {
  68. $html = '';
  69. $sessions = SessionManager::get_sessions_list(null, array('name ASC'));
  70. // Actions
  71. $html .= '<div class="actions">';
  72. // Link back to the documents overview
  73. $html .= '<a href="../admin/index.php">'.Display::return_icon(
  74. 'back.png',
  75. get_lang('BackTo').' '.get_lang('PlatformAdmin'),
  76. '',
  77. ICON_SIZE_MEDIUM
  78. ).'</a>';
  79. $html .= '</div>';
  80. $html .= Display::return_message(get_lang('CopyCourseFromSessionToSessionExplanation'));
  81. $html .= '<form name="formulaire" method="post" action="'.api_get_self().'" >';
  82. $html .= '<table border="0" cellpadding="5" cellspacing="0" width="100%">';
  83. // origin
  84. $html .= '<tr><td width="15%"><b>'.get_lang('OriginCoursesFromSession').':</b></td>';
  85. $html .= '<td width="10%" align="left">'.make_select_session_list(
  86. 'sessions_list_origin',
  87. $sessions,
  88. array('onchange' => 'javascript: xajax_search_courses(this.value,\'origin\');')
  89. ).'</td>';
  90. $html .= '<td width="50%"><div id="ajax_list_courses_origin">';
  91. $html .= '<select id="origin" name="SessionCoursesListOrigin[]" style="width:380px;"></select></div></td></tr>';
  92. //destination
  93. $html .= '<tr><td width="15%"><b>'.get_lang('DestinationCoursesFromSession').':</b></td>';
  94. $html .= '<td width="10%" align="left"><div id="ajax_sessions_list_destination">';
  95. $html .= '<select name="sessions_list_destination" onchange="javascript: xajax_search_courses(this.value,\'destination\');">';
  96. $html .= '<option value = "0">'.get_lang('ThereIsNotStillASession').'</option></select ></div></td>';
  97. $html .= '<td width="50%">';
  98. $html .= '<div id="ajax_list_courses_destination">';
  99. $html .= '<select id="destination" name="SessionCoursesListDestination[]" style="width:380px;" ></select></div></td>';
  100. $html .= '</tr></table>';
  101. $html .= '<h3>'.get_lang('TypeOfCopy').'</h3>';
  102. $html .= '<label class="radio"><input type="radio" id="copy_option_1" name="copy_option" value="full_copy" checked="checked"/>';
  103. $html .= get_lang('FullCopy').'</label><br/>';
  104. $html .= '<label class="radio"><input type="radio" id="copy_option_2" name="copy_option" value="select_items" disabled="disabled"/>';
  105. $html .= ' '.get_lang('LetMeSelectItems').'</label><br/>';
  106. $html .= '<label class="checkbox"><input type="checkbox" id="copy_base_content_id" name="copy_only_session_items" />'.get_lang(
  107. 'CopyOnlySessionItems'
  108. ).'</label><br /><br/>';
  109. $html .= '<button class="save" type="submit" onclick="javascript:if(!confirm('."'".addslashes(
  110. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES)
  111. )."'".')) return false;">'.get_lang('CopyCourse').'</button>';
  112. $html .= '</form>';
  113. echo $html;
  114. }
  115. function search_courses($id_session, $type) {
  116. global $tbl_course, $tbl_session_rel_course, $course_list;
  117. $xajax_response = new XajaxResponse();
  118. $select_destination = '';
  119. if (!empty($type)) {
  120. $id_session = intval($id_session);
  121. if ($type == 'origin') {
  122. $course_list = SessionManager::get_course_list_by_session_id($id_session);
  123. $temp_course_list = array();
  124. $return = '<select id="origin" name="SessionCoursesListOrigin[]" style="width:380px;" onclick="javascript: checkSelected(this.id,\'copy_option_2\',\'title_option2\',\'destination\');">';
  125. foreach ($course_list as $course) {
  126. $temp_course_list[] = "'{$course['code']}'";
  127. $course_title = str_replace("'", "\'", $course_title);
  128. $return .= '<option value="'.$course['code'].'" title="'.@htmlspecialchars(
  129. $course['title'].' ('.$course['visual_code'].')',
  130. ENT_QUOTES,
  131. api_get_system_encoding()
  132. ).'">'.$course['title'].' ('.$course['visual_code'].')</option>';
  133. }
  134. $return .= '</select>';
  135. $_SESSION['course_list'] = $temp_course_list;
  136. $_SESSION['session_origin'] = $id_session;
  137. // Build select for destination sessions where is not included current session from select origin
  138. if (!empty($id_session)) {
  139. $sessions = SessionManager::get_sessions_list(null, array('name ASC'));
  140. $select_destination .= '<select name="sessions_list_destination" width="380px" onchange = "javascript: xajax_search_courses(this.value,\'destination\');">';
  141. $select_destination .= '<option value = "0">-- '.get_lang('SelectASession').' --</option>';
  142. foreach ($sessions as $session) {
  143. if ($id_session == $session['id']) {
  144. continue;
  145. }
  146. ;
  147. if (!empty($session['category_name'])) {
  148. $session['category_name'] = ' ('.$session['category_name'].') ';
  149. }
  150. $select_destination .= '<option value="'.$session['id'].'">'.$session['name'].' '.$session['category_name'].'</option>';
  151. }
  152. $select_destination .= '</select>';
  153. $xajax_response->addAssign(
  154. 'ajax_sessions_list_destination',
  155. 'innerHTML',
  156. api_utf8_encode($select_destination)
  157. );
  158. } else {
  159. $select_destination .= '<select name="sessions_list_destination" width="380px" onchange = "javascript: xajax_search_courses(this.value,\'destination\');">';
  160. $select_destination .= '<option value = "0">'.get_lang('ThereIsNotStillASession').'</option>';
  161. $select_destination .= '</select>';
  162. $xajax_response->addAssign(
  163. 'ajax_sessions_list_destination',
  164. 'innerHTML',
  165. api_utf8_encode($select_destination)
  166. );
  167. }
  168. // Select multiple destination empty
  169. $select_multiple_empty = '<select id="destination" name="SessionCoursesListDestination[]" style="width:380px;"></select>';
  170. // Send response by ajax
  171. $xajax_response->addAssign('ajax_list_courses_origin', 'innerHTML', api_utf8_encode($return));
  172. $xajax_response->addAssign(
  173. 'ajax_list_courses_destination',
  174. 'innerHTML',
  175. api_utf8_encode($select_multiple_empty)
  176. );
  177. } else {
  178. //Left Select - Destination
  179. // Search courses by id_session where course codes is include en courses list destination
  180. $sql = "SELECT c.code, c.visual_code, c.title, src.id_session
  181. FROM $tbl_course c, $tbl_session_rel_course src
  182. WHERE src.c_id = c.id
  183. AND src.id_session = '".intval($id_session)."'";
  184. //AND c.code IN ($list_courses_origin)";
  185. $rs = Database::query($sql);
  186. $course_list_destination = array();
  187. $return = '<select id="destination" name="SessionCoursesListDestination[]" style="width:380px;" >';
  188. while ($course = Database :: fetch_array($rs)) {
  189. $course_list_destination[] = $course['c_id'];
  190. $course_title = str_replace("'", "\'", $course_title);
  191. $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>';
  192. }
  193. $return .= '</select>';
  194. $_SESSION['course_list_destination'] = $course_list_destination;
  195. // Send response by ajax
  196. $xajax_response->addAssign('ajax_list_courses_destination', 'innerHTML', api_utf8_encode($return));
  197. }
  198. }
  199. return $xajax_response;
  200. }
  201. $xajax->processRequests();
  202. /* HTML head extra */
  203. $htmlHeadXtra[] = $xajax->getJavascript(api_get_path(WEB_LIBRARY_PATH).'xajax/');
  204. $htmlHeadXtra[] = '<script>
  205. function checkSelected(id_select,id_radio,id_title,id_destination) {
  206. var num=0;
  207. obj_origin = document.getElementById(id_select);
  208. obj_destination = document.getElementById(id_destination);
  209. for (x=0;x<obj_origin.options.length;x++) {
  210. if (obj_origin.options[x].selected) {
  211. if (obj_destination.options.length > 0) {
  212. for (y=0;y<obj_destination.options.length;y++) {
  213. if (obj_origin.options[x].value == obj_destination.options[y].value) {
  214. obj_destination.options[y].selected = true;
  215. }
  216. }
  217. }
  218. num++;
  219. } else {
  220. if (obj_destination.options.length > 0) {
  221. for (y=0;y<obj_destination.options.length;y++) {
  222. if (obj_origin.options[x].value == obj_destination.options[y].value) {
  223. obj_destination.options[y].selected = false;
  224. }
  225. }
  226. }
  227. }
  228. }
  229. if (num == 1) {
  230. document.getElementById(id_radio).disabled = false;
  231. document.getElementById(id_title).style.color = \'#000\';
  232. } else {
  233. document.getElementById(id_radio).disabled = true;
  234. document.getElementById(id_title).style.color = \'#aaa\';
  235. }
  236. }
  237. </script>';
  238. Display::display_header($nameTools);
  239. $with_base_content = true;
  240. if (isset($_POST['copy_only_session_items']) && $_POST['copy_only_session_items']) {
  241. $with_base_content = false;
  242. }
  243. /* MAIN CODE */
  244. if ((isset($_POST['action']) && $_POST['action'] == 'course_select_form') || (isset($_POST['copy_option']) && $_POST['copy_option'] == 'full_copy')) {
  245. $destination_course = $origin_course = $destination_session = $origin_session = '';
  246. if (isset ($_POST['action']) && $_POST['action'] == 'course_select_form') {
  247. $destination_course = $_POST['destination_course'];
  248. $origin_course = $_POST['origin_course'];
  249. $destination_session = $_POST['destination_session'];
  250. $origin_session = $_POST['origin_session'];
  251. $course = CourseSelectForm :: get_posted_course('copy_course', $origin_session, $origin_course);
  252. $cr = new CourseRestorer($course);
  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. $courseDestinationInfo = api_get_course_info_by_id($course_destinatination);
  283. $course_destinatination = $courseDestinationInfo['code'];
  284. $course_origin = api_get_course_info($course_code);
  285. $cb = new CourseBuilder('', $course_origin);
  286. $course = $cb->build($origin_session, $course_code, $with_base_content);
  287. $cr = new CourseRestorer($course);
  288. $cr->restore($course_destinatination, $destination_session);
  289. }
  290. Display::display_confirmation_message(get_lang('CopyFinished'));
  291. display_form();
  292. } else {
  293. Display::display_error_message(get_lang('YouMustSelectACourseFromOriginalSession'));
  294. display_form();
  295. }
  296. }
  297. } elseif (isset($_POST['copy_option']) && $_POST['copy_option'] == 'select_items') {
  298. // Else, if a CourseSelectForm is requested, show it
  299. if (api_get_setting('show_glossary_in_documents') != 'none') {
  300. Display::display_normal_message(get_lang('ToExportDocumentsWithGlossaryYouHaveToSelectGlossary'));
  301. }
  302. $arr_course_origin = array();
  303. $arr_course_destination = array();
  304. $destination_session = '';
  305. $origin_session = '';
  306. if (isset($_POST['SessionCoursesListOrigin'])) {
  307. $arr_course_origin = $_POST['SessionCoursesListOrigin'];
  308. }
  309. if (isset($_POST['SessionCoursesListDestination'])) {
  310. $arr_course_destination = $_POST['SessionCoursesListDestination'];
  311. }
  312. if (isset($_POST['sessions_list_destination'])) {
  313. $destination_session = $_POST['sessions_list_destination'];
  314. }
  315. if (isset($_POST['sessions_list_origin'])) {
  316. $origin_session = $_POST['sessions_list_origin'];
  317. }
  318. if ((is_array($arr_course_origin) && count($arr_course_origin) > 0) && !empty($destination_session)) {
  319. Display::display_normal_message(get_lang('ToExportLearnpathWithQuizYouHaveToSelectQuiz'));
  320. $course_origin = api_get_course_info($arr_course_origin[0]);
  321. $cb = new CourseBuilder('', $course_origin);
  322. $course = $cb->build($origin_session, $arr_course_origin[0], $with_base_content);
  323. //$hidden_fields['same_file_name_option'] = $_POST['same_file_name_option'];
  324. $hidden_fields['destination_course'] = $arr_course_destination[0];
  325. $hidden_fields['origin_course'] = $arr_course_origin[0];
  326. $hidden_fields['destination_session'] = $destination_session;
  327. $hidden_fields['origin_session'] = $origin_session;
  328. CourseSelectForm :: display_form($course, $hidden_fields, true);
  329. echo '<div style="float:right"><a href="javascript:window.back();">'.Display::return_icon(
  330. 'back.png',
  331. get_lang('Back').' '.get_lang('To').' '.get_lang('PlatformAdmin'),
  332. array('style' => 'vertical-align:middle')
  333. ).get_lang('Back').'</a></div>';
  334. } else {
  335. Display::display_error_message(
  336. get_lang('You must select a course from original session and select a destination session')
  337. );
  338. display_form();
  339. }
  340. } else {
  341. display_form();
  342. }
  343. Display::display_footer();