course_list.php 20 KB

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