Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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          $combineddocument = document_services::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
  86          $response->status = $combineddocument->get_status();
  87          $response->filecount = $combineddocument->get_document_count();
  88      }
  89  
  90      if (in_array($response->status, $completestatuslist)) {
  91          $pages = document_services::get_page_images_for_attempt($assignment,
  92                                                                  $userid,
  93                                                                  $attemptnumber,
  94                                                                  $readonly);
  95  
  96          $response->pagecount = $combineddocument->get_page_count();
  97  
  98          $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
  99  
 100          // The readonly files are stored in a different file area.
 101          $filearea = document_services::PAGE_IMAGE_FILEAREA;
 102          if ($readonly) {
 103              $filearea = document_services::PAGE_IMAGE_READONLY_FILEAREA;
 104          }
 105          $response->partial = $combineddocument->is_partial_conversion();
 106  
 107          foreach ($pages as $id => $pagefile) {
 108              $index = count($response->pages);
 109              $page = new stdClass();
 110              $comments = page_editor::get_comments($grade->id, $index, $draft);
 111              $page->url = moodle_url::make_pluginfile_url($context->id,
 112                                                          'assignfeedback_editpdf',
 113                                                          $filearea,
 114                                                          $grade->id,
 115                                                          '/',
 116                                                          $pagefile->get_filename())->out();
 117              $page->comments = $comments;
 118              if ($imageinfo = $pagefile->get_imageinfo()) {
 119                  $page->width = $imageinfo['width'];
 120                  $page->height = $imageinfo['height'];
 121              } else {
 122                  $page->width = 0;
 123                  $page->height = 0;
 124              }
 125              $annotations = page_editor::get_annotations($grade->id, $index, $draft);
 126              $page->annotations = $annotations;
 127              $response->pages[] = $page;
 128          }
 129  
 130          $component = 'assignfeedback_editpdf';
 131          $filearea = document_services::PAGE_IMAGE_FILEAREA;
 132          $filepath = '/';
 133          $fs = get_file_storage();
 134          $files = $fs->get_directory_files($context->id, $component, $filearea, $grade->id, $filepath);
 135          $response->pageready = count($files);
 136      }
 137  
 138      echo json_encode($response);
 139      die();
 140  } else if ($action == 'savepage') {
 141      require_capability('mod/assign:grade', $context);
 142  
 143      $response = new stdClass();
 144      $response->errors = array();
 145  
 146      $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
 147  
 148      $pagejson = required_param('page', PARAM_RAW);
 149      $page = json_decode($pagejson);
 150      $index = required_param('index', PARAM_INT);
 151  
 152      $added = page_editor::set_comments($grade->id, $index, $page->comments);
 153      if ($added != count($page->comments)) {
 154          array_push($response->errors, get_string('couldnotsavepage', 'assignfeedback_editpdf', $index+1));
 155      }
 156      $added = page_editor::set_annotations($grade->id, $index, $page->annotations);
 157      if ($added != count($page->annotations)) {
 158          array_push($response->errors, get_string('couldnotsavepage', 'assignfeedback_editpdf', $index+1));
 159      }
 160      echo json_encode($response);
 161      die();
 162  
 163  } else if ($action == 'generatepdf') {
 164  
 165      require_capability('mod/assign:grade', $context);
 166      $response = new stdClass();
 167      $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
 168      $file = document_services::generate_feedback_document($assignment, $userid, $attemptnumber);
 169  
 170      $response->url = '';
 171      if ($file) {
 172          $url = moodle_url::make_pluginfile_url($assignment->get_context()->id,
 173                                                 'assignfeedback_editpdf',
 174                                                 document_services::FINAL_PDF_FILEAREA,
 175                                                 $grade->id,
 176                                                 '/',
 177                                                 $file->get_filename(),
 178                                                 false);
 179          $response->url = $url->out(true);
 180          $response->filename = $file->get_filename();
 181      }
 182  
 183      echo json_encode($response);
 184      die();
 185  } else if ($action == 'loadquicklist') {
 186      require_capability('mod/assign:grade', $context);
 187  
 188      $result = comments_quick_list::get_comments();
 189  
 190      echo json_encode($result);
 191      die();
 192  
 193  } else if ($action == 'addtoquicklist') {
 194      require_capability('mod/assign:grade', $context);
 195  
 196      $comment = required_param('commenttext', PARAM_RAW);
 197      $width = required_param('width', PARAM_INT);
 198      $colour = required_param('colour', PARAM_ALPHA);
 199  
 200      $result = comments_quick_list::add_comment($comment, $width, $colour);
 201  
 202      echo json_encode($result);
 203      die();
 204  } else if ($action == 'revertchanges') {
 205      require_capability('mod/assign:grade', $context);
 206  
 207      $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
 208  
 209      $result = page_editor::revert_drafts($gradeid);
 210  
 211      echo json_encode($result);
 212      die();
 213  } else if ($action == 'removefromquicklist') {
 214      require_capability('mod/assign:grade', $context);
 215  
 216      $commentid = required_param('commentid', PARAM_INT);
 217  
 218      $result = comments_quick_list::remove_comment($commentid);
 219  
 220      echo json_encode($result);
 221      die();
 222  } else if ($action == 'deletefeedbackdocument') {
 223      require_capability('mod/assign:grade', $context);
 224  
 225      $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
 226      $result = document_services::delete_feedback_document($assignment, $userid, $attemptnumber);
 227  
 228      $result = $result && page_editor::unrelease_drafts($grade->id);
 229      echo json_encode($result);
 230      die();
 231  } else if ($action == 'rotatepage') {
 232      require_capability('mod/assign:grade', $context);
 233      $response = new stdClass();
 234      $index = required_param('index', PARAM_INT);
 235      $grade = $assignment->get_user_grade($userid, true, $attemptnumber);
 236      $rotateleft = required_param('rotateleft', PARAM_BOOL);
 237      $filearea = document_services::PAGE_IMAGE_FILEAREA;
 238      $pagefile = document_services::rotate_page($assignment, $userid, $attemptnumber, $index, $rotateleft);
 239      $page = new stdClass();
 240      $page->url = moodle_url::make_pluginfile_url($context->id, document_services::COMPONENT, $filearea,
 241          $grade->id, '/', $pagefile->get_filename())->out();
 242      if ($imageinfo = $pagefile->get_imageinfo()) {
 243          $page->width = $imageinfo['width'];
 244          $page->height = $imageinfo['height'];
 245      } else {
 246          $page->width = 0;
 247          $page->height = 0;
 248      }
 249      $response = (object)['page' => $page];
 250      echo json_encode($response);
 251      die();
 252  }
 253