Skip to content

Commit

Permalink
Implement #42 delimited text profile field (#49)
Browse files Browse the repository at this point in the history
I have gone
  • Loading branch information
TomoTsuyuki authored Oct 31, 2023
1 parent 15c815a commit e48fc2c
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 10 deletions.
31 changes: 30 additions & 1 deletion classes/domain/autogroup_set.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand All @@ -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
*/
Expand Down
16 changes: 16 additions & 0 deletions classes/form/autogroup_set_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
*/
Expand Down
28 changes: 28 additions & 0 deletions classes/sort_module.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ abstract class sort_module {
* @var array
*/
protected $config = array();
/**
* @var array
*/
protected $delimiters = [];

/**
* @param stdClass $config
Expand Down Expand Up @@ -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 '';
}
}
11 changes: 9 additions & 2 deletions classes/sort_module/profile_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
7 changes: 7 additions & 0 deletions classes/sort_module/user_info_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

}
152 changes: 152 additions & 0 deletions classes/sort_module/user_info_field_multivalue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?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/>.

/**
* 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 <[email protected]>
* @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;
}

}
12 changes: 10 additions & 2 deletions edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions lang/en/local_autogroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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?';

Expand Down Expand Up @@ -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';
Expand Down
5 changes: 3 additions & 2 deletions manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit e48fc2c

Please sign in to comment.