courses_categories.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * View (MVC patter) for courses categories
  5. * @author Christian Fasanando <christian1827@gmail.com> - Beeznest
  6. * @package chamilo.auth
  7. */
  8. if (isset($_REQUEST['action']) && Security::remove_XSS($_REQUEST['action']) !== 'subscribe') {
  9. $stok = Security::get_token();
  10. } else {
  11. $stok = Security::getTokenFromSession();
  12. }
  13. $showCourses = CoursesAndSessionsCatalog::showCourses();
  14. $showSessions = CoursesAndSessionsCatalog::showSessions();
  15. $pageCurrent = isset($pageCurrent) ? $pageCurrent : isset($_GET['pageCurrent']) ? intval($_GET['pageCurrent']) : 1;
  16. $pageLength = isset($pageLength) ? $pageLength : isset($_GET['pageLength']) ? intval($_GET['pageLength']) : CoursesAndSessionsCatalog::PAGE_LENGTH;
  17. $pageTotal = intval(ceil(intval($countCoursesInCategory) / $pageLength));
  18. $cataloguePagination = $pageTotal > 1 ? CourseCategory::getCatalogPagination($pageCurrent, $pageLength, $pageTotal) : '';
  19. $searchTerm = isset($_REQUEST['search_term']) ? Security::remove_XSS($_REQUEST['search_term']) : '';
  20. if ($showSessions && isset($_POST['date'])) {
  21. $date = $_POST['date'];
  22. } else {
  23. $date = date('Y-m-d');
  24. }
  25. $userInfo = api_get_user_info();
  26. $code = isset($code) ? $code : null;
  27. ?>
  28. <script>
  29. $(document).ready( function() {
  30. $('.star-rating li a').on('click', function(event) {
  31. var id = $(this).parents('ul').attr('id');
  32. $('#vote_label2_' + id).html("<?php echo get_lang('Loading'); ?>");
  33. $.ajax({
  34. url: $(this).attr('data-link'),
  35. success: function(data) {
  36. $("#rating_wrapper_"+id).html(data);
  37. if(data == 'added') {
  38. //$('#vote_label2_' + id).html("{'Saved'|get_lang}");
  39. }
  40. if(data == 'updated') {
  41. //$('#vote_label2_' + id).html("{'Saved'|get_lang}");
  42. }
  43. }
  44. });
  45. });
  46. var getSessionId = function (el) {
  47. var parts = el.id.split('_');
  48. return parseInt(parts[1], 10);
  49. };
  50. <?php if ($showSessions) { ?>
  51. $('#date').datepicker({
  52. dateFormat: 'yy-mm-dd'
  53. });
  54. <?php } ?>
  55. });
  56. </script>
  57. <div class="row">
  58. <div class="col-md-12">
  59. <h2 class="title-courses"><?php echo get_lang('CourseManagement'); ?></h2>
  60. <div class="search-courses">
  61. <div class="row">
  62. <?php if ($showCourses) { ?>
  63. <div class="col-md-<?php echo($showSessions ? '4' : '6'); ?>">
  64. <?php if (!isset($_GET['hidden_links']) || intval($_GET['hidden_links']) != 1) { ?>
  65. <form method="post"
  66. action="<?php echo CourseCategory::getCourseCategoryUrl(1, $pageLength, 'ALL', 0, 'subscribe'); ?>">
  67. <input type="hidden" name="sec_token" value="<?php echo $stok; ?>">
  68. <input type="hidden" name="search_course" value="1"/>
  69. <label><?php echo get_lang('Search'); ?></label>
  70. <div class="input-group">
  71. <input class="form-control" type="text" name="search_term"
  72. value="<?php echo(empty($_POST['search_term'])
  73. ? ''
  74. : api_htmlentities($searchTerm)); ?>"/>
  75. <div class="input-group-btn">
  76. <button class="btn btn-default" type="submit">
  77. <em class="fa fa-search"></em> <?php echo get_lang('Search'); ?>
  78. </button>
  79. </div>
  80. </div>
  81. </form>
  82. <?php } ?>
  83. </div>
  84. <div class="col-md-<?php echo($showSessions ? '4' : '6'); ?>">
  85. <?php
  86. $webAction = api_get_path(WEB_CODE_PATH).'auth/courses.php';
  87. $action = !empty($_REQUEST['action'])
  88. ? Security::remove_XSS($_REQUEST['action'])
  89. : 'display_courses';
  90. $pageLength = !empty($_REQUEST['pageLength'])
  91. ? intval($_REQUEST['pageLength'])
  92. : CoursesAndSessionsCatalog::PAGE_LENGTH;
  93. $pageCurrent = !empty($_REQUEST['pageCurrent']) ? intval($_REQUEST['pageCurrent']) : 1;
  94. $form = '<form action="'.$webAction.'" method="GET">';
  95. $form .= '<input type="hidden" name="action" value="'.$action.'">';
  96. $form .= '<input type="hidden" name="pageCurrent" value="'.$pageCurrent.'">';
  97. $form .= '<input type="hidden" name="pageLength" value="'.$pageLength.'">';
  98. $form .= '<div class="form-group">';
  99. $form .= '<label>'.get_lang('CourseCategories').'</label>';
  100. $form .= '<select name="category_code" onchange="submit();" class="selectpicker show-tick form-control">';
  101. $codeType = isset($_REQUEST['category_code'])
  102. ? Security::remove_XSS($_REQUEST['category_code'])
  103. : '';
  104. foreach ($browse_course_categories[0] as $category) {
  105. $categoryCode = $category['code'];
  106. $countCourse = $category['count_courses'];
  107. $form .= '<option '.($categoryCode == $codeType ? 'selected="selected" ' : '')
  108. .' value="'.$category['code'].'">'.$category['name'].' ('.$countCourse.') </option>';
  109. if (!empty($browse_course_categories[$categoryCode])) {
  110. foreach ($browse_course_categories[$categoryCode] as $subCategory) {
  111. $subCategoryCode = $subCategory['code'];
  112. $form .= '<option '
  113. .($subCategoryCode == $codeType
  114. ? 'selected="selected" '
  115. : '')
  116. .' value="'.$subCategory['code'].'">---'.$subCategory['name'].' ('.$subCategory['count_courses'].')</option>';
  117. }
  118. }
  119. }
  120. $form .= '</select>';
  121. $form .= '</div>';
  122. $form .= '</form>';
  123. echo $form;
  124. ?>
  125. </div>
  126. <?php } ?>
  127. <?php if ($showSessions) { ?>
  128. <div class="col-md-4">
  129. <div class="return-catalog">
  130. <a class="btn btn-default btn-lg btn-block"
  131. href="<?php echo CourseCategory::getCourseCategoryUrl(1, $pageLength, null, 0, 'display_sessions'); ?>">
  132. <em class="fa fa-arrow-right"></em> <?php echo get_lang('SessionList'); ?>
  133. </a>
  134. </div>
  135. </div>
  136. <?php } ?>
  137. </div>
  138. </div>
  139. </div>
  140. </div>
  141. <?php
  142. if ($showCourses && $action != 'display_sessions') {
  143. if (!empty($message)) {
  144. echo Display::return_message($message, 'confirmation', false);
  145. }
  146. if (!empty($error)) {
  147. echo Display::return_message($error, 'error', false);
  148. }
  149. if (!empty($content)) {
  150. echo $content;
  151. }
  152. if (!empty($searchTerm)) {
  153. echo "<p><strong>".get_lang('SearchResultsFor')." ".$searchTerm."</strong><br />";
  154. }
  155. $ajax_url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=add_course_vote';
  156. $user_id = api_get_user_id();
  157. $categoryList = CourseManager::getCategoriesList();
  158. if (!empty($browse_courses_in_category)) {
  159. echo '<div class="grid-courses row">';
  160. foreach ($browse_courses_in_category as $course) {
  161. $course_hidden = $course['visibility'] == COURSE_VISIBILITY_HIDDEN;
  162. if ($course_hidden) {
  163. continue;
  164. }
  165. $userRegisteredInCourse = CourseManager::is_user_subscribed_in_course($user_id, $course['code']);
  166. $userRegisteredInCourseAsTeacher = CourseManager::is_course_teacher($user_id, $course['code']);
  167. $userRegistered = $userRegisteredInCourse && $userRegisteredInCourseAsTeacher;
  168. $course_public = $course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD;
  169. $course_open = $course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM;
  170. $course_private = $course['visibility'] == COURSE_VISIBILITY_REGISTERED;
  171. $course_closed = $course['visibility'] == COURSE_VISIBILITY_CLOSED;
  172. $course_subscribe_allowed = $course['subscribe'] == 1;
  173. $course_unsubscribe_allowed = $course['unsubscribe'] == 1;
  174. $count_connections = $course['count_connections'];
  175. $creation_date = substr($course['creation_date'], 0, 10);
  176. // display the course bloc
  177. $html = '<div class="col-xs-12 col-sm-6 col-md-4"><div class="items items-courses">';
  178. $course['category_title'] = '';
  179. if (isset($course['category'])) {
  180. $course['category_title'] = isset($categoryList[$course['category']]) ? $categoryList[$course['category']] : '';
  181. }
  182. // display thumbnail
  183. $html .= returnThumbnail($course, $userRegistered);
  184. $separator = null;
  185. $subscribeButton = return_register_button($course, $stok, $code, $searchTerm);
  186. // start buycourse validation
  187. // display the course price and buy button if the buycourses plugin is enabled and this course is configured
  188. $plugin = BuyCoursesPlugin::create();
  189. $isThisCourseInSale = $plugin->buyCoursesForGridCatalogValidator(
  190. $course['real_id'],
  191. BuyCoursesPlugin::PRODUCT_TYPE_COURSE
  192. );
  193. if ($isThisCourseInSale) {
  194. // set the Price label
  195. $separator = $isThisCourseInSale['html'];
  196. // set the Buy button instead register.
  197. if ($isThisCourseInSale['verificator']) {
  198. $subscribeButton = $plugin->returnBuyCourseButton(
  199. $course['real_id'],
  200. BuyCoursesPlugin::PRODUCT_TYPE_COURSE
  201. );
  202. }
  203. }
  204. // end buycourse validation
  205. // display course title and button bloc
  206. $html .= '<div class="description">';
  207. $html .= return_title($course, $userRegisteredInCourse);
  208. $html .= return_teacher($course);
  209. // display button line
  210. $html .= '<div class="toolbar row">';
  211. $html .= $separator ? '<div class="col-sm-4">'.$separator.'</div>' : '';
  212. $html .= '<div class="col-sm-8">';
  213. // if user registered as student
  214. if ($userRegisteredInCourse) {
  215. $html .= return_already_registered_label('student');
  216. if (!$course_closed) {
  217. if ($course_unsubscribe_allowed) {
  218. $html .= return_unregister_button($course, $stok, $searchTerm, $code);
  219. }
  220. }
  221. } elseif ($userRegisteredInCourseAsTeacher) {
  222. // if user registered as teacher
  223. if ($course_unsubscribe_allowed) {
  224. $html .= return_unregister_button($course, $stok, $searchTerm, $code);
  225. }
  226. } else {
  227. // if user not registered in the course
  228. if (!$course_closed) {
  229. if (!$course_private) {
  230. if ($course_subscribe_allowed) {
  231. $html .= $subscribeButton;
  232. }
  233. }
  234. }
  235. }
  236. $html .= '</div>';
  237. $html .= '</div>';
  238. $html .= '</div>';
  239. $html .= '</div>';
  240. $html .= '</div>';
  241. echo $html;
  242. }
  243. echo '</div>';
  244. } else {
  245. if (!isset($_REQUEST['subscribe_user_with_password']) &&
  246. !isset($_REQUEST['subscribe_course'])
  247. ) {
  248. echo Display::return_message(
  249. get_lang('ThereAreNoCoursesInThisCategory'),
  250. 'warning'
  251. );
  252. }
  253. }
  254. }
  255. echo '<div class="col-md-12">';
  256. echo $cataloguePagination;
  257. echo '</div>';
  258. /**
  259. * Display the course catalog image of a course
  260. * @param array $course
  261. * @param bool $registeredUser
  262. *
  263. * @return string HTML string
  264. */
  265. function returnThumbnail($course, $registeredUser)
  266. {
  267. $html = '';
  268. $title = cut($course['title'], 70);
  269. $linkCourse = api_get_course_url($course['code']);
  270. // course path
  271. $course_path = api_get_path(SYS_COURSE_PATH).$course['directory'];
  272. if (file_exists($course_path.'/course-pic.png')) {
  273. $course_medium_image = api_get_path(WEB_COURSE_PATH).$course['directory'].'/course-pic.png'; // redimensioned image 85x85
  274. } else {
  275. // without picture
  276. $course_medium_image = Display::return_icon(
  277. 'session_default.png',
  278. null,
  279. null,
  280. null,
  281. null,
  282. true
  283. );
  284. }
  285. $html .= '<div class="image">';
  286. if (!$registeredUser) {
  287. $html .= '<img class="img-responsive"'
  288. .' src="'.$course_medium_image.'" '
  289. .' alt="'.api_htmlentities($title).'"/>';
  290. } else {
  291. $html .= '<a href="'.$linkCourse.'" title="'.$course['title'].'">'
  292. .'<img class="img-responsive" src="'.$course_medium_image.'" '
  293. .'alt="'.api_htmlentities($title).'"/></a>';
  294. }
  295. $categoryTitle = isset($course['category_title']) ? $course['category_title'] : '';
  296. if (!empty($categoryTitle)) {
  297. $html .= '<span class="category">'.$categoryTitle.'</span>';
  298. $html .= '<div class="cribbon"></div>';
  299. }
  300. $html .= '<div class="user-actions">';
  301. $html .= return_description_button($course);
  302. $html .= '</div></div>';
  303. return $html;
  304. }
  305. /**
  306. * @param array $courseInfo
  307. * @return string
  308. */
  309. function return_teacher($courseInfo)
  310. {
  311. $teachers = CourseManager::getTeachersFromCourse($courseInfo['real_id']);
  312. $length = count($teachers);
  313. if (!$length) {
  314. return '';
  315. }
  316. $html = '<div class="block-author">';
  317. foreach ($teachers as $value) {
  318. $name = $value['firstname'].' '.$value['lastname'];
  319. if ($length > 2) {
  320. $html .= '<a href="'.$value['url'].'" class="ajax" data-title="'.$name.'" title="'.$name.'">
  321. <img src="'.$value['avatar'].'" alt="'.get_lang('UserPicture').'"/></a>';
  322. } else {
  323. $html .= '<a href="'.$value['url'].'" class="ajax" data-title="'.$name.'" title="'.$name.'">
  324. <img src="'.$value['avatar'].'" alt="'.get_lang('UserPicture').'"/></a>';
  325. $html .= '<div class="teachers-details"><h5>
  326. <a href="'.$value['url'].'" class="ajax" data-title="'.$name.'">'
  327. . $name.'</a></h5><p>'.get_lang('Teacher').'</p></div>';
  328. }
  329. }
  330. $html .= '</div>';
  331. return $html;
  332. }
  333. /**
  334. * Display the title of a course in course catalog
  335. * @param array $course
  336. * @param bool $registeredUser
  337. *
  338. * @return string HTML string
  339. */
  340. function return_title($course, $registeredUser)
  341. {
  342. $html = '';
  343. $linkCourse = api_get_course_url($course['code']);
  344. $html .= '<div class="block-title"><h4 class="title">';
  345. if (!$registeredUser) {
  346. $html .= $course['title'];
  347. } else {
  348. $html .= '<a title="'.$course['title'].'" href="'.$linkCourse.'">'.$course['title'].'</a>';
  349. }
  350. $html .= '</h4></div>';
  351. if (api_get_configuration_value('hide_course_rating') === false) {
  352. $ajax_url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=add_course_vote';
  353. $rating = Display::return_rating_system(
  354. 'star_'.$course['real_id'],
  355. $ajax_url.'&course_id='.$course['real_id'],
  356. $course['point_info']
  357. );
  358. $html .= '<div class="ranking">'.$rating.'</div>';
  359. }
  360. return $html;
  361. }
  362. /**
  363. * Display the description button of a course in the course catalog
  364. * @param array $course
  365. *
  366. * @return string HTML string
  367. */
  368. function return_description_button($course)
  369. {
  370. $title = $course['title'];
  371. $html = '';
  372. if (api_get_setting('show_courses_descriptions_in_catalog') == 'true') {
  373. $html = Display::url(
  374. Display::returnFontAwesomeIcon('info-circle'),
  375. api_get_path(WEB_CODE_PATH).'inc/ajax/course_home.ajax.php?a=show_course_information&code='.$course['code'],
  376. array(
  377. 'class' => 'ajax btn btn-default btn-sm',
  378. 'data-title' => $title,
  379. 'title' => get_lang('Description'),
  380. 'aria-label' => get_lang('Description'),
  381. 'data-size' => 'lg'
  382. )
  383. );
  384. }
  385. return $html;
  386. }
  387. /**
  388. * Display the goto course button of a course in the course catalog
  389. * @param $course
  390. * @return string HTML string
  391. */
  392. function return_goto_button($course)
  393. {
  394. $title = get_lang('GoToCourse');
  395. $html = Display::url(
  396. Display::returnFontAwesomeIcon('share'),
  397. api_get_course_url($course['code']),
  398. array(
  399. 'class' => 'btn btn-default btn-sm',
  400. 'title' => $title,
  401. 'aria-label' => $title,
  402. )
  403. );
  404. return $html.PHP_EOL;
  405. }
  406. /**
  407. * Display the already registerd text in a course in the course catalog
  408. * @param $in_status
  409. *
  410. * @return string HTML string
  411. */
  412. function return_already_registered_label($in_status)
  413. {
  414. $icon = '<em class="fa fa-check"></em>';
  415. $title = get_lang("YouAreATeacherOfThisCourse");
  416. if ($in_status == 'student') {
  417. $icon = '<em class="fa fa-check"></em>';
  418. $title = get_lang("AlreadySubscribed");
  419. }
  420. $html = Display::tag(
  421. 'span',
  422. $icon.' '.$title,
  423. array(
  424. 'id' => 'register',
  425. 'class' => 'label-subscribed text-success',
  426. 'title' => $title,
  427. 'aria-label' => $title,
  428. )
  429. );
  430. return $html.PHP_EOL;
  431. }
  432. /**
  433. * Display the register button of a course in the course catalog
  434. * @param $course
  435. * @param $stok
  436. * @param $code
  437. * @param $search_term
  438. *
  439. * @return string
  440. */
  441. function return_register_button($course, $stok, $code, $search_term)
  442. {
  443. $title = get_lang('Subscribe');
  444. $html = Display::url(
  445. Display::returnFontAwesomeIcon('check').' '.$title,
  446. api_get_self().'?action=subscribe_course&sec_token='.$stok.
  447. '&subscribe_course='.$course['code'].'&search_term='.$search_term.'&category_code='.$code,
  448. array('class' => 'btn btn-success btn-sm', 'title' => $title, 'aria-label' => $title)
  449. );
  450. return $html;
  451. }
  452. /**
  453. * Display the unregister button of a course in the course catalog
  454. * @param $course
  455. * @param $stok
  456. * @param $search_term
  457. * @param $code
  458. *
  459. * @return string
  460. */
  461. function return_unregister_button($course, $stok, $search_term, $code)
  462. {
  463. $title = get_lang('Unsubscription');
  464. $html = Display::url(
  465. Display::returnFontAwesomeIcon('sign-in').' '.$title,
  466. api_get_self().'?action=unsubscribe&sec_token='.$stok
  467. .'&unsubscribe='.$course['code'].'&search_term='.$search_term.'&category_code='.$code,
  468. array('class' => 'btn btn-danger btn-sm', 'title' => $title, 'aria-label' => $title)
  469. );
  470. return $html;
  471. }