course->id, 'course', 'view sloodle user', '', "{$this->course->id}");
//sloodle_add_to_log($this->course->id, 'module_viewed', 'view.php', array('_type'=>'user','id'=>$USER->id,'course'=>$this->course->id), 'user: view sloodle user');
// We need to establish some permissions here
//$this->course_context = get_context_instance(CONTEXT_COURSE, $this->courseid);
//$this->system_context = get_context_instance(CONTEXT_SYSTEM);
$this->course_context = context_course::instance($this->courseid, IGNORE_MISSING);
$this->system_context = context_system::instance();
// Make sure the user has permission to view this course (but let anybody view the site course details)
// Fetch some parameters
$this->moodleuserid = required_param('id', PARAM_RAW);
$this->deletesloodleentry = optional_param('delete', null, PARAM_INT);
$this->userconfirmed = optional_param('confirm', null, PARAM_RAW);
$this->courseid = optional_param('course', SITEID, PARAM_INT);
$this->searchstr = addslashes(optional_param('search', '', PARAM_TEXT));
$this->deleteuserobjects = optional_param('deleteuserobjects', null, PARAM_TEXT);
// If we are viewing 'all' avatar entries, then revert to the site course
if (strcasecmp($this->moodleuserid, 'all') == 0) $this->courseid = SITEID;
// Fetch our Moodle and SLOODLE course data
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->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;
// Ensure the user logs in
require_login();
if (isguestuser()) print_error(get_string('noguestaccess', 'sloodle'));
//add_to_log($this->course->id, 'course', 'view sloodle user', '', "{$this->course->id}");
sloodle_add_to_log($this->course->id, 'module_viewed', 'view.php', array('_type'=>'user','id'=>$USER->id,'course'=>$this->course->id), 'user: view sloodle user');
// We need to establish some permissions here
//$this->course_context = get_context_instance(CONTEXT_COURSE, $this->courseid);
//$this->system_context = get_context_instance(CONTEXT_SYSTEM);
$this->course_context = context_course::instance($this->courseid, IGNORE_MISSING);
$this->system_context = context_system::instance();
$this->viewingself = false;
$this->canedit = false;
// Is the user trying to view their own profile?
if ($this->moodleuserid == $USER->id) {
$this->viewingself = true;
$this->canedit = true;
}
else {
// The "all" view should be only available to admins
if ( !has_capability('moodle/site:viewparticipants', $this->system_context) ){
print_error(get_string('insufficientpermissiontoviewpage', 'sloodle'));
exit();
}
// Does the user have permission to edit other peoples' profiles in the system and/or course?
// If not, can they at least view others' profiles for the system or course?
if (has_capability('moodle/user:editprofile', $this->system_context) || has_capability('moodle/user:editprofile', $this->course_context)) {
// User can edit profiles
$this->canedit = true;
}
else if (!(has_capability('moodle/user:viewdetails', $this->system_context) || has_capability('moodle/user:viewdetails', $this->course_context))) {
// If this is the site course, then let it through anyway
if ($this->courseid != SITEID) {
print_error(get_string('insufficientpermissiontoviewpage','sloodle'));
exit();
}
}
}
}
/**
* Print the course settings page header.
*/
function sloodle_print_header()
{
}
/**
* Render the view of the module or feature.
* This MUST be overridden to provide functionality.
*/
function render()
{
global $CFG, $USER, $OUTPUT;
// Were any of the delete parameters specified in HTTP (e.g. from a form)?
if (!empty($this->deleteuserobjects) || !empty($this->deletesloodleentry) || !empty($this->userconfirmed)) {
// Convert them to session parameters
if (!empty($this->deleteuserobjects)) $_SESSION['deleteuserobjects'] = $this->deleteuserobjects;
if (!empty($this->deletesloodleentry)) $_SESSION['deletesloodleentry'] = $this->deletesloodleentry;
if (!empty($this->userconfirmed)) $_SESSION['userconfirmed'] = $this->userconfirmed;
// Construct our full URL, with GET parameters
$url = sloodle_get_web_path();
$url .= "?_type=user&id={$this->moodleuserid}";
if (!empty($this->courseid)) $url .= "&course={$this->courseid}";
if (!empty($this->searchstr)) $url .= "&search={$this->searchstr}";
if (!empty($this->start)) $url .= "&start={$this->start}";
// Reload this page without those parameters
redirect($url);
exit();
}
// Extract data from our session parameters
if (isset($_SESSION['deleteuserobjects'])) {
$this->deleteuserobjects = $_SESSION['deleteuserobjects'];
unset($_SESSION['deleteuserobjects']);
}
if (isset($_SESSION['deletesloodleentry'])) {
$this->deletesloodleentry = $_SESSION['deletesloodleentry'];
unset($_SESSION['deletesloodleentry']);
}
if (isset($_SESSION['userconfirmed'])) {
$this->userconfirmed = $_SESSION['userconfirmed'];
unset($_SESSION['userconfirmed']);
}
// Check the mode: all, search, pending, or single
$allentries = false;
$searchentries = false;
if (strcasecmp($this->moodleuserid, 'all') == 0) {
$allentries = true;
$this->moodleuserid = -1;
}
else if (strcasecmp($this->moodleuserid, 'search') == 0) {
$searchentries = true;
$this->moodleuserid = -1;
}
else {
// Make sure the Moodle user ID is an integer
$this->moodleuserid = (integer)$this->moodleuserid;
if ($this->moodleuserid <= 0) print_error(ucwords(get_string('unknownuser', 'sloodle')));
}
// Get the URL and names of the course
$courseurl = $CFG->wwwroot.'/course/view.php?_type=user&id='.$this->courseid;
$courseshortname = $this->course->shortname;
$coursefullname = $this->course->fullname;
// This value will indicate if we are currently confirming a deletion
$confirmingdeletion = false;
// These are localization strings used by the deletion confirmation form
$form_yes = get_string('Yes', 'sloodle');
$form_no = get_string('No', 'sloodle');
// Are we deleting a Sloodle entry?
$deletemsg = '';
if ($this->deletesloodleentry != NULL) {
// Determine if the user is allowed to delete this entry
$allowdelete = $this->canedit; // Just go with the editing ability for now... will maybe change this later. -PRB
// Has the deletion been confirmed?
if ($this->userconfirmed == $form_yes) {
if (sloodle_record_exists('sloodle_users', 'id', $this->deletesloodleentry)) {
// Is the user allowed to delete this?
if ($allowdelete) {
// Make sure it's a valid ID
if (is_int($this->deletesloodleentry) && $this->deletesloodleentry > 0) {
// Attempt to delete the entry
$deleteresult = sloodle_delete_records('sloodle_users', 'id', $this->deletesloodleentry);
if ($deleteresult === FALSE) {
$deletemsg = get_string('deletionfailed', 'sloodle').': '.get_string('databasequeryfailed', 'sloodle');
}
else {
$deletemsg = get_string('deletionsuccessful', 'sloodle');
}
}
else {
$deletemsg = get_string('deletionfailed', 'sloodle').': '.get_string('invalidid', 'sloodle');
}
}
else {
$deletemsg = get_string('deletionfailed', 'sloodle').': '.get_string('insufficientpermission', 'sloodle');
}
}
}
else if (is_null($this->userconfirmed)) {
// User needs to confirm deletion
$confirmingdeletion = true;
$form_url = SLOODLE_WWWROOT."/view.php";
$deletemsg .= '
'.get_string('delete','sloodle').' '.get_string('ID','sloodle').': '.$this->deletesloodleentry.'
'.get_string('confirmdelete','sloodle').'
';
$deletemsg .= '
';
}
else {
$deletemsg = get_string('deletecancelled','sloodle');
}
}
// Are we getting all entries, searching, or just viewing one?
if ($allentries) {
// All entries
$moodleuserdata = null;
// Fetch a list of all Sloodle user entries
$sloodleentries = sloodle_get_records('sloodle_users');
}
else if ($searchentries && !empty($this->searchstr)) {
// Search entries
$moodleuserdata = null;
$LIKE = sloodle_sql_ilike();
$params = array('%'.$this->searchstr.'%', '%'.$this->searchstr.'%');
global $CFG;
$fullsloodleentries = sloodle_get_records_sql_params("select * from {$CFG->prefix}sloodle_users where avname $LIKE ? OR uuid $LIKE ?", $params);
if (!$fullsloodleentries) $fullsloodleentries = array();
$sloodleentries = array();
// Eliminate entries belonging to avatars who are not in the current course
foreach ($fullsloodleentries as $fse) {
// Does the Moodle user have permission?
if (has_capability('mod/sloodle:courseparticipate', $this->course_context, $fse->userid)) {
// Copy it to our filtered list
$sloodleentries[] = $fse;
}
}
}
else {
// Attempt to fetch the Moodle user data
$moodleuserdata = sloodle_get_record('user', 'id', $this->moodleuserid);
// Fetch a list of all Sloodle user entries associated with this Moodle account
$sloodleentries = sloodle_get_records('sloodle_users', 'userid', $this->moodleuserid);
}
// Post-process the query results
if ($sloodleentries === FALSE) $sloodleentries = array();
$numsloodleentries = count($sloodleentries);
// Get the localization strings
$strsloodle = get_string('modulename', 'sloodle');
$strsloodles = get_string('modulenameplural', 'sloodle');
$strunknown = get_string('unknown', 'sloodle');
// Construct the breadcrumb links
$navigation = "";
if ($this->courseid != 1) $navigation .= "$courseshortname -> ";
$navigation .= "courseid}\">".get_string('sloodleuserprofiles', 'sloodle') . ' -> ';
if ($this->moodleuserid > 0) {
if ($moodleuserdata) $navigation .= $moodleuserdata->firstname.' '.$moodleuserdata->lastname;
else $navigation .= get_string('unknownuser','sloodle');
}
else if ($searchentries) {
$navigation .= get_string('avatarsearch', 'sloodle');
}
else {
$navigation .= get_string('allentries', 'sloodle');
}
// Display the header
sloodle_print_header(get_string('sloodleuserprofile', 'sloodle'), get_string('sloodleuserprofile','sloodle'), $navigation, "", "", true);
echo '';
// Display the deletion message if we have one
if (!empty($deletemsg)) {
echo '
';
echo $deletemsg;
echo '
';
}
echo '
';
// Are we dealing with an actual Moodle account?
if ($this->moodleuserid > 0) {
echo '
';
// Yes - do we have an account?
if ($moodleuserdata) {
// Yes - display the name and other general info
echo ''. $moodleuserdata->firstname .' '. $moodleuserdata->lastname.'';
echo " (wwwroot}/user/view.php?id={$this->moodleuserid}&course={$this->courseid}\">".get_string('moodleuserprofile','sloodle').")
";
}
else {
echo get_string('moodleusernotfound', 'sloodle').'
';
}
echo "
\n";
// Check for issues such as no entries, or multiple entries
if ($numsloodleentries == 0) {
echo '';
print_string('noentries', 'sloodle');
echo '';
// If it is the profile owner who is viewing this, then offer a link to the loginzone entry page
if ($this->moodleuserid == $USER->id) {
echo "
wwwroot}/mod/sloodle/classroom/loginzone.php?id={$this->courseid}\">";
print_string('getnewloginzoneallocation', 'sloodle');
echo '
';
}
}
else if ($numsloodleentries > 1) {
echo '
';
print_string('multipleentries', 'sloodle');
echo $OUTPUT->help_icon('help:multipleentries', 'sloodle');
echo '';
}
echo '';
}
else if ($searchentries) {
// Searching for users
echo '
'.get_string('avatarsearch','sloodle').": \"{$this->searchstr}\"";
// Check to see if there are no entries
if ($numsloodleentries == 0) {
echo '
';
print_string('noentries', 'sloodle');
echo '';
}
}
else {
// Assume we're listing all entries - explain what this means
echo '
'.get_string('allentries','sloodle').'';
echo '
'.get_string('allentries:info', 'sloodle').'
';
// Check to see if there are no entries
if ($numsloodleentries == 0) {
echo '
';
print_string('noentries', 'sloodle');
echo '';
}
}
// Construct and display a table of Sloodle entries
if ($numsloodleentries > 0) {
$sloodletable = new stdClass();
$sloodletable->head = array(get_string('avatarname', 'sloodle'),
get_string('avataruuid', 'sloodle'),
get_string('profilePic', 'sloodle'),
get_string('lastactive', 'sloodle'),
''
);
$sloodletable->align = array('left', 'left', 'left', 'left', 'left');
$sloodletable->size = array('28%', '42%', '20%','20%', '10%');
$deletestr = get_string('delete', 'sloodle');
// We want to build a list of Sloodle user objects too
$userobjects = array();
// Create a dummy Sloodle Session
$sloodle = new SloodleSession(false);
// Check if our start is past the end of our results
if ($this->start >= count($sloodleentries)) $this->start = 0;
// Go through each Sloodle entry for this user
$resultnum = 0;
$resultsdisplayed = 0;
$maxperpage = 20;
//
foreach ($sloodleentries as $su) {
// Only display this result if it is after our starting result number
if ($resultnum >= $this->start) {
// Add this user's Sloodle objects (if we're not in 'all' or search modes)
if (!$allentries && !$searchentries) {
if ($sloodle->user->load_avatar($su->uuid, '')) {
$userobjects += $sloodle->user->get_user_objects();
}
}
// Is this entry being deleted (i.e. is the user being asked to confirm its deletion)?
$deletingcurrent = ($confirmingdeletion == true && $su->id == $this->deletesloodleentry);
// Reset the line's content
$line = array();
// Fetch the avatar name and UUID
$curavname = '-';
$curuuid = '-';
if (!empty($su->avname)) $curavname = $su->avname;
if (!empty($su->uuid)) $curuuid = $su->uuid;
// If we are in all or searching mode, add a link to the Sloodle user profile
if ($allentries || $searchentries) {
//$curavname .= "
(wwwroot}/mod/sloodle/view.php?_type=user&id={$su->userid}&course={$this->courseid}\">".get_string('sloodleuserprofile','sloodle').")";
$curavname = "
wwwroot}/mod/sloodle/view.php?_type=user&id={$su->userid}&course={$this->courseid}\">{$curavname}";
}
// Add a red cross beside the avatar name if it is being deleted
if ($deletingcurrent) $curavname = '
X '.$curavname;
// Add them to the table
$line[] = $curavname;
$line[] = $curuuid;
//grab image
$endlink="";
$startlink="";
if (empty($su->profilepic)) {
// There is no stored avatar picture URL.
// If we know the grid type and if we're only displaying the avatar(s) for a single user, then try to grab the image externally.
// Note that this page is also used to display a list of all avatars on the site, so we wouldn't want to do a curl request for each one.
if (!empty($CFG->sloodle_gridtype) && !$allentries && !$searchentries) {
if ($CFG->sloodle_gridtype=="SecondLife"){
//scrape image
$profile_key_prefix = "
');
$nameEnd = strpos($body, '');
$nameStr = substr($body,$nameStart+7,$nameEnd-$nameStart-7);
$imageStart = strpos($body, $profile_img_prefix);
$imageEnd = strpos($body, $profile_img_suffix);
$imgStr = substr($body,$imageStart+30,$imageEnd-$imageStart -39);
$avimage = $imgStr;
if (!$imageStart){
$avimage= $CFG->wwwroot."/mod/sloodle/lib/media/empty.jpg";
}
else{
$su->profilepic = $avimage;
sloodle_update_record("sloodle_users",$su);
}
$startlink = '
';
$endlink="";
}
else{
//grid type is opensim
$avimage= $CFG->wwwroot."/mod/sloodle/lib/media/empty.jpg";
}
}//gridtype was not specified so just put empty.pngs for the users until admin specifies gridtype
else{
$avimage= $CFG->wwwroot."/mod/sloodle/lib/media/empty.jpg";
}
}//profile pic is already in db
else{
$avimage=$su->profilepic;
}
$line[]=$startlink.'

'.$endlink;
// Do we know when the avatar was last active
if (!empty($su->lastactive)) {
// Calculate the time difference
$difference = time() - (int)$su->lastactive;
if ($difference < 0) $difference = 0;
// Add it to the table
$line[] = sloodle_describe_approx_time($difference, true);
}
else {
$line[] = '('.$strunknown.')';
}
// Display the "delete" action
if ($this->canedit || $su->userid == $USER->id) {
if ($allentries) $deleteurl = $CFG->wwwroot."/mod/sloodle/view.php?_type=user&id=all&course={$this->courseid}&delete={$su->id}&start={$this->start}";
else if ($searchentries) $deleteurl = $CFG->wwwroot."/mod/sloodle/view.php?_type=user&id=search&course={$this->courseid}&search={$this->searchstr}&delete={$su->id}&start={$this->start}";
else $deleteurl = $CFG->wwwroot."/mod/sloodle/view.php?_type=user&id={$this->moodleuserid}&course={$this->courseid}&delete={$su->id}&start={$this->start}";
$deletecaption = get_string('clicktodeleteentry','sloodle');
$line[] = "
$deletestr";
}
else {
$line[] = '
'.get_string('delete','sloodle').'';
}
// 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=user&course={$this->courseid}";
if ($searchentries) $basicurl .= "&id=search&search={$this->searchstr}";
else if ($allentries) $basicurl .= "&id=all";
else $basicurl .= "&id={$this->moodleuserid}";
// 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($sloodleentries)) $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);
}
// Display a link allowing admin users to add an avatar if this is a single avatar page
if (!$allentries && !$searchentries)
{
if ( has_capability('moodle/site:viewparticipants', $this->system_context) )
{
echo "
";
echo "wwwroot}/mod/sloodle/view.php?_type=addavatar&user={$this->moodleuserid}&course={$this->courseid}\" title=\"".get_string('addavatarhere','sloodle')."\">";
echo "
wwwroot}/mod/sloodle/lib/media/add.png\" alt=\"[plus icon]\" /> ";
print_string('addavatar', 'sloodle');
echo "
\n";
}
}
// Construct and display a table of Sloodle entries
if ($numsloodleentries > 0) {
// Display a list of user-authorised objects
if (!$allentries && !$searchentries) {
echo '
'.get_string('userobjects','sloodle');
echo $OUTPUT->help_icon('userobjects','sloodle');
echo "
\n";
// Have we been asked to delete the user objects?
if ($this->deleteuserobjects == 'true') {
// Yes - display a confirmation form
echo '
'.get_string('confirmdeleteuserobjects','sloodle').'
';
echo '
';
echo '';
echo ' | ';
echo '';
echo ' |
';
}
else if ($this->deleteuserobjects == 'confirm') {
// Delete each one
$numdeleted = 0;
foreach ($userobjects as $obj) {
sloodle_delete_records('sloodle_user_object', 'id', $obj->id);
$numdeleted++;
}
$userobjects = array();
echo get_string('numdeleted','sloodle').': '.$numdeleted.'
';
}
// Do we have any objects to display?
if (count($userobjects) > 0) {
// Yes - prepare the table
$sloodletable = new stdClass();
$sloodletable->head = array(get_string('ID', 'sloodle'),
get_string('avataruuid', 'sloodle'),
get_string('uuid', 'sloodle'),
get_string('name', 'sloodle'),
get_string('isauthorized', 'sloodle'),
get_string('lastused', 'sloodle')
);
$sloodletable->align = array('center', 'left', 'left', 'left', 'center', 'left');
//$sloodletable->size = array('5%', '5%', '27%', '35%', '20%', '8%');
// Store the current timestamp for consistency
$curtime = time();
// Go through each object
foreach ($userobjects as $obj) {
$line = array();
$line[] = $obj->id;
$line[] = $obj->avuuid;
$line[] = $obj->objuuid;
$line[] = $obj->objname;
if ($obj->authorized) $line[] = ucwords(get_string('yes'));
else $line[] = ucwords(get_string('no'));
$lastused = (int)$obj->timeupdated;
if ($lastused > 0) $line[] = sloodle_describe_approx_time($curtime - $lastused, true);
else $line[] = '('.get_string('unknown','sloodle').')';
$sloodletable->data[] = $line;
}
// Display the table
sloodle_print_table($sloodletable);
// Display a button to delete all the Sloodle objects
if (empty($deleteuserobjects)) {
echo '
';
}
}
else {
// No user objects
echo '
';
print_string('noentries', 'sloodle');
echo '';
}
}
}
echo '
';
}
/**
* Print the page footer.
*/
function sloodle_print_footer()
{
sloodle_print_footer($this->course);
}
}