diff --git a/lang/en/choicegroup.php b/lang/en/choicegroup.php
index 38df724..85ff78d 100644
--- a/lang/en/choicegroup.php
+++ b/lang/en/choicegroup.php
@@ -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';
+
+
diff --git a/lib.php b/lib.php
index b0e0252..0a64775 100644
--- a/lib.php
+++ b/lib.php
@@ -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;
}
@@ -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
diff --git a/mod_form.php b/mod_form.php
index 378e0ae..f449d7a 100644
--- a/mod_form.php
+++ b/mod_form.php
@@ -128,72 +128,27 @@ function definition() {
$mform->addElement('header', 'groups', get_string('groupsheader', 'choicegroup'));
- $mform->addElement('html', '
');
$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'));
@@ -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();
@@ -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');
}
}
diff --git a/renderer.php b/renderer.php
index 2691764..1f6d742 100644
--- a/renderer.php
+++ b/renderer.php
@@ -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'));
@@ -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('
', $group_members_names), array('class' => 'choicegroups-membersnames hidden', 'id' => 'choicegroup_'.$option->attributes->value));
diff --git a/yui/form/form.js b/yui/form/form.js
index 5a1fb36..371e784 100644
--- a/yui/form/form.js
+++ b/yui/form/form.js
@@ -10,20 +10,9 @@ YUI.add('moodle-mod_choicegroup-form', function(Y) {
var CSS = {
},
SELECTORS = {
- AVAILABLE_GRPS_SELECT: '#availablegroups',
- AVAILABLE_GRPS_SELECT_OPTIONS: "select[id='availablegroups'] option",
- SELECTED_GRPS_SELECT: '#id_selectedGroups',
- ADD_GRP_BTN: '#addGroupButton',
- DEL_GRP_BTN: '#removeGroupButton',
FORM: '#mform1',
- LIMIT_UI_INPUT: '#ui_limit_input',
- LIMIT_UI_DIV: '#fitem_id_limit_0',
- LIMIT_UI_LABEL: '#label_for_limit_ui',
APPLY_LIMIT_TO_ALL_GRPS_BTN: '#id_setlimit',
ENABLE_DISABLE_LIMITING_SELECT: '#id_limitanswers',
- EXPAND_ALL_GRPNGS_BTN: '#expandButton',
- COLLAPSE_ALL_GRPNGS_BTN: '#collapseButton',
- SERIALIZED_SELECTED_GRPS_LIST: '#serializedselectedgroups',
GLOBAL_LIMIT_INPUT: '#id_generallimitation',
HIDDEN_LIMIT_INPUTS: 'input.limit_input_node',
};
@@ -34,25 +23,12 @@ YUI.add('moodle-mod_choicegroup-form', function(Y) {
// -------------------------------
// Global Variables
// -------------------------------
-
var CHAR_LIMITUI_PAR_LEFT = M.util.get_string('char_limitui_parenthesis_start', 'choicegroup');
var CHAR_LIMITUI_PAR_RIGHT = M.util.get_string('char_limitui_parenthesis_end', 'choicegroup');
- var CHAR_SELECT_BULLET_COLLAPSED = M.util.get_string('char_bullet_collapsed', 'choicegroup');
- var CHAR_SELECT_BULLET_EXPANDED = M.util.get_string('char_bullet_expanded', 'choicegroup');
-
- var availableGroupsNode = Y.one(SELECTORS.AVAILABLE_GRPS_SELECT);
- var addGroupButtonNode = Y.one(SELECTORS.ADD_GRP_BTN);
- var selectedGroupsNode = Y.one(SELECTORS.SELECTED_GRPS_SELECT);
- var removeGroupButtonNode = Y.one(SELECTORS.DEL_GRP_BTN);
var formNode = Y.one(SELECTORS.FORM);
- var uiInputLimitNode = Y.one(SELECTORS.LIMIT_UI_INPUT);
var applyLimitToAllGroupsButtonNode = Y.one(SELECTORS.APPLY_LIMIT_TO_ALL_GRPS_BTN);
var limitAnswersSelectNode = Y.one(SELECTORS.ENABLE_DISABLE_LIMITING_SELECT);
- var limitInputUIDIVNode = Y.one(SELECTORS.LIMIT_UI_DIV);
- var expandButtonNode = Y.one(SELECTORS.EXPAND_ALL_GRPNGS_BTN);
- var collapseButtonNode = Y.one(SELECTORS.COLLAPSE_ALL_GRPNGS_BTN);
- var serializedSelectedGroupsListNode = Y.one(SELECTORS.SERIALIZED_SELECTED_GRPS_LIST);
var groupingsNodesContainer = new Array();
@@ -85,97 +61,6 @@ YUI.add('moodle-mod_choicegroup-form', function(Y) {
});
}
- function addOptionNodeToSelectedGroupsList(optNode) {
- if (optNode.hasClass('grouping') == true) {
- // check if option is collapsed
- if (((typeof groupingsNodesContainer[optNode.get('value')]) == 'undefined') || ( groupingsNodesContainer[optNode.get('value')].length == 0)) {
- // it is expanded, take nodes from UI
- // This is a grouping, so instead of adding this item we actually need to add everything underneath it
- var sib = optNode.next(); // sib means sibling, as in, the next element in the DOM tree
- while (sib && sib.hasClass('nested') && sib.hasClass('group')) {
- // add sib
- selectedGroupsNode.append(sib.cloneNode(true));
- // go to next node
- sib = sib.next();
- }
- } else {
- // yes it IS collapsed, need to take the nodes from the container rather than from the UI
- groupingsNodesContainer[optNode.get('value')].forEach(function (underlyingGroupNode) {
- selectedGroupsNode.append(underlyingGroupNode.cloneNode(true));
- });
- }
- } else {
- selectedGroupsNode.append(optNode.cloneNode(true));
- }
- if (limitAnswersSelectNode.get('value') == '1') {
- updateLimitUIOfAllSelectedGroups();
- }
- }
-
- function updateGroupLimit(e) {
- var selectedOptionsNodes = Y.all(SELECTORS.SELECTED_GRPS_SELECT + " option:checked");
- // get value of input box
- var limit = uiInputLimitNode.get('value');
- selectedOptionsNodes.each(function(optNode) {
- getInputLimitNodeOfSelectedGroupNode(optNode).set('value', limit);
- updateLimitUIOfSelectedGroup(optNode);
- });
- }
-
- function collapseGrouping(groupingNode) {
- // Change the text of this