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

Documentation generated on Mon, 16 Jun 2008 15:57:04 +0100 by phpDocumentor 1.4.0