From 9acbb49ab0802f88b8357a40f319e3b9c3a052af Mon Sep 17 00:00:00 2001 From: Artem Baranovskyi <artem.baranovsky1980@gmail.com> Date: Thu, 8 Aug 2024 11:05:29 +0300 Subject: [PATCH] Added forming mark input form name identifier. Some code style fixes. --- asyst/Source/Skript/german/run_LR_SBERT.py | 13 ++++++- asystgrade/classes/api/client.php | 13 ++++++- asystgrade/classes/api/http_client.php | 16 ++++++-- .../classes/api/http_client_interface.php | 8 +++- asystgrade/lib.php | 39 ++++++++++--------- 5 files changed, 63 insertions(+), 26 deletions(-) diff --git a/asyst/Source/Skript/german/run_LR_SBERT.py b/asyst/Source/Skript/german/run_LR_SBERT.py index 1e92be2..c66c9e0 100755 --- a/asyst/Source/Skript/german/run_LR_SBERT.py +++ b/asyst/Source/Skript/german/run_LR_SBERT.py @@ -14,11 +14,21 @@ from sklearn.linear_model import LogisticRegression, Perceptron from sklearn.metrics import confusion_matrix from sklearn.model_selection import cross_validate, cross_val_predict +""" +This script has been adapted from the original script authored by Yunus Eryilmaz. + +This script has been modified to adapt the source and structure of input-output data +for specific use case (data is given as params, result returns as an array instead of files). +""" + __author__ = "Yunus Eryilmaz" __version__ = "1.0" __date__ = "21.07.2021" __source__ = "https://pypi.org/project/sentence-transformers/0.3.0/" - +__source__ = "https://transfer.hft-stuttgart.de/gitlab/ulrike.pado/ASYST/-/blob/main/Source/Skript/german/run_LR_SBERT.py" +__adapted_by__ = "Artem Baranovskyi" +__adaptation_date__ = "14.09.2024" +__adaptation_version__ = "1.0" def process_data(data): @@ -26,7 +36,6 @@ def process_data(data): parser.add_argument( "--model_dir", - # default=None, default="/var/www/html/moodle/asyst/Source/Skript/german/models", type=str, required=False, diff --git a/asystgrade/classes/api/client.php b/asystgrade/classes/api/client.php index 67f5bb2..61cdaa9 100755 --- a/asystgrade/classes/api/client.php +++ b/asystgrade/classes/api/client.php @@ -2,18 +2,29 @@ namespace local_asystgrade\api; +use Exception; + defined('MOODLE_INTERNAL') || die(); class client { private $endpoint; private $httpClient; + /** + * @param string $endpoint + * @param http_client|null $httpClient + */ public function __construct(string $endpoint, http_client $httpClient = null) { $this->endpoint = $endpoint; $this->httpClient = $httpClient ?: new http_client(); } - public function send_data($data) { + /** + * @param array $data + * @return bool|string + * @throws Exception + */ + public function send_data(array $data) { $response = $this->httpClient->post($this->endpoint, $data); return $response; diff --git a/asystgrade/classes/api/http_client.php b/asystgrade/classes/api/http_client.php index 0a65aa9..3c055d9 100755 --- a/asystgrade/classes/api/http_client.php +++ b/asystgrade/classes/api/http_client.php @@ -2,10 +2,20 @@ namespace local_asystgrade\api; +use Exception; + defined('MOODLE_INTERNAL') || die(); class http_client implements http_client_interface { - public function post($url, $data) { + + /** + * @param string $url + * @param array $data + * @return bool|string + * @throws Exception + */ + public function post(string $url, array $data): bool|string + { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); @@ -14,13 +24,13 @@ class http_client implements http_client_interface { $response = curl_exec($ch); if (curl_errno($ch)) { - throw new \Exception('Curl error: ' . curl_error($ch)); + throw new Exception('Curl error: ' . curl_error($ch)); } curl_close($ch); if ($response === false) { - throw new \Exception('Error sending data to API'); + throw new Exception('Error sending data to API'); } return $response; diff --git a/asystgrade/classes/api/http_client_interface.php b/asystgrade/classes/api/http_client_interface.php index 9c76ff5..c1dd3e5 100755 --- a/asystgrade/classes/api/http_client_interface.php +++ b/asystgrade/classes/api/http_client_interface.php @@ -5,5 +5,11 @@ namespace local_asystgrade\api; defined('MOODLE_INTERNAL') || die(); interface http_client_interface { - public function post($url, $data); + + /** + * @param string $url + * @param array $data + * @return bool|string + */ + public function post(string $url, array $data): bool|string; } \ No newline at end of file diff --git a/asystgrade/lib.php b/asystgrade/lib.php index 66bf60c..cc87de7 100755 --- a/asystgrade/lib.php +++ b/asystgrade/lib.php @@ -20,9 +20,7 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. -//use Exception; -//use local_asystgrade\api\client; -//require_once $dirroot . 'local_asystgrade\api\client.php'; +use local_asystgrade\api\client; defined('MOODLE_INTERNAL') || die(); @@ -38,7 +36,7 @@ function local_asystgrade_before_footer() global $PAGE, $DB; // Получение параметров из URL - $qid = optional_param('qid', null, PARAM_INT); + $qid = optional_param('qid', null, PARAM_INT); $slot = optional_param('slot', false, PARAM_INT); if ($PAGE->url->compare(new moodle_url('/mod/quiz/report.php'), URL_MATCH_BASE) && $slot) { @@ -63,9 +61,10 @@ function local_asystgrade_before_footer() )->graderinfo; $studentAnswers = []; + $inputNames = []; foreach ($question_attempts as $question_attempt) { - // Получение вÑех шагов Ð´Ð»Ñ Ð´Ð°Ð½Ð½Ð¾Ð³Ð¾ questionusageid + // Obtaining all steps for this questionusageid $quizattempt_steps = $DB->get_recordset( 'question_attempt_steps', [ @@ -78,7 +77,7 @@ function local_asystgrade_before_footer() // Processing every quiz attempt step foreach ($quizattempt_steps as $quizattempt_step) { if ($quizattempt_step->state === 'complete') { - $userid = $quizattempt_step->userid; + $userid = $quizattempt_step->userid; $attemptstepid = $quizattempt_step->id; // Obtaining student's answer @@ -95,7 +94,10 @@ function local_asystgrade_before_footer() // Forming student's answers array $studentAnswers[] = $studentAnswer; - error_log("User ID: $userid, Student Answer: $studentAnswer, Reference Answer: $referenceAnswer"); + // Forming correct mark text input field name: q + questionusageid : question's slot + _mark + $inputNames[] = "q" . $question_attempt->questionusageid . ":" . $question_attempt->slot . "_-mark"; + + error_log("User ID: $userid, Student Answer: $studentAnswer, Reference Answer: $referenceAnswer, Input Name: $inputNames[-1]"); } } @@ -124,7 +126,7 @@ function local_asystgrade_before_footer() // Initializing API client try { - $apiClient = new \local_asystgrade\api\client($apiendpoint); + $apiClient = new client($apiendpoint); error_log('ApiClient initiated.'); // Sending data on API and obtaining auto grades @@ -144,14 +146,13 @@ function local_asystgrade_before_footer() $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; - // How forms param name="q2:1_-mark" see at https://github.com/moodle/moodle/blob/main/question/behaviour/rendererbase.php#L132 - // and https://github.com/moodle/moodle/blob/main/question/engine/questionattempt.php#L381 , L407 - // TODO: fix question attempt -> ID and question attempt -> step - $input_name = "q" . ($index + 2) . ":1_-mark"; // Q is an question attempt -> ID of mdl_quiz_attempts, :1_ is question attempt -> step - $script .= " + foreach ($grades as $index => $grade) { + if (isset($grade['predicted_grade'])) { + $predicted_grade = $grade['predicted_grade'] == 'correct' ? 1 : 0; + // How forms param name="q2:1_-mark" see at https://github.com/moodle/moodle/blob/main/question/behaviour/rendererbase.php#L132 + // and https://github.com/moodle/moodle/blob/main/question/engine/questionattempt.php#L381 , L407 + $input_name = $inputNames[$index]; // Q is an question attempt -> ID of mdl_quiz_attempts, :1_ is question attempt -> step + $script .= " console.log('Trying to update input: {$input_name} with grade: {$predicted_grade}'); var gradeInput = document.querySelector('input[name=\"{$input_name}\"]'); if (gradeInput) { @@ -160,8 +161,8 @@ function local_asystgrade_before_footer() } else { console.log('Input not found: {$input_name}'); }"; - } - } + } + } $script .= " }); </script>"; @@ -174,7 +175,7 @@ function local_asystgrade_before_footer() spl_autoload_register(function ($classname) { // Check if the class name starts with our plugin's namespace if (strpos($classname, 'local_asystgrade\\') === 0) { - // Преобразуем проÑтранÑтво имен в путь + // Transforming the Namespace into the Path $classname = str_replace('local_asystgrade\\', '', $classname); $classname = str_replace('\\', DIRECTORY_SEPARATOR, $classname); $filepath = __DIR__ . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $classname . '.php'; -- GitLab