MDL-43197 gradereport_user: Parents/mentors can see all the relevant grades

This commit is contained in:
Frederic Massart 2014-08-11 11:53:06 +08:00
parent e1eb180806
commit abbee05eb3
3 changed files with 58 additions and 8 deletions

View File

@ -79,6 +79,9 @@ $anyreport = has_capability('moodle/user:viewuseractivitiesreport', $personalco
$modes = array();
// Used for grade reports, it represents whether we should be viewing the report as ourselves, or as the targetted user.
$viewasuser = false;
if (has_capability('moodle/grade:viewall', $coursecontext)) {
//ok - can view all course grades
$modes[] = 'grade';
@ -90,10 +93,12 @@ if (has_capability('moodle/grade:viewall', $coursecontext)) {
} else if ($course->showgrades and has_capability('moodle/grade:viewall', $personalcontext)) {
// ok - can view grades of this user - parent most probably
$modes[] = 'grade';
$viewasuser = true;
} else if ($course->showgrades and $anyreport) {
// ok - can view grades of this user - parent most probably
$modes[] = 'grade';
$viewasuser = true;
}
if (empty($modes)) {
@ -132,7 +137,7 @@ switch ($mode) {
$functionname = 'grade_report_'.$CFG->grade_profilereport.'_profilereport';
if (function_exists($functionname)) {
$functionname($course, $user);
$functionname($course, $user, $viewasuser);
}
break;

View File

@ -1,6 +1,14 @@
This files describes API changes in /grade/report/*,
information provided here is intended especially for developers.
=== 2.6.5, 2.7.2 ===
* The callback function grade_report_*_profilereport now takes one more parameter $viewasuser. This parameter
is set to true when the report must be viewed as the user whose grades are being displayed. For instance,
when a mentor/parent is viewing the report, they should see the same grades, not more, not less. When the
setting is set to false (default), the capability checks, visibility and access levels are using the
currently logged in user.
=== 2.6 ===
* grade_report_grader::get_toggles_html() and grade_report_grader::print_toggle()
can not be used any more

View File

@ -151,14 +151,34 @@ class grade_report_user extends grade_report {
public $baseurl;
public $pbarurl;
/**
* The modinfo object to be used.
*
* @var course_modinfo
*/
protected $modinfo = null;
/**
* View as user.
*
* When this is set to true, the visibility checks, and capability checks will be
* applied to the user whose grades are being displayed. This is very useful when
* a mentor/parent is viewing the report of their mentee because they need to have
* access to the same information, but not more, not less.
*
* @var boolean
*/
protected $viewasuser = false;
/**
* Constructor. Sets local copies of user preferences and initialises grade_tree.
* @param int $courseid
* @param object $gpr grade plugin return tracking object
* @param string $context
* @param int $userid The id of the user
* @param bool $viewasuser Set this to true when the current user is a mentor/parent of the targetted user.
*/
public function __construct($courseid, $gpr, $context, $userid) {
public function __construct($courseid, $gpr, $context, $userid, $viewasuser = null) {
global $DB, $CFG;
parent::__construct($courseid, $gpr, $context);
@ -174,6 +194,8 @@ class grade_report_user extends grade_report {
$this->showlettergrade = grade_get_setting($this->courseid, 'report_user_showlettergrade', !empty($CFG->grade_report_user_showlettergrade));
$this->showaverage = grade_get_setting($this->courseid, 'report_user_showaverage', !empty($CFG->grade_report_user_showaverage));
$this->viewasuser = $viewasuser;
// The default grade decimals is 2
$defaultdecimals = 2;
if (property_exists($CFG, 'grade_decimalpoints')) {
@ -203,11 +225,19 @@ class grade_report_user extends grade_report {
$this->tabledata = array();
$this->canviewhidden = has_capability('moodle/grade:viewhidden', context_course::instance($this->courseid));
// get the user (for full name)
// Get the user (for full name).
$this->user = $DB->get_record('user', array('id' => $userid));
// What user are we viewing this as?
$coursecontext = context_course::instance($this->courseid);
if ($viewasuser) {
$this->modinfo = new course_modinfo($this->course, $this->user->id);
$this->canviewhidden = has_capability('moodle/grade:viewhidden', $coursecontext, $this->user->id);
} else {
$this->modinfo = $this->gtree->modinfo;
$this->canviewhidden = has_capability('moodle/grade:viewhidden', $coursecontext);
}
// base url for sorting by first/last name
$this->baseurl = $CFG->wwwroot.'/grade/report?id='.$courseid.'&userid='.$userid;
$this->pbarurl = $this->baseurl;
@ -363,7 +393,7 @@ class grade_report_user extends grade_report {
// The grade object can be marked visible but still be hidden if
// the student cannot see the activity due to conditional access
// and it's set to be hidden entirely.
$instances = $this->gtree->modinfo->get_instances_of($grade_object->itemmodule);
$instances = $this->modinfo->get_instances_of($grade_object->itemmodule);
if (!empty($instances[$grade_object->iteminstance])) {
$cm = $instances[$grade_object->iteminstance];
if (!$cm->uservisible) {
@ -904,7 +934,14 @@ function grade_report_user_settings_definition(&$mform) {
$mform->addHelpButton('report_user_showtotalsifcontainhidden', 'hidetotalifhiddenitems', 'grades');
}
function grade_report_user_profilereport($course, $user) {
/**
* Profile report callback.
*
* @param object $course The course.
* @param object $user The user.
* @param boolean $viewasuser True when we are viewing this as the targetted user sees it.
*/
function grade_report_user_profilereport($course, $user, $viewasuser = false) {
global $OUTPUT;
if (!empty($course->showgrades)) {
@ -916,7 +953,7 @@ function grade_report_user_profilereport($course, $user) {
/// 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);
$report = new grade_report_user($course->id, $gpr, $context, $user->id, $viewasuser);
// print the page
echo '<div class="grade-report-user">'; // css fix to share styles with real report page