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

New Feature: per-group limits #22

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
16 changes: 15 additions & 1 deletion backup/moodle2/backup_groupselect_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,31 @@ protected function define_structure() {
// Define each element separated
$groupselect = new backup_nested_element('groupselect', array('id'), array(
'name', 'intro', 'introformat', 'targetgrouping', 'contentformat',
'password', 'maxmembers', 'timeavailable', 'timedue',
'password', 'maxmembers', 'individual_limits', 'timeavailable', 'timedue',
'timecreated', 'timemodified'));

$limits = new backup_nested_element('limits');

$limit = new backup_nested_element('limit', array('id'), array(
'groupselect', 'groupid', 'lim'));

// Build the tree
// (love this)
$groupselect->add_child($limits);
$limits->add_child($limit);

// Define sources
$groupselect->set_source_table('groupselect', array('id' => backup::VAR_ACTIVITYID));
$limit->set_source_sql('
SELECT *
FROM {groupselect_limits}
WHERE groupselect = ?',
array(backup::VAR_PARENTID)
);

// Define id annotations
// (none)
$limit->annotate_ids('group', 'groupid');

// Define file annotations
$groupselect->annotate_files('mod_groupselect', 'intro', null); // This file areas haven't itemid
Expand Down
13 changes: 13 additions & 0 deletions backup/moodle2/restore_groupselect_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected function define_structure() {

$paths = array();
$paths[] = new restore_path_element('groupselect', '/activity/groupselect');
$paths[] = new restore_path_element('groupselect_limit', '/activity/groupselect/limits/limit');

// Return the paths wrapped into standard activity structure
return $this->prepare_activity_structure($paths);
Expand All @@ -53,6 +54,18 @@ protected function process_groupselect($data) {
$this->apply_activity_instance($newitemid);
}

protected function process_groupselect_limit($data) {
global $DB;

$data = (object)$data;
$oldid = $data->id;

$data->groupselect = $this->get_new_parentid('groupselect');
$data->groupid = $this->get_mappingid('group', $data->groupid);

$newitemid = $DB->insert_record('groupselect_limits', $data);
}

protected function after_execute() {
// Add groupselect related files, no need to match by itemname (just internally handled context)
$this->add_related_files('mod_groupselect', 'intro', null);
Expand Down
21 changes: 18 additions & 3 deletions db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="groupselect" COMMENT="Defines groupselects">
<TABLE NAME="groupselect" COMMENT="Defines groupselects" NEXT="groupselect_limits">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" ENUM="false" NEXT="course"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="name"/>
Expand All @@ -13,8 +13,9 @@
<FIELD NAME="introformat" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="intro" NEXT="targetgrouping"/>
<FIELD NAME="targetgrouping" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="ID of grouping the selection may be done from or zero if not limited" PREVIOUS="introformat" NEXT="password"/>
<FIELD NAME="password" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="targetgrouping" NEXT="maxmembers"/>
<FIELD NAME="maxmembers" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="password" NEXT="timeavailable"/>
<FIELD NAME="timeavailable" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="maxmembers" NEXT="timedue"/>
<FIELD NAME="maxmembers" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="password" NEXT="individual_limits"/>
<FIELD NAME="individual_limits" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="maxmembers" NEXT="timeavailable"/>
<FIELD NAME="timeavailable" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="individual_limits" NEXT="timedue"/>
<FIELD NAME="timedue" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="timeavailable" NEXT="timecreated"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" COMMENT="When the instance was created" PREVIOUS="timedue" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="timecreated"/>
Expand All @@ -26,5 +27,19 @@
<INDEX NAME="course" UNIQUE="false" FIELDS="course"/>
</INDEXES>
</TABLE>
<TABLE NAME="groupselect_limits" COMMENT="Defines per-group limits for a groupselect instance" PREVIOUS="groupselect">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" ENUM="false" NEXT="groupselect"/>
<FIELD NAME="groupselect" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="groupid"/>
<FIELD NAME="groupid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" ENUM="false" PREVIOUS="groupselect" NEXT="lim"/>
<FIELD NAME="lim" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="groupid"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for groupselect limits table"/>
</KEYS>
<INDEXES>
<INDEX NAME="groupselect-groupid" UNIQUE="true" FIELDS="groupselect,groupid"/>
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
13 changes: 13 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ function xmldb_groupselect_upgrade($oldversion) {
upgrade_mod_savepoint(true, 2011101800, 'groupselect');
}

if ($oldversion < 2012071900) {
$table = new xmldb_table('groupselect');
$field = new xmldb_field('individual_limits');
$field->set_attributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$dbman->add_field($table, $field);

$table = new xmldb_table('groupselect_limits');
if (!$dbman->table_exists($table)) {
$dbman->install_one_table_from_xmldb_file($CFG->dirroot.'/mod/groupselect/db/install.xml', 'groupselect_limits', true);
}

upgrade_mod_savepoint(true, 2012071900, 'groupselect');
}

return true;
}
3 changes: 3 additions & 0 deletions lang/en/groupselect.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
$string['groupselect:select'] = 'Allow becoming of group member';
$string['groupselect:unselect'] = 'Allow leaving of group';
$string['incorrectpassword'] = 'Incorrect password';
$string['limit'] = 'Limit';
$string['limits'] = 'Limits';
$string['limits_intro'] = 'Use this page to configure the "max members per group" setting on a per-group basis. If a box is left empty, the group will be limited to the default number set on the main configuration page (currently {$a}). A value of 0 indicates that unlimited members are allowed.';
$string['managegroups'] = 'Manage groups';
$string['maxlimitreached'] = 'Maximum number reached';
$string['maxmembers'] = 'Max members per group';
Expand Down
23 changes: 23 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,26 @@ function groupselect_reset_userdata($data) {
// no resetting here - all data is stored in the group tables
return array();
}

/**
* Adds module specific settings to the settings block
*
* @param settings_navigation $settings The settings navigation object
* @param navigation_node $forumnode The node to add module settings to
*/
function groupselect_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $groupselectnode) {
global $USER, $PAGE, $CFG, $DB, $OUTPUT;

$groupselectobject = $DB->get_record("groupselect", array("id" => $PAGE->cm->instance));
if (empty($PAGE->cm->context)) {
$PAGE->cm->context = get_context_instance(CONTEXT_MODULE, $PAGE->cm->instance);
}

$canmanage = has_capability('moodle/course:managegroups', $PAGE->cm->context);

if ($canmanage) {
$url = new moodle_url('/mod/groupselect/limits.php', array('id' => $PAGE->cm->id));
$string = get_string('limits', 'groupselect');
$groupselectnode->add($string, $url, settings_navigation::TYPE_SETTING);
}
}
130 changes: 130 additions & 0 deletions limits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?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/>.

/**
* Group limit interface
*
* @package mod_groupselect
* @author Adam Olley <[email protected]>
* @copyright 2012 NetSpot Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require('../../config.php');
require_once('locallib.php');
require_once('limits_form.php');

$id = required_param('id', PARAM_INT); // Course Module ID, or
$signup = optional_param('signup', 0, PARAM_INT);
$confirm = optional_param('confirm', 0, PARAM_BOOL);

$params = array();
$params['id'] = $id;
if ($signup) {
$params['signup'] = $signup;
}
if ($confirm) {
$params['confirm'] = $confirm;
}

if (!$cm = get_coursemodule_from_id('groupselect', $id)) {
print_error('invalidcoursemodule');
}

if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
print_error('coursemisconf');
}

if (!$groupselect = $DB->get_record('groupselect', array('id' => $cm->instance))) {
print_error('invalidcoursemodule');
}

$PAGE->set_url('/mod/groupselect/limits.php');

require_login($course, true, $cm);
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_capability('moodle/course:managegroups', $context);

$groups = groups_get_all_groups($course->id, 0, $groupselect->targetgrouping);

if ($course->id == SITEID) {
$viewothers = has_capability('moodle/site:viewparticipants', $sitecontext);
} else {
$viewothers = has_capability('moodle/course:viewparticipants', $context);
}

$strlimit = get_string('modulename', 'groupselect') . ' - ' . get_string('limits', 'groupselect');
$strgroupselect = get_string('modulename', 'groupselect');

$PAGE->set_title(format_string($groupselect->name));
$PAGE->set_heading($course->fullname);
$PAGE->set_cacheable(true);
$PAGE->set_cm($cm);
$PAGE->set_context($context);
$PAGE->set_pagelayout('incourse');

$mform = new mod_groupselect_limits_form($groups);
$formdata = array('id' => $id);
$formdata = array_merge($formdata, groupselect_retrieve_limits_formdata($groupselect->id));

if ($data = $mform->get_data()) {
// Save form data.
if ($data->limit) {
$limits = array();
foreach ($data->limit as $groupid => $lim) {
if ($lim === '') {
continue;
}
$lim = intval($lim);
$limits[$groupid] = $lim;
}
groupselect_save_limits($groupselect->id, $limits);
}
redirect("$CFG->wwwroot/mod/groupselect/view.php?id=$id");
}

echo $OUTPUT->header();
echo '<div class="managelink"><a href="'."$CFG->wwwroot/group/index.php?id=$course->id".'">'.get_string('managegroups', 'groupselect').'</a></div>';

if (empty($CFG->enablegroupings) or empty($cm->groupingid)) {
echo $OUTPUT->heading(get_string('pluginname', 'groupselect'));
} else {
$grouping = groups_get_grouping($cm->groupingid);
echo $OUTPUT->heading(get_string('pluginname', 'groupselect').' '.format_string($grouping->name));
}

echo $OUTPUT->box(get_string('limits_intro', 'groupselect', intval($groupselect->maxmembers)),
'intro generalbox boxwidthnormal boxaligncenter');
if ($groups) {
$data = array();

foreach ($groups as $group) {
$ismember = isset($mygroups[$group->id]);
$usercount = isset($counts[$group->id]) ? $counts[$group->id]->usercount : 0;
$grpname = format_string($group->name);

$line = array();
if ($ismember) {
$grpname = '<div class="mygroup">'.$grpname.'</div>';
}
}
} else {
echo $OUTPUT->notification(get_string('nogroups', 'groupselect'));
}
$mform->set_data($formdata);
$mform->display();

echo $OUTPUT->footer();
53 changes: 53 additions & 0 deletions limits_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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/>.

/**
* Group limit interface form
*
* @package mod_groupselect
* @author Adam Olley <[email protected]>
* @copyright 2012 NetSpot Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once ($CFG->dirroot.'/course/moodleform_mod.php');

class mod_groupselect_limits_form extends moodleform {

public function mod_groupselect_limits_form ($groups) {
$this->groups = $groups;
parent::moodleform();
}

public function definition() {
global $COURSE;

$mform =& $this->_form;

$strlimit = get_string('limit', 'groupselect');

$mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT);

foreach ($this->groups as $group) {
$elname = 'limit[' . $group->id . ']';
$mform->addElement('text', $elname, $group->name . ' ' . $strlimit, array('size' => 4));
$mform->setType($elname, PARAM_INT);
}

$this->add_action_buttons();
}
}
Loading