]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-19002 new simplified file api file limits; docs are still not finished
authorskodak <skodak>
Tue, 28 Apr 2009 21:20:43 +0000 (21:20 +0000)
committerskodak <skodak>
Tue, 28 Apr 2009 21:20:43 +0000 (21:20 +0000)
lib/filelib.php
mod/glossary/edit.php
mod/glossary/edit_form.php

index 35a4c94d6762d0dd2766bc0a140bfd9b94659fc0..d06deadc2b696cae9fb298180b561a06d64a91ec 100644 (file)
@@ -107,6 +107,168 @@ function get_file_url($path, $options=null, $type='coursefile') {
     return $ffurl;
 }
 
+/**
+ * Prepares standardised text field fro editing with Editor formslib element
+ * @param object $data $database entry field
+ * @param string $field name of data field
+ * @param array $options various options
+ * @param object $context context, required for existing data
+ * @param string $filearea file area name
+ * @param int $itemid item id, required if item exists
+ * @return object modified data object
+ */
+function file_prepare_standard_editor($data, $field, array $options, $context=null, $filearea=null, $itemid=null) {
+    $options = (array)$options;
+    if (!isset($options['trusttext'])) {
+        $options['trusttext'] = false;
+    }
+    if (!isset($options['forcehttps'])) {
+        $options['forcehttps'] = false;
+    }
+    if (!isset($options['subdirs'])) {
+        $options['subdirs'] = false;
+    }
+
+    if (empty($data->id) or empty($context)) {
+        $contextid = null;
+        $data->id = null;
+        if (!isset($data->{$field})) {
+            $data->{$field} = '';
+        }
+        if (!isset($data->{$field.'format'})) {
+            $data->{$field.'format'} = FORMAT_HTML; // TODO: use better default
+        }
+        $data->{$field} = clean_text($data->{$field}, $data->{$field.'format'});
+
+    } else {
+        if ($options['trusttext']) {
+            if (!isset($data->{$field.'trust'})) {
+                $data->{$field.'trust'} = 0;
+            }
+            $data = trusttext_pre_edit($data, $field, $context);
+        } else {
+            $data->{$field} = clean_text($data->{$field}, $data->{$field.'format'});
+        }
+        $contextid = $context->id;
+    }
+
+    $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']);
+    $data->{'editor_'.$field} = array('text'=>$currenttext, 'format'=>$data->{$field.'format'}, 'itemid'=>$draftid_editor);
+
+    return $data;
+}
+
+/**
+ * Prepares editing with File manager formslib element
+ * @param object $data $database entry field
+ * @param string $field name of data field
+ * @param array $options various options
+ * @param object $context context, required for existing data
+ * @param string $filearea file area name
+ * @param int $itemid item id, required if item exists
+ * @return object modified data object
+ */
+function file_postupdate_standard_editor($data, $field, array $options, $context, $filearea, $itemid) {
+    $options = (array)$options;
+    if (!isset($options['trusttext'])) {
+        $options['trusttext'] = false;
+    }
+    if (!isset($options['forcehttps'])) {
+        $options['forcehttps'] = false;
+    }
+    if (!isset($options['subdirs'])) {
+        $options['subdirs'] = false;
+    }
+    if (!isset($options['maxfiles'])) {
+        $options['maxfiles'] = -1; // unlimited
+    }
+    if (!isset($options['maxbytes'])) {
+        $options['maxbytes'] = 0; // unlimited
+    }
+
+    if ($options['trusttext']) {
+        $data->definitiontrust = trusttext_trusted($context);
+    } else {
+        $data->definitiontrust = 0;
+    }
+
+    $editor = $data->{'editor_'.$field};
+
+    $data->{$field} = file_save_draft_area_files($editor['itemid'], $context->id, $filearea, $itemid, $options, $editor['text'], $options['forcehttps']);
+    $data->{$field.'format'} = $editor['format'];
+
+    return $data;
+}
+
+/**
+ * Saves text and files modified by Editor formslib element
+ * @param object $data $database entry field
+ * @param string $field name of data field
+ * @param array $options various options
+ * @param object $context context - must already exist
+ * @param string $filearea file area name
+ * @param int $itemid must already exist, usually means data is in db
+ * @return object modified data obejct
+ */
+function file_prepare_standard_filemanager($data, $field, array $options, $context=null, $filearea=null, $itemid=null) {
+    $options = (array)$options;
+    if (!isset($options['subdirs'])) {
+        $options['subdirs'] = false;
+    }
+    if (empty($data->id) or empty($context)) {
+        $data->id = null;
+        $contextid = null;
+    } else {
+        $contextid = $context->id;
+    }
+
+    $draftid_editor = file_get_submitted_draft_itemid('filemanager_'.$field);
+    file_prepare_draft_area($draftid_editor, $contextid, $filearea, $data->id, $options['subdirs']);
+    $data->{'filemanager_'.$field} = $draftid_editor;
+
+    return $data;
+}
+
+/**
+ * Saves files modified by File manager formslib element
+ * @param object $data $database entry field
+ * @param string $field name of data field
+ * @param array $options various options
+ * @param object $context context - must already exist
+ * @param string $filearea file area name
+ * @param int $itemid must already exist, usually means data is in db
+ * @return object modified data obejct
+ */
+function file_postupdate_standard_filemanager($data, $field, array $options, $context, $filearea, $itemid) {
+    $options = (array)$options;
+    if (!isset($options['subdirs'])) {
+        $options['subdirs'] = false;
+    }
+    if (!isset($options['maxfiles'])) {
+        $options['maxfiles'] = -1; // unlimited
+    }
+    if (!isset($options['maxbytes'])) {
+        $options['maxbytes'] = 0; // unlimited
+    }
+
+    if (empty($data->{'filemanager_'.$field})) {
+        $data->$field = '';
+
+    } else {
+        file_save_draft_area_files($data->{'filemanager_'.$field}, $context->id, $filearea, $data->id, $options);
+        $fs = get_file_storage();
+
+        if ($fs->get_area_files($context->id, $filearea, $itemid)) {
+            $data->$field = '1'; // TODO: this is an ugly hack
+        } else {
+            $data->$field = '';
+        }
+    }
+
+    return $data;
+}
+
 /**
  * @return int a random but available draft itemid that can be used to create a new draft
  * file area.
index fedfa06ba0b6aac6757b419ef0efce69b61b7c0b..ac8c73b3df694892ea50650e0b38ea5574256fa2 100644 (file)
@@ -43,9 +43,6 @@ if ($id) { // if entry is specified
         }
     }
 
-    // clean up text before edit if needed
-    $entry = trusttext_pre_edit($entry, 'definition', $context);
-
     //prepare extra data
     if ($aliases = $DB->get_records_menu("glossary_alias", array("entryid"=>$id), '', 'id, alias')) {
         $entry->aliases = implode("\n", $aliases) . "\n";
@@ -58,20 +55,19 @@ if ($id) { // if entry is specified
 } else { // new entry
     require_capability('mod/glossary:write', $context);
     $entry = new object();
-    $entry->id               = null;
-    $entry->definition       = '';
-    $entry->definitionformat = FORMAT_HTML; // TODO: better default value
+    $entry->id = null;
 }
 
-$entry->cmid = $cm->id;
+$maxfiles = 99;                // TODO: add some setting
+$maxbytes = $course->maxbytes; // TODO: add some setting
+
+$definitionoptions = array('trusttext'=>true, 'subdirs'=>false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes);
+$attachmentoptions = array('subdirs'=>false, 'maxfiles'=>$maxfiles, 'maxbytes'=>$maxbytes);
 
-$draftid_editor = file_get_submitted_draft_itemid('entry');
-$currenttext = file_prepare_draft_area($draftid_editor, $context->id, 'glossary_entry', $entry->id, true, $entry->definition);
-$entry->entry = array('text'=>$currenttext, 'format'=>$entry->definitionformat, 'itemid'=>$draftid_editor);
+$entry = file_prepare_standard_editor($entry, 'definition', $definitionoptions, $context, 'glossary_entry', $entry->id);
+$entry = file_prepare_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'glossary_attachment', $entry->id);
 
-$draftitemid = file_get_submitted_draft_itemid('attachments');
-file_prepare_draft_area($draftitemid, $context->id, 'glossary_attachment', $entry->id , false);
-$entry->attachments = $draftitemid;
+$entry->cmid = $cm->id;
 
 // set form initial data
 $mform->set_data($entry);
@@ -84,9 +80,14 @@ if ($mform->is_cancelled()){
         redirect("view.php?id=$cm->id");
     }
 
-} else if ($data = $mform->get_data()) {
+} else if ($entry = $mform->get_data()) {
     $timenow = time();
 
+    $categories = empty($entry->categories) ? array() : $entry->categories;
+    unset($entry->categories);
+    $aliases = trim($entry->aliases);
+    unset($entry->aliases);
+
     if (empty($entry->id)) {
         $entry->glossaryid       = $glossary->id;
         $entry->timecreated      = $timenow;
@@ -96,15 +97,15 @@ if ($mform->is_cancelled()){
         $entry->teacherentry     = has_capability('mod/glossary:manageentries', $context);
     }
 
-    $entry->concept          = trim($data->concept);
+    $entry->concept          = trim($entry->concept);
     $entry->definition       = '';          // updated later
     $entry->definitionformat = FORMAT_HTML; // updated later
-    $entry->definitiontrust  = trusttext_trusted($context);
+    $entry->definitiontrust  = 0;           // updated later
     $entry->timemodified     = $timenow;
     $entry->approved         = 0;
-    $entry->usedynalink      = isset($data->usedynalink) ?   $data->usedynalink : 0;
-    $entry->casesensitive    = isset($data->casesensitive) ? $data->casesensitive : 0;
-    $entry->fullmatch        = isset($data->fullmatch) ?     $data->fullmatch : 0;
+    $entry->usedynalink      = isset($entry->usedynalink) ?   $entry->usedynalink : 0;
+    $entry->casesensitive    = isset($entry->casesensitive) ? $entry->casesensitive : 0;
+    $entry->fullmatch        = isset($entry->fullmatch) ?     $entry->fullmatch : 0;
 
     if ($glossary->defaultapproval or has_capability('mod/glossary:approve', $context)) {
         $entry->approved = 1;
@@ -124,16 +125,11 @@ if ($mform->is_cancelled()){
                    $entry->id, $cm->id);
     }
 
-    // save and relink embedded images
-    $entry->definitionformat = $data->entry['format'];
-    $entry->definition       = file_save_draft_area_files($draftid_editor, $context->id, 'glossary_entry', $entry->id, array('subdirs'=>true), $data->entry['text']);
-
-    // save attachments
-    $info = file_get_draft_area_info($draftitemid);
-    $entry->attachment = ($info['filecount']>0) ? '1' : '';
-    file_save_draft_area_files($draftitemid, $context->id, 'glossary_attachment', $entry->id);
+    // save and relink embedded images and save attachments
+    $entry = file_postupdate_standard_editor($entry, 'definition', $definitionoptions, $context, 'glossary_entry', $entry->id);
+    $entry = file_postupdate_standard_filemanager($entry, 'attachment', $attachmentoptions, $context, 'glossary_attachment', $entry->id);
 
-    // store the final values
+    // store the updated value values
     $DB->update_record('glossary_entries', $entry);
 
     //refetch complete entry
@@ -142,8 +138,8 @@ if ($mform->is_cancelled()){
     // update entry categories
     $DB->delete_records('glossary_entries_categories', array('entryid'=>$entry->id));
     // TODO: this deletes cats from both both main and secondary glossary :-(
-    if (!empty($data->categories) and array_search(0, $data->categories) === false) {
-        foreach ($data->categories as $catid) {
+    if (!empty($categories) and array_search(0, $categories) === false) {
+        foreach ($categories as $catid) {
             $newcategory = new object();
             $newcategory->entryid    = $entry->id;
             $newcategory->categoryid = $catid;
@@ -153,7 +149,6 @@ if ($mform->is_cancelled()){
 
     // update aliases
     $DB->delete_records('glossary_alias', array('entryid'=>$entry->id));
-    $aliases = trim($data->aliases);
     if ($aliases !== '') {
         $aliases = explode("\n", $aliases);
         foreach ($aliases as $alias) {
index 90b82572894c50d5d1ad2ad90667471e605e30ea..4be22c591498f660d3c5fba12a6ef4acd4dde0e9 100644 (file)
@@ -17,10 +17,10 @@ class mod_glossary_entry_form extends moodleform {
         $mform->setType('concept', PARAM_TEXT);
         $mform->addRule('concept', null, 'required', null, 'client');
 
-        $mform->addElement('editor', 'entry', get_string('definition', 'glossary'), array('maxfiles' => EDITOR_UNLIMITED_FILES));
-        $mform->setType('entry', PARAM_RAW);
-        $mform->addRule('entry', get_string('required'), 'required', null, 'client');
-        $mform->setHelpButton('entry', array('reading', 'writing', 'questions', 'richtext2'), false, 'editorhelpbutton');
+        $mform->addElement('editor', 'editor_definition', get_string('definition', 'glossary'), array('maxfiles' => EDITOR_UNLIMITED_FILES));
+        $mform->setType('editor_definition', PARAM_RAW);
+        $mform->addRule('editor_definition', get_string('required'), 'required', null, 'client');
+        $mform->setHelpButton('editor_definition', array('reading', 'writing', 'questions', 'richtext2'), false, 'editorhelpbutton');
 
         if ($categories = $DB->get_records_menu('glossary_categories', array('glossaryid'=>$glossary->id), 'name ASC', 'id, name')){
             $categories = array(0 => get_string('notcategorised', 'glossary')) + $categories;
@@ -33,14 +33,14 @@ class mod_glossary_entry_form extends moodleform {
         $mform->setType('aliases', PARAM_TEXT);
         $mform->setHelpButton('aliases', array('aliases2', strip_tags(get_string('aliases', 'glossary')), 'glossary'));
 
-        $mform->addElement('filemanager', 'attachments', get_string('attachment', 'glossary'),
+        $mform->addElement('filemanager', 'filemanager_attachment', get_string('attachment', 'glossary'),
             array('subdirs'=>0,
 //                  'maxbytes'=>$glossary->maxbytes,
 //                  'maxfiles'=>-1,
                   'filetypes'=>'*',
                   'returnvalue'=>'ref_id'
             ));
-        $mform->setHelpButton('attachments', array('attachment2', get_string('attachment', 'glossary'), 'glossary'));
+        $mform->setHelpButton('filemanager_attachment', array('attachment2', get_string('attachment', 'glossary'), 'glossary'));
 
         if (!$glossary->usedynalink) {
             $mform->addElement('hidden', 'usedynalink',   $CFG->glossary_linkentries);