@ -6,8 +6,6 @@ error_reporting(0);
# generate exercises based on wiki content and its structure
# generate exercises based on wiki content and its structure
#
#
# still to implement (cf http://fabien.benetou.fr/Cookbook/Cognition#DailyExercisesFeed )
# still to implement (cf http://fabien.benetou.fr/Cookbook/Cognition#DailyExercisesFeed )
# is word X from page A, B, C or D? (type=expressioninpage)
# does page A contains word X, Y or Z? (type=pagehasexpression)
# which of those group corresponds the graph visualization V, A, B or C?
# which of those group corresponds the graph visualization V, A, B or C?
# using groupnetworkvisualization.php
# using groupnetworkvisualization.php
# what is the correct order for pages A, B and C on descending criteria i A>B>C? A>C>B? B>C>A? B>A>C? C>B>A? C>A>B?
# what is the correct order for pages A, B and C on descending criteria i A>B>C? A>C>B? B>C>A? B>A>C? C>B>A? C>A>B?
@ -20,19 +18,25 @@ error_reporting(0);
# list lines with "* word = definition"
# list lines with "* word = definition"
# split word and definition
# split word and definition
# pick one and display the other with alternatives
# pick one and display the other with alternatives
# extend type=pagehasexpression
# implemented
# implemented
# is word X from page A, B, C or D? (type=expressioninpage)
# does page A contains word X, Y or Z? (type=pagehasexpression)
# which of those page corresponds the updates visualization V, A, B or C? (type=historyvisualization)
# which of those page corresponds the updates visualization V, A, B or C? (type=historyvisualization)
# is page A linked to page B? (type=pagelink)
# is page A linked to page B? (type=pagelink)
# consider a difficulty parameter
# consider a difficulty parameter
# should be linked to the counter
# should be linked to the counter
# e.g. increasing the number of possibilities (at least 1 out of 5 pages instead of at least 1 out of 3)
# e.g. increasing the number of possibilities (at least 1 out of 5 pages instead of at least 1 out of 3)
# XXX
# consider doing a trip rather than random jumps
# picking a random page seems to fail from time to time
# i.e. if pick randomly from the linked pages or from the group or from the the whole wiki of the previous page
# some (very few) indices are not used, resulting in picked an empty string
# markup to display results
# http://php.net/manual/en/function.array-rand.php seems to fix it
# MentalExercises (:ExercisesResults:)
# default to urrently logged-in user
# sparklines for visuals
# also implies to record score per session with date
$RecipeInfo['Exercises']['Version'] = '2011-12-08';
$RecipeInfo['Exercises']['Version'] = '2015-16 -08';
SDV($HandleActions['Exercises'], 'Exercises');
SDV($HandleActions['Exercises'], 'Exercises');
SDV($HandleAuth['Exercises'],'read');
SDV($HandleAuth['Exercises'],'read');
@ -47,7 +51,7 @@ function Exercises($pagename, $auth){
$availableexercisetypes = array("pagelink", "expressioninpage", "pagehasexpression", "historyvisualization");
$availableexercisetypes = array("pagelink", "expressioninpage", "pagehasexpression", "historyvisualization");
//should also test if the pim_functions.php generated the PJS has been loaded, else not make historyvisualization available
//should also test if the pim_functions.php generated the PJS has been loaded, else not make historyvisualization available
if ($type=="random")
if ($type=="random" || $type=="" )
$type= $availableexercisetypes[rand(0,count($availableexercisetypes)-1)];
$type= $availableexercisetypes[rand(0,count($availableexercisetypes)-1)];
$result = "";
$result = "";
@ -58,41 +62,54 @@ function Exercises($pagename, $auth){
// it used to determine the number of false answers
// it used to determine the number of false answers
// it can be overwritten per each exercise
// it can be overwritten per each exercise
$minlinelength = 20;
// this could also be consider a difficulty, the shorter the harder
// it can be overwritten per each exercise
$counterparam = "";
$counterparam = "";
if ( $counter > 0) {
if ( $counter > 0) {
$counterparam = "counter=$counter&";
$counterparam = "counter=$counter&";
}
}
// 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");
switch ($type){
}
case "pagelink":
// 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
//randomly pick a page in the possible pages
$sourcepage = $pages[rand(0,count($pages)-1)];
$sourcepage = $pages[array_rand($pages)];
unset($pages[array_search($sourcepage,$pages)]);
switch ($type){
case "pagelink":
$content = ReadPage($sourcepage,READPAGE_CURRENT);
$content = ReadPage($sourcepage,READPAGE_CURRENT);
$links = $content["targets"];
$links = $content["targets"];
$links_array = explode(",",$links);
$links_array = explode(",",$links);
// XXX this does not seem to be used
//$answers = array();
while ((count($links_array)< 1 ) & & ( count ( $ pages ) > 0)) {
//consider pilling up potential in $answers[]
$sourcepage = $pages[array_rand($pages)];
unset($pages[array_search($sourcepage,$pages)]);
$content = ReadPage($sourcepage,READPAGE_CURRENT);
$links = $content["targets"];
$links_array = explode(",",$links);
}
if ((count($links_array)< 1 ) ) {
$result .= "Unfortunately it seems no suitable page has been found for this game. ";
$result .= "Try in another group or [[AllPages/AllPages?type=$type& counter=$counter|in the entire wiki]].";
break;
}
//randomly pick a page amongst the linked pages
//randomly pick a page amongst the linked pages
$answers[] = $links_array[rand(0,count($links_array)-1)];
$answers[] = $links_array[array_rand($links_array)];
unset($pages[array_search($answers[0],$links_array)]);
unset($pages[array_search($answers[0],$links_array)]);
unset($pages[array_search($answers[0],$pages)]);
unset($pages[array_search($answers[0],$pages)]);
//randomly pick n-1 others pages which may not be amongst the list of linked page
//randomly pick n-1 others pages which may not be amongst the list of linked page
for ($i=1;$i< $n;$i++) {
for ($i=1;$i< $n;$i++) {
$answers[] = $pages[rand(0,count($pages)-1)];
$answers[] = $pages[array_ rand($pages)];
unset($pages[array_search($answers[$i],$pages)]);
unset($pages[array_search($answers[$i],$pages)]);
}
}
@ -104,49 +121,29 @@ function Exercises($pagename, $auth){
shuffle($answers);
shuffle($answers);
for ($i=0;$i< count ( $ answers ) ; $ i + + ) {
for ($i=0;$i< count ( $ answers ) ; $ i + + ) {
if ($answers[$i] != "") {
if ( $i == count($answers) -1 )
if ( $i == count($answers) -1 )
$result .= " or ";
$result .= " or ";
$currentpage = $answers[$i];
//display the groupname everytime only if using AllPages
//display the groupname everytime only if using AllPages
if ($group != "AllPages") {
if ($group != "AllPages")
list($currentgroup,$currentpage) = explode(".",$answers[$i]);
list($currentgroup,$currentpage) = explode(".",$answers[$i]);
$result .="[[$pagename?action=ExercisesCheck& ".$counterparam
$result .="[[$pagename?action=ExercisesCheck& ".$counterparam
."type=$type& source=$sourcepage& answer=".$answers[$i]."|".$currentpage."]], ";
."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 .="? ";
$result .="\n\nClick on the answer.";
$result .="\n\nClick on the answer.";
break;
break;
case "historyvisualization":
case "historyvisualization":
$processingpath = '/mirrors/fabien/pub/libraries/processing.js';
$processingpath = "$ScriptUrl/pub/libraries/processing.js";
$processingpath = "$ScriptUrl/pub/libraries/processing.js";
$processinglib = "< script src = \"$processingpath\" type = \"text/javascript\" > < / script > ";
$processinglib = "< script src = \"$processingpath\" type = \"text/javascript\" > < / script > ";
// 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)];
$processingfile = "/mirrors/fabien/pub/visualization/edits_per_page/$sourcepage.pjs";
$processingfile = "$ScriptUrl/pub/visualization/edits_per_page/$sourcepage.pjs";
$processingfile = "$ScriptUrl/pub/visualization/edits_per_page/$sourcepage.pjs";
// TODO if it does not exist, pick another page, adapt code above
//randomly pick a page amongst the linked pages
$answers[] = $sourcepage;
$answers[] = $sourcepage;
unset($pages[array_search($answers[0],$pages)]);
//randomly pick n-1 others pages which may not be amongst the list of linked page
//randomly pick n-1 others pages which may not be amongst the list of linked page
for ($i=1;$i< $n;$i++) {
for ($i=1;$i< $n;$i++) {
$answers[] = $pages[rand(0,count( $pages)-1 )];
$answers[] = $pages[array_rand($pages)];
unset($pages[array_search($answers[$i],$pages)]);
unset($pages[array_search($answers[$i],$pages)]);
}
}
@ -155,69 +152,39 @@ function Exercises($pagename, $auth){
$verbatimresult = $historyvisualization;
$verbatimresult = $historyvisualization;
$result .="Which of those page corresponds the updates visualization:";
$result .="Which of those page corresponds the updates visualization:";
// there should now be at least 1 needle in the haystack, the first one
// note that there might be more than 1 correct answer but this is not a problem as long as the question is stated clearly.
// note that there might be more but this is not a problem as long as the question is stated clearly.
shuffle($answers);
shuffle($answers);
for ($i=0;$i< count ( $ answers ) ; $ i + + ) {
for ($i=0;$i< count ( $ answers ) ; $ i + + ) {
if ($answers[$i] != "") {
if ( $i == count($answers) -1 )
if ( $i == count($answers) -1 )
$result .= " or ";
$result .= " or ";
//display the groupname everytime only if using AllPages
//display the groupname everytime only if using AllPages
$currentpage = $answers[$i];
if ($group != "AllPages") {
if ($group != "AllPages")
list($currentgroup,$currentpage) = explode(".",$answers[$i]);
list($currentgroup,$currentpage) = explode(".",$answers[$i]);
$result .="[[$pagename?action=ExercisesCheck& ".$counterparam
$result .="[[$pagename?action=ExercisesCheck& ".$counterparam
."type=$type& source=$sourcepage& answer=".md5($answers[$i])."|".$currentpage."]], ";
."type=$type& source=$sourcepage& answer=".md5($answers[$i])."|".$currentpage."]], ";
} else {
$result .="[[$pagename?action=ExercisesCheck& ".$counterparam
."type=$type& source=$sourcepage& answer=".md5($answers[$i])."|".$answers[$i]."]], ";
}
}
}
}
$result .="? ";
$result .="? ";
$result .="\n\nClick on the answer.";
$result .="\n\nClick on the answer.";
// XXX checking of the result is not yet done, might have to hide (e.g. via hash) the answer and the source
break;
break;
case "pagehasexpression":
case "pagehasexpression":
// 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)];
$sourcepage = $pages[array_rand($pages)];
unset($pages[array_search($sourcepage,$pages)]);
$content = ReadPage($sourcepage,READPAGE_CURRENT);
$content = ReadPage($sourcepage,READPAGE_CURRENT);
$text = $content["text"];
$text = $content["text"];
$lines = explode("\n",$text);
$lines = explode("\n",$text);
// $links = $content["targets"];
// $links_array = explode(",",$links);
// could be use for increase difficulty, e.g. pick content from related pages
//$answers = array();
//consider pilling up potential in $answers[]
$minlinelength = 20;
// this could also be consider a difficulty, the shorter the harder
//$answers[]["source"] = $sourcepage;
$pickedline = $lines[rand(0,count($lines)-1)];
$pickedline = $lines[rand(0,count($lines)-1)];
unset($lines[array_search($pickedline,$lines)]);
unset($lines[array_search($pickedline,$lines)]);
// remove markups
//pregreplace("/(:.*:)/","",$pickedline);
while (( strlen($pickedline) < $minlinelength ) & & ( count($lines) > 0 ) ) {
while (( strlen($pickedline) < $minlinelength ) & & ( count($lines) > 0 ) ) {
$pickedline = $lines[rand(0,count($lines)-1)];
$pickedline = $lines[rand(0,count($lines)-1)];
//pregreplace("/(:.*:)/","",$pickedline);
unset($lines[array_search($pickedline,$lines)]);
unset($lines[array_search($pickedline,$lines)]);
}
}
//$answers[]["line"] = $pickedline;
$actualanswer = $pickedline;
$actualanswer = $pickedline;
$answers[] = $pickedline;
$answers[] = $pickedline;
//randomly pick n-1 others pages which may not be amongst the list of linked page
for ($i=1;$i< $n;$i++) {
for ($i=1;$i< $n;$i++) {
$otherpage = $pages[rand(0,count($pages)-1)];
$otherpage = $pages[rand(0,count($pages)-1)];
$otherpage = $pages[array_rand($pages)];
$otherpage = $pages[array_rand($pages)];
@ -225,30 +192,22 @@ function Exercises($pagename, $auth){
$content = ReadPage($otherpage,READPAGE_CURRENT);
$content = ReadPage($otherpage,READPAGE_CURRENT);
$text = $content["text"];
$text = $content["text"];
$lines = explode("\n",$text);
$lines = explode("\n",$text);
//$answers[]["source"] = $otherpage;
$pickedline = $lines[rand(0,count($lines)-1)];
$pickedline = $lines[rand(0,count($lines)-1)];
unset($lines[array_search($pickedline,$lines)]);
unset($lines[array_search($pickedline,$lines)]);
while (( strlen($pickedline) < $minlinelength ) & & ( count($lines) > 0 ) ) {
while (( strlen($pickedline) < $minlinelength ) & & ( count($lines) > 0 ) ) {
$pickedline = $lines[rand(0,count($lines)-1)];
$pickedline = $lines[rand(0,count($lines)-1)];
unset($lines[array_search($pickedline,$lines)]);
unset($lines[array_search($pickedline,$lines)]);
}
}
//$answers[]["line"] = $pickedline;
$answers[] = $pickedline;
$answers[] = $pickedline;
}
}
//display
//display
$result .= "Does the page [[$sourcepage]] has expression ";
$result .= "Does the page [[$sourcepage]] has expression ";
// 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);
shuffle($answers);
for ($i=0;$i< $n;$i++) {
for ($i=0;$i< $n;$i++) {
if ($answers[$i]==$actualanswer)
$hashedanswer = md5($answers[$i]);
$hashedanswer = md5($sourcepage);
else
$hashedanswer = md5($answers[$i]);
$result .="\n* [[$pagename?action=ExercisesCheck& ".$counterparam
$result .="\n* [[$pagename?action=ExercisesCheck& ".$counterparam
."type=$type& source=$sourcepage& answer=$hashedanswer|$i]] [@".$answers[$i]."@] ";
."type=$type& source=$sourcepage& answer=$hashedanswer|$i]] [@".$answers[$i]."@] ";
//should be better parsed!
//should be better parsed!
@ -257,8 +216,43 @@ function Exercises($pagename, $auth){
break;
break;
case "expressioninpage":
case "expressioninpage":
$content = ReadPage($sourcepage,READPAGE_CURRENT);
$text = $content["text"];
$lines = explode("\n",$text);
$pickedline = $lines[array_rand($lines)];
unset($lines[array_search($pickedline,$lines)]);
while (( strlen($pickedline) < $minlinelength ) & & ( count($lines) > 0 ) ) {
$pickedline = $lines[rand(0,count($lines)-1)];
unset($lines[array_search($pickedline,$lines)]);
}
$answers[] = $sourcepage;
//randomly pick n-1 others pages which may not be amongst the list of linked page
for ($i=1;$i< $n;$i++) {
$otherpage = $pages[array_rand($pages)];
unset($pages[array_search($otherpage,$pages)]);
$answers[] = $otherpage;
}
//display
$result .= "Does the page expression \n* [@$pickedline@] \n\nbelongs to ";
// 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< $n;$i++) {
$hashedanswer = md5($answers[$i]);
if ( $i == count($answers) -1 )
$result .= " or ";
$result .="[[$pagename?action=ExercisesCheck& ".$counterparam
."type=$type& source=$sourcepage& answer=$hashedanswer|".$answers[$i]."]], ";
}
$result .="?\n\nClick on the answer.";
// explode content by line "%0a" or word " "
// explode content by line "%0a" or word " "
$result .= "Does the expression 'EXPRESSION' come from PAGEA, PAGEB or PAGEC?";
break;
break;
default:
default:
$result .= "Exercise type unknown.";
$result .= "Exercise type unknown.";
@ -266,7 +260,7 @@ function Exercises($pagename, $auth){
unset($availableexercisetypes[array_search($type,$availableexercisetypes)]);
unset($availableexercisetypes[array_search($type,$availableexercisetypes)]);
if (count($availableexercisetypes) > 0) {
if (count($availableexercisetypes) > 0) {
$result .= " Try other types of exercises:";
$result .= "\n\n Try other types of exercises:";
foreach ($availableexercisetypes as $e) {
foreach ($availableexercisetypes as $e) {
$result .= " [[$pagename?action=Exercises&type=$e|$e]],";
$result .= " [[$pagename?action=Exercises&type=$e|$e]],";
}
}
@ -350,12 +344,29 @@ function ExercisesCheck($pagename, $auth){
$result .="\n\nSee if you can [[$pagename?action=Exercises& type=$type& counter=1|solve yet another one]]. ";
$result .="\n\nSee if you can [[$pagename?action=Exercises& type=$type& counter=1|solve yet another one]]. ";
}
}
} else {
} else {
$result .="No, it was the expresso in of [[$sourcepage]].";
$result .="No, it was the expressio n of [[$sourcepage]].";
// display it again
// display it again
if ($counter > 0) $result .="\nIt means you are losing your $counter points.";
if ($counter > 0) $result .="\nIt means you are losing your $counter points.";
$result .="\n\nTry to redeem yourself by [[$pagename?action=Exercises& type=$type|trying another time]]. ";
$result .="\n\nTry to redeem yourself by [[$pagename?action=Exercises& type=$type|trying another time]]. ";
}
}
break;
break;
case "expressioninpage":
if ( md5($sourcepage) === $answer ) {
$result .="Excellent! that expression did come from [[$sourcepage]].";
if ( $counter > 0) {
$counter++;
$result .="\n\nSee if you can [[$pagename?action=Exercises& type=$type& counter=$counter|solve yet another one]]. ";
} else {
$result .="\n\nSee if you can [[$pagename?action=Exercises& type=$type& counter=1|solve yet another one]]. ";
}
} else {
$result .="No, [[$sourcepage]] was the page that expression came from.";
// display it again
if ($counter > 0) $result .="\nIt means you are losing your $counter points.";
$result .="\n\nTry to redeem yourself by [[$pagename?action=Exercises& type=$type|trying another time]]. ";
}
break;
default:
default:
$result .= "Exercise type unknown, this most likely indicate that the checking for the solution has not yet been implemented, please consider notifying the author. For now try [[$pagename?action=Exercises& type=random|a random type of exercise]].";
$result .= "Exercise type unknown, this most likely indicate that the checking for the solution has not yet been implemented, please consider notifying the author. For now try [[$pagename?action=Exercises& type=random|a random type of exercise]].";
}
}