64 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			64 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
require_once "PicturedTask.php";
 | 
						|
 | 
						|
class OpenEndedTask extends PicturedTask
 | 
						|
{
 | 
						|
    public function __construct(array $a = null)
 | 
						|
    {
 | 
						|
        parent::__construct("openended", $a);
 | 
						|
 | 
						|
        $this->correct_answer = $a["correct_answer"] ?? [];
 | 
						|
        $this->player_answer = $this->player_answer ?? "";
 | 
						|
        $this->setMaxMark(1.0);
 | 
						|
    }
 | 
						|
 | 
						|
    public function addCorrectAnswer(string $ca): void {
 | 
						|
        $this->correct_answer[] = $ca;
 | 
						|
    }
 | 
						|
 | 
						|
    public function clearCorrectAnswers(): void {
 | 
						|
        $this->correct_answer = [];
 | 
						|
    }
 | 
						|
 | 
						|
    public function saveAnswer(mixed $ans): bool
 | 
						|
    {
 | 
						|
        // collect transformations
 | 
						|
        $transform_fns = [];
 | 
						|
        foreach ($this->getFlags() as $flag) {
 | 
						|
            switch ($flag) {
 | 
						|
                case "makeuppercase":
 | 
						|
                    $transform_fns[] = "strtoupper";
 | 
						|
                    break;
 | 
						|
                case "makelowercase":
 | 
						|
                    $transform_fns[] = "strtolower";
 | 
						|
                    break;
 | 
						|
                case "removespaces":
 | 
						|
                    $transform_fns[] = fn($str) => str_replace(" ", "", $str);
 | 
						|
                    break;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        // run the transformations
 | 
						|
        foreach ($transform_fns as $tfn) {
 | 
						|
            $ans = $tfn($ans);
 | 
						|
        }
 | 
						|
 | 
						|
        // save the answer
 | 
						|
        $this->player_answer = $ans;
 | 
						|
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    public function staticCheck(): void
 | 
						|
    {
 | 
						|
        $mark = in_array($this->player_answer, $this->correct_answer) ? $this->getMaxMark() : 0.0;
 | 
						|
        $this->setMark($mark);
 | 
						|
    }
 | 
						|
 | 
						|
    function toArray(string $mode = "all"): array {
 | 
						|
        $a = parent::toArray($mode);
 | 
						|
        $a["correct_answer"] = $this->correct_answer;
 | 
						|
        return $a;
 | 
						|
    }
 | 
						|
} |