Interesting use of snippets
Joel Palmius <[email protected]> Thu, 2 Mar 2006 15:00:06 +0100 (CET)
| Newsgroups | gmane.comp.apache.mod-survey.general |
|---|---|
| Message-ID | <[email protected]> |
I've just worked a bit with a survey containing a somewhat different
routing.
The survey is supposed to evaluate a number of "functions" at the
university. There are 40 of those, and there are seven questions about
each function. Clearly, asking everyone about all functions would cause a
lot of questions to be asked, especially since most of the respondents
have no idea whatsoever about some of the functions.
So idea is: Page 1, ask respondents which functions he is familiar with.
Plain MATRIX, are you familiar with ... yes/no for each function.
Following pages: Display one page per previously selected function, do not
show the pages which contain functions the user is not familiar with.
So problem becomes: How to route the user to show only, say, seven
user-selected pages?
Theory: Build an array in memory containing a list of pages the user still
have to visit. We do this as a pre-parse snippet:
{&
$thispage = "per";
if(!$session->getValue($thispage))
{
@tosee = ();
for($i = 1; $i <= 40; $i++)
{
$n = "" . $i;
if($i < 10) { $n = "0$n"; }
$subm = $session->getValue("SUBMITTED_kont$n");
if($subm eq "1")
{
push(@tosee,"pageFunk$n.survey");
}
}
push(@tosee,"pageBak.survey");
$lst = join(';',@tosee);
$session->setValue("tosee",$lst);
}
&}
The selectable functions are named kont01..kont40. If yes (1) was answered
for a function, add it to the list. Join the list as a semi-colon
delimited string and put it in the session.
Now, for each page, pull the first part of the list of pages and use that
for routing to the next page. Again a pre-parse perl snippet:
<ROUTE CONTINUE="{&
$thispage = "per";
if(!$session->getValue($thispage))
{
$tosee = $session->getValue("tosee");
@pags = split(";",$tosee);
$next = splice(@pags,0,1);
if(!$next) { $next = "pageBak.survey"; }
print $next;
$lst = join(';',@pags);
$session->setValue($thispage,$next);
$session->setValue("tosee",$lst);
}
else
{
print $session->getValue($thispage);
}
&}" />
Splice array, rejoin it as a semi-colon delimited string, and put it back
in session. If there was no more parts in the array, go to a specific
page.
In both of the above snippets you see an if() statement checking if a
value is defined in the session. The reason for this is that pre-parse
snippets are parsed twice: once at display and once at submit. We only
want to parse the array once, thus we define a value in the session the
first time we do the parse. The $thispage variable is the name of the page
we are currently working with.
Dunno if anyone gets anything out of the above discussion, but personally
I'm pretty satisfied with the solution. :-)
// Joel
Skickat av Joel Palmius <[email protected]>
till survey-discussion