MDL-10065 move lib/simpletest/grade/simpletest elsewhere
[moodle.git] / grade / report / lib.php
CommitLineData
eea6690a 1<?php // $Id$
38b9e8a8 2/**
eea6690a 3 * File containing the grade_report class.
4 * @package gradebook
38b9e8a8 5 */
eea6690a 6
7require_once($CFG->libdir.'/gradelib.php');
8
9/**
10 * An abstract class containing variables and methods used by all or most reports.
11 * @abstract
12 * @package gradebook
13 */
14class grade_report {
15 /**
16 * The courseid.
17 * @var int $courseid
18 */
19 var $courseid;
20
d30c4481 21 /** Grade plugin return tracking object.
22 var $gpr;
23
eea6690a 24 /**
25 * The context.
26 * @var int $context
27 */
28 var $context;
29
30 /**
31 * The grade_tree object.
32 * @var object $gtree
33 */
34 var $gtree;
35
36 /**
37 * User preferences related to this report.
38 * @var array $user_prefs
39 */
40 var $user_prefs = array();
41
42 /**
43 * The roles for this report.
44 * @var string $gradebookroles
45 */
46 var $gradebookroles;
47
48 /**
49 * base url for sorting by first/last name.
50 * @var string $baseurl
51 */
52 var $baseurl;
53
54 /**
55 * base url for paging.
56 * @var string $pbarurl
57 */
58 var $pbarurl;
59
60 /**
61 * Current page (for paging).
62 * @var int $page
63 */
64 var $page;
65
388234f4 66 /**
67 * Array of cached language strings (using get_string() all the time takes a long time!).
68 * @var array $lang_strings
69 */
70 var $lang_strings = array();
71
eea6690a 72 /**
73 * Constructor. Sets local copies of user preferences and initialises grade_tree.
74 * @param int $courseid
d30c4481 75 * @param object $gpr grade plugin return tracking object
eea6690a 76 * @param string $context
77 * @param int $page The current page being viewed (when report is paged)
78 */
d30c4481 79 function grade_report($courseid, $gpr, $context, $page=null) {
eea6690a 80 global $CFG;
81
82 $this->courseid = $courseid;
d30c4481 83 $this->gpr = $gpr;
84 $this->context = $context;
85 $this->page = $page;
eea6690a 86
87 // roles to be displayed in the gradebook
88 $this->gradebookroles = $CFG->gradebookroles;
89
90 // Grab the grade_tree for this course
938e00b6 91 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'));
38b9e8a8 92 }
93
eea6690a 94 /**
95 * Given the name of a user preference (without grade_report_ prefix), locally saves then returns
96 * the value of that preference. If the preference has already been fetched before,
97 * the saved value is returned. If the preference is not set at the User level, the $CFG equivalent
98 * is given (site default).
99 * @param string $pref The name of the preference (do not include the grade_report_ prefix)
bb384a8e 100 * @param int $itemid An optional itemid to check for a more fine-grained preference
eea6690a 101 * @return mixed The value of the preference
102 */
bb384a8e 103 function get_pref($pref, $itemid=null) {
eea6690a 104 global $CFG;
38b9e8a8 105
bb384a8e 106 if (empty($this->user_prefs[$pref.$itemid])) {
eea6690a 107 $fullprefname = 'grade_report_' . $pref;
bb384a8e 108 if (!empty($itemid)) {
109 $value = get_user_preferences($fullprefname . $itemid, $this->get_pref($pref));
110 } else {
111 $value = get_user_preferences($fullprefname, $CFG->$fullprefname);
112 }
113 $this->user_prefs[$pref.$itemid] = $value;
eea6690a 114 }
bb384a8e 115 return $this->user_prefs[$pref.$itemid];
eea6690a 116 }
bb384a8e 117
eea6690a 118 /**
119 * Uses set_user_preferences() to update the value of a user preference.
120 * Also updates the object's corresponding variable.
121 * @param string $pref_name The name of the preference.
122 * @param mixed $pref_value The value of the preference.
bb384a8e 123 * @param int $itemid An optional itemid to which the preference will be assigned
eea6690a 124 * @return bool Success or failure.
125 * TODO print visual feedback
126 */
bb384a8e 127 function set_pref($pref, $pref_value, $itemid=null) {
128 $fullprefname = 'grade_report_' . $pref;
129 if ($result = set_user_preferences(array($fullprefname.$itemid => $pref_value))) {
130 $this->user_prefs[$pref.$itemid] = $pref_value;
eea6690a 131 }
132 return $result;
38b9e8a8 133 }
38b9e8a8 134
eea6690a 135 /**
136 * Handles form data sent by this report for this report. Abstract method to implement in all children.
137 * @abstract
138 * @param array $data
139 * @return mixed True or array of errors
140 */
141 function process_data($data) {
142 // Implement in children classes
143 }
38b9e8a8 144
eea6690a 145 /**
146 * Processes a single action against a category, grade_item or grade.
147 * @param string $target Sortorder
148 * @param string $action Which action to take (edit, delete etc...)
149 * @return
150 * TODO Update this, it's quite old and needs a major makeover
151 */
152 function process_action($target, $action) {
153 $element = $this->gtree->locate_element($target);
154
155 switch ($action) {
156 case 'edit':
157 break;
158 case 'delete':
159 if ($confirm == 1) { // Perform the deletion
160 //TODO: add proper delete support for grade items and categories
161 //$element['object']->delete();
162 // Print result message
163
164 } else { // Print confirmation dialog
165 $eid = $element['eid'];
388234f4 166 $strdeletecheckfull = $this->get_lang_string('deletecheck', '', $element['object']->get_name());
eea6690a 167 $linkyes = GRADE_EDIT_URL . "/tree.php?target=$eid&amp;action=delete&amp;confirm=1$this->gtree->commonvars";
168 $linkno = GRADE_EDIT_URL . "/tree.php?$this->gtree->commonvars";
169 notice_yesno($strdeletecheckfull, $linkyes, $linkno);
170 }
171 break;
172
173 case 'hide':
174 // TODO Implement calendar for selection of a date to hide element until
175 $element['object']->set_hidden(1);
938e00b6 176 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'));
eea6690a 177 break;
178 case 'show':
179 $element['object']->set_hidden(0);
938e00b6 180 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'));
eea6690a 181 break;
182 case 'lock':
183 // TODO Implement calendar for selection of a date to lock element after
184 if (!$element['object']->set_locked(1)) {
185 debugging("Could not update the element's locked state!");
186 }
938e00b6 187 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'));
eea6690a 188 break;
189 case 'unlock':
190 if (!$element['object']->set_locked(0)) {
191 debugging("Could not update the element's locked state!");
192 }
938e00b6 193 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'));
eea6690a 194 break;
195 default:
196 break;
197 }
198 }
199
200 /**
201 * format grade using lang specific decimal point and thousand separator
202 * the result is suitable for printing on html page
203 * @static
204 * @param float $gradeval raw grade value pulled from db
205 * @return string $gradeval formatted grade value
206 */
207 function get_grade_clean($gradeval) {
208 global $CFG;
38b9e8a8 209
eea6690a 210 if (is_null($gradeval)) {
211 $gradeval = '';
212 } else {
213 // decimal points as specified by user
bb384a8e 214 $decimals = $this->get_pref('decimalpoints');
1fdab481 215 $gradeval = number_format($gradeval, $decimals, $this->get_lang_string('decpoint', 'langconfig'),
216 $this->get_lang_string('thousandsep', 'langconfig'));
eea6690a 217 }
218
219 return $gradeval;
220
221 /*
222 // commenting this out, if this is added, we also need to find the number of decimal place preserved
223 // so it can go into number_format
224 if ($gradeval != 0) {
225 $gradeval = rtrim(trim($gradeval, "0"), ".");
226 } else {
227 $gradeval = 0;
228 }
229 */
230
231 }
232
233 /**
234 * Given a user input grade, format it to standard format i.e. no thousand separator, and . as decimal point
235 * @static
236 * @param string $gradeval grade value from user input, language specific format
237 * @return string - grade value for storage, en format
238 */
239 function format_grade($gradeval) {
240
388234f4 241 $decimalpt = $this->get_lang_string('decpoint', 'langconfig');
242 $thousandsep = $this->get_lang_string('thousandsep', 'langconfig');
eea6690a 243 // replace decimal point with '.';
244 $gradeval = str_replace($decimalpt, '.', $gradeval);
245 // thousand separator is not useful
246 $gradeval = str_replace($thousandsep, '', $gradeval);
247
248 return clean_param($gradeval, PARAM_NUMBER);
249 }
38b9e8a8 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 */
258 function get_lang_string($strcode, $section=null) {
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
bb384a8e 265 /**
266 * Computes then returns the percentage value of the grade value within the given range.
267 * @param float $gradeval
268 * @param float $grademin
269 * @param float $grademx
270 * @return float $percentage
271 */
272 function grade_to_percentage($gradeval, $grademin, $grademax) {
273 if ($grademin >= $grademax) {
274 debugging("The minimum grade ($grademin) was higher than or equal to the maximum grade ($grademax)!!! Cannot proceed with computation.");
275 }
276 $offset_value = $gradeval - $grademin;
277 $offset_max = $grademax - $grademin;
278 $factor = 100 / $offset_max;
279 $percentage = $offset_value * $factor;
280 return $percentage;
281 }
38b9e8a8 282}
eea6690a 283?>