Skip to content

Commit

Permalink
MDL-67186 group: add custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
TomoTsuyuki committed Aug 9, 2023
1 parent 1474f74 commit d8a4cc9
Show file tree
Hide file tree
Showing 29 changed files with 1,739 additions and 18 deletions.
21 changes: 21 additions & 0 deletions admin/settings/courses.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,27 @@
$CFG->wwwroot . '/course/pending.php', array('moodle/site:approvecourse')));
}

// Add a category for the Groups.
$ADMIN->add('courses', new admin_category('groups', new lang_string('groups')));
$ADMIN->add(
'groups',
new admin_externalpage(
'group_customfield',
new lang_string('group_customfield', 'admin'),
$CFG->wwwroot . '/group/customfield.php',
['moodle/group:configurecustomfields']
)
);
$ADMIN->add(
'groups',
new admin_externalpage(
'grouping_customfield',
new lang_string('grouping_customfield', 'admin'),
$CFG->wwwroot . '/group/grouping_customfield.php',
['moodle/group:configurecustomfields']
)
);

// Add a category for the Activity Chooser.
$ADMIN->add('courses', new admin_category('activitychooser', new lang_string('activitychoosercategory', 'course')));
$temp = new admin_settingpage('activitychoosersettings', new lang_string('activitychoosersettings', 'course'));
Expand Down
50 changes: 50 additions & 0 deletions backup/moodle2/backup_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,10 @@ protected function define_structure() {
'name', 'idnumber', 'description', 'descriptionformat', 'enrolmentkey',
'picture', 'visibility', 'participation', 'timecreated', 'timemodified'));

$groupcustomfields = new backup_nested_element('groupcustomfields');
$groupcustomfield = new backup_nested_element('groupcustomfield', ['id'], [
'shortname', 'type', 'value', 'valueformat', 'groupid']);

$members = new backup_nested_element('group_members');

$member = new backup_nested_element('group_member', array('id'), array(
Expand All @@ -1365,6 +1369,10 @@ protected function define_structure() {
'name', 'idnumber', 'description', 'descriptionformat', 'configdata',
'timecreated', 'timemodified'));

$groupingcustomfields = new backup_nested_element('groupingcustomfields');
$groupingcustomfield = new backup_nested_element('groupingcustomfield', ['id'], [
'shortname', 'type', 'value', 'valueformat', 'groupingid']);

$groupinggroups = new backup_nested_element('grouping_groups');

$groupinggroup = new backup_nested_element('grouping_group', array('id'), array(
Expand All @@ -1373,12 +1381,16 @@ protected function define_structure() {
// Build the tree

$groups->add_child($group);
$groups->add_child($groupcustomfields);
$groupcustomfields->add_child($groupcustomfield);
$groups->add_child($groupings);

$group->add_child($members);
$members->add_child($member);

$groupings->add_child($grouping);
$groupings->add_child($groupingcustomfields);
$groupingcustomfields->add_child($groupingcustomfield);
$grouping->add_child($groupinggroups);
$groupinggroups->add_child($groupinggroup);

Expand All @@ -1405,6 +1417,10 @@ protected function define_structure() {
if ($userinfo) {
$member->set_source_table('groups_members', array('groupid' => backup::VAR_PARENTID));
}

$courseid = $this->task->get_courseid();
$groupcustomfield->set_source_array($this->get_group_custom_fields_for_backup($courseid));
$groupingcustomfield->set_source_array($this->get_grouping_custom_fields_for_backup($courseid));
}

// Define id annotations (as final)
Expand All @@ -1420,6 +1436,40 @@ protected function define_structure() {
// Return the root element (groups)
return $groups;
}

/**
* Get custom fields array for group
* @param int $courseid
* @return array
*/
protected function get_group_custom_fields_for_backup(int $courseid): array {
global $DB;
$handler = \core_group\customfield\group_handler::create();
$fieldsforbackup = [];
if ($groups = $DB->get_records('groups', ['courseid' => $courseid], '', 'id')) {
foreach ($groups as $group) {
$fieldsforbackup = array_merge($fieldsforbackup, $handler->get_instance_data_for_backup($group->id));
}
}
return $fieldsforbackup;
}

/**
* Get custom fields array for grouping
* @param int $courseid
* @return array
*/
protected function get_grouping_custom_fields_for_backup(int $courseid): array {
global $DB;
$handler = \core_group\customfield\grouping_handler::create();
$fieldsforbackup = [];
if ($groupings = $DB->get_records('groupings', ['courseid' => $courseid], '', 'id')) {
foreach ($groupings as $grouping) {
$fieldsforbackup = array_merge($fieldsforbackup, $handler->get_instance_data_for_backup($grouping->id));
}
}
return $fieldsforbackup;
}
}

/**
Expand Down
27 changes: 27 additions & 0 deletions backup/moodle2/restore_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,10 @@ protected function define_structure() {
$groupinfo = $this->get_setting_value('groups');
if ($groupinfo) {
$paths[] = new restore_path_element('group', '/groups/group');
$paths[] = new restore_path_element('groupcustomfield', '/groups/groupcustomfields/groupcustomfield');
$paths[] = new restore_path_element('grouping', '/groups/groupings/grouping');
$paths[] = new restore_path_element('groupingcustomfield',
'/groups/groupings/groupingcustomfields/groupingcustomfield');
$paths[] = new restore_path_element('grouping_group', '/groups/groupings/grouping/grouping_groups/grouping_group');
}
return $paths;
Expand Down Expand Up @@ -1225,6 +1228,18 @@ public function process_group($data) {
cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($data->courseid));
}

/**
* Restore group custom field values.
* @param array $data data for group custom field
* @return void
*/
public function process_groupcustomfield($data) {
$newgroup = $this->get_mapping('group', $data['groupid']);
$data['groupid'] = $newgroup->newitemid;
$handler = \core_group\customfield\group_handler::create();
$handler->restore_instance_data_from_backup($this->task, $data);
}

public function process_grouping($data) {
global $DB;

Expand Down Expand Up @@ -1270,6 +1285,18 @@ public function process_grouping($data) {
cache_helper::invalidate_by_definition('core', 'groupdata', array(), array($data->courseid));
}

/**
* Restore grouping custom field values.
* @param array $data data for grouping custom field
* @return void
*/
public function process_groupingcustomfield($data) {
$newgroup = $this->get_mapping('grouping', $data['groupingid']);
$data['groupingid'] = $newgroup->newitemid;
$handler = \core_group\customfield\grouping_handler::create();
$handler->restore_instance_data_from_backup($this->task, $data);
}

public function process_grouping_group($data) {
global $CFG;

Expand Down
109 changes: 109 additions & 0 deletions backup/tests/backup_restore_group_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?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/>.

namespace core_backup;

use core_backup_backup_restore_base_testcase;

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once('backup_restore_base_testcase.php');
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');

/**
* Backup restore permission tests.
*
* @package core_backup
* @author Tomo Tsuyuki <[email protected]>
* @copyright 2023 Catalyst IT Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class backup_restore_group_test extends core_backup_backup_restore_base_testcase {

/**
* Test for backup/restore with customfields.
* @covers \backup_groups_structure_step
* @covers \restore_groups_structure_step
*/
public function test_backup_restore_group_with_customfields(): void {

$course1 = self::getDataGenerator()->create_course();
$course2 = self::getDataGenerator()->create_course();

$groupfieldcategory = self::getDataGenerator()->create_custom_field_category([
'component' => 'core_group',
'area' => 'group',
]);
$groupcustomfield = self::getDataGenerator()->create_custom_field([
'shortname' => 'testgroupcustomfield1',
'type' => 'text',
'categoryid' => $groupfieldcategory->get('id'),
]);
$groupingfieldcategory = self::getDataGenerator()->create_custom_field_category([
'component' => 'core_group',
'area' => 'grouping',
]);
$groupingcustomfield = self::getDataGenerator()->create_custom_field([
'shortname' => 'testgroupingcustomfield1',
'type' => 'text',
'categoryid' => $groupingfieldcategory->get('id'),
]);

$group1 = self::getDataGenerator()->create_group([
'courseid' => $course1->id,
'name' => 'Test group 1',
'customfield_testgroupcustomfield1' => 'Custom input for group1',
]);
$grouping1 = self::getDataGenerator()->create_grouping([
'courseid' => $course1->id,
'name' => 'Test grouping 1',
'customfield_testgroupingcustomfield1' => 'Custom input for grouping1',
]);

// Perform backup and restore.
$backupid = $this->perform_backup($course1);
$this->perform_restore($backupid, $course2);

// Test group.
$groups = groups_get_all_groups($course2->id);
$this->assertCount(1, $groups);
$group = reset($groups);

// Confirm the group is not same group as original one.
$this->assertNotEquals($group1->id, $group->id);
$this->assertEquals($group1->name, $group->name);

// Confirm custom field is restored in the new group.
$grouphandler = \core_group\customfield\group_handler::create();
$data = $grouphandler->export_instance_data_object($group->id);
$this->assertSame('Custom input for group1', $data->testgroupcustomfield1);

// Test grouping.
$groupings = groups_get_all_groupings($course2->id);
$this->assertCount(1, $groupings);
$grouping = reset($groupings);

// Confirm this is not same grouping as original one.
$this->assertNotEquals($grouping1->id, $grouping->id);

// Confirm custom field is restored in the new grouping.
$groupinghandler = \core_group\customfield\grouping_handler::create();
$data = $groupinghandler->export_instance_data_object($grouping->id);
$this->assertSame('Custom input for grouping1', $data->testgroupingcustomfield1);
}
}
Loading

0 comments on commit d8a4cc9

Please sign in to comment.