Skip to content

Commit

Permalink
Merge pull request #26 from codecraft/pulls/reset-to-first-question
Browse files Browse the repository at this point in the history
Updated reset to go to the first question
  • Loading branch information
jakxnz authored Oct 28, 2020
2 parents f6cd6b2 + 46b6dd2 commit f97b373
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ env:

matrix:
include:
- php: 7.0
env: DB=MYSQL RECIPE_VERSION=4.3.6 PHPUNIT_COVERAGE_TEST=1
- php: 7.1
env: DB=MYSQL RECIPE_VERSION=4.3.6 PHPUNIT_TEST=1
env: DB=MYSQL RECIPE_VERSION=4.3.6 PHPUNIT_COVERAGE_TEST=1
- php: 7.2
env: DB=MYSQL RECIPE_VERSION=4.4.7 PHPUNIT_TEST=1
- php: 7.3
Expand Down
37 changes: 30 additions & 7 deletions src/Control/PathfinderRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public function getResults()
$selfExcludedIds = SiteTree::get()->filter(['HideFromPathfinders' => true])->column();
$excludePageIds = $this->ExcludedPages()->column();
$excludeIds = array_merge($excludePageIds, $selfExcludedIds);

if ($this->data()->getPage()) {
// Also exclude the Pathfinder's page
$excludeIds[] = $this->data()->getPage()->ID;
Expand Down Expand Up @@ -544,14 +544,34 @@ public function Link($action = null)
* @return string
*/
public function getStartLink()
{
if (!$this->Questions()->count()) {
return $this->Link('?questions-missing=1');
}

return $this->Link('start');
}

/**
* @return string
*/
public function getResetLink()
{
return $this->Link('reset');
}

/**
* @return string|null
*/
public function getFirstQuestionLink()
{
$questions = $this->Questions();

if (!$questions->count()) {
return $this->Link('?questions-missing=1');
return null;
}

return $this->Link(sprintf('start'));
return $this->Link(sprintf('question?id=%s&step=1', $questions->first()->ID));
}

/**
Expand Down Expand Up @@ -639,17 +659,20 @@ public function index()
*/
public function start()
{
$link = $this->Link(sprintf('question?id=%s&step=1', $this->Questions()->first()->ID));

return $this->clearAll()->redirect($this->getStore()->augmentURL($link));
// Take the user to the first question
return $this->clearAll()->redirect($this->getStore()->augmentURL($this->getFirstQuestionLink()));
}

/**
* @return HTTPResponse
*/
public function reset()
{
return $this->clearAll()->redirect($this->getStore()->augmentURL($this->getController()->Link()));
// Clear the store
$this->getStore()->clear();

// Take the user to the first question
return $this->clearAll()->redirect($this->getStore()->augmentURL($this->getFirstQuestionLink()));
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Model/Pathfinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ public static function reset_link($arguments, $content = null)

return sprintf(
'<a href="%s" title="%s">%s</a>',
$page->Link(Controller::join_links(
PathfinderPageController::config()->get('pathfinder_url_segment'),
'pathfinder/reset'
)),
$page->Pathfinder()->getRequestHandler()->getResetLink(),
$content,
$content
);
Expand Down
2 changes: 1 addition & 1 deletion tests/units/PathfinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testResetLinkShortCode()

$this->assertSame(
Pathfinder::reset_link([], 'Start again'),
'<a href="/pathfinder-page/pathfinder/reset" title="Start again">Start again</a>',
'<a href="/pathfinder-page/pathfinder/reset/" title="Start again">Start again</a>',
'Reset link should produce expected link attributes and content'
);
}
Expand Down

0 comments on commit f97b373

Please sign in to comment.