MDL-19404 Getting default values for grade_item object when creating a new grade_cate...
[moodle.git] / grade / report / overview / lib.php
CommitLineData
aa330ebb 1<?php // $Id$
8ad36f4c 2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
aa330ebb 25/**
26 * File in which the overview_report class is defined.
27 * @package gradebook
28 */
29
30require_once($CFG->dirroot . '/grade/report/lib.php');
31require_once($CFG->libdir.'/tablelib.php');
32
33/**
34 * Class providing an API for the overview report building and displaying.
35 * @uses grade_report
36 * @package gradebook
37 */
38class grade_report_overview extends grade_report {
39
40 /**
41 * The user.
42 * @var object $user
43 */
5c75a0a3 44 public $user;
aa330ebb 45
46 /**
47 * A flexitable to hold the data.
48 * @var object $table
49 */
5c75a0a3 50 public $table;
aa330ebb 51
60574063 52 /**
53 * show student ranks
54 */
5c75a0a3 55 public $showrank;
60574063 56
aa330ebb 57 /**
58 * Constructor. Sets local copies of user preferences and initialises grade_tree.
59 * @param int $userid
60 * @param object $gpr grade plugin return tracking object
61 * @param string $context
62 */
5c75a0a3 63 public function __construct($userid, $gpr, $context) {
64 global $CFG, $COURSE, $DB;
65 parent::__construct($COURSE->id, $gpr, $context);
aa330ebb 66
60574063 67 $this->showrank = grade_get_setting($this->courseid, 'report_overview_showrank', !empty($CFG->grade_report_overview_showrank));
68
aa330ebb 69 // get the user (for full name)
5c75a0a3 70 $this->user = $DB->get_record('user', array('id' => $userid));
aa330ebb 71
72 // base url for sorting by first/last name
73 $this->baseurl = $CFG->wwwroot.'/grade/overview/index.php?id='.$userid;
74 $this->pbarurl = $this->baseurl;
75
76 $this->setup_table();
77 }
78
79 /**
80 * Prepares the headers and attributes of the flexitable.
81 */
5c75a0a3 82 public function setup_table() {
aa330ebb 83 /*
60574063 84 * Table has 3 columns
85 *| course | final grade | rank (optional) |
86 */
aa330ebb 87
88 // setting up table headers
60574063 89 if ($this->showrank) {
90 $tablecolumns = array('coursename', 'grade', 'rank');
91 $tableheaders = array($this->get_lang_string('coursename', 'grades'),
92 $this->get_lang_string('grade'),
93 $this->get_lang_string('rank', 'grades'));
94 } else {
95 $tablecolumns = array('coursename', 'grade');
96 $tableheaders = array($this->get_lang_string('coursename', 'grades'),
97 $this->get_lang_string('grade'));
98 }
aa330ebb 99 $this->table = new flexible_table('grade-report-overview-'.$this->user->id);
100
101 $this->table->define_columns($tablecolumns);
102 $this->table->define_headers($tableheaders);
103 $this->table->define_baseurl($this->baseurl);
104
105 $this->table->set_attribute('cellspacing', '0');
106 $this->table->set_attribute('id', 'overview-grade');
107 $this->table->set_attribute('class', 'boxaligncenter generaltable');
108
109 $this->table->setup();
110 }
111
5c75a0a3 112 public function fill_table() {
00006755 113 global $CFG, $DB;
aa330ebb 114
9a10e2d8 115 // MDL-11679, only show 'mycourses' instead of all courses
252d14db 116 if ($courses = get_my_courses($this->user->id, 'c.sortorder ASC', 'id, shortname, showgrades')) {
38cb3391 117 $numusers = $this->get_numusers(false);
60574063 118
aa330ebb 119 foreach ($courses as $course) {
252d14db 120 if (!$course->showgrades) {
121 continue;
122 }
60574063 123 $courselink = '<a href="'.$CFG->wwwroot.'/grade/report/user/index.php?id='.$course->id.'">'.$course->shortname.'</a>';
00006755 124 $canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $course->id));
60574063 125
aa330ebb 126 // Get course grade_item
00006755 127 $course_item = grade_item::fetch_course_item($course->id);
128
129 // Get the stored grade
130 $course_grade = new grade_grade(array('itemid'=>$course_item->id, 'userid'=>$this->user->id));
131 $course_grade->grade_item =& $course_item;
132 $finalgrade = $course_grade->finalgrade;
133
134 if (!$canviewhidden and !is_null($finalgrade)) {
135 if ($course_grade->is_hidden()) {
136 $finalgrade = null;
137
138 } else {
139 // This is a really ugly hack, it will be fixed in 2.0
140 $items = grade_item::fetch_all(array('courseid'=>$course->id));
141 $grades = array();
142 $sql = "SELECT g.*
143 FROM {grade_grades} g
144 JOIN {grade_items} gi ON gi.id = g.itemid
145 WHERE g.userid = ? AND gi.courseid = ?";
146 if ($gradesrecords = $DB->get_records_sql($sql, array($this->user->id, $course->id))) {
147 foreach ($gradesrecords as $grade) {
148 $grades[$grade->itemid] = new grade_grade($grade, false);
149 }
150 unset($gradesrecords);
151 }
152 foreach ($items as $itemid=>$unused) {
153 if (!isset($grades[$itemid])) {
154 $grade_grade = new grade_grade();
155 $grade_grade->userid = $this->user->id;
156 $grade_grade->itemid = $items[$itemid]->id;
157 $grades[$itemid] = $grade_grade;
158 }
159 $grades[$itemid]->grade_item =& $items[$itemid];
160 }
161 $hiding_affected = grade_grade::get_hiding_affected($grades, $items);
162 if (array_key_exists($course_item->id, $hiding_affected['altered'])) {
163 $finalgrade = $hiding_affected['altered'][$course_item->id];
164
165 } else if (!empty($hiding_affected['unknown'][$course_item->id])) {
166 $finalgrade = null;
167 }
168
169 unset($hiding_affected);
170 unset($grades);
171 unset($items);
172 }
60574063 173 }
174
00006755 175 $data = array($courselink, grade_format_gradevalue($finalgrade, $course_item, true));
aa330ebb 176
60574063 177 if (!$this->showrank) {
178 //nothing to do
179
180 } else if (!is_null($finalgrade)) {
aa330ebb 181 /// find the number of users with a higher grade
00006755 182 /// please note this can not work if hidden grades involved :-( to be fixed in 2.0
183 $params = array($finalgrade, $course_item->id);
aa330ebb 184 $sql = "SELECT COUNT(DISTINCT(userid))
d24832f9 185 FROM {grade_grades}
5c75a0a3 186 WHERE finalgrade IS NOT NULL AND finalgrade > ?
187 AND itemid = ?";
188 $rank = $DB->count_records_sql($sql, $params) + 1;
aa330ebb 189
60574063 190 $data[] = "$rank/$numusers";
191
aa330ebb 192 } else {
193 // no grade, no rank
60574063 194 $data[] = '-';
aa330ebb 195 }
196
60574063 197 $this->table->add_data($data);
aa330ebb 198 }
aa330ebb 199 return true;
60574063 200
aa330ebb 201 } else {
202 notify(get_string('nocourses', 'grades'));
203 return false;
204 }
205 }
206
207 /**
208 * Prints or returns the HTML from the flexitable.
209 * @param bool $return Whether or not to return the data instead of printing it directly.
210 * @return string
211 */
5c75a0a3 212 public function print_table($return=false) {
aa330ebb 213 ob_start();
214 $this->table->print_html();
215 $html = ob_get_clean();
216 if ($return) {
217 return $html;
218 } else {
219 echo $html;
220 }
221 }
222
223 /**
224 * Processes the data sent by the form (grades and feedbacks).
225 * @var array $data
226 * @return bool Success or Failure (array of errors).
227 */
653a8648 228 function process_data($data) {
5c75a0a3 229 }
653a8648 230 function process_action($target, $action) {
aa330ebb 231 }
60574063 232}
aa330ebb 233
60574063 234function grade_report_overview_settings_definition(&$mform) {
235 global $CFG;
236
237 $options = array(-1 => get_string('default', 'grades'),
238 0 => get_string('hide'),
239 1 => get_string('show'));
240
241 if (empty($CFG->grade_overviewreport_showrank)) {
242 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
243 } else {
244 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
245 }
246
247 $mform->addElement('select', 'report_overview_showrank', get_string('showrank', 'grades'), $options);
d24832f9 248 $mform->setHelpButton('report_overview_showrank', array('showrank', get_string('showrank', 'grades'), 'grade'));
aa330ebb 249}
60574063 250
aa330ebb 251?>