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] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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 participants 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 core\dml\sql_join;
  32  use table_sql;
  33  use moodle_url;
  34  use html_writer;
  35  use stdClass;
  36  
  37  defined('MOODLE_INTERNAL') || die();
  38  
  39  global $CFG;
  40  require_once($CFG->libdir.'/tablelib.php');
  41  
  42  /**
  43   * Class  H5P activity participants report.
  44   *
  45   * @package    mod_h5pactivity
  46   * @since      Moodle 3.9
  47   * @copyright  2020 Ferran Recio <ferran@moodle.com>
  48   */
  49  class participants extends table_sql implements report {
  50  
  51      /** @var manager the H5P activity manager instance. */
  52      private $manager;
  53  
  54      /** @var array the users scored attempts. */
  55      private $scores;
  56  
  57      /** @var array the user attempts count. */
  58      private $count;
  59  
  60      /**
  61       * Create a new participants report.
  62       *
  63       * @param manager $manager h5pactivitymanager object
  64       */
  65      public function __construct(manager $manager) {
  66          parent::__construct('mod_h5pactivity-participants');
  67          $this->manager = $manager;
  68          $this->scores = $manager->get_users_scaled_score();
  69          $this->count = $manager->count_users_attempts();
  70  
  71          // Setup table_sql.
  72          $columns = ['fullname', 'timemodified', 'score', 'attempts'];
  73          $headers = [
  74              get_string('fullname'), get_string('date'),
  75              get_string('score', 'mod_h5pactivity'), get_string('attempts', 'mod_h5pactivity'),
  76          ];
  77          $this->define_columns($columns);
  78          $this->define_headers($headers);
  79          $this->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
  80          $this->sortable(true);
  81          $this->no_sorting('score');
  82          $this->no_sorting('timemodified');
  83          $this->no_sorting('attempts');
  84          $this->pageable(true);
  85  
  86          $capjoin = $this->manager->get_active_users_join(true);
  87  
  88          // Final SQL.
  89          $this->set_sql(
  90              'DISTINCT u.id, u.picture, u.firstname, u.lastname, u.firstnamephonetic, u.lastnamephonetic,
  91              u.middlename, u.alternatename, u.imagealt, u.email',
  92              "{user} u $capjoin->joins",
  93              $capjoin->wheres,
  94              $capjoin->params);
  95      }
  96  
  97      /**
  98       * Return the report user record.
  99       *
 100       * Participants report has no specific user.
 101       *
 102       * @return stdClass|null a user or null
 103       */
 104      public function get_user(): ?stdClass {
 105          return null;
 106      }
 107  
 108      /**
 109       * Return the report attempt object.
 110       *
 111       * Participants report has no specific attempt.
 112       *
 113       * @return attempt|null the attempt object or null
 114       */
 115      public function get_attempt(): ?attempt {
 116          return null;
 117      }
 118  
 119      /**
 120       * Print the report.
 121       */
 122      public function print(): void {
 123          global $PAGE, $OUTPUT;
 124  
 125          $this->define_baseurl($PAGE->url);
 126  
 127          echo $OUTPUT->heading(get_string('attempts_report', 'mod_h5pactivity'));
 128  
 129          $this->out($this->get_page_size(), true);
 130      }
 131  
 132      /**
 133       * Warning in case no user has the selected initials letters.
 134       *
 135       */
 136      public function print_nothing_to_display() {
 137          global $OUTPUT;
 138          echo $this->render_reset_button();
 139          $this->print_initials_bar();
 140          echo $OUTPUT->notification(get_string('noparticipants', 'mod_h5pactivity'), 'warning');
 141      }
 142  
 143      /**
 144       * Generate the fullname column.
 145       *
 146       * @param stdClass $user
 147       * @return string
 148       */
 149      public function col_fullname($user): string {
 150          global $OUTPUT;
 151          $cm = $this->manager->get_coursemodule();
 152          return $OUTPUT->user_picture($user, ['size' => 35, 'courseid' => $cm->course, 'includefullname' => true]);
 153      }
 154  
 155      /**
 156       * Generate score column.
 157       *
 158       * @param stdClass $user the user record
 159       * @return string
 160       */
 161      public function col_score(stdClass $user): string {
 162          $cm = $this->manager->get_coursemodule();
 163          if (isset($this->scores[$user->id])) {
 164              $score = $this->scores[$user->id];
 165              $maxgrade = floatval(100);
 166              $scaled = round($maxgrade * $score->scaled).'%';
 167              if (empty($score->attemptid)) {
 168                  return $scaled;
 169              } else {
 170                  $url = new moodle_url('/mod/h5pactivity/report.php', ['a' => $cm->instance, 'attemptid' => $score->attemptid]);
 171                  return html_writer::link($url, $scaled);
 172              }
 173          }
 174          return '';
 175      }
 176  
 177      /**
 178       * Generate attempts count column, if any.
 179       *
 180       * @param stdClass $user the user record
 181       * @return string
 182       */
 183      public function col_attempts(stdClass $user): string {
 184          $cm = $this->manager->get_coursemodule();
 185          if (isset($this->count[$user->id])) {
 186              $msg = get_string('review_user_attempts', 'mod_h5pactivity', $this->count[$user->id]);
 187              $url = new moodle_url('/mod/h5pactivity/report.php', ['a' => $cm->instance, 'userid' => $user->id]);
 188              return html_writer::link($url, $msg);
 189          }
 190          return '';
 191  
 192      }
 193  
 194      /**
 195       * Generate attempt timemodified column, if any.
 196       *
 197       * @param stdClass $user the user record
 198       * @return string
 199       */
 200      public function col_timemodified(stdClass $user): string {
 201          if (isset($this->scores[$user->id])) {
 202              $score = $this->scores[$user->id];
 203              return userdate($score->timemodified);
 204          }
 205          return '';
 206      }
 207  }