local.inc.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. *
  5. * SCRIPT PURPOSE
  6. *
  7. * This script initializes and manages Chamilo session information. It
  8. * keeps available session information up to date.
  9. *
  10. * You can request a course id. It will check if the course Id requested is the
  11. * same as the current one. If it isn't it will update session information from
  12. * the database. You can also force the course reset if you want ($cidReset).
  13. *
  14. * All the course information is stored in the $_course array.
  15. *
  16. * You can request a group id. The script will check if the group id requested is the
  17. * same as the current one. If it isn't it will update session information from
  18. * the database. You can also force the course reset if you want ($gidReset).
  19. *
  20. The course id is stored in $_cid session variable.
  21. * The group id is stored in $_gid session variable.
  22. *
  23. *
  24. * VARIABLES AFFECTING THE SCRIPT BEHAVIOR
  25. *
  26. * string $login
  27. * string $password
  28. * boolean $logout
  29. *
  30. * string $cidReq : course id requested
  31. * boolean $cidReset : ask for a course Reset, if no $cidReq is provided in the
  32. * same time, all course informations is removed from the
  33. * current session
  34. *
  35. * int $gidReq : group Id requested
  36. * boolean $gidReset : ask for a group Reset, if no $gidReq is provided in the
  37. * same time, all group informations is removed from the
  38. * current session
  39. *
  40. *
  41. * VARIABLES SET AND RETURNED BY THE SCRIPT
  42. *
  43. * All the variables below are set and returned by this script.
  44. *
  45. * USER VARIABLES
  46. *
  47. * string $_user ['firstName' ]
  48. * string $_user ['lastName' ]
  49. * string $_user ['mail' ]
  50. * string $_user ['lastLogin' ]
  51. * string $_user ['official_code']
  52. * string $_user ['picture_uri' ]
  53. * string $_user['user_id']
  54. *
  55. * boolean $is_platformAdmin
  56. * boolean $is_allowedCreateCourse
  57. *
  58. * COURSE VARIABLES
  59. *
  60. * string $_cid (the course id)
  61. *
  62. * int $_course['id' ] - auto-assigned integer
  63. * string $_course['name' ] - the title of the course
  64. * string $_course['official_code'] - the visual / fake / official code
  65. * string $_course['sysCode' ]
  66. * string $_course['path' ]
  67. * string $_course['dbName' ]
  68. * string $_course['dbNameGlu' ]
  69. * string $_course['titular' ]
  70. * string $_course['language' ]
  71. * string $_course['extLink' ]['url' ]
  72. * string $_course['extLink' ]['name']
  73. * string $_course['categoryCode']
  74. * string $_course['categoryName']
  75. * boolean $is_courseMember
  76. * boolean $is_courseTutor
  77. * boolean $is_courseAdmin
  78. *
  79. *
  80. * GROUP VARIABLES
  81. *
  82. * int $_gid (the group id)
  83. *
  84. *
  85. * IMPORTANT ADVICE FOR DEVELOPERS
  86. *
  87. * We strongly encourage developers to use a connection layer at the top of
  88. * their scripts rather than use these variables, as they are, inside the core
  89. * of their scripts. It will make code maintenance much easier.
  90. *
  91. * Many if the functions you need you can already find in the
  92. * main_api.lib.php
  93. *
  94. * We encourage you to use functions to access these global "kernel" variables.
  95. * You can add them to e.g. the main API library.
  96. *
  97. *
  98. * SCRIPT STRUCTURE
  99. *
  100. * 1. The script determines if there is an authentication attempt. This part
  101. * only chek if the login name and password are valid. Afterwards, it set the
  102. * $_user['user_id'] (user id) and the $uidReset flag. Other user informations are retrieved
  103. * later. It's also in this section that optional external authentication
  104. * devices step in.
  105. *
  106. * 2. The script determines what other session informations have to be set or
  107. * reset, setting correctly $cidReset (for course) and $gidReset (for group).
  108. *
  109. * 3. If needed, the script retrieves the other user informations (first name,
  110. * last name, ...) and stores them in session.
  111. *
  112. * 4. If needed, the script retrieves the course information and stores them
  113. * in session
  114. *
  115. * 5. The script initializes the user permission status and permission for the
  116. * course level
  117. *
  118. * 6. If needed, the script retrieves group informations an store them in
  119. * session.
  120. *
  121. * 7. The script initializes the user status and permission for the group level.
  122. *
  123. * @package chamilo.include
  124. */
  125. /*
  126. INIT SECTION
  127. variables should be initialised here
  128. */
  129. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  130. // verified if exists the username and password in session current
  131. if (isset($_SESSION['info_current_user'][1]) && isset($_SESSION['info_current_user'][2])) {
  132. require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  133. require_once (api_get_path(LIBRARY_PATH).'legal.lib.php');
  134. }
  135. // parameters passed via GET
  136. $logout = isset($_GET["logout"]) ? $_GET["logout"] : '';
  137. $gidReq = isset($_GET["gidReq"]) ? Database::escape_string($_GET["gidReq"]) : '';
  138. //this fixes some problems with generic functionalities like
  139. //My Agenda & What's New icons linking to courses
  140. // $cidReq can be set in the index.php file of a course-area
  141. $cidReq = isset($cidReq) ? Database::escape_string($cidReq) : '';
  142. // $cidReq can be set in URL-parameter
  143. $cidReq = isset($_GET["cidReq"]) ? Database::escape_string($_GET["cidReq"]) : $cidReq;
  144. $cidReset = isset($cidReset) ? Database::escape_string($cidReset) : '';
  145. // $cidReset can be set in URL-parameter
  146. $cidReset = (isset($_GET['cidReq']) && ((isset($_SESSION['_cid']) && $_GET['cidReq']!=$_SESSION['_cid']) || (!isset($_SESSION['_cid'])))) ? Database::escape_string($_GET["cidReq"]) : $cidReset;
  147. // $cDir is a special url param sent by courses/.htaccess
  148. $cDir = (!empty($_GET['cDir']) ? $_GET['cDir'] : null);
  149. $gidReset = isset($gidReset) ? $gidReset : '';
  150. // $gidReset can be set in URL-parameter
  151. // parameters passed via POST
  152. $login = isset($_POST["login"]) ? $_POST["login"] : '';
  153. // passed through other means
  154. //$cidReq -- passed from course folder index.php
  155. /*
  156. MAIN CODE
  157. */
  158. if (!empty($_SESSION['_user']['user_id']) && ! ($login || $logout)) {
  159. // uid is in session => login already done, continue with this value
  160. $_user['user_id'] = $_SESSION['_user']['user_id'];
  161. } else {
  162. if (isset($_user['user_id'])) {
  163. unset($_user['user_id']);
  164. }
  165. //$_SESSION['info_current_user'][1] is user name
  166. //$_SESSION['info_current_user'][2] is current password encrypted
  167. //$_SESSION['update_term_and_condition'][1] is current user id, of user in session
  168. if (api_get_setting('allow_terms_conditions')=='true') {
  169. if (isset($_POST['login']) && isset($_POST['password']) && isset($_SESSION['update_term_and_condition'][1])) {
  170. $user_id=$_SESSION['update_term_and_condition'][1]; // user id
  171. // update the terms & conditions
  172. //verify type of terms and conditions
  173. $info_legal = explode(':',$_POST['legal_info']);
  174. $legal_type=LegalManager::get_type_of_terms_and_conditions($info_legal[0],$info_legal[1]);
  175. //is necessary verify check
  176. if ($legal_type==1) {
  177. if ((isset($_POST['legal_accept']) && $_POST['legal_accept']=='1')) {
  178. $legal_option=true;
  179. } else {
  180. $legal_option=false;
  181. }
  182. }
  183. //no is check option
  184. if ($legal_type==0) {
  185. $legal_option=true;
  186. }
  187. if (isset($_POST['legal_accept_type']) && $legal_option===true) {
  188. $cond_array = explode(':',$_POST['legal_accept_type']);
  189. if (!empty($cond_array[0]) && !empty($cond_array[1])){
  190. $time = time();
  191. $condition_to_save = intval($cond_array[0]).':'.intval($cond_array[1]).':'.$time;
  192. UserManager::update_extra_field_value($user_id,'legal_accept',$condition_to_save);
  193. }
  194. }
  195. }
  196. }
  197. if ((isset($_POST['login']) && isset($_POST['password']))) {
  198. // $login && $password are given to log in
  199. $login = $_POST['login'];
  200. $password = $_POST['password'];
  201. //lookup the user in the main database
  202. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  203. $sql = "SELECT user_id, username, password, auth_source, active, expiration_date
  204. FROM $user_table
  205. WHERE username = '".trim(addslashes($login))."'";
  206. $result = Database::query($sql);
  207. if (Database::num_rows($result) > 0) {
  208. $uData = Database::fetch_array($result);
  209. if ($uData['auth_source'] == PLATFORM_AUTH_SOURCE) {
  210. //the authentification of this user is managed by Chamilo itself
  211. $password = trim(stripslashes($password));
  212. // determine if the password needs to be encrypted before checking
  213. // $userPasswordCrypted is set in an external configuration file
  214. /*if ($userPasswordCrypted) {
  215. $password = md5($password);
  216. } */
  217. if (api_get_setting('allow_terms_conditions')=='true') {
  218. if (isset($_POST['password']) && isset($_SESSION['info_current_user'][2]) && $_POST['password']==$_SESSION['info_current_user'][2]) {
  219. $password=$_POST['password'];
  220. } else {
  221. $password = api_get_encrypted_password($password);
  222. }
  223. } else {
  224. $password = api_get_encrypted_password($password);
  225. }
  226. if (api_get_setting('allow_terms_conditions')=='true') {
  227. if ($password == $uData['password'] AND (trim($login) == $uData['username'])) {
  228. $temp_user_id = $uData['user_id'];
  229. $term_and_condition_status=api_check_term_condition($temp_user_id);//false or true
  230. if ($term_and_condition_status===false) {
  231. $_SESSION['update_term_and_condition']=array(true,$temp_user_id);
  232. $_SESSION['info_current_user']=array(true,$login,$password);
  233. header('Location: '.api_get_path(WEB_CODE_PATH).'auth/inscription.php');
  234. exit;
  235. } else {
  236. unset($_SESSION['update_term_and_condition']);
  237. unset($_SESSION['info_current_user']);
  238. }
  239. }
  240. }
  241. // check the user's password
  242. if ($password == $uData['password'] AND (trim($login) == $uData['username'])) {
  243. // check if the account is active (not locked)
  244. if ($uData['active']=='1') {
  245. // check if the expiration date has not been reached
  246. if ($uData['expiration_date']>date('Y-m-d H:i:s') OR $uData['expiration_date']=='0000-00-00 00:00:00') {
  247. global $_configuration;
  248. if ($_configuration['multiple_access_urls']==true) {
  249. //check the access_url configuration setting if the user is registered in the access_url_rel_user table
  250. //getting the current access_url_id of the platform
  251. $current_access_url_id = api_get_current_access_url_id();
  252. // my user is subscribed in these sites => $my_url_list
  253. $my_url_list = api_get_access_url_from_user($uData['user_id']);
  254. if (is_array($my_url_list) && count($my_url_list)>0 ){
  255. // the user have the permissions to enter at this site
  256. if (in_array($current_access_url_id, $my_url_list)) {
  257. $_user['user_id'] = $uData['user_id'];
  258. api_session_register('_user');
  259. event_login();
  260. } else {
  261. $loginFailed = true;
  262. api_session_unregister('_uid');
  263. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=access_url_inactive');
  264. exit;
  265. }
  266. } else {
  267. $loginFailed = true;
  268. api_session_unregister('_uid');
  269. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=access_url_inactive');
  270. exit;
  271. }
  272. } else {
  273. $_user['user_id'] = $uData['user_id'];
  274. api_session_register('_user');
  275. event_login();
  276. }
  277. } else {
  278. $loginFailed = true;
  279. api_session_unregister('_uid');
  280. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=account_expired');
  281. exit;
  282. }
  283. } else {
  284. $loginFailed = true;
  285. api_session_unregister('_uid');
  286. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=account_inactive');
  287. exit;
  288. }
  289. } else {
  290. // login failed: username or password incorrect
  291. $loginFailed = true;
  292. api_session_unregister('_uid');
  293. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=user_password_incorrect');
  294. exit;
  295. }
  296. if (isset($uData['creator_id']) && $_user['user_id'] != $uData['creator_id']) {
  297. //first login for a not self registred
  298. //e.g. registered by a teacher
  299. //do nothing (code may be added later)
  300. }
  301. } elseif (!empty($extAuthSource[$uData['auth_source']]['login']) && file_exists($extAuthSource[$uData['auth_source']]['login'])) {
  302. /*
  303. * Process external authentication
  304. * on the basis of the given login name
  305. */
  306. $loginFailed = true; // Default initialisation. It could
  307. // change after the external authentication
  308. $key = $uData['auth_source']; //'ldap','shibboleth'...
  309. /* >>>>>>>> External authentication modules <<<<<<<<< */
  310. // see configuration.php to define these
  311. include_once($extAuthSource[$key]['login']);
  312. /* >>>>>>>> External authentication modules <<<<<<<<< */
  313. } else // no standard Chamilo login - try external authentification
  314. {
  315. //huh... nothing to do... we shouldn't get here
  316. 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);
  317. }
  318. if (!empty($_SESSION['request_uri'])) {
  319. $req = $_SESSION['request_uri'];
  320. unset($_SESSION['request_uri']);
  321. header('location: '.$req);
  322. } else {
  323. if (isset($param)) {
  324. header('location: '.api_get_path(WEB_PATH).api_get_setting('page_after_login').$param);
  325. } else {
  326. // here is the main redirect of a *normal* login page in Dokeos
  327. header('location: '.api_get_path(WEB_PATH).api_get_setting('page_after_login'));
  328. }
  329. }
  330. } else {
  331. // login failed, Database::num_rows($result) <= 0
  332. $loginFailed = true; // Default initialisation. It could
  333. // change after the external authentication
  334. /*
  335. * In this section:
  336. * there is no entry for the $login user in the Dokeos
  337. * database. This also means there is no auth_source for the user.
  338. * We let all external procedures attempt to add him/her
  339. * to the system.
  340. *
  341. * Process external login on the basis
  342. * of the authentication source list
  343. * provided by the configuration settings.
  344. * If the login succeeds, for going further,
  345. * Dokeos needs the $_user['user_id'] variable to be
  346. * set and registered in the session. It's the
  347. * responsability of the external login script
  348. * to provide this $_user['user_id'].
  349. */
  350. if (isset($extAuthSource) && is_array($extAuthSource)) {
  351. foreach($extAuthSource as $thisAuthSource) {
  352. if (!empty($thisAuthSource['newUser']) && file_exists($thisAuthSource['newUser'])) {
  353. include_once($thisAuthSource['newUser']);
  354. } else {
  355. error_log('Dokeos Authentication file '. $thisAuthSource['newUser']. ' could not be found - this might prevent your system from using the authentication process in the user creation process',0);
  356. }
  357. }
  358. } //end if is_array($extAuthSource)
  359. } //end else login failed
  360. } elseif(api_get_setting('sso_authentication')==='true' && !in_array('webservices', explode('/', $_SERVER['REQUEST_URI']))) {
  361. /**
  362. * TODO:
  363. * - Implement user interface for api_get_setting('sso_authentication')
  364. * } elseif (api_get_setting('sso_authentication')=='true') {
  365. * - Work on a better validation for webservices paths. Current is very poor and exit
  366. * - $master variable should be recovered from chamilo settings.
  367. */
  368. $master = array(
  369. 'domain' => api_get_setting('sso_authentication_domain'), // 'localhost/project/drupal5',
  370. 'auth_uri' => api_get_setting('sso_authentication_auth_uri'), // '/?q=user',
  371. 'deauth_uri' => api_get_setting('sso_authentication_unauth_uri'), // '/?q=logout',
  372. 'protocol' => api_get_setting('sso_authentication_protocol') // 'http://',
  373. );
  374. $referer = $master['protocol'] . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  375. if (isset($_SESSION['_user']['user_id'])) {
  376. if ($logout) {
  377. // Library needed by index.php
  378. include_once api_get_path(LIBRARY_PATH) . 'online.inc.php';
  379. include_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  380. // Prevent index.php to redirect
  381. global $logout_no_redirect;
  382. $logout_no_redirect = TRUE;
  383. // Make custom redirect after logout
  384. online_logout();
  385. header('Location: '. $master['protocol'] . $master['domain'] . $master['deauth_uri']);
  386. exit;
  387. }
  388. } elseif(!$logout) {
  389. $master_url = $master['domain'] . $master['auth_uri'];
  390. // Handle cookie comming from Master Server
  391. if (!isset($_GET['sso_referer']) && !isset($_GET['loginFailed'])) {
  392. // Target to redirect after success SSO
  393. $target = api_get_path(WEB_PATH);
  394. // Redirect to master server
  395. header('Location: ' . $master['protocol'] . $master_url . '&sso_referer=' . urlencode($referer) . '&sso_target=' . urlencode($target));
  396. exit;
  397. } elseif (isset($_GET['sso_cookie'])) {
  398. if (isset($_GET['sso_referer']) ? $_GET['sso_referer'] === $master['protocol']. $master_url : FALSE) {
  399. $sso = unserialize(base64_decode($_GET['sso_cookie']));
  400. //lookup the user in the main database
  401. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  402. $sql = "SELECT user_id, username, password, auth_source, active, expiration_date
  403. FROM $user_table
  404. WHERE username = '".trim(addslashes($sso['username']))."'";
  405. $result = Database::query($sql);
  406. if (Database::num_rows($result) > 0) {
  407. $uData = Database::fetch_array($result);
  408. // check the user's password
  409. if ($uData['auth_source'] == PLATFORM_AUTH_SOURCE) {
  410. // Make sure password is encrypted with md5
  411. if (!$userPasswordCrypted) {
  412. $uData['password'] = md5($uData['password']);
  413. }
  414. //the authentification of this user is managed by Dokeos itself// check the user's password
  415. // password hash comes into a sha1
  416. if ($sso['secret'] === sha1($uData['password']) && ($sso['username'] == $uData['username'])) {
  417. // check if the account is active (not locked)
  418. if ($uData['active']=='1') {
  419. // check if the expiration date has not been reached
  420. if ($uData['expiration_date']>date('Y-m-d H:i:s') OR $uData['expiration_date']=='0000-00-00 00:00:00') {
  421. global $_configuration;
  422. if ($_configuration['multiple_access_urls']==true) {
  423. //check the access_url configuration setting if the user is registered in the access_url_rel_user table
  424. //getting the current access_url_id of the platform
  425. $current_access_url_id = api_get_current_access_url_id();
  426. // my user is subscribed in these sites => $my_url_list
  427. $my_url_list = api_get_access_url_from_user($uData['user_id']);
  428. if (is_array($my_url_list) && count($my_url_list)>0 ) {
  429. if (in_array($current_access_url_id, $my_url_list)) {
  430. // the user has permission to enter at this site
  431. $_user['user_id'] = $uData['user_id'];
  432. api_session_register('_user');
  433. event_login();
  434. // Redirect to homepage
  435. $sso_target = isset($sso['target']) ? $sso['target'] : api_get_path(WEB_PATH) .'.index.php';
  436. header('Location: '. $sso_target);
  437. } else {
  438. // user does not have permission for this site
  439. $loginFailed = true;
  440. api_session_unregister('_uid');
  441. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=access_url_inactive');
  442. exit;
  443. }
  444. } else {
  445. // there is no URL in the multiple urls list for this user
  446. $loginFailed = true;
  447. api_session_unregister('_uid');
  448. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=access_url_inactive');
  449. exit;
  450. }
  451. } else {
  452. //single URL access
  453. $_user['user_id'] = $uData['user_id'];
  454. api_session_register('_user');
  455. event_login();
  456. // Redirect to homepage
  457. $sso_target = isset($sso['target']) ? $sso['target'] : api_get_path(WEB_PATH) .'.index.php';
  458. header('Location: '. $sso_target);
  459. }
  460. } else {
  461. // user account expired
  462. $loginFailed = true;
  463. api_session_unregister('_uid');
  464. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=account_expired');
  465. exit;
  466. }
  467. } else {
  468. //user not active
  469. $loginFailed = true;
  470. api_session_unregister('_uid');
  471. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=account_inactive');
  472. exit;
  473. }
  474. } else {
  475. //sha1 of password is wrong
  476. $loginFailed = true;
  477. api_session_unregister('_uid');
  478. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=account_inactive');
  479. exit;
  480. }
  481. } else {
  482. //auth_source is wrong
  483. $loginFailed = true;
  484. api_session_unregister('_uid');
  485. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=account_inactive');
  486. exit;
  487. }
  488. } else {
  489. //no user by that login
  490. $loginFailed = true;
  491. api_session_unregister('_uid');
  492. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=account_inactive');
  493. exit;
  494. }
  495. } else {
  496. //request comes from unknown source
  497. $loginFailed = true;
  498. api_session_unregister('_uid');
  499. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=account_inactive');
  500. exit;
  501. }
  502. }
  503. }
  504. } elseif (api_get_setting('openid_authentication')=='true') {
  505. if (!empty($_POST['openid_url'])) {
  506. include('main/auth/openid/login.php');
  507. openid_begin(trim($_POST['openid_url']),api_get_path(WEB_PATH).'index.php');
  508. //this last function should trigger a redirect, so we can die here safely
  509. die('Openid login redirection should be in progress');
  510. } elseif (!empty($_GET['openid_identity']))
  511. { //it's usual for PHP to replace '.' (dot) by '_' (underscore) in URL parameters
  512. include('main/auth/openid/login.php');
  513. $res = openid_complete($_GET);
  514. if ($res['status'] == 'success') {
  515. $id1 = Database::escape_string($res['openid.identity']);
  516. //have another id with or without the final '/'
  517. $id2 = (substr($id1,-1,1)=='/'?substr($id1,0,-1):$id1.'/');
  518. //lookup the user in the main database
  519. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  520. $sql = "SELECT user_id, username, password, auth_source, active, expiration_date
  521. FROM $user_table
  522. WHERE openid = '$id1'
  523. OR openid = '$id2' ";
  524. $result = Database::query($sql);
  525. if ($result !== false) {
  526. if (Database::num_rows($result)>0) {
  527. //$row = Database::fetch_array($res);
  528. $uData = Database::fetch_array($result);
  529. if ($uData['auth_source'] == PLATFORM_AUTH_SOURCE) {
  530. //the authentification of this user is managed by Dokeos itself
  531. // check if the account is active (not locked)
  532. if ($uData['active']=='1') {
  533. // check if the expiration date has not been reached
  534. if ($uData['expiration_date']>date('Y-m-d H:i:s') OR $uData['expiration_date']=='0000-00-00 00:00:00') {
  535. $_user['user_id'] = $uData['user_id'];
  536. api_session_register('_user');
  537. event_login();
  538. } else {
  539. $loginFailed = true;
  540. api_session_unregister('_uid');
  541. header('Location: index.php?loginFailed=1&error=account_expired');
  542. exit;
  543. }
  544. } else {
  545. $loginFailed = true;
  546. api_session_unregister('_uid');
  547. header('Location: index.php?loginFailed=1&error=account_inactive');
  548. exit;
  549. }
  550. if (isset($uData['creator_id']) && $_user['user_id'] != $uData['creator_id'])
  551. {
  552. //first login for a not self registred
  553. //e.g. registered by a teacher
  554. //do nothing (code may be added later)
  555. }
  556. }
  557. } else {
  558. //Redirect to the subscription form
  559. 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');
  560. //$loginFailed = true;
  561. }
  562. } else {
  563. $loginFailed = true;
  564. }
  565. } else {
  566. $loginFailed = true;
  567. }
  568. }
  569. }
  570. // else {} => continue as anonymous user
  571. $uidReset = true;
  572. // $cidReset = true;
  573. // $gidReset = true;
  574. }
  575. //Now check for anonymous user mode
  576. if (isset($use_anonymous) && $use_anonymous == true) {
  577. //if anonymous mode is set, then try to set the current user as anonymous
  578. //if he doesn't have a login yet
  579. api_set_anonymous();
  580. } else {
  581. //if anonymous mode is not set, then check if this user is anonymous. If it
  582. //is, clean it from being anonymous (make him a nobody :-))
  583. api_clear_anonymous();
  584. }
  585. // if there is a cDir parameter in the URL (coming from courses/.htaccess redirection)
  586. if (!empty($cDir)) {
  587. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  588. $c = CourseManager::get_course_id_from_path($cDir);
  589. if ($c != false) { $cidReq = $c; }
  590. }
  591. // if the requested course is different from the course in session
  592. if (!empty($cidReq) && (!isset($_SESSION['_cid']) or (isset($_SESSION['_cid']) && $cidReq != $_SESSION['_cid']))) {
  593. $cidReset = true;
  594. $gidReset = true; // As groups depend from courses, group id is reset
  595. }
  596. // if the requested group is different from the group in session
  597. $gid = isset($_SESSION['_gid'])?$_SESSION['_gid']:'';
  598. if ($gidReq && $gidReq != $gid) {
  599. $gidReset = true;
  600. }
  601. /* USER INIT */
  602. if (isset($uidReset) && $uidReset) // session data refresh requested
  603. {
  604. $is_platformAdmin = false; $is_allowedCreateCourse = false;
  605. if (isset($_user['user_id']) && $_user['user_id']) // a uid is given (log in succeeded)
  606. {
  607. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  608. $admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
  609. if ($_configuration['tracking_enabled']) {
  610. $sql = "SELECT user.*, a.user_id is_admin,
  611. UNIX_TIMESTAMP(login.login_date) login_date
  612. FROM $user_table
  613. LEFT JOIN $admin_table a
  614. ON user.user_id = a.user_id
  615. LEFT JOIN ".$_configuration['statistics_database'].".track_e_login login
  616. ON user.user_id = login.login_user_id
  617. WHERE user.user_id = '".$_user['user_id']."'
  618. ORDER BY login.login_date DESC LIMIT 1";
  619. } else {
  620. $sql = "SELECT user.*, a.user_id is_admin
  621. FROM $user_table
  622. LEFT JOIN $admin_table a
  623. ON user.user_id = a.user_id
  624. WHERE user.user_id = '".$_user['user_id']."'";
  625. }
  626. $result = Database::query($sql);
  627. if (Database::num_rows($result) > 0) {
  628. // Extracting the user data
  629. $uData = Database::fetch_array($result);
  630. $_user ['firstName'] = $uData ['firstname' ];
  631. $_user ['lastName' ] = $uData ['lastname' ];
  632. $_user ['mail' ] = $uData ['email' ];
  633. $_user ['lastLogin'] = $uData ['login_date'];
  634. $_user ['official_code'] = $uData ['official_code'];
  635. $_user ['picture_uri'] = $uData ['picture_uri'];
  636. $_user ['user_id'] = $uData ['user_id'];
  637. $_user ['language'] = $uData ['language'];
  638. $_user ['auth_source'] = $uData ['auth_source'];
  639. $_user ['theme'] = $uData ['theme'];
  640. $_user ['status'] = $uData ['status'];
  641. $is_platformAdmin = (bool) (! is_null( $uData['is_admin']));
  642. $is_allowedCreateCourse = (bool) (($uData ['status'] == 1) or (api_get_setting('drhCourseManagerRights') and $uData['status'] == 4));
  643. api_session_register('_user');
  644. } else {
  645. header('location:'.api_get_path(WEB_PATH));
  646. //exit("WARNING UNDEFINED UID !! ");
  647. }
  648. } else { // no uid => logout or Anonymous
  649. api_session_unregister('_user');
  650. api_session_unregister('_uid');
  651. }
  652. api_session_register('is_platformAdmin');
  653. api_session_register('is_allowedCreateCourse');
  654. } else { // continue with the previous values
  655. $_user = $_SESSION['_user'];
  656. $is_platformAdmin = $_SESSION['is_platformAdmin'];
  657. $is_allowedCreateCourse = $_SESSION['is_allowedCreateCourse'];
  658. }
  659. /* COURSE INIT */
  660. if (isset($cidReset) && $cidReset) { // course session data refresh requested or empty data
  661. if ($cidReq) {
  662. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  663. $course_cat_table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  664. $sql = "SELECT course.*, course_category.code faCode, course_category.name faName
  665. FROM $course_table
  666. LEFT JOIN $course_cat_table
  667. ON course.category_code = course_category.code
  668. WHERE course.code = '$cidReq'";
  669. $result = Database::query($sql);
  670. if (Database::num_rows($result)>0) {
  671. $cData = Database::fetch_array($result);
  672. //@TODO real_cid should be cid, for working with numeric course id
  673. $_real_cid = $cData['id'];
  674. $_cid = $cData['code'];
  675. $_course = array();
  676. $_course['real_id'] = $cData['id'];
  677. $_course['id'] = $cData['code']; //auto-assigned integer
  678. $_course['name'] = $cData['title'];
  679. $_course['official_code'] = $cData['visual_code']; // use in echo
  680. $_course['sysCode'] = $cData['code']; // use as key in db
  681. $_course['path'] = $cData['directory']; // use as key in path
  682. $_course['dbName'] = $cData['db_name']; // use as key in db list
  683. $_course['dbNameGlu'] = $_configuration['table_prefix'] . $cData['db_name'] . $_configuration['db_glue']; // use in all queries
  684. $_course['titular'] = $cData['tutor_name'];
  685. $_course['language'] = $cData['course_language'];
  686. $_course['extLink']['url' ] = $cData['department_url'];
  687. $_course['extLink']['name'] = $cData['department_name'];
  688. $_course['categoryCode'] = $cData['faCode'];
  689. $_course['categoryName'] = $cData['faName'];
  690. $_course['visibility'] = $cData['visibility'];
  691. $_course['subscribe_allowed'] = $cData['subscribe'];
  692. $_course['unubscribe_allowed'] = $cData['unsubscribe'];
  693. api_session_register('_cid');
  694. api_session_register('_course');
  695. //@TODO real_cid should be cid, for working with numeric course id
  696. api_session_register('_real_cid');
  697. // if a session id has been given in url, we store the session
  698. if (api_get_setting('use_session_mode')=='true') {
  699. // Database Table Definitions
  700. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  701. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  702. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  703. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  704. if (!empty($_GET['id_session'])) {
  705. $_SESSION['id_session'] = Database::escape_string($_GET['id_session']);
  706. $sql = 'SELECT name FROM '.$tbl_session . ' WHERE id="'.intval($_SESSION['id_session']) . '"';
  707. $rs = Database::query($sql);
  708. list($_SESSION['session_name']) = Database::fetch_array($rs);
  709. } else {
  710. api_session_unregister('session_name');
  711. api_session_unregister('id_session');
  712. }
  713. }
  714. if ($_configuration['tracking_enabled'] && !isset($_SESSION['login_as'])) {
  715. //We add a new record in the course tracking table
  716. $course_tracking_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  717. $time = api_get_datetime();
  718. $sql="INSERT INTO $course_tracking_table(course_code, user_id, login_course_date, logout_course_date, counter, session_id)" .
  719. "VALUES('".$_course['sysCode']."', '".$_user['user_id']."', '$time', '$time', '1', ".api_get_session_id().")";
  720. Database::query($sql);
  721. }
  722. } else {
  723. //exit("WARNING UNDEFINED CID !! ");
  724. header('location:'.api_get_path(WEB_PATH));
  725. }
  726. } else {
  727. api_session_unregister('_cid');
  728. api_session_unregister('_real_cid');
  729. api_session_unregister('_course');
  730. }
  731. } else { // continue with the previous values
  732. if (empty($_SESSION['_course']) OR empty($_SESSION['_cid'])) { //no previous values...
  733. $_cid = -1; //set default values that will be caracteristic of being unset
  734. $_course = -1;
  735. } else {
  736. $_cid = $_SESSION['_cid' ];
  737. $_course = $_SESSION['_course'];
  738. // these lines are usefull for tracking. Indeed we can have lost the id_session and not the cid.
  739. // Moreover, if we want to track a course with another session it can be usefull
  740. if (!empty($_GET['id_session'])) {
  741. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  742. $_SESSION['id_session'] = Database::escape_string($_GET['id_session']);
  743. $sql = 'SELECT name FROM '.$tbl_session . ' WHERE id="'.intval($_SESSION['id_session']). '"';
  744. $rs = Database::query($sql);
  745. list($_SESSION['session_name']) = Database::fetch_array($rs);
  746. }
  747. if ($_configuration['tracking_enabled'] && !isset($_SESSION['login_as'])) {
  748. $course_tracking_table = Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS);
  749. if (isset($_configuration['session_lifetime'])) {
  750. $session_lifetime=$_configuration['session_lifetime'];
  751. } else {
  752. $session_lifetime=3600;
  753. }
  754. $course_code=$_course['sysCode'];
  755. $time = api_get_datetime();
  756. //We select the last record for the current course in the course tracking table
  757. // But only if the login date is < thant now + max_life_time
  758. $sql="SELECT course_access_id FROM $course_tracking_table
  759. WHERE user_id=".intval($_user ['user_id'])."
  760. AND course_code='$course_code'
  761. AND login_course_date > now() - INTERVAL $session_lifetime SECOND
  762. ORDER BY login_course_date DESC LIMIT 0,1";
  763. $result=Database::query($sql,__FILE__,__LINE__);
  764. if (Database::num_rows($result)>0) {
  765. $i_course_access_id = Database::result($result,0,0);
  766. //We update the course tracking table
  767. $sql="UPDATE $course_tracking_table " .
  768. "SET logout_course_date = '$time', " .
  769. "counter = counter+1 " .
  770. "WHERE course_access_id=".intval($i_course_access_id);
  771. Database::query($sql,__FILE__,__LINE__);
  772. } else {
  773. $sql="INSERT INTO $course_tracking_table
  774. (course_code, user_id, login_course_date, logout_course_date, counter)" .
  775. "VALUES('".$course_code."', '".$_user['user_id']."', '$time', '$time', '1')";
  776. Database::query($sql,__FILE__,__LINE__);
  777. }
  778. }
  779. }
  780. }
  781. /* COURSE / USER REL. INIT */
  782. if ((isset($uidReset) && $uidReset) || (isset($cidReset) && $cidReset)) { // session data refresh requested
  783. if (isset($_user['user_id']) && $_user['user_id'] && isset($_cid) && $_cid) { // have keys to search data
  784. if (api_get_setting('use_session_mode') != 'true') {
  785. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  786. $sql = "SELECT * FROM $course_user_table
  787. WHERE user_id = '".$_user['user_id']."' AND relation_type<>".COURSE_RELATION_TYPE_RRHH."
  788. AND course_code = '$cidReq'";
  789. $result = Database::query($sql);
  790. if (Database::num_rows($result) > 0) { // this user have a recorded state for this course
  791. $cuData = Database::fetch_array($result);
  792. $is_courseMember = true;
  793. $is_courseTutor = (bool) ($cuData['tutor_id' ] == 1 );
  794. $is_courseAdmin = (bool) ($cuData['status'] == 1 );
  795. api_session_register('_courseUser');
  796. } else { // this user has no status related to this course
  797. $is_courseMember = false;
  798. $is_courseAdmin = false;
  799. $is_courseTutor = false;
  800. }
  801. $is_courseAdmin = (bool) ($is_courseAdmin || $is_platformAdmin);
  802. } else {
  803. $tbl_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  804. $sql = "SELECT * FROM ".$tbl_course_user."
  805. WHERE user_id = '".$_user['user_id']."' AND relation_type<>".COURSE_RELATION_TYPE_RRHH."
  806. AND course_code = '$cidReq'";
  807. $result = Database::query($sql);
  808. if (Database::num_rows($result) > 0) { // this user have a recorded state for this course
  809. $cuData = Database::fetch_array($result);
  810. $_courseUser['role'] = $cuData['role' ];
  811. $is_courseMember = true;
  812. $is_courseTutor = (bool) ($cuData['tutor_id' ] == 1 );
  813. $is_courseAdmin = (bool) ($cuData['status'] == 1 );
  814. api_session_register('_courseUser');
  815. }
  816. if (!isset($is_courseAdmin)) { // this user has no status related to this course
  817. // is it the session coach or the session admin ?
  818. $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  819. $tbl_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  820. $tbl_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  821. $sql = " SELECT session.id_coach, session_admin_id FROM $tbl_session session,$tbl_session_course_user session_rcru
  822. WHERE session_rcru.id_session = session.id AND session_rcru.course_code = '$_cid' AND session_rcru.id_user='{$_user['user_id']}' AND session_rcru.status = 2";
  823. /*$sql = "SELECT session.id_coach, session_admin_id
  824. FROM ".$tbl_session." as session
  825. INNER JOIN ".$tbl_session_course_user."
  826. ON session_rel_course.id_session = session.id
  827. AND session_rel_course.course_code='$_cid'";
  828. */
  829. $result = Database::query($sql);
  830. $row = Database::store_result($result);
  831. if ($row[0]['id_coach']==$_user['user_id']) {
  832. $_courseUser['role'] = 'Professor';
  833. $is_courseMember = true;
  834. $is_courseTutor = true;
  835. $is_courseCoach = true;
  836. $is_sessionAdmin = false;
  837. if (api_get_setting('extend_rights_for_coach')=='true') {
  838. $is_courseAdmin = true;
  839. } else {
  840. $is_courseAdmin = false;
  841. }
  842. api_session_register('_courseUser');
  843. } elseif ($row[0]['session_admin_id']==$_user['user_id']) {
  844. $_courseUser['role'] = 'Professor';
  845. $is_courseMember = false;
  846. $is_courseTutor = false;
  847. $is_courseAdmin = false;
  848. $is_courseCoach = false;
  849. $is_sessionAdmin = true;
  850. } else {
  851. // Check if the current user is the course coach
  852. $sql = "SELECT 1
  853. FROM ".$tbl_session_course_user."
  854. WHERE course_code='$_cid'
  855. AND id_user = '".$_user['user_id']."'
  856. AND id_session = '".api_get_session_id()."'
  857. AND status = 2";
  858. $result = Database::query($sql);
  859. if ($row = Database::fetch_array($result)) {
  860. $_courseUser['role'] = 'Professor';
  861. $is_courseMember = true;
  862. $is_courseTutor = true;
  863. $is_courseCoach = true;
  864. $is_sessionAdmin = false;
  865. if (api_get_setting('extend_rights_for_coach')=='true') {
  866. $is_courseAdmin = true;
  867. } else {
  868. $is_courseAdmin = false;
  869. }
  870. api_session_register('_courseUser');
  871. } else {
  872. if (api_get_session_id() != 0) {
  873. // Check if the user is a student is this session
  874. $sql = "SELECT * FROM ".$tbl_session_course_user."
  875. WHERE id_user = '".$_user['user_id']."'
  876. AND id_session = '".api_get_session_id()."'
  877. AND course_code = '$cidReq' AND status NOT IN(2)";
  878. $result = Database::query($sql);
  879. if (Database::num_rows($result) > 0) { // this user have a recorded state for this course
  880. while($row = Database::fetch_array($result)){
  881. $is_courseMember = true;
  882. $is_courseTutor = false;
  883. $is_courseAdmin = false;
  884. $is_sessionAdmin = false;
  885. api_session_register('_courseUser');
  886. }
  887. }
  888. } else {
  889. //unregister user
  890. $is_courseMember = false;
  891. $is_courseTutor = false;
  892. $is_courseAdmin = false;
  893. $is_sessionAdmin = false;
  894. api_session_unregister('_courseUser');
  895. //$_course['visibility'] = 0; this depends the
  896. }
  897. }
  898. }
  899. }
  900. }
  901. } else { // keys missing => not anymore in the course - user relation
  902. // course
  903. $is_courseMember = false;
  904. $is_courseAdmin = false;
  905. $is_courseTutor = false;
  906. $is_courseCoach = false;
  907. $is_sessionAdmin = false;
  908. api_session_unregister('_courseUser');
  909. }
  910. //DEPRECATED
  911. //$is_courseAllowed=($_cid && ($_course['visibility'] || $is_courseMember || $is_platformAdmin))?true:false;
  912. //NEW
  913. if (isset($_course)) {
  914. if ($_course['visibility'] == COURSE_VISIBILITY_OPEN_WORLD)
  915. $is_allowed_in_course = true;
  916. elseif ($_course['visibility'] == COURSE_VISIBILITY_OPEN_PLATFORM && isset($_user['user_id']) && !api_is_anonymous($_user['user_id']))
  917. $is_allowed_in_course = true;
  918. elseif ($_course['visibility'] == COURSE_VISIBILITY_REGISTERED && ($is_platformAdmin || $is_courseMember))
  919. $is_allowed_in_course = true;
  920. elseif ($_course['visibility'] == COURSE_VISIBILITY_CLOSED && ($is_platformAdmin || $is_courseAdmin))
  921. $is_allowed_in_course = true;
  922. else $is_allowed_in_course = false;
  923. }
  924. // requires testing!!!
  925. // check the session visibility
  926. if ($is_allowed_in_course) {
  927. $my_session_id = api_get_session_id();
  928. //if I'm in a session
  929. //var_dump($is_platformAdmin, $is_courseTutor,api_is_coach());
  930. if ($my_session_id!=0)
  931. if (!$is_platformAdmin) {
  932. // admin and session coach are *not* affected to the invisible session mode
  933. // the coach is not affected because he can log in some days after the end date of a session
  934. $session_visibility = api_get_session_visibility($my_session_id);
  935. if ($session_visibility==SESSION_INVISIBLE)
  936. $is_allowed_in_course =false;
  937. }
  938. }
  939. // save the states
  940. api_session_register('is_courseMember');
  941. api_session_register('is_courseAdmin');
  942. //api_session_register('is_courseAllowed'); //deprecated old permission var
  943. api_session_register('is_courseTutor');
  944. api_session_register('is_allowed_in_course'); //new permission var
  945. api_session_register('is_courseCoach');
  946. api_session_register('is_sessionAdmin');
  947. } else { // continue with the previous values
  948. if (isset($_SESSION ['_courseUser'])) {
  949. $_courseUser = $_SESSION ['_courseUser'];
  950. }
  951. $is_courseMember = $_SESSION ['is_courseMember' ];
  952. $is_courseAdmin = $_SESSION ['is_courseAdmin' ];
  953. //$is_courseAllowed = $_SESSION ['is_courseAllowed']; //deprecated
  954. $is_allowed_in_course = $_SESSION ['is_allowed_in_course'];
  955. $is_courseTutor = $_SESSION ['is_courseTutor' ];
  956. $is_courseCoach = $_SESSION ['is_courseCoach' ];
  957. }
  958. /* GROUP INIT */
  959. if ((isset($gidReset) && $gidReset) || (isset($cidReset) && $cidReset)) { // session data refresh requested
  960. if ($gidReq && $_cid ) { // have keys to search data
  961. $group_table = Database::get_course_table(TABLE_GROUP);
  962. $sql = "SELECT * FROM $group_table WHERE id = '$gidReq'";
  963. $result = Database::query($sql);
  964. if (Database::num_rows($result) > 0) { // This group has recorded status related to this course
  965. $gpData = Database::fetch_array($result);
  966. $_gid = $gpData ['id' ];
  967. api_session_register('_gid');
  968. } else {
  969. exit("WARNING UNDEFINED GID !! ");
  970. }
  971. } elseif (isset($_SESSION['_gid']) or isset($_gid)) { // Keys missing => not anymore in the group - course relation
  972. api_session_unregister('_gid');
  973. }
  974. } elseif (isset($_SESSION['_gid'])) { // continue with the previous values
  975. $_gid = $_SESSION ['_gid' ];
  976. } else { //if no previous value, assign caracteristic undefined value
  977. $_gid = -1;
  978. }
  979. //set variable according to student_view_enabled choices
  980. if (api_get_setting('student_view_enabled') == "true") {
  981. if (isset($_GET['isStudentView'])) {
  982. if ($_GET['isStudentView'] == 'true') {
  983. if (isset($_SESSION['studentview'])) {
  984. if (!empty($_SESSION['studentview'])) {
  985. // switching to studentview
  986. $_SESSION['studentview'] = 'studentview';
  987. }
  988. }
  989. } elseif ($_GET['isStudentView'] == 'false') {
  990. if (isset($_SESSION['studentview'])) {
  991. if (!empty($_SESSION['studentview'])) {
  992. // switching to teacherview
  993. $_SESSION['studentview'] = 'teacherview';
  994. }
  995. }
  996. }
  997. } elseif (!empty($_SESSION['studentview'])) {
  998. //all is fine, no change to that, obviously
  999. } elseif (empty($_SESSION['studentview'])) {
  1000. // We are in teacherview here
  1001. $_SESSION['studentview'] = 'teacherview';
  1002. }
  1003. }
  1004. if (isset($_cid)) {
  1005. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  1006. $time = api_get_datetime();
  1007. $sql="UPDATE $tbl_course SET last_visit= '$time' WHERE code='$_cid'";
  1008. Database::query($sql);
  1009. }