Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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   * Cohort UI related functions and classes.
  19   *
  20   * @package    core_cohort
  21   * @copyright  2012 Petr Skoda  {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once($CFG->dirroot . '/cohort/lib.php');
  28  require_once($CFG->dirroot . '/user/selector/lib.php');
  29  
  30  
  31  /**
  32   * Cohort assignment candidates
  33   */
  34  class cohort_candidate_selector extends user_selector_base {
  35      protected $cohortid;
  36  
  37      public function __construct($name, $options) {
  38          $this->cohortid = $options['cohortid'];
  39          parent::__construct($name, $options);
  40      }
  41  
  42      /**
  43       * Candidate users
  44       * @param string $search
  45       * @return array
  46       */
  47      public function find_users($search) {
  48          global $DB;
  49          // By default wherecondition retrieves all users except the deleted, not confirmed and guest.
  50          list($wherecondition, $params) = $this->search_sql($search, 'u');
  51          $params['cohortid'] = $this->cohortid;
  52  
  53          $fields      = 'SELECT ' . $this->required_fields_sql('u');
  54          $countfields = 'SELECT COUNT(1)';
  55  
  56          $sql = " FROM {user} u
  57              LEFT JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
  58                  WHERE cm.id IS NULL AND $wherecondition";
  59  
  60          list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
  61          $order = ' ORDER BY ' . $sort;
  62  
  63          if (!$this->is_validating()) {
  64              $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
  65              if ($potentialmemberscount > $this->maxusersperpage) {
  66                  return $this->too_many_results($search, $potentialmemberscount);
  67              }
  68          }
  69  
  70          $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
  71  
  72          if (empty($availableusers)) {
  73              return array();
  74          }
  75  
  76  
  77          if ($search) {
  78              $groupname = get_string('potusersmatching', 'cohort', $search);
  79          } else {
  80              $groupname = get_string('potusers', 'cohort');
  81          }
  82  
  83          return array($groupname => $availableusers);
  84      }
  85  
  86      protected function get_options() {
  87          $options = parent::get_options();
  88          $options['cohortid'] = $this->cohortid;
  89          $options['file'] = 'cohort/locallib.php';
  90          return $options;
  91      }
  92  }
  93  
  94  
  95  /**
  96   * Cohort assignment candidates
  97   */
  98  class cohort_existing_selector extends user_selector_base {
  99      protected $cohortid;
 100  
 101      public function __construct($name, $options) {
 102          $this->cohortid = $options['cohortid'];
 103          parent::__construct($name, $options);
 104      }
 105  
 106      /**
 107       * Candidate users
 108       * @param string $search
 109       * @return array
 110       */
 111      public function find_users($search) {
 112          global $DB;
 113          // By default wherecondition retrieves all users except the deleted, not confirmed and guest.
 114          list($wherecondition, $params) = $this->search_sql($search, 'u');
 115          $params['cohortid'] = $this->cohortid;
 116  
 117          $fields      = 'SELECT ' . $this->required_fields_sql('u');
 118          $countfields = 'SELECT COUNT(1)';
 119  
 120          $sql = " FROM {user} u
 121                   JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
 122                  WHERE $wherecondition";
 123  
 124          list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
 125          $order = ' ORDER BY ' . $sort;
 126  
 127          if (!$this->is_validating()) {
 128              $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
 129              if ($potentialmemberscount > $this->maxusersperpage) {
 130                  return $this->too_many_results($search, $potentialmemberscount);
 131              }
 132          }
 133  
 134          $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
 135  
 136          if (empty($availableusers)) {
 137              return array();
 138          }
 139  
 140  
 141          if ($search) {
 142              $groupname = get_string('currentusersmatching', 'cohort', $search);
 143          } else {
 144              $groupname = get_string('currentusers', 'cohort');
 145          }
 146  
 147          return array($groupname => $availableusers);
 148      }
 149  
 150      protected function get_options() {
 151          $options = parent::get_options();
 152          $options['cohortid'] = $this->cohortid;
 153          $options['file'] = 'cohort/locallib.php';
 154          return $options;
 155      }
 156  }
 157