Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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  namespace mod_assign;
  18  
  19  use assignfeedback_editpdf\document_services;
  20  use assignfeedback_editpdf\combined_document;
  21  use mod_assign_test_generator;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  require_once($CFG->dirroot . '/mod/assign/locallib.php');
  27  require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
  28  
  29  /**
  30   * Provides the unit tests for feedback.
  31   *
  32   * @package     mod_assign
  33   * @category    test
  34   * @copyright   2019 Ilya Tregubov ilyatregubov@catalyst-au.net
  35   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class feedback_test extends \advanced_testcase {
  38  
  39      // Use the generator helper.
  40      use mod_assign_test_generator;
  41  
  42      /**
  43       * Helper to create a stored file object with the given supplied content.
  44       *
  45       * @param   int $contextid context id for assigment
  46       * @param   int $itemid item id from assigment grade
  47       * @param   string $filearea File area
  48       * @param   int $timemodified Time modified
  49       * @param   string $filecontent The content of the mocked file
  50       * @param   string $filename The file name to use in the stored_file
  51       * @param   string $filerecord Any overrides to the filerecord
  52       * @return  stored_file
  53       */
  54      protected function create_stored_file($contextid, $itemid, $filearea, $timemodified,
  55                                            $filecontent = 'content', $filename = 'combined.pdf', $filerecord = []) {
  56          $filerecord = array_merge([
  57              'contextid' => $contextid,
  58              'component' => 'assignfeedback_editpdf',
  59              'filearea'  => $filearea,
  60              'itemid'    => $itemid,
  61              'filepath'  => '/',
  62              'filename'  => $filename,
  63              'timemodified' => $timemodified,
  64          ], $filerecord);
  65  
  66          $fs = get_file_storage();
  67          $file = $fs->create_file_from_string($filerecord, $filecontent);
  68  
  69          return $file;
  70      }
  71  
  72      /**
  73       * Convenience function to create an instance of an assignment.
  74       *
  75       * @param array $params Array of parameters to pass to the generator
  76       * @return assign The assign class.
  77       */
  78      protected function create_instance($params = array()) {
  79          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  80          $instance = $generator->create_instance($params);
  81          $cm = get_coursemodule_from_instance('assign', $instance->id);
  82          $context = \context_module::instance($cm->id);
  83          return new \assign($context, $cm, $params['course']);
  84      }
  85  
  86      /**
  87       * Test fetching combined.pdf for state checking.
  88       */
  89      public function test_get_combined_document_for_attempt() {
  90  
  91          $this->resetAfterTest(true);
  92  
  93          $course = $this->getDataGenerator()->create_course();
  94  
  95          $user = $this->getDataGenerator()->create_user();
  96          $this->getDataGenerator()->enrol_user($user->id, $course->id, 'student');
  97  
  98          $teacher = $this->getDataGenerator()->create_user();
  99          $this->getDataGenerator()->enrol_user($teacher->id, $course->id, 'editingteacher');
 100  
 101          $assign = $this->create_instance([
 102              'course' => $course,
 103              'name' => 'Assign 1',
 104              'attemptreopenmethod' => ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL,
 105              'maxattempts' => 3,
 106              'assignsubmission_onlinetext_enabled' => true,
 107              'assignfeedback_comments_enabled' => true
 108          ]);
 109  
 110          $submission = new \stdClass();
 111          $submission->assignment = $assign->get_instance()->id;
 112          $submission->userid = $user->id;
 113          $submission->timecreated = time();
 114          $submission->timemodified = time();
 115          $submission->onlinetext_editor = ['text' => 'Submission text',
 116              'format' => FORMAT_MOODLE];
 117  
 118          $this->setUser($user);
 119          $notices = [];
 120          $assign->save_submission($submission, $notices);
 121  
 122          $this->setUser($teacher);
 123  
 124          $grade = '3.14';
 125          $teachercommenttext = 'This is better. Thanks.';
 126          $data = new \stdClass();
 127          $data->attemptnumber = 1;
 128          $data->grade = $grade;
 129          $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext, 'format' => FORMAT_MOODLE];
 130  
 131          // Give the submission a grade.
 132          $assign->save_grade($user->id, $data);
 133  
 134          $grade = $assign->get_user_grade($user->id, true, -1);
 135  
 136          $contextid = $assign->get_context()->id;
 137          $itemid = $grade->id;
 138  
 139          // Create combined document in combined area.
 140          $this->create_stored_file($contextid, $itemid, 'combined', time());
 141  
 142          $document = document_services::get_combined_document_for_attempt($assign, $user->id, -1);
 143          $status = $document->get_status();
 144  
 145          $this->assertEquals($status, combined_document::STATUS_COMPLETE);
 146  
 147          // Create orphaned combined document in partial area.
 148          $this->create_stored_file($contextid, $itemid, 'partial', time() - 3600);
 149  
 150          $document = document_services::get_combined_document_for_attempt($assign, $user->id, -1);
 151          $status = $document->get_status();
 152  
 153          $this->assertEquals($status, combined_document::STATUS_FAILED);
 154      }
 155  
 156  }