/** * Gets a blank course with 2 teachers and 10 students ready for each test. */ protected function setUp() { global $PAGE, $DB; // First test will set up DB submissions, so we keep it hanging around for the others. $this->resetAfterTest(); // Make a course. $generator = $this->getDataGenerator(); $this->course = $generator->create_course(); $this->setAdminUser(); $PAGE->set_course($this->course); $manager = new course_enrolment_manager($PAGE, $this->course); $plugins = $manager->get_enrolment_plugins(); $instances = $manager->get_enrolment_instances(); /* @var enrol_manual_plugin $manualenrolplugin */ $manualenrolplugin = reset($plugins); $manualenrolinstance = reset($instances); $studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student')); $teacherroleid = $DB->get_field('role', 'id', array('shortname' => 'teacher')); // Make some students. for ($i = 0; $i < 10; $i++) { $student = $generator->create_user(); $this->students[$student->id] = $student; $manualenrolplugin->enrol_user($manualenrolinstance, $student->id, $studentroleid); } // Make a couple of teachers. $teacher1 = $generator->create_user(); $this->teachers[$teacher1->id] = $teacher1; $manualenrolplugin->enrol_user($manualenrolinstance, $teacher1->id, $teacherroleid); $teacher2 = $generator->create_user(); $this->teachers[$teacher2->id] = $teacher2; $manualenrolplugin->enrol_user($manualenrolinstance, $teacher2->id, $teacherroleid); }
break; case 3: default: $today = time(); $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0); $timestart = $today; break; } if ($duration <= 0) { $timeend = 0; } else { $timeend = $timestart + $duration * 24 * 60 * 60; } $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST); $instances = $manager->get_enrolment_instances(); $plugins = $manager->get_enrolment_plugins(true); // Do not allow actions on disabled plugins. if (!array_key_exists($enrolid, $instances)) { throw new enrol_ajax_exception('invalidenrolinstance'); } $instance = $instances[$enrolid]; if (!isset($plugins[$instance->enrol])) { throw new enrol_ajax_exception('enrolnotpermitted'); } $plugin = $plugins[$instance->enrol]; if ($plugin->allow_enrol($instance) && has_capability('enrol/' . $plugin->get_name() . ':enrol', $context)) { $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend); if ($recovergrades) { require_once $CFG->libdir . '/gradelib.php'; grade_recover_history_grades($user->id, $instance->courseid); }
function enrol_user($username, $course_id, $roleid = 5, $timestart = 0, $timeend = 0) { global $CFG, $DB, $PAGE; $username = utf8_decode($username); $username = strtolower($username); /* Create the user before if it is not created yet */ $conditions = array('username' => $username); $user = $DB->get_record('user', $conditions); if (!$user) { $this->create_joomdle_user($username); } $user = $DB->get_record('user', $conditions); $conditions = array('id' => $course_id); $course = $DB->get_record('course', $conditions); if (!$course) { return 0; } // Get enrol start and end dates of manual enrolment plugin if ($CFG->version >= 2011061700) { $manager = new course_enrolment_manager($PAGE, $course); } else { $manager = new course_enrolment_manager($course); } $instances = $manager->get_enrolment_instances(); $plugins = $manager->get_enrolment_plugins(); $enrolid = 1; //manual if (!$timestart) { // Set NOW as enrol start if not one defined $today = time(); $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), date('H', $today), date('i', $today), date('s', $today)); $timestart = $today; } // $timeend = 0; $found = false; foreach ($instances as $instance) { if ($instance->enrol == 'manual') { $found = true; break; } } if (!$found) { return 0; } $plugin = $plugins['manual']; if ($instance->enrolperiod != 0) { $timeend = $timestart + $instance->enrolperiod; } // First, check if user is already enroled but suspended, so we just need to enable it $conditions = array('courseid' => $course_id, 'enrol' => 'manual'); $enrol = $DB->get_record('enrol', $conditions); if (!$enrol) { return 0; } $conditions = array('username' => $username); $user = $DB->get_record('user', $conditions); if (!$user) { return 0; } $conditions = array('enrolid' => $enrol->id, 'userid' => $user->id); $ue = $DB->get_record('user_enrolments', $conditions); if ($ue) { // User already enroled // Can be suspended, or maybe enrol time passed // Just activate enrolment and set new dates $ue->status = 0; //active $ue->timestart = $timestart; $ue->timeend = $timeend; $ue->timemodified = $timestart; $DB->update_record('user_enrolments', $ue); return 1; } $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend); return 1; }
/** * This test the most complex decorator. The aim is to hide users who are in groups that are all hidden * and include those who are either in no group at all, or who have a group which is set to display. */ public function test_groupid_attach_countrwapper() { global $PAGE, $DB; $this->setAdminUser(); // Make basic course submissions. Keep track of which students have what. // Make courses. $generator = $this->getDataGenerator(); $coursea = $generator->create_course(); $courseb = $generator->create_course(); // Make four students and a teacher. $student1a = $generator->create_user(); $student2a = $generator->create_user(); $student3b = $generator->create_user(); $student4b = $generator->create_user(); $teacher = $generator->create_user(); // Enrol the users into the courses. $studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student')); $teacherroleid = $DB->get_field('role', 'id', array('shortname' => 'teacher')); $PAGE->set_course($coursea); $manager = new course_enrolment_manager($PAGE, $coursea); $plugins = $manager->get_enrolment_plugins(); $instances = $manager->get_enrolment_instances(); /* @var enrol_manual_plugin $manualenrolplugin */ $manualenrolplugin = reset($plugins); $manualenrolinstance = reset($instances); $manualenrolplugin->enrol_user($manualenrolinstance, $student1a->id, $studentroleid); $manualenrolplugin->enrol_user($manualenrolinstance, $student2a->id, $studentroleid); $manualenrolplugin->enrol_user($manualenrolinstance, $teacher->id, $teacherroleid); $PAGE->set_course($courseb); $manager = new course_enrolment_manager($PAGE, $courseb); $plugins = $manager->get_enrolment_plugins(); $instances = $manager->get_enrolment_instances(); $manualenrolplugin = reset($plugins); $manualenrolinstance = reset($instances); $manualenrolplugin->enrol_user($manualenrolinstance, $student3b->id, $studentroleid); $manualenrolplugin->enrol_user($manualenrolinstance, $student4b->id, $studentroleid); $manualenrolplugin->enrol_user($manualenrolinstance, $teacher->id, $teacherroleid); // Make two groups in each course. $group1a = new stdClass(); $group1a->courseid = $coursea->id; $group1a = $generator->create_group($group1a); $group2a = new stdClass(); $group2a->courseid = $coursea->id; $group2a = $generator->create_group($group2a); $group3b = new stdClass(); $group3b->courseid = $courseb->id; $group3b = $generator->create_group($group3b); $group4b = new stdClass(); $group4b->courseid = $courseb->id; $group4b = $generator->create_group($group4b); // Make the students into group members. // To test all angles: // Student 1a is in two groups: 1a and 2a. // Student 2a is in 1 group: group 1a. // Student 3b is in one group: 3b. // Student 4a is in no groups. $groupmembership = new stdclass(); $groupmembership->groupid = $group1a->id; $groupmembership->userid = $student1a->id; $groupmembership->timeadded = time(); $DB->insert_record('groups_members', $groupmembership); $groupmembership = new stdclass(); $groupmembership->groupid = $group1a->id; $groupmembership->userid = $student2a->id; $groupmembership->timeadded = time(); $DB->insert_record('groups_members', $groupmembership); $groupmembership = new stdclass(); $groupmembership->groupid = $group3b->id; $groupmembership->userid = $student3b->id; $groupmembership->timeadded = time(); $DB->insert_record('groups_members', $groupmembership); $groupmembership = new stdclass(); $groupmembership->groupid = $group2a->id; $groupmembership->userid = $student1a->id; $groupmembership->timeadded = time(); $DB->insert_record('groups_members', $groupmembership); // Make an assignment for each course. $assigngenerator = new block_ajax_marking_mod_assign_generator($generator); $assignrecord = new stdClass(); $assignrecord->assessed = 1; $assignrecord->scale = 4; $assignrecord->course = $coursea->id; $assigna = $assigngenerator->create_instance($assignrecord); $assignrecord = new stdClass(); $assignrecord->assessed = 1; $assignrecord->scale = 4; $assignrecord->course = $courseb->id; $assignb = $assigngenerator->create_instance($assignrecord); // Make a student submission for each one. $submission = new stdClass(); $submission->userid = $student1a->id; $submission->assignment = $assigna->id; $assigngenerator->create_assign_submission($submission); $submission = new stdClass(); $submission->userid = $student2a->id; $submission->assignment = $assigna->id; $assigngenerator->create_assign_submission($submission); $submission = new stdClass(); $submission->userid = $student3b->id; $submission->assignment = $assignb->id; $assigngenerator->create_assign_submission($submission); $submission = new stdClass(); $submission->userid = $student4b->id; $submission->assignment = $assignb->id; $assigngenerator->create_assign_submission($submission); // A basic query should now return all four submissions. $this->setUser($teacher->id); // Get coutwrapper without any decorators. $nodesbuilder = new ReflectionClass('block_ajax_marking_nodes_builder'); $moduleunionmethod = $nodesbuilder->getMethod('get_module_queries_array'); $moduleunionmethod->setAccessible(true); $modulequeries = $moduleunionmethod->invoke($nodesbuilder, array()); $moduleunionmethod = $nodesbuilder->getMethod('get_count_wrapper_query'); $moduleunionmethod->setAccessible(true); /* @var block_ajax_marking_query $countwrapper */ $countwrapper = $moduleunionmethod->invoke($nodesbuilder, $modulequeries, array()); $this->assertInstanceOf('block_ajax_marking_query', $countwrapper); $totalnumberofsubmissions = 4; // Check that the raw countwrapper gets what we need. $sql = $countwrapper->debuggable_query(); $recordset = $DB->get_recordset_sql($countwrapper->get_sql(), $countwrapper->get_params()); // Count: should be 4. No other 'group by' filters, so just one record with an itemcount for all submissions. $pregroupscount = 0; $row = $recordset->current(); $pregroupscount += $row->itemcount; $this->assertEquals($totalnumberofsubmissions, $pregroupscount, 'Query not working before we even get to the groups decorator'); // OK - query works. now get the basic group ids that we expect. // Wrap countwrapper in group id decorator. $countwrapper = new block_ajax_marking_filter_groupid_attach_countwrapper($countwrapper); // Add a select or two so we can see if the groupid is there. $countwrapper->add_select(array('table' => 'membergroupquery', 'column' => 'groupid')); $countwrapper->add_select(array('table' => 'moduleunion', 'column' => 'userid'), true); $countwrapper->add_select(array('table' => 'moduleunion', 'column' => 'coursemoduleid')); // For debugging. Stop here and copy this to see what's in the query. $wrappedquery = $countwrapper->debuggable_query(); $results = $countwrapper->execute(); // Sanity check: we should still get the whole lot before messing with anything. $this->assertEquals($totalnumberofsubmissions, count($results), 'Groups decorator has broken the query before any settings changed'); // Make sure we have the right groups - should be the highest groupid available if they are in more than one // group, or the one group id if they are in one group. // Student 1a is in two groups. Should be the max id. $message = 'Student has the wrong groupid - ' . $results[$student1a->id]->groupid . ' but ought to be the highest out of group1a and group2a ids: ' . $group2a->id; $this->assertEquals($group2a->id, $results[$student1a->id]->groupid, $message); // Student 2a should be there with the id from group1a. Single group membership. $message = 'Student 2a should be in group 1a (id: ' . $group1a->id . ' but is instead in a group with id ' . $results[$student2a->id]->groupid; $this->assertEquals($group1a->id, $results[$student2a->id]->groupid, $message); // Student 4b has no group, so ought to be without any groupid. $message = 'Student 4b should have zero for a groupid due to being in no group, but doesn\'t'; $this->assertEquals(0, $results[$student4b->id]->groupid, $message); // Hide a group2a at coursemoduleid level. $newsetting = new stdClass(); $newsetting->userid = $teacher->id; $newsetting->tablename = 'course_modules'; $newsetting->instanceid = $assigna->cmid; $newsetting->display = 1; $newsetting->id = $DB->insert_record('block_ajax_marking', $newsetting); $newgroupsetting = new stdClass(); $newgroupsetting->configid = $newsetting->id; $newgroupsetting->groupid = $group2a->id; $newgroupsetting->display = 0; $newgroupsetting->id = $DB->insert_record('block_ajax_marking_groups', $newgroupsetting); $results = $countwrapper->execute(); $message = 'Hiding at coursemodule level has hidden the user instead of used the other available groupid'; $this->assertArrayHasKey($student1a->id, $results, $message); $message = 'Hiding a group at coursemodule level didn\'t work as it ought to have made the student ' . 'in multiple groups who previously had a high id have a low id.'; $this->assertEquals($group1a->id, $results[$student1a->id]->groupid, $message); // Now hide the only group a student is in and verify that we have that student hidden. $newgroupsetting = new stdClass(); $newgroupsetting->configid = $newsetting->id; $newgroupsetting->groupid = $group1a->id; $newgroupsetting->display = 0; $newgroupsetting->id = $DB->insert_record('block_ajax_marking_groups', $newgroupsetting); $results = $countwrapper->execute(); $message = 'Hiding group 1a failed to leave student 2a with a null groupid'; $this->assertArrayNotHasKey($student2a->id, $results, $message); $message = 'Hiding group 1a failed to leave student 1a with a null groupid'; $this->assertArrayNotHasKey($student1a->id, $results, $message); // Hide both groups group at course level. // Clean out the settings first. $DB->delete_records('block_ajax_marking_groups'); $DB->delete_records('block_ajax_marking'); // Test for non-group users coming up as 0. }