forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-35465 refactor cohort user selectors to locallib.php
It is recommended to keep lib.php files as small as possible and without extra includes.
- v3.2.2
- v3.2.1
- v3.2.0
- v3.2.0-rc5
- v3.2.0-rc4
- v3.2.0-rc3
- v3.2.0-rc2
- v3.2.0-rc1
- v3.2.0-beta
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.1.0-rc2
- v3.1.0-rc1
- v3.1.0-beta
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0-rc4
- v3.0.0-rc3
- v3.0.0-rc2
- v3.0.0-rc1
- v3.0.0-beta
- v2.9.9
- v2.9.8
- v2.9.7
- v2.9.6
- v2.9.5
- v2.9.4
- v2.9.3
- v2.9.2
- v2.9.1
- v2.9.0
- v2.9.0-rc2
- v2.9.0-rc1
- v2.9.0-beta
- v2.8.12
- v2.8.11
- v2.8.10
- v2.8.9
- v2.8.8
- v2.8.7
- v2.8.6
- v2.8.5
- v2.8.4
- v2.8.3
- v2.8.2
- v2.8.1
- v2.8.0
- v2.8.0-rc2
- v2.8.0-rc1
- v2.8.0-beta
- v2.7.19
- v2.7.18
- v2.7.17
- v2.7.16
- v2.7.15
- v2.7.14
- v2.7.13
- v2.7.12
- v2.7.11
- v2.7.10
- v2.7.9
- v2.7.8
- v2.7.7
- v2.7.6
- v2.7.5
- v2.7.4
- v2.7.3
- v2.7.2
- v2.7.1
- v2.7.0
- v2.7.0-rc2
- v2.7.0-rc1
- v2.7.0-beta
- v2.6.11
- v2.6.10
- v2.6.9
- v2.6.8
- v2.6.7
- v2.6.6
- v2.6.5
- v2.6.4
- v2.6.3
- v2.6.2
- v2.6.1
- v2.6.0
- v2.6.0-rc1
- v2.6.0-beta
- v2.5.9
- v2.5.8
- v2.5.7
- v2.5.6
- v2.5.5
- v2.5.4
- v2.5.3
- v2.5.2
- v2.5.1
- v2.5.0
- v2.5.0-rc1
- v2.5.0-beta
- v2.4.11
- v2.4.10
- v2.4.9
- v2.4.8
- v2.4.7
- v2.4.6
- v2.4.5
- v2.4.4
- v2.4.3
- v2.4.2
- v2.4.1
- v2.4.0
- v2.4.0-rc1
- v2.4.0-beta
Showing
3 changed files
with
157 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Cohort UI related functions and classes. | ||
* | ||
* @package core_cohort | ||
* @copyright 2012 Petr Skoda {@link http://skodak.org} | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
require_once($CFG->dirroot . '/cohot/lib.php'); | ||
require_once($CFG->dirroot . '/user/selector/lib.php'); | ||
|
||
/** | ||
* Cohort assignment candidates | ||
*/ | ||
class cohort_candidate_selector extends user_selector_base { | ||
protected $cohortid; | ||
|
||
public function __construct($name, $options) { | ||
$this->cohortid = $options['cohortid']; | ||
parent::__construct($name, $options); | ||
} | ||
|
||
/** | ||
* Candidate users | ||
* @param <type> $search | ||
* @return array | ||
*/ | ||
public function find_users($search) { | ||
global $DB; | ||
//by default wherecondition retrieves all users except the deleted, not confirmed and guest | ||
list($wherecondition, $params) = $this->search_sql($search, 'u'); | ||
$params['cohortid'] = $this->cohortid; | ||
|
||
$fields = 'SELECT ' . $this->required_fields_sql('u'); | ||
$countfields = 'SELECT COUNT(1)'; | ||
|
||
$sql = " FROM {user} u | ||
LEFT JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid) | ||
WHERE cm.id IS NULL AND $wherecondition"; | ||
|
||
$order = ' ORDER BY u.lastname ASC, u.firstname ASC'; | ||
|
||
if (!$this->is_validating()) { | ||
$potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params); | ||
if ($potentialmemberscount > 100) { | ||
return $this->too_many_results($search, $potentialmemberscount); | ||
} | ||
} | ||
|
||
$availableusers = $DB->get_records_sql($fields . $sql . $order, $params); | ||
|
||
if (empty($availableusers)) { | ||
return array(); | ||
} | ||
|
||
|
||
if ($search) { | ||
$groupname = get_string('potusersmatching', 'cohort', $search); | ||
} else { | ||
$groupname = get_string('potusers', 'cohort'); | ||
} | ||
|
||
return array($groupname => $availableusers); | ||
} | ||
|
||
protected function get_options() { | ||
$options = parent::get_options(); | ||
$options['cohortid'] = $this->cohortid; | ||
$options['file'] = 'cohort/locallib.php'; | ||
return $options; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Cohort assignment candidates | ||
*/ | ||
class cohort_existing_selector extends user_selector_base { | ||
protected $cohortid; | ||
|
||
public function __construct($name, $options) { | ||
$this->cohortid = $options['cohortid']; | ||
parent::__construct($name, $options); | ||
} | ||
|
||
/** | ||
* Candidate users | ||
* @param <type> $search | ||
* @return array | ||
*/ | ||
public function find_users($search) { | ||
global $DB; | ||
//by default wherecondition retrieves all users except the deleted, not confirmed and guest | ||
list($wherecondition, $params) = $this->search_sql($search, 'u'); | ||
$params['cohortid'] = $this->cohortid; | ||
|
||
$fields = 'SELECT ' . $this->required_fields_sql('u'); | ||
$countfields = 'SELECT COUNT(1)'; | ||
|
||
$sql = " FROM {user} u | ||
JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid) | ||
WHERE $wherecondition"; | ||
|
||
$order = ' ORDER BY u.lastname ASC, u.firstname ASC'; | ||
|
||
if (!$this->is_validating()) { | ||
$potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params); | ||
if ($potentialmemberscount > 100) { | ||
return $this->too_many_results($search, $potentialmemberscount); | ||
} | ||
} | ||
|
||
$availableusers = $DB->get_records_sql($fields . $sql . $order, $params); | ||
|
||
if (empty($availableusers)) { | ||
return array(); | ||
} | ||
|
||
|
||
if ($search) { | ||
$groupname = get_string('currentusersmatching', 'cohort', $search); | ||
} else { | ||
$groupname = get_string('currentusers', 'cohort'); | ||
} | ||
|
||
return array($groupname => $availableusers); | ||
} | ||
|
||
protected function get_options() { | ||
$options = parent::get_options(); | ||
$options['cohortid'] = $this->cohortid; | ||
$options['file'] = 'cohort/locallib.php'; | ||
return $options; | ||
} | ||
} | ||
|