MDL-11515:
[moodle-linuxchix.git] / grade / report / lib.php
blobec13c0521b69e1b9ca588657caba5fac9243c5b0
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 /**
22 * The course.
23 * @var object $course
25 var $course;
27 /** Grade plugin return tracking object.
28 var $gpr;
30 /**
31 * The context.
32 * @var int $context
34 var $context;
36 /**
37 * The grade_tree object.
38 * @var object $gtree
40 var $gtree;
42 /**
43 * User preferences related to this report.
44 * @var array $prefs
46 var $prefs = array();
48 /**
49 * The roles for this report.
50 * @var string $gradebookroles
52 var $gradebookroles;
54 /**
55 * base url for sorting by first/last name.
56 * @var string $baseurl
58 var $baseurl;
60 /**
61 * base url for paging.
62 * @var string $pbarurl
64 var $pbarurl;
66 /**
67 * Current page (for paging).
68 * @var int $page
70 var $page;
72 /**
73 * Array of cached language strings (using get_string() all the time takes a long time!).
74 * @var array $lang_strings
76 var $lang_strings = array();
78 //// GROUP VARIABLES (including SQL)
80 /**
81 * The current group being displayed.
82 * @var int $currentgroup
84 var $currentgroup;
86 /**
87 * A HTML select element used to select the current group.
88 * @var string $group_selector
90 var $group_selector;
92 /**
93 * An SQL fragment used to add linking information to the group tables.
94 * @var string $groupsql
96 var $groupsql;
98 /**
99 * An SQL constraint to append to the queries used by this object to build the report.
100 * @var string $groupwheresql
102 var $groupwheresql;
106 * Constructor. Sets local copies of user preferences and initialises grade_tree.
107 * @param int $courseid
108 * @param object $gpr grade plugin return tracking object
109 * @param string $context
110 * @param int $page The current page being viewed (when report is paged)
112 function grade_report($courseid, $gpr, $context, $page=null) {
113 global $CFG, $COURSE;
115 if (!$CFG->gradebookroles) {
116 error ('no roles defined in admin->appearance->graderoles');
120 $this->courseid = $courseid;
121 if ($this->courseid == $COURSE->id) {
122 $this->course = $COURSE;
123 } else {
124 $this->course = get_record('course', 'id', $this->courseid);
127 $this->gpr = $gpr;
128 $this->context = $context;
129 $this->page = $page;
131 // roles to be displayed in the gradebook
132 $this->gradebookroles = $CFG->gradebookroles;
134 // init gtree in child class
138 * Given the name of a user preference (without grade_report_ prefix), locally saves then returns
139 * the value of that preference. If the preference has already been fetched before,
140 * the saved value is returned. If the preference is not set at the User level, the $CFG equivalent
141 * is given (site default).
142 * @static (Can be called statically, but then doesn't benefit from caching)
143 * @param string $pref The name of the preference (do not include the grade_report_ prefix)
144 * @param int $objectid An optional itemid or categoryid to check for a more fine-grained preference
145 * @return mixed The value of the preference
147 function get_pref($pref, $objectid=null) {
148 global $CFG;
149 $fullprefname = 'grade_report_' . $pref;
151 $retval = null;
153 if (!isset($this) OR get_class($this) != 'grade_report') {
154 if (!empty($objectid)) {
155 $retval = get_user_preferences($fullprefname . $objectid, grade_report::get_pref($pref));
156 } else {
157 $retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
159 } else {
160 if (empty($this->prefs[$pref.$objectid])) {
162 if (!empty($objectid)) {
163 $retval = get_user_preferences($fullprefname . $objectid);
164 if (empty($retval)) {
165 // No item pref found, we are returning the global preference
166 $retval = $this->get_pref($pref);
167 $objectid = null;
169 } else {
170 $retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
172 $this->prefs[$pref.$objectid] = $retval;
173 } else {
174 $retval = $this->prefs[$pref.$objectid];
178 return $retval;
182 * Uses set_user_preferences() to update the value of a user preference. If 'default' is given as the value,
183 * the preference will be removed in favour of a higher-level preference.
184 * @static
185 * @param string $pref_name The name of the preference.
186 * @param mixed $pref_value The value of the preference.
187 * @param int $itemid An optional itemid to which the preference will be assigned
188 * @return bool Success or failure.
189 * TODO print visual feedback
191 function set_pref($pref, $pref_value='default', $itemid=null) {
192 $fullprefname = 'grade_report_' . $pref;
193 if ($pref_value == 'default') {
194 return unset_user_preference($fullprefname.$itemid);
195 } else {
196 return set_user_preference($fullprefname.$itemid, $pref_value);
201 * Handles form data sent by this report for this report. Abstract method to implement in all children.
202 * @abstract
203 * @param array $data
204 * @return mixed True or array of errors
206 function process_data($data) {
207 // Implement in children classes
211 * Processes a single action against a category, grade_item or grade.
212 * @param string $target Sortorder
213 * @param string $action Which action to take (edit, delete etc...)
214 * @return
216 function process_action($target, $action) {
217 //implement if needed
221 * First checks the cached language strings, then returns match if found, or uses get_string()
222 * to get it from the DB, caches it then returns it.
223 * @param string $strcode
224 * @param string $section Optional language section
225 * @return string
227 function get_lang_string($strcode, $section=null) {
228 if (empty($this->lang_strings[$strcode])) {
229 $this->lang_strings[$strcode] = get_string($strcode, $section);
231 return $this->lang_strings[$strcode];
235 * Computes then returns the percentage value of the grade value within the given range.
236 * @param float $gradeval
237 * @param float $grademin
238 * @param float $grademx
239 * @return float $percentage
241 function grade_to_percentage($gradeval, $grademin, $grademax) {
242 if ($grademin >= $grademax) {
243 debugging("The minimum grade ($grademin) was higher than or equal to the maximum grade ($grademax)!!! Cannot proceed with computation.");
245 $offset_value = $gradeval - $grademin;
246 $offset_max = $grademax - $grademin;
247 $factor = 100 / $offset_max;
248 $percentage = $offset_value * $factor;
249 return $percentage;
253 * Fetches and returns an array of grade letters indexed by their grade boundaries, as stored in course item settings or site preferences.
254 * @return array
256 function get_grade_letters() {
257 global $COURSE;
258 $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
259 return grade_get_letters($context);
263 * Fetches and returns a count of all the users that will be shown on this page.
264 * @return int Count of users
266 function get_numusers() {
267 global $CFG;
269 $countsql = "SELECT COUNT(DISTINCT u.id)
270 FROM {$CFG->prefix}grade_grades g RIGHT OUTER JOIN
271 {$CFG->prefix}user u ON u.id = g.userid
272 LEFT JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
273 $this->groupsql
274 WHERE ra.roleid in ($this->gradebookroles)
275 $this->groupwheresql
276 AND ra.contextid ".get_related_contexts_string($this->context);
277 return count_records_sql($countsql);
281 * Sets up this object's group variables, mainly to restrict the selection of users to display.
283 function setup_groups() {
284 global $CFG;
286 /// find out current groups mode
287 $this->group_selector = groups_print_course_menu($this->course, $this->pbarurl, true);
288 $this->currentgroup = groups_get_course_group($this->course);
290 if ($this->currentgroup) {
291 $this->groupsql = " LEFT JOIN {$CFG->prefix}groups_members gm ON gm.userid = u.id ";
292 $this->groupwheresql = " AND gm.groupid = $this->currentgroup ";
297 * Returns an arrow icon inside an <a> tag, for the purpose of sorting a column.
298 * @param string $direction
299 * @param string $sort_link
300 * @param string HTML
302 function get_sort_arrow($direction='move', $sort_link=null) {
303 $matrix = array('up' => 'asc', 'down' => 'desc', 'move' => 'desc');
304 $strsort = $this->get_lang_string('sort' . $matrix[$direction]);
305 $arrow = print_arrow($direction, $strsort, true);
306 $html = '<a href="'.$sort_link .'">' . $arrow . '</a>';
307 return $html;
311 * Builds and returns a HTML link to the grade or view page of the module given.
312 * If no itemmodule is given, only the name of the category/item is returned, no link.
313 * @param string $modulename The shortname of the module, will become the visible header
314 * @param string $itemmodule The name of the module type (e.g. assignment, quiz...)
315 * @param int $iteminstance The instance number of the module
316 * @return string HTML
318 function get_module_link($modulename, $itemmodule=null, $iteminstance=null) {
319 global $CFG;
321 $link = null;
322 if (!is_null($itemmodule) AND !is_null($iteminstance)) {
323 // Add module icon if toggle is enabled
324 if ($this->get_pref('showactivityicons')) {
325 $modulename = '<img src="' . $CFG->modpixpath . '/' . $itemmodule
326 . '/icon.gif" class="icon activity" alt="' . $modulename . '" />' . $modulename;
329 $coursemodule = get_coursemodule_from_instance($itemmodule, $iteminstance, $this->course->id);
331 $dir = $CFG->dirroot . "/mod/$itemmodule/";
332 $url = $CFG->wwwroot . "/mod/$itemmodule/";
334 if (file_exists($dir . 'grade.php')) {
335 $url .= 'grade.php';
336 } else {
337 $url .= 'view.php';
340 $url .= "?id=$coursemodule->id";
342 return '<a href="' . $url . '">' . $modulename . '</a>';
345 return $modulename;