user.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. <?php
  2. /**
  3. * Defines a base class for viewing information about a specific SLOODLE user (avatar).
  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 user information.
  22. * @package sloodle
  23. */
  24. class sloodle_view_user 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. * Integer ID of the Moodle user being viewed.
  46. * Supplied by request parameter.
  47. * @var integer
  48. * @access private
  49. */
  50. var $moodleuserid = null;
  51. /**
  52. * Integer ID of a SLOODLE user entry to delete, where supplied by request parameter.
  53. * Null or 0 indicates nothing is to be deleted.
  54. * @var integer
  55. * @access private
  56. */
  57. var $deletesloodleentry = null;
  58. /**
  59. * Indicates a confirmation for certain actions, such as deletion, supplied by request parameter.
  60. * @var string
  61. * @access private
  62. */
  63. var $userconfirmed = null;
  64. /**
  65. * The search string for users where appropriate.
  66. * @var string
  67. * @access private
  68. */
  69. var $searchstr = '';
  70. /**
  71. * List of user objects to be deleted (stored as a string, retrieved from request parameters).
  72. * @var string
  73. * @access private
  74. */
  75. var $deleteuserobjects = null;
  76. /**
  77. * Moodle permissions context for the current course.
  78. * @var object
  79. * @access private
  80. */
  81. var $course_context = null;
  82. /**
  83. * Moodle permissions context for the system.
  84. * @var object
  85. * @access private
  86. */
  87. var $system_context = null;
  88. /**
  89. * Is the user viewing their own profile?
  90. * @var bool
  91. * @access private
  92. */
  93. var $viewingself = false;
  94. /**
  95. * Can the user edit the data they are viewing?
  96. * @var bool
  97. * @access private
  98. */
  99. var $canedit = false;
  100. /**
  101. * The result number to start displaying from
  102. * @var integer
  103. * @access private
  104. */
  105. var $start = 0;
  106. /**
  107. * Constructor.
  108. */
  109. function sloodle_view_user()
  110. {
  111. }
  112. /**
  113. * Check and process the request parameters.
  114. */
  115. function process_request()
  116. {
  117. // Ensure the user logs in
  118. require_login();
  119. if (isguestuser()) error(get_string('noguestaccess', 'sloodle'));
  120. //add_to_log($this->course->id, 'course', 'view sloodle user', '', "{$this->course->id}");
  121. // We need to establish some permissions here
  122. $this->course_context = get_context_instance(CONTEXT_COURSE, $this->courseid);
  123. $this->system_context = get_context_instance(CONTEXT_SYSTEM);
  124. // Make sure the user has permission to view this course (but let anybody view the site course details)
  125. // Fetch some parameters
  126. $this->moodleuserid = required_param('id', PARAM_RAW);
  127. $this->deletesloodleentry = optional_param('delete', null, PARAM_INT);
  128. $this->userconfirmed = optional_param('confirm', null, PARAM_RAW);
  129. $this->courseid = optional_param('course', SITEID, PARAM_INT);
  130. $this->searchstr = addslashes(optional_param('search', '', PARAM_TEXT));
  131. $this->deleteuserobjects = optional_param('deleteuserobjects', null, PARAM_TEXT);
  132. // If we are viewing 'all' avatar entries, then revert to the site course
  133. if (strcasecmp($this->moodleuserid, 'all') == 0) $this->courseid = SITEID;
  134. // Fetch our Moodle and SLOODLE course data
  135. if (!$this->course = sloodle_get_record('course', 'id', $this->courseid)) error('Could not find course.');
  136. $this->sloodle_course = new SloodleCourse();
  137. if (!$this->sloodle_course->load($this->course)) error(get_string('failedcourseload', 'sloodle'));
  138. $this->start = optional_param('start', 0, PARAM_INT);
  139. if ($this->start < 0) $this->start = 0;
  140. // Moodle 2 rendering functions like to know the course.
  141. // They get upset if you try to pass a course into sloodle_print_footer() that isn't what they were expecting.
  142. if ($this->course) {
  143. global $PAGE;
  144. if (isset($PAGE) && method_exists($PAGE, 'set_course')) {
  145. $PAGE->set_course($this->course);
  146. }
  147. }
  148. }
  149. /**
  150. * Check that the user is logged-in and has permission to alter course settings.
  151. */
  152. function check_permission()
  153. {
  154. global $CFG, $USER;
  155. // Ensure the user logs in
  156. require_login();
  157. if (isguestuser()) error(get_string('noguestaccess', 'sloodle'));
  158. //add_to_log($this->course->id, 'course', 'view sloodle user', '', "{$this->course->id}");
  159. // We need to establish some permissions here
  160. $this->course_context = get_context_instance(CONTEXT_COURSE, $this->courseid);
  161. $this->system_context = get_context_instance(CONTEXT_SYSTEM);
  162. $this->viewingself = false;
  163. $this->canedit = false;
  164. // Is the user trying to view their own profile?
  165. if ($this->moodleuserid == $USER->id) {
  166. $this->viewingself = true;
  167. $this->canedit = true;
  168. } else {
  169. // The "all" view should be only available to admins
  170. if ( !has_capability('moodle/site:viewparticipants', $this->system_context) ){
  171. error(get_string('insufficientpermissiontoviewpage', 'sloodle'));
  172. exit();
  173. }
  174. // Does the user have permission to edit other peoples' profiles in the system and/or course?
  175. // If not, can they at least view others' profiles for the system or course?
  176. if (has_capability('moodle/user:editprofile', $this->system_context) || has_capability('moodle/user:editprofile', $this->course_context)) {
  177. // User can edit profiles
  178. $this->canedit = true;
  179. } else if (!(has_capability('moodle/user:viewdetails', $this->system_context) || has_capability('moodle/user:viewdetails', $this->course_context))) {
  180. // If this is the site course, then let it through anyway
  181. if ($this->courseid != SITEID) {
  182. error(get_string('insufficientpermissiontoviewpage','sloodle'));
  183. exit();
  184. }
  185. }
  186. }
  187. }
  188. /**
  189. * Print the course settings page header.
  190. */
  191. function sloodle_print_header()
  192. {
  193. }
  194. /**
  195. * Render the view of the module or feature.
  196. * This MUST be overridden to provide functionality.
  197. */
  198. function render()
  199. {
  200. global $CFG, $USER;
  201. // Were any of the delete parameters specified in HTTP (e.g. from a form)?
  202. if (!empty($this->deleteuserobjects) || !empty($this->deletesloodleentry) || !empty($this->userconfirmed)) {
  203. // Convert them to session parameters
  204. if (!empty($this->deleteuserobjects)) $_SESSION['deleteuserobjects'] = $this->deleteuserobjects;
  205. if (!empty($this->deletesloodleentry)) $_SESSION['deletesloodleentry'] = $this->deletesloodleentry;
  206. if (!empty($this->userconfirmed)) $_SESSION['userconfirmed'] = $this->userconfirmed;
  207. // Construct our full URL, with GET parameters
  208. $url = sloodle_get_web_path();
  209. $url .= "?_type=user&id={$this->moodleuserid}";
  210. if (!empty($this->courseid)) $url .= "&course={$this->courseid}";
  211. if (!empty($this->searchstr)) $url .= "&search={$this->searchstr}";
  212. if (!empty($this->start)) $url .= "&start={$this->start}";
  213. // Reload this page without those parameters
  214. redirect($url);
  215. exit();
  216. }
  217. // Extract data from our session parameters
  218. if (isset($_SESSION['deleteuserobjects'])) {
  219. $this->deleteuserobjects = $_SESSION['deleteuserobjects'];
  220. unset($_SESSION['deleteuserobjects']);
  221. }
  222. if (isset($_SESSION['deletesloodleentry'])) {
  223. $this->deletesloodleentry = $_SESSION['deletesloodleentry'];
  224. unset($_SESSION['deletesloodleentry']);
  225. }
  226. if (isset($_SESSION['userconfirmed'])) {
  227. $this->userconfirmed = $_SESSION['userconfirmed'];
  228. unset($_SESSION['userconfirmed']);
  229. }
  230. // Check the mode: all, search, pending, or single
  231. $allentries = false;
  232. $searchentries = false;
  233. if (strcasecmp($this->moodleuserid, 'all') == 0) {
  234. $allentries = true;
  235. $this->moodleuserid = -1;
  236. } else if (strcasecmp($this->moodleuserid, 'search') == 0) {
  237. $searchentries = true;
  238. $this->moodleuserid = -1;
  239. } else {
  240. // Make sure the Moodle user ID is an integer
  241. $this->moodleuserid = (integer)$this->moodleuserid;
  242. if ($this->moodleuserid <= 0) error(ucwords(get_string('unknownuser', 'sloodle')));
  243. }
  244. // Get the URL and names of the course
  245. $courseurl = $CFG->wwwroot.'/course/view.php?_type=user&amp;id='.$this->courseid;
  246. $courseshortname = $this->course->shortname;
  247. $coursefullname = $this->course->fullname;
  248. // This value will indicate if we are currently confirming a deletion
  249. $confirmingdeletion = false;
  250. // These are localization strings used by the deletion confirmation form
  251. $form_yes = get_string('Yes', 'sloodle');
  252. $form_no = get_string('No', 'sloodle');
  253. // Are we deleting a Sloodle entry?
  254. $deletemsg = '';
  255. if ($this->deletesloodleentry != NULL) {
  256. // Determine if the user is allowed to delete this entry
  257. $allowdelete = $this->canedit; // Just go with the editing ability for now... will maybe change this later. -PRB
  258. // Has the deletion been confirmed?
  259. if ($this->userconfirmed == $form_yes) {
  260. if (sloodle_record_exists('sloodle_users', 'id', $this->deletesloodleentry)) {
  261. // Is the user allowed to delete this?
  262. if ($allowdelete) {
  263. // Make sure it's a valid ID
  264. if (is_int($this->deletesloodleentry) && $this->deletesloodleentry > 0) {
  265. // Attempt to delete the entry
  266. $deleteresult = sloodle_delete_records('sloodle_users', 'id', $this->deletesloodleentry);
  267. if ($deleteresult === FALSE) {
  268. $deletemsg = get_string('deletionfailed', 'sloodle').': '.get_string('databasequeryfailed', 'sloodle');
  269. } else {
  270. $deletemsg = get_string('deletionsuccessful', 'sloodle');
  271. }
  272. } else {
  273. $deletemsg = get_string('deletionfailed', 'sloodle').': '.get_string('invalidid', 'sloodle');
  274. }
  275. } else {
  276. $deletemsg = get_string('deletionfailed', 'sloodle').': '.get_string('insufficientpermission', 'sloodle');
  277. }
  278. }
  279. } else if (is_null($this->userconfirmed)) {
  280. // User needs to confirm deletion
  281. $confirmingdeletion = true;
  282. $form_url = SLOODLE_WWWROOT."/view.php";
  283. $deletemsg .= '<h3>'.get_string('delete','sloodle').' '.get_string('ID','sloodle').': '.$this->deletesloodleentry.'<br/>'.get_string('confirmdelete','sloodle').'</h3>';
  284. $deletemsg .= '<form action="'.$form_url.'" method="get">';
  285. $deletemsg .= '<input type="hidden" name="_type" value="user" />';
  286. if ($allentries) $deletemsg .= '<input type="hidden" name="id" value="all" />';
  287. else if ($searchentries) $deletemsg .= '<input type="hidden" name="id" value="search" /><input type="hidden" name="search" value="'.$this->searchstr.'" />';
  288. else $deletemsg .= '<input type="hidden" name="id" value="'.$this->moodleuserid.'" />';
  289. if (!is_null($this->courseid)) $deletemsg .= '<input type="hidden" name="course" value="'.$this->courseid.'" />';
  290. $deletemsg .= '<input type="hidden" name="delete" value="'.$this->deletesloodleentry.'" />';
  291. $deletemsg .= '<input type="hidden" name="start" value="'.$this->start.'" />';
  292. $deletemsg .= '<input style="color:green;" type="submit" value="'.$form_yes.'" name="confirm" />&nbsp;&nbsp;';
  293. $deletemsg .= '<input style="color:red;" type="submit" value="'.$form_no.'" name="confirm" />';
  294. $deletemsg .= '</form><br/>';
  295. } else {
  296. $deletemsg = get_string('deletecancelled','sloodle');
  297. }
  298. }
  299. // Are we getting all entries, searching, or just viewing one?
  300. if ($allentries) {
  301. // All entries
  302. $moodleuserdata = null;
  303. // Fetch a list of all Sloodle user entries
  304. $sloodleentries = sloodle_get_records('sloodle_users');
  305. } else if ($searchentries && !empty($this->searchstr)) {
  306. // Search entries
  307. $moodleuserdata = null;
  308. $LIKE = sloodle_sql_ilike();
  309. $params = array('%'.$this->searchstr.'%', '%'.$this->searchstr.'%');
  310. global $CFG;
  311. $fullsloodleentries = sloodle_get_records_sql_params("select * from {$CFG->prefix}sloodle_users where avname $LIKE ? OR uuid $LIKE ?", $params);
  312. if (!$fullsloodleentries) $fullsloodleentries = array();
  313. $sloodleentries = array();
  314. // Eliminate entries belonging to avatars who are not in the current course
  315. foreach ($fullsloodleentries as $fse) {
  316. // Does the Moodle user have permission?
  317. if (has_capability('mod/sloodle:courseparticipate', $this->course_context, $fse->userid)) {
  318. // Copy it to our filtered list
  319. $sloodleentries[] = $fse;
  320. }
  321. }
  322. } else {
  323. // Attempt to fetch the Moodle user data
  324. $moodleuserdata = sloodle_get_record('user', 'id', $this->moodleuserid);
  325. // Fetch a list of all Sloodle user entries associated with this Moodle account
  326. $sloodleentries = sloodle_get_records('sloodle_users', 'userid', $this->moodleuserid);
  327. }
  328. // Post-process the query results
  329. if ($sloodleentries === FALSE) $sloodleentries = array();
  330. $numsloodleentries = count($sloodleentries);
  331. // Get the localization strings
  332. $strsloodle = get_string('modulename', 'sloodle');
  333. $strsloodles = get_string('modulenameplural', 'sloodle');
  334. $strunknown = get_string('unknown', 'sloodle');
  335. // Construct the breadcrumb links
  336. $navigation = "";
  337. if ($this->courseid != 1) $navigation .= "<a href=\"$courseurl\">$courseshortname</a> -> ";
  338. $navigation .= "<a href=\"".SLOODLE_WWWROOT."/view.php?_type=users&amp;course={$this->courseid}\">".get_string('sloodleuserprofiles', 'sloodle') . '</a> -> ';
  339. if ($this->moodleuserid > 0) {
  340. if ($moodleuserdata) $navigation .= $moodleuserdata->firstname.' '.$moodleuserdata->lastname;
  341. else $navigation .= get_string('unknownuser','sloodle');
  342. } else if ($searchentries) {
  343. $navigation .= get_string('avatarsearch', 'sloodle');
  344. } else {
  345. $navigation .= get_string('allentries', 'sloodle');
  346. }
  347. // Display the header
  348. sloodle_print_header(get_string('sloodleuserprofile', 'sloodle'), get_string('sloodleuserprofile','sloodle'), $navigation, "", "", true);
  349. echo '<div style="text-align:center;padding-left:8px;padding-right:8px;">';
  350. // Display the deletion message if we have one
  351. if (!empty($deletemsg)) {
  352. echo '<div style="text-align:center; padding:3px; border:solid 1px #aaaaaa; background-color:#dfdfdf; font-weight:bold; color:#dd0000;">';
  353. echo $deletemsg;
  354. echo '</div>';
  355. }
  356. echo '<br/>';
  357. // Are we dealing with an actual Moodle account?
  358. if ($this->moodleuserid > 0) {
  359. echo '<p>';
  360. // Yes - do we have an account?
  361. if ($moodleuserdata) {
  362. // Yes - display the name and other general info
  363. echo '<span style="font-size:18pt; font-weight:bold;">'. $moodleuserdata->firstname .' '. $moodleuserdata->lastname.'</span>';
  364. echo " <span style=\"font-size:10pt; color:#444444; font-style:italic;\">(<a href=\"{$CFG->wwwroot}/user/view.php?id={$this->moodleuserid}&amp;course={$this->courseid}\">".get_string('moodleuserprofile','sloodle')."</a>)</span><br/>";
  365. } else {
  366. echo get_string('moodleusernotfound', 'sloodle').'<br/>';
  367. }
  368. echo "<br/><br/>\n";
  369. // Check for issues such as no entries, or multiple entries
  370. if ($numsloodleentries == 0) {
  371. echo '<span style="color:red; font-weight:bold;">';
  372. print_string('noentries', 'sloodle');
  373. echo '</span>';
  374. // If it is the profile owner who is viewing this, then offer a link to the loginzone entry page
  375. if ($this->moodleuserid == $USER->id) {
  376. echo "<br/><br/><p style=\"padding:8px; border:solid 1px #555555;\"><a href=\"{$CFG->wwwroot}/mod/sloodle/classroom/loginzone.php?id={$this->courseid}\">";
  377. print_string('getnewloginzoneallocation', 'sloodle');
  378. echo '</a></p>';
  379. }
  380. } else if ($numsloodleentries > 1) {
  381. echo '<span style="color:red; font-weight:bold; border:solid 2px #990000; padding:4px; background-color:white;">';
  382. print_string('multipleentries', 'sloodle');
  383. sloodle_helpbutton('multiple_entries', get_string('help:multipleentries', 'sloodle'), 'sloodle', true, false);
  384. echo '</span>';
  385. }
  386. echo '</p>';
  387. } else if ($searchentries) {
  388. // Searching for users
  389. echo '<span style="font-size:18pt; font-weight:bold; ">'.get_string('avatarsearch','sloodle').": \"{$this->searchstr}\"</span><br/><br/>";
  390. // Check to see if there are no entries
  391. if ($numsloodleentries == 0) {
  392. echo '<span style="color:red; font-weight:bold;">';
  393. print_string('noentries', 'sloodle');
  394. echo '</span>';
  395. }
  396. } else {
  397. // Assume we're listing all entries - explain what this means
  398. echo '<span style="font-size:18pt; font-weight:bold; ">'.get_string('allentries','sloodle').'</span><br/>';
  399. echo '<center><p style="width:550px; text-align:left;">'.get_string('allentries:info', 'sloodle').'</p></center>';
  400. // Check to see if there are no entries
  401. if ($numsloodleentries == 0) {
  402. echo '<span style="color:red; font-weight:bold;">';
  403. print_string('noentries', 'sloodle');
  404. echo '</span>';
  405. }
  406. }
  407. // Construct and display a table of Sloodle entries
  408. if ($numsloodleentries > 0) {
  409. $sloodletable = new stdClass();
  410. $sloodletable->head = array( get_string('avatarname', 'sloodle'),
  411. get_string('avataruuid', 'sloodle'),
  412. get_string('profilePic', 'sloodle'),
  413. get_string('lastactive', 'sloodle'),
  414. ''
  415. );
  416. $sloodletable->align = array('left', 'left', 'left', 'left', 'left');
  417. $sloodletable->size = array('28%', '42%', '20%','20%', '10%');
  418. $deletestr = get_string('delete', 'sloodle');
  419. // We want to build a list of Sloodle user objects too
  420. $userobjects = array();
  421. // Create a dummy Sloodle Session
  422. $sloodle = new SloodleSession(false);
  423. // Check if our start is past the end of our results
  424. if ($this->start >= count($sloodleentries)) $this->start = 0;
  425. // Go through each Sloodle entry for this user
  426. $resultnum = 0;
  427. $resultsdisplayed = 0;
  428. $maxperpage = 20;
  429. foreach ($sloodleentries as $su) {
  430. // Only display this result if it is after our starting result number
  431. if ($resultnum >= $this->start) {
  432. // Add this user's Sloodle objects (if we're not in 'all' or search modes)
  433. if (!$allentries && !$searchentries) {
  434. if ($sloodle->user->load_avatar($su->uuid, '')) {
  435. $userobjects += $sloodle->user->get_user_objects();
  436. }
  437. }
  438. // Is this entry being deleted (i.e. is the user being asked to confirm its deletion)?
  439. $deletingcurrent = ($confirmingdeletion == true && $su->id == $this->deletesloodleentry);
  440. // Reset the line's content
  441. $line = array();
  442. // Fetch the avatar name and UUID
  443. $curavname = '-';
  444. $curuuid = '-';
  445. if (!empty($su->avname)) $curavname = $su->avname;
  446. if (!empty($su->uuid)) $curuuid = $su->uuid;
  447. // If we are in all or searching mode, add a link to the Sloodle user profile
  448. if ($allentries || $searchentries) {
  449. //$curavname .= " <span style=\"font-size:10pt; color:#444444; font-style:italic;\">(<a href=\"{$CFG->wwwroot}/mod/sloodle/view.php?_type=user&amp;id={$su->userid}&amp;course={$this->courseid}\">".get_string('sloodleuserprofile','sloodle')."</a>)</span>";
  450. $curavname = "<a href=\"{$CFG->wwwroot}/mod/sloodle/view.php?_type=user&amp;id={$su->userid}&amp;course={$this->courseid}\">{$curavname}</a>";
  451. }
  452. // Add a red cross beside the avatar name if it is being deleted
  453. if ($deletingcurrent) $curavname = '<span style="color:red; font-weight:bold;">X</span> '.$curavname;
  454. // Add them to the table
  455. $line[] = $curavname;
  456. $line[] = $curuuid;
  457. //grab image
  458. $endlink="";
  459. $startlink="";
  460. if (empty($su->profilepic)) {
  461. // There is no stored avatar picture URL.
  462. // 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.
  463. // 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.
  464. if (!empty($CFG->sloodle_gridtype) && !$allentries && !$searchentries) {
  465. if ($CFG->sloodle_gridtype=="SecondLife"){
  466. //scrape image
  467. $profile_key_prefix = "<meta name=\"imageid\" content=\"";
  468. $profile_img_prefix = "<img alt=\"profile image\" src=\"http://secondlife.com/app/image/";
  469. $profile_img_suffix ="parcel";
  470. $url = "http://world.secondlife.com/resident/".$curuuid;
  471. $ch = curl_init(); // initialize curl handle
  472. curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
  473. curl_setopt($ch, CURLOPT_FAILONERROR,0);
  474. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
  475. curl_setopt($ch, CURLOPT_TIMEOUT, 10); // times out after 4s
  476. curl_setopt($ch, CURLOPT_POST, 1); // set POST method
  477. curl_setopt($ch, CURLOPT_POSTFIELDS,""); // add POST fields
  478. $body= curl_exec($ch); // run the whole process
  479. curl_close($ch);
  480. $nameStart = strpos($body, '<title>');
  481. $nameEnd = strpos($body, '</title>');
  482. $nameStr = substr($body,$nameStart+7,$nameEnd-$nameStart-7);
  483. $imageStart = strpos($body, $profile_img_prefix);
  484. $imageEnd = strpos($body, $profile_img_suffix);
  485. $imgStr = substr($body,$imageStart+30,$imageEnd-$imageStart -39);
  486. $avimage = $imgStr;
  487. if (!$imageStart){
  488. $avimage= $CFG->wwwroot."/mod/sloodle/lib/media/empty.jpg";
  489. }else{
  490. $su->profilepic = $avimage;
  491. sloodle_update_record("sloodle_users",$su);
  492. }
  493. $startlink = '<a href="'.$url.'" target="_blank">';
  494. $endlink="</a>";
  495. }else{
  496. //grid type is opensim
  497. $avimage= $CFG->wwwroot."/mod/sloodle/lib/media/empty.jpg";
  498. }
  499. }//gridtype was not specified so just put empty.pngs for the users until admin specifies gridtype
  500. else{
  501. $avimage= $CFG->wwwroot."/mod/sloodle/lib/media/empty.jpg";
  502. }
  503. }//profile pic is already in db
  504. else{
  505. $avimage=$su->profilepic;
  506. }
  507. $line[]=$startlink.'<img style="width:40px;height:40px" src="'.$avimage.'">'.$endlink;
  508. // Do we know when the avatar was last active
  509. if (!empty($su->lastactive)) {
  510. // Calculate the time difference
  511. $difference = time() - (int)$su->lastactive;
  512. if ($difference < 0) $difference = 0;
  513. // Add it to the table
  514. $line[] = sloodle_describe_approx_time($difference, true);
  515. } else {
  516. $line[] = '('.$strunknown.')';
  517. }
  518. // Display the "delete" action
  519. if ($this->canedit || $su->userid == $USER->id) {
  520. if ($allentries) $deleteurl = $CFG->wwwroot."/mod/sloodle/view.php?_type=user&amp;id=all&amp;course={$this->courseid}&amp;delete={$su->id}&amp;start={$this->start}";
  521. else if ($searchentries) $deleteurl = $CFG->wwwroot."/mod/sloodle/view.php?_type=user&amp;id=search&amp;course={$this->courseid}&amp;search={$this->searchstr}&amp;delete={$su->id}&amp;start={$this->start}";
  522. else $deleteurl = $CFG->wwwroot."/mod/sloodle/view.php?_type=user&amp;id={$this->moodleuserid}&amp;course={$this->courseid}&amp;delete={$su->id}&amp;start={$this->start}";
  523. $deletecaption = get_string('clicktodeleteentry','sloodle');
  524. $line[] = "<a href=\"$deleteurl\" title=\"$deletecaption\">$deletestr</a>";
  525. } else {
  526. $line[] = '<span style="color:#777777;" title="'.get_string('nodeletepermission','sloodle').'">'.get_string('delete','sloodle').'</span>';
  527. }
  528. // Add the line to the table
  529. $sloodletable->data[] = $line;
  530. $resultsdisplayed++;
  531. }
  532. // Have we displayed the maximum number of results for this page?
  533. $resultnum++;
  534. if ($resultsdisplayed >= $maxperpage) break;
  535. }
  536. // Construct our basic URL to this page
  537. $basicurl = SLOODLE_WWWROOT."/view.php?_type=user&amp;course={$this->courseid}";
  538. if ($searchentries) $basicurl .= "&amp;id=search&amp;search={$this->searchstr}";
  539. else if ($allentries) $basicurl .= "&amp;id=all";
  540. else $basicurl .= "&amp;id={$this->moodleuserid}";
  541. // Construct the next/previous links
  542. $previousstart = max(0, $this->start - $maxperpage);
  543. $nextstart = $this->start + $maxperpage;
  544. $prevlink = null;
  545. $nextlink = null;
  546. if ($previousstart != $this->start) $prevlink = "<a href=\"{$basicurl}&amp;start={$previousstart}\">&lt;&lt;</a>&nbsp;&nbsp;";
  547. if ($nextstart < count($sloodleentries)) $nextlink = "<a href=\"{$basicurl}&amp;start={$nextstart}\">&gt;&gt;</a>";
  548. // Display the next/previous links, if we have at least one
  549. if (!empty($prevlink) || !empty($nextlink)) {
  550. echo '<p style="text-align:center; font-size:14pt;">';
  551. if (!empty($prevlink)) echo $prevlink;
  552. else echo '<span style="color:#777777;">&lt;&lt;</span>&nbsp;&nbsp;';
  553. if (!empty($nextlink)) echo $nextlink;
  554. else echo '<span style="color:#777777;">&gt;&gt;</span>&nbsp;&nbsp;';
  555. echo '</p>';
  556. }
  557. // Display the table
  558. sloodle_print_table($sloodletable);
  559. }
  560. // Display a link allowing admin users to add an avatar if this is a single avatar page
  561. if (!$allentries && !$searchentries)
  562. {
  563. if ( has_capability('moodle/site:viewparticipants', $this->system_context) )
  564. {
  565. echo "<p style=\"font-weight:bold;\">";
  566. echo "<a href=\"{$CFG->wwwroot}/mod/sloodle/view.php?_type=addavatar&amp;user={$this->moodleuserid}&amp;course={$this->courseid}\" title=\"".get_string('addavatarhere','sloodle')."\">";
  567. echo "<img src=\"{$CFG->wwwroot}/mod/sloodle/lib/media/add.png\" alt=\"[plus icon]\" /> ";
  568. print_string('addavatar', 'sloodle');
  569. echo "</a></p>\n";
  570. }
  571. }
  572. // Construct and display a table of Sloodle entries
  573. if ($numsloodleentries > 0) {
  574. // Display a list of user-authorised objects
  575. if (!$allentries && !$searchentries) {
  576. echo '<br/><h3>'.get_string('userobjects','sloodle');
  577. sloodle_helpbutton('user_objects', get_string('userobjects','sloodle'), 'sloodle', true, false, '', false);
  578. echo "</h3>\n";
  579. // Have we been asked to delete the user objects?
  580. if ($this->deleteuserobjects == 'true') {
  581. // Yes - display a confirmation form
  582. echo '<h4 style="color:red; font-weight:bold;">'.get_string('confirmdeleteuserobjects','sloodle').'</h4>';
  583. echo '<table style="border-style:none; margin-left:auto; margin-right:auto;"><tr><td>';
  584. echo '<form action="'.SLOODLE_WWWROOT.'/view.php" method="GET">';
  585. echo '<input type="hidden" name="_type" value="user" />';
  586. echo '<input type="hidden" name="id" value="'.$this->moodleuserid.'" >';
  587. if (!empty($courseid)) echo '<input type="hidden" name="course" value="'.$this->courseid.'" >';
  588. echo '<input type="hidden" name="deleteuserobjects" value="confirm" >';
  589. echo '<input type="hidden" name="start" value="'.$this->start.'" />';
  590. echo '<input type="submit" value="'.get_string('yes').'" title="'.get_string('deleteuserobjects:help','sloodle').'" >';
  591. echo '</form>';
  592. echo '</td><td>';
  593. echo '<form action="'.SLOODLE_WWWROOT.'/view.php" method="GET">';
  594. echo '<input type="hidden" name="_type" value="user" />';
  595. echo '<input type="hidden" name="id" value="'.$this->moodleuserid.'" >';
  596. if (!empty($this->courseid)) echo '<input type="hidden" name="course" value="'.$this->courseid.'" >';
  597. echo '<input type="hidden" name="start" value="'.$this->start.'" />';
  598. echo '<input type="submit" value="'.get_string('no').'" >';
  599. echo '</form>';
  600. echo '</td></tr></table><br>';
  601. } else if ($this->deleteuserobjects == 'confirm') {
  602. // Delete each one
  603. $numdeleted = 0;
  604. foreach ($userobjects as $obj) {
  605. sloodle_delete_records('sloodle_user_object', 'id', $obj->id);
  606. $numdeleted++;
  607. }
  608. $userobjects = array();
  609. echo get_string('numdeleted','sloodle').': '.$numdeleted.'<br><br>';
  610. }
  611. // Do we have any objects to display?
  612. if (count($userobjects) > 0) {
  613. // Yes - prepare the table
  614. $sloodletable = new stdClass();
  615. $sloodletable->head = array( get_string('ID', 'sloodle'),
  616. get_string('avataruuid', 'sloodle'),
  617. get_string('uuid', 'sloodle'),
  618. get_string('name', 'sloodle'),
  619. get_string('isauthorized', 'sloodle'),
  620. get_string('lastused', 'sloodle')
  621. );
  622. $sloodletable->align = array('center', 'left', 'left', 'left', 'center', 'left');
  623. //$sloodletable->size = array('5%', '5%', '27%', '35%', '20%', '8%');
  624. // Store the current timestamp for consistency
  625. $curtime = time();
  626. // Go through each object
  627. foreach ($userobjects as $obj) {
  628. $line = array();
  629. $line[] = $obj->id;
  630. $line[] = $obj->avuuid;
  631. $line[] = $obj->objuuid;
  632. $line[] = $obj->objname;
  633. if ($obj->authorized) $line[] = ucwords(get_string('yes'));
  634. else $line[] = ucwords(get_string('no'));
  635. $lastused = (int)$obj->timeupdated;
  636. if ($lastused > 0) $line[] = sloodle_describe_approx_time($curtime - $lastused, true);
  637. else $line[] = '('.get_string('unknown','sloodle').')';
  638. $sloodletable->data[] = $line;
  639. }
  640. // Display the table
  641. sloodle_print_table($sloodletable);
  642. // Display a button to delete all the Sloodle objects
  643. if (empty($deleteuserobjects)) {
  644. echo '<br><form action="'.SLOODLE_WWWROOT.'/view.php" method="GET">';
  645. echo '<input type="hidden" name="_type" value="user" />';
  646. echo '<input type="hidden" name="id" value="'.$this->moodleuserid.'" >';
  647. if (!empty($this->courseid)) echo '<input type="hidden" name="course" value="'.$this->courseid.'" >';
  648. echo '<input type="hidden" name="deleteuserobjects" value="true" >';
  649. echo '<input type="hidden" name="start" value="'.$this->start.'" />';
  650. echo '<input type="submit" value="'.get_string('deleteuserobjects','sloodle').'" title="'.get_string('deleteuserobjects:help','sloodle').'" >';
  651. echo '</form><br>';
  652. }
  653. } else {
  654. // No user objects
  655. echo '<span style="color:red; font-weight:bold;">';
  656. print_string('noentries', 'sloodle');
  657. echo '</span>';
  658. }
  659. }
  660. }
  661. echo '</div>';
  662. }
  663. /**
  664. * Print the page footer.
  665. */
  666. function sloodle_print_footer()
  667. {
  668. sloodle_print_footer($this->course);
  669. }
  670. }
  671. ?>