| 1 | efrain | 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 |     /**
 | 
        
           | 1441 | ariadna | 49 |      * @var string $includes One of self/parents/all
 | 
        
           |  |  | 50 |      */
 | 
        
           |  |  | 51 |     protected $includes = 'parents';
 | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 |     /**
 | 
        
           | 1 | efrain | 54 |      * @var int $contextid The context id to fetch cohorts in.
 | 
        
           |  |  | 55 |      */
 | 
        
           |  |  | 56 |     protected $contextid = 0;
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 |     /**
 | 
        
           |  |  | 59 |      * @var boolean $allowmultiple Allow selecting more than one cohort.
 | 
        
           |  |  | 60 |      */
 | 
        
           |  |  | 61 |     protected $multiple = false;
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 |     /**
 | 
        
           |  |  | 64 |      * @var array $requiredcapabilities Array of extra capabilities to check at the cohort context.
 | 
        
           |  |  | 65 |      */
 | 
        
           |  |  | 66 |     protected $requiredcapabilities = array();
 | 
        
           |  |  | 67 |   | 
        
           |  |  | 68 |     /**
 | 
        
           |  |  | 69 |      * Constructor
 | 
        
           |  |  | 70 |      *
 | 
        
           |  |  | 71 |      * @param string $elementname Element name
 | 
        
           |  |  | 72 |      * @param mixed $elementlabel Label(s) for an element
 | 
        
           |  |  | 73 |      * @param array $options Options to control the element's display
 | 
        
           |  |  | 74 |      *                       Valid options are:
 | 
        
           |  |  | 75 |      *                       'multiple' - boolean multi select
 | 
        
           |  |  | 76 |      *                       'exclude' - array or int, list of course ids to never show
 | 
        
           |  |  | 77 |      *                       'requiredcapabilities' - array of capabilities. Uses ANY to combine them.
 | 
        
           |  |  | 78 |      */
 | 
        
           |  |  | 79 |     public function __construct($elementname = null, $elementlabel = null, $options = array()) {
 | 
        
           |  |  | 80 |         if (isset($options['multiple'])) {
 | 
        
           |  |  | 81 |             $this->multiple = $options['multiple'];
 | 
        
           |  |  | 82 |         }
 | 
        
           |  |  | 83 |         if (isset($options['contextid'])) {
 | 
        
           |  |  | 84 |             $this->contextid = $options['contextid'];
 | 
        
           |  |  | 85 |         } else {
 | 
        
           |  |  | 86 |             $this->contextid = context_system::instance()->id;
 | 
        
           |  |  | 87 |         }
 | 
        
           |  |  | 88 |         if (isset($options['exclude'])) {
 | 
        
           |  |  | 89 |             $this->exclude = $options['exclude'];
 | 
        
           |  |  | 90 |             if (!is_array($this->exclude)) {
 | 
        
           |  |  | 91 |                 $this->exclude = array($this->exclude);
 | 
        
           |  |  | 92 |             }
 | 
        
           |  |  | 93 |         }
 | 
        
           | 1441 | ariadna | 94 |         if (isset($options['includes'])) {
 | 
        
           |  |  | 95 |             $this->includes = $options['includes'];
 | 
        
           |  |  | 96 |         }
 | 
        
           | 1 | efrain | 97 |         if (isset($options['requiredcapabilities'])) {
 | 
        
           |  |  | 98 |             $this->requiredcapabilities = $options['requiredcapabilities'];
 | 
        
           |  |  | 99 |         }
 | 
        
           |  |  | 100 |   | 
        
           |  |  | 101 |         $validattributes = array(
 | 
        
           |  |  | 102 |             'ajax' => 'core/form-cohort-selector',
 | 
        
           |  |  | 103 |             'data-exclude' => implode(',', $this->exclude),
 | 
        
           | 1441 | ariadna | 104 |             'data-includes' => $this->includes,
 | 
        
           | 1 | efrain | 105 |             'data-contextid' => (int)$this->contextid
 | 
        
           |  |  | 106 |         );
 | 
        
           |  |  | 107 |         if ($this->multiple) {
 | 
        
           |  |  | 108 |             $validattributes['multiple'] = 'multiple';
 | 
        
           |  |  | 109 |         }
 | 
        
           |  |  | 110 |         if (isset($options['noselectionstring'])) {
 | 
        
           |  |  | 111 |             $validattributes['noselectionstring'] = $options['noselectionstring'];
 | 
        
           |  |  | 112 |         }
 | 
        
           |  |  | 113 |         if (isset($options['placeholder'])) {
 | 
        
           |  |  | 114 |             $validattributes['placeholder'] = $options['placeholder'];
 | 
        
           |  |  | 115 |         }
 | 
        
           |  |  | 116 |   | 
        
           |  |  | 117 |         parent::__construct($elementname, $elementlabel, array(), $validattributes);
 | 
        
           |  |  | 118 |     }
 | 
        
           |  |  | 119 |   | 
        
           |  |  | 120 |     /**
 | 
        
           |  |  | 121 |      * Set the value of this element. If values can be added or are unknown, we will
 | 
        
           |  |  | 122 |      * make sure they exist in the options array.
 | 
        
           |  |  | 123 |      * @param string|array $value The value to set.
 | 
        
           |  |  | 124 |      * @return boolean
 | 
        
           |  |  | 125 |      */
 | 
        
           |  |  | 126 |     public function setValue($value) {
 | 
        
           |  |  | 127 |         global $DB;
 | 
        
           |  |  | 128 |         $values = (array) $value;
 | 
        
           |  |  | 129 |         $cohortstofetch = array();
 | 
        
           |  |  | 130 |   | 
        
           |  |  | 131 |         foreach ($values as $onevalue) {
 | 
        
           |  |  | 132 |             if ($onevalue && !$this->optionExists($onevalue) &&
 | 
        
           |  |  | 133 |                     ($onevalue !== '_qf__force_multiselect_submission')) {
 | 
        
           |  |  | 134 |                 array_push($cohortstofetch, $onevalue);
 | 
        
           |  |  | 135 |             }
 | 
        
           |  |  | 136 |         }
 | 
        
           |  |  | 137 |   | 
        
           |  |  | 138 |         if (empty($cohortstofetch)) {
 | 
        
           |  |  | 139 |             $this->setSelected($values);
 | 
        
           |  |  | 140 |             return true;
 | 
        
           |  |  | 141 |         }
 | 
        
           |  |  | 142 |   | 
        
           |  |  | 143 |         list($whereclause, $params) = $DB->get_in_or_equal($cohortstofetch, SQL_PARAMS_NAMED, 'id');
 | 
        
           |  |  | 144 |   | 
        
           |  |  | 145 |         $list = $DB->get_records_select('cohort', 'id ' . $whereclause, $params, 'name');
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 |         $currentcontext = context_helper::instance_by_id($this->contextid);
 | 
        
           |  |  | 148 |         foreach ($list as $cohort) {
 | 
        
           |  |  | 149 |             // Make sure we can see the cohort.
 | 
        
           |  |  | 150 |             if (!cohort_can_view_cohort($cohort, $currentcontext)) {
 | 
        
           |  |  | 151 |                 continue;
 | 
        
           |  |  | 152 |             }
 | 
        
           |  |  | 153 |             $label = format_string($cohort->name, true, ['context' => $currentcontext]);
 | 
        
           |  |  | 154 |             $this->addOption($label, $cohort->id);
 | 
        
           |  |  | 155 |         }
 | 
        
           |  |  | 156 |   | 
        
           |  |  | 157 |         $this->setSelected($values);
 | 
        
           |  |  | 158 |         return true;
 | 
        
           |  |  | 159 |     }
 | 
        
           |  |  | 160 | }
 |