Source for file user.php

Documentation is available at user.php

  1. <?php    
  2.     /**
  3.     * Sloodle user library.
  4.     *
  5.     * Provides functionality for reading, managing and editing user data.
  6.     *
  7.     * @package sloodle
  8.     * @copyright Copyright (c) 2007-8 Sloodle (various contributors)
  9.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  10.     * @since Sloodle 0.2
  11.     *
  12.     * @contributor Peter R. Bloomfield
  13.     *
  14.     */
  15.     
  16.     // This library expects that the Sloodle config file has already been included
  17.     //  (along with the Moodle libraries)
  18.     
  19.     /** Include the Sloodle IO library. */
  20.     require_once(SLOODLE_DIRROOT.'/lib/io.php');
  21.     /** Include the general Sloodle functionality. */
  22.     require_once(SLOODLE_DIRROOT.'/lib/general.php');
  23.     /** Include the Sloodle course data structure. */
  24.     require_once(SLOODLE_DIRROOT.'/lib/course.php');
  25.     /** Include the user object data structure */
  26.     require_once(SLOODLE_DIRROOT.'/lib/user_object.php');
  27.     
  28.     
  29.     /**
  30.     * A class to represent a single user, including Moodle and Sloodle data.
  31.     * @package sloodle
  32.     */
  33.     class SloodleUser
  34.     {
  35.     // DATA //
  36.     
  37.         /**
  38.         * Internal only - reference to the containing {@link SloodleSession} object.
  39.         * Note: always check that it is not null before use!
  40.         * @var object 
  41.         * @access protected
  42.         */
  43.         var $_session = null;
  44.     
  45.         /**
  46.         * Internal only - avatar data.
  47.         * In Moodle, corresponds to a record from the 'sloodle_users' table.
  48.         * @var object 
  49.         * @access private
  50.         */
  51.         var $avatar_data = null;
  52.         
  53.         /**
  54.         * Internal only - user data. (i.e. VLE user)
  55.         * In Moodle, corresponds to a record from the 'user' table.
  56.         * @var obejct 
  57.         * @access private
  58.         */
  59.         var $user_data = null;
  60.         
  61.         
  62.     // CONSTRUCTOR //
  63.     
  64.         /**
  65.         * Class constructor.
  66.         * @param object &$_session Reference to the containing {@link SloodleSession} object, if available.
  67.         * @access public
  68.         */
  69.         function SloodleUser(&$_session)
  70.         {
  71.             if (!is_null($_session)) $this->_session = &$_session;
  72.         }
  73.         
  74.         
  75.     // ACCESSORS //
  76.     
  77.         /**
  78.         * Gets the unique ID of the avatar.
  79.         * @return mixed Type depends on VLE. (Integer on Moodle). Returns null if there is no avatar.
  80.         * @access public
  81.         */
  82.         function get_avatar_id()
  83.         {
  84.             if (!isset($this->avatar_data->id)) return null;
  85.             return $this->avatar_data->id;
  86.         }
  87.         
  88.         /**
  89.         * Gets the unique ID of the VLE user.
  90.         * @return mixed Type depends on VLE. (Integer on Moodle). Returns null if there is no user
  91.         * @access public
  92.         */
  93.         function get_user_id()
  94.         {
  95.             if (!isset($this->user_data->id)) return null;
  96.             return $this->user_data->id;
  97.         }
  98.         
  99.         /**
  100.         * Determines whether or not an avatar is loaded.
  101.         * @return bool 
  102.         */
  103.         function is_avatar_loaded()
  104.         {
  105.             return isset($this->avatar_data);
  106.         }
  107.         
  108.         /**
  109.         * Determines whether or not a VLE user is loaded.
  110.         * @return bool 
  111.         */
  112.         function is_user_loaded()
  113.         {
  114.             return isset($this->user_data);
  115.         }
  116.         
  117.         
  118.         /**
  119.         * Gets the UUID of the avatar
  120.         * @return string 
  121.         */
  122.         function get_avatar_uuid()
  123.         {
  124.             return $this->avatar_data->uuid;
  125.         }
  126.         
  127.         /**
  128.         * Sets the UUID of the avatar
  129.         * @param string $uuid The new UUID
  130.         * @return void 
  131.         */
  132.         function set_avatar_uuid($uuid)
  133.         {
  134.             $this->avatar_data->uuid $uuid;
  135.         }
  136.         
  137.         /**
  138.         * Gets the name of the avatar
  139.         * @return string 
  140.         */
  141.         function get_avatar_name()
  142.         {
  143.             return $this->avatar_data->avname;
  144.         }
  145.         
  146.         /**
  147.         * Sets the name of the avatar
  148.         * @param string $avname The new avatar name
  149.         * @return void 
  150.         */
  151.         function set_avatar_name($avname)
  152.         {
  153.             $this->avatar_data->avname $avname;
  154.         }
  155.         
  156.         /**
  157.         * Gets the user's username
  158.         * @return string 
  159.         */
  160.         function get_username()
  161.         {
  162.             return $this->user_data->username;
  163.         }
  164.         
  165.         /**
  166.         * Gets the first name of the user
  167.         * @return string 
  168.         */
  169.         function get_user_firstname()
  170.         {
  171.             return $this->user_data->firstname;
  172.         }
  173.         
  174.         /**
  175.         * Gets the last name of the user
  176.         * @return string 
  177.         */
  178.         function get_user_lastname()
  179.         {
  180.             return $this->user_data->lastname;
  181.         }
  182.         
  183.         /**
  184.         * Gets the timestamp of whenever the avatar was last active
  185.         * @return int 
  186.         */
  187.         function get_avatar_last_active()
  188.         {
  189.             return (int)$this->avatar_data->lastactive;
  190.         }
  191.         
  192.         /**
  193.         * Sets the timestamp of when the user was last active
  194.         * @param int $timestamp A UNIX timestamp, or null to use the current time
  195.         * @return void 
  196.         */
  197.         function set_avatar_last_active($timestamp null)
  198.         {
  199.             if ($timestamp == null$timestamp time();
  200.             $this->avatar_data->lastactive $timestamp;
  201.         }
  202.         
  203.         /**
  204.         * Gets the user's email address.
  205.         * @return string|nullThe user's email address, or null if none is specified of if email is disabled.
  206.         */
  207.         function get_user_email()
  208.         {
  209.             if (isset($this->user_data->email&& !empty($this->user_data->emailstop))
  210.                 return $this->user_data->email;
  211.             return null;
  212.         }
  213.         
  214.         
  215.     // USER LINK FUNCTIONS //
  216.         
  217.         /**
  218.         * Determines whether or not the current user and avatar are linked.
  219.         * @return bool True if they are linked, or false if not.
  220.         */
  221.         function is_avatar_linked()
  222.         {
  223.             // Make sure there is data in both caches
  224.             if (empty($this->avatar_data|| empty($this->user_data)) return false;
  225.             // Check for the link (ignore the number 0, as that is not a valid ID)
  226.             if ($this->avatar_data->userid != && $this->avatar_data->userid == $this->user_data->idreturn true;
  227.             return false;
  228.         }
  229.     
  230.         /**
  231.         * Links the current avatar to the current user.
  232.         * <b>NOTE:</b> does not remove any other avatar links to the VLE user.
  233.         * @return bool True if successful or false otherwise.
  234.         * @access public
  235.         */
  236.         function link_avatar()
  237.         {
  238.             // Make sure there is data in both caches
  239.             if (empty($this->avatar_data|| empty($this->user_data)) return false;
  240.             
  241.             // Set the linked user ID and update the database record
  242.             $olduserid $this->avatar_data->userid;
  243.             $this->avatar_data->userid $this->user_data->id;
  244.             if (update_record('sloodle_users'$this->avatar_data)) return true;
  245.             // The operation failed, so change the user ID back
  246.             $this->avatar_data->userid $olduserid;
  247.             return false;
  248.         }
  249.         
  250.         
  251.     // DATABASE FUNCTIONS //
  252.     
  253.         /**
  254.         * Deletes the current avatar from the database.
  255.         * @return bool True if successful, or false on failure
  256.         * @access public
  257.         */
  258.         function delete_avatar()
  259.         {
  260.             // Make sure we have avatar data
  261.             if (empty($this->avatar_data)) return false;
  262.             
  263.             // Attempt to delete the record from the database
  264.             return delete_records('sloodle_users''id'$this->avatar_data->id);
  265.         }
  266.         
  267.         /**
  268.         * Loads the specified avatar from the database.
  269.         * @param mixed $id The ID of the avatar (type depends on VLE; integer for Moodle)
  270.         * @return bool True if successful, or false otherwise.
  271.         * @access public
  272.         */
  273.         function load_avatar_by_id($id)
  274.         {
  275.             // Make sure the ID is valid
  276.             if (!is_int($id|| $id <= 0return false;
  277.             // Fetch the avatar data
  278.             $this->avatar_data = get_record('sloodle_users''id'$id);
  279.             if (!$this->avatar_data{
  280.                 $this->avatar_data = null;
  281.                 return false;
  282.             }
  283.             return true;
  284.         }
  285.         
  286.         /**
  287.         * Finds an avatar with the given UUID and/or name, and loads its data.
  288.         * The UUID is searched for first. If that is not found, then the name is used.
  289.         * @param string $uuid The UUID of the avatar, or blank to search only by name.
  290.         * @param string $avname The name of the avatar, or blank to search only by UUID.
  291.         * @return bool True if successful, or false otherwise
  292.         * @access public
  293.         */
  294.         function load_avatar($uuid$avname)
  295.         {
  296.             // Both parameters can't be empty
  297.             if (empty($uuid&& empty($avname)) return false;
  298.             
  299.             // Attempt to search by UUID first
  300.             if (!empty($uuid)) {
  301.                 $this->avatar_data = get_record('sloodle_users''uuid'$uuid);
  302.                 if ($this->avatar_datareturn true;
  303.             }
  304.             
  305.             // Attempt to search by name
  306.             if (!empty($avname)) {
  307.                 $this->avatar_data = get_record('sloodle_users''avname'$avname);
  308.                 if ($this->avatar_datareturn true;
  309.             }
  310.             
  311.             // The search failed
  312.             $this->avatar_data = null;
  313.             return false;
  314.         }
  315.         
  316.         /**
  317.         * Load the specified user from the database
  318.         * @param mixed $id The unique identifier for the VLE user. (Type depends on VLE; integer for Moodle)
  319.         * @return bool True if successful, or false on failure
  320.         * @access public
  321.         */
  322.         function load_user($id)
  323.         {
  324.             // Make sure the ID is valid
  325.             $id = (int)$id;
  326.             if ($id <= 0return false;
  327.             
  328.             // Attempt to load the data
  329.             $this->user_data = get_complete_user_data('id'$id);
  330.             if (!$this->user_data{
  331.                 $this->user_data = null;
  332.                 return false;
  333.             }
  334.  
  335.             
  336.             return true;
  337.         }
  338.         
  339.         /**
  340.         * Uses the current avatar data to update the database.
  341.         * @return bool True if successful, or false if the update fails
  342.         * @access public
  343.         */
  344.         function write_avatar()
  345.         {
  346.             // Make sure we have avatar data
  347.             if (empty($this->avatar_data|| $this->avatar_data->id <= 0return false;
  348.             // Make the update
  349.             return update_record('sloodle_users'$this->avatar_data);
  350.         }
  351.         
  352.         /**
  353.         * Adds a new avatar to the database, and link it to the specified user.
  354.         * If successful, it deletes any matching avatar details from pending users list.
  355.         * @param mixed $userid Site-wide unique ID of a user (type depends on VLE; integer for Moodle)
  356.         * @param string $uuid UUID of the avatar
  357.         * @param string $avname Name of the avatar
  358.         * @return bool True if successful, or false if not.
  359.         * @access public
  360.         */
  361.         function add_linked_avatar($userid$uuid$avname)
  362.         {
  363.             // Setup our object
  364.             $this->avatar_data = new stdClass();
  365.             $this->avatar_data->id 0;
  366.             $this->avatar_data->userid $userid;
  367.             $this->avatar_data->uuid $uuid;
  368.             $this->avatar_data->avname $avname;
  369.             
  370.             // Add the data to the database
  371.             $this->avatar_data->id insert_record('sloodle_users'$this->avatar_data);
  372.             if (!$this->avatar_data->id{
  373.                 $this->avatar_data = null;
  374.                 return false;
  375.             }
  376.             
  377.             // Delete any pending avatars with the same details
  378.             delete_records('sloodle_pending_avatars''uuid'$uuid'avname'$avname);
  379.             
  380.             return true;
  381.         }
  382.         
  383.         /**
  384.         * Adds a new unlinked avatar to the database (the entry is pending linking)
  385.         * @param string $uuid UUID of the avatar
  386.         * @param string $avname Name of the avatar
  387.         * @param int $timestamp The timestamp at which to mark the update (or null to use the current timestamp). Entries expire after a certain period.
  388.         * @return object|boolReturns the database object if successul, or false if not.
  389.         * @access public
  390.         */
  391.         function add_pending_avatar($uuid$avname$timestamp null)
  392.         {
  393.             // Setup the timestamp
  394.             if ($timestamp == null$timestamp time();
  395.             
  396.             // Setup our object
  397.             $pending_avatar new stdClass();
  398.             $pending_avatar->id 0;
  399.             $pending_avatar->uuid $uuid;
  400.             $pending_avatar->avname $avname;
  401.             $pending_avatar->lst sloodle_random_security_token();
  402.             $pending_avatar->timeupdated $timestamp;
  403.             
  404.             // Add the data to the database
  405.             $pending_avatar->id insert_record('sloodle_pending_avatars'$pending_avatar);
  406.             if (!$pending_avatar->id{
  407.                 return false;
  408.             }
  409.             
  410.             return $pending_avatar;
  411.         }
  412.         
  413.         
  414.         /**
  415.         * Auto-register a new user account for the current avatar.
  416.         * NOTE: this does NOT respect ANYTHING but the most basic Moodle accounts.
  417.         * Use at your own risk!
  418.         * @return string|boolThe new password (plaintext) if successful, or false if not
  419.         * @access public
  420.         */
  421.         function autoregister_avatar_user()
  422.         {
  423.             global $CFG;
  424.         
  425.             // Make sure we have avatar data, and reset the user data
  426.             if (empty($this->avatar_data)) return false;
  427.             $this->user_data = null;
  428.             
  429.             // Construct a basic username
  430.             $nameparts explode(' '$this->avatar_data->avname);
  431.             $baseusername strip_tags(stripslashes(implode(''$nameparts)));
  432.             $username $baseusername;
  433.             $conflict_moodle record_exists('user''username'$username);
  434.             
  435.             // If that didn't work, then try a few random variants (just a number added to the end of the name)
  436.             $MAX_RANDOM_TRIES 3;
  437.             $rnd_try 0;
  438.             while ($rnd_try $MAX_RANDOM_TRIES && $conflict_moodle{
  439.                 // Pick a random 3 digit number
  440.                 $rnd_num mt_rand(100998);
  441.                 if ($rnd_num >= 666$rnd_num++// Some users may object to this number
  442.                 
  443.                 // Construct a new username to try
  444.                 $username $baseusername . (string)$rnd_num;
  445.                 // Check for conflicts
  446.                 $conflict_moodle record_exists('user''username'$username);
  447.                 
  448.                 // Next attempt
  449.                 $rnd_try++;
  450.             }
  451.             
  452.             // Stop if we haven't found a unique name
  453.             if ($conflict_moodlereturn false;
  454.             
  455.             // Looks like we got an OK username
  456.             // Generate a random password
  457.             $plain_password sloodle_random_web_password();
  458.             
  459.             // Create the new user
  460.             $this->user_data = create_user_record($username$plain_password);
  461.             if (!$this->user_data{
  462.                 $this->user_data = null;
  463.                 return false;
  464.             }
  465.             
  466.             // Get the complete user data again, so that we have the password this time
  467.             $this->user_data = get_complete_user_data('id'$this->user_data->id);
  468.             
  469.             // Attempt to use the first and last names of the avatar
  470.             $this->user_data->firstname $nameparts[0];
  471.             if (isset($nameparts[1])) $this->user_data->lastname $nameparts[1];
  472.             else $this->user_data->lastname $nameparts[0];
  473.             // Prevent emails from being sent to this user
  474.             $this->user_data->emailstop 1;
  475.             
  476.             // Attempt to update the database (we don't really care if this fails, since everything else will have worked)
  477.             update_record('user'$this->user_data);
  478.             
  479.             // Now link the avatar to this account
  480.             $this->avatar_data->userid $this->user_data->id;
  481.             update_record('sloodle_users'$this->avatar_data);
  482.             
  483.             return $plain_password;
  484.         }
  485.        
  486.         /**
  487.         * Load the avatar linked to the current user.
  488.         * @return bool,string True if a link was loaded, false if there was no link, or string 'multi' if multiple avatars are linked
  489.         * @access public
  490.         */
  491.         function load_linked_avatar()
  492.         {
  493.             // Make sure we have some user data
  494.             if (empty($this->user_data)) return false;
  495.             $this->avatar_data = null;
  496.             
  497.             // Fetch all avatar records which are linked to the user
  498.             $recs get_records('sloodle_users''userid'$this->user_data->id);
  499.             if (!is_array($recs)) return false;
  500.             if (count($recs1return 'multi';
  501.             
  502.             // Store the avatar data
  503.             reset($recs);
  504.             $this->avatar_data = current($recs);
  505.             return true;
  506.         }
  507.  
  508.         /**
  509.         * Find the VLE user linked to the current avatar.
  510.         * @return bool True if successful, or false if no link was found
  511.         * @access public
  512.         */
  513.         function load_linked_user()
  514.         {
  515.             // Make sure we have some avatar data
  516.             if (empty($this->avatar_data)) return false;
  517.             
  518.             // Fetch the user data
  519.             $this->user_data = get_complete_user_data('id'$this->avatar_data->userid);
  520.             if ($this->user_datareturn true;
  521.             return false;
  522.         }
  523.         
  524.         
  525.     ///// LOGIN FUNCTIONS /////
  526.     
  527.         /**
  528.         * Internally 'log-in' the current user.
  529.         * In Moodle, this just stores all the user data in the global $USER variable.
  530.         * This function will not perform automatic registration.
  531.         * @return bool True if successful, or false otherwise.
  532.         * @access public
  533.         */
  534.         function login()
  535.         {
  536.             global $USER;
  537.             // Make sure we have some user data
  538.             if (empty($this->user_data)) return false;
  539.             $USER get_complete_user_data('id'$this->user_data->id);
  540.             return true;
  541.         }
  542.         
  543.         
  544.     ///// COURSE FUNCTIONS /////
  545.     
  546.         /**
  547.         * Gets a numeric array of {@link SloodleCourse} objects for courses the user is enrolled in.
  548.         * WARNING: this function is not very efficient, and will likely be very slow on large sites.
  549.         * @param mixed $category Unique identifier of a category to limit the query to. Ignored if null. (Type depends on VLE; integer for Moodle)
  550.         * @return array A numeric array of {@link SloodleCourse} objects
  551.         * @access public
  552.         */
  553.         function get_enrolled_courses($category null)
  554.         {
  555.             // Make sure we have user data
  556.             if (empty($this->user_data)) return array();
  557.             // If it is the guess user, then they are not enrolled at all
  558.             if (isguestuser($this->user_data->id)) return array();            
  559.             
  560.             // Convert the category ID as appropriate
  561.             if ($category == null || $category || !is_int($category)) $category 0;
  562.             
  563.             // Modified from "get_user_capability_course()" in Moodle's "lib/accesslib.php"
  564.             
  565.             // Get a list of all courses on the system
  566.             $usercourses array();
  567.             $courses get_courses($category);
  568.             // Go through each course
  569.             foreach ($courses as $course{
  570.                 // Check if the user can view this course and is not a guest in it.
  571.                 // (Note: the site course is always available to all users.)
  572.                 $course_context get_context_instance(CONTEXT_COURSE$course->id);
  573.                 if ($course->id == SITEID || (has_capability('moodle/course:view'$course_context$this->user_data->id&& !has_capability('moodle/legacy:guest'$course_context$this->user_data->idfalse))) {
  574.                     $sc new SloodleCourse();
  575.                     $sc->load($course);
  576.                     $usercourses[$sc;
  577.                 }
  578.             }
  579.             return $usercourses;
  580.         }
  581.         
  582.         /**
  583.         * Gets a numeric array of {@link SloodleCourse} objects for courses the user is Sloodle staff.
  584.         * This relates to the "mod/sloodle:staff" capability.
  585.         * WARNING: this function is not very efficient, and will likely be very slow on large sites.
  586.         * @param mixed $category Unique identifier of a category to limit the query to. Ignored if null. (Type depends on VLE; integer for Moodle)
  587.         * @return array A numeric array of {@link SloodleCourse} objects
  588.         * @access public
  589.         */
  590.         function get_staff_courses($category null)
  591.         {
  592.             // Make sure we have user data
  593.             if (empty($this->user_data)) return array();
  594.             
  595.             // Convert the category ID as appropriate
  596.             if ($category == null || $category || !is_int($category)) $category 0;
  597.             
  598.             // Modified from "get_user_capability_course()" in Moodle's "lib/accesslib.php"
  599.             
  600.             // Get a list of all courses on the system
  601.             $usercourses array();
  602.             $courses get_courses($category);
  603.             // Go through each course
  604.             foreach ($courses as $course{
  605.                 // Check if the user can teach using Sloodle on this course
  606.                 if (has_capability('mod/sloodle:staff'get_context_instance(CONTEXT_COURSE$course->id)$this->user_data->id)) {
  607.                     $sc new SloodleCourse();
  608.                     $sc->load($course);
  609.                     $usercourses[$sc;
  610.                 }
  611.             }
  612.             return $usercourses;
  613.         }
  614.         
  615.         /**
  616.         * Is the current user enrolled in the specified course?
  617.         * NOTE: a side effect of this is that it logs-in the user
  618.         * @param mixed $course Unique identifier of the course -- type depends on VLE (integer for Moodle)
  619.         * @param bool True if the user is enrolled, or false if not.
  620.         * @access public
  621.         * @todo Update to match parameter format and handling of {@link enrol()} function.
  622.         */
  623.         function is_enrolled($courseid)
  624.         {
  625.             global $USER;
  626.             // Attempt to log-in the user
  627.             if (!$this->login()) return false;
  628.             
  629.             // NOTE: this stuff was lifted from the Moodle 1.8 "course/enrol.php" script
  630.             
  631.             // Create a context for this course
  632.             if (!$context get_context_instance(CONTEXT_COURSE$courseid)) return false;
  633.             // Ensure we have up-to-date capabilities for the current user
  634.             load_all_capabilities();
  635.             
  636.             // Check if the user can view the course, and does not simply have guest access to it
  637.             // Allow the site course
  638.             return ($courseid == SITEID || (has_capability('moodle/course:view'$context&& !has_capability('moodle/legacy:guest'$contextNULLfalse)));
  639.         }
  640.         
  641.         /**
  642.         * Is the current user Sloodle staff in the specified course?
  643.         * NOTE: a side effect of this is that it logs-in the user
  644.         * @param mixed $course Unique identifier of the course -- type depends on VLE (integer for Moodle)
  645.         * @param bool True if the user is staff, or false if not.
  646.         * @access public
  647.         * @todo Update to match parameter format and handling of {@link enrol()} function.
  648.         */
  649.         function is_staff($courseid)
  650.         {
  651.             global $USER;
  652.             // Attempt to log-in the user
  653.             if (!$this->login()) return false;
  654.             
  655.             // NOTE: this stuff was lifted from the Moodle 1.8 "course/enrol.php" script
  656.             
  657.             // Create a context for this course
  658.             if (!$context get_context_instance(CONTEXT_COURSE$courseid)) return false;
  659.             // Ensure we have up-to-date capabilities for the current user
  660.             load_all_capabilities();
  661.             
  662.             // Check if the user can view the course, does not simply have guest access to it, *and* is staff
  663.             return (has_capability('moodle/course:view'$context&& !has_capability('moodle/legacy:guest'$contextNULLfalse&& has_capability('mod/sloodle:staff'$context));
  664.         }
  665.         
  666.         /**
  667.         * Enrols the current user in the specified course
  668.         * NOTE: a side effect of this is that it logs-in the user
  669.         * @param object $sloodle_course {@link SloodleCourse} object setup for the necessary course. If null, then the {@link $_session} member is queried instead.
  670.         * @param bool True if successful (or the user was already enrolled), or false otherwise
  671.         * @access public
  672.         */
  673.         function enrol($sloodle_course null)
  674.         {
  675.             global $USER$CFG;
  676.             // Attempt to log-in the user
  677.             if (!$this->login()) return false;
  678.             
  679.             // Was course data provided?
  680.             if (empty($sloodle_course)) {
  681.                 // No - attempt to get some from the Sloodle session
  682.                 if (empty($this->_session)) return false;
  683.                 if (empty($this->_session->course)) return false;
  684.                 $sloodle_course $this->_session->course;
  685.             }
  686.             
  687.             // NOTE: much of this stuff was lifted from the Moodle 1.8 "course/enrol.php" script
  688.             
  689.             // Fetch the Moodle course data, and a course context
  690.             $course $sloodle_course->get_course_object();
  691.             if (!$context get_context_instance(CONTEXT_COURSE$course->id)) return false;
  692.             
  693.             // Ensure we have up-to-date capabilities for the current user
  694.             load_all_capabilities();
  695.             
  696.             // Check if the user can view the course, and does not simply have guest access to it
  697.             // (No point trying to enrol somebody if they are already enrolled!)
  698.             if (has_capability('moodle/course:view'$context&& !has_capability('moodle/legacy:guest'$contextNULLfalse)) return true;
  699.             
  700.             // Make sure auto-registration is enabled for this site/course, and that the controller (if applicable) is enabled
  701.             if (!$sloodle_course->check_autoreg()) return false;            
  702.             
  703.             // Can't enrol users on meta courses or the site course
  704.             if ($course->metacourse || $course->id == SITEIDreturn false;
  705.             
  706.             // Is there an enrolment period in effect?
  707.             if ($course->enrolperiod{
  708.                 if ($roles get_user_roles($context$USER->id)) {
  709.                     foreach ($roles as $role{
  710.                         if ($role->timestart && ($role->timestart >= time())) {
  711.                             return false;
  712.                         }
  713.                     }
  714.                 }
  715.             }
  716.             // Make sure the course is enrollable
  717.             if (!$course->enrollable ||
  718.                     ($course->enrollable == && $course->enrolstartdate && $course->enrolstartdate time()) ||
  719.                     ($course->enrollable == && $course->enrolenddate && $course->enrolenddate <= time())
  720.             {
  721.                 return false;
  722.             }
  723.             
  724.             // Finally, after all that, enrol the user
  725.             if (!enrol_into_course($course$USER'manual')) return false;
  726.         
  727.             // Everything seems fine
  728.             // Log the auto-enrolment
  729.             add_to_log($course->id'sloodle''update''''auto-enrolment');
  730.             return true;
  731.         }
  732.     
  733.     
  734.     ///// PASSWORD /////
  735.     
  736.         /**
  737.         * Resets the user's password
  738.         * @param bool $require If true, then the script will be terminated if the operation fails
  739.         * @return string|boolThe new password if successful, or false otherwise (if $require was false).
  740.         */
  741.         function reset_password($require true)
  742.         {
  743.             // Check that the user is loaded
  744.             if (empty($this->user_data)) {
  745.                 if ($require{
  746.                     $this->_session->response->quick_output(-301'USER_AUTH''User data not loaded'false);
  747.                     exit();
  748.                 }
  749.                 return false;
  750.             }
  751.             // If the user has an email address on file, then we can't reset the password
  752.             if (!empty($this->user_data->email)) {
  753.                 if ($require{
  754.                     $this->_session->response->quick_output(-341'USER_AUTH''User has email address in database. Cannot use Sloodle password reset.'false);
  755.                     exit();
  756.                 }
  757.                 return false;
  758.             }
  759.             
  760.             // Generate a new random password
  761.             $password sloodle_random_web_password();
  762.             // Update the user's password data
  763.             if (!update_internal_user_password($this->user_data$password)) {
  764.                 if ($require{
  765.                     $this->_session->response->quick_output(-103'SYSTEM''Failed to update user password'false);
  766.                     exit();
  767.                 }
  768.                 return false;
  769.             }
  770.             
  771.             return $password;
  772.         }
  773.         
  774.         /**
  775.         * If the system is waiting to send a password notification to this user, then remove it
  776.         * @return void 
  777.         */
  778.         function purge_password_notifications()
  779.         {
  780.             // Check that the user is loaded
  781.             if (empty($this->user_data)) return;
  782.             // Delete the database entries
  783.             delete_records('sloodle_login_notifications''username'$this->user_data->username);
  784.         }
  785.         
  786.         
  787.     ///// USER-CENTRIC OBJECTS /////
  788.     
  789.         /**
  790.         * Authorises the given object for the current avatar.
  791.         * Note: the object must already exist in the database.
  792.         * @param int $authid The ID of the authorisation entry
  793.         * @return bool True if successful, or false otherwise
  794.         */
  795.         function authorise_user_object($authid)
  796.         {
  797.             // Make sure an avatar is loaded
  798.             if (!$this->is_avatar_loaded()) return false;
  799.             
  800.             // Does the object already exist in the database?
  801.             $auth get_record('sloodle_user_object''id'$authid'avuuid'$this->get_avatar_uuid());
  802.             if (!$authreturn false;
  803.             // Update the existing record
  804.             $auth->authorised 1;
  805.             $auth->timeupdated time();
  806.             
  807.             return update_record('sloodle_user_object'$auth);
  808.         }
  809.         
  810.         
  811.         /**
  812.         * Adds or udpates the given user object as unauthorised.
  813.         * (This function can be called statically).
  814.         * @param string $avuuid UUID of the avatar the object will be authorised for
  815.         * @param string $objuuid UUID of the object
  816.         * @param string $objname Name of the object
  817.         * @param string $password Password to store for the object
  818.         * @return int|boolInteger ID of the authorisation entry, or false otherwise
  819.         */
  820.         function add_user_object($avuuid$objuuid$objname$password)
  821.         {
  822.             // Make sure our other parameters are valid
  823.             if (empty($objuuid|| empty($password)) return false;
  824.             
  825.             // Does the object already exist in the database?
  826.             $auth get_record('sloodle_user_object''objuuid'$objuuid);
  827.             $success false;
  828.             if (!$auth{
  829.                 // No - insert a new record
  830.                 $auth new stdClass();
  831.                 $auth->avuuid $avuuid;
  832.                 $auth->objuuid $objuuid;
  833.                 $auth->objname $objname;
  834.                 $auth->password $password;
  835.                 $auth->authorised 0;
  836.                 $auth->timeupdated time();
  837.                 $success insert_record('sloodle_user_object'$auth);
  838.                 
  839.             else {
  840.                 // Yes - update the existing record
  841.                 $auth->avuuid $avuuid;
  842.                 $auth->objuuid $objuuid;
  843.                 $auth->objname $objname;
  844.                 $auth->password $password;
  845.                 $auth->authorised 0;
  846.                 $auth->timeupdated time();
  847.                 
  848.                 if (update_record('sloodle_user_object'$auth)) $success $auth->id;
  849.             }
  850.             
  851.             return $success;
  852.         }
  853.         
  854.         
  855.         /**
  856.         * Gets a list of all user-centric objects authorised for the current avatar.
  857.         * @return array A numeric array of {@link SloodleUserObject} objects
  858.         */
  859.         function get_user_objects()
  860.         {
  861.             // Make sure an avatar is loaded
  862.             if (!$this->is_avatar_loaded()) return array();
  863.             // Get all objects authorised for this avatar's UUID
  864.             $recs get_records('sloodle_user_object''avuuid'$this->get_avatar_uuid());
  865.             if (!$recsreturn array();
  866.             // Construct an array of SloodleUserObject's
  867.             $output array();
  868.             foreach ($recs as $r{
  869.                 $obj new SloodleUserObject();
  870.                 $obj->id $r->id;
  871.                 $obj->avuuid $r->avuuid;
  872.                 $obj->objuuid $r->objuuid;
  873.                 $obj->objname $r->objname;
  874.                 $obj->password $r->password;
  875.                 $obj->authorized = (bool)$r->authorised// Note different spelling... oops! -PB
  876.                 $obj->timeupdated $r->timeupdated;
  877.                 
  878.                 $output[$obj;
  879.             }
  880.             
  881.             return $output;
  882.         }
  883.         
  884.         
  885.         /**
  886.         * Deletes a user-centric object by UUID.
  887.         * Note: the object must have been authorised for the current avatar.
  888.         * @param string $uuid The UUID of the object to delete
  889.         * @return void 
  890.         */
  891.         function delete_user_object($uuid)
  892.         {
  893.             if (!$this->is_avatar_loaded()) return;
  894.             delete_records('sloodle_user_object''avuuid'$this->get_avatar_uuid()'objuuid'$uuid);
  895.         }
  896.         
  897.     }
  898.     
  899.  
  900. ?>

Documentation generated on Fri, 17 Jul 2009 11:02:37 +0100 by phpDocumentor 1.4.0