Commit d8cb4615 authored by Mark Nelson's avatar Mark Nelson
Browse files

MDL-59612 core: added previous and next links to activities

Part of MDL-59313.
parent d509f80c
<?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/>.
/**
* File containing the class activity navigation renderable.
*
* @package core_course
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_course\output;
defined('MOODLE_INTERNAL') || die();
use renderable;
use templatable;
/**
* The class activity navigation renderable.
*
* @package core_course
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activity_navigation implements renderable, templatable {
/**
* @var string The html for the prev link
*/
public $prevlink = '';
/**
* @var string The html for the next link
*/
public $nextlink = '';
/**
* Constructor.
*
* @param \cm_info|null $prevmod The previous module to display, null if none.
* @param \cm_info|null $nextmod The previous module to display, null if none.
*/
public function __construct($prevmod, $nextmod) {
global $OUTPUT;
// Check if there is a previous module to display.
if ($prevmod) {
$linkname = $prevmod->name;
if (!$prevmod->visible) {
$linkname .= ' ' . get_string('hiddenwithbrackets');
}
$link = new \action_link($prevmod->url, $OUTPUT->larrow() . ' ' . $linkname);
$this->prevlink = $OUTPUT->render($link);
}
// Check if there is a next module to display.
if ($nextmod) {
$linkname = $nextmod->name;
if (!$nextmod->visible) {
$linkname .= ' ' . get_string('hiddenwithbrackets');
}
$link = new \action_link($nextmod->url, $linkname . ' ' . $OUTPUT->rarrow());
$this->nextlink = $OUTPUT->render($link);
}
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param \renderer_base $output Renderer base.
* @return \stdClass
*/
public function export_for_template(\renderer_base $output) {
$data = new \stdClass();
$data->prevlink = $this->prevlink;
$data->nextlink = $this->nextlink;
return $data;
}
}
......@@ -2100,6 +2100,19 @@ class core_course_renderer extends plugin_renderer_base {
set_attributes(array('class' => 'frontpage-category-names'));
return $this->coursecat_tree($chelper, coursecat::get(0));
}
/**
* Renders the activity navigation.
*
* Defer to template.
*
* @param \core_course\output\activity_navigation $page
* @return string html for the page
*/
public function render_activity_navigation(\core_course\output\activity_navigation $page) {
$data = $page->export_for_template($this->output);
return $this->output->render_from_template('core_course/activity_navigation', $data);
}
}
/**
......
{{!
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/activity_navigation
Displays the activity navigation
Context variables required for this template:
* prevlink - The link to the previous module the user can see
* nextlink - The link to the next module the user can see
Example context (json):
{
"prevlink": "<a href='#'>The origins of Moodle</a>",
"nextlink": "<a href='#'>Moodlers - are they contagious?</a>"
}
}}
<div>
{{< core/columns-1to1to1}}
{{$column1}}
<div class="pull-left">
{{{prevlink}}}
</div>
{{/column1}}
{{$column3}}
<div class="pull-right">
{{{nextlink}}}
</div>
{{/column3}}
{{/ core/columns-1to1to1}}
</div>
......@@ -931,6 +931,7 @@ $string['hiddensections'] = 'Hidden sections';
$string['hiddensections_help'] = 'This setting determines whether hidden sections are displayed to students in collapsed form (perhaps for a course in weekly format to indicate holidays) or are completely hidden.';
$string['hiddensectionscollapsed'] = 'Hidden sections are shown in collapsed form';
$string['hiddensectionsinvisible'] = 'Hidden sections are completely invisible';
$string['hiddenwithbrackets'] = '(hidden)';
$string['hide'] = 'Hide';
$string['hideadvancedsettings'] = 'Hide advanced settings';
$string['hidechartdata'] = 'Hide chart data';
......
......@@ -813,6 +813,68 @@ class core_renderer extends renderer_base {
return '<div role="main">'.$this->unique_main_content_token.'</div>';
}
/**
* Returns standard navigation between activities in a course.
*
* @return string the navigation HTML.
*/
public function activity_navigation() {
// First we should check if we want to add navigation.
$context = $this->page->context;
if ($this->page->pagelayout !== 'incourse' || $context->contextlevel != CONTEXT_MODULE) {
return '';
}
// If the activity is in stealth mode, show no links.
if ($this->page->cm->is_stealth()) {
return '';
}
// Get a list of all the activities in the course.
$course = $this->page->cm->get_course();
$modules = get_fast_modinfo($course->id)->get_cms();
// Put the modules into an array in order by the position they are shown in the course.
$mods = [];
foreach ($modules as $module) {
// Only add activities the user can access, aren't in stealth mode and have a url (eg. mod_label does not).
if (!$module->uservisible || $module->is_stealth() || empty($module->url)) {
continue;
}
$mods[$module->id] = $module;
}
$nummods = count($mods);
// If there is only one mod then do nothing.
if ($nummods == 1) {
return '';
}
// Get an array of just the course module ids used to get the cmid value based on their position in the course.
$modids = array_keys($mods);
// Get the position in the array of the course module we are viewing.
$position = array_search($this->page->cm->id, $modids);
$prevmod = null;
$nextmod = null;
// Check if we have a previous mod to show.
if ($position > 0) {
$prevmod = $mods[$modids[$position - 1]];
}
// Check if we have a next mod to show.
if ($position < ($nummods - 1)) {
$nextmod = $mods[$modids[$position + 1]];
}
$activitynav = new \core_course\output\activity_navigation($prevmod, $nextmod);
$renderer = $this->page->get_renderer('core', 'course');
return $renderer->render($activitynav);
}
/**
* The standard tags (typically script tags that are not needed earlier) that
* should be output after everything else. Designed to be called in theme layout.php files.
......
......@@ -82,6 +82,7 @@
{{/hasregionmainsettingsmenu}}
{{{ output.course_content_header }}}
{{{ output.main_content }}}
{{{ output.activity_navigation }}}
{{{ output.course_content_footer }}}
</div>
</section>
......
......@@ -69,6 +69,7 @@ echo $OUTPUT->doctype() ?>
<?php
echo $OUTPUT->course_content_header();
echo $OUTPUT->main_content();
echo $OUTPUT->activity_navigation();
echo $OUTPUT->course_content_footer();
?>
</section>
......
......@@ -76,6 +76,7 @@ echo $OUTPUT->doctype() ?>
<?php
echo $OUTPUT->course_content_header();
echo $OUTPUT->main_content();
echo $OUTPUT->activity_navigation();
echo $OUTPUT->course_content_footer();
?>
</section>
......
......@@ -9,6 +9,8 @@ information provided here is intended especially for theme designer.
the first one was renamed to loginform.mustache - see MDL-58970.
* The Boost flat navigation nodes now have several data-attributes which let plugin developers
access properties from the underlying navigation nodes in the browser - see MDL-59425.
* Navigation between activities via a previous and next link was added to Boost, Clean and Bootstrapbase. This
is made possible by a new function core_renderer->activity_navigation().
=== 3.3 ===
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment