From e0f7a085dc1b400940bcfae70f7fb800d0f929e7 Mon Sep 17 00:00:00 2001 From: Adomas Ciplys Date: Fri, 8 Nov 2024 19:26:21 +0100 Subject: [PATCH 1/8] Created student answers model class --- .../models/student_answers_relation.php | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 server/moodle/mod/livequiz/classes/models/student_answers_relation.php diff --git a/server/moodle/mod/livequiz/classes/models/student_answers_relation.php b/server/moodle/mod/livequiz/classes/models/student_answers_relation.php new file mode 100644 index 000000000..967fe636a --- /dev/null +++ b/server/moodle/mod/livequiz/classes/models/student_answers_relation.php @@ -0,0 +1,75 @@ +. + +/** + * Class representing the relationship between students and answers. + * @package mod_livequiz + * @copyright 2024 Software AAU + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_livequiz\models; + +use dml_exception; + +/** + * Class student_answers_relation + * @package mod_livequiz\student_answers_relation + */ +class Student_answers_relation { + /** + * Insert student answer relation. Represents an answer given by a student in a participation. + * + * @param int $studentid + * @param int $answerid + * @param int $participationid + * @return void + * @throws dml_exception + */ + public static function insert_student_answer_relation(int $studentid, int $answerid, int $participationid): void { + global $DB; + $DB->insert_record('livequiz_student_answers', [ + 'student_id' => $studentid, + 'answer' => $answerid, + 'participation_id' => $participationid, + ]); + } + + /** + * Get all answers for a student in a given participation + * + * @param $quizid int + * @return array // An array of question objects. + * @throws dml_exception + */ + public static function get_answers_from_student_in_participation(int $studentid, int $participationid): array { + global $DB; + + $answerids = $DB->get_records( + 'livequiz_student_answers', + ['student_id' => $studentid, 'participation_id' => $participationid], + '', + 'answer_id' + ); + $answers = []; + + foreach ($answerids as $answerid) { + $answers[] = answer::get_answer_from_id($answerid->answer_id); + } + + return $answers; + } +} From ed4df0df84268efb48f2a94f9a58c08caf98e224 Mon Sep 17 00:00:00 2001 From: Adomas Ciplys Date: Fri, 8 Nov 2024 20:00:56 +0100 Subject: [PATCH 2/8] Fixed naming error when inserting --- .../models/student_answers_relation.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/server/moodle/mod/livequiz/classes/models/student_answers_relation.php b/server/moodle/mod/livequiz/classes/models/student_answers_relation.php index 967fe636a..11a899d67 100644 --- a/server/moodle/mod/livequiz/classes/models/student_answers_relation.php +++ b/server/moodle/mod/livequiz/classes/models/student_answers_relation.php @@ -24,12 +24,13 @@ namespace mod_livequiz\models; use dml_exception; +use Exception; /** * Class student_answers_relation * @package mod_livequiz\student_answers_relation */ -class Student_answers_relation { +class student_answers_relation { /** * Insert student answer relation. Represents an answer given by a student in a participation. * @@ -41,9 +42,9 @@ class Student_answers_relation { */ public static function insert_student_answer_relation(int $studentid, int $answerid, int $participationid): void { global $DB; - $DB->insert_record('livequiz_student_answers', [ + $DB->insert_record('livequiz_students_answers', [ 'student_id' => $studentid, - 'answer' => $answerid, + 'answer_id' => $answerid, 'participation_id' => $participationid, ]); } @@ -59,15 +60,20 @@ public static function get_answers_from_student_in_participation(int $studentid, global $DB; $answerids = $DB->get_records( - 'livequiz_student_answers', + 'livequiz_students_answers', ['student_id' => $studentid, 'participation_id' => $participationid], '', 'answer_id' ); $answers = []; - + foreach ($answerids as $answerid) { - $answers[] = answer::get_answer_from_id($answerid->answer_id); + // There is chance we are getting an answer that does not exist. + try { + $answers[] = answer::get_answer_from_id($answerid->answer_id); + } catch (Exception $e) { + error_log('Could not get answer from id: ' . $answerid->answer_id); + } } return $answers; From 040b994a79bff006c19287da87f7e783d2905591 Mon Sep 17 00:00:00 2001 From: Adomas Ciplys Date: Fri, 8 Nov 2024 20:29:59 +0100 Subject: [PATCH 3/8] Fixed doc comments --- .../models/student_answers_relation.php | 20 ++++------------ .../classes/services/livequiz_services.php | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/server/moodle/mod/livequiz/classes/models/student_answers_relation.php b/server/moodle/mod/livequiz/classes/models/student_answers_relation.php index 11a899d67..7e46fe16c 100644 --- a/server/moodle/mod/livequiz/classes/models/student_answers_relation.php +++ b/server/moodle/mod/livequiz/classes/models/student_answers_relation.php @@ -52,11 +52,12 @@ public static function insert_student_answer_relation(int $studentid, int $answe /** * Get all answers for a student in a given participation * - * @param $quizid int - * @return array // An array of question objects. + * @param int $studentid + * @param int $participationid + * @return array An array of answer id's * @throws dml_exception */ - public static function get_answers_from_student_in_participation(int $studentid, int $participationid): array { + public static function get_answersids_from_student_in_participation(int $studentid, int $participationid): array { global $DB; $answerids = $DB->get_records( @@ -65,17 +66,6 @@ public static function get_answers_from_student_in_participation(int $studentid, '', 'answer_id' ); - $answers = []; - - foreach ($answerids as $answerid) { - // There is chance we are getting an answer that does not exist. - try { - $answers[] = answer::get_answer_from_id($answerid->answer_id); - } catch (Exception $e) { - error_log('Could not get answer from id: ' . $answerid->answer_id); - } - } - - return $answers; + return $answerids; } } diff --git a/server/moodle/mod/livequiz/classes/services/livequiz_services.php b/server/moodle/mod/livequiz/classes/services/livequiz_services.php index 5a975a0a2..0457341f8 100644 --- a/server/moodle/mod/livequiz/classes/services/livequiz_services.php +++ b/server/moodle/mod/livequiz/classes/services/livequiz_services.php @@ -32,6 +32,7 @@ use mod_livequiz\models\question; use mod_livequiz\models\questions_answers_relation; use mod_livequiz\models\quiz_questions_relation; +use mod_livequiz\models\student_answers_relation; use PhpXmlRpc\Exception; use function PHPUnit\Framework\throwException; @@ -215,4 +216,26 @@ private function get_questions_with_answers(int $quizid): array { } return $questions; } + + /** + * Gets answers from a student in a specific participation. + * + * @param int $studentid The ID of the student. + * @param int $participationid The ID of the participation. + * @return answer[] The list of answers. + * @throws dml_exception + */ + public function get_answers_from_stundent_in_participation(int $studentid, int $participationid): array { + $answers = []; + $answerids = student_answers_relation::get_answersids_from_student_in_participation($studentid, $participationid); + foreach ($answerids as $answerid) { + // There is chance we are getting an answer that does not exist. + try { + $answers[] = answer::get_answer_from_id($answerid->answer_id); + } catch (Exception $e) { + error_log('Could not get answer from id: ' . $answerid->answer_id); + } + } + return $answers; + } } From ba91387db5379133996178bc7ba95f9fda48adee Mon Sep 17 00:00:00 2001 From: Adomas Ciplys Date: Fri, 8 Nov 2024 21:33:23 +0100 Subject: [PATCH 4/8] Added test for the model --- .../models/student_answers_relation.php | 7 +- .../classes/services/livequiz_services.php | 4 +- .../phpunit/student_answers_relation_test.php | 115 ++++++++++++++++++ 3 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 server/moodle/mod/livequiz/tests/phpunit/student_answers_relation_test.php diff --git a/server/moodle/mod/livequiz/classes/models/student_answers_relation.php b/server/moodle/mod/livequiz/classes/models/student_answers_relation.php index 7e46fe16c..1b43fc036 100644 --- a/server/moodle/mod/livequiz/classes/models/student_answers_relation.php +++ b/server/moodle/mod/livequiz/classes/models/student_answers_relation.php @@ -40,9 +40,9 @@ class student_answers_relation { * @return void * @throws dml_exception */ - public static function insert_student_answer_relation(int $studentid, int $answerid, int $participationid): void { + public static function insert_student_answer_relation(int $studentid, int $answerid, int $participationid): int { global $DB; - $DB->insert_record('livequiz_students_answers', [ + return $DB->insert_record('livequiz_students_answers', [ 'student_id' => $studentid, 'answer_id' => $answerid, 'participation_id' => $participationid, @@ -60,12 +60,13 @@ public static function insert_student_answer_relation(int $studentid, int $answe public static function get_answersids_from_student_in_participation(int $studentid, int $participationid): array { global $DB; - $answerids = $DB->get_records( + $answerrecords = $DB->get_records( 'livequiz_students_answers', ['student_id' => $studentid, 'participation_id' => $participationid], '', 'answer_id' ); + $answerids = array_column($answerrecords, 'answer_id'); return $answerids; } } diff --git a/server/moodle/mod/livequiz/classes/services/livequiz_services.php b/server/moodle/mod/livequiz/classes/services/livequiz_services.php index 0457341f8..780bbf2a9 100644 --- a/server/moodle/mod/livequiz/classes/services/livequiz_services.php +++ b/server/moodle/mod/livequiz/classes/services/livequiz_services.php @@ -231,9 +231,9 @@ public function get_answers_from_stundent_in_participation(int $studentid, int $ foreach ($answerids as $answerid) { // There is chance we are getting an answer that does not exist. try { - $answers[] = answer::get_answer_from_id($answerid->answer_id); + $answers[] = answer::get_answer_from_id($answerid); } catch (Exception $e) { - error_log('Could not get answer from id: ' . $answerid->answer_id); + error_log('Could not get answer from id: ' . $answerid); } } return $answers; diff --git a/server/moodle/mod/livequiz/tests/phpunit/student_answers_relation_test.php b/server/moodle/mod/livequiz/tests/phpunit/student_answers_relation_test.php new file mode 100644 index 000000000..b1be6dfab --- /dev/null +++ b/server/moodle/mod/livequiz/tests/phpunit/student_answers_relation_test.php @@ -0,0 +1,115 @@ +. + +/** + * LiveQuiz student_answers_relation_test + * + * This class contains unit tests for functions in the student_answers_relation class. + * + * @package mod_livequiz + * @copyright 2024 Software AAU + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +namespace mod_livequiz; +use mod_livequiz\models\student_answers_relation; +use mod_livequiz\models\answer; + +/** + * student_answers_relation + */ +final class student_answers_relation_test extends \advanced_testcase { + /** + * Create participation test data. Used in every test. + * @return array + */ + protected function create_test_data(): array { + return $studentanswertestdata = [ + 'studentid' => 1, + 'participationid' => 1, + 'answerid' => 1, + ]; + } + + /** + * Create answer test data. + * @return answer[] + */ + protected function create_answer_data(): array { + global $DB; + $answers = []; + for ($i = 0; $i < 10; $i++) { + $answer = new answer(1, 'Answer Option' . $i, 'Answer Explenation' . $i); + $answerid = answer::insert_answer($answer); + $answers[] = answer::get_answer_from_id($answerid); // This ensures id's are set since set_id() is private. + } + return $answers; + } + /** + * Setup before each test. + * @return void + */ + protected function setUp(): void { + parent::setUp(); + $this->resetAfterTest(true); + } + /** + * Test of insert_student_answer_relation + * It is impossible to assert the actual table id, since it changes every time. + * @covers \mod_livequiz\models\student_answers_relation::insert_student_answer_relation + * @return void + */ + public function test_insert_student_answer_relation(): void { + $data = $this->create_test_data(); + + // If this call returns an id, it means the data was inserted correctly. + $actual = student_answers_relation::insert_student_answer_relation( + $data['studentid'], + $data['answerid'], + $data['participationid'], + ); + + $this->assertIsNumeric($actual); + $this->assertGreaterThan(0, $actual); + } + /** + * Test of get_answersids_from_student_in_participation + * @covers \mod_livequiz\models\student_quiz_relation::get_answersids_from_student_in_participation + * @return void + */ + public function test_get_answersids_from_student_in_participation(): void { + $studentanswerdata = $this->create_test_data(); + $answerdata = $this->create_answer_data(); + + // Simulates multiple answers to a participation from a student. + for ($i = 0; $i < 10; $i++) { + student_answers_relation::insert_student_answer_relation( + $studentanswerdata['studentid'], + $answerdata[$i]->get_id(), + $studentanswerdata['participationid'], + ); + } + + // Get all answers for a student in a participation. + $answers = student_answers_relation::get_answersids_from_student_in_participation( + $studentanswerdata['studentid'], + $studentanswerdata['participationid'] + ); + // For each answer ensure we are fetching the same id's we inserted. + for ($i = 0; $i < 10; $i++) { + $this->assertEquals($answers[$i], $answerdata[$i]->get_id()); + } + } +} \ No newline at end of file From 56f15cc0e7f460ca99833f6b07bf353d613f1352 Mon Sep 17 00:00:00 2001 From: Adomas Ciplys Date: Sat, 9 Nov 2024 11:22:43 +0100 Subject: [PATCH 5/8] Added tests for the livequiz service --- .../tests/phpunit/livequiz_service_test.php | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/server/moodle/mod/livequiz/tests/phpunit/livequiz_service_test.php b/server/moodle/mod/livequiz/tests/phpunit/livequiz_service_test.php index e1d94fe6c..22c8d53c4 100644 --- a/server/moodle/mod/livequiz/tests/phpunit/livequiz_service_test.php +++ b/server/moodle/mod/livequiz/tests/phpunit/livequiz_service_test.php @@ -319,4 +319,50 @@ public function test_update_livequiz(): void { self::assertEquals($questions[1], $finalquestions[1]); self::assertEquals($questions[2], $finalquestions[2]); } + + /** + * Test getting answers from a student in participation. + * + * @covers \mod_livequiz\services\livequiz_services::get_answers_from_stundent_in_participation + * @return void + */ + public function test_get_answers_from_stundent_in_participation(): void { + global $DB; + $service = livequiz_services::get_singleton_service_instance(); + $livequiz = $this->create_livequiz_with_questions_and_answers_for_test(); + $question = $livequiz->get_questions()[0]; + $answers = $question->get_answers(); + $answercount = count($answers); + + $answerswithid = []; + + // Insert answers into db. + for ($i = 0; $i < $answercount; $i++) { + $answerid = answer::insert_answer($answers[$i]); + $answerswithid[] = answer::get_answer_from_id($answerid); // This ensures id's are set since set_id() is private. + // Simulate answers where studentid = 1 ; participationid = 1. + $DB->insert_record('livequiz_students_answers', [ + 'student_id' => 1, + 'answer_id' => $answerid, + 'participation_id' => 1, + ]); + } + // Fetch all answers for studentid = 1 ; participationid = 1. + $returnedanswers = $service->get_answers_from_stundent_in_participation(1, 1); + + $this->assertEquals($answerswithid[0]->get_id(), $returnedanswers[0]->get_id()); + $this->assertEquals($answerswithid[0]->get_correct(), $returnedanswers[0]->get_correct()); + $this->assertEquals($answerswithid[0]->get_description(), $returnedanswers[0]->get_description()); + $this->assertEquals($answerswithid[0]->get_explanation(), $returnedanswers[0]->get_explanation()); + + $this->assertEquals($answerswithid[1]->get_id(), $returnedanswers[1]->get_id()); + $this->assertEquals($answerswithid[1]->get_correct(), $returnedanswers[1]->get_correct()); + $this->assertEquals($answerswithid[1]->get_description(), $returnedanswers[1]->get_description()); + $this->assertEquals($answerswithid[1]->get_explanation(), $returnedanswers[1]->get_explanation()); + + $this->assertEquals($answerswithid[2]->get_id(), $returnedanswers[2]->get_id()); + $this->assertEquals($answerswithid[2]->get_correct(), $returnedanswers[2]->get_correct()); + $this->assertEquals($answerswithid[2]->get_description(), $returnedanswers[2]->get_description()); + $this->assertEquals($answerswithid[2]->get_explanation(), $returnedanswers[2]->get_explanation()); + } } From f43a8b394105bf7ea42feceaa6b60e994fa9b1a8 Mon Sep 17 00:00:00 2001 From: Adomas Ciplys Date: Sat, 9 Nov 2024 11:31:03 +0100 Subject: [PATCH 6/8] Fixed some spelling mistakes --- .../mod/livequiz/classes/services/livequiz_services.php | 2 +- .../tests/phpunit/student_answers_relation_test.php | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/server/moodle/mod/livequiz/classes/services/livequiz_services.php b/server/moodle/mod/livequiz/classes/services/livequiz_services.php index 780bbf2a9..f2c95cd8f 100644 --- a/server/moodle/mod/livequiz/classes/services/livequiz_services.php +++ b/server/moodle/mod/livequiz/classes/services/livequiz_services.php @@ -229,7 +229,7 @@ public function get_answers_from_stundent_in_participation(int $studentid, int $ $answers = []; $answerids = student_answers_relation::get_answersids_from_student_in_participation($studentid, $participationid); foreach ($answerids as $answerid) { - // There is chance we are getting an answer that does not exist. + // There is chance we are searching for an answer that does not exist. try { $answers[] = answer::get_answer_from_id($answerid); } catch (Exception $e) { diff --git a/server/moodle/mod/livequiz/tests/phpunit/student_answers_relation_test.php b/server/moodle/mod/livequiz/tests/phpunit/student_answers_relation_test.php index b1be6dfab..606dfaf42 100644 --- a/server/moodle/mod/livequiz/tests/phpunit/student_answers_relation_test.php +++ b/server/moodle/mod/livequiz/tests/phpunit/student_answers_relation_test.php @@ -67,7 +67,6 @@ protected function setUp(): void { } /** * Test of insert_student_answer_relation - * It is impossible to assert the actual table id, since it changes every time. * @covers \mod_livequiz\models\student_answers_relation::insert_student_answer_relation * @return void */ @@ -102,14 +101,14 @@ public function test_get_answersids_from_student_in_participation(): void { ); } - // Get all answers for a student in a participation. - $answers = student_answers_relation::get_answersids_from_student_in_participation( + // Get all answerids for a student in a participation. + $answerids = student_answers_relation::get_answersids_from_student_in_participation( $studentanswerdata['studentid'], $studentanswerdata['participationid'] ); // For each answer ensure we are fetching the same id's we inserted. for ($i = 0; $i < 10; $i++) { - $this->assertEquals($answers[$i], $answerdata[$i]->get_id()); + $this->assertEquals($answerids[$i], $answerdata[$i]->get_id()); } } } \ No newline at end of file From c04462c5542b6ab93961fa4b88468e5ebdbe0b2a Mon Sep 17 00:00:00 2001 From: Adomas Ciplys Date: Sat, 9 Nov 2024 11:40:46 +0100 Subject: [PATCH 7/8] Fixed codesniffer issues --- .../mod/livequiz/classes/services/livequiz_services.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/server/moodle/mod/livequiz/classes/services/livequiz_services.php b/server/moodle/mod/livequiz/classes/services/livequiz_services.php index f2c95cd8f..05f87098a 100644 --- a/server/moodle/mod/livequiz/classes/services/livequiz_services.php +++ b/server/moodle/mod/livequiz/classes/services/livequiz_services.php @@ -229,12 +229,7 @@ public function get_answers_from_stundent_in_participation(int $studentid, int $ $answers = []; $answerids = student_answers_relation::get_answersids_from_student_in_participation($studentid, $participationid); foreach ($answerids as $answerid) { - // There is chance we are searching for an answer that does not exist. - try { - $answers[] = answer::get_answer_from_id($answerid); - } catch (Exception $e) { - error_log('Could not get answer from id: ' . $answerid); - } + $answers[] = answer::get_answer_from_id($answerid); } return $answers; } From c22e04d5455b641238d6986fbb993f7559c4eca6 Mon Sep 17 00:00:00 2001 From: Adomas Ciplys Date: Sat, 9 Nov 2024 11:43:59 +0100 Subject: [PATCH 8/8] Codesniffer fixes again again --- .../livequiz/tests/phpunit/student_answers_relation_test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/moodle/mod/livequiz/tests/phpunit/student_answers_relation_test.php b/server/moodle/mod/livequiz/tests/phpunit/student_answers_relation_test.php index 606dfaf42..39347d2e5 100644 --- a/server/moodle/mod/livequiz/tests/phpunit/student_answers_relation_test.php +++ b/server/moodle/mod/livequiz/tests/phpunit/student_answers_relation_test.php @@ -111,4 +111,4 @@ public function test_get_answersids_from_student_in_participation(): void { $this->assertEquals($answerids[$i], $answerdata[$i]->get_id()); } } -} \ No newline at end of file +}