course_list.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. $courseToDelete = api_get_course_info($course_code);
  296. if ($courseToDelete) {
  297. CourseManager::delete_course($course_code);
  298. $obj_cat = new Category();
  299. $obj_cat->update_category_delete($courseToDelete['real_id']);
  300. }
  301. }
  302. }
  303. break;
  304. }
  305. }
  306. $content = '';
  307. $message = '';
  308. $actions = '';
  309. if (isset($_GET['search']) && $_GET['search'] === 'advanced') {
  310. // Get all course categories
  311. $interbreadcrumb[] = array(
  312. 'url' => 'index.php',
  313. 'name' => get_lang('PlatformAdmin')
  314. );
  315. $interbreadcrumb[] = array(
  316. 'url' => 'course_list.php',
  317. 'name' => get_lang('CourseList')
  318. );
  319. $tool_name = get_lang('SearchACourse');
  320. $form = new FormValidator('advanced_course_search', 'get');
  321. $form->addElement('header', $tool_name);
  322. $form->addText('keyword_code', get_lang('CourseCode'), false);
  323. $form->addText('keyword_title', get_lang('Title'), false);
  324. // Category code
  325. $url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_category';
  326. $form->addElement(
  327. 'select_ajax',
  328. 'keyword_category',
  329. get_lang('CourseFaculty'),
  330. null,
  331. array(
  332. 'url' => $url
  333. )
  334. );
  335. $el = $form->addSelectLanguage('keyword_language', get_lang('CourseLanguage'));
  336. $el->addOption(get_lang('All'), '%');
  337. $form->addElement('radio', 'keyword_visibility', get_lang("CourseAccess"), get_lang('OpenToTheWorld'), COURSE_VISIBILITY_OPEN_WORLD);
  338. $form->addElement('radio', 'keyword_visibility', null, get_lang('OpenToThePlatform'), COURSE_VISIBILITY_OPEN_PLATFORM);
  339. $form->addElement('radio', 'keyword_visibility', null, get_lang('Private'), COURSE_VISIBILITY_REGISTERED);
  340. $form->addElement('radio', 'keyword_visibility', null, get_lang('CourseVisibilityClosed'), COURSE_VISIBILITY_CLOSED);
  341. $form->addElement('radio', 'keyword_visibility', null, get_lang('CourseVisibilityHidden'), COURSE_VISIBILITY_HIDDEN);
  342. $form->addElement('radio', 'keyword_visibility', null, get_lang('All'), '%');
  343. $form->addElement('radio', 'keyword_subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
  344. $form->addElement('radio', 'keyword_subscribe', null, get_lang('Denied'), 0);
  345. $form->addElement('radio', 'keyword_subscribe', null, get_lang('All'), '%');
  346. $form->addElement('radio', 'keyword_unsubscribe', get_lang('Unsubscription'), get_lang('AllowedToUnsubscribe'), 1);
  347. $form->addElement('radio', 'keyword_unsubscribe', null, get_lang('NotAllowedToUnsubscribe'), 0);
  348. $form->addElement('radio', 'keyword_unsubscribe', null, get_lang('All'), '%');
  349. $form->addButtonSearch(get_lang('SearchCourse'));
  350. $defaults['keyword_language'] = '%';
  351. $defaults['keyword_visibility'] = '%';
  352. $defaults['keyword_subscribe'] = '%';
  353. $defaults['keyword_unsubscribe'] = '%';
  354. $form->setDefaults($defaults);
  355. $content .= $form->returnForm();
  356. } else {
  357. $interbreadcrumb[] = array(
  358. 'url' => 'index.php',
  359. 'name' => get_lang('PlatformAdmin')
  360. );
  361. $tool_name = get_lang('CourseList');
  362. if (isset($_GET['delete_course'])) {
  363. $courseToDelete = api_get_course_info($_GET['delete_course']);
  364. if ($courseToDelete) {
  365. CourseManager::delete_course($_GET['delete_course']);
  366. $obj_cat = new Category();
  367. $obj_cat->update_category_delete($courseToDelete['real_id']);
  368. }
  369. }
  370. // Create a search-box
  371. $form = new FormValidator(
  372. 'search_simple',
  373. 'get',
  374. '',
  375. '',
  376. array(),
  377. FormValidator::LAYOUT_INLINE
  378. );
  379. $form->addElement(
  380. 'text',
  381. 'keyword',
  382. null,
  383. array('id' => 'course-search-keyword', 'aria-label' => get_lang('SearchCourse'))
  384. );
  385. $form->addButtonSearch(get_lang('SearchCourse'));
  386. $advanced = '<a class="btn btn-default" href="'.api_get_path(WEB_CODE_PATH).'admin/course_list.php?search=advanced"><em class="fa fa-search"></em> '.get_lang('AdvancedSearch').'</a>';
  387. // Create a filter by session
  388. $sessionFilter = new FormValidator(
  389. 'course_filter',
  390. 'get',
  391. '',
  392. '',
  393. array(),
  394. FormValidator::LAYOUT_INLINE
  395. );
  396. $url = api_get_path(WEB_AJAX_PATH).'session.ajax.php?a=search_session';
  397. $sessionSelect = $sessionFilter->addElement(
  398. 'select_ajax',
  399. 'session_name',
  400. get_lang('SearchCourseBySession'),
  401. null,
  402. array('url' => $url)
  403. );
  404. if (!empty($sessionId)) {
  405. $sessionInfo = SessionManager::fetch($sessionId);
  406. $sessionSelect->addOption(
  407. $sessionInfo['name'],
  408. $sessionInfo['id'],
  409. ['selected' => 'selected']
  410. );
  411. }
  412. $courseListUrl = api_get_self();
  413. $actions1 = Display::url(
  414. Display::return_icon(
  415. 'new_course.png',
  416. get_lang('AddCourse'),
  417. [],
  418. ICON_SIZE_MEDIUM
  419. ),
  420. api_get_path(WEB_CODE_PATH).'admin/course_add.php'
  421. );
  422. if (api_get_setting('course_validation') === 'true') {
  423. $actions1 .= Display::url(
  424. Display::return_icon(
  425. 'course_request_pending.png',
  426. get_lang('ReviewCourseRequests'),
  427. [],
  428. ICON_SIZE_MEDIUM
  429. ),
  430. api_get_path(WEB_CODE_PATH).'admin/course_request_review.php'
  431. );
  432. }
  433. $actions2 = $form->returnForm();
  434. $actions3 = $sessionFilter->returnForm();
  435. $actions4 = $advanced;
  436. $actions4 .= '
  437. <script>
  438. $(function() {
  439. $("#session_name").on("change", function() {
  440. var sessionId = $(this).val();
  441. if (!sessionId) {
  442. return;
  443. }
  444. window.location = "'.$courseListUrl.'?session_id="+sessionId;
  445. });
  446. });
  447. </script>';
  448. $actions = Display::toolbarAction(
  449. 'toolbar',
  450. [$actions1, $actions2, $actions3, $actions4],
  451. [2, 4, 3, 3]
  452. );
  453. if (isset($_GET['session_id']) && !empty($_GET['session_id'])) {
  454. // Create a sortable table with the course data filtered by session
  455. $table = new SortableTable(
  456. 'courses',
  457. 'get_number_of_courses',
  458. 'get_course_data_by_session',
  459. 2
  460. );
  461. } else {
  462. // Create a sortable table with the course data
  463. $table = new SortableTable(
  464. 'courses',
  465. 'get_number_of_courses',
  466. 'get_course_data',
  467. 2,
  468. 20,
  469. 'ASC',
  470. 'course-list'
  471. );
  472. }
  473. $parameters = [];
  474. if (isset($_GET['keyword'])) {
  475. $parameters = array('keyword' => Security::remove_XSS($_GET['keyword']));
  476. } elseif (isset($_GET['keyword_code'])) {
  477. $parameters['keyword_code'] = Security::remove_XSS($_GET['keyword_code']);
  478. $parameters['keyword_title'] = Security::remove_XSS($_GET['keyword_title']);
  479. $parameters['keyword_category'] = Security::remove_XSS($_GET['keyword_category']);
  480. $parameters['keyword_language'] = Security::remove_XSS($_GET['keyword_language']);
  481. $parameters['keyword_visibility'] = Security::remove_XSS($_GET['keyword_visibility']);
  482. $parameters['keyword_subscribe'] = Security::remove_XSS($_GET['keyword_subscribe']);
  483. $parameters['keyword_unsubscribe'] = Security::remove_XSS($_GET['keyword_unsubscribe']);
  484. }
  485. $table->set_additional_parameters($parameters);
  486. $table->set_header(0, '', false, 'width="8px"');
  487. $table->set_header(1, get_lang('Title'), true, null, array('class' => 'title'));
  488. $table->set_header(2, get_lang('Code'));
  489. $table->set_header(3, get_lang('Language'), false, 'width="70px"');
  490. $table->set_header(4, get_lang('Category'));
  491. $table->set_header(5, get_lang('SubscriptionAllowed'), true, 'width="60px"');
  492. $table->set_header(6, get_lang('UnsubscriptionAllowed'), false, 'width="50px"');
  493. $table->set_header(
  494. 7,
  495. get_lang('Action'),
  496. false,
  497. null,
  498. array('class' => 'td_actions')
  499. );
  500. $table->set_form_actions(
  501. array('delete_courses' => get_lang('DeleteCourse')),
  502. 'course'
  503. );
  504. $content .= $table->return_table();
  505. }
  506. $tpl = new Template($tool_name);
  507. $tpl->assign('actions', $actions);
  508. $tpl->assign('message', $message);
  509. $tpl->assign('content', $content);
  510. $tpl->display_one_col_template();