diff --git a/grade/classes/output/import_action_bar.php b/grade/classes/output/import_action_bar.php new file mode 100644 index 00000000000..e856c9df56a --- /dev/null +++ b/grade/classes/output/import_action_bar.php @@ -0,0 +1,100 @@ +. + +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 + * @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; + } +} diff --git a/grade/classes/output/import_key_manager_action_bar.php b/grade/classes/output/import_key_manager_action_bar.php new file mode 100644 index 00000000000..f4ba20c54a1 --- /dev/null +++ b/grade/classes/output/import_key_manager_action_bar.php @@ -0,0 +1,63 @@ +. + +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 + * @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; + } +} diff --git a/grade/import/csv/index.php b/grade/import/csv/index.php index ecdcb3a5e0b..b72d3775b42 100644 --- a/grade/import/csv/index.php +++ b/grade/import/csv/index.php @@ -51,8 +51,9 @@ $separatemode = (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and !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'); diff --git a/grade/import/direct/index.php b/grade/import/direct/index.php index 0d795d8bbf0..e5ea3373a4e 100644 --- a/grade/import/direct/index.php +++ b/grade/import/direct/index.php @@ -47,8 +47,9 @@ $separatemode = (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and !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'); diff --git a/grade/import/index.php b/grade/import/index.php new file mode 100644 index 00000000000..fc8d4f9cbf3 --- /dev/null +++ b/grade/import/index.php @@ -0,0 +1,56 @@ +. + +/** + * Redirects the user to a default grades import plugin page. + * + * @package core_grades + * @copyright 2021 Mihail Geshoski + * @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(); diff --git a/grade/import/key.php b/grade/import/key.php index ed5d931a4d3..79f6b5e9e11 100644 --- a/grade/import/key.php +++ b/grade/import/key.php @@ -80,6 +80,7 @@ if ($id and $delete) { 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); @@ -133,7 +134,9 @@ $PAGE->navbar->add($strheading); /// 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(); diff --git a/grade/import/keymanager.php b/grade/import/keymanager.php index 6cdc379112d..decad005c78 100644 --- a/grade/import/keymanager.php +++ b/grade/import/keymanager.php @@ -45,7 +45,9 @@ if (!isset($plugins['keymanager'])) { 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'); @@ -78,10 +80,5 @@ $table->align = array('left', 'left', 'left', 'center'); $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(); diff --git a/grade/import/xml/index.php b/grade/import/xml/index.php index d587d1fff9f..3be84baf7c6 100644 --- a/grade/import/xml/index.php +++ b/grade/import/xml/index.php @@ -87,8 +87,9 @@ if ($data = $mform->get_data()) { } } -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(); diff --git a/grade/templates/import_action_bar.mustache b/grade/templates/import_action_bar.mustache new file mode 100644 index 00000000000..8ae6392a894 --- /dev/null +++ b/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 . +}} +{{! + @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 + } + } +}} +
+
+
+
+ {{#generalnavselector}} + {{>core/url_select}} + {{/generalnavselector}} +
+
+ {{#importselector}} + {{>core/url_select}} + {{/importselector}} +
+
+
+
diff --git a/grade/templates/import_key_manager_action_bar.mustache b/grade/templates/import_key_manager_action_bar.mustache new file mode 100644 index 00000000000..d35a310d71d --- /dev/null +++ b/grade/templates/import_key_manager_action_bar.mustache @@ -0,0 +1,99 @@ +{{! + 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 . +}} +{{! + @template core_grades/import_key_manager_action_bar + + Actions bar for the gradebook imports key manager page. + + Context variables required for this template: + * generalnavselector - The data object containing the required properties to render core/url_select. + * importselector - The data object containing the required properties to render the import options selector. + * adduserkeybutton - The data object containing the required properties to render the 'add user key' button. + + 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 + }, + "adduserkeybutton": { + "id": "single_button12345", + "method" : "get", + "classes": "singlebutton", + "formid": null, + "url" : "#", + "primary" : true, + "tooltip" : null, + "label" : "Add user key", + "attributes": [] + } + } +}} +
+
+
+
+ {{#generalnavselector}} + {{>core/url_select}} + {{/generalnavselector}} +
+
+ {{#importselector}} + {{>core/url_select}} + {{/importselector}} +
+ {{#adduserkeybutton}} +
+ {{>core/single_button}} +
+ {{/adduserkeybutton}} +
+
+
diff --git a/lang/en/userkey.php b/lang/en/userkey.php index 65330d6e45c..db20bae5134 100644 --- a/lang/en/userkey.php +++ b/lang/en/userkey.php @@ -22,6 +22,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string['adduserkey'] = 'Add user key'; $string['createnewkey'] = 'Create a new user key'; $string['createuserkey'] = 'Create user key'; $string['deletekeyconfirm'] = 'Do you really want to delete this user key?';