Skip to content

Commit

Permalink
MDL-14589 file api improvements - converting more params to general $…
Browse files Browse the repository at this point in the history
…options array
  • Loading branch information
skodak committed May 18, 2009
1 parent a07ca51 commit a19a06d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
4 changes: 2 additions & 2 deletions course/editsection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
require_capability('moodle/course:update', $context);

$draftitemid = file_get_submitted_draft_itemid('summary');
$currenttext = file_prepare_draft_area($draftitemid, $context->id, 'course_section', $section->id, true, $section->summary);
$currenttext = file_prepare_draft_area($draftitemid, $context->id, 'course_section', $section->id, array('subdirs'=>true), $section->summary);

$mform = new editsection_form(null, $course);
$data = array('id'=>$section->id, 'summary'=>array('text'=>$currenttext, 'format'=>FORMAT_HTML, 'itemid'=>$draftitemid));
Expand Down Expand Up @@ -56,4 +56,4 @@
$mform->display();
print_footer($course);

?>

3 changes: 1 addition & 2 deletions course/modedit.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@

if (plugin_supports('mod', $form->modulename, FEATURE_MOD_INTRO, true)) {
$draftid_editor = file_get_submitted_draft_itemid('introeditor');
$currentintro = file_prepare_draft_area($draftid_editor, $context->id, $form->modulename.'_intro', 0, true, $form->intro);
$currentintro = file_prepare_draft_area($draftid_editor, $context->id, $form->modulename.'_intro', 0, array('subdirs'=>true), $form->intro);
$form->introeditor = array('text'=>$currentintro, 'format'=>$form->introformat, 'itemid'=>$draftid_editor);
}

Expand Down Expand Up @@ -593,4 +593,3 @@
$mform->display();
print_footer($course);
}
?>
32 changes: 22 additions & 10 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function file_prepare_standard_editor($data, $field, array $options, $context=nu

if ($options['maxfiles'] != 0) {
$draftid_editor = file_get_submitted_draft_itemid($field);
$currenttext = file_prepare_draft_area($draftid_editor, $contextid, $filearea, $data->id, $options['subdirs'], $data->{$field}, $options['forcehttps']);
$currenttext = file_prepare_draft_area($draftid_editor, $contextid, $filearea, $data->id, $options, $data->{$field});
$data->{$field.'_editor'} = array('text'=>$currenttext, 'format'=>$data->{$field.'format'}, 'itemid'=>$draftid_editor);
} else {
$data->{$field.'_editor'} = array('text'=>$data->{$field}, 'format'=>$data->{$field.'format'}, 0);
Expand Down Expand Up @@ -243,7 +243,7 @@ function file_prepare_standard_filemanager($data, $field, array $options, $conte
}

$draftid_editor = file_get_submitted_draft_itemid($field.'_filemanager');
file_prepare_draft_area($draftid_editor, $contextid, $filearea, $data->id, $options['subdirs']);
file_prepare_draft_area($draftid_editor, $contextid, $filearea, $data->id, $options);
$data->{$field.'_filemanager'} = $draftid_editor;

return $data;
Expand Down Expand Up @@ -320,14 +320,21 @@ function file_get_unused_draft_itemid() {
* @param integer $contextid This parameter and the next two identify the file area to copy files from.
* @param string $filearea helps indentify the file area.
* @param integer $itemid helps identify the file area. Can be null if there are no files yet.
* @param boolean $subdirs allow directory structure within the file area.
* @param array $options text and file options ('subdirs'=>false, 'forcehttps'=>false)
* @param string $text some html content that needs to have embedded links rewritten to point to the draft area.
* @param boolean $forcehttps force https urls.
* @return string if $text was passed in, the rewritten $text is returned. Otherwise NULL.
*/
function file_prepare_draft_area(&$draftitemid, $contextid, $filearea, $itemid, $subdirs=false, $text=null, $forcehttps=false) {
function file_prepare_draft_area(&$draftitemid, $contextid, $filearea, $itemid, array $options=null, $text=null) {
global $CFG, $USER;

$options = (array)$options;
if (!isset($options['subdirs'])) {
$options['subdirs'] = false;
}
if (!isset($options['forcehttps'])) {
$options['forcehttps'] = false;
}

$usercontext = get_context_instance(CONTEXT_USER, $USER->id);
$fs = get_file_storage();

Expand All @@ -337,7 +344,7 @@ function file_prepare_draft_area(&$draftitemid, $contextid, $filearea, $itemid,
$file_record = array('contextid'=>$usercontext->id, 'filearea'=>'user_draft', 'itemid'=>$draftitemid);
if (!is_null($itemid) and $files = $fs->get_area_files($contextid, $filearea, $itemid)) {
foreach ($files as $file) {
if (!$subdirs and ($file->is_directory() or $file->get_filepath() !== '/')) {
if (!$options['subdirs'] and ($file->is_directory() or $file->get_filepath() !== '/')) {
continue;
}
$fs->create_file_from_storedfile($file_record, $file);
Expand All @@ -352,7 +359,7 @@ function file_prepare_draft_area(&$draftitemid, $contextid, $filearea, $itemid,
}

// relink embedded files - editor can not handle @@PLUGINFILE@@ !
return file_rewrite_pluginfile_urls($text, 'draftfile.php', $usercontext->id, 'user_draft', $draftitemid, $forcehttps);
return file_rewrite_pluginfile_urls($text, 'draftfile.php', $usercontext->id, 'user_draft', $draftitemid, $options['forcehttps']);
}

/**
Expand All @@ -362,12 +369,17 @@ function file_prepare_draft_area(&$draftitemid, $contextid, $filearea, $itemid,
* @param integer $contextid This parameter and the next two identify the file area to use.
* @param string $filearea helps indentify the file area.
* @param integer $itemid helps identify the file area.
* @param boot $forcehttps if we should output a https URL.
* @param array $options text and file options ('forcehttps'=>false)
* @return string the processed text.
*/
function file_rewrite_pluginfile_urls($text, $file, $contextid, $filearea, $itemid, $forcehttps=false) {
function file_rewrite_pluginfile_urls($text, $file, $contextid, $filearea, $itemid, array $options=null) {
global $CFG;

$options = (array)$options;
if (!isset($options['forcehttps'])) {
$options['forcehttps'] = false;
}

if (!$CFG->slasharguments) {
$file = $file . '?file=';
}
Expand All @@ -378,7 +390,7 @@ function file_rewrite_pluginfile_urls($text, $file, $contextid, $filearea, $item
$baseurl .= "$itemid/";
}

if ($forcehttps) {
if ($options['forcehttps']) {
$baseurl = str_replace('http://', 'https://', $baseurl);
}

Expand Down
4 changes: 2 additions & 2 deletions mod/forum/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@
$mform_post = new mod_forum_post_form('post.php', array('course'=>$course, 'cm'=>$cm, 'coursecontext'=>$coursecontext, 'modcontext'=>$modcontext, 'forum'=>$forum, 'post'=>$post));

$draftitemid = file_get_submitted_draft_itemid('attachments');
file_prepare_draft_area($draftitemid, $modcontext->id, 'forum_attachment', empty($post->id)?null:$post->id , false);
file_prepare_draft_area($draftitemid, $modcontext->id, 'forum_attachment', empty($post->id)?null:$post->id);

//load data into form NOW!

Expand Down Expand Up @@ -495,7 +495,7 @@
}

$draftid_editor = file_get_submitted_draft_itemid('message');
$currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'forum_post', empty($post->id) ? null : $post->id, true, $post->message);
$currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'forum_post', empty($post->id) ? null : $post->id, array('subdirs'=>true), $post->message);
$mform_post->set_data(array( 'attachments'=>$draftitemid,
'general'=>$heading,
'subject'=>$post->subject,
Expand Down

0 comments on commit a19a06d

Please sign in to comment.