MDL-75423 gradereport_singleview: Update the actions bar

Creates a single action bar class to output the action elements in
the single view report. Also, enables the singleview report class
to set and return the appropriate item selector (in raw HTML)
based on the currently selected single view item type.
This commit is contained in:
Mihail Geshoski 2022-10-24 03:14:30 +11:00
parent e7e18d066a
commit b16067d8f0
6 changed files with 80 additions and 168 deletions

View File

@ -20,27 +20,51 @@ namespace gradereport_singleview\output;
use moodle_url;
use renderer_base;
use gradereport_singleview\report\singleview;
/**
* Class gradeitem_action_bar
* Renderable class for the action bar elements in the single view report page.
*
* @package gradereport_singleview
* @copyright 2022 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class gradeitem_action_bar extends \core_grades\output\action_bar {
class action_bar extends \core_grades\output\action_bar {
protected \gradereport_singleview\report\singleview $report;
/** @var singleview $report The single view report class. */
protected singleview $report;
public function __construct(\context $context, \gradereport_singleview\report\singleview $report) {
/** @var string $itemtype The single view item type. */
protected string $itemtype;
/**
* The class constructor.
*
* @param \context $context The context object.
* @param singleview $report The single view report class.
* @param string $itemtype The single view item type.
*/
public function __construct(\context $context, singleview $report, string $itemtype) {
parent::__construct($context);
$this->report = $report;
$this->itemtype = $itemtype;
}
/**
* Returns the template for the action bar.
*
* @return string
*/
public function get_template(): string {
return 'gradereport_singleview/gradeitem_action_bar';
return 'gradereport_singleview/action_bar';
}
/**
* Export the data for the mustache template.
*
* @param \renderer_base $output renderer to be used to render the action bar elements.
* @return array
*/
public function export_for_template(renderer_base $output) {
$courseid = $this->context->instanceid;
// Get the data used to output the general navigation selector.
@ -52,24 +76,19 @@ class gradeitem_action_bar extends \core_grades\output\action_bar {
);
$data = $generalnavselector->export_for_template($output);
$data['gradeselectactive'] = true;
$data['gradezerolink'] = new moodle_url(
'/grade/report/singleview/index.php',
['id' => $courseid, 'item' => 'grade_select']
);
$data['userzerolink'] = new moodle_url(
'/grade/report/singleview/index.php',
['id' => $courseid, 'item' => 'user_select']
);
// The data required to output the page toggle element.
$data['pagetoggler'] = [
'displaylabel' => true,
'userselectactive' => $this->itemtype === 'user',
'gradeselectactive' => $this->itemtype === 'grade',
'gradezerolink' => new moodle_url('/grade/report/singleview/index.php',
['id' => $courseid, 'item' => 'grade_select']),
'userzerolink' => new moodle_url('/grade/report/singleview/index.php',
['id' => $courseid, 'item' => 'user_select'])
];
$data['groupselector'] = $this->report->group_selector;
// Grade item selector.
$screen = new \gradereport_singleview\local\screen\user($this->report->courseid, null, $this->report->currentgroup);
$options = $screen->options();
$gradeitemselector = new \core\output\select_menu('itemid', $options, $this->report->screen->item->id);
$gradeitemselector->set_label(get_string('assessmentname', 'gradereport_singleview'));
$data['gradeitemselector'] = $gradeitemselector->export_for_template($output);
$data['itemselector'] = $this->report->itemselector;
$data['pbarurl'] = $this->report->pbarurl->out(false);

View File

@ -1,78 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace gradereport_singleview\output;
use moodle_url;
use renderer_base;
/**
* Class user_action_bar
*
* @package gradereport_singleview
* @copyright 2022 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_action_bar extends \core_grades\output\action_bar {
protected \gradereport_singleview\report\singleview $report;
public function __construct(\context $context, \gradereport_singleview\report\singleview $report) {
parent::__construct($context);
$this->report = $report;
}
public function get_template(): string {
return 'gradereport_singleview/user_action_bar';
}
public function export_for_template(renderer_base $output) {
$courseid = $this->context->instanceid;
// Get the data used to output the general navigation selector.
$generalnavselector = new \core_grades\output\general_action_bar(
$this->context,
new moodle_url('/grade/report/singleview/index.php', ['id' => $courseid]),
'report',
'singleview'
);
$data = $generalnavselector->export_for_template($output);
$data['userselectactive'] = true;
$data['gradezerolink'] = new moodle_url(
'/grade/report/singleview/index.php',
['id' => $courseid, 'item' => 'grade_select']
);
$data['userzerolink'] = new moodle_url(
'/grade/report/singleview/index.php',
['id' => $courseid, 'item' => 'user_select']
);
$data['groupselector'] = $this->report->group_selector;
// User selector.
$screen = new \gradereport_singleview\local\screen\grade($this->report->courseid, null, $this->report->currentgroup);
$options = $screen->options();
$userselector = new \core\output\select_menu('itemid', $options, $this->report->screen->item->id);
$userselector->set_label(get_string('user'));
$data['userselector'] = $userselector->export_for_template($output);
$data['pbarurl'] = $this->report->pbarurl->out(false);
return $data;
}
}

View File

@ -34,6 +34,9 @@ require_once($CFG->dirroot . '/grade/report/lib.php');
*/
class singleview extends grade_report {
/** @var string|null $itemselector The raw HTML of the item selector based on the selected single view item type. */
public ?string $itemselector = null;
/**
* Return the list of valid screens, used to validate the input.
*
@ -99,6 +102,8 @@ class singleview extends grade_report {
// The setup_group method is used to validate group mode and permissions and define the currentgroup value.
$this->setup_groups();
$this->setup_item_selector($itemtype, $itemid);
$screenclass = "\\gradereport_singleview\\local\\screen\\${itemtype}";
$this->screen = new $screenclass($courseid, $itemid, $this->currentgroup);
@ -135,4 +140,22 @@ class singleview extends grade_report {
$renderer = $PAGE->get_renderer('core_grades');
return $renderer->group_selector($course, $urlroot->out());
}
/**
* Function used to set the the appropriate item selector (raw HTML) based on the selected single view item type.
*
* @param string $itemtype The single view item type.
* @param int|null $itemid The item ID.
*/
protected function setup_item_selector(string $itemtype, ?int $itemid) {
global $PAGE;
$renderer = $PAGE->get_renderer('gradereport_singleview');
if ($itemtype === 'user' || $itemtype === 'user_select' ) {
$this->itemselector = $renderer->users_selector($this->course, $itemid, $this->currentgroup);
} else if ($itemtype === 'grade' || $itemtype === 'grade_select' ) {
$this->itemselector = $renderer->grade_items_selector($this->course, $itemid);
}
}
}

View File

@ -93,17 +93,18 @@ $report = new gradereport_singleview\report\singleview($courseid, $gpr, $context
$reportname = $report->screen->heading();
$actionbar = new \core_grades\output\general_action_bar($context,
new moodle_url('/grade/report/singleview/index.php', ['id' => $courseid]), 'report', 'singleview');
if ($itemtype == 'user' || $itemtype == 'user_select') {
$actionbar = new \gradereport_singleview\output\action_bar($context, $report, 'user');
} else if ($itemtype == 'grade' || $itemtype == 'grade_select') {
$actionbar = new \gradereport_singleview\output\action_bar($context, $report, 'grade');
} else {
$actionbar = new \core_grades\output\general_action_bar($context, new moodle_url('/grade/report/singleview/index.php',
['id' => $courseid]), 'report', 'singleview');
}
if ($itemtype == 'user') {
$actionbar = new \gradereport_singleview\output\user_action_bar($context, $report);
print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, false,
true, null, null, $report->screen->item, $actionbar);
} else if ($itemtype == 'grade') {
$actionbar = new \gradereport_singleview\output\gradeitem_action_bar($context, $report);
print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, false,
true, null, null, null, $actionbar);
} else {
print_grade_page_head($course->id, 'report', 'singleview', $reportname, false, false,
true, null, null, null, $actionbar);

View File

@ -15,7 +15,7 @@
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template gradereport_singleview/user_action_bar
@template gradereport_singleview/action_bar
}}
<div class="container-fluid tertiary-navigation">
<div class="row">
@ -29,20 +29,15 @@
{{{.}}}
</div>
{{/groupselector}}
{{#userselector}}
{{#itemselector}}
<div class="navitem">
{{>core/select_menu}}
{{{.}}}
</div>
{{#js}}
document.querySelector('.tertiary-navigation [name="itemid"]').addEventListener('change', function(e) {
var url = new URL("{{{pbarurl}}}");
url.searchParams.set('itemid', e.target.value);
window.location.href = url;
});
{{/js}}
{{/userselector}}
<div class="navitem ml-auto">
{{>gradereport_singleview/page_toggler}}
</div>
{{/itemselector}}
{{#pagetoggler}}
<div class="navitem ml-auto">
{{>gradereport_singleview/page_toggler}}
</div>
{{/pagetoggler}}
</div>
</div>

View File

@ -1,48 +0,0 @@
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template gradereport_singleview/gradeitem_action_bar
}}
<div class="container-fluid tertiary-navigation">
<div class="row">
{{#generalnavselector}}
<div class="navitem">
{{>core/tertiary_navigation_selector}}
</div>
{{/generalnavselector}}
{{#groupselector}}
<div class="navitem">
{{{.}}}
</div>
{{/groupselector}}
{{#gradeitemselector}}
<div class="navitem">
{{>core/select_menu}}
</div>
{{#js}}
document.querySelector('.tertiary-navigation [name="itemid"]').addEventListener('change', function(e) {
var url = new URL("{{{pbarurl}}}");
url.searchParams.set('itemid', e.target.value);
window.location.href = url;
});
{{/js}}
{{/gradeitemselector}}
<div class="navitem ml-auto">
{{>gradereport_singleview/page_toggler}}
</div>
</div>
</div>