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