public function test_cohort_get_visible_list() { global $DB; $this->resetAfterTest(); $category1 = $this->getDataGenerator()->create_category(); $category2 = $this->getDataGenerator()->create_category(); $course1 = $this->getDataGenerator()->create_course(array('category' => $category1->id)); $course2 = $this->getDataGenerator()->create_course(array('category' => $category2->id)); $course3 = $this->getDataGenerator()->create_course(); $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid' => context_coursecat::instance($category1->id)->id)); $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid' => context_coursecat::instance($category2->id)->id)); $cohort3 = $this->getDataGenerator()->create_cohort(array('contextid' => context_system::instance()->id)); $cohort4 = $this->getDataGenerator()->create_cohort(array('contextid' => context_system::instance()->id)); $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); $user3 = $this->getDataGenerator()->create_user(); $user4 = $this->getDataGenerator()->create_user(); $user5 = $this->getDataGenerator()->create_user(); $manualenrol = enrol_get_plugin('manual'); $enrol1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual')); $enrol2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual')); $manualenrol->enrol_user($enrol1, $user1->id); $manualenrol->enrol_user($enrol1, $user3->id); $manualenrol->enrol_user($enrol1, $user4->id); $manualenrol->enrol_user($enrol2, $user2->id); cohort_add_member($cohort1->id, $user1->id); cohort_add_member($cohort3->id, $user1->id); cohort_add_member($cohort1->id, $user3->id); cohort_add_member($cohort2->id, $user2->id); $list = cohort_get_visible_list($course1); $this->assertEquals(2, count($list)); $this->assertNotEmpty($list[$cohort1->id]); $this->assertRegExp('/\\(2\\)$/', $list[$cohort1->id]); $this->assertNotEmpty($list[$cohort3->id]); $this->assertRegExp('/\\(1\\)$/', $list[$cohort3->id]); $list = cohort_get_visible_list($course1, false); $this->assertEquals(3, count($list)); $this->assertNotEmpty($list[$cohort1->id]); $this->assertRegExp('/\\(2\\)$/', $list[$cohort1->id]); $this->assertNotEmpty($list[$cohort3->id]); $this->assertRegExp('/\\(1\\)$/', $list[$cohort3->id]); $this->assertNotEmpty($list[$cohort4->id]); $this->assertRegExp('/[^\\)]$/', $list[$cohort4->id]); $list = cohort_get_visible_list($course2); $this->assertEquals(1, count($list)); $this->assertNotEmpty($list[$cohort2->id]); $this->assertRegExp('/\\(1\\)$/', $list[$cohort2->id]); $list = cohort_get_visible_list($course2, false); $this->assertEquals(3, count($list)); $this->assertNotEmpty($list[$cohort2->id]); $this->assertRegExp('/\\(1\\)$/', $list[$cohort2->id]); $this->assertNotEmpty($list[$cohort3->id]); $this->assertRegExp('/[^\\)]$/', $list[$cohort3->id]); $this->assertNotEmpty($list[$cohort4->id]); $this->assertRegExp('/[^\\)]$/', $list[$cohort4->id]); $list = cohort_get_visible_list($course3); $this->assertEquals(0, count($list)); $list = cohort_get_visible_list($course3, false); $this->assertEquals(2, count($list)); $this->assertNotEmpty($list[$cohort3->id]); $this->assertRegExp('/[^\\)]$/', $list[$cohort3->id]); $this->assertNotEmpty($list[$cohort4->id]); $this->assertRegExp('/[^\\)]$/', $list[$cohort4->id]); }
/** * Form Definition */ function definition() { global $CFG, $COURSE; $mform =& $this->_form; $mform->addElement('header', 'autogroup', get_string('autocreategroups', 'group')); $options = array(0 => get_string('all')); $options += $this->_customdata['roles']; $mform->addElement('select', 'roleid', get_string('selectfromrole', 'group'), $options); $student = get_archetype_roles('student'); $student = reset($student); if ($student and array_key_exists($student->id, $options)) { $mform->setDefault('roleid', $student->id); } $context = context_course::instance($COURSE->id); if (has_capability('moodle/cohort:view', $context)) { $options = cohort_get_visible_list($COURSE); if ($options) { $options = array(0 => get_string('anycohort', 'cohort')) + $options; $mform->addElement('select', 'cohortid', get_string('selectfromcohort', 'cohort'), $options); $mform->setDefault('cohortid', '0'); } else { $mform->addElement('hidden', 'cohortid'); $mform->setType('cohortid', PARAM_INT); $mform->setConstant('cohortid', '0'); } } else { $mform->addElement('hidden', 'cohortid'); $mform->setType('cohortid', PARAM_INT); $mform->setConstant('cohortid', '0'); } $options = array('groups' => get_string('numgroups', 'group'), 'members' => get_string('nummembers', 'group')); $mform->addElement('select', 'groupby', get_string('groupby', 'group'), $options); $mform->addElement('text', 'number', get_string('number', 'group'), 'maxlength="4" size="4"'); $mform->setType('number', PARAM_INT); $mform->addRule('number', null, 'numeric', null, 'client'); $mform->addRule('number', get_string('required'), 'required', null, 'client'); $mform->addElement('checkbox', 'nosmallgroups', get_string('nosmallgroups', 'group')); $mform->disabledIf('nosmallgroups', 'groupby', 'noteq', 'members'); $mform->setAdvanced('nosmallgroups'); $options = array('no' => get_string('noallocation', 'group'), 'random' => get_string('random', 'group'), 'firstname' => get_string('byfirstname', 'group'), 'lastname' => get_string('bylastname', 'group'), 'idnumber' => get_string('byidnumber', 'group')); $mform->addElement('select', 'allocateby', get_string('allocateby', 'group'), $options); $mform->setDefault('allocateby', 'random'); $mform->setAdvanced('allocateby'); $mform->addElement('text', 'namingscheme', get_string('namingscheme', 'group')); $mform->addHelpButton('namingscheme', 'namingscheme', 'group'); $mform->addRule('namingscheme', get_string('required'), 'required', null, 'client'); $mform->setType('namingscheme', PARAM_TEXT); // there must not be duplicate group names in course $template = get_string('grouptemplate', 'group'); $gname = groups_parse_name($template, 0); if (!groups_get_group_by_name($COURSE->id, $gname)) { $mform->setDefault('namingscheme', $template); } $options = array('0' => get_string('no'), '-1' => get_string('newgrouping', 'group')); if ($groupings = groups_get_all_groupings($COURSE->id)) { foreach ($groupings as $grouping) { $options[$grouping->id] = strip_tags(format_string($grouping->name)); } } $mform->addElement('select', 'grouping', get_string('createingrouping', 'group'), $options); if ($groupings) { $mform->setDefault('grouping', '-1'); } $mform->addElement('text', 'groupingname', get_string('groupingname', 'group'), $options); $mform->setType('groupingname', PARAM_TEXT); $mform->disabledIf('groupingname', 'grouping', 'noteq', '-1'); $mform->addElement('hidden', 'courseid'); $mform->setType('courseid', PARAM_INT); $mform->addElement('hidden', 'seed'); $mform->setType('seed', PARAM_INT); $buttonarray = array(); $buttonarray[] =& $mform->createElement('submit', 'preview', get_string('preview')); $buttonarray[] =& $mform->createElement('submit', 'submitbutton', get_string('submit')); $buttonarray[] =& $mform->createElement('cancel'); $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); $mform->closeHeaderBefore('buttonar'); }