[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/editor/atto/ -> autosave-ajax.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Save and load draft text while a user is still editing a form.
  19   *
  20   * @package    editor_atto
  21   * @copyright  2014 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  define('AJAX_SCRIPT', true);
  26  
  27  require_once(dirname(__FILE__) . '/../../../config.php');
  28  
  29  $contextid = required_param('contextid', PARAM_INT);
  30  $elementid = required_param('elementid', PARAM_ALPHANUMEXT);
  31  $pagehash = required_param('pagehash', PARAM_ALPHANUMEXT);
  32  $pageinstance = required_param('pageinstance', PARAM_ALPHANUMEXT);
  33  $now = time();
  34  // This is the oldest time any autosave text will be recovered from.
  35  // This is so that there is a good chance the draft files will still exist (there are many variables so
  36  // this is impossible to guarantee).
  37  $before = $now - 60*60*24*4;
  38  
  39  list($context, $course, $cm) = get_context_info_array($contextid);
  40  $PAGE->set_url('/lib/editor/atto/autosave-ajax.php');
  41  $PAGE->set_context($context);
  42  
  43  require_login($course, false, $cm);
  44  require_sesskey();
  45  
  46  $action = required_param('action', PARAM_ALPHA);
  47  
  48  $response = array();
  49  
  50  if ($action === 'save') {
  51      $drafttext = required_param('drafttext', PARAM_RAW);
  52      $params = array('elementid' => $elementid,
  53                      'userid' => $USER->id,
  54                      'pagehash' => $pagehash,
  55                      'contextid' => $contextid);
  56  
  57      $record = $DB->get_record('editor_atto_autosave', $params);
  58      if ($record && $record->pageinstance != $pageinstance) {
  59          print_error('concurrent access from the same user is not supported');
  60          die();
  61      }
  62  
  63      if (!$record) {
  64          $record = new stdClass();
  65          $record->elementid = $elementid;
  66          $record->userid = $USER->id;
  67          $record->pagehash = $pagehash;
  68          $record->contextid = $contextid;
  69          $record->drafttext = $drafttext;
  70          $record->pageinstance = $pageinstance;
  71          $record->timemodified = $now;
  72  
  73          $DB->insert_record('editor_atto_autosave', $record);
  74  
  75          // No response means no error.
  76          die();
  77      } else {
  78          $record->drafttext = $drafttext;
  79          $record->timemodified = time();
  80          $DB->update_record('editor_atto_autosave', $record);
  81  
  82          // No response means no error.
  83          die();
  84      }
  85  } else if ($action == 'resume') {
  86      $params = array('elementid' => $elementid,
  87                      'userid' => $USER->id,
  88                      'pagehash' => $pagehash,
  89                      'contextid' => $contextid);
  90  
  91      $newdraftid = required_param('draftid', PARAM_INT);
  92  
  93      $record = $DB->get_record('editor_atto_autosave', $params);
  94  
  95      if (!$record) {
  96          $record = new stdClass();
  97          $record->elementid = $elementid;
  98          $record->userid = $USER->id;
  99          $record->pagehash = $pagehash;
 100          $record->contextid = $contextid;
 101          $record->pageinstance = $pageinstance;
 102          $record->pagehash = $pagehash;
 103          $record->draftid = $newdraftid;
 104          $record->timemodified = time();
 105          $record->drafttext = '';
 106  
 107          $DB->insert_record('editor_atto_autosave', $record);
 108  
 109          // No response means no error.
 110          die();
 111      } else {
 112          // Copy all draft files from the old draft area.
 113          $usercontext = context_user::instance($USER->id);
 114          $stale = $record->timemodified < $before;
 115          require_once($CFG->libdir . '/filelib.php');
 116  
 117          // This function copies all the files in one draft area, to another area (in this case it's
 118          // another draft area). It also rewrites the text to @@PLUGINFILE@@ links.
 119          $newdrafttext = file_save_draft_area_files($record->draftid,
 120                                                     $usercontext->id,
 121                                                     'user',
 122                                                     'draft',
 123                                                     $newdraftid,
 124                                                     array(),
 125                                                     $record->drafttext);
 126  
 127          // Final rewrite to the new draft area (convert the @@PLUGINFILES@@ again).
 128          $newdrafttext = file_rewrite_pluginfile_urls($newdrafttext,
 129                                                       'draftfile.php',
 130                                                       $usercontext->id,
 131                                                       'user',
 132                                                       'draft',
 133                                                       $newdraftid);
 134          $record->drafttext = $newdrafttext;
 135  
 136          $record->pageinstance = $pageinstance;
 137          $record->draftid = $newdraftid;
 138          $record->timemodified = time();
 139          $DB->update_record('editor_atto_autosave', $record);
 140  
 141          // A response means the draft has been restored and here is the auto-saved text.
 142          if (!$stale) {
 143              $response['result'] = $record->drafttext;
 144              echo json_encode($response);
 145          }
 146          die();
 147      }
 148  } else if ($action == 'reset') {
 149      $params = array('elementid' => $elementid,
 150                      'userid' => $USER->id,
 151                      'pagehash' => $pagehash,
 152                      'contextid' => $contextid);
 153  
 154      $DB->delete_records('editor_atto_autosave', $params);
 155      die();
 156  }
 157  
 158  print_error('invalidarguments');


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1