3 * File containing the grade_report class.
7 require_once($CFG->libdir
.'/gradelib.php');
10 * An abstract class containing variables and methods used by all or most reports.
27 /** Grade plugin return tracking object.
37 * The grade_tree object.
43 * User preferences related to this report.
49 * The roles for this report.
50 * @var string $gradebookroles
55 * base url for sorting by first/last name.
56 * @var string $baseurl
61 * base url for paging.
62 * @var string $pbarurl
67 * Current page (for paging).
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)
81 * The current group being displayed.
82 * @var int $currentgroup
87 * A HTML select element used to select the current group.
88 * @var string $group_selector
93 * An SQL fragment used to add linking information to the group tables.
94 * @var string $groupsql
99 * An SQL constraint to append to the queries used by this object to build the report.
100 * @var string $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 $this->courseid
= $courseid;
116 if ($this->courseid
== $COURSE->id
) {
117 $this->course
= $COURSE;
119 $this->course
= get_record('course', 'id', $this->courseid
);
123 $this->context
= $context;
126 // roles to be displayed in the gradebook
127 $this->gradebookroles
= $CFG->gradebookroles
;
129 // init gtree in child class
133 * Given the name of a user preference (without grade_report_ prefix), locally saves then returns
134 * the value of that preference. If the preference has already been fetched before,
135 * the saved value is returned. If the preference is not set at the User level, the $CFG equivalent
136 * is given (site default).
137 * @static (Can be called statically, but then doesn't benefit from caching)
138 * @param string $pref The name of the preference (do not include the grade_report_ prefix)
139 * @param int $objectid An optional itemid or categoryid to check for a more fine-grained preference
140 * @return mixed The value of the preference
142 function get_pref($pref, $objectid=null) {
144 $fullprefname = 'grade_report_' . $pref;
148 if (!isset($this) OR get_class($this) != 'grade_report') {
149 if (!empty($objectid)) {
150 $retval = get_user_preferences($fullprefname . $objectid, grade_report
::get_pref($pref));
152 $retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
155 if (empty($this->prefs
[$pref.$objectid])) {
157 if (!empty($objectid)) {
158 $retval = get_user_preferences($fullprefname . $objectid);
159 if (empty($retval)) {
160 // No item pref found, we are returning the global preference
161 $retval = $this->get_pref($pref);
165 $retval = get_user_preferences($fullprefname, $CFG->$fullprefname);
167 $this->prefs
[$pref.$objectid] = $retval;
169 $retval = $this->prefs
[$pref.$objectid];
177 * Uses set_user_preferences() to update the value of a user preference. If 'default' is given as the value,
178 * the preference will be removed in favour of a higher-level preference.
180 * @param string $pref_name The name of the preference.
181 * @param mixed $pref_value The value of the preference.
182 * @param int $itemid An optional itemid to which the preference will be assigned
183 * @return bool Success or failure.
184 * TODO print visual feedback
186 function set_pref($pref, $pref_value='default', $itemid=null) {
187 $fullprefname = 'grade_report_' . $pref;
188 if ($pref_value == 'default') {
189 return unset_user_preference($fullprefname.$itemid);
191 return set_user_preference($fullprefname.$itemid, $pref_value);
196 * Handles form data sent by this report for this report. Abstract method to implement in all children.
199 * @return mixed True or array of errors
201 function process_data($data) {
202 // Implement in children classes
206 * Processes a single action against a category, grade_item or grade.
207 * @param string $target Sortorder
208 * @param string $action Which action to take (edit, delete etc...)
211 function process_action($target, $action) {
212 //implement if needed
216 * First checks the cached language strings, then returns match if found, or uses get_string()
217 * to get it from the DB, caches it then returns it.
218 * @param string $strcode
219 * @param string $section Optional language section
222 function get_lang_string($strcode, $section=null) {
223 if (empty($this->lang_strings
[$strcode])) {
224 $this->lang_strings
[$strcode] = get_string($strcode, $section);
226 return $this->lang_strings
[$strcode];
230 * Computes then returns the percentage value of the grade value within the given range.
231 * @param float $gradeval
232 * @param float $grademin
233 * @param float $grademx
234 * @return float $percentage
236 function grade_to_percentage($gradeval, $grademin, $grademax) {
237 if ($grademin >= $grademax) {
238 debugging("The minimum grade ($grademin) was higher than or equal to the maximum grade ($grademax)!!! Cannot proceed with computation.");
240 $offset_value = $gradeval - $grademin;
241 $offset_max = $grademax - $grademin;
242 $factor = 100 / $offset_max;
243 $percentage = $offset_value * $factor;
248 * Fetches and returns an array of grade letters indexed by their grade boundaries, as stored in preferences.
251 function get_grade_letters() {
253 for ($i = 1; $i <= 10; $i++
) {
254 $boundary = grade_report
::get_pref('gradeboundary' . $i);
255 $letter = grade_report
::get_pref('gradeletter' . $i);
256 if (!is_null($boundary) && $boundary != -1 && !empty($letter)) {
257 $letters[$boundary] = $letter;
264 * Fetches and returns a count of all the users that will be shown on this page.
265 * @return int Count of users
267 function get_numusers() {
270 $countsql = "SELECT COUNT(DISTINCT u.id)
271 FROM {$CFG->prefix}grade_grades g RIGHT OUTER JOIN
272 {$CFG->prefix}user u ON u.id = g.userid
273 LEFT JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
275 WHERE ra.roleid in ($this->gradebookroles)
277 AND ra.contextid ".get_related_contexts_string($this->context
);
278 return count_records_sql($countsql);
282 * Sets up this object's group variables, mainly to restrict the selection of users to display.
284 function setup_groups() {
287 /// find out current groups mode
288 $this->group_selector
= groups_print_course_menu($this->course
, $this->pbarurl
, true);
289 $this->currentgroup
= groups_get_course_group($this->course
);
291 if ($this->currentgroup
) {
292 $this->groupsql
= " LEFT JOIN {$CFG->prefix}groups_members gm ON gm.userid = u.id ";
293 $this->groupwheresql
= " AND gm.groupid = $this->currentgroup ";
298 * Returns an arrow icon inside an <a> tag, for the purpose of sorting a column.
299 * @param string $direction
300 * @param string $sort_link
303 function get_sort_arrow($direction='move', $sort_link=null) {
304 $matrix = array('up' => 'asc', 'down' => 'desc', 'move' => 'desc');
305 $strsort = $this->get_lang_string('sort' . $matrix[$direction]);
306 $arrow = print_arrow($direction, $strsort, true);
307 $html = '<a href="'.$sort_link .'">' . $arrow . '</a>';
312 * Builds and returns a HTML link to the grade or view page of the module given.
313 * If no itemmodule is given, only the name of the category/item is returned, no link.
314 * @param string $modulename The shortname of the module, will become the visible header
315 * @param string $itemmodule The name of the module type (e.g. assignment, quiz...)
316 * @param int $iteminstance The instance number of the module
317 * @return string HTML
319 function get_module_link($modulename, $itemmodule=null, $iteminstance=null) {
323 if (!is_null($itemmodule) AND !is_null($iteminstance)) {
324 // Add module icon if toggle is enabled
325 if ($this->get_pref('showactivityicons')) {
326 $modulename = '<img src="' . $CFG->modpixpath
. '/' . $itemmodule
327 . '/icon.gif" class="icon activity" alt="' . $modulename . '" />' . $modulename;
330 $coursemodule = get_coursemodule_from_instance($itemmodule, $iteminstance, $this->course
->id
);
332 $dir = $CFG->dirroot
. "/mod/$itemmodule/";
333 $url = $CFG->wwwroot
. "/mod/$itemmodule/";
335 if (file_exists($dir . 'grade.php')) {
341 $url .= "?id=$coursemodule->id";
342 return '<a href="' . $url . '">' . $modulename . '</a>';