MDL-18014 Atto autosave: Add a timemodified field to the auto save drafts

Do not restore any draft older than 4 days to (sort of) sync with the draft file
lifetime.
This commit is contained in:
Damyon Wiese 2014-08-19 12:30:13 +08:00
parent 942501f318
commit 9eea856632
4 changed files with 33 additions and 4 deletions

View File

@ -30,6 +30,11 @@ $contextid = required_param('contextid', PARAM_INT);
$elementid = required_param('elementid', PARAM_ALPHANUMEXT);
$pagehash = required_param('pagehash', PARAM_ALPHANUMEXT);
$pageinstance = required_param('pageinstance', PARAM_ALPHANUMEXT);
$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;
list($context, $course, $cm) = get_context_info_array($contextid);
$PAGE->set_url('/lib/editor/atto/autosave-ajax.php');
@ -61,6 +66,7 @@ if ($action === 'save') {
$record->contextid = $contextid;
$record->drafttext = $drafttext;
$record->pageinstance = $pageinstance;
$record->timemodified = $now;
$DB->insert_record('editor_atto_autosave', $record);
@ -68,6 +74,7 @@ if ($action === 'save') {
die();
} else {
$record->drafttext = $drafttext;
$record->timemodified = time();
$DB->update_record('editor_atto_autosave', $record);
// No response means no error.
@ -92,6 +99,7 @@ if ($action === 'save') {
$record->pageinstance = $pageinstance;
$record->pagehash = $pagehash;
$record->draftid = $newdraftid;
$record->timemodified = time();
$record->drafttext = '';
$DB->insert_record('editor_atto_autosave', $record);
@ -101,6 +109,7 @@ if ($action === 'save') {
} else {
// Copy all draft files from the old draft area.
$usercontext = context_user::instance($USER->id);
$stale = $record->timemodified < $before;
require_once($CFG->libdir . '/filelib.php');
// This function copies all the files in one draft area, to another area (in this case it's
@ -124,10 +133,13 @@ if ($action === 'save') {
$record->pageinstance = $pageinstance;
$record->draftid = $newdraftid;
$record->timemodified = time();
$DB->update_record('editor_atto_autosave', $record);
// A response means the draft has been restored and here is the auto-saved text.
echo $record->drafttext;
if (!$stale) {
echo $record->drafttext;
}
die();
}
} else if ($action == 'reset') {

5
lib/editor/atto/db/install.xml Normal file → Executable file
View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/editor/atto/db" VERSION="20140703" COMMENT="XMLDB file for Moodle lib/editor/atto"
<XMLDB PATH="lib/editor/atto/db" VERSION="20140819" COMMENT="XMLDB file for Moodle lib/editor/atto"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
@ -14,6 +14,7 @@
<FIELD NAME="drafttext" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="The draft text"/>
<FIELD NAME="draftid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="Optional draft area id containing draft files."/>
<FIELD NAME="pageinstance" TYPE="char" LENGTH="64" NOTNULL="true" SEQUENCE="false" COMMENT="The browser tab instance that last saved the draft text. This is to prevent multiple tabs from the same user saving different text alternately."/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Store the last modified time for the auto save text."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
@ -21,4 +22,4 @@
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>

View File

@ -81,5 +81,21 @@ function xmldb_editor_atto_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2014081400, 'editor', 'atto');
}
if ($oldversion < 2014081900) {
// Define field timemodified to be added to editor_atto_autosave.
$table = new xmldb_table('editor_atto_autosave');
$field = new xmldb_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'pageinstance');
// Conditionally launch add field timemodified.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Atto savepoint reached.
upgrade_plugin_savepoint(true, 2014081900, 'editor', 'atto');
}
return true;
}

View File

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