courseid = optional_param('course', SITEID, PARAM_INT);
        if (!$this->course = sloodle_get_record('course', 'id', $this->courseid)) print_error('Could not find course.');
        $this->sloodle_course = new SloodleCourse();
        if (!$this->sloodle_course->load($this->course)) print_error(get_string('failedcourseload', 'sloodle'));
        //$this->course_context = get_context_instance(CONTEXT_COURSE, $this->course->id);
        $this->course_context = context_course::instance($this->course->id, IGNORE_MISSING);
        
        // Construct the course URL, and fetch the names
        $this->courseurl = $CFG->wwwroot.'/course/view.php?id='.$this->courseid;
        $this->courseshortname = $this->course->shortname;
        $this->coursefullname = $this->course->fullname;
        
        // Fetch the other parameters
        $this->searchstr = addslashes(optional_param('search', '', PARAM_TEXT));
        $this->sloodleonly = optional_param('sloodleonly', false, PARAM_BOOL);
        $this->start = optional_param('start', 0, PARAM_INT);
        if ($this->start < 0) $this->start = 0;
        // Moodle 2 rendering functions like to know the course.
        // They get upset if you try to pass a course into sloodle_print_footer() that isn't what they were expecting.
        if ($this->course) {
            global $PAGE;
            if (isset($PAGE) && method_exists($PAGE, 'set_course')) {
                $PAGE->set_course($this->course);
            }
        }
    }
    /**
    * Check that the user is logged-in and has permission to alter course settings.
    */
    function check_permission()
    {
        global $CFG, $USER;
    
        if ($this->courseid != SITEID) require_capability('mod/sloodle:courseparticipate', $this->course_context);
    }
    /**
    * Print the course settings page header.
    */
    function sloodle_print_header()
    {
        $navigation = '';
        if ($this->courseid != SITEID) $navigation .= "courseurl}\">{$this->courseshortname} -> ";
        $navigation .= get_string('sloodleuserprofiles', 'sloodle');
        sloodle_print_header(get_string('sloodleuserprofiles', 'sloodle'), get_string('sloodleuserprofiles', 'sloodle'), $navigation, "", "", false);
    }
    /**
    * Render the view of the module or feature.
    * This MUST be overridden to provide functionality.
    */
    function render()
    {
        global $CFG, $USER;
        
        // Get the localization strings
        $strsloodle = get_string('modulename', 'sloodle');
        $strsloodles = get_string('modulenameplural', 'sloodle');
        
        // Open the main body section
        echo '
';
        
        //------------------------------------------------------
        sloodle_print_box_start('generalbox boxwidthwide boxaligncenter');
        echo '
';
        // // SEARCH FORMS // //
        echo '';
        /*
        Ed: Dropping this for now - breaking on Moodle 2, and it's weird to have a full site search under your course.
        // COURSE SELECT FORM //
        echo '| ';
        
        echo "';
        echo '';
        */
        
        // USER SEARCH FORM //
        echo ' | ';    
        echo "';
        echo '';
        
        // AVATAR SEARCH //
        echo ' | ';
        //echo ''.get_string('specialpages','sloodle').' ';
        // // - END FORMS - // //
        echo '';        
        echo "';
        echo '
 | 
';
        echo '
';
        // Provide some admin-only links
        //$system_context = get_context_instance(CONTEXT_SYSTEM);
        $system_context = context_system::instance();
        if ( has_capability('moodle/site:viewparticipants', $system_context) ) {
            echo '
';
            /*
            // Might be useful to add a "show admins" link, since they are not normally 'enrolled' in any course
            echo '  |  ';*/
            // Link to view all avatars on the site
            echo "wwwroot}/mod/sloodle/view.php?_type=user&id=all\" title=\"".get_string('viewall','sloodle')."\">";
            print_string('viewall','sloodle');
            echo '';
            echo '
';
        }
        
        sloodle_print_box_end();
        //------------------------------------------------------
        
        // Are we searching for users?
        if ($this->searchstr != NULL) {
            // Display the search term
            echo '
'.get_string('usersearch','sloodle').': '.$this->searchstr.'';
            // Search the list of users
            //$fulluserlist = get_users(true, $this->searchstr);
            //$fulluserlist = get_users_by_capability('mod/sloodle:courseparticipate', $this->course_context);
            $fulluserlist = get_users_by_capability($this->course_context, 'mod/sloodle:courseparticipate', 'u.id, u.firstname, u.lastname', 'u.firstname, u.lastname');
            if (!$fulluserlist) $fulluserlist = array();
            $userlist = array();
            // Filter it down to members of the course
            foreach ($fulluserlist as $ful) {
                if ( ($ful->firstname == $this->searchstr) || ($ful->lastname == $this->searchstr) ) {
                    $userlist[] = $ful;
                } 
            }
        } 
        else {
            // Getting all users in a course
            // Display the name of the course
            echo '
'.s($this->coursefullname).'';
            // Obtain a list of all Moodle users enrolled in the specified course
            //$userlist = get_course_users($this->courseid, 'lastname, firstname', '', 'u.id, firstname, lastname');
            $userlist = get_users_by_capability($this->course_context, 'mod/sloodle:courseparticipate', 'u.id, u.firstname, u.lastname', 'u.firstname, u.lastname');
        }
        
        // Construct and display a table of Sloodle entries
        if ($userlist) {
            $sloodletable = new stdClass();
            $sloodletable->head  = array(get_string('user', 'sloodle'), get_string('avatar', 'sloodle'));
            $sloodletable->align = array('left', 'left');
            $sloodletable->size  = array('50%', '50%');
            
            // Check if our start is past the end of our results
            if ($this->start >= count($userlist)) $this->start = 0;
            
            // Go through each entry to add it to the table
            $resultnum = 0;
            $resultsdisplayed = 0;
            $maxperpage = 20;
            foreach ($userlist as $u) {
                // Only display this result if it is after our starting result number
                if ($resultnum >= $this->start) {
                    // Reset the line's content
                    $line = array();
                    
                    // Construct URLs to this user's Moodle and SLOODLE profile pages
                    $url_moodleprofile = $CFG->wwwroot."/user/view.php?id={$u->id}&course={$this->courseid}";
                    $url_sloodleprofile = SLOODLE_WWWROOT."/view.php?_type=user&id={$u->id}&course={$this->courseid}";
                    // Add the Moodle name
                    $line[] = "
{$u->firstname} {$u->lastname}";
                    
                    // Get the Sloodle data for this Moodle user
                    $sloodledata = sloodle_get_records('sloodle_users', 'userid', $u->id);
                    if ($sloodledata) {
                        // Display all avatars names, if available
                        $avnames = '';
                        $firstentry = true;
                        foreach ($sloodledata as $sd) {
                            // If this entry is empty, then skip it
                            if (empty($sd->avname) || ctype_space($sd->avname)) continue;
                            // Comma separated entries
                            if ($firstentry) $firstentry = false;
                            else $avnames .= ', ';
                            // Add the current name
                            $avnames .= $sd->avname;
                        }
                        // Add the avatar name(s) to the line
                        $line[] = "
{$avnames}";
                        
                    }
                    else {
                        // The query failed - if we are showing only Sloodle-enabled users, then skip the rest
                        if ($this->sloodleonly) continue;
                        $line[] = '-';
                    }
                    
                    // Add the line to the table
                    $sloodletable->data[] = $line;
                    $resultsdisplayed++;
                }
                
                // Have we displayed the maximum number of results for this page?
                $resultnum++;
                if ($resultsdisplayed >= $maxperpage) break;
            }
            
            // Construct our basic URL to this page
            $basicurl = SLOODLE_WWWROOT."/view.php?_type=users&course={$this->courseid}";
            if ($this->sloodleonly) $basicurl .= "&sloodleonly=true";
            if (!empty($this->searchstr)) $basicurl .= "&search={$this->searchstr}";
            
            // Construct the next/previous links
            $previousstart = max(0, $this->start - $maxperpage);
            $nextstart = $this->start + $maxperpage;
            $prevlink = null;
            $nextlink = null;
            if ($previousstart != $this->start) $prevlink = "
<<  ";            
            if ($nextstart < count($userlist)) $nextlink = "
>>";
            
            // Display the next/previous links, if we have at least one
            if (!empty($prevlink) || !empty($nextlink)) {
                echo '
';
                if (!empty($prevlink)) echo $prevlink;
                else echo '<<  ';
                if (!empty($nextlink)) echo $nextlink;
                else echo '>>  ';
                echo '
';
            }
            
            // Display the table
            sloodle_print_table($sloodletable);
        }
        else {
            // Failed to query for list of users
            echo '
';
            print_string('nouserdata','sloodle');
            echo '
';
        }
        echo '