[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/assign/feedback/editpdf/ -> 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   * Process ajax requests
  19   *
  20   * @package assignfeedback_editpdf
  21   * @copyright  2012 Davo Smith
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  use \assignfeedback_editpdf\document_services;
  26  use \assignfeedback_editpdf\page_editor;
  27  use \assignfeedback_editpdf\comments_quick_list;
  28  
  29  define('AJAX_SCRIPT', true);
  30  
  31  require('../../../../config.php');
  32  require_once($CFG->dirroot . '/mod/assign/locallib.php');
  33  
  34  require_sesskey();
  35  
  36  $action = optional_param('action', '', PARAM_ALPHANUM);
  37  $assignmentid = required_param('assignmentid', PARAM_INT);
  38  $userid = required_param('userid', PARAM_INT);
  39  $attemptnumber = required_param('attemptnumber', PARAM_INT);
  40  $readonly = optional_param('readonly', false, PARAM_BOOL);
  41  
  42  $cm = \get_coursemodule_from_instance('assign', $assignmentid, 0, false, MUST_EXIST);
  43  $context = \context_module::instance($cm->id);
  44  
  45  $assignment = new \assign($context, null, null);
  46  
  47  require_login($assignment->get_course(), false, $cm);
  48  
  49  if (!$assignment->can_view_submission($userid)) {
  50      print_error('nopermission');
  51  }
  52  
  53  if ($action == 'loadallpages') {
  54      $draft = true;
  55      if (!has_capability('mod/assign:grade', $context)) {
  56          $draft = false;
  57          $readonly = true; // A student always sees the readonly version.
  58          require_capability('mod/assign:submit', $context);
  59      }
  60  
  61      // Whoever is viewing the readonly version should not use the drafts, but the actual annotations.
  62      if ($readonly) {
  63          $draft = false;
  64      }
  65  
  66      $pages = document_services::get_page_images_for_attempt($assignment,
  67                                                              $userid,
  68                                                              $attemptnumber,
  69                                                              $readonly);
  70  
  71      $response = new stdClass();
  72      $response->pagecount = count($pages);
  73      $response->pages = array();
  74  
  75      $grade = $assignment->get_user_grade($userid, true);
  76  
  77      // The readonly files are stored in a different file area.
  78      $filearea = document_services::PAGE_IMAGE_FILEAREA;
  79      if ($readonly) {
  80          $filearea = document_services::PAGE_IMAGE_READONLY_FILEAREA;
  81      }
  82  
  83      foreach ($pages as $id => $pagefile) {
  84          $index = count($response->pages);
  85          $page = new stdClass();
  86          $comments = page_editor::get_comments($grade->id, $index, $draft);
  87          $page->url = moodle_url::make_pluginfile_url($context->id,
  88                                                       'assignfeedback_editpdf',
  89                                                       $filearea,
  90                                                       $grade->id,
  91                                                       '/',
  92                                                       $pagefile->get_filename())->out();
  93          $page->comments = $comments;
  94          $annotations = page_editor::get_annotations($grade->id, $index, $draft);
  95          $page->annotations = $annotations;
  96          array_push($response->pages, $page);
  97      }
  98  
  99      echo json_encode($response);
 100      die();
 101  } else if ($action == 'savepage') {
 102      require_capability('mod/assign:grade', $context);
 103  
 104      $response = new stdClass();
 105      $response->errors = array();
 106  
 107      $grade = $assignment->get_user_grade($userid, true);
 108  
 109      $pagejson = required_param('page', PARAM_RAW);
 110      $page = json_decode($pagejson);
 111      $index = required_param('index', PARAM_INT);
 112  
 113      $added = page_editor::set_comments($grade->id, $index, $page->comments);
 114      if ($added != count($page->comments)) {
 115          array_push($response->errors, get_string('couldnotsavepage', 'assignfeedback_editpdf', $index+1));
 116      }
 117      $added = page_editor::set_annotations($grade->id, $index, $page->annotations);
 118      if ($added != count($page->annotations)) {
 119          array_push($response->errors, get_string('couldnotsavepage', 'assignfeedback_editpdf', $index+1));
 120      }
 121      echo json_encode($response);
 122      die();
 123  
 124  } else if ($action == 'generatepdf') {
 125  
 126      require_capability('mod/assign:grade', $context);
 127      $response = new stdClass();
 128      $grade = $assignment->get_user_grade($userid, true);
 129      $file = document_services::generate_feedback_document($assignment, $userid, $attemptnumber);
 130  
 131      $response->url = '';
 132      if ($file) {
 133          $url = moodle_url::make_pluginfile_url($assignment->get_context()->id,
 134                                                 'assignfeedback_editpdf',
 135                                                 document_services::FINAL_PDF_FILEAREA,
 136                                                 $grade->id,
 137                                                 '/',
 138                                                 $file->get_filename(),
 139                                                 false);
 140          $response->url = $url->out(true);
 141          $response->filename = $file->get_filename();
 142      }
 143  
 144      echo json_encode($response);
 145      die();
 146  } else if ($action == 'loadquicklist') {
 147      require_capability('mod/assign:grade', $context);
 148  
 149      $result = comments_quick_list::get_comments();
 150  
 151      echo json_encode($result);
 152      die();
 153  
 154  } else if ($action == 'addtoquicklist') {
 155      require_capability('mod/assign:grade', $context);
 156  
 157      $comment = required_param('commenttext', PARAM_RAW);
 158      $width = required_param('width', PARAM_INT);
 159      $colour = required_param('colour', PARAM_ALPHA);
 160  
 161      $result = comments_quick_list::add_comment($comment, $width, $colour);
 162  
 163      echo json_encode($result);
 164      die();
 165  } else if ($action == 'revertchanges') {
 166      require_capability('mod/assign:grade', $context);
 167  
 168      $grade = $assignment->get_user_grade($userid, true);
 169  
 170      $result = page_editor::revert_drafts($gradeid);
 171  
 172      echo json_encode($result);
 173      die();
 174  } else if ($action == 'removefromquicklist') {
 175      require_capability('mod/assign:grade', $context);
 176  
 177      $commentid = required_param('commentid', PARAM_INT);
 178  
 179      $result = comments_quick_list::remove_comment($commentid);
 180  
 181      echo json_encode($result);
 182      die();
 183  } else if ($action == 'deletefeedbackdocument') {
 184      require_capability('mod/assign:grade', $context);
 185  
 186      $grade = $assignment->get_user_grade($userid, true);
 187      $result = document_services::delete_feedback_document($assignment, $userid, $attemptnumber);
 188  
 189      $result = $result && page_editor::unrelease_drafts($grade->id);
 190      echo json_encode($result);
 191      die();
 192  }
 193  


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