weekly on-sync release 2.8dev
[moodle.git] / mod / scorm / renderer.php
CommitLineData
b5656373
DM
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
26defined('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 */
34class 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
81/**
82 * Component representing a SCORM attempts bar.
83 *
84 * @copyright 2013 Dan Marsden
85 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
86 * @package mod_scorm
87 */
88class mod_scorm_attempt_bar implements renderable {
89
90 /**
91 * @var array An array of the attemptids
92 */
93 public $attemptids;
94
95 /**
96 * @var int The attempt you are currently viewing.
97 */
98 public $attempt;
99
100 /**
101 * @var string|moodle_url If this is a string then it is the url which will be appended with $pagevar,
102 * an equals sign and the attempt number.
103 * If this is a moodle_url object then the pagevar param will be replaced by
104 * the attempt no, for each attempt.
105 */
106 public $baseurl;
107
108 /**
109 * @var string This is the variable name that you use for the attempt in your
110 * code (ie. 'tablepage', 'blogpage', etc)
111 */
112 public $pagevar;
113
114 /**
115 * @var string A HTML link representing the "previous" attempt.
116 */
117 public $previouslink = null;
118
119 /**
120 * @var string A HTML link representing the "next" attempt.
121 */
122 public $nextlink = null;
123
124 /**
125 * @var array An array of strings. One of them is just a string: the current attempt
126 */
127 public $attemptlinks = array();
128
129 /**
130 * Constructor mod_scorm_attempt_bar with only the required params.
131 *
132 * @param array $attemptids an array of attempts the user has made
133 * @param int $attempt The attempt you are currently viewing
134 * @param string|moodle_url $baseurl url of the current page, the $pagevar parameter is added
135 * @param string $pagevar name of page parameter that holds the attempt number
136 */
137 public function __construct($attemptids, $attempt, $baseurl, $pagevar = 'page') {
138 $this->attemptids = $attemptids;
139 $this->attempt = $attempt;
140 $this->baseurl = $baseurl;
141 $this->pagevar = $pagevar;
142 }
143
144 /**
145 * Prepares the scorm attempt bar for output.
146 *
147 * This method validates the arguments set up for the scorm attempt bar and then
148 * produces fragments of HTML to assist display later on.
149 *
150 * @param renderer_base $output
151 * @param moodle_page $page
152 * @param string $target
153 * @throws coding_exception
154 */
155 public function prepare(renderer_base $output, moodle_page $page, $target) {
156 if (empty($this->attemptids)) {
157 throw new coding_exception('mod_scorm_attempt_bar requires a attemptids value.');
158 }
159 if (!isset($this->attempt) || is_null($this->attempt)) {
160 throw new coding_exception('mod_scorm_attempt_bar requires a attempt value.');
161 }
162 if (empty($this->baseurl)) {
163 throw new coding_exception('mod_scorm_attempt_bar requires a baseurl value.');
164 }
165
166 if (count($this->attemptids) > 1) {
167 $lastattempt = end($this->attemptids); // Get last attempt.
168 $firstattempt = reset($this->attemptids); // get first attempt.
169
170 $nextattempt = 0;
171 $prevattempt = null;
172 $previous = 0;
173 foreach ($this->attemptids as $attemptid) {
174 if ($this->attempt == $attemptid) {
175 $this->attemptlinks[] = $attemptid;
176 $prevattempt = $previous;
177 } else {
178 $attemptlink = html_writer::link(
179 new moodle_url($this->baseurl, array($this->pagevar=>$attemptid)), $attemptid);
180 $this->attemptlinks[] = $attemptlink;
181 if (empty($nextattempt) && $prevattempt !== null) {
182 // Set the nextattempt var as we have set previous attempt earlier.
183 $nextattempt = $attemptid;
184 }
185 }
186 $previous = $attemptid; // Store this attempt as previous in case we need it.
187 }
188
189 if ($this->attempt != $firstattempt) {
190 $this->previouslink = html_writer::link(
191 new moodle_url($this->baseurl, array($this->pagevar=>$prevattempt)),
192 get_string('previous'), array('class'=>'previous'));
193 }
194
195 if ($this->attempt != $lastattempt) {
196 $this->nextlink = html_writer::link(
197 new moodle_url($this->baseurl, array($this->pagevar=>$nextattempt)),
198 get_string('next'), array('class'=>'next'));
199 }
200 }
201 }
202}