user_portal.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. }
  154. }
  155. // Check if a user is enrolled only in one course for going directly to the course after the login.
  156. if (api_get_setting('go_to_course_after_login') == 'true') {
  157. $count_of_sessions = $courseAndSessions['session_count'];
  158. $count_of_courses_no_sessions = $courseAndSessions['course_count'];
  159. // User is subscribe in 1 session and 0 courses.
  160. if ($count_of_sessions == 1 && $count_of_courses_no_sessions == 0) {
  161. $sessions = SessionManager::get_sessions_by_user($userId);
  162. if (isset($sessions[0])) {
  163. $sessionInfo = $sessions[0];
  164. // Session only has 1 course.
  165. if (isset($sessionInfo['courses']) && count($sessionInfo['courses']) == 1) {
  166. $courseCode = $sessionInfo['courses'][0]['code'];
  167. $courseInfo = api_get_course_info_by_id($sessionInfo['courses'][0]['real_id']);
  168. $courseUrl = $courseInfo['course_public_url'].'?id_session='.$sessionInfo['session_id'];
  169. header('Location:'.$courseUrl);
  170. exit;
  171. }
  172. // Session has many courses.
  173. if (isset($sessionInfo['session_id'])) {
  174. $url = api_get_path(WEB_CODE_PATH).'session/?session_id='.$sessionInfo['session_id'];
  175. header('Location:'.$url);
  176. exit;
  177. }
  178. }
  179. }
  180. // User is subscribed to 1 course.
  181. if (!isset($_SESSION['coursesAlreadyVisited']) && $count_of_sessions == 0 && $count_of_courses_no_sessions == 1) {
  182. $courses = CourseManager::get_courses_list_by_user_id(
  183. $userId
  184. );
  185. if (!empty($courses) && isset($courses[0]) && isset($courses[0]['code'])) {
  186. $courseInfo = api_get_course_info_by_id($courses[0]['real_id']);
  187. if (!empty($courseInfo)) {
  188. $courseUrl = $courseInfo['course_public_url'];
  189. header('Location:'.$courseUrl);
  190. exit;
  191. }
  192. }
  193. }
  194. }
  195. // Show the chamilo mascot
  196. if (empty($courseAndSessions['html']) && !isset($_GET['history'])) {
  197. $controller->tpl->assign(
  198. 'welcome_to_course_block',
  199. $controller->return_welcome_to_course_block()
  200. );
  201. }
  202. $controller->tpl->assign('content', $courseAndSessions['html']);
  203. // Display the Site Use Cookie Warning Validation
  204. $useCookieValidation = api_get_setting('cookie_warning');
  205. if ($useCookieValidation === 'true') {
  206. if (isset($_POST['acceptCookies'])) {
  207. api_set_site_use_cookie_warning_cookie();
  208. } else {
  209. if (!api_site_use_cookie_warning_cookie_exist()) {
  210. if (Template::isToolBarDisplayedForUser()) {
  211. $controller->tpl->assign('toolBarDisplayed', true);
  212. } else {
  213. $controller->tpl->assign('toolBarDisplayed', false);
  214. }
  215. $controller->tpl->assign('displayCookieUsageWarning', true);
  216. }
  217. }
  218. }
  219. //check for flash and message
  220. $sniff_notification = '';
  221. $some_activex = isset($_SESSION['sniff_check_some_activex']) ? $_SESSION['sniff_check_some_activex'] : null;
  222. $some_plugins = isset($_SESSION['sniff_check_some_plugins']) ? $_SESSION['sniff_check_some_plugins'] : null;
  223. if (!empty($some_activex) || !empty($some_plugins)) {
  224. if (!preg_match("/flash_yes/", $some_activex) && !preg_match("/flash_yes/", $some_plugins)) {
  225. $sniff_notification = Display::return_message(get_lang('NoFlash'), 'warning', true);
  226. //js verification - To annoying of redirecting every time the page
  227. $controller->tpl->assign('sniff_notification', $sniff_notification);
  228. }
  229. }
  230. $controller->tpl->assign('profile_block', $controller->return_profile_block());
  231. $controller->tpl->assign('user_image_block', $controller->return_user_image_block());
  232. $controller->tpl->assign('course_block', $controller->return_course_block());
  233. $controller->tpl->assign('navigation_course_links', $controller->return_navigation_links());
  234. $controller->tpl->assign('search_block', $controller->return_search_block());
  235. $controller->tpl->assign('classes_block', $controller->return_classes_block());
  236. $controller->tpl->assign('skills_block', $controller->returnSkillLinks());
  237. $historyClass = '';
  238. if (!empty($_GET['history'])) {
  239. $historyClass = 'courses-history';
  240. }
  241. $controller->tpl->assign('course_history_page', $historyClass);
  242. if ($myCourseListAsCategory) {
  243. $controller->tpl->assign('header', get_lang('MyCourses'));
  244. }
  245. $allow = api_get_configuration_value('gradebook_dependency');
  246. if (!empty($courseAndSessions['courses']) && $allow) {
  247. $courseList = api_get_configuration_value('gradebook_dependency_mandatory_courses');
  248. $courseList = isset($courseList['courses']) ? $courseList['courses'] : [];
  249. $mandatoryCourse = [];
  250. if (!empty($courseList)) {
  251. foreach ($courseList as $courseId) {
  252. $courseInfo = api_get_course_info_by_id($courseId);
  253. $mandatoryCourse[] = $courseInfo['code'];
  254. }
  255. }
  256. // @todo improve calls of course info
  257. $subscribedCourses = !empty($courseAndSessions['courses']) ? $courseAndSessions['courses'] : [];
  258. $mainCategoryList = [];
  259. foreach ($subscribedCourses as $courseInfo) {
  260. $courseCode = $courseInfo['code'];
  261. $categories = Category::load(null, null, $courseCode);
  262. /** @var Category $category */
  263. $category = !empty($categories[0]) ? $categories[0] : [];
  264. if (!empty($category)) {
  265. $mainCategoryList[] = $category;
  266. }
  267. }
  268. $total = [];
  269. foreach ($mainCategoryList as $category) {
  270. $parentScore = Category::getCurrentScore(
  271. $userId,
  272. $category->get_id(),
  273. $category->get_course_code(),
  274. 0,
  275. true
  276. );
  277. $dependencies = $category->getCourseListDependency();
  278. $children = [];
  279. $totalScoreWithChildren = 0;
  280. if (!empty($dependencies)) {
  281. foreach ($dependencies as $courseId) {
  282. $courseInfo = api_get_course_info_by_id($courseId);
  283. $courseCode = $courseInfo['code'];
  284. $categories = Category::load(null, null, $courseCode);
  285. /** @var Category $subCategory */
  286. $subCategory = !empty($categories[0]) ? $categories[0] : null;
  287. if (!empty($subCategory)) {
  288. $score = Category::getCurrentScore(
  289. $userId,
  290. $subCategory->get_id(),
  291. $subCategory->get_course_code(),
  292. 0,
  293. true
  294. );
  295. $totalScoreWithChildren += $score;
  296. $children[$subCategory->get_course_code()] = ['score' => $score];
  297. }
  298. }
  299. }
  300. $totalScoreWithChildren += $parentScore;
  301. $totalScoreWithChildrenAverage = $parentScore;
  302. if (!empty($children)) {
  303. $totalScoreWithChildrenAverage = $totalScoreWithChildren / (1 + count($children));
  304. }
  305. $total[$category->get_course_code()] = [
  306. 'score' => $parentScore,
  307. 'total_score_with_children' => api_number_format($totalScoreWithChildrenAverage),
  308. 'children' => $children
  309. ];
  310. }
  311. $finalScore = 0;
  312. $customTotalPercentage = 0;
  313. $maxPercentage = 80;
  314. $maxCustomPercentageCounter = 0;
  315. $validatedCoursesPercentage = 0;
  316. foreach ($total as $courseCode => $data) {
  317. $totalScoreWithChildren = $data['total_score_with_children'];
  318. if (in_array($courseCode, $mandatoryCourse)) {
  319. if ($totalScoreWithChildren == 100) {
  320. $finalScore = 0;
  321. $customTotalPercentage += 10;
  322. $validatedCoursesPercentage += 10;
  323. break;
  324. }
  325. } else {
  326. $maxCustomPercentageCounter++;
  327. if ($customTotalPercentage < $maxPercentage &&
  328. $totalScoreWithChildren == 100
  329. ) {
  330. $customTotalPercentage += 10;
  331. }
  332. }
  333. $finalScore += $totalScoreWithChildren;
  334. }
  335. $completed = false;
  336. if ($validatedCoursesPercentage == 20 && $customTotalPercentage == 80) {
  337. $completed = true;
  338. }
  339. $controller->tpl->assign(
  340. 'grade_book_result_validate',
  341. $validatedCoursesPercentage
  342. );
  343. $controller->tpl->assign('grade_book_result_completed', $completed);
  344. /*if ($finalScore > 0) {
  345. $finalScore = (int) $finalScore / count($total);
  346. if ($finalScore == 100) {
  347. $completed = true;
  348. }
  349. }*/
  350. }
  351. $controller->tpl->display_two_col_template();
  352. // Deleting the session_id.
  353. Session::erase('session_id');
  354. Session::erase('studentview');
  355. api_remove_in_gradebook();