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.

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

   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   * Defines the renderer for the scorm module.
  19   *
  20   * @package mod_scorm
  21   * @copyright 2013 Dan Marsden
  22   * @author Dan Marsden <dan@danmarsden.com>
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * The renderer for the scorm module.
  30   *
  31   * @copyright 2013 Dan Marsden
  32   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class mod_scorm_renderer extends plugin_renderer_base {
  35      public function view_user_heading($user, $course, $baseurl, $attempt, $attemptids) {
  36          $output = '';
  37          $output .= $this->box_start('generalbox boxaligncenter');
  38          $output .= html_writer::start_tag('div', array('class' => 'mdl-align'));
  39          $output .= $this->user_picture($user, array('courseid' => $course->id, 'link' => true));
  40          $url = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id));
  41          $output .= html_writer::link($url, fullname($user));
  42          $baseurl->param('attempt', '');
  43          $pb = new mod_scorm_attempt_bar($attemptids, $attempt, $baseurl, 'attempt');
  44          $output .= $this->render($pb);
  45          $output .= html_writer::end_tag('div');
  46          $output .= $this->box_end();
  47          return $output;
  48      }
  49      /**
  50       * scorm attempt bar renderer
  51       *
  52       * @param mod_scorm_attempt_bar $attemptbar
  53       * @return string
  54       */
  55      protected function render_mod_scorm_attempt_bar(mod_scorm_attempt_bar $attemptbar) {
  56          $output = '';
  57          $attemptbar = clone($attemptbar);
  58          $attemptbar->prepare($this, $this->page, $this->target);
  59  
  60          if (count($attemptbar->attemptids) > 1) {
  61              $output .= get_string('attempt', 'scorm') . ':';
  62  
  63              if (!empty($attemptbar->previouslink)) {
  64                  $output .= '&#160;(' . $attemptbar->previouslink . ')&#160;';
  65              }
  66  
  67              foreach ($attemptbar->attemptlinks as $link) {
  68                  $output .= "&#160;&#160;$link";
  69              }
  70  
  71              if (!empty($attemptbar->nextlink)) {
  72                  $output .= '&#160;&#160;(' . $attemptbar->nextlink . ')';
  73              }
  74          }
  75  
  76          return html_writer::tag('div', $output, array('class' => 'paging'));
  77      }
  78  
  79      /**
  80       * Rendered HTML for the report action is provided.
  81       *
  82       * @param \mod_scorm\output\actionbar $actionbar actionbar object.
  83       * @return bool|string rendered HTML for the report action.
  84       */
  85      public function report_actionbar(\mod_scorm\output\actionbar $actionbar): string {
  86          return $this->render_from_template('mod_scorm/report_actionbar', $actionbar->export_for_template($this));
  87      }
  88  
  89      /**
  90       * Rendered HTML for the user report action is provided
  91       *
  92       * @param \mod_scorm\output\userreportsactionbar $userreportsactionbar userreportsactionbar object
  93       * @return string rendered HTML for the user report action.
  94       */
  95      public function user_report_actionbar(\mod_scorm\output\userreportsactionbar $userreportsactionbar): string {
  96          return $this->render_from_template('mod_scorm/user_report_actionbar', $userreportsactionbar->export_for_template($this));
  97      }
  98  
  99      /**
 100       * Generate the SCORM's "Exit activity" button
 101       *
 102       * @param string $url The url to be hooked up to the exit button
 103       * @return string
 104       */
 105      public function generate_exitbar(string $url): string {
 106          return $this->render_from_template('mod_scorm/player_exitbar', ['action' => $url]);
 107      }
 108  }
 109  
 110  /**
 111   * Component representing a SCORM attempts bar.
 112   *
 113   * @copyright 2013 Dan Marsden
 114   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 115   * @package mod_scorm
 116   */
 117  class mod_scorm_attempt_bar implements renderable {
 118  
 119      /**
 120       * @var array An array of the attemptids
 121       */
 122      public $attemptids;
 123  
 124      /**
 125       * @var int The attempt you are currently viewing.
 126       */
 127      public $attempt;
 128  
 129      /**
 130       * @var string|moodle_url If this  is a string then it is the url which will be appended with $pagevar,
 131       * an equals sign and the attempt number.
 132       * If this is a moodle_url object then the pagevar param will be replaced by
 133       * the attempt no, for each attempt.
 134       */
 135      public $baseurl;
 136  
 137      /**
 138       * @var string This is the variable name that you use for the attempt in your
 139       * code (ie. 'tablepage', 'blogpage', etc)
 140       */
 141      public $pagevar;
 142  
 143      /**
 144       * @var string A HTML link representing the "previous" attempt.
 145       */
 146      public $previouslink = null;
 147  
 148      /**
 149       * @var string A HTML link representing the "next" attempt.
 150       */
 151      public $nextlink = null;
 152  
 153      /**
 154       * @var array An array of strings. One of them is just a string: the current attempt
 155       */
 156      public $attemptlinks = array();
 157  
 158      /**
 159       * Constructor mod_scorm_attempt_bar with only the required params.
 160       *
 161       * @param array $attemptids an array of attempts the user has made
 162       * @param int $attempt The attempt you are currently viewing
 163       * @param string|moodle_url $baseurl url of the current page, the $pagevar parameter is added
 164       * @param string $pagevar name of page parameter that holds the attempt number
 165       */
 166      public function __construct($attemptids, $attempt, $baseurl, $pagevar = 'page') {
 167          $this->attemptids = $attemptids;
 168          $this->attempt    = $attempt;
 169          $this->baseurl    = $baseurl;
 170          $this->pagevar    = $pagevar;
 171      }
 172  
 173      /**
 174       * Prepares the scorm attempt bar for output.
 175       *
 176       * This method validates the arguments set up for the scorm attempt bar and then
 177       * produces fragments of HTML to assist display later on.
 178       *
 179       * @param renderer_base $output
 180       * @param moodle_page $page
 181       * @param string $target
 182       * @throws coding_exception
 183       */
 184      public function prepare(renderer_base $output, moodle_page $page, $target) {
 185          if (empty($this->attemptids)) {
 186              throw new coding_exception('mod_scorm_attempt_bar requires a attemptids value.');
 187          }
 188          if (!isset($this->attempt) || is_null($this->attempt)) {
 189              throw new coding_exception('mod_scorm_attempt_bar requires a attempt value.');
 190          }
 191          if (empty($this->baseurl)) {
 192              throw new coding_exception('mod_scorm_attempt_bar requires a baseurl value.');
 193          }
 194  
 195          if (count($this->attemptids) > 1) {
 196              $lastattempt = end($this->attemptids); // Get last attempt.
 197              $firstattempt = reset($this->attemptids); // get first attempt.
 198  
 199              $nextattempt = 0;
 200              $prevattempt = null;
 201              $previous = 0;
 202              foreach ($this->attemptids as $attemptid) {
 203                  if ($this->attempt == $attemptid) {
 204                      $this->attemptlinks[] = $attemptid;
 205                      $prevattempt = $previous;
 206                  } else {
 207                      $attemptlink = html_writer::link(
 208                          new moodle_url($this->baseurl, array($this->pagevar => $attemptid)), $attemptid);
 209                      $this->attemptlinks[] = $attemptlink;
 210                      if (empty($nextattempt) && $prevattempt !== null) {
 211                          // Set the nextattempt var as we have set previous attempt earlier.
 212                          $nextattempt = $attemptid;
 213                      }
 214                  }
 215                  $previous = $attemptid; // Store this attempt as previous in case we need it.
 216              }
 217  
 218              if ($this->attempt != $firstattempt) {
 219                  $this->previouslink = html_writer::link(
 220                      new moodle_url($this->baseurl, array($this->pagevar => $prevattempt)),
 221                      get_string('previous'), array('class' => 'previous'));
 222              }
 223  
 224              if ($this->attempt != $lastattempt) {
 225                  $this->nextlink = html_writer::link(
 226                      new moodle_url($this->baseurl, array($this->pagevar => $nextattempt)),
 227                      get_string('next'), array('class' => 'next'));
 228              }
 229          }
 230      }
 231  }