course_list.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Framework\Container;
  4. /**
  5. * This script shows a list of courses and allows searching for courses codes
  6. * and names
  7. * @package chamilo.admin
  8. */
  9. $cidReset = true;
  10. ////require_once '../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. * @param int $from
  67. * @param int $number_of_items
  68. * @param int $column
  69. * @param string $direction
  70. *
  71. * @return array
  72. */
  73. function get_course_data($from, $number_of_items, $column, $direction)
  74. {
  75. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  76. $sql = "SELECT 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. FROM $course_table course";
  88. if ((api_is_platform_admin() || api_is_session_admin()) &&
  89. api_is_multiple_url_enabled() && api_get_current_access_url_id() != -1
  90. ) {
  91. $access_url_rel_course_table = Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  92. $sql.= " INNER JOIN $access_url_rel_course_table url_rel_course
  93. ON (course.id = url_rel_course.c_id)";
  94. }
  95. if (isset ($_GET['keyword'])) {
  96. $keyword = Database::escape_string("%".trim($_GET['keyword'])."%");
  97. $sql .= " WHERE (
  98. title LIKE '".$keyword."' OR
  99. code LIKE '".$keyword."' OR
  100. visual_code LIKE '".$keyword."'
  101. )
  102. ";
  103. } elseif (isset($_GET['keyword_code'])) {
  104. $keyword_code = Database::escape_string("%".$_GET['keyword_code']."%");
  105. $keyword_title = Database::escape_string("%".$_GET['keyword_title']."%");
  106. $keyword_category = Database::escape_string("%".$_GET['keyword_category']."%");
  107. $keyword_language = Database::escape_string("%".$_GET['keyword_language']."%");
  108. $keyword_visibility = Database::escape_string("%".$_GET['keyword_visibility']."%");
  109. $keyword_subscribe = Database::escape_string($_GET['keyword_subscribe']);
  110. $keyword_unsubscribe = Database::escape_string($_GET['keyword_unsubscribe']);
  111. $sql .= " WHERE
  112. (code LIKE '".$keyword_code."' OR visual_code LIKE '".$keyword_code."') AND
  113. title LIKE '".$keyword_title."' AND
  114. category_code LIKE '".$keyword_category."' AND
  115. course_language LIKE '".$keyword_language."' AND
  116. visibility LIKE '".$keyword_visibility."' AND
  117. subscribe LIKE '".$keyword_subscribe."' AND
  118. unsubscribe LIKE '".$keyword_unsubscribe."'";
  119. }
  120. // Adding the filter to see the user's only of the current access_url.
  121. if ((api_is_platform_admin() || api_is_session_admin()) &&
  122. api_is_multiple_url_enabled() && api_get_current_access_url_id() != -1
  123. ) {
  124. $sql.= " AND url_rel_course.access_url_id=".api_get_current_access_url_id();
  125. }
  126. $sql .= " ORDER BY col$column $direction ";
  127. $sql .= " LIMIT $from, $number_of_items";
  128. $res = Database::query($sql);
  129. $courses = array();
  130. $languages = api_get_languages_to_array();
  131. while ($course = Database::fetch_array($res)) {
  132. // Place colour icons in front of courses.
  133. $show_visual_code = $course['visual_code'] != $course[2] ? Display::label($course['visual_code'], 'info') : null;
  134. $course[1] = get_course_visibility_icon($course[8]).'<a href="'.api_get_path(WEB_COURSE_PATH).$course[9].'/index.php">'.$course[1].'</a> '.$show_visual_code;
  135. $course[5] = $course[5] == SUBSCRIBE_ALLOWED ? get_lang('Yes') : get_lang('No');
  136. $course[6] = $course[6] == UNSUBSCRIBE_ALLOWED ? get_lang('Yes') : get_lang('No');
  137. $language = isset($languages[$course[3]]) ? $languages[$course[3]] : $course[3];
  138. $course_rem = array(
  139. $course[0],
  140. $course[1],
  141. $course[2],
  142. $language,
  143. $course[4],
  144. $course[5],
  145. $course[6],
  146. $course[7],
  147. );
  148. $courses[] = $course_rem;
  149. }
  150. return $courses;
  151. }
  152. /**
  153. * Get course data to display filtered by session name
  154. * @param int $from
  155. * @param int $number_of_items
  156. * @param int $column
  157. * @param string $direction
  158. * @return array
  159. */
  160. function get_course_data_by_session($from, $number_of_items, $column, $direction)
  161. {
  162. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  163. $session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  164. $session = Database::get_main_table(TABLE_MAIN_SESSION);
  165. $sql = "SELECT
  166. c.code AS col0,
  167. c.title AS col1,
  168. c.code AS col2,
  169. c.course_language AS col3,
  170. c.category_code AS col4,
  171. c.subscribe AS col5,
  172. c.unsubscribe AS col6,
  173. c.code AS col7,
  174. c.visibility AS col8,
  175. c.directory as col9,
  176. c.visual_code
  177. FROM $course_table c
  178. INNER JOIN $session_rel_course r
  179. ON c.id = r.c_id
  180. INNER JOIN $session s
  181. ON r.session_id = s.id
  182. ";
  183. if (isset($_GET['session_id']) && !empty($_GET['session_id'])) {
  184. $sessionId = intval($_GET['session_id']);
  185. $sql.= " WHERE s.id = ".$sessionId;
  186. }
  187. $sql .= " ORDER BY col$column $direction ";
  188. $sql .= " LIMIT $from,$number_of_items";
  189. $res = Database::query($sql);
  190. $courses = array ();
  191. while ($course = Database::fetch_array($res)) {
  192. // Place colour icons in front of courses.
  193. $show_visual_code = $course['visual_code'] != $course[2] ? Display::label($course['visual_code'], 'info') : null;
  194. $course[1] = get_course_visibility_icon($course[8]).'<a href="'.api_get_path(WEB_COURSE_PATH).$course[9].'/index.php">'.$course[1].'</a> '.$show_visual_code;
  195. $course[5] = $course[5] == SUBSCRIBE_ALLOWED ? get_lang('Yes') : get_lang('No');
  196. $course[6] = $course[6] == UNSUBSCRIBE_ALLOWED ? get_lang('Yes') : get_lang('No');
  197. $course_rem = array($course[0], $course[1], $course[2], $course[3], $course[4], $course[5], $course[6], $course[7]);
  198. $courses[] = $course_rem;
  199. }
  200. return $courses;
  201. }
  202. /**
  203. * Filter to display the edit-buttons
  204. */
  205. function modify_courses_filter($code)
  206. {
  207. $icourse = api_get_course_info($code);
  208. $path = api_get_path(WEB_CODE_PATH);
  209. return
  210. '<a href="course_information.php?code='.$code.'">'.
  211. Display::return_icon('synthese_view.gif', get_lang('Info')).'</a>&nbsp;'.
  212. //'<a href="../course_home/course_home.php?cidReq='.$code.'">'.
  213. //Display::return_icon('course_home.gif', get_lang('CourseHomepage')).'</a>&nbsp;'. // This is not the preferable way to go to the homepage.
  214. '<a href="'.api_get_path(WEB_COURSE_PATH).$icourse['path'].'/index.php">'.
  215. Display::return_icon('course_home.gif', get_lang('CourseHomepage')).'</a>&nbsp;'.
  216. '<a href="'.$path.'tracking/courseLog.php?'.api_get_cidreq_params($code).'">'.
  217. Display::return_icon('statistics.gif', get_lang('Tracking')).'</a>&nbsp;'.
  218. '<a href="'.$path.'admin/course_edit.php?id='.$icourse['real_id'].'">'.
  219. Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>&nbsp;'.
  220. '<a href="'.$path.'coursecopy/create_backup.php?'.api_get_cidreq_params($code).'">'.
  221. Display::return_icon('backup.gif', get_lang('CreateBackup')).'</a>&nbsp;'.
  222. '<a href="'.$path.'admin/course_list.php?delete_course='.$code.'" onclick="javascript: if (!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))."'".')) return false;">'.
  223. Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  224. }
  225. /**
  226. * Return an icon representing the visibility of the course
  227. */
  228. function get_course_visibility_icon($v) {
  229. $style = 'margin-bottom:0;margin-right:5px;';
  230. switch($v) {
  231. case 0:
  232. return Display::return_icon('bullet_red.png', get_lang('CourseVisibilityClosed'), array('style' => $style));
  233. break;
  234. case 1:
  235. return Display::return_icon('bullet_orange.png', get_lang('Private'), array('style' => $style));
  236. break;
  237. case 2:
  238. return Display::return_icon('bullet_green.png', get_lang('OpenToThePlatform'), array('style' => $style));
  239. break;
  240. case 3:
  241. return Display::return_icon('bullet_blue.png', get_lang('OpenToTheWorld'), array('style' => $style));
  242. break;
  243. case 4:
  244. return Display::return_icon('bullet_grey.png', get_lang('CourseVisibilityHidden'), array('style' => $style));
  245. break;
  246. default:
  247. return '';
  248. }
  249. }
  250. if (isset ($_POST['action'])) {
  251. switch ($_POST['action']) {
  252. // Delete selected courses
  253. case 'delete_courses':
  254. $course_codes = $_POST['course'];
  255. if (count($course_codes) > 0) {
  256. foreach ($course_codes as $course_code) {
  257. CourseManager::delete_course($course_code);
  258. $obj_cat = new Category();
  259. $obj_cat->update_category_delete($course_code);
  260. }
  261. }
  262. break;
  263. }
  264. }
  265. $content = '';
  266. $message = '';
  267. $actions = '';
  268. if (isset ($_GET['search']) && $_GET['search'] === 'advanced') {
  269. // Get all course categories
  270. $interbreadcrumb[] = array('url' => Container::getRouter()->generate('administration'), 'name' => get_lang('PlatformAdmin'));
  271. $interbreadcrumb[] = array('url' => 'course_list.php', 'name' => get_lang('CourseList'));
  272. $tool_name = get_lang('SearchACourse');
  273. //api_display_tool_title($tool_name);
  274. $form = new FormValidator('advanced_course_search', 'get');
  275. $form->addElement('header', $tool_name);
  276. $form->addText('keyword_code', get_lang('CourseCode'), false);
  277. $form->addText('keyword_title', get_lang('Title'), false);
  278. // Category code
  279. $url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_category';
  280. $form->addElement(
  281. 'select_ajax',
  282. 'keyword_category',
  283. get_lang('CourseFaculty'),
  284. null,
  285. array(
  286. 'url' => $url
  287. )
  288. );
  289. $el = $form->addElement('select_language', 'keyword_language', get_lang('CourseLanguage'));
  290. $el->addOption(get_lang('All'), '%');
  291. $form->addElement('radio', 'keyword_visibility', get_lang("CourseAccess"), get_lang('OpenToTheWorld'), COURSE_VISIBILITY_OPEN_WORLD);
  292. $form->addElement('radio', 'keyword_visibility', null, get_lang('OpenToThePlatform'), COURSE_VISIBILITY_OPEN_PLATFORM);
  293. $form->addElement('radio', 'keyword_visibility', null, get_lang('Private'), COURSE_VISIBILITY_REGISTERED);
  294. $form->addElement('radio', 'keyword_visibility', null, get_lang('CourseVisibilityClosed'), COURSE_VISIBILITY_CLOSED);
  295. $form->addElement('radio', 'keyword_visibility', null, get_lang('CourseVisibilityHidden'), COURSE_VISIBILITY_HIDDEN);
  296. $form->addElement('radio', 'keyword_visibility', null, get_lang('All'), '%');
  297. $form->addElement('radio', 'keyword_subscribe', get_lang('Subscription'), get_lang('Allowed'), 1);
  298. $form->addElement('radio', 'keyword_subscribe', null, get_lang('Denied'), 0);
  299. $form->addElement('radio', 'keyword_subscribe', null, get_lang('All'), '%');
  300. $form->addElement('radio', 'keyword_unsubscribe', get_lang('Unsubscription'), get_lang('AllowedToUnsubscribe'), 1);
  301. $form->addElement('radio', 'keyword_unsubscribe', null, get_lang('NotAllowedToUnsubscribe'), 0);
  302. $form->addElement('radio', 'keyword_unsubscribe', null, get_lang('All'), '%');
  303. $form->addButtonSearch(get_lang('SearchCourse'));
  304. $defaults['keyword_language'] = '%';
  305. $defaults['keyword_visibility'] = '%';
  306. $defaults['keyword_subscribe'] = '%';
  307. $defaults['keyword_unsubscribe'] = '%';
  308. $form->setDefaults($defaults);
  309. $content .= $form->returnForm();
  310. } else {
  311. $interbreadcrumb[] = array ('url' => Container::getRouter()->generate('administration'), "name" => get_lang('PlatformAdmin'));
  312. $tool_name = get_lang('CourseList');
  313. if (isset($_GET['delete_course'])) {
  314. CourseManager::delete_course($_GET['delete_course']);
  315. $obj_cat = new Category();
  316. $obj_cat->update_category_delete($_GET['delete_course']);
  317. }
  318. // Create a search-box
  319. $form = new FormValidator('search_simple', 'get', '', '', array(), FormValidator::LAYOUT_INLINE);
  320. $form->addElement('text', 'keyword', null, array('id' => 'course-search-keyword'));
  321. $form->addButtonSearch(get_lang('SearchCourse'));
  322. $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>';
  323. // Create a filter by session
  324. $sessionFilter = new FormValidator('course_filter', 'get', '', '', array(), FormValidator::LAYOUT_INLINE);
  325. $url = api_get_path(WEB_AJAX_PATH).'session.ajax.php?a=search_session';
  326. $sessionSelect = $sessionFilter->addElement(
  327. 'select_ajax',
  328. 'session_name',
  329. get_lang('SearchCourseBySession'),
  330. null,
  331. array('url' => $url)
  332. );
  333. if (!empty($sessionId)) {
  334. $sessionInfo = SessionManager::fetch($sessionId);
  335. $sessionSelect->addOption($sessionInfo['name'], $sessionInfo['id'], ['selected' => 'selected']);
  336. }
  337. $courseListUrl = api_get_self();
  338. $actions .= '<div class="row">';
  339. $actions .= '<div class="col-md-2">';
  340. $actions .= '<a href="course_add.php">'.Display::return_icon('new_course.png', get_lang('AddCourse'),'',ICON_SIZE_MEDIUM).'</a> ';
  341. if (api_get_setting('course.course_validation') == 'true') {
  342. $actions .= '<a href="course_request_review.php">'.Display::return_icon('course_request_pending.png', get_lang('ReviewCourseRequests'),'',ICON_SIZE_MEDIUM).'</a>';
  343. }
  344. $actions .= '</div>';
  345. $actions .= '<div class="col-md-4">';
  346. $actions .= $form->returnForm();
  347. $actions .= '</div>';
  348. $actions .= '<div class="col-md-4">';
  349. $actions .= $sessionFilter->returnForm();
  350. $actions .= '</div>';
  351. $actions .= '<div class="col-md-2">';
  352. $actions .= '<div class="pull-right">';
  353. $actions .= $advanced;
  354. $actions .= '</div>';
  355. $actions .= '</div>';
  356. $actions .= '</div>';
  357. $actions .= '
  358. <script>
  359. $(function() {
  360. $("#session_name").on("change", function() {
  361. var sessionId = $(this).val();
  362. if (!sessionId) {
  363. return;
  364. }
  365. window.location = "'.$courseListUrl.'?session_id="+sessionId;
  366. });
  367. });
  368. </script>';
  369. if (isset($_GET['session_id']) && !empty($_GET['session_id'])) {
  370. // Create a sortable table with the course data filtered by session
  371. $table = new SortableTable('courses', 'get_number_of_courses', 'get_course_data_by_session', 2);
  372. } else {
  373. // Create a sortable table with the course data
  374. $table = new SortableTable('courses', 'get_number_of_courses', 'get_course_data', 2, 20, 'ASC', 'course-list');
  375. }
  376. $parameters=array();
  377. if (isset ($_GET['keyword'])) {
  378. $parameters = array ('keyword' => Security::remove_XSS($_GET['keyword']));
  379. } elseif (isset ($_GET['keyword_code'])) {
  380. $parameters['keyword_code'] = Security::remove_XSS($_GET['keyword_code']);
  381. $parameters['keyword_title'] = Security::remove_XSS($_GET['keyword_title']);
  382. $parameters['keyword_category'] = Security::remove_XSS($_GET['keyword_category']);
  383. $parameters['keyword_language'] = Security::remove_XSS($_GET['keyword_language']);
  384. $parameters['keyword_visibility'] = Security::remove_XSS($_GET['keyword_visibility']);
  385. $parameters['keyword_subscribe'] = Security::remove_XSS($_GET['keyword_subscribe']);
  386. $parameters['keyword_unsubscribe'] = Security::remove_XSS($_GET['keyword_unsubscribe']);
  387. }
  388. $table->set_additional_parameters($parameters);
  389. $table->set_header(0, '', false, 'width="8px"');
  390. $table->set_header(1, get_lang('Title'), true, null, array('class' => 'title'));
  391. $table->set_header(2, get_lang('Code'));
  392. $table->set_header(3, get_lang('Language'), false, 'width="70px"');
  393. $table->set_header(4, get_lang('Category'));
  394. $table->set_header(5, get_lang('SubscriptionAllowed'), true, 'width="60px"');
  395. $table->set_header(6, get_lang('UnsubscriptionAllowed'), false, 'width="50px"');
  396. //$table->set_header(7, get_lang('Teacher'));
  397. $table->set_header(7, get_lang('Action'), false, null, array('class'=>'td_actions'));
  398. $table->set_column_filter(7, 'modify_courses_filter');
  399. $table->set_form_actions(array('delete_courses' => get_lang('DeleteCourse')), 'course');
  400. $content .= $table->return_table();
  401. }
  402. echo $actions;
  403. echo $content;