Source for file users.php

Documentation is available at users.php

  1. <?php
  2. /**
  3. * Defines a base class for viewing information about SLOODLE users (avatars).
  4. * Class is inherited from the base view class.
  5. *
  6. @package sloodle
  7. @copyright Copyright (c) 2009 Sloodle (various contributors)
  8. @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  9. *
  10. @contributor Peter R. Bloomfield
  11. */
  12.  
  13. /** The base view class */
  14. require_once(SLOODLE_DIRROOT.'/view/base/base_view.php');
  15. /** SLOODLE course data structure */
  16. require_once(SLOODLE_LIBROOT.'/course.php');
  17. /** Sloodle Session code. */
  18. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  19. /** General Sloodle functionality. */
  20. require_once(SLOODLE_LIBROOT.'/general.php');
  21.  
  22. /**
  23. * Class for rendering a view of SLOODLE users.
  24. @package sloodle
  25. */
  26. {
  27.     /**
  28.     * Integer ID of the course which is being accessed.
  29.     * @var integer 
  30.     * @access private
  31.     */
  32.     var $courseid = 0;
  33.     
  34.     /**
  35.     * The VLE course object, retrieved directly from database.
  36.     * @var object 
  37.     * @access private
  38.     */
  39.     var $course = 0;
  40.  
  41.     /**
  42.     * SLOODLE course object, retrieved directly from database.
  43.     * @var object 
  44.     * @access private
  45.     */
  46.     var $sloodle_course = null;
  47.     
  48.     /**
  49.     * The search string for users where appropriate.
  50.     * @var string 
  51.     * @access private
  52.     */
  53.     var $searchstr = '';
  54.  
  55.     /**
  56.     * Moodle permissions context for the current course.
  57.     * @var object 
  58.     * @access private
  59.     */
  60.     var $course_context = null;
  61.     
  62.     /**
  63.     * Moodle permissions context for the system.
  64.     * @var object 
  65.     * @access private
  66.     */
  67.     var $system_context = null;
  68.     
  69.     /**
  70.     * Indicates if we should only show users who have avatars. Fetched from request parameters where given.
  71.     * @var bool 
  72.     * @access private
  73.     */
  74.     var $sloodleonly = true;
  75.     
  76.     /**
  77.     * URL for accessing the current course.
  78.     * @var string 
  79.     * @access private
  80.     */
  81.     var $courseurl = '';
  82.     
  83.     /**
  84.     * Short name of the current course.
  85.     * @var string 
  86.     * @access private
  87.     */
  88.     var $courseshortname = '';
  89.     
  90.     /**
  91.     * Full name of the current course.
  92.     * @var string 
  93.     * @access private
  94.     */
  95.     var $coursefullname = '';
  96.     
  97.     /**
  98.     * The result number to start displaying from
  99.     * @var integer 
  100.     * @access private
  101.     */
  102.     var $start = 0;
  103.  
  104.  
  105.     /**
  106.     * Constructor.
  107.     */
  108.     function sloodle_view_users()
  109.     {
  110.     }
  111.  
  112.     /**
  113.     * Check and process the request parameters.
  114.     */
  115.     function process_request()
  116.     {
  117.         global $CFG$USER;
  118.     
  119.         // Fetch our Moodle and SLOODLE course data
  120.         $this->courseid = optional_param('course'SITEIDPARAM_INT);
  121.         if (!$this->course = get_record('course''id'$this->courseid)) error('Could not find course.');
  122.         $this->sloodle_course = new SloodleCourse();
  123.         if (!$this->sloodle_course->load($this->course)) error(get_string('failedcourseload''sloodle'));
  124.         
  125.         // Construct the course URL, and fetch the names
  126.         $this->courseurl = $CFG->wwwroot.'/course/view.php?id='.$this->courseid;
  127.         $this->courseshortname = $this->course->shortname;
  128.         $this->coursefullname = $this->course->fullname;
  129.         
  130.         // Fetch the other parameters
  131.         $this->searchstr = addslashes(optional_param('search'''PARAM_TEXT));
  132.         $this->sloodleonly = optional_param('sloodleonly'falsePARAM_BOOL);
  133.         $this->start = optional_param('start'0PARAM_INT);
  134.         if ($this->start < 0$this->start = 0;
  135.     }
  136.  
  137.     /**
  138.     * Check that the user is logged-in and has permission to alter course settings.
  139.     */
  140.     function check_permission()
  141.     {
  142.         global $CFG$USER;
  143.     
  144.         // Ensure the user logs in
  145.         require_login();
  146.         if (isguestuser()) error(get_string('noguestaccess''sloodle'));
  147.         //add_to_log($this->course->id, 'course', 'view sloodle user', '', "{$this->course->id}");
  148.  
  149.         
  150.         // We need to establish some permissions here
  151.         $this->course_context = get_context_instance(CONTEXT_COURSE$this->courseid);
  152.         $this->system_context = get_context_instance(CONTEXT_SYSTEM);
  153.         // Make sure the user has permission to view this course (but let anybody view the site course details)
  154.         if ($this->courseid != SITEIDrequire_capability('moodle/course:view'$this->course_context);
  155.     }
  156.  
  157.     /**
  158.     * Print the course settings page header.
  159.     */
  160.     function print_header()
  161.     {
  162.         $navigation '';
  163.         if ($this->courseid != SITEID$navigation .= "<a href=\"{$this->courseurl}\">{$this->courseshortname}</a> -> ";
  164.         $navigation .= get_string('sloodleuserprofiles', 'sloodle');
  165.         print_header(get_string('sloodleuserprofiles', 'sloodle'), get_string('sloodleuserprofiles', 'sloodle'), $navigation, "", "", false);
  166.     }
  167.  
  168.     /**
  169.     * Render the view of the module or feature.
  170.     * This MUST be overridden to provide functionality.
  171.     */
  172.     function render()
  173.     {
  174.         global $CFG, $USER;
  175.         
  176.         // Get the localization strings
  177.         $strsloodle = get_string('modulename', 'sloodle');
  178.         $strsloodles = get_string('modulenameplural', 'sloodle');
  179.         
  180.         // Open the main body section
  181.         echo '<div style="text-align:center;padding-left:8px;padding-right:8px;">';
  182.         
  183.         
  184. //------------------------------------------------------
  185.         
  186.         print_box_start('generalbox boxwidthwide boxaligncenter');
  187.         echo '<table style="text-align:left; vertical-align:top; margin-left:auto; margin-right:auto;">';
  188. // // SEARCH FORMS // //
  189.         echo '<tr>';
  190.  
  191.         // COURSE SELECT FORM //
  192.         echo '<td style="width:350px; border:solid 1px #bbbbbb; padding:4px; vertical-align:top; width:33%;">';
  193.         
  194.         echo "<form action=\"{$CFG->wwwroot}/mod/sloodle/view.php\" method=\"get\">";
  195.         echo '<input type="hidden" name="_type" value="users" />';
  196.         echo '<span style="font-weight:bold;">'.get_string('changecourse','sloodle').'</span><br/>';
  197.         
  198.         echo '<select name="course" size="1">';
  199.         
  200.         // Get a list of all courses
  201.         $allcourses = get_courses('all', 'c.shortname', 'c.id, c.shortname, c.fullname');
  202.         if (!$allcourses) $allcourses = array();
  203.         foreach ($allcourses as $as) {
  204.             // Is the user able to view this particular course?
  205.             if ($as->id == SITEID || has_capability('moodle/course:view'get_context_instance(CONTEXT_COURSE$as->id))) {
  206.                 // Output this as an option
  207.                 echo "<option value=\"{$as->id}\"";
  208.                 if ($as->id == $this->courseidecho "selected";
  209.                 echo ">{$as->fullname}</option>";
  210.             }
  211.         }
  212.         echo '</select><br/>';
  213.         
  214.         echo '<input type="checkbox" value="true" name="sloodleonly"';
  215.         if ($this->sloodleonlyecho "checked";
  216.         echo '/>'.get_string('showavatarsonly','sloodle').'<br/>';
  217.         echo '<input type="submit" value="'.get_string('submit','sloodle').'" />';
  218.         
  219.         echo '</form>';
  220.         
  221.         echo '</td>';
  222.         
  223.         // USER SEARCH FORM //
  224.         echo '<td style="width:350px; border:solid 1px #bbbbbb; padding:4px; vertical-align:top; width:33%;">';    
  225.         
  226.         echo "<form action=\"{$CFG->wwwroot}/mod/sloodle/view.php\" method=\"get\">";
  227.         echo '<input type="hidden" name="_type" value="users" />';
  228.         
  229.         echo '<span style="font-weight:bold;">'.get_string('usersearch','sloodle').'</span><br/>';
  230.         
  231.         echo '<input type="hidden" value="'.$this->courseid.'" name="course"/>';
  232.         echo '<input type="text" value="'.$this->searchstr.'" name="search" size="30" maxlength="30"/><br/>';
  233.         
  234.         echo '<input type="checkbox" value="true" name="sloodleonly"';
  235.         if ($this->sloodleonlyecho "checked";
  236.         echo '/>'.get_string('showavatarsonly','sloodle').'<br/>';
  237.         
  238.         echo '<input type="submit" value="'.get_string('submit','sloodle').'" />';
  239.         echo '</form>';
  240.         
  241.         echo '</td>';
  242.         
  243.         
  244.         
  245.         // AVATAR SEARCH //
  246.         echo '<td style="width:350px; border:solid 1px #bbbbbb; padding:4px; vertical-align:top; width:33%;">';
  247.         //echo '<span style="font-weight:bold;">'.get_string('specialpages','sloodle').'</span><br/>';        
  248.         echo "<form action=\"{$CFG->wwwroot}/mod/sloodle/view.php\" method=\"get\">";
  249.         echo '<input type="hidden" name="_type" value="user" />';
  250.         
  251.         echo '<span style="font-weight:bold;">'.get_string('avatarsearch','sloodle').'</span><br/>';
  252.         
  253.         echo '<input type="hidden" value="search" name="id"/>';
  254.         echo '<input type="hidden" value="'.$this->courseid.'" name="course"/>';
  255.         echo '<input type="text" value="'.$this->searchstr.'" name="search" size="30" maxlength="30"/><br/>';
  256.         echo '<br/><input type="submit" value="'.get_string('submit','sloodle').'" />';
  257.         echo '</form>';
  258.         
  259.         // Only provide an "all" link if the user is an admin
  260.         if (isadmin()) {
  261.             echo '<p>';
  262.             echo "<a href=\"{$CFG->wwwroot}/mod/sloodle/view.php?_type=user&amp;id=all\" title=\"".get_string('viewall','sloodle')."\">";
  263.             print_string('viewall','sloodle');
  264.             echo '</a><br/>';
  265.             echo '</p>';
  266.         }
  267.         
  268.         echo '</td>';
  269.         
  270.         
  271.         
  272. // // - END FORMS - // //
  273.         echo '</tr>';
  274.         echo '</table>';
  275.         print_box_end();
  276.  
  277.         
  278. //------------------------------------------------------
  279.  
  280.         
  281.         // Are we searching for users?
  282.         if ($this->searchstr != NULL)
  283.         {
  284.             // Display the search term
  285.             echo '<br/><span style="font-size:16pt; font-weight:bold;">'.get_string('usersearch','sloodle').': '.$this->searchstr.'</span><br/><br/>';
  286.             // Search the list of users
  287.             $fulluserlist get_users(true$this->searchstr);
  288.             if (!$fulluserlist$fulluserlist array();
  289.             $userlist array();
  290.             // Filter it down to members of the course
  291.             foreach ($fulluserlist as $ful{
  292.                 // Is this user on this course?
  293.                 if (has_capability('moodle/course:view', $this->course_context$ful->id)) {
  294.                     // Copy it to our filtered list
  295.                     $userlist[] = $ful;
  296.                 }
  297.             }
  298.             
  299.             
  300.         } else {
  301.             // Getting all users in a course
  302.             // Display the name of the course
  303.             echo '<br/><span style="font-size:18pt; font-weight:bold;">'.$this->coursefullname.'</span><br/><br/>';
  304.             // Obtain a list of all Moodle users enrolled in the specified course
  305.             $userlist get_course_users($this->courseid'lastname, firstname''''u.id, firstname, lastname');
  306.         }
  307.         
  308.         // Construct and display a table of Sloodle entries
  309.         if ($userlist) {
  310.             $sloodletable = new stdClass();
  311.             $sloodletable->head array(    get_string('user''sloodle'),
  312.                                             get_string('avatar''sloodle')
  313.                                         );
  314.             $sloodletable->align array('left''left');
  315.             $sloodletable->size array('50%''50%');
  316.             
  317.             // Check if our start is past the end of our results
  318.             if ($this->start >= count($userlist)) $this->start = 0;
  319.             
  320.             // Go through each entry to add it to the table
  321.             $resultnum 0;
  322.             $resultsdisplayed 0;
  323.             $maxperpage 20;
  324.             foreach ($userlist as $u{
  325.                 // Only display this result if it is after our starting result number
  326.                 if ($resultnum >= $this->start{
  327.                     // Reset the line's content
  328.                     $line = array();
  329.                     
  330.                     // Construct URLs to this user's Moodle and SLOODLE profile pages
  331.                     $url_moodleprofile = $CFG->wwwroot."/user/view.php?id={$u->id}&amp;course={$this->courseid}";
  332.                     $url_sloodleprofile = SLOODLE_WWWROOT."/view.php?_type=user&amp;id={$u->id}&amp;course={$this->courseid}";
  333.  
  334.                     // Add the Moodle name
  335.                     $line[] = "<a href=\"{$url_moodleprofile}\">{$u->firstname} {$u->lastname}</a>";
  336.                     
  337.                     // Get the Sloodle data for this Moodle user
  338.                     $sloodledata = get_records('sloodle_users', 'userid', $u->id);
  339.                     if ($sloodledata{
  340.                         // Display all avatars names, if available
  341.                         $avnames = '';
  342.                         $firstentry = true;
  343.                         foreach ($sloodledata as $sd) {
  344.                             // If this entry is empty, then skip it
  345.                             if (empty($sd->avname|| ctype_space($sd->avname)) continue;
  346.                             // Comma separated entries
  347.                             if ($firstentry$firstentry false;
  348.                             else $avnames .= ', ';
  349.                             // Add the current name
  350.                             $avnames .= $sd->avname;
  351.                         }
  352.                         // Add the avatar name(s) to the line
  353.                         $line[] = "<a href=\"{$url_sloodleprofile}\">{$avnames}</a>";
  354.                         
  355.                     } else {
  356.                         // The query failed - if we are showing only Sloodle-enabled users, then skip the rest
  357.                         if ($this->sloodleonlycontinue;
  358.                         $line['-';
  359.                     }
  360.                     
  361.                     // Add the line to the table
  362.                     $sloodletable->data[$line;
  363.                     $resultsdisplayed++;
  364.                 }
  365.                 
  366.                 // Have we displayed the maximum number of results for this page?
  367.                 $resultnum++;
  368.                 if ($resultsdisplayed >= $maxperpage) break;
  369.             }
  370.             
  371.             // Construct our basic URL to this page
  372.             $basicurl = SLOODLE_WWWROOT."/view.php?_type=users&amp;course={$this->courseid}";
  373.             if ($this->sloodleonly$basicurl .= "&amp;sloodleonly=true";
  374.             if (!empty($this->searchstr)) $basicurl .= "&amp;search={$this->searchstr}";
  375.             
  376.             // Construct the next/previous links
  377.             $previousstart = max(0, $this->start - $maxperpage);
  378.             $nextstart $this->start + $maxperpage;
  379.             $prevlink null;
  380.             $nextlink null;
  381.             if ($previousstart != $this->start$prevlink "<a href=\"{$basicurl}&amp;start={$previousstart}\" style=\"color:#0000ff;\">&lt;&lt;</a>&nbsp;&nbsp;";            
  382.             if ($nextstart < count($userlist)) $nextlink = "<a href=\"{$basicurl}&amp;start={$nextstart}\" style=\"color:#0000ff;\">&gt;&gt;</a>";
  383.             
  384.             // Display the next/previous links, if we have at least one
  385.             if (!empty($prevlink) || !empty($nextlink)) {
  386.                 echo '<p style="text-align:center; font-size:14pt;">';
  387.                 if (!empty($prevlink)) echo $prevlink;
  388.                 else echo '<span style="color:#777777;">&lt;&lt;</span>&nbsp;&nbsp;';
  389.                 if (!empty($nextlink)) echo $nextlink;
  390.                 else echo '<span style="color:#777777;">&gt;&gt;</span>&nbsp;&nbsp;';
  391.                 echo '</p>';
  392.             }
  393.             
  394.             // Display the table
  395.             print_table($sloodletable);
  396.             
  397.         } else {
  398.             // Failed to query for list of users
  399.             echo '<div style="font-weight:bold; color:red;">';
  400.             print_string('nouserdata','sloodle');
  401.             echo '</div>';
  402.         }
  403.         echo '</div>';
  404.         
  405.         
  406.         // Close the main body section
  407.         echo '</div>';
  408.  
  409.     }
  410.  
  411.     /**
  412.     * Print the page footer.
  413.     */
  414.     function print_footer()
  415.     {
  416.         print_footer($this->course);
  417.     }
  418.  
  419. }
  420.  
  421.  

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