MDL-63876 badges: Allow criteria to be optional

Competencies criteria type is only added if competencies are enabled.
This commit is contained in:
Damyon Wiese
2019-03-29 11:31:15 +08:00
parent 15e6a76e6f
commit 43f1c8e23c
4 changed files with 92 additions and 28 deletions
+16 -20
View File
@@ -86,23 +86,6 @@ define('BADGE_CRITERIA_TYPE_COHORT', 8);
*/
define('BADGE_CRITERIA_TYPE_COMPETENCY', 9);
/*
* Criteria type constant to class name mapping
*/
global $BADGE_CRITERIA_TYPES;
$BADGE_CRITERIA_TYPES = array(
BADGE_CRITERIA_TYPE_OVERALL => 'overall',
BADGE_CRITERIA_TYPE_ACTIVITY => 'activity',
BADGE_CRITERIA_TYPE_MANUAL => 'manual',
BADGE_CRITERIA_TYPE_SOCIAL => 'social',
BADGE_CRITERIA_TYPE_COURSE => 'course',
BADGE_CRITERIA_TYPE_COURSESET => 'courseset',
BADGE_CRITERIA_TYPE_PROFILE => 'profile',
BADGE_CRITERIA_TYPE_BADGE => 'badge',
BADGE_CRITERIA_TYPE_COHORT => 'cohort',
BADGE_CRITERIA_TYPE_COMPETENCY => 'competency',
);
/**
* Award criteria abstract definition
*
@@ -168,13 +151,17 @@ abstract class award_criteria {
* @return award_criteria
*/
public static function build($params) {
global $CFG, $BADGE_CRITERIA_TYPES;
global $CFG;
if (!isset($params['criteriatype']) || !isset($BADGE_CRITERIA_TYPES[$params['criteriatype']])) {
require_once($CFG->libdir . '/badgeslib.php');
$types = badges_list_criteria(false);
if (!isset($params['criteriatype']) || !isset($types[$params['criteriatype']])) {
print_error('error:invalidcriteriatype', 'badges');
}
$class = 'award_criteria_' . $BADGE_CRITERIA_TYPES[$params['criteriatype']];
$class = 'award_criteria_' . $types[$params['criteriatype']];
require_once($CFG->dirroot . '/badges/criteria/' . $class . '.php');
return new $class($params);
@@ -501,4 +488,13 @@ abstract class award_criteria {
}
}
}
/**
* Allow some specific criteria types to be disabled based on config.
*
* @return boolean
*/
public static function is_enabled() {
return true;
}
}
+32 -8
View File
@@ -37,6 +37,9 @@ class award_criteria_competency extends award_criteria {
/* @var int Criteria [BADGE_CRITERIA_TYPE_COMPETENCY] */
public $criteriatype = BADGE_CRITERIA_TYPE_COMPETENCY;
public $required_param = 'competency';
public $optional_params = [];
public $required_param = 'competency';
public $self_validation = true;
@@ -56,10 +59,14 @@ class award_criteria_competency extends award_criteria {
if ($short) {
$competency->set('description', '');
}
if ($pluginsfunction = get_plugins_with_function('render_competency_summary')) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$output[] = $pluginfunction($competency, $competency->get_framework(), !$short, !$short);
if (!self::is_enabled()) {
$output[] = get_string('competenciesarenotenabled', 'core_competency');
} else {
if ($pluginsfunction = get_plugins_with_function('render_competency_summary')) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$output[] = $pluginfunction($competency, $competency->get_framework(), !$short, !$short);
}
}
}
}
@@ -101,11 +108,12 @@ class award_criteria_competency extends award_criteria {
if ($pluginsfunction = get_plugins_with_function('competency_picker')) {
foreach ($pluginsfunction as $plugintype => $plugins) {
foreach ($plugins as $pluginfunction) {
$output[] = $pluginfunction($mform, $courseid, $context, 'competency');
$output[] = $pluginfunction($mform, $courseid, $context, 'competency_competencies');
}
}
}
$mform->getElement('competency')->setValue($competencies);
$mform->getElement('competency_competencies')->setValue($competencies);
$mform->addRule('competency_competencies', get_string('requiredcompetency', 'badges'), 'required');
// Add aggregation.
if (!$none) {
@@ -130,8 +138,8 @@ class award_criteria_competency extends award_criteria {
* @param array $params Values from the form or any other array.
*/
public function save($params = array()) {
$competencies = $params['competency'];
unset($params['competency']);
$competencies = $params['competency_competencies'];
unset($params['competency_competencies']);
if (is_string($competencies)) {
$competencies = explode(',', $competencies);
}
@@ -156,6 +164,9 @@ class award_criteria_competency extends award_criteria {
$overall = false;
$competencyids = [];
if (!self::is_enabled()) {
return false;
}
foreach ($this->params as $param) {
$competencyids[] = $param['competency'];
}
@@ -208,6 +219,10 @@ class award_criteria_competency extends award_criteria {
$badge = $DB->get_record('badge', array('id' => $this->badgeid));
if (!self::is_enabled()) {
return array($join, $where, $params);
}
if ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) {
// User has received ANY of the required competencies (we can use an in or equals list).
foreach ($this->params as $param) {
@@ -245,4 +260,13 @@ class award_criteria_competency extends award_criteria {
}
return array($join, $where, $params);
}
/**
* Hide this criteria when competencies are disabled.
*
* @return boolean
*/
public static function is_enabled() {
return \core_competency\api::is_enabled();
}
}
+1
View File
@@ -467,6 +467,7 @@ $string['relative'] = 'Relative date';
$string['relatedbages'] = 'Related badges';
$string['revoke'] = 'Revoke badge';
$string['requiredcohort'] = 'At least one cohort should be added to the cohort criterion.';
$string['requiredcompetency'] = 'At least one competency should be added to the competency criterion.';
$string['requiredcourse'] = 'At least one course should be added to the courseset criterion.';
$string['requiredbadge'] = 'At least one badge should be added to the badge criterion.';
$string['reviewbadge'] = 'Changes in badge access';
+43
View File
@@ -218,6 +218,7 @@ class badge {
* @return array
*/
public function get_accepted_criteria() {
global $CFG;
$criteriatypes = array();
if ($this->type == BADGE_TYPE_COURSE) {
@@ -240,6 +241,12 @@ class badge {
BADGE_CRITERIA_TYPE_COMPETENCY
);
}
$alltypes = badges_list_criteria();
foreach ($criteriatypes as $index => $type) {
if (!isset($alltypes[$type])) {
unset($criteriatypes[$index]);
}
}
return $criteriatypes;
}
@@ -1535,3 +1542,39 @@ function badges_setup_backpack_js() {
$PAGE->requires->js('/badges/backpack.js', true);
}
}
/**
* Return all the enabled criteria types for this site.
*
* @return array
*/
function badges_list_criteria($enabled = true) {
global $CFG;
$types = array(
BADGE_CRITERIA_TYPE_OVERALL => 'overall',
BADGE_CRITERIA_TYPE_ACTIVITY => 'activity',
BADGE_CRITERIA_TYPE_MANUAL => 'manual',
BADGE_CRITERIA_TYPE_SOCIAL => 'social',
BADGE_CRITERIA_TYPE_COURSE => 'course',
BADGE_CRITERIA_TYPE_COURSESET => 'courseset',
BADGE_CRITERIA_TYPE_PROFILE => 'profile',
BADGE_CRITERIA_TYPE_BADGE => 'badge',
BADGE_CRITERIA_TYPE_COHORT => 'cohort',
BADGE_CRITERIA_TYPE_COMPETENCY => 'competency',
);
if ($enabled) {
foreach ($types as $key => $type) {
$class = 'award_criteria_' . $type;
$file = $CFG->dirroot . '/badges/criteria/' . $class . '.php';
if (file_exists($file)) {
require_once($file);
if (!$class::is_enabled()) {
unset($types[$key]);
}
}
}
}
return $types;
}