MDL-51179 Atto: Extend autosave fix to cover text changes

Added new functions to editor api - set/get_text so the
original form text can be determined from an editor.

When calling use_editor() you should first call set_text() with
the text that will be inserted in the form element.

There is also a new scheduled task for cleaning Atto autosave drafts.
This commit is contained in:
Damyon Wiese 2015-09-01 12:23:00 +08:00
parent 6f0dfdd969
commit 988592c556
15 changed files with 162 additions and 3 deletions

View File

@ -2319,6 +2319,7 @@ class admin_setting_confightmleditor extends admin_setting_configtext {
}
$editor = editors_get_preferred_editor(FORMAT_HTML);
$editor->set_text($data);
$editor->use_editor($this->get_id(), array('noclean'=>true));
return format_admin_setting($this, $this->visiblename,

View File

@ -1003,6 +1003,7 @@ function print_textarea($unused, $rows, $cols, $width, $height, $name, $value=''
editors_head_setup();
$editor = editors_get_preferred_editor(FORMAT_HTML);
$editor->set_text($value);
$editor->use_editor($id, array('legacy'=>true));
$str .= "\n".'<textarea class="form-textarea" id="'. $id .'" name="'. $name .'" rows="'. $rows .'" cols="'. $cols .'" spellcheck="true">'."\n";
@ -2378,4 +2379,4 @@ function get_referer($stripquery = true) {
} else {
return '';
}
}
}

View File

@ -0,0 +1,58 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* A scheduled task.
*
* @package editor_atto
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace editor_atto\task;
use \core\task\scheduled_task;
/**
* Simple task to run the autosave cleanup task.
*/
class autosave_cleanup_task extends scheduled_task {
/**
* Get a descriptive name for this task (shown to admins).
*
* @return string
*/
public function get_name() {
return get_string('taskautosavecleanup', 'editor_atto');
}
/**
* Do the job.
* Throw exceptions on errors (the job will be retried).
*/
public function execute() {
global $DB;
$now = time();
// This is the oldest time any autosave text will be recovered from.
// This is so that there is a good chance the draft files will still exist (there are many variables so
// this is impossible to guarantee).
$before = $now - 60*60*24*4;
$DB->delete_records_select('editor_atto_autosave', 'timemodified < :before', array('before' => $before));
}
}

View File

@ -0,0 +1,44 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Definition of core scheduled tasks.
*
* The handlers defined on this file are processed and registered into
* the Moodle DB after any install or upgrade operation. All plugins
* support this.
*
* @package core
* @category task
* @copyright 2013 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/* List of handlers */
$tasks = array(
array(
'classname' => 'editor_atto\task\autosave_cleanup_task',
'blocking' => 0,
'minute' => 'R',
'hour' => 'R',
'day' => '*',
'dayofweek' => 'R',
'month' => '*'
)
);

View File

@ -36,6 +36,7 @@ $string['pluginname'] = 'Atto HTML editor';
$string['subplugintype_atto'] = 'Atto plugin';
$string['subplugintype_atto_plural'] = 'Atto plugins';
$string['settings'] = 'Atto toolbar settings';
$string['taskautosavecleanup'] = 'Delete expired autosave drafts from the database.';
$string['textrecovered'] = 'A draft version of this text was automatically restored.';
$string['toolbarconfig'] = 'Toolbar config';
$string['toolbarconfig_desc'] = 'The list of plugins and the order they are displayed can be configured here. The configuration consists of groups (one per line) followed by the ordered list of plugins for that group. The group is separated from the plugins with an equals sign and the plugins are separated with commas. The group names must be unique and should indicate what the buttons have in common. Button and group names should not be repeated and may only contain alphanumeric characters.';

View File

@ -161,6 +161,8 @@ class atto_texteditor extends texteditor {
}
$contentcss = $PAGE->theme->editor_css_url()->out(false);
// Note <> is a safe separator, because it will not appear in the output of s().
$pagehash = sha1($PAGE->url . '<>' . s($this->get_text()));
$params = array(
'elementid' => $elementid,
'content_css' => $contentcss,
@ -171,7 +173,7 @@ class atto_texteditor extends texteditor {
'directionality' => $directionality,
'filepickeroptions' => array(),
'plugins' => $plugins,
'pageHash' => sha1($PAGE->url)
'pageHash' => $pagehash,
);
if ($fpoptions) {
$params['filepickeroptions'] = $fpoptions;

View File

@ -75,3 +75,24 @@ Feature: Atto Autosave
And I follow "Course 1"
And I navigate to "Edit settings" node in "Course administration"
Then I should not see "This is my draft"
@javascript
Scenario: Do not restore a draft if text has been modified
Given I log in as "teacher1"
And I follow "Course 1"
And I navigate to "Edit settings" node in "Course administration"
And I set the field "Course summary" to "This is my draft"
# Wait for the autosave
And I wait "5" seconds
And I log out
And I log in as "teacher2"
And I follow "Course 1"
And I navigate to "Edit settings" node in "Course administration"
And I set the field "Course summary" to "Modified text"
And I click on "Save and display" "button"
And I log out
When I log in as "teacher1"
And I follow "Course 1"
And I navigate to "Edit settings" node in "Course administration"
Then I should not see "This is my draft"
And I should see "Modified text"

View File

@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2015051100; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2015051101; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2015050500; // Requires this Moodle version.
$plugin->component = 'editor_atto'; // Full name of the plugin (used for diagnostics).

View File

@ -201,6 +201,28 @@ abstract class texteditor {
*/
public abstract function supports_repositories();
/**
* @var string $text The text set to the editor in the form.
* @since 2.8.8, 2.9.2, 3.0
*/
protected $text = '';
/**
* Set the text set for this form field. Will be called before "use_editor".
* @param string $text The text for the form field.
*/
public function set_text($text) {
$this->text = $text;
}
/**
* Get the text set for this form field. Can be called from "use_editor".
* @return string
*/
public function get_text() {
return $this->text;
}
/**
* Add required JS needed for editor
* @param string $elementid id of text area to be converted to editor

View File

@ -377,6 +377,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element {
}
// print text area - TODO: add on-the-fly switching, size configuration, etc.
$editor->set_text($text);
$editor->use_editor($id, $this->_options, $fpoptions);
$rows = empty($this->_attributes['rows']) ? 15 : $this->_attributes['rows'];

View File

@ -3,6 +3,7 @@ information provided here is intended especially for developers.
=== 3.0 ===
* Users of the text editor API to manually create a text editor should call set_text before calling use_editor.
* get_referer() has been deprecated, please use the get_local_referer function instead.
* \core\progress\null is renamed to \core\progress\none for improved PHP7 compatibility as null is a reserved word (see MDL-50453).
* \webservice_xmlrpc_client now respects proxy server settings. If your XMLRPC server is available on your local network and not via your proxy server, you may need to add it to the list of proxy

View File

@ -143,6 +143,7 @@ class data_field_textarea extends data_field_base {
foreach ($formats as $fid) {
$formats[$fid] = $strformats[$fid];
}
$editor->set_text($text);
$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.'" spellcheck="true">'.s($text).'</textarea></div>';

View File

@ -218,6 +218,7 @@ if ($mode == 'listtemplate'){
echo '<div class="template_heading"><label for="edit-listtemplateheader">'.get_string('header','data').'</label></div>';
$field = 'listtemplateheader';
$editor->set_text($data->listtemplateheader);
$editor->use_editor($field, $options);
echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplateheader).'</textarea></div>';
@ -315,6 +316,7 @@ if ($mode == 'listtemplate'){
}
$field = 'template';
$editor->set_text($data->{$mode});
$editor->use_editor($field, $options);
echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->{$mode}).'</textarea></div>';
echo '</td>';
@ -327,6 +329,7 @@ if ($mode == 'listtemplate'){
echo '<div class="template_heading"><label for="edit-listtemplatefooter">'.get_string('footer','data').'</label></div>';
$field = 'listtemplatefooter';
$editor->set_text($data->listtemplatefooter);
$editor->use_editor($field, $options);
echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->listtemplatefooter).'</textarea></div>';
echo '</td>';
@ -338,6 +341,7 @@ if ($mode == 'listtemplate'){
echo '<div class="template_heading"><label for="edit-rsstitletemplate">'.get_string('rsstitletemplate','data').'</label></div>';
$field = 'rsstitletemplate';
$editor->set_text($data->rsstitletemplate);
$editor->use_editor($field, $options);
echo '<div><textarea id="'.$field.'" name="'.$field.'" rows="15" cols="80">'.s($data->rsstitletemplate).'</textarea></div>';
echo '</td>';

View File

@ -82,6 +82,7 @@ abstract class qbehaviour_renderer extends plugin_renderer_base {
$commenttext = format_text($commenttext, $commentformat, array('para' => false));
$editor->set_text($commenttext);
$editor->use_editor($id, array('context' => $options->context));
$commenteditor = html_writer::tag('div', html_writer::tag('textarea', s($commenttext),

View File

@ -236,6 +236,7 @@ class qtype_essay_format_editor_renderer extends plugin_renderer_base {
list($draftitemid, $response) = $this->prepare_response_for_editing(
$name, $step, $context);
$editor->set_text($response);
$editor->use_editor($id, $this->get_editor_options($context),
$this->get_filepicker_options($context, $draftitemid));