course_list.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script shows a list of courses and allows searching for courses codes
  5. * and names
  6. * @package chamilo.admin
  7. */
  8. $cidReset = true;
  9. require_once __DIR__.'/../inc/global.inc.php';
  10. $this_section = SECTION_PLATFORM_ADMIN;
  11. api_protect_admin_script();
  12. $sessionId = isset($_GET['session_id']) ? $_GET['session_id'] : null;
  13. /**
  14. * Get the number of courses which will be displayed
  15. */
  16. function get_number_of_courses()
  17. {
  18. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  19. $sql = "SELECT COUNT(code) AS total_number_of_items FROM $course_table c";
  20. if ((api_is_platform_admin() || api_is_session_admin()) &&
  21. api_is_multiple_url_enabled() && api_get_current_access_url_id() != -1
  22. ) {
  23. $access_url_rel_course_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  24. $sql .= " INNER JOIN $access_url_rel_course_table url_rel_course
  25. ON (c.id = url_rel_course.c_id)";
  26. }
  27. if (isset($_GET['keyword'])) {
  28. $keyword = Database::escape_string("%".$_GET['keyword']."%");
  29. $sql .= " WHERE (
  30. c.title LIKE '".$keyword."' OR
  31. c.code LIKE '".$keyword."' OR
  32. c.visual_code LIKE '".$keyword."'
  33. )
  34. ";
  35. } elseif (isset($_GET['keyword_code'])) {
  36. $keyword_code = Database::escape_string("%".$_GET['keyword_code']."%");
  37. $keyword_title = Database::escape_string("%".$_GET['keyword_title']."%");
  38. $keyword_category = Database::escape_string("%".$_GET['keyword_category']."%");
  39. $keyword_language = Database::escape_string("%".$_GET['keyword_language']."%");
  40. $keyword_visibility = Database::escape_string("%".$_GET['keyword_visibility']."%");
  41. $keyword_subscribe = Database::escape_string($_GET['keyword_subscribe']);
  42. $keyword_unsubscribe = Database::escape_string($_GET['keyword_unsubscribe']);
  43. $sql .= " WHERE
  44. (c.code LIKE '".$keyword_code."' OR c.visual_code LIKE '".$keyword_code."') AND
  45. c.title LIKE '".$keyword_title."' AND
  46. c.category_code LIKE '".$keyword_category."' AND
  47. c.course_language LIKE '".$keyword_language."' AND
  48. c.visibility LIKE '".$keyword_visibility."' AND
  49. c.subscribe LIKE '".$keyword_subscribe."' AND
  50. c.unsubscribe LIKE '".$keyword_unsubscribe."'
  51. ";
  52. }
  53. // adding the filter to see the user's only of the current access_url
  54. if ((api_is_platform_admin() || api_is_session_admin()) &&
  55. api_is_multiple_url_enabled() && api_get_current_access_url_id() != -1
  56. ) {
  57. $sql .= " AND url_rel_course.access_url_id = ".api_get_current_access_url_id();
  58. }
  59. $res = Database::query($sql);
  60. $obj = Database::fetch_object($res);
  61. return $obj->total_number_of_items;
  62. }
  63. /**
  64. * Get course data to display
  65. * @param int $from
  66. * @param int $number_of_items
  67. * @param int $column
  68. * @param string $direction
  69. *
  70. * @return array
  71. */
  72. function get_course_data($from, $number_of_items, $column, $direction)
  73. {
  74. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  75. $sql = "SELECT code AS col0,
  76. title AS col1,
  77. code AS col2,
  78. course_language AS col3,
  79. category_code AS col4,
  80. subscribe AS col5,
  81. unsubscribe AS col6,
  82. code AS col7,
  83. visibility AS col8,
  84. directory as col9,
  85. visual_code
  86. FROM $course_table course";
  87. if ((api_is_platform_admin() || api_is_session_admin()) &&
  88. api_is_multiple_url_enabled() && api_get_current_access_url_id() != -1
  89. ) {
  90. $access_url_rel_course_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  91. $sql .= " INNER JOIN $access_url_rel_course_table url_rel_course
  92. ON (course.id = url_rel_course.c_id)";
  93. }
  94. if (isset($_GET['keyword'])) {
  95. $keyword = Database::escape_string("%".trim($_GET['keyword'])."%");
  96. $sql .= " WHERE (
  97. title LIKE '".$keyword."' OR
  98. code LIKE '".$keyword."' OR
  99. visual_code LIKE '".$keyword."'
  100. )
  101. ";
  102. } elseif (isset($_GET['keyword_code'])) {
  103. $keyword_code = Database::escape_string("%".$_GET['keyword_code']."%");
  104. $keyword_title = Database::escape_string("%".$_GET['keyword_title']."%");
  105. $keyword_category = Database::escape_string("%".$_GET['keyword_category']."%");
  106. $keyword_language = Database::escape_string("%".$_GET['keyword_language']."%");
  107. $keyword_visibility = Database::escape_string("%".$_GET['keyword_visibility']."%");
  108. $keyword_subscribe = Database::escape_string($_GET['keyword_subscribe']);
  109. $keyword_unsubscribe = Database::escape_string($_GET['keyword_unsubscribe']);
  110. $sql .= " WHERE
  111. (code LIKE '".$keyword_code."' OR visual_code LIKE '".$keyword_code."') AND
  112. title LIKE '".$keyword_title."' AND
  113. category_code LIKE '".$keyword_category."' AND
  114. course_language LIKE '".$keyword_language."' AND
  115. visibility LIKE '".$keyword_visibility."' AND
  116. subscribe LIKE '".$keyword_subscribe."' AND
  117. unsubscribe LIKE '".$keyword_unsubscribe."'";
  118. }
  119. // Adding the filter to see the user's only of the current access_url.
  120. if ((api_is_platform_admin() || api_is_session_admin()) &&
  121. api_is_multiple_url_enabled() && api_get_current_access_url_id() != -1
  122. ) {
  123. $sql .= " AND url_rel_course.access_url_id=".api_get_current_access_url_id();
  124. }
  125. $sql .= " ORDER BY col$column $direction ";
  126. $sql .= " LIMIT $from, $number_of_items";
  127. $res = Database::query($sql);
  128. $courses = array();
  129. $languages = api_get_languages_to_array();
  130. while ($course = Database::fetch_array($res)) {
  131. // Place colour icons in front of courses.
  132. $show_visual_code = $course['visual_code'] != $course[2] ? Display::label($course['visual_code'], 'info') : null;
  133. $course[1] = get_course_visibility_icon($course[8]).'<a href="'.api_get_path(WEB_COURSE_PATH).$course[9].'/index.php">'.$course[1].'</a> '.$show_visual_code;
  134. $course[5] = $course[5] == SUBSCRIBE_ALLOWED ? get_lang('Yes') : get_lang('No');
  135. $course[6] = $course[6] == UNSUBSCRIBE_ALLOWED ? get_lang('Yes') : get_lang('No');
  136. $language = isset($languages[$course[3]]) ? $languages[$course[3]] : $course[3];
  137. $course_rem = array(
  138. $course[0],
  139. $course[1],
  140. $course[2],
  141. $language,
  142. $course[4],
  143. $course[5],
  144. $course[6],
  145. $course[7],
  146. );
  147. $courses[] = $course_rem;
  148. }
  149. return $courses;
  150. }
  151. /**
  152. * Get course data to display filtered by session name
  153. * @param int $from
  154. * @param int $number_of_items
  155. * @param int $column
  156. * @param string $direction
  157. * @return array
  158. */
  159. function get_course_data_by_session($from, $number_of_items, $column, $direction)
  160. {
  161. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  162. $session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  163. $session = Database::get_main_table(TABLE_MAIN_SESSION);
  164. $sql = "SELECT
  165. c.code AS col0,
  166. c.title AS col1,
  167. c.code AS col2,
  168. c.course_language AS col3,
  169. c.category_code AS col4,
  170. c.subscribe AS col5,
  171. c.unsubscribe AS col6,
  172. c.code AS col7,
  173. c.visibility AS col8,
  174. c.directory as col9,
  175. c.visual_code
  176. FROM $course_table c
  177. INNER JOIN $session_rel_course r
  178. ON c.id = r.c_id
  179. INNER JOIN $session s
  180. ON r.session_id = s.id
  181. ";
  182. if (isset($_GET['session_id']) && !empty($_GET['session_id'])) {
  183. $sessionId = intval($_GET['session_id']);
  184. $sql .= " WHERE s.id = ".$sessionId;
  185. }
  186. $sql .= " ORDER BY col$column $direction ";
  187. $sql .= " LIMIT $from,$number_of_items";
  188. $res = Database::query($sql);
  189. $courses = array();
  190. while ($course = Database::fetch_array($res)) {
  191. // Place colour icons in front of courses.
  192. $show_visual_code = $course['visual_code'] != $course[2] ? Display::label($course['visual_code'], 'info') : null;
  193. $course[1] = get_course_visibility_icon($course[8]).'<a href="'.api_get_path(WEB_COURSE_PATH).$course[9].'/index.php">'.$course[1].'</a> '.$show_visual_code;
  194. $course[5] = $course[5] == SUBSCRIBE_ALLOWED ? get_lang('Yes') : get_lang('No');
  195. $course[6] = $course[6] == UNSUBSCRIBE_ALLOWED ? get_lang('Yes') : get_lang('No');
  196. $course_rem = array($course[0], $course[1], $course[2], $course[3], $course[4], $course[5], $course[6], $course[7]);
  197. $courses[] = $course_rem;
  198. }
  199. return $courses;
  200. }
  201. /**
  202. * Filter to display the edit-buttons
  203. */
  204. function modify_courses_filter($code)
  205. {
  206. $icourse = api_get_course_info($code);
  207. $path = api_get_path(WEB_CODE_PATH);
  208. return
  209. '<a href="course_information.php?code='.$code.'">'.
  210. Display::return_icon('synthese_view.gif', get_lang('Info')).'</a>&nbsp;'.
  211. '<a href="'.api_get_path(WEB_COURSE_PATH).$icourse['path'].'/index.php">'.
  212. Display::return_icon('course_home.gif', get_lang('CourseHomepage')).'</a>&nbsp;'.
  213. '<a href="'.$path.'tracking/courseLog.php?'.api_get_cidreq_params($code).'">'.
  214. Display::return_icon('statistics.gif', get_lang('Tracking')).'</a>&nbsp;'.
  215. '<a href="'.$path.'admin/course_edit.php?id='.$icourse['real_id'].'">'.
  216. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>&nbsp;'.
  217. '<a href="'.$path.'coursecopy/create_backup.php?'.api_get_cidreq_params($code).'">'.
  218. Display::return_icon('backup.gif', get_lang('CreateBackup')).'</a>&nbsp;'.
  219. '<a href="'.$path.'admin/course_list.php?delete_course='.$code.'" onclick="javascript: if (!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))."'".')) return false;">'.
  220. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  221. }
  222. /**
  223. * Return an icon representing the visibility of the course
  224. */
  225. function get_course_visibility_icon($v)
  226. {
  227. $style = 'margin-bottom:0;margin-right:5px;';
  228. switch ($v) {
  229. case 0:
  230. return Display::return_icon('bullet_red.png', get_lang('CourseVisibilityClosed'), array('style' => $style));
  231. break;
  232. case 1:
  233. return Display::return_icon('bullet_orange.png', get_lang('Private'), array('style' => $style));
  234. break;
  235. case 2:
  236. return Display::return_icon('bullet_green.png', get_lang('OpenToThePlatform'), array('style' => $style));
  237. break;
  238. case 3:
  239. return Display::return_icon('bullet_blue.png', get_lang('OpenToTheWorld'), array('style' => $style));
  240. break;
  241. case 4:
  242. return Display::return_icon('bullet_grey.png', get_lang('CourseVisibilityHidden'), array('style' => $style));
  243. break;
  244. default:
  245. return '';
  246. }
  247. }
  248. if (isset($_POST['action'])) {
  249. switch ($_POST['action']) {
  250. // Delete selected courses
  251. case 'delete_courses':
  252. $course_codes = $_POST['course'];
  253. if (count($course_codes) > 0) {
  254. foreach ($course_codes as $course_code) {
  255. CourseManager::delete_course($course_code);
  256. $obj_cat = new Category();
  257. $obj_cat->update_category_delete($course_code);
  258. }
  259. }
  260. break;
  261. }
  262. }
  263. $content = '';
  264. $message = '';
  265. $actions = '';
  266. if (isset($_GET['search']) && $_GET['search'] === 'advanced') {
  267. // Get all course categories
  268. $interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  269. $interbreadcrumb[] = array('url' => 'course_list.php', 'name' => get_lang('CourseList'));
  270. $tool_name = get_lang('SearchACourse');
  271. $form = new FormValidator('advanced_course_search', 'get');
  272. $form->addElement('header', $tool_name);
  273. $form->addText('keyword_code', get_lang('CourseCode'), false);
  274. $form->addText('keyword_title', get_lang('Title'), false);
  275. // Category code
  276. $url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_category';
  277. $form->addElement(
  278. 'select_ajax',
  279. 'keyword_category',
  280. get_lang('CourseFaculty'),
  281. null,
  282. array(
  283. 'url' => $url
  284. )
  285. );
  286. $el = $form->addSelectLanguage('keyword_language', get_lang('CourseLanguage'));
  287. $el->addOption(get_lang('All'), '%');
  288. $form->addElement('radio', 'keyword_visibility', get_lang("CourseAccess"), get_lang('OpenToTheWorld'), COURSE_VISIBILITY_OPEN_WORLD);
  289. $form->addElement('radio', 'keyword_visibility', null, get_lang('OpenToThePlatform'), COURSE_VISIBILITY_OPEN_PLATFORM);
  290. $form->addElement('radio', 'keyword_visibility', null, get_lang('Private'), COURSE_VISIBILITY_REGISTERED);
  291. $form->addElement('radio', 'keyword_visibility', null, get_lang('CourseVisibilityClosed'), COURSE_VISIBILITY_CLOSED);
  292. $form->addElement('radio', 'keyword_visibility', null, get_lang('CourseVisibilityHidden'), COURSE_VISIBILITY_HIDDEN);
  293. $form->addElement('radio', 'keyword_visibility', null, get_lang('All'), '%');
  294. $form->addElement('radio', 'keyword_subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
  295. $form->addElement('radio', 'keyword_subscribe', null, get_lang('Denied'), 0);
  296. $form->addElement('radio', 'keyword_subscribe', null, get_lang('All'), '%');
  297. $form->addElement('radio', 'keyword_unsubscribe', get_lang('Unsubscription'), get_lang('AllowedToUnsubscribe'), 1);
  298. $form->addElement('radio', 'keyword_unsubscribe', null, get_lang('NotAllowedToUnsubscribe'), 0);
  299. $form->addElement('radio', 'keyword_unsubscribe', null, get_lang('All'), '%');
  300. $form->addButtonSearch(get_lang('SearchCourse'));
  301. $defaults['keyword_language'] = '%';
  302. $defaults['keyword_visibility'] = '%';
  303. $defaults['keyword_subscribe'] = '%';
  304. $defaults['keyword_unsubscribe'] = '%';
  305. $form->setDefaults($defaults);
  306. $content .= $form->returnForm();
  307. } else {
  308. $interbreadcrumb[] = array('url' => 'index.php', "name" => get_lang('PlatformAdmin'));
  309. $tool_name = get_lang('CourseList');
  310. if (isset($_GET['delete_course'])) {
  311. CourseManager::delete_course($_GET['delete_course']);
  312. $obj_cat = new Category();
  313. $obj_cat->update_category_delete($_GET['delete_course']);
  314. }
  315. // Create a search-box
  316. $form = new FormValidator(
  317. 'search_simple',
  318. 'get',
  319. '',
  320. '',
  321. array(),
  322. FormValidator::LAYOUT_INLINE
  323. );
  324. $form->addElement('text', 'keyword', null, array('id' => 'course-search-keyword'));
  325. $form->addButtonSearch(get_lang('SearchCourse'));
  326. $advanced = '<a class="btn btn-default" href="'.api_get_path(WEB_CODE_PATH).'admin/course_list.php?search=advanced"><em class="fa fa-search"></em> '.get_lang('AdvancedSearch').'</a>';
  327. // Create a filter by session
  328. $sessionFilter = new FormValidator('course_filter', 'get', '', '', array(), FormValidator::LAYOUT_INLINE);
  329. $url = api_get_path(WEB_AJAX_PATH).'session.ajax.php?a=search_session';
  330. $sessionSelect = $sessionFilter->addElement(
  331. 'select_ajax',
  332. 'session_name',
  333. get_lang('SearchCourseBySession'),
  334. null,
  335. array('url' => $url)
  336. );
  337. if (!empty($sessionId)) {
  338. $sessionInfo = SessionManager::fetch($sessionId);
  339. $sessionSelect->addOption($sessionInfo['name'], $sessionInfo['id'], ['selected' => 'selected']);
  340. }
  341. $courseListUrl = api_get_self();
  342. $actions1 = Display::url(
  343. Display::return_icon('new_course.png', get_lang('AddCourse'), [], ICON_SIZE_MEDIUM),
  344. api_get_path(WEB_CODE_PATH).'admin/course_add.php'
  345. );
  346. if (api_get_setting('course_validation') === 'true') {
  347. $actions1 .= Display::url(
  348. Display::return_icon('course_request_pending.png', get_lang('ReviewCourseRequests'), [], ICON_SIZE_MEDIUM),
  349. api_get_path(WEB_CODE_PATH).'admin/course_request_review.php'
  350. );
  351. }
  352. $actions2 = $form->returnForm();
  353. $actions3 = $sessionFilter->returnForm();
  354. $actions4 = $advanced;
  355. $actions4 .= '
  356. <script>
  357. $(function() {
  358. $("#session_name").on("change", function() {
  359. var sessionId = $(this).val();
  360. if (!sessionId) {
  361. return;
  362. }
  363. window.location = "'.$courseListUrl.'?session_id="+sessionId;
  364. });
  365. });
  366. </script>';
  367. $actions = Display::toolbarAction('toolbar', [$actions1, $actions2, $actions3, $actions4], [2, 4, 3, 3]);
  368. if (isset($_GET['session_id']) && !empty($_GET['session_id'])) {
  369. // Create a sortable table with the course data filtered by session
  370. $table = new SortableTable(
  371. 'courses',
  372. 'get_number_of_courses',
  373. 'get_course_data_by_session',
  374. 2
  375. );
  376. } else {
  377. // Create a sortable table with the course data
  378. $table = new SortableTable(
  379. 'courses',
  380. 'get_number_of_courses',
  381. 'get_course_data',
  382. 2,
  383. 20,
  384. 'ASC',
  385. 'course-list'
  386. );
  387. }
  388. $parameters = array();
  389. if (isset($_GET['keyword'])) {
  390. $parameters = array('keyword' => Security::remove_XSS($_GET['keyword']));
  391. } elseif (isset($_GET['keyword_code'])) {
  392. $parameters['keyword_code'] = Security::remove_XSS($_GET['keyword_code']);
  393. $parameters['keyword_title'] = Security::remove_XSS($_GET['keyword_title']);
  394. $parameters['keyword_category'] = Security::remove_XSS($_GET['keyword_category']);
  395. $parameters['keyword_language'] = Security::remove_XSS($_GET['keyword_language']);
  396. $parameters['keyword_visibility'] = Security::remove_XSS($_GET['keyword_visibility']);
  397. $parameters['keyword_subscribe'] = Security::remove_XSS($_GET['keyword_subscribe']);
  398. $parameters['keyword_unsubscribe'] = Security::remove_XSS($_GET['keyword_unsubscribe']);
  399. }
  400. $table->set_additional_parameters($parameters);
  401. $table->set_header(0, '', false, 'width="8px"');
  402. $table->set_header(1, get_lang('Title'), true, null, array('class' => 'title'));
  403. $table->set_header(2, get_lang('Code'));
  404. $table->set_header(3, get_lang('Language'), false, 'width="70px"');
  405. $table->set_header(4, get_lang('Category'));
  406. $table->set_header(5, get_lang('SubscriptionAllowed'), true, 'width="60px"');
  407. $table->set_header(6, get_lang('UnsubscriptionAllowed'), false, 'width="50px"');
  408. $table->set_header(
  409. 7,
  410. get_lang('Action'),
  411. false,
  412. null,
  413. array('class' => 'td_actions')
  414. );
  415. $table->set_column_filter(7, 'modify_courses_filter');
  416. $table->set_form_actions(
  417. array('delete_courses' => get_lang('DeleteCourse')),
  418. 'course'
  419. );
  420. $content .= $table->return_table();
  421. }
  422. $tpl = new Template($tool_name);
  423. $tpl->assign('actions', $actions);
  424. $tpl->assign('message', $message);
  425. $tpl->assign('content', $content);
  426. $tpl->display_one_col_template();