MDL-32005 Add idnumber field to groups and groupings
This commit is contained in:
Regular → Executable
+13
-5
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<XMLDB PATH="lib/db" VERSION="20120505" COMMENT="XMLDB file for core Moodle tables"
|
||||
<XMLDB PATH="lib/db" VERSION="20120515" COMMENT="XMLDB file for core Moodle tables"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
|
||||
>
|
||||
@@ -2012,8 +2012,9 @@
|
||||
<TABLE NAME="groups" COMMENT="Each record represents a group." PREVIOUS="tag_instance" NEXT="groupings">
|
||||
<FIELDS>
|
||||
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="courseid"/>
|
||||
<FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" PREVIOUS="id" NEXT="name"/>
|
||||
<FIELD NAME="name" TYPE="char" LENGTH="254" NOTNULL="true" SEQUENCE="false" COMMENT="Short human readable unique name for the group." PREVIOUS="courseid" NEXT="description"/>
|
||||
<FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" PREVIOUS="id" NEXT="idnumber"/>
|
||||
<FIELD NAME="idnumber" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false" PREVIOUS="courseid" NEXT="name"/>
|
||||
<FIELD NAME="name" TYPE="char" LENGTH="254" NOTNULL="true" SEQUENCE="false" COMMENT="Short human readable unique name for the group." PREVIOUS="idnumber" NEXT="description"/>
|
||||
<FIELD NAME="description" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="name" NEXT="descriptionformat"/>
|
||||
<FIELD NAME="descriptionformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="description" NEXT="enrolmentkey"/>
|
||||
<FIELD NAME="enrolmentkey" TYPE="char" LENGTH="50" NOTNULL="false" SEQUENCE="false" PREVIOUS="descriptionformat" NEXT="picture"/>
|
||||
@@ -2026,13 +2027,17 @@
|
||||
<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="courseid"/>
|
||||
<KEY NAME="courseid" TYPE="foreign" FIELDS="courseid" REFTABLE="course" REFFIELDS="id" PREVIOUS="primary"/>
|
||||
</KEYS>
|
||||
<INDEXES>
|
||||
<INDEX NAME="idnumber" UNIQUE="false" FIELDS="idnumber"/>
|
||||
</INDEXES>
|
||||
</TABLE>
|
||||
<TABLE NAME="groupings" COMMENT="A grouping is a collection of groups. WAS: groups_groupings" PREVIOUS="groups" NEXT="groups_members">
|
||||
<FIELDS>
|
||||
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="courseid"/>
|
||||
<FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="name"/>
|
||||
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Short human readable unique name for group." PREVIOUS="courseid" NEXT="description"/>
|
||||
<FIELD NAME="description" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="name" NEXT="descriptionformat"/>
|
||||
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="Short human readable unique name for group." PREVIOUS="courseid" NEXT="idnumber"/>
|
||||
<FIELD NAME="idnumber" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false" PREVIOUS="name" NEXT="description"/>
|
||||
<FIELD NAME="description" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="idnumber" NEXT="descriptionformat"/>
|
||||
<FIELD NAME="descriptionformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="description" NEXT="configdata"/>
|
||||
<FIELD NAME="configdata" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="extra configuration data - may be used by group IU tools" PREVIOUS="descriptionformat" NEXT="timecreated"/>
|
||||
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="configdata" NEXT="timemodified"/>
|
||||
@@ -2042,6 +2047,9 @@
|
||||
<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="courseid"/>
|
||||
<KEY NAME="courseid" TYPE="foreign" FIELDS="courseid" REFTABLE="course" REFFIELDS="id" PREVIOUS="primary"/>
|
||||
</KEYS>
|
||||
<INDEXES>
|
||||
<INDEX NAME="idnumber" UNIQUE="false" FIELDS="idnumber"/>
|
||||
</INDEXES>
|
||||
</TABLE>
|
||||
<TABLE NAME="groups_members" COMMENT="Link a user to a group." PREVIOUS="groupings" NEXT="groupings_groups">
|
||||
<FIELDS>
|
||||
|
||||
@@ -448,6 +448,7 @@ function xmldb_main_upgrade($oldversion) {
|
||||
$field = new xmldb_field('coursedisplay', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'completionnotify');
|
||||
|
||||
// Conditionally launch add field coursedisplay
|
||||
//
|
||||
if (!$dbman->field_exists($table, $field)) {
|
||||
$dbman->add_field($table, $field);
|
||||
}
|
||||
@@ -501,5 +502,41 @@ function xmldb_main_upgrade($oldversion) {
|
||||
upgrade_main_savepoint(true, 2012050400.02);
|
||||
}
|
||||
|
||||
if ($oldversion < 2012051100.01) {
|
||||
|
||||
// Define field idnumber to be added to groups
|
||||
$table = new xmldb_table('groups');
|
||||
$field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'courseid');
|
||||
$index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
|
||||
|
||||
// Conditionally launch add field idnumber
|
||||
if (!$dbman->field_exists($table, $field)) {
|
||||
$dbman->add_field($table, $field);
|
||||
}
|
||||
|
||||
// Conditionally launch add index idnumber
|
||||
if (!$dbman->index_exists($table, $index)) {
|
||||
$dbman->add_index($table, $index);
|
||||
}
|
||||
|
||||
// Define field idnumber to be added to groupings
|
||||
$table = new xmldb_table('groupings');
|
||||
$field = new xmldb_field('idnumber', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null, 'name');
|
||||
$index = new xmldb_index('idnumber', XMLDB_INDEX_NOTUNIQUE, array('idnumber'));
|
||||
|
||||
// Conditionally launch add field idnumber
|
||||
if (!$dbman->field_exists($table, $field)) {
|
||||
$dbman->add_field($table, $field);
|
||||
}
|
||||
|
||||
// Conditionally launch add index idnumber
|
||||
if (!$dbman->index_exists($table, $index)) {
|
||||
$dbman->add_index($table, $index);
|
||||
}
|
||||
|
||||
// Main savepoint reached
|
||||
upgrade_main_savepoint(true, 2012051100.01);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
+39
-1
@@ -93,6 +93,25 @@ function groups_get_group_by_name($courseid, $name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the groupid of a group with the idnumber specified for the course.
|
||||
* Group idnumbers should be unique within course
|
||||
*
|
||||
* @category group
|
||||
* @param int $courseid The id of the course
|
||||
* @param string $idnumber idnumber of group
|
||||
* @return group object
|
||||
*/
|
||||
function groups_get_group_by_idnumber($courseid, $idnumber) {
|
||||
global $DB;
|
||||
if (empty($idnumber)) {
|
||||
return false;
|
||||
} else if ($group = $DB->get_record('groups', array('courseid' => $courseid, 'idnumber' => $idnumber))) {
|
||||
return $group;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the groupingid of a grouping with the name specified for the course.
|
||||
* Grouping names should be unique in course
|
||||
@@ -110,6 +129,25 @@ function groups_get_grouping_by_name($courseid, $name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the groupingid of a grouping with the idnumber specified for the course.
|
||||
* Grouping names should be unique within course
|
||||
*
|
||||
* @category group
|
||||
* @param int $courseid The id of the course
|
||||
* @param string $idnumber idnumber of the group
|
||||
* @return grouping object
|
||||
*/
|
||||
function groups_get_grouping_by_idnumber($courseid, $idnumber) {
|
||||
global $DB;
|
||||
if (empty($idnumber)) {
|
||||
return false;
|
||||
} else if ($grouping = $DB->get_record('groupings', array('courseid' => $courseid, 'idnumber' => $idnumber))) {
|
||||
return $grouping;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the group object
|
||||
*
|
||||
@@ -762,4 +800,4 @@ function _group_verify_activegroup($courseid, $groupmode, $groupingid, array $al
|
||||
$SESSION->activegroup[$courseid][$groupmode][$groupingid] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Tests events subsystems
|
||||
*
|
||||
* @package core
|
||||
* @subpackage group
|
||||
* @copyright 2007 onwards Martin Dougiamas (http://dougiamas.com)
|
||||
* @author Andrew Nicols
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
class grouplib_testcase extends advanced_testcase {
|
||||
|
||||
public function test_groups_get_group_by_idnumber() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$generator = $this->getDataGenerator();
|
||||
|
||||
// Create a course category and course
|
||||
$cat = $generator->create_category(array('parent' => 0));
|
||||
$course = $generator->create_course(array('category' => $cat->id));
|
||||
|
||||
$idnumber1 = 'idnumber1';
|
||||
$idnumber2 = 'idnumber2';
|
||||
|
||||
/**
|
||||
* Test with an empty and a null idnumber
|
||||
*/
|
||||
// An empty idnumber should always return a false value
|
||||
$this->assertFalse(groups_get_group_by_idnumber($course->id, ''));
|
||||
$this->assertFalse(groups_get_group_by_idnumber($course->id, null));
|
||||
|
||||
// Even when a group exists which also has an empty idnumber
|
||||
$generator->create_group(array('courseid' => $course->id));
|
||||
$this->assertFalse(groups_get_group_by_idnumber($course->id, ''));
|
||||
$this->assertFalse(groups_get_group_by_idnumber($course->id, null));
|
||||
|
||||
/**
|
||||
* Test with a valid idnumber
|
||||
*/
|
||||
// There is no matching idnumber at present
|
||||
$this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber1));
|
||||
|
||||
// We should now have a valid group returned by the idnumber search
|
||||
$group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber1));
|
||||
$this->assertEquals(groups_get_group_by_idnumber($course->id, $idnumber1), $group);
|
||||
|
||||
// An empty idnumber should still return false
|
||||
$this->assertFalse(groups_get_group_by_idnumber($course->id, ''));
|
||||
$this->assertFalse(groups_get_group_by_idnumber($course->id, null));
|
||||
|
||||
/**
|
||||
* Test with another idnumber
|
||||
*/
|
||||
// There is no matching idnumber at present
|
||||
$this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber2));
|
||||
|
||||
// We should now have a valid group returned by the idnumber search
|
||||
$group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber2));
|
||||
$this->assertEquals(groups_get_group_by_idnumber($course->id, $idnumber2), $group);
|
||||
|
||||
/**
|
||||
* Group idnumbers are unique within a course so test that we don't
|
||||
* retrieve groups for the first course
|
||||
*/
|
||||
|
||||
// Create a second course
|
||||
$course = $generator->create_course(array('category' => $cat->id));
|
||||
|
||||
// An empty idnumber should always return a false value
|
||||
$this->assertFalse(groups_get_group_by_idnumber($course->id, ''));
|
||||
$this->assertFalse(groups_get_group_by_idnumber($course->id, null));
|
||||
|
||||
// Our existing idnumbers shouldn't be returned here as we're in a different course
|
||||
$this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber1));
|
||||
$this->assertFalse(groups_get_group_by_idnumber($course->id, $idnumber2));
|
||||
|
||||
// We should be able to reuse the idnumbers again since this is a different course
|
||||
$group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber1));
|
||||
$this->assertEquals(groups_get_group_by_idnumber($course->id, $idnumber1), $group);
|
||||
|
||||
$group = $generator->create_group(array('courseid' => $course->id, 'idnumber' => $idnumber2));
|
||||
$this->assertEquals(groups_get_group_by_idnumber($course->id, $idnumber2), $group);
|
||||
}
|
||||
|
||||
public function test_groups_get_grouping_by_idnumber() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$generator = $this->getDataGenerator();
|
||||
|
||||
// Create a course category and course
|
||||
$cat = $generator->create_category(array('parent' => 0));
|
||||
$course = $generator->create_course(array('category' => $cat->id));
|
||||
|
||||
$idnumber1 = 'idnumber1';
|
||||
$idnumber2 = 'idnumber2';
|
||||
|
||||
/**
|
||||
* Test with an empty and a null idnumber
|
||||
*/
|
||||
// An empty idnumber should always return a false value
|
||||
$this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
|
||||
$this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
|
||||
|
||||
// Even when a grouping exists which also has an empty idnumber
|
||||
$generator->create_grouping(array('courseid' => $course->id));
|
||||
$this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
|
||||
$this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
|
||||
|
||||
/**
|
||||
* Test with a valid idnumber
|
||||
*/
|
||||
// There is no matching idnumber at present
|
||||
$this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber1));
|
||||
|
||||
// We should now have a valid group returned by the idnumber search
|
||||
$grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber1));
|
||||
$this->assertEquals(groups_get_grouping_by_idnumber($course->id, $idnumber1), $grouping);
|
||||
|
||||
// An empty idnumber should still return false
|
||||
$this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
|
||||
$this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
|
||||
|
||||
/**
|
||||
* Test with another idnumber
|
||||
*/
|
||||
// There is no matching idnumber at present
|
||||
$this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber2));
|
||||
|
||||
// We should now have a valid grouping returned by the idnumber search
|
||||
$grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber2));
|
||||
$this->assertEquals(groups_get_grouping_by_idnumber($course->id, $idnumber2), $grouping);
|
||||
|
||||
/**
|
||||
* Grouping idnumbers are unique within a course so test that we don't
|
||||
* retrieve groupings for the first course
|
||||
*/
|
||||
|
||||
// Create a second course
|
||||
$course = $generator->create_course(array('category' => $cat->id));
|
||||
|
||||
// An empty idnumber should always return a false value
|
||||
$this->assertFalse(groups_get_grouping_by_idnumber($course->id, ''));
|
||||
$this->assertFalse(groups_get_grouping_by_idnumber($course->id, null));
|
||||
|
||||
// Our existing idnumbers shouldn't be returned here as we're in a different course
|
||||
$this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber1));
|
||||
$this->assertFalse(groups_get_grouping_by_idnumber($course->id, $idnumber2));
|
||||
|
||||
// We should be able to reuse the idnumbers again since this is a different course
|
||||
$grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber1));
|
||||
$this->assertEquals(groups_get_grouping_by_idnumber($course->id, $idnumber1), $grouping);
|
||||
|
||||
$grouping = $generator->create_grouping(array('courseid' => $course->id, 'idnumber' => $idnumber2));
|
||||
$this->assertEquals(groups_get_grouping_by_idnumber($course->id, $idnumber2), $grouping);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user