2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Course selector field.
21 * Allows auto-complete ajax searching for courses and can restrict by enrolment, permissions, viewhidden...
24 * @copyright 2015 Damyon Wiese <damyon@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 require_once($CFG->libdir
. '/form/autocomplete.php');
32 * Form field type for choosing a course.
34 * Allows auto-complete ajax searching for courses and can restrict by enrolment, permissions, viewhidden...
37 * @copyright 2015 Damyon Wiese <damyon@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class MoodleQuickForm_course
extends MoodleQuickForm_autocomplete
{
43 * @var array $exclude Exclude a list of courses from the list (e.g. the current course).
45 protected $exclude = array();
48 * @var boolean $allowmultiple Allow selecting more than one course.
50 protected $multiple = false;
53 * @var array $requiredcapabilities Array of extra capabilities to check at the course context.
55 protected $requiredcapabilities = array();
58 * @var bool $limittoenrolled Only allow enrolled courses.
60 protected $limittoenrolled = false;
65 * @param string $elementname Element name
66 * @param mixed $elementlabel Label(s) for an element
67 * @param array $options Options to control the element's display
69 * 'multiple' - boolean multi select
70 * 'exclude' - array or int, list of course ids to never show
71 * 'requiredcapabilities' - array of capabilities. Uses ANY to combine them.
72 * 'limittoenrolled' - boolean Limits to enrolled courses.
73 * 'includefrontpage' - boolean Enables the frontpage to be selected.
75 public function __construct($elementname = null, $elementlabel = null, $options = array()) {
76 if (isset($options['multiple'])) {
77 $this->multiple
= $options['multiple'];
79 if (isset($options['exclude'])) {
80 $this->exclude
= $options['exclude'];
81 if (!is_array($this->exclude
)) {
82 $this->exclude
= array($this->exclude
);
85 if (isset($options['requiredcapabilities'])) {
86 $this->requiredcapabilities
= $options['requiredcapabilities'];
88 if (isset($options['limittoenrolled'])) {
89 $this->limittoenrolled
= $options['limittoenrolled'];
92 $validattributes = array(
93 'ajax' => 'core/form-course-selector',
94 'data-requiredcapabilities' => implode(',', $this->requiredcapabilities
),
95 'data-exclude' => implode(',', $this->exclude
),
96 'data-limittoenrolled' => (int)$this->limittoenrolled
98 if ($this->multiple
) {
99 $validattributes['multiple'] = 'multiple';
101 if (isset($options['noselectionstring'])) {
102 $validattributes['noselectionstring'] = $options['noselectionstring'];
104 if (isset($options['placeholder'])) {
105 $validattributes['placeholder'] = $options['placeholder'];
107 if (!empty($options['includefrontpage'])) {
108 $validattributes['data-includefrontpage'] = SITEID
;
111 parent
::__construct($elementname, $elementlabel, array(), $validattributes);
115 * Set the value of this element. If values can be added or are unknown, we will
116 * make sure they exist in the options array.
117 * @param string|array $value The value to set.
120 public function setValue($value) {
122 $values = (array) $value;
123 $coursestofetch = array();
125 foreach ($values as $onevalue) {
126 if ((!$this->optionExists($onevalue)) &&
127 ($onevalue !== '_qf__force_multiselect_submission')) {
128 array_push($coursestofetch, $onevalue);
132 if (empty($coursestofetch)) {
133 return $this->setSelected($values);
136 // There is no API function to load a list of course from a list of ids.
137 $ctxselect = context_helper
::get_preload_record_columns_sql('ctx');
138 $fields = array('c.id', 'c.category', 'c.sortorder',
139 'c.shortname', 'c.fullname', 'c.idnumber',
140 'c.startdate', 'c.visible', 'c.cacherev');
141 list($whereclause, $params) = $DB->get_in_or_equal($coursestofetch, SQL_PARAMS_NAMED
, 'id');
143 $sql = "SELECT ". join(',', $fields). ", $ctxselect
145 JOIN {context} ctx ON c.id = ctx.instanceid AND ctx.contextlevel = :contextcourse
146 WHERE c.id ". $whereclause." ORDER BY c.sortorder";
147 $list = $DB->get_records_sql($sql, array('contextcourse' => CONTEXT_COURSE
) +
$params);
149 $coursestoselect = array();
150 foreach ($list as $course) {
151 context_helper
::preload_from_record($course);
152 $context = context_course
::instance($course->id
);
153 // Make sure we can see the course.
154 if (!$course->visible
&& !has_capability('moodle/course:viewhiddencourses', $context)) {
157 $label = format_string(get_course_display_name_for_list($course), true, ['context' => $context]);
158 $this->addOption($label, $course->id
);
159 array_push($coursestoselect, $course->id
);
162 return $this->setSelected($values);