Skip to content

Commit

Permalink
Merge pull request #205 from AAU-P5-Moodle/Repository-Refactor
Browse files Browse the repository at this point in the history
Fix/add -- Code better follows MVSC pattern and addition of repository pattern
  • Loading branch information
ChristofferGamel authored Dec 5, 2024
2 parents 95983ca + 4f6a372 commit 8dba264
Show file tree
Hide file tree
Showing 20 changed files with 705 additions and 349 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
use core_external\external_value;
use dml_exception;
use invalid_parameter_exception;
use mod_livequiz\models\livequiz_quiz_lecturer_relation;
use mod_livequiz\models\livequiz;
use mod_livequiz\models\quiz_questions_relation;
use mod_livequiz\models\question;
use mod_livequiz\services\livequiz_services;

/**
* Class get_lecturer_quiz
Expand Down Expand Up @@ -61,16 +59,17 @@ public static function execute(int $lecturerid): array {
self::validate_parameters(self::execute_parameters(), [
'lecturerid' => $lecturerid,
]);
$service = livequiz_services::get_singleton_service_instance();
// Get all quizzes from the lecturer.
$rawquizzes = livequiz_quiz_lecturer_relation::get_lecturer_quiz_relations_by_lecturer_id($lecturerid);
$rawquizzes = $service->get_lecturer_quiz_relations_by_lecturer_id($lecturerid);
$quizzes = [];
// Loop through all quizzes from the lecturer and find the corresponding questions.
foreach ($rawquizzes as $rawquiz) {
$quizobject = livequiz::get_livequiz_instance($rawquiz->quiz_id);
if (self::quiz_active($quizobject)) { // Check if the quiz is active/deleted.
$rawquestions = quiz_questions_relation::get_questions_from_quiz_id($rawquiz->quiz_id);
$rawquestions = $service->get_questions_from_quiz_id($rawquiz->quiz_id);
foreach ($rawquestions as $rawquestion) {
$question = question::get_question_from_id($rawquestion->get_id());
$question = $service->get_question_from_id($rawquestion->get_id());
$quizobject->add_question($question);
}
$preparedquiz = $quizobject->prepare_for_template();
Expand Down
11 changes: 6 additions & 5 deletions server/moodle/mod/livequiz/classes/external/reuse_question.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ public static function execute(int $quizid, array $questionids, int $lecturerid)
'questionids' => $questionids, // These are id's of the questions to be reused, in the new livequiz.
'lecturerid' => $lecturerid,
]);
$services = livequiz_services::get_singleton_service_instance();
$service = livequiz_services::get_singleton_service_instance();
try {
$livequiz = $services->get_livequiz_instance($quizid);
$livequiz = $service->get_livequiz_instance($quizid);
$existingquestions = $livequiz->get_questions(); // These are the questions already present in the livequiz.
$questionids = self::filter_unique_questions($questionids);
$questionstoadd = [];
foreach ($questionids as $id) {
$unique = true;
$tempquestion = question::get_question_with_answers_from_id($id);
$tempquestion = $service->get_question_with_answers_from_id($id);
$tempquestion->reset_id();
foreach ($tempquestion->get_answers() as $answer) {
$answer->reset_id();
Expand All @@ -94,7 +94,7 @@ public static function execute(int $quizid, array $questionids, int $lecturerid)
}
}
$livequiz->add_questions($questionstoadd);
$livequiz = $services->submit_quiz($livequiz, $lecturerid); // Refresh the livequiz object.
$livequiz = $service->submit_quiz($livequiz, $lecturerid); // Refresh the livequiz object.
$returnquestions = [];
$rawquestions = $livequiz->get_questions();
foreach ($rawquestions as $rawquestion) {
Expand Down Expand Up @@ -122,9 +122,10 @@ public static function execute_returns(): external_multiple_structure {
* @throws dml_exception
*/
private static function filter_unique_questions(array $questions): array {
$service = livequiz_services::get_singleton_service_instance();
$uniquequestions = [];
foreach ($questions as $questionid) {
$question = question::get_question_with_answers_from_id($questionid);
$question = $service->get_question_with_answers_from_id($questionid);
$unique = true;
foreach ($uniquequestions as $uniquequestion) {
// Check if the questions are identical.
Expand Down
90 changes: 9 additions & 81 deletions server/moodle/mod/livequiz/classes/models/answer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

namespace mod_livequiz\models;

use dml_exception;
use Exception;
use stdClass;

/**
Expand Down Expand Up @@ -64,62 +62,6 @@ public function __construct(int $correct, string $description, string $explanati
return $this;
}

/**
* Given an answer object, this method will insert the answer to the database
*
* @param answer $answer
* @return int
* @throws dml_exception
*/
public static function insert_answer(answer $answer): int {
global $DB;

$answerdata = [
'correct' => $answer->correct,
'description' => $answer->description,
'explanation' => $answer->explanation,
];

return $DB->insert_record('livequiz_answers', $answerdata);
}

/**
* Get an answer, given its id.
*
* @param int $id
* @return mixed
* @throws dml_exception
* @throws Exception
*/
public static function get_answer_from_id(int $id): answer {
global $DB;
$answerdata = $DB->get_record('livequiz_answers', ['id' => $id]);
if (!$answerdata) {
throw new Exception("No answer found in answers table with id: " . $id);
}
$answer = new answer($answerdata->correct, $answerdata->description, $answerdata->explanation);
$answer->set_id($answerdata->id);
return $answer;
}

/**
* Update an answer, given its id.
*
* @throws dml_exception
*/
public function update_answer(): void {
global $DB;

$answerdata = [
'id' => $this->id,
'correct' => $this->correct,
'description' => $this->description,
'explanation' => $this->explanation,
];

$DB->update_record('livequiz_answers', $answerdata);
}

/**
* Gets the ID of the answer.
*
Expand Down Expand Up @@ -156,22 +98,6 @@ public function get_explanation(): string {
return $this->explanation;
}


/**
* Gets the sanitized answer object.
*
* @return stdClass
*/
public function get_sanitized_answer(): stdClass {
$sanitizedanswer = new stdClass();

$sanitizedanswer->id = $this->id;
$sanitizedanswer->description = $this->description;
$sanitizedanswer->explanation = $this->explanation;

return $sanitizedanswer;
}

/**
* Sets the ID of the answer.
*
Expand Down Expand Up @@ -214,17 +140,19 @@ public function set_explanation(string $explanation): void {
public function reset_id(): void {
$this->set_id(0);
}

/**
* Deletes an answer from the database.
* Gets the sanitized answer object.
*
* @param int $answerid
* @return bool
* @throws dml_exception
* @return stdClass
*/
public static function delete_answer(int $answerid): bool {
global $DB;
public function get_sanitized_answer(): stdClass {
$sanitizedanswer = new stdClass();

$sanitizedanswer->id = $this->get_id();
$sanitizedanswer->description = $this->get_description();
$sanitizedanswer->explanation = $this->get_explanation();

return $DB->delete_records('livequiz_answers', ['id' => $answerid]);
return $sanitizedanswer;
}
}
18 changes: 0 additions & 18 deletions server/moodle/mod/livequiz/classes/models/livequiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,24 +127,6 @@ public static function get_livequiz_instance(int $id): livequiz {
);
}

/**
* Updates the livequiz in the database, and updates the timemodified field.
*
* @return bool
* @throws dml_exception
*/
public function update_quiz(): bool {
global $DB;

$this->set_timemodified();

$record = new stdClass();
$record->id = $this->get_id();
$record->timemodified = $this->get_timemodified();

return $DB->update_record('livequiz', $record);
}

/**
* Gets the associated ID for the livequiz.
*
Expand Down
31 changes: 0 additions & 31 deletions server/moodle/mod/livequiz/classes/models/participation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

namespace mod_livequiz\models;

use dml_exception;
use stdClass;

/**
* Class Participation
Expand Down Expand Up @@ -54,35 +52,6 @@ public function __construct($studentid, $livequizid) {
$this->studentid = $studentid;
$this->livequizid = $livequizid;
}
/**
* Summary of add_participation
* @return boolean
*/
public function add_participation(): bool {
global $DB;
$record = new stdClass();
$record->studentid = $this->studentid;
$record->livequizid = $this->livequizid;
$success = false;
try {
$success = $DB->insert_record('participation', $record);
} catch (dml_exception $dmle) {
echo $dmle->getMessage();
}
return $success;
}

/**
* Summary of get_participation_by_studentid
* @param $studentid
* @return false|mixed|stdClass
* @throws dml_exception
*/
public static function get_participation_by_studentid($studentid): mixed {
global $DB;
return $DB->get_record('participation', ['studentid' => $studentid]);
}

/**
* get_id
* @return int
Expand Down
Loading

0 comments on commit 8dba264

Please sign in to comment.