MDL-78962 core/loadingicon: remove jQuery requirement in the API
[moodle.git] / lib / form / cohort.php
blob88945a793668363cbdd692da35908d945807d036
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.
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/>.
18 /**
19 * Course selector field.
21 * Allows auto-complete ajax searching for cohort.
23 * @package core_form
24 * @copyright 2015 Damyon Wiese <damyon@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 global $CFG;
29 require_once($CFG->libdir . '/form/autocomplete.php');
30 require_once($CFG->dirroot . '/cohort/lib.php');
32 /**
33 * Form field type for choosing a cohort.
35 * Allows auto-complete ajax searching for cohort.
37 * @package core_form
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 {
43 /**
44 * @var array $exclude Exclude a list of cohorts from the list (e.g. the current cohort).
46 protected $exclude = array();
48 /**
49 * @var int $contextid The context id to fetch cohorts in.
51 protected $contextid = 0;
53 /**
54 * @var boolean $allowmultiple Allow selecting more than one cohort.
56 protected $multiple = false;
58 /**
59 * @var array $requiredcapabilities Array of extra capabilities to check at the cohort context.
61 protected $requiredcapabilities = array();
63 /**
64 * Constructor
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
69 * Valid options are:
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'];
80 } else {
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.
115 * @return boolean
117 public function setValue($value) {
118 global $DB;
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);
131 return true;
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)) {
142 continue;
144 $label = format_string($cohort->name, true, $currentcontext);
145 $this->addOption($label, $cohort->id);
148 $this->setSelected($values);
149 return true;