From 14748951dab9ba15b71f84f343fc45ce11a13def Mon Sep 17 00:00:00 2001
From: Artem Baranovskyi <artem.baranovsky1980@gmail.com>
Date: Mon, 26 Aug 2024 11:49:30 +0300
Subject: [PATCH] Fixed maximum mark issue. Added phpunit setup to
 infrastructure.

---
 Dockerfile                              | 14 +------------
 asystgrade/classes/privacy/provider.php |  0
 asystgrade/lib.php                      | 27 ++++++++++++++-----------
 asystgrade/tests/quiz_api_test.php      | 23 ++++++++++-----------
 install_moodle.sh                       | 14 ++++++++++++-
 readme.md                               |  4 ++++
 6 files changed, 44 insertions(+), 38 deletions(-)
 mode change 100644 => 100755 asystgrade/classes/privacy/provider.php

diff --git a/Dockerfile b/Dockerfile
index 35dacfd..6a5c4c2 100755
--- a/Dockerfile
+++ b/Dockerfile
@@ -17,7 +17,7 @@ ENV MOODLE_BASE_DIR_DATA ${MOODLE_BASE_DIR_DATA}
 
 # Installing necessary packages
 RUN apt-get update && apt-get upgrade -y && \
-    apt-get install -y apache2 php libapache2-mod-php php-mysqli php-mysql php-xml php-pdo php-pdo-mysql mariadb-client mariadb-server wget unzip p7zip-full python3 python3-pip iputils-ping php-mbstring graphviz aspell ghostscript clamav php8.2-pspell php8.2-curl php8.2-gd php8.2-intl php8.2-mysql php8.2-xml php8.2-xmlrpc php8.2-ldap php8.2-zip php8.2-soap php8.2-mbstring openssl git nano supervisor php-xdebug ca-certificates && \
+    apt-get install -y apache2 php libapache2-mod-php php-mysqli php-mysql php-xml php-pdo php-pdo-mysql mariadb-client mariadb-server wget unzip p7zip-full python3 python3-pip iputils-ping php-mbstring graphviz aspell ghostscript clamav php8.2-pspell php8.2-curl php8.2-gd php8.2-intl php8.2-mysql php8.2-xml php8.2-xmlrpc php8.2-ldap php8.2-zip php8.2-soap php8.2-mbstring openssl git nano supervisor php-xdebug && \
     apt-get clean && rm -rf /var/lib/apt/lists/*
 
 # Setting necessary php params
@@ -126,18 +126,6 @@ RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
     -out /etc/ssl/certs/apache-selfsigned.crt \
     -subj "/C=EU/ST=Berlin/L=Berlin/O=HFT/CN=www.moodle.loc"
 
-# Установка корневых сертификатов
-RUN update-ca-certificates
-
-RUN curl -O https://curl.se/ca/cacert.pem \
-    && mv cacert.pem /etc/ssl/certs/cacert.pem \
-
-# Настройка php.ini для OpenSSL
-RUN echo "openssl.cafile=/etc/ssl/certs/ca-certificates.crt" >> /etc/php/8.2/cli/php.ini \
-    && echo "openssl.capath=/etc/ssl/certs" >> /etc/php/8.2/cli/php.ini \
-    && echo "openssl.cafile=/etc/ssl/certs/ca-certificates.crt" >> /etc/php/8.2/apache2/php.ini \
-    && echo "openssl.capath=/etc/ssl/certs" >> /etc/php/8.2/apache2/php.ini \
-
 # Configure SSL virtual host
 RUN echo  \
     "<VirtualHost *:443>\n" \
diff --git a/asystgrade/classes/privacy/provider.php b/asystgrade/classes/privacy/provider.php
old mode 100644
new mode 100755
diff --git a/asystgrade/lib.php b/asystgrade/lib.php
index 77a2be3..94cfceb 100755
--- a/asystgrade/lib.php
+++ b/asystgrade/lib.php
@@ -36,7 +36,6 @@ defined('MOODLE_INTERNAL') || die();
 function local_asystgrade_before_footer()
 {
     global $PAGE;
-
     // Obtaining parameters from URL
     $qid = optional_param('qid', null, PARAM_INT);
     $slot = optional_param('slot', false, PARAM_INT);
@@ -51,7 +50,7 @@ function local_asystgrade_before_footer()
 
         $question_attempts = $quizQuery->get_question_attempts($qid, $slot);
         $referenceAnswer = $quizQuery->get_reference_answer($qid);
-
+        $maxmark = (float)$question_attempts->current()->maxmark;
         $data = prepare_api_data($quizQuery, $question_attempts, $referenceAnswer);
 
         foreach (array_keys($data['studentData']) as $studentId) {
@@ -92,7 +91,7 @@ function local_asystgrade_before_footer()
 
         error_log('After API call');
 
-        pasteGradedMarks($grades, $inputNames);
+        pasteGradedMarks($grades, $inputNames, $maxmark);
 
         error_log('URL matches /mod/quiz/report.php in page_init');
     }
@@ -100,17 +99,20 @@ function local_asystgrade_before_footer()
 
 /**
  * Adds JavasScript scrypt to update marks
- *
- * @param mixed $grades
- * @param mixed $inputNames
+ * @param  array $grades
+ * @param  array $inputNames
+ * @param  float $maxmark
  * @return void
  */
-function pasteGradedMarks(mixed $grades, mixed $inputNames): void
+
+function pasteGradedMarks(array $grades, array $inputNames, float $maxmark): void
 {
-    echo generate_script($grades, $inputNames);;
+    echo generate_script($grades, $inputNames, $maxmark);
 }
 
 /**
+ * Processes question attempts and answers to prepare for API a data to estimate answers
+ *
  * @param QuizQuery $database
  * @param $question_attempts
  * @param $referenceAnswer
@@ -153,17 +155,18 @@ function prepare_api_data(QuizQuery $database, $question_attempts, $referenceAns
 /**
  * Builds JavasScript scrypt to update marks using DOM manipulations
  *
- * @param $grades
- * @param $inputNames
+ * @param  array $grades
+ * @param  array $inputNames
+ * @param  float $maxmark
  * @return string
  */
-function generate_script($grades, $inputNames) {
+function generate_script(array $grades, array $inputNames, float $maxmark) {
     $script = "<script type='text/javascript'>
         document.addEventListener('DOMContentLoaded', function() {";
 
     foreach ($grades as $index => $grade) {
         if (isset($grade['predicted_grade'])) {
-            $predicted_grade = $grade['predicted_grade'] == 'correct' ? 1 : 0;
+            $predicted_grade = $grade['predicted_grade'] == 'correct' ? $maxmark : 0;
             $input_name = $inputNames[$index];
             $script .= "
                 console.log('Trying to update input: {$input_name} with grade: {$predicted_grade}');
diff --git a/asystgrade/tests/quiz_api_test.php b/asystgrade/tests/quiz_api_test.php
index 0fec02d..cf34328 100755
--- a/asystgrade/tests/quiz_api_test.php
+++ b/asystgrade/tests/quiz_api_test.php
@@ -13,6 +13,17 @@ require_once($CFG->dirroot . '/mod/quiz/tests/generator/lib.php');
 class quiz_api_test extends advanced_testcase
 {
 
+    protected function setUp(): void
+    {
+        global $DB;
+
+        $this->resetAfterTest();
+        parent::setUp();
+
+        $DB->execute('TRUNCATE TABLE {quiz_attempts}');
+        $DB->execute('TRUNCATE TABLE {quiz_slots}');
+    }
+
     /**
      * @throws Exception
      */
@@ -299,7 +310,6 @@ class quiz_api_test extends advanced_testcase
 
         return $DB->get_record('question_categories', array('id' => $categoryid));
     }
-
     private function create_quiz_attempt($quizid, $userid, $questionid, $exapmle_answers, $student_id)
     {
         global $DB;
@@ -385,15 +395,4 @@ class quiz_api_test extends advanced_testcase
             echo "Error inserting quiz attempt: " . $e->getMessage() . "\n";
         }
     }
-
-    protected function setUp(): void
-    {
-        global $DB;
-
-        $this->resetAfterTest();
-        parent::setUp();
-
-        $DB->execute('TRUNCATE TABLE {quiz_attempts}');
-        $DB->execute('TRUNCATE TABLE {quiz_slots}');
-    }
 }
diff --git a/install_moodle.sh b/install_moodle.sh
index 2c43916..f292d37 100755
--- a/install_moodle.sh
+++ b/install_moodle.sh
@@ -48,7 +48,19 @@ docker-compose exec moodle php ${MOODLE_BASE_DIR}/admin/cli/install.php \
  fi
 
 # Composer installation to run phpunit tests
- docker-compose exec moodle composer install
+ docker-compose exec moodle php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
+ docker-compose exec moodle php composer-setup.php --install-dir=/usr/local/bin --filename=composer
+ docker-compose exec moodle php -r "unlink('composer-setup.php');"
+ docker-compose exec moodle php composer install --no-interaction \
+                                                 --no-plugins \
+                                                 --no-scripts \
+                                                 --no-dev \
+                                                 --prefer-dist && composer dump-autoload
+
+docker-compose exec moodle apt-get update && apt-get install -y locales && \
+    echo "en_AU.UTF-8 UTF-8" >> /etc/locale.gen && \
+    locale-gen
+
 # Next, configure PHPUnit for Moodle:
  docker-compose exec moodle php admin/tool/phpunit/cli/init.php
 
diff --git a/readme.md b/readme.md
index 7afb314..7977046 100755
--- a/readme.md
+++ b/readme.md
@@ -12,6 +12,10 @@ Use these creds to access Moodle admin page
 admin:rootpassword
 These creds could be easily changed as other environmental variables at .env
 
+After installation the Database will have all necessary entities to check plugins functionality (Cource / Test / Students / QuizAttempts ...).
+
+For demo, it's quite enough to get the link https://www.moodle.loc/mod/quiz/report.php?id=2&mode=grading&slot=1&qid=1&grade=needsgrading and wait for auto answer valuation.
+
 ## Development tips
 To facilitate DB monitoring at IDE set such a Database connection URL: 
 ~~~bash
-- 
GitLab