Source for file view_user.php

Documentation is available at view_user.php

  1. <?php
  2.     
  3.     /**
  4.     * Sloodle user profile page.
  5.     *
  6.     * Shows all Sloodle avatar entries associated with a particular user's Moodle account.
  7.     *
  8.     * @package sloodle
  9.     * @copyright Copyright (c) 2008 Sloodle (various contributors)
  10.     * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  11.     *
  12.     * @contributor Peter R. Bloomfield
  13.     *
  14.     */
  15.     
  16.     //
  17.     // This script will display Sloodle-related user information about a particular Moodle user.
  18.     // Notably, this will include avatar data (i.e. name and UUID).
  19.     // In the case of a user viewing their own profile, or an admin viewing anybody's profile,
  20.     //  there will be the ability to unregister an avatar from the Moodle account, and to
  21.     //  delete allocated LoginZone positions.
  22.     //
  23.     
  24.     //
  25.     // This script requires the following paramter:
  26.     //  id = the ID number of a Moodle user, 'all' for all Sloodle entries, 'search' to search avatars by name/uuid, or 'pending' to list pending (unlinked) avatars
  27.     //
  28.     // The following parameter should be specified when a Sloodle entry is to be deleted:
  29.     //  delete = ID number the Sloodle entry to be deleted
  30.     // The following parameter should be used when the user confirms or rejects deletion
  31.     //  confirm = "Yes" (according to a localization table) if confirmed, unspecified if not yet confirmed, or anything else if cancelled.
  32.     //
  33.     // Optionally, the following parameter can also be specified. It defaults to the site if unspecified:
  34.     //  course = ID of a Moodle course
  35.     //
  36.     // In order to search for avatars, the following parameter is required:
  37.     //  search = a term to search for (avatar name/uuid)
  38.  
  39.  
  40.     /** Sloodle/Moodle configuration. */
  41.     require_once('../sl_config.php');
  42.     /** General Sloodle functionality. */
  43.     require_once(SLOODLE_LIBROOT.'/general.php');
  44.     
  45.     // Enforce login
  46.     require_login();
  47.     // Refuse guest access
  48.     if (isguestuser()) {
  49.         error(get_string('noguestaccess''sloodle'));
  50.         exit;
  51.     }
  52.     
  53.     // Fetch the parameters
  54.     $moodleuserid required_param('id'PARAM_RAW);
  55.     $deletesloodleentry optional_param('delete'NULLPARAM_INT);
  56.     $userconfirmed optional_param('confirm'NULLPARAM_RAW);
  57.     $courseid optional_param('course'1PARAM_INT);
  58.     $searchstr addslashes(optional_param('search'''PARAM_TEXT));
  59.     
  60.     // Check the mode: all, search, pending, or single
  61.     $allentries false;
  62.     $searchentries false;
  63.     if (strcasecmp($moodleuserid'all'== 0{
  64.         $allentries true;
  65.         $moodleuserid = -1;
  66.     else if (strcasecmp($moodleuserid'search'== 0{
  67.         $searchentries true;
  68.         $moodleuserid = -1;
  69.     else {
  70.         // Make sure the Moodle user ID is an integer
  71.         $moodleuserid = (integer)$moodleuserid;
  72.         if ($moodleuserid <= 0error(ucwords(get_string('unknownuser''sloodle')));
  73.     }
  74.     
  75.     
  76.     // Get the name of the course
  77.     $courserecord get_record('course''id'$courseid);
  78.     if (!$courserecorderror(get_string('invalidcourseid','sloodle'));
  79.     $courseurl $CFG->wwwroot.'/course/view.php?id='.$courseid;
  80.     $courseshortname $courserecord->shortname;
  81.     $coursefullname $courserecord->fullname;
  82.     
  83.     // We need to establish some permissions here
  84.     $course_context get_context_instance(CONTEXT_COURSE$courseid);
  85.     $system_context get_context_instance(CONTEXT_SYSTEM);
  86.     $viewingself false;
  87.     $canedit false;
  88.     // Is the user trying to view their own profile?
  89.     if ($moodleuserid == $USER->id{
  90.         $viewingself true;
  91.         $canedit true;
  92.     else {
  93.         // Does the user have permission to edit other peoples' profiles in the system and/or course?
  94.         // If not, can they at least view others' profiles?
  95.         if (has_capability('moodle/user:editprofile'$system_context|| has_capability('moodle/user:editprofile'$course_context)) {
  96.             // User can edit profiles
  97.             $canedit true;
  98.         else if (!(has_capability('moodle/user:viewdetails'$system_context|| has_capability('moodle/user:viewdetails'$course_context))) {
  99.             // Cannot view profiles
  100.             error(get_string('insufficientpermissiontoviewpage','sloodle'));
  101.             exit();
  102.         }
  103.     }
  104.     
  105.     // This value will indicate if we are currently confirming a deletion
  106.     $confirmingdeletion false;
  107.     
  108.     // These are localization strings used by the deletion confirmation form
  109.     $form_yes get_string('Yes''sloodle');
  110.     $form_no get_string('No''sloodle');
  111.     
  112.     
  113.     // Are we deleting a Sloodle entry?
  114.     $deletemsg '';    
  115.     if ($deletesloodleentry != NULL{
  116.         // Determine if the user is allowed to delete this entry
  117.         $allowdelete $canedit// Just go with the editing ability for now... will maybe change this later. -PRB
  118.         
  119.         // Has the deletion been confirmed?
  120.         if ($userconfirmed == $form_yes{
  121.             if (record_exists('sloodle_users''id'$deletesloodleentry)) {
  122.                 // Is the user allowed to delete this?
  123.                 if ($allowdelete{
  124.                     // Make sure it's a valid ID
  125.                     if (is_int($deletesloodleentry&& $deletesloodleentry 0{
  126.                         // Attempt to delete the entry
  127.                         $deleteresult delete_records('sloodle_users''id'$deletesloodleentry);
  128.                         if ($deleteresult === FALSE{
  129.                             $deletemsg get_string('deletionfailed''sloodle').': '.get_string('databasequeryfailed''sloodle');
  130.                         else {
  131.                             $deletemsg get_string('deletionsuccessful''sloodle');
  132.                         }
  133.                     else {
  134.                         $deletemsg get_string('deletionfailed''sloodle').': '.get_string('invalidid''sloodle');
  135.                     }
  136.                 else {
  137.                     $deletemsg get_string('deletionfailed''sloodle').': '.get_string('insufficientpermission''sloodle');
  138.                 }
  139.             }
  140.         else if (is_null($userconfirmed)) {
  141.             // User needs to confirm deletion
  142.             $confirmingdeletion true;
  143.             
  144.             $form_url SLOODLE_WWWROOT."/view/view_user.php";
  145.             
  146.             $deletemsg .= '<h3>'.get_string('delete','sloodle').' '.get_string('ID','sloodle').': '.$deletesloodleentry.'<br/>'.get_string('confirmdelete','sloodle').'</h3>';
  147.             $deletemsg .= '<form action="'.$form_url.'" method="get">';
  148.             
  149.             if ($allentries$deletemsg .= '<input type="hidden" name="id" value="all" />';
  150.             else $deletemsg .= '<input type="hidden" name="id" value="'.$moodleuserid.'" />';
  151.             
  152.             if (!is_null($courseid)) $deletemsg .= '<input type="hidden" name="course" value="'.$courseid.'" />';
  153.             $deletemsg .= '<input type="hidden" name="delete" value="'.$deletesloodleentry.'" />';
  154.             $deletemsg .= '<input style="color:green;" type="submit" value="'.$form_yes.'" name="confirm" />&nbsp;&nbsp;';
  155.             $deletemsg .= '<input style="color:red;" type="submit" value="'.$form_no.'" name="confirm" />';
  156.             $deletemsg .= '</form><br/>';
  157.             
  158.         else {
  159.             $deletemsg get_string('deletecancelled','sloodle');
  160.         }
  161.     }
  162.     
  163.     // Are we getting all entries, searching, or just viewing one?
  164.     if ($allentries{
  165.         // All entries
  166.         $moodleuserdata null;
  167.         // Fetch a list of all Sloodle user entries
  168.         $sloodleentries get_records('sloodle_users');
  169.     else if ($searchentries && !empty($searchstr)) {
  170.         // Search entries
  171.         $moodleuserdata null;
  172.         $LIKE sql_ilike();
  173.         $sloodleentries get_records_select('sloodle_users'"`avname` $LIKE '%$searchstr%' OR `uuid` $LIKE '%$searchstr%'"'`avname`');
  174.     else {
  175.         // Attempt to fetch the Moodle user data
  176.         $moodleuserdata get_record('user''id'$moodleuserid);
  177.         // Fetch a list of all Sloodle user entries associated with this Moodle account
  178.         $sloodleentries get_records('sloodle_users''userid'$moodleuserid);
  179.     }
  180.     // Post-process the query results
  181.     if ($sloodleentries === FALSE$sloodleentries array();
  182.     $numsloodleentries count($sloodleentries);
  183.     
  184.     
  185.     // Get the localization strings
  186.     $strsloodle get_string('modulename''sloodle');
  187.     $strsloodles get_string('modulenameplural''sloodle');
  188.     $strunknown get_string('unknown''sloodle');
  189.     $strminute get_string('minute''sloodle');
  190.     $strminutes get_string('minutes''sloodle');
  191.     $strhour get_string('hour''sloodle');
  192.     $strhours get_string('hours''sloodle');
  193.     $strday get_string('day''sloodle');
  194.     $strdays get_string('days''sloodle');
  195.     $strweek get_string('week''sloodle');
  196.     $strweeks get_string('weeks''sloodle');
  197.  
  198.     
  199.     // Construct the breadcrumb links
  200.     $navigation "";
  201.     if ($courseid != 1$navigation .= "<a href=\"$courseurl\">$courseshortname</a> -> ";
  202.     $navigation .= "<a href=\"".SLOODLE_WWWROOT."/view/view_users.php?course=$courseid\">".get_string('sloodleuserprofiles''sloodle''</a> -> ';
  203.     if ($moodleuserid 0{
  204.         if ($moodleuserdata$navigation .= $moodleuserdata->firstname.' '.$moodleuserdata->lastname;
  205.         else $navigation .= get_string('unknownuser','sloodle');
  206.     else {
  207.         $navigation.= get_string('allentries''sloodle');
  208.     }
  209.     
  210.     // Display the header
  211.     print_header(get_string('sloodleuserprofile''sloodle')get_string('sloodleuserprofile','sloodle')$navigation""""true);
  212.     
  213.     echo '<div style="text-align:center;padding-left:8px;padding-right:8px;">';
  214.     // Display the deletion message if we have one
  215.     if (!empty($deletemsg)) {
  216.         echo '<div style="text-align:center; padding:3px; border:solid 1px #aaaaaa; background-color:#dfdfdf; font-weight:bold; color:#dd0000;">';
  217.         echo $deletemsg;
  218.         echo '</div>';
  219.     }
  220.     echo '<br/>';
  221.     
  222.     // Are we dealing with an actual Moodle account
  223.     if ($moodleuserid 0{
  224.         echo '<p>';
  225.         // Yes - do we have an account?
  226.         if ($moodleuserdata{
  227.             // Yes - display the name and other general info
  228.             echo '<span style="font-size:18pt; font-weight:bold;">'$moodleuserdata->firstname .' '$moodleuserdata->lastname.'</span>';
  229.             echo " <span style=\"font-size:10pt; color:#444444; font-style:italic;\">(<a href=\"{$CFG->wwwroot}/user/view.php?id=$moodleuserid&amp;course=$courseid\">".get_string('moodleuserprofile','sloodle')."</a>)</span><br/>";
  230.         else {
  231.             echo get_string('moodleusernotfound''sloodle').'<br/>';
  232.         }        
  233.         echo "<br/><br/>\n";
  234.         
  235.         // Check for issues such as no entries, or multiple entries
  236.         if ($numsloodleentries == 0{
  237.             echo '<span style="color:red; font-weight:bold;">';
  238.             print_string('noentries''sloodle');
  239.             echo '</span>';
  240.             // If it is the profile owner who is viewing this, then offer a link to the loginzone entry page
  241.             if ($moodleuserid == $USER->id{
  242.                 echo "<br/><br/><p style=\"padding:8px; border:solid 1px #555555;\"><a href=\"{$CFG->wwwroot}/mod/sloodle/classroom/loginzone.php?id=$courseid\">";
  243.                 print_string('getnewloginzoneallocation''sloodle');
  244.                 echo '</a></p>';
  245.             }            
  246.             
  247.         else if ($numsloodleentries 1{
  248.             echo '<span style="color:red; font-weight:bold; border:solid 2px #990000; padding:4px; background-color:white;">';
  249.             print_string('multipleentries''sloodle');
  250.             helpbutton('multiple_entries'get_string('help:multipleentries''sloodle')'sloodle'truefalse);
  251.             echo '</span>';
  252.         }
  253.         echo '</p>';
  254.         
  255.     else if ($searchentries{
  256.         // Searching for users
  257.         echo '<span style="font-size:18pt; font-weight:bold; ">'.get_string('avatarsearch','sloodle').": \"$searchstr\"</span><br/><br/>";
  258.         // Check to see if there are no entries
  259.         if ($numsloodleentries == 0{
  260.             echo '<span style="color:red; font-weight:bold;">';
  261.             print_string('noentries''sloodle');
  262.             echo '</span>';
  263.         }
  264.         
  265.     else {
  266.         // Assume we're listing all entries - explain what this means
  267.         echo '<span style="font-size:18pt; font-weight:bold; ">'.get_string('allentries','sloodle').'</span><br/>';
  268.         echo '<center><p style="width:550px; text-align:left;">'.get_string('allentries:info''sloodle').'</p></center>';
  269.         
  270.         // Check to see if there are no entries
  271.         if ($numsloodleentries == 0{
  272.             echo '<span style="color:red; font-weight:bold;">';
  273.             print_string('noentries''sloodle');
  274.             echo '</span>';
  275.         }
  276.     }
  277.     
  278.     // Construct and display a table of Sloodle entries
  279.     if ($numsloodleentries 0{
  280.         $sloodletable new stdClass();
  281.         $sloodletable->head array(    get_string('ID''sloodle'),
  282.                                         get_string('linkedtomoodleusernum''sloodle'),
  283.                                         get_string('avatarname''sloodle'),
  284.                                         get_string('avataruuid''sloodle'),
  285.                                         get_string('lastactive''sloodle'),
  286.                                         ''
  287.                                     );
  288.         $sloodletable->align array('center''center''left''left''left''left');
  289.         $sloodletable->size array('5%''5%''27%''35%''20%''8%');
  290.         
  291.         $deletestr get_string('delete''sloodle');
  292.                 
  293.         // Go through each Sloodle entry for this user
  294.         foreach ($sloodleentries as $su{
  295.             // Is this entry being deleted (i.e. is the user being asked to confirm its deletion)?
  296.             $deletingcurrent ($confirmingdeletion == true && $su->id == $deletesloodleentry);
  297.             
  298.             // Reset the line's content
  299.             $line array();
  300.             
  301.             // Add the ID to the line
  302.             if ($deletingcurrent$line['<span style="color:red; font-weight:bold;">'.$su->id.'</span>';
  303.             else $line[$su->id;
  304.             
  305.             // Add the Moodle user ID and link
  306.             $line["<a href=\"{$CFG->wwwroot}/user/view.php?id={$su->userid}&amp;course=$courseid\">{$su->userid}</a>";            
  307.         
  308.             // Fetch the avatar name and UUID
  309.             $curavname '-';
  310.             $curuuid '-';
  311.             if (!empty($su->avname)) $curavname $su->avname;
  312.             if (!empty($su->uuid)) $curuuid $su->uuid;
  313.             
  314.             // If we are in all or searching mode, add a link to the Sloodle user profile
  315.             if ($allentries || $searchentries{
  316.                 $curavname .= " <span style=\"font-size:10pt; color:#444444; font-style:italic;\">(<a href=\"{$CFG->wwwroot}/mod/sloodle/view/view_user.php?id={$su->userid}&amp;course=$courseid\">".get_string('sloodleuserprofile','sloodle')."</a>)</span>";
  317.             }
  318.             
  319.             // Add them to the table
  320.             $line[$curavname;
  321.             $line[$curuuid
  322.             
  323.             // Do we know when the avatar was last active
  324.             if (!empty($su->lastactive)) {
  325.                 // Calculate the time difference
  326.                 $difference time(- (int)$su->lastactive;
  327.                 
  328.                 // Assume first that the user is online now
  329.                 // (Updates can easily take a minute or so... ignore anything less than 1 minute)
  330.                 $duration '';
  331.                 if ($difference 60{
  332.                     $duration ucwords(get_string('now''sloodle'));
  333.                 else if ($difference 119// < 2 minutes
  334.                     $duration "$strminute";
  335.                 else if ($difference 3600// < 1 hour
  336.                     $duration = (string)(int)($difference 60)." $strminutes";
  337.                 else if ($difference 7200// < 2 hours
  338.                     $duration "$strhour";
  339.                 else if ($difference 86400// < 1 day
  340.                     $duration = (string)(int)($difference 3600)." $strhours";
  341.                 else if ($difference 172800// < 2 days
  342.                     $duration "$strday";
  343.                 else if ($difference 604800// < 1 week
  344.                     $duration = (string)(int)($difference 86400)." $strdays";
  345.                 else if ($difference 1209600// < 2 weeks
  346.                     $duration "$strweek";
  347.                 else {
  348.                     $duration = (string)(int)($difference 604800)." $strweeks";
  349.                 }
  350.                 
  351.                 // Add it to the table
  352.                 $line[$duration;
  353.             else {
  354.                 $line['('.$strunknown.')';
  355.             }
  356.             
  357.             // Display the "delete" action
  358.             if ($canedit{
  359.                 if ($allentries$deleteurl $CFG->wwwroot."/mod/sloodle/view/view_user.php?id=all&amp;course=$courseid&amp;delete={$su->id}";
  360.                 else $deleteurl $CFG->wwwroot."/mod/sloodle/view/view_user.php?id=$moodleuserid&amp;course=$courseid&amp;delete={$su->id}";
  361.                 $deletecaption get_string('clicktodeleteentry','sloodle');
  362.                 $line["<a href=\"$deleteurl\" title=\"$deletecaption\">$deletestr</a>";
  363.                 
  364.             else {
  365.                 $line['<span style="color:#777777;" title="'.get_string('nodeletepermission','sloodle').'">'.get_string('delete','sloodle').'</span>';
  366.             }
  367.             
  368.             // Add the line to the table
  369.             $sloodletable->data[$line;
  370.         }
  371.         
  372.         // Display the table
  373.         print_table($sloodletable);
  374.     }
  375.     echo '</div>';
  376.     
  377.     // Display the footer
  378.     print_footer();
  379. ?>

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