Merge branch 'MDL-68343-master' of git://github.com/rezaies/moodle
[moodle.git] / grade / report / singleview / classes / local / screen / tablelike.php
CommitLineData
57fac09a
DW
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 * The gradebook simple view - base class for the table
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
25namespace gradereport_singleview\local\screen;
26
27use html_table;
28use html_writer;
29use stdClass;
30use grade_item;
31use grade_grade;
32use gradereport_singleview\local\ui\bulk_insert;
33
34defined('MOODLE_INTERNAL') || die;
35
36/**
37 * The gradebook simple view - base class for the table
38 *
39 * @package gradereport_singleview
40 * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
43abstract class tablelike extends screen {
44
45 /** @var array $headers A list of table headers */
46 protected $headers = array();
47
48 /** @var array $initerrors A list of errors that mean we should not show the table */
49 protected $initerrors = array();
50
51 /** @var array $definition Describes the columns in the table */
52 protected $definition = array();
53
54 /**
55 * Format a row of the table
56 *
57 * @param mixed $item
58 * @return string
59 */
60 public abstract function format_line($item);
61
43cd76e8
JC
62 /**
63 * Get the summary for this table.
64 *
65 * @return string
66 */
67 public abstract function summary();
68
57fac09a
DW
69 /**
70 * Get the table headers
71 *
72 * @return array
73 */
74 public function headers() {
75 return $this->headers;
76 }
77
78 /**
79 * Set the table headers
80 *
81 * @param array $overwrite New headers
82 * @return tablelike This
83 */
84 public function set_headers($overwrite) {
85 $this->headers = $overwrite;
86 return $this;
87 }
88
89 /**
90 * Get the list of errors
91 *
92 * @return array
93 */
94 public function init_errors() {
95 return $this->initerrors;
96 }
97
98 /**
99 * Set an error detected while building the page.
100 *
101 * @param string $mesg
102 */
103 public function set_init_error($mesg) {
104 $this->initerrors[] = $mesg;
105 }
106
107 /**
108 * Get the table definition
109 *
110 * @return array The definition.
111 */
112 public function definition() {
113 return $this->definition;
114 }
115
116 /**
117 * Set the table definition
118 *
119 * @param array $overwrite New definition
120 * @return tablelike This
121 */
122 public function set_definition($overwrite) {
123 $this->definition = $overwrite;
124 return $this;
125 }
126
57fac09a
DW
127 /**
128 * Get a element to generate the HTML for this table row
129 * @param array $line This is a list of lines in the table (modified)
130 * @param grade_grade $grade The grade.
131 * @return string
132 */
133 public function format_definition($line, $grade) {
134 foreach ($this->definition() as $i => $field) {
135 // Table tab index.
136 $tab = ($i * $this->total) + $this->index;
137 $classname = '\\gradereport_singleview\\local\\ui\\' . $field;
138 $html = new $classname($grade, $tab);
139
140 if ($field == 'finalgrade' and !empty($this->structure)) {
141 $html .= $this->structure->get_grade_analysis_icon($grade);
142 }
143
43cd0e3c
TN
144 // Singleview users without proper permissions should be presented
145 // disabled checkboxes for the Exclude grade attribute.
146 if ($field == 'exclude' && !has_capability('moodle/grade:manage', $this->context)){
ae66ed23 147 $html->disabled = true;
43cd0e3c
TN
148 }
149
57fac09a
DW
150 $line[] = $html;
151 }
152 return $line;
153 }
154
155 /**
156 * Get the HTML for the whole table
157 * @return string
158 */
159 public function html() {
160 global $OUTPUT;
161
162 if (!empty($this->initerrors)) {
163 $warnings = '';
164 foreach ($this->initerrors as $mesg) {
165 $warnings .= $OUTPUT->notification($mesg);
166 }
167 return $warnings;
168 }
169 $table = new html_table();
170
171 $table->head = $this->headers();
172
43cd76e8
JC
173 $summary = $this->summary();
174 if (!empty($summary)) {
976dac3f
SR
175 $table->caption = $summary;
176 $table->captionhide = true;
43cd76e8
JC
177 }
178
57fac09a
DW
179 // To be used for extra formatting.
180 $this->index = 0;
181 $this->total = count($this->items);
182
183 foreach ($this->items as $item) {
184 if ($this->index >= ($this->perpage * $this->page) &&
185 $this->index < ($this->perpage * ($this->page + 1))) {
186 $table->data[] = $this->format_line($item);
187 }
188 $this->index++;
189 }
190
191 $underlying = get_class($this);
192
2a6cdc51 193 $data = new stdClass();
57fac09a
DW
194 $data->table = $table;
195 $data->instance = $this;
196
197 $buttonattr = array('class' => 'singleview_buttons submit');
198 $buttonhtml = implode(' ', $this->buttons());
199
200 $buttons = html_writer::tag('div', $buttonhtml, $buttonattr);
2a6cdc51 201 $selectview = new select($this->courseid, $this->itemid, $this->groupid);
57fac09a 202
f548d49f
ZD
203 $sessionvalidation = html_writer::empty_tag('input',
204 array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
205
2a6cdc51
DW
206 $html = $selectview->html();
207 $html .= html_writer::tag('form',
f548d49f 208 $buttons . html_writer::table($table) . $this->bulk_insert() . $buttons . $sessionvalidation,
57fac09a
DW
209 array('method' => 'POST')
210 );
2a6cdc51
DW
211 $html .= $selectview->html();
212 return $html;
57fac09a
DW
213 }
214
215 /**
216 * Get the HTML for the bulk insert form
217 *
218 * @return string
219 */
220 public function bulk_insert() {
221 return html_writer::tag(
222 'div',
223 (new bulk_insert($this->item))->html(),
224 array('class' => 'singleview_bulk')
225 );
226 }
227
228 /**
229 * Get the buttons for saving changes.
230 *
231 * @return array
232 */
233 public function buttons() {
ec3857fe
FM
234 global $OUTPUT;
235
236 $save = $OUTPUT->render_from_template('gradereport_singleview/button', [
57fac09a 237 'type' => 'submit',
bd6bfb7f 238 'value' => get_string('save', 'gradereport_singleview'),
ec3857fe 239 ]);
57fac09a
DW
240
241 return array($save);
242 }
243}