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