2009-07-07 02:26:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-10 06:34:20 +00:00
|
|
|
|
2007-08-10 09:34:50 +00:00
|
|
|
/**
|
|
|
|
* File in which the overview_report class is defined.
|
|
|
|
* @package gradebook
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once($CFG->dirroot . '/grade/report/lib.php');
|
|
|
|
require_once($CFG->libdir.'/tablelib.php');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class providing an API for the overview report building and displaying.
|
|
|
|
* @uses grade_report
|
|
|
|
* @package gradebook
|
|
|
|
*/
|
|
|
|
class grade_report_overview extends grade_report {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The user.
|
|
|
|
* @var object $user
|
|
|
|
*/
|
2008-06-03 16:10:57 +00:00
|
|
|
public $user;
|
2007-08-10 09:34:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A flexitable to hold the data.
|
|
|
|
* @var object $table
|
|
|
|
*/
|
2008-06-03 16:10:57 +00:00
|
|
|
public $table;
|
2007-08-10 09:34:50 +00:00
|
|
|
|
2007-10-27 17:58:36 +00:00
|
|
|
/**
|
|
|
|
* show student ranks
|
|
|
|
*/
|
2008-06-03 16:10:57 +00:00
|
|
|
public $showrank;
|
2007-10-27 17:58:36 +00:00
|
|
|
|
2007-08-10 09:34:50 +00:00
|
|
|
/**
|
|
|
|
* Constructor. Sets local copies of user preferences and initialises grade_tree.
|
|
|
|
* @param int $userid
|
|
|
|
* @param object $gpr grade plugin return tracking object
|
|
|
|
* @param string $context
|
|
|
|
*/
|
2008-06-03 16:10:57 +00:00
|
|
|
public function __construct($userid, $gpr, $context) {
|
|
|
|
global $CFG, $COURSE, $DB;
|
|
|
|
parent::__construct($COURSE->id, $gpr, $context);
|
2007-08-10 09:34:50 +00:00
|
|
|
|
2007-10-27 17:58:36 +00:00
|
|
|
$this->showrank = grade_get_setting($this->courseid, 'report_overview_showrank', !empty($CFG->grade_report_overview_showrank));
|
|
|
|
|
2007-08-10 09:34:50 +00:00
|
|
|
// get the user (for full name)
|
2008-06-03 16:10:57 +00:00
|
|
|
$this->user = $DB->get_record('user', array('id' => $userid));
|
2007-08-10 09:34:50 +00:00
|
|
|
|
|
|
|
// base url for sorting by first/last name
|
|
|
|
$this->baseurl = $CFG->wwwroot.'/grade/overview/index.php?id='.$userid;
|
|
|
|
$this->pbarurl = $this->baseurl;
|
|
|
|
|
|
|
|
$this->setup_table();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares the headers and attributes of the flexitable.
|
|
|
|
*/
|
2008-06-03 16:10:57 +00:00
|
|
|
public function setup_table() {
|
2007-08-10 09:34:50 +00:00
|
|
|
/*
|
2007-10-27 17:58:36 +00:00
|
|
|
* Table has 3 columns
|
|
|
|
*| course | final grade | rank (optional) |
|
|
|
|
*/
|
2007-08-10 09:34:50 +00:00
|
|
|
|
|
|
|
// setting up table headers
|
2007-10-27 17:58:36 +00:00
|
|
|
if ($this->showrank) {
|
|
|
|
$tablecolumns = array('coursename', 'grade', 'rank');
|
|
|
|
$tableheaders = array($this->get_lang_string('coursename', 'grades'),
|
|
|
|
$this->get_lang_string('grade'),
|
|
|
|
$this->get_lang_string('rank', 'grades'));
|
|
|
|
} else {
|
|
|
|
$tablecolumns = array('coursename', 'grade');
|
|
|
|
$tableheaders = array($this->get_lang_string('coursename', 'grades'),
|
|
|
|
$this->get_lang_string('grade'));
|
|
|
|
}
|
2007-08-10 09:34:50 +00:00
|
|
|
$this->table = new flexible_table('grade-report-overview-'.$this->user->id);
|
|
|
|
|
|
|
|
$this->table->define_columns($tablecolumns);
|
|
|
|
$this->table->define_headers($tableheaders);
|
|
|
|
$this->table->define_baseurl($this->baseurl);
|
|
|
|
|
|
|
|
$this->table->set_attribute('cellspacing', '0');
|
|
|
|
$this->table->set_attribute('id', 'overview-grade');
|
|
|
|
$this->table->set_attribute('class', 'boxaligncenter generaltable');
|
|
|
|
|
|
|
|
$this->table->setup();
|
|
|
|
}
|
|
|
|
|
2008-06-03 16:10:57 +00:00
|
|
|
public function fill_table() {
|
2009-08-18 04:59:57 +00:00
|
|
|
global $CFG, $DB, $OUTPUT;
|
2007-08-10 09:34:50 +00:00
|
|
|
|
2007-10-16 05:35:24 +00:00
|
|
|
// MDL-11679, only show 'mycourses' instead of all courses
|
2008-10-13 12:35:23 +00:00
|
|
|
if ($courses = get_my_courses($this->user->id, 'c.sortorder ASC', 'id, shortname, showgrades')) {
|
2007-10-31 08:59:25 +00:00
|
|
|
$numusers = $this->get_numusers(false);
|
2007-10-27 17:58:36 +00:00
|
|
|
|
2007-08-10 09:34:50 +00:00
|
|
|
foreach ($courses as $course) {
|
2008-10-13 12:35:23 +00:00
|
|
|
if (!$course->showgrades) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-10-27 17:58:36 +00:00
|
|
|
$courselink = '<a href="'.$CFG->wwwroot.'/grade/report/user/index.php?id='.$course->id.'">'.$course->shortname.'</a>';
|
2008-10-14 20:36:04 +00:00
|
|
|
$canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $course->id));
|
2007-10-27 17:58:36 +00:00
|
|
|
|
2007-08-10 09:34:50 +00:00
|
|
|
// Get course grade_item
|
2008-10-14 20:36:04 +00:00
|
|
|
$course_item = grade_item::fetch_course_item($course->id);
|
|
|
|
|
|
|
|
// Get the stored grade
|
|
|
|
$course_grade = new grade_grade(array('itemid'=>$course_item->id, 'userid'=>$this->user->id));
|
|
|
|
$course_grade->grade_item =& $course_item;
|
|
|
|
$finalgrade = $course_grade->finalgrade;
|
|
|
|
|
|
|
|
if (!$canviewhidden and !is_null($finalgrade)) {
|
|
|
|
if ($course_grade->is_hidden()) {
|
|
|
|
$finalgrade = null;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// This is a really ugly hack, it will be fixed in 2.0
|
|
|
|
$items = grade_item::fetch_all(array('courseid'=>$course->id));
|
|
|
|
$grades = array();
|
|
|
|
$sql = "SELECT g.*
|
|
|
|
FROM {grade_grades} g
|
|
|
|
JOIN {grade_items} gi ON gi.id = g.itemid
|
|
|
|
WHERE g.userid = ? AND gi.courseid = ?";
|
|
|
|
if ($gradesrecords = $DB->get_records_sql($sql, array($this->user->id, $course->id))) {
|
|
|
|
foreach ($gradesrecords as $grade) {
|
|
|
|
$grades[$grade->itemid] = new grade_grade($grade, false);
|
|
|
|
}
|
|
|
|
unset($gradesrecords);
|
|
|
|
}
|
|
|
|
foreach ($items as $itemid=>$unused) {
|
|
|
|
if (!isset($grades[$itemid])) {
|
|
|
|
$grade_grade = new grade_grade();
|
|
|
|
$grade_grade->userid = $this->user->id;
|
|
|
|
$grade_grade->itemid = $items[$itemid]->id;
|
|
|
|
$grades[$itemid] = $grade_grade;
|
|
|
|
}
|
|
|
|
$grades[$itemid]->grade_item =& $items[$itemid];
|
|
|
|
}
|
|
|
|
$hiding_affected = grade_grade::get_hiding_affected($grades, $items);
|
|
|
|
if (array_key_exists($course_item->id, $hiding_affected['altered'])) {
|
|
|
|
$finalgrade = $hiding_affected['altered'][$course_item->id];
|
|
|
|
|
|
|
|
} else if (!empty($hiding_affected['unknown'][$course_item->id])) {
|
|
|
|
$finalgrade = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($hiding_affected);
|
|
|
|
unset($grades);
|
|
|
|
unset($items);
|
|
|
|
}
|
2007-10-27 17:58:36 +00:00
|
|
|
}
|
|
|
|
|
2008-10-14 20:36:04 +00:00
|
|
|
$data = array($courselink, grade_format_gradevalue($finalgrade, $course_item, true));
|
2007-08-10 09:34:50 +00:00
|
|
|
|
2007-10-27 17:58:36 +00:00
|
|
|
if (!$this->showrank) {
|
|
|
|
//nothing to do
|
|
|
|
|
|
|
|
} else if (!is_null($finalgrade)) {
|
2007-08-10 09:34:50 +00:00
|
|
|
/// find the number of users with a higher grade
|
2008-10-14 20:36:04 +00:00
|
|
|
/// please note this can not work if hidden grades involved :-( to be fixed in 2.0
|
|
|
|
$params = array($finalgrade, $course_item->id);
|
2007-08-10 09:34:50 +00:00
|
|
|
$sql = "SELECT COUNT(DISTINCT(userid))
|
2008-06-02 16:06:33 +00:00
|
|
|
FROM {grade_grades}
|
2008-06-03 16:10:57 +00:00
|
|
|
WHERE finalgrade IS NOT NULL AND finalgrade > ?
|
|
|
|
AND itemid = ?";
|
|
|
|
$rank = $DB->count_records_sql($sql, $params) + 1;
|
2007-08-10 09:34:50 +00:00
|
|
|
|
2007-10-27 17:58:36 +00:00
|
|
|
$data[] = "$rank/$numusers";
|
|
|
|
|
2007-08-10 09:34:50 +00:00
|
|
|
} else {
|
|
|
|
// no grade, no rank
|
2007-10-27 17:58:36 +00:00
|
|
|
$data[] = '-';
|
2007-08-10 09:34:50 +00:00
|
|
|
}
|
|
|
|
|
2007-10-27 17:58:36 +00:00
|
|
|
$this->table->add_data($data);
|
2007-08-10 09:34:50 +00:00
|
|
|
}
|
|
|
|
return true;
|
2007-10-27 17:58:36 +00:00
|
|
|
|
2007-08-10 09:34:50 +00:00
|
|
|
} else {
|
2009-08-18 04:59:57 +00:00
|
|
|
echo $OUTPUT->notification(get_string('nocourses', 'grades'));
|
2007-08-10 09:34:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints or returns the HTML from the flexitable.
|
|
|
|
* @param bool $return Whether or not to return the data instead of printing it directly.
|
|
|
|
* @return string
|
|
|
|
*/
|
2008-06-03 16:10:57 +00:00
|
|
|
public function print_table($return=false) {
|
2007-08-10 09:34:50 +00:00
|
|
|
ob_start();
|
|
|
|
$this->table->print_html();
|
|
|
|
$html = ob_get_clean();
|
|
|
|
if ($return) {
|
|
|
|
return $html;
|
|
|
|
} else {
|
|
|
|
echo $html;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes the data sent by the form (grades and feedbacks).
|
|
|
|
* @var array $data
|
|
|
|
* @return bool Success or Failure (array of errors).
|
|
|
|
*/
|
2009-04-16 07:16:44 +00:00
|
|
|
function process_data($data) {
|
2008-06-03 16:10:57 +00:00
|
|
|
}
|
2009-04-16 07:16:44 +00:00
|
|
|
function process_action($target, $action) {
|
2007-08-10 09:34:50 +00:00
|
|
|
}
|
2007-10-27 17:58:36 +00:00
|
|
|
}
|
2007-08-10 09:34:50 +00:00
|
|
|
|
2007-10-27 17:58:36 +00:00
|
|
|
function grade_report_overview_settings_definition(&$mform) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$options = array(-1 => get_string('default', 'grades'),
|
|
|
|
0 => get_string('hide'),
|
|
|
|
1 => get_string('show'));
|
|
|
|
|
|
|
|
if (empty($CFG->grade_overviewreport_showrank)) {
|
|
|
|
$options[-1] = get_string('defaultprev', 'grades', $options[0]);
|
|
|
|
} else {
|
|
|
|
$options[-1] = get_string('defaultprev', 'grades', $options[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$mform->addElement('select', 'report_overview_showrank', get_string('showrank', 'grades'), $options);
|
2008-06-02 16:06:33 +00:00
|
|
|
$mform->setHelpButton('report_overview_showrank', array('showrank', get_string('showrank', 'grades'), 'grade'));
|
2007-08-10 09:34:50 +00:00
|
|
|
}
|
2007-10-27 17:58:36 +00:00
|
|
|
|
2009-11-01 12:11:29 +00:00
|
|
|
|