course_list.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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]).PHP_EOL
  155. .Display::url(Security::remove_XSS($course[1]), $coursePath.$course[9].'/index.php').PHP_EOL
  156. .$show_visual_code;
  157. $course[5] = $course[5] == SUBSCRIBE_ALLOWED ? get_lang('Yes') : get_lang('No');
  158. $course[6] = $course[6] == UNSUBSCRIBE_ALLOWED ? get_lang('Yes') : get_lang('No');
  159. $language = isset($languages[$course[3]]) ? $languages[$course[3]] : $course[3];
  160. $courseCode = $course[0];
  161. $courseId = $course['id'];
  162. $actions = [];
  163. $actions[] = Display::url(
  164. Display::return_icon('info2.png', get_lang('Info')),
  165. "course_information.php?code=$courseCode"
  166. );
  167. $actions[] = Display::url(
  168. Display::return_icon('course_home.png', get_lang('CourseHomepage')),
  169. $coursePath.$course['directory'].'/index.php'
  170. );
  171. $actions[] = Display::url(
  172. Display::return_icon('statistics.png', get_lang('Tracking')),
  173. $path.'tracking/courseLog.php?'.api_get_cidreq_params($courseCode)
  174. );
  175. $actions[] = Display::url(
  176. Display::return_icon('edit.png', get_lang('Edit')),
  177. $path.'admin/course_edit.php?id='.$courseId
  178. );
  179. $actions[] = Display::url(
  180. Display::return_icon('backup.png', get_lang('CreateBackup')),
  181. $path.'coursecopy/create_backup.php?'.api_get_cidreq_params($courseCode)
  182. );
  183. $actions[] = Display::url(
  184. Display::return_icon('delete.png', get_lang('Delete')),
  185. $path.'admin/course_list.php?delete_course='.$courseCode,
  186. [
  187. 'onclick' => "javascript: if (!confirm('"
  188. .addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))."')) return false;",
  189. ]
  190. );
  191. $courseItem = [
  192. $course[0],
  193. $course[1],
  194. $course[2],
  195. $language,
  196. $course[4],
  197. $course[5],
  198. $course[6],
  199. implode(PHP_EOL, $actions),
  200. ];
  201. $courses[] = $courseItem;
  202. }
  203. return $courses;
  204. }
  205. /**
  206. * Get course data to display filtered by session name.
  207. *
  208. * @param int $from
  209. * @param int $number_of_items
  210. * @param int $column
  211. * @param string $direction
  212. *
  213. * @throws Exception
  214. *
  215. * @return array
  216. */
  217. function get_course_data_by_session($from, $number_of_items, $column, $direction)
  218. {
  219. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  220. $session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  221. $session = Database::get_main_table(TABLE_MAIN_SESSION);
  222. $sql = "SELECT
  223. c.code AS col0,
  224. c.title AS col1,
  225. c.code AS col2,
  226. c.course_language AS col3,
  227. c.category_code AS col4,
  228. c.subscribe AS col5,
  229. c.unsubscribe AS col6,
  230. c.code AS col7,
  231. c.visibility AS col8,
  232. c.directory as col9,
  233. c.visual_code
  234. FROM $course_table c
  235. INNER JOIN $session_rel_course r
  236. ON c.id = r.c_id
  237. INNER JOIN $session s
  238. ON r.session_id = s.id
  239. ";
  240. if (isset($_GET['session_id']) && !empty($_GET['session_id'])) {
  241. $sessionId = intval($_GET['session_id']);
  242. $sql .= " WHERE s.id = ".$sessionId;
  243. }
  244. $sql .= " ORDER BY col$column $direction ";
  245. $sql .= " LIMIT $from,$number_of_items";
  246. $res = Database::query($sql);
  247. $courseUrl = api_get_path(WEB_COURSE_PATH);
  248. $courses = [];
  249. while ($course = Database::fetch_array($res)) {
  250. // Place colour icons in front of courses.
  251. $showVisualCode = $course['visual_code'] != $course[2] ? Display::label($course['visual_code'], 'info') : null;
  252. $course[1] = get_course_visibility_icon($course[8]).
  253. '<a href="'.$courseUrl.$course[9].'/index.php">'.
  254. $course[1].
  255. '</a> '.
  256. $showVisualCode;
  257. $course[5] = $course[5] == SUBSCRIBE_ALLOWED ? get_lang('Yes') : get_lang('No');
  258. $course[6] = $course[6] == UNSUBSCRIBE_ALLOWED ? get_lang('Yes') : get_lang('No');
  259. $row = [
  260. $course[0],
  261. $course[1],
  262. $course[2],
  263. $course[3],
  264. $course[4],
  265. $course[5],
  266. $course[6],
  267. $course[7],
  268. ];
  269. $courses[] = $row;
  270. }
  271. return $courses;
  272. }
  273. /**
  274. * Return an icon representing the visibility of the course.
  275. *
  276. * @param string $visibility
  277. *
  278. * @return string
  279. */
  280. function get_course_visibility_icon($visibility)
  281. {
  282. $style = 'margin-bottom:0;margin-right:5px;';
  283. switch ($visibility) {
  284. case 0:
  285. return Display::return_icon(
  286. 'bullet_red.png',
  287. get_lang('CourseVisibilityClosed'),
  288. ['style' => $style]
  289. );
  290. break;
  291. case 1:
  292. return Display::return_icon(
  293. 'bullet_orange.png',
  294. get_lang('Private'),
  295. ['style' => $style]
  296. );
  297. break;
  298. case 2:
  299. return Display::return_icon(
  300. 'bullet_green.png',
  301. get_lang('OpenToThePlatform'),
  302. ['style' => $style]
  303. );
  304. break;
  305. case 3:
  306. return Display::return_icon(
  307. 'bullet_blue.png',
  308. get_lang('OpenToTheWorld'),
  309. ['style' => $style]
  310. );
  311. break;
  312. case 4:
  313. return Display::return_icon(
  314. 'bullet_grey.png',
  315. get_lang('CourseVisibilityHidden'),
  316. ['style' => $style]
  317. );
  318. break;
  319. default:
  320. return '';
  321. }
  322. }
  323. if (isset($_POST['action'])) {
  324. switch ($_POST['action']) {
  325. // Delete selected courses
  326. case 'delete_courses':
  327. if (!empty($_POST['course'])) {
  328. $course_codes = $_POST['course'];
  329. if (count($course_codes) > 0) {
  330. foreach ($course_codes as $course_code) {
  331. CourseManager::delete_course($course_code);
  332. }
  333. }
  334. Display::addFlash(Display::return_message(get_lang('Deleted')));
  335. }
  336. break;
  337. }
  338. }
  339. $content = '';
  340. $message = '';
  341. $actions = '';
  342. if (isset($_GET['search']) && $_GET['search'] === 'advanced') {
  343. // Get all course categories
  344. $interbreadcrumb[] = [
  345. 'url' => 'index.php',
  346. 'name' => get_lang('PlatformAdmin'),
  347. ];
  348. $interbreadcrumb[] = [
  349. 'url' => 'course_list.php',
  350. 'name' => get_lang('CourseList'),
  351. ];
  352. $tool_name = get_lang('SearchACourse');
  353. $form = new FormValidator('advanced_course_search', 'get');
  354. $form->addElement('header', $tool_name);
  355. $form->addText('keyword_code', get_lang('CourseCode'), false);
  356. $form->addText('keyword_title', get_lang('Title'), false);
  357. // Category code
  358. $url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_category';
  359. $form->addElement(
  360. 'select_ajax',
  361. 'keyword_category',
  362. get_lang('CourseFaculty'),
  363. null,
  364. [
  365. 'url' => $url,
  366. ]
  367. );
  368. $el = $form->addSelectLanguage('keyword_language', get_lang('CourseLanguage'));
  369. $el->addOption(get_lang('All'), '%');
  370. $form->addElement('radio', 'keyword_visibility', get_lang("CourseAccess"), get_lang('OpenToTheWorld'), COURSE_VISIBILITY_OPEN_WORLD);
  371. $form->addElement('radio', 'keyword_visibility', null, get_lang('OpenToThePlatform'), COURSE_VISIBILITY_OPEN_PLATFORM);
  372. $form->addElement('radio', 'keyword_visibility', null, get_lang('Private'), COURSE_VISIBILITY_REGISTERED);
  373. $form->addElement('radio', 'keyword_visibility', null, get_lang('CourseVisibilityClosed'), COURSE_VISIBILITY_CLOSED);
  374. $form->addElement('radio', 'keyword_visibility', null, get_lang('CourseVisibilityHidden'), COURSE_VISIBILITY_HIDDEN);
  375. $form->addElement('radio', 'keyword_visibility', null, get_lang('All'), '%');
  376. $form->addElement('radio', 'keyword_subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
  377. $form->addElement('radio', 'keyword_subscribe', null, get_lang('Denied'), 0);
  378. $form->addElement('radio', 'keyword_subscribe', null, get_lang('All'), '%');
  379. $form->addElement('radio', 'keyword_unsubscribe', get_lang('Unsubscription'), get_lang('AllowedToUnsubscribe'), 1);
  380. $form->addElement('radio', 'keyword_unsubscribe', null, get_lang('NotAllowedToUnsubscribe'), 0);
  381. $form->addElement('radio', 'keyword_unsubscribe', null, get_lang('All'), '%');
  382. $form->addButtonSearch(get_lang('SearchCourse'));
  383. $defaults['keyword_language'] = '%';
  384. $defaults['keyword_visibility'] = '%';
  385. $defaults['keyword_subscribe'] = '%';
  386. $defaults['keyword_unsubscribe'] = '%';
  387. $form->setDefaults($defaults);
  388. $content .= $form->returnForm();
  389. } else {
  390. $interbreadcrumb[] = [
  391. 'url' => 'index.php',
  392. 'name' => get_lang('PlatformAdmin'),
  393. ];
  394. $tool_name = get_lang('CourseList');
  395. if (isset($_GET['delete_course'])) {
  396. CourseManager::delete_course($_GET['delete_course']);
  397. Display::addFlash(Display::return_message(get_lang('Deleted')));
  398. }
  399. // Create a search-box
  400. $form = new FormValidator(
  401. 'search_simple',
  402. 'get',
  403. '',
  404. '',
  405. [],
  406. FormValidator::LAYOUT_INLINE
  407. );
  408. $form->addElement(
  409. 'text',
  410. 'keyword',
  411. null,
  412. ['id' => 'course-search-keyword', 'aria-label' => get_lang('SearchCourse')]
  413. );
  414. $form->addButtonSearch(get_lang('SearchCourse'));
  415. $advanced = '<a class="btn btn-default" href="'.api_get_path(WEB_CODE_PATH).'admin/course_list.php?search=advanced">
  416. <em class="fa fa-search"></em> '.
  417. get_lang('AdvancedSearch').'</a>';
  418. // Create a filter by session
  419. $sessionFilter = new FormValidator(
  420. 'course_filter',
  421. 'get',
  422. '',
  423. '',
  424. [],
  425. FormValidator::LAYOUT_INLINE
  426. );
  427. $url = api_get_path(WEB_AJAX_PATH).'session.ajax.php?a=search_session';
  428. $sessionSelect = $sessionFilter->addElement(
  429. 'select_ajax',
  430. 'session_name',
  431. get_lang('SearchCourseBySession'),
  432. null,
  433. ['id' => 'session_name', 'url' => $url]
  434. );
  435. if (!empty($sessionId)) {
  436. $sessionInfo = SessionManager::fetch($sessionId);
  437. $sessionSelect->addOption(
  438. $sessionInfo['name'],
  439. $sessionInfo['id'],
  440. ['selected' => 'selected']
  441. );
  442. }
  443. $courseListUrl = api_get_self();
  444. $actions1 = Display::url(
  445. Display::return_icon(
  446. 'new_course.png',
  447. get_lang('AddCourse'),
  448. [],
  449. ICON_SIZE_MEDIUM
  450. ),
  451. api_get_path(WEB_CODE_PATH).'admin/course_add.php'
  452. );
  453. if (api_get_setting('course_validation') === 'true') {
  454. $actions1 .= Display::url(
  455. Display::return_icon(
  456. 'course_request_pending.png',
  457. get_lang('ReviewCourseRequests'),
  458. [],
  459. ICON_SIZE_MEDIUM
  460. ),
  461. api_get_path(WEB_CODE_PATH).'admin/course_request_review.php'
  462. );
  463. }
  464. $actions2 = $form->returnForm();
  465. $actions3 = $sessionFilter->returnForm();
  466. $actions4 = $advanced;
  467. $actions4 .= '
  468. <script>
  469. $(function() {
  470. $("#session_name").on("change", function() {
  471. var sessionId = $(this).val();
  472. if (!sessionId) {
  473. return;
  474. }
  475. window.location = "'.$courseListUrl.'?session_id="+sessionId;
  476. });
  477. });
  478. </script>';
  479. $actions = Display::toolbarAction(
  480. 'toolbar',
  481. [$actions1, $actions2, $actions3, $actions4],
  482. [2, 4, 3, 3]
  483. );
  484. if (isset($_GET['session_id']) && !empty($_GET['session_id'])) {
  485. // Create a sortable table with the course data filtered by session
  486. $table = new SortableTable(
  487. 'courses',
  488. 'get_number_of_courses',
  489. 'get_course_data_by_session',
  490. 2
  491. );
  492. } else {
  493. // Create a sortable table with the course data
  494. $table = new SortableTable(
  495. 'courses',
  496. 'get_number_of_courses',
  497. 'get_course_data',
  498. 2,
  499. 20,
  500. 'ASC',
  501. 'course-list'
  502. );
  503. }
  504. $parameters = [];
  505. if (isset($_GET['keyword'])) {
  506. $parameters = ['keyword' => Security::remove_XSS($_GET['keyword'])];
  507. } elseif (isset($_GET['keyword_code'])) {
  508. $parameters['keyword_code'] = Security::remove_XSS($_GET['keyword_code']);
  509. $parameters['keyword_title'] = Security::remove_XSS($_GET['keyword_title']);
  510. if (isset($_GET['keyword_category'])) {
  511. $parameters['keyword_category'] = Security::remove_XSS($_GET['keyword_category']);
  512. }
  513. $parameters['keyword_language'] = Security::remove_XSS($_GET['keyword_language']);
  514. $parameters['keyword_visibility'] = Security::remove_XSS($_GET['keyword_visibility']);
  515. $parameters['keyword_subscribe'] = Security::remove_XSS($_GET['keyword_subscribe']);
  516. $parameters['keyword_unsubscribe'] = Security::remove_XSS($_GET['keyword_unsubscribe']);
  517. }
  518. $table->set_additional_parameters($parameters);
  519. $table->set_header(0, '', false, 'width="8px"');
  520. $table->set_header(1, get_lang('Title'), true, null, ['class' => 'title']);
  521. $table->set_header(2, get_lang('Code'));
  522. $table->set_header(3, get_lang('Language'), false, 'width="70px"');
  523. $table->set_header(4, get_lang('Category'));
  524. $table->set_header(5, get_lang('SubscriptionAllowed'), true, 'width="60px"');
  525. $table->set_header(6, get_lang('UnsubscriptionAllowed'), false, 'width="50px"');
  526. $table->set_header(
  527. 7,
  528. get_lang('Action'),
  529. false,
  530. null,
  531. ['class' => 'td_actions']
  532. );
  533. $table->set_form_actions(
  534. ['delete_courses' => get_lang('DeleteCourse')],
  535. 'course'
  536. );
  537. $content .= $table->return_table();
  538. }
  539. $tpl = new Template($tool_name);
  540. $tpl->assign('actions', $actions);
  541. $tpl->assign('message', $message);
  542. $tpl->assign('content', $content);
  543. $tpl->display_one_col_template();