Skip to content

Commit

Permalink
MDL-72873 core_grades: Add tertiary navigation in grade outcomes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihail Geshoski committed Dec 15, 2021
1 parent c326402 commit afbc8b2
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 58 deletions.
65 changes: 65 additions & 0 deletions grade/classes/output/course_outcomes_action_bar.php
@@ -0,0 +1,65 @@
<?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 course outcomes 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 course_outcomes_action_bar extends action_bar {

/**
* Returns the template for the action bar.
*
* @return string
*/
public function get_template(): string {
return 'core_grades/course_outcomes_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/edit/outcome/course.php', ['id' => $courseid]), 'outcome', 'course');
$data = $generalnavselector->export_for_template($output);

if (has_capability('moodle/grade:manageoutcomes', $this->context)) {
// Add a button to the action bar with a link to the 'manage outcomes' page.
$manageoutcomeslink = new moodle_url('/grade/edit/outcome/index.php', ['id' => $courseid]);
$manageoutcomesbutton = new \single_button($manageoutcomeslink, get_string('manageoutcomes', 'grades'),
'get', true);
$data['manageoutcomesbutton'] = $manageoutcomesbutton->export_for_template($output);
}

return $data;
}
}
95 changes: 95 additions & 0 deletions grade/classes/output/manage_outcomes_action_bar.php
@@ -0,0 +1,95 @@
<?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 manage outcomes 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 manage_outcomes_action_bar extends action_bar {

/** @var bool $hasoutcomes Whether there are existing outcomes. */
protected $hasoutcomes;

/**
* The class constructor.
*
* @param \context $context The context object.
* @param bool $hasoutcomes Whether there are existing outcomes.
*/
public function __construct(\context $context, bool $hasoutcomes) {
parent::__construct($context);
$this->hasoutcomes = $hasoutcomes;
}

/**
* Returns the template for the action bar.
*
* @return string
*/
public function get_template(): string {
return 'core_grades/manage_outcomes_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 {
$data = [];
$courseid = 0;
// Display the following buttons only if the user is in course gradebook.
if ($this->context->contextlevel === CONTEXT_COURSE) {
$courseid = $this->context->instanceid;
// Add a button to the action bar with a link to the 'course outcomes' page.
$backlink = new moodle_url('/grade/edit/outcome/course.php', ['id' => $courseid]);
$backbutton = new \single_button($backlink, get_string('back'), 'get');
$data['backbutton'] = $backbutton->export_for_template($output);

// Add a button to the action bar with a link to the 'import outcomes' page. The import outcomes
// functionality is currently only available in the course context.
$importoutcomeslink = new moodle_url('/grade/edit/outcome/import.php', ['courseid' => $courseid]);
$importoutcomesbutton = new \single_button($importoutcomeslink, get_string('importoutcomes', 'grades'),
'get');
$data['importoutcomesbutton'] = $importoutcomesbutton->export_for_template($output);
}

// Add a button to the action bar with a link to the 'add new outcome' page.
$addoutcomelink = new moodle_url('/grade/edit/outcome/edit.php', ['courseid' => $courseid]);
$addoutcomebutton = new \single_button($addoutcomelink, get_string('outcomecreate', 'grades'),
'get', true);
$data['addoutcomebutton'] = $addoutcomebutton->export_for_template($output);

if ($this->hasoutcomes) {
// Add a button to the action bar which enables export of all existing outcomes.
$exportoutcomeslink = new moodle_url('/grade/edit/outcome/export.php',
['id' => $courseid, 'sesskey' => sesskey()]);
$exportoutcomesbutton = new \single_button($exportoutcomeslink, get_string('exportalloutcomes', 'grades'),
'get');
$data['exportoutcomesbutton'] = $exportoutcomesbutton->export_for_template($output);
}

return $data;
}
}
6 changes: 4 additions & 2 deletions grade/edit/outcome/course.php
Expand Up @@ -132,8 +132,10 @@
redirect('course.php?id='.$courseid); // we must redirect to get fresh data
}

/// Print header
print_grade_page_head($COURSE->id, 'outcome', 'course');
$actionbar = new \core_grades\output\course_outcomes_action_bar($context);
// Print header.
print_grade_page_head($COURSE->id, 'outcome', 'course', false, false, false,
true, null, null, null, $actionbar);

require('course_form.html');

Expand Down
11 changes: 0 additions & 11 deletions grade/edit/outcome/course_form.html
Expand Up @@ -61,17 +61,6 @@
</td>
</tr>
</table>

<?php
if (has_capability('moodle/grade:manageoutcomes', $context)) {
?>
<p class="mdl-align">
<a href="<?php echo $CFG->wwwroot ?>/grade/edit/outcome/index.php?id=<?php echo $courseid; ?>"><?php echo get_string('editoutcomes','grades'); ?></a>
</p>
<?php
}
?>

<input name="id" type="hidden" value="<?php echo $courseid?>"/>
<input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
</div>
Expand Down
9 changes: 2 additions & 7 deletions grade/edit/outcome/edit.php
Expand Up @@ -41,7 +41,7 @@
$PAGE->set_pagelayout('admin');

$systemcontext = context_system::instance();
$heading = null;
$heading = get_string('addoutcome', 'grades');

// a bit complex access control :-O
if ($id) {
Expand Down Expand Up @@ -74,7 +74,6 @@
}

} else if ($courseid){
$heading = get_string('addoutcome', 'grades');
/// adding new outcome from course
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
require_login($course);
Expand Down Expand Up @@ -159,11 +158,7 @@
redirect($returnurl);
}

if ($courseid) {
print_grade_page_head($courseid, 'outcome', 'edit', $heading);
} else {
echo $OUTPUT->header();
}
print_grade_page_head($courseid ?: SITEID, 'outcome', 'edit', $heading, false, false, false);

if (!grade_scale::fetch_all_local($courseid) && !grade_scale::fetch_all_global()) {
echo $OUTPUT->confirm(get_string('noscales', 'grades'), $CFG->wwwroot.'/grade/edit/scale/edit.php?courseid='.$courseid, $returnurl);
Expand Down
48 changes: 28 additions & 20 deletions grade/edit/outcome/import.php
Expand Up @@ -59,14 +59,19 @@

$upload_form = new import_outcomes_form();

// display import form
if (!$upload_form->get_data()) {
print_grade_page_head($courseid, 'outcome', 'import', get_string('importoutcomes', 'grades'));
if ($upload_form->is_cancelled()) {
redirect(new moodle_url('/grade/edit/outcome/index.php', ['id' => $courseid]));
die;
}

print_grade_page_head($courseid, 'outcome', 'import', get_string('importoutcomes', 'grades'),
false, false, false);

if (!$upload_form->get_data()) { // Display the import form.
$upload_form->display();
echo $OUTPUT->footer();
die;
}
print_grade_page_head($courseid, 'outcome', 'import', get_string('importoutcomes', 'grades'));

$imported_file = $CFG->tempdir . '/outcomeimport/importedfile_'.time().'.csv';
make_temp_directory('outcomeimport');
Expand Down Expand Up @@ -99,6 +104,7 @@
$imported_headers = array(); // will later be initialized with the values found in the file

$fatal_error = false;
$errormessage = '';

// data should be separated by a ';'. *NOT* by a comma! TODO: version 2.0
// or whenever we can depend on PHP5, set the second parameter (8192) to 0 (unlimited line length) : the database can store over 128k per line.
Expand All @@ -124,11 +130,8 @@
}
}
if ($error) {
echo $OUTPUT->box_start('generalbox importoutcomenofile buttons');
echo get_string('importoutcomenofile', 'grades', $line);
echo $OUTPUT->single_button(new moodle_url('/grade/edit/outcome/import.php', array('courseid'=> $courseid)), get_string('back'), 'get');
echo $OUTPUT->box_end();
$fatal_error = true;
$errormessage = get_string('importoutcomenofile', 'grades', $line);
break;
}

Expand All @@ -143,23 +146,16 @@
// sanity check #2: every line must have the same number of columns as there are
// headers. If not, processing stops.
if ( count($csv_data) != count($file_headers) ) {
echo $OUTPUT->box_start('generalbox importoutcomenofile');
echo get_string('importoutcomenofile', 'grades', $line);
echo $OUTPUT->single_button(new moodle_url('/grade/edit/outcome/import.php', array('courseid'=> $courseid)), get_string('back'), 'get');
echo $OUTPUT->box_end();
$fatal_error = true;
//echo $OUTPUT->box(var_export($csv_data, true) ."<br />". var_export($header, true));
$errormessage = get_string('importoutcomenofile', 'grades', $line);
break;
}

// sanity check #3: all required fields must be present on the current line.
foreach ($headers as $header => $position) {
if ($csv_data[$imported_headers[$header]] == '') {
echo $OUTPUT->box_start('generalbox importoutcomenofile');
echo get_string('importoutcomenofile', 'grades', $line);
echo $OUTPUT->single_button(new moodle_url('/grade/edit/outcome/import.php', array('courseid'=> $courseid)), get_string('back'), 'get');
echo $OUTPUT->box_end();
$fatal_error = true;
$errormessage = get_string('importoutcomenofile', 'grades', $line);
break;
}
}
Expand All @@ -182,7 +178,8 @@

if ($outcome) {
// already exists, print a message and skip.
echo $OUTPUT->box(get_string('importskippedoutcome', 'grades', $csv_data[$imported_headers['outcome_shortname']]));
echo $OUTPUT->notification(get_string('importskippedoutcome', 'grades',
$csv_data[$imported_headers['outcome_shortname']]), 'info', false);
continue;
}

Expand All @@ -196,7 +193,8 @@
$scale_id = key($scale);
} else {
if (!has_capability('moodle/course:managescales', $context)) {
echo $OUTPUT->box(get_string('importskippednomanagescale', 'grades', $csv_data[$imported_headers['outcome_shortname']]));
echo $OUTPUT->notification(get_string('importskippedoutcome', 'grades',
$csv_data[$imported_headers['outcome_shortname']]), 'warning', false);
continue;
} else {
// scale doesn't exists : create it.
Expand Down Expand Up @@ -233,7 +231,17 @@
$outcome_success_strings = new StdClass();
$outcome_success_strings->name = $outcome_data['fullname'];
$outcome_success_strings->id = $outcome_id;
echo $OUTPUT->box(get_string('importoutcomesuccess', 'grades', $outcome_success_strings));
echo $OUTPUT->notification(get_string('importoutcomesuccess', 'grades', $outcome_success_strings),
'success', false);
}

if ($fatal_error) {
echo $OUTPUT->notification($errormessage, 'error', false);
echo $OUTPUT->single_button(new moodle_url('/grade/edit/outcome/import.php', ['courseid' => $courseid]),
get_string('back'), 'get');
} else {
echo $OUTPUT->single_button(new moodle_url('/grade/edit/outcome/index.php', ['id' => $courseid]),
get_string('continue'), 'get');
}
} else {
echo $OUTPUT->box(get_string('importoutcomenofile', 'grades', 0));
Expand Down
7 changes: 5 additions & 2 deletions grade/edit/outcome/import_outcomes_form.php
Expand Up @@ -51,8 +51,11 @@ public function definition() {
$mform->addRule('userfile', get_string('required'), 'required', null, 'server');
$mform->addHelpButton('userfile', 'importoutcomes', 'grades');

$mform->addElement('submit', 'save', get_string('uploadthisfile'));

$buttonarray = [
$mform->createElement('submit', 'save', get_string('uploadthisfile')),
$mform->createElement('cancel')
];
$mform->addGroup($buttonarray, 'buttonar', '', ' ', false);
}
}

Expand Down

0 comments on commit afbc8b2

Please sign in to comment.