Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessibility for Group choosing #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lang/en/choicegroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,7 @@
$string['double_click_group_legend'] = 'Double click on a group to add it.';


$string['grouplimit'] = 'Group limit';
$string['usegroup'] = 'Use this group';


126 changes: 63 additions & 63 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,29 +188,24 @@ function choicegroup_add_instance($choicegroup) {
$choicegroup->timeclose = 0;
}

//insert answers
$choicegroup->id = $DB->insert_record("choicegroup", $choicegroup);

// deserialize the selected groups

$groupIDs = explode(';', $choicegroup->serializedselectedgroups);
$groupIDs = array_diff( $groupIDs, array( '' ) );

foreach ($groupIDs as $groupID) {
$groupID = trim($groupID);
if (isset($groupID) && $groupID != '') {
$option = new stdClass();
$option->groupid = $groupID;
$option->choicegroupid = $choicegroup->id;
$property = 'group_' . $groupID . '_limit';
if (isset($choicegroup->$property)) {
$option->maxanswers = $choicegroup->$property;

// Insert options
foreach ($choicegroup->cgroup as $key => $notused) {
$value = isset($choicegroup->lgroup[$key])? $choicegroup->lgroup[$key] : 0;
if ($value >= 0) {
$group = new stdClass();
$group->choicegroupid = $choicegroup->id;
$group->groupid = $key;
if ($choicegroup->limitanswers) {
$group->maxanswers = $value;
} else {
$group->maxanswers = 0;
}
$option->timemodified = time();
$DB->insert_record("choicegroup_options", $option);
}
$DB->insert_record("choicegroup_options", $group);
}
}

return $choicegroup->id;
}

Expand Down Expand Up @@ -238,55 +233,60 @@ function choicegroup_update_instance($choicegroup) {
if (empty($choicegroup->multipleenrollmentspossible)) {
$choicegroup->multipleenrollmentspossible = 0;
}


// deserialize the selected groups

$groupIDs = explode(';', $choicegroup->serializedselectedgroups);
$groupIDs = array_diff( $groupIDs, array( '' ) );

// prepare pre-existing selected groups from database

if (!($preExistingGroups = $DB->get_records("choicegroup_options", array("choicegroupid" => $choicegroup->id), "id"))) {
return false;
}

// walk through form-selected groups
foreach ($groupIDs as $groupID) {
$groupID = trim($groupID);
if (isset($groupID) && $groupID != '') {
$option = new stdClass();
$option->groupid = $groupID;
$option->choicegroupid = $choicegroup->id;
$property = 'group_' . $groupID . '_limit';
if (isset($choicegroup->$property)) {
$option->maxanswers = $choicegroup->$property;
}
$option->timemodified = time();
// Find out if this selection already exists
foreach ($preExistingGroups as $key => $preExistingGroup) {
if ($option->groupid == $preExistingGroup->groupid) {
// match found, so instead of creating a new record we should merely update a pre-existing record
$option->id = $preExistingGroup->id;
$DB->update_record("choicegroup_options", $option);
// remove the element from the array to not deal with it later
unset($preExistingGroups[$key]);
continue 2; // continue the big loop
}
}
$DB->insert_record("choicegroup_options", $option);
}

}
// remove all remaining pre-existing groups which did not appear in the form (and are thus assumed to have been deleted)
foreach ($preExistingGroups as $preExistingGroup) {
$DB->delete_records("choicegroup_options", array("id"=>$preExistingGroup->id));

//print_object($choicegroup);
if (isset($choicegroup->lgroup)) {
// Update, delete or insert groups
foreach ($choicegroup->lgroup as $key => $value) {
$group = new stdClass();
$group->groupid = $key;
$group->choicegroupid = $choicegroup->id;
if ($choicegroup->limitanswers) {
$group->maxanswers = $value;
} else {
$group->maxanswers = 0;
}
if (isset($choicegroup->groupid[$key]) && !empty($choicegroup->groupid[$key])) { // Existing choicegroup_group record
$group->id = $choicegroup->groupid[$key];
if (isset($choicegroup->cgroup[$key]) && $value >= 0) {
$group->timemodified = time();
$DB->update_record("choicegroup_options", $group);
} else { // Empty old option - needs to be deleted.
$DB->delete_records("choicegroup_options", array('id' => $group->id));
}
} else {
if (isset($choicegroup->cgroup[$key]) && $value >= 0) {
$group->timemodified = time();
$DB->insert_record("choicegroup_options", $group);
}
}
}
}

return $DB->update_record('choicegroup', $choicegroup);

}

/**
* Returns all groups in a specified course, if boolean is true returns only group's id
*
* @param int $courseid
* @param bool $onlyids
* @return object
*/
function choicegroup_detected_groups($courseid, $onlyids = false) {
global $DB;

$groups = groups_get_all_groups($courseid);
if (!is_array($groups)) {
$groups = array();
}
if ($onlyids) {
$groups = array_keys($groups);
}
return $groups;
}

/**
* @global object
* @param object $choicegroup
Expand Down
112 changes: 48 additions & 64 deletions mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,72 +128,27 @@ function definition() {


$mform->addElement('header', 'groups', get_string('groupsheader', 'choicegroup'));
$mform->addElement('html', '<fieldset class="clearfix">
<div class="fcontainer clearfix">
<div id="fitem_id_option_0" class="fitem fitem_fselect ">
<div class="fitemtitle"><label for="id_option_0">'.get_string('groupsheader', 'choicegroup').'</label><span class="helptooltip"><a href="'. $CFG->wwwroot .'/help.php?component=choicegroup&amp;identifier=choicegroupoptions&amp;lang='.current_language().'" title="'.get_string('choicegroupoptions_help', 'choicegroup').'" aria-haspopup="true" target="_blank"><img src="'.$CFG->wwwroot.'/theme/image.php?theme='.$PAGE->theme->name.'&component=core&image=help" alt="'.get_string('choicegroupoptions_help', 'choicegroup').'" class="iconhelp"></a></span></div><div class="felement fselect">
<div class="tablecontainer">
<table><tr><th>'.get_string('available_groups', 'choicegroup').'</th><th>&nbsp;</th><th>'.get_string('selected_groups', 'choicegroup').'</th><th>&nbsp;</th></tr><tr><td style="vertical-align: top">');

$mform->addElement('html','<select id="availablegroups" name="availableGroups" multiple size=10 style="width:200px">');
foreach ($groupings as $groupingID => $grouping) {
// find all linked groups to this grouping
if (isset($grouping->linkedGroupsIDs) && count($grouping->linkedGroupsIDs) > 1) { // grouping has more than 2 items, thus we should display it (otherwise it would be clearer to display only that single group alone)
$mform->addElement('html', '<option value="'.$groupingID.'" style="font-weight: bold" class="grouping">'.get_string('char_bullet_expanded', 'choicegroup').$grouping->name.'</option>');

$mform->addElement('static', 'groupserror', "", "");
foreach ($groupings as $grouping) {
if (isset($grouping->linkedGroupsIDs) && count($grouping->linkedGroupsIDs) > 1) {
$mform->addElement('html', '<fieldset class="grouping"><legend>'.$grouping->name.'</legend>');
foreach ($grouping->linkedGroupsIDs as $linkedGroupID) {
if (isset($groups[$linkedGroupID])) {
$mform->addElement('html', '<option value="'.$linkedGroupID.'" class="group nested">&nbsp;&nbsp;&nbsp;&nbsp;'.$groups[$linkedGroupID]->name.'</option>');
$groups[$linkedGroupID]->mentioned = true;
}
}
$group = $groups[$linkedGroupID];
$this->addgroup($group->id, $group->name);
$groups[$linkedGroupID]->mentioned = true;
}
$mform->addElement('html', '</fieldset>');
}
}
foreach ($groups as $group) {
if ($group->mentioned === false) {
$mform->addElement('html', '<option value="'.$group->id.'" class="group toplevel">'.format_string($group->name).'</option>');
$this->addgroup($group->id, $group->name);
}
}
$mform->addElement('html','</select><br><button name="expandButton" type="button" disabled id="expandButton">'.get_string('expand_all_groupings', 'choicegroup').'</button><button name="collapseButton" type="button" disabled id="collapseButton">'.get_string('collapse_all_groupings', 'choicegroup').'</button><br>'.get_string('double_click_grouping_legend', 'choicegroup').'<br>'.get_string('double_click_group_legend', 'choicegroup'));






$mform->addElement('html','
</td><td><button id="addGroupButton" name="add" type="button" disabled>'.get_string('add', 'choicegroup').'</button><div><button name="remove" type="button" disabled id="removeGroupButton">'.get_string('del', 'choicegroup').'</button></div></td>');
$mform->addElement('html','<td style="vertical-align: top"><select id="id_selectedGroups" name="selectedGroups" multiple size=10 style="width:200px"></select></td>');

$mform->addElement('html','<td><div><div id="fitem_id_limit_0" class="fitem fitem_ftext" style="display:none"><div class=""><label for="id_limit_0" id="label_for_limit_ui">'.get_string('set_limit_for_group', 'choicegroup').'</label></div><div class="ftext">
<input class="mod-choicegroup-limit-input" type="text" value="0" id="ui_limit_input" disabled="disabled"></div></div></div></td></tr></table></div>
</div></div>

</div>
</fieldset>');

$mform->setExpanded('groups');

foreach ($groups as $group) {
$mform->addElement('hidden', 'group_' . $group->id . '_limit', '', array('id' => 'group_' . $group->id . '_limit', 'class' => 'limit_input_node'));
$mform->setType('group_' . $group->id . '_limit', PARAM_RAW);
}


$serializedselectedgroupsValue = '';
if (isset($this->_instance) && $this->_instance != '') {
// this is presumably edit mode, try to fill in the data for javascript
$cg = choicegroup_get_choicegroup($this->_instance);
foreach ($cg->option as $optionID => $groupID) {
$serializedselectedgroupsValue .= ';' . $groupID;
$mform->setDefault('group_' . $groupID . '_limit', $cg->maxanswers[$optionID]);
}

}


$mform->addElement('hidden', 'serializedselectedgroups', $serializedselectedgroupsValue, array('id' => 'serializedselectedgroups'));
$mform->setType('serializedselectedgroups', PARAM_RAW);

switch (get_config('choicegroup', 'sortgroupsby')) {
case CHOICEGROUP_SORTGROUPS_CREATEDATE:
$systemdefault = array(CHOICEGROUP_SORTGROUPS_SYSTEMDEFAULT => get_string('systemdefault_date', 'choicegroup'));
Expand Down Expand Up @@ -228,6 +183,23 @@ function definition() {
$this->add_action_buttons();
}

private function addgroup($groupid, $groupname) {
$mform =& $this->_form;
$buttonarray = array();
$buttonarray[] =& $mform->createElement('text', 'lgroup['.$groupid.']', get_string('grouplimit', 'choicegroup'),
array('size' => 4, 'class' => 'limit_input_node'));
$buttonarray[] =& $mform->createElement('checkbox', 'cgroup['.$groupid.']', '',
' '.get_string('usegroup', 'choicegroup'));
$mform->setType('cgroup['.$groupid.']', PARAM_INT);
$mform->setType('lgroup['.$groupid.']', PARAM_INT);
$mform->addGroup($buttonarray, 'groupelement', $groupname, array(' '), false);
$mform->disabledIf('lgroup['.$groupid.']', 'cgroup['.$groupid.']', 'notchecked');
$mform->setDefault('lgroup['.$groupid.']', 0);
$mform->addElement('hidden', 'groupid['.$groupid.']', '');
$mform->setType('groupid['.$groupid.']', PARAM_INT);
$mform->disabledIf('lgroup['.$groupid.']', 'limitanswers', 'eq', 0);
}

function data_preprocessing(&$default_values){
global $DB;
$this->js_call();
Expand All @@ -238,21 +210,33 @@ function data_preprocessing(&$default_values){
$default_values['timerestrict'] = 1;
}

}
if (!$this->current->instance) {
return;
}
$groupsok = $DB->get_records('choicegroup_options', array('choicegroupid' => $this->current->instance), 'groupid', 'id, groupid, maxanswers');
if (!empty($groupsok)) {
$groups = choicegroup_detected_groups($COURSE->id, true);
foreach ($groupsok as $group) {
if (in_array($group->groupid, $groups)) {
$defaultvalues['lgroup['.$group->groupid.']'] = $group->maxanswers;
$defaultvalues['cgroup['.$group->groupid.']'] = true;
$defaultvalues['groupid['.$group->groupid.']'] = $group->id;
}
}
}
}

function validation($data, $files) {
$errors = parent::validation($data, $files);

$groupIDs = explode(';', $data['serializedselectedgroups']);
$groupIDs = array_diff( $groupIDs, array( '' ) );
$numgroups = isset($data['cgroup']) ? count($data['cgroup']) : 0;

if (array_key_exists('multipleenrollmentspossible', $data) && $data['multipleenrollmentspossible'] === '1') {
if (count($groupIDs) < 1) {
$errors['serializedselectedgroups'] = get_string('fillinatleastoneoption', 'choicegroup');
if ($numgroups < 1) {
$errors['groupserror'] = get_string('fillinatleastoneoption', 'choicegroup');
}
} else {
if (count($groupIDs) < 2) {
$errors['serializedselectedgroups'] = get_string('fillinatleasttwooptions', 'choicegroup');
if ($numgroups < 2) {
$errors['groupserror'] = get_string('fillinatleasttwooptions', 'choicegroup');
}
}

Expand Down
10 changes: 6 additions & 4 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ public function display_options($options, $coursemoduleid, $vertical = true, $pu
$group_members_names[] = $group_user->lastname . ', ' . $group_user->firstname;
}
sort($group_members_names);
if (!empty($option->attributes->disabled) || ($limitanswers && sizeof($group_members) >= $option->maxanswers) && empty($option->attributes->checked)) {
$labeltext .= ' ' . html_writer::tag('em', get_string('full', 'choicegroup'));
$option->attributes->disabled=true;
if (!empty($option->attributes->disabled) || ($limitanswers && $option->maxanswers > 0 && count($group_members) >= $option->maxanswers) && empty($option->attributes->checked)) {
if(($limitanswers && $option->maxanswers > 0 && count($group_members) >= $option->maxanswers) && empty($option->attributes->checked)) {
$labeltext .= ' ' . html_writer::tag('em', get_string('full', 'choicegroup'));
}
$option->attributes->disabled = true;
$availableoption--;
}
$labeltext .= html_writer::tag('div', format_text($group->description), array('class' => 'choicegroups-descriptions hidden'));
Expand All @@ -139,7 +141,7 @@ public function display_options($options, $coursemoduleid, $vertical = true, $pu
($showresults == CHOICEGROUP_SHOWRESULTS_AFTER_ANSWER and $current) or
($showresults == CHOICEGROUP_SHOWRESULTS_AFTER_CLOSE and !$choicegroupopen)) {

$maxanswers = ($limitanswers) ? (' / '.$option->maxanswers) : ('');
$maxanswers = ($limitanswers && $option->maxanswers > 0 ) ? (' / '.$option->maxanswers) : ('');
$html .= html_writer::tag('td', sizeof($group_members_names).$maxanswers, array('class' => 'center'));
if ($publish == CHOICEGROUP_PUBLISH_NAMES) {
$group_members_html = html_writer::tag('div', implode('<br />', $group_members_names), array('class' => 'choicegroups-membersnames hidden', 'id' => 'choicegroup_'.$option->attributes->value));
Expand Down
Loading