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-55491 badges: Add cohort as badge criteria
- Loading branch information
Showing
6 changed files
with
569 additions
and
43 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 |
---|---|---|
|
@@ -31,18 +31,26 @@ | |
/** | ||
* Badge award criteria -- award on cohort membership | ||
* | ||
* @package core | ||
* @subpackage badges | ||
* @copyright 2016 onwards Catalyst IT {@link https://www.catalyst.net.nz/} | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
* @author Eugene Venter <[email protected]> | ||
*/ | ||
class award_criteria_cohort extends award_criteria { | ||
|
||
/* @var int Criteria [BADGE_CRITERIA_TYPE_COHORT] */ | ||
/* @var int $criteriatype Criteria [BADGE_CRITERIA_TYPE_COHORT] */ | ||
public $criteriatype = BADGE_CRITERIA_TYPE_COHORT; | ||
|
||
/* @var string $required_param Required form param */ | ||
public $required_param = 'cohort'; | ||
|
||
/* @var array $optional_params Optional form params */ | ||
public $optional_params = array(); | ||
|
||
/** | ||
* Get criteria details for displaying to users | ||
* | ||
* @param string $short Print short version of criteria | ||
* @return string | ||
*/ | ||
public function get_details($short = '') { | ||
|
@@ -69,6 +77,7 @@ public function get_details($short = '') { | |
/** | ||
* Add appropriate new criteria options to the form | ||
* | ||
* @param object $mform moodle form | ||
*/ | ||
public function get_options(&$mform) { | ||
global $DB; | ||
|
@@ -77,7 +86,7 @@ public function get_options(&$mform) { | |
$mform->addElement('header', 'first_header', $this->get_title()); | ||
$mform->addHelpButton('first_header', 'criteria_' . $this->criteriatype, 'badges'); | ||
|
||
// Get cohorts | ||
// Get cohorts. | ||
$cohorts = $DB->get_records_menu('cohort', array(), 'name ASC', 'id, name'); | ||
if (!empty($cohorts)) { | ||
$select = array(); | ||
|
@@ -122,10 +131,11 @@ public function get_options(&$mform) { | |
/** | ||
* Save criteria records | ||
* | ||
* @param $params criteria params | ||
* @param array $params Values from the form or any other array. | ||
*/ | ||
public function save($params = array()) { | ||
$cohorts = $params['cohort_cohorts']; | ||
|
||
unset($params['cohort_cohorts']); | ||
foreach ($cohorts as $cohortid) { | ||
$params["cohort_{$cohortid}"] = $cohortid; | ||
|
@@ -137,7 +147,11 @@ public function save($params = array()) { | |
/** | ||
* Review this criteria and decide if it has been completed | ||
* | ||
* @return bool Whether criteria is complete | ||
* @param int $userid User whose criteria completion needs to be reviewed. | ||
* @param bool $filtered An additional parameter indicating that user list | ||
* has been reduced and some expensive checks can be skipped. | ||
* | ||
* @return bool Whether criteria is complete. | ||
*/ | ||
public function review($userid, $filtered = false) { | ||
global $DB; | ||
|
@@ -197,9 +211,43 @@ public function validate() { | |
return array(true, ''); | ||
} | ||
|
||
/** | ||
* Returns array with sql code and parameters returning all ids | ||
* of users who meet this particular criterion. | ||
* | ||
* @return array list($join, $where, $params) | ||
*/ | ||
public function get_completed_criteria_sql() { | ||
// TODO; | ||
|
||
return array($join, $where, $params); | ||
$join = ''; | ||
$where = ''; | ||
$params = array(); | ||
|
||
if ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) { | ||
// User is a member of ANY of the specified cohorts. | ||
$join = " LEFT JOIN {cohort_members} cm ON cm.userid = u.id"; | ||
$where = "AND ("; | ||
$i = 0; | ||
foreach ($this->params as $param) { | ||
if ($i == 0) { | ||
$where .= ' cm.cohortid = :cohortid'.$i; | ||
} else { | ||
$where .= ' OR cm.cohortid = :cohortid'.$i; | ||
} | ||
$params['cohortid'.$i] = $param['cohort']; | ||
$i++; | ||
} | ||
$where .= ") "; | ||
return array($join, $where, $params); | ||
} else { | ||
// User is a member of ALL of the specified cohorts. | ||
$join = " LEFT JOIN {cohort_members} cm ON cm.userid = u.id"; | ||
$i = 0; | ||
foreach ($this->params as $param) { | ||
$i++; | ||
$where = ' AND cm.cohortid = :cohortid'.$i; | ||
$params['cohortid'.$i] = $param['cohort']; | ||
} | ||
return array($join, $where, $params); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.