public function test_mod_ratingallocate_generated_module()
 {
     $record = mod_ratingallocate_generator::get_default_values();
     foreach ($record as $name => $value) {
         if (subStr($name, strlen($name) - 7, 7) === 'maxsize') {
             $record[$name] = 10;
         }
         if (subStr($name, strlen($name) - 6, 6) === 'active') {
             $record[$name] = true;
         }
     }
     $record['num_students'] = 22;
     $test_module = new mod_ratingallocate_generated_module($this, $record);
     $this->assertCount($record['num_students'], $test_module->students);
     $this->assertCount(20, $test_module->allocations);
     $ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user($this, $test_module->mod_db, $test_module->teacher);
     foreach ($ratingallocate->get_choices_with_allocationcount() as $choice) {
         $this->assertEquals(10, $choice->{'usercount'});
     }
 }
 /**
  * Test if option titles are returned according to a mixture of defined and custom values,
  */
 public function test_get_option_titles_mixed()
 {
     $settings = array(1 => 'Ja1234');
     //Test data
     $ratings = array(0, 1, 1, 1, 1);
     $expected_result = $settings;
     $expected_result[0] = 'No';
     //Depends on language file
     $record = mod_ratingallocate_generator::get_default_values();
     $record['strategyopt']['strategy_yesno'] = $settings;
     $test_module = new mod_ratingallocate_generated_module($this, $record);
     $ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user($this, $test_module->mod_db, $test_module->teacher);
     $result = $ratingallocate->get_options_titles($ratings);
     $this->assertEquals($expected_result, $result);
 }
 private static function get_ratingallocate_for_teacher_open_in($num_days, advanced_testcase $tc)
 {
     $record = mod_ratingallocate_generator::get_default_values();
     $record['accesstimestart'] = time() + $num_days * 24 * 60 * 60;
     $record['accesstimestop'] = time() + ($num_days + 6) * 24 * 60 * 60;
     $record['publishdate'] = time() + ($num_days + 7) * 24 * 60 * 60;
     $test_module = new mod_ratingallocate_generated_module($tc, $record);
     return mod_ratingallocate_generator::get_ratingallocate_for_user($tc, $test_module->mod_db, $test_module->teacher);
 }
 /**
  * Create an ratingallocate module with 4 enroled students and their ratings.
  * @param $ratingperiodended determines if the rating period should have ended.
  * @param int $algorithmstatus the algorithm status of the modul to be created.
  * @param datetime $algorithmstarttime the start time of the algorithm.
  */
 private function create_ratingallocate($ratingperiodended, $algorithmstatus = \mod_ratingallocate\algorithm_status::notstarted, $algorithmstarttime = null)
 {
     global $DB;
     $this->resetAfterTest();
     $this->setAdminUser();
     $course = $this->getDataGenerator()->create_course();
     $this->teacher = mod_ratingallocate_generator::create_user_and_enrol($this, $course, true);
     $this->setUser($this->teacher);
     // There should not be any module for that course first
     $this->assertFalse($DB->record_exists(this_db\ratingallocate::TABLE, array(this_db\ratingallocate::COURSE => $course->id)));
     $data = mod_ratingallocate_generator::get_default_values();
     $data['course'] = $course;
     // Shift the rating period depending on its ending.
     $data['accesstimestart'] = time();
     $data['accesstimestop'] = time();
     if ($ratingperiodended) {
         $data['accesstimestart'] -= 6 * 24 * 60 * 60;
         // Necessary to ensure access time stop being in the past
         --$data['accesstimestop'];
     } else {
         $data['accesstimestop'] += 6 * 24 * 60 * 60;
     }
     $data['algorithmstatus'] = $algorithmstatus;
     $data['algorithmstarttime'] = $algorithmstarttime;
     // create activity
     $this->mod = $this->getDataGenerator()->create_module(ratingallocate_MOD_NAME, $data);
     $this->assertEquals(2, $DB->count_records(this_db\ratingallocate_choices::TABLE, array(this_db\ratingallocate_choices::RATINGALLOCATEID => $this->mod->id)));
     $student_1 = mod_ratingallocate_generator::create_user_and_enrol($this, $course);
     $student_2 = mod_ratingallocate_generator::create_user_and_enrol($this, $course);
     $student_3 = mod_ratingallocate_generator::create_user_and_enrol($this, $course);
     $student_4 = mod_ratingallocate_generator::create_user_and_enrol($this, $course);
     $ratingallocate = mod_ratingallocate_generator::get_ratingallocate_for_user($this, $this->mod, $this->teacher);
     $choices = $ratingallocate->get_rateable_choices();
     $choice1 = reset($choices);
     $choice2 = end($choices);
     //Create preferences
     $prefers_non = 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);
     }
     $prefers_first = json_decode(json_encode($prefers_non), true);
     $prefers_first[$choice1->{this_db\ratingallocate_choices::ID}][this_db\ratingallocate_ratings::RATING] = true;
     $prefers_second = json_decode(json_encode($prefers_non), true);
     $prefers_second[$choice2->{this_db\ratingallocate_choices::ID}][this_db\ratingallocate_ratings::RATING] = true;
     //assign preferences
     mod_ratingallocate_generator::save_rating_for_user($this, $this->mod, $student_1, $prefers_first);
     mod_ratingallocate_generator::save_rating_for_user($this, $this->mod, $student_2, $prefers_first);
     mod_ratingallocate_generator::save_rating_for_user($this, $this->mod, $student_3, $prefers_second);
     mod_ratingallocate_generator::save_rating_for_user($this, $this->mod, $student_4, $prefers_second);
     $this->assertEquals(0, $DB->count_records(this_db\ratingallocate_allocations::TABLE, array(this_db\ratingallocate_allocations::RATINGALLOCATEID => $this->mod->id)));
 }