Skip to content

Commit

Permalink
MDL-63876 badges: Allow criteria to be optional
Browse files Browse the repository at this point in the history
Competencies criteria type is only added if competencies are enabled.
  • Loading branch information
Damyon Wiese committed Mar 29, 2019
1 parent 15e6a76 commit 43f1c8e
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 28 deletions.
36 changes: 16 additions & 20 deletions badges/criteria/award_criteria.php
Expand Up @@ -86,23 +86,6 @@
*/
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
*
Expand Down Expand Up @@ -168,13 +151,17 @@ public function __construct($params) {
* @return award_criteria
*/
public static function build($params) {
global $CFG, $BADGE_CRITERIA_TYPES;
global $CFG;

require_once($CFG->libdir . '/badgeslib.php');

$types = badges_list_criteria(false);

if (!isset($params['criteriatype']) || !isset($BADGE_CRITERIA_TYPES[$params['criteriatype']])) {
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);
Expand Down Expand Up @@ -501,4 +488,13 @@ public function make_clone($newbadgeid) {
}
}
}

/**
* Allow some specific criteria types to be disabled based on config.
*
* @return boolean
*/
public static function is_enabled() {
return true;
}
}
40 changes: 32 additions & 8 deletions badges/criteria/award_criteria_competency.php
Expand Up @@ -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;
Expand All @@ -56,10 +59,14 @@ public function get_details($short = '') {
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);
}
}
}
}
Expand Down Expand Up @@ -101,11 +108,12 @@ public function get_options(&$mform) {
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) {
Expand All @@ -130,8 +138,8 @@ public function get_options(&$mform) {
* @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);
}
Expand All @@ -156,6 +164,9 @@ public function review($userid, $filtered = false) {
$overall = false;
$competencyids = [];

if (!self::is_enabled()) {
return false;
}
foreach ($this->params as $param) {
$competencyids[] = $param['competency'];
}
Expand Down Expand Up @@ -208,6 +219,10 @@ public function get_completed_criteria_sql() {

$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) {
Expand Down Expand Up @@ -245,4 +260,13 @@ public function get_completed_criteria_sql() {
}
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 change: 1 addition & 0 deletions lang/en/badges.php
Expand Up @@ -467,6 +467,7 @@
$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';
Expand Down
43 changes: 43 additions & 0 deletions lib/badgeslib.php
Expand Up @@ -218,6 +218,7 @@ public static function get_aggregation_methods() {
* @return array
*/
public function get_accepted_criteria() {
global $CFG;
$criteriatypes = array();

if ($this->type == BADGE_TYPE_COURSE) {
Expand All @@ -240,6 +241,12 @@ public function get_accepted_criteria() {
BADGE_CRITERIA_TYPE_COMPETENCY
);
}
$alltypes = badges_list_criteria();
foreach ($criteriatypes as $index => $type) {
if (!isset($alltypes[$type])) {
unset($criteriatypes[$index]);
}
}

return $criteriatypes;
}
Expand Down Expand Up @@ -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;
}

0 comments on commit 43f1c8e

Please sign in to comment.