copy_course_session_selected.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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_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(
  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. /**
  56. * @param string $name
  57. */
  58. function make_select_session_list($name, $sessions, $attr = array())
  59. {
  60. $attrs = '';
  61. if (count($attr) > 0) {
  62. foreach ($attr as $key => $value) {
  63. $attrs .= ' ' . $key . '="' . $value . '"';
  64. }
  65. }
  66. $output = '<select name="' . $name . '" ' . $attrs . '>';
  67. if (count($sessions) == 0) {
  68. $output .= '<option value = "0">' . get_lang(
  69. 'ThereIsNotStillASession'
  70. ) . '</option>';
  71. } else {
  72. $output .= '<option value = "0">' . get_lang(
  73. 'SelectASession'
  74. ) . '</option>';
  75. }
  76. if (is_array($sessions)) {
  77. foreach ($sessions as $session) {
  78. $category_name = '';
  79. if (!empty($session['category_name'])) {
  80. $category_name = ' (' . $session['category_name'] . ')';
  81. }
  82. $output .= '<option value="' . $session['id'] . '">' . $session['name'] . ' ' . $category_name . '</option>';
  83. }
  84. }
  85. $output .= '</select>';
  86. return $output;
  87. }
  88. /**
  89. * Show the form to copy courses
  90. * @global string $returnLink
  91. * @global string $courseCode
  92. */
  93. function displayForm()
  94. {
  95. global $returnLink, $courseCode;
  96. $courseInfo = api_get_course_info();
  97. $sessionId = api_get_session_id();
  98. $userId = api_get_user_id();
  99. $sessions = SessionManager::getSessionsCoachedByUser($userId);
  100. $html = '';
  101. // Actions
  102. $html .= '<div class="actions">';
  103. // Link back to the documents overview
  104. $html .= '<a href="' . $returnLink . '">' . Display::return_icon(
  105. 'back.png', get_lang('BackTo') . ' ' . get_lang('Maintenance'), '', ICON_SIZE_MEDIUM
  106. ) . '</a>';
  107. $html .= '</div>';
  108. $html .= Display::return_message(
  109. get_lang('CopyCourseFromSessionToSessionExplanation')
  110. );
  111. $html .= '<form name="formulaire" method="post" action="' . api_get_self(
  112. ) . '?' . api_get_cidreq() . '" >';
  113. $html .= '<table border="0" cellpadding="5" cellspacing="0" width="100%">';
  114. // Source
  115. $html .= '<tr><td width="15%"><b>' . get_lang(
  116. 'OriginCoursesFromSession'
  117. ) . ':</b></td>';
  118. $html .= '<td width="10%" align="left">' . api_get_session_name(
  119. $sessionId
  120. ) . '</td>';
  121. $html .= '<td width="50%">';
  122. $html .= "{$courseInfo['title']} ({$courseInfo['code']})" . '</td></tr>';
  123. // Destination
  124. $html .= '<tr><td width="15%"><b>' . get_lang(
  125. 'DestinationCoursesFromSession'
  126. ) . ':</b></td>';
  127. $html .= '<td width="10%" align="left"><div id="ajax_sessions_list_destination">';
  128. $html .= '<select name="sessions_list_destination" onchange="javascript: xajax_searchCourses(this.value,\'destination\');">';
  129. if (empty($sessions)) {
  130. $html .= '<option value = "0">' . get_lang(
  131. 'ThereIsNotStillASession'
  132. ) . '</option>';
  133. } else {
  134. $html .= '<option value = "0">' . get_lang(
  135. 'SelectASession'
  136. ) . '</option>';
  137. foreach ($sessions as $session) {
  138. if ($session['id'] == $sessionId) {
  139. continue;
  140. }
  141. if (!SessionManager::sessionHasCourse($session['id'], $courseCode)) {
  142. continue;
  143. }
  144. $html .= '<option value="' . $session['id'] . '">' . $session['name'] . '</option>';
  145. }
  146. }
  147. $html .= '</select ></div></td>';
  148. $html .= '<td width="50%">';
  149. $html .= '<div id="ajax_list_courses_destination">';
  150. $html .= '<select id="destination" name="SessionCoursesListDestination[]" style="width:380px;" ></select></div></td>';
  151. $html .= '</tr></table>';
  152. $html .= "<fieldset>";
  153. $html .= '<legend>' . get_lang('TypeOfCopy') . ' <small>(' . get_lang('CopyOnlySessionItems') . ')</small></legend>';
  154. $html .= '<label class="radio"><input type="radio" id="copy_option_1" name="copy_option" value="full_copy" checked="checked"/>';
  155. $html .= get_lang('FullCopy') . '</label>';
  156. $html .= '<label class="radio"><input type="radio" id="copy_option_2" name="copy_option" value="select_items"/>';
  157. $html .= ' ' . get_lang('LetMeSelectItems') . '</label><br/>';
  158. $html .= "</fieldset>";
  159. $html .= '<button class="save" type="submit" onclick="javascript:if(!confirm(' . "'" . addslashes(
  160. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES)
  161. ) . "'" . ')) return false;">' . get_lang('CopyCourse') . '</button>';
  162. $html .= '</form>';
  163. echo $html;
  164. }
  165. function searchCourses($idSession, $type)
  166. {
  167. $xajaxResponse = new xajaxResponse();
  168. $return = null;
  169. $courseCode = api_get_course_id();
  170. if (!empty($type)) {
  171. $idSession = intval($idSession);
  172. $courseList = SessionManager::get_course_list_by_session_id($idSession);
  173. $return .= '<select id="destination" name="SessionCoursesListDestination[]" style="width:380px;" >';
  174. foreach ($courseList as $course) {
  175. $course_list_destination[] = $course['code'];
  176. if ($course['code'] != $courseCode) {
  177. continue;
  178. }
  179. $courseTitle = str_replace("'", "\'", $course['title']);
  180. $return .= '<option value="' . $course['code'] . '" title="' . @htmlspecialchars(
  181. $course['title'] . ' (' . $course['visual_code'] . ')', ENT_QUOTES, api_get_system_encoding()
  182. ) . '">' .
  183. $course['title'] . ' (' . $course['visual_code'] . ')</option>';
  184. }
  185. $return .= '</select>';
  186. $_SESSION['course_list_destination'] = $course_list_destination;
  187. // Send response by ajax
  188. $xajaxResponse->addAssign(
  189. 'ajax_list_courses_destination', 'innerHTML', api_utf8_encode($return)
  190. );
  191. }
  192. return $xajaxResponse;
  193. }
  194. $xajax->processRequests();
  195. /* HTML head extra */
  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 ((isset($_POST['action']) && $_POST['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 (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
  242. $destinationCourse = $_POST['destination_course'];
  243. $destinationSession = $_POST['destination_session'];
  244. $course = CourseSelectForm::get_posted_course(
  245. 'copy_course', $originSession, $originCourse
  246. );
  247. $cr = new CourseRestorer($course);
  248. $cr->restore($destinationCourse, $destinationSession);
  249. Display::display_confirmation_message(get_lang('CopyFinished'));
  250. displayForm();
  251. } else {
  252. $arrCourseOrigin = array();
  253. $arrCourseDestination = array();
  254. $destinationSession = '';
  255. if (isset($_POST['SessionCoursesListDestination'])) {
  256. $arrCourseDestination = $_POST['SessionCoursesListDestination'];
  257. if (!empty($arrCourseDestination)) {
  258. $arrCourseOrigin = SessionManager::get_course_list_by_session_id(
  259. api_get_session_id(),
  260. $courseInfo['title']
  261. );
  262. }
  263. }
  264. if (isset($_POST['sessions_list_destination'])) {
  265. $destinationSession = $_POST['sessions_list_destination'];
  266. }
  267. if ((is_array($arrCourseOrigin) && count($arrCourseOrigin) > 0) && !empty($destinationSession)) {
  268. //We need only one value
  269. if (count($arrCourseOrigin) > 1 || count($arrCourseDestination) > 1) {
  270. Display::display_error_message(
  271. get_lang('YouMustSelectACourseFromOriginalSession')
  272. );
  273. } else {
  274. $courseDestination = $arrCourseDestination[0];
  275. $cb = new CourseBuilder('', $courseInfo);
  276. $course = $cb->build(
  277. $originSession, $courseCode
  278. );
  279. $cr = new CourseRestorer($course);
  280. $cr->restore($courseDestination, $destinationSession);
  281. Display::display_confirmation_message(get_lang('CopyFinished'));
  282. }
  283. displayForm();
  284. } else {
  285. Display::display_error_message(
  286. get_lang('YouMustSelectACourseFromOriginalSession')
  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::display_normal_message(
  295. get_lang('ToExportDocumentsWithGlossaryYouHaveToSelectGlossary')
  296. );
  297. }
  298. $arrCourseDestination = array();
  299. $destinationSession = '';
  300. if (isset($_POST['SessionCoursesListDestination'])) {
  301. $arrCourseDestination = $_POST['SessionCoursesListDestination'];
  302. }
  303. if (isset($_POST['sessions_list_destination'])) {
  304. $destinationSession = $_POST['sessions_list_destination'];
  305. }
  306. if (!empty($destinationSession)) {
  307. Display::display_normal_message(
  308. get_lang('ToExportLearnpathWithQuizYouHaveToSelectQuiz')
  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', get_lang('Back') . ' ' . get_lang('To') . ' ' . get_lang(
  320. 'PlatformAdmin'
  321. ), array('style' => 'vertical-align:middle')
  322. ) .
  323. get_lang('Back') . '</a></div>';
  324. } else {
  325. Display::display_error_message(
  326. get_lang('You must select a course from original session and select a destination session')
  327. );
  328. displayForm();
  329. }
  330. } else {
  331. displayForm();
  332. }
  333. Display::display_footer();