users.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. /** The base view class */
  13. require_once(SLOODLE_DIRROOT.'/view/base/base_view.php');
  14. /** SLOODLE course data structure */
  15. require_once(SLOODLE_LIBROOT.'/course.php');
  16. /** Sloodle Session code. */
  17. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  18. /** General Sloodle functionality. */
  19. require_once(SLOODLE_LIBROOT.'/general.php');
  20. /**
  21. * Class for rendering a view of SLOODLE users.
  22. * @package sloodle
  23. */
  24. class sloodle_view_users extends sloodle_base_view
  25. {
  26. /**
  27. * Integer ID of the course which is being accessed.
  28. * @var integer
  29. * @access private
  30. */
  31. var $courseid = 0;
  32. /**
  33. * The VLE course object, retrieved directly from database.
  34. * @var object
  35. * @access private
  36. */
  37. var $course = 0;
  38. /**
  39. * SLOODLE course object, retrieved directly from database.
  40. * @var object
  41. * @access private
  42. */
  43. var $sloodle_course = null;
  44. /**
  45. * The search string for users where appropriate.
  46. * @var string
  47. * @access private
  48. */
  49. var $searchstr = '';
  50. /**
  51. * Moodle permissions context for the current course.
  52. * @var object
  53. * @access private
  54. */
  55. var $course_context = null;
  56. /**
  57. * Moodle permissions context for the system.
  58. * @var object
  59. * @access private
  60. */
  61. var $system_context = null;
  62. /**
  63. * Indicates if we should only show users who have avatars. Fetched from request parameters where given.
  64. * @var bool
  65. * @access private
  66. */
  67. var $sloodleonly = true;
  68. /**
  69. * URL for accessing the current course.
  70. * @var string
  71. * @access private
  72. */
  73. var $courseurl = '';
  74. /**
  75. * Short name of the current course.
  76. * @var string
  77. * @access private
  78. */
  79. var $courseshortname = '';
  80. /**
  81. * Full name of the current course.
  82. * @var string
  83. * @access private
  84. */
  85. var $coursefullname = '';
  86. /**
  87. * The result number to start displaying from
  88. * @var integer
  89. * @access private
  90. */
  91. var $start = 0;
  92. /**
  93. * Constructor.
  94. */
  95. function sloodle_view_users()
  96. {
  97. }
  98. /**
  99. * Check and process the request parameters.
  100. */
  101. function process_request()
  102. {
  103. global $CFG, $USER;
  104. // Fetch our Moodle and SLOODLE course data
  105. $this->courseid = optional_param('course', SITEID, PARAM_INT);
  106. if (!$this->course = sloodle_get_record('course', 'id', $this->courseid)) error('Could not find course.');
  107. $this->sloodle_course = new SloodleCourse();
  108. if (!$this->sloodle_course->load($this->course)) error(get_string('failedcourseload', 'sloodle'));
  109. $this->course_context = get_context_instance(CONTEXT_COURSE, $this->course->id);
  110. // Construct the course URL, and fetch the names
  111. $this->courseurl = $CFG->wwwroot.'/course/view.php?id='.$this->courseid;
  112. $this->courseshortname = $this->course->shortname;
  113. $this->coursefullname = $this->course->fullname;
  114. // Fetch the other parameters
  115. $this->searchstr = addslashes(optional_param('search', '', PARAM_TEXT));
  116. $this->sloodleonly = optional_param('sloodleonly', false, PARAM_BOOL);
  117. $this->start = optional_param('start', 0, PARAM_INT);
  118. if ($this->start < 0) $this->start = 0;
  119. // Moodle 2 rendering functions like to know the course.
  120. // They get upset if you try to pass a course into sloodle_print_footer() that isn't what they were expecting.
  121. if ($this->course) {
  122. global $PAGE;
  123. if (isset($PAGE) && method_exists($PAGE, 'set_course')) {
  124. $PAGE->set_course($this->course);
  125. }
  126. }
  127. }
  128. /**
  129. * Check that the user is logged-in and has permission to alter course settings.
  130. */
  131. function check_permission()
  132. {
  133. global $CFG, $USER;
  134. if ($this->courseid != SITEID) require_capability('mod/sloodle:courseparticipate', $this->course_context);
  135. }
  136. /**
  137. * Print the course settings page header.
  138. */
  139. function sloodle_print_header()
  140. {
  141. $navigation = '';
  142. if ($this->courseid != SITEID) $navigation .= "<a href=\"{$this->courseurl}\">{$this->courseshortname}</a> -> ";
  143. $navigation .= get_string('sloodleuserprofiles', 'sloodle');
  144. sloodle_print_header(get_string('sloodleuserprofiles', 'sloodle'), get_string('sloodleuserprofiles', 'sloodle'), $navigation, "", "", false);
  145. }
  146. /**
  147. * Render the view of the module or feature.
  148. * This MUST be overridden to provide functionality.
  149. */
  150. function render()
  151. {
  152. global $CFG, $USER;
  153. // Get the localization strings
  154. $strsloodle = get_string('modulename', 'sloodle');
  155. $strsloodles = get_string('modulenameplural', 'sloodle');
  156. // Open the main body section
  157. echo '<div style="text-align:center;padding-left:8px;padding-right:8px;">';
  158. //------------------------------------------------------
  159. sloodle_print_box_start('generalbox boxwidthwide boxaligncenter');
  160. echo '<table style="text-align:left; vertical-align:top; margin-left:auto; margin-right:auto;">';
  161. // // SEARCH FORMS // //
  162. echo '<tr>';
  163. /*
  164. Ed: Dropping this for now - breaking on Moodle 2, and it's weird to have a full site search under your course.
  165. // COURSE SELECT FORM //
  166. echo '<td style="width:350px; border:solid 1px #bbbbbb; padding:4px; vertical-align:top; width:33%;">';
  167. echo "<form action=\"{$CFG->wwwroot}/mod/sloodle/view.php\" method=\"get\">";
  168. echo '<input type="hidden" name="_type" value="users" />';
  169. echo '<span style="font-weight:bold;">'.get_string('changecourse','sloodle').'</span><br/>';
  170. echo '<select name="course" size="1">';
  171. // Get a list of all courses
  172. $allcourses = get_courses('all', 'c.shortname', 'c.id, c.shortname, c.fullname');
  173. if (!$allcourses) $allcourses = array();
  174. foreach ($allcourses as $as) {
  175. // Is the user able to view this particular course?
  176. if ($as->id == SITEID || has_capability('mod/sloodle:courseparticipate', get_context_instance(CONTEXT_COURSE, $as->id))) {
  177. // Output this as an option
  178. echo "<option value=\"{$as->id}\"";
  179. if ($as->id == $this->courseid) echo "selected";
  180. echo ">{$as->fullname}</option>";
  181. }
  182. }
  183. echo '</select><br/>';
  184. echo '<input type="checkbox" value="true" name="sloodleonly"';
  185. if ($this->sloodleonly) echo "checked";
  186. echo '/>'.get_string('showavatarsonly','sloodle').'<br/>';
  187. echo '<input type="submit" value="'.get_string('submit','sloodle').'" />';
  188. echo '</form>';
  189. echo '</td>';
  190. */
  191. // USER SEARCH FORM //
  192. echo '<td style="width:350px; border:solid 1px #bbbbbb; padding:4px; vertical-align:top; width:33%;">';
  193. echo "<form action=\"{$CFG->wwwroot}/mod/sloodle/view.php\" method=\"get\">";
  194. echo '<input type="hidden" name="_type" value="users" />';
  195. echo '<span style="font-weight:bold;">'.get_string('usersearch','sloodle').'</span><br/>';
  196. echo '<input type="hidden" value="'.s($this->courseid).'" name="course"/>';
  197. echo '<input type="text" value="'.$this->searchstr.'" name="search" size="30" maxlength="30"/><br/>';
  198. echo '<input type="checkbox" value="true" name="sloodleonly"';
  199. if ($this->sloodleonly) echo "checked";
  200. echo '/>'.get_string('showavatarsonly','sloodle').'<br/>';
  201. echo '<input type="submit" value="'.get_string('submit','sloodle').'" />';
  202. echo '</form>';
  203. echo '</td>';
  204. // AVATAR SEARCH //
  205. echo '<td style="width:350px; border:solid 1px #bbbbbb; padding:4px; vertical-align:top; width:33%;">';
  206. //echo '<span style="font-weight:bold;">'.get_string('specialpages','sloodle').'</span><br/>';
  207. echo "<form action=\"{$CFG->wwwroot}/mod/sloodle/view.php\" method=\"get\">";
  208. echo '<input type="hidden" name="_type" value="user" />';
  209. echo '<span style="font-weight:bold;">'.get_string('avatarsearch','sloodle').'</span><br/>';
  210. echo '<input type="hidden" value="search" name="id"/>';
  211. echo '<input type="hidden" value="'.s($this->courseid).'" name="course"/>';
  212. echo '<input type="text" value="'.s($this->searchstr).'" name="search" size="30" maxlength="30"/><br/>';
  213. echo '<br/><input type="submit" value="'.get_string('submit','sloodle').'" />';
  214. echo '</form>';
  215. echo '</td>';
  216. // // - END FORMS - // //
  217. echo '</tr>';
  218. echo '</table>';
  219. // Provide some admin-only links
  220. $system_context = get_context_instance(CONTEXT_SYSTEM);
  221. if ( has_capability('moodle/site:viewparticipants', $system_context) ) {
  222. echo '<p>';
  223. /*
  224. // Might be useful to add a "show admins" link, since they are not normally 'enrolled' in any course
  225. echo '&nbsp;&nbsp;|&nbsp;&nbsp;';*/
  226. // Link to view all avatars on the site
  227. echo "<a href=\"{$CFG->wwwroot}/mod/sloodle/view.php?_type=user&amp;id=all\" title=\"".get_string('viewall','sloodle')."\">";
  228. print_string('viewall','sloodle');
  229. echo '</a>';
  230. echo '</p>';
  231. }
  232. sloodle_print_box_end();
  233. //------------------------------------------------------
  234. // Are we searching for users?
  235. if ($this->searchstr != NULL)
  236. {
  237. // Display the search term
  238. echo '<br/><span style="font-size:16pt; font-weight:bold;">'.get_string('usersearch','sloodle').': '.$this->searchstr.'</span><br/><br/>';
  239. // Search the list of users
  240. // $fulluserlist = get_users(true, $this->searchstr);
  241. //$fulluserlist = get_users_by_capability('mod/sloodle:courseparticipate', $this->course_context);
  242. $fulluserlist = get_users_by_capability($this->course_context, 'mod/sloodle:courseparticipate', 'u.id, u.firstname, u.lastname', 'u.firstname, u.lastname');
  243. if (!$fulluserlist) $fulluserlist = array();
  244. $userlist = array();
  245. // Filter it down to members of the course
  246. foreach ($fulluserlist as $ful) {
  247. if ( ($ful->firstname == $this->searchstr) || ($ful->lastname == $this->searchstr) ) {
  248. $userlist[] = $ful;
  249. }
  250. }
  251. } else {
  252. // Getting all users in a course
  253. // Display the name of the course
  254. echo '<br/><span style="font-size:18pt; font-weight:bold;">'.s($this->coursefullname).'</span><br/><br/>';
  255. // Obtain a list of all Moodle users enrolled in the specified course
  256. //$userlist = get_course_users($this->courseid, 'lastname, firstname', '', 'u.id, firstname, lastname');
  257. $userlist = get_users_by_capability($this->course_context, 'mod/sloodle:courseparticipate', 'u.id, u.firstname, u.lastname', 'u.firstname, u.lastname');
  258. }
  259. // Construct and display a table of Sloodle entries
  260. if ($userlist) {
  261. $sloodletable = new stdClass();
  262. $sloodletable->head = array( get_string('user', 'sloodle'),
  263. get_string('avatar', 'sloodle')
  264. );
  265. $sloodletable->align = array('left', 'left');
  266. $sloodletable->size = array('50%', '50%');
  267. // Check if our start is past the end of our results
  268. if ($this->start >= count($userlist)) $this->start = 0;
  269. // Go through each entry to add it to the table
  270. $resultnum = 0;
  271. $resultsdisplayed = 0;
  272. $maxperpage = 20;
  273. foreach ($userlist as $u) {
  274. // Only display this result if it is after our starting result number
  275. if ($resultnum >= $this->start) {
  276. // Reset the line's content
  277. $line = array();
  278. // Construct URLs to this user's Moodle and SLOODLE profile pages
  279. $url_moodleprofile = $CFG->wwwroot."/user/view.php?id={$u->id}&amp;course={$this->courseid}";
  280. $url_sloodleprofile = SLOODLE_WWWROOT."/view.php?_type=user&amp;id={$u->id}&amp;course={$this->courseid}";
  281. // Add the Moodle name
  282. $line[] = "<a href=\"{$url_moodleprofile}\">{$u->firstname} {$u->lastname}</a>";
  283. // Get the Sloodle data for this Moodle user
  284. $sloodledata = sloodle_get_records('sloodle_users', 'userid', $u->id);
  285. if ($sloodledata) {
  286. // Display all avatars names, if available
  287. $avnames = '';
  288. $firstentry = true;
  289. foreach ($sloodledata as $sd) {
  290. // If this entry is empty, then skip it
  291. if (empty($sd->avname) || ctype_space($sd->avname)) continue;
  292. // Comma separated entries
  293. if ($firstentry) $firstentry = false;
  294. else $avnames .= ', ';
  295. // Add the current name
  296. $avnames .= $sd->avname;
  297. }
  298. // Add the avatar name(s) to the line
  299. $line[] = "<a href=\"{$url_sloodleprofile}\">{$avnames}</a>";
  300. } else {
  301. // The query failed - if we are showing only Sloodle-enabled users, then skip the rest
  302. if ($this->sloodleonly) continue;
  303. $line[] = '-';
  304. }
  305. // Add the line to the table
  306. $sloodletable->data[] = $line;
  307. $resultsdisplayed++;
  308. }
  309. // Have we displayed the maximum number of results for this page?
  310. $resultnum++;
  311. if ($resultsdisplayed >= $maxperpage) break;
  312. }
  313. // Construct our basic URL to this page
  314. $basicurl = SLOODLE_WWWROOT."/view.php?_type=users&amp;course={$this->courseid}";
  315. if ($this->sloodleonly) $basicurl .= "&amp;sloodleonly=true";
  316. if (!empty($this->searchstr)) $basicurl .= "&amp;search={$this->searchstr}";
  317. // Construct the next/previous links
  318. $previousstart = max(0, $this->start - $maxperpage);
  319. $nextstart = $this->start + $maxperpage;
  320. $prevlink = null;
  321. $nextlink = null;
  322. if ($previousstart != $this->start) $prevlink = "<a href=\"{$basicurl}&amp;start={$previousstart}\" style=\"color:#0000ff;\">&lt;&lt;</a>&nbsp;&nbsp;";
  323. if ($nextstart < count($userlist)) $nextlink = "<a href=\"{$basicurl}&amp;start={$nextstart}\" style=\"color:#0000ff;\">&gt;&gt;</a>";
  324. // Display the next/previous links, if we have at least one
  325. if (!empty($prevlink) || !empty($nextlink)) {
  326. echo '<p style="text-align:center; font-size:14pt;">';
  327. if (!empty($prevlink)) echo $prevlink;
  328. else echo '<span style="color:#777777;">&lt;&lt;</span>&nbsp;&nbsp;';
  329. if (!empty($nextlink)) echo $nextlink;
  330. else echo '<span style="color:#777777;">&gt;&gt;</span>&nbsp;&nbsp;';
  331. echo '</p>';
  332. }
  333. // Display the table
  334. sloodle_print_table($sloodletable);
  335. } else {
  336. // Failed to query for list of users
  337. echo '<div style="font-weight:bold; color:red;">';
  338. print_string('nouserdata','sloodle');
  339. echo '</div>';
  340. }
  341. echo '</div>';
  342. // Close the main body section
  343. echo '</div>';
  344. }
  345. /**
  346. * Print the page footer.
  347. */
  348. function sloodle_print_footer()
  349. {
  350. sloodle_print_footer($this->course);
  351. }
  352. }
  353. ?>