Source of file ProgressStore.php
Size: 6,608 Bytes - Last Modified: 2021-12-24T06:40:50+00:00
/var/www/docs.ssmods.com/process/src/src/Model/Store/ProgressStore.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 | <?php namespace CodeCraft\Pathfinder\Model\Store; use CodeCraft\Pathfinder\Control\PathfinderRequestHandler; use CodeCraft\Pathfinder\Model\Answer; use CodeCraft\Pathfinder\Model\Pathfinder; use CodeCraft\Pathfinder\Model\Question; use SilverStripe\Control\RequestHandler; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Extensible; use SilverStripe\Core\Injector\Injectable; use SilverStripe\ORM\DataList; /** * A storage model for tracking a Pathfinder's previously answered questions */ class ProgressStore { use Configurable; use Injectable; use Extensible; /** * @var string */ private static $storage_name = 'PathfinderProgress'; /** * @var bool */ private static $validate_sequence = true; /** * @var array */ protected $entries = []; /** * @var Pathfinder */ protected $handler; /** * @param RequestHandler $handler * * @return void */ public function initAfterRequestHandler($handler) { // Do any useful things to initialise with the handler $this->setHandler($handler); } /** * Add an item to the current progress * * @param ProgressEntry $entry * * @return ProgressStore $this */ public function add(ProgressEntry $entry) { // Definitely don't eat our ->toArray() dog food here, // we want the store in its full form $entries = $this->get(); // Add the entry $entries[] = $entry; // Update our local reference $this->entries = $entries; return $this; } /** * Get the current progress * * @return array|ProgressEntry[] */ public function get() { return $this->entries; } /** * Set the current progress * * @param array $entries * * @return ProgressStore $this */ public function set(array $entries) { $this->entries = $entries; return $this; } /** * Clear the current progress store * * @return ProgressStore */ public function clear() { $this->set([]); return $this; } /** * @param int $pos * @return ProgressStore */ public function clearAfterPos($pos) { if ($pos <= 0) { return $this->clear(); } if ($this->count() < $pos) { return $this; } $this->set(array_slice($this->get(), 0, $pos)); return $this; } /** * Add progress by question, answer and choices * * @param Question $question * @param Answer $answer * @param DataList|Choice[] $choices * * @return ProgressStore $this */ public function addProgress(Question $question, Answer $answer, DataList $choices) { $entry = ProgressEntry::create([ 'QuestionID' => $question->ID, 'AnswerID' => $answer->ID, 'ChoiceIDs' => $choices->column() ]); $this->add($entry); return $this; } /** * @return Pathfinder */ public function getHandler() { return $this->handler; } /** * @param PathfinderRequestHandler $handler * @return ProgressStore $this */ public function setHandler($handler) { $this->handler = $handler; return $this; } /** * Invoked as part of {@see PathfinderRequestHandler::Form()} * * @return void */ public function updateForm($form) { return $form; } /** * Modify the redirect url used to continue to the next step in the pathfinder * * @param string $url * @param Form $form * @return string */ public function augmentURL($url, $form = null) { return $url; } /** * @return string */ public function getStorageName() { return sprintf( '%s%s', $this->config()->get('storage_name'), $this->getHandler() ? $this->getHandler()->ID : '' ); } /** * @param int $pos 0 = First item * @return ProgressEntry|null */ public function getByPos($pos) { $progress = $this->get(); $key = $pos - 1; if (!is_array($progress) || !count($progress) || !array_key_exists($key, $progress)) { return null; } $entry = $progress[$key]; if (!$entry instanceof ProgressEntry) { throw new \Exception(sprintf( 'Unexpected item in set of progress entries. Expected all entries to be a "%s"', ProgressEntry::class )); } return $entry; } /** * @return array */ public function toArray() { $entries = []; foreach ($this->get() as $entry) { if (!$entry instanceof ProgressEntry) { throw new \Exception(sprintf( 'Unexpected item in set of progress entries. Expected all entries to be a "%s"', ProgressEntry::class )); } $entries[] = $entry->toArray(); } return $entries; } /** * @return int */ public function count() { return count($this->get()); } /** * @return ProgressEntry|null */ public function last() { return $this->getByPos($this->count()); } /** * A heuristic for whether this story can/should determine if a specific * question is the next in sequence * * @return bool */ public function canValidateSequence() { return $this->config()->get('validate_sequence'); } /** * Determines if a question is the next in sequence * * @return bool|null */ public function isInSequence($question, $pos) { if ($pos == 1) { // The first question is always in sequence return true; } if ($pos > $this->count() + 1) { // Position is too far ahead return false; } $entry = $this->getByPos($pos - 1); $answer = Answer::get()->byID($entry ? $entry->AnswerID : 0); if (!$answer || !$answer->getNextQuestion()) { // No previous answer led to his question return false; } // Does the previous answer lead to the matching question? return $answer->getNextQuestion()->ID === $question->ID; } } |