|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 /* vim: set expandtab tabstop=4 shiftwidth=4: */ 00003 // +----------------------------------------------------------------------+ 00004 // | PHP version 4.0 | 00005 // +----------------------------------------------------------------------+ 00006 // | Copyright (c) 1997-2003 The PHP Group | 00007 // +----------------------------------------------------------------------+ 00008 // | This source file is subject to version 2.0 of the PHP license, | 00009 // | that is bundled with this package in the file LICENSE, and is | 00010 // | available at through the world-wide-web at | 00011 // | http://www.php.net/license/2_02.txt. | 00012 // | If you did not receive a copy of the PHP license and are unable to | 00013 // | obtain it through the world-wide-web, please send a note to | 00014 // | license@php.net so we can mail you a copy immediately. | 00015 // +----------------------------------------------------------------------+ 00016 // | Author: Alexey Borzov <avb@php.net> | 00017 // +----------------------------------------------------------------------+ 00018 // 00019 // $Id: Compare.php,v 1.2 2010/12/14 17:35:59 moodlerobot Exp $ 00020 00021 require_once 'HTML/QuickForm/Rule.php'; 00022 00033 class HTML_QuickForm_Rule_Compare extends HTML_QuickForm_Rule 00034 { 00040 var $_operators = array( 00041 'eq' => '==', 00042 'neq' => '!=', 00043 'gt' => '>', 00044 'gte' => '>=', 00045 'lt' => '<', 00046 'lte' => '<=' 00047 ); 00048 00049 00057 function _findOperator($name) 00058 { 00059 if (empty($name)) { 00060 return '=='; 00061 } elseif (isset($this->_operators[$name])) { 00062 return $this->_operators[$name]; 00063 } elseif (in_array($name, $this->_operators)) { 00064 return $name; 00065 } else { 00066 return '=='; 00067 } 00068 } 00069 00070 00071 function validate($values, $operator = null) 00072 { 00073 $operator = $this->_findOperator($operator); 00074 if ('==' != $operator && '!=' != $operator) { 00075 $compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);'); 00076 } else { 00077 $compareFn = create_function('$a, $b', 'return $a ' . $operator . ' $b;'); 00078 } 00079 00080 return $compareFn($values[0], $values[1]); 00081 } 00082 00083 00084 function getValidationScript($operator = null) 00085 { 00086 $operator = $this->_findOperator($operator); 00087 if ('==' != $operator && '!=' != $operator) { 00088 $check = "!(Number({jsVar}[0]) {$operator} Number({jsVar}[1]))"; 00089 } else { 00090 $check = "!({jsVar}[0] {$operator} {jsVar}[1])"; 00091 } 00092 return array('', "'' != {jsVar}[0] && {$check}"); 00093 } 00094 } 00095 ?>