Skip to content

Commit 4105915

Browse files
committed
Fix github issue #316 regarding a bad dataset with groupingid=0 after backup and restore without groups/groupings
1 parent 3798407 commit 4105915

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

backup/moodle2/restore_ratingallocate_activity_stepslib.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,9 @@ protected function process_ratingallocate_group_choices($data) {
160160
$data->choiceid = $this->get_new_parentid(this_db\ratingallocate_choices::TABLE);
161161
if ((int) $data->groupid !== 0) {
162162
$data->groupid = $this->get_mappingid('group', $data->groupid);
163+
$newitemid = $DB->insert_record(this_db\ratingallocate_group_choices::TABLE, $data);
164+
$this->set_mapping(this_db\ratingallocate_group_choices::TABLE, $oldid, $newitemid);
163165
}
164-
165-
$newitemid = $DB->insert_record(this_db\ratingallocate_group_choices::TABLE, $data);
166-
$this->set_mapping(this_db\ratingallocate_group_choices::TABLE, $oldid, $newitemid);
167166
}
168167

169168
/**
@@ -180,10 +179,9 @@ protected function process_ratingallocate_ch_gengroups($data) {
180179
$data->choiceid = $this->get_new_parentid(this_db\ratingallocate_choices::TABLE);
181180
if ((int) $data->groupid !== 0) {
182181
$data->groupid = $this->get_mappingid('group', $data->groupid);
182+
$newitemid = $DB->insert_record(this_db\ratingallocate_ch_gengroups::TABLE, $data);
183+
$this->set_mapping(this_db\ratingallocate_ch_gengroups::TABLE, $oldid, $newitemid);
183184
}
184-
185-
$newitemid = $DB->insert_record(this_db\ratingallocate_ch_gengroups::TABLE, $data);
186-
$this->set_mapping(this_db\ratingallocate_ch_gengroups::TABLE, $oldid, $newitemid);
187185
}
188186

189187
/**
@@ -200,10 +198,9 @@ protected function process_ratingallocate_groupings($data) {
200198
$data->ratingallocateid = $this->get_new_parentid(this_db\ratingallocate::TABLE);
201199
if ((int) $data->groupingid !== 0) {
202200
$data->groupingid = $this->get_mappingid('grouping', $data->groupingid);
201+
$newitemid = $DB->insert_record(this_db\ratingallocate_groupings::TABLE, $data);
202+
$this->set_mapping(this_db\ratingallocate_groupings::TABLE, $oldid, $newitemid);
203203
}
204-
205-
$newitemid = $DB->insert_record(this_db\ratingallocate_groupings::TABLE, $data);
206-
$this->set_mapping(this_db\ratingallocate_groupings::TABLE, $oldid, $newitemid);
207204
}
208205

209206
/**

locallib.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,24 +1468,26 @@ public function distrubute_choices() {
14681468
* @throws required_capability_exception
14691469
*/
14701470
public function synchronize_allocation_and_grouping() {
1471+
global $DB;
1472+
14711473
require_capability('moodle/course:managegroups', $this->context);
14721474

1475+
// Fix for github bug issue #316. Delete bad dataset with groupingid=0 after backup and restore without groups/groupings.
1476+
$where = "ratingallocateid = $this->ratingallocateid AND groupingid = 0";
1477+
$DB->delete_records_select(this_db\ratingallocate_groupings::TABLE, $where);
14731478
// Search if there is already a grouping from us.
1474-
if (!$groupingids = $this->db->get_record(this_db\ratingallocate_groupings::TABLE,
1475-
['ratingallocateid' => $this->ratingallocateid],
1476-
'groupingid')) {
1479+
$where = "ratingallocateid = $this->ratingallocateid AND groupingid <> 0";
1480+
if (!$groupingids = $this->db->get_record_select(this_db\ratingallocate_groupings::TABLE, $where, [], 'groupingid')) {
14771481
// Create grouping.
14781482
$data = new stdClass();
14791483
$data->name = get_string('groupingname', RATINGALLOCATE_MOD_NAME, $this->ratingallocate->name);
14801484
$data->courseid = $this->course->id;
14811485
$groupingid = groups_create_grouping($data);
1482-
14831486
// Insert groupingid and ratingallocateid into the table.
14841487
$data = new stdClass();
14851488
$data->groupingid = $groupingid;
14861489
$data->ratingallocateid = $this->ratingallocateid;
14871490
$this->db->insert_record(this_db\ratingallocate_groupings::TABLE, $data);
1488-
14891491
} else {
14901492
// If there is already a grouping for this allocation assign the corresponing id to groupingid.
14911493
$groupingid = $groupingids->groupingid;
@@ -1497,22 +1499,17 @@ public function synchronize_allocation_and_grouping() {
14971499
foreach ($choices as $choice) {
14981500
if ($this->db->record_exists(this_db\ratingallocate_choices::TABLE,
14991501
['id' => $choice->id])) {
1500-
15011502
// Checks if there is already a group for this choice.
1502-
1503-
if ($groupids = $this->db->get_record(this_db\ratingallocate_ch_gengroups::TABLE,
1504-
['choiceid' => $choice->id],
1505-
'groupid')) {
1506-
1503+
$where = "choiceid = $choice->id AND groupid <> 0";
1504+
if ($groupids = $this->db->get_record_select(this_db\ratingallocate_ch_gengroups::TABLE,
1505+
$where, [], 'groupid')) {
15071506
$groupid = $groupids->groupid;
15081507
$group = groups_get_group($groupid);
1509-
15101508
// Delete all the members from the existing group for this choice.
15111509
if ($group) {
15121510
groups_delete_group_members_by_group($group->id);
15131511
groups_assign_grouping($groupingid, $group->id);
15141512
}
1515-
15161513
} else {
15171514
// If the group for this choice does not exist yet, create it.
15181515
$data = new stdClass();
@@ -1521,29 +1518,27 @@ public function synchronize_allocation_and_grouping() {
15211518
$createdid = groups_create_group($data);
15221519
if ($createdid) {
15231520
groups_assign_grouping($groupingid, $createdid);
1524-
15251521
// Insert the mapping between group and choice into the Table.
15261522
$this->db->insert_record(this_db\ratingallocate_ch_gengroups::TABLE,
15271523
['choiceid' => $choice->id, 'groupid' => $createdid]);
15281524
}
15291525
}
15301526
}
15311527
}
1532-
15331528
// Add all participants in the correct group.
15341529
$allocations = $this->get_allocations();
15351530
foreach ($allocations as $allocation) {
15361531
$choiceid = $allocation->choiceid;
15371532
$userid = $allocation->userid;
1538-
15391533
// Get the group corresponding to the choiceid.
15401534
$groupids = $this->db->get_record(this_db\ratingallocate_ch_gengroups::TABLE,
15411535
['choiceid' => $choiceid],
15421536
'groupid');
1543-
$groupid = $groupids->groupid;
1544-
$group = groups_get_group($groupid);
1545-
if ($group) {
1546-
groups_add_member($group, $userid);
1537+
if ($groupid = $groupids->groupid) {
1538+
$group = groups_get_group($groupid);
1539+
if ($group) {
1540+
groups_add_member($group, $userid);
1541+
}
15471542
}
15481543
}
15491544
// Invalidate the grouping cache for the course.

0 commit comments

Comments
 (0)