MDL-10452 - start quiz UI is confusing if you have quiz:preview capability. Change...
[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
63 /**
64 * Constructor. Sets local copies of user preferences and initialises grade_tree.
65 * @param int $courseid
66 * @param string $context
67 * @param int $page The current page being viewed (when report is paged)
68 */
69 function grade_report($courseid, $context, $page=null) {
70 global $CFG;
71
72 $this->courseid = $courseid;
73 $this->context = $context;
74 $this->page = $page;
75
76 // roles to be displayed in the gradebook
77 $this->gradebookroles = $CFG->gradebookroles;
78
79 // Grab the grade_tree for this course
938e00b6 80 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'));
38b9e8a8 81 }
82
eea6690a 83 /**
84 * Given the name of a user preference (without grade_report_ prefix), locally saves then returns
85 * the value of that preference. If the preference has already been fetched before,
86 * the saved value is returned. If the preference is not set at the User level, the $CFG equivalent
87 * is given (site default).
88 * @param string $pref The name of the preference (do not include the grade_report_ prefix)
89 * @return mixed The value of the preference
90 */
91 function get_pref($pref) {
92 global $CFG;
38b9e8a8 93
eea6690a 94 if (empty($this->user_prefs[$pref])) {
95 $fullprefname = 'grade_report_' . $pref;
96 $this->user_prefs[$pref] = get_user_preferences($fullprefname, $CFG->$fullprefname);
97 }
98 return $this->user_prefs[$pref];
99 }
100 /**
101 * Uses set_user_preferences() to update the value of a user preference.
102 * Also updates the object's corresponding variable.
103 * @param string $pref_name The name of the preference.
104 * @param mixed $pref_value The value of the preference.
105 * @return bool Success or failure.
106 * TODO print visual feedback
107 */
108 function set_user_pref($pref, $pref_value) {
109 if ($result = set_user_preferences(array($pref => $pref_value))) {
110 $this->$pref = $pref_value;
111 }
112 return $result;
38b9e8a8 113 }
38b9e8a8 114
eea6690a 115 /**
116 * Handles form data sent by this report for this report. Abstract method to implement in all children.
117 * @abstract
118 * @param array $data
119 * @return mixed True or array of errors
120 */
121 function process_data($data) {
122 // Implement in children classes
123 }
38b9e8a8 124
eea6690a 125 /**
126 * Processes a single action against a category, grade_item or grade.
127 * @param string $target Sortorder
128 * @param string $action Which action to take (edit, delete etc...)
129 * @return
130 * TODO Update this, it's quite old and needs a major makeover
131 */
132 function process_action($target, $action) {
133 $element = $this->gtree->locate_element($target);
134
135 switch ($action) {
136 case 'edit':
137 break;
138 case 'delete':
139 if ($confirm == 1) { // Perform the deletion
140 //TODO: add proper delete support for grade items and categories
141 //$element['object']->delete();
142 // Print result message
143
144 } else { // Print confirmation dialog
145 $eid = $element['eid'];
146 $strdeletecheckfull = get_string('deletecheck', '', $element['object']->get_name());
147 $linkyes = GRADE_EDIT_URL . "/tree.php?target=$eid&amp;action=delete&amp;confirm=1$this->gtree->commonvars";
148 $linkno = GRADE_EDIT_URL . "/tree.php?$this->gtree->commonvars";
149 notice_yesno($strdeletecheckfull, $linkyes, $linkno);
150 }
151 break;
152
153 case 'hide':
154 // TODO Implement calendar for selection of a date to hide element until
155 $element['object']->set_hidden(1);
938e00b6 156 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'));
eea6690a 157 break;
158 case 'show':
159 $element['object']->set_hidden(0);
938e00b6 160 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'));
eea6690a 161 break;
162 case 'lock':
163 // TODO Implement calendar for selection of a date to lock element after
164 if (!$element['object']->set_locked(1)) {
165 debugging("Could not update the element's locked state!");
166 }
938e00b6 167 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'));
eea6690a 168 break;
169 case 'unlock':
170 if (!$element['object']->set_locked(0)) {
171 debugging("Could not update the element's locked state!");
172 }
938e00b6 173 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'));
eea6690a 174 break;
175 default:
176 break;
177 }
178 }
179
180 /**
181 * format grade using lang specific decimal point and thousand separator
182 * the result is suitable for printing on html page
183 * @static
184 * @param float $gradeval raw grade value pulled from db
185 * @return string $gradeval formatted grade value
186 */
187 function get_grade_clean($gradeval) {
188 global $CFG;
38b9e8a8 189
eea6690a 190 if (is_null($gradeval)) {
191 $gradeval = '';
192 } else {
193 // decimal points as specified by user
194 $decimals = get_user_preferences('grade_report_decimalpoints', $CFG->grade_report_decimalpoints);
195 $gradeval = number_format($gradeval, $decimals, get_string('decpoint', 'langconfig'), get_string('thousandsep', 'langconfig'));
196 }
197
198 return $gradeval;
199
200 /*
201 // commenting this out, if this is added, we also need to find the number of decimal place preserved
202 // so it can go into number_format
203 if ($gradeval != 0) {
204 $gradeval = rtrim(trim($gradeval, "0"), ".");
205 } else {
206 $gradeval = 0;
207 }
208 */
209
210 }
211
212 /**
213 * Given a user input grade, format it to standard format i.e. no thousand separator, and . as decimal point
214 * @static
215 * @param string $gradeval grade value from user input, language specific format
216 * @return string - grade value for storage, en format
217 */
218 function format_grade($gradeval) {
219
220 $decimalpt = get_string('decpoint', 'langconfig');
221 $thousandsep = get_string('thousandsep', 'langconfig');
222 // replace decimal point with '.';
223 $gradeval = str_replace($decimalpt, '.', $gradeval);
224 // thousand separator is not useful
225 $gradeval = str_replace($thousandsep, '', $gradeval);
226
227 return clean_param($gradeval, PARAM_NUMBER);
228 }
38b9e8a8 229
38b9e8a8 230}
eea6690a 231?>