2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
25 namespace gradereport_singleview\local\screen;
28 use gradereport_singleview;
32 use gradereport_singleview\local\ui\range;
33 use gradereport_singleview\local\ui\bulk_insert;
38 defined('MOODLE_INTERNAL') || die;
43 * @package gradereport_singleview
44 * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47 class user extends tablelike implements selectable_items {
49 /** @var array $categories A cache for grade_item categories */
50 private $categories = array();
52 /** @var int $requirespaging Do we have more items than the paging limit? */
53 private $requirespaging = true;
56 * Get the description for the screen.
60 public function description() {
61 return get_string('gradeitems', 'grades');
65 * Convert the list of items to a list of options.
69 public function options() {
71 foreach ($this->items as $itemid => $item) {
72 $result[$itemid] = $item->get_name();
78 * Get the type of items on this screen.
82 public function item_type() {
87 * Should we show the group selector on this screen?
91 public function display_group_selector() {
98 * @param bool $selfitemisempty Have we selected an item yet?
100 public function init($selfitemisempty = false) {
103 if (!$selfitemisempty) {
104 $this->item = $DB->get_record('user', array('id' => $this->itemid));
107 $params = array('courseid' => $this->courseid);
109 $seq = new grade_seq($this->courseid, true);
110 foreach ($seq->items as $key => $item) {
111 if (isset($item->itemmodule)) {
112 list($courseid, $cmid) = get_course_and_cm_from_instance($item->iteminstance, $item->itemmodule);
113 $seq->items[$key]->cmid = $cmid->id;
117 $this->items = array();
118 foreach ($seq->items as $itemid => $item) {
119 if (grade::filter($item)) {
120 $this->items[$itemid] = $item;
124 $this->requirespaging = count($this->items) > $this->perpage;
126 $this->setup_structure();
128 $this->definition = array(
129 'finalgrade', 'feedback', 'override', 'exclude'
131 $this->set_headers($this->original_headers());
135 * Get the list of headers for the table.
137 * @return array List of headers
139 public function original_headers() {
141 '', // For filter icon.
142 get_string('assessmentname', 'gradereport_singleview'),
143 get_string('gradecategory', 'grades'),
144 get_string('range', 'grades'),
145 get_string('grade', 'grades'),
146 get_string('feedback', 'grades'),
147 $this->make_toggle_links('override'),
148 $this->make_toggle_links('exclude')
153 * Format each row of the table.
155 * @param grade_item $item
158 public function format_line($item) {
161 $grade = $this->fetch_grade_or_default($item, $this->item->id);
164 $lockeditem = $lockeditemgrade = 0;
165 if (!empty($grade->locked)) {
168 if (!empty($grade->grade_item->locked)) {
169 $lockeditemgrade = 1;
171 // Check both grade and grade item.
172 if ($lockeditem || $lockeditemgrade) {
173 $lockicon = $OUTPUT->pix_icon('t/locked', 'grade is locked');
177 if (isset($item->cmid)) {
178 $realmodid = $item->cmid;
181 $iconstring = get_string('filtergrades', 'gradereport_singleview', $item->get_name());
183 // Create a fake gradetreeitem so we can call get_element_header().
184 // The type logic below is from grade_category->_get_children_recursion().
185 $gradetreeitem = array();
186 if (in_array($item->itemtype, array('course', 'category'))) {
187 $gradetreeitem['type'] = $item->itemtype.'item';
189 $gradetreeitem['type'] = 'item';
191 $gradetreeitem['object'] = $item;
192 $gradetreeitem['userid'] = $this->item->id;
194 $itemlabel = $this->structure->get_element_header($gradetreeitem, true, false, false, false, true);
195 $grade->label = $item->get_name();
197 $itemlabel = $item->get_name();
198 if (!empty($realmodid)) {
199 $url = new moodle_url('/mod/' . $item->itemmodule . '/view.php', array('id' => $realmodid));
200 $itemlabel = html_writer::link($url, $item->get_name());
204 $OUTPUT->action_icon($this->format_link('grade', $item->id), new pix_icon('t/editstring', $iconstring)),
205 $this->format_icon($item) . $lockicon . $itemlabel,
206 $this->category($item),
209 $lineclasses = array(
216 $outputline = array();
218 foreach ($line as $key => $value) {
219 $cell = new \html_table_cell($value);
220 if ($isheader = $i == 1) {
221 $cell->header = $isheader;
222 $cell->scope = "row";
224 if (array_key_exists($key, $lineclasses)) {
225 $cell->attributes['class'] = $lineclasses[$key];
227 $outputline[] = $cell;
231 return $this->format_definition($outputline, $grade);
235 * Helper to get the icon for an item.
237 * @param grade_item $item
240 private function format_icon($item) {
241 $element = array('type' => 'item', 'object' => $item);
242 return $this->structure->get_element_icon($element);
246 * Helper to get the category for an item.
248 * @param grade_item $item
249 * @return grade_category
251 private function category($item) {
254 if (empty($item->categoryid)) {
256 if ($item->itemtype == 'course') {
257 return $this->course->fullname;
260 $params = array('id' => $item->iteminstance);
261 $elem = $DB->get_record('grade_categories', $params);
263 return $elem->fullname;
266 if (!isset($this->categories[$item->categoryid])) {
267 $category = $item->get_parent_category();
269 $this->categories[$category->id] = $category;
272 return $this->categories[$item->categoryid]->get_name();
276 * Get the heading for the page.
280 public function heading() {
281 return fullname($this->item);
285 * Get the summary for this table.
289 public function summary() {
290 return get_string('summaryuser', 'gradereport_singleview');
298 public function pager() {
301 if (!$this->supports_paging()) {
305 return $OUTPUT->paging_bar(
306 count($this->items), $this->page, $this->perpage,
307 new moodle_url('/grade/report/singleview/index.php', array(
308 'perpage' => $this->perpage,
309 'id' => $this->courseid,
310 'groupid' => $this->groupid,
311 'itemid' => $this->itemid,
318 * Does this page require paging?
322 public function supports_paging() {
323 return $this->requirespaging;
328 * Process the data from the form.
331 * @return array of warnings
333 public function process($data) {
334 $bulk = new bulk_insert($this->item);
335 // Bulk insert messages the data to be passed in
336 // ie: for all grades of empty grades apply the specified value.
337 if ($bulk->is_applied($data)) {
338 $filter = $bulk->get_type($data);
339 $insertvalue = $bulk->get_insert_value($data);
340 // Appropriately massage data that may not exist.
342 $userid = $this->item->id;
343 foreach ($this->items as $gradeitemid => $gradeitem) {
344 $null = $gradeitem->gradetype == GRADE_TYPE_SCALE ? -1 : '';
345 $field = "finalgrade_{$gradeitem->id}_{$gradeitemid}";
346 if (isset($data->$field)) {
350 $grade = grade_grade::fetch(array(
351 'itemid' => $gradeitem->id,
355 $data->$field = empty($grade) ? $null : $grade->finalgrade;
356 $data->{"old$field"} = $data->$field;
359 foreach ($data as $varname => $value) {
360 if (!preg_match('/^finalgrade_(\d+)_/', $varname, $matches)) {
364 $gradeitem = grade_item::fetch(array(
365 'courseid' => $this->courseid,
369 $isscale = ($gradeitem->gradetype == GRADE_TYPE_SCALE);
371 $empties = (trim($value) === '' or ($isscale and $value == -1));
373 if ($filter == 'all' or $empties) {
374 $data->$varname = ($isscale and empty($insertvalue)) ?
380 return parent::process($data);