copy_course_session.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. /* For licensing terms, see /dokeos_license.txt */
  3. /**
  4. * ==============================================================================
  5. * Copy resources from one course in a session to another one.
  6. *
  7. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  8. * @package dokeos.backup
  9. * ==============================================================================
  10. */
  11. /* INIT SECTION */
  12. // name of the language file that needs to be included
  13. $language_file = array('coursebackup','admin');
  14. $cidReset = true;
  15. require_once '../inc/global.inc.php';
  16. require_once api_get_path(LIBRARY_PATH) . 'fileManage.lib.php';
  17. require_once api_get_path(LIBRARY_PATH) . 'sessionmanager.lib.php';
  18. require_once 'classes/CourseBuilder.class.php';
  19. require_once 'classes/CourseRestorer.class.php';
  20. require_once 'classes/CourseSelectForm.class.php';
  21. require_once '../inc/lib/xajax/xajax.inc.php';
  22. $xajax = new xajax();
  23. $xajax -> registerFunction('search_courses');
  24. if (!api_is_allowed_to_edit())
  25. {
  26. api_not_allowed(true);
  27. }
  28. //remove memory and time limits as much as possible as this might be a long process...
  29. if(function_exists('ini_set'))
  30. {
  31. ini_set('memory_limit','256M');
  32. ini_set('max_execution_time',1800);
  33. }
  34. $this_section=SECTION_PLATFORM_ADMIN;
  35. $nameTools = get_lang('CopyCourse');
  36. $interbreadcrumb[]=array('url' => '../admin/index.php',"name" => get_lang('PlatformAdmin'));
  37. // Database Table Definitions
  38. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  39. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  40. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  41. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  42. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  43. /* FUNCTIONS */
  44. function make_select_session_list($name,$sessions,$attr = array()) {
  45. $attrs = '';
  46. if (count($attr) > 0) {
  47. foreach ($attr as $key => $value) {
  48. $attrs .= ' '.$key.'='.$value. ' ';
  49. }
  50. }
  51. $output = '<select name="'.$name.'" '.$attrs.'>';
  52. if (count($sessions) == 0) {
  53. $output .= '<option value = "0">'.get_lang('ThereIsNotStillASession').'</option>';
  54. } else {
  55. $output .= '<option value = "0">'.get_lang('SelectASession').'</option>';
  56. }
  57. if (is_array($sessions)) {
  58. foreach ($sessions as $session) {
  59. $output .= '<option value="'.$session['id'].'">'.$session['name'].' ('.$session['category_name'].')</option>';
  60. }
  61. }
  62. $output .= '</select>';
  63. return $output;
  64. }
  65. function display_form() {
  66. global $charset;
  67. $html = '';
  68. $sessions = SessionManager::get_sessions_list();
  69. $html .= '<form name="formulaire" method="post" action="'.api_get_self().'" >';
  70. $html .= '<table border="0" cellpadding="5" cellspacing="0" width="100%" align="center">';
  71. $html .= '<tr><td width="30%" align="center"><b>'.get_lang('OriginCoursesFromSession').':</b></td>';
  72. $html .= '<td >&nbsp;</td><td align="center" width="30%"><b>'.get_lang('DestinationCoursesFromSession').':</b></td></tr>';
  73. $html .= '<tr><td width="30%" align="center">'.make_select_session_list('sessions_list_origin',$sessions,array('onchange'=>'xajax_search_courses(this.value,\'origin\')')).'</td>';
  74. $html .= '<td>&nbsp;</td><td width="30%" align="center"><div id="ajax_sessions_list_destination">';
  75. $html .= '<select name="sessions_list_destination" onchange = "xajax_search_courses(this.value,\'destination\')">';
  76. $html .= '<option value = "0">'.get_lang('ThereIsNotStillASession').'</option></select ></div></td></tr>';
  77. $html .= '<tr><td width="30%" align="center"><div id="ajax_list_courses_origin">';
  78. $html .= '<select id="origin" name="SessionCoursesListOrigin[]" multiple="multiple" size="20" style="width:320px;"></select></div></td>';
  79. // Options configuration
  80. $html .= '<td align="top"><div class="sectiontitle">'.get_lang('CopyCourse').'</div>';
  81. $introduction = get_lang('CopyCourseFromSessionToSessionExplanation',true);
  82. $html .= '<div class="normal-message">'.$introduction.'</div>';
  83. $html .= '<div style="height:150px;padding-top:10px;padding-bottom:50px"><h3>'.get_lang('TypeOfCopy').'</h3>';
  84. $html .= '<input type="radio" class="checkbox" id="copy_option_1" name="copy_option" value="full_copy" checked="checked"/>';
  85. $html .= '<label for="copy_option_1">'.get_lang('FullCopy').'</label><br/>';
  86. $html .= '<input type="radio" class="checkbox" id="copy_option_2" name="copy_option" value="select_items" disabled="disabled"/>';
  87. $html .= '<label for="copy_option_2"><span id="title_option2" style="color:#aaa">'.get_lang('LetMeSelectItems').'</span></label><br/><br/>';
  88. $html .= '<button class="save" type="submit" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset))."'".')) return false;">'.get_lang('CopyCourse').'</button></div>';
  89. // actions
  90. $html .= '<div class="sectiontitle">';
  91. // link back to the documents overview
  92. $html .= '<a href="../admin/index.php">'.Display::return_icon('back.png',get_lang('Back').' '.get_lang('To').' '.get_lang('PlatformAdmin'),array('style'=>'vertical-align:middle')).get_lang('Back').' '.get_lang('To').' '.get_lang('PlatformAdmin').'</a>';
  93. $html .= '</div>';
  94. $html .= '</td><td width="30%" align="center">';
  95. $html .= '<div id="ajax_list_courses_destination">';
  96. $html .= '<select id="destination" name="SessionCoursesListDestination[]" multiple="multiple" size="20" style="width:320px;" ></select></div></td>';
  97. $html .= '</tr></table></form>';
  98. echo $html;
  99. }
  100. function search_courses($id_session,$type) {
  101. global $tbl_course, $tbl_session_rel_course, $course_list;
  102. $xajax_response = new XajaxResponse();
  103. $return_origin = '';
  104. $select_destination = '';
  105. if (!empty($type)) {
  106. $id_session = intval($id_session);
  107. if ($type == 'origin') {
  108. // search courses by id_session for origin list
  109. $sql = "SELECT c.code, c.visual_code, c.title, src.id_session
  110. FROM $tbl_course c, $tbl_session_rel_course src
  111. WHERE src.course_code = c.code
  112. AND src.id_session = '".intval($id_session)."'";
  113. $rs = Database::query($sql, __FILE__, __LINE__);
  114. $course_list = array();
  115. $return .= '<select id="origin" name="SessionCoursesListOrigin[]" multiple="multiple" size="20" style="width:320px;" onclick="checkSelected(this.id,\'copy_option_2\',\'title_option2\',\'destination\')">';
  116. while($course = Database :: fetch_array($rs)) {
  117. $course_list[] = "'{$course['code']}'";
  118. $course_title=str_replace("'","\'",$course_title);
  119. $return .= '<option value="'.$course['code'].'" title="'.htmlspecialchars($course['title'].' ('.$course['visual_code'].')',ENT_QUOTES).'">'.$course['title'].' ('.$course['visual_code'].')</option>';
  120. }
  121. $return .= '</select>';
  122. $_SESSION['course_list'] = $course_list;
  123. $_SESSION['session_origin'] = $id_session;
  124. // Build select for destination sessions where is not included current session from select origin
  125. if (!empty($id_session)) {
  126. $session_table =Database::get_main_table(TABLE_MAIN_SESSION);
  127. $session_category_table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  128. $sql = " SELECT s.id, s.name, sc.name as category_name
  129. FROM $session_table s , $session_category_table sc
  130. WHERE s.session_category_id = sc.id AND s.id NOT IN('$id_session')";
  131. $rs_select_destination = Database::query($sql, __FILE__, __LINE__);
  132. $select_destination .= '<select name="sessions_list_destination" onchange = "xajax_search_courses(this.value,\'destination\')">';
  133. $select_destination .= '<option value = "0">'.get_lang('SelectASession').'</option>';
  134. while($session = Database :: fetch_array($rs_select_destination)) {
  135. $select_destination .= '<option value="'.$session['id'].'">'.$session['name'].' ('.$session['category_name'].')</option>';
  136. }
  137. $select_destination .= '</select>';
  138. $xajax_response -> addAssign('ajax_sessions_list_destination','innerHTML',api_utf8_encode($select_destination));
  139. } else{
  140. $select_destination .= '<select name="sessions_list_destination" onchange = "xajax_search_courses(this.value,\'destination\')">';
  141. $select_destination .= '<option value = "0">'.get_lang('ThereIsNotStillASession').'</option>';
  142. $select_destination .= '</select>';
  143. $xajax_response -> addAssign('ajax_sessions_list_destination','innerHTML',api_utf8_encode($select_destination));
  144. }
  145. // select multiple destination empty
  146. $select_multiple_empty = '<select id="destination" name="SessionCoursesListDestination[]" multiple="multiple" size="20" style="width:320px;"></select>';
  147. // send response by ajax
  148. $xajax_response -> addAssign('ajax_list_courses_origin','innerHTML',api_utf8_encode($return));
  149. $xajax_response -> addAssign('ajax_list_courses_destination','innerHTML',api_utf8_encode($select_multiple_empty));
  150. } else {
  151. $list_courses_origin = implode(',',$_SESSION['course_list']);
  152. $session_origin = $_SESSION['session_origin'];
  153. // search courses by id_session where course codes is include en courses list destination
  154. $sql = "SELECT c.code, c.visual_code, c.title, src.id_session
  155. FROM $tbl_course c, $tbl_session_rel_course src
  156. WHERE src.course_code = c.code
  157. AND src.id_session = '".intval($id_session)."'
  158. AND c.code IN ($list_courses_origin)";
  159. $rs = Database::query($sql, __FILE__, __LINE__);
  160. $course_list_destination = array();
  161. $return .= '<select id="destination" name="SessionCoursesListDestination[]" multiple="multiple" size="20" style="width:320px;" onmouseover="this.disabled=true;" onmouseout="this.disabled=false;">';
  162. while($course = Database :: fetch_array($rs)) {
  163. $course_list_destination[] = $course['code'];
  164. $course_title=str_replace("'","\'",$course_title);
  165. $return .= '<option value="'.$course['code'].'" title="'.htmlspecialchars($course['title'].' ('.$course['visual_code'].')',ENT_QUOTES).'">'.$course['title'].' ('.$course['visual_code'].')</option>';
  166. }
  167. $return .= '</select>';
  168. $_SESSION['course_list_destination'] = $course_list_destination;
  169. // send response by ajax
  170. $xajax_response -> addAssign('ajax_list_courses_destination','innerHTML',api_utf8_encode($return));
  171. // disable option from session courses list origin where if no the same con the destination
  172. $sql = "SELECT c.code, c.visual_code, c.title, src.id_session
  173. FROM $tbl_course c, $tbl_session_rel_course src
  174. WHERE src.course_code = c.code
  175. AND src.id_session = '".intval($session_origin)."'";
  176. $result = Database::query($sql, __FILE__, __LINE__);
  177. $return_option_disabled = '<select id="origin" name="SessionCoursesListOrigin[]" multiple="multiple" size="20" style="width:320px;" onclick="checkSelected(this.id,\'copy_option_2\',\'title_option2\',\'destination\')">';
  178. while($cours = Database :: fetch_array($result)) {
  179. $course_title=str_replace("'","\'",$course_title);
  180. if (count($course_list_destination) > 0) {
  181. if (!in_array($cours['code'],$course_list_destination)) {
  182. $return_option_disabled .= '<optgroup style="color:#ccc" label="'.$cours['title'].' ('.$cours['visual_code'].')" >'.$cours['title'].' ('.$cours['visual_code'].')</optgroup>';
  183. } else {
  184. $return_option_disabled .= '<option value="'.$cours['code'].'" title="'.htmlspecialchars($cours['title'].' ('.$cours['visual_code'].')',ENT_QUOTES).'">'.$cours['title'].' ('.$cours['visual_code'].')</option>';
  185. }
  186. } else {
  187. if (empty($id_session)) {
  188. $return_option_disabled .= '<option value="'.$cours['code'].'" title="'.htmlspecialchars($cours['title'].' ('.$cours['visual_code'].')',ENT_QUOTES).'">'.$cours['title'].' ('.$cours['visual_code'].')</option>';
  189. } else {
  190. $return_option_disabled .= '<optgroup style="color:#ccc" label="'.$cours['title'].'('.$cours['visual_code'].')" >'.$cours['title'].' ('.$cours['visual_code'].')</optgroup>';
  191. }
  192. }
  193. }
  194. $return_option_disabled .= '</select>';
  195. // send response by ajax
  196. $xajax_response -> addAssign('ajax_list_courses_origin','innerHTML',api_utf8_encode($return_option_disabled));
  197. }
  198. }
  199. return $xajax_response;
  200. }
  201. $xajax -> processRequests();
  202. /* HTML head extra */
  203. $htmlHeadXtra[] = $xajax->getJavascript('../inc/lib/xajax/');
  204. $htmlHeadXtra[] = '<script language="javascript">
  205. function checkSelected(id_select,id_radio,id_title,id_destination) {
  206. var num=0;
  207. obj_origin = document.getElementById(id_select);
  208. obj_destination = document.getElementById(id_destination);
  209. for (x=0;x<obj_origin.options.length;x++) {
  210. if (obj_origin.options[x].selected) {
  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 = true;
  215. }
  216. }
  217. }
  218. num++;
  219. } else {
  220. if (obj_destination.options.length > 0) {
  221. for (y=0;y<obj_destination.options.length;y++) {
  222. if (obj_origin.options[x].value == obj_destination.options[y].value) {
  223. obj_destination.options[y].selected = false;
  224. }
  225. }
  226. }
  227. }
  228. }
  229. if (num == 1) {
  230. document.getElementById(id_radio).disabled = false;
  231. document.getElementById(id_title).style.color = \'#000\';
  232. } else {
  233. document.getElementById(id_radio).disabled = true;
  234. document.getElementById(id_title).style.color = \'#aaa\';
  235. }
  236. }
  237. </script>';
  238. Display::display_header($nameTools);
  239. /* MAIN CODE */
  240. if ((isset ($_POST['action']) && $_POST['action'] == 'course_select_form') || (isset ($_POST['copy_option']) && $_POST['copy_option'] == 'full_copy')) {
  241. $destination_course = $origin_course = $destination_session = $origin_session = '';
  242. if (isset ($_POST['action']) && $_POST['action'] == 'course_select_form') {
  243. $destination_course = $_POST['destination_course'];
  244. $origin_course = $_POST['origin_course'];
  245. $destination_session = $_POST['destination_session'];
  246. $origin_session = $_POST['origin_session'];
  247. $course = CourseSelectForm :: get_posted_course('copy_course',$origin_session,$origin_course);
  248. $cr = new CourseRestorer($course);
  249. //$cr->set_file_option($_POST['same_file_name_option']);
  250. $cr->restore($destination_course,$destination_session);
  251. Display::display_normal_message(get_lang('CopyFinished'));
  252. display_form();
  253. } else {
  254. $arr_course_origin = array();
  255. $arr_course_destination = array();
  256. $destination_session = '';
  257. $origin_session = '';
  258. if (isset($_POST['SessionCoursesListOrigin'])) {
  259. $arr_course_origin = $_POST['SessionCoursesListOrigin'];
  260. }
  261. if (isset($_POST['SessionCoursesListDestination'])) {
  262. $arr_course_destination = $_POST['SessionCoursesListDestination'];
  263. }
  264. if (isset($_POST['sessions_list_destination'])) {
  265. $destination_session = $_POST['sessions_list_destination'];
  266. }
  267. if (isset($_POST['sessions_list_origin'])) {
  268. $origin_session = $_POST['sessions_list_origin'];
  269. }
  270. if ((is_array($arr_course_origin) && count($arr_course_origin) > 0) && !empty($destination_session)) {
  271. foreach ($arr_course_origin as $course_origin) {
  272. $cb = new CourseBuilder();
  273. $course = $cb->build($origin_session,$course_origin);
  274. $cr = new CourseRestorer($course);
  275. //$cr->set_file_option($_POST['same_file_name_option']);
  276. $cr->restore($course_origin,$destination_session);
  277. }
  278. Display::display_normal_message(get_lang('CopyFinished'));
  279. display_form();
  280. } else {
  281. Display::display_error_message(get_lang('YouMustSelectACourseFromOriginalSession'));
  282. display_form();
  283. }
  284. }
  285. } elseif (isset ($_POST['copy_option']) && $_POST['copy_option'] == 'select_items') {
  286. // Else, if a CourseSelectForm is requested, show it
  287. if (api_get_setting('show_glossary_in_documents') != 'none') {
  288. Display::display_normal_message(get_lang('ToExportDocumentsWithGlossaryYouHaveToSelectGlossary'));
  289. }
  290. $arr_course_origin = array();
  291. $arr_course_destination = array();
  292. $destination_session = '';
  293. $origin_session = '';
  294. if (isset($_POST['SessionCoursesListOrigin'])) {
  295. $arr_course_origin = $_POST['SessionCoursesListOrigin'];
  296. }
  297. if (isset($_POST['SessionCoursesListDestination'])) {
  298. $arr_course_destination = $_POST['SessionCoursesListDestination'];
  299. }
  300. if (isset($_POST['sessions_list_destination'])) {
  301. $destination_session = $_POST['sessions_list_destination'];
  302. }
  303. if (isset($_POST['sessions_list_origin'])) {
  304. $origin_session = $_POST['sessions_list_origin'];
  305. }
  306. if ((is_array($arr_course_origin) && count($arr_course_origin) > 0) && !empty($destination_session)) {
  307. Display::display_normal_message(get_lang('ToExportLearnpathWithQuizYouHaveToSelectQuiz'));
  308. $cb = new CourseBuilder();
  309. $course = $cb->build($origin_session,$arr_course_origin[0]);
  310. //$hidden_fields['same_file_name_option'] = $_POST['same_file_name_option'];
  311. $hidden_fields['destination_course'] = $arr_course_origin[0];
  312. $hidden_fields['origin_course'] = $arr_course_origin[0];
  313. $hidden_fields['destination_session'] = $destination_session;
  314. $hidden_fields['origin_session'] = $origin_session;
  315. CourseSelectForm :: display_form($course,$hidden_fields, true);
  316. echo '<div style="float:right"><a href="javascript:window.back();">'.Display::return_icon('back.png',get_lang('Back').' '.get_lang('To').' '.get_lang('PlatformAdmin'),array('style'=>'vertical-align:middle')).get_lang('Back').'</a></div>';
  317. } else {
  318. Display::display_error_message(get_lang('You must select a course from original session and select a destination session'));
  319. display_form();
  320. }
  321. } else {
  322. display_form();
  323. }
  324. /* FOOTER */
  325. Display::display_footer();