Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_rule_type_file_size.inc
1 <?php
18 require_once dirname(__FILE__).'/../../form_question_rule/form_question_rule.inc';
19 
33 {
34 
35 
42  function __construct($assetid=0)
43  {
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
56  public static function getOperators()
57  {
58  $operators = Array(
59  '<=' => translate('core_form_rule_file_size_<='),
60  '>=' => translate('core_form_rule_file_size_>='),
61  );
62  return $operators;
63 
64  }//end getOperators()
65 
66 
83  function evaluate($answer, $rule_data, &$q_asset)
84  {
85  //get all data we need from either current submission or incomplete submission
86  if (!empty($_FILES['q'.$q_asset->id]['size'])) {
87  $filesize = $_FILES['q'.$q_asset->id]['size'];
88  }
89  else {
90  $extra_data = $q_asset->getExtraData();
91  $temp_file = array_get_index($extra_data, 'temp_filesystem_path','');
92  $filesize = (int) filesize ($temp_file);
93  }
94 
95  $operator = array_get_index($rule_data, 'operator', '<=');
96 
97  // comparison value will be the concatenation of the value and the
98  // multiplier
99  $comparison = array_get_index($rule_data, 'value', '').array_get_index($rule_data, 'multiplier', '');
100 
101 
102  $comparison = strtolower(trim($comparison));
103  $comp_value = floatval($comparison);
104 
105  // allow users to specify abbreviated forms of file size (kB, MB, GB)
106  // the conversion used is 1024 to the kByte, rather than the decimal 1000
107  if ((substr($comparison,-2) == 'kb') || (substr($comparison,-1) == 'k')) {
108  $comp_value *= 1024;
109  } else if ((substr($comparison,-2) == 'mb') || (substr($comparison,-1) == 'm')) {
110  $comp_value *= 1024 * 1024;
111  } else if ((substr($comparison,-2) == 'gb') || (substr($comparison,-1) == 'g')) {
112  $comp_value *= 1024 * 1024 * 1024;
113  }
114 
115  $expression = "return $filesize $operator $comp_value;";
116  $valid = eval($expression);
117 
118  return $valid;
119 
120  }//end evaluate()
121 
122 
132  function ruleDescription(&$q_asset, $rule_data)
133  {
134  $operator = array_get_index($rule_data, 'operator', '<=');
135  $comparison = array_get_index($rule_data, 'value', '').' '.array_get_index($rule_data, 'multiplier', '');
136 
137  $error_str = translate('core_form_rule_file_size_desc_'.$operator, $q_asset->attr('name'), $comparison);
138 
139  return $error_str;
140 
141  }//end ruleDescription()
142 
143 
144 }//end class
145 ?>