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   * Contains class mod_h5pactivity\output\report\attempts
  19   *
  20   * @package   mod_h5pactivity
  21   * @copyright 2020 Ferran Recio
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_h5pactivity\output;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use mod_h5pactivity\local\attempt;
  30  use mod_h5pactivity\output\attempt as output_attempt;
  31  use renderable;
  32  use templatable;
  33  use renderer_base;
  34  use user_picture;
  35  use stdClass;
  36  
  37  /**
  38   * Class to output an attempts report on mod_h5pactivity.
  39   *
  40   * @copyright 2020 Ferran Recio
  41   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class reportattempts implements renderable, templatable {
  44  
  45      /** @var attempt[] attempts */
  46      public $attempts;
  47  
  48      /** @var stdClass user record */
  49      public $user;
  50  
  51      /** @var int courseid necesary to present user picture */
  52      public $courseid;
  53  
  54      /** @var attempt scored attempt */
  55      public $scored;
  56  
  57      /** @var string scored attempt title */
  58      public $title;
  59  
  60      /**
  61       * Constructor.
  62       *
  63       * The "scored attempt" is the attempt used for grading. By default it is the max score attempt
  64       * but this could be defined in the activity settings. In some cases this scored attempts does not
  65       * exists at all, this is the reason why it's an optional param.
  66       *
  67       * @param array $attempts an array of attempts
  68       * @param stdClass $user a user record
  69       * @param int $courseid course id
  70       * @param string|null $title title to display on the scored attempt (null if none attempt is the scored one)
  71       * @param attempt|null $scored the scored attempt (null if none)
  72       */
  73      public function __construct(array $attempts, stdClass $user, int $courseid, string $title = null, attempt $scored = null) {
  74          $this->attempts = $attempts;
  75          $this->user = $user;
  76          $this->courseid = $courseid;
  77          $this->title = $title;
  78          $this->scored = $scored;
  79      }
  80  
  81      /**
  82       * Export this data so it can be used as the context for a mustache template.
  83       *
  84       * @param renderer_base $output
  85       * @return stdClass
  86       */
  87      public function export_for_template(renderer_base $output) {
  88          global $USER;
  89  
  90          $data = (object)['attempts' => [], 'user' => $this->user];
  91          foreach ($this->attempts as $attempt) {
  92              $outputattempt = new output_attempt($attempt);
  93              $data->attempts[] = $outputattempt->export_for_template($output);
  94          }
  95          $data->attemptscount = count($data->attempts);
  96  
  97          $userpicture = new user_picture($this->user);
  98          $userpicture->courseid = $this->courseid;
  99          $data->user->fullname = fullname($this->user);
 100          $data->user->picture = $output->render($userpicture);
 101  
 102          if ($USER->id == $this->user->id) {
 103              $data->title = get_string('myattempts', 'mod_h5pactivity');
 104          }
 105  
 106          if (!empty($this->title)) {
 107              $scored = (object)[
 108                  'title' => $this->title,
 109                  'attempts' => [],
 110              ];
 111              $outputattempt = new output_attempt($this->scored);
 112              $scored->attempts[] = $outputattempt->export_for_template($output);
 113              $data->scored = $scored;
 114          }
 115  
 116          return $data;
 117      }
 118  }