From: tjhunt Date: Wed, 1 Apr 2009 04:34:58 +0000 (+0000) Subject: file api: MDL-18754 improve the names of some functions. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=edc0c493322388f4a5754146e04b44082351b7ee;p=moodle.git file api: MDL-18754 improve the names of some functions. The new names are documented on http://docs.moodle.org/en/Development:Convert_Draftarea_Files --- diff --git a/course/editsection.php b/course/editsection.php index 86c9574dab..2f08c1e1a7 100644 --- a/course/editsection.php +++ b/course/editsection.php @@ -20,8 +20,8 @@ $context = get_context_instance(CONTEXT_COURSE, $course->id); require_capability('moodle/course:update', $context); - $draftitemid = file_get_submitted_draftitemid('summary'); - $currenttext = file_prepare_draftarea($draftitemid, $context->id, 'course_section', $section->id, true, $section->summary); + $draftitemid = file_get_submitted_draft_itemid('summary'); + $currenttext = file_prepare_draft_area($draftitemid, $context->id, 'course_section', $section->id, true, $section->summary); $mform = new editsection_form(null, $course); $data = array('id'=>$section->id, 'summary'=>array('text'=>$currenttext, 'format'=>FORMAT_HTML, 'itemid'=>$draftitemid)); @@ -33,7 +33,7 @@ } else if ($data = $mform->get_data()) { - $text = file_convert_draftarea($data->summary['itemid'], $context->id, 'course_section', $section->id, true, $data->summary['text']); + $text = file_save_draft_area_files($data->summary['itemid'], $context->id, 'course_section', $section->id, true, $data->summary['text']); $DB->set_field("course_sections", "summary", $text, array("id"=>$section->id)); add_to_log($course->id, "course", "editsection", "editsection.php?id=$section->id", "$section->section"); redirect("view.php?id=$course->id"); diff --git a/course/format/topics/format.php b/course/format/topics/format.php index 0356a611ae..c1cdd512ba 100644 --- a/course/format/topics/format.php +++ b/course/format/topics/format.php @@ -136,7 +136,7 @@ echo '
'; $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); - $summarytext = file_convert_relative_pluginfiles($thissection->summary, 'pluginfile.php', "$coursecontext->id/course_section/$thissection->id/"); + $summarytext = file_rewrite_pluginfile_urls($thissection->summary, 'pluginfile.php', $coursecontext->id, 'course_section', $thissection->id); $summaryformatoptions = new object(); $summaryformatoptions->noclean = true; echo format_text($summarytext, FORMAT_HTML, $summaryformatoptions); diff --git a/course/format/weeks/format.php b/course/format/weeks/format.php index 937542cf04..4eaa588c3a 100644 --- a/course/format/weeks/format.php +++ b/course/format/weeks/format.php @@ -126,7 +126,7 @@ echo '
'; $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); - $summarytext = file_convert_relative_pluginfiles($thissection->summary, 'pluginfile.php', "$coursecontext->id/course_section/$thissection->id/"); + $summarytext = file_rewrite_pluginfile_urls($thissection->summary, 'pluginfile.php', $coursecontext->id, 'course_section', $thissection->id); $summaryformatoptions = new object(); $summaryformatoptions->noclean = true; echo format_text($summarytext, FORMAT_HTML, $summaryformatoptions); diff --git a/index.php b/index.php index d0e8f5d75d..a619fafeb0 100644 --- a/index.php +++ b/index.php @@ -146,7 +146,7 @@ } $context = get_context_instance(CONTEXT_COURSE, SITEID); - $summarytext = file_convert_relative_pluginfiles($section->summary, 'pluginfile.php', "$context->id/course_section/$section->id/"); + $summarytext = file_rewrite_pluginfile_urls($section->summary, 'pluginfile.php', $context->id, 'course_section', $section->id); $summaryformatoptions = new object(); $summaryformatoptions->noclean = true; diff --git a/lib/filelib.php b/lib/filelib.php index 50aa5e44c9..4050407a3f 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -1,6 +1,37 @@ libdir/file/file_exceptions.php"); require_once("$CFG->libdir/file/file_storage.php"); @@ -70,10 +101,10 @@ function get_file_url($path, $options=null, $type='coursefile') { } /** - * Returns empty user upload draft area information - * @return int draftareaid + * @return int an available draft itemid that can be used to create a new draft + * file area. */ -function file_get_new_draftitemid() { +function file_get_unused_draft_itemid() { global $DB, $USER; if (isguestuser() or !isloggedin()) { @@ -94,17 +125,19 @@ function file_get_new_draftitemid() { } /** - * Creates new draft area if not exists yet and copies files there - * @param int &$draftitemid - * @param int $contextid - * @param string $filearea - * @param int $itemid (null menas no existing files yet) - * @param bool subdirs allow directory structure - * @param string $text usually html text with embedded links to draft area - * @param boolean $forcehttps force https - * @return string text with relative links starting with @@PLUGINFILE@@ + * Initialise a draft file area from a real one by copying the files. A draft + * area will be created if one does not already exist. Normally you should + * get $draftitemid by calling file_get_submitted_draft_itemid('elementname'); + * @param int &$draftitemid the id of the draft area to use, or 0 to create a new one, in which case this parameter is updated. + * @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 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_draftarea(&$draftitemid, $contextid, $filearea, $itemid, $subdirs=false, $text=null, $forcehttps=false) { +function file_prepare_draft_area(&$draftitemid, $contextid, $filearea, $itemid, $subdirs=false, $text=null, $forcehttps=false) { global $CFG, $USER; $usercontext = get_context_instance(CONTEXT_USER, $USER->id); @@ -112,7 +145,7 @@ function file_prepare_draftarea(&$draftitemid, $contextid, $filearea, $itemid, $ if (empty($draftitemid)) { // create a new area and copy existing files into - $draftitemid = file_get_new_draftitemid(); + $draftitemid = file_get_unused_draft_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) { @@ -130,77 +163,89 @@ function file_prepare_draftarea(&$draftitemid, $contextid, $filearea, $itemid, $ return null; } - /// relink embedded files - editor can not handle @@PLUGINFILE@@ ! - return file_convert_relative_pluginfiles($text, 'draftfile.php', "$usercontext->id/user_draft/$draftitemid/", $forcehttps); + // relink embedded files - editor can not handle @@PLUGINFILE@@ ! + return file_rewrite_pluginfile_urls($text, 'draftfile.php', $usercontext->id, 'user_draft', $draftitemid, $forcehttps); } /** - * Convert relative @@PLUGINFILE@@ into real absolute paths - * @param string $text - * @param string $file pluginfile.php, draftfile.php, etc - * @param string $pluginstub path 'contextid/area/itemid/' - * @param boot $forcehttps - * @return string text with absolute links - * + * Convert encoded URLs in $text from the @@PLUGINFILE@@/... form to an actual URL. + * @param string $text The content that may contain ULRs in need of rewriting. + * @param string $file The script that should be used to serve these files. pluginfile.php, draftfile.php, etc. + * @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. + * @return string the processed text. */ -function file_convert_relative_pluginfiles($text, $file, $pluginstub, $forcehttps=false) { +function file_rewrite_pluginfile_urls($text, $file, $contextid, $filearea, $itemid, $forcehttps=false) { global $CFG; - if ($CFG->slasharguments) { - $draftbase = "$CFG->wwwroot/$file/$pluginstub"; - } else { - $draftbase = "$CFG->wwwroot/draftfile.php?file=/$pluginstub"; + if (!$CFG->slasharguments) { + $file = $file . '?file='; } + $baseurl = "$CFG->wwwroot/$file/$contextid/$filearea/$itemid/"; + if ($forcehttps) { - $draftbase = str_replace('http://', 'https://', $draftbase); + $baseurl = str_replace('http://', 'https://', $baseurl); } - return str_replace('@@PLUGINFILE@@/', $draftbase, $text); + return str_replace('@@PLUGINFILE@@/', $baseurl, $text); } /** - * Returns information about files in draft area - * @param $draftitemid - * @return array TODO: count=>n + * Returns information about files in a draft area. + * @param integer $draftitemid the draft area item id. + * @return array with the following entries: + * 'filecount' => number of files in the draft area. + * (more information will be added as needed). */ -function get_draftarea_info($draftitemid) { - +function file_get_draft_area_info($draftitemid) { global $CFG, $USER; $usercontext = get_context_instance(CONTEXT_USER, $USER->id); $fs = get_file_storage(); - // number of files + $results = array(); + + // The number of files $draftfiles = $fs->get_area_files($usercontext->id, 'user_draft', $draftitemid, 'id', false); + $results['filecount'] = $draftfiles; - return array('filecount'=>count($draftfiles)); + return $results; } /** - * Returns draftitemid of given editor element. - * @param string $elname name of formlib editor element - * @return int 0 if not submitted yet + * Returns draft area itemid for a given element. + * @param string $elname name of formlib editor element, or a hidden form field that stores the draft area item id, etc. + * @return inteter the itemid, or 0 if there is not one yet. */ -function file_get_submitted_draftitemid($elname) { - if (!empty($_REQUEST[$elname]['itemid']) and confirm_sesskey()) { - return (int)$_REQUEST[$elname]['itemid']; +function file_get_submitted_draft_itemid($elname) { + $param = optional_param($elname, 0, PARAM_INT); + if ($param) { + confirm_sesskey(); + } + if (is_array($param)) { + $param = $param['itemid']; } - return 0; + return $param; } /** - * Converts absolute links in text and merges draft files to target area. - * @param int $draftitemid - * @param int $contextid - * @param string $filearea - * @param int $itemid - * @param bool subdirs allow directory structure - * @param string $text usually html text with embedded links to draft area - * @param boolean $forcehttps force https - * @return string text with relative links starting with @@PLUGINFILE@@ + * Saves files from a draft file area to a real one (merging the list of files). + * Can rewrite URLs in some content at the same time if desired. + * @param int $draftitemid the id of the draft area to use. Normally obtained + * from file_get_submitted_draft_itemid('elementname') or similar. + * @param integer $contextid This parameter and the next two identify the file area to save to. + * @param string $filearea helps indentify the file area. + * @param integer $itemid helps identify the file area. + * @param boolean $subdirs allow directory structure within the file area. + * @param string $text some html content that needs to have embedded links rewritten + * to the @@PLUGINFILE@@ form for saving in the database. + * @param boolean $forcehttps force https urls. + * @return string if $text was passed in, the rewritten $text is returned. Otherwise NULL. */ -function file_convert_draftarea($draftitemid, $contextid, $filearea, $itemid, $subdirs=false, $text=null, $forcehttps=false) { +function file_save_draft_area_files($draftitemid, $contextid, $filearea, $itemid, $subdirs=false, $text=null, $forcehttps=false) { global $CFG, $USER; $usercontext = get_context_instance(CONTEXT_USER, $USER->id); @@ -261,8 +306,7 @@ function file_convert_draftarea($draftitemid, $contextid, $filearea, $itemid, $s return null; } - /// relink embedded files if text submitted - no absolute links allowed in database! - + // relink embedded files if text submitted - no absolute links allowed in database! if ($CFG->slasharguments) { $draftbase = "$CFG->wwwroot/draftfile.php/$usercontext->id/user_draft/$draftitemid/"; } else { diff --git a/lib/form/editor.php b/lib/form/editor.php index 46afcae474..f7f3321717 100644 --- a/lib/form/editor.php +++ b/lib/form/editor.php @@ -157,7 +157,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { $str .= ''; } else { // no changes of format allowed - $str .= ''; + $str .= ''; $str .= $formats[$format]; } $str .= '
'; @@ -166,7 +166,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { if (empty($draftitemid)) { // no existing area info provided - let's use fresh new draft area require_once("$CFG->libdir/filelib.php"); - $this->setValue(array('itemid'=>file_get_new_draftitemid())); + $this->setValue(array('itemid'=>file_get_unused_draft_itemid())); $draftitemid = $this->_values['itemid']; } $str .= '
'; diff --git a/lib/form/filemanager.php b/lib/form/filemanager.php index 6d375ab7ee..8b54718111 100644 --- a/lib/form/filemanager.php +++ b/lib/form/filemanager.php @@ -161,7 +161,7 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element { if (empty($draftitemid)) { // no existing area info provided - let's use fresh new draft area require_once("$CFG->libdir/filelib.php"); - $this->setValue(file_get_new_draftitemid()); + $this->setValue(file_get_unused_draft_itemid()); $draftitemid = $this->getValue(); } diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 9dc282e8aa..cd40af8f36 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -2972,7 +2972,7 @@ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=fa $post->course = $course->id; $post->forum = $forum->id; - $post->message = file_convert_relative_pluginfiles($post->message, 'pluginfile.php', "$modcontext->id/forum_post/$post->id/"); + $post->message = file_rewrite_pluginfile_urls($post->message, 'pluginfile.php', $modcontext->id, 'forum_post', $post->id); // caching if (!isset($cm->cache)) { @@ -4136,9 +4136,9 @@ function forum_add_attachment($post, $forum, $cm, $mform=null, &$message=null) { $context = get_context_instance(CONTEXT_MODULE, $cm->id); - $info = get_draftarea_info($post->attachments); + $info = file_get_draft_area_info($post->attachments); $present = ($info['filecount']>0) ? '1' : ''; - file_convert_draftarea($post->attachments, $context->id, 'forum_attachment', $post->id, false); + file_save_draft_area_files($post->attachments, $context->id, 'forum_attachment', $post->id, false); $DB->set_field('forum_posts', 'attachment', $present, array('id'=>$post->id)); @@ -4165,8 +4165,7 @@ function forum_add_new_post($post, $mform, &$message) { if (! $post->id = $DB->insert_record("forum_posts", $post)) { return false; } - - $message = file_convert_draftarea($post->itemid, $context->id, 'forum_post', $post->id, true, $message); + $message = file_save_draft_area_files($post->itemid, $context->id, 'forum_post', $post->id, true, $message); $DB->set_field('forum_posts', 'message', $message, array('id'=>$post->id)); forum_add_attachment($post, $forum, $cm, $mform, $message); @@ -4206,7 +4205,7 @@ function forum_update_post($post, $mform, &$message) { $discussion->timestart = $post->timestart; $discussion->timeend = $post->timeend; } - $post->message = file_convert_draftarea($post->itemid, $context->id, 'forum_post', $post->id, true, $post->message); + $post->message = file_save_draft_area_files($post->itemid, $context->id, 'forum_post', $post->id, true, $post->message); $DB->set_field('forum_posts', 'message', $post->message, array('id'=>$post->id)); if (!$DB->update_record('forum_discussions', $discussion)) { @@ -4257,7 +4256,7 @@ function forum_add_discussion($discussion, $mform=null, &$message=null) { return 0; } - $text = file_convert_draftarea($discussion->itemid, $context->id, 'forum_post', $post->id, true, $post->message); + $text = file_save_draft_area_files($discussion->itemid, $context->id, 'forum_post', $post->id, true, $post->message); $DB->set_field('forum_posts', 'message', $text, array('id'=>$post->id)); // Now do the main entry for the discussion, linking to this first post diff --git a/mod/forum/post.php b/mod/forum/post.php index a5f9e2de55..fd184730b8 100644 --- a/mod/forum/post.php +++ b/mod/forum/post.php @@ -13,8 +13,6 @@ $name = optional_param('name', '', PARAM_CLEAN); $confirm = optional_param('confirm', 0, PARAM_INT); $groupid = optional_param('groupid', null, PARAM_INT); - $draftitemid = optional_param('attachments', 0, PARAM_INT); - //these page_params will be passed as hidden variables later in the form. $page_params = array('reply'=>$reply, 'forum'=>$forum, 'edit'=>$edit); @@ -459,7 +457,8 @@ $mform_post = new mod_forum_post_form('post.php', array('course'=>$course, 'cm'=>$cm, 'coursecontext'=>$coursecontext, 'modcontext'=>$modcontext, 'forum'=>$forum, 'post'=>$post)); - file_prepare_draftarea($draftitemid, $modcontext->id, 'forum_attachment', empty($post->id)?null:$post->id , false); + $draftitemid = file_get_submitted_draft_itemid('attachments'); + file_prepare_draft_area($draftitemid, $modcontext->id, 'forum_attachment', empty($post->id)?null:$post->id , false); //load data into form NOW! @@ -496,8 +495,8 @@ $subscribe = !empty($USER->autosubscribe); } - $draftid_editor = file_get_submitted_draftitemid('message'); - $currenttext = file_prepare_draftarea($draftid_editor, $modcontext->id, 'forum_post', empty($post->id) ? null : $post->id, true, $post->message); + $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); $mform_post->set_data(array( 'attachments'=>$draftitemid, 'general'=>$heading, 'subject'=>$post->subject,