addavatar.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * Defines a class to render a page for adding avatar details into SLOODLE manually.
  4. * Class is inherited from the base view class.
  5. *
  6. * @package sloodle
  7. * @copyright Copyright (c) 2008-10 SLOODLE community (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. // Under all circumstances:
  14. // This page expects to be accessed with an HTTP parameter 'user', indicating the ID of the Moodle user to add an avatar to.
  15. // Optionally, a 'course' parameter can specify the course ID we came from to facilitate breadcrumb linking. (It will default to the site course without it.)
  16. // When submitting avatar data in a form:
  17. // Parameter 'sloodleuuid' and 'sloodleavname' must specify the UUID and name of the avatar being added.
  18. // Additionally, the 'sesskey' parameter should specify the user's session key to prevent XSS-style attacks.
  19. /** The base view class */
  20. require_once(SLOODLE_DIRROOT.'/view/base/base_view.php');
  21. /** Include our general SLOODLE functionality */
  22. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  23. /**
  24. * Class for rendering a view of SLOODLE course information.
  25. * @package sloodle
  26. */
  27. class sloodle_view_addavatar extends sloodle_base_view
  28. {
  29. /**
  30. * The VLE course object, retrieved directly from database.
  31. * Corresponds to the course the user came from, and is used only to facilitate navigation.
  32. * @var object
  33. * @access private
  34. */
  35. var $course = 0;
  36. /**
  37. * The user to whom we are adding an avatar. (A SloodleUser object)
  38. * @var object
  39. * @access private
  40. */
  41. var $user = null;
  42. /**
  43. * Indicates if an avatar is already registered to this user.
  44. * 0 means no, 1 means one avatar is registered, and 2 means more than 1 avatar is registered.
  45. * @var integer
  46. * @access private
  47. */
  48. var $has_avatar = 0;
  49. /**
  50. * This array will store a list of error messages to report to the user when the page is rendered.
  51. * @var array
  52. * @access private
  53. */
  54. var $msg_error = array();
  55. /**
  56. * This array will store a list of information messages (such as success repots) to display to the user when the page is rendered.
  57. * @var array
  58. * @access private
  59. */
  60. var $msg_info = array();
  61. /**
  62. * Indicates the status of the avatar add operation. 0 = nothing was done, -1 = attempted but failed, 1 = attempted and succeeded
  63. * @var integer
  64. * @var private
  65. */
  66. var $add_status = 0;
  67. /**
  68. * Constructor.
  69. */
  70. function sloodle_view_addavatar()
  71. {
  72. // Setup a dummy SloodleSession and use its user object
  73. $dummysession = new SloodleSession(false);
  74. $this->user = $dummysession->user;
  75. }
  76. /**
  77. * Check the request parameters to see which course was specified.
  78. */
  79. function process_request()
  80. {
  81. // Load the requested user
  82. $userid = required_param('user', PARAM_INT);
  83. if (!$this->user->load_user($userid)) error('User not found.');
  84. // Look for any existing registered avatars
  85. $linked_avatar = $this->user->load_linked_avatar();
  86. if ($linked_avatar === true) $this->has_avatar = 1;
  87. else if ($linked_avatar === 'multi') $this->has_avatar = 2;
  88. // Fetch the Moodle course data
  89. $courseid = optional_param('course', SITEID, PARAM_INT);
  90. if (!$this->course = sloodle_get_record('course', 'id', $courseid)) error('Could not find course.');
  91. // Moodle 2 rendering functions like to know the course.
  92. // They get upset if you try to pass a course into sloodle_print_footer() that isn't what they were expecting.
  93. if ($this->course) {
  94. global $PAGE;
  95. if (isset($PAGE) && method_exists($PAGE, 'set_course')) {
  96. $PAGE->set_course($this->course);
  97. }
  98. }
  99. }
  100. /**
  101. * Check that the user is logged-in and has permission to alter course settings.
  102. */
  103. function check_permission()
  104. {
  105. $system_context = get_context_instance(CONTEXT_SYSTEM);
  106. // Only allow admins to do this.
  107. if (!has_capability('moodle/user:editprofile', $system_context)) print_error('Only site administrators have access to this page.');
  108. }
  109. /**
  110. * Process any form data which has been submitted.
  111. * This must be overridden to add functionality.
  112. */
  113. function process_form()
  114. {
  115. // Was a form submitted?
  116. if (empty($_REQUEST['submit'])) return;
  117. // Make sure the correct session key was specified.
  118. $form_sesskey = required_param('sesskey', PARAM_RAW);
  119. if ($form_sesskey != sesskey())
  120. {
  121. $this->msg_error[] = get_string('invalidsesskey', 'sloodle');
  122. $this->add_status = -1;
  123. return;
  124. }
  125. // Grab the avatar data
  126. $form_uuid = required_param('sloodleuuid', PARAM_TEXT);
  127. $form_avname = required_param('sloodleavname', PARAM_TEXT);
  128. // Make sure the given UUID doesn't already exist in the database
  129. if (sloodle_count_records('sloodle_users', 'uuid', $form_uuid))
  130. {
  131. $this->msg_error[] = get_string('avataruuidalreadyindb', 'sloodle');
  132. $this->add_status = -1;
  133. return;
  134. }
  135. // Register the avatar to the user
  136. if ($this->user->add_linked_avatar($this->user->get_user_id(), $form_uuid, $form_avname))
  137. {
  138. $this->msg_info[] = get_string('addavatar:success', 'sloodle');
  139. $this->add_status = 1;
  140. } else {
  141. $this->msg_info[] = get_string('addavatar:fail', 'sloodle');
  142. $this->add_status = -1;
  143. }
  144. }
  145. /**
  146. * Print the course settings page header.
  147. */
  148. function sloodle_print_header()
  149. {
  150. global $CFG;
  151. // Construct the breadcrumb links
  152. $userid = $this->user->get_user_id();
  153. $navigation = "";
  154. if ($this->course->id != SITEID) $navigation .= "<a href=\"{$CFG->wwwroot}/course/view.php?_type=user&amp;id={$this->course->id}\">{$this->course->shortname}</a> -> ";
  155. $navigation .= "<a href=\"".SLOODLE_WWWROOT."/view.php?_type=users&amp;course={$this->course->id}\">".get_string('sloodleuserprofiles', 'sloodle') . '</a> -> ';
  156. $navigation .= "<a href=\"".SLOODLE_WWWROOT."/view.php?_type=user&amp;id={$userid}&amp;course={$this->course->id}\">".$this->user->get_user_firstname()." ".$this->user->get_user_lastname()."</a> -> ";
  157. $navigation .= get_string('addavatar', 'sloodle');
  158. // Display the header
  159. sloodle_print_header(get_string('addavatar', 'sloodle'), get_string('addavatar','sloodle'), $navigation, "", "", true);
  160. }
  161. /**
  162. * Render the view of the module or feature.
  163. * This MUST be overridden to provide functionality.
  164. */
  165. function render()
  166. {
  167. global $CFG;
  168. // Fetch string table text
  169. $stravatarname = get_string('avatarname', 'sloodle');
  170. $stravataruuid = get_string('avataruuid', 'sloodle');
  171. $strsubmit = get_string('submit', 'sloodle');
  172. $struser = get_string('user', 'sloodle');
  173. //------------------------------------------------------
  174. // Display any information messages
  175. if (count($this->msg_info) > 0)
  176. {
  177. sloodle_print_box_start('generalbox boxwidthwide boxaligncenter centerpara');
  178. echo "<ul style=\"list-style:none;\">\n";
  179. foreach ($this->msg_info as $msg)
  180. {
  181. echo "<li><img src=\"{$CFG->wwwroot}/pix/i/tick_green_big.gif\" alt=\"[tick icon]\" /> <strong>{$msg}</strong></li>\n";
  182. }
  183. echo "</ul>\n";
  184. sloodle_print_box_end();
  185. }
  186. // Display any error messages
  187. if (count($this->msg_error) > 0)
  188. {
  189. sloodle_print_box_start('generalbox boxwidthwide boxaligncenter centerpara');
  190. echo "<ul style=\"list-style:none;\">\n";
  191. foreach ($this->msg_error as $msg)
  192. {
  193. echo "<li><img src=\"{$CFG->wwwroot}/pix/i/cross_red_big.gif\" alt=\"[error icon]\" /> {$msg}</li>\n";
  194. }
  195. echo "</ul>\n";
  196. sloodle_print_box_end();
  197. }
  198. // Prepare the form default values
  199. $form_default_avname = '';
  200. $form_default_uuid = '';
  201. if ($this->add_status == -1)
  202. {
  203. $form_default_avname = optional_param('sloodleavname', '', PARAM_TEXT);
  204. $form_default_uuid = optional_param('sloodleuuid', '', PARAM_TEXT);
  205. }
  206. // Display the form
  207. $sk = sesskey();
  208. $userid = $this->user->get_user_id();
  209. $userfullname = $this->user->get_user_firstname().' '.$this->user->get_user_lastname();
  210. sloodle_print_box_start('generalbox boxwidthwide boxaligncenter centerpara');
  211. echo "<h2>",get_string('addavatar', 'sloodle'),"</h2>\n";
  212. echo <<<ADD_AVATAR_FORM
  213. <form action="{$CFG->wwwroot}/mod/sloodle/view.php" method="GET">
  214. <fieldset>
  215. <input type="hidden" name="_type" value="addavatar"/>
  216. <input type="hidden" name="user" value="{$userid}"/>
  217. <input type="hidden" name="course" value="{$this->course->id}"/>
  218. <input type="hidden" name="sesskey" value="{$sk}"/>
  219. {$struser}: <strong>{$userfullname}</strong><br/><br/>
  220. <label for="sloodleavname">{$stravatarname}: </label>
  221. <input type="text" name="sloodleavname" id="sloodleavname" value="{$form_default_avname}" size="45" /><br/><br/>
  222. <label for="sloodleuuid">{$stravataruuid}: </label>
  223. <input type="text" name="sloodleuuid" id="sloodleuuid" value="{$form_default_uuid}" size="45" /><br/><br/>
  224. <input type="submit" name="submit" value="{$strsubmit}" />
  225. </fieldset>
  226. </form>
  227. ADD_AVATAR_FORM;
  228. sloodle_print_box_end();
  229. echo "<div style=\"text-align:center;\">\n";
  230. echo "<a href=\"".SLOODLE_WWWROOT."/view.php?_type=user&amp;id={$userid}&amp;course={$this->course->id}\">&lt;&lt;&lt; ".get_string('backtoavatarpage','sloodle')."</a>";
  231. echo "</div>\n";
  232. }
  233. /**
  234. * Print the footer for this course.
  235. */
  236. function sloodle_print_footer()
  237. {
  238. sloodle_print_footer($this->course);
  239. }
  240. }
  241. ?>