user_portal.php 15 KB

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