Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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 concurrent ajax request.
  19   * ALL RETURNED INFO IS PUBLIC.
  20   *
  21   * @package assignfeedback_editpdf
  22   * @copyright  2013 Jerome Mouneyrac
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  define('AJAX_SCRIPT', true);
  27  // To be able to process concurrent ajax request with the generate pdf ajax request we can not use cookie.
  28  define('NO_MOODLE_COOKIES', true);
  29  
  30  use \assignfeedback_editpdf\document_services;
  31  require_once('../../../../config.php');
  32  
  33  try {
  34      $assignmentid = required_param('assignmentid', PARAM_INT);
  35      $userid = required_param('userid', PARAM_INT);
  36      $attemptnumber = required_param('attemptnumber', PARAM_INT);
  37  
  38      // Retrieve the assignments.
  39      require_once($CFG->dirroot . '/mod/assign/locallib.php');
  40      $cm = get_coursemodule_from_instance('assign', $assignmentid, 0, false, MUST_EXIST);
  41      $context = context_module::instance($cm->id);
  42      $assignment = new assign($context, null, null);
  43  
  44      // Get the generated images from file API call.
  45      $grade = $assignment->get_user_grade($userid, false, $attemptnumber);
  46  
  47      // Check we found a grade.
  48      if (empty($grade)) {
  49          throw new coding_exception('grade not found');
  50      }
  51  
  52      // No need to handle the readonly files here, the should be already generated.
  53      $component = 'assignfeedback_editpdf';
  54      $filearea = document_services::PAGE_IMAGE_FILEAREA;
  55      $filepath = '/';
  56      $fs = get_file_storage();
  57      $files = $fs->get_directory_files($context->id, $component, $filearea, $grade->id, $filepath);
  58  
  59      // The important security part: we ONLY RETURN the total NUMBER of generated images.
  60      echo $OUTPUT->header();
  61      echo json_encode(count($files));
  62      echo $OUTPUT->footer();
  63  } catch (Exception $e) {
  64      // This should never happened!
  65      // Return a 500 HTTP header so Y.io gets it as a failure.
  66      if (substr(php_sapi_name(), 0, 3) == 'cgi') {
  67          header("Status: 500 Internal Server Error");
  68      } else {
  69          header('HTTP/1.0 500 Internal Server Error');
  70      }
  71      throw new moodle_exception('An exception was caught but can not be returned for security purpose.
  72          To easily debug, comment the try catch.');
  73  }