Skip to content

Commit

Permalink
GH-791 Continue refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
davidszkiba authored and mona-shakiba committed Feb 10, 2025
1 parent d3fd88f commit 5136943
Show file tree
Hide file tree
Showing 33 changed files with 215 additions and 225 deletions.
29 changes: 21 additions & 8 deletions classes/local/none.php → classes/local/fail.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,49 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Сlass status.
* Сlass fail.
*
* @package local_catquiz
* @copyright 2023 Wunderbyte GmbH <[email protected]>
* @copyright 2025 Wunderbyte GmbH <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_catquiz\local;

use Exception;
use local_catquiz\local\status;

/**
* Provides methods to obtain results.
*
* Overrides abstract methods from result
*
* @package local_catquiz
* @copyright 2023 Wunderbyte GmbH <[email protected]>
* @copyright 2025 Wunderbyte GmbH <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class none extends result {
class fail extends result {
/**
* Just returns the current result
*
* @return result
*/
public function and_then(callable $op): result {
return $this;
}

/**
* Calls the given callable
*
* @return result
*/
public function or_else(callable $op): result {
return $op($this);
}

public function expect() {
/**
* Throws an exception
*
* @throws Exception
*/
public function expect(): result {
throw new Exception($this->geterrormessage());
}
}
35 changes: 22 additions & 13 deletions classes/local/result.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Сlass status.
* Сlass result.
*
* @package local_catquiz
* @copyright 2023 Wunderbyte GmbH <[email protected]>
* @copyright 2025 Wunderbyte GmbH <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

Expand All @@ -30,9 +30,8 @@
/**
* Provides methods to obtain results.
*
*
* @package local_catquiz
* @copyright 2023 Wunderbyte GmbH <[email protected]>
* @copyright 2025 Wunderbyte GmbH <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class result {
Expand Down Expand Up @@ -63,33 +62,43 @@ public function __construct($value = null, string $status = status::OK) {
* @param string $status
* @param mixed|null $value
*
* @return none
*
* @return fail
*/
public static function err(string $status = status::ERROR_GENERAL, $value = null) {
return new none($value, $status);
public static function err(string $status = status::ERROR_GENERAL, $value = null): fail {
return new fail($value, $status);
}

/**
* Returns OK result.
*
* @param mixed|null $value
*
* @return some
*
* @return success
*/
public static function ok($value = null) {
return new some($value, status::OK);
public static function ok($value = null): success {
return new success($value, status::OK);
}

/**
* Calls the given callable if successful
*
* @return result
*/
abstract public function and_then(callable $op): result;

/**
* Calls the given callable if not successful
*
* @return result
*/
abstract public function or_else(callable $op): result;

/**
* Throws an exception if not successful
*
* @throws Exception
*/
abstract public function expect();
abstract public function expect(): result;

/**
* Returns status.
Expand Down
29 changes: 19 additions & 10 deletions classes/local/some.php → classes/local/success.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,45 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Сlass status.
* Сlass success.
*
* @package local_catquiz
* @copyright 2023 Wunderbyte GmbH <[email protected]>
* @copyright 2025 Wunderbyte GmbH <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_catquiz\local;

use local_catquiz\local\status;

/**
* Provides methods to obtain results.
*
* Overrides abstract methods from result
*
* @package local_catquiz
* @copyright 2023 Wunderbyte GmbH <[email protected]>
* @copyright 2025 Wunderbyte GmbH <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class some extends result {

class success extends result {
/**
* Calls the given callable
*
* @return result
*/
public function and_then(callable $op): result {
return $op($this);
}

/**
* Just returns the current result
*
* @return result
*/
public function or_else(callable $op): result {
return $this;
}

public function expect() {
/**
* Just returns the current result
*/
public function expect(): result {
return $this;
}
}
2 changes: 1 addition & 1 deletion classes/teststrategy/preselect_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function process(array &$context, callable $nexttask): result {
* @param callable $next Callable that calls the next middleware
* @return result
*/
abstract public function run(array &$context, callable $next): result;
abstract public function run(array &$context): result;

/**
* If a middleware requires a specific key to be available in the $context
Expand Down
6 changes: 3 additions & 3 deletions classes/teststrategy/preselect_task/addscalestandarderror.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ class addscalestandarderror extends preselect_task {
* @return result
*
*/
public function run(array &$context, callable $next): result {
public function run(array &$context): result {
$this->progress = $context['progress'];
$responses = $this->progress->get_user_responses();
if (! $responses) {
return $next($context);
return result::ok($context);
}

$userresponses = model_responses::create_from_array([$context['userid'] => ['component' => $responses]]);
Expand All @@ -72,7 +72,7 @@ public function run(array &$context, callable $next): result {
$context['se'][$catscaleid] = $se;
}

return $next($context);
return result::ok($context);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions classes/teststrategy/preselect_task/checkpagereload.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class checkpagereload extends preselect_task {

/**
* @var progress $progress
*/
Expand All @@ -51,13 +50,14 @@ final class checkpagereload extends preselect_task {
* @return result
*
*/
public function run(array &$context, callable $next): result {
public function run(array &$context): result {
$this->progress = $context['progress'];
if (($this->progress->is_first_question() && !$this->progress->get_last_question())
if (
($this->progress->is_first_question() && !$this->progress->get_last_question())
|| $this->progress->has_new_response()
|| $this->progress->get_force_new_question()
) {
return $next($context);
return result::ok($context);
}

return result::ok($this->progress->get_last_question());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ class filterbyquestionsperscale extends preselect_task {
* @return result
*
*/
public function run(array &$context, callable $next): result {
public function run(array &$context): result {
$this->progress = $context['progress'];

if (!in_array($context['teststrategy'], [LOCAL_CATQUIZ_STRATEGY_ALLSUBS])) {
return $next($context);
return result::ok($context);
}

$minquestionsperscale = $context['min_attempts_per_scale'];
Expand All @@ -88,7 +88,7 @@ public function run(array &$context, callable $next): result {
PHP_EOL
);
}
return $next($context);
return result::ok($context);
}

// If we are here, not all scales have the minimum questions.
Expand All @@ -104,7 +104,7 @@ public function run(array &$context, callable $next): result {
);
}
}
return $next($context);
return result::ok($context);
}

/**
Expand Down
22 changes: 8 additions & 14 deletions classes/teststrategy/preselect_task/filterbystandarderror.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filterbystandarderror extends preselect_task {

/**
* @var progress
*/
private progress $progress;

/**
* @var callable $next
*/
private $next;

/**
* Run method.
*
Expand All @@ -64,21 +58,20 @@ class filterbystandarderror extends preselect_task {
* @return result
*
*/
public function run(array &$context, callable $next): result {
public function run(array &$context): result {
$this->context = $context;
$this->next = $next;
$this->progress = $context['progress'];

if ($this->progress->is_first_question()) {
return $next($context);
return result::ok($context);
}

if (!$this->progress->has_new_response()) {
return $next($context);
return result::ok($context);
}

if ($this->progress->get_last_question()->is_pilot) {
return $next($context);
return result::ok($context);
}

$lastquestion = $this->progress->get_last_question();
Expand All @@ -98,7 +91,8 @@ public function run(array &$context, callable $next): result {
getenv('CATQUIZ_CREATE_TESTOUTPUT') && printf(
"%d: [SE] drop %s%s",
count($this->progress->get_playedquestions()),
(catscale::return_catscale_object($scaleid))->name, PHP_EOL
(catscale::return_catscale_object($scaleid))->name,
PHP_EOL
);
$this->progress->drop_scale($scaleid);
$inheritscales = $this->get_scale_heirs($scaleid);
Expand Down Expand Up @@ -144,7 +138,7 @@ public function run(array &$context, callable $next): result {
}
}

return $next($context);
return result::ok($context);
}

/**
Expand Down Expand Up @@ -210,7 +204,7 @@ private function filter_for_cat(array $updatedscales): result {
if ($drop) {
return result::err(status::ERROR_NO_REMAINING_QUESTIONS);
}
return ($this->next)($this->context);
return result::ok($this->context);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions classes/teststrategy/preselect_task/filterbytestinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filterbytestinfo extends preselect_task {

/**
* @var progress
*/
Expand All @@ -55,17 +54,18 @@ class filterbytestinfo extends preselect_task {
* @return result
*
*/
public function run(array &$context, callable $next): result {
public function run(array &$context): result {
$this->context = $context;
$this->progress = $context['progress'];

if (!in_array($context['teststrategy'], [ // TODO: use something like strategy::supports_dynamic_scales()!
if (!in_array($context['teststrategy'], [
LOCAL_CATQUIZ_STRATEGY_LOWESTSUB,
LOCAL_CATQUIZ_STRATEGY_HIGHESTSUB,
LOCAL_CATQUIZ_STRATEGY_RELSUBS,
LOCAL_CATQUIZ_STRATEGY_ALLSUBS,
])) {
return $next($context);
])
) {
return result::ok($context);
}

foreach ($this->progress->get_abilities() as $scaleid => $ability) {
Expand Down Expand Up @@ -146,7 +146,7 @@ public function run(array &$context, callable $next): result {
}
}

return $next($context);
return result::ok($context);
}

/**
Expand Down
Loading

0 comments on commit 5136943

Please sign in to comment.