Project 'ulrike.pado/asyst-moodle-plugin' was moved to 'knight/asyst-moodle-plugin'. Please update any links and bookmarks that may still have the old path.
Commit 9acbb49a authored by Artem Baranovskyi's avatar Artem Baranovskyi
Browse files

Added forming mark input form name identifier.

Some code style fixes.
2 merge requests!3Fixed whole Docker infrastructure with dummy moodle plugin.,!2Draft: Resolve "can the post be sanitised before passing to the external service?"
Showing with 63 additions and 26 deletions
+63 -26
...@@ -14,11 +14,21 @@ from sklearn.linear_model import LogisticRegression, Perceptron ...@@ -14,11 +14,21 @@ from sklearn.linear_model import LogisticRegression, Perceptron
from sklearn.metrics import confusion_matrix from sklearn.metrics import confusion_matrix
from sklearn.model_selection import cross_validate, cross_val_predict 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" __author__ = "Yunus Eryilmaz"
__version__ = "1.0" __version__ = "1.0"
__date__ = "21.07.2021" __date__ = "21.07.2021"
__source__ = "https://pypi.org/project/sentence-transformers/0.3.0/" __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): def process_data(data):
...@@ -26,7 +36,6 @@ def process_data(data): ...@@ -26,7 +36,6 @@ def process_data(data):
parser.add_argument( parser.add_argument(
"--model_dir", "--model_dir",
# default=None,
default="/var/www/html/moodle/asyst/Source/Skript/german/models", default="/var/www/html/moodle/asyst/Source/Skript/german/models",
type=str, type=str,
required=False, required=False,
......
...@@ -2,18 +2,29 @@ ...@@ -2,18 +2,29 @@
namespace local_asystgrade\api; namespace local_asystgrade\api;
use Exception;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
class client { class client {
private $endpoint; private $endpoint;
private $httpClient; private $httpClient;
/**
* @param string $endpoint
* @param http_client|null $httpClient
*/
public function __construct(string $endpoint, http_client $httpClient = null) { public function __construct(string $endpoint, http_client $httpClient = null) {
$this->endpoint = $endpoint; $this->endpoint = $endpoint;
$this->httpClient = $httpClient ?: new http_client(); $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); $response = $this->httpClient->post($this->endpoint, $data);
return $response; return $response;
......
...@@ -2,10 +2,20 @@ ...@@ -2,10 +2,20 @@
namespace local_asystgrade\api; namespace local_asystgrade\api;
use Exception;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
class http_client implements http_client_interface { 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); $ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
...@@ -14,13 +24,13 @@ class http_client implements http_client_interface { ...@@ -14,13 +24,13 @@ class http_client implements http_client_interface {
$response = curl_exec($ch); $response = curl_exec($ch);
if (curl_errno($ch)) { if (curl_errno($ch)) {
throw new \Exception('Curl error: ' . curl_error($ch)); throw new Exception('Curl error: ' . curl_error($ch));
} }
curl_close($ch); curl_close($ch);
if ($response === false) { if ($response === false) {
throw new \Exception('Error sending data to API'); throw new Exception('Error sending data to API');
} }
return $response; return $response;
......
...@@ -5,5 +5,11 @@ namespace local_asystgrade\api; ...@@ -5,5 +5,11 @@ namespace local_asystgrade\api;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
interface http_client_interface { 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
...@@ -20,9 +20,7 @@ ...@@ -20,9 +20,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
//use Exception; use local_asystgrade\api\client;
//use local_asystgrade\api\client;
//require_once $dirroot . 'local_asystgrade\api\client.php';
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
...@@ -38,7 +36,7 @@ function local_asystgrade_before_footer() ...@@ -38,7 +36,7 @@ function local_asystgrade_before_footer()
global $PAGE, $DB; global $PAGE, $DB;
// Получение параметров из URL // Получение параметров из URL
$qid = optional_param('qid', null, PARAM_INT); $qid = optional_param('qid', null, PARAM_INT);
$slot = optional_param('slot', false, PARAM_INT); $slot = optional_param('slot', false, PARAM_INT);
if ($PAGE->url->compare(new moodle_url('/mod/quiz/report.php'), URL_MATCH_BASE) && $slot) { if ($PAGE->url->compare(new moodle_url('/mod/quiz/report.php'), URL_MATCH_BASE) && $slot) {
...@@ -63,9 +61,10 @@ function local_asystgrade_before_footer() ...@@ -63,9 +61,10 @@ function local_asystgrade_before_footer()
)->graderinfo; )->graderinfo;
$studentAnswers = []; $studentAnswers = [];
$inputNames = [];
foreach ($question_attempts as $question_attempt) { foreach ($question_attempts as $question_attempt) {
// Получение всех шагов для данного questionusageid // Obtaining all steps for this questionusageid
$quizattempt_steps = $DB->get_recordset( $quizattempt_steps = $DB->get_recordset(
'question_attempt_steps', 'question_attempt_steps',
[ [
...@@ -78,7 +77,7 @@ function local_asystgrade_before_footer() ...@@ -78,7 +77,7 @@ function local_asystgrade_before_footer()
// Processing every quiz attempt step // Processing every quiz attempt step
foreach ($quizattempt_steps as $quizattempt_step) { foreach ($quizattempt_steps as $quizattempt_step) {
if ($quizattempt_step->state === 'complete') { if ($quizattempt_step->state === 'complete') {
$userid = $quizattempt_step->userid; $userid = $quizattempt_step->userid;
$attemptstepid = $quizattempt_step->id; $attemptstepid = $quizattempt_step->id;
// Obtaining student's answer // Obtaining student's answer
...@@ -95,7 +94,10 @@ function local_asystgrade_before_footer() ...@@ -95,7 +94,10 @@ function local_asystgrade_before_footer()
// Forming student's answers array // Forming student's answers array
$studentAnswers[] = $studentAnswer; $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() ...@@ -124,7 +126,7 @@ function local_asystgrade_before_footer()
// Initializing API client // Initializing API client
try { try {
$apiClient = new \local_asystgrade\api\client($apiendpoint); $apiClient = new client($apiendpoint);
error_log('ApiClient initiated.'); error_log('ApiClient initiated.');
// Sending data on API and obtaining auto grades // Sending data on API and obtaining auto grades
...@@ -144,14 +146,13 @@ function local_asystgrade_before_footer() ...@@ -144,14 +146,13 @@ function local_asystgrade_before_footer()
$script = " $script = "
<script type='text/javascript'> <script type='text/javascript'>
document.addEventListener('DOMContentLoaded', function() {"; document.addEventListener('DOMContentLoaded', function() {";
foreach ($grades as $index => $grade) { foreach ($grades as $index => $grade) {
if (isset($grade['predicted_grade'])) { if (isset($grade['predicted_grade'])) {
$predicted_grade = $grade['predicted_grade'] == 'correct' ? 1 : 0; $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 // 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 // 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 = $inputNames[$index]; // Q is an question attempt -> ID of mdl_quiz_attempts, :1_ is 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 .= "
$script .= "
console.log('Trying to update input: {$input_name} with grade: {$predicted_grade}'); console.log('Trying to update input: {$input_name} with grade: {$predicted_grade}');
var gradeInput = document.querySelector('input[name=\"{$input_name}\"]'); var gradeInput = document.querySelector('input[name=\"{$input_name}\"]');
if (gradeInput) { if (gradeInput) {
...@@ -160,8 +161,8 @@ function local_asystgrade_before_footer() ...@@ -160,8 +161,8 @@ function local_asystgrade_before_footer()
} else { } else {
console.log('Input not found: {$input_name}'); console.log('Input not found: {$input_name}');
}"; }";
} }
} }
$script .= " $script .= "
}); });
</script>"; </script>";
...@@ -174,7 +175,7 @@ function local_asystgrade_before_footer() ...@@ -174,7 +175,7 @@ function local_asystgrade_before_footer()
spl_autoload_register(function ($classname) { spl_autoload_register(function ($classname) {
// Check if the class name starts with our plugin's namespace // Check if the class name starts with our plugin's namespace
if (strpos($classname, 'local_asystgrade\\') === 0) { if (strpos($classname, 'local_asystgrade\\') === 0) {
// Преобразуем пространство имен в путь // Transforming the Namespace into the Path
$classname = str_replace('local_asystgrade\\', '', $classname); $classname = str_replace('local_asystgrade\\', '', $classname);
$classname = str_replace('\\', DIRECTORY_SEPARATOR, $classname); $classname = str_replace('\\', DIRECTORY_SEPARATOR, $classname);
$filepath = __DIR__ . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $classname . '.php'; $filepath = __DIR__ . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $classname . '.php';
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment