dokeos-include
[ class tree: dokeos-include ] [ index: dokeos-include ] [ all elements ]

Source for file local.inc.php

Documentation is available at local.inc.php

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

Documentation generated on Thu, 12 Jun 2008 14:00:50 -0500 by phpDocumentor 1.4.1