PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/scorm/renderer.php

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