diff --git a/classes/domain/autogroup_set.php b/classes/domain/autogroup_set.php index c4752c5..b9ea3e6 100644 --- a/classes/domain/autogroup_set.php +++ b/classes/domain/autogroup_set.php @@ -329,6 +329,16 @@ public function get_group_by_options() { return $this->sortmodule->get_config_options(); } + /** + * Returns the options to be displayed on the autgroup_set + * editing form. These are defined per-module. + * + * @return array + */ + public function get_delimited_by_options() { + return $this->sortmodule->get_delimiter_options(); + } + /** * @return int the count of groups linked to this groupset */ @@ -348,7 +358,7 @@ public function get_membership_counts() { } /** - * returns the name of the field this is currently + * returns the actual value of the field this is currently * grouping by. * * @return string @@ -357,6 +367,25 @@ public function grouping_by() { return $this->sortmodule->grouping_by(); } + /** + * returns the display name of the field this is currently + * grouping by. + * + * @return string + */ + public function grouping_by_text() { + return $this->sortmodule->grouping_by_text(); + } + + /** + * returns delimiter. + * + * @return string + */ + public function delimited_by() { + return $this->sortmodule->delimited_by(); + } + /** * @param int $courseid */ diff --git a/classes/form/autogroup_set_settings.php b/classes/form/autogroup_set_settings.php index 287b118..87dfefa 100644 --- a/classes/form/autogroup_set_settings.php +++ b/classes/form/autogroup_set_settings.php @@ -49,6 +49,8 @@ public function definition() { $this->add_group_by_options(); + $this->add_field_delimiter_options(); + $this->add_role_options(); $this->add_action_buttons(); @@ -72,6 +74,20 @@ private function add_group_by_options() { } } + /** + * Add delimiter options if the sortmodule has the option. + * + * @return void + */ + protected function add_field_delimiter_options() { + $delimiteroptions = $this->_customdata->get_delimited_by_options(); + if ($delimiteroptions) { + $mform = &$this->_form; + $mform->addElement('select', 'delimitedby', get_string('set_delimitedby', 'local_autogroup'), $delimiteroptions); + $mform->setDefault('delimitedby', $this->_customdata->delimited_by()); + } + } + /** * @throws \coding_exception */ diff --git a/classes/sort_module.php b/classes/sort_module.php index f243553..3e95bb1 100644 --- a/classes/sort_module.php +++ b/classes/sort_module.php @@ -54,6 +54,10 @@ abstract class sort_module { * @var array */ protected $config = array(); + /** + * @var array + */ + protected $delimiters = []; /** * @param stdClass $config @@ -110,4 +114,28 @@ public function __set($attribute, $value) { */ abstract public function grouping_by(); + /** + * Return display string which explains how users are being grouped + * + * @return string + */ + abstract public function grouping_by_text(); + + /** + * Get array of delimiter string. + * + * @return array + */ + public function get_delimiter_options() { + return array_combine($this->delimiters, $this->delimiters); + } + + /** + * Get delimiter string. + * + * @return string + */ + public function delimited_by() { + return ''; + } } diff --git a/classes/sort_module/profile_field.php b/classes/sort_module/profile_field.php index e247522..b23b72e 100644 --- a/classes/sort_module/profile_field.php +++ b/classes/sort_module/profile_field.php @@ -102,10 +102,17 @@ public function eligible_groups_for_user(stdClass $user) { * @return bool|string */ public function grouping_by() { + return empty($this->field) ? false : $this->field; + } + + /** + * @return bool|string + */ + public function grouping_by_text() { if (empty ($this->field)) { return false; } - return (string)$this->field; + $options = $this->get_config_options(); + return isset($options[$this->field]) ? $options[$this->field] : $this->field; } - } diff --git a/classes/sort_module/user_info_field.php b/classes/sort_module/user_info_field.php index bd6d6ed..f6f7a63 100644 --- a/classes/sort_module/user_info_field.php +++ b/classes/sort_module/user_info_field.php @@ -118,4 +118,11 @@ public function grouping_by() { return (string)$field; } + /** + * @return bool|string + */ + public function grouping_by_text() { + return ucfirst(format_string($this->grouping_by())); + } + } diff --git a/classes/sort_module/user_info_field_multivalue.php b/classes/sort_module/user_info_field_multivalue.php new file mode 100644 index 0000000..e96b8ea --- /dev/null +++ b/classes/sort_module/user_info_field_multivalue.php @@ -0,0 +1,152 @@ +. + +/** + * autogroup local plugin + * + * A course object relates to a Moodle course and acts as a container + * for multiple groups. Initialising a course object will automatically + * load each autogroup group for that course into memory. + * + * @package local_autogroup + * @copyright 2023 Catalyst IT Australia Pty Ltd + * @author Tomo Tsuyuki + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace local_autogroup\sort_module; + +use local_autogroup\sort_module; +use stdClass; + +/** + * Class course + * @package local_autogroup\domain + */ +class user_info_field_multivalue extends sort_module { + /** + * @var string + */ + private $field = ''; + /** + * @var string + */ + private $delimiter = ''; + /** + * @var array + */ + protected $delimiters = [',', '|',';']; + + /** + * @param stdClass $config + * @param int $courseid + */ + public function __construct($config, $courseid) { + if ($this->config_is_valid($config)) { + $this->field = $config->field; + $this->delimiter = empty($config->delimiter) ? '' : $config->delimiter; + } + $this->courseid = (int)$courseid; + } + + /** + * @param stdClass $config + * @return bool + */ + public function config_is_valid(stdClass $config) { + if (!isset($config->field)) { + return false; + } + + // Ensure that the stored option is valid. + if (array_key_exists($config->field, $this->get_config_options())) { + return true; + } + + return false; + } + + /** + * Returns the options to be displayed on the autgroup_set + * editing form. These are defined per-module. + * + * @return array + */ + public function get_config_options() { + global $DB; + + $options = []; + $infofields = $DB->get_records('user_info_field', ['datatype' => 'text']); + + foreach ($infofields as $field) { + $options[$field->id] = $field->name; + } + return $options; + } + + /** + * @param stdClass $user + * @return array $result + */ + public function eligible_groups_for_user(stdClass $user) { + global $DB; + + $field = $this->field; + if (empty($field)) { + return []; + } + $data = $DB->get_record('user_info_data', ['fieldid' => $field, 'userid' => $user->id]); + if ($data && !empty($data->data)) { + $delimiteddata = explode($this->delimiter, $data->data); + $trimmeddata = array_map('trim', $delimiteddata); + return $trimmeddata; + } + return []; + } + + /** + * @return bool|string + */ + public function grouping_by() { + return empty($this->field) ? false : $this->field; + } + + /** + * @return bool|string + */ + public function grouping_by_text() { + global $DB; + if (empty ($this->field)) { + return false; + } + + $field = $DB->get_field('user_info_field', 'name', ['id' => $this->field]); + if (empty($field)) { + return false; + } + return (string)$field; + } + + /** + * Get delimiter string. + * + * @return string + */ + public function delimited_by() { + return $this->delimiter; + } + +} diff --git a/edit.php b/edit.php index c03c3af..497575c 100644 --- a/edit.php +++ b/edit.php @@ -141,10 +141,18 @@ $updategroupmembership = true; } else { + $updated = false; $options = new stdClass(); - $options->field = $data->groupby; + if (!empty($data->groupby) && $data->groupby != $groupset->grouping_by()) { + $options->field = $data->groupby; + $updated = true; + } + if (!empty($data->delimitedby) && $data->delimitedby != $groupset->delimited_by()) { + $options->delimiter = $data->delimitedby; + $updated = true; + } - if ($options->field != $groupset->grouping_by()) { + if ($updated) { // User has selected another option. $groupset->set_options($options); $groupset->save($DB, $cleanupold); diff --git a/lang/en/local_autogroup.php b/lang/en/local_autogroup.php index 69f66b3..07f9515 100644 --- a/lang/en/local_autogroup.php +++ b/lang/en/local_autogroup.php @@ -45,6 +45,7 @@ $string['set_groups'] = 'Number of groups'; $string['set_roles'] = 'Eligible Roles'; $string['set_groupby'] = 'Group by'; +$string['set_delimitedby'] = 'Delimited by'; $string['confirmdelete'] = 'Are you sure you wish to remove this auto group set?'; @@ -84,6 +85,7 @@ // Sort module names. $string['sort_module:profile_field'] = 'Profile field'; $string['sort_module:user_info_field'] = 'Custom profile field'; +$string['sort_module:user_info_field_multivalue'] = 'Custom profile field multi values'; // Privacy provider $string['privacy:metadata:local_autogroup'] = 'Data relating users for the local autogroup plugin'; diff --git a/manage.php b/manage.php index dd07ddd..e6f136d 100644 --- a/manage.php +++ b/manage.php @@ -55,6 +55,9 @@ die(); } +$PAGE->set_context($context); +$PAGE->set_url(local_autogroup_renderer::URL_COURSE_MANAGE, array('courseid' => $courseid)); + $course = $DB->get_record('course', array('id' => $courseid)); $groupsets = $DB->get_records('local_autogroup_set', array('courseid' => $courseid)); @@ -64,8 +67,6 @@ $heading = \get_string('coursesettingstitle', 'local_autogroup', $course->shortname); -$PAGE->set_context($context); -$PAGE->set_url(local_autogroup_renderer::URL_COURSE_MANAGE, array('courseid' => $courseid)); $PAGE->set_title($heading); $PAGE->set_heading($heading); $PAGE->set_pagelayout('incourse'); diff --git a/renderer.php b/renderer.php index 1fd54fc..49d4898 100644 --- a/renderer.php +++ b/renderer.php @@ -97,8 +97,8 @@ private function groupsets_table_group(local_autogroup\domain\autogroup_set $gro // Get the groupset type. $row [] = ucfirst(local_autogroup_sanitise_sort_module_name($groupset->sortmodule)); - // Get the grouping by text. - $row [] = ucfirst(format_string($groupset->grouping_by())); + // Get the grouping by text which is used in the edit screen. + $row [] = ucfirst($groupset->grouping_by_text()); // Get the count of groups. $row [] = $groupset->get_group_count(); diff --git a/version.php b/version.php index 90677c7..b0813b3 100644 --- a/version.php +++ b/version.php @@ -28,7 +28,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2023062100; +$plugin->version = 2023062101; $plugin->requires = 2013111800.00; // Requires this Moodle version (2.7). $plugin->release = '2.8.3'; // Plugin release. $plugin->component = 'local_autogroup'; // Full name of the plugin (used for diagnostics).