MDL-59613 course: Add dropdown menu for activity list
[moodle.git] / course / classes / output / activity_navigation.php
CommitLineData
d8cb4615
MN
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * File containing the class activity navigation renderable.
19 *
20 * @package core_course
21 * @copyright 2017 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24namespace core_course\output;
25
26defined('MOODLE_INTERNAL') || die();
27
28use renderable;
29use templatable;
4698e169 30use url_select;
d8cb4615
MN
31
32/**
33 * The class activity navigation renderable.
34 *
35 * @package core_course
36 * @copyright 2017 Mark Nelson <markn@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class activity_navigation implements renderable, templatable {
40
41 /**
ed765cfa 42 * @var \action_link The action link object for the prev link.
d8cb4615 43 */
ed765cfa 44 public $prevlink = null;
d8cb4615
MN
45
46 /**
ed765cfa 47 * @var \action_link The action link object for the next link.
d8cb4615 48 */
ed765cfa 49 public $nextlink = null;
d8cb4615 50
4698e169
JP
51 /**
52 * @var url_select The url select object for the activity selector menu.
53 */
54 public $activitylist = null;
55
d8cb4615
MN
56 /**
57 * Constructor.
58 *
59 * @param \cm_info|null $prevmod The previous module to display, null if none.
60 * @param \cm_info|null $nextmod The previous module to display, null if none.
4698e169 61 * @param array $activitylist The list of activity URLs (as key) and names (as value) for the activity dropdown menu.
d8cb4615 62 */
4698e169 63 public function __construct($prevmod, $nextmod, $activitylist = array()) {
d8cb4615
MN
64 global $OUTPUT;
65
66 // Check if there is a previous module to display.
67 if ($prevmod) {
1678181a 68 $linkurl = new \moodle_url($prevmod->url, array('forceview' => 1));
d8cb4615
MN
69 $linkname = $prevmod->name;
70 if (!$prevmod->visible) {
71 $linkname .= ' ' . get_string('hiddenwithbrackets');
72 }
73
ed765cfa
JP
74 $attributes = [
75 'classes' => 'btn btn-link',
76 'id' => 'prev-activity-link',
77 'title' => $linkname,
78 ];
79 $this->prevlink = new \action_link($linkurl, $OUTPUT->larrow() . ' ' . $linkname, null, $attributes);
d8cb4615
MN
80 }
81
82 // Check if there is a next module to display.
83 if ($nextmod) {
1678181a 84 $linkurl = new \moodle_url($nextmod->url, array('forceview' => 1));
d8cb4615
MN
85 $linkname = $nextmod->name;
86 if (!$nextmod->visible) {
87 $linkname .= ' ' . get_string('hiddenwithbrackets');
88 }
89
ed765cfa
JP
90 $attributes = [
91 'classes' => 'btn btn-link',
92 'id' => 'next-activity-link',
93 'title' => $linkname,
94 ];
95 $this->nextlink = new \action_link($linkurl, $linkname . ' ' . $OUTPUT->rarrow(), null, $attributes);
d8cb4615 96 }
4698e169
JP
97
98 // Render the activity list dropdown menu if available.
99 if (!empty($activitylist)) {
100 $select = new url_select($activitylist, '', array('' => get_string('jumpto')));
101 $select->set_label(get_string('jumpto'), array('class' => 'sr-only'));
102 $select->attributes = array('id' => 'jump-to-activity');
103 $this->activitylist = $select;
104 }
d8cb4615
MN
105 }
106
107 /**
108 * Export this data so it can be used as the context for a mustache template.
109 *
110 * @param \renderer_base $output Renderer base.
111 * @return \stdClass
112 */
113 public function export_for_template(\renderer_base $output) {
114 $data = new \stdClass();
ed765cfa
JP
115 if ($this->prevlink) {
116 $data->prevlink = $this->prevlink->export_for_template($output);
117 }
118
119 if ($this->nextlink) {
120 $data->nextlink = $this->nextlink->export_for_template($output);
121 }
d8cb4615 122
4698e169
JP
123 if ($this->activitylist) {
124 $data->activitylist = $this->activitylist->export_for_template($output);
125 }
126
d8cb4615
MN
127 return $data;
128 }
129}