MDL-9137 Added the overview report
[moodle.git] / grade / report / overview / lib.php
CommitLineData
aa330ebb 1<?php // $Id$
2/**
3 * File in which the overview_report class is defined.
4 * @package gradebook
5 */
6
7require_once($CFG->dirroot . '/grade/report/lib.php');
8require_once($CFG->libdir.'/tablelib.php');
9
10/**
11 * Class providing an API for the overview report building and displaying.
12 * @uses grade_report
13 * @package gradebook
14 */
15class grade_report_overview extends grade_report {
16
17 /**
18 * The user.
19 * @var object $user
20 */
21 var $user;
22
23 /**
24 * A flexitable to hold the data.
25 * @var object $table
26 */
27 var $table;
28
29 /**
30 * Constructor. Sets local copies of user preferences and initialises grade_tree.
31 * @param int $userid
32 * @param object $gpr grade plugin return tracking object
33 * @param string $context
34 */
35 function grade_report_overview($userid, $gpr, $context) {
36 global $CFG, $COURSE;
37 parent::grade_report($COURSE->id, $gpr, $context);
38
39 // get the user (for full name)
40 $this->user = get_record('user', 'id', $userid);
41
42 // base url for sorting by first/last name
43 $this->baseurl = $CFG->wwwroot.'/grade/overview/index.php?id='.$userid;
44 $this->pbarurl = $this->baseurl;
45
46 $this->setup_table();
47 }
48
49 /**
50 * Prepares the headers and attributes of the flexitable.
51 */
52 function setup_table() {
53 /*
54 * Table has 3 columns
55 *| course | final grade | rank |
56 */
57
58 // setting up table headers
59 $tablecolumns = array('coursename', 'grade', 'rank');
60 $tableheaders = array($this->get_lang_string('coursename', 'grades'),
61 $this->get_lang_string('grade'),
62 $this->get_lang_string('rank', 'grades'));
63
64 $this->table = new flexible_table('grade-report-overview-'.$this->user->id);
65
66 $this->table->define_columns($tablecolumns);
67 $this->table->define_headers($tableheaders);
68 $this->table->define_baseurl($this->baseurl);
69
70 $this->table->set_attribute('cellspacing', '0');
71 $this->table->set_attribute('id', 'overview-grade');
72 $this->table->set_attribute('class', 'boxaligncenter generaltable');
73
74 $this->table->setup();
75 }
76
77 function fill_table() {
78 global $CFG;
79 $numusers = $this->get_numusers();
80
81 if ($courses = get_courses('all', null, 'c.id, c.shortname')) {
82 foreach ($courses as $course) {
83 // Get course grade_item
84 $grade_item_id = get_field('grade_items', 'id', 'itemtype', 'course', 'courseid', $course->id);
85
86 // Get the grade
87 $finalgrade = get_field('grade_grades', 'finalgrade', 'itemid', $grade_item_id, 'userid', $this->user->id);
88
89 /// prints rank
90 if ($finalgrade) {
91 /// find the number of users with a higher grade
92 $sql = "SELECT COUNT(DISTINCT(userid))
93 FROM {$CFG->prefix}grade_grades
94 WHERE finalgrade > $finalgrade
95 AND itemid = $grade_item_id";
96 $rank = count_records_sql($sql) + 1;
97
98 $rankdata = "$rank/$numusers";
99 } else {
100 // no grade, no rank
101 $rankdata = "-";
102 }
103
104 $this->table->add_data(array($course->shortname, $finalgrade, $rankdata));
105 }
106
107 return true;
108 } else {
109 notify(get_string('nocourses', 'grades'));
110 return false;
111 }
112 }
113
114 /**
115 * Prints or returns the HTML from the flexitable.
116 * @param bool $return Whether or not to return the data instead of printing it directly.
117 * @return string
118 */
119 function print_table($return=false) {
120 ob_start();
121 $this->table->print_html();
122 $html = ob_get_clean();
123 if ($return) {
124 return $html;
125 } else {
126 echo $html;
127 }
128 }
129
130 /**
131 * Processes the data sent by the form (grades and feedbacks).
132 * @var array $data
133 * @return bool Success or Failure (array of errors).
134 */
135 function process_data($data) {
136 }
137
138}
139?>