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();
198 $OUTPUT->action_icon($this->format_link('grade', $item->id), new pix_icon('t/editstring', $iconstring)),
199 $this->format_icon($item) . $lockicon . $itemlabel,
200 $this->category($item),
203 $lineclasses = array(
210 $outputline = array();
212 foreach ($line as $key => $value) {
213 $cell = new \html_table_cell($value);
214 if ($isheader = $i == 1) {
215 $cell->header = $isheader;
216 $cell->scope = "row";
218 if (array_key_exists($key, $lineclasses)) {
219 $cell->attributes['class'] = $lineclasses[$key];
221 $outputline[] = $cell;
225 return $this->format_definition($outputline, $grade);
229 * Helper to get the icon for an item.
231 * @param grade_item $item
234 private function format_icon($item) {
235 $element = array('type' => 'item', 'object' => $item);
236 return $this->structure->get_element_icon($element);
240 * Helper to get the category for an item.
242 * @param grade_item $item
243 * @return grade_category
245 private function category($item) {
248 if (empty($item->categoryid)) {
250 if ($item->itemtype == 'course') {
251 return $this->course->fullname;
254 $params = array('id' => $item->iteminstance);
255 $elem = $DB->get_record('grade_categories', $params);
257 return $elem->fullname;
260 if (!isset($this->categories[$item->categoryid])) {
261 $category = $item->get_parent_category();
263 $this->categories[$category->id] = $category;
266 return $this->categories[$item->categoryid]->get_name();
270 * Get the heading for the page.
274 public function heading() {
275 return fullname($this->item);
279 * Get the summary for this table.
283 public function summary() {
284 return get_string('summaryuser', 'gradereport_singleview');
292 public function pager() {
295 if (!$this->supports_paging()) {
299 return $OUTPUT->paging_bar(
300 count($this->items), $this->page, $this->perpage,
301 new moodle_url('/grade/report/singleview/index.php', array(
302 'perpage' => $this->perpage,
303 'id' => $this->courseid,
304 'groupid' => $this->groupid,
305 'itemid' => $this->itemid,
312 * Does this page require paging?
316 public function supports_paging() {
317 return $this->requirespaging;
322 * Process the data from the form.
325 * @return array of warnings
327 public function process($data) {
328 $bulk = new bulk_insert($this->item);
329 // Bulk insert messages the data to be passed in
330 // ie: for all grades of empty grades apply the specified value.
331 if ($bulk->is_applied($data)) {
332 $filter = $bulk->get_type($data);
333 $insertvalue = $bulk->get_insert_value($data);
334 // Appropriately massage data that may not exist.
336 $userid = $this->item->id;
337 foreach ($this->items as $gradeitemid => $gradeitem) {
338 $null = $gradeitem->gradetype == GRADE_TYPE_SCALE ? -1 : '';
339 $field = "finalgrade_{$gradeitem->id}_{$gradeitemid}";
340 if (isset($data->$field)) {
344 $grade = grade_grade::fetch(array(
345 'itemid' => $gradeitem->id,
349 $data->$field = empty($grade) ? $null : $grade->finalgrade;
350 $data->{"old$field"} = $data->$field;
353 foreach ($data as $varname => $value) {
354 if (!preg_match('/^finalgrade_(\d+)_/', $varname, $matches)) {
358 $gradeitem = grade_item::fetch(array(
359 'courseid' => $this->courseid,
363 $isscale = ($gradeitem->gradetype == GRADE_TYPE_SCALE);
365 $empties = (trim($value) === '' or ($isscale and $value == -1));
367 if ($filter == 'all' or $empties) {
368 $data->$varname = ($isscale and empty($insertvalue)) ?
374 return parent::process($data);