MDL-33425 user: Replaced filepicker with filemanager on edit profile page
authorRajesh Taneja <rajesh@moodle.com>
Wed, 13 Jun 2012 09:27:17 +0000 (17:27 +0800)
committerRajesh Taneja <rajesh@moodle.com>
Mon, 18 Jun 2012 02:19:48 +0000 (10:19 +0800)
lib/filestorage/stored_file.php
user/edit.php
user/edit_form.php
user/editadvanced.php
user/editadvanced_form.php
user/editlib.php

index ea718ae..e9410bd 100644 (file)
@@ -390,6 +390,29 @@ class stored_file {
         return copy($path, $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.
      *
index 6fa1608..ca52ed3 100644 (file)
@@ -153,7 +153,19 @@ $editoroptions = array(
 );
 
 $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 @@ if ($usernew = $userform->get_data()) {
 
     //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
index 2fdfe1e..e53672f 100644 (file)
@@ -18,6 +18,11 @@ class user_edit_form extends moodleform {
         } 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 @@ class user_edit_form extends moodleform {
         $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)) {
index 3c660d7..5226c0a 100644 (file)
@@ -134,8 +134,19 @@ if ($user->id !== -1) {
     );
 }
 
+// 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 @@ if ($usernew = $userform->get_data()) {
 
     //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
index 58ab2a0..7092f02 100644 (file)
@@ -19,6 +19,11 @@ class user_editadvanced_form extends moodleform {
         } 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 @@ class user_editadvanced_form extends moodleform {
         $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);
index 8110f61..e9f7d4c 100644 (file)
@@ -42,7 +42,7 @@ 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");
 
@@ -50,20 +50,40 @@ function useredit_update_picture(stdClass $usernew, moodleform $userform) {
     $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"');