MDL-14967 Bug in DB upgrade for get_coursemodule_from_instance: named params were...
[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
dde8e548 138 if (empty($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;
54294d3b 173 $shortprefname = 'grade_' . $pref;
38b9e8a8 174
e50ce569 175 $retval = null;
176
438a5aa9 177 if (!isset($this) OR get_class($this) != 'grade_report') {
8c5a416e 178 if (!empty($objectid)) {
179 $retval = get_user_preferences($fullprefname . $objectid, grade_report::get_pref($pref));
54294d3b 180 } elseif (isset($CFG->$fullprefname)) {
e50ce569 181 $retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
54294d3b 182 } elseif (isset($CFG->$shortprefname)) {
183 $retval = get_user_preferences($fullprefname, $CFG->$shortprefname);
184 } else {
185 $retval = null;
bb384a8e 186 }
501e0e34 187 } else {
8c5a416e 188 if (empty($this->prefs[$pref.$objectid])) {
e50ce569 189
8c5a416e 190 if (!empty($objectid)) {
191 $retval = get_user_preferences($fullprefname . $objectid);
e50ce569 192 if (empty($retval)) {
193 // No item pref found, we are returning the global preference
194 $retval = $this->get_pref($pref);
8c5a416e 195 $objectid = null;
e50ce569 196 }
501e0e34 197 } else {
e50ce569 198 $retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
501e0e34 199 }
8c5a416e 200 $this->prefs[$pref.$objectid] = $retval;
e50ce569 201 } else {
8c5a416e 202 $retval = $this->prefs[$pref.$objectid];
501e0e34 203 }
eea6690a 204 }
e50ce569 205
206 return $retval;
eea6690a 207 }
bb384a8e 208
eea6690a 209 /**
501e0e34 210 * Uses set_user_preferences() to update the value of a user preference. If 'default' is given as the value,
211 * the preference will be removed in favour of a higher-level preference.
212 * @static
eea6690a 213 * @param string $pref_name The name of the preference.
214 * @param mixed $pref_value The value of the preference.
bb384a8e 215 * @param int $itemid An optional itemid to which the preference will be assigned
eea6690a 216 * @return bool Success or failure.
eea6690a 217 */
501e0e34 218 function set_pref($pref, $pref_value='default', $itemid=null) {
bb384a8e 219 $fullprefname = 'grade_report_' . $pref;
501e0e34 220 if ($pref_value == 'default') {
221 return unset_user_preference($fullprefname.$itemid);
222 } else {
223 return set_user_preference($fullprefname.$itemid, $pref_value);
eea6690a 224 }
38b9e8a8 225 }
38b9e8a8 226
eea6690a 227 /**
228 * Handles form data sent by this report for this report. Abstract method to implement in all children.
229 * @abstract
230 * @param array $data
231 * @return mixed True or array of errors
232 */
233 function process_data($data) {
234 // Implement in children classes
235 }
38b9e8a8 236
eea6690a 237 /**
238 * Processes a single action against a category, grade_item or grade.
239 * @param string $target Sortorder
240 * @param string $action Which action to take (edit, delete etc...)
241 * @return
eea6690a 242 */
243 function process_action($target, $action) {
2cc773f5 244 //implement if needed
eea6690a 245 }
246
388234f4 247 /**
248 * First checks the cached language strings, then returns match if found, or uses get_string()
249 * to get it from the DB, caches it then returns it.
250 * @param string $strcode
251 * @param string $section Optional language section
252 * @return string
253 */
254 function get_lang_string($strcode, $section=null) {
255 if (empty($this->lang_strings[$strcode])) {
256 $this->lang_strings[$strcode] = get_string($strcode, $section);
257 }
258 return $this->lang_strings[$strcode];
259 }
260
90d3960c 261 /**
262 * Fetches and returns a count of all the users that will be shown on this page.
28bcbc38 263 * @param boolean $groups include groups limit
90d3960c 264 * @return int Count of users
265 */
28bcbc38 266 function get_numusers($groups=true) {
90d3960c 267 global $CFG;
268
28bcbc38 269 $groupsql = "";
270 $groupwheresql = "";
271 if ($groups) {
272 $groupsql = $this->groupsql;
bbadce53 273 $groupwheresql = $this->groupwheresql;
28bcbc38 274 }
275
90d3960c 276 $countsql = "SELECT COUNT(DISTINCT u.id)
277 FROM {$CFG->prefix}grade_grades g RIGHT OUTER JOIN
278 {$CFG->prefix}user u ON u.id = g.userid
279 LEFT JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
28bcbc38 280 $groupsql
90d3960c 281 WHERE ra.roleid in ($this->gradebookroles)
28bcbc38 282 $groupwheresql
90d3960c 283 AND ra.contextid ".get_related_contexts_string($this->context);
284 return count_records_sql($countsql);
285 }
286
287 /**
288 * Sets up this object's group variables, mainly to restrict the selection of users to display.
289 */
290 function setup_groups() {
291 global $CFG;
292
293 /// find out current groups mode
b2bc96d1 294 $this->group_selector = groups_print_course_menu($this->course, $this->pbarurl, true);
295 $this->currentgroup = groups_get_course_group($this->course);
90d3960c 296
297 if ($this->currentgroup) {
298 $this->groupsql = " LEFT JOIN {$CFG->prefix}groups_members gm ON gm.userid = u.id ";
299 $this->groupwheresql = " AND gm.groupid = $this->currentgroup ";
300 }
301 }
2e3987a9 302
303 /**
304 * Returns an arrow icon inside an <a> tag, for the purpose of sorting a column.
305 * @param string $direction
306 * @param string $sort_link
307 * @param string HTML
308 */
309 function get_sort_arrow($direction='move', $sort_link=null) {
859c7259 310 $matrix = array('up' => 'asc', 'down' => 'desc', 'move' => 'desc');
2e3987a9 311 $strsort = $this->get_lang_string('sort' . $matrix[$direction]);
312 $arrow = print_arrow($direction, $strsort, true);
313 $html = '<a href="'.$sort_link .'">' . $arrow . '</a>';
314 return $html;
315 }
38b9e8a8 316}
eea6690a 317?>