You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
misc/pmwiki_recipes/exercises.php

367 lines
14 KiB

<?php if (!defined('PmWiki')) exit();
error_reporting(0);
// for demo
# generate exercises based on wiki content and its structure
#
# 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?
# 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?
# e.g. size, frequency update, last edition, ...
# what is the color hashing used in RevertedPIM for this group, color A, B or C?
# might require JS
# keyword or URL in pages
# definition of word and vice versa
# limited to pages with definitions (e.g. some languages)
# list lines with "* word = definition"
# split word and definition
# pick one and display the other with alternatives
# implemented
# which of those page corresponds the updates visualization V, A, B or C? (type=historyvisualization)
# is page A linked to page B? (type=pagelink)
# consider a difficulty parameter
# 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)
# XXX
# picking a random page seems to fail from time to time
# some (very few) indices are not used, resulting in picked an empty string
# http://php.net/manual/en/function.array-rand.php seems to fix it
$RecipeInfo['Exercises']['Version'] = '2011-12-08';
SDV($HandleActions['Exercises'], 'Exercises');
SDV($HandleAuth['Exercises'],'read');
function Exercises($pagename, $auth){
global $ScriptUrl;
$type = $_GET["type"];
$counter = (int) $_GET["counter"];
$difficulty = (int) $_GET["difficulty"];
list($group,$page) = explode(".",$pagename);
$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
if ($type=="random")
$type= $availableexercisetypes[rand(0,count($availableexercisetypes)-1)];
$result = "";
$verbatimresult = "";
$n=3;
// n should be based on $difficulty, e.g. n=2+$difficulty or n=2^$difficulty
// it used to determine the number of false answers
// it can be overwritten per each exercise
$counterparam = "";
if ( $counter > 0) {
$counterparam = "counter=$counter&";
}
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
$sourcepage = $pages[rand(0,count($pages)-1)];
$content = ReadPage($sourcepage,READPAGE_CURRENT);
$links = $content["targets"];
$links_array = explode(",",$links);
//$answers = array();
//consider pilling up potential in $answers[]
//randomly pick a page amongst the linked pages
$answers[] = $links_array[rand(0,count($links_array)-1)];
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
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 ";
// 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<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 "historyvisualization":
$processingpath = '/mirrors/fabien/pub/libraries/processing.js';
$processingpath = "$ScriptUrl/pub/libraries/processing.js";
$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";
//randomly pick a page amongst the linked pages
$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
for ($i=1;$i<$n;$i++) {
$answers[] = $pages[rand(0,count($pages)-1)];
unset($pages[array_search($answers[$i],$pages)]);
}
//display
$historyvisualization = $processinglib.'<canvas data-src="'.$processingfile.'"></canvas>';
$verbatimresult = $historyvisualization;
$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 but this is not a problem as long as the question is stated clearly.
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=".md5($answers[$i])."|".$currentpage."]], ";
} else {
$result .="[[$pagename?action=ExercisesCheck&".$counterparam
."type=$type&source=$sourcepage&answer=".md5($answers[$i])."|".$answers[$i]."]], ";
}
}
}
$result .="? ";
$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;
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);
$text = $content["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)];
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[]["line"] = $pickedline;
$actualanswer = $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++) {
$otherpage = $pages[rand(0,count($pages)-1)];
$otherpage = $pages[array_rand($pages)];
unset($pages[array_search($otherpage,$pages)]);
$content = ReadPage($otherpage,READPAGE_CURRENT);
$text = $content["text"];
$lines = explode("\n",$text);
//$answers[]["source"] = $otherpage;
$pickedline = $lines[rand(0,count($lines)-1)];
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[]["line"] = $pickedline;
$answers[] = $pickedline;
}
//display
$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);
for ($i=0;$i<$n;$i++) {
if ($answers[$i]==$actualanswer)
$hashedanswer = md5($sourcepage);
else
$hashedanswer = md5($answers[$i]);
$result .="\n* [[$pagename?action=ExercisesCheck&".$counterparam
."type=$type&source=$sourcepage&answer=$hashedanswer|$i]] [@".$answers[$i]."@] ";
//should be better parsed!
}
$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.";
}
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 .= " or a [[$pagename?action=Exercises&type=random|random]] one.";
if ( $counter > 0) {
$result .= " (note that it resets the counter)";
}
}
$result .= "\n\n%center%[-Generated by [[http://fabien.benetou.fr/MemoryRecalls/ImprovingPIM#PIMBasedExercises|PIM Based Exercises]].-]%%";
$renderedresult = MarkupToHTML($pagename, $result);
print "<html>".$verbatimresult.$renderedresult."</html>";
//$text = RetrieveAuthSection($pagename,$auth);
//$content = MarkupToHTML($pagename, $text);
//print $content;
}
SDV($HandleActions['ExercisesCheck'], 'ExercisesCheck');
SDV($HandleAuth['ExercisesCheck'],'read');
function ExercisesCheck($pagename, $auth){
$type = $_GET["type"];
$sourcepage = $_GET["source"];
$answer = $_GET["answer"];
$counter = (int) $_GET["counter"];
list($group,$page) = explode(".",$pagename);
if ($group == "AllPages"){
// equivalent to getting ALL pages
$pages = ListPages();
} else {
$pages = ListPages("/$group\./e");
}
switch ($type) {
case "pagelink":
$content = ReadPage($sourcepage,READPAGE_CURRENT);
$links = $content["targets"];
$links_array = explode(",",$links);
$formattedlinks = implode(", ",$links_array);
if ( in_array($answer,$links_array) ) {
$result .="Excellent! [[$sourcepage]] is indeed linked to [[$answer]]. ";
$result .="Note that $formattedlinks also are. ";
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]] is not linked to [[$answer]] ";
$result .="but $formattedlinks are. ";
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;
case "historyvisualization":
if ( md5($sourcepage) === $answer ) {
$result .="Excellent! [[$sourcepage]] indeed has that history of editions.";
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, it was the visualization of [[$sourcepage]] history of editions.";
// 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;
case "pagehasexpression":
if ( md5($sourcepage) === $answer ) {
$result .="Excellent! [[$sourcepage]] indeed has that expression.";
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, it was the expressoin of [[$sourcepage]].";
// 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:
$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 .= "\n\n%center%[-Generated by [[http://fabien.benetou.fr/MemoryRecalls/ImprovingPIM#PIMBasedExercises|PIM Based Exercises]].-]%%";
$renderedresult = MarkupToHTML($pagename, $result);
print $renderedresult;
}
?>