/**
  * 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();
 }