course_list.php 20 KB

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