copy_course_session_selected.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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) || 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_course = Database::get_main_table(TABLE_MAIN_COURSE);
  50. /* FUNCTIONS */
  51. /**
  52. * @param string $name
  53. */
  54. function make_select_session_list($name, $sessions, $attr = array())
  55. {
  56. $attrs = '';
  57. if (count($attr) > 0) {
  58. foreach ($attr as $key => $value) {
  59. $attrs .= ' '.$key.'="'.$value.'"';
  60. }
  61. }
  62. $output = '<select name="'.$name.'" '.$attrs.'>';
  63. if (count($sessions) == 0) {
  64. $output .= '<option value = "0">'.get_lang(
  65. 'ThereIsNotStillASession'
  66. ).'</option>';
  67. } else {
  68. $output .= '<option value = "0">'.get_lang(
  69. 'SelectASession'
  70. ).'</option>';
  71. }
  72. if (is_array($sessions)) {
  73. foreach ($sessions as $session) {
  74. $category_name = '';
  75. if (!empty($session['category_name'])) {
  76. $category_name = ' ('.$session['category_name'].')';
  77. }
  78. $output .= '<option value="'.$session['id'].'">'.$session['name'].' '.$category_name.'</option>';
  79. }
  80. }
  81. $output .= '</select>';
  82. return $output;
  83. }
  84. /**
  85. * Show the form to copy courses
  86. * @global string $returnLink
  87. * @global string $courseCode
  88. */
  89. function displayForm()
  90. {
  91. global $returnLink, $courseCode;
  92. $courseInfo = api_get_course_info();
  93. $sessionId = api_get_session_id();
  94. $userId = api_get_user_id();
  95. $sessions = SessionManager::getSessionsCoachedByUser($userId);
  96. $html = '';
  97. // Actions
  98. $html .= '<div class="actions">';
  99. // Link back to the documents overview
  100. $html .= '<a href="'.$returnLink.'">'.Display::return_icon(
  101. 'back.png', get_lang('BackTo').' '.get_lang('Maintenance'), '', ICON_SIZE_MEDIUM
  102. ).'</a>';
  103. $html .= '</div>';
  104. $html .= Display::return_message(
  105. get_lang('CopyCourseFromSessionToSessionExplanation')
  106. );
  107. $html .= '<form name="formulaire" method="post" action="'.api_get_self(
  108. ).'?'.api_get_cidreq().'" >';
  109. $html .= '<table border="0" cellpadding="5" cellspacing="0" width="100%">';
  110. // Source
  111. $html .= '<tr><td width="15%"><b>'.get_lang(
  112. 'OriginCoursesFromSession'
  113. ).':</b></td>';
  114. $html .= '<td width="10%" align="left">'.api_get_session_name(
  115. $sessionId
  116. ).'</td>';
  117. $html .= '<td width="50%">';
  118. $html .= "{$courseInfo['title']} ({$courseInfo['code']})".'</td></tr>';
  119. // Destination
  120. $html .= '<tr><td width="15%"><b>'.get_lang(
  121. 'DestinationCoursesFromSession'
  122. ).':</b></td>';
  123. $html .= '<td width="10%" align="left"><div id="ajax_sessions_list_destination">';
  124. $html .= '<select name="sessions_list_destination" onchange="javascript: xajax_searchCourses(this.value,\'destination\');">';
  125. if (empty($sessions)) {
  126. $html .= '<option value = "0">'.get_lang(
  127. 'ThereIsNotStillASession'
  128. ).'</option>';
  129. } else {
  130. $html .= '<option value = "0">'.get_lang(
  131. 'SelectASession'
  132. ).'</option>';
  133. foreach ($sessions as $session) {
  134. if ($session['id'] == $sessionId) {
  135. continue;
  136. }
  137. if (!SessionManager::sessionHasCourse($session['id'], $courseCode)) {
  138. continue;
  139. }
  140. $html .= '<option value="'.$session['id'].'">'.$session['name'].'</option>';
  141. }
  142. }
  143. $html .= '</select ></div></td>';
  144. $html .= '<td width="50%">';
  145. $html .= '<div id="ajax_list_courses_destination">';
  146. $html .= '<select id="destination" name="SessionCoursesListDestination[]" style="width:380px;" ></select></div></td>';
  147. $html .= '</tr></table>';
  148. $html .= "<fieldset>";
  149. $html .= '<legend>'.get_lang('TypeOfCopy').' <small>('.get_lang('CopyOnlySessionItems').')</small></legend>';
  150. $html .= '<label class="radio"><input type="radio" id="copy_option_1" name="copy_option" value="full_copy" checked="checked"/>';
  151. $html .= get_lang('FullCopy').'</label>';
  152. $html .= '<label class="radio"><input type="radio" id="copy_option_2" name="copy_option" value="select_items"/>';
  153. $html .= ' '.get_lang('LetMeSelectItems').'</label><br/>';
  154. $html .= "</fieldset>";
  155. $html .= '<button class="save" type="submit" onclick="javascript:if(!confirm('."'".addslashes(
  156. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES)
  157. )."'".')) return false;">'.get_lang('CopyCourse').'</button>';
  158. $html .= '</form>';
  159. echo $html;
  160. }
  161. function searchCourses($idSession, $type)
  162. {
  163. $xajaxResponse = new xajaxResponse();
  164. $return = null;
  165. $courseCode = api_get_course_id();
  166. if (!empty($type)) {
  167. $idSession = intval($idSession);
  168. $courseList = SessionManager::get_course_list_by_session_id($idSession);
  169. $return .= '<select id="destination" name="SessionCoursesListDestination[]" style="width:380px;" >';
  170. foreach ($courseList as $course) {
  171. $course_list_destination[] = $course['code'];
  172. if ($course['code'] != $courseCode) {
  173. continue;
  174. }
  175. $courseTitle = str_replace("'", "\'", $course['title']);
  176. $return .= '<option value="'.$course['code'].'" title="'.@htmlspecialchars(
  177. $course['title'].' ('.$course['visual_code'].')', ENT_QUOTES, api_get_system_encoding()
  178. ).'">'.
  179. $course['title'].' ('.$course['visual_code'].')</option>';
  180. }
  181. $return .= '</select>';
  182. $_SESSION['course_list_destination'] = $course_list_destination;
  183. // Send response by ajax
  184. $xajaxResponse->addAssign(
  185. 'ajax_list_courses_destination', 'innerHTML', api_utf8_encode($return)
  186. );
  187. }
  188. return $xajaxResponse;
  189. }
  190. $xajax->processRequests();
  191. /* HTML head extra */
  192. $htmlHeadXtra[] = $xajax->getJavascript(
  193. api_get_path(WEB_LIBRARY_PATH).'xajax/'
  194. );
  195. $htmlHeadXtra[] = '<script>
  196. function checkSelected(id_select,id_radio,id_title,id_destination) {
  197. var num=0;
  198. obj_origin = document.getElementById(id_select);
  199. obj_destination = document.getElementById(id_destination);
  200. for (x=0;x<obj_origin.options.length;x) {
  201. if (obj_origin.options[x].selected) {
  202. if (obj_destination.options.length > 0) {
  203. for (y=0;y<obj_destination.options.length;y) {
  204. if (obj_origin.options[x].value == obj_destination.options[y].value) {
  205. obj_destination.options[y].selected = true;
  206. }
  207. }
  208. }
  209. num;
  210. } else {
  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 = false;
  215. }
  216. }
  217. }
  218. }
  219. }
  220. if (num == 1) {
  221. document.getElementById(id_radio).disabled = false;
  222. document.getElementById(id_title).style.color = \'#000\';
  223. } else {
  224. document.getElementById(id_radio).disabled = true;
  225. document.getElementById(id_title).style.color = \'#aaa\';
  226. }
  227. }
  228. </script>';
  229. Display::display_header($nameTools);
  230. /* MAIN CODE */
  231. if ((isset($_POST['action']) && $_POST['action'] == 'course_select_form') ||
  232. (isset($_POST['copy_option']) && $_POST['copy_option'] == 'full_copy')
  233. ) {
  234. $destinationCourse = $destinationSession = '';
  235. $originCourse = api_get_course_id();
  236. $originSession = api_get_session_id();
  237. if (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
  238. $destinationCourse = $_POST['destination_course'];
  239. $destinationSession = $_POST['destination_session'];
  240. $course = CourseSelectForm::get_posted_course(
  241. 'copy_course', $originSession, $originCourse
  242. );
  243. $cr = new CourseRestorer($course);
  244. $cr->restore($destinationCourse, $destinationSession);
  245. echo Display::return_message(get_lang('CopyFinished'), 'confirmation');
  246. displayForm();
  247. } else {
  248. $arrCourseOrigin = array();
  249. $arrCourseDestination = array();
  250. $destinationSession = '';
  251. if (isset($_POST['SessionCoursesListDestination'])) {
  252. $arrCourseDestination = $_POST['SessionCoursesListDestination'];
  253. if (!empty($arrCourseDestination)) {
  254. $arrCourseOrigin = SessionManager::get_course_list_by_session_id(
  255. api_get_session_id(),
  256. $courseInfo['title']
  257. );
  258. }
  259. }
  260. if (isset($_POST['sessions_list_destination'])) {
  261. $destinationSession = $_POST['sessions_list_destination'];
  262. }
  263. if ((is_array($arrCourseOrigin) && count($arrCourseOrigin) > 0) && !empty($destinationSession)) {
  264. //We need only one value
  265. if (count($arrCourseOrigin) > 1 || count($arrCourseDestination) > 1) {
  266. echo Display::return_message(
  267. get_lang('YouMustSelectACourseFromOriginalSession'),
  268. 'error'
  269. );
  270. } else {
  271. $courseDestination = $arrCourseDestination[0];
  272. $cb = new CourseBuilder('', $courseInfo);
  273. $course = $cb->build(
  274. $originSession, $courseCode
  275. );
  276. $cr = new CourseRestorer($course);
  277. $cr->restore($courseDestination, $destinationSession);
  278. echo Display::return_message(get_lang('CopyFinished'), 'confirmation');
  279. }
  280. displayForm();
  281. } else {
  282. echo Display::return_message(
  283. get_lang('YouMustSelectACourseFromOriginalSession'),
  284. 'error'
  285. );
  286. displayForm();
  287. }
  288. }
  289. } elseif (isset($_POST['copy_option']) && $_POST['copy_option'] == 'select_items') {
  290. // Else, if a CourseSelectForm is requested, show it
  291. if (api_get_setting('show_glossary_in_documents') != 'none') {
  292. echo Display::return_message(
  293. get_lang('ToExportDocumentsWithGlossaryYouHaveToSelectGlossary'),
  294. 'normal'
  295. );
  296. }
  297. $arrCourseDestination = array();
  298. $destinationSession = '';
  299. if (isset($_POST['SessionCoursesListDestination'])) {
  300. $arrCourseDestination = $_POST['SessionCoursesListDestination'];
  301. }
  302. if (isset($_POST['sessions_list_destination'])) {
  303. $destinationSession = $_POST['sessions_list_destination'];
  304. }
  305. if (!empty($destinationSession)) {
  306. echo Display::return_message(
  307. get_lang('ToExportLearnpathWithQuizYouHaveToSelectQuiz'),
  308. 'normal'
  309. );
  310. $cb = new CourseBuilder('', $courseInfo);
  311. $course = $cb->build($sessionId, $courseCode);
  312. $hiddenFields['destination_course'] = $arrCourseDestination[0];
  313. $hiddenFields['destination_session'] = $destinationSession;
  314. $hiddenFields['origin_course'] = api_get_course_id();
  315. $hiddenFields['origin_session'] = api_get_session_id();
  316. CourseSelectForm :: display_form($course, $hiddenFields, true);
  317. echo '<div style="float:right"><a href="javascript:window.history.go(-1);">'.
  318. Display::return_icon(
  319. 'back.png',
  320. get_lang('Back').' '.get_lang('To').' '.get_lang(
  321. 'PlatformAdmin'
  322. ),
  323. array('style' => 'vertical-align:middle')
  324. ).
  325. get_lang('Back').'</a></div>';
  326. } else {
  327. echo Display::return_message(
  328. get_lang('You must select a course from original session and select a destination session'),
  329. 'error'
  330. );
  331. displayForm();
  332. }
  333. } else {
  334. displayForm();
  335. }
  336. Display::display_footer();