copy_course_session_selected.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
  4. use Chamilo\CourseBundle\Component\CourseCopy\CourseRestorer;
  5. use Chamilo\CourseBundle\Component\CourseCopy\CourseSelectForm;
  6. use ChamiloSession as Session;
  7. /**
  8. * Copy resources from one course in a session to another one.
  9. *
  10. * @author Christian Fasanando
  11. * @author Julio Montoya <gugli100@gmail.com> Lots of bug fixes/improvements
  12. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com> Code conventions
  13. *
  14. * @package chamilo.backup
  15. */
  16. require_once __DIR__.'/../inc/global.inc.php';
  17. $current_course_tool = TOOL_COURSE_MAINTENANCE;
  18. api_protect_course_script(true, true);
  19. api_set_more_memory_and_time_limits();
  20. $xajax = new xajax();
  21. $xajax->registerFunction('searchCourses');
  22. if (!api_is_allowed_to_edit()) {
  23. api_not_allowed(true);
  24. }
  25. if (!api_is_coach()) {
  26. api_not_allowed(true);
  27. }
  28. $action = isset($_POST['action']) ? $_POST['action'] : '';
  29. $courseId = api_get_course_int_id();
  30. $courseInfo = api_get_course_info_by_id($courseId);
  31. $courseCode = $courseInfo['code'];
  32. $sessionId = api_get_session_id();
  33. if (empty($courseCode) || empty($sessionId)) {
  34. api_not_allowed(true);
  35. }
  36. $this_section = SECTION_COURSES;
  37. $nameTools = get_lang('Copy course');
  38. $returnLink = api_get_path(WEB_CODE_PATH).'course_info/maintenance_coach.php?'.api_get_cidreq();
  39. $interbreadcrumb[] = [
  40. 'url' => $returnLink,
  41. 'name' => get_lang('Backup'),
  42. ];
  43. // Database Table Definitions
  44. $tbl_session_rel_course_rel_user = Database::get_main_table(
  45. TABLE_MAIN_SESSION_COURSE_USER
  46. );
  47. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  48. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  49. /**
  50. * @param string $name
  51. */
  52. function make_select_session_list($name, $sessions, $attr = [])
  53. {
  54. $attrs = '';
  55. if (count($attr) > 0) {
  56. foreach ($attr as $key => $value) {
  57. $attrs .= ' '.$key.'="'.$value.'"';
  58. }
  59. }
  60. $output = '<select name="'.$name.'" '.$attrs.'>';
  61. if (count($sessions) == 0) {
  62. $output .= '<option value = "0">'.get_lang(
  63. 'ThereIsNotStillASession'
  64. ).'</option>';
  65. } else {
  66. $output .= '<option value = "0">'.get_lang(
  67. 'SelectASession'
  68. ).'</option>';
  69. }
  70. if (is_array($sessions)) {
  71. foreach ($sessions as $session) {
  72. $category_name = '';
  73. if (!empty($session['category_name'])) {
  74. $category_name = ' ('.$session['category_name'].')';
  75. }
  76. $output .= '<option value="'.$session['id'].'">'.$session['name'].' '.$category_name.'</option>';
  77. }
  78. }
  79. $output .= '</select>';
  80. return $output;
  81. }
  82. /**
  83. * Show the form to copy courses.
  84. *
  85. * @global string $returnLink
  86. * @global string $courseCode
  87. */
  88. function displayForm()
  89. {
  90. global $returnLink, $courseCode;
  91. $courseInfo = api_get_course_info();
  92. $sessionId = api_get_session_id();
  93. $userId = api_get_user_id();
  94. $sessions = SessionManager::getSessionsCoachedByUser($userId);
  95. $html = '';
  96. // Actions
  97. $html .= '<div class="actions">';
  98. // Link back to the documents overview
  99. $html .= '<a href="'.$returnLink.'">'.Display::return_icon(
  100. 'back.png',
  101. get_lang('Back to').' '.get_lang('Backup'),
  102. '',
  103. ICON_SIZE_MEDIUM
  104. ).'</a>';
  105. $html .= '</div>';
  106. $html .= Display::return_message(
  107. get_lang('Copy courseFromSessionToSessionExplanation')
  108. );
  109. $html .= '<form name="formulaire" method="post" action="'.api_get_self().'?'.api_get_cidreq().'" >';
  110. $html .= '<table border="0" cellpadding="5" cellspacing="0" width="100%">';
  111. // Source
  112. $html .= '<tr><td width="15%"><b>'.get_lang(
  113. 'OriginCoursesFromSession'
  114. ).':</b></td>';
  115. $html .= '<td width="10%" align="left">'.api_get_session_name(
  116. $sessionId
  117. ).'</td>';
  118. $html .= '<td width="50%">';
  119. $html .= "{$courseInfo['title']} ({$courseInfo['code']})".'</td></tr>';
  120. // Destination
  121. $html .= '<tr><td width="15%"><b>'.get_lang(
  122. 'DestinationCoursesFromSession'
  123. ).':</b></td>';
  124. $html .= '<td width="10%" align="left"><div id="ajax_sessions_list_destination">';
  125. $html .= '<select name="sessions_list_destination" onchange="javascript: xajax_searchCourses(this.value,\'destination\');">';
  126. if (empty($sessions)) {
  127. $html .= '<option value = "0">'.get_lang(
  128. 'ThereIsNotStillASession'
  129. ).'</option>';
  130. } else {
  131. $html .= '<option value = "0">'.get_lang(
  132. 'SelectASession'
  133. ).'</option>';
  134. foreach ($sessions as $session) {
  135. if ($session['id'] == $sessionId) {
  136. continue;
  137. }
  138. if (!SessionManager::sessionHasCourse($session['id'], $courseCode)) {
  139. continue;
  140. }
  141. $html .= '<option value="'.$session['id'].'">'.$session['name'].'</option>';
  142. }
  143. }
  144. $html .= '</select ></div></td>';
  145. $html .= '<td width="50%">';
  146. $html .= '<div id="ajax_list_courses_destination">';
  147. $html .= '<select id="destination" name="SessionCoursesListDestination[]" style="width:380px;" ></select></div></td>';
  148. $html .= '</tr></table>';
  149. $html .= "<fieldset>";
  150. $html .= '<legend>'.get_lang('Type of copy').' <small>('.get_lang('Copy only session items').')</small></legend>';
  151. $html .= '<label class="radio"><input type="radio" id="copy_option_1" name="copy_option" value="full_copy" checked="checked"/>';
  152. $html .= get_lang('Full copy').'</label>';
  153. $html .= '<label class="radio"><input type="radio" id="copy_option_2" name="copy_option" value="select_items"/>';
  154. $html .= ' '.get_lang('Let me select learning objects').'</label><br/>';
  155. $html .= "</fieldset>";
  156. $html .= '<button class="save" type="submit" onclick="javascript:if(!confirm('."'".addslashes(
  157. api_htmlentities(get_lang('Please confirm your choice'), ENT_QUOTES)
  158. )."'".')) return false;">'.get_lang('Copy course').'</button>';
  159. $html .= '</form>';
  160. echo $html;
  161. }
  162. function searchCourses($idSession, $type)
  163. {
  164. $xajaxResponse = new xajaxResponse();
  165. $return = null;
  166. $courseCode = api_get_course_id();
  167. if (!empty($type)) {
  168. $idSession = (int) $idSession;
  169. $courseList = SessionManager::get_course_list_by_session_id($idSession);
  170. $return .= '<select id="destination" name="SessionCoursesListDestination[]" style="width:380px;" >';
  171. foreach ($courseList as $course) {
  172. $course_list_destination[] = $course['code'];
  173. if ($course['code'] != $courseCode) {
  174. continue;
  175. }
  176. $courseTitle = str_replace("'", "\'", $course['title']);
  177. $return .= '<option value="'.$course['code'].'" title="'.@htmlspecialchars(
  178. $course['title'].' ('.$course['visual_code'].')',
  179. ENT_QUOTES,
  180. api_get_system_encoding()
  181. ).'">'.
  182. $course['title'].' ('.$course['visual_code'].')</option>';
  183. }
  184. $return .= '</select>';
  185. Session::write('course_list_destination', $course_list_destination);
  186. // Send response by ajax
  187. $xajaxResponse->addAssign(
  188. 'ajax_list_courses_destination',
  189. 'innerHTML',
  190. api_utf8_encode($return)
  191. );
  192. }
  193. return $xajaxResponse;
  194. }
  195. $xajax->processRequests();
  196. $htmlHeadXtra[] = $xajax->getJavascript(
  197. api_get_path(WEB_LIBRARY_PATH).'xajax/'
  198. );
  199. $htmlHeadXtra[] = '<script>
  200. function checkSelected(id_select,id_radio,id_title,id_destination) {
  201. var num=0;
  202. obj_origin = document.getElementById(id_select);
  203. obj_destination = document.getElementById(id_destination);
  204. for (x=0;x<obj_origin.options.length;x) {
  205. if (obj_origin.options[x].selected) {
  206. if (obj_destination.options.length > 0) {
  207. for (y=0;y<obj_destination.options.length;y) {
  208. if (obj_origin.options[x].value == obj_destination.options[y].value) {
  209. obj_destination.options[y].selected = true;
  210. }
  211. }
  212. }
  213. num;
  214. } else {
  215. if (obj_destination.options.length > 0) {
  216. for (y=0;y<obj_destination.options.length;y) {
  217. if (obj_origin.options[x].value == obj_destination.options[y].value) {
  218. obj_destination.options[y].selected = false;
  219. }
  220. }
  221. }
  222. }
  223. }
  224. if (num == 1) {
  225. document.getElementById(id_radio).disabled = false;
  226. document.getElementById(id_title).style.color = \'#000\';
  227. } else {
  228. document.getElementById(id_radio).disabled = true;
  229. document.getElementById(id_title).style.color = \'#aaa\';
  230. }
  231. }
  232. </script>';
  233. Display::display_header($nameTools);
  234. /* MAIN CODE */
  235. if (($action === 'course_select_form') ||
  236. (isset($_POST['copy_option']) && $_POST['copy_option'] == 'full_copy')
  237. ) {
  238. $destinationCourse = $destinationSession = '';
  239. $originCourse = api_get_course_id();
  240. $originSession = api_get_session_id();
  241. if ($action === 'course_select_form') {
  242. $destinationCourse = $_POST['destination_course'];
  243. $destinationSession = $_POST['destination_session'];
  244. $course = CourseSelectForm::get_posted_course(
  245. 'copy_course',
  246. $originSession,
  247. $originCourse
  248. );
  249. $cr = new CourseRestorer($course);
  250. $cr->restore($destinationCourse, $destinationSession);
  251. echo Display::return_message(get_lang('Copying is finished'), 'confirmation');
  252. displayForm();
  253. } else {
  254. $arrCourseOrigin = [];
  255. $arrCourseDestination = [];
  256. $destinationSession = '';
  257. if (isset($_POST['SessionCoursesListDestination'])) {
  258. $arrCourseDestination = $_POST['SessionCoursesListDestination'];
  259. if (!empty($arrCourseDestination)) {
  260. $arrCourseOrigin = SessionManager::get_course_list_by_session_id(
  261. api_get_session_id(),
  262. $courseInfo['title']
  263. );
  264. }
  265. }
  266. if (isset($_POST['sessions_list_destination'])) {
  267. $destinationSession = $_POST['sessions_list_destination'];
  268. }
  269. if ((is_array($arrCourseOrigin) && count($arrCourseOrigin) > 0) && !empty($destinationSession)) {
  270. //We need only one value
  271. if (count($arrCourseOrigin) > 1 || count($arrCourseDestination) > 1) {
  272. echo Display::return_message(
  273. get_lang('You must select a course from original session'),
  274. 'error'
  275. );
  276. } else {
  277. $courseDestination = $arrCourseDestination[0];
  278. $cb = new CourseBuilder('', $courseInfo);
  279. $course = $cb->build(
  280. $originSession,
  281. $courseCode
  282. );
  283. $cr = new CourseRestorer($course);
  284. $cr->restore($courseDestination, $destinationSession);
  285. echo Display::return_message(get_lang('Copying is finished'), 'confirmation');
  286. }
  287. displayForm();
  288. } else {
  289. echo Display::return_message(
  290. get_lang('You must select a course from original session'),
  291. 'error'
  292. );
  293. displayForm();
  294. }
  295. }
  296. } elseif (isset($_POST['copy_option']) && $_POST['copy_option'] == 'select_items') {
  297. // Else, if a CourseSelectForm is requested, show it
  298. if (api_get_setting('show_glossary_in_documents') != 'none') {
  299. echo Display::return_message(
  300. get_lang('To export a document that has glossary terms with its references to the glossary, you have to make sure you include the glossary tool in the export'),
  301. 'normal'
  302. );
  303. }
  304. $arrCourseDestination = [];
  305. $destinationSession = '';
  306. if (isset($_POST['SessionCoursesListDestination'])) {
  307. $arrCourseDestination = $_POST['SessionCoursesListDestination'];
  308. }
  309. if (isset($_POST['sessions_list_destination'])) {
  310. $destinationSession = $_POST['sessions_list_destination'];
  311. }
  312. if (!empty($destinationSession)) {
  313. echo Display::return_message(
  314. get_lang('If you want to export a course containing a test, you have to make sure the corresponding tests are included in the export, so you have to select them in the list of tests.'),
  315. 'normal'
  316. );
  317. $cb = new CourseBuilder('', $courseInfo);
  318. $course = $cb->build($sessionId, $courseCode);
  319. $hiddenFields['destination_course'] = $arrCourseDestination[0];
  320. $hiddenFields['destination_session'] = $destinationSession;
  321. $hiddenFields['origin_course'] = api_get_course_id();
  322. $hiddenFields['origin_session'] = api_get_session_id();
  323. CourseSelectForm :: display_form($course, $hiddenFields, true);
  324. echo '<div style="float:right"><a href="javascript:window.history.go(-1);">'.
  325. Display::return_icon(
  326. 'back.png',
  327. get_lang('Back').' '.get_lang('To').' '.get_lang(
  328. 'PlatformAdmin'
  329. ),
  330. ['style' => 'vertical-align:middle']
  331. ).
  332. get_lang('Back').'</a></div>';
  333. } else {
  334. echo Display::return_message(
  335. get_lang('You must select a course from original session and select a destination session'),
  336. 'error'
  337. );
  338. displayForm();
  339. }
  340. } else {
  341. displayForm();
  342. }
  343. Display::display_footer();