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 cohort.
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');
30 require_once($CFG->dirroot
. '/cohort/lib.php');
33 * Form field type for choosing a cohort.
35 * Allows auto-complete ajax searching for cohort.
38 * @copyright 2016 Damyon Wiese <damyon@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class MoodleQuickForm_cohort
extends MoodleQuickForm_autocomplete
{
44 * @var array $exclude Exclude a list of cohorts from the list (e.g. the current cohort).
46 protected $exclude = array();
49 * @var int $contextid The context id to fetch cohorts in.
51 protected $contextid = 0;
54 * @var boolean $allowmultiple Allow selecting more than one cohort.
56 protected $multiple = false;
59 * @var array $requiredcapabilities Array of extra capabilities to check at the cohort context.
61 protected $requiredcapabilities = array();
66 * @param string $elementname Element name
67 * @param mixed $elementlabel Label(s) for an element
68 * @param array $options Options to control the element's display
70 * 'multiple' - boolean multi select
71 * 'exclude' - array or int, list of course ids to never show
72 * 'requiredcapabilities' - array of capabilities. Uses ANY to combine them.
74 public function __construct($elementname = null, $elementlabel = null, $options = array()) {
75 if (isset($options['multiple'])) {
76 $this->multiple
= $options['multiple'];
78 if (isset($options['contextid'])) {
79 $this->contextid
= $options['contextid'];
81 $this->contextid
= context_system
::instance()->id
;
83 if (isset($options['exclude'])) {
84 $this->exclude
= $options['exclude'];
85 if (!is_array($this->exclude
)) {
86 $this->exclude
= array($this->exclude
);
89 if (isset($options['requiredcapabilities'])) {
90 $this->requiredcapabilities
= $options['requiredcapabilities'];
93 $validattributes = array(
94 'ajax' => 'core/form-cohort-selector',
95 'data-exclude' => implode(',', $this->exclude
),
96 'data-contextid' => (int)$this->contextid
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'];
108 parent
::__construct($elementname, $elementlabel, array(), $validattributes);
112 * Set the value of this element. If values can be added or are unknown, we will
113 * make sure they exist in the options array.
114 * @param string|array $value The value to set.
117 public function setValue($value) {
119 $values = (array) $value;
120 $cohortstofetch = array();
122 foreach ($values as $onevalue) {
123 if ($onevalue && !$this->optionExists($onevalue) &&
124 ($onevalue !== '_qf__force_multiselect_submission')) {
125 array_push($cohortstofetch, $onevalue);
129 if (empty($cohortstofetch)) {
130 $this->setSelected($values);
134 list($whereclause, $params) = $DB->get_in_or_equal($cohortstofetch, SQL_PARAMS_NAMED
, 'id');
136 $list = $DB->get_records_select('cohort', 'id ' . $whereclause, $params, 'name');
138 $currentcontext = context_helper
::instance_by_id($this->contextid
);
139 foreach ($list as $cohort) {
140 // Make sure we can see the cohort.
141 if (!cohort_can_view_cohort($cohort, $currentcontext)) {
144 $label = format_string($cohort->name
, true, $currentcontext);
145 $this->addOption($label, $cohort->id
);
148 $this->setSelected($values);