Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-39558 badges: Add backup/restore of manually awarded badges

  • Loading branch information...
commit 18811be3783d5ff3eb886e35b4be1fe07b3b6423 1 parent 56d54e1
@ybozhko ybozhko authored damyon committed
View
12 backup/moodle2/backup_stepslib.php
@@ -844,6 +844,10 @@ protected function define_structure() {
$parameter = new backup_nested_element('parameter', array('id'), array('critid',
'name', 'value', 'criteriatype'));
+ $manual_awards = new backup_nested_element('manual_awards');
+ $manual_award = new backup_nested_element('manual_award', array('id'), array('badgeid',
+ 'recipientid', 'issuerid', 'issuerrole', 'datemet'));
+
// Build the tree.
$badges->add_child($badge);
@@ -851,6 +855,8 @@ protected function define_structure() {
$criteria->add_child($criterion);
$criterion->add_child($parameters);
$parameters->add_child($parameter);
+ $badge->add_child($manual_awards);
+ $manual_awards->add_child($manual_award);
// Define sources.
@@ -864,6 +870,8 @@ protected function define_structure() {
$parameterparams = array('critid' => backup::VAR_PARENTID);
$parameter->set_source_sql($parametersql, $parameterparams);
+ $manual_award->set_source_table('badge_manual_award', array('badgeid' => backup::VAR_PARENTID));
+
// Define id annotations.
$badge->annotate_ids('user', 'usercreated');
@@ -871,6 +879,10 @@ protected function define_structure() {
$criterion->annotate_ids('badge', 'badgeid');
$parameter->annotate_ids('criterion', 'critid');
$badge->annotate_files('badges', 'badgeimage', 'id');
+ $manual_award->annotate_ids('badge', 'badgeid');
+ $manual_award->annotate_ids('user', 'recipientid');
+ $manual_award->annotate_ids('user', 'issuerid');
+ $manual_award->annotate_ids('role', 'issuerrole');
// Return the root element ($badges).
return $badges;
View
4 backup/moodle2/restore_final_task.class.php
@@ -67,9 +67,7 @@ public function build() {
}
// Conditionally restore course badges.
- if ($this->get_setting_value('badges') &&
- $this->get_target() !== backup::TARGET_CURRENT_ADDING &&
- $this->get_target() !== backup::TARGET_EXISTING_ADDING) {
+ if ($this->get_setting_value('badges')) {
$this->add_step(new restore_badges_structure_step('course_badges', 'badges.xml'));
}
View
1  backup/moodle2/restore_root_task.class.php
@@ -193,6 +193,7 @@ protected function define_settings() {
$badges->get_ui()->set_changeable($changeable);
$this->add_setting($badges);
$activities->add_dependency($badges);
+ $users->add_dependency($badges);
// Define Calendar events (dependent of users)
$defaultvalue = false; // Safer default
View
33 backup/moodle2/restore_stepslib.php
@@ -2007,6 +2007,7 @@ protected function define_structure() {
$paths[] = new restore_path_element('badge', '/badges/badge');
$paths[] = new restore_path_element('criterion', '/badges/badge/criteria/criterion');
$paths[] = new restore_path_element('parameter', '/badges/badge/criteria/criterion/parameters/parameter');
+ $paths[] = new restore_path_element('manual_award', '/badges/badge/manual_awards/manual_award');
return $paths;
}
@@ -2014,7 +2015,7 @@ protected function define_structure() {
public function process_badge($data) {
global $DB, $CFG;
- require_once $CFG->libdir . '/badgeslib.php';
+ require_once($CFG->libdir . '/badgeslib.php');
$data = (object)$data;
$data->usercreated = $this->get_mappingid('user', $data->usercreated);
@@ -2068,7 +2069,8 @@ public function process_criterion($data) {
public function process_parameter($data) {
global $DB, $CFG;
- require_once $CFG->libdir . '/badgeslib.php';
+
+ require_once($CFG->libdir . '/badgeslib.php');
$data = (object)$data;
$criteriaid = $this->get_new_parentid('criterion');
@@ -2087,8 +2089,13 @@ public function process_parameter($data) {
$params['name'] = $oldparam[0] . '_' . $this->get_courseid();
$params['value'] = $oldparam[0] == 'course' ? $this->get_courseid() : $data->value;
} else if ($data->criteriatype == BADGE_CRITERIA_TYPE_MANUAL) {
- $params['name'] = $data->name;
- $params['value'] = $data->value;
+ $role = $this->get_mappingid('role', $data->value);
+ if (!empty($role)) {
+ $params['name'] = 'role_' . $role;
+ $params['value'] = $role;
+ } else {
+ return;
+ }
}
if (!$DB->record_exists('badge_criteria_param', $params)) {
@@ -2096,6 +2103,24 @@ public function process_parameter($data) {
}
}
+ public function process_manual_award($data) {
+ global $DB;
+
+ $data = (object)$data;
+ $role = $this->get_mappingid('role', $data->issuerrole);
+
+ if (!empty($role)) {
+ $award = array(
+ 'badgeid' => $this->get_new_parentid('badge'),
+ 'recipientid' => $this->get_mappingid('user', $data->recipientid),
+ 'issuerid' => $this->get_mappingid('user', $data->issuerid),
+ 'issuerrole' => $role,
+ 'datemet' => $this->apply_date_offset($data->datemet)
+ );
+ $DB->insert_record('badge_manual_award', $award);
+ }
+ }
+
protected function after_execute() {
// Add related files.
$this->add_related_files('badges', 'badgeimage', 'badge');
Please sign in to comment.
Something went wrong with that request. Please try again.