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   * H5P activity attempts report
  19   *
  20   * @package    mod_h5pactivity
  21   * @since      Moodle 3.9
  22   * @copyright  2020 Ferran Recio <ferran@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace mod_h5pactivity\local\report;
  27  
  28  use mod_h5pactivity\local\report;
  29  use mod_h5pactivity\local\manager;
  30  use mod_h5pactivity\local\attempt;
  31  use mod_h5pactivity\output\reportattempts;
  32  use stdClass;
  33  
  34  /**
  35   * Class  H5P activity attempts report.
  36   *
  37   * @package    mod_h5pactivity
  38   * @since      Moodle 3.9
  39   * @copyright  2020 Ferran Recio <ferran@moodle.com>
  40   */
  41  class attempts implements report {
  42  
  43      /** @var manager the H5P activity manager instance. */
  44      private $manager;
  45  
  46      /** @var stdClass the user record. */
  47      private $user;
  48  
  49      /**
  50       * Create a new participants report.
  51       *
  52       * @param manager $manager h5pactivity manager object
  53       * @param stdClass $user user record
  54       */
  55      public function __construct(manager $manager, stdClass $user) {
  56          $this->manager = $manager;
  57          $this->user = $user;
  58      }
  59  
  60      /**
  61       * Return the report user record.
  62       *
  63       * @return stdClass|null a user or null
  64       */
  65      public function get_user(): ?stdClass {
  66          return $this->user;
  67      }
  68  
  69      /**
  70       * Return the report attempt object.
  71       *
  72       * Attempts report has no specific attempt.
  73       *
  74       * @return attempt|null the attempt object or null
  75       */
  76      public function get_attempt(): ?attempt {
  77          return null;
  78      }
  79  
  80      /**
  81       * Print the report.
  82       */
  83      public function print(): void {
  84          global $OUTPUT;
  85  
  86          $manager = $this->manager;
  87          $cm = $manager->get_coursemodule();
  88  
  89          $scored = $this->get_scored();
  90          $title = $scored->title ?? null;
  91          $scoredattempt = $scored->attempt ?? null;
  92  
  93          $attempts = $this->get_attempts();
  94  
  95          $widget = new reportattempts($attempts, $this->user, $cm->course, $title, $scoredattempt);
  96          echo $OUTPUT->render($widget);
  97      }
  98  
  99      /**
 100       * Return the current report attempts.
 101       *
 102       * This method is used to render the report in both browser and mobile.
 103       *
 104       * @return attempts[]
 105       */
 106      public function get_attempts(): array {
 107          return $this->manager->get_user_attempts($this->user->id);
 108      }
 109  
 110      /**
 111       * Return the current report attempts.
 112       *
 113       * This method is used to render the report in both browser and mobile.
 114       *
 115       * @return stdClass|null a structure with
 116       *      - title => name of the selected attempt (or null)
 117       *      - attempt => the selected attempt object (or null)
 118       *      - gradingmethos => the activity grading method (or null)
 119       */
 120      public function get_scored(): ?stdClass {
 121          $manager = $this->manager;
 122          $scores = $manager->get_users_scaled_score($this->user->id);
 123          $score = $scores[$this->user->id] ?? null;
 124  
 125          if (empty($score->attemptid)) {
 126              return null;
 127          }
 128  
 129          list($grademethod, $title) = $manager->get_selected_attempt();
 130          $scoredattempt = $manager->get_attempt($score->attemptid);
 131  
 132          $result = (object)[
 133              'title' => $title,
 134              'attempt' => $scoredattempt,
 135              'grademethod' => $grademethod,
 136          ];
 137          return $result;
 138      }
 139  }