diff --git a/pmwiki_recipes/exercises.php b/pmwiki_recipes/exercises.php index 841c8c0..975bcab 100644 --- a/pmwiki_recipes/exercises.php +++ b/pmwiki_recipes/exercises.php @@ -22,72 +22,94 @@ SDV($HandleActions['Exercises'], 'Exercises'); SDV($HandleAuth['Exercises'],'read'); function Exercises($pagename, $auth){ + $type = $_GET["type"]; - $exercisetype="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 - $pages = ListPages(); - } else { - $pages = ListPages("/$group\./e"); + $availableexercisetypes = array("pagelink", "expressioninpage"); - } + 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 + $pages = ListPages(); + } else { + $pages = ListPages("/$group\./e"); - //randomly pick a page in the possible pages - $sourcepage = $pages[rand(0,count($pages))]; - // -1? + } - $content = ReadPage($sourcepage,READPAGE_CURRENT); - //$text = $content["text"]; - // to use later on for expression exercises - $links = $content["targets"]; - $links_array = explode(",",$links); + //randomly pick a page in the possible pages + $sourcepage = $pages[rand(0,count($pages)-1)]; - //$answers = array(); - //consider pilling up potential in $answers[] + $content = ReadPage($sourcepage,READPAGE_CURRENT); + //$text = $content["text"]; + // to use later on for expression exercises + $links = $content["targets"]; + $links_array = explode(",",$links); - //randomly pick a page amongst the linked pages - $pageA = $links_array[rand(0,count($links_array)-1)]; + //$answers = array(); + //consider pilling up potential in $answers[] - 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)]); + //randomly pick a page amongst the linked pages + $pageA = $links_array[rand(0,count($links_array)-1)]; - //display - $result .="Is page [[$sourcepage]] linked to "; - $counter = (int) $_GET["counter"]; + 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)]); - $counterparam = ""; - if ( $counter > 0) { - $counterparam = "counter=$counter&"; - } + //display + $result .="Is page [[$sourcepage]] linked to "; + $counter = (int) $_GET["counter"]; - $answers = array ($pageA,$pageB,$pageC); - shuffle($answers); - for ($i=0;$i 0) { + $counterparam = "counter=$counter&"; } - } + + $answers = array ($pageA,$pageB,$pageC); + shuffle($answers); + for ($i=0;$i 0) { + $result .= " Try other types of exercises:"; + foreach ($availableexercisetypes as $e) { + $result .= " [[$pagename?action=Exercises&type=$e|$e]],"; + } + $result .= "."; + } + if ( $counter > 0) { + $result .= " (note that it resets the counter)"; + } $renderedresult = MarkupToHTML($pagename, $result); print $renderedresult; @@ -135,6 +157,8 @@ function ExercisesCheck($pagename, $auth){ $result .="\n\nTry to redeem yourself by [[$pagename?action=Exercises|trying another time]]. "; } break; + default: + $result .= "Exercise type unknown."; } $result .= "\n\nGenerated by [[http://fabien.benetou.fr/MemoryRecalls/ImprovingPIM#PIMBasedExercises|PIM Based Exercises]]"; $renderedresult = MarkupToHTML($pagename, $result);