From: Ben H. <bdv...@us...> - 2010-11-22 23:06:00
|
Update of /cvsroot/stack/stack-dev/lib In directory sfp-cvsdas-3.v30.ch3.sourceforge.com:/tmp/cvs-serv29766/lib Modified Files: Tag: question_reporting AimConverter.php error.php stringUtil.php Log Message: Merging from the current HEAD into question_reporting. Apologies in advance if this all goes horribly wrong. Index: error.php =================================================================== RCS file: /cvsroot/stack/stack-dev/lib/error.php,v retrieving revision 1.21.2.1 retrieving revision 1.21.2.2 diff -C2 -d -r1.21.2.1 -r1.21.2.2 *** error.php 29 Nov 2009 13:24:10 -0000 1.21.2.1 --- error.php 22 Nov 2010 23:05:52 -0000 1.21.2.2 *************** *** 30,34 **** private $fail; //bool if error has occured, should the program fail? private $userError; //array of errors for display to the user. format key=>errormessage ! private $logger; function __construct($fail=false) --- 30,34 ---- private $fail; //bool if error has occured, should the program fail? private $userError; //array of errors for display to the user. format key=>errormessage ! //private static $logger; function __construct($fail=false) *************** *** 37,41 **** $this->status = false; //no error yet $this->userError = array(); ! $this->logger = new Logger('ErrorLog'); } --- 37,41 ---- $this->status = false; //no error yet $this->userError = array(); ! //$this->logger = new Logger('ErrorLog'); } Index: stringUtil.php =================================================================== RCS file: /cvsroot/stack/stack-dev/lib/stringUtil.php,v retrieving revision 1.19 retrieving revision 1.19.2.1 diff -C2 -d -r1.19 -r1.19.2.1 *** stringUtil.php 27 Aug 2009 16:23:00 -0000 1.19 --- stringUtil.php 22 Nov 2010 23:05:52 -0000 1.19.2.1 *************** *** 226,252 **** } - /** - * Converts a CSV string into an array, removing empty entries. - * - * @param string in - * @return array out - * @access public - */ - public function cvsToArray($token=',') - { - $exploded = explode($token, $this->string); - - //remove any null entries - for($i = 0; $i < count($exploded); $i++) - { - $trim = trim($exploded[$i]); - if(!empty($trim)) - { - $toReturn[] = $exploded[$i]; - } - } - - return $toReturn; - } /** --- 226,229 ---- *************** *** 478,481 **** --- 455,484 ---- } + + /** + * Converts a CSV string into an array, removing empty entries. + * + * @param string in + * @return array out + * @access public + */ + public function cvsToArray($token=',') + { + $exploded = explode($token, $this->string); + + //remove any null entries + for($i = 0; $i < count($exploded); $i++) + { + $trim = trim($exploded[$i]); + if(!empty($trim)) + { + $toReturn[] = $exploded[$i]; + } + } + + return $toReturn; + } + + /** * Converts an array to a CSV *************** *** 515,518 **** --- 518,616 ---- } } + + /** + * Handles complex (comma-containing) list elements, + * i.e. sets {}, functions() and nested lists[[]] + * Strict checking on nesting. + * Helper for listToArrayWorkhorse() + */ + private function nextElement($list) { + if($list == '') return null; + // delimited by next comma at same degree of nesting + $startDelimiter = "[({"; + $endDelimiter = "])}"; + $nesting = array(0=>0, 1=>0, 2=>0); // stores nesting for delimiters above + for($i = 0; $i < strlen($list); $i++) { + $startChar = strpos($startDelimiter, $list[$i]);// which start delimiter + $endChar = strpos($endDelimiter, $list[$i]);// which end delimiter (if any) + + // change nesting for delimiter if specified + if($startChar !== false) $nesting[$startChar]++; + else if($endChar !== false) $nesting[$endChar]--; + + // otherwise, return element if all nestings are zero + else if($list[$i] == ',' + && $nesting[0] == 0 && $nesting[1] == 0 &&$nesting[2] == 0) { + return substr($list, 0, $i); + } + } + + // end of list reached + if($nesting[0] == 0 && $nesting[1] == 0 &&$nesting[2] == 0) { + return $list; + } + else return null; // invalid nesting + } + + /** + * Converts a list structure into an array. + * Handles nested lists, sets and functions with help from nextElement(). + */ + private function listToArrayWorkhorse($list,$rec=true) { + $list = trim($list); + $list = substr($list, 1, strlen($list) - 2);// trims outermost [] only + $e = $this->nextElement($list); + while($e !== null) { + if($e[0]=='[') { + if ($rec) { + $array[] = $this->listToArrayWorkhorse($e,$rec); + } else { + $array[] = $e; + } + } + else $array[] = $e; + $list = substr($list, strlen($e)+1); + $e = $this->nextElement($list); + } + return $array; + } + + /** + * Converts a list structure into an array. + * Handles nested lists, sets and functions with help from nextElement(). + */ + public function listToArray($rec=true) { + $array = $this->listToArrayWorkhorse($this->string,$rec); + return $array; + } + + /** + * Returns a more intuitive datestamp + */ + public static function prettifyDate($datestamp) { + /* + * Rules: + * show time if this week + * show day + month if this year + * show month + year if not this year + */ + + $dayStart = strtotime("00:00"); + $monthAgo = strtotime('-1 month'); + $yearAgo = strtotime('-1 year'); + + //echo "yearstart: $yearStart monthStart: $monthStart dayStart: $dayStart"; + + $time = strtotime($datestamp); + + if($time >= $dayStart) return date("g:ia", $time);// today + if($time > $monthAgo) return date("g:ia, j M", $time);// not today + if($time > $yearAgo) return date("M Y", $time); // not this year + if($time > $yearAgo) return date("j M", $time); // not this month + + + // failed to prettify somehow + return $datestamp; + } } Index: AimConverter.php =================================================================== RCS file: /cvsroot/stack/stack-dev/lib/AimConverter.php,v retrieving revision 1.6 retrieving revision 1.6.2.1 diff -C2 -d -r1.6 -r1.6.2.1 *** AimConverter.php 11 Sep 2009 08:43:55 -0000 1.6 --- AimConverter.php 22 Nov 2010 23:05:52 -0000 1.6.2.1 *************** *** 167,171 **** case "h": case "hide": ! $questionVariables .= "$text\n"; break; --- 167,184 ---- case "h": case "hide": ! // convert rand function ! $rand_pattern = '/:=rand\((-?\d+)\.\.(-?\d+)\)/'; ! preg_match($rand_pattern, $text, $matches); ! $lower = $matches[1]; ! $upper = $matches[2]; ! /* $questionVariables .= 'rand('.($upper-$lower).')'; // range ! if($lower != 0) { ! if($matches[1] > 0) echo '+'; ! echo $matches[1]; ! }*/ ! $text = preg_replace('/rand\('.$lower.'..'.$upper.'\)/', "rand_with_step($lower, $upper, 1)", $text); ! // change assignment operator ! $text = str_replace(':=', '=', $text); ! $questionVariables .= $text."\n"; break; |