copy_course_session_selected.php 14 KB

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