Skip to content

Commit

Permalink
MDL-73863 course: Add tertiary navigation in completion pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihail Geshoski committed Mar 10, 2022
1 parent 01eb6d2 commit 36fe569
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 17 deletions.
28 changes: 28 additions & 0 deletions completion/classes/manager.php
Expand Up @@ -298,6 +298,34 @@ public static function get_available_completion_tabs($courseorid) {
return $tabs;
}

/**
* Returns an array with the available completion options (url => name) for the current course and user.
*
* @param int $courseid The course id.
* @return array
*/
public static function get_available_completion_options(int $courseid): array {
$coursecontext = context_course::instance($courseid);
$options = [];

if (has_capability('moodle/course:update', $coursecontext)) {
$completionlink = new moodle_url('/course/completion.php', ['id' => $courseid]);
$options[$completionlink->out(false)] = get_string('coursecompletion', 'completion');
}

if (has_capability('moodle/course:manageactivities', $coursecontext)) {
$defaultcompletionlink = new moodle_url('/course/defaultcompletion.php', ['id' => $courseid]);
$options[$defaultcompletionlink->out(false)] = get_string('defaultcompletion', 'completion');
}

if (self::can_edit_bulk_completion($courseid)) {
$bulkcompletionlink = new moodle_url('/course/bulkcompletion.php', ['id' => $courseid]);
$options[$bulkcompletionlink->out(false)] = get_string('bulkactivitycompletion', 'completion');
}

return $options;
}

/**
* Applies completion from the bulk edit form to all selected modules
*
Expand Down
2 changes: 2 additions & 0 deletions completion/upgrade.txt
Expand Up @@ -8,6 +8,8 @@ information provided here is intended especially for developers.
tested. Currently contains - viewed, usegrade, passgrade. Any plugin that are dependent on these criteria can now check this array instead of retesting it.
* The method \completion_criteria_completion::mark_complete() now has the optional $timecompleted parameter to specify when the
criteria was completed.
* New method get_available_completion_options() has been added in the core_completion\manager class. This method can be used
to obtain an array with the available completion options ([url => name]) for the current course and user.

=== 3.11 ===
* New Behat steps for activity completion in the behat_completion class:
Expand Down
6 changes: 4 additions & 2 deletions course/bulkcompletion.php
Expand Up @@ -69,9 +69,11 @@

// Print the form.
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('bulkactivitycompletion', 'completion'));

echo $renderer->navigation($course, 'bulkcompletion');
$actionbar = new \core_course\output\completion_action_bar($course->id, $PAGE->url);
echo $renderer->render_course_completion_action_bar($actionbar);

echo $OUTPUT->heading(get_string('bulkactivitycompletion', 'completion'));

$PAGE->requires->js_call_amd('core_form/changechecker', 'watchFormById', ['theform']);

Expand Down
11 changes: 11 additions & 0 deletions course/classes/output/bulk_activity_completion_renderer.php
Expand Up @@ -112,4 +112,15 @@ public function edit_default_completion($form, $modules) {
];
return parent::render_from_template('core_course/editdefaultcompletion', $data);
}

/**
* Renders the course completion action bar.
*
* @param \core_course\output\completion_action_bar $actionbar
* @return string The HTML output
*/
public function render_course_completion_action_bar(\core_course\output\completion_action_bar $actionbar): string {
$data = $actionbar->export_for_template($this->output);
return $this->output->render_from_template('core_course/completion_action_bar', $data);
}
}
68 changes: 68 additions & 0 deletions course/classes/output/completion_action_bar.php
@@ -0,0 +1,68 @@
<?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_course\output;

use core_completion\manager;
use moodle_url;
use renderable;
use renderer_base;
use templatable;
use url_select;

/**
* Renderable class for the action bar elements in the course completion pages.
*
* @package core_course
* @copyright 2022 Mihail Geshoski <mihail@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class completion_action_bar implements templatable, renderable {

/** @var int $courseid The course id. */
private $courseid;

/** @var moodle_url $currenturl The URL of the current page. */
private $currenturl;

/**
* The class constructor.
*
* @param int $courseid The course id.
* @param moodle_url $pageurl The URL of the current page.
*/
public function __construct(int $courseid, moodle_url $pageurl) {
$this->courseid = $courseid;
$this->currenturl = $pageurl;
}

/**
* Export the data for the mustache template.
*
* @param renderer_base $output renderer to be used to render the action bar elements.
* @return array The array which contains the data required to output the tertiary navigation selector for the course
* completion pages.
*/
public function export_for_template(renderer_base $output): array {
$urlselect = new url_select(manager::get_available_completion_options($this->courseid),
$this->currenturl->out(false), null, 'coursecompletionactionselect');
$urlselect->set_label(get_string('coursecompletionnavigation', 'completion'), ['class' => 'sr-only']);

return [
'urlselect' => $urlselect->export_for_template($output),
];
}
}
11 changes: 7 additions & 4 deletions course/completion.php
Expand Up @@ -57,8 +57,9 @@
if (!has_capability('moodle/course:update', $context)) {
// User is not allowed to modify course completion.
// Check if they can see default completion or edit bulk completion and redirect there.
if ($tabs = core_completion\manager::get_available_completion_tabs($course)) {
redirect($tabs[0]->link);
if ($options = core_completion\manager::get_available_completion_options($course->id)) {
// Redirect to the first available completion page.
redirect(array_key_first($options));
} else {
require_capability('moodle/course:update', $context);
}
Expand Down Expand Up @@ -161,9 +162,11 @@

// Print the form.
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('editcoursecompletionsettings', 'core_completion'));

echo $renderer->navigation($course, 'completion');
$actionbar = new \core_course\output\completion_action_bar($course->id, $PAGE->url);
echo $renderer->render_course_completion_action_bar($actionbar);

echo $OUTPUT->heading(get_string('editcoursecompletionsettings', 'core_completion'));

$form->display();

Expand Down
8 changes: 5 additions & 3 deletions course/defaultcompletion.php
Expand Up @@ -51,7 +51,7 @@
// Set up the page.
navigation_node::override_active_url(new moodle_url('/course/completion.php', array('id' => $course->id)));
$PAGE->set_course($course);
$PAGE->set_url('/course/bulkcompletion.php', array('id' => $course->id));
$PAGE->set_url('/course/defaultcompletion.php', array('id' => $course->id));
$PAGE->set_title($course->shortname);
$PAGE->set_heading($course->fullname);
$PAGE->set_pagelayout('admin');
Expand All @@ -64,9 +64,11 @@

// Print the form.
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('defaultcompletion', 'completion'));

echo $renderer->navigation($course, 'defaultcompletion');
$actionbar = new \core_course\output\completion_action_bar($course->id, $PAGE->url);
echo $renderer->render_course_completion_action_bar($actionbar);

echo $OUTPUT->heading(get_string('defaultcompletion', 'completion'));

$PAGE->requires->js_call_amd('core_form/changechecker', 'watchFormById', ['theform']);

Expand Down
3 changes: 1 addition & 2 deletions course/editbulkcompletion.php
Expand Up @@ -67,9 +67,8 @@
$renderer = $PAGE->get_renderer('core_course', 'bulk_activity_completion');

echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('bulkactivitycompletion', 'completion'));

echo $renderer->navigation($course, 'bulkcompletion');
echo $OUTPUT->heading(get_string('bulkactivitycompletion', 'completion'));

echo $renderer->edit_bulk_completion($form, $manager->get_activities(array_keys($cms)));

Expand Down
3 changes: 1 addition & 2 deletions course/editdefaultcompletion.php
Expand Up @@ -65,9 +65,8 @@
$renderer = $PAGE->get_renderer('core_course', 'bulk_activity_completion');

echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('defaultcompletion', 'completion'));

echo $renderer->navigation($course, 'defaultcompletion');
echo $OUTPUT->heading(get_string('defaultcompletion', 'completion'));

echo $renderer->edit_default_completion($form, $modules);

Expand Down
7 changes: 3 additions & 4 deletions course/lib.php
Expand Up @@ -4125,12 +4125,11 @@ function course_get_user_administration_options($course, $context) {
global $CFG;
$isfrontpage = $course->id == SITEID;
$completionenabled = $CFG->enablecompletion && $course->enablecompletion;
$hascompletiontabs = count(core_completion\manager::get_available_completion_tabs($course, $context)) > 0;
$hascompletionoptions = count(core_completion\manager::get_available_completion_options($course->id)) > 0;
$options = new stdClass;
$options->update = has_capability('moodle/course:update', $context);
$options->editcompletion = $CFG->enablecompletion &&
$course->enablecompletion &&
($options->update || $hascompletiontabs);
$options->editcompletion = $CFG->enablecompletion && $course->enablecompletion &&
($options->update || $hascompletionoptions);
$options->filters = has_capability('moodle/filter:manage', $context) &&
count(filter_get_available_in_context($context)) > 0;
$options->reports = has_capability('moodle/site:viewreports', $context);
Expand Down
53 changes: 53 additions & 0 deletions course/templates/completion_action_bar.mustache
@@ -0,0 +1,53 @@
{{!
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_course/completion_action_bar
Actions bar in the course completion pages.
Context variables required for this template:
* urlselect - The data object containing the required properties to render core/url_select.
Example context (json):
{
"urlselect": {
"id": "url_select_test",
"action": "https://example.com/post",
"formid": "url_select_form",
"sesskey": "sesskey",
"classes": "urlselect",
"label": "",
"helpicon": false,
"showbutton": null,
"options": [
{
"name": "Some name",
"value": "/mod/data/someurl.php",
"selected": false
}
],
"disabled": false,
"title": null
}
}
}}
<div class="container-fluid tertiary-navigation">
<div class="row">
{{#urlselect}}
<div class="navitem">
{{>core/url_select}}
</div>
{{/urlselect}}
</div>
</div>
1 change: 1 addition & 0 deletions lang/en/completion.php
Expand Up @@ -129,6 +129,7 @@
$string['coursecompletedmessage'] = '<p>Congratulations!</p><p>You have completed the course <a href="{$a->courselink}">{$a->coursename}</a>.</p>';
$string['coursecompletion'] = 'Course completion';
$string['coursecompletioncondition'] = 'Condition: {$a}';
$string['coursecompletionnavigation'] = 'Course completion tertiary navigation';
$string['coursegrade'] = 'Course grade';
$string['coursesavailable'] = 'Courses available';
$string['coursesavailableexplaination'] = 'Note: Course completion conditions must be set for a course to appear in the above list.';
Expand Down

0 comments on commit 36fe569

Please sign in to comment.