return new stored_file($this, $newrecord);
}
+ /**
+ * Move one or more files from a given itemid location in the current user's draft files
+ * to a new filearea. Note that you can't rename files using this function.
+ * @param int $itemid - existing itemid in user draft_area with one or more files
+ * @param int $newcontextid - the new contextid to move files to
+ * @param string $newfilearea - the new filearea to move files to
+ * @param string $newfilepath - the new path to move all files to
+ * @param bool $overwrite - overwrite files from the destination if they exist
+ * @param int $newuserid - new userid if required
+ * @return mixed stored_file object or false if error; may throw exception if duplicate found
+ * @return array(contenthash, filesize, newfile)
+ */
+ public function move_draft_to_final($itemid, $newcontextid, $newfilearea, $newitemid,
+ $newfilepath='/', $overwrite=false) {
+
+ global $USER;
+
+ /// Get files from the draft area
+ if (!$usercontext = get_context_instance(CONTEXT_USER, $USER->id)) {
+ return false;
+ }
+ if (!$files = $this->get_area_files($usercontext->id, 'user_draft', $itemid, 'filename', false)) {
+ return false;
+ }
+
+ /// Process each file in turn
+
+ $returnfiles = array();
+ foreach ($files as $file) {
+
+ /// Delete any existing files in destination if required
+ if ($oldfile = $this->get_file($newcontextid, $newfilearea, $newitemid,
+ $newfilepath, $file->get_filename())) {
+ if ($overwrite) {
+ $oldfile->delete();
+ } else {
+ continue; // Can't overwrite the existing file so skip it
+ }
+ }
+
+ /// Create the new file
+ $newrecord = new object();
+ $newrecord->contextid = $newcontextid;
+ $newrecord->filearea = $newfilearea;
+ $newrecord->itemid = $newitemid;
+ $newrecord->filepath = $newfilepath;
+ $newrecord->filename = $file->get_filename();
+ $newrecord->timecreated = $file->get_timecreated();
+ $newrecord->timemodified = $file->get_timemodified();
+ $newrecord->mimetype = $file->get_mimetype();
+ $newrecord->userid = $file->get_userid();
+
+ if ($newfile = $this->create_file_from_storedfile($newrecord, $file->get_id())) {
+ $file->delete();
+ $returnfiles[] = $newfile;
+ }
+ }
+
+ return $returnfiles;
+ }
+
+
/**
* Add file content to sha1 pool
* @param string $pathname path to file
* @param string $newfilearea
* @param string $newfilepath
* @param string $newfilename - use specified filename, if not specified name of uploaded file used
- * @param bool $override override file if exists
+ * @param bool $overwrite - overwrite file if exists
* @param int $newuserid - new userid if required
* @return mixed stored_file object or false if error; may throw exception if duplicate found
*/
function save_stored_file($elname, $newcontextid, $newfilearea, $newitemid, $newfilepath='/',
- $newfilename=null, $override=false, $newuserid=null) {
+ $newfilename=null, $overwrite=false, $newuserid=null) {
global $USER;
$fs = get_file_storage();
if ($file = $fs->get_file($newcontextid, $newfilearea, $newitemid, $newfilepath, $newfilename)) {
- if ($override) {
+ if ($overwrite) {
$file->delete();
} else {
return false;
return $fs->create_file_from_pathname($file_record, $_FILES[$elname]['tmp_name']);
- } else if (!empty($this->_form->_submitValues[$elname])) { // Submit data has itemid in user's draft_area
-
- $itemid = $this->_form->_submitValues[$elname];
-
- if (!$context = get_context_instance(CONTEXT_USER, $USER->id)) {
- return false;
- }
+ } else { // We check if the file has been uploaded already into the user's draft area
- $fs = get_file_storage();
+ $values = $this->get_data();
- /// Delete any existing files in destination if required
- if ($file = $fs->get_file($newcontextid, $newfilearea, $newitemid, $newfilepath, $newfilename)) {
- if ($override) {
- $file->delete();
- } else {
- return false;
- }
- }
+ if (!empty($values->$elname)) {
- /// Get files from the draft area (even though we know there can be only one)
- if (!$files = $fs->get_area_files($context->id, 'user_draft', $itemid, 'filename', false)) {
- return false;
- }
+ $itemid = $values->$elname;
- $file = array_pop($files); // There will be only one file
+ $fs = get_file_storage();
- $newrecord = new object();
- $newrecord->contextid = $newcontextid;
- $newrecord->filearea = $newfilearea;
- $newrecord->itemid = $newitemid;
- $newrecord->filepath = $newfilepath;
- $newrecord->filename = ($newfilename ? $newfilename : $file->get_filename());
- $newrecord->timecreated = $file->get_timecreated();
- $newrecord->timemodified = $file->get_timemodified();
- $newrecord->mimetype = $file->get_mimetype();
- $newrecord->userid = $file->get_userid();
+ $newfiles = $fs->move_draft_to_final($itemid, $newcontextid, $newfilearea, $newitemid, $newfilepath, $overwrite);
- if (!$newfile = $fs->create_file_from_storedfile($newrecord, $file->get_id())) {
- return false;
+ return array_pop($newfiles);
}
-
- $file->delete();
- return $newfile;
}
return false;