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 mixed $attributes Array of typical HTML attributes plus additional options, such as:
68 * 'multiple' - boolean multi select
69 * 'exclude' - array or int, list of course ids to never show
70 * 'requiredcapabilities' - array of capabilities. Uses ANY to combine them.
71 * 'limittoenrolled' - boolean Limits to enrolled courses.
72 * 'includefrontpage' - boolean Enables the frontpage to be selected.
73 * 'onlywithcompletion' - only courses where completion is enabled
75 public function __construct($elementname = null, $elementlabel = null, $attributes = array()) {
76 if (!is_array($attributes)) {
79 if (isset($attributes['multiple'])) {
80 $this->multiple
= $attributes['multiple'];
82 if (isset($attributes['exclude'])) {
83 $this->exclude
= $attributes['exclude'];
84 if (!is_array($this->exclude
)) {
85 $this->exclude
= array($this->exclude
);
87 unset($attributes['exclude']);
89 if (isset($attributes['requiredcapabilities'])) {
90 $this->requiredcapabilities
= $attributes['requiredcapabilities'];
91 unset($attributes['requiredcapabilities']);
93 if (isset($attributes['limittoenrolled'])) {
94 $this->limittoenrolled
= $attributes['limittoenrolled'];
95 unset($attributes['limittoenrolled']);
99 'ajax' => 'core/form-course-selector',
100 'data-requiredcapabilities' => implode(',', $this->requiredcapabilities
),
101 'data-exclude' => implode(',', $this->exclude
),
102 'data-limittoenrolled' => (int)$this->limittoenrolled
104 if (!empty($attributes['includefrontpage'])) {
105 $attributes['data-includefrontpage'] = SITEID
;
106 unset($attributes['includefrontpage']);
108 if (!empty($options['onlywithcompletion'])) {
109 $validattributes['data-onlywithcompletion'] = 1;
112 parent
::__construct($elementname, $elementlabel, array(), $attributes);
116 * Set the value of this element. If values can be added or are unknown, we will
117 * make sure they exist in the options array.
118 * @param string|array $value The value to set.
121 public function setValue($value) {
123 $values = (array) $value;
124 $coursestofetch = array();
126 foreach ($values as $onevalue) {
127 if ((!$this->optionExists($onevalue)) &&
128 ($onevalue !== '_qf__force_multiselect_submission')) {
129 array_push($coursestofetch, $onevalue);
133 if (empty($coursestofetch)) {
134 return $this->setSelected($values);
137 // There is no API function to load a list of course from a list of ids.
138 $ctxselect = context_helper
::get_preload_record_columns_sql('ctx');
139 $fields = array('c.id', 'c.category', 'c.sortorder',
140 'c.shortname', 'c.fullname', 'c.idnumber',
141 'c.startdate', 'c.visible', 'c.cacherev');
142 list($whereclause, $params) = $DB->get_in_or_equal($coursestofetch, SQL_PARAMS_NAMED
, 'id');
144 $sql = "SELECT ". join(',', $fields). ", $ctxselect
146 JOIN {context} ctx ON c.id = ctx.instanceid AND ctx.contextlevel = :contextcourse
147 WHERE c.id ". $whereclause." ORDER BY c.sortorder";
148 $list = $DB->get_records_sql($sql, array('contextcourse' => CONTEXT_COURSE
) +
$params);
150 $mycourses = enrol_get_my_courses(null, null, 0, array_keys($list));
151 $coursestoselect = array();
152 foreach ($list as $course) {
153 context_helper
::preload_from_record($course);
154 $context = context_course
::instance($course->id
);
155 // Make sure we can see the course.
156 if (!array_key_exists($course->id
, $mycourses) && !core_course_category
::can_view_course_info($course)) {
159 $label = format_string(get_course_display_name_for_list($course), true, ['context' => $context]);
160 $this->addOption($label, $course->id
);
161 array_push($coursestoselect, $course->id
);
164 return $this->setSelected($values);