Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
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 /** 19 * Course selector field. 20 * 21 * Allows auto-complete ajax searching for cohort. 22 * 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 26 */ 27 28 global $CFG; 29 require_once($CFG->libdir . '/form/autocomplete.php'); 30 require_once($CFG->dirroot . '/cohort/lib.php'); 31 32 /** 33 * Form field type for choosing a cohort. 34 * 35 * Allows auto-complete ajax searching for cohort. 36 * 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 40 */ 41 class MoodleQuickForm_cohort extends MoodleQuickForm_autocomplete { 42 43 /** 44 * @var array $exclude Exclude a list of cohorts from the list (e.g. the current cohort). 45 */ 46 protected $exclude = array(); 47 48 /** 49 * @var int $contextid The context id to fetch cohorts in. 50 */ 51 protected $contextid = 0; 52 53 /** 54 * @var boolean $allowmultiple Allow selecting more than one cohort. 55 */ 56 protected $multiple = false; 57 58 /** 59 * @var array $requiredcapabilities Array of extra capabilities to check at the cohort context. 60 */ 61 protected $requiredcapabilities = array(); 62 63 /** 64 * Constructor 65 * 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. 73 */ 74 public function __construct($elementname = null, $elementlabel = null, $options = array()) { 75 if (isset($options['multiple'])) { 76 $this->multiple = $options['multiple']; 77 } 78 if (isset($options['contextid'])) { 79 $this->contextid = $options['contextid']; 80 } else { 81 $this->contextid = context_system::instance()->id; 82 } 83 if (isset($options['exclude'])) { 84 $this->exclude = $options['exclude']; 85 if (!is_array($this->exclude)) { 86 $this->exclude = array($this->exclude); 87 } 88 } 89 if (isset($options['requiredcapabilities'])) { 90 $this->requiredcapabilities = $options['requiredcapabilities']; 91 } 92 93 $validattributes = array( 94 'ajax' => 'core/form-cohort-selector', 95 'data-exclude' => implode(',', $this->exclude), 96 'data-contextid' => (int)$this->contextid 97 ); 98 if ($this->multiple) { 99 $validattributes['multiple'] = 'multiple'; 100 } 101 if (isset($options['noselectionstring'])) { 102 $validattributes['noselectionstring'] = $options['noselectionstring']; 103 } 104 if (isset($options['placeholder'])) { 105 $validattributes['placeholder'] = $options['placeholder']; 106 } 107 108 parent::__construct($elementname, $elementlabel, array(), $validattributes); 109 } 110 111 /** 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 116 */ 117 public function setValue($value) { 118 global $DB; 119 $values = (array) $value; 120 $cohortstofetch = array(); 121 122 foreach ($values as $onevalue) { 123 if ($onevalue && !$this->optionExists($onevalue) && 124 ($onevalue !== '_qf__force_multiselect_submission')) { 125 array_push($cohortstofetch, $onevalue); 126 } 127 } 128 129 if (empty($cohortstofetch)) { 130 $this->setSelected($values); 131 return true; 132 } 133 134 list($whereclause, $params) = $DB->get_in_or_equal($cohortstofetch, SQL_PARAMS_NAMED, 'id'); 135 136 $list = $DB->get_records_select('cohort', 'id ' . $whereclause, $params, 'name'); 137 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; 143 } 144 $label = format_string($cohort->name, true, $currentcontext); 145 $this->addOption($label, $cohort->id); 146 } 147 148 $this->setSelected($values); 149 return true; 150 } 151 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body