Skip to content

Commit

Permalink
MDL-51179 Atto: Extend autosave fix to cover text changes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Damyon Wiese committed Sep 1, 2015
1 parent 6f0dfdd commit 988592c
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/adminlib.php
Expand Up @@ -2319,6 +2319,7 @@ public function output_html($data, $query='') {
}

$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,
Expand Down
3 changes: 2 additions & 1 deletion lib/deprecatedlib.php
Expand Up @@ -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";
Expand Down Expand Up @@ -2378,4 +2379,4 @@ function get_referer($stripquery = true) {
} else {
return '';
}
}
}
58 changes: 58 additions & 0 deletions lib/editor/atto/classes/task/autosave_cleanup_task.php
@@ -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));
}

}
44 changes: 44 additions & 0 deletions lib/editor/atto/db/tasks.php
@@ -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' => '*'
)
);
1 change: 1 addition & 0 deletions lib/editor/atto/lang/en/editor_atto.php
Expand Up @@ -36,6 +36,7 @@
$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.';
Expand Down
4 changes: 3 additions & 1 deletion lib/editor/atto/lib.php
Expand Up @@ -161,6 +161,8 @@ protected function get_init_params($elementid, array $options = null, array $fpo
}
$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,
Expand All @@ -171,7 +173,7 @@ protected function get_init_params($elementid, array $options = null, array $fpo
'directionality' => $directionality,
'filepickeroptions' => array(),
'plugins' => $plugins,
'pageHash' => sha1($PAGE->url)
'pageHash' => $pagehash,
);
if ($fpoptions) {
$params['filepickeroptions'] = $fpoptions;
Expand Down
21 changes: 21 additions & 0 deletions lib/editor/atto/tests/behat/autosave.feature
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion lib/editor/atto/version.php
Expand Up @@ -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).
22 changes: 22 additions & 0 deletions lib/editorlib.php
Expand Up @@ -201,6 +201,28 @@ public abstract function get_preferred_format();
*/
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
Expand Down
1 change: 1 addition & 0 deletions lib/form/editor.php
Expand Up @@ -377,6 +377,7 @@ function toHtml() {
}

// 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'];
Expand Down
1 change: 1 addition & 0 deletions lib/upgrade.txt
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions mod/data/field/textarea/field.class.php
Expand Up @@ -143,6 +143,7 @@ function display_add_field($recordid = 0, $formdata = null) {
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>';
Expand Down
4 changes: 4 additions & 0 deletions mod/data/templates.php
Expand Up @@ -218,6 +218,7 @@
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>';

Expand Down Expand Up @@ -315,6 +316,7 @@
}

$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>';
Expand All @@ -327,6 +329,7 @@
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>';
Expand All @@ -338,6 +341,7 @@
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>';
Expand Down
1 change: 1 addition & 0 deletions question/behaviour/rendererbase.php
Expand Up @@ -82,6 +82,7 @@ public function manual_comment_fields(question_attempt $qa, question_display_opt

$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),
Expand Down
1 change: 1 addition & 0 deletions question/type/essay/renderer.php
Expand Up @@ -236,6 +236,7 @@ public function response_area_input($name, $qa, $step, $lines, $context) {
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));

Expand Down

0 comments on commit 988592c

Please sign in to comment.