copy_course_session_selected.php 13 KB

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