copy_course_session_selected.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com> Code conventions
  9. * @package chamilo.backup
  10. */
  11. require_once '../inc/global.inc.php';
  12. $current_course_tool = TOOL_COURSE_MAINTENANCE;
  13. api_protect_course_script(true, true);
  14. require_once 'classes/CourseBuilder.class.php';
  15. require_once 'classes/CourseRestorer.class.php';
  16. require_once 'classes/CourseSelectForm.class.php';
  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($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(
  40. WEB_CODE_PATH
  41. ) . 'course_info/maintenance_coach.php?' . api_get_cidreq();
  42. $interbreadcrumb[] = array(
  43. 'url' => $returnLink,
  44. 'name' => get_lang('Maintenance')
  45. );
  46. // Database Table Definitions
  47. $tbl_session_rel_course_rel_user = Database::get_main_table(
  48. TABLE_MAIN_SESSION_COURSE_USER
  49. );
  50. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  51. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  52. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  53. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  54. /* FUNCTIONS */
  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['course_list_destination'] = $course_list_destination;
  184. // Send response by ajax
  185. $xajaxResponse->addAssign(
  186. 'ajax_list_courses_destination', 'innerHTML', api_utf8_encode($return)
  187. );
  188. }
  189. return $xajaxResponse;
  190. }
  191. $xajax->processRequests();
  192. /* HTML head extra */
  193. $htmlHeadXtra[] = $xajax->getJavascript(
  194. api_get_path(WEB_LIBRARY_PATH) . 'xajax/'
  195. );
  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', $originSession, $originCourse
  243. );
  244. $cr = new CourseRestorer($course);
  245. $cr->restore($destinationCourse, $destinationSession);
  246. Display::display_confirmation_message(get_lang('CopyFinished'));
  247. displayForm();
  248. } else {
  249. $arrCourseOrigin = array();
  250. $arrCourseDestination = array();
  251. $destinationSession = '';
  252. if (isset($_POST['SessionCoursesListDestination'])) {
  253. $arrCourseDestination = $_POST['SessionCoursesListDestination'];
  254. if (!empty($arrCourseDestination)) {
  255. $arrCourseOrigin = SessionManager::get_course_list_by_session_id(
  256. api_get_session_id(),
  257. $courseInfo['title']
  258. );
  259. }
  260. }
  261. if (isset($_POST['sessions_list_destination'])) {
  262. $destinationSession = $_POST['sessions_list_destination'];
  263. }
  264. if ((is_array($arrCourseOrigin) && count($arrCourseOrigin) > 0) && !empty($destinationSession)) {
  265. //We need only one value
  266. if (count($arrCourseOrigin) > 1 || count($arrCourseDestination) > 1) {
  267. Display::display_error_message(
  268. get_lang('YouMustSelectACourseFromOriginalSession')
  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. Display::display_confirmation_message(get_lang('CopyFinished'));
  279. }
  280. displayForm();
  281. } else {
  282. Display::display_error_message(
  283. get_lang('YouMustSelectACourseFromOriginalSession')
  284. );
  285. displayForm();
  286. }
  287. }
  288. } elseif (isset($_POST['copy_option']) && $_POST['copy_option'] == 'select_items') {
  289. // Else, if a CourseSelectForm is requested, show it
  290. if (api_get_setting('show_glossary_in_documents') != 'none') {
  291. Display::display_normal_message(
  292. get_lang('ToExportDocumentsWithGlossaryYouHaveToSelectGlossary')
  293. );
  294. }
  295. $arrCourseDestination = array();
  296. $destinationSession = '';
  297. if (isset($_POST['SessionCoursesListDestination'])) {
  298. $arrCourseDestination = $_POST['SessionCoursesListDestination'];
  299. }
  300. if (isset($_POST['sessions_list_destination'])) {
  301. $destinationSession = $_POST['sessions_list_destination'];
  302. }
  303. if (!empty($destinationSession)) {
  304. Display::display_normal_message(
  305. get_lang('ToExportLearnpathWithQuizYouHaveToSelectQuiz')
  306. );
  307. $cb = new CourseBuilder('', $courseInfo);
  308. $course = $cb->build($sessionId, $courseCode);
  309. $hiddenFields['destination_course'] = $arrCourseDestination[0];
  310. $hiddenFields['destination_session'] = $destinationSession;
  311. $hiddenFields['origin_course'] = api_get_course_id();
  312. $hiddenFields['origin_session'] = api_get_session_id();
  313. CourseSelectForm :: display_form($course, $hiddenFields, true);
  314. echo '<div style="float:right"><a href="javascript:window.history.go(-1);">' .
  315. Display::return_icon(
  316. 'back.png', get_lang('Back') . ' ' . get_lang('To') . ' ' . get_lang(
  317. 'PlatformAdmin'
  318. ), array('style' => 'vertical-align:middle')
  319. ) .
  320. get_lang('Back') . '</a></div>';
  321. } else {
  322. Display::display_error_message(
  323. get_lang('You must select a course from original session and select a destination session')
  324. );
  325. displayForm();
  326. }
  327. } else {
  328. displayForm();
  329. }
  330. Display::display_footer();