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]

   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   * A scheduled task.
  19   *
  20   * @package    assignfeedback_editpdf
  21   * @copyright  2016 Damyon Wiese
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace assignfeedback_editpdf\task;
  25  
  26  use core\task\scheduled_task;
  27  use assignfeedback_editpdf\document_services;
  28  use assignfeedback_editpdf\combined_document;
  29  use context_module;
  30  use assign;
  31  
  32  /**
  33   * Simple task to convert submissions to pdf in the background.
  34   * @copyright  2016 Damyon Wiese
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class convert_submissions extends scheduled_task {
  38  
  39      /**
  40       * Get a descriptive name for this task (shown to admins).
  41       *
  42       * @return string
  43       */
  44      public function get_name() {
  45          return get_string('preparesubmissionsforannotation', 'assignfeedback_editpdf');
  46      }
  47  
  48      /**
  49       * Do the job.
  50       * Throw exceptions on errors (the job will be retried).
  51       */
  52      public function execute() {
  53          global $CFG, $DB;
  54  
  55          require_once($CFG->dirroot . '/mod/assign/locallib.php');
  56  
  57          $records = $DB->get_records('assignfeedback_editpdf_queue');
  58  
  59          $assignmentcache = array();
  60  
  61          $conversionattemptlimit = !empty($CFG->conversionattemptlimit) ? $CFG->conversionattemptlimit : 3;
  62          foreach ($records as $record) {
  63              $submissionid = $record->submissionid;
  64              $submission = $DB->get_record('assign_submission', array('id' => $submissionid), '*', IGNORE_MISSING);
  65              if (!$submission || $record->attemptedconversions >= $conversionattemptlimit) {
  66                  // Submission no longer exists; or we've exceeded the conversion attempt limit.
  67                  $DB->delete_records('assignfeedback_editpdf_queue', array('id' => $record->id));
  68                  continue;
  69              }
  70  
  71              // Record that we're attempting the conversion ahead of time.
  72              // We can't do this afterwards as its possible for the conversion process to crash the script entirely.
  73              $DB->set_field('assignfeedback_editpdf_queue', 'attemptedconversions',
  74                      $record->attemptedconversions + 1, ['id' => $record->id]);
  75  
  76              $assignmentid = $submission->assignment;
  77              $attemptnumber = $record->submissionattempt;
  78  
  79              if (empty($assignmentcache[$assignmentid])) {
  80                  $cm = get_coursemodule_from_instance('assign', $assignmentid, 0, false, MUST_EXIST);
  81                  $context = context_module::instance($cm->id);
  82  
  83                  $assignment = new assign($context, null, null);
  84                  $assignmentcache[$assignmentid] = $assignment;
  85              } else {
  86                  $assignment = $assignmentcache[$assignmentid];
  87              }
  88  
  89              $users = array();
  90              if ($submission->userid) {
  91                  array_push($users, $submission->userid);
  92              } else {
  93                  $members = $assignment->get_submission_group_members($submission->groupid, true);
  94  
  95                  foreach ($members as $member) {
  96                      array_push($users, $member->id);
  97                  }
  98              }
  99  
 100              mtrace('Convert ' . count($users) . ' submission attempt(s) for assignment ' . $assignmentid);
 101              $conversionrequirespolling = false;
 102  
 103              foreach ($users as $userid) {
 104                  try {
 105                      $combineddocument = document_services::get_combined_pdf_for_attempt($assignment, $userid, $attemptnumber);
 106                      switch ($combineddocument->get_status()) {
 107                          case combined_document::STATUS_READY:
 108                          case combined_document::STATUS_READY_PARTIAL:
 109                          case combined_document::STATUS_PENDING_INPUT:
 110                              // The document has not been converted yet or is somehow still ready.
 111                              $conversionrequirespolling = true;
 112                              continue 2;
 113                      }
 114                      document_services::get_page_images_for_attempt(
 115                              $assignment,
 116                              $userid,
 117                              $attemptnumber,
 118                              false
 119                          );
 120                      document_services::get_page_images_for_attempt(
 121                              $assignment,
 122                              $userid,
 123                              $attemptnumber,
 124                              true
 125                          );
 126                  } catch (\moodle_exception $e) {
 127                      mtrace('Conversion failed with error:' . $e->errorcode);
 128                  }
 129              }
 130  
 131              // Remove from queue.
 132              if (!$conversionrequirespolling) {
 133                  $DB->delete_records('assignfeedback_editpdf_queue', array('id' => $record->id));
 134              }
 135  
 136          }
 137      }
 138  
 139  }