本文整理汇总了PHP中advanced_testcase::getDataGenerator方法的典型用法代码示例。如果您正苦于以下问题:PHP advanced_testcase::getDataGenerator方法的具体用法?PHP advanced_testcase::getDataGenerator怎么用?PHP advanced_testcase::getDataGenerator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类advanced_testcase
的用法示例。
在下文中一共展示了advanced_testcase::getDataGenerator方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create_logged_in_users
/**
* Create (simulated) logged in users and add some of them to groups in a course
*/
public function create_logged_in_users()
{
global $DB;
$generator = advanced_testcase::getDataGenerator();
$data = array();
// Create 2 courses.
$course1 = $generator->create_course();
$data['course1'] = $course1;
$course2 = $generator->create_course();
$data['course2'] = $course2;
// Create 9 (simulated) logged in users enroled into $course1.
for ($i = 1; $i <= 9; $i++) {
$user = $generator->create_user();
$DB->set_field('user', 'lastaccess', time(), array('id' => $user->id));
$generator->enrol_user($user->id, $course1->id);
$DB->insert_record('user_lastaccess', array('userid' => $user->id, 'courseid' => $course1->id, 'timeaccess' => time()));
$data['user' . $i] = $user;
}
// Create 3 (simulated) logged in users who are not enroled into $course1.
for ($i = 10; $i <= 12; $i++) {
$user = $generator->create_user();
$DB->set_field('user', 'lastaccess', time(), array('id' => $user->id));
$data['user' . $i] = $user;
}
// Create 3 groups in course 1.
$group1 = $generator->create_group(array('courseid' => $course1->id));
$data['group1'] = $group1;
$group2 = $generator->create_group(array('courseid' => $course1->id));
$data['group2'] = $group2;
$group3 = $generator->create_group(array('courseid' => $course1->id));
$data['group3'] = $group3;
// Add 3 users to course group 1.
$generator->create_group_member(array('groupid' => $group1->id, 'userid' => $data['user1']->id));
$generator->create_group_member(array('groupid' => $group1->id, 'userid' => $data['user2']->id));
$generator->create_group_member(array('groupid' => $group1->id, 'userid' => $data['user3']->id));
// Add 4 users to course group 2.
$generator->create_group_member(array('groupid' => $group2->id, 'userid' => $data['user3']->id));
$generator->create_group_member(array('groupid' => $group2->id, 'userid' => $data['user4']->id));
$generator->create_group_member(array('groupid' => $group2->id, 'userid' => $data['user5']->id));
$generator->create_group_member(array('groupid' => $group2->id, 'userid' => $data['user6']->id));
return $data;
// Return the user, course and group objects.
}
示例2: __construct
/**
* Generates a fully set up mod_ratingallocate module
* @param advanced_testcase $tc
* @param array $record
* @param boolean $assert_intermediate_result
*/
public function __construct(advanced_testcase $tc, $record = null, $assert_intermediate_result = true)
{
global $DB;
$tc->resetAfterTest();
$tc->setAdminUser();
if (is_null($record)) {
$record = array();
} elseif (!is_array($record)) {
$tc->fail('$record must be null or an array');
}
if (!array_key_exists('course', $record)) {
$record['course'] = $tc->getDataGenerator()->create_course();
}
$this->course = $record['course'];
$this->teacher = mod_ratingallocate_generator::create_user_and_enrol($tc, $this->course, true);
$tc->setUser($this->teacher);
if ($assert_intermediate_result) {
$tc->assertFalse($DB->record_exists(this_db\ratingallocate::TABLE, array(this_db\ratingallocate::COURSE => $this->course->id)), 'There should not be any module for that course first');
}
// create activity
$this->mod_db = $tc->getDataGenerator()->create_module(ratingallocate_MOD_NAME, $record);
$tc->assertEquals(2, $DB->count_records(this_db\ratingallocate_choices::TABLE), array(this_db\ratingallocate_choices::ID => $this->mod_db->id));
// create students
$num_students = array_key_exists('num_students', $record) ? $record['num_students'] : 20;
for ($i = 0; $i < $num_students; $i++) {
$this->students[$i] = mod_ratingallocate_generator::create_user_and_enrol($tc, $this->course);
}
// load choices
$ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user($tc, $this->mod_db, $this->teacher);
$this->choices = $choices = $ratingallocate->get_rateable_choices();
$choices_nummerated = array_values($choices);
$num_choices = sizeof($choices_nummerated);
// create students' preferences as array
// array ('Choice 1' => 1 )
if (!array_key_exists('ratings', $record)) {
$record['ratings'] = array();
for ($i = 0; $i < $num_students; $i++) {
$record['ratings'][$i] = array($choices_nummerated[$i % $num_choices]->{this_db\ratingallocate_choices::TITLE} => 1);
}
}
$this->ratings = $record['ratings'];
// Create preferences
$prefers_non = array();
$choice_id_by_title = array();
foreach ($choices as $choice) {
$prefers_non[$choice->{this_db\ratingallocate_choices::ID}] = array(this_db\ratingallocate_ratings::CHOICEID => $choice->{this_db\ratingallocate_choices::ID}, this_db\ratingallocate_ratings::RATING => 0);
$choice_id_by_title[$choice->{this_db\ratingallocate_choices::TITLE}] = $choice->{this_db\ratingallocate_choices::ID};
}
// rate for student
for ($i = 0; $i < $num_students; $i++) {
$rating = json_decode(json_encode($prefers_non), true);
// create user's rating according to the modules specifications
foreach ($this->ratings[$i] as $choice_name => $preference) {
$choice_id = $choice_id_by_title[$choice_name];
$rating[$choice_id][this_db\ratingallocate_ratings::RATING] = $preference;
}
// assign preferences
mod_ratingallocate_generator::save_rating_for_user($tc, $this->mod_db, $this->students[$i], $rating);
if ($assert_intermediate_result) {
$alloc = mod_ratingallocate_generator::get_ratingallocate_for_user($tc, $this->mod_db, $this->students[$i]);
$saved_ratings = $alloc->get_rating_data_for_user($this->students[$i]->id);
$saved_rating_arr = array();
foreach ($saved_ratings as $saved_rat) {
if (!$saved_rat->{this_db\ratingallocate_ratings::RATING} == 0) {
$saved_rating_arr[$saved_rat->{this_db\ratingallocate_choices::TITLE}] = $saved_rat->{this_db\ratingallocate_ratings::RATING};
}
}
$tc->assertEquals($this->ratings[$i], $saved_rating_arr);
}
}
// allocate choices
$ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user($tc, $this->mod_db, $this->teacher);
$time_needed = $ratingallocate->distrubute_choices();
$tc->assertGreaterThan(0, $time_needed);
$tc->assertLessThan(0.2, $time_needed, 'Allocation is very slow');
$this->allocations = $ratingallocate->get_allocations();
}