2007-07-16 04:09:00 +00:00
< ? php // $Id$
2007-10-10 06:34:20 +00:00
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
// //
// This program 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 2 of the License, or //
// (at your option) any later version. //
// //
// This program 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: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
2007-07-16 04:09:00 +00:00
/**
* File in which the user_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 user report building and displaying .
* @ uses grade_report
* @ package gradebook
*/
class grade_report_user extends grade_report {
/**
* The user .
* @ var object $user
*/
var $user ;
/**
* A flexitable to hold the data .
* @ var object $table
*/
var $table ;
2007-10-08 23:09:10 +00:00
/**
* Flat structure similar to grade tree
*/
var $gseq ;
2007-10-27 15:33:43 +00:00
/**
* show student ranks
*/
var $showrank ;
/**
* Show hidden items even when user does not have required cap
*/
var $showhiddenitems ;
2007-07-16 04:09:00 +00:00
/**
* Constructor . Sets local copies of user preferences and initialises grade_tree .
* @ param int $courseid
2007-07-17 06:09:03 +00:00
* @ param object $gpr grade plugin return tracking object
2007-07-16 04:09:00 +00:00
* @ param string $context
* @ param int $userid The id of the user
*/
2007-07-17 06:09:03 +00:00
function grade_report_user ( $courseid , $gpr , $context , $userid ) {
2007-07-16 04:09:00 +00:00
global $CFG ;
2007-07-17 06:09:03 +00:00
parent :: grade_report ( $courseid , $gpr , $context );
2007-07-16 04:09:00 +00:00
2007-10-30 21:25:50 +00:00
$this -> showrank = grade_get_setting ( $this -> courseid , 'report_user_showrank' , $CFG -> grade_report_user_showrank );
$this -> showhiddenitems = grade_get_setting ( $this -> courseid , 'report_user_showhiddenitems' , $CFG -> grade_report_user_showhiddenitems );
2007-10-27 15:33:43 +00:00
2007-10-08 23:09:10 +00:00
$switch = grade_get_setting ( $this -> courseid , 'aggregationposition' , $CFG -> grade_aggregationposition );
2007-10-28 12:51:16 +00:00
// Grab the grade_seq for this course
2007-10-08 23:09:10 +00:00
$this -> gseq = new grade_seq ( $this -> courseid , $switch );
2007-07-25 19:57:47 +00:00
2007-07-16 04:09:00 +00:00
// get the user (for full name)
$this -> user = get_record ( 'user' , 'id' , $userid );
// base url for sorting by first/last name
$this -> baseurl = $CFG -> wwwroot . '/grade/report?id=' . $courseid . '&userid=' . $userid ;
2007-07-20 09:34:35 +00:00
$this -> pbarurl = $this -> baseurl ;
2007-07-16 04:09:00 +00:00
2007-10-28 12:51:16 +00:00
// no groups on this report - rank is from all course users
2007-07-20 09:34:35 +00:00
$this -> setup_table ();
2007-07-16 04:09:00 +00:00
}
/**
* Prepares the headers and attributes of the flexitable .
*/
function setup_table () {
2007-10-21 12:56:19 +00:00
global $CFG ;
2007-07-16 04:09:00 +00:00
/*
2007-10-28 12:51:16 +00:00
* Table has 5 - 6 columns
*| itemname / description | final grade | percentage final grade | rank ( optional ) | feedback |
*/
2007-07-16 04:09:00 +00:00
// setting up table headers
2007-10-27 15:33:43 +00:00
if ( $this -> showrank ) {
2007-10-21 12:56:19 +00:00
// TODO: this is broken if hidden grades present!!
$tablecolumns = array ( 'itemname' , 'category' , 'grade' , 'percentage' , 'rank' , 'feedback' );
$tableheaders = array ( $this -> get_lang_string ( 'gradeitem' , 'grades' ), $this -> get_lang_string ( 'category' ), $this -> get_lang_string ( 'grade' ),
$this -> get_lang_string ( 'percent' , 'grades' ), $this -> get_lang_string ( 'rank' , 'grades' ),
$this -> get_lang_string ( 'feedback' ));
} else {
$tablecolumns = array ( 'itemname' , 'category' , 'grade' , 'percentage' , 'feedback' );
$tableheaders = array ( $this -> get_lang_string ( 'gradeitem' , 'grades' ), $this -> get_lang_string ( 'category' ), $this -> get_lang_string ( 'grade' ),
$this -> get_lang_string ( 'percent' , 'grades' ), $this -> get_lang_string ( 'feedback' ));
}
2007-07-16 04:09:00 +00:00
$this -> table = new flexible_table ( 'grade-report-user-' . $this -> courseid );
$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' , 'user-grade' );
$this -> table -> set_attribute ( 'class' , 'boxaligncenter generaltable' );
2007-10-27 20:37:22 +00:00
// not sure tables should be sortable or not, because if we allow it then sorted results distort grade category structure and sortorder
2007-07-16 04:09:00 +00:00
$this -> table -> set_control_variables ( array (
TABLE_VAR_SORT => 'ssort' ,
TABLE_VAR_HIDE => 'shide' ,
TABLE_VAR_SHOW => 'sshow' ,
TABLE_VAR_IFIRST => 'sifirst' ,
TABLE_VAR_ILAST => 'silast' ,
TABLE_VAR_PAGE => 'spage'
));
$this -> table -> setup ();
}
function fill_table () {
global $CFG ;
2007-09-05 04:39:07 +00:00
$numusers = $this -> get_numusers ( false ); // total course users
2007-10-17 09:45:14 +00:00
$items =& $this -> gseq -> items ;
$grades = array ();
2007-07-16 04:09:00 +00:00
2007-10-17 20:38:52 +00:00
$canviewhidden = has_capability ( 'moodle/grade:viewhidden' , get_context_instance ( CONTEXT_COURSE , $this -> courseid ));
2007-07-16 04:09:00 +00:00
2007-10-21 13:22:07 +00:00
// fetch or create all grades
2007-10-17 09:45:14 +00:00
foreach ( $items as $key => $unused ) {
2007-10-21 13:22:07 +00:00
if ( ! $grade_grade = grade_grade :: fetch ( array ( 'itemid' => $items [ $key ] -> id , 'userid' => $this -> user -> id ))) {
$grade_grade = new grade_grade ();
$grade_grade -> userid = $this -> user -> id ;
$grade_grade -> itemid = $items [ $key ] -> id ;
}
2007-10-17 09:45:14 +00:00
$grades [ $key ] = $grade_grade ;
2007-10-21 13:22:07 +00:00
$grades [ $key ] -> grade_item =& $items [ $key ];
2007-10-17 09:45:14 +00:00
}
2007-10-17 20:38:52 +00:00
if ( $canviewhidden ) {
2007-10-19 08:51:52 +00:00
$altered = array ();
$unknown = array ();
2007-10-17 20:38:52 +00:00
} else {
$hiding_affected = grade_grade :: get_hiding_affected ( $grades , $items );
2007-10-19 08:51:52 +00:00
$altered = $hiding_affected [ 'altered' ];
$unknown = $hiding_affected [ 'unknown' ];
unset ( $hiding_affected );
2007-10-17 20:38:52 +00:00
}
2007-10-17 09:45:14 +00:00
2007-10-19 08:51:52 +00:00
foreach ( $items as $itemid => $unused ) {
$grade_item =& $items [ $itemid ];
$grade_grade =& $grades [ $itemid ];
2007-10-30 21:25:50 +00:00
if ( ! $canviewhidden and $grade_item -> is_hidden ()) {
if ( $this -> showhiddenitems == 0 ) {
// no hidden items at all
continue ;
} else if ( $this -> showhiddenitems == 1 and ! $grade_item -> is_hiddenuntil ()) {
// hidden until that are still hidden are visible
continue ;
}
2007-10-21 12:56:19 +00:00
}
2007-10-28 12:51:16 +00:00
$class = 'gradeitem' ;
if ( $grade_item -> is_course_item ()) {
$class = 'courseitem' ;
} else if ( $grade_item -> is_category_item ()) {
$class = 'categoryitem' ;
}
2007-10-19 08:51:52 +00:00
if ( in_array ( $itemid , $unknown )) {
$gradeval = null ;
} else if ( array_key_exists ( $itemid , $altered )) {
$gradeval = $altered [ $itemid ];
} else {
$gradeval = $grade_grade -> finalgrade ;
}
2007-10-17 09:45:14 +00:00
$data = array ();
2007-07-16 04:09:00 +00:00
2007-10-08 23:09:10 +00:00
/// prints grade item name
if ( $grade_item -> is_course_item () or $grade_item -> is_category_item ()) {
2007-10-28 12:51:16 +00:00
$data [] = '<span class="' . $class . '">' . $grade_item -> get_name () . '</span>' ;
2007-10-08 23:09:10 +00:00
} else {
2007-10-28 12:51:16 +00:00
$data [] = '<span class="' . $class . '">' . $this -> get_module_link ( $grade_item -> get_name (), $grade_item -> itemmodule , $grade_item -> iteminstance ) . '</span>' ;
2007-10-08 23:09:10 +00:00
}
2007-07-16 04:09:00 +00:00
2007-10-08 23:09:10 +00:00
/// prints category
$cat = $grade_item -> get_parent_category ();
2007-10-28 12:51:16 +00:00
$data [] = '<span class="' . $class . '">' . $cat -> get_name () . '</span>' ;
2007-08-24 05:02:51 +00:00
2007-10-08 23:09:10 +00:00
/// prints the grade
if ( $grade_grade -> is_excluded ()) {
$excluded = get_string ( 'excluded' , 'grades' ) . ' ' ;
} else {
$excluded = '' ;
}
2007-07-16 04:09:00 +00:00
2007-10-17 20:38:52 +00:00
if ( $grade_item -> needsupdate ) {
2007-10-28 12:51:16 +00:00
$data [] = '<span class="' . $class . ' gradingerror">' . get_string ( 'error' ) . '</span>' ;
2007-10-17 20:38:52 +00:00
2007-10-19 08:51:52 +00:00
} else if ( ! empty ( $CFG -> grade_hiddenasdate ) and ! is_null ( $grade_grade -> finalgrade ) and ! $canviewhidden and $grade_grade -> is_hidden ()
and ! $grade_item -> is_category_item () and ! $grade_item -> is_course_item ()) {
// the problem here is that we do not have the time when grade value was modified, 'timemodified' is general modification date for grade_grades records
2007-10-28 12:51:16 +00:00
$data [] = '<span class="' . $class . ' gradeddate">' . $excluded . get_string ( 'gradedon' , 'grades' , userdate ( $grade_grade -> timemodified , get_string ( 'strftimedatetimeshort' ))) . '</span>' ;
2007-10-17 09:45:14 +00:00
2007-10-08 23:09:10 +00:00
} else {
2007-10-28 12:51:16 +00:00
$data [] = '<span class="' . $class . '">' . $excluded . grade_format_gradevalue ( $gradeval , $grade_item , true );
2007-10-08 23:09:10 +00:00
}
/// prints percentage
2007-10-17 20:38:52 +00:00
if ( $grade_item -> needsupdate ) {
2007-10-28 12:51:16 +00:00
$data [] = '<span class="' . $class . 'gradingerror">' . get_string ( 'error' ) . '</span>' ;
2007-07-21 14:24:39 +00:00
2007-10-17 09:45:14 +00:00
} else {
2007-10-28 12:51:16 +00:00
$data [] = '<span class="' . $class . '">' . grade_format_gradevalue ( $gradeval , $grade_item , true , GRADE_DISPLAY_TYPE_PERCENTAGE ) . '</span>' ;
2007-10-08 23:09:10 +00:00
}
2007-10-17 09:45:14 +00:00
2007-10-08 23:09:10 +00:00
/// prints rank
2007-10-27 15:33:43 +00:00
if ( $this -> showrank ) {
2007-10-21 12:56:19 +00:00
// TODO: this is broken if hidden grades present!!
if ( $grade_item -> needsupdate ) {
2007-10-28 12:51:16 +00:00
$data [] = '<span class="' . $class . 'gradingerror">' . get_string ( 'error' ) . '</span>' ;
2007-10-27 15:33:43 +00:00
2007-10-21 12:56:19 +00:00
} else if ( is_null ( $gradeval )) {
// no grade, no rank
2007-10-28 12:51:16 +00:00
$data [] = '<span class="' . $class . '">-</span>' ;;
2007-10-27 15:33:43 +00:00
2007-10-21 12:56:19 +00:00
} else {
/// find the number of users with a higher grade
$sql = " SELECT COUNT(DISTINCT(userid))
FROM { $CFG -> prefix } grade_grades
WHERE finalgrade > { $grade_grade -> finalgrade }
AND itemid = { $grade_item -> id } " ;
$rank = count_records_sql ( $sql ) + 1 ;
2007-10-27 15:33:43 +00:00
2007-10-28 12:51:16 +00:00
$data [] = '<span class="' . $class . '">' . " $rank / $numusers " . '</span>' ;
2007-10-21 12:56:19 +00:00
}
2007-07-16 04:09:00 +00:00
}
2007-10-28 12:51:16 +00:00
/// prints feedback
if ( empty ( $grade_grade -> feedback ) or ( ! $canviewhidden and $grade_grade -> is_hidden ())) {
$data [] = '<div class="feedbacktext"> </div>' ;
2007-10-17 09:45:14 +00:00
} else {
2007-10-28 12:51:16 +00:00
$data [] = '<div class="feedbacktext">' . format_text ( $grade_grade -> feedback , $grade_grade -> feedbackformat ) . '</div>' ;
2007-10-08 23:09:10 +00:00
}
2007-10-17 09:45:14 +00:00
2007-10-08 23:09:10 +00:00
$this -> table -> add_data ( $data );
2007-07-16 04:09:00 +00:00
}
2007-10-08 23:09:10 +00:00
return true ;
2007-07-16 04:09:00 +00:00
}
/**
* 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
*/
function print_table ( $return = false ) {
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 ) .
*/
function process_data ( $data ) {
}
2007-10-27 15:33:43 +00:00
}
function grade_report_user_settings_definition ( & $mform ) {
global $CFG ;
$options = array ( - 1 => get_string ( 'default' , 'grades' ),
0 => get_string ( 'hide' ),
1 => get_string ( 'show' ));
2007-10-27 20:37:22 +00:00
if ( empty ( $CFG -> grade_report_user_showrank )) {
2007-10-27 15:33:43 +00:00
$options [ - 1 ] = get_string ( 'defaultprev' , 'grades' , $options [ 0 ]);
} else {
$options [ - 1 ] = get_string ( 'defaultprev' , 'grades' , $options [ 1 ]);
}
$mform -> addElement ( 'select' , 'report_user_showrank' , get_string ( 'showrank' , 'grades' ), $options );
$mform -> setHelpButton ( 'report_user_showrank' , array ( false , get_string ( 'showrank' , 'grades' ),
false , true , false , get_string ( 'configshowrank' , 'grades' )));
$options = array ( - 1 => get_string ( 'default' , 'grades' ),
0 => get_string ( 'hide' ),
2007-10-30 21:25:50 +00:00
1 => get_string ( 'showhiddenuntilonly' , 'grades' ),
2 => get_string ( 'show' ));
2007-10-27 15:33:43 +00:00
2007-10-27 20:37:22 +00:00
if ( empty ( $CFG -> grade_report_user_showhiddenitems )) {
2007-10-27 15:33:43 +00:00
$options [ - 1 ] = get_string ( 'defaultprev' , 'grades' , $options [ 0 ]);
} else {
$options [ - 1 ] = get_string ( 'defaultprev' , 'grades' , $options [ 1 ]);
}
$mform -> addElement ( 'select' , 'report_user_showhiddenitems' , get_string ( 'showhiddenitems' , 'grades' ), $options );
$mform -> setHelpButton ( 'report_user_showhiddenitems' , array ( false , get_string ( 'showhiddenitems' , 'grades' ),
false , true , false , get_string ( 'configshowhiddenitems' , 'grades' )));
2007-10-28 17:15:00 +00:00
}
function grade_report_user_profilereport ( $course , $user ) {
if ( ! empty ( $course -> showgrades )) {
$context = get_context_instance ( CONTEXT_COURSE , $course -> id );
//first make sure we have proper final grades - this must be done before constructing of the grade tree
grade_regrade_final_grades ( $course -> id );
/// return tracking object
$gpr = new grade_plugin_return ( array ( 'type' => 'report' , 'plugin' => 'user' , 'courseid' => $course -> id , 'userid' => $user -> id ));
// Create a report instance
$report = new grade_report_user ( $course -> id , $gpr , $context , $user -> id );
2007-07-16 04:09:00 +00:00
2007-10-28 17:15:00 +00:00
// print the page
echo '<div class="grade-report-user">' ; // css fix to share styles with real report page
print_heading ( get_string ( 'modulename' , 'gradereport_user' ) . ' - ' . fullname ( $report -> user ));
if ( $report -> fill_table ()) {
echo $report -> print_table ( true );
}
echo '</div>' ;
}
2007-07-16 04:09:00 +00:00
}
2007-10-28 17:15:00 +00:00
2007-07-16 04:09:00 +00:00
?>