MDL-27183 quiz reports: don't show data to users not in any group

if the quiz is set to separate groups, and the user does not have access all groups.
This commit is contained in:
Tim Hunt 2011-12-07 19:02:12 +00:00
parent f89a83b87b
commit e4977ba5c6
6 changed files with 53 additions and 11 deletions

View File

@ -72,6 +72,7 @@ abstract class quiz_attempt_report extends quiz_default_report {
/** /**
* Get information about which students to show in the report. * Get information about which students to show in the report.
* @param object $cm the coures module. * @param object $cm the coures module.
* @param object $course the course settings.
* @return an array with four elements: * @return an array with four elements:
* 0 => integer the current group id (0 for none). * 0 => integer the current group id (0 for none).
* 1 => array ids of all the students in this course. * 1 => array ids of all the students in this course.
@ -79,8 +80,12 @@ abstract class quiz_attempt_report extends quiz_default_report {
* 3 => array ids of all the students to show in the report. Will be the * 3 => array ids of all the students to show in the report. Will be the
* same as either element 1 or 2. * same as either element 1 or 2.
*/ */
protected function load_relevant_students($cm) { protected function load_relevant_students($cm, $course = null) {
$currentgroup = groups_get_activity_group($cm, true); $currentgroup = $this->get_current_group($cm, $course, $this->context);
if ($currentgroup == self::NO_GROUPS_ALLOWED) {
return array($currentgroup, array(), array(), array());
}
if (!$students = get_users_by_capability($this->context, if (!$students = get_users_by_capability($this->context,
array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'), array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'),

View File

@ -43,6 +43,8 @@ defined('MOODLE_INTERNAL') || die();
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
abstract class quiz_default_report { abstract class quiz_default_report {
const NO_GROUPS_ALLOWED = -2;
/** /**
* Override this function to displays the report. * Override this function to displays the report.
* @param $cm the course-module for this quiz. * @param $cm the course-module for this quiz.
@ -51,6 +53,14 @@ abstract class quiz_default_report {
*/ */
public abstract function display($cm, $course, $quiz); public abstract function display($cm, $course, $quiz);
/**
* Initialise some parts of $PAGE and start output.
*
* @param object $cm the course_module information.
* @param object $coures the course settings.
* @param object $quiz the quiz settings.
* @param string $reportmode the report name.
*/
public function print_header_and_tabs($cm, $course, $quiz, $reportmode = 'overview') { public function print_header_and_tabs($cm, $course, $quiz, $reportmode = 'overview') {
global $PAGE, $OUTPUT; global $PAGE, $OUTPUT;
@ -59,4 +69,24 @@ abstract class quiz_default_report {
$PAGE->set_heading($course->fullname); $PAGE->set_heading($course->fullname);
echo $OUTPUT->header(); echo $OUTPUT->header();
} }
/**
* Get the current group for the user user looking at the report.
*
* @param object $cm the course_module information.
* @param object $coures the course settings.
* @param context $context the quiz context.
* @return int the current group id, if applicable. 0 for all users,
* NO_GROUPS_ALLOWED if the user cannot see any group.
*/
public function get_current_group($cm, $course, $context) {
$groupmode = groups_get_activity_groupmode($cm, $course);
$currentgroup = groups_get_activity_group($cm, true);
if ($groupmode == SEPARATEGROUPS && !$currentgroup && !has_capability('moodle/site:accessallgroups', $context)) {
$currentgroup = self::NO_GROUPS_ALLOWED;
}
return $currentgroup;
}
} }

View File

@ -117,10 +117,14 @@ class quiz_grading_report extends quiz_default_report {
} }
// Get the group, and the list of significant users. // Get the group, and the list of significant users.
$this->currentgroup = groups_get_activity_group($this->cm, true); $this->currentgroup = $this->get_current_group($cm, $course, $this->context);
$this->users = get_users_by_capability($this->context, if ($this->currentgroup == self::NO_GROUPS_ALLOWED) {
array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'), '', '', '', '', $this->users = array();
$this->currentgroup, '', false); } else {
$this->users = get_users_by_capability($this->context,
array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'), '', '', '', '',
$this->currentgroup, '', false);
}
// Start output. // Start output.
$this->print_header_and_tabs($cm, $course, $quiz, 'grading'); $this->print_header_and_tabs($cm, $course, $quiz, 'grading');

View File

@ -47,7 +47,7 @@ class quiz_overview_report extends quiz_attempt_report {
$download = optional_param('download', '', PARAM_ALPHA); $download = optional_param('download', '', PARAM_ALPHA);
list($currentgroup, $students, $groupstudents, $allowed) = list($currentgroup, $students, $groupstudents, $allowed) =
$this->load_relevant_students($cm); $this->load_relevant_students($cm, $course);
$pageoptions = array(); $pageoptions = array();
$pageoptions['id'] = $cm->id; $pageoptions['id'] = $cm->id;

View File

@ -55,7 +55,7 @@ class quiz_responses_report extends quiz_attempt_report {
$download = optional_param('download', '', PARAM_ALPHA); $download = optional_param('download', '', PARAM_ALPHA);
list($currentgroup, $students, $groupstudents, $allowed) = list($currentgroup, $students, $groupstudents, $allowed) =
$this->load_relevant_students($cm); $this->load_relevant_students($cm, $course);
$pageoptions = array(); $pageoptions = array();
$pageoptions['id'] = $cm->id; $pageoptions['id'] = $cm->id;

View File

@ -85,13 +85,16 @@ class quiz_statistics_report extends quiz_default_report {
} }
// Find out current groups mode // Find out current groups mode
$groupmode = groups_get_activity_groupmode($cm); $currentgroup = $this->get_current_group($cm, $course, $context);
$currentgroup = groups_get_activity_group($cm, true);
$nostudentsingroup = false; // True if a group is selected and there is no one in it. $nostudentsingroup = false; // True if a group is selected and there is no one in it.
if (empty($currentgroup)) { if (empty($currentgroup)) {
$currentgroup = 0; $currentgroup = 0;
$groupstudents = array(); $groupstudents = array();
} else if ($currentgroup == self::NO_GROUPS_ALLOWED) {
$groupstudents = array();
$nostudentsingroup = true;
} else { } else {
// All users who can attempt quizzes and who are in the currently selected group // All users who can attempt quizzes and who are in the currently selected group
$groupstudents = get_users_by_capability($context, $groupstudents = get_users_by_capability($context,
@ -152,7 +155,7 @@ class quiz_statistics_report extends quiz_default_report {
if (!$this->table->is_downloading()) { if (!$this->table->is_downloading()) {
$this->print_header_and_tabs($cm, $course, $quiz, 'statistics'); $this->print_header_and_tabs($cm, $course, $quiz, 'statistics');
if ($groupmode) { if (groups_get_activity_groupmode($cm)) {
groups_print_activity_menu($cm, $reporturl->out()); groups_print_activity_menu($cm, $reporturl->out());
if ($currentgroup && !$groupstudents) { if ($currentgroup && !$groupstudents) {
$OUTPUT->notification(get_string('nostudentsingroup', 'quiz_statistics')); $OUTPUT->notification(get_string('nostudentsingroup', 'quiz_statistics'));