From 6c09afa6024bb0d2a347fc3d74c53fc92bf4b253 Mon Sep 17 00:00:00 2001 From: Fabien Benetou Date: Tue, 16 Aug 2011 15:59:38 +0200 Subject: [PATCH] user proper random pick, generalized md5 and a bit of refactoring --- pmwiki_recipes/exercises.php | 231 ++++++++++++++++++----------------- 1 file changed, 121 insertions(+), 110 deletions(-) diff --git a/pmwiki_recipes/exercises.php b/pmwiki_recipes/exercises.php index a852e4e..3157a29 100644 --- a/pmwiki_recipes/exercises.php +++ b/pmwiki_recipes/exercises.php @@ -6,8 +6,6 @@ error_reporting(0); # 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? @@ -20,19 +18,25 @@ error_reporting(0); # list lines with "* word = definition" # split word and definition # pick one and display the other with alternatives +# extend type=pagehasexpression # 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) # 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 +# consider doing a trip rather than random jumps +# i.e. if pick randomly from the linked pages or from the group or from the the whole wiki of the previous page +# markup to display results +# 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($HandleAuth['Exercises'],'read'); @@ -47,7 +51,7 @@ function Exercises($pagename, $auth){ $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") + if ($type=="random" || $type=="") $type= $availableexercisetypes[rand(0,count($availableexercisetypes)-1)]; $result = ""; @@ -58,41 +62,54 @@ function Exercises($pagename, $auth){ // it used to determine the number of false answers // 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 = ""; if ( $counter > 0) { $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 - $sourcepage = $pages[rand(0,count($pages)-1)]; + //randomly pick a page in the possible pages + $sourcepage = $pages[array_rand($pages)]; + unset($pages[array_search($sourcepage,$pages)]); + switch ($type){ + case "pagelink": $content = ReadPage($sourcepage,READPAGE_CURRENT); $links = $content["targets"]; $links_array = explode(",",$links); - - //$answers = array(); - //consider pilling up potential in $answers[] + // XXX this does not seem to be used + while ((count($links_array)<1) && (count($pages)>0)) { + $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 - $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],$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)]; + $answers[] = $pages[array_rand($pages)]; unset($pages[array_search($answers[$i],$pages)]); } @@ -104,49 +121,29 @@ function Exercises($pagename, $auth){ shuffle($answers); for ($i=0;$i"; - // 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"; + // TODO if it does not exist, pick another page, adapt code above - //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)]; + $answers[] = $pages[array_rand($pages)]; unset($pages[array_search($answers[$i],$pages)]); } @@ -155,69 +152,39 @@ function Exercises($pagename, $auth){ $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. + // note that there might be more than 1 correct answer but this is not a problem as long as the question is stated clearly. shuffle($answers); for ($i=0;$i 0 ) ) { $pickedline = $lines[rand(0,count($lines)-1)]; + //pregreplace("/(:.*:)/","",$pickedline); 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)]; @@ -225,30 +192,22 @@ function Exercises($pagename, $auth){ $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]); + $hashedanswer = md5($answers[$i]); $result .="\n* [[$pagename?action=ExercisesCheck&".$counterparam ."type=$type&source=$sourcepage&answer=$hashedanswer|$i]] [@".$answers[$i]."@] "; //should be better parsed! @@ -257,8 +216,43 @@ function Exercises($pagename, $auth){ break; 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 " " - $result .= "Does the expression 'EXPRESSION' come from PAGEA, PAGEB or PAGEC?"; break; default: $result .= "Exercise type unknown."; @@ -266,7 +260,7 @@ function Exercises($pagename, $auth){ unset($availableexercisetypes[array_search($type,$availableexercisetypes)]); if (count($availableexercisetypes) > 0) { - $result .= " Try other types of exercises:"; + $result .= "\n\nTry other types of exercises:"; foreach ($availableexercisetypes as $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]]. "; } } else { - $result .="No, it was the expressoin of [[$sourcepage]]."; + $result .="No, it was the expression 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; + 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: $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]]."; }