Commit a569a330 authored by Damyon Wiese's avatar Damyon Wiese Committed by jun

Merge branch 'MDL-59875_m35v1' of https://github.com/sbourget/moodle

parents 1b98e553 a7370e0d
......@@ -106,6 +106,37 @@ class core_badges_observer {
}
}
/**
* Triggered when 'badge_awarded' event happens.
*
* @param \core\event\badge_awarded $event event generated when a badge is awarded.
*/
public static function badge_criteria_review(\core\event\badge_awarded $event) {
global $DB, $CFG;
if (!empty($CFG->enablebadges)) {
require_once($CFG->dirroot.'/lib/badgeslib.php');
$userid = $event->relateduserid;
if ($rs = $DB->get_records('badge_criteria', array('criteriatype' => BADGE_CRITERIA_TYPE_BADGE))) {
foreach ($rs as $r) {
$badge = new badge($r->badgeid);
if (!$badge->is_active() || $badge->is_issued($userid)) {
continue;
}
if ($badge->criteria[BADGE_CRITERIA_TYPE_BADGE]->review($userid)) {
$badge->criteria[BADGE_CRITERIA_TYPE_BADGE]->mark_complete($userid);
if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
$badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
$badge->issue($userid);
}
}
}
}
}
}
/**
* Triggered when 'user_updated' event happens.
*
......
......@@ -68,6 +68,12 @@ define('BADGE_CRITERIA_TYPE_COURSESET', 5);
*/
define('BADGE_CRITERIA_TYPE_PROFILE', 6);
/*
* Badge completion criteria type
* Criteria type constant, primarily for storing criteria type in the database.
*/
define('BADGE_CRITERIA_TYPE_BADGE', 7);
/*
* Criteria type constant to class name mapping
*/
......@@ -79,7 +85,8 @@ $BADGE_CRITERIA_TYPES = array(
BADGE_CRITERIA_TYPE_SOCIAL => 'social',
BADGE_CRITERIA_TYPE_COURSE => 'course',
BADGE_CRITERIA_TYPE_COURSESET => 'courseset',
BADGE_CRITERIA_TYPE_PROFILE => 'profile'
BADGE_CRITERIA_TYPE_PROFILE => 'profile',
BADGE_CRITERIA_TYPE_BADGE => 'badge',
);
/**
......
This diff is collapsed.
......@@ -4,6 +4,71 @@ Feature: Award badges
As an admin
I need to add criteria to badges in the system
@javascript
Scenario: Award badge on other badges as criteria
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "courses" exist:
| fullname | shortname | category | groupmode |
| Course 1 | C1 | 0 | 1 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And I log in as "teacher1"
And I am on "Course 1" course homepage
# Create course badge 1.
And I navigate to "Add a new badge" node in "Course administration > Badges"
And I follow "Add a new badge"
And I set the following fields to these values:
| Name | Course Badge 1 |
| Description | Course badge 1 description |
| issuername | Tester of course 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 "Manual issue by role"
And I expand all fieldsets
# Set to ANY of the roles awards badge.
And I set the field "Teacher" to "1"
And I set the field "Any of the selected roles awards the badge" to "1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
# Badge #2
And I navigate to "Add a new badge" node in "Course administration > Badges"
And I follow "Add a new badge"
And I set the following fields to these values:
| Name | Course Badge 2 |
| Description | Course badge 2 description |
| issuername | Tester of course badge |
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
# Set "course badge 1" as criteria
And I set the field "type" to "Awarded badges"
And I set the field "id_badge_badges" to "Course Badge 1"
And I press "Save"
And I press "Enable access"
And I press "Continue"
And I follow "Manage badges"
And I follow "Course Badge 1"
And I follow "Recipients (0)"
And I press "Award badge"
# Award course badge 1 to student 1.
And I set the field "potentialrecipients[]" to "Student 1 (student1@example.com)"
When I press "Award badge"
And I follow "Course Badge 1"
And I follow "Recipients (1)"
Then I should see "Recipients (1)"
And I log out
# Student 1 should have both badges.
And I log in as "student1"
And I follow "Profile" in the user menu
When I click on "Course 1" "link" in the "region-main" "region"
Then I should see "Course Badge 1"
And I should see "Course Badge 2"
@javascript
Scenario: Award profile badge
Given I log in as "admin"
......
......@@ -27,6 +27,8 @@
$string['actions'] = 'Actions';
$string['activate'] = 'Enable access';
$string['activatesuccess'] = 'Access to the badges was successfully enabled.';
$string['addbadge'] = 'Add a badge as criteria';
$string['addbadge_help'] = 'Select all badges that should be added to this badge requirement. Hold CTRL key to select multiple items.';
$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.';
......@@ -39,6 +41,7 @@ $string['aggregationmethod'] = 'Aggregation method';
$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['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';
......@@ -51,6 +54,7 @@ Note: It is recommended to leave this option disabled if the website cannot be a
$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['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';
......@@ -176,22 +180,26 @@ $string['criteria_descr_short2'] = 'Awarded by <strong>{$a}</strong> of: ';
$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_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_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_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:';
$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_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.';
......@@ -205,6 +213,8 @@ $string['criteria_5'] = 'Completing a set of courses';
$string['criteria_5_help'] = 'Allows a badge to be awarded to users who have completed a set of courses. Each course can have additional parameters such as minimum grade and date of course completion. ';
$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['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';
......@@ -248,7 +258,9 @@ $string['error:invalidbadgeurl'] = 'Invalid badge issuer URL format.';
$string['error:invalidcriteriatype'] = 'Invalid criteria type.';
$string['error:invalidexpiredate'] = 'Expiry date has to be in the future.';
$string['error:invalidexpireperiod'] = 'Expiry period cannot be negative or equal 0.';
$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 available to be added as criteria. Make sure that your other badges are enabled ';
$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>';
......@@ -369,6 +381,7 @@ $string['recipientvalidationproblem'] = 'Current user cannot be verified as a re
$string['relative'] = 'Relative date';
$string['revoke'] = 'Revoke badge';
$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';
$string['reviewconfirm'] = '<p>This will make your badge visible to users and allow them to start earning it.</p>
......
......@@ -191,6 +191,7 @@ class badge {
BADGE_CRITERIA_TYPE_OVERALL,
BADGE_CRITERIA_TYPE_MANUAL,
BADGE_CRITERIA_TYPE_COURSE,
BADGE_CRITERIA_TYPE_BADGE,
BADGE_CRITERIA_TYPE_ACTIVITY
);
} else if ($this->type == BADGE_TYPE_SITE) {
......@@ -198,6 +199,7 @@ class badge {
BADGE_CRITERIA_TYPE_OVERALL,
BADGE_CRITERIA_TYPE_MANUAL,
BADGE_CRITERIA_TYPE_COURSESET,
BADGE_CRITERIA_TYPE_BADGE,
BADGE_CRITERIA_TYPE_PROFILE,
);
}
......
......@@ -46,6 +46,10 @@ $observers = array(
'eventname' => '\core\event\course_module_completion_updated',
'callback' => 'core_badges_observer::course_module_criteria_review',
),
array(
'eventname' => '\core\event\badge_awarded',
'callback' => 'core_badges_observer::badge_criteria_review',
),
array(
'eventname' => '\core\event\course_completed',
'callback' => 'core_badges_observer::course_criteria_review',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment