MDL-11718 partial cleanup in overview report - unfortunately the problems with hidden...
[moodle.git] / grade / report / overview / lib.php
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 //                                                                       //
5 // NOTICE OF COPYRIGHT                                                   //
6 //                                                                       //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
8 //          http://moodle.com                                            //
9 //                                                                       //
10 // Copyright (C) 1999 onwards  Martin Dougiamas  http://moodle.com       //
11 //                                                                       //
12 // This program is free software; you can redistribute it and/or modify  //
13 // it under the terms of the GNU General Public License as published by  //
14 // the Free Software Foundation; either version 2 of the License, or     //
15 // (at your option) any later version.                                   //
16 //                                                                       //
17 // This program is distributed in the hope that it will be useful,       //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
20 // GNU General Public License for more details:                          //
21 //                                                                       //
22 //          http://www.gnu.org/copyleft/gpl.html                         //
23 //                                                                       //
24 ///////////////////////////////////////////////////////////////////////////
25 /**
26  * File in which the overview_report class is defined.
27  * @package gradebook
28  */
30 require_once($CFG->dirroot . '/grade/report/lib.php');
31 require_once($CFG->libdir.'/tablelib.php');
33 /**
34  * Class providing an API for the overview report building and displaying.
35  * @uses grade_report
36  * @package gradebook
37  */
38 class grade_report_overview extends grade_report {
40     /**
41      * The user.
42      * @var object $user
43      */
44     var $user;
46     /**
47      * A flexitable to hold the data.
48      * @var object $table
49      */
50     var $table;
52     /**
53      * show student ranks
54      */
55     var $showrank;
57     /**
58      * Constructor. Sets local copies of user preferences and initialises grade_tree.
59      * @param int $userid
60      * @param object $gpr grade plugin return tracking object
61      * @param string $context
62      */
63     function grade_report_overview($userid, $gpr, $context) {
64         global $CFG, $COURSE;
65         parent::grade_report($COURSE->id, $gpr, $context);
67         $this->showrank = grade_get_setting($this->courseid, 'report_overview_showrank', !empty($CFG->grade_report_overview_showrank));
69         // get the user (for full name)
70         $this->user = get_record('user', 'id', $userid);
72         // base url for sorting by first/last name
73         $this->baseurl = $CFG->wwwroot.'/grade/overview/index.php?id='.$userid;
74         $this->pbarurl = $this->baseurl;
76         $this->setup_table();
77     }
79     /**
80      * Prepares the headers and attributes of the flexitable.
81      */
82     function setup_table() {
83         /*
84          * Table has 3 columns
85          *| course  | final grade | rank (optional) |
86          */
88         // setting up table headers
89         if ($this->showrank) {
90             $tablecolumns = array('coursename', 'grade', 'rank');
91             $tableheaders = array($this->get_lang_string('coursename', 'grades'),
92                                   $this->get_lang_string('grade'),
93                                   $this->get_lang_string('rank', 'grades'));
94         } else {
95             $tablecolumns = array('coursename', 'grade');
96             $tableheaders = array($this->get_lang_string('coursename', 'grades'),
97                                   $this->get_lang_string('grade'));
98         }
99         $this->table = new flexible_table('grade-report-overview-'.$this->user->id);
101         $this->table->define_columns($tablecolumns);
102         $this->table->define_headers($tableheaders);
103         $this->table->define_baseurl($this->baseurl);
105         $this->table->set_attribute('cellspacing', '0');
106         $this->table->set_attribute('id', 'overview-grade');
107         $this->table->set_attribute('class', 'boxaligncenter generaltable');
109         $this->table->setup();
110     }
112     function fill_table() {
113         global $CFG;
115         // MDL-11679, only show 'mycourses' instead of all courses
116         if ($courses = get_my_courses($this->user->id, 'c.sortorder ASC', 'id, shortname')) {
117             $numusers = $this->get_numusers();
119             foreach ($courses as $course) {
120                 $courselink = '<a href="'.$CFG->wwwroot.'/grade/report/user/index.php?id='.$course->id.'">'.$course->shortname.'</a>';
122                 // Get course grade_item
123                 $grade_item = grade_item::fetch_course_item($course->id);
125                 // Get the grade
126                 $grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$this->user->id));
127                 $grade->grade_item =& $grade_item;
128                 $finalgrade = $grade->finalgrade;
130                 // TODO: this DOES NOT work properly if there are any hidden grades,
131                 //       rank might be wrong & totals might be different from user report!!!
132                 if ($grade->is_hidden() and !has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $course->id))) {
133                     $finalgrade = null;
134                 }
136                 $data = array($courselink, grade_format_gradevalue($finalgrade, $grade_item, true));
138                 if (!$this->showrank) {
139                     //nothing to do
141                 } else if (!is_null($finalgrade)) {
142                     /// find the number of users with a higher grade
143                     $sql = "SELECT COUNT(DISTINCT(userid))
144                               FROM {$CFG->prefix}grade_grades
145                              WHERE finalgrade IS NOT NULL AND finalgrade > $finalgrade
146                                    AND itemid = {$grade_item->id}";
147                     $rank = count_records_sql($sql) + 1;
149                     $data[] = "$rank/$numusers";
151                 } else {
152                     // no grade, no rank
153                     $data[] = '-';
154                 }
156                 $this->table->add_data($data);
157             }
158             return true;
160         } else {
161             notify(get_string('nocourses', 'grades'));
162             return false;
163         }
164     }
166     /**
167      * Prints or returns the HTML from the flexitable.
168      * @param bool $return Whether or not to return the data instead of printing it directly.
169      * @return string
170      */
171     function print_table($return=false) {
172         ob_start();
173         $this->table->print_html();
174         $html = ob_get_clean();
175         if ($return) {
176             return $html;
177         } else {
178             echo $html;
179         }
180     }
182     /**
183      * Processes the data sent by the form (grades and feedbacks).
184      * @var array $data
185      * @return bool Success or Failure (array of errors).
186      */
187     function process_data($data) {
188     }
191 function grade_report_overview_settings_definition(&$mform) {
192     global $CFG;
194     $options = array(-1 => get_string('default', 'grades'),
195                       0 => get_string('hide'),
196                       1 => get_string('show'));
198     if (empty($CFG->grade_overviewreport_showrank)) {
199         $options[-1] = get_string('defaultprev', 'grades', $options[0]);
200     } else {
201         $options[-1] = get_string('defaultprev', 'grades', $options[1]);
202     }
204     $mform->addElement('select', 'report_overview_showrank', get_string('showrank', 'grades'), $options);
205     $mform->setHelpButton('report_overview_showrank', array(false, get_string('showrank', 'grades'),
206                           false, true, false, get_string('configshowrank', 'grades')));
209 ?>