MDL-14826 Added missing string. Merged from 1.9
[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 */
d24832f9 37abstract 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)) {
284abb09 147 error ('no roles defined in admin->appearance->graderoles');
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
4faf5f99 165 // init gtree in child class
38b9e8a8 166 }
167
eea6690a 168 /**
169 * Given the name of a user preference (without grade_report_ prefix), locally saves then returns
170 * the value of that preference. If the preference has already been fetched before,
171 * the saved value is returned. If the preference is not set at the User level, the $CFG equivalent
172 * is given (site default).
501e0e34 173 * @static (Can be called statically, but then doesn't benefit from caching)
eea6690a 174 * @param string $pref The name of the preference (do not include the grade_report_ prefix)
8c5a416e 175 * @param int $objectid An optional itemid or categoryid to check for a more fine-grained preference
eea6690a 176 * @return mixed The value of the preference
177 */
d24832f9 178 public function get_pref($pref, $objectid=null) {
eea6690a 179 global $CFG;
501e0e34 180 $fullprefname = 'grade_report_' . $pref;
54294d3b 181 $shortprefname = 'grade_' . $pref;
38b9e8a8 182
e50ce569 183 $retval = null;
184
438a5aa9 185 if (!isset($this) OR get_class($this) != 'grade_report') {
8c5a416e 186 if (!empty($objectid)) {
187 $retval = get_user_preferences($fullprefname . $objectid, grade_report::get_pref($pref));
54294d3b 188 } elseif (isset($CFG->$fullprefname)) {
e50ce569 189 $retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
54294d3b 190 } elseif (isset($CFG->$shortprefname)) {
d24832f9 191 $retval = get_user_preferences($fullprefname, $CFG->$shortprefname);
54294d3b 192 } else {
193 $retval = null;
bb384a8e 194 }
501e0e34 195 } else {
8c5a416e 196 if (empty($this->prefs[$pref.$objectid])) {
e50ce569 197
8c5a416e 198 if (!empty($objectid)) {
199 $retval = get_user_preferences($fullprefname . $objectid);
e50ce569 200 if (empty($retval)) {
201 // No item pref found, we are returning the global preference
202 $retval = $this->get_pref($pref);
8c5a416e 203 $objectid = null;
e50ce569 204 }
501e0e34 205 } else {
e50ce569 206 $retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
501e0e34 207 }
8c5a416e 208 $this->prefs[$pref.$objectid] = $retval;
e50ce569 209 } else {
8c5a416e 210 $retval = $this->prefs[$pref.$objectid];
501e0e34 211 }
eea6690a 212 }
e50ce569 213
214 return $retval;
eea6690a 215 }
bb384a8e 216
eea6690a 217 /**
501e0e34 218 * Uses set_user_preferences() to update the value of a user preference. If 'default' is given as the value,
219 * the preference will be removed in favour of a higher-level preference.
220 * @static
eea6690a 221 * @param string $pref_name The name of the preference.
222 * @param mixed $pref_value The value of the preference.
bb384a8e 223 * @param int $itemid An optional itemid to which the preference will be assigned
eea6690a 224 * @return bool Success or failure.
eea6690a 225 */
d24832f9 226 public function set_pref($pref, $pref_value='default', $itemid=null) {
bb384a8e 227 $fullprefname = 'grade_report_' . $pref;
501e0e34 228 if ($pref_value == 'default') {
229 return unset_user_preference($fullprefname.$itemid);
230 } else {
231 return set_user_preference($fullprefname.$itemid, $pref_value);
eea6690a 232 }
38b9e8a8 233 }
38b9e8a8 234
eea6690a 235 /**
236 * Handles form data sent by this report for this report. Abstract method to implement in all children.
237 * @abstract
238 * @param array $data
239 * @return mixed True or array of errors
240 */
d24832f9 241 abstract function process_data($data);
38b9e8a8 242
eea6690a 243 /**
244 * Processes a single action against a category, grade_item or grade.
245 * @param string $target Sortorder
246 * @param string $action Which action to take (edit, delete etc...)
247 * @return
eea6690a 248 */
d24832f9 249 abstract function process_action($target, $action);
eea6690a 250
388234f4 251 /**
252 * First checks the cached language strings, then returns match if found, or uses get_string()
253 * to get it from the DB, caches it then returns it.
254 * @param string $strcode
255 * @param string $section Optional language section
256 * @return string
257 */
d24832f9 258 public function get_lang_string($strcode, $section=null) {
388234f4 259 if (empty($this->lang_strings[$strcode])) {
260 $this->lang_strings[$strcode] = get_string($strcode, $section);
261 }
262 return $this->lang_strings[$strcode];
263 }
264
90d3960c 265 /**
266 * Fetches and returns a count of all the users that will be shown on this page.
28bcbc38 267 * @param boolean $groups include groups limit
90d3960c 268 * @return int Count of users
269 */
d24832f9 270 public function get_numusers($groups=true) {
271 global $CFG, $DB;
90d3960c 272
28bcbc38 273 $groupsql = "";
274 $groupwheresql = "";
d24832f9 275 list($usql, $params) = $DB->get_in_or_equal(explode(',', $this->gradebookroles));
276
28bcbc38 277 if ($groups) {
278 $groupsql = $this->groupsql;
bbadce53 279 $groupwheresql = $this->groupwheresql;
d24832f9 280 $params = array_merge($params, $this->groupwheresql_params);
28bcbc38 281 }
282
90d3960c 283 $countsql = "SELECT COUNT(DISTINCT u.id)
d24832f9 284 FROM {grade_grades} g RIGHT OUTER JOIN
285 {user} u ON u.id = g.userid
286 LEFT JOIN {role_assignments} ra ON u.id = ra.userid
28bcbc38 287 $groupsql
d24832f9 288 WHERE ra.roleid $usql
28bcbc38 289 $groupwheresql
90d3960c 290 AND ra.contextid ".get_related_contexts_string($this->context);
d24832f9 291 return $DB->count_records_sql($countsql, $params);
90d3960c 292 }
293
294 /**
295 * Sets up this object's group variables, mainly to restrict the selection of users to display.
296 */
0fd8bc00 297 protected function setup_groups() {
90d3960c 298 global $CFG;
299
300 /// find out current groups mode
b2bc96d1 301 $this->group_selector = groups_print_course_menu($this->course, $this->pbarurl, true);
302 $this->currentgroup = groups_get_course_group($this->course);
90d3960c 303
304 if ($this->currentgroup) {
d24832f9 305 $this->groupsql = " LEFT JOIN {groups_members} gm ON gm.userid = u.id ";
306 $this->groupwheresql = " AND gm.groupid = ? ";
307 $this->groupwheresql_params = array($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) {
859c7259 318 $matrix = array('up' => 'asc', 'down' => 'desc', 'move' => 'desc');
2e3987a9 319 $strsort = $this->get_lang_string('sort' . $matrix[$direction]);
320 $arrow = print_arrow($direction, $strsort, true);
321 $html = '<a href="'.$sort_link .'">' . $arrow . '</a>';
322 return $html;
323 }
38b9e8a8 324}
eea6690a 325?>