local.inc.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. *
  5. * SCRIPT PURPOSE
  6. *
  7. *
  8. * You can request a course id. It will check if the course Id requested is the
  9. * same as the current one. If it isn't it will update session information from
  10. * the database. You can also force the course reset if you want ($cidReset).
  11. *
  12. * All the course information is stored in the $_course array.
  13. *
  14. * You can request a group id. The script will check if the group id requested is the
  15. * same as the current one. If it isn't it will update session information from
  16. * the database. You can also force the course reset if you want ($gidReset).
  17. *
  18. * The course id is stored in $_cid session variable.
  19. * The group id is stored in $_gid session variable.
  20. *
  21. * @package chamilo.include
  22. */
  23. // verified if exists the username and password in session current
  24. use \ChamiloSession as Session;
  25. // Conditional login
  26. /*
  27. * Disabling for now
  28. if (isset($_SESSION['conditional_login']['uid']) && $_SESSION['conditional_login']['can_login']=== true) {
  29. $uData = UserManager::get_user_info_by_id($_SESSION['conditional_login']['uid']);
  30. ConditionalLogin::check_conditions($uData);
  31. $_user['user_id'] = $_SESSION['conditional_login']['uid'];
  32. $_user['status'] = $uData['status'];
  33. Session::write('_user', $_user);
  34. Session::erase('conditional_login');
  35. $uidReset=true;
  36. }*/
  37. // parameters passed via GET
  38. $gidReq = isset($_GET["gidReq"]) ? Database::escape_string($_GET["gidReq"]) : null;
  39. //this fixes some problems with generic functionalities like
  40. //My Agenda & What's New icons linking to courses
  41. // $cidReq can be set in the session
  42. $cidReq = isset($_SESSION['_cid']) ? Database::escape_string($_SESSION['_cid']) : null;
  43. // $cidReq can be set in URL-parameter
  44. $cidReq = isset($_GET["cidReq"]) ? Database::escape_string($_GET["cidReq"]) : $cidReq;
  45. $cidReset = isset($cidReset) ? Database::escape_string($cidReset) : '';
  46. // $cidReset can be set in URL-parameter
  47. $cidReset = (isset($_GET['cidReq']) && ((isset($_SESSION['_cid']) && $_GET['cidReq'] != $_SESSION['_cid']) || (!isset($_SESSION['_cid'])))) ? Database::escape_string($_GET["cidReq"]) : $cidReset;
  48. // $gidReset can be set in URL-parameter
  49. $gidReset = isset($gidReset) ? $gidReset : '';
  50. // parameters passed via POST
  51. $login = isset($_POST["login"]) ? $_POST["login"] : '';
  52. // register if the user is just logging in, in order to redirect him
  53. $logging_in = false;
  54. /* MAIN CODE */
  55. $errorMessage = null;
  56. $loginFailed = true;
  57. if (!empty($_SESSION['_user']['user_id']) && !$login) {
  58. // uid is in session => login already done, continue with this value
  59. $_user['user_id'] = $_SESSION['_user']['user_id'];
  60. //Check if we have to reset user data
  61. //This param can be used to reload user data if user has been logged by external script
  62. if (isset($_SESSION['_user']['uidReset']) && $_SESSION['_user']['uidReset']) {
  63. $uidReset = true;
  64. }
  65. } else {
  66. if (isset($_user['user_id'])) {
  67. unset($_user['user_id']);
  68. }
  69. //Platform legal terms and conditions
  70. if (api_get_setting('allow_terms_conditions') == 'true') {
  71. if (isset($_POST['login']) && isset($_POST['password']) && isset($_SESSION['term_and_condition']['user_id'])) {
  72. $user_id = $_SESSION['term_and_condition']['user_id']; // user id
  73. // Update the terms & conditions
  74. $legal_type = null;
  75. //verify type of terms and conditions
  76. if (isset($_POST['legal_info'])) {
  77. $info_legal = explode(':', $_POST['legal_info']);
  78. $legal_type = LegalManager::get_type_of_terms_and_conditions($info_legal[0], $info_legal[1]);
  79. }
  80. //is necessary verify check
  81. if ($legal_type == 1) {
  82. if ((isset($_POST['legal_accept']) && $_POST['legal_accept']=='1')) {
  83. $legal_option = true;
  84. } else {
  85. $legal_option = false;
  86. }
  87. }
  88. //no is check option
  89. if ($legal_type == 0) {
  90. $legal_option=true;
  91. }
  92. if (isset($_POST['legal_accept_type']) && $legal_option===true) {
  93. $cond_array = explode(':', $_POST['legal_accept_type']);
  94. if (!empty($cond_array[0]) && !empty($cond_array[1])){
  95. $time = time();
  96. $condition_to_save = intval($cond_array[0]).':'.intval($cond_array[1]).':'.$time;
  97. UserManager::update_extra_field_value($user_id,'legal_accept',$condition_to_save);
  98. }
  99. }
  100. }
  101. }
  102. //IF cas is activated and user isn't logged in
  103. if (api_get_setting('cas_activate') == 'true') {
  104. $cas_activated = true;
  105. } else {
  106. $cas_activated = false;
  107. }
  108. $cas_login = false;
  109. if ($cas_activated AND !isset($_user['user_id']) AND !isset($_POST['login'])) {
  110. require_once api_get_path(SYS_PATH).'main/auth/cas/authcas.php';
  111. $cas_login = cas_is_authenticated();
  112. }
  113. if ((isset($_POST['login']) AND isset($_POST['password']) ) OR ($cas_login)) {
  114. // $login && $password are given to log in
  115. if ( $cas_login && empty($_POST['login']) ) {
  116. $login = $cas_login;
  117. } else {
  118. $login = $_POST['login'];
  119. $password = $_POST['password'];
  120. }
  121. //Lookup the user in the main database
  122. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  123. $sql = "SELECT user_id, username, auth_source, password FROM $user_table
  124. WHERE username = '".Database::escape_string($login)."'";
  125. $result = Database::query($sql);
  126. // @todo use a UserProvider
  127. if (Database::num_rows($result) > 0) {
  128. $uData = Database::fetch_array($result);
  129. if ($uData['auth_source'] == PLATFORM_AUTH_SOURCE || $uData['auth_source'] == CAS_AUTH_SOURCE) {
  130. //The authentification of this user is managed by Chamilo itself
  131. $password = api_get_encrypted_password(trim(stripslashes($password)));
  132. // Check the user's password
  133. if (($password == $uData['password'] or $cas_login) and (trim($login) == $uData['username'])) {
  134. $uData = api_get_user_info($uData['user_id'], false, false, true);
  135. $extraFields = $uData['extra_fields'];
  136. // $update_type = UserManager::get_extra_user_data_by_field($uData['user_id'], 'update_type');
  137. $update_type = isset($extraFields['extra_update_type']) ? $extraFields['extra_update_type'] : null;
  138. if (!empty($extAuthSource[$update_type]['updateUser']) && file_exists($extAuthSource[$update_type]['updateUser'])) {
  139. include_once $extAuthSource[$update_type]['updateUser'];
  140. }
  141. // Check if the account is active (not locked)
  142. if ($uData['active'] == '1') {
  143. // Check if the expiration date has not been reached
  144. if ($uData['expiration_date'] > date('Y-m-d H:i:s') OR $uData['expiration_date'] == '0000-00-00 00:00:00') {
  145. global $_configuration;
  146. if (isset($_configuration['multiple_access_urls']) && $_configuration['multiple_access_urls']) {
  147. //Check if user is an admin
  148. $my_user_is_admin = UserManager::is_admin($uData['user_id']);
  149. // This user is subscribed in these sites => $my_url_list
  150. $my_url_list = api_get_access_url_from_user($uData['user_id']);
  151. //Check the access_url configuration setting if the user is registered in the access_url_rel_user table
  152. //Getting the current access_url_id of the platform
  153. $current_access_url_id = api_get_current_access_url_id();
  154. if ($my_user_is_admin === false) {
  155. if (is_array($my_url_list) && count($my_url_list) > 0) {
  156. // the user have the permissions to enter at this site
  157. if (in_array($current_access_url_id, $my_url_list)) {
  158. ConditionalLogin::check_conditions($uData);
  159. Session::write('_user', $uData);
  160. $logging_in = true;
  161. } else {
  162. $loginFailed = true;
  163. Session::erase('_uid');
  164. $errorMessage = 'access_url_inactive';
  165. }
  166. } else {
  167. $loginFailed = true;
  168. Session::erase('_uid');
  169. $errorMessage = 'access_url_inactive';
  170. }
  171. } else {
  172. //Only admins of the "main" (first) Chamilo portal can login wherever they want
  173. //Check if this admin have the access_url_id = 1 which means the principal
  174. if (in_array(1, $my_url_list)) {
  175. ConditionalLogin::check_conditions($uData);
  176. Session::write('_user', $uData);
  177. } else {
  178. //This means a secondary admin wants to login so we check as he's a normal user
  179. if (in_array($current_access_url_id, $my_url_list)) {
  180. Session::write('_user', $uData);
  181. } else {
  182. $loginFailed = true;
  183. Session::erase('_uid');
  184. $errorMessage = 'access_url_inactive';
  185. }
  186. }
  187. }
  188. } else {
  189. ConditionalLogin::check_conditions($uData);
  190. Session::write('_user', $uData);
  191. $logging_in = true;
  192. }
  193. } else {
  194. $loginFailed = true;
  195. Session::erase('_uid');
  196. $errorMessage = 'account_expired';
  197. }
  198. } else {
  199. $loginFailed = true;
  200. Session::erase('_uid');
  201. $errorMessage = 'account_inactive';
  202. }
  203. } else {
  204. // login failed: username or password incorrect
  205. $loginFailed = true;
  206. Session::erase('_uid');
  207. $errorMessage = 'user_password_incorrect';
  208. }
  209. if (isset($uData['creator_id']) && isset($_user) && $_user['user_id'] != $uData['creator_id']) {
  210. //first login for a not self registred
  211. //e.g. registered by a teacher
  212. //do nothing (code may be added later)
  213. }
  214. } elseif (!empty($extAuthSource[$uData['auth_source']]['login']) && file_exists($extAuthSource[$uData['auth_source']]['login'])) {
  215. /*
  216. * Process external authentication
  217. * on the basis of the given login name
  218. */
  219. $loginFailed = true; // Default initialisation. It could
  220. // change after the external authentication
  221. $key = $uData['auth_source']; //'ldap','shibboleth'...
  222. /* >>>>>>>> External authentication modules <<<<<<<<< */
  223. // see configuration.php to define these
  224. include_once($extAuthSource[$key]['login']);
  225. /* >>>>>>>> External authentication modules <<<<<<<<< */
  226. } else { // no standard Chamilo login - try external authentification
  227. //huh... nothing to do... we shouldn't get here
  228. error_log('Chamilo Authentication file '. $extAuthSource[$uData['auth_source']]['login']. ' could not be found - this might prevent your system from doing the corresponding authentication process',0);
  229. }
  230. } else {
  231. // login failed, Database::num_rows($result) <= 0
  232. $loginFailed = true; // Default initialisation. It could
  233. // change after the external authentication
  234. /*
  235. * In this section:
  236. * there is no entry for the $login user in the Chamilo
  237. * database. This also means there is no auth_source for the user.
  238. * We let all external procedures attempt to add him/her
  239. * to the system.
  240. *
  241. * Process external login on the basis
  242. * of the authentication source list
  243. * provided by the configuration settings.
  244. * If the login succeeds, for going further,
  245. * Chamilo needs the $_user['user_id'] variable to be
  246. * set and registered in the session. It's the
  247. * responsability of the external login script
  248. * to provide this $_user['user_id'].
  249. */
  250. if (isset($extAuthSource) && is_array($extAuthSource)) {
  251. foreach($extAuthSource as $thisAuthSource) {
  252. if (!empty($thisAuthSource['newUser']) && file_exists($thisAuthSource['newUser'])) {
  253. include_once($thisAuthSource['newUser']);
  254. } else {
  255. error_log('Chamilo Authentication file '. $thisAuthSource['newUser']. ' could not be found - this might prevent your system from using the authentication process in the user creation process',0);
  256. }
  257. }
  258. } //end if is_array($extAuthSource)
  259. if ($loginFailed) { //If we are here username given is wrong
  260. //header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=user_password_incorrect');
  261. $errorMessage = 'user_password_incorrect';
  262. }
  263. } //end else login failed
  264. } elseif (api_get_setting('sso_authentication') === 'true' && !in_array('webservices', explode('/', $_SERVER['REQUEST_URI']))) {
  265. /**
  266. * TODO:
  267. * - Work on a better validation for webservices paths. Current is very poor and exit
  268. */
  269. $subsso = api_get_setting('sso_authentication_subclass');
  270. if (!empty($subsso)) {
  271. require_once api_get_path(SYS_CODE_PATH).'auth/sso/sso.'.$subsso.'.class.php';
  272. $subsso = 'sso'.$subsso;
  273. $osso = new $subsso(); //load the subclass
  274. } else {
  275. $osso = new sso();
  276. }
  277. if (isset($_SESSION['_user']['user_id'])) {
  278. } else {
  279. // Handle cookie comming from Master Server
  280. if (!isset($_GET['sso_referer']) && !isset($_GET['loginFailed']) && isset($_GET['sso_cookie'])) {
  281. // Redirect to master server
  282. $osso->ask_master();
  283. } elseif (isset($_GET['sso_cookie'])) {
  284. // Here we are going to check the origin of
  285. // what the call says should be used for
  286. // authentication, and ensure we know it
  287. $matches_domain = false;
  288. if (isset($_GET['sso_referer'])) {
  289. $protocol = api_get_setting('sso_authentication_protocol');
  290. // sso_authentication_domain can list
  291. // several, comma-separated, domains
  292. $master_urls = split(',',api_get_setting('sso_authentication_domain'));
  293. if (!empty($master_urls)) {
  294. $master_auth_uri = api_get_setting('sso_authentication_auth_uri');
  295. foreach ($master_urls as $mu) {
  296. if (empty($mu)) { continue; }
  297. // for each URL, check until we find *one* that matches the $_GET['sso_referer'], then skip the rest
  298. if ($protocol.trim($mu).$master_auth_uri === $_GET['sso_referer']) {
  299. $matches_domain = true;
  300. break;
  301. }
  302. }
  303. } else {
  304. error_log('Your sso_authentication_master param is empty. Check the platform configuration, security section. It can be a list of comma-separated domains');
  305. }
  306. }
  307. if ($matches_domain) {
  308. //make all the process of checking
  309. //if the user exists (delegated to the sso class)
  310. $osso->check_user();
  311. } else {
  312. error_log('Check the sso_referer URL in your script, it doesn\'t match any of the possibilities');
  313. //Request comes from unknown source
  314. $loginFailed = true;
  315. Session::erase('_uid');
  316. $errorMessage = 'unrecognize_sso_origin';
  317. }
  318. }
  319. }//end logout ... else ... login
  320. } elseif (api_get_setting('openid_authentication') == 'true') {
  321. if (!empty($_POST['openid_url'])) {
  322. include api_get_path(SYS_CODE_PATH).'auth/openid/login.php';
  323. openid_begin(trim($_POST['openid_url']), api_get_path(WEB_PATH).'index.php');
  324. //this last function should trigger a redirect, so we can die here safely
  325. die('Openid login redirection should be in progress');
  326. } elseif (!empty($_GET['openid_identity'])) {
  327. //it's usual for PHP to replace '.' (dot) by '_' (underscore) in URL parameters
  328. include(api_get_path(SYS_CODE_PATH).'auth/openid/login.php');
  329. $res = openid_complete($_GET);
  330. if ($res['status'] == 'success') {
  331. $id1 = Database::escape_string($res['openid.identity']);
  332. //have another id with or without the final '/'
  333. $id2 = (substr($id1,-1,1)=='/'?substr($id1,0,-1):$id1.'/');
  334. //lookup the user in the main database
  335. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  336. $sql = "SELECT user_id, username, password, auth_source, active, expiration_date
  337. FROM $user_table
  338. WHERE openid = '$id1'
  339. OR openid = '$id2' ";
  340. $result = Database::query($sql);
  341. if ($result !== false) {
  342. if (Database::num_rows($result)>0) {
  343. //$row = Database::fetch_array($res);
  344. $uData = Database::fetch_array($result);
  345. if ($uData['auth_source'] == PLATFORM_AUTH_SOURCE) {
  346. // the authentification of this user is managed by Chamilo itself
  347. // check if the account is active (not locked)
  348. if ($uData['active']=='1') {
  349. // check if the expiration date has not been reached
  350. if ($uData['expiration_date']>date('Y-m-d H:i:s') OR $uData['expiration_date']=='0000-00-00 00:00:00') {
  351. $_user['user_id'] = $uData['user_id'];
  352. $_user['status'] = $uData['status'];
  353. Session::write('_user', $_user);
  354. } else {
  355. $loginFailed = true;
  356. Session::erase('_uid');
  357. $errorMessage = 'account_expired';
  358. }
  359. } else {
  360. $loginFailed = true;
  361. Session::erase('_uid');
  362. $errorMessage = 'account_inactive';
  363. }
  364. if (isset($uData['creator_id']) && $_user['user_id'] != $uData['creator_id']) {
  365. //first login for a not self registred
  366. //e.g. registered by a teacher
  367. //do nothing (code may be added later)
  368. }
  369. }
  370. } else {
  371. //Redirect to the subscription form
  372. header('Location: '.api_get_path(WEB_CODE_PATH).'auth/inscription.php?username='.$res['openid.sreg.nickname'].'&email='.$res['openid.sreg.email'].'&openid='.$res['openid.identity'].'&openid_msg=idnotfound');
  373. //$loginFailed = true;
  374. }
  375. } else {
  376. $loginFailed = true;
  377. }
  378. } else {
  379. $loginFailed = true;
  380. }
  381. }
  382. } elseif (KeyAuth::is_enabled()) {
  383. $success = KeyAuth::instance()->login();
  384. if ($success) {
  385. $use_anonymous = false;
  386. }
  387. }
  388. $uidReset = true;
  389. } // end
  390. if ($loginFailed == true && !empty($errorMessage)) {
  391. header('Location: '.api_get_path(WEB_PUBLIC_PATH).'index?error='.$errorMessage);
  392. exit;
  393. }
  394. //Now check for anonymous user mode
  395. if (isset($use_anonymous) && $use_anonymous) {
  396. //if anonymous mode is set, then try to set the current user as anonymous
  397. //if he doesn't have a login yet
  398. api_set_anonymous();
  399. } else {
  400. //if anonymous mode is not set, then check if this user is anonymous. If it
  401. //is, clean it from being anonymous (make him a nobody :-))
  402. api_clear_anonymous();
  403. }
  404. // if the requested course is different from the course in session
  405. if (!empty($cidReq) && (!isset($_SESSION['_cid']) or (isset($_SESSION['_cid']) && $cidReq != $_SESSION['_cid']))) {
  406. $cidReset = true;
  407. $gidReset = true; // As groups depend from courses, group id is reset
  408. }
  409. // Setting app user variable
  410. $_user = Session::read('_user');
  411. if ($_user && !isset($_user['complete_name'])) {
  412. $_user = api_get_user_info(api_get_user_id(), false, false, true);
  413. Session::write('_user', $_user);
  414. }
  415. $app['current_user'] = $_user;
  416. /* USER INIT */
  417. if (isset($uidReset) && $uidReset) { // session data refresh requested
  418. unset($_SESSION['_user']['uidReset']);
  419. $is_platformAdmin = false;
  420. $is_allowedCreateCourse = false;
  421. if (isset($_user['user_id']) && $_user['user_id'] && !api_is_anonymous()) {
  422. // a uid is given (log in succeeded)
  423. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  424. $admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
  425. $track_e_login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  426. $sql = "SELECT user.*, a.user_id is_admin, login.login_date
  427. FROM $user_table
  428. LEFT JOIN $admin_table a
  429. ON user.user_id = a.user_id
  430. LEFT JOIN $track_e_login login
  431. ON user.user_id = login.login_user_id
  432. WHERE user.user_id = '".$_user['user_id']."'
  433. ORDER BY login.login_date DESC LIMIT 1";
  434. $result = Database::query($sql);
  435. if (Database::num_rows($result) > 0) {
  436. // Extracting the user data
  437. $uData = Database::fetch_array($result);
  438. $_user = api_format_user($uData, false);
  439. $_user['lastLogin'] = api_strtotime($uData['login_date'], 'UTC');
  440. $is_platformAdmin = (bool) (! is_null( $uData['is_admin']));
  441. $is_allowedCreateCourse = (bool) (($uData ['status'] == COURSEMANAGER) or (api_get_setting('drhCourseManagerRights') and $uData['status'] == DRH));
  442. ConditionalLogin::check_conditions($uData);
  443. Session::write('_user', $_user);
  444. UserManager::update_extra_field_value($_user['user_id'], 'already_logged_in', 'true');
  445. Session::write('is_platformAdmin', $is_platformAdmin);
  446. Session::write('is_allowedCreateCourse', $is_allowedCreateCourse);
  447. } else {
  448. header('location:'.api_get_path(WEB_PATH));
  449. exit;
  450. }
  451. } else { // no uid => logout or Anonymous
  452. Session::erase('_user');
  453. Session::erase('_uid');
  454. }
  455. Session::write('is_platformAdmin', $is_platformAdmin);
  456. Session::write('is_allowedCreateCourse', $is_allowedCreateCourse);
  457. } else { // continue with the previous values
  458. $_user = isset($_SESSION['_user']) ? $_SESSION['_user'] : null;
  459. $is_platformAdmin = isset($_SESSION['is_platformAdmin']) ? $_SESSION['is_platformAdmin'] : false;
  460. $is_allowedCreateCourse = isset($_SESSION['is_allowedCreateCourse']) ? $_SESSION['is_allowedCreateCourse'] : false;
  461. }
  462. if (!isset($_SESSION['login_as'])) {
  463. $save_course_access = true;
  464. //The value $_dont_save_user_course_access should be added before the call of global.inc.php see the main/inc/chat.ajax.php file
  465. //Disables the updates in the TRACK_E_COURSE_ACCESS table
  466. if (isset($_dont_save_user_course_access) && $_dont_save_user_course_access == true) {
  467. $save_course_access = false;
  468. }
  469. if ($save_course_access) {
  470. $course_tracking_table = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  471. /*
  472. * When $_configuration['session_lifetime'] is too big 100 hours (in order to let users take exercises with no problems)
  473. * the function Tracking::get_time_spent_on_the_course() returns big values (200h) due the condition:
  474. * login_course_date > now() - INTERVAL $session_lifetime SECOND
  475. *
  476. */
  477. /*
  478. if (isset($_configuration['session_lifetime'])) {
  479. $session_lifetime = $_configuration['session_lifetime'];
  480. } else {
  481. $session_lifetime = 3600; // 1 hour
  482. }*/
  483. $session_lifetime = 3600; // 1 hour
  484. $course_code = api_get_course_id();
  485. $courseId = api_get_course_int_id();
  486. $time = api_get_datetime();
  487. if (isset($_user['user_id']) && !empty($_user['user_id']) && !empty($courseId)) {
  488. //We select the last record for the current course in the course tracking table
  489. //But only if the login date is < than now + max_life_time
  490. $sql = "SELECT course_access_id FROM $course_tracking_table
  491. WHERE user_id = ".intval($_user ['user_id'])." AND
  492. c_id = '$courseId' AND
  493. session_id = ".api_get_session_id()." AND
  494. login_course_date > now() - INTERVAL $session_lifetime SECOND
  495. ORDER BY login_course_date DESC LIMIT 0,1";
  496. $result = Database::query($sql);
  497. if (Database::num_rows($result) > 0) {
  498. $i_course_access_id = Database::result($result,0,0);
  499. //We update the course tracking table
  500. $sql = "UPDATE $course_tracking_table SET logout_course_date = '$time', counter = counter+1
  501. WHERE course_access_id = ".intval($i_course_access_id)." AND session_id = ".api_get_session_id();
  502. Database::query($sql);
  503. } else {
  504. $sql="INSERT INTO $course_tracking_table (c_id, user_id, login_course_date, logout_course_date, counter, session_id)" .
  505. "VALUES('".$courseId."', '".$_user['user_id']."', '$time', '$time', '1','".api_get_session_id()."')";
  506. Database::query($sql);
  507. }
  508. }
  509. }
  510. }
  511. /* COURSE / USER REL. INIT */
  512. $user_id = isset($_user['user_id']) ? $_user['user_id'] : null;
  513. //Course permissions
  514. $is_courseAdmin = false; //course teacher
  515. $is_courseTutor = false; //course teacher - some rights
  516. $is_courseMember = false; //course student
  517. $is_courseCoach = false; //course coach
  518. //Course - User permissions
  519. $is_sessionAdmin = false;
  520. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  521. //set variable according to student_view_enabled choices
  522. if (api_get_setting('student_view_enabled') == "true") {
  523. if (isset($_GET['isStudentView'])) {
  524. if ($_GET['isStudentView'] == 'true') {
  525. if (isset($_SESSION['studentview'])) {
  526. if (!empty($_SESSION['studentview'])) {
  527. // switching to studentview
  528. $_SESSION['studentview'] = 'studentview';
  529. }
  530. }
  531. } elseif ($_GET['isStudentView'] == 'false') {
  532. if (isset($_SESSION['studentview'])) {
  533. if (!empty($_SESSION['studentview'])) {
  534. // switching to teacherview
  535. $_SESSION['studentview'] = 'teacherview';
  536. }
  537. }
  538. }
  539. } elseif (!empty($_SESSION['studentview'])) {
  540. //all is fine, no change to that, obviously
  541. } elseif (empty($_SESSION['studentview'])) {
  542. // We are in teacherview here
  543. $_SESSION['studentview'] = 'teacherview';
  544. }
  545. }
  546. //Redirect::session_request_uri($logging_in, $user_id);