support for styles.php in grade reports
[moodle.git] / grade / report / lib.php
CommitLineData
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
30require_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 */
37class grade_report {
38 /**
39 * The courseid.
40 * @var int $courseid
41 */
42 var $courseid;
43
b2bc96d1 44 /**
45 * The course.
46 * @var object $course
47 */
48 var $course;
49
d30c4481 50 /** Grade plugin return tracking object.
51 var $gpr;
52
eea6690a 53 /**
54 * The context.
55 * @var int $context
56 */
57 var $context;
58
59 /**
60 * The grade_tree object.
61 * @var object $gtree
62 */
63 var $gtree;
64
65 /**
66 * User preferences related to this report.
e50ce569 67 * @var array $prefs
eea6690a 68 */
e50ce569 69 var $prefs = array();
eea6690a 70
71 /**
72 * The roles for this report.
73 * @var string $gradebookroles
74 */
75 var $gradebookroles;
76
77 /**
78 * base url for sorting by first/last name.
79 * @var string $baseurl
80 */
81 var $baseurl;
82
83 /**
84 * base url for paging.
85 * @var string $pbarurl
86 */
87 var $pbarurl;
88
89 /**
90 * Current page (for paging).
91 * @var int $page
92 */
93 var $page;
94
388234f4 95 /**
96 * Array of cached language strings (using get_string() all the time takes a long time!).
97 * @var array $lang_strings
98 */
99 var $lang_strings = array();
100
90d3960c 101//// GROUP VARIABLES (including SQL)
102
103 /**
104 * The current group being displayed.
105 * @var int $currentgroup
106 */
107 var $currentgroup;
108
109 /**
110 * A HTML select element used to select the current group.
111 * @var string $group_selector
112 */
113 var $group_selector;
114
115 /**
116 * An SQL fragment used to add linking information to the group tables.
117 * @var string $groupsql
118 */
119 var $groupsql;
120
121 /**
122 * An SQL constraint to append to the queries used by this object to build the report.
123 * @var string $groupwheresql
124 */
125 var $groupwheresql;
126
127
eea6690a 128 /**
129 * Constructor. Sets local copies of user preferences and initialises grade_tree.
130 * @param int $courseid
d30c4481 131 * @param object $gpr grade plugin return tracking object
eea6690a 132 * @param string $context
133 * @param int $page The current page being viewed (when report is paged)
134 */
d30c4481 135 function grade_report($courseid, $gpr, $context, $page=null) {
b2bc96d1 136 global $CFG, $COURSE;
eea6690a 137
0893aa30 138 if (!$CFG->gradebookroles) {
284abb09 139 error ('no roles defined in admin->appearance->graderoles');
0893aa30 140 }
284abb09 141
0893aa30 142
4faf5f99 143 $this->courseid = $courseid;
b2bc96d1 144 if ($this->courseid == $COURSE->id) {
145 $this->course = $COURSE;
146 } else {
147 $this->course = get_record('course', 'id', $this->courseid);
148 }
41f22daa 149
4faf5f99 150 $this->gpr = $gpr;
151 $this->context = $context;
152 $this->page = $page;
eea6690a 153
154 // roles to be displayed in the gradebook
155 $this->gradebookroles = $CFG->gradebookroles;
156
4faf5f99 157 // init gtree in child class
38b9e8a8 158 }
159
eea6690a 160 /**
161 * Given the name of a user preference (without grade_report_ prefix), locally saves then returns
162 * the value of that preference. If the preference has already been fetched before,
163 * the saved value is returned. If the preference is not set at the User level, the $CFG equivalent
164 * is given (site default).
501e0e34 165 * @static (Can be called statically, but then doesn't benefit from caching)
eea6690a 166 * @param string $pref The name of the preference (do not include the grade_report_ prefix)
8c5a416e 167 * @param int $objectid An optional itemid or categoryid to check for a more fine-grained preference
eea6690a 168 * @return mixed The value of the preference
169 */
8c5a416e 170 function get_pref($pref, $objectid=null) {
eea6690a 171 global $CFG;
501e0e34 172 $fullprefname = 'grade_report_' . $pref;
38b9e8a8 173
e50ce569 174 $retval = null;
175
438a5aa9 176 if (!isset($this) OR get_class($this) != 'grade_report') {
8c5a416e 177 if (!empty($objectid)) {
178 $retval = get_user_preferences($fullprefname . $objectid, grade_report::get_pref($pref));
bb384a8e 179 } else {
e50ce569 180 $retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
bb384a8e 181 }
501e0e34 182 } else {
8c5a416e 183 if (empty($this->prefs[$pref.$objectid])) {
e50ce569 184
8c5a416e 185 if (!empty($objectid)) {
186 $retval = get_user_preferences($fullprefname . $objectid);
e50ce569 187 if (empty($retval)) {
188 // No item pref found, we are returning the global preference
189 $retval = $this->get_pref($pref);
8c5a416e 190 $objectid = null;
e50ce569 191 }
501e0e34 192 } else {
e50ce569 193 $retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
501e0e34 194 }
8c5a416e 195 $this->prefs[$pref.$objectid] = $retval;
e50ce569 196 } else {
8c5a416e 197 $retval = $this->prefs[$pref.$objectid];
501e0e34 198 }
eea6690a 199 }
e50ce569 200
201 return $retval;
eea6690a 202 }
bb384a8e 203
eea6690a 204 /**
501e0e34 205 * Uses set_user_preferences() to update the value of a user preference. If 'default' is given as the value,
206 * the preference will be removed in favour of a higher-level preference.
207 * @static
eea6690a 208 * @param string $pref_name The name of the preference.
209 * @param mixed $pref_value The value of the preference.
bb384a8e 210 * @param int $itemid An optional itemid to which the preference will be assigned
eea6690a 211 * @return bool Success or failure.
212 * TODO print visual feedback
213 */
501e0e34 214 function set_pref($pref, $pref_value='default', $itemid=null) {
bb384a8e 215 $fullprefname = 'grade_report_' . $pref;
501e0e34 216 if ($pref_value == 'default') {
217 return unset_user_preference($fullprefname.$itemid);
218 } else {
219 return set_user_preference($fullprefname.$itemid, $pref_value);
eea6690a 220 }
38b9e8a8 221 }
38b9e8a8 222
eea6690a 223 /**
224 * Handles form data sent by this report for this report. Abstract method to implement in all children.
225 * @abstract
226 * @param array $data
227 * @return mixed True or array of errors
228 */
229 function process_data($data) {
230 // Implement in children classes
231 }
38b9e8a8 232
eea6690a 233 /**
234 * Processes a single action against a category, grade_item or grade.
235 * @param string $target Sortorder
236 * @param string $action Which action to take (edit, delete etc...)
237 * @return
eea6690a 238 */
239 function process_action($target, $action) {
2cc773f5 240 //implement if needed
eea6690a 241 }
242
388234f4 243 /**
244 * First checks the cached language strings, then returns match if found, or uses get_string()
245 * to get it from the DB, caches it then returns it.
246 * @param string $strcode
247 * @param string $section Optional language section
248 * @return string
249 */
250 function get_lang_string($strcode, $section=null) {
251 if (empty($this->lang_strings[$strcode])) {
252 $this->lang_strings[$strcode] = get_string($strcode, $section);
253 }
254 return $this->lang_strings[$strcode];
255 }
256
bb384a8e 257 /**
258 * Computes then returns the percentage value of the grade value within the given range.
259 * @param float $gradeval
260 * @param float $grademin
261 * @param float $grademx
262 * @return float $percentage
263 */
264 function grade_to_percentage($gradeval, $grademin, $grademax) {
265 if ($grademin >= $grademax) {
266 debugging("The minimum grade ($grademin) was higher than or equal to the maximum grade ($grademax)!!! Cannot proceed with computation.");
267 }
268 $offset_value = $gradeval - $grademin;
269 $offset_max = $grademax - $grademin;
270 $factor = 100 / $offset_max;
271 $percentage = $offset_value * $factor;
272 return $percentage;
273 }
32b97bb2 274
275 /**
41f22daa 276 * Fetches and returns an array of grade letters indexed by their grade boundaries, as stored in course item settings or site preferences.
32b97bb2 277 * @return array
278 */
279 function get_grade_letters() {
41f22daa 280 global $COURSE;
41f22daa 281 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
4f30c61d 282 return grade_get_letters($context);
32b97bb2 283 }
90d3960c 284
285 /**
286 * Fetches and returns a count of all the users that will be shown on this page.
287 * @return int Count of users
288 */
289 function get_numusers() {
290 global $CFG;
291
292 $countsql = "SELECT COUNT(DISTINCT u.id)
293 FROM {$CFG->prefix}grade_grades g RIGHT OUTER JOIN
294 {$CFG->prefix}user u ON u.id = g.userid
295 LEFT JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
296 $this->groupsql
297 WHERE ra.roleid in ($this->gradebookroles)
298 $this->groupwheresql
299 AND ra.contextid ".get_related_contexts_string($this->context);
300 return count_records_sql($countsql);
301 }
302
303 /**
304 * Sets up this object's group variables, mainly to restrict the selection of users to display.
305 */
306 function setup_groups() {
307 global $CFG;
308
309 /// find out current groups mode
b2bc96d1 310 $this->group_selector = groups_print_course_menu($this->course, $this->pbarurl, true);
311 $this->currentgroup = groups_get_course_group($this->course);
90d3960c 312
313 if ($this->currentgroup) {
314 $this->groupsql = " LEFT JOIN {$CFG->prefix}groups_members gm ON gm.userid = u.id ";
315 $this->groupwheresql = " AND gm.groupid = $this->currentgroup ";
316 }
317 }
2e3987a9 318
319 /**
320 * Returns an arrow icon inside an <a> tag, for the purpose of sorting a column.
321 * @param string $direction
322 * @param string $sort_link
323 * @param string HTML
324 */
325 function get_sort_arrow($direction='move', $sort_link=null) {
859c7259 326 $matrix = array('up' => 'asc', 'down' => 'desc', 'move' => 'desc');
2e3987a9 327 $strsort = $this->get_lang_string('sort' . $matrix[$direction]);
328 $arrow = print_arrow($direction, $strsort, true);
329 $html = '<a href="'.$sort_link .'">' . $arrow . '</a>';
330 return $html;
331 }
332
333 /**
334 * Builds and returns a HTML link to the grade or view page of the module given.
335 * If no itemmodule is given, only the name of the category/item is returned, no link.
336 * @param string $modulename The shortname of the module, will become the visible header
337 * @param string $itemmodule The name of the module type (e.g. assignment, quiz...)
338 * @param int $iteminstance The instance number of the module
c1d0d07e 339 * @param bool itemhidden - if the grade item is hidden
2e3987a9 340 * @return string HTML
341 */
c1d0d07e 342 function get_module_link($modulename, $itemmodule=null, $iteminstance=null, $itemhidden=false) {
b2bc96d1 343 global $CFG;
2e3987a9 344
345 $link = null;
346 if (!is_null($itemmodule) AND !is_null($iteminstance)) {
1815b45c 347 // Add module icon if toggle is enabled
348 if ($this->get_pref('showactivityicons')) {
349 $modulename = '<img src="' . $CFG->modpixpath . '/' . $itemmodule
350 . '/icon.gif" class="icon activity" alt="' . $modulename . '" />' . $modulename;
351 }
352
b2bc96d1 353 $coursemodule = get_coursemodule_from_instance($itemmodule, $iteminstance, $this->course->id);
41f22daa 354
2e3987a9 355 $dir = $CFG->dirroot . "/mod/$itemmodule/";
356 $url = $CFG->wwwroot . "/mod/$itemmodule/";
357
358 if (file_exists($dir . 'grade.php')) {
359 $url .= 'grade.php';
360 } else {
361 $url .= 'view.php';
362 }
363
364 $url .= "?id=$coursemodule->id";
548bf946 365 return '<a href="' . $url . '">' . $modulename . '</a>';
2e3987a9 366 }
367
368 return $modulename;
369 }
38b9e8a8 370}
eea6690a 371?>