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 '', // For activity icon.
143 get_string('assessmentname', 'gradereport_singleview'),
144 get_string('gradecategory', 'grades'),
145 get_string('range', 'grades'),
146 get_string('grade', 'grades'),
147 get_string('feedback', 'grades'),
148 $this->make_toggle_links('override'),
149 $this->make_toggle_links('exclude')
154 * Format each row of the table.
156 * @param grade_item $item
159 public function format_line($item) {
162 $grade = $this->fetch_grade_or_default($item, $this->item
->id
);
165 $lockeditem = $lockeditemgrade = 0;
166 if (!empty($grade->locked
)) {
169 if (!empty($grade->grade_item
->locked
)) {
170 $lockeditemgrade = 1;
172 // Check both grade and grade item.
173 if ($lockeditem ||
$lockeditemgrade) {
174 $lockicon = $OUTPUT->pix_icon('t/locked', 'grade is locked');
178 if (isset($item->cmid
)) {
179 $realmodid = $item->cmid
;
182 $iconstring = get_string('filtergrades', 'gradereport_singleview', $item->get_name());
184 // Create a fake gradetreeitem so we can call get_element_header().
185 // The type logic below is from grade_category->_get_children_recursion().
186 $gradetreeitem = array();
187 if (in_array($item->itemtype
, array('course', 'category'))) {
188 $gradetreeitem['type'] = $item->itemtype
.'item';
190 $gradetreeitem['type'] = 'item';
192 $gradetreeitem['object'] = $item;
193 $gradetreeitem['userid'] = $this->item
->id
;
195 $itemlabel = $this->structure
->get_element_header($gradetreeitem, true, false);
196 $grade->label
= $item->get_name();
199 $OUTPUT->action_icon($this->format_link('grade', $item->id
), new pix_icon('t/editstring', $iconstring)),
200 $this->format_icon($item) . $lockicon, $itemlabel, $this->category($item), (new range($item))
202 return $this->format_definition($line, $grade);
206 * Helper to get the icon for an item.
208 * @param grade_item $item
211 private function format_icon($item) {
212 $element = array('type' => 'item', 'object' => $item);
213 return $this->structure
->get_element_icon($element);
217 * Helper to get the category for an item.
219 * @param grade_item $item
220 * @return grade_category
222 private function category($item) {
225 if (empty($item->categoryid
)) {
227 if ($item->itemtype
== 'course') {
228 return $this->course
->fullname
;
231 $params = array('id' => $item->iteminstance
);
232 $elem = $DB->get_record('grade_categories', $params);
234 return $elem->fullname
;
237 if (!isset($this->categories
[$item->categoryid
])) {
238 $category = $item->get_parent_category();
240 $this->categories
[$category->id
] = $category;
243 return $this->categories
[$item->categoryid
]->get_name();
247 * Get the heading for the page.
251 public function heading() {
252 return fullname($this->item
);
260 public function pager() {
263 if (!$this->supports_paging()) {
267 return $OUTPUT->paging_bar(
268 count($this->items
), $this->page
, $this->perpage
,
269 new moodle_url('/grade/report/singleview/index.php', array(
270 'perpage' => $this->perpage
,
271 'id' => $this->courseid
,
272 'groupid' => $this->groupid
,
273 'itemid' => $this->itemid
,
280 * Does this page require paging?
284 public function supports_paging() {
285 return $this->requirespaging
;
290 * Process the data from the form.
293 * @return array of warnings
295 public function process($data) {
296 $bulk = new bulk_insert($this->item
);
297 // Bulk insert messages the data to be passed in
298 // ie: for all grades of empty grades apply the specified value.
299 if ($bulk->is_applied($data)) {
300 $filter = $bulk->get_type($data);
301 $insertvalue = $bulk->get_insert_value($data);
302 // Appropriately massage data that may not exist.
304 $userid = $this->item
->id
;
305 foreach ($this->items
as $gradeitemid => $gradeitem) {
306 $null = $gradeitem->gradetype
== GRADE_TYPE_SCALE ?
-1 : '';
307 $field = "finalgrade_{$gradeitem->id}_{$gradeitemid}";
308 if (isset($data->$field)) {
312 $grade = grade_grade
::fetch(array(
313 'itemid' => $gradeitem->id
,
317 $data->$field = empty($grade) ?
$null : $grade->finalgrade
;
318 $data->{"old$field"} = $data->$field;
321 foreach ($data as $varname => $value) {
322 if (!preg_match('/^finalgrade_(\d+)_/', $varname, $matches)) {
326 $gradeitem = grade_item
::fetch(array(
327 'courseid' => $this->courseid
,
331 $isscale = ($gradeitem->gradetype
== GRADE_TYPE_SCALE
);
333 $empties = (trim($value) === '' or ($isscale and $value == -1));
335 if ($filter == 'all' or $empties) {
336 $data->$varname = ($isscale and empty($insertvalue)) ?
342 return parent
::process($data);