user_portal.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /*
  4. user_portal.php can be found in:
  5. UserPortalController :: indexAction
  6. Chamilo\CoreBundle\Controller\UserPortalController
  7. */
  8. // @todo change root .htaccess
  9. header('Location: web/app_dev.php/userportal');
  10. exit;
  11. use ChamiloSession as Session;
  12. use Doctrine\Common\Collections\Criteria;
  13. /**
  14. * This is the index file displayed when a user is logged in on Chamilo.
  15. *
  16. * It displays:
  17. * - personal course list
  18. * - menu bar
  19. * Search for CONFIGURATION parameters to modify settings
  20. * @package chamilo.main
  21. * @todo Shouldn't the CONFVAL_ constant be moved to the config page? Has anybody any idea what the are used for?
  22. * If these are really configuration settings then we can add those to the dokeos config settings.
  23. * @todo check for duplication of functions with index.php (user_portal.php is orginally a copy of index.php)
  24. * @todo display_digest, shouldn't this be removed and be made into an extension?
  25. */
  26. /* Flag forcing the 'current course' reset, as we're not inside a course anymore */
  27. $cidReset = true;
  28. // For HTML editor repository.
  29. if (isset($_SESSION['this_section'])) {
  30. unset($_SESSION['this_section']);
  31. }
  32. /* Included libraries */
  33. require_once './main/inc/global.inc.php';
  34. $this_section = SECTION_COURSES;
  35. api_block_anonymous_users(); // Only users who are logged in can proceed.
  36. $userId = api_get_user_id();
  37. /* Constants and CONFIGURATION parameters */
  38. $load_dirs = api_get_setting('show_documents_preview');
  39. $displayMyCourseViewBySessionLink = api_get_setting('my_courses_view_by_session') === 'true';
  40. $nameTools = get_lang('MyCourses');
  41. // Load course notification by ajax
  42. $loadNotificationsByAjax = api_get_configuration_value('user_portal_load_notification_by_ajax');
  43. if ($loadNotificationsByAjax) {
  44. $htmlHeadXtra[] = '<script>
  45. $(function() {
  46. $(".course_notification").each(function(index) {
  47. var div = $(this);
  48. var id = $(this).attr("id");
  49. var idList = id.split("_");
  50. var courseId = idList[1];
  51. var sessionId = idList[2];
  52. var status = idList[3];
  53. $.ajax({
  54. type: "GET",
  55. url: "'.api_get_path(WEB_AJAX_PATH).'course_home.ajax.php?a=get_notification&course_id="+courseId+"&session_id="+sessionId+"&status="+status,
  56. success: function(data) {
  57. div.append(data);
  58. }
  59. });
  60. });
  61. });
  62. </script>';
  63. }
  64. /*
  65. Header
  66. Include the HTTP, HTML headers plus the top banner.
  67. */
  68. if ($load_dirs) {
  69. $url = api_get_path(WEB_AJAX_PATH).'document.ajax.php?a=document_preview';
  70. $folder_icon = api_get_path(WEB_IMG_PATH).'icons/22/folder.png';
  71. $close_icon = api_get_path(WEB_IMG_PATH).'loading1.gif';
  72. $htmlHeadXtra[] = '<script>
  73. $(document).ready(function() {
  74. $(".document_preview_container").hide();
  75. $(".document_preview").click(function() {
  76. var my_id = this.id;
  77. var course_id = my_id.split("_")[2];
  78. var session_id = my_id.split("_")[3];
  79. //showing div
  80. $(".document_preview_container").hide();
  81. $("#document_result_" +course_id+"_" + session_id).show();
  82. // Loading
  83. var image = $("img", this);
  84. image.attr("src", "'.$close_icon.'");
  85. $.ajax({
  86. url: "'.$url.'",
  87. data: "course_id="+course_id+"&session_id="+session_id,
  88. success: function(return_value) {
  89. image.attr("src", "'.$folder_icon.'");
  90. $("#document_result_" +course_id+"_" + session_id).html(return_value);
  91. }
  92. });
  93. });
  94. });
  95. </script>';
  96. }
  97. if ($displayMyCourseViewBySessionLink) {
  98. $htmlHeadXtra[] = '
  99. <script>
  100. userId = '.$userId.'
  101. $(document).ready(function() {
  102. changeMyCoursesView($.cookie("defaultMyCourseView" + userId));
  103. });
  104. /**
  105. * Keep in cookie the last teacher view for the My Courses Tab. default view, or view by session
  106. * @param inView
  107. */
  108. function changeMyCoursesView(inView) {
  109. $.cookie("defaultMyCourseView"+userId, inView, { expires: 365 });
  110. if (inView == '.IndexManager::VIEW_BY_SESSION.') {
  111. $("#viewBySession").addClass("btn-primary");
  112. $("#viewByDefault").removeClass("btn-primary");
  113. } else {
  114. $("#viewByDefault").addClass("btn-primary");
  115. $("#viewBySession").removeClass("btn-primary");
  116. }
  117. }
  118. </script>';
  119. }
  120. $myCourseListAsCategory = api_get_configuration_value('my_courses_list_as_category');
  121. $controller = new IndexManager(get_lang('MyCourses'));
  122. if (!$myCourseListAsCategory) {
  123. // Main courses and session list
  124. if (isset($_COOKIE['defaultMyCourseView'.$userId]) &&
  125. $_COOKIE['defaultMyCourseView'.$userId] == IndexManager::VIEW_BY_SESSION &&
  126. $displayMyCourseViewBySessionLink
  127. ) {
  128. $courseAndSessions = $controller->returnCoursesAndSessionsViewBySession($userId);
  129. IndexManager::setDefaultMyCourseView(IndexManager::VIEW_BY_SESSION, $userId);
  130. } else {
  131. $courseAndSessions = $controller->returnCoursesAndSessions($userId);
  132. IndexManager::setDefaultMyCourseView(IndexManager::VIEW_BY_DEFAULT, $userId);
  133. }
  134. // if teacher, session coach or admin, display the button to change te course view
  135. if ($displayMyCourseViewBySessionLink &&
  136. (
  137. api_is_drh() ||
  138. api_is_session_general_coach() ||
  139. api_is_platform_admin() ||
  140. api_is_session_admin() ||
  141. api_is_teacher()
  142. )
  143. ) {
  144. $courseAndSessions['html'] = "
  145. <div class='view-by-session-link'>
  146. <div class='btn-group pull-right'>
  147. <a class='btn btn-default' id='viewByDefault' href='user_portal.php'
  148. onclick='changeMyCoursesView(\"".IndexManager::VIEW_BY_DEFAULT."\")'>
  149. ".get_lang('MyCoursesDefaultView')."
  150. </a>
  151. <a class='btn btn-default' id='viewBySession' href='user_portal.php'
  152. onclick='changeMyCoursesView(\"".IndexManager::VIEW_BY_SESSION."\")'>
  153. ".get_lang('MyCoursesSessionView')."
  154. </a>
  155. </div>
  156. </div>
  157. <br /><br />
  158. ".$courseAndSessions['html'];
  159. }
  160. } else {
  161. $categoryCode = isset($_GET['category']) ? $_GET['category'] : '';
  162. if (!$categoryCode) {
  163. $courseAndSessions = $controller->returnCourseCategoryListFromUser($userId);
  164. } else {
  165. $courseAndSessions = $controller->returnCoursesAndSessions(
  166. $userId,
  167. false,
  168. $categoryCode
  169. );
  170. $getCategory = CourseCategory::getCategory($categoryCode);
  171. $controller->tpl->assign('category', $getCategory);
  172. }
  173. }
  174. // Check if a user is enrolled only in one course for going directly to the course after the login.
  175. if (api_get_setting('go_to_course_after_login') == 'true') {
  176. $count_of_sessions = $courseAndSessions['session_count'];
  177. $count_of_courses_no_sessions = $courseAndSessions['course_count'];
  178. // User is subscribe in 1 session and 0 courses.
  179. if ($count_of_sessions == 1 && $count_of_courses_no_sessions == 0) {
  180. $sessions = SessionManager::get_sessions_by_user($userId);
  181. if (isset($sessions[0])) {
  182. $sessionInfo = $sessions[0];
  183. // Session only has 1 course.
  184. if (isset($sessionInfo['courses']) &&
  185. count($sessionInfo['courses']) == 1
  186. ) {
  187. $courseCode = $sessionInfo['courses'][0]['code'];
  188. $courseInfo = api_get_course_info_by_id($sessionInfo['courses'][0]['real_id']);
  189. $courseUrl = $courseInfo['course_public_url'].'?id_session='.$sessionInfo['session_id'];
  190. header('Location:'.$courseUrl);
  191. exit;
  192. }
  193. // Session has many courses.
  194. if (isset($sessionInfo['session_id'])) {
  195. $url = api_get_path(WEB_CODE_PATH).'session/?session_id='.$sessionInfo['session_id'];
  196. header('Location:'.$url);
  197. exit;
  198. }
  199. }
  200. }
  201. // User is subscribed to 1 course.
  202. if (!isset($_SESSION['coursesAlreadyVisited']) &&
  203. $count_of_sessions == 0 &&
  204. $count_of_courses_no_sessions == 1
  205. ) {
  206. $courses = CourseManager::get_courses_list_by_user_id(
  207. $userId
  208. );
  209. if (!empty($courses) && isset($courses[0]) && isset($courses[0]['code'])) {
  210. $courseInfo = api_get_course_info_by_id($courses[0]['real_id']);
  211. if (!empty($courseInfo)) {
  212. $courseUrl = $courseInfo['course_public_url'];
  213. header('Location:'.$courseUrl);
  214. exit;
  215. }
  216. }
  217. }
  218. }
  219. // Show the chamilo mascot
  220. if (empty($courseAndSessions['html']) && !isset($_GET['history'])) {
  221. $controller->tpl->assign(
  222. 'welcome_to_course_block',
  223. $controller->return_welcome_to_course_block()
  224. );
  225. }
  226. $controller->tpl->assign('content', $courseAndSessions['html']);
  227. // Display the Site Use Cookie Warning Validation
  228. $useCookieValidation = api_get_setting('cookie_warning');
  229. if ($useCookieValidation === 'true') {
  230. if (isset($_POST['acceptCookies'])) {
  231. api_set_site_use_cookie_warning_cookie();
  232. } else {
  233. if (!api_site_use_cookie_warning_cookie_exist()) {
  234. if (Template::isToolBarDisplayedForUser()) {
  235. $controller->tpl->assign('toolBarDisplayed', true);
  236. } else {
  237. $controller->tpl->assign('toolBarDisplayed', false);
  238. }
  239. $controller->tpl->assign('displayCookieUsageWarning', true);
  240. }
  241. }
  242. }
  243. //check for flash and message
  244. $sniff_notification = '';
  245. $some_activex = isset($_SESSION['sniff_check_some_activex']) ? $_SESSION['sniff_check_some_activex'] : null;
  246. $some_plugins = isset($_SESSION['sniff_check_some_plugins']) ? $_SESSION['sniff_check_some_plugins'] : null;
  247. if (!empty($some_activex) || !empty($some_plugins)) {
  248. if (!preg_match("/flash_yes/", $some_activex) && !preg_match("/flash_yes/", $some_plugins)) {
  249. $sniff_notification = Display::return_message(get_lang('NoFlash'), 'warning', true);
  250. //js verification - To annoying of redirecting every time the page
  251. $controller->tpl->assign('sniff_notification', $sniff_notification);
  252. }
  253. }
  254. $controller->tpl->assign('profile_block', $controller->return_profile_block());
  255. $controller->tpl->assign('user_image_block', $controller->return_user_image_block());
  256. $controller->tpl->assign('course_block', $controller->return_course_block());
  257. $controller->tpl->assign('navigation_course_links', $controller->return_navigation_links());
  258. $controller->tpl->assign('search_block', $controller->return_search_block());
  259. $controller->tpl->assign('classes_block', $controller->return_classes_block());
  260. $controller->tpl->assign('skills_block', $controller->returnSkillLinks());
  261. $historyClass = '';
  262. if (!empty($_GET['history'])) {
  263. $historyClass = 'courses-history';
  264. }
  265. $controller->tpl->assign('course_history_page', $historyClass);
  266. if ($myCourseListAsCategory) {
  267. $controller->tpl->assign('header', get_lang('MyCourses'));
  268. }
  269. $allow = api_get_configuration_value('gradebook_dependency');
  270. if ($allow) {
  271. $courseAndSessions = $controller->returnCoursesAndSessions(
  272. $userId,
  273. false,
  274. '',
  275. false
  276. );
  277. $courseList = api_get_configuration_value('gradebook_dependency_mandatory_courses');
  278. $courseList = isset($courseList['courses']) ? $courseList['courses'] : [];
  279. $mandatoryCourse = [];
  280. if (!empty($courseList)) {
  281. foreach ($courseList as $courseId) {
  282. $courseInfo = api_get_course_info_by_id($courseId);
  283. $mandatoryCourse[] = $courseInfo['code'];
  284. }
  285. }
  286. // @todo improve calls of course info
  287. $subscribedCourses = !empty($courseAndSessions['courses']) ? $courseAndSessions['courses'] : [];
  288. $mainCategoryList = [];
  289. foreach ($subscribedCourses as $courseInfo) {
  290. $courseCode = $courseInfo['code'];
  291. $categories = Category::load(null, null, $courseCode);
  292. /** @var Category $category */
  293. $category = !empty($categories[0]) ? $categories[0] : [];
  294. if (!empty($category)) {
  295. $mainCategoryList[] = $category;
  296. }
  297. }
  298. $result = [];
  299. $result20 = 0;
  300. $result80 = 0;
  301. $countCoursesPassedNoDependency = 0;
  302. /** @var Category $category */
  303. foreach ($mainCategoryList as $category) {
  304. $userFinished = Category::userFinishedCourse(
  305. $userId,
  306. $category,
  307. true
  308. );
  309. if ($userFinished) {
  310. if (in_array($category->get_course_code(), $mandatoryCourse)) {
  311. if ($result20 < 20) {
  312. $result20 += 10;
  313. }
  314. } else {
  315. $countCoursesPassedNoDependency++;
  316. if ($result80 < 80) {
  317. $result80 += 10;
  318. }
  319. }
  320. }
  321. }
  322. $finalResult = $result20 + $result80;
  323. $gradeBookList = api_get_configuration_value('gradebook_badge_sidebar');
  324. $gradeBookList = isset($gradeBookList['gradebooks']) ? $gradeBookList['gradebooks'] : [];
  325. $badgeList = [];
  326. foreach ($gradeBookList as $id) {
  327. $categories = Category::load($id);
  328. /** @var Category $category */
  329. $category = !empty($categories[0]) ? $categories[0] : [];
  330. $badgeList[$id]['name'] = $category->get_name();
  331. $badgeList[$id]['finished'] = false;
  332. $badgeList[$id]['skills'] = [];
  333. if (!empty($category)) {
  334. $minToValidate = $category->getMinimumToValidate();
  335. $dependencies = $category->getCourseListDependency();
  336. $countDependenciesPassed = 0;
  337. foreach ($dependencies as $courseId) {
  338. $courseInfo = api_get_course_info_by_id($courseId);
  339. $courseCode = $courseInfo['code'];
  340. $categories = Category::load(null, null, $courseCode);
  341. $subCategory = !empty($categories[0]) ? $categories[0] : null;
  342. if (!empty($subCategory)) {
  343. $score = Category::userFinishedCourse(
  344. $userId,
  345. $subCategory,
  346. true
  347. );
  348. if ($score) {
  349. $countDependenciesPassed++;
  350. }
  351. }
  352. }
  353. $userFinished =
  354. $countDependenciesPassed == count($dependencies) &&
  355. $countCoursesPassedNoDependency >= $minToValidate
  356. ;
  357. if ($userFinished) {
  358. $badgeList[$id]['finished'] = true;
  359. }
  360. $objSkill = new Skill();
  361. $skills = $category->get_skills();
  362. $skillList = [];
  363. foreach ($skills as $skill) {
  364. $skillList[] = $objSkill->get($skill['id']);
  365. }
  366. $badgeList[$id]['skills'] = $skillList;
  367. }
  368. }
  369. $controller->tpl->assign(
  370. 'grade_book_sidebar',
  371. true
  372. );
  373. $controller->tpl->assign(
  374. 'grade_book_progress',
  375. $finalResult
  376. );
  377. $controller->tpl->assign('grade_book_badge_list', $badgeList);
  378. /*if ($finalScore > 0) {
  379. $finalScore = (int) $finalScore / count($total);
  380. if ($finalScore == 100) {
  381. $completed = true;
  382. }
  383. }*/
  384. }
  385. $controller->tpl->display_two_col_template();
  386. // Deleting the session_id.
  387. Session::erase('session_id');
  388. Session::erase('studentview');
  389. api_remove_in_gradebook();