]> git.mjollnir.org Git - moodle.git/commitdiff
file api: MDL-18754 improve the names of some functions.
authortjhunt <tjhunt>
Wed, 1 Apr 2009 04:34:58 +0000 (04:34 +0000)
committertjhunt <tjhunt>
Wed, 1 Apr 2009 04:34:58 +0000 (04:34 +0000)
The new names are documented on
http://docs.moodle.org/en/Development:Convert_Draftarea_Files

course/editsection.php
course/format/topics/format.php
course/format/weeks/format.php
index.php
lib/filelib.php
lib/form/editor.php
lib/form/filemanager.php
mod/forum/lib.php
mod/forum/post.php

index 86c9574dab0946bffe5c46ed837a295507a52f5b..2f08c1e1a7cd38e789ed2c853fa1b6981e9fb697 100644 (file)
@@ -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");
index 0356a611aee9200c9f392f437631499b0aedce9e..c1cdd512baddd3de03eb9ce1816164136b0d8a73 100644 (file)
         echo '<div class="summary">';
 
         $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);
index 937542cf044cb7063a8f206d77af5a5b2a0ce0b7..4eaa588c3afd32eb7b3191ac92a8f7cbf5288fd2 100644 (file)
         echo '<div class="summary">';
 
         $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);
index d0e8f5d75d269def920a25bd5ae3a8af85714e78..a619fafeb0a5614c835f752c68d5de6a9b38becb 100644 (file)
--- a/index.php
+++ b/index.php
             }
 
             $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;
 
index 50aa5e44c9cdd43ecb221093e93ae1d60e20dc75..4050407a3f6c1f43c8f8b32fffd026013d3df536 100644 (file)
@@ -1,6 +1,37 @@
 <?php //$Id$
 
-define('BYTESERVING_BOUNDARY', 's1k2o3d4a5k6s7'); //unique string constant
+///////////////////////////////////////////////////////////////////////////
+//                                                                       //
+// NOTICE OF COPYRIGHT                                                   //
+//                                                                       //
+// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
+//          http://moodle.org                                            //
+//                                                                       //
+// Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com     //
+//                                                                       //
+// This program is free software; you can redistribute it and/or modify  //
+// it under the terms of the GNU General Public License as published by  //
+// the Free Software Foundation; either version 2 of the License, or     //
+// (at your option) any later version.                                   //
+//                                                                       //
+// This program is distributed in the hope that it will be useful,       //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
+// GNU General Public License for more details:                          //
+//                                                                       //
+//          http://www.gnu.org/copyleft/gpl.html                         //
+//                                                                       //
+///////////////////////////////////////////////////////////////////////////
+
+/**
+ * Functions for file handling.
+ *
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @package files
+ */
+
+/** @var string unique string constant. */
+define('BYTESERVING_BOUNDARY', 's1k2o3d4a5k6s7');
 
 require_once("$CFG->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 <type> $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 {
index 46afcae474b35d8990f79f97a00cb98a25af1c24..f7f3321717479e332a1ca5f5a52895a4c87570d7 100644 (file)
@@ -157,7 +157,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element {
             $str .= '</select>';
         } else {
             // no changes of format allowed
-            $str .= '<input type="hidden" name="'.$elname.'[format]" value="'.s($formats[$format]).'" />';
+            $str .= '<input type="hidden" name="'.$elname.'[format]" value="' . $format . '" />';
             $str .= $formats[$format];
         }
         $str .= '</div>';
@@ -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 .= '<div><input type="hidden" name="'.$elname.'[itemid]" value="'.$draftitemid.'" /></div>';
index 6d375ab7ee5e3cfe464a7c61dd511d02ecea908e..8b5471811190d954b9577cc6a63dd09e8f46bc3d 100644 (file)
@@ -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();
         }
 
index 9dc282e8aafdf41d39879e2e84e2299adac844b4..cd40af8f3636e8b3e6aa8822dea2cfdf42249a55 100644 (file)
@@ -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
index a5f9e2de55489ccff20456c1273818a4c45285af..fd184730b8e1295e15f3706d4e02a0a7e6613207 100644 (file)
@@ -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);
 
     $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!
 
         $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,