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

Source for file filter_user.lib.php

Documentation is available at filter_user.lib.php

  1. <?php //$id:$
  2. /**
  3.  * This script declares a set of functions that will enable authorization check
  4.  * for a user's access to a course directory, as well as course name
  5.  * translations for search results display.
  6.  * @package dokeos.search
  7.  * @author Yannick Warnier <yannick.warnier@dokeos.com>
  8.  * @uses The Dokeos database library, to access the tables using its facilities
  9.  * @uses The Dokeos main api library to execute database queries
  10.  */
  11. /**
  12.  * Checks if a user can access a given course
  13.  * 
  14.  * The function gets the course code from the course directory, then
  15.  * checks in the course_user table if the user has access to that course.
  16.  * @param integer User ID (inside Dokeos)
  17.  * @param string  Course directory
  18.  * @return boolean True if user has access, false otherwise
  19.  */
  20. function get_boolean_user_access_to_course_dir($user_id,$course_dir){
  21.   if(api_is_platform_admin()){return true;}
  22.   $course      Database::get_main_table(TABLE_MAIN_COURSE);
  23.   //Get the course code
  24.   $sql "SELECT code FROM $course WHERE directory = '$course_dir'";
  25.   $res api_sql_query($sql);
  26.   if(Database::num_rows($res)>0){
  27.     //Course found. Get the course code.
  28.     $row Database::fetch_array($res);
  29.     $course_code $row['code'];
  30.     //Check user permissions
  31.     $sql "SELECT * FROM $course_user 
  32.         WHERE course_code = '$course_code
  33.     AND user_id = '$user_id'";
  34.     $res api_sql_query($sql);
  35.     if(Database::num_rows($res)>0){
  36.       //User permission found, go further and check there is a status
  37.       $row Database::fetch_array($res);
  38.       $rel $row['status'];
  39.       //if(!empty($rel)){
  40.         //Status found (we may later check this further to refine permissions)
  41.         //Sometimes for now it appears that the status can be 0, though.
  42.           return true;
  43.       //}
  44.       //Status not found, problem, return false.
  45.       //return false;
  46.     }else{
  47.       //No course-user relation found, return false
  48.       return false;
  49.     }
  50.   }else{
  51.     //No course found, return false
  52.     return false;
  53.   }
  54. }
  55. /**
  56.  * Check course URL to get a course code and check it against user permissions
  57.  *
  58.  * Make this function always return true when no check is to be done
  59.  * @param    string    URL to check
  60.  * @return    boolean    True on user having access to the course or course not found, false otherwise
  61.  
  62.  */
  63. function access_check($url,$default=true){
  64.   $matches array()
  65.   $match1 preg_match('/courses\/([^\/]*)\//',$url,$matches);
  66.   if(!$match1){
  67.     $match2 preg_match('/cidReq=([^&]*)/',$url,$matches)
  68.   }
  69.   if($match1 or $match2){
  70.     $has_access get_boolean_user_access_to_course_dir($_SESSION['_user']['user_id'],$matches[1]);
  71.     if(!$has_access){
  72.       //user has no access to this course, skip it
  73.       return false;
  74.     }//else grant access
  75.     else
  76.     {
  77.       return true;
  78.     }
  79.   }
  80.   return $default;
  81. }
  82. /**
  83.  * Translates a course code into a course name into a string
  84.  * 
  85.  * This function should only be used if needed by a funny course-name rule
  86.  * @param    string    The string to transform
  87.  * @result    string    The transformed string
  88.  */
  89. function subst_course_code($string){
  90.   $matches array();
  91.   if(preg_match('/(PORTAL_[0-9]{1,4})/',$string,$matches)){
  92.     $course      Database::get_main_table(TABLE_MAIN_COURSE);
  93.     //Get the course code
  94.     $sql "SELECT title FROM $course WHERE code = '".$matches[1]."'";
  95.     $res api_sql_query($sql);
  96.     if(Database::num_rows($res)>0){
  97.       $row Database::fetch_array($res);
  98.       $string preg_replace('/(.*)\?cidReq=('.$matches[1].')(.*)/',' '.$row['title'].' - \1 \3',$string);
  99.       $string preg_replace('/'.$matches[1].'/',$row['title'],$string);
  100.     }
  101.   }
  102.   return $string;
  103. }
  104. ?>

Documentation generated on Thu, 12 Jun 2008 13:29:57 -0500 by phpDocumentor 1.4.1