eea6690a |
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 | /////////////////////////////////////////////////////////////////////////// |
38b9e8a8 |
25 | /** |
eea6690a |
26 | * File containing the grade_report class. |
27 | * @package gradebook |
38b9e8a8 |
28 | */ |
eea6690a |
29 | |
30 | require_once($CFG->libdir.'/gradelib.php'); |
31 | |
32 | /** |
33 | * An abstract class containing variables and methods used by all or most reports. |
34 | * @abstract |
35 | * @package gradebook |
36 | */ |
d24832f9 |
37 | abstract class grade_report { |
eea6690a |
38 | /** |
39 | * The courseid. |
40 | * @var int $courseid |
41 | */ |
d24832f9 |
42 | public $courseid; |
eea6690a |
43 | |
b2bc96d1 |
44 | /** |
45 | * The course. |
46 | * @var object $course |
47 | */ |
d24832f9 |
48 | public $course; |
b2bc96d1 |
49 | |
d30c4481 |
50 | /** Grade plugin return tracking object. |
4889285b |
51 | * @var object $gpr |
52 | */ |
53 | public $gpr; |
d30c4481 |
54 | |
eea6690a |
55 | /** |
56 | * The context. |
57 | * @var int $context |
58 | */ |
d24832f9 |
59 | public $context; |
eea6690a |
60 | |
61 | /** |
62 | * The grade_tree object. |
63 | * @var object $gtree |
64 | */ |
d24832f9 |
65 | public $gtree; |
eea6690a |
66 | |
67 | /** |
68 | * User preferences related to this report. |
e50ce569 |
69 | * @var array $prefs |
eea6690a |
70 | */ |
d24832f9 |
71 | public $prefs = array(); |
eea6690a |
72 | |
73 | /** |
74 | * The roles for this report. |
75 | * @var string $gradebookroles |
76 | */ |
d24832f9 |
77 | public $gradebookroles; |
eea6690a |
78 | |
79 | /** |
80 | * base url for sorting by first/last name. |
81 | * @var string $baseurl |
82 | */ |
d24832f9 |
83 | public $baseurl; |
eea6690a |
84 | |
85 | /** |
86 | * base url for paging. |
87 | * @var string $pbarurl |
88 | */ |
d24832f9 |
89 | public $pbarurl; |
eea6690a |
90 | |
91 | /** |
92 | * Current page (for paging). |
93 | * @var int $page |
94 | */ |
d24832f9 |
95 | public $page; |
eea6690a |
96 | |
388234f4 |
97 | /** |
98 | * Array of cached language strings (using get_string() all the time takes a long time!). |
99 | * @var array $lang_strings |
100 | */ |
d24832f9 |
101 | public $lang_strings = array(); |
388234f4 |
102 | |
90d3960c |
103 | //// GROUP VARIABLES (including SQL) |
104 | |
105 | /** |
106 | * The current group being displayed. |
107 | * @var int $currentgroup |
108 | */ |
d24832f9 |
109 | public $currentgroup; |
90d3960c |
110 | |
111 | /** |
112 | * A HTML select element used to select the current group. |
113 | * @var string $group_selector |
114 | */ |
d24832f9 |
115 | public $group_selector; |
90d3960c |
116 | |
117 | /** |
118 | * An SQL fragment used to add linking information to the group tables. |
119 | * @var string $groupsql |
120 | */ |
d24832f9 |
121 | protected $groupsql; |
90d3960c |
122 | |
123 | /** |
124 | * An SQL constraint to append to the queries used by this object to build the report. |
125 | * @var string $groupwheresql |
126 | */ |
d24832f9 |
127 | protected $groupwheresql; |
128 | |
129 | /** |
130 | * The ordered params for $groupwheresql |
131 | * @var array $groupwheresql_params |
132 | */ |
133 | protected $groupwheresql_params = array(); |
90d3960c |
134 | |
135 | |
eea6690a |
136 | /** |
137 | * Constructor. Sets local copies of user preferences and initialises grade_tree. |
138 | * @param int $courseid |
d30c4481 |
139 | * @param object $gpr grade plugin return tracking object |
eea6690a |
140 | * @param string $context |
141 | * @param int $page The current page being viewed (when report is paged) |
142 | */ |
d24832f9 |
143 | public function __construct($courseid, $gpr, $context, $page=null) { |
5c75a0a3 |
144 | global $CFG, $COURSE, $DB; |
eea6690a |
145 | |
dde8e548 |
146 | if (empty($CFG->gradebookroles)) { |
771dc7b2 |
147 | print_error('norolesdefined', 'grades'); |
0893aa30 |
148 | } |
284abb09 |
149 | |
0893aa30 |
150 | |
4faf5f99 |
151 | $this->courseid = $courseid; |
b2bc96d1 |
152 | if ($this->courseid == $COURSE->id) { |
153 | $this->course = $COURSE; |
154 | } else { |
5c75a0a3 |
155 | $this->course = $DB->get_record('course', array('id' => $this->courseid)); |
b2bc96d1 |
156 | } |
41f22daa |
157 | |
4faf5f99 |
158 | $this->gpr = $gpr; |
159 | $this->context = $context; |
160 | $this->page = $page; |
eea6690a |
161 | |
162 | // roles to be displayed in the gradebook |
163 | $this->gradebookroles = $CFG->gradebookroles; |
164 | |
dc482cfa |
165 | // Set up link to preferences page |
166 | $this->preferences_page = $CFG->wwwroot.'/grade/report/grader/preferences.php?id='.$courseid; |
167 | |
4faf5f99 |
168 | // init gtree in child class |
38b9e8a8 |
169 | } |
170 | |
eea6690a |
171 | /** |
172 | * Given the name of a user preference (without grade_report_ prefix), locally saves then returns |
173 | * the value of that preference. If the preference has already been fetched before, |
174 | * the saved value is returned. If the preference is not set at the User level, the $CFG equivalent |
175 | * is given (site default). |
501e0e34 |
176 | * @static (Can be called statically, but then doesn't benefit from caching) |
eea6690a |
177 | * @param string $pref The name of the preference (do not include the grade_report_ prefix) |
8c5a416e |
178 | * @param int $objectid An optional itemid or categoryid to check for a more fine-grained preference |
eea6690a |
179 | * @return mixed The value of the preference |
180 | */ |
d24832f9 |
181 | public function get_pref($pref, $objectid=null) { |
eea6690a |
182 | global $CFG; |
501e0e34 |
183 | $fullprefname = 'grade_report_' . $pref; |
54294d3b |
184 | $shortprefname = 'grade_' . $pref; |
38b9e8a8 |
185 | |
e50ce569 |
186 | $retval = null; |
187 | |
438a5aa9 |
188 | if (!isset($this) OR get_class($this) != 'grade_report') { |
8c5a416e |
189 | if (!empty($objectid)) { |
190 | $retval = get_user_preferences($fullprefname . $objectid, grade_report::get_pref($pref)); |
54294d3b |
191 | } elseif (isset($CFG->$fullprefname)) { |
e50ce569 |
192 | $retval = get_user_preferences($fullprefname, $CFG->$fullprefname); |
54294d3b |
193 | } elseif (isset($CFG->$shortprefname)) { |
d24832f9 |
194 | $retval = get_user_preferences($fullprefname, $CFG->$shortprefname); |
54294d3b |
195 | } else { |
196 | $retval = null; |
bb384a8e |
197 | } |
501e0e34 |
198 | } else { |
8c5a416e |
199 | if (empty($this->prefs[$pref.$objectid])) { |
e50ce569 |
200 | |
8c5a416e |
201 | if (!empty($objectid)) { |
202 | $retval = get_user_preferences($fullprefname . $objectid); |
e50ce569 |
203 | if (empty($retval)) { |
204 | // No item pref found, we are returning the global preference |
205 | $retval = $this->get_pref($pref); |
8c5a416e |
206 | $objectid = null; |
e50ce569 |
207 | } |
501e0e34 |
208 | } else { |
e50ce569 |
209 | $retval = get_user_preferences($fullprefname, $CFG->$fullprefname); |
501e0e34 |
210 | } |
8c5a416e |
211 | $this->prefs[$pref.$objectid] = $retval; |
e50ce569 |
212 | } else { |
8c5a416e |
213 | $retval = $this->prefs[$pref.$objectid]; |
501e0e34 |
214 | } |
eea6690a |
215 | } |
e50ce569 |
216 | |
217 | return $retval; |
eea6690a |
218 | } |
bb384a8e |
219 | |
eea6690a |
220 | /** |
501e0e34 |
221 | * Uses set_user_preferences() to update the value of a user preference. If 'default' is given as the value, |
222 | * the preference will be removed in favour of a higher-level preference. |
223 | * @static |
eea6690a |
224 | * @param string $pref_name The name of the preference. |
225 | * @param mixed $pref_value The value of the preference. |
bb384a8e |
226 | * @param int $itemid An optional itemid to which the preference will be assigned |
eea6690a |
227 | * @return bool Success or failure. |
eea6690a |
228 | */ |
d24832f9 |
229 | public function set_pref($pref, $pref_value='default', $itemid=null) { |
bb384a8e |
230 | $fullprefname = 'grade_report_' . $pref; |
501e0e34 |
231 | if ($pref_value == 'default') { |
232 | return unset_user_preference($fullprefname.$itemid); |
233 | } else { |
234 | return set_user_preference($fullprefname.$itemid, $pref_value); |
eea6690a |
235 | } |
38b9e8a8 |
236 | } |
38b9e8a8 |
237 | |
eea6690a |
238 | /** |
239 | * Handles form data sent by this report for this report. Abstract method to implement in all children. |
240 | * @abstract |
241 | * @param array $data |
242 | * @return mixed True or array of errors |
243 | */ |
d24832f9 |
244 | abstract function process_data($data); |
38b9e8a8 |
245 | |
eea6690a |
246 | /** |
247 | * Processes a single action against a category, grade_item or grade. |
248 | * @param string $target Sortorder |
249 | * @param string $action Which action to take (edit, delete etc...) |
250 | * @return |
eea6690a |
251 | */ |
d24832f9 |
252 | abstract function process_action($target, $action); |
eea6690a |
253 | |
388234f4 |
254 | /** |
255 | * First checks the cached language strings, then returns match if found, or uses get_string() |
256 | * to get it from the DB, caches it then returns it. |
257 | * @param string $strcode |
258 | * @param string $section Optional language section |
259 | * @return string |
260 | */ |
d24832f9 |
261 | public function get_lang_string($strcode, $section=null) { |
388234f4 |
262 | if (empty($this->lang_strings[$strcode])) { |
263 | $this->lang_strings[$strcode] = get_string($strcode, $section); |
264 | } |
265 | return $this->lang_strings[$strcode]; |
266 | } |
267 | |
90d3960c |
268 | /** |
269 | * Fetches and returns a count of all the users that will be shown on this page. |
28bcbc38 |
270 | * @param boolean $groups include groups limit |
90d3960c |
271 | * @return int Count of users |
272 | */ |
d24832f9 |
273 | public function get_numusers($groups=true) { |
274 | global $CFG, $DB; |
90d3960c |
275 | |
28bcbc38 |
276 | $groupsql = ""; |
277 | $groupwheresql = ""; |
b50371da |
278 | list($usql, $params) = $DB->get_in_or_equal(explode(',', $this->gradebookroles), SQL_PARAMS_NAMED, 'grbr0'); |
d24832f9 |
279 | |
28bcbc38 |
280 | if ($groups) { |
281 | $groupsql = $this->groupsql; |
bbadce53 |
282 | $groupwheresql = $this->groupwheresql; |
d24832f9 |
283 | $params = array_merge($params, $this->groupwheresql_params); |
28bcbc38 |
284 | } |
285 | |
90d3960c |
286 | $countsql = "SELECT COUNT(DISTINCT u.id) |
b50371da |
287 | FROM {user} u |
288 | JOIN {role_assignments} ra ON u.id = ra.userid |
289 | $groupsql |
290 | WHERE ra.roleid $usql AND u.deleted = 0 |
291 | $groupwheresql |
292 | AND ra.contextid ".get_related_contexts_string($this->context); |
d24832f9 |
293 | return $DB->count_records_sql($countsql, $params); |
90d3960c |
294 | } |
295 | |
296 | /** |
297 | * Sets up this object's group variables, mainly to restrict the selection of users to display. |
298 | */ |
0fd8bc00 |
299 | protected function setup_groups() { |
90d3960c |
300 | /// find out current groups mode |
b2bc96d1 |
301 | $this->group_selector = groups_print_course_menu($this->course, $this->pbarurl, true); |
b50371da |
302 | $this->currentgroup = groups_get_course_group($this->course); |
90d3960c |
303 | |
304 | if ($this->currentgroup) { |
b50371da |
305 | $this->groupsql = " JOIN {groups_members} gm ON gm.userid = u.id "; |
306 | $this->groupwheresql = " AND gm.groupid = :gr_grpid "; |
307 | $this->groupwheresql_params = array('gr_grpid'=>$this->currentgroup); |
90d3960c |
308 | } |
309 | } |
2e3987a9 |
310 | |
311 | /** |
312 | * Returns an arrow icon inside an <a> tag, for the purpose of sorting a column. |
313 | * @param string $direction |
314 | * @param string $sort_link |
315 | * @param string HTML |
316 | */ |
d24832f9 |
317 | protected function get_sort_arrow($direction='move', $sort_link=null) { |
dc482cfa |
318 | $matrix = array('up' => 'desc', 'down' => 'asc', 'move' => 'desc'); |
2e3987a9 |
319 | $strsort = $this->get_lang_string('sort' . $matrix[$direction]); |
dc482cfa |
320 | |
2e3987a9 |
321 | $arrow = print_arrow($direction, $strsort, true); |
dc482cfa |
322 | $html = '<a href="'.$sort_link .'" alt="'.$strsort.'" title="'.$strsort.'">' . $arrow . '</a>'; |
2e3987a9 |
323 | return $html; |
324 | } |
38b9e8a8 |
325 | } |
eea6690a |
326 | ?> |