sort_my_courses.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. $cidReset = true; // Flag forcing the 'current course' reset
  4. require_once __DIR__.'/../inc/global.inc.php';
  5. api_block_anonymous_users();
  6. $auth = new Auth();
  7. $user_course_categories = CourseManager::get_user_course_categories(api_get_user_id());
  8. $courses_in_category = $auth->get_courses_in_category();
  9. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
  10. $currentUrl = api_get_self();
  11. $interbreadcrumb[] = [
  12. 'url' => api_get_self(),
  13. 'name' => get_lang('Sort courses'),
  14. ];
  15. // We are moving the course of the user to a different user defined course category (=Sort My Courses).
  16. if (isset($_POST['submit_change_course_category'])) {
  17. $result = $auth->updateCourseCategory($_POST['course_2_edit_category'], $_POST['course_categories']);
  18. if ($result) {
  19. Display::addFlash(
  20. Display::return_message(get_lang('The course has been added to the category'))
  21. );
  22. }
  23. header('Location: '.api_get_self());
  24. exit;
  25. }
  26. // We edit course category
  27. if (isset($_POST['submit_edit_course_category']) &&
  28. isset($_POST['title_course_category'])
  29. ) {
  30. $result = $auth->store_edit_course_category($_POST['title_course_category'], $_POST['category_id']);
  31. if ($result) {
  32. Display::addFlash(
  33. Display::return_message(get_lang('Category updated'))
  34. );
  35. }
  36. header('Location: '.api_get_self());
  37. exit;
  38. }
  39. // We are creating a new user defined course category (= Create Course Category).
  40. if (isset($_POST['create_course_category']) &&
  41. isset($_POST['title_course_category']) &&
  42. strlen(trim($_POST['title_course_category'])) > 0
  43. ) {
  44. $result = $auth->store_course_category($_POST['title_course_category']);
  45. if ($result) {
  46. Display::addFlash(
  47. Display::return_message(get_lang('Course category is created'))
  48. );
  49. } else {
  50. Display::addFlash(
  51. Display::return_message(
  52. get_lang('A course category with the same name already exists.'),
  53. 'error'
  54. )
  55. );
  56. }
  57. header('Location: '.api_get_self());
  58. exit;
  59. }
  60. // We are moving a course or category of the user up/down the list (=Sort My Courses).
  61. if (isset($_GET['move'])) {
  62. if (isset($_GET['course'])) {
  63. $result = $auth->move_course($_GET['move'], $_GET['course'], $_GET['category']);
  64. if ($result) {
  65. Display::addFlash(
  66. Display::return_message(get_lang('Courses sorted'))
  67. );
  68. }
  69. }
  70. if (isset($_GET['category']) && !isset($_GET['course'])) {
  71. $result = $auth->move_category($_GET['move'], $_GET['category']);
  72. if ($result) {
  73. Display::addFlash(
  74. Display::return_message(get_lang('Category sorting done'))
  75. );
  76. }
  77. }
  78. header('Location: '.api_get_self());
  79. exit;
  80. }
  81. switch ($action) {
  82. case 'edit_category':
  83. $categoryId = isset($_GET['category_id']) ? (int) $_GET['category_id'] : 0;
  84. $categoryInfo = $auth->getUserCourseCategory($categoryId);
  85. if ($categoryInfo) {
  86. $categoryName = $categoryInfo['title'];
  87. $form = new FormValidator(
  88. 'edit_course_category',
  89. 'post',
  90. $currentUrl.'?action=edit_category'
  91. );
  92. $form->addText('title_course_category', get_lang('Name'));
  93. $form->addHidden('category_id', $categoryId);
  94. $form->addButtonSave(get_lang('Edit'), 'submit_edit_course_category');
  95. $form->setDefaults(['title_course_category' => $categoryName]);
  96. $form->display();
  97. }
  98. exit;
  99. break;
  100. case 'edit_course_category':
  101. $edit_course = (int) $_GET['course_id'];
  102. $defaultCategoryId = isset($_GET['category_id']) ? (int) $_GET['category_id'] : 0;
  103. $courseInfo = api_get_course_info_by_id($edit_course);
  104. if (empty($courseInfo)) {
  105. exit;
  106. }
  107. $form = new FormValidator(
  108. 'edit_course_category',
  109. 'post',
  110. $currentUrl.'?action=edit_course_category'
  111. );
  112. $form->addHeader($courseInfo['title']);
  113. $options = [];
  114. foreach ($user_course_categories as $row) {
  115. $options[$row['id']] = $row['title'];
  116. }
  117. asort($options);
  118. $form->addSelect(
  119. 'course_categories',
  120. get_lang('Categories'),
  121. $options,
  122. ['disable_js' => true, 'placeholder' => get_lang('Please select an option')]
  123. );
  124. $form->addHidden('course_2_edit_category', $edit_course);
  125. if (!empty($defaultCategoryId)) {
  126. $form->setDefaults(['course_categories' => $defaultCategoryId]);
  127. }
  128. $form->addButtonSave(get_lang('Save'), 'submit_change_course_category');
  129. $form->display();
  130. exit;
  131. break;
  132. case 'deletecoursecategory':
  133. // we are deleting a course category
  134. if (isset($_GET['id'])) {
  135. if (Security::check_token('get')) {
  136. $result = $auth->delete_course_category($_GET['id']);
  137. if ($result) {
  138. Display::addFlash(
  139. Display::return_message(get_lang('The category was deleted'))
  140. );
  141. }
  142. }
  143. }
  144. header('Location: '.api_get_self());
  145. exit;
  146. break;
  147. case 'createcoursecategory':
  148. $form = new FormValidator(
  149. 'create_course_category',
  150. 'post',
  151. $currentUrl.'?action=createcoursecategory'
  152. );
  153. $form->addText('title_course_category', get_lang('Name'));
  154. $form->addButtonSave(get_lang('Add category'), 'create_course_category');
  155. $form->display();
  156. exit;
  157. break;
  158. case 'set_collapsable':
  159. if (!api_get_configuration_value('allow_user_course_category_collapsable')) {
  160. api_not_allowed(true);
  161. }
  162. $userId = api_get_user_id();
  163. $categoryId = isset($_REQUEST['categoryid']) ? (int) $_REQUEST['categoryid'] : 0;
  164. $option = isset($_REQUEST['option']) ? (int) $_REQUEST['option'] : 0;
  165. $redirect = isset($_REQUEST['redirect']) ? $_REQUEST['redirect'] : 0;
  166. if (empty($userId) || empty($categoryId)) {
  167. api_not_allowed(true);
  168. }
  169. $table = Database::get_main_table(TABLE_USER_COURSE_CATEGORY);
  170. $sql = "UPDATE $table
  171. SET collapsed = $option
  172. WHERE user_id = $userId AND id = $categoryId";
  173. Database::query($sql);
  174. Display::addFlash(Display::return_message(get_lang('Update successful')));
  175. if ($redirect === 'home') {
  176. $url = api_get_path(WEB_PATH).'user_portal.php';
  177. header('Location: '.$url);
  178. exit;
  179. }
  180. $url = api_get_self();
  181. header('Location: '.$url);
  182. exit;
  183. break;
  184. }
  185. Display::display_header();
  186. $stok = Security::get_token();
  187. $courses_without_category = isset($courses_in_category[0]) ? $courses_in_category[0] : null;
  188. echo '<div id="actions" class="actions">';
  189. if ($action != 'createcoursecategory') {
  190. echo '<a class="ajax" href="'.$currentUrl.'?action=createcoursecategory">';
  191. echo Display::return_icon('new_folder.png', get_lang('Create a personal courses category'), '', '32');
  192. echo '</a>';
  193. }
  194. echo '</div>';
  195. if (!empty($message)) {
  196. echo Display::return_message($message, 'confirm', false);
  197. }
  198. $allowCollapsable = api_get_configuration_value('allow_user_course_category_collapsable');
  199. $teachersIcon = Display::return_icon('teacher.png', get_lang('Trainers'), null, ICON_SIZE_TINY);
  200. // COURSES WITH CATEGORIES
  201. if (!empty($user_course_categories)) {
  202. $counter = 0;
  203. $last = end($user_course_categories);
  204. foreach ($user_course_categories as $row) {
  205. echo Display::page_subheader($row['title']);
  206. echo '<a name="category'.$row['id'].'"></a>';
  207. $url = $currentUrl.'?categoryid='.$row['id'].'&sec_token='.$stok;
  208. if ($allowCollapsable) {
  209. if (isset($row['collapsed']) && $row['collapsed'] == 0) {
  210. echo Display::url(
  211. '<i class="fa fa-folder-open"></i>',
  212. $url.'&action=set_collapsable&option=1'
  213. );
  214. } else {
  215. echo Display::url(
  216. '<i class="fa fa-folder"></i>',
  217. $url.'&action=set_collapsable&option=0'
  218. );
  219. }
  220. }
  221. echo Display::url(
  222. Display::return_icon('edit.png', get_lang('Edit'), '', 22),
  223. $currentUrl.'?action=edit_category&category_id='.$row['id'].'&sec_token='.$stok,
  224. ['class' => 'ajax']
  225. );
  226. if (0 != $counter) {
  227. echo Display::url(
  228. Display::return_icon('up.png', get_lang('Up'), '', 22),
  229. $currentUrl.'?move=up&category='.$row['id'].'&sec_token='.$stok
  230. );
  231. } else {
  232. echo Display::return_icon('up_na.png', get_lang('Up'), '', 22);
  233. }
  234. if ($row['id'] != $last['id']) {
  235. echo Display::url(
  236. Display::return_icon('down.png', get_lang('down'), '', 22),
  237. $currentUrl.'?move=down&category='.$row['id'].'&sec_token='.$stok
  238. );
  239. } else {
  240. echo Display::return_icon('down_na.png', get_lang('down'), '', 22);
  241. }
  242. echo Display::url(
  243. Display::return_icon(
  244. 'delete.png',
  245. get_lang('Delete'),
  246. [
  247. 'onclick' => "javascript: if (!confirm('".addslashes(
  248. api_htmlentities(
  249. get_lang('Are you sure you want to delete this courses category? Courses inside this category will be moved outside the categories'),
  250. ENT_QUOTES,
  251. api_get_system_encoding()
  252. )
  253. )."')) return false;",
  254. ],
  255. 22
  256. ),
  257. $currentUrl.'?action=deletecoursecategory&id='.$row['id'].'&sec_token='.$stok
  258. );
  259. $counter++;
  260. echo '<br /><br />';
  261. // Show the courses inside this category
  262. echo '<table class="data_table">';
  263. $number_of_courses = isset($courses_in_category[$row['id']]) ? count($courses_in_category[$row['id']]) : 0;
  264. $key = 0;
  265. if (!empty($courses_in_category[$row['id']])) {
  266. foreach ($courses_in_category[$row['id']] as $course) {
  267. echo '<tr><td>';
  268. echo '<a name="course'.$course['code'].'"></a>';
  269. echo '<strong>'.$course['title'].'</strong>';
  270. echo ' ('.$course['visual_code'].')';
  271. echo '<br />';
  272. echo $teachersIcon;
  273. echo '&nbsp;';
  274. echo CourseManager::getTeacherListFromCourseCodeToString($course['code']);
  275. echo '<br />';
  276. if (api_get_setting('display_teacher_in_courselist') === 'true') {
  277. echo $course['tutor'];
  278. }
  279. echo '</td><td valign="top">'; ?>
  280. <div style="float:left;width:110px;">
  281. <?php
  282. if (api_get_setting('show_courses_descriptions_in_catalog') == 'true') {
  283. $icon_title = get_lang('Course description').' - '.$course['title']; ?>
  284. <a href="<?php echo api_get_path(WEB_CODE_PATH); ?>inc/ajax/course_home.ajax.php?a=show_course_information&code=<?php echo $course['code']; ?>" data-title="<?php echo $icon_title; ?>" title="<?php echo $icon_title; ?>" class="ajax">
  285. <?php
  286. echo Display::return_icon('info.png', $icon_title, '', '22');
  287. } ?>
  288. </a>
  289. <?php
  290. echo Display::url(
  291. Display::return_icon('edit.png', get_lang('Edit'), '', 22),
  292. $currentUrl.'?action=edit_course_category&category_id='.$row['id'].'&course_id='.$course['real_id'].'&sec_token='.$stok,
  293. ['class' => 'ajax']
  294. );
  295. if ($key > 0) {
  296. ?>
  297. <a href="<?php echo $currentUrl; ?>?action=<?php echo $action; ?>&amp;move=up&amp;course=<?php echo $course['code']; ?>&amp;category=<?php echo $course['user_course_cat']; ?>&amp;sec_token=<?php echo $stok; ?>">
  298. <?php echo Display::display_icon('up.png', get_lang('Up'), '', 22); ?>
  299. </a>
  300. <?php
  301. } else {
  302. echo Display::display_icon('up_na.png', get_lang('Up'), '', 22);
  303. }
  304. if ($key < $number_of_courses - 1) {
  305. ?>
  306. <a href="<?php echo $currentUrl; ?>?action=<?php echo $action; ?>&amp;move=down&amp;course=<?php echo $course['code']; ?>&amp;category=<?php echo $course['user_course_cat']; ?>&amp;sec_token=<?php echo $stok; ?>">
  307. <?php echo Display::return_icon('down.png', get_lang('down'), '', 22); ?>
  308. </a>
  309. <?php
  310. } else {
  311. echo Display::return_icon('down_na.png', get_lang('down'), '', 22);
  312. } ?>
  313. </div>
  314. <div style="float:left; margin-right:10px;">
  315. <?php
  316. if ($course['status'] != 1) {
  317. if ($course['unsubscr'] == 1) {
  318. ?>
  319. <form action="<?php echo api_get_self(); ?>" method="post" onsubmit="javascript: if (!confirm('<?php echo addslashes(api_htmlentities(get_lang("Are you sure you want to unsubscribe?"), ENT_QUOTES, api_get_system_encoding())); ?>')) return false">
  320. <input type="hidden" name="sec_token" value="<?php echo $stok; ?>">
  321. <input type="hidden" name="unsubscribe" value="<?php echo $course['code']; ?>" />
  322. <button class="btn btn-default" value="<?php echo get_lang('Unsubscribe'); ?>" name="unsub">
  323. <?php echo get_lang('Unsubscribe'); ?>
  324. </button>
  325. </form>
  326. </div>
  327. <?php
  328. }
  329. }
  330. $key++;
  331. }
  332. echo '</table>';
  333. }
  334. }
  335. }
  336. echo Display::page_subheader(get_lang('No courses category'));
  337. echo '<table class="data_table">';
  338. // COURSES WITHOUT CATEGORY
  339. if (!empty($courses_without_category)) {
  340. $number_of_courses = count($courses_without_category);
  341. $key = 0;
  342. foreach ($courses_without_category as $course) {
  343. echo '<tr><td>';
  344. echo '<a name="course'.$course['code'].'"></a>';
  345. echo '<strong>'.$course['title'].'</strong>';
  346. echo ' ('.$course['visual_code'].')';
  347. echo '<br />';
  348. echo $teachersIcon;
  349. echo '&nbsp;';
  350. echo CourseManager::getTeacherListFromCourseCodeToString($course['code']);
  351. echo '<br />';
  352. if (api_get_setting('display_teacher_in_courselist') === 'true') {
  353. echo $course['tutor'];
  354. }
  355. echo '</td><td valign="top">'; ?>
  356. <div style="float:left; width:110px">
  357. <?php
  358. if (api_get_setting('show_courses_descriptions_in_catalog') == 'true') {
  359. $icon_title = get_lang('Course description').' - '.$course['title']; ?>
  360. <a href="<?php echo api_get_path(WEB_CODE_PATH); ?>inc/ajax/course_home.ajax.php?a=show_course_information&code=<?php echo $course['code']; ?>" data-title="<?php echo $icon_title; ?>" title="<?php echo $icon_title; ?>" class="ajax">
  361. <?php echo Display::return_icon('info.png', $icon_title, '', '22'); ?>
  362. </a>
  363. <?php
  364. }
  365. echo '';
  366. if (isset($_GET['edit']) && $course['code'] == $_GET['edit']) {
  367. echo Display::return_icon('edit_na.png', get_lang('Edit'), '', 22);
  368. } else {
  369. echo Display::url(
  370. Display::return_icon('edit.png', get_lang('Edit'), '', 22),
  371. $currentUrl.'?action=edit_course_category&course_id='.$course['real_id'].'&'.$stok,
  372. ['class' => 'ajax']
  373. );
  374. }
  375. if ($key > 0) {
  376. ?>
  377. <a href="<?php echo $currentUrl; ?>?action=<?php echo $action; ?>&amp;move=up&amp;course=<?php echo $course['code']; ?>&amp;category=<?php echo $course['user_course_cat']; ?>&amp;sec_token=<?php echo $stok; ?>">
  378. <?php echo Display::display_icon('up.png', get_lang('Up'), '', 22); ?>
  379. </a>
  380. <?php
  381. } else {
  382. echo Display::return_icon('up_na.png', get_lang('Up'), '', 22);
  383. }
  384. if ($key < $number_of_courses - 1) {
  385. ?>
  386. <a href="<?php echo $currentUrl; ?>?action=<?php echo $action; ?>&amp;move=down&amp;course=<?php echo $course['code']; ?>&amp;category=<?php echo $course['user_course_cat']; ?>&amp;sec_token=<?php echo $stok; ?>">
  387. <?php echo Display::display_icon('down.png', get_lang('down'), '', 22); ?>
  388. </a>
  389. <?php
  390. } else {
  391. echo Display::return_icon('down_na.png', get_lang('down'), '', 22);
  392. } ?>
  393. </div>
  394. <div style="float:left; margin-right:10px;">
  395. <?php
  396. if ($course['status'] != 1) {
  397. if ($course['unsubscr'] == 1) {
  398. ?>
  399. <!-- changed link to submit to avoid action by the search tool indexer -->
  400. <form action="<?php echo api_get_self(); ?>" method="post" onsubmit="javascript: if (!confirm('<?php echo addslashes(api_htmlentities(get_lang("Are you sure you want to unsubscribe?"), ENT_QUOTES, api_get_system_encoding())); ?>')) return false;">
  401. <input type="hidden" name="sec_token" value="<?php echo $stok; ?>">
  402. <input type="hidden" name="unsubscribe" value="<?php echo $course['code']; ?>" />
  403. <button class="btn btn-default" value="<?php echo get_lang('Unsubscribe'); ?>" name="unsub">
  404. <?php echo get_lang('Unsubscribe'); ?>
  405. </button>
  406. </form>
  407. </div>
  408. <?php
  409. }
  410. } ?>
  411. </td>
  412. </tr>
  413. <?php
  414. $key++;
  415. }
  416. }
  417. ?>
  418. </table>
  419. <?php
  420. Display::display_footer();