MDL-11718 course settings for reports - for now only user report (rank and showing...
[moodle.git] / grade / report / overview / lib.php
CommitLineData
aa330ebb 1<?php // $Id$
8ad36f4c 2
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///////////////////////////////////////////////////////////////////////////
aa330ebb 25/**
26 * File in which the overview_report class is defined.
27 * @package gradebook
28 */
29
30require_once($CFG->dirroot . '/grade/report/lib.php');
31require_once($CFG->libdir.'/tablelib.php');
32
33/**
34 * Class providing an API for the overview report building and displaying.
35 * @uses grade_report
36 * @package gradebook
37 */
38class grade_report_overview extends grade_report {
39
40 /**
41 * The user.
42 * @var object $user
43 */
44 var $user;
45
46 /**
47 * A flexitable to hold the data.
48 * @var object $table
49 */
50 var $table;
51
52 /**
53 * Constructor. Sets local copies of user preferences and initialises grade_tree.
54 * @param int $userid
55 * @param object $gpr grade plugin return tracking object
56 * @param string $context
57 */
58 function grade_report_overview($userid, $gpr, $context) {
59 global $CFG, $COURSE;
60 parent::grade_report($COURSE->id, $gpr, $context);
61
62 // get the user (for full name)
63 $this->user = get_record('user', 'id', $userid);
64
65 // base url for sorting by first/last name
66 $this->baseurl = $CFG->wwwroot.'/grade/overview/index.php?id='.$userid;
67 $this->pbarurl = $this->baseurl;
68
69 $this->setup_table();
70 }
71
72 /**
73 * Prepares the headers and attributes of the flexitable.
74 */
75 function setup_table() {
76 /*
77 * Table has 3 columns
78 *| course | final grade | rank |
79 */
80
81 // setting up table headers
82 $tablecolumns = array('coursename', 'grade', 'rank');
83 $tableheaders = array($this->get_lang_string('coursename', 'grades'),
84 $this->get_lang_string('grade'),
85 $this->get_lang_string('rank', 'grades'));
86
87 $this->table = new flexible_table('grade-report-overview-'.$this->user->id);
88
89 $this->table->define_columns($tablecolumns);
90 $this->table->define_headers($tableheaders);
91 $this->table->define_baseurl($this->baseurl);
92
93 $this->table->set_attribute('cellspacing', '0');
94 $this->table->set_attribute('id', 'overview-grade');
95 $this->table->set_attribute('class', 'boxaligncenter generaltable');
96
97 $this->table->setup();
98 }
99
100 function fill_table() {
101 global $CFG;
102 $numusers = $this->get_numusers();
103
9a10e2d8 104 // if ($courses = get_courses('all', null, 'c.id, c.shortname')) {
105 // MDL-11679, only show 'mycourses' instead of all courses
106 if ($courses = get_my_courses($this->user->id, 'c.sortorder ASC', 'id, shortname')) {
aa330ebb 107 foreach ($courses as $course) {
108 // Get course grade_item
e699389e 109 if (!$grade_item = grade_item::fetch(array('itemtype' => 'course', 'courseid' => $course->id))) {
110 // Create the course item if it doesn't already exist.
111 $coursecat = grade_category::fetch_course_category($course->id);
112 $grade_item = $coursecat->get_grade_item();
113 }
aa330ebb 114
115 // Get the grade
7d7029f5 116 $finalgrade = get_field('grade_grades', 'finalgrade', 'itemid', $grade_item->id, 'userid', $this->user->id);
aa330ebb 117
118 /// prints rank
119 if ($finalgrade) {
120 /// find the number of users with a higher grade
121 $sql = "SELECT COUNT(DISTINCT(userid))
122 FROM {$CFG->prefix}grade_grades
123 WHERE finalgrade > $finalgrade
7d7029f5 124 AND itemid = $grade_item->id";
aa330ebb 125 $rank = count_records_sql($sql) + 1;
126
127 $rankdata = "$rank/$numusers";
128 } else {
129 // no grade, no rank
130 $rankdata = "-";
131 }
132
7d7029f5 133 $courselink = '<a href="' . $CFG->wwwroot . '/grade/report/user/index.php?id=' . $course->id . '">' . $course->shortname . '</a>';
134
135 $this->table->add_data(array($courselink,
136 round(grade_to_percentage($finalgrade, $grade_item->grademin, $grade_item->grademax), 1) . '%',
137 $rankdata));
aa330ebb 138 }
139
140 return true;
141 } else {
142 notify(get_string('nocourses', 'grades'));
143 return false;
144 }
145 }
146
147 /**
148 * Prints or returns the HTML from the flexitable.
149 * @param bool $return Whether or not to return the data instead of printing it directly.
150 * @return string
151 */
152 function print_table($return=false) {
153 ob_start();
154 $this->table->print_html();
155 $html = ob_get_clean();
156 if ($return) {
157 return $html;
158 } else {
159 echo $html;
160 }
161 }
162
163 /**
164 * Processes the data sent by the form (grades and feedbacks).
165 * @var array $data
166 * @return bool Success or Failure (array of errors).
167 */
168 function process_data($data) {
169 }
170
171}
172?>