Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Student Answer Relation #128

Merged
merged 8 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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;
use 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): int {
global $DB;
return $DB->insert_record('livequiz_students_answers', [
'student_id' => $studentid,
'answer_id' => $answerid,
'participation_id' => $participationid,
]);
}

/**
* Get all answers for a student in a given participation
*
* @param int $studentid
* @param int $participationid
* @return array An array of answer id's
* @throws dml_exception
*/
public static function get_answersids_from_student_in_participation(int $studentid, int $participationid): array {
global $DB;

$answerrecords = $DB->get_records(
'livequiz_students_answers',
['student_id' => $studentid, 'participation_id' => $participationid],
'',
'answer_id'
);
$answerids = array_column($answerrecords, 'answer_id');
return $answerids;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -215,4 +216,21 @@ 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) {
$answers[] = answer::get_answer_from_id($answerid);
}
return $answers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* 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
* @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 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($answerids[$i], $answerdata[$i]->get_id());
}
}
}
Loading