Merge branch 'MDL-47746-master' of git://github.com/zbdd/moodle
[moodle.git] / grade / report / singleview / classes / local / screen / user.php
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/>.
17 /**
18  * The user screen.
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  */
25 namespace gradereport_singleview\local\screen;
27 use grade_seq;
28 use gradereport_singleview;
29 use moodle_url;
30 use pix_icon;
31 use html_writer;
32 use gradereport_singleview\local\ui\range;
33 use gradereport_singleview\local\ui\bulk_insert;
34 use grade_item;
35 use grade_grade;
36 use stdClass;
38 defined('MOODLE_INTERNAL') || die;
40 /**
41  * The user screen.
42  *
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
46  */
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;
55     /**
56      * Get the description for the screen.
57      *
58      * @return string
59      */
60     public function description() {
61         return get_string('gradeitems', 'grades');
62     }
64     /**
65      * Convert the list of items to a list of options.
66      *
67      * @return array
68      */
69     public function options() {
70         $result = array();
71         foreach ($this->items as $itemid => $item) {
72             $result[$itemid] = $item->get_name();
73         }
74         return $result;
75     }
77     /**
78      * Get the type of items on this screen.
79      *
80      * @return string
81      */
82     public function item_type() {
83         return 'grade';
84     }
86     /**
87      * Should we show the group selector on this screen?
88      *
89      * @return bool
90      */
91     public function display_group_selector() {
92         return false;
93     }
95     /**
96      * Init the screen
97      *
98      * @param bool $selfitemisempty Have we selected an item yet?
99      */
100     public function init($selfitemisempty = false) {
101         global $DB;
103         if (!$selfitemisempty) {
104             $this->item = $DB->get_record('user', array('id' => $this->itemid));
105         }
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;
114             }
115         }
117         $this->items = array();
118         foreach ($seq->items as $itemid => $item) {
119             if (grade::filter($item)) {
120                 $this->items[$itemid] = $item;
121             }
122         }
124         $this->requirespaging = count($this->items) > $this->perpage;
126         $this->setup_structure();
128         $this->definition = array(
129             'finalgrade', 'feedback', 'override', 'exclude'
130         );
131         $this->set_headers($this->original_headers());
132     }
134     /**
135      * Get the list of headers for the table.
136      *
137      * @return array List of headers
138      */
139     public function original_headers() {
140         return array(
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')
149         );
150     }
152     /**
153      * Format each row of the table.
154      *
155      * @param grade_item $item
156      * @return string
157      */
158     public function format_line($item) {
159         global $OUTPUT;
161         $grade = $this->fetch_grade_or_default($item, $this->item->id);
162         $lockicon = '';
164         $lockeditem = $lockeditemgrade = 0;
165         if (!empty($grade->locked)) {
166             $lockeditem = 1;
167         }
168         if (!empty($grade->grade_item->locked)) {
169             $lockeditemgrade = 1;
170         }
171         // Check both grade and grade item.
172         if ($lockeditem || $lockeditemgrade) {
173              $lockicon = $OUTPUT->pix_icon('t/locked', 'grade is locked');
174         }
176         $realmodid = '';
177         if (isset($item->cmid)) {
178             $realmodid = $item->cmid;
179         }
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';
188         } else {
189             $gradetreeitem['type'] = 'item';
190         }
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());
201         }
203         $line = array(
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),
207             new range($item)
208         );
209         $lineclasses = array(
210             "action",
211             "gradeitem",
212             "category",
213             "range"
214         );
216         $outputline = array();
217         $i = 0;
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";
223             }
224             if (array_key_exists($key, $lineclasses)) {
225                 $cell->attributes['class'] = $lineclasses[$key];
226             }
227             $outputline[] = $cell;
228             $i++;
229         }
231         return $this->format_definition($outputline, $grade);
232     }
234     /**
235      * Helper to get the icon for an item.
236      *
237      * @param grade_item $item
238      * @return string
239      */
240     private function format_icon($item) {
241         $element = array('type' => 'item', 'object' => $item);
242         return $this->structure->get_element_icon($element);
243     }
245     /**
246      * Helper to get the category for an item.
247      *
248      * @param grade_item $item
249      * @return grade_category
250      */
251     private function category($item) {
252         global $DB;
254         if (empty($item->categoryid)) {
256             if ($item->itemtype == 'course') {
257                 return $this->course->fullname;
258             }
260             $params = array('id' => $item->iteminstance);
261             $elem = $DB->get_record('grade_categories', $params);
263             return $elem->fullname;
264         }
266         if (!isset($this->categories[$item->categoryid])) {
267             $category = $item->get_parent_category();
269             $this->categories[$category->id] = $category;
270         }
272         return $this->categories[$item->categoryid]->get_name();
273     }
275     /**
276      * Get the heading for the page.
277      *
278      * @return string
279      */
280     public function heading() {
281         return fullname($this->item);
282     }
284     /**
285      * Get the summary for this table.
286      *
287      * @return string
288      */
289     public function summary() {
290         return get_string('summaryuser', 'gradereport_singleview');
291     }
293     /**
294      * Default pager
295      *
296      * @return string
297      */
298     public function pager() {
299         global $OUTPUT;
301         if (!$this->supports_paging()) {
302             return '';
303         }
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,
312                 'item' => 'user'
313             ))
314         );
315     }
317     /**
318      * Does this page require paging?
319      *
320      * @return bool
321      */
322     public function supports_paging() {
323         return $this->requirespaging;
324     }
327     /**
328      * Process the data from the form.
329      *
330      * @param array $data
331      * @return array of warnings
332      */
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)) {
347                     continue;
348                 }
350                 $grade = grade_grade::fetch(array(
351                     'itemid' => $gradeitem->id,
352                     'userid' => $userid
353                 ));
355                 $data->$field = empty($grade) ? $null : $grade->finalgrade;
356                 $data->{"old$field"} = $data->$field;
357             }
359             foreach ($data as $varname => $value) {
360                 if (!preg_match('/^finalgrade_(\d+)_/', $varname, $matches)) {
361                     continue;
362                 }
364                 $gradeitem = grade_item::fetch(array(
365                     'courseid' => $this->courseid,
366                     'id' => $matches[1]
367                 ));
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)) ?
375                         -1 : $insertvalue;
376                 }
377             }
378         }
380         return parent::process($data);
381     }