Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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\combined_document;
  27  use \assignfeedback_editpdf\page_editor;
  28  use \assignfeedback_editpdf\comments_quick_list;
  29  
  30  define('AJAX_SCRIPT', true);
  31  
  32  require('../../../../config.php');
  33  require_once($CFG->dirroot . '/mod/assign/locallib.php');
  34  
  35  require_sesskey();
  36  
  37  $action = optional_param('action', '', PARAM_ALPHANUM);
  38  $assignmentid = required_param('assignmentid', PARAM_INT);
  39  $userid = required_param('userid', PARAM_INT);
  40  $attemptnumber = required_param('attemptnumber', PARAM_INT);
  41  $readonly = optional_param('readonly', false, PARAM_BOOL);
  42  
  43  $cm = \get_coursemodule_from_instance('assign', $assignmentid, 0, false, MUST_EXIST);
  44  $context = \context_module::instance($cm->id);
  45  
  46  $assignment = new \assign($context, null, null);
  47  
  48  require_login($assignment->get_course(), false, $cm);
  49  
  50  if (!$assignment->can_view_submission($userid)) {
  51      print_error('nopermission');
  52  }
  53  
  54  if ($action === 'pollconversions') {
  55      $draft = true;
  56      if (!has_capability('mod/assign:grade', $context)) {
  57          // A student always sees the readonly version.
  58          $readonly = true;
  59          $draft = false;
  60          require_capability('mod/assign:submit', $context);
  61      }
  62  
  63      if ($readonly) {
  64          // Whoever is viewing the readonly version should not use the drafts, but the actual annotations.
  65          $draft = false;
  66      }
  67  
  68      $response = (object) [
  69              'status' => -1,
  70              'filecount' => 0,
  71              'pagecount' => 0,
  72              'pageready' => 0,
  73              'partial' => false,
  74              'pages' => [],
  75          ];
  76  
  77      $combineddocument = document_services::get_combined_document_for_attempt($assignment, $userid, $attemptnumber);
  78      $response->status = $combineddocument->get_status();
  79      $response->filecount = $combineddocument->get_document_count();
  80  
  81      $readystatuslist = [combined_document::STATUS_READY, combined_document::STATUS_READY_PARTIAL];
  82      $completestatuslist = [combined_document::STATUS_COMPLETE, combined_document::STATUS_FAILED];
  83  
  84      if (in_array($response->status, $readystatuslist)) {
  85          // It seems that the files for this submission haven't been combined by the
  86          // "\assignfeedback_editpdf\task\convert_submissions" scheduled task.
  87          // Try to combine them in the user session.
  88          $combineddocument = document_services::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
  89          $response->status = $combineddocument->get_status();
  90          $response->filecount = $combineddocument->get_document_count();
  91  
  92          // Check status of the combined document and remove the submission
  93          // from the task queue if combination completed.
  94          if (in_array($response->status, $completestatuslist)) {
  95              $submission = $assignment->get_user_submission($userid, false, $attemptnumber);
  96              if ($submission) {
  97                  $DB->delete_records('assignfeedback_editpdf_queue', array('submissionid' => $submission->id));
  98              }
  99          }
 100      }
 101  
 102      if (in_array($response->status, $completestatuslist)) {
 103          $pages = document_services::get_page_images_for_attempt($assignment,
 104                                                                  $userid,
 105                                                                  $attemptnumber,
 106                                                                  $readonly);
 107  
 108          $response->pagecount = $combineddocument->get_page_count();
 109  
 110          $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
 111  
 112          // The readonly files are stored in a different file area.
 113          $filearea = document_services::PAGE_IMAGE_FILEAREA;
 114          if ($readonly) {
 115              $filearea = document_services::PAGE_IMAGE_READONLY_FILEAREA;
 116          }
 117          $response->partial = $combineddocument->is_partial_conversion();
 118  
 119          foreach ($pages as $id => $pagefile) {
 120              $index = count($response->pages);
 121              $page = new stdClass();
 122              $comments = page_editor::get_comments($grade->id, $index, $draft);
 123              $page->url = moodle_url::make_pluginfile_url($context->id,
 124                                                          'assignfeedback_editpdf',
 125                                                          $filearea,
 126                                                          $grade->id,
 127                                                          '/',
 128                                                          $pagefile->get_filename())->out();
 129              $page->comments = $comments;
 130              if ($imageinfo = $pagefile->get_imageinfo()) {
 131                  $page->width = $imageinfo['width'];
 132                  $page->height = $imageinfo['height'];
 133              } else {
 134                  $page->width = 0;
 135                  $page->height = 0;
 136              }
 137              $annotations = page_editor::get_annotations($grade->id, $index, $draft);
 138              $page->annotations = $annotations;
 139              $response->pages[] = $page;
 140          }
 141  
 142          $component = 'assignfeedback_editpdf';
 143          $filearea = document_services::PAGE_IMAGE_FILEAREA;
 144          $filepath = '/';
 145          $fs = get_file_storage();
 146          $files = $fs->get_directory_files($context->id, $component, $filearea, $grade->id, $filepath);
 147          $response->pageready = count($files);
 148      }
 149  
 150      echo json_encode($response);
 151      die();
 152  } else if ($action == 'savepage') {
 153      require_capability('mod/assign:grade', $context);
 154  
 155      $response = new stdClass();
 156      $response->errors = array();
 157  
 158      $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
 159  
 160      $pagejson = required_param('page', PARAM_RAW);
 161      $page = json_decode($pagejson);
 162      $index = required_param('index', PARAM_INT);
 163  
 164      $added = page_editor::set_comments($grade->id, $index, $page->comments);
 165      if ($added != count($page->comments)) {
 166          array_push($response->errors, get_string('couldnotsavepage', 'assignfeedback_editpdf', $index+1));
 167      }
 168      $added = page_editor::set_annotations($grade->id, $index, $page->annotations);
 169      if ($added != count($page->annotations)) {
 170          array_push($response->errors, get_string('couldnotsavepage', 'assignfeedback_editpdf', $index+1));
 171      }
 172      echo json_encode($response);
 173      die();
 174  
 175  } else if ($action == 'generatepdf') {
 176  
 177      require_capability('mod/assign:grade', $context);
 178      $response = new stdClass();
 179      $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
 180      $file = document_services::generate_feedback_document($assignment, $userid, $attemptnumber);
 181  
 182      $response->url = '';
 183      if ($file) {
 184          $url = moodle_url::make_pluginfile_url($assignment->get_context()->id,
 185                                                 'assignfeedback_editpdf',
 186                                                 document_services::FINAL_PDF_FILEAREA,
 187                                                 $grade->id,
 188                                                 '/',
 189                                                 $file->get_filename(),
 190                                                 false);
 191          $response->url = $url->out(true);
 192          $response->filename = $file->get_filename();
 193      }
 194  
 195      echo json_encode($response);
 196      die();
 197  } else if ($action == 'loadquicklist') {
 198      require_capability('mod/assign:grade', $context);
 199  
 200      $result = comments_quick_list::get_comments();
 201  
 202      echo json_encode($result);
 203      die();
 204  
 205  } else if ($action == 'addtoquicklist') {
 206      require_capability('mod/assign:grade', $context);
 207  
 208      $comment = required_param('commenttext', PARAM_RAW);
 209      $width = required_param('width', PARAM_INT);
 210      $colour = required_param('colour', PARAM_ALPHA);
 211  
 212      $result = comments_quick_list::add_comment($comment, $width, $colour);
 213  
 214      echo json_encode($result);
 215      die();
 216  } else if ($action == 'revertchanges') {
 217      require_capability('mod/assign:grade', $context);
 218  
 219      $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
 220  
 221      $result = page_editor::revert_drafts($gradeid);
 222  
 223      echo json_encode($result);
 224      die();
 225  } else if ($action == 'removefromquicklist') {
 226      require_capability('mod/assign:grade', $context);
 227  
 228      $commentid = required_param('commentid', PARAM_INT);
 229  
 230      $result = comments_quick_list::remove_comment($commentid);
 231  
 232      echo json_encode($result);
 233      die();
 234  } else if ($action == 'deletefeedbackdocument') {
 235      require_capability('mod/assign:grade', $context);
 236  
 237      $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
 238      $result = document_services::delete_feedback_document($assignment, $userid, $attemptnumber);
 239  
 240      $result = $result && page_editor::unrelease_drafts($grade->id);
 241      echo json_encode($result);
 242      die();
 243  } else if ($action == 'rotatepage') {
 244      require_capability('mod/assign:grade', $context);
 245      $response = new stdClass();
 246      $index = required_param('index', PARAM_INT);
 247      $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
 248      $rotateleft = required_param('rotateleft', PARAM_BOOL);
 249      $filearea = document_services::PAGE_IMAGE_FILEAREA;
 250      $pagefile = document_services::rotate_page($assignment, $userid, $attemptnumber, $index, $rotateleft);
 251      $page = new stdClass();
 252      $page->url = moodle_url::make_pluginfile_url($context->id, document_services::COMPONENT, $filearea,
 253          $grade->id, '/', $pagefile->get_filename())->out();
 254      if ($imageinfo = $pagefile->get_imageinfo()) {
 255          $page->width = $imageinfo['width'];
 256          $page->height = $imageinfo['height'];
 257      } else {
 258          $page->width = 0;
 259          $page->height = 0;
 260      }
 261      $response = (object)['page' => $page];
 262      echo json_encode($response);
 263      die();
 264  }
 265