Merge branch 'MDL-55491-master' of git://github.com/sarjona/moodle

This commit is contained in:
David Monllao 2018-04-11 17:53:37 +02:00
commit 728b851eb5
8 changed files with 819 additions and 0 deletions

View File

@ -168,4 +168,49 @@ class core_badges_observer {
}
}
}
/**
* Triggered when the 'cohort_member_added' event happens.
*
* @param \core\event\cohort_member_added $event generated when a user is added to a cohort
*/
public static function cohort_criteria_review(\core\event\cohort_member_added $event) {
global $DB, $CFG;
if (!empty($CFG->enablebadges)) {
require_once($CFG->dirroot.'/lib/badgeslib.php');
$cohortid = $event->objectid;
$userid = $event->relateduserid;
// Get relevant badges.
$badgesql = "SELECT badgeid
FROM {badge_criteria_param} cp
JOIN {badge_criteria} c ON cp.critid = c.id
WHERE c.criteriatype = ?
AND cp.name = ?";
$badges = $DB->get_records_sql($badgesql, array(BADGE_CRITERIA_TYPE_COHORT, "cohort_{$cohortid}"));
if (empty($badges)) {
return;
}
foreach ($badges as $b) {
$badge = new badge($b->badgeid);
if (!$badge->is_active()) {
continue;
}
if ($badge->is_issued($userid)) {
continue;
}
if ($badge->criteria[BADGE_CRITERIA_TYPE_COHORT]->review($userid)) {
$badge->criteria[BADGE_CRITERIA_TYPE_COHORT]->mark_complete($userid);
if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
$badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
$badge->issue($userid);
}
}
}
}
}
}

View File

@ -74,6 +74,12 @@ define('BADGE_CRITERIA_TYPE_PROFILE', 6);
*/
define('BADGE_CRITERIA_TYPE_BADGE', 7);
/*
* Cohort criteria type
* Criteria type constant, primarily for storing criteria type in the database.
*/
define('BADGE_CRITERIA_TYPE_COHORT', 8);
/*
* Criteria type constant to class name mapping
*/
@ -87,6 +93,7 @@ $BADGE_CRITERIA_TYPES = array(
BADGE_CRITERIA_TYPE_COURSESET => 'courseset',
BADGE_CRITERIA_TYPE_PROFILE => 'profile',
BADGE_CRITERIA_TYPE_BADGE => 'badge',
BADGE_CRITERIA_TYPE_COHORT => 'cohort',
);
/**

View File

@ -0,0 +1,253 @@
<?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/>.
/**
* This file contains the cohort membership badge award criteria type class
*
* @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 <eugene@catalyst.net.nz>
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot.'/cohort/lib.php');
/**
* 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 <eugene@catalyst.net.nz>
*/
class award_criteria_cohort extends award_criteria {
/* @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 = '') {
global $DB, $OUTPUT;
$output = array();
foreach ($this->params as $p) {
$cohortname = $DB->get_field('cohort', 'name', array('id' => $p['cohort']));
if (!$cohortname) {
$str = $OUTPUT->error_text(get_string('error:nosuchcohort', 'badges'));
} else {
$str = html_writer::tag('b', '"' . $cohortname . '"');
}
$output[] = $str;
}
if ($short) {
return implode(', ', $output);
} else {
return html_writer::alist($output, array(), 'ul');
}
}
/**
* Add appropriate new criteria options to the form
*
* @param object $mform moodle form
*/
public function get_options(&$mform) {
global $DB;
$none = false;
$mform->addElement('header', 'first_header', $this->get_title());
$mform->addHelpButton('first_header', 'criteria_' . $this->criteriatype, 'badges');
// Get cohorts.
$cohorts = $DB->get_records_menu('cohort', array(), 'name ASC', 'id, name');
if (!empty($cohorts)) {
$select = array();
$selected = array();
foreach ($cohorts as $cid => $cohortname) {
$select[$cid] = format_string($cohortname, true);
}
if ($this->id !== 0) {
$selected = array_keys($this->params);
}
$settings = array('multiple' => 'multiple', 'size' => 20, 'class' => 'selectcohort');
$mform->addElement('select', 'cohort_cohorts', get_string('addcohort', 'badges'), $select, $settings);
$mform->addRule('cohort_cohorts', get_string('requiredcohort', 'badges'), 'required');
$mform->addHelpButton('cohort_cohorts', 'addcohort', 'badges');
if ($this->id !== 0) {
$mform->setDefault('cohort_cohorts', $selected);
}
} else {
$mform->addElement('static', 'nocohorts', '', get_string('error:nocohorts', 'badges'));
$none = true;
}
// Add aggregation.
if (!$none) {
$mform->addElement('header', 'aggregation', get_string('method', 'badges'));
$agg = array();
$agg[] =& $mform->createElement('radio', 'agg', '', get_string('allmethodcohort', 'badges'), 1);
$agg[] =& $mform->createElement('radio', 'agg', '', get_string('anymethodcohort', 'badges'), 2);
$mform->addGroup($agg, 'methodgr', '', array('<br/>'), false);
if ($this->id !== 0) {
$mform->setDefault('agg', $this->method);
} else {
$mform->setDefault('agg', BADGE_CRITERIA_AGGREGATION_ANY);
}
}
return array($none, get_string('noparamstoadd', 'badges'));
}
/**
* Save criteria records
*
* @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;
}
parent::save($params);
}
/**
* Review this criteria and decide if it has been completed
*
* @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;
$overall = false;
foreach ($this->params as $param) {
$cohort = $DB->get_record('cohort', array('id' => $param['cohort']));
// Extra check in case a cohort was deleted while badge is still active.
if (!$cohort) {
if ($this->method == BADGE_CRITERIA_AGGREGATION_ALL) {
return false;
} else {
continue;
}
}
if ($this->method == BADGE_CRITERIA_AGGREGATION_ALL) {
if (cohort_is_member($cohort->id, $userid)) {
$overall = true;
continue;
} else {
return false;
}
} else if ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) {
if (cohort_is_member($cohort->id, $userid)) {
return true;
} else {
$overall = false;
continue;
}
}
}
return $overall;
}
/**
* Checks criteria for any major problems.
*
* @return array A list containing status and an error message (if any).
*/
public function validate() {
global $DB;
$params = array_keys($this->params);
$method = ($this->method == BADGE_CRITERIA_AGGREGATION_ALL);
$singleparam = (count($params) == 1);
foreach ($params as $param) {
// Perform check if there only one parameter with any type of aggregation,
// Or there are more than one parameter with aggregation ALL.
if (($singleparam || $method) && !$DB->record_exists('cohort', array('id' => $param))) {
return array(false, get_string('error:invalidparamcohort', 'badges'));
}
}
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() {
$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);
}
}
}

View File

@ -481,6 +481,38 @@ class core_badges_badgeslib_testcase extends advanced_testcase {
$this->assertTrue($badge->is_issued($this->user->id));
}
/**
* Test badges observer when cohort_member_added event is fired.
*/
public function test_badges_observer_cohort_criteria_review() {
global $CFG;
require_once("$CFG->dirroot/cohort/lib.php");
$cohort = $this->getDataGenerator()->create_cohort();
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
$badge = new badge($this->badgeid);
$this->assertFalse($badge->is_issued($this->user->id));
// Set up the badge criteria.
$criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
$criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
$criteriaoverall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_COHORT, 'badgeid' => $badge->id));
$criteriaoverall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY, 'cohort_cohorts' => array('0' => $cohort->id)));
// Make the badge active.
$badge->set_status(BADGE_STATUS_ACTIVE);
// Add the user to the cohort.
cohort_add_member($cohort->id, $this->user->id);
// Verify that the badge was awarded.
$this->assertDebuggingCalled();
$this->assertTrue($badge->is_issued($this->user->id));
}
/**
* Test badges assertion generated when a badge is issued.
*/

View File

@ -0,0 +1,462 @@
@core @core_badges @_file_upload
Feature: Award badges based on cohort
In order to award badges to users based on their cohort membership
As an admin
I need to add cohort criteria to badges in the system
@javascript
Scenario: Award cohort membership badge for a member of a single cohort.
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user2 | CH2 |
And I log in as "admin"
And I navigate to "Add a new badge" node in "Site administration > Badges"
And I set the following fields to these values:
| Name | Site Badge |
| Description | Site badge description |
| issuername | Tester of site badge |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Cohort membership"
And I set the field "id_cohort_cohorts" to "One Cohort"
And I press "Save"
And I press "Enable access"
When I press "Continue"
Then I should see "Recipients (1)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
@javascript
Scenario: Award cohort membership badge for a member of all required cohorts.
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
| Three Cohort | CH3 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user1 | CH2 |
| user2 | CH1 |
| user2 | CH3 |
And I log in as "admin"
And I navigate to "Add a new badge" node in "Site administration > Badges"
And I set the following fields to these values:
| Name | Site Badge |
| Description | Site badge description |
| issuername | Tester of site badge |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Cohort membership"
And I expand all fieldsets
And I set the field "id_cohort_cohorts" to "One Cohort,Two Cohort"
And I set the field "id_agg_1" to "1"
And I press "Save"
When I press "Enable access"
And I press "Continue"
Then I should see "Recipients (1)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
@javascript
Scenario: Award cohort membership badge for a member of any required cohorts.
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
| Three Cohort | CH3 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
| user3 | third | User | third@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user1 | CH2 |
| user2 | CH1 |
| user2 | CH3 |
| user3 | CH2 |
| user3 | CH3 |
And I log in as "admin"
And I navigate to "Add a new badge" node in "Site administration > Badges"
And I set the following fields to these values:
| Name | Site Badge |
| Description | Site badge description |
| issuername | Tester of site badge |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Cohort membership"
And I set the field "id_cohort_cohorts" to "One Cohort"
And I press "Save"
When I press "Enable access"
And I press "Continue"
Then I should see "Recipients (2)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
And I log out
And I log in as "user2"
And I follow "Profile" in the user menu
And I should see "Site Badge"
@javascript
Scenario: Award badge based on a single cohort membership and other criteria.
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user2 | CH2 |
And I log in as "admin"
And I navigate to "Add a new badge" node in "Site administration > Badges"
And I set the following fields to these values:
| Name | Site Badge |
| Description | Site badge description |
| issuername | Tester of site badge |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Cohort membership"
And I set the field "id_cohort_cohorts" to "One Cohort"
And I press "Save"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
And I set the field "Manager" to "1"
And I set the field "Any of the selected roles awards the badge" to "1"
And I press "Save"
When I press "Enable access"
And I press "Continue"
And I follow "Recipients (0)"
And I press "Award badge"
And I set the field "potentialrecipients[]" to "First User (first@example.com)"
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Second User (second@example.com)"
And I press "Award badge"
And I follow "Site Badge"
Then I should see "Recipients (1)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
And I log out
And I log in as "user2"
And I follow "Profile" in the user menu
And I should not see "Site Badge"
@javascript
Scenario: Award badge based on a single cohort membership or other criteria.
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
| user3 | Third | User | third@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user2 | CH2 |
| user3 | CH2 |
And I log in as "admin"
And I navigate to "Add a new badge" node in "Site administration > Badges"
And I set the following fields to these values:
| Name | Site Badge |
| Description | Site badge description |
| issuername | Tester of site badge |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Cohort membership"
And I set the field "id_cohort_cohorts" to "One Cohort"
And I press "Save"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
And I set the field "Manager" to "1"
And I set the field "Any of the selected roles awards the badge" to "1"
And I press "Save"
And I set the field "update" to "Any"
When I press "Enable access"
And I press "Continue"
And I follow "Recipients (1)"
And I press "Award badge"
And I set the field "potentialrecipients[]" to "First User (first@example.com)"
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Second User (second@example.com)"
And I press "Award badge"
And I follow "Site Badge"
Then I should see "Recipients (2)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
And I log out
And I log in as "user2"
And I follow "Profile" in the user menu
And I should see "Site Badge"
And I log out
And I log in as "user3"
And I follow "Profile" in the user menu
And I should not see "Site Badge"
@javascript
Scenario: Award badge based on a multiple cohort membership or other criteria.
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
| user3 | third | User | third@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user1 | CH2 |
| user2 | CH2 |
| user2 | CH2 |
And I log in as "admin"
And I navigate to "Add a new badge" node in "Site administration > Badges"
And I set the following fields to these values:
| Name | Site Badge |
| Description | Site badge description |
| issuername | Tester of site badge |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Cohort membership"
And I set the field "id_cohort_cohorts" to "One Cohort"
And I press "Save"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
And I set the field "Manager" to "1"
And I set the field "Any of the selected roles awards the badge" to "1"
And I press "Save"
And I set the field "update" to "Any"
When I press "Enable access"
And I press "Continue"
And I follow "Recipients (1)"
And I press "Award badge"
And I set the field "potentialrecipients[]" to "First User (first@example.com)"
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Second User (second@example.com)"
And I press "Award badge"
And I follow "Site Badge"
Then I should see "Recipients (2)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
And I log out
And I log in as "user2"
And I follow "Profile" in the user menu
And I should see "Site Badge"
And I log out
And I log in as "user3"
And I follow "Profile" in the user menu
And I should not see "Site Badge"
@javascript
Scenario: Award badge based on a multiple cohort membership and other criteria.
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
| user3 | Third | User | third@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user1 | CH2 |
| user2 | CH1 |
| user3 | CH2 |
And I log in as "admin"
And I navigate to "Add a new badge" node in "Site administration > Badges"
And I set the following fields to these values:
| Name | Site Badge |
| Description | Site badge description |
| issuername | Tester of site badge |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Cohort membership"
And I expand all fieldsets
And I set the field "id_cohort_cohorts" to "One Cohort,Two Cohort"
And I set the field "id_agg_1" to "1"
And I press "Save"
And I set the field "type" to "Manual issue by role"
And I expand all fieldsets
And I set the field "Manager" to "1"
And I set the field "Any of the selected roles awards the badge" to "1"
And I press "Save"
And I set the field "update" to "All"
When I press "Enable access"
And I press "Continue"
And I follow "Recipients (0)"
And I press "Award badge"
And I set the field "potentialrecipients[]" to "First User (first@example.com)"
And I press "Award badge"
And I set the field "potentialrecipients[]" to "Second User (second@example.com)"
And I press "Award badge"
And I follow "Site Badge"
Then I should see "Recipients (1)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge"
And I log out
And I log in as "user2"
And I follow "Profile" in the user menu
And I should not see "Site Badge"
And I log out
And I log in as "user3"
And I follow "Profile" in the user menu
And I should not see "Site Badge"
@javascript
Scenario: Award multiple badges based on single cohort membership
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
| user3 | Third | User | third@example.com |
And the following "cohort members" exist:
| user | cohort |
| user1 | CH1 |
| user1 | CH2 |
| user2 | CH2 |
And I log in as "admin"
And I navigate to "Add a new badge" node in "Site administration > Badges"
And I set the following fields to these values:
| Name | Site Badge 1 |
| Description | Site badge description |
| issuername | Tester of site badge |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Cohort membership"
And I set the field "id_cohort_cohorts" to "One Cohort"
And I press "Save"
And I press "Enable access"
When I press "Continue"
And I should see "Recipients (1)"
And I navigate to "Add a new badge" node in "Site administration > Badges"
And I set the following fields to these values:
| Name | Site Badge 2 |
| Description | Site badge description |
| issuername | Tester of site badge |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Cohort membership"
And I set the field "id_cohort_cohorts" to "Two Cohort"
And I press "Save"
And I press "Enable access"
And I press "Continue"
Then I should see "Recipients (2)"
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge 1"
And I should see "Site Badge 2"
And I log out
And I log in as "user2"
And I follow "Profile" in the user menu
And I should not see "Site Badge 1"
And I should see "Site Badge 2"
And I log out
And I log in as "user3"
And I follow "Profile" in the user menu
And I should not see "Site Badge 1"
And I should not see "Site Badge 2"
@javascript
Scenario: Award multiple badges based on multiple cohort memberships
Given the following "cohorts" exist:
| name | idnumber |
| One Cohort | CH1 |
| Two Cohort | CH2 |
| Three Cohort | CH3 |
And the following "users" exist:
| username | firstname | lastname | email |
| user1 | First | User | first@example.com |
| user2 | Second | User | second@example.com |
| user3 | Third | User | third@example.com |
And I log in as "admin"
And I navigate to "Add a new badge" node in "Site administration > Badges"
And I set the following fields to these values:
| Name | Site Badge 1 |
| Description | Site badge description |
| issuername | Tester of site badge |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Cohort membership"
And I expand all fieldsets
And I set the field "id_cohort_cohorts" to "One Cohort,Two Cohort"
And I set the field "id_agg_1" to "1"
And I press "Save"
And I press "Enable access"
When I press "Continue"
And I should see "Recipients (0)"
And I navigate to "Add a new badge" node in "Site administration > Badges"
And I set the following fields to these values:
| Name | Site Badge 2 |
| Description | Site badge description |
| issuername | Tester of site badge |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Cohort membership"
And I expand all fieldsets
And I set the field "id_cohort_cohorts" to "Three Cohort,Two Cohort"
And I set the field "id_agg_1" to "1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
Then I navigate to "Cohorts" node in "Site administration > Users > Accounts"
And I add "First User (first@example.com)" user to "CH1" cohort members
And I add "First User (first@example.com)" user to "CH2" cohort members
And I add "Second User (second@example.com)" user to "CH2" cohort members
And I add "Second User (second@example.com)" user to "CH3" cohort members
And I log out
And I log in as "user1"
And I follow "Profile" in the user menu
And I should see "Site Badge 1"
And I should not see "Site Badge 2"
And I log out
And I log in as "user2"
And I follow "Profile" in the user menu
And I should not see "Site Badge 1"
And I should see "Site Badge 2"
And I log out
And I log in as "user3"
And I follow "Profile" in the user menu
And I should not see "Site Badge 1"
And I should not see "Site Badge 2"

View File

@ -32,6 +32,8 @@ $string['addbadge_help'] = 'Select all badges that should be added to this badge
$string['addbadgecriteria'] = 'Add badge criteria';
$string['addcriteria'] = 'Add criteria';
$string['addcriteriatext'] = 'To start adding criteria, please select one of the options from the drop-down menu.';
$string['addcohort'] = 'Add cohort';
$string['addcohort_help'] = 'Select all cohorts that should be added to this badge requirement. Hold CTRL key to select multiple items.';
$string['addcourse'] = 'Add courses';
$string['addcourse_help'] = 'Select all courses that should be added to this badge requirement. Hold CTRL key to select multiple items.';
$string['addtobackpack'] = 'Add to backpack';
@ -42,6 +44,7 @@ $string['all'] = 'All';
$string['allmethod'] = 'All of the selected conditions are met';
$string['allmethodactivity'] = 'All of the selected activities are complete';
$string['allmethodbadges'] = 'All of the selected badges have been earned';
$string['allmethodcohort'] = 'Membership in all the selected cohorts';
$string['allmethodcourseset'] = 'All of the selected courses are complete';
$string['allmethodmanual'] = 'All of the selected roles award the badge';
$string['allmethodprofile'] = 'All of the selected profile fields have been completed';
@ -55,6 +58,7 @@ $string['any'] = 'Any';
$string['anymethod'] = 'Any of the selected conditions is met';
$string['anymethodactivity'] = 'Any of the selected activities is complete';
$string['anymethodbadges'] = 'Any of the selected badges have been earned';
$string['anymethodcohort'] = 'Membership in any of the selected cohorts';
$string['anymethodcourseset'] = 'Any of the selected courses is complete';
$string['anymethodmanual'] = 'Any of the selected roles awards the badge';
$string['anymethodprofile'] = 'Any of the selected profile fields has been completed';
@ -181,18 +185,21 @@ $string['criteria_descr_short4'] = 'Complete the course ';
$string['criteria_descr_short5'] = 'Complete <strong>{$a}</strong> of: ';
$string['criteria_descr_short6'] = 'Complete <strong>{$a}</strong> of: ';
$string['criteria_descr_short7'] = 'Complete <strong>{$a}</strong> of: ';
$string['criteria_descr_short8'] = 'Cohort membership in <strong>{$a}</strong> of: ';
$string['criteria_descr_single_short1'] = 'Complete: ';
$string['criteria_descr_single_short2'] = 'Awarded by: ';
$string['criteria_descr_single_short4'] = 'Complete the course ';
$string['criteria_descr_single_short5'] = 'Complete: ';
$string['criteria_descr_single_short6'] = 'Complete: ';
$string['criteria_descr_single_short7'] = 'Complete: ';
$string['criteria_descr_single_short8'] = 'Membership in: ';
$string['criteria_descr_single_1'] = 'The following activity has to be completed:';
$string['criteria_descr_single_2'] = 'This badge has to be awarded by a user with the following role:';
$string['criteria_descr_single_4'] = 'Users must complete the course';
$string['criteria_descr_single_5'] = 'The following course has to be completed:';
$string['criteria_descr_single_6'] = 'The following user profile field has to be completed:';
$string['criteria_descr_single_7'] = 'The following badge has to be earned:';
$string['criteria_descr_single_8'] = 'Membership in the following cohort is required:';
$string['criteria_descr_0'] = 'Users are awarded this badge when they complete <strong>{$a}</strong> of the listed requirements.';
$string['criteria_descr_1'] = '<strong>{$a}</strong> of the following activities are completed:';
$string['criteria_descr_2'] = 'This badge has to be awarded by the users with <strong>{$a}</strong> of the following roles:';
@ -200,6 +207,7 @@ $string['criteria_descr_4'] = 'Users must complete the course';
$string['criteria_descr_5'] = '<strong>{$a}</strong> of the following courses have to be completed:';
$string['criteria_descr_6'] = '<strong>{$a}</strong> of the following user profile fields have to be completed:';
$string['criteria_descr_7'] = '<strong>{$a}</strong> of the following badges have to be earned:';
$string['criteria_descr_8'] = 'Membership in <strong>{$a}</strong> of the following cohorts is required:';
$string['criteria_0'] = 'This badge is awarded when...';
$string['criteria_1'] = 'Activity completion';
$string['criteria_1_help'] = 'Allows a badge to be awarded to users based on the completion of a set of activities within a course.';
@ -215,6 +223,8 @@ $string['criteria_6'] = 'Profile completion';
$string['criteria_6_help'] = 'Allows a badge to be awarded to users for completing certain fields in their profile. You can select from default and custom profile fields that are available to users. ';
$string['criteria_7'] = 'Awarded badges';
$string['criteria_7_help'] = 'Allows a badge to be awarded to users based on the other badges thay have earned.';
$string['criteria_8'] = 'Cohort membership';
$string['criteria_8_help'] = 'Allows a badge to be awarded to users for becoming a member of certian cohorts.';
$string['criterror'] = 'Current parameters issues';
$string['criterror_help'] = 'This fieldset shows all parameters that were initially added to this badge requirement but are no longer available. It is recommended that you un-check such parameters to make sure that users can earn this badge in the future.';
$string['currentimage'] = 'Current image';
@ -261,11 +271,15 @@ $string['error:invalidexpireperiod'] = 'Expiry period cannot be negative or equa
$string['error:invalidparambadge'] = 'Badge does not exist. ';
$string['error:noactivities'] = 'There are no activities with completion criteria enabled in this course.';
$string['error:nobadges'] = 'There are no course or site badges with access enabled to be added as criteria.';
$string['error:invalidparamcohort'] = 'Cohort does not exist. ';
$string['error:noactivities'] = 'There are no activities with completion criteria enabled in this course.';
$string['error:nocohorts'] = 'No cohorts';
$string['error:nocourses'] = 'Course completion is not enabled for any of the courses in this site, so none can be displayed. Course completion may be enabled in the course settings.';
$string['error:nogroups'] = '<p>There are no public collections of badges available in your backpack. </p>
<p>Only public collections are shown, <a href="http://backpack.openbadges.org">visit your backpack</a> to create some public collections.</p>';
$string['error:nopermissiontoview'] = 'You have no permissions to view badge recipients';
$string['error:nosuchbadge'] = 'Badge with id {$a} does not exist.';
$string['error:nosuchcohort'] = 'Warning: This cohort is no longer available.';
$string['error:nosuchcourse'] = 'Warning: This course is no longer available.';
$string['error:nosuchfield'] = 'Warning: This user profile field is no longer available.';
$string['error:nosuchmod'] = 'Warning: This activity is no longer available.';
@ -380,6 +394,7 @@ $string['recipientidentificationproblem'] = 'Cannot find a recipient of this bad
$string['recipientvalidationproblem'] = 'Current user cannot be verified as a recipient of this badge.';
$string['relative'] = 'Relative date';
$string['revoke'] = 'Revoke badge';
$string['requiredcohort'] = 'At least one cohort should be added to the cohort 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';

View File

@ -201,6 +201,7 @@ class badge {
BADGE_CRITERIA_TYPE_COURSESET,
BADGE_CRITERIA_TYPE_BADGE,
BADGE_CRITERIA_TYPE_PROFILE,
BADGE_CRITERIA_TYPE_COHORT,
);
}

View File

@ -58,6 +58,10 @@ $observers = array(
'eventname' => '\core\event\user_updated',
'callback' => 'core_badges_observer::profile_criteria_review',
),
array(
'eventname' => '\core\event\cohort_member_added',
'callback' => 'core_badges_observer::cohort_criteria_review',
),
// Competencies.
array(