Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-33425 user: Replaced filepicker with filemanager on edit profile …

…page
  • Loading branch information...
commit 4e782b32f368ec19a16c74449a011f45f137f442 1 parent 85c4758
Rajesh Taneja rajeshtaneja authored
23 lib/filestorage/stored_file.php
@@ -391,6 +391,29 @@ public function copy_content_to($pathname) {
391 391 }
392 392
393 393 /**
  394 + * Copy content of file to temporary folder and returns file path
  395 + *
  396 + * @param string $dir name of the temporary directory
  397 + * @param string $fileprefix prefix of temporary file.
  398 + * @return string|bool path of temporary file or false.
  399 + */
  400 + public function copy_content_to_temp($dir = 'files', $fileprefix = 'tempup_') {
  401 + $tempfile = false;
  402 + if (!$dir = make_temp_directory($dir)) {
  403 + return false;
  404 + }
  405 + if (!$tempfile = tempnam($dir, $fileprefix)) {
  406 + return false;
  407 + }
  408 + if (!$this->copy_content_to($tempfile)) {
  409 + // something went wrong
  410 + @unlink($tempfile);
  411 + return false;
  412 + }
  413 + return $tempfile;
  414 + }
  415 +
  416 + /**
394 417 * List contents of archive.
395 418 *
396 419 * @param file_packer $packer file packer instance
16 user/edit.php
@@ -153,7 +153,19 @@
153 153 );
154 154
155 155 $user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
156   -$userform = new user_edit_form(null, array('editoroptions'=>$editoroptions));
  156 +// Prepare filemanager draft area.
  157 +$draftitemid = 0;
  158 +$filemanagercontext = $editoroptions['context'];
  159 +$filemanageroptions = array('maxbytes' => $CFG->maxbytes,
  160 + 'subdirs' => 0,
  161 + 'maxfiles' => 1,
  162 + 'accepted_types' => 'web_image');
  163 +file_prepare_draft_area($draftitemid, $filemanagercontext->id, 'user', 'newicon', 0, $filemanageroptions);
  164 +$user->imagefile = $draftitemid;
  165 +//create form
  166 +$userform = new user_edit_form(null, array(
  167 + 'editoroptions' => $editoroptions,
  168 + 'filemanageroptions' => $filemanageroptions));
157 169 if (empty($user->country)) {
158 170 // MDL-16308 - we must unset the value here so $CFG->country can be used as default one
159 171 unset($user->country);
@@ -211,7 +223,7 @@
211 223
212 224 //update user picture
213 225 if (!empty($CFG->gdversion) and empty($CFG->disableuserimages)) {
214   - useredit_update_picture($usernew, $userform);
  226 + useredit_update_picture($usernew, $userform, $filemanageroptions);
215 227 }
216 228
217 229 // update mail bounces
7 user/edit_form.php
@@ -18,6 +18,11 @@ function definition () {
18 18 } else {
19 19 $editoroptions = null;
20 20 }
  21 + if (is_array($this->_customdata) && array_key_exists('filemanageroptions', $this->_customdata)) {
  22 + $filemanageroptions = $this->_customdata['filemanageroptions'];
  23 + } else {
  24 + $filemanageroptions = null;
  25 + }
21 26 //Accessibility: "Required" is bad legend text.
22 27 $strgeneral = get_string('general');
23 28 $strrequired = get_string('required');
@@ -32,7 +37,7 @@ function definition () {
32 37 $mform->addElement('header', 'moodle', $strgeneral);
33 38
34 39 /// shared fields
35   - useredit_shared_definition($mform, $editoroptions);
  40 + useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
36 41
37 42 /// extra settigs
38 43 if (!empty($CFG->gdversion) and !empty($CFG->disableuserimages)) {
15 user/editadvanced.php
@@ -134,8 +134,19 @@
134 134 );
135 135 }
136 136
  137 +// Prepare filemanager draft area.
  138 +$draftitemid = 0;
  139 +$filemanagercontext = $editoroptions['context'];
  140 +$filemanageroptions = array('maxbytes' => $CFG->maxbytes,
  141 + 'subdirs' => 0,
  142 + 'maxfiles' => 1,
  143 + 'accepted_types' => 'web_image');
  144 +file_prepare_draft_area($draftitemid, $filemanagercontext->id, 'user', 'newicon', 0, $filemanageroptions);
  145 +$user->imagefile = $draftitemid;
137 146 //create form
138   -$userform = new user_editadvanced_form(null, array('editoroptions'=>$editoroptions));
  147 +$userform = new user_editadvanced_form(null, array(
  148 + 'editoroptions' => $editoroptions,
  149 + 'filemanageroptions' => $filemanageroptions));
139 150 $userform->set_data($user);
140 151
141 152 if ($usernew = $userform->get_data()) {
@@ -203,7 +214,7 @@
203 214
204 215 //update user picture
205 216 if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
206   - useredit_update_picture($usernew, $userform);
  217 + useredit_update_picture($usernew, $userform, $filemanageroptions);
207 218 }
208 219
209 220 // update mail bounces
7 user/editadvanced_form.php
@@ -19,6 +19,11 @@ function definition() {
19 19 } else {
20 20 $editoroptions = null;
21 21 }
  22 + if (is_array($this->_customdata) && array_key_exists('filemanageroptions', $this->_customdata)) {
  23 + $filemanageroptions = $this->_customdata['filemanageroptions'];
  24 + } else {
  25 + $filemanageroptions = null;
  26 + }
22 27
23 28 //Accessibility: "Required" is bad legend text.
24 29 $strgeneral = get_string('general');
@@ -58,7 +63,7 @@ function definition() {
58 63 $mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
59 64 $mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
60 65 /// shared fields
61   - useredit_shared_definition($mform, $editoroptions);
  66 + useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
62 67
63 68 /// Next the customisable profile fields
64 69 profile_definition($mform);
44 user/editlib.php
@@ -42,7 +42,7 @@ function useredit_update_user_preference($usernew) {
42 42 * @param moodleform $userform The form that was submitted to edit the form
43 43 * @return bool True if the user was updated, false if it stayed the same.
44 44 */
45   -function useredit_update_picture(stdClass $usernew, moodleform $userform) {
  45 +function useredit_update_picture(stdClass $usernew, moodleform $userform, $filemanageroptions = array()) {
46 46 global $CFG, $DB;
47 47 require_once("$CFG->libdir/gdlib.php");
48 48
@@ -50,20 +50,40 @@ function useredit_update_picture(stdClass $usernew, moodleform $userform) {
50 50 $user = $DB->get_record('user', array('id'=>$usernew->id), 'id, picture', MUST_EXIST);
51 51
52 52 $newpicture = $user->picture;
53   -
  53 + // Get file_storage to process files.
  54 + $fs = get_file_storage();
54 55 if (!empty($usernew->deletepicture)) {
55 56 // The user has chosen to delete the selected users picture
56   - $fs = get_file_storage();
57 57 $fs->delete_area_files($context->id, 'user', 'icon'); // drop all images in area
58 58 $newpicture = 0;
59 59
60   - } else if ($iconfile = $userform->save_temp_file('imagefile')) {
61   - // There is a new image that has been uploaded
62   - // Process the new image and set the user to make use of it.
63   - // NOTE: Uploaded images always take over Gravatar
64   - $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile);
65   - // Delete the file that has now been processed
66   - @unlink($iconfile);
  60 + } else {
  61 + // Save newly uploaded file, this will avoid context mismatch for newly created users.
  62 + file_save_draft_area_files($usernew->imagefile, $context->id, 'user', 'newicon', 0, $filemanageroptions);
  63 + if (($iconfiles = $fs->get_area_files($context->id, 'user', 'newicon')) && count($iconfiles) == 2) {
  64 + // Get file which was uploaded in draft area
  65 + foreach ($iconfiles as $file) {
  66 + if (!$file->is_directory()) {
  67 + break;
  68 + }
  69 + }
  70 + // Copy file to temporary location and the send it for processing icon
  71 + if ($iconfile = $file->copy_content_to_temp()) {
  72 + // There is a new image that has been uploaded
  73 + // Process the new image and set the user to make use of it.
  74 + // NOTE: Uploaded images always take over Gravatar
  75 + $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile);
  76 + // Delete temporary file
  77 + @unlink($iconfile);
  78 + // Remove uploaded file.
  79 + $fs->delete_area_files($context->id, 'user', 'newicon');
  80 + } else {
  81 + // Something went wrong while creating temp file.
  82 + // Remove uploaded file.
  83 + $fs->delete_area_files($context->id, 'user', 'newicon');
  84 + return false;
  85 + }
  86 + }
67 87 }
68 88
69 89 if ($newpicture != $user->picture) {
@@ -101,7 +121,7 @@ function useredit_update_interests($user, $interests) {
101 121 tag_set('user', $user->id, $interests);
102 122 }
103 123
104   -function useredit_shared_definition(&$mform, $editoroptions = null) {
  124 +function useredit_shared_definition(&$mform, $editoroptions = null, $filemanageroptions = null) {
105 125 global $CFG, $USER, $DB;
106 126
107 127 $user = $DB->get_record('user', array('id' => $USER->id));
@@ -268,7 +288,7 @@ function useredit_shared_definition(&$mform, $editoroptions = null) {
268 288 $mform->addElement('checkbox', 'deletepicture', get_string('delete'));
269 289 $mform->setDefault('deletepicture', 0);
270 290
271   - $mform->addElement('filepicker', 'imagefile', get_string('newpicture'), '', array('maxbytes'=>get_max_upload_file_size($CFG->maxbytes)));
  291 + $mform->addElement('filemanager', 'imagefile', get_string('newpicture'), '', $filemanageroptions);
272 292 $mform->addHelpButton('imagefile', 'newpicture');
273 293
274 294 $mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');

0 comments on commit 4e782b3

Please sign in to comment.
Something went wrong with that request. Please try again.