Skip to content

Commit

Permalink
MDL-77033 cores_grades: Refactor cell menus to be reusable.
Browse files Browse the repository at this point in the history
We need similar menus on gradebook setup page. So moving from
grader plugin to core
  • Loading branch information
ilyatregubov committed Mar 30, 2023
1 parent bf3c078 commit 9ba802c
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 317 deletions.
222 changes: 187 additions & 35 deletions grade/lib.php

Large diffs are not rendered by default.

323 changes: 90 additions & 233 deletions grade/report/grader/lib.php

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions grade/report/grader/tests/behat/behat_gradereport_grader.php
Expand Up @@ -58,6 +58,8 @@ protected function get_user_id($name) {
/**
* Gets the grade item id from its name.
*
* @deprecated since 4.2
* @todo MDL-77107 This will be deleted in Moodle 4.6.
* @throws Exception
* @param string $itemname
* @return int
Expand All @@ -66,6 +68,9 @@ protected function get_grade_item_id($itemname) {

global $DB;

debugging('behat_gradereport_grader::get_grade_item_id() is deprecated, please use' .
' behat_grades::get_grade_item_id() instead.', DEBUG_DEVELOPER);

if ($id = $DB->get_field('grade_items', 'id', array('itemname' => $itemname))) {
return $id;
}
Expand Down
52 changes: 30 additions & 22 deletions grade/report/lib.php
Expand Up @@ -92,12 +92,6 @@ abstract class grade_report {
*/
public $page;

/**
* Array of cached language strings (using get_string() all the time takes a long time!).
* @var array $lang_strings
*/
public $lang_strings = array();

// GROUP VARIABLES (including SQL)

/**
Expand Down Expand Up @@ -307,37 +301,51 @@ abstract public function process_data($data);
abstract public function process_action($target, $action);

/**
* Returns an array of links to appropriate report pages for the current element
* Add additional links specific to plugin
* @param context_course $context Course context
* @param int $courseid Course ID
* @param array $element An array representing an element in the grade_tree
* @param grade_plugin_return $gpr A grade_plugin_return object
* @param string $mode Mode - gradeitem or user
* @return array Link to appropriate report
*/
public function get_report_links(context_course $context, int $courseid, array $element,
grade_plugin_return $gpr, string $mode): array {

$reports = [];
foreach (core_component::get_plugin_list('gradereport') as $plugin => $plugindir) {
$params = [$context, $courseid, $element, $gpr, $mode];
$component = 'gradereport_' . $plugin;
if ($reportlink = component_callback($component, 'get_report_link', $params)) {
$reports[] = $reportlink;
* @param string $mode Mode (user or grade item)
* @param stdClass $templatecontext Template context
* @param bool $otherplugins If we need to insert links to other plugins
* @return ?stdClass Updated template context
*/
public static function get_additional_context(context_course $context, int $courseid, array $element,
grade_plugin_return $gpr, string $mode, stdClass $templatecontext, bool $otherplugins = false): ?stdClass {

if (!$otherplugins) {
$component = 'gradereport_' . $gpr->plugin;
$params = [$context, $courseid, $element, $gpr, $mode, $templatecontext];
return component_callback($component, 'get_report_link', $params);
} else {
// Loop through all installed grade reports.
foreach (core_component::get_plugin_list('gradereport') as $plugin => $plugindir) {
$params = [$context, $courseid, $element, $gpr, $mode, $templatecontext];
$component = 'gradereport_' . $plugin;
$templatecontextupdated = component_callback($component, 'get_report_link', $params);
if ($templatecontextupdated) {
$templatecontext = $templatecontextupdated;
}
}

return $templatecontext;
}
return $reports;
}

/**
* First checks the cached language strings, then returns match if found, or uses get_string()
* to get it from the DB, caches it then returns it.
*
* @deprecated since 4.2
* @todo MDL-77307 This will be deleted in Moodle 4.6.
* @param string $strcode
* @param string $section Optional language section
* @return string
*/
public function get_lang_string($strcode, $section=null) {
debugging('grade_report::get_lang_string() is deprecated, please use' .
' grade_helper::get_lang_string() instead.', DEBUG_DEVELOPER);

if (empty($this->lang_strings[$strcode])) {
$this->lang_strings[$strcode] = get_string($strcode, $section);
}
Expand Down Expand Up @@ -588,7 +596,7 @@ protected function get_sort_arrow(string $direction = 'down', ?moodle_url $sortl
global $OUTPUT;
$pix = ['up' => 't/sort_desc', 'down' => 't/sort_asc'];
$matrix = ['up' => 'desc', 'down' => 'asc'];
$strsort = $this->get_lang_string($matrix[$direction], 'moodle');
$strsort = grade_helper::get_lang_string($matrix[$direction], 'moodle');
$arrow = $OUTPUT->pix_icon($pix[$direction], '', '', ['class' => 'sorticon']);
return html_writer::link($sortlink, $arrow, ['title' => $strsort, 'aria-label' => $strsort]);
}
Expand Down
10 changes: 5 additions & 5 deletions grade/report/overview/lib.php
Expand Up @@ -151,13 +151,13 @@ public function setup_table() {
// setting up table headers
if ($this->showrank['any']) {
$tablecolumns = array('coursename', 'grade', 'rank');
$tableheaders = array($this->get_lang_string('coursename', 'grades'),
$this->get_lang_string('gradenoun'),
$this->get_lang_string('rank', 'grades'));
$tableheaders = array(grade_helper::get_lang_string('coursename', 'grades'),
grade_helper::get_lang_string('gradenoun'),
grade_helper::get_lang_string('rank', 'grades'));
} else {
$tablecolumns = array('coursename', 'grade');
$tableheaders = array($this->get_lang_string('coursename', 'grades'),
$this->get_lang_string('gradenoun'));
$tableheaders = array(grade_helper::get_lang_string('coursename', 'grades'),
grade_helper::get_lang_string('gradenoun'));
}
$this->table = new flexible_table('grade-report-overview-'.$this->user->id);

Expand Down
14 changes: 10 additions & 4 deletions grade/report/singleview/lib.php
Expand Up @@ -30,12 +30,16 @@
* @param array $element An array representing an element in the grade_tree
* @param grade_plugin_return $gpr A grade_plugin_return object
* @param string $mode Mode - gradeitem or user
* @return string|null
* @param ?stdClass $templatecontext Template context
* @return stdClass|null
*/
function gradereport_singleview_get_report_link(context_course $context, int $courseid,
array $element, grade_plugin_return $gpr, string $mode): ?string {
array $element, grade_plugin_return $gpr, string $mode, ?stdClass $templatecontext): ?stdClass {

$reportstring = grade_helper::get_lang_string('singleviewreport_' . $mode, 'gradereport_singleview');
if (!isset($templatecontext)) {
$templatecontext = new stdClass();
}

if ($mode == 'gradeitem') {
// View all grades items.
Expand All @@ -50,8 +54,9 @@ function gradereport_singleview_get_report_link(context_course $context, int $co
'itemid' => $element['object']->id
]);
$gpr->add_url_params($url);
return html_writer::link($url, $reportstring,
$templatecontext->reporturl0 = html_writer::link($url, $reportstring,
['class' => 'dropdown-item', 'aria-label' => $reportstring, 'role' => 'menuitem']);
return $templatecontext;
}
}
} else if ($mode == 'user') {
Expand All @@ -66,8 +71,9 @@ function gradereport_singleview_get_report_link(context_course $context, int $co
$url = new moodle_url('/grade/report/singleview/index.php',
['id' => $courseid, 'itemid' => $element['userid'], 'item' => 'user']);
$gpr->add_url_params($url);
return html_writer::link($url, $reportstring,
$templatecontext->reporturl0 = html_writer::link($url, $reportstring,
['class' => 'dropdown-item', 'aria-label' => $reportstring, 'role' => 'menuitem']);
return $templatecontext;
}
}
return null;
Expand Down
5 changes: 4 additions & 1 deletion grade/report/upgrade.txt
Expand Up @@ -11,12 +11,15 @@ information provided here is intended especially for developers.
* 'Show show/hide icons' setting has been removed from grader report (link is moved to grade action menu)
* 'Enable AJAX' interface has been deprecated for grader report
* 'Quick feedback' interface has been deprecated for grader report
* A new method grade_report::get_report_links() is created to obtain links to other grade plugins report pages.
* A new method grade_report::get_additional_context() is created to obtain links to other grade plugins report pages.
It loops through all installed grade report plugins and checks if callback function gradereport_*_get_report_link
is implemented in for given grade report plugin in the corresponding lib.php
* The setting $CFG->grade_report_studentsperpage has been completely removed because it's not required anymore. This setting
was used to set the default number of students displayed per page in the grader report. Now the default is set to
20.
* The get_grade_item_id() function in behat_gradereport_grader.php has been deprecated. Please use
behat_grades::get_grade_item_id() instead.
* The grade_report::get_lang_string() has been deprecated. Please use grade_helper::get_lang_string() instead.

=== 3.6 ===
* External function gradereport_user_external::get_grade_items now return the following information (only for course managers).
Expand Down
24 changes: 13 additions & 11 deletions grade/report/user/classes/report/user.php
Expand Up @@ -27,7 +27,9 @@

defined('MOODLE_INTERNAL') || die;

global $CFG;
require_once($CFG->dirroot.'/grade/report/lib.php');
require_once($CFG->dirroot.'/grade/lib.php');

/**
* Class providing an API for the user report building and displaying.
Expand Down Expand Up @@ -373,51 +375,51 @@ public function setup_table() {
// Setting up table headers.

$this->tablecolumns = ['itemname'];
$this->tableheaders = [$this->get_lang_string('gradeitem', 'grades')];
$this->tableheaders = [\grade_helper::get_lang_string('gradeitem', 'grades')];

if ($this->showweight) {
$this->tablecolumns[] = 'weight';
$this->tableheaders[] = $this->get_lang_string('weightuc', 'grades');
$this->tableheaders[] = \grade_helper::get_lang_string('weightuc', 'grades');
}

if ($this->showgrade) {
$this->tablecolumns[] = 'grade';
$this->tableheaders[] = $this->get_lang_string('grade', 'grades');
$this->tableheaders[] = \grade_helper::get_lang_string('grade', 'grades');
}

if ($this->showrange) {
$this->tablecolumns[] = 'range';
$this->tableheaders[] = $this->get_lang_string('range', 'grades');
$this->tableheaders[] = \grade_helper::get_lang_string('range', 'grades');
}

if ($this->showpercentage) {
$this->tablecolumns[] = 'percentage';
$this->tableheaders[] = $this->get_lang_string('percentage', 'grades');
$this->tableheaders[] = \grade_helper::get_lang_string('percentage', 'grades');
}

if ($this->showlettergrade) {
$this->tablecolumns[] = 'lettergrade';
$this->tableheaders[] = $this->get_lang_string('lettergrade', 'grades');
$this->tableheaders[] = \grade_helper::get_lang_string('lettergrade', 'grades');
}

if ($this->showrank) {
$this->tablecolumns[] = 'rank';
$this->tableheaders[] = $this->get_lang_string('rank', 'grades');
$this->tableheaders[] = \grade_helper::get_lang_string('rank', 'grades');
}

if ($this->showaverage) {
$this->tablecolumns[] = 'average';
$this->tableheaders[] = $this->get_lang_string('average', 'grades');
$this->tableheaders[] = \grade_helper::get_lang_string('average', 'grades');
}

if ($this->showfeedback) {
$this->tablecolumns[] = 'feedback';
$this->tableheaders[] = $this->get_lang_string('feedback', 'grades');
$this->tableheaders[] = \grade_helper::get_lang_string('feedback', 'grades');
}

if ($this->showcontributiontocoursetotal) {
$this->tablecolumns[] = 'contributiontocoursetotal';
$this->tableheaders[] = $this->get_lang_string('contributiontocoursetotal', 'grades');
$this->tableheaders[] = \grade_helper::get_lang_string('contributiontocoursetotal', 'grades');
}
}

Expand Down Expand Up @@ -1008,7 +1010,7 @@ public function print_table(bool $return = false) {

$table = new \html_table();
$table->attributes = [
'summary' => s($this->get_lang_string('tablesummary', 'gradereport_user')),
'summary' => s(\grade_helper::get_lang_string('tablesummary', 'gradereport_user')),
'class' => 'generaltable boxaligncenter user-grade',
];

Expand Down
12 changes: 9 additions & 3 deletions grade/report/user/lib.php
Expand Up @@ -247,15 +247,20 @@ function gradereport_user_myprofile_navigation(tree $tree, stdClass $user, bool
* @param array $element An array representing an element in the grade_tree
* @param grade_plugin_return $gpr A grade_plugin_return object
* @param string $mode Mode - gradeitem or user
* @return string|null
* @param ?stdClass $templatecontext Template context
* @return stdClass|null
*/
function gradereport_user_get_report_link(context_course $context, int $courseid, array $element,
grade_plugin_return $gpr, string $mode): ?string {
grade_plugin_return $gpr, string $mode, ?stdClass $templatecontext): ?stdClass {
global $CFG;

if ($mode == 'user') {
$reportstring = grade_helper::get_lang_string('userreport_' . $mode, 'gradereport_user');

if (!isset($templatecontext)) {
$templatecontext = new stdClass();
}

// FIXME: MDL-52678 This get_capability_info is hacky and we should have an API for inserting grade row links instead.
$canseeuserreport = false;
if (get_capability_info('gradereport/' . $CFG->grade_profilereport . ':view')) {
Expand All @@ -266,8 +271,9 @@ function gradereport_user_get_report_link(context_course $context, int $courseid
$url = new moodle_url('/grade/report/' . $CFG->grade_profilereport . '/index.php',
['userid' => $element['userid'], 'id' => $courseid]);
$gpr->add_url_params($url);
return html_writer::link($url, $reportstring,
$templatecontext->reporturl1 = html_writer::link($url, $reportstring,
['class' => 'dropdown-item', 'aria-label' => $reportstring, 'role' => 'menuitem']);
return $templatecontext;
}
}
return null;
Expand Down
Expand Up @@ -15,7 +15,7 @@
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template gradereport_grader/cellmenu
@template core_grades/cellmenu
This template renders action menu for a given cell.
Example context (json):
Expand All @@ -31,8 +31,8 @@
"ascendinglastnameurl": "<a class='dropdown-item' aria-label='Ascending' role='menuitem' href='index.php?id=13&amp;sortitemid=lastname&amp;sort=asc&amp;gpr_type=report&amp;gpr_plugin=grader&amp;gpr_courseid=13'>Ascending</a>",
"descendinglastnameurl": "<a class='dropdown-item' aria-label='Descending' role='menuitem' href='index.php?id=13&amp;sortitemid=lastname&amp;sort=desc&amp;gpr_type=report&amp;gpr_plugin=grader&amp;gpr_courseid=13'>Descending</a>",
"divider1": "true",
"divider2": "true"
"datatype": item,
"divider2": "true",
"datatype": "item",
"dataid": "123"
}
}}
Expand Down

0 comments on commit 9ba802c

Please sign in to comment.