MDL-23787 added possibility to embed images into textarea fields in database activity

This commit is contained in:
Marina Glancy 2012-02-23 14:30:26 +08:00
parent c4a12afaf9
commit 292077063e
3 changed files with 137 additions and 18 deletions

View file

@ -28,6 +28,27 @@ class data_field_textarea extends data_field_base {
var $type = 'textarea';
/**
* Returns options for embedded files
*
* @return array
*/
private function get_options() {
if (!isset($this->field->param5)) {
$this->field->param5 = 0;
}
$options = array();
$options['trusttext'] = false;
$options['forcehttps'] = false;
$options['subdirs'] = false;
$options['maxfiles'] = -1;
$options['context'] = $this->context;
$options['maxbytes'] = $this->field->param5;
$options['changeformat'] = 0;
$options['noclean'] = false;
return $options;
}
function display_add_field($recordid=0) {
global $CFG, $DB, $OUTPUT, $PAGE;
@ -37,27 +58,63 @@ class data_field_textarea extends data_field_base {
$str = '<div title="'.$this->field->description.'">';
editors_head_setup();
$options = array();
$options['trusttext'] = false;
$options['forcehttps'] = false;
$options['subdirs'] = false;
$options['maxfiles'] = 0;
$options['maxbytes'] = 0;
$options['changeformat'] = 0;
$options['noclean'] = false;
$options = $this->get_options();
$itemid = $this->field->id;
$field = 'field_'.$itemid;
if ($recordid && $content = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))){
$text = $content->content;
$format = $content->content1;
$text = clean_text($text, $format);
} else if (can_use_html_editor()) {
$format = FORMAT_HTML;
$text = clean_text($content->content, $format);
$text = file_prepare_draft_area($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $text);
} else {
$format = FORMAT_PLAIN;
$draftitemid = file_get_unused_draft_itemid();
if (can_use_html_editor()) {
$format = FORMAT_HTML;
} else {
$format = FORMAT_PLAIN;
}
}
// get filepicker info
//
$fpoptions = array();
if ($options['maxfiles'] != 0 ) {
$args = new stdClass();
// need these three to filter repositories list
$args->accepted_types = array('image');
$args->return_types = (FILE_INTERNAL | FILE_EXTERNAL);
$args->context = $this->context;
$args->env = 'filepicker';
// advimage plugin
$image_options = initialise_filepicker($args);
$image_options->context = $this->context;
$image_options->client_id = uniqid();
$image_options->maxbytes = $options['maxbytes'];
$image_options->env = 'editor';
$image_options->itemid = $draftitemid;
// moodlemedia plugin
$args->accepted_types = array('video', 'audio');
$media_options = initialise_filepicker($args);
$media_options->context = $this->context;
$media_options->client_id = uniqid();
$media_options->maxbytes = $options['maxbytes'];
$media_options->env = 'editor';
$media_options->itemid = $draftitemid;
// advlink plugin
$args->accepted_types = '*';
$link_options = initialise_filepicker($args);
$link_options->context = $this->context;
$link_options->client_id = uniqid();
$link_options->maxbytes = $options['maxbytes'];
$link_options->env = 'editor';
$link_options->itemid = $draftitemid;
$fpoptions['image'] = $image_options;
$fpoptions['media'] = $media_options;
$fpoptions['link'] = $link_options;
}
$editor = editors_get_preferred_editor($format);
@ -66,7 +123,8 @@ class data_field_textarea extends data_field_base {
foreach ($formats as $fid) {
$formats[$fid] = $strformats[$fid];
}
$editor->use_editor($field, $options);
$editor->use_editor($field, $options, $fpoptions);
$str .= '<input type="hidden" name="'.$field.'_itemid" value="'.$draftitemid.'" />';
$str .= '<div><textarea id="'.$field.'" name="'.$field.'" rows="'.$this->field->param3.'" cols="'.$this->field->param2.'">'.s($text).'</textarea></div>';
$str .= '<div><select name="'.$field.'_content1">';
foreach ($formats as $key=>$desc) {
@ -111,17 +169,68 @@ class data_field_textarea extends data_field_base {
$names = explode('_', $name);
if (!empty($names[2])) {
$content->$names[2] = clean_param($value, PARAM_NOTAGS); // content[1-4]
if ($names[2] == 'itemid') {
// the value will be retrieved by file_get_submitted_draft_itemid, do not need to save in DB
return true;
} else {
$content->$names[2] = clean_param($value, PARAM_NOTAGS); // content[1-4]
}
} else {
$content->content = clean_param($value, PARAM_CLEAN);
}
if ($oldcontent = $DB->get_record('data_content', array('fieldid'=>$this->field->id, 'recordid'=>$recordid))) {
$content->id = $oldcontent->id;
return $DB->update_record('data_content', $content);
} else {
return $DB->insert_record('data_content', $content);
$content->id = $DB->insert_record('data_content', $content);
if (!$content->id) {
return false;
}
}
if (!empty($content->content)) {
$draftitemid = file_get_submitted_draft_itemid('field_'. $this->field->id. '_itemid');
$options = $this->get_options();
$content->content = file_save_draft_area_files($draftitemid, $this->context->id, 'mod_data', 'content', $content->id, $options, $content->content);
}
$rv = $DB->update_record('data_content', $content);
return $rv;
}
/**
* Display the content of the field in browse mode
*
* @param int $recordid
* @param object $template
* @return bool|string
*/
function display_browse_field($recordid, $template) {
global $DB;
if ($content = $DB->get_record('data_content', array('fieldid' => $this->field->id, 'recordid' => $recordid))) {
if (isset($content->content)) {
$options = new stdClass();
if ($this->field->param1 == '1') { // We are autolinking this field, so disable linking within us
$options->filter = false;
}
$options->para = false;
$str = file_rewrite_pluginfile_urls($content->content, 'pluginfile.php', $this->context->id, 'mod_data', 'content', $content->id, $this->get_options());
$str = format_text($str, $content->content1, $options);
} else {
$str = '';
}
return $str;
}
return false;
}
/**
* Whether this module support files
*
* @param string $relativepath
* @return bool
*/
function file_ok($relativepath) {
return true;
}
}

View file

@ -24,4 +24,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['maxbytes'] = 'Maximum embedded file size (bytes)';
$string['maxbytes_desc'] = 'If set to zero will be unlimited by default';
$string['pluginname'] = 'Text area';

View file

@ -38,5 +38,13 @@
?> /> <?php echo get_string('rows', 'data'); ?>
</td>
</tr>
<tr>
<td class="c0"><label for="param5"><?php echo get_string('maxbytes', 'datafield_textarea'); ?></label></td>
<td class="c1">
<input class="c1" type="text" name="param5" id="param5"
value="<?php if (isset($this->field->param5)) { p($this->field->param5); } else { echo '0'; } ?>" />
<?php echo get_string('maxbytes_desc', 'datafield_textarea'); ?>
</td>
</tr>
</table>
<input type="hidden" name="param4" id="param4" value="<?php echo FORMAT_HTML; ?>" />