MDL-67264 core_templates: If classes exists render it out
[moodle.git] / course / classes / output / modchooser.php
CommitLineData
4f2378d9
FM
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 * The modchooser renderable.
19 *
20 * @package core_course
21 * @copyright 2016 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace core_course\output;
26defined('MOODLE_INTERNAL') || die();
27
28use core\output\chooser;
29use core\output\chooser_section;
30use context_course;
31use lang_string;
32use moodle_url;
33use pix_icon;
34use renderer_base;
35use stdClass;
36
37/**
38 * The modchooser renderable class.
39 *
40 * @package core_course
41 * @copyright 2016 Frédéric Massart - FMCorz.net
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44class modchooser extends chooser {
45
46 /** @var stdClass The course. */
47 public $course;
48
49 /**
50 * Constructor.
51 *
52 * @param stdClass $course The course.
53 * @param stdClass[] $modules The modules.
54 */
55 public function __construct(stdClass $course, array $modules) {
56 $this->course = $course;
57
58 $sections = [];
59 $context = context_course::instance($course->id);
60
e584e6ae 61 // Activities.
4f2378d9
FM
62 $activities = array_filter($modules, function($mod) {
63 return ($mod->archetype !== MOD_ARCHETYPE_RESOURCE && $mod->archetype !== MOD_ARCHETYPE_SYSTEM);
64 });
65 if (count($activities)) {
66 $sections[] = new chooser_section('activities', new lang_string('activities'),
67 array_map(function($module) use ($context) {
68 return new modchooser_item($module, $context);
69 }, $activities)
70 );
71 }
72
73 $resources = array_filter($modules, function($mod) {
74 return ($mod->archetype === MOD_ARCHETYPE_RESOURCE);
75 });
76 if (count($resources)) {
77 $sections[] = new chooser_section('resources', new lang_string('resources'),
78 array_map(function($module) use ($context) {
79 return new modchooser_item($module, $context);
80 }, $resources)
81 );
82 }
83
84 $actionurl = new moodle_url('/course/jumpto.php');
85 $title = new lang_string('addresourceoractivity');
86 parent::__construct($actionurl, $title, $sections, 'jumplink');
87
88 $this->set_instructions(new lang_string('selectmoduletoviewhelp'));
89 $this->add_param('course', $course->id);
90 }
91
92 /**
93 * Export for template.
94 *
95 * @param renderer_base The renderer.
96 * @return stdClass
97 */
98 public function export_for_template(renderer_base $output) {
99 $data = parent::export_for_template($output);
100 $data->courseid = $this->course->id;
101 return $data;
102 }
103
104}