user_portal.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. (
  129. api_is_drh() ||
  130. api_is_session_general_coach() ||
  131. api_is_platform_admin() ||
  132. api_is_session_admin() ||
  133. api_is_teacher()
  134. )
  135. ) {
  136. $courseAndSessions['html'] = "
  137. <div class='view-by-session-link'>
  138. <div class='btn-group pull-right'>
  139. <a class='btn btn-default' id='viewByDefault' href='user_portal.php'
  140. onclick='changeMyCoursesView(\"".IndexManager::VIEW_BY_DEFAULT."\")'>
  141. ".get_lang('MyCoursesDefaultView')."
  142. </a>
  143. <a class='btn btn-default' id='viewBySession' href='user_portal.php'
  144. onclick='changeMyCoursesView(\"".IndexManager::VIEW_BY_SESSION."\")'>
  145. ".get_lang('MyCoursesSessionView')."
  146. </a>
  147. </div>
  148. </div>
  149. <br /><br />
  150. ".$courseAndSessions['html'];
  151. }
  152. } else {
  153. $categoryCode = isset($_GET['category']) ? $_GET['category'] : '';
  154. if (!$categoryCode) {
  155. $courseAndSessions = $controller->returnCourseCategoryListFromUser($userId);
  156. } else {
  157. $courseAndSessions = $controller->returnCoursesAndSessions(
  158. $userId,
  159. false,
  160. $categoryCode
  161. );
  162. $getCategory = CourseCategory::getCategory($categoryCode);
  163. $controller->tpl->assign('category', $getCategory);
  164. }
  165. }
  166. // Check if a user is enrolled only in one course for going directly to the course after the login.
  167. if (api_get_setting('go_to_course_after_login') == 'true') {
  168. $count_of_sessions = $courseAndSessions['session_count'];
  169. $count_of_courses_no_sessions = $courseAndSessions['course_count'];
  170. // User is subscribe in 1 session and 0 courses.
  171. if ($count_of_sessions == 1 && $count_of_courses_no_sessions == 0) {
  172. $sessions = SessionManager::get_sessions_by_user($userId);
  173. if (isset($sessions[0])) {
  174. $sessionInfo = $sessions[0];
  175. // Session only has 1 course.
  176. if (isset($sessionInfo['courses']) &&
  177. count($sessionInfo['courses']) == 1
  178. ) {
  179. $courseCode = $sessionInfo['courses'][0]['code'];
  180. $courseInfo = api_get_course_info_by_id($sessionInfo['courses'][0]['real_id']);
  181. $courseUrl = $courseInfo['course_public_url'].'?id_session='.$sessionInfo['session_id'];
  182. header('Location:'.$courseUrl);
  183. exit;
  184. }
  185. // Session has many courses.
  186. if (isset($sessionInfo['session_id'])) {
  187. $url = api_get_path(WEB_CODE_PATH).'session/?session_id='.$sessionInfo['session_id'];
  188. header('Location:'.$url);
  189. exit;
  190. }
  191. }
  192. }
  193. // User is subscribed to 1 course.
  194. if (!isset($_SESSION['coursesAlreadyVisited']) &&
  195. $count_of_sessions == 0 &&
  196. $count_of_courses_no_sessions == 1
  197. ) {
  198. $courses = CourseManager::get_courses_list_by_user_id(
  199. $userId
  200. );
  201. if (!empty($courses) && isset($courses[0]) && isset($courses[0]['code'])) {
  202. $courseInfo = api_get_course_info_by_id($courses[0]['real_id']);
  203. if (!empty($courseInfo)) {
  204. $courseUrl = $courseInfo['course_public_url'];
  205. header('Location:'.$courseUrl);
  206. exit;
  207. }
  208. }
  209. }
  210. }
  211. // Show the chamilo mascot
  212. if (empty($courseAndSessions['html']) && !isset($_GET['history'])) {
  213. $controller->tpl->assign(
  214. 'welcome_to_course_block',
  215. $controller->return_welcome_to_course_block()
  216. );
  217. }
  218. $controller->tpl->assign('content', $courseAndSessions['html']);
  219. // Display the Site Use Cookie Warning Validation
  220. $useCookieValidation = api_get_setting('cookie_warning');
  221. if ($useCookieValidation === 'true') {
  222. if (isset($_POST['acceptCookies'])) {
  223. api_set_site_use_cookie_warning_cookie();
  224. } else {
  225. if (!api_site_use_cookie_warning_cookie_exist()) {
  226. if (Template::isToolBarDisplayedForUser()) {
  227. $controller->tpl->assign('toolBarDisplayed', true);
  228. } else {
  229. $controller->tpl->assign('toolBarDisplayed', false);
  230. }
  231. $controller->tpl->assign('displayCookieUsageWarning', true);
  232. }
  233. }
  234. }
  235. //check for flash and message
  236. $sniff_notification = '';
  237. $some_activex = isset($_SESSION['sniff_check_some_activex']) ? $_SESSION['sniff_check_some_activex'] : null;
  238. $some_plugins = isset($_SESSION['sniff_check_some_plugins']) ? $_SESSION['sniff_check_some_plugins'] : null;
  239. if (!empty($some_activex) || !empty($some_plugins)) {
  240. if (!preg_match("/flash_yes/", $some_activex) && !preg_match("/flash_yes/", $some_plugins)) {
  241. $sniff_notification = Display::return_message(get_lang('NoFlash'), 'warning', true);
  242. //js verification - To annoying of redirecting every time the page
  243. $controller->tpl->assign('sniff_notification', $sniff_notification);
  244. }
  245. }
  246. $controller->tpl->assign('profile_block', $controller->return_profile_block());
  247. $controller->tpl->assign('user_image_block', $controller->return_user_image_block());
  248. $controller->tpl->assign('course_block', $controller->return_course_block());
  249. $controller->tpl->assign('navigation_course_links', $controller->return_navigation_links());
  250. $controller->tpl->assign('search_block', $controller->return_search_block());
  251. $controller->tpl->assign('classes_block', $controller->return_classes_block());
  252. $controller->tpl->assign('skills_block', $controller->returnSkillLinks());
  253. $historyClass = '';
  254. if (!empty($_GET['history'])) {
  255. $historyClass = 'courses-history';
  256. }
  257. $controller->tpl->assign('course_history_page', $historyClass);
  258. if ($myCourseListAsCategory) {
  259. $controller->tpl->assign('header', get_lang('MyCourses'));
  260. }
  261. $allow = api_get_configuration_value('gradebook_dependency');
  262. if ($allow) {
  263. $courseAndSessions = $controller->returnCoursesAndSessions(
  264. $userId,
  265. false,
  266. '',
  267. false
  268. );
  269. $courseList = api_get_configuration_value('gradebook_dependency_mandatory_courses');
  270. $courseList = isset($courseList['courses']) ? $courseList['courses'] : [];
  271. $mandatoryCourse = [];
  272. if (!empty($courseList)) {
  273. foreach ($courseList as $courseId) {
  274. $courseInfo = api_get_course_info_by_id($courseId);
  275. $mandatoryCourse[] = $courseInfo['code'];
  276. }
  277. }
  278. // @todo improve calls of course info
  279. $subscribedCourses = !empty($courseAndSessions['courses']) ? $courseAndSessions['courses'] : [];
  280. $mainCategoryList = [];
  281. foreach ($subscribedCourses as $courseInfo) {
  282. $courseCode = $courseInfo['code'];
  283. $categories = Category::load(null, null, $courseCode);
  284. /** @var Category $category */
  285. $category = !empty($categories[0]) ? $categories[0] : [];
  286. if (!empty($category)) {
  287. $mainCategoryList[] = $category;
  288. }
  289. }
  290. $result = [];
  291. $result20 = 0;
  292. $result80 = 0;
  293. $countCoursesPassedNoDependency = 0;
  294. /** @var Category $category */
  295. foreach ($mainCategoryList as $category) {
  296. $userFinished = Category::userFinishedCourse(
  297. $userId,
  298. $category,
  299. true
  300. );
  301. if ($userFinished) {
  302. if (in_array($category->get_course_code(), $mandatoryCourse)) {
  303. if ($result20 < 20) {
  304. $result20 += 10;
  305. }
  306. } else {
  307. $countCoursesPassedNoDependency++;
  308. if ($result80 < 80) {
  309. $result80 += 10;
  310. }
  311. }
  312. }
  313. }
  314. $finalResult = $result20 + $result80;
  315. $gradeBookList = api_get_configuration_value('gradebook_badge_sidebar');
  316. $gradeBookList = isset($gradeBookList['gradebooks']) ? $gradeBookList['gradebooks'] : [];
  317. $badgeList = [];
  318. foreach ($gradeBookList as $id) {
  319. $categories = Category::load($id);
  320. /** @var Category $category */
  321. $category = !empty($categories[0]) ? $categories[0] : [];
  322. $badgeList[$id]['name'] = $category->get_name();
  323. $badgeList[$id]['finished'] = false;
  324. $badgeList[$id]['skills'] = [];
  325. if (!empty($category)) {
  326. $minToValidate = $category->getMinimumToValidate();
  327. $dependencies = $category->getCourseListDependency();
  328. $gradeBooksToValidateInDependence = $category->getGradeBooksToValidateInDependence();
  329. $countDependenciesPassed = 0;
  330. foreach ($dependencies as $courseId) {
  331. $courseInfo = api_get_course_info_by_id($courseId);
  332. $courseCode = $courseInfo['code'];
  333. $categories = Category::load(null, null, $courseCode);
  334. $subCategory = !empty($categories[0]) ? $categories[0] : null;
  335. if (!empty($subCategory)) {
  336. $score = Category::userFinishedCourse(
  337. $userId,
  338. $subCategory,
  339. true
  340. );
  341. if ($score) {
  342. $countDependenciesPassed++;
  343. }
  344. }
  345. }
  346. /*$userFinished =
  347. $countDependenciesPassed == count($dependencies) &&
  348. $countCoursesPassedNoDependency >= $minToValidate
  349. ;*/
  350. $userFinished =
  351. $countDependenciesPassed >= $gradeBooksToValidateInDependence &&
  352. $countCoursesPassedNoDependency >= $minToValidate
  353. ;
  354. if ($userFinished) {
  355. $badgeList[$id]['finished'] = true;
  356. }
  357. $objSkill = new Skill();
  358. $skills = $category->get_skills();
  359. $skillList = [];
  360. foreach ($skills as $skill) {
  361. $skillList[] = $objSkill->get($skill['id']);
  362. }
  363. $badgeList[$id]['skills'] = $skillList;
  364. }
  365. }
  366. $controller->tpl->assign(
  367. 'grade_book_sidebar',
  368. true
  369. );
  370. $controller->tpl->assign(
  371. 'grade_book_progress',
  372. $finalResult
  373. );
  374. $controller->tpl->assign('grade_book_badge_list', $badgeList);
  375. /*if ($finalScore > 0) {
  376. $finalScore = (int) $finalScore / count($total);
  377. if ($finalScore == 100) {
  378. $completed = true;
  379. }
  380. }*/
  381. }
  382. $controller->tpl->display_two_col_template();
  383. // Deleting the session_id.
  384. Session::erase('session_id');
  385. Session::erase('studentview');
  386. api_remove_in_gradebook();