PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/h5pactivity/classes/output/attempt.php

https://bitbucket.org/moodle/moodle
PHP | 228 lines | 140 code | 16 blank | 72 comment | 19 complexity | f3053bf1a15272f1cc2f34a8f238f68b MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Contains class mod_h5pactivity\output\reportlink
  18. *
  19. * @package mod_h5pactivity
  20. * @copyright 2020 Ferran Recio
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. namespace mod_h5pactivity\output;
  24. defined('MOODLE_INTERNAL') || die();
  25. use mod_h5pactivity\local\attempt as activity_attempt;
  26. use renderable;
  27. use templatable;
  28. use renderer_base;
  29. use moodle_url;
  30. use user_picture;
  31. use stdClass;
  32. /**
  33. * Class to help display report link in mod_h5pactivity.
  34. *
  35. * @copyright 2020 Ferran Recio
  36. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37. */
  38. class attempt implements renderable, templatable {
  39. /** @var activity_attempt attempt */
  40. public $attempt;
  41. /** @var stdClass user record */
  42. public $user;
  43. /** @var int courseid necesary to present user picture */
  44. public $courseid;
  45. /**
  46. * Constructor.
  47. *
  48. * @param activity_attempt $attempt the attempt object
  49. * @param stdClass $user a user record (default null).
  50. * @param int $courseid optional course id (default null).
  51. */
  52. public function __construct(activity_attempt $attempt, stdClass $user = null, int $courseid = null) {
  53. $this->attempt = $attempt;
  54. $this->user = $user;
  55. $this->courseid = $courseid;
  56. }
  57. /**
  58. * Export this data so it can be used as the context for a mustache template.
  59. *
  60. * @param renderer_base $output
  61. * @return stdClass
  62. */
  63. public function export_for_template(renderer_base $output) {
  64. $attempt = $this->attempt;
  65. $data = (object)[
  66. 'id' => $attempt->get_id(),
  67. 'h5pactivityid' => $attempt->get_h5pactivityid(),
  68. 'userid' => $attempt->get_userid(),
  69. 'timecreated' => $attempt->get_timecreated(),
  70. 'timemodified' => $attempt->get_timemodified(),
  71. 'attempt' => $attempt->get_attempt(),
  72. 'rawscore' => $attempt->get_rawscore(),
  73. 'maxscore' => $attempt->get_maxscore(),
  74. 'duration' => '-',
  75. 'durationcompact' => '-',
  76. 'completion' => $attempt->get_completion(),
  77. 'completionicon' => $this->completion_icon($output, $attempt->get_completion()),
  78. 'completiontext' => $this->completion_icon($output, $attempt->get_completion(), true),
  79. 'success' => $attempt->get_success(),
  80. 'successicon' => $this->success_icon($output, $attempt->get_success()),
  81. 'successtext' => $this->success_icon($output, $attempt->get_success(), true),
  82. 'scaled' => $attempt->get_scaled(),
  83. 'reporturl' => new moodle_url('/mod/h5pactivity/report.php', [
  84. 'a' => $attempt->get_h5pactivityid(), 'attemptid' => $attempt->get_id()
  85. ]),
  86. ];
  87. if ($attempt->get_duration() !== null) {
  88. $data->durationvalue = $attempt->get_duration();
  89. $duration = $this->extract_duration($data->durationvalue);
  90. $data->duration = $this->format_duration($duration);
  91. $data->durationcompact = $this->format_duration_short($duration);
  92. }
  93. if (!empty($data->maxscore)) {
  94. $data->score = get_string('score_out_of', 'mod_h5pactivity', $data);
  95. }
  96. if ($this->user) {
  97. $data->user = $this->user;
  98. $userpicture = new user_picture($this->user);
  99. $userpicture->courseid = $this->courseid;
  100. $data->user->picture = $output->render($userpicture);
  101. $data->user->fullname = fullname($this->user);
  102. }
  103. return $data;
  104. }
  105. /**
  106. * Return a completion icon HTML.
  107. *
  108. * @param renderer_base $output the renderer base object
  109. * @param int|null $completion the current completion value
  110. * @param bool $showtext if the icon must have a text or only icon
  111. * @return string icon HTML
  112. */
  113. private function completion_icon(renderer_base $output, int $completion = null, bool $showtext = false): string {
  114. if ($completion === null) {
  115. return '';
  116. }
  117. if ($completion) {
  118. $alt = get_string('attempt_completion_yes', 'mod_h5pactivity');
  119. $icon = 'i/completion-auto-y';
  120. } else {
  121. $alt = get_string('attempt_completion_no', 'mod_h5pactivity');
  122. $icon = 'i/completion-auto-n';
  123. }
  124. $text = '';
  125. if ($showtext) {
  126. $text = $alt;
  127. $alt = '';
  128. }
  129. return $output->pix_icon($icon, $alt).$text;
  130. }
  131. /**
  132. * Return a success icon
  133. * @param renderer_base $output the renderer base object
  134. * @param int|null $success the current success value
  135. * @param bool $showtext if the icon must have a text or only icon
  136. * @return string icon HTML
  137. */
  138. private function success_icon(renderer_base $output, int $success = null, bool $showtext = false): string {
  139. if ($success === null) {
  140. $alt = get_string('attempt_success_unknown', 'mod_h5pactivity');
  141. if ($showtext) {
  142. return $alt;
  143. }
  144. $icon = 'i/empty';
  145. } else if ($success) {
  146. $alt = get_string('attempt_success_pass', 'mod_h5pactivity');
  147. $icon = 'i/checkedcircle';
  148. } else {
  149. $alt = get_string('attempt_success_fail', 'mod_h5pactivity');
  150. $icon = 'i/uncheckedcircle';
  151. }
  152. $text = '';
  153. if ($showtext) {
  154. $text = $alt;
  155. $alt = '';
  156. }
  157. return $output->pix_icon($icon, $alt).$text;
  158. }
  159. /**
  160. * Return the duration in long format (localized)
  161. *
  162. * @param stdClass $duration object with (h)hours, (m)minutes and (s)seconds
  163. * @return string the long format duration
  164. */
  165. private function format_duration (stdClass $duration): string {
  166. $result = [];
  167. if ($duration->h) {
  168. $result[] = get_string('numhours', 'moodle', $duration->h);
  169. }
  170. if ($duration->m) {
  171. $result[] = get_string('numminutes', 'moodle', $duration->m);
  172. }
  173. if ($duration->s) {
  174. $result[] = get_string('numseconds', 'moodle', $duration->s);
  175. }
  176. return implode(' ', $result);
  177. }
  178. /**
  179. * Return the duration en short format (for example: 145' 43'')
  180. *
  181. * Note: this method is used to make duration responsive.
  182. *
  183. * @param stdClass $duration object with (h)hours, (m)minutes and (s)seconds
  184. * @return string the short format duration
  185. */
  186. private function format_duration_short (stdClass $duration): string {
  187. $result = [];
  188. if ($duration->h || $duration->m) {
  189. $result[] = ($duration->h * 60 + $duration->m)."'";
  190. }
  191. if ($duration->s) {
  192. $result[] = $duration->s."''";
  193. }
  194. return implode(' ', $result);
  195. }
  196. /**
  197. * Extract hours and minutes from second duration.
  198. *
  199. * Note: this function is used to generate the param for format_duration
  200. * and format_duration_short
  201. *
  202. * @param int $seconds number of second
  203. * @return stdClass with (h)hours, (m)minutes and (s)seconds
  204. */
  205. private function extract_duration (int $seconds): stdClass {
  206. $h = floor($seconds / 3600);
  207. $m = floor(($seconds - $h * 3600) / 60);
  208. $s = $seconds - ($h * 3600 + $m * 60);
  209. return (object)['h' => $h, 'm' => $m, 's' => $s];
  210. }
  211. }