Skip to content

Commit

Permalink
MDL-72873 core_grades: Add tertiary navigation in grade import plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihail Geshoski committed Dec 15, 2021
1 parent 08e90ee commit bd13229
Show file tree
Hide file tree
Showing 11 changed files with 414 additions and 10 deletions.
100 changes: 100 additions & 0 deletions grade/classes/output/import_action_bar.php
@@ -0,0 +1,100 @@
<?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/>.

namespace core_grades\output;

use moodle_url;

/**
* Renderable class for the action bar elements in the gradebook import pages.
*
* @package core_grades
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class import_action_bar extends action_bar {

/** @var moodle_url $importactiveurl The URL that should be set as active in the imports URL selector element. */
protected $importactiveurl;

/** @var string $activeplugin The plugin of the current import grades page (xml, csv, ...). */
protected $activeplugin;

/**
* The class constructor.
*
* @param \context $context The context object.
* @param moodle_url $importactiveurl The URL that should be set as active in the imports URL selector element.
* @param string $activeplugin The plugin of the current import grades page (xml, csv, ...).
*/
public function __construct(\context $context, moodle_url $importactiveurl, string $activeplugin) {
parent::__construct($context);
$this->importactiveurl = $importactiveurl;
$this->activeplugin = $activeplugin;
}

/**
* Returns the template for the action bar.
*
* @return string
*/
public function get_template(): string {
return 'core_grades/import_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): array {
if ($this->context->contextlevel !== CONTEXT_COURSE) {
return [];
}
$courseid = $this->context->instanceid;
// Get the data used to output the general navigation selector.
$generalnavselector = new general_action_bar($this->context,
new moodle_url('/grade/import/index.php', ['id' => $courseid]), 'import', $this->activeplugin);
$data = $generalnavselector->export_for_template($output);

// Get all grades import plugins. If there isn't any available import plugins there is no need to create and
// display the imports navigation selector menu. Therefore, return only the current data.
if (!$imports = \grade_helper::get_plugins_import($courseid)) {
return $data;
}

// If imports key management is enabled, always display this item at the end of the list.
if (array_key_exists('keymanager', $imports)) {
$keymanager = $imports['keymanager'];
unset($imports['keymanager']);
$imports['keymanager'] = $keymanager;
}

$importsmenu = [];
// Generate the data for the imports navigation selector menu.
foreach ($imports as $import) {
$importsmenu[$import->link->out()] = $import->string;
}

// This navigation selector menu will contain the links to all available grade export plugin pages.
$importsurlselect = new \url_select($importsmenu, $this->importactiveurl->out(false), null,
'gradesimportactionselect');
$data['importselector'] = $importsurlselect->export_for_template($output);

return $data;
}
}
63 changes: 63 additions & 0 deletions grade/classes/output/import_key_manager_action_bar.php
@@ -0,0 +1,63 @@
<?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/>.

namespace core_grades\output;

use moodle_url;

/**
* Renderable class for the action bar elements in the gradebook import key manager page.
*
* @package core_grades
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class import_key_manager_action_bar extends action_bar {

/**
* Returns the template for the action bar.
*
* @return string
*/
public function get_template(): string {
return 'core_grades/import_key_manager_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): array {
if ($this->context->contextlevel !== CONTEXT_COURSE) {
return [];
}
$courseid = $this->context->instanceid;
// Get the data used to output the general navigation selector and imports navigation selector.
$importnavselectors = new import_action_bar($this->context,
new moodle_url('/grade/import/keymanager.php', ['id' => $courseid]), 'keymanager');
$data = $importnavselectors->export_for_template($output);

// Add a button to the action bar with a link to the 'add user key' page.
$adduserkeylink = new moodle_url('/grade/import/key.php', ['courseid' => $courseid]);
$adduserkeybutton = new \single_button($adduserkeylink, get_string('adduserkey', 'userkey'),
'get', true);
$data['adduserkeybutton'] = $adduserkeybutton->export_for_template($output);

return $data;
}
}
3 changes: 2 additions & 1 deletion grade/import/csv/index.php
Expand Up @@ -51,8 +51,9 @@
!has_capability('moodle/site:accessallgroups', $context));
$currentgroup = groups_get_course_group($course);

$actionbar = new \core_grades\output\import_action_bar($context, $PAGE->url, 'csv');
print_grade_page_head($course->id, 'import', 'csv', get_string('importcsv', 'grades'), false, false, true,
'importcsv', 'grades');
'importcsv', 'grades', null, $actionbar);

$renderer = $PAGE->get_renderer('gradeimport_csv');

Expand Down
3 changes: 2 additions & 1 deletion grade/import/direct/index.php
Expand Up @@ -47,8 +47,9 @@
!has_capability('moodle/site:accessallgroups', $context));
$currentgroup = groups_get_course_group($course);

$actionbar = new \core_grades\output\import_action_bar($context, $PAGE->url, 'direct');
print_grade_page_head($course->id, 'import', 'direct', get_string('pluginname', 'gradeimport_direct'), false, false, true,
'userdata', 'gradeimport_direct');
'userdata', 'gradeimport_direct', null, $actionbar);

$renderer = $PAGE->get_renderer('gradeimport_csv');

Expand Down
56 changes: 56 additions & 0 deletions grade/import/index.php
@@ -0,0 +1,56 @@
<?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/>.

/**
* Redirects the user to a default grades import plugin page.
*
* @package core_grades
* @copyright 2021 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once(__DIR__ . '/../../config.php');

// Course ID.
$courseid = required_param('id', PARAM_INT);

$PAGE->set_url(new moodle_url('/grade/import/index.php', ['id' => $courseid]));

// Basic access checks.
if (!$course = $DB->get_record('course', ['id' => $courseid])) {
throw new moodle_exception('invalidcourseid', 'error');
}
require_login($course);
$context = context_course::instance($courseid);
require_capability('moodle/grade:import', $context);

$importplugins = core_component::get_plugin_list('gradeimport');
if (!empty($importplugins)) {
$importplugin = array_key_first($importplugins);
$url = new moodle_url("/grade/import/{$importplugin}/index.php", ['id' => $courseid]);
redirect($url);
}

// Otherwise, output the page with a notification stating that there are no available grade import options.
$PAGE->set_title(get_string('import', 'grades'));
$PAGE->set_pagelayout('incourse');
$PAGE->set_heading($course->fullname);
$PAGE->set_pagetype('course-view-' . $course->format);

echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('import', 'grades'));
echo html_writer::div($OUTPUT->notification(get_string('nogradeimport', 'grades'), 'error'), 'mt-3');
echo $OUTPUT->footer();
3 changes: 3 additions & 0 deletions grade/import/key.php
Expand Up @@ -80,6 +80,7 @@
if (!$confirm) {
$PAGE->set_title(get_string('deleteselectedkey'));
$PAGE->set_heading($course->fullname);
$PAGE->set_secondary_active_tab('grades');
echo $OUTPUT->header();
$optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1);
$optionsno = array('id'=>$courseid);
Expand Down Expand Up @@ -133,7 +134,9 @@
/// Print header
$PAGE->set_title($strkeys);
$PAGE->set_heading($course->fullname);
$PAGE->set_secondary_active_tab('grades');
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);

$editform->display();
echo $OUTPUT->footer();
9 changes: 3 additions & 6 deletions grade/import/keymanager.php
Expand Up @@ -45,7 +45,9 @@
print_error('nopermissions');
}

print_grade_page_head($course->id, 'import', 'keymanager', get_string('keymanager', 'grades'));
$actionbar = new \core_grades\output\import_key_manager_action_bar($context);
print_grade_page_head($course->id, 'import', 'keymanager', get_string('keymanager', 'grades'),
false, false, true, 'importcsv', 'grades', null, $actionbar);

$stredit = get_string('edit');
$strdelete = get_string('delete');
Expand Down Expand Up @@ -78,10 +80,5 @@
$table->width = '90%';
$table->data = $data;
echo html_writer::table($table);

echo $OUTPUT->container_start('buttons mdl-align');
echo $OUTPUT->single_button(new moodle_url('key.php', array('courseid'=>$course->id)), get_string('newuserkey', 'userkey'));
echo $OUTPUT->container_end();

echo $OUTPUT->footer();

5 changes: 3 additions & 2 deletions grade/import/xml/index.php
Expand Up @@ -87,8 +87,9 @@
}
}

print_grade_page_head($COURSE->id, 'import', 'xml',
get_string('importxml', 'grades'), false, false, true, 'importxml', 'gradeimport_xml');
$actionbar = new \core_grades\output\import_action_bar($context, $PAGE->url, 'xml');
print_grade_page_head($COURSE->id, 'import', 'xml', get_string('importxml', 'grades'),
false, false, true, 'importxml', 'gradeimport_xml', null, $actionbar);

$mform->display();

Expand Down
82 changes: 82 additions & 0 deletions grade/templates/import_action_bar.mustache
@@ -0,0 +1,82 @@
{{!
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 core_grades/import_action_bar
Actions bar for the gradebook import pages.
Context variables required for this template:
* generalnavselector - The data object containing the required properties to render the general navigation selector.
* importselector - The data object containing the required properties to render the import options selector.
Example context (json):
{
"generalnavselector": {
"id": "url_select12345",
"action": "https://example.com/get",
"classes": "urlselect",
"formid": "gradesactionselect",
"sesskey": "sesskey",
"label": "",
"helpicon": false,
"showbutton": null,
"options": [{
"name": "View", "isgroup": true, "options":
[
{"name": "Grader report", "isgroup": false, "value": "/grade/report/grader/index.php"}
]},
{"name": "Setup", "isgroup": true, "options":
[
{"name": "Gradebook setup", "isgroup": false, "value": "/grade/edit/tree/index.php"}
]}],
"disabled": false,
"title": null
},
"importselector": {
"id": "url_select56789",
"action": "https://example.com/get",
"formid": "gradesimportactionselect",
"sesskey": "sesskey",
"classes": "urlselect",
"label": "",
"helpicon": false,
"showbutton": null,
"options": [
{
"name": "CSV file",
"value": "/grade/import/csv/index.php",
"selected": true
}
],
"disabled": false,
"title": null
}
}
}}
<div class="container-fluid mb-4">
<div class="row">
<div class="d-flex">
<div>
{{#generalnavselector}}
{{>core/url_select}}
{{/generalnavselector}}
</div>
<div class="ml-2">
{{#importselector}}
{{>core/url_select}}
{{/importselector}}
</div>
</div>
</div>
</div>

0 comments on commit bd13229

Please sign in to comment.