1 |
efrain |
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 |
* UI element for a text input field.
|
|
|
19 |
*
|
|
|
20 |
* @package gradereport_singleview
|
|
|
21 |
* @copyright 2014 Moodle Pty Ltd (http://moodle.com)
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace gradereport_singleview\local\ui;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* UI element for a text input field.
|
|
|
31 |
*
|
|
|
32 |
* @package gradereport_singleview
|
|
|
33 |
* @copyright 2014 Moodle Pty Ltd (http://moodle.com)
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class text_attribute extends element {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Is this input disabled?
|
|
|
40 |
* @var bool $isdisabled
|
|
|
41 |
*/
|
|
|
42 |
private $isdisabled;
|
|
|
43 |
|
|
|
44 |
/** @var bool If this is a read-only input. */
|
|
|
45 |
private bool $isreadonly;
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* @var string|null The input type to pass to the template.
|
|
|
49 |
* This defaults to text but can be overridden to number for grade inputs.
|
|
|
50 |
*/
|
|
|
51 |
private $type = null;
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* @var string|null The value to set for the input's `min` attribute.
|
|
|
55 |
* This is set if a minimum grade is provided for the grade input field.
|
|
|
56 |
*/
|
|
|
57 |
private $min = null;
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* @var string|null The value to set for the input's `max` attribute.
|
|
|
61 |
* This is set if a maximum grade is provided for the grade input field.
|
|
|
62 |
*/
|
|
|
63 |
private $max = null;
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Constructor
|
|
|
67 |
*
|
|
|
68 |
* @param string $name The input name (the first bit)
|
|
|
69 |
* @param string $value The input initial value.
|
|
|
70 |
* @param string $label The label for this input field.
|
|
|
71 |
* @param bool $isdisabled Is this input disabled.
|
|
|
72 |
* @param bool $isreadonly If this is a read-only input.
|
|
|
73 |
*/
|
|
|
74 |
public function __construct(string $name, string $value, string $label, bool $isdisabled = false, bool $isreadonly = false) {
|
|
|
75 |
$this->isdisabled = $isdisabled;
|
|
|
76 |
$this->isreadonly = $isreadonly;
|
|
|
77 |
parent::__construct($name, $value, $label);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Nasty function allowing custom textbox behaviour outside the class.
|
|
|
82 |
* @return bool Is this a textbox.
|
|
|
83 |
*/
|
|
|
84 |
public function is_textbox(): bool {
|
|
|
85 |
return true;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Render the html for this field.
|
|
|
90 |
* @return string The HTML.
|
|
|
91 |
*/
|
|
|
92 |
public function html(): string {
|
|
|
93 |
global $OUTPUT;
|
|
|
94 |
|
|
|
95 |
$context = (object) [
|
|
|
96 |
'id' => $this->name,
|
|
|
97 |
'name' => $this->name,
|
|
|
98 |
'value' => $this->value,
|
|
|
99 |
'disabled' => $this->isdisabled,
|
|
|
100 |
'readonly' => $this->isreadonly,
|
|
|
101 |
];
|
|
|
102 |
|
|
|
103 |
$context->label = '';
|
|
|
104 |
if (preg_match("/^feedback/", $this->name)) {
|
|
|
105 |
$context->label = get_string('feedbackfor', 'gradereport_singleview', $this->label);
|
|
|
106 |
} else if (preg_match("/^finalgrade/", $this->name)) {
|
|
|
107 |
$context->label = get_string('gradefor', 'gradereport_singleview', $this->label);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
// Set this input field with type="number" if the decimal separator for current language is set to a period.
|
|
|
111 |
// Other decimal separators may not be recognised by browsers yet which may cause issues when entering grades.
|
|
|
112 |
$decsep = get_string('decsep', 'core_langconfig');
|
|
|
113 |
$context->isnumeric = $this->type === 'number' && $decsep === '.';
|
|
|
114 |
if ($context->isnumeric) {
|
|
|
115 |
$context->type = $this->type;
|
|
|
116 |
$context->min = $this->min;
|
|
|
117 |
$context->max = $this->max;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
return $OUTPUT->render_from_template('gradereport_singleview/text_attribute', $context);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* Input type setter.
|
|
|
125 |
*
|
|
|
126 |
* @param string|null $type
|
|
|
127 |
* @return void
|
|
|
128 |
*/
|
|
|
129 |
public function set_type(?string $type): void {
|
|
|
130 |
$this->type = $type;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
/**
|
|
|
134 |
* Min attribute setter.
|
|
|
135 |
*
|
|
|
136 |
* @param string|null $min
|
|
|
137 |
* @return void
|
|
|
138 |
*/
|
|
|
139 |
public function set_min(?string $min): void {
|
|
|
140 |
$this->min = $min;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Max attribute setter.
|
|
|
145 |
*
|
|
|
146 |
* @param string|null $max
|
|
|
147 |
* @return void
|
|
|
148 |
*/
|
|
|
149 |
public function set_max(?string $max): void {
|
|
|
150 |
$this->max = $max;
|
|
|
151 |
}
|
|
|
152 |
}
|