Skip to content

Commit

Permalink
Merge branch 'MDL-76445-master-2' of https://github.com/mihailges/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyatregubov committed Jun 13, 2023
2 parents d8e7c9d + c95c632 commit 621b090
Show file tree
Hide file tree
Showing 14 changed files with 483 additions and 76 deletions.
31 changes: 31 additions & 0 deletions grade/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,37 @@ function grade_get_plugin_info($courseid, $active_type, $active_plugin) {
return $plugin_info;
}

/**
* Load a valid list of gradable users in a course.
*
* @param int $courseid The course ID.
* @param int|null $groupid The group ID (optional).
* @return array $users A list of enrolled gradable users.
*/
function get_gradable_users(int $courseid, ?int $groupid = null): array {
global $CFG;

$context = context_course::instance($courseid);
// Create a graded_users_iterator because it will properly check the groups etc.
$defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
$onlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol) ||
!has_capability('moodle/course:viewsuspendedusers', $context);

$course = get_course($courseid);
$gui = new graded_users_iterator($course, null, $groupid);
$gui->require_active_enrolment($onlyactiveenrol);
$gui->init();

// Flatten the users.
$users = [];
while ($user = $gui->next_user()) {
$users[$user->user->id] = $user->user;
}
$gui->close();

return $users;
}

/**
* A simple class containing info about grade plugins.
* Can be subclassed for special rules
Expand Down
2 changes: 1 addition & 1 deletion grade/report/singleview/classes/local/screen/grade.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function original_definition(): array {
*/
public function init($selfitemisempty = false) {

$this->items = $this->load_users();
$this->items = get_gradable_users($this->courseid, $this->groupid);
$this->totalitemcount = count($this->items);

if ($selfitemisempty) {
Expand Down
27 changes: 7 additions & 20 deletions grade/report/singleview/classes/local/screen/screen.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,28 +405,15 @@ public function supports_next_prev(): bool {

/**
* Load a valid list of users for this gradebook as the screen "items".
* @return array $users A list of enroled users.
*
* @deprecated since Moodle 4.3
* @return array A list of enroled users.
*/
protected function load_users(): array {
global $CFG;

// Create a graded_users_iterator because it will properly check the groups etc.
$defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
$showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
$showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $this->context);

require_once($CFG->dirroot.'/grade/lib.php');
$gui = new \graded_users_iterator($this->course, null, $this->groupid);
$gui->require_active_enrolment($showonlyactiveenrol);
$gui->init();

// Flatten the users.
$users = [];
while ($user = $gui->next_user()) {
$users[$user->user->id] = $user->user;
}
$gui->close();
return $users;
debugging('The function ' . __FUNCTION__ . '() is deprecated. Please use get_gradable_users() instead.',
DEBUG_DEVELOPER);

return get_gradable_users($this->courseid, $this->groupid);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions grade/report/singleview/classes/local/screen/select.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
/**
* The gradebook simple view - initial view to select your search options
*
* @deprecated since Moodle 4.3
* @package gradereport_singleview
* @copyright 2014 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Expand All @@ -44,11 +45,15 @@ class select extends screen {
/**
* Initialise this screen
*
* @deprecated since Moodle 4.3
* @param bool $selfitemisempty Has an item been selected (will be false)
*/
public function init($selfitemisempty = false) {
global $DB;

debugging('The function ' . __FUNCTION__ . '() is deprecated as part of the deprecation process of the ' .
'\gradereport_singleview\local\screen\select class which is no longer used.', DEBUG_DEVELOPER);

$roleids = explode(',', get_config('moodle', 'gradebookroles'));

$this->items = [];
Expand All @@ -66,20 +71,28 @@ public function init($selfitemisempty = false) {
/**
* Get the type of items on this screen, not valid so return false.
*
* @deprecated since Moodle 4.3
* @return string|null
*/
public function item_type(): ?string {
debugging('The function ' . __FUNCTION__ . '() is deprecated as part of the deprecation process of the ' .
'\gradereport_singleview\local\screen\select class which is no longer used.', DEBUG_DEVELOPER);

return false;
}

/**
* Return the HTML for the page.
*
* @deprecated since Moodle 4.3
* @return string
*/
public function html(): string {
global $OUTPUT, $COURSE;

debugging('The function ' . __FUNCTION__ . '() is deprecated as part of the deprecation process of the ' .
'\gradereport_singleview\local\screen\select class which is no longer used.', DEBUG_DEVELOPER);

if ($this->itemid === null) {
$userlink = new \moodle_url('/grade/report/singleview/index.php', ['id' => $COURSE->id, 'item' => 'user_select']);
$gradelink = new \moodle_url('/grade/report/singleview/index.php', ['id' => $COURSE->id, 'item' => 'grade_select']);
Expand Down Expand Up @@ -137,17 +150,27 @@ public function html(): string {

/**
* Should we show the next prev selector?
*
* @deprecated since Moodle 4.3
* @return bool
*/
public function supports_next_prev(): bool {
debugging('The function ' . __FUNCTION__ . '() is deprecated as part of the deprecation process of the ' .
'\gradereport_singleview\local\screen\select class which is no longer used.', DEBUG_DEVELOPER);

return false;
}

/**
* Should we show the base singlereport group selector?
*
* @deprecated since Moodle 4.3
* @return bool
*/
public function display_group_selector(): bool {
debugging('The function ' . __FUNCTION__ . '() is deprecated as part of the deprecation process of the ' .
'\gradereport_singleview\local\screen\select class which is no longer used.', DEBUG_DEVELOPER);

if ($this->itemid === null) {
return false;
}
Expand All @@ -157,18 +180,26 @@ public function display_group_selector(): bool {
/**
* Get the heading for the screen.
*
* @deprecated since Moodle 4.3
* @return string
*/
public function heading(): string {
debugging('The function ' . __FUNCTION__ . '() is deprecated as part of the deprecation process of the ' .
'\gradereport_singleview\local\screen\select class which is no longer used.', DEBUG_DEVELOPER);

return ' ';
}

/**
* Does this screen support paging?
*
* @deprecated since Moodle 4.3
* @return bool
*/
public function supports_paging(): bool {
debugging('The function ' . __FUNCTION__ . '() is deprecated as part of the deprecation process of the ' .
'\gradereport_singleview\local\screen\select class which is no longer used.', DEBUG_DEVELOPER);

return false;
}
}
2 changes: 1 addition & 1 deletion grade/report/singleview/classes/local/screen/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function item_type(): string {
public function init($selfitemisempty = false) {

if (!$selfitemisempty) {
$validusers = $this->load_users();
$validusers = get_gradable_users($this->courseid, $this->groupid);
if (!isset($validusers[$this->itemid])) {
// If the passed user id is not valid, show the first user from the list instead.
$this->item = reset($validusers);
Expand Down
128 changes: 91 additions & 37 deletions grade/report/singleview/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,15 @@

// Making this work with profile reports.
$userid = optional_param('userid', null, PARAM_INT);

$defaulttype = $userid ? 'user' : 'select';

$itemid = optional_param('itemid', null, PARAM_INT);
$itemtype = optional_param('item', $defaulttype, PARAM_TEXT);
$itemtype = optional_param('item', null, PARAM_TEXT);
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 100, PARAM_INT);

$edit = optional_param('edit', -1, PARAM_BOOL); // Sticky editing mode.

if (empty($itemid) && ($itemtype !== 'user_select' && $itemtype !== 'grade_select')) {
$itemid = $userid;
$itemtype = $defaulttype;
}

$courseparams = ['id' => $courseid];
$pageparams = [
'id' => $courseid,
'group' => $groupid,
'userid' => $userid,
'itemid' => $itemid,
'item' => $itemtype,
'page' => $page,
'perpage' => $perpage,
];
$PAGE->set_url(new moodle_url('/grade/report/singleview/index.php', $pageparams));

$PAGE->set_pagelayout('report');
$PAGE->set_other_editing_capability('moodle/grade:edit');

Expand All @@ -68,10 +51,6 @@

require_login($course);

if (!in_array($itemtype, gradereport_singleview\report\singleview::valid_screens())) {
throw new \moodle_exception('notvalid', 'gradereport_singleview', '', $itemtype);
}

$context = context_course::instance($course->id);

// This is the normal requirements.
Expand All @@ -85,6 +64,90 @@
'courseid' => $courseid
]);

// Last selected report session tracking.
if (!isset($USER->grade_last_report)) {
$USER->grade_last_report = [];
}
$USER->grade_last_report[$course->id] = 'singleview';
// If the item type is not explicitly defined or not valid, try to use the last viewed one (obtain in from the session)
// or fallback to the user select (zero) state.
if (!$itemtype || !in_array($itemtype, \gradereport_singleview\report\singleview::valid_screens())) {
$itemtype = isset($SESSION->gradereport_singleview["itemtype-{$context->id}"]) ?
$SESSION->gradereport_singleview["itemtype-{$context->id}"] : 'user_select';
}

$currentgroup = $gpr->groupid;
// To make some other functions work better later.
if (!$currentgroup) {
$currentgroup = null;
}

$lastvieweduseritemid = $SESSION->gradereport_singleview["useritem-{$context->id}"] ?? null;
$lastviewedgradeitemid = $SESSION->gradereport_singleview["gradeitem-{$context->id}"] ?? null;

switch ($itemtype) {
case 'user_select':
// If there is a stored user item (last viewed) in a session variable, bypass the user select zero state
// and display this user item. Also, make sure that the stored last viewed user is part of the current
// list of gradable users in this course.
if ($lastvieweduseritemid && array_key_exists($lastvieweduseritemid, get_gradable_users($courseid, $currentgroup))) {
$itemtype = 'user';
$itemid = $lastvieweduseritemid;
} else {
$itemid = null;
}
break;
case 'user':
if (is_null($itemid)) {
$itemid = $userid ?? $lastvieweduseritemid;
}
// If the item id (user id) cannot be defined or the user id is not part of the list of gradable users,
// display the user select zero state.
if (is_null($itemid) || !array_key_exists($itemid, get_gradable_users($courseid, $currentgroup))) {
$itemtype = 'user_select';
}
break;
case 'grade_select':
// If there is a stored grade item (last viewed) in a session variable, bypass the grade item select zero state
// and display this grade item.
if ($lastviewedgradeitemid) {
$itemtype = 'grade';
$itemid = $lastviewedgradeitemid;
} else {
$itemid = null;
}
break;
case 'grade':
// If there is a stored grade item (last viewed) in a session variable, use it.
if (is_null($itemid) && $lastviewedgradeitemid) {
$itemid = $lastviewedgradeitemid;
}
$gtree = new grade_tree($courseid, false, false, null, !$CFG->enableoutcomes);
$gradeableitems = $gtree->get_items();
// The item id (grade item id) cannot be defined, display the grade select zero state.
if (is_null($itemid) || !array_key_exists($itemid, $gtree->get_items())) {
$itemtype = 'grade_select';
}
break;
}

$report = new gradereport_singleview\report\singleview($courseid, $gpr, $context, $itemtype, $itemid);

$pageparams = [
'id' => $courseid,
'userid' => $userid,
'itemid' => $itemid,
'item' => $itemtype,
'page' => $page,
'perpage' => $perpage,
];

if (!is_null($groupid)) {
$pageparams['group'] = $groupid;
}

$PAGE->set_url(new moodle_url('/grade/report/singleview/index.php', $pageparams));

// Build editing on/off button for themes that need it.
$button = '';
if ($PAGE->user_allowed_editing() && !$PAGE->theme->haseditswitch) {
Expand All @@ -97,14 +160,6 @@
$button = $OUTPUT->edit_button(new moodle_url($PAGE->url, $options), 'get');
}

// Last selected report session tracking.
if (!isset($USER->grade_last_report)) {
$USER->grade_last_report = [];
}
$USER->grade_last_report[$course->id] = 'singleview';

$report = new gradereport_singleview\report\singleview($courseid, $gpr, $context, $itemtype, $itemid);

$reportname = $report->screen->heading();

if ($itemtype == 'user' || $itemtype == 'user_select') {
Expand Down Expand Up @@ -149,6 +204,11 @@
grade_regrade_final_grades_if_required($course);

echo $report->output();
// Save the screen state in a session variable as last viewed state.
$SESSION->gradereport_singleview["itemtype-{$context->id}"] = $itemtype;
if ($itemid) {
$SESSION->gradereport_singleview["{$itemtype}item-{$context->id}"] = $itemid;
}

if (($itemtype !== 'select') && ($itemtype !== 'grade_select') &&($itemtype !== 'user_select')) {
$item = (isset($userid)) ? $userid : $itemid;
Expand All @@ -157,12 +217,6 @@
$showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
$showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context);

$currentgroup = $gpr->groupid;

// To make some other functions work better later.
if (!$currentgroup) {
$currentgroup = null;
}
$gui = new graded_users_iterator($course, null, $currentgroup);
$gui->require_active_enrolment($showonlyactiveenrol);
$gui->init();
Expand Down
1 change: 1 addition & 0 deletions grade/report/singleview/lang/en/deprecated.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
filtergrades,gradereport_singleview
viewsingleuserorgradeitem,gradereport_singleview
4 changes: 3 additions & 1 deletion grade/report/singleview/lang/en/gradereport_singleview.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
$string['viewby'] = 'View by';
$string['viewsingleuser'] = 'Select a user above to view all their grades';
$string['viewsinglegradeitem'] = 'Select a grade item above';
$string['viewsingleuserorgradeitem'] = 'View all the grades of a single user or grade item.';
$string['searchgrades'] = 'Search grade items';
$string['selectagrade'] = 'Select a grade item';
$string['selectgradeitemlink'] = 'Select a grade item';
Expand All @@ -88,3 +87,6 @@

// Deprecated since Moodle 4.1.
$string['filtergrades'] = 'Show grades for {$a}.';

// Deprecated since Moodle 4.3.
$string['viewsingleuserorgradeitem'] = 'View all the grades of a single user or grade item.';
Loading

0 comments on commit 621b090

Please sign in to comment.