Skip to content

Commit

Permalink
refactor: use PHP 8 attributes to configure array_converter
Browse files Browse the repository at this point in the history
  • Loading branch information
MHajoha committed Oct 23, 2024
1 parent 7608d9b commit 316218b
Show file tree
Hide file tree
Showing 37 changed files with 693 additions and 297 deletions.
10 changes: 2 additions & 8 deletions classes/api/attempt_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

namespace qtype_questionpy\api;

use qtype_questionpy\array_converter\array_converter;
use qtype_questionpy\array_converter\converter_config;

defined('MOODLE_INTERNAL') || die;
use qtype_questionpy\array_converter\attributes\array_key;

/**
* A file used in an attempt at a QuestionPy question.
Expand All @@ -34,6 +31,7 @@ class attempt_file {
public string $name;

/** @var string|null */
#[array_key("mime_type")]
public ?string $mimetype = null;

/** @var string $data */
Expand All @@ -52,7 +50,3 @@ public function __construct(string $name, string $data, ?string $mimetype = null
$this->mimetype = $mimetype;
}
}

array_converter::configure(attempt_file::class, function (converter_config $config) {
$config->rename("mimetype", "mime_type");
});
12 changes: 3 additions & 9 deletions classes/api/attempt_scored.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

namespace qtype_questionpy\api;

use qtype_questionpy\array_converter\array_converter;
use qtype_questionpy\array_converter\converter_config;

defined('MOODLE_INTERNAL') || die;
use qtype_questionpy\array_converter\attributes\array_key;

/**
* A scored attempt at a QuestionPy question.
Expand All @@ -31,9 +28,11 @@
*/
class attempt_scored extends attempt {
/** @var string|null */
#[array_key("scoring_state")]
public ?string $scoringstate;

/** @var string */
#[array_key("scoring_code")]
public string $scoringcode;

/** @var float|null */
Expand All @@ -54,8 +53,3 @@ public function __construct(int $variant, attempt_ui $ui, string $scoringcode, ?
$this->scoringcode = $scoringcode;
}
}

array_converter::configure(attempt_scored::class, function (converter_config $config) {
$config->rename("scoringstate", "scoring_state")
->rename("scoringcode", "scoring_code");
});
10 changes: 2 additions & 8 deletions classes/api/attempt_started.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

namespace qtype_questionpy\api;

use qtype_questionpy\array_converter\array_converter;
use qtype_questionpy\array_converter\converter_config;

defined('MOODLE_INTERNAL') || die;
use qtype_questionpy\array_converter\attributes\array_key;

/**
* Response from the server for a newly started attempt.
Expand All @@ -31,6 +28,7 @@
*/
class attempt_started extends attempt {
/** @var string */
#[array_key("attempt_state")]
public string $attemptstate;

/**
Expand All @@ -45,7 +43,3 @@ public function __construct(int $variant, attempt_ui $ui, string $attemptstate)
$this->attemptstate = $attemptstate;
}
}

array_converter::configure(attempt_started::class, function (converter_config $config) {
$config->rename("attemptstate", "attempt_state");
});
22 changes: 8 additions & 14 deletions classes/api/attempt_ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

namespace qtype_questionpy\api;

use qtype_questionpy\array_converter\array_converter;
use qtype_questionpy\array_converter\converter_config;

defined('MOODLE_INTERNAL') || die;
use qtype_questionpy\array_converter\attributes\array_element_class;
use qtype_questionpy\array_converter\attributes\array_key;

/**
* Model defining an attempt's UI source and associated data, such as parameters for placeholders.
Expand All @@ -34,24 +32,30 @@ class attempt_ui {
public string $formulation;

/** @var string|null */
#[array_key("general_feedback")]
public ?string $generalfeedback = null;

/** @var string|null */
#[array_key("specific_feedback")]
public ?string $specificfeedback = null;

/** @var string|null */
#[array_key("right_answer")]
public ?string $rightanswer = null;

/** @var array<string, string> string to string mapping of placeholder names to the values (to be replaced in the content) */
public array $placeholders = [];

/** @var string[]|null */
#[array_key("css_files")]
public ?array $cssfiles = null;

/** @var array<string, attempt_file> specifics TBD */
#[array_element_class(attempt_file::class)]
public array $files = [];

/** @var string specifics TBD */
#[array_key("cache_control")]
public string $cachecontrol = "PRIVATE_CACHE";

/**
Expand All @@ -63,13 +67,3 @@ public function __construct(string $formulation) {
$this->formulation = $formulation;
}
}

array_converter::configure(attempt_ui::class, function (converter_config $config) {
$config
->rename("generalfeedback", "general_feedback")
->rename("specificfeedback", "specific_feedback")
->rename("rightanswer", "right_answer")
->rename("cssfiles", "css_files")
->rename("cachecontrol", "cache_control")
->array_elements("files", attempt_file::class);
});
13 changes: 3 additions & 10 deletions classes/api/question_edit_form_response.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@

namespace qtype_questionpy\api;

use qtype_questionpy\array_converter\array_converter;
use qtype_questionpy\array_converter\converter_config;
use qtype_questionpy\array_converter\attributes\array_key;
use qtype_questionpy\form\qpy_form;

defined('MOODLE_INTERNAL') || die;

/**
* Response from the server to a request for the question edit form.
*
Expand All @@ -35,21 +32,17 @@ class question_edit_form_response {
public qpy_form $definition;

/** @var array */
#[array_key("form_data")]
public array $formdata;

/**
* Initialize a new question response.
*
* @param qpy_form $definition form definition
* @param array $formdata current values of the form elements
* @param array $formdata current values of the form elements
*/
public function __construct(qpy_form $definition, array $formdata) {
$this->definition = $definition;
$this->formdata = $formdata;
}
}

array_converter::configure(question_edit_form_response::class, function (converter_config $config) {
$config
->rename("formdata", "form_data");
});
25 changes: 9 additions & 16 deletions classes/api/question_response.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

namespace qtype_questionpy\api;

use qtype_questionpy\array_converter\array_converter;
use qtype_questionpy\array_converter\converter_config;

defined('MOODLE_INTERNAL') || die;
use qtype_questionpy\array_converter\attributes\array_key;

/**
* Response from the server for a created or updated question.
Expand All @@ -31,48 +28,44 @@
*/
class question_response {
/** @var string */
#[array_key("question_state")]
public string $state;

/** @var string */
#[array_key("scoring_method")]
public string $scoringmethod;

/** @var float|int */
#[array_key("score_min")]
public float $scoremin = 0;

/** @var float|int */
#[array_key("score_max")]
public float $scoremax = 1;

/** @var float|null */
public ?float $penalty = null;

/** @var float|null */
#[array_key("random_guess_score")]
public ?float $randomguessscore = null;

/** @var bool */
#[array_key("render_every_view")]
public bool $rendereveryview = false;

/** @var string|null */
#[array_key("general_feedback")]
public ?string $generalfeedback = null;

/**
* Initialize a new question response.
*
* @param string $state new question state
* @param string $state new question state
* @param string $scoringmethod
*/
public function __construct(string $state, string $scoringmethod) {
$this->state = $state;
$this->scoringmethod = $scoringmethod;
}
}

array_converter::configure(question_response::class, function (converter_config $config) {
$config
->rename("state", "question_state")
->rename("scoringmethod", "scoring_method")
->rename("scoremin", "score_min")
->rename("scoremax", "score_max")
->rename("randomguessscore", "random_guess_score")
->rename("rendereveryview", "render_every_view")
->rename("generalfeedback", "general_feedback");
});
13 changes: 3 additions & 10 deletions classes/api/status.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

namespace qtype_questionpy\api;

use qtype_questionpy\array_converter\array_converter;
use qtype_questionpy\array_converter\converter_config;

defined('MOODLE_INTERNAL') || die;
use qtype_questionpy\array_converter\attributes\array_key;

/**
* Response from the server containing server status.
Expand All @@ -37,9 +34,11 @@ class status {
public string $version;

/** @var bool */
#[array_key("allow_lms_packages")]
public bool $allowlmspackages = false;

/** @var string */
#[array_key("max_package_size")]
public string $maxpackagesize;

/** @var usage|null */
Expand All @@ -60,9 +59,3 @@ public function __construct(string $name, string $version, int $maxpackagesize)
$this->maxpackagesize = display_size($maxpackagesize, 1, 'MB');
}
}

array_converter::configure(status::class, function (converter_config $config) {
$config
->rename("allowlmspackages", "allow_lms_packages")
->rename("maxpackagesize", "max_package_size");
});
14 changes: 3 additions & 11 deletions classes/api/usage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@

namespace qtype_questionpy\api;

use qtype_questionpy\array_converter\array_converter;
use qtype_questionpy\array_converter\converter_config;
use qtype_questionpy\form\conditions\in;

defined('MOODLE_INTERNAL') || die;
use qtype_questionpy\array_converter\attributes\array_key;

/**
* Server usage.
Expand All @@ -32,9 +28,11 @@
*/
class usage {
/** @var int */
#[array_key("requests_in_process")]
public int $requestsinprocess;

/** @var int */
#[array_key("requests_in_queue")]
public int $requestsinqueue;

/**
Expand All @@ -48,9 +46,3 @@ public function __construct(int $requestsinprocess, int $requestsinqueue) {
$this->requestsinqueue = $requestsinqueue;
}
}

array_converter::configure(usage::class, function (converter_config $config) {
$config
->rename("requestsinprocess", "requests_in_process")
->rename("requestsinqueue", "requests_in_queue");
});
Loading

0 comments on commit 316218b

Please sign in to comment.