support for multiple types of exercises

master
Fabien Benetou 14 years ago
parent 5821b36d0b
commit a617218f07
  1. 130
      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<count($answers);$i++) {
if ($answers[$i] != "") {
if ( $i == count($answers) -1 )
$result .= " or ";
//display the groupname everytime only if using AllPages
if ($group != "AllPages") {
list($currentgroup,$currentpage) = explode(".",$answers[$i]);
$result .="[[$pagename?action=ExercisesCheck&".$counterparam
."type=$exercisetype&source=$sourcepage&answer=".$answers[$i]."|".$currentpage."]], ";
} else {
$result .="[[$pagename?action=ExercisesCheck&".$counterparam
."type=$exercisetype&source=$sourcepage&answer=".$answers[$i]."|".$answers[$i]."]], ";
$counterparam = "";
if ( $counter > 0) {
$counterparam = "counter=$counter&";
}
}
$answers = array ($pageA,$pageB,$pageC);
shuffle($answers);
for ($i=0;$i<count($answers);$i++) {
if ($answers[$i] != "") {
if ( $i == count($answers) -1 )
$result .= " or ";
//display the groupname everytime only if using AllPages
if ($group != "AllPages") {
list($currentgroup,$currentpage) = explode(".",$answers[$i]);
$result .="[[$pagename?action=ExercisesCheck&".$counterparam
."type=$type&source=$sourcepage&answer=".$answers[$i]."|".$currentpage."]], ";
} else {
$result .="[[$pagename?action=ExercisesCheck&".$counterparam
."type=$type&source=$sourcepage&answer=".$answers[$i]."|".$answers[$i]."]], ";
}
}
}
$result .="? ";
$result .="\n\nClick on the answer.";
break;
case "expressioninpage":
// explode content by line "%0a" or word " "
$result .= "Does the expression 'EXPRESSION' come from PAGEA PAGEB or PAGEC?";
break;
default:
$result .= "Exercise type unknown.";
}
$result .="? ";
$result .="\n\nClick on the answer.";
$result .= "\n\nGenerated by [[http://fabien.benetou.fr/MemoryRecalls/ImprovingPIM#PIMBasedExercises|PIM Based Exercises]]";
$result .= "\n\nGenerated by [[http://fabien.benetou.fr/MemoryRecalls/ImprovingPIM#PIMBasedExercises|PIM Based Exercises]]. ";
unset($availableexercisetypes[array_search($type,$availableexercisetypes)]);
if (count($availableexercisetypes) > 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);

Loading…
Cancel
Save