From: Ben H. <bdv...@us...> - 2010-11-22 23:06:00
|
Update of /cvsroot/stack/stack-dev/lib/blocks/active In directory sfp-cvsdas-3.v30.ch3.sourceforge.com:/tmp/cvs-serv29766/lib/blocks/active Added Files: Tag: question_reporting IfBlock.php ForEachBlock.php SizeBlock.php BoldBlock.php Log Message: Merging from the current HEAD into question_reporting. Apologies in advance if this all goes horribly wrong. --- NEW FILE: IfBlock.php --- <?php /** * * Welcome to STACK. A system for teaching and assessment using a * computer algebra kernel. * * This file is licensed under the GPL License. * * A copy of the license is in your STACK distribution called * license.txt. If you are missing this file you can obtain * it from: * http://www.stack.bham.ac.uk/license.txt * * * @author Matti Harjula * */ global $config; $root = $config->get('docroot'); require_once($root.'/lib/blocks/BlockInterface.php'); require_once($root.'/lib/ui/BlockHandler.php'); /** * A Block to handle if-else-if structures of this form: * {% if test1 %}... * {% else if test2 %}... * {% else %}... * {% end if %} */ class IfBlock extends BlockInterface { public function getSubBlockIdentifiers(){return array("else");} public function getBlockIdentifier(){return "if";} public function processBlock($content,$vars,$casTextHandler){ $theReturn=""; // This would be so simple if there were no else or else if... as now we need to take in to account nested ifs that may also have elses. // but as our parser can already handle these things rather well this is not that big of an deal $parsedContent=BlockHandler::blockHandlerParser($content,array("else")); $branches=array("true"=>""); $whichBranch="true"; $weHaveElseBranch=false; $weHaveElseIfBranch=false; foreach($parsedContent as $bloc) if(is_array($bloc)){ $branches[$whichBranch].=self::backToStringForm(array($bloc)); }else{ // Now then is there an else of elseif $i=0; while(true){ $startOfFirstBlock=strpos($bloc,"{%",$i); if($startOfFirstBlock>0){ $branches[$whichBranch].=substr($bloc,$i,$startOfFirstBlock-$i); }else if($startOfFirstBlock===FALSE){ $branches[$whichBranch].=substr($bloc,$i); break; } $endOfFirstStartBlock=strpos($bloc,"%}",$startOfFirstBlock+2); $firstStartBlock=substr($bloc,$startOfFirstBlock,$endOfFirstStartBlock-$startOfFirstBlock+2); $blockTokens=self::tokenCleaner(explode(" ",trim(substr($firstStartBlock,2,-2)))); if(count($blockTokens)==1){ // just the else $weHaveElseBranch=true; $whichBranch="false"; $branches["false"]=""; $i=$endOfFirstStartBlock+2; }else{ $weHaveElseIfBranch=true; $whichBranch=implode(" ",array_splice($blockTokens,1)); $branches[$whichBranch]=""; $i=$endOfFirstStartBlock+2; } } } if($vars[0] === 'true') $theReturn.=$branches['true']; else{ // if we have an "else if" there we need to build a new if block so that it can be evaluated if($weHaveElseIfBranch){ $first=true; $second=false; foreach($branches as $test => $content){ if($first){ $first=false; //skipping the true branch $second=true; } else if($second){ $second=false; // The second one is special $theReturn.="{% ".$test." %}$content"; }else if($test=="false") $theReturn.="{% else %}$content"; else $theReturn.="{% else ".$test." %}$content"; } $theReturn.="{% end if %}"; }else if($weHaveElseBranch) $theReturn.=$branches["false"]; } return $theReturn; } private static function backToStringForm($blocks){ $R=""; foreach($blocks as $block) if(is_array($block)){ if(count($block['vars'])>0) $R.="{% ".$block['block_type']." ".implode(" ",$block['vars'])." %}"; else $R.="{% ".$block['block_type']." %}"; $R.=$block['content']; $R.="{% end ".$block['block_type']." %}"; }else $R.=$block; return $R; } private static function tokenCleaner($a){ $b=array(); foreach($a as $t) if(trim($t)!="") $b[]=$t; return $b; } } ?> --- NEW FILE: BoldBlock.php --- <?php global $config; $root = $config->get('docroot'); require_once($root.'/lib/blocks/BlockInterface.php'); class BoldBlock extends BlockInterface{ public function getSubBlockIdentifiers(){return False;} public function getBlockIdentifier(){return "bold";} public function processBlock($content,$vars,$casTextHandler){ return "<b>".$content."</b>"; } } ?> --- NEW FILE: SizeBlock.php --- <?php global $config; $root = $config->get('docroot'); require_once($root.'/lib/blocks/BlockInterface.php'); class SizeBlock extends BlockInterface{ public function getSubBlockIdentifiers(){return False;} public function getBlockIdentifier(){return "size";} public function processBlock($content,$vars,$casTextHandler){ return "<font size='".$vars[0]."'>".$content."</font>"; } } ?> --- NEW FILE: ForEachBlock.php --- <?php /** * * Welcome to STACK. A system for teaching and assessment using a * computer algebra kernel. * * This file is licensed under the GPL License. * * A copy of the license is in your STACK distribution called * license.txt. If you are missing this file you can obtain * it from: * http://www.stack.bham.ac.uk/license.txt * * * @author Matti Harjula * */ global $config; $root = $config->get('docroot'); require_once($root.'/lib/blocks/BlockInterface.php'); /** * A Block to handle foreach structures of this form: * {% foreach [1,2,3,4] as XXX %} * Something XXX * {% end foreach %} * * The block takes any comma-separated list of things that has been wrapped in * something and splits it to values that are injected to a place in the contents * of the block the marker of that place can be freely selected and can be any * continuous string. The contents are repeated as many times as there are items * in the list. * * The use of the "as"-keyword is mandatory. One can define multiple lists to be * iterated and they will be iterated in sync. If these lists are not of the same * length the iteration stops when the first list stops. * * {% foreach [1,2,3,4] as XXX * [2,3,4,5] as YYY %} * Something XXX and YYY * {% end foreach %} * * Something 1 and 2 * Something 2 and 3 * Something 3 and 4 * Something 4 and 5 */ class ForEachBlock extends BlockInterface { public function getSubBlockIdentifiers(){return array("else");} public function getBlockIdentifier(){return "foreach";} public function processBlock($content,$vars,$casTextHandler){ $replacements=array(); $minLength=99999; for($i=0;$i*3<=count($vars);$i+=3){ $items=self::tokenizer(substr($vars[$i],1,-1)); $replacements[$vars[$i+2]]=$items; $minLength=min($minLength,count($items)); } $r=""; for($i=0;$i<$minLength;$i++){ $t=$content; foreach($replacements as $token => $values) $t=str_replace($token,$values[$i],$t); $r.=$t; } return $r; } /** * StringUtil probably has something for this task... */ public static function tokenizer($in) { $braceCount = 0; $parenthesisCount = 0; $bracketCount = 0; $out = array (); $current = ''; $unPlaced = 0; for ($i = 0; $i < strlen($in); $i++) { $unPlaced++; $char = $in[$i]; switch ($char) { case '{' : $braceCount++; $current .= $char; break; case '}' : $braceCount--; $current .= $char; break; case '(' : $parenthesisCount++; $current .= $char; break; case ')' : $parenthesisCount--; $current .= $char; break; case '[' : $bracketCount++; $current .= $char; break; case ']' : $bracketCount--; $current .= $char; break; case ',' : if ($bracketCount == 0 && $parenthesisCount == 0 && $braceCount == 0) { $out[] = $current; $current = ''; $unPlaced = 0; } else $current .= $char; break; default; $current .= $char; } } if ($unPlaced > 0 && $bracketCount == 0 && $parenthesisCount == 0 && $braceCount == 0) $out[] = $current; return $out; } } ?> |