OU bug 3463 - warnings when you start an attempt in 'each attempt builds on last...
[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).
501e0e34 99 * @static (Can be called statically, but then doesn't benefit from caching)
eea6690a 100 * @param string $pref The name of the preference (do not include the grade_report_ prefix)
bb384a8e 101 * @param int $itemid An optional itemid to check for a more fine-grained preference
eea6690a 102 * @return mixed The value of the preference
103 */
bb384a8e 104 function get_pref($pref, $itemid=null) {
eea6690a 105 global $CFG;
501e0e34 106 $fullprefname = 'grade_report_' . $pref;
38b9e8a8 107
501e0e34 108 if (!isset($this)) {
bb384a8e 109 if (!empty($itemid)) {
501e0e34 110 $value = get_user_preferences($fullprefname . $itemid, grade_report::get_pref($pref));
bb384a8e 111 } else {
112 $value = get_user_preferences($fullprefname, $CFG->$fullprefname);
113 }
501e0e34 114 return $value;
115
116 } else {
117 if (empty($this->user_prefs[$pref.$itemid])) {
118 if (!empty($itemid)) {
119 $value = get_user_preferences($fullprefname . $itemid, $this->get_pref($pref));
120 } else {
121 $value = get_user_preferences($fullprefname, $CFG->$fullprefname);
122 }
123 $this->user_prefs[$pref.$itemid] = $value;
124 }
125 return $this->user_prefs[$pref.$itemid];
eea6690a 126 }
eea6690a 127 }
bb384a8e 128
eea6690a 129 /**
501e0e34 130 * Uses set_user_preferences() to update the value of a user preference. If 'default' is given as the value,
131 * the preference will be removed in favour of a higher-level preference.
132 * @static
eea6690a 133 * @param string $pref_name The name of the preference.
134 * @param mixed $pref_value The value of the preference.
bb384a8e 135 * @param int $itemid An optional itemid to which the preference will be assigned
eea6690a 136 * @return bool Success or failure.
137 * TODO print visual feedback
138 */
501e0e34 139 function set_pref($pref, $pref_value='default', $itemid=null) {
bb384a8e 140 $fullprefname = 'grade_report_' . $pref;
501e0e34 141 if ($pref_value == 'default') {
142 return unset_user_preference($fullprefname.$itemid);
143 } else {
144 return set_user_preference($fullprefname.$itemid, $pref_value);
eea6690a 145 }
38b9e8a8 146 }
38b9e8a8 147
eea6690a 148 /**
149 * Handles form data sent by this report for this report. Abstract method to implement in all children.
150 * @abstract
151 * @param array $data
152 * @return mixed True or array of errors
153 */
154 function process_data($data) {
155 // Implement in children classes
156 }
38b9e8a8 157
eea6690a 158 /**
159 * Processes a single action against a category, grade_item or grade.
160 * @param string $target Sortorder
161 * @param string $action Which action to take (edit, delete etc...)
162 * @return
163 * TODO Update this, it's quite old and needs a major makeover
164 */
165 function process_action($target, $action) {
166 $element = $this->gtree->locate_element($target);
167
168 switch ($action) {
169 case 'edit':
170 break;
171 case 'delete':
172 if ($confirm == 1) { // Perform the deletion
173 //TODO: add proper delete support for grade items and categories
174 //$element['object']->delete();
175 // Print result message
176
177 } else { // Print confirmation dialog
178 $eid = $element['eid'];
388234f4 179 $strdeletecheckfull = $this->get_lang_string('deletecheck', '', $element['object']->get_name());
eea6690a 180 $linkyes = GRADE_EDIT_URL . "/tree.php?target=$eid&amp;action=delete&amp;confirm=1$this->gtree->commonvars";
181 $linkno = GRADE_EDIT_URL . "/tree.php?$this->gtree->commonvars";
182 notice_yesno($strdeletecheckfull, $linkyes, $linkno);
183 }
184 break;
185
186 case 'hide':
187 // TODO Implement calendar for selection of a date to hide element until
188 $element['object']->set_hidden(1);
938e00b6 189 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'));
eea6690a 190 break;
191 case 'show':
192 $element['object']->set_hidden(0);
938e00b6 193 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'));
eea6690a 194 break;
195 case 'lock':
196 // TODO Implement calendar for selection of a date to lock element after
197 if (!$element['object']->set_locked(1)) {
198 debugging("Could not update the element's locked state!");
199 }
938e00b6 200 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'));
eea6690a 201 break;
202 case 'unlock':
203 if (!$element['object']->set_locked(0)) {
204 debugging("Could not update the element's locked state!");
205 }
938e00b6 206 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'));
eea6690a 207 break;
208 default:
209 break;
210 }
211 }
212
213 /**
214 * format grade using lang specific decimal point and thousand separator
215 * the result is suitable for printing on html page
216 * @static
217 * @param float $gradeval raw grade value pulled from db
218 * @return string $gradeval formatted grade value
219 */
220 function get_grade_clean($gradeval) {
221 global $CFG;
38b9e8a8 222
eea6690a 223 if (is_null($gradeval)) {
224 $gradeval = '';
225 } else {
226 // decimal points as specified by user
bb384a8e 227 $decimals = $this->get_pref('decimalpoints');
1fdab481 228 $gradeval = number_format($gradeval, $decimals, $this->get_lang_string('decpoint', 'langconfig'),
229 $this->get_lang_string('thousandsep', 'langconfig'));
eea6690a 230 }
231
232 return $gradeval;
233
234 /*
235 // commenting this out, if this is added, we also need to find the number of decimal place preserved
236 // so it can go into number_format
237 if ($gradeval != 0) {
238 $gradeval = rtrim(trim($gradeval, "0"), ".");
239 } else {
240 $gradeval = 0;
241 }
242 */
243
244 }
245
246 /**
247 * Given a user input grade, format it to standard format i.e. no thousand separator, and . as decimal point
248 * @static
249 * @param string $gradeval grade value from user input, language specific format
250 * @return string - grade value for storage, en format
251 */
252 function format_grade($gradeval) {
253
388234f4 254 $decimalpt = $this->get_lang_string('decpoint', 'langconfig');
255 $thousandsep = $this->get_lang_string('thousandsep', 'langconfig');
eea6690a 256 // replace decimal point with '.';
257 $gradeval = str_replace($decimalpt, '.', $gradeval);
258 // thousand separator is not useful
259 $gradeval = str_replace($thousandsep, '', $gradeval);
260
261 return clean_param($gradeval, PARAM_NUMBER);
262 }
38b9e8a8 263
388234f4 264 /**
265 * First checks the cached language strings, then returns match if found, or uses get_string()
266 * to get it from the DB, caches it then returns it.
267 * @param string $strcode
268 * @param string $section Optional language section
269 * @return string
270 */
271 function get_lang_string($strcode, $section=null) {
272 if (empty($this->lang_strings[$strcode])) {
273 $this->lang_strings[$strcode] = get_string($strcode, $section);
274 }
275 return $this->lang_strings[$strcode];
276 }
277
bb384a8e 278 /**
279 * Computes then returns the percentage value of the grade value within the given range.
280 * @param float $gradeval
281 * @param float $grademin
282 * @param float $grademx
283 * @return float $percentage
284 */
285 function grade_to_percentage($gradeval, $grademin, $grademax) {
286 if ($grademin >= $grademax) {
287 debugging("The minimum grade ($grademin) was higher than or equal to the maximum grade ($grademax)!!! Cannot proceed with computation.");
288 }
289 $offset_value = $gradeval - $grademin;
290 $offset_max = $grademax - $grademin;
291 $factor = 100 / $offset_max;
292 $percentage = $offset_value * $factor;
293 return $percentage;
294 }
38b9e8a8 295}
eea6690a 296?>