weekly release 4.5dev
[moodle.git] / mod / scorm / renderer.php
blob632bd1d00d573927c4a2609c5003ed1c476f2bc6
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.
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/>.
17 /**
18 * Defines the renderer for the scorm module.
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
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * The renderer for the scorm module.
31 * @copyright 2013 Dan Marsden
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
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;
49 /**
50 * scorm attempt bar renderer
52 * @param mod_scorm_attempt_bar $attemptbar
53 * @return string
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);
60 if (count($attemptbar->attemptids) > 1) {
61 $output .= get_string('attempt', 'scorm') . ':';
63 if (!empty($attemptbar->previouslink)) {
64 $output .= '&#160;(' . $attemptbar->previouslink . ')&#160;';
67 foreach ($attemptbar->attemptlinks as $link) {
68 $output .= "&#160;&#160;$link";
71 if (!empty($attemptbar->nextlink)) {
72 $output .= '&#160;&#160;(' . $attemptbar->nextlink . ')';
76 return html_writer::tag('div', $output, array('class' => 'paging'));
79 /**
80 * Rendered HTML for the report action is provided.
82 * @param \mod_scorm\output\actionbar $actionbar actionbar object.
83 * @return bool|string rendered HTML for the report action.
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));
89 /**
90 * Rendered HTML for the user report action is provided
92 * @param \mod_scorm\output\userreportsactionbar $userreportsactionbar userreportsactionbar object
93 * @return string rendered HTML for the user report action.
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));
99 /**
100 * Generate the SCORM's "Exit activity" button
102 * @param string $url The url to be hooked up to the exit button
103 * @return string
105 public function generate_exitbar(string $url): string {
106 return $this->render_from_template('mod_scorm/player_exitbar', ['action' => $url]);
111 * Component representing a SCORM attempts bar.
113 * @copyright 2013 Dan Marsden
114 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
115 * @package mod_scorm
117 class mod_scorm_attempt_bar implements renderable {
120 * @var array An array of the attemptids
122 public $attemptids;
125 * @var int The attempt you are currently viewing.
127 public $attempt;
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.
135 public $baseurl;
138 * @var string This is the variable name that you use for the attempt in your
139 * code (ie. 'tablepage', 'blogpage', etc)
141 public $pagevar;
144 * @var string A HTML link representing the "previous" attempt.
146 public $previouslink = null;
149 * @var string A HTML link representing the "next" attempt.
151 public $nextlink = null;
154 * @var array An array of strings. One of them is just a string: the current attempt
156 public $attemptlinks = array();
159 * Constructor mod_scorm_attempt_bar with only the required params.
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
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;
174 * Prepares the scorm attempt bar for output.
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.
179 * @param renderer_base $output
180 * @param moodle_page $page
181 * @param string $target
182 * @throws coding_exception
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.');
188 if (!isset($this->attempt) || is_null($this->attempt)) {
189 throw new coding_exception('mod_scorm_attempt_bar requires a attempt value.');
191 if (empty($this->baseurl)) {
192 throw new coding_exception('mod_scorm_attempt_bar requires a baseurl value.');
195 if (count($this->attemptids) > 1) {
196 $lastattempt = end($this->attemptids); // Get last attempt.
197 $firstattempt = reset($this->attemptids); // get first attempt.
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;
215 $previous = $attemptid; // Store this attempt as previous in case we need it.
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'));
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'));