Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]

   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          $options['includecustomfields'] = true;
  40          parent::__construct($name, $options);
  41      }
  42  
  43      /**
  44       * Candidate users
  45       * @param string $search
  46       * @return array
  47       */
  48      public function find_users($search) {
  49          global $DB;
  50  
  51          // By default wherecondition retrieves all users except the deleted, not confirmed and guest.
  52          list($wherecondition, $params) = $this->search_sql($search, 'u');
  53          $params = array_merge($params, $this->userfieldsparams);
  54  
  55          $params['cohortid'] = $this->cohortid;
  56  
  57          $fields      = 'SELECT u.id, ' . $this->userfieldsselects;
  58          $countfields = 'SELECT COUNT(1)';
  59  
  60          $sql = " FROM {user} u
  61              LEFT JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
  62                  $this->userfieldsjoin
  63                  WHERE cm.id IS NULL AND $wherecondition";
  64  
  65          list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext, $this->userfieldsmappings);
  66          $order = ' ORDER BY ' . $sort;
  67  
  68          if (!$this->is_validating()) {
  69              $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
  70              if ($potentialmemberscount > $this->maxusersperpage) {
  71                  return $this->too_many_results($search, $potentialmemberscount);
  72              }
  73          }
  74  
  75          $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
  76  
  77          if (empty($availableusers)) {
  78              return array();
  79          }
  80  
  81  
  82          if ($search) {
  83              $groupname = get_string('potusersmatching', 'cohort', $search);
  84          } else {
  85              $groupname = get_string('potusers', 'cohort');
  86          }
  87  
  88          return array($groupname => $availableusers);
  89      }
  90  
  91      protected function get_options() {
  92          $options = parent::get_options();
  93          $options['cohortid'] = $this->cohortid;
  94          $options['file'] = 'cohort/locallib.php';
  95          return $options;
  96      }
  97  }
  98  
  99  
 100  /**
 101   * Cohort assignment candidates
 102   */
 103  class cohort_existing_selector extends user_selector_base {
 104      protected $cohortid;
 105  
 106      public function __construct($name, $options) {
 107          $this->cohortid = $options['cohortid'];
 108          $options['includecustomfields'] = true;
 109          parent::__construct($name, $options);
 110      }
 111  
 112      /**
 113       * Candidate users
 114       * @param string $search
 115       * @return array
 116       */
 117      public function find_users($search) {
 118          global $DB;
 119  
 120          // By default wherecondition retrieves all users except the deleted, not confirmed and guest.
 121          list($wherecondition, $params) = $this->search_sql($search, 'u');
 122          $params = array_merge($params, $this->userfieldsparams);
 123  
 124          $params['cohortid'] = $this->cohortid;
 125  
 126          $fields      = 'SELECT u.id, ' . $this->userfieldsselects;
 127          $countfields = 'SELECT COUNT(1)';
 128  
 129          $sql = " FROM {user} u
 130                   JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
 131                   $this->userfieldsjoin
 132                  WHERE $wherecondition";
 133  
 134          list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext, $this->userfieldsmappings);
 135          $order = ' ORDER BY ' . $sort;
 136  
 137          if (!$this->is_validating()) {
 138              $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
 139              if ($potentialmemberscount > $this->maxusersperpage) {
 140                  return $this->too_many_results($search, $potentialmemberscount);
 141              }
 142          }
 143  
 144          $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
 145  
 146          if (empty($availableusers)) {
 147              return array();
 148          }
 149  
 150  
 151          if ($search) {
 152              $groupname = get_string('currentusersmatching', 'cohort', $search);
 153          } else {
 154              $groupname = get_string('currentusers', 'cohort');
 155          }
 156  
 157          return array($groupname => $availableusers);
 158      }
 159  
 160      protected function get_options() {
 161          $options = parent::get_options();
 162          $options['cohortid'] = $this->cohortid;
 163          $options['file'] = 'cohort/locallib.php';
 164          return $options;
 165      }
 166  }
 167