@@ -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