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.
   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   * Base class for unit tests for mod_assign.
  19   *
  20   * @package    mod_assign
  21   * @category   phpunit
  22   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/mod/assign/locallib.php');
  30  require_once (__DIR__ . '/fixtures/testable_assign.php');
  31  
  32  /**
  33   * Generator helper trait.
  34   *
  35   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  trait mod_assign_test_generator {
  39  
  40      /**
  41       * Convenience function to create a testable instance of an assignment.
  42       *
  43       * @param array $params Array of parameters to pass to the generator
  44       * @return testable_assign Testable wrapper around the assign class.
  45       */
  46      protected function create_instance($course, $params = [], $options = []) {
  47          $params['course'] = $course->id;
  48  
  49          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
  50          $instance = $generator->create_instance($params, $options);
  51          $cm = get_coursemodule_from_instance('assign', $instance->id);
  52          $context = context_module::instance($cm->id);
  53  
  54          return new mod_assign_testable_assign($context, $cm, $course);
  55      }
  56  
  57      /**
  58       * Add a user submission to the assignment.
  59       *
  60       * @param   \stdClass   $student The user to submit for
  61       * @param   \assign     $assign The assignment to submit to
  62       * @param   string      $onlinetext The text tobe submitted
  63       * @param   bool        $changeuser Whether to switch user to the user being submitted as.
  64       */
  65      protected function add_submission($student, $assign, $onlinetext = null, $changeuser = true) {
  66          // Add a submission.
  67          if ($changeuser) {
  68              $this->setUser($student);
  69          }
  70  
  71          if ($onlinetext === null) {
  72              $onlinetext = 'Submission text';
  73          }
  74  
  75          $data = (object) [
  76              'userid' => $student->id,
  77  
  78              'onlinetext_editor' => [
  79                  'itemid' => file_get_unused_draft_itemid(),
  80                  'text' => $onlinetext,
  81                  'format' => FORMAT_HTML,
  82              ]
  83          ];
  84  
  85          $assign->save_submission($data, $notices);
  86      }
  87  
  88      /**
  89       * Submit the assignemnt for grading.
  90       *
  91       * @param   \stdClass   $student The user to submit for
  92       * @param   \assign     $assign The assignment to submit to
  93       * @param   array       $data Additional data to set
  94       * @param   bool        $changeuser Whether to switch user to the user being submitted as.
  95       */
  96      public function submit_for_grading($student, $assign, $data = [], $changeuser = true) {
  97          if ($changeuser) {
  98              $this->setUser($student);
  99          }
 100  
 101          $data = (object) array_merge($data, [
 102                  'userid' => $student->id,
 103              ]);
 104  
 105          $sink = $this->redirectMessages();
 106          $assign->submit_for_grading($data, []);
 107          $sink->close();
 108  
 109          return $data;
 110      }
 111  
 112      /**
 113       * Mark the submission.
 114       *
 115       * @param   \stdClass   $teacher The user to mark as
 116       * @param   \assign     $assign The assignment to mark
 117       * @param   \stdClass   $student The user to grade
 118       * @param   array       $data Additional data to set
 119       * @param   bool        $changeuser Whether to switch user to the user being submitted as.
 120       */
 121      protected function mark_submission($teacher, $assign, $student, $grade = 50.0, $data = [], $attempt = 0) {
 122          global $DB;
 123  
 124          // Mark the submission.
 125          $this->setUser($teacher);
 126          $data = (object) array_merge($data, [
 127                  'grade' => $grade,
 128              ]);
 129  
 130          // Bump all timecreated and timemodified for this user back.
 131          $DB->execute('UPDATE {assign_submission} SET timecreated = timecreated - 1, timemodified = timemodified - 1 WHERE userid = :userid',
 132              ['userid' => $student->id]);
 133  
 134          $assign->testable_apply_grade_to_user($data, $student->id, $attempt);
 135      }
 136  }