course_list.php 21 KB

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