Commit 9acbb49a authored by Artem Baranovskyi's avatar Artem Baranovskyi
Browse files

Added forming mark input form name identifier.

Some code style fixes.
parent 9c0313f6
......@@ -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,
......
......@@ -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;
......
......@@ -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;
......
......@@ -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
......@@ -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();
......@@ -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',
[
......@@ -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
......@@ -149,8 +151,7 @@ function local_asystgrade_before_footer()
$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
$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}\"]');
......@@ -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';
......
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