Browse files

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

…page
  • Loading branch information...
1 parent 85c4758 commit 4e782b32f368ec19a16c74449a011f45f137f442 @rajeshtaneja rajeshtaneja committed Jun 13, 2012
Showing with 94 additions and 18 deletions.
  1. +23 −0 lib/filestorage/stored_file.php
  2. +14 −2 user/edit.php
  3. +6 −1 user/edit_form.php
  4. +13 −2 user/editadvanced.php
  5. +6 −1 user/editadvanced_form.php
  6. +32 −12 user/editlib.php
View
23 lib/filestorage/stored_file.php
@@ -391,6 +391,29 @@ public function copy_content_to($pathname) {
}
/**
+ * Copy content of file to temporary folder and returns file path
+ *
+ * @param string $dir name of the temporary directory
+ * @param string $fileprefix prefix of temporary file.
+ * @return string|bool path of temporary file or false.
+ */
+ public function copy_content_to_temp($dir = 'files', $fileprefix = 'tempup_') {
+ $tempfile = false;
+ if (!$dir = make_temp_directory($dir)) {
+ return false;
+ }
+ if (!$tempfile = tempnam($dir, $fileprefix)) {
+ return false;
+ }
+ if (!$this->copy_content_to($tempfile)) {
+ // something went wrong
+ @unlink($tempfile);
+ return false;
+ }
+ return $tempfile;
+ }
+
+ /**
* List contents of archive.
*
* @param file_packer $packer file packer instance
View
16 user/edit.php
@@ -153,7 +153,19 @@
);
$user = file_prepare_standard_editor($user, 'description', $editoroptions, $personalcontext, 'user', 'profile', 0);
-$userform = new user_edit_form(null, array('editoroptions'=>$editoroptions));
+// Prepare filemanager draft area.
+$draftitemid = 0;
+$filemanagercontext = $editoroptions['context'];
+$filemanageroptions = array('maxbytes' => $CFG->maxbytes,
+ 'subdirs' => 0,
+ 'maxfiles' => 1,
+ 'accepted_types' => 'web_image');
+file_prepare_draft_area($draftitemid, $filemanagercontext->id, 'user', 'newicon', 0, $filemanageroptions);
+$user->imagefile = $draftitemid;
+//create form
+$userform = new user_edit_form(null, array(
+ 'editoroptions' => $editoroptions,
+ 'filemanageroptions' => $filemanageroptions));
if (empty($user->country)) {
// MDL-16308 - we must unset the value here so $CFG->country can be used as default one
unset($user->country);
@@ -211,7 +223,7 @@
//update user picture
if (!empty($CFG->gdversion) and empty($CFG->disableuserimages)) {
- useredit_update_picture($usernew, $userform);
+ useredit_update_picture($usernew, $userform, $filemanageroptions);
}
// update mail bounces
View
7 user/edit_form.php
@@ -18,6 +18,11 @@ function definition () {
} else {
$editoroptions = null;
}
+ if (is_array($this->_customdata) && array_key_exists('filemanageroptions', $this->_customdata)) {
+ $filemanageroptions = $this->_customdata['filemanageroptions'];
+ } else {
+ $filemanageroptions = null;
+ }
//Accessibility: "Required" is bad legend text.
$strgeneral = get_string('general');
$strrequired = get_string('required');
@@ -32,7 +37,7 @@ function definition () {
$mform->addElement('header', 'moodle', $strgeneral);
/// shared fields
- useredit_shared_definition($mform, $editoroptions);
+ useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
/// extra settigs
if (!empty($CFG->gdversion) and !empty($CFG->disableuserimages)) {
View
15 user/editadvanced.php
@@ -134,8 +134,19 @@
);
}
+// Prepare filemanager draft area.
+$draftitemid = 0;
+$filemanagercontext = $editoroptions['context'];
+$filemanageroptions = array('maxbytes' => $CFG->maxbytes,
+ 'subdirs' => 0,
+ 'maxfiles' => 1,
+ 'accepted_types' => 'web_image');
+file_prepare_draft_area($draftitemid, $filemanagercontext->id, 'user', 'newicon', 0, $filemanageroptions);
+$user->imagefile = $draftitemid;
//create form
-$userform = new user_editadvanced_form(null, array('editoroptions'=>$editoroptions));
+$userform = new user_editadvanced_form(null, array(
+ 'editoroptions' => $editoroptions,
+ 'filemanageroptions' => $filemanageroptions));
$userform->set_data($user);
if ($usernew = $userform->get_data()) {
@@ -203,7 +214,7 @@
//update user picture
if (!empty($CFG->gdversion) and empty($USER->newadminuser)) {
- useredit_update_picture($usernew, $userform);
+ useredit_update_picture($usernew, $userform, $filemanageroptions);
}
// update mail bounces
View
7 user/editadvanced_form.php
@@ -19,6 +19,11 @@ function definition() {
} else {
$editoroptions = null;
}
+ if (is_array($this->_customdata) && array_key_exists('filemanageroptions', $this->_customdata)) {
+ $filemanageroptions = $this->_customdata['filemanageroptions'];
+ } else {
+ $filemanageroptions = null;
+ }
//Accessibility: "Required" is bad legend text.
$strgeneral = get_string('general');
@@ -58,7 +63,7 @@ function definition() {
$mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
$mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
/// shared fields
- useredit_shared_definition($mform, $editoroptions);
+ useredit_shared_definition($mform, $editoroptions, $filemanageroptions);
/// Next the customisable profile fields
profile_definition($mform);
View
44 user/editlib.php
@@ -42,28 +42,48 @@ function useredit_update_user_preference($usernew) {
* @param moodleform $userform The form that was submitted to edit the form
* @return bool True if the user was updated, false if it stayed the same.
*/
-function useredit_update_picture(stdClass $usernew, moodleform $userform) {
+function useredit_update_picture(stdClass $usernew, moodleform $userform, $filemanageroptions = array()) {
global $CFG, $DB;
require_once("$CFG->libdir/gdlib.php");
$context = get_context_instance(CONTEXT_USER, $usernew->id, MUST_EXIST);
$user = $DB->get_record('user', array('id'=>$usernew->id), 'id, picture', MUST_EXIST);
$newpicture = $user->picture;
-
+ // Get file_storage to process files.
+ $fs = get_file_storage();
if (!empty($usernew->deletepicture)) {
// The user has chosen to delete the selected users picture
- $fs = get_file_storage();
$fs->delete_area_files($context->id, 'user', 'icon'); // drop all images in area
$newpicture = 0;
- } else if ($iconfile = $userform->save_temp_file('imagefile')) {
- // There is a new image that has been uploaded
- // Process the new image and set the user to make use of it.
- // NOTE: Uploaded images always take over Gravatar
- $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile);
- // Delete the file that has now been processed
- @unlink($iconfile);
+ } else {
+ // Save newly uploaded file, this will avoid context mismatch for newly created users.
+ file_save_draft_area_files($usernew->imagefile, $context->id, 'user', 'newicon', 0, $filemanageroptions);
+ if (($iconfiles = $fs->get_area_files($context->id, 'user', 'newicon')) && count($iconfiles) == 2) {
+ // Get file which was uploaded in draft area
+ foreach ($iconfiles as $file) {
+ if (!$file->is_directory()) {
+ break;
+ }
+ }
+ // Copy file to temporary location and the send it for processing icon
+ if ($iconfile = $file->copy_content_to_temp()) {
+ // There is a new image that has been uploaded
+ // Process the new image and set the user to make use of it.
+ // NOTE: Uploaded images always take over Gravatar
+ $newpicture = (int)process_new_icon($context, 'user', 'icon', 0, $iconfile);
+ // Delete temporary file
+ @unlink($iconfile);
+ // Remove uploaded file.
+ $fs->delete_area_files($context->id, 'user', 'newicon');
+ } else {
+ // Something went wrong while creating temp file.
+ // Remove uploaded file.
+ $fs->delete_area_files($context->id, 'user', 'newicon');
+ return false;
+ }
+ }
}
if ($newpicture != $user->picture) {
@@ -101,7 +121,7 @@ function useredit_update_interests($user, $interests) {
tag_set('user', $user->id, $interests);
}
-function useredit_shared_definition(&$mform, $editoroptions = null) {
+function useredit_shared_definition(&$mform, $editoroptions = null, $filemanageroptions = null) {
global $CFG, $USER, $DB;
$user = $DB->get_record('user', array('id' => $USER->id));
@@ -268,7 +288,7 @@ function useredit_shared_definition(&$mform, $editoroptions = null) {
$mform->addElement('checkbox', 'deletepicture', get_string('delete'));
$mform->setDefault('deletepicture', 0);
- $mform->addElement('filepicker', 'imagefile', get_string('newpicture'), '', array('maxbytes'=>get_max_upload_file_size($CFG->maxbytes)));
+ $mform->addElement('filemanager', 'imagefile', get_string('newpicture'), '', $filemanageroptions);
$mform->addHelpButton('imagefile', 'newpicture');
$mform->addElement('text', 'imagealt', get_string('imagealt'), 'maxlength="100" size="30"');

0 comments on commit 4e782b3

Please sign in to comment.