course_list.php 21 KB

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