diff --git a/lib/editor/atto/autosave-ajax.php b/lib/editor/atto/autosave-ajax.php index 00572c0c72d..36dcc060760 100644 --- a/lib/editor/atto/autosave-ajax.php +++ b/lib/editor/atto/autosave-ajax.php @@ -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') { diff --git a/lib/editor/atto/db/install.xml b/lib/editor/atto/db/install.xml old mode 100644 new mode 100755 index 7134f35499f..8d17ee419bc --- a/lib/editor/atto/db/install.xml +++ b/lib/editor/atto/db/install.xml @@ -1,5 +1,5 @@ - @@ -14,6 +14,7 @@ + @@ -21,4 +22,4 @@ - + \ No newline at end of file diff --git a/lib/editor/atto/db/upgrade.php b/lib/editor/atto/db/upgrade.php index 9aa772561b5..42ef164067b 100644 --- a/lib/editor/atto/db/upgrade.php +++ b/lib/editor/atto/db/upgrade.php @@ -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; } diff --git a/lib/editor/atto/version.php b/lib/editor/atto/version.php index c3208c368a8..0edaf3b1d0f 100644 --- a/lib/editor/atto/version.php +++ b/lib/editor/atto/version.php @@ -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).