From deee57aa0152b1c82e62dc8da770e5b99f85e61b Mon Sep 17 00:00:00 2001 From: Fabien Benetou Date: Fri, 12 Aug 2011 23:40:25 +0200 Subject: [PATCH] partly managing difficulty --- pmwiki_recipes/exercises.php | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/pmwiki_recipes/exercises.php b/pmwiki_recipes/exercises.php index 9851144..63f4144 100644 --- a/pmwiki_recipes/exercises.php +++ b/pmwiki_recipes/exercises.php @@ -23,14 +23,16 @@ SDV($HandleAuth['Exercises'],'read'); function Exercises($pagename, $auth){ $type = $_GET["type"]; + $counter = (int) $_GET["counter"]; + $difficulty = (int) $_GET["difficulty"]; + list($group,$page) = explode(".",$pagename); - $availableexercisetypes = array("pagelink", "expressioninpage"); + $availableexercisetypes = array("pagelink", "expressioninpage", "pagehasexpression"); if ($type=="random") $type= $availableexercisetypes[rand(0,count($availableexercisetypes)-1)]; switch ($type){ case "pagelink": - list($group,$page) = explode(".",$pagename); // the pattern should be improve, e.g. remove PmWiki. , RecentChanges, GroupFooter, GroupHeader, Template, ... if ($group == "AllPages"){ // equivalent to getting ALL pages @@ -53,27 +55,31 @@ function Exercises($pagename, $auth){ //consider pilling up potential in $answers[] //randomly pick a page amongst the linked pages - $pageA = $links_array[rand(0,count($links_array)-1)]; + $answers[] = $links_array[rand(0,count($links_array)-1)]; - unset($pages[array_search($pageA,$links_array)]); - unset($pages[array_search($pageA,$pages)]); - //randomly pick 2 others pages which are not amongst the list of linked page - $pageB = $pages[rand(0,count($pages)-1)]; - unset($pages[array_search($pageB,$pages)]); - $pageC = $pages[rand(0,count($pages)-1)]; - unset($pages[array_search($pageC,$pages)]); + unset($pages[array_search($answers[0],$links_array)]); + unset($pages[array_search($answers[0],$pages)]); + + //randomly pick n-1 others pages which may not be amongst the list of linked page + // n should be based on $difficulty, e.g. n=2+$difficulty or n=2^$difficulty + $n=3; + for ($i=1;$i<$n;$i++) { + $answers[] = $pages[rand(0,count($pages)-1)]; + unset($pages[array_search($answers[$i],$pages)]); + } //display $result .="Is page [[$sourcepage]] linked to "; - $counter = (int) $_GET["counter"]; $counterparam = ""; if ( $counter > 0) { $counterparam = "counter=$counter&"; } - $answers = array ($pageA,$pageB,$pageC); + // there should now be at least 1 needle in the haystack, the first one + // note that there might be more but this is not a problem as long as the question is stated clearly. shuffle($answers); + for ($i=0;$i