Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 1 addition & 166 deletions classes/ability.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,7 @@ function (coursework $coursework) {
$this->allow_show_allocation_to_the_allocated_assessor();
$this->allow_show_allocation_to_allocators();

// Grading table row rules

// Show grading table row
$this->allow_show_grading_table_row_if_allocation_enabled_and_user_has_any_allocation();
$this->allow_show_grading_table_row_if_allocation_enabled_and_all_initial_feedback_done_and_user_can_do_agreed_grades();
$this->allow_show_grading_table_row_if_allocation_not_enabled_and_user_is_assessor_of_any_stage();
$this->allow_show_grading_table_row_if_user_has_added_feedback_for_this_submission();
$this->allow_show_grading_table_row_if_user_can_administer_grades();
$this->allow_show_grading_table_row_if_user_can_grant_extension_and_no_allocation();
// CTP-4215 external examiner with capability mod/coursework:viewallgradesatalltimes needs to see all submissions.
$this->allow_show_grading_table_row_if_user_can_view_grades_at_all_times();
$this->allow_show_grading_table_row_if_user_can_submit_on_behalf_of();
$this->allow_show_grading_table_row_if_user_can_export_final_grades();
// Grading table row rules have been simplified / moved to grading_table_row_base:::user_visible().

// Deadline extension rules

Expand Down Expand Up @@ -1187,130 +1175,6 @@ function (allocation $allocation) {
);
}

protected function allow_show_grading_table_row_if_allocation_enabled_and_user_has_any_allocation() {
$this->allow(
'show',
'mod_coursework\grading_table_row_base',
function (grading_table_row_base $gradingtablerow) {
$allocatable = $gradingtablerow->get_allocatable();

if ($gradingtablerow->get_coursework()->allocation_enabled()) {
return allocation::allocatable_is_allocated_to_assessor(
$gradingtablerow->get_coursework()->id(),
$allocatable->id(),
$allocatable->type(),
$this->userid
);
}
return false;
}
);
}

protected function allow_show_grading_table_row_if_allocation_enabled_and_all_initial_feedback_done_and_user_can_do_agreed_grades() {
$this->allow(
'show',
'mod_coursework\grading_table_row_base',
function (grading_table_row_base $gradingtablerow) {
$canaddagreedgrade = has_capability(
'mod/coursework:addagreedgrade',
$gradingtablerow->get_coursework()
->get_context()
);

if (
$gradingtablerow->get_coursework()
->allocation_enabled() && $gradingtablerow->has_submission()
) {
$submissionhasallinitialassessorfeedbacks = $gradingtablerow->get_submission()
->get_state() >= submission::FULLY_GRADED;
if (
$canaddagreedgrade &&
$submissionhasallinitialassessorfeedbacks
) {
$submissioninsample = $gradingtablerow->get_submission()->sampled_feedback_exists();
return (!$gradingtablerow->get_coursework()->sampling_enabled() || $submissioninsample) ? true : false;
}
}
return false;
}
);
}

protected function allow_show_grading_table_row_if_allocation_not_enabled_and_user_is_assessor_of_any_stage() {
$this->allow(
'show',
'mod_coursework\grading_table_row_base',
function (grading_table_row_base $gradingtablerow) {
if (!$gradingtablerow->get_coursework()->allocation_enabled()) {
foreach ($gradingtablerow->get_coursework()->marking_stages() as $stage) {
if ($stage->user_is_assessor($this->userid)) {
return true;
}
}
}
return false;
}
);
}

protected function allow_show_grading_table_row_if_user_has_added_feedback_for_this_submission() {
$this->allow(
'show',
'mod_coursework\grading_table_row_base',
function (grading_table_row_base $gradingtablerow) {
// Check using cached object to avoid repeated DB calls on grading page.
return $gradingtablerow->has_submission()
&& feedback::get_cached_object(
$gradingtablerow->get_coursework()->id(),
[
'submissionid' => $gradingtablerow->get_submission()->id(),
'assessorid' => $this->userid,
],
);
}
);
}

protected function allow_show_grading_table_row_if_user_can_view_grades_at_all_times() {
$this->allow(
'show',
'mod_coursework\grading_table_row_base',
function (grading_table_row_base $gradingtablerow) {
return has_capability(
'mod/coursework:viewallgradesatalltimes',
$gradingtablerow->get_coursework()->get_context()
);
}
);
}

protected function allow_show_grading_table_row_if_user_can_submit_on_behalf_of() {
$this->allow(
'show',
'mod_coursework\grading_table_row_base',
function (grading_table_row_base $gradingtablerow) {
return has_capability(
'mod/coursework:submitonbehalfof',
$gradingtablerow->get_coursework()->get_context()
);
}
);
}

protected function allow_show_grading_table_row_if_user_can_export_final_grades() {
$this->allow(
'show',
'mod_coursework\grading_table_row_base',
function (grading_table_row_base $gradingtablerow) {
return has_capability(
'mod/coursework:canexportfinalgrades',
$gradingtablerow->get_coursework()->get_context()
);
}
);
}

protected function allow_show_feedback_if_user_can_view_grades_at_all_times_or_administer() {
$this->allow(
'show',
Expand Down Expand Up @@ -1373,35 +1237,6 @@ function (feedback $feedback) {
);
}

private function allow_show_grading_table_row_if_user_can_administer_grades() {
$this->allow(
'show',
'mod_coursework\grading_table_row_base',
function (grading_table_row_base $row) {
return has_capability(
'mod/coursework:administergrades',
$row->get_coursework()
->get_context()
);
}
);
}

private function allow_show_grading_table_row_if_user_can_grant_extension_and_no_allocation() {
$this->allow(
'show',
'mod_coursework\grading_table_row_base',
function (grading_table_row_base $row) {

return (!$row->get_coursework()->allocation_enabled() && has_capability(
'mod/coursework:grantextensions',
$row->get_coursework()
->get_context()
));
}
);
}

private function allow_edit_feedback_if_user_can_administer_grades() {
$this->allow(
'edit',
Expand Down
47 changes: 38 additions & 9 deletions classes/grading_report.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ class grading_report {
* @throws \dml_exception
*/
public static function get_table_rows_for_page(coursework $coursework): array {

global $USER;
$participants = $coursework->get_allocatables();
$extensions = $coursework->extensions_enabled()
? deadline_extension::get_all_for_coursework($coursework->id) : [];
Expand All @@ -56,10 +54,11 @@ public static function get_table_rows_for_page(coursework $coursework): array {

// Make tablerow objects so we can use the methods to check permissions and set things.
$rows = [];
$ability = new ability($USER->id, $coursework);

$participantsfound = 0;

$canseeallrows = self::can_see_all_rows($coursework);

foreach ($participants as $key => $participant) {
// To save multiple queries to DB for extensions and deadlines, add them here.
$extension = array_filter(
Expand Down Expand Up @@ -90,12 +89,7 @@ function ($ext) use ($participant) {
);

// Now, we skip the ones who should not be visible on this page.
$canshow = $ability->can('show', $row);
if (!$canshow && !isset($options['unallocated'])) {
unset($participants[$key]);
continue;
}
if ($canshow && isset($options['unallocated'])) {
if (!$canseeallrows && !$row->user_visible()) {
unset($participants[$key]);
continue;
}
Expand All @@ -108,6 +102,41 @@ function ($ext) use ($participant) {
return $rows;
}


/**
* Can the user see all rows in this report (to save checking for each row).
* @param coursework $coursework
* @return bool
*/
public static function can_see_all_rows(coursework $coursework): bool {
global $USER;
if (
has_any_capability(
[
'mod/coursework:viewallgradesatalltimes',
'mod/coursework:submitonbehalfof',
'mod/coursework:canexportfinalgrades',
'mod/coursework:grantextensions',
'mod/coursework:administergrades',
],
$coursework->get_context()
)
) {
return true;
}

if (!$coursework->allocation_enabled()) {
// If we are able to grade any stage for this row, it's visible.
foreach ($coursework->marking_stages() as $stage) {
if ($stage->user_is_assessor($USER->id)) {
return true;
}
}
}

return false;
}

/**
* Get the submission IDs that the user can see in this grading report.
* @param coursework $coursework
Expand Down
54 changes: 54 additions & 0 deletions classes/grading_table_row_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use core\exception\moodle_exception;
use html_writer;
use mod_coursework\allocation\allocatable;
use mod_coursework\models\allocation;
use mod_coursework\models\coursework;
use mod_coursework\models\deadline_extension;
use mod_coursework\models\feedback;
Expand Down Expand Up @@ -418,4 +419,57 @@ public function get_personaldeadline(): ?personaldeadline {

return $this->personaldeadline;
}

/**
* Is this row (specifically) visible to the current user?
* Note that user may be able to see all rows which, for efficiency in a loop, this does not check.
* @see grading_report::can_see_all_rows()
* @return bool
*/
public function user_visible(): bool {
global $USER;

$coursework = $this->get_coursework();
$submission = $this->get_submission();

if ($coursework->allocation_enabled()) {
$allocatable = $this->get_allocatable();
if (
allocation::allocatable_is_allocated_to_assessor(
$coursework->id(),
$allocatable->id(),
$allocatable->type(),
$USER->id
)
) {
return true;
}

// Visible if row is ready for agreed feedback and user is allowed to add agreed feedback.
if (
$submission
&& has_capability('mod/coursework:addagreedgrade', $coursework->get_context())
&& (!$coursework->sampling_enabled() || $submission->sampled_feedback_exists())
&& $submission->get_state() >= submission::FULLY_GRADED
) {
return true;
}
}

if (
$submission
&& feedback::get_cached_object(
$coursework->id(),
[
'submissionid' => $submission->id(),
'assessorid' => $USER,
],
)
) {
// If we've already graded any stage for this submission, row is visible.
return true;
}

return false;
}
}
4 changes: 1 addition & 3 deletions classes/renderers/grading_report_renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ public static function get_table_row_data(coursework $coursework, grading_table_
* @return ?object
*/
public static function export_one_row_data(coursework $coursework, int $allocatableid, string $allocatabletype): ?object {
global $USER;
$classname = "\\mod_coursework\\models\\$allocatabletype";
$allocatable = $classname::get_cached_object_from_id($allocatableid);
if (!$allocatable) {
Expand All @@ -222,7 +221,6 @@ public static function export_one_row_data(coursework $coursework, int $allocata
$submissionfilesarray = $submission
? submission::get_all_submission_files_data($coursework, [$submission->id()], true)
: [];
$ability = new ability($USER->id, $coursework);

// New grading_table_row_base.
$row = new grading_table_row_base(
Expand All @@ -232,7 +230,7 @@ public static function export_one_row_data(coursework $coursework, int $allocata
personaldeadline::get_for_allocatable($coursework->id, $allocatableid, $allocatabletype),
!empty($submissionfilesarray) ? array_pop($submissionfilesarray) : []
);
if (!$ability->can('show', $row)) {
if (!grading_report::can_see_all_rows($coursework) && !$row->user_visible()) {
return null;
}
$data = self::get_table_row_data($coursework, $row);
Expand Down