Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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       * @param int|bool $currentgroup False if groups not used, 0 for all groups, group id (int) to filter by specific group
  65       */
  66      public function __construct(manager $manager, $currentgroup = false) {
  67          parent::__construct('mod_h5pactivity-participants');
  68          $this->manager = $manager;
  69          $this->scores = $manager->get_users_scaled_score();
  70          $this->count = $manager->count_users_attempts();
  71  
  72          // Setup table_sql.
  73          $columns = ['fullname', 'timemodified', 'score', 'attempts'];
  74          $headers = [
  75              get_string('fullname'), get_string('date'),
  76              get_string('score', 'mod_h5pactivity'), get_string('attempts', 'mod_h5pactivity'),
  77          ];
  78          $this->define_columns($columns);
  79          $this->define_headers($headers);
  80          $this->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
  81          $this->sortable(true);
  82          $this->no_sorting('score');
  83          $this->no_sorting('timemodified');
  84          $this->no_sorting('attempts');
  85          $this->pageable(true);
  86  
  87          $capjoin = $this->manager->get_active_users_join(true, $currentgroup);
  88  
  89          // Final SQL.
  90          $this->set_sql(
  91              'DISTINCT u.id, u.picture, u.firstname, u.lastname, u.firstnamephonetic, u.lastnamephonetic,
  92              u.middlename, u.alternatename, u.imagealt, u.email',
  93              "{user} u $capjoin->joins",
  94              $capjoin->wheres,
  95              $capjoin->params);
  96      }
  97  
  98      /**
  99       * Return the report user record.
 100       *
 101       * Participants report has no specific user.
 102       *
 103       * @return stdClass|null a user or null
 104       */
 105      public function get_user(): ?stdClass {
 106          return null;
 107      }
 108  
 109      /**
 110       * Return the report attempt object.
 111       *
 112       * Participants report has no specific attempt.
 113       *
 114       * @return attempt|null the attempt object or null
 115       */
 116      public function get_attempt(): ?attempt {
 117          return null;
 118      }
 119  
 120      /**
 121       * Print the report.
 122       */
 123      public function print(): void {
 124          global $PAGE, $OUTPUT;
 125  
 126          $this->define_baseurl($PAGE->url);
 127  
 128          $this->out($this->get_page_size(), true);
 129      }
 130  
 131      /**
 132       * Warning in case no user has the selected initials letters.
 133       *
 134       */
 135      public function print_nothing_to_display() {
 136          global $OUTPUT;
 137          echo $this->render_reset_button();
 138          $this->print_initials_bar();
 139          echo $OUTPUT->notification(get_string('noparticipants', 'mod_h5pactivity'), 'warning');
 140      }
 141  
 142      /**
 143       * Generate the fullname column.
 144       *
 145       * @param stdClass $user
 146       * @return string
 147       */
 148      public function col_fullname($user): string {
 149          global $OUTPUT;
 150          $cm = $this->manager->get_coursemodule();
 151          return $OUTPUT->user_picture($user, ['size' => 35, 'courseid' => $cm->course, 'includefullname' => true]);
 152      }
 153  
 154      /**
 155       * Generate score column.
 156       *
 157       * @param stdClass $user the user record
 158       * @return string
 159       */
 160      public function col_score(stdClass $user): string {
 161          $cm = $this->manager->get_coursemodule();
 162          if (isset($this->scores[$user->id])) {
 163              $score = $this->scores[$user->id];
 164              $maxgrade = floatval(100);
 165              $scaled = round($maxgrade * $score->scaled).'%';
 166              if (empty($score->attemptid)) {
 167                  return $scaled;
 168              } else {
 169                  $url = new moodle_url('/mod/h5pactivity/report.php', ['a' => $cm->instance, 'attemptid' => $score->attemptid]);
 170                  return html_writer::link($url, $scaled);
 171              }
 172          }
 173          return '';
 174      }
 175  
 176      /**
 177       * Generate attempts count column, if any.
 178       *
 179       * @param stdClass $user the user record
 180       * @return string
 181       */
 182      public function col_attempts(stdClass $user): string {
 183          $cm = $this->manager->get_coursemodule();
 184          if (isset($this->count[$user->id])) {
 185              $msg = get_string('review_user_attempts', 'mod_h5pactivity', $this->count[$user->id]);
 186              $url = new moodle_url('/mod/h5pactivity/report.php', ['a' => $cm->instance, 'userid' => $user->id]);
 187              return html_writer::link($url, $msg);
 188          }
 189          return '';
 190  
 191      }
 192  
 193      /**
 194       * Generate attempt timemodified column, if any.
 195       *
 196       * @param stdClass $user the user record
 197       * @return string
 198       */
 199      public function col_timemodified(stdClass $user): string {
 200          if (isset($this->scores[$user->id])) {
 201              $score = $this->scores[$user->id];
 202              return userdate($score->timemodified);
 203          }
 204          return '';
 205      }
 206  }