Changed the default user icon for something better. Much better. Not the best I could...
[moodle-pu.git] / grade / report / lib.php
blob5d0baa0bf05ff9a9761608236ac80d3678e0737d
1 <?php // $Id$
2 /**
3 * File containing the grade_report class.
4 * @package gradebook
5 */
7 require_once($CFG->libdir.'/gradelib.php');
9 /**
10 * An abstract class containing variables and methods used by all or most reports.
11 * @abstract
12 * @package gradebook
14 class grade_report {
15 /**
16 * The courseid.
17 * @var int $courseid
19 var $courseid;
21 /** Grade plugin return tracking object.
22 var $gpr;
24 /**
25 * The context.
26 * @var int $context
28 var $context;
30 /**
31 * The grade_tree object.
32 * @var object $gtree
34 var $gtree;
36 /**
37 * User preferences related to this report.
38 * @var array $prefs
40 var $prefs = array();
42 /**
43 * The roles for this report.
44 * @var string $gradebookroles
46 var $gradebookroles;
48 /**
49 * base url for sorting by first/last name.
50 * @var string $baseurl
52 var $baseurl;
54 /**
55 * base url for paging.
56 * @var string $pbarurl
58 var $pbarurl;
60 /**
61 * Current page (for paging).
62 * @var int $page
64 var $page;
66 /**
67 * Array of cached language strings (using get_string() all the time takes a long time!).
68 * @var array $lang_strings
70 var $lang_strings = array();
72 //// GROUP VARIABLES (including SQL)
74 /**
75 * The current group being displayed.
76 * @var int $currentgroup
78 var $currentgroup;
80 /**
81 * A HTML select element used to select the current group.
82 * @var string $group_selector
84 var $group_selector;
86 /**
87 * An SQL fragment used to add linking information to the group tables.
88 * @var string $groupsql
90 var $groupsql;
92 /**
93 * An SQL constraint to append to the queries used by this object to build the report.
94 * @var string $groupwheresql
96 var $groupwheresql;
99 /**
100 * Constructor. Sets local copies of user preferences and initialises grade_tree.
101 * @param int $courseid
102 * @param object $gpr grade plugin return tracking object
103 * @param string $context
104 * @param int $page The current page being viewed (when report is paged)
106 function grade_report($courseid, $gpr, $context, $page=null) {
107 global $CFG;
109 $this->courseid = $courseid;
110 $this->gpr = $gpr;
111 $this->context = $context;
112 $this->page = $page;
114 // roles to be displayed in the gradebook
115 $this->gradebookroles = $CFG->gradebookroles;
117 // init gtree in child class
121 * Given the name of a user preference (without grade_report_ prefix), locally saves then returns
122 * the value of that preference. If the preference has already been fetched before,
123 * the saved value is returned. If the preference is not set at the User level, the $CFG equivalent
124 * is given (site default).
125 * @static (Can be called statically, but then doesn't benefit from caching)
126 * @param string $pref The name of the preference (do not include the grade_report_ prefix)
127 * @param int $objectid An optional itemid or categoryid to check for a more fine-grained preference
128 * @return mixed The value of the preference
130 function get_pref($pref, $objectid=null) {
131 global $CFG;
132 $fullprefname = 'grade_report_' . $pref;
134 $retval = null;
136 if (!isset($this) OR get_class($this) != 'grade_report') {
137 if (!empty($objectid)) {
138 $retval = get_user_preferences($fullprefname . $objectid, grade_report::get_pref($pref));
139 } else {
140 $retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
142 } else {
143 if (empty($this->prefs[$pref.$objectid])) {
145 if (!empty($objectid)) {
146 $retval = get_user_preferences($fullprefname . $objectid);
147 if (empty($retval)) {
148 // No item pref found, we are returning the global preference
149 $retval = $this->get_pref($pref);
150 $objectid = null;
152 } else {
153 $retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
155 $this->prefs[$pref.$objectid] = $retval;
156 } else {
157 $retval = $this->prefs[$pref.$objectid];
161 return $retval;
165 * Uses set_user_preferences() to update the value of a user preference. If 'default' is given as the value,
166 * the preference will be removed in favour of a higher-level preference.
167 * @static
168 * @param string $pref_name The name of the preference.
169 * @param mixed $pref_value The value of the preference.
170 * @param int $itemid An optional itemid to which the preference will be assigned
171 * @return bool Success or failure.
172 * TODO print visual feedback
174 function set_pref($pref, $pref_value='default', $itemid=null) {
175 $fullprefname = 'grade_report_' . $pref;
176 if ($pref_value == 'default') {
177 return unset_user_preference($fullprefname.$itemid);
178 } else {
179 return set_user_preference($fullprefname.$itemid, $pref_value);
184 * Handles form data sent by this report for this report. Abstract method to implement in all children.
185 * @abstract
186 * @param array $data
187 * @return mixed True or array of errors
189 function process_data($data) {
190 // Implement in children classes
194 * Processes a single action against a category, grade_item or grade.
195 * @param string $target Sortorder
196 * @param string $action Which action to take (edit, delete etc...)
197 * @return
199 function process_action($target, $action) {
200 //implement if needed
204 * First checks the cached language strings, then returns match if found, or uses get_string()
205 * to get it from the DB, caches it then returns it.
206 * @param string $strcode
207 * @param string $section Optional language section
208 * @return string
210 function get_lang_string($strcode, $section=null) {
211 if (empty($this->lang_strings[$strcode])) {
212 $this->lang_strings[$strcode] = get_string($strcode, $section);
214 return $this->lang_strings[$strcode];
218 * Computes then returns the percentage value of the grade value within the given range.
219 * @param float $gradeval
220 * @param float $grademin
221 * @param float $grademx
222 * @return float $percentage
224 function grade_to_percentage($gradeval, $grademin, $grademax) {
225 if ($grademin >= $grademax) {
226 debugging("The minimum grade ($grademin) was higher than or equal to the maximum grade ($grademax)!!! Cannot proceed with computation.");
228 $offset_value = $gradeval - $grademin;
229 $offset_max = $grademax - $grademin;
230 $factor = 100 / $offset_max;
231 $percentage = $offset_value * $factor;
232 return $percentage;
236 * Fetches and returns an array of grade letters indexed by their grade boundaries, as stored in preferences.
237 * @return array
239 function get_grade_letters() {
240 $letters = array();
241 for ($i = 1; $i <= 10; $i++) {
242 $boundary = grade_report::get_pref('gradeboundary' . $i);
243 $letter = grade_report::get_pref('gradeletter' . $i);
244 if (!is_null($boundary) && $boundary != -1 && !empty($letter)) {
245 $letters[$boundary] = $letter;
248 return $letters;
252 * Fetches and returns a count of all the users that will be shown on this page.
253 * @return int Count of users
255 function get_numusers() {
256 global $CFG;
258 $countsql = "SELECT COUNT(DISTINCT u.id)
259 FROM {$CFG->prefix}grade_grades g RIGHT OUTER JOIN
260 {$CFG->prefix}user u ON u.id = g.userid
261 LEFT JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
262 $this->groupsql
263 WHERE ra.roleid in ($this->gradebookroles)
264 $this->groupwheresql
265 AND ra.contextid ".get_related_contexts_string($this->context);
266 return count_records_sql($countsql);
270 * Sets up this object's group variables, mainly to restrict the selection of users to display.
272 function setup_groups() {
273 global $CFG;
275 /// find out current groups mode
276 $course = get_record('course', 'id', $this->courseid);
277 $groupmode = $course->groupmode;
278 ob_start();
279 $this->currentgroup = setup_and_print_groups($course, $groupmode, $this->pbarurl);
280 $this->group_selector = ob_get_clean();
282 // update paging after group
283 $this->baseurl .= 'group='.$this->currentgroup;
284 $this->pbarurl .= 'group='.$this->currentgroup;
286 if ($this->currentgroup) {
287 $this->groupsql = " LEFT JOIN {$CFG->prefix}groups_members gm ON gm.userid = u.id ";
288 $this->groupwheresql = " AND gm.groupid = $this->currentgroup ";
293 * Returns an arrow icon inside an <a> tag, for the purpose of sorting a column.
294 * @param string $direction
295 * @param string $sort_link
296 * @param string HTML
298 function get_sort_arrow($direction='move', $sort_link=null) {
299 $matrix = array('up' => 'asc', 'down' => 'desc', 'move' => 'desc');
300 $strsort = $this->get_lang_string('sort' . $matrix[$direction]);
301 $arrow = print_arrow($direction, $strsort, true);
302 $html = '<a href="'.$sort_link .'">' . $arrow . '</a>';
303 return $html;
307 * Builds and returns a HTML link to the grade or view page of the module given.
308 * If no itemmodule is given, only the name of the category/item is returned, no link.
309 * @param string $modulename The shortname of the module, will become the visible header
310 * @param string $itemmodule The name of the module type (e.g. assignment, quiz...)
311 * @param int $iteminstance The instance number of the module
312 * @return string HTML
314 function get_module_link($modulename, $itemmodule=null, $iteminstance=null) {
315 global $CFG, $COURSE;
317 $link = null;
318 if (!is_null($itemmodule) AND !is_null($iteminstance)) {
319 // Add module icon if toggle is enabled
320 if ($this->get_pref('showactivityicons')) {
321 $modulename = '<img src="' . $CFG->modpixpath . '/' . $itemmodule
322 . '/icon.gif" class="icon activity" alt="' . $modulename . '" />' . $modulename;
325 $coursemodule = get_coursemodule_from_instance($itemmodule, $iteminstance, $COURSE->id);
327 $dir = $CFG->dirroot . "/mod/$itemmodule/";
328 $url = $CFG->wwwroot . "/mod/$itemmodule/";
330 if (file_exists($dir . 'grade.php')) {
331 $url .= 'grade.php';
332 } else {
333 $url .= 'view.php';
336 $url .= "?id=$coursemodule->id";
337 return '<a href="' . $url . '">' . $modulename . '</a>';
340 return $modulename;