|
| 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 | +namespace local_learningtools\output; |
| 18 | + |
| 19 | +use moodle_url; |
| 20 | +use core\output\select_menu; |
| 21 | +use core\output\comboboxsearch; |
| 22 | + |
| 23 | +/** |
| 24 | + * Renderable class for the general action bar in the gradebook pages. |
| 25 | + * |
| 26 | + * This class is responsible for rendering the general navigation select menu in the gradebook pages. |
| 27 | + * |
| 28 | + * @package local_learningtools |
| 29 | + * @copyright 2021 bdecent gmbh <https://bdecent.de> |
| 30 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 31 | + */ |
| 32 | +class general_action_bar { |
| 33 | + |
| 34 | + /** @var moodle_url $activeurl The URL that should be set as active in the URL selector element. */ |
| 35 | + protected $activeurl; |
| 36 | + |
| 37 | + /** |
| 38 | + * The type of the current gradebook page (report, settings, import, export, scales, outcomes, letters). |
| 39 | + * |
| 40 | + * @var string $activetype |
| 41 | + */ |
| 42 | + protected $activetype; |
| 43 | + |
| 44 | + /** @var string $activeplugin The plugin of the current gradebook page (grader, fullview, ...). */ |
| 45 | + protected $activeplugin; |
| 46 | + |
| 47 | + /** |
| 48 | + * Summary of context |
| 49 | + * @var \context |
| 50 | + */ |
| 51 | + protected $context; |
| 52 | + |
| 53 | + /** |
| 54 | + * Course id. |
| 55 | + * @var int |
| 56 | + */ |
| 57 | + protected $courseid; |
| 58 | + |
| 59 | + /** |
| 60 | + * Section id. |
| 61 | + * @var int |
| 62 | + */ |
| 63 | + protected $sectionid; |
| 64 | + |
| 65 | + /** |
| 66 | + * Activity |
| 67 | + * @var int |
| 68 | + */ |
| 69 | + protected $activity; |
| 70 | + |
| 71 | + /** |
| 72 | + * The class constructor. |
| 73 | + * |
| 74 | + * @param \context $context The context object. |
| 75 | + * @param moodle_url $activeurl The URL that should be set as active in the URL selector element. |
| 76 | + * @param string $activetype The type of the current gradebook page (report, settings, import, export, scales, |
| 77 | + * outcomes, letters). |
| 78 | + * @param string $activeplugin The plugin of the current gradebook page (grader, fullview, ...). |
| 79 | + * @param int $courseid Course ID. |
| 80 | + * @param int $sectionid Section ID. |
| 81 | + * @param int $activity Activity ID. |
| 82 | + */ |
| 83 | + public function __construct(\context $context, moodle_url $activeurl, string $activetype, string $activeplugin, int $courseid, |
| 84 | + int $sectionid, int $activity) { |
| 85 | + $this->activeurl = $activeurl; |
| 86 | + $this->activetype = $activetype; |
| 87 | + $this->activeplugin = $activeplugin; |
| 88 | + $this->context = $context; |
| 89 | + $this->courseid = $courseid; |
| 90 | + $this->sectionid = $sectionid; |
| 91 | + $this->activity = $activity; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Export the data for the mustache template. |
| 96 | + * |
| 97 | + * @param \renderer_base $output renderer to be used to render the action bar elements. |
| 98 | + * @return array |
| 99 | + */ |
| 100 | + public function export_for_template(\renderer_base $output): array { |
| 101 | + global $USER, $DB; |
| 102 | + $selectmenu = $this->get_action_selector(); |
| 103 | + |
| 104 | + if (is_null($selectmenu)) { |
| 105 | + return []; |
| 106 | + } |
| 107 | + |
| 108 | + $collapsemenudirection = right_to_left() ? 'dropdown-menu-left' : 'dropdown-menu-right'; |
| 109 | + |
| 110 | + $collapse = new comboboxsearch( |
| 111 | + true, |
| 112 | + get_string('collapsedcolumns', 'gradereport_grader', 0), |
| 113 | + null, |
| 114 | + 'collapse-columns', |
| 115 | + 'collapsecolumn', |
| 116 | + 'collapsecolumndropdown p-3 flex-column ' . $collapsemenudirection, |
| 117 | + null, |
| 118 | + true, |
| 119 | + get_string('aria:dropdowncolumns', 'gradereport_grader'), |
| 120 | + 'collapsedcolumns' |
| 121 | + ); |
| 122 | + |
| 123 | + $course = get_course($this->courseid); |
| 124 | + |
| 125 | + $sections = $this->get_sections(); |
| 126 | + $activities = $this->get_activities(); |
| 127 | + |
| 128 | + $viewpageurl = new moodle_url('/local/learningtools/ltool/note/view.php', ['id' => $this->courseid]); |
| 129 | + |
| 130 | + $collapsedcolumns = [ |
| 131 | + 'classes' => 'd-none', |
| 132 | + 'content' => $collapse->export_for_template($output), |
| 133 | + 'viewpageurl' => $viewpageurl->out(false), |
| 134 | + 'sections' => !empty($sections) ? $sections : [], |
| 135 | + 'activities' => !empty($activities) ? $activities : [], |
| 136 | + ]; |
| 137 | + |
| 138 | + return [ |
| 139 | + 'generalnavselector' => $selectmenu->export_for_template($output), |
| 140 | + 'collapsedcolumns' => $collapsedcolumns, |
| 141 | + ]; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Returns the template for the action bar. |
| 146 | + * |
| 147 | + * @return string |
| 148 | + */ |
| 149 | + public function get_template(): string { |
| 150 | + return 'local_learningtools/tertiary_navigation'; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Returns the URL selector object. |
| 155 | + * |
| 156 | + * @return \select_menu|null The URL select object. |
| 157 | + */ |
| 158 | + private function get_action_selector(): ?select_menu { |
| 159 | + if ($this->context->contextlevel !== CONTEXT_COURSE) { |
| 160 | + return null; |
| 161 | + } |
| 162 | + |
| 163 | + $courseid = $this->context->instanceid; |
| 164 | + $menus = []; |
| 165 | + |
| 166 | + // Get all available learning tool subplugins. |
| 167 | + $subplugins = local_learningtools_get_subplugins(); |
| 168 | + |
| 169 | + // Add each subplugin to the menus. |
| 170 | + foreach ($subplugins as $shortname => $toolobj) { |
| 171 | + |
| 172 | + // Check if user has capability to view this tool. |
| 173 | + if ($shortname == 'note') { |
| 174 | + // Get tool-specific URL if the tool has a navigation method. |
| 175 | + if (method_exists($toolobj, 'get_navigation_url')) { |
| 176 | + $toolurl = $toolobj->get_navigation_url($courseid); |
| 177 | + } else { |
| 178 | + // Default URL pattern for tools. |
| 179 | + $toolurl = new moodle_url('/local/learningtools/ltool/'.$shortname.'/view.php', |
| 180 | + ['id' => $courseid]); |
| 181 | + } |
| 182 | + |
| 183 | + // Get tool name. |
| 184 | + $toolname = get_string('toolname', 'ltool_'.$shortname); |
| 185 | + |
| 186 | + // Add to menus. |
| 187 | + $menus[$toolurl->out(false)] = $toolname; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + $selectmenu = new select_menu('learningtoolsselect', $menus, $this->activeurl->out(false)); |
| 192 | + $selectmenu->set_label(get_string('learningtools', 'local_learningtools'), ['class' => 'sr-only']); |
| 193 | + |
| 194 | + return $selectmenu; |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Get the related sections. |
| 199 | + * |
| 200 | + * @return array |
| 201 | + */ |
| 202 | + public function get_sections() { |
| 203 | + global $USER, $DB; |
| 204 | + $course = get_course($this->courseid); |
| 205 | + $data = []; |
| 206 | + |
| 207 | + $sql = "SELECT n.*, cm.id as cmid, m.name as modulename |
| 208 | + FROM {ltool_note_data} n |
| 209 | + LEFT JOIN {course_modules} cm ON cm.id = n.coursemodule |
| 210 | + LEFT JOIN {modules} m ON m.id = cm.module |
| 211 | + WHERE n.course = :courseid AND n.pagetype = :pagetype |
| 212 | + AND n.userid = :userid |
| 213 | + ORDER BY n.timecreated DESC"; |
| 214 | + $params = [ |
| 215 | + 'courseid' => $this->courseid, |
| 216 | + 'userid' => $USER->id, |
| 217 | + 'pagetype' => 'course-view-section-' . $course->format, |
| 218 | + ]; |
| 219 | + |
| 220 | + $notes = $DB->get_records_sql($sql, $params); |
| 221 | + |
| 222 | + foreach ($notes as $note) { |
| 223 | + $sectionurl = new \moodle_url($note->pageurl); |
| 224 | + $sectionid = $sectionurl->get_param('id'); |
| 225 | + |
| 226 | + if (!isset($data[$sectionid])) { |
| 227 | + $section = $DB->get_record('course_sections', ['course' => $this->courseid, 'id' => $sectionid]); |
| 228 | + if ($section) { |
| 229 | + $sectionname = $section->name ?: ( |
| 230 | + $section->section == 0 |
| 231 | + ? get_string('general') |
| 232 | + : get_string('section', 'local_learningtools') . ' ' . $section->section |
| 233 | + ); |
| 234 | + |
| 235 | + $filterurl = new \moodle_url('/local/learningtools/ltool/note/view.php', [ |
| 236 | + 'id' => $this->courseid, |
| 237 | + 'sectionid' => $section->id, |
| 238 | + 'filter' => 'section', |
| 239 | + ]); |
| 240 | + |
| 241 | + $data[$sectionid] = [ |
| 242 | + 'sectionname' => $sectionname, |
| 243 | + 'sectionid' => $section->id, |
| 244 | + 'filterurl' => $filterurl->out(false), |
| 245 | + 'selected' => ($section->id == $this->sectionid) ? "selected" : "", |
| 246 | + ]; |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + return array_values($data); // Re-index to return a clean list. |
| 252 | + } |
| 253 | + |
| 254 | + /** |
| 255 | + * Gets the activity selected record. |
| 256 | + * @return array course activity selector records. |
| 257 | + */ |
| 258 | + public function get_activities() { |
| 259 | + global $DB, $USER; |
| 260 | + |
| 261 | + $sql = "SELECT lnd.coursemodule, lnd.course |
| 262 | + FROM {ltool_note_data} lnd |
| 263 | + LEFT JOIN {course_modules} cm ON cm.id = lnd.coursemodule |
| 264 | + WHERE cm.deletioninprogress = 0 |
| 265 | + AND lnd.course = :course |
| 266 | + AND lnd.coursemodule != 0 |
| 267 | + AND lnd.userid = :userid"; |
| 268 | + |
| 269 | + $params = [ |
| 270 | + 'course' => $this->courseid, |
| 271 | + 'userid' => $USER->id, |
| 272 | + ]; |
| 273 | + |
| 274 | + if (!empty($this->sectionid)) { |
| 275 | + $sql .= " AND cm.section = :sectionid"; |
| 276 | + $params['sectionid'] = $this->sectionid; |
| 277 | + } |
| 278 | + |
| 279 | + $sql .= " GROUP BY lnd.coursemodule, lnd.course"; |
| 280 | + |
| 281 | + $records = $DB->get_records_sql($sql, $params); |
| 282 | + $data = []; |
| 283 | + |
| 284 | + if (!empty($records)) { |
| 285 | + foreach ($records as $record) { |
| 286 | + $record->courseid = $record->course; |
| 287 | + $list['mod'] = local_learningtools_get_module_name($record); |
| 288 | + $filterurl = new moodle_url('/local/learningtools/ltool/note/view.php', ['id' => $this->courseid, |
| 289 | + 'activity' => $record->coursemodule, |
| 290 | + 'filter' => 'activity']); |
| 291 | + $list['filterurl'] = $filterurl->out(false); |
| 292 | + if ($record->coursemodule == $this->activity) { |
| 293 | + $list['selected'] = "selected"; |
| 294 | + } else { |
| 295 | + $list['selected'] = ""; |
| 296 | + } |
| 297 | + $data[] = $list; |
| 298 | + } |
| 299 | + |
| 300 | + } |
| 301 | + return $data; |
| 302 | + } |
| 303 | +} |
0 commit comments