Completed
Branch FET-8365-add-indexes (822683)
by
unknown
430:30 queued 414:13
created

EEM_Question::system_question_maxes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4286
cc 1
eloc 10
nc 1
nop 0
1
<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed');
2
/**
3
 * Event Espresso
4
 *
5
 * Event Registration and Management Plugin for WordPress
6
 *
7
 * @ package			Event Espresso
8
 * @ author				Seth Shoultes
9
 * @ copyright		(c) 2008-2011 Event Espresso  All Rights Reserved.
10
 * @ license			http://eventespresso.com/support/terms-conditions/   * see Plugin Licensing *
11
 * @ link					http://www.eventespresso.com
12
 * @ version		 	4.0
13
 *
14
 * ------------------------------------------------------------------------
15
 *
16
 * Question Model
17
 *
18
 * @package			Event Espresso
19
 * @subpackage		includes/models/
20
 * @author				Michael Nelson
21
 *
22
 * ------------------------------------------------------------------------
23
 */
24
require_once ( EE_MODELS . 'EEM_Soft_Delete_Base.model.php' );
25
require_once( EE_CLASSES . 'EE_Question.class.php');
26
27
class EEM_Question extends EEM_Soft_Delete_Base {
28
29
	// constant used to indicate that the question type is COUNTRY
30
	const QST_type_country = 'COUNTRY';
31
32
	// constant used to indicate that the question type is DATE
33
	const QST_type_date = 'DATE';
34
35
	// constant used to indicate that the question type is DROPDOWN
36
	const QST_type_dropdown = 'DROPDOWN';
37
38
	// constant used to indicate that the question type is CHECKBOX
39
	const QST_type_checkbox = 'CHECKBOX';
40
41
	// constant used to indicate that the question type is RADIO_BTN
42
	const QST_type_radio = 'RADIO_BTN';
43
44
	// constant used to indicate that the question type is STATE
45
	const QST_type_state = 'STATE';
46
47
	// constant used to indicate that the question type is TEXT
48
	const QST_type_text = 'TEXT';
49
50
	// constant used to indicate that the question type is TEXTAREA
51
	const QST_type_textarea = 'TEXTAREA';
52
53
	// constant used to indicate that the question type is a TEXTAREA that allows simple html
54
	const QST_type_html_textarea = 'HTML_TEXTAREA';
55
	/**
56
	 * Question types that are interchangeable, even after answers have been provided for them.
57
	 * Top-level keys are category slugs, next level is an array of question types. If question types
58
	 * aren't in this array, it is assumed they AREN'T interchangeable with any other question types.
59
	 * @var array
60
	 */
61
	protected $_question_type_categories = null;
62
	/**
63
	 * lists all the question types which should be allowed. Ideally, this will be extensible.
64
	 * @access private
65
	 * @var array of strings
66
	 */
67
	protected $_allowed_question_types;
68
69
	// private instance of the Attendee object
70
	protected static $_instance = NULL;
71
72
	protected function __construct( $timezone = NULL ) {
73
		$this->singular_item = __('Question','event_espresso');
74
		$this->plural_item = __('Questions','event_espresso');
75
		$this->_allowed_question_types=apply_filters(
76
			'FHEE__EEM_Question__construct__allowed_question_types',
77
			array(
78
				EEM_Question::QST_type_text =>__('Text','event_espresso'),
79
				EEM_Question::QST_type_textarea =>__('Textarea','event_espresso'),
80
				EEM_Question::QST_type_checkbox =>__('Checkboxes','event_espresso'),
81
				EEM_Question::QST_type_radio =>__('Radio Buttons','event_espresso'),
82
				EEM_Question::QST_type_dropdown =>__('Dropdown','event_espresso'),
83
				EEM_Question::QST_type_state =>__('State/Province Dropdown','event_espresso'),
84
				EEM_Question::QST_type_country =>__('Country Dropdown','event_espresso'),
85
				EEM_Question::QST_type_date =>__('Date Picker','event_espresso'),
86
				EEM_Question::QST_type_html_textarea => __( 'HTML Textarea', 'event_espresso' ),
87
			)
88
		);
89
		$this->_question_type_categories = apply_filters(
90
				'FHEE__EEM_Question__construct__question_type_categories',
91
				array(
92
				'text' => array(
93
						self::QST_type_text,
94
						self::QST_type_textarea,
95
						self::QST_type_html_textarea,
96
					),
97
				'single-answer-enum' => array(
98
					self::QST_type_radio,
99
					self::QST_type_dropdown
100
				),
101
			)
102
		);
103
104
		$this->_tables = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('Question' => new ...p_question', 'QST_ID')) of type array<string,object<EE_P...ct<EE_Primary_Table>"}> is incompatible with the declared type array<integer,object<EE_Table_Base>> of property $_tables.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
105
			'Question'=>new EE_Primary_Table('esp_question','QST_ID')
106
		);
107
		$this->_fields = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('Question' => arra...esso'), false, false))) of type array<string,array<strin...shed_Flag_Field>\"}>"}> is incompatible with the declared type array<integer,object<EE_Model_Field_Base>> of property $_fields.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
108
			'Question'=>array(
109
				'QST_ID'=>new EE_Primary_Key_Int_Field('QST_ID', __('Question ID','event_espresso')),
110
				'QST_display_text'=>new EE_Full_HTML_Field('QST_display_text', __('Question Text','event_espresso'), true, ''),
1 ignored issue
show
Documentation introduced by
'' is of type string, but the function expects a null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
				'QST_admin_label'=>new EE_Plain_Text_Field('QST_admin_label', __('Question Label (admin-only)','event_espresso'), true, ''),
1 ignored issue
show
Documentation introduced by
'' is of type string, but the function expects a null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
112
				'QST_system'=>new EE_Plain_Text_Field('QST_system', __('Internal string ID for question','event_espresso'), TRUE, NULL ),
113
				'QST_type'=>new EE_Enum_Text_Field('QST_type', __('Question Type','event_espresso'),false, 'TEXT',$this->_allowed_question_types),
114
				'QST_required'=>new EE_Boolean_Field('QST_required', __('Required Question?','event_espresso'), false, false),
1 ignored issue
show
Documentation introduced by
false is of type boolean, but the function expects a null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
115
				'QST_required_text'=>new EE_Simple_HTML_Field('QST_required_text', __('Text to Display if Not Provided','event_espresso'), true, ''),
1 ignored issue
show
Documentation introduced by
'' is of type string, but the function expects a null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
116
				'QST_order'=>new EE_Integer_Field('QST_order', __('Question Order','event_espresso'), false, 0),
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
				'QST_admin_only'=>new EE_Boolean_Field('QST_admin_only', __('Admin-Only Question?','event_espresso'), false, false),
1 ignored issue
show
Documentation introduced by
false is of type boolean, but the function expects a null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
118
				'QST_max' => new EE_Infinite_Integer_Field( 'QST_max', __( 'Max Size', 'event_espresso'	), false, EE_INF ),
119
				'QST_wp_user'=>new EE_WP_User_Field('QST_wp_user', __('Question Creator ID','event_espresso'), false ),
120
				'QST_deleted'=>new EE_Trashed_Flag_Field('QST_deleted', __('Flag Indicating question was deleted','event_espresso'), false, false)
121
			)
122
		);
123
		$this->_model_relations = array(
0 ignored issues
show
Documentation Bug introduced by
It seems like array('Question_Group' =...EE_Has_Many_Relation()) of type array<string,object<EE_H...E_Has_Many_Relation>"}> is incompatible with the declared type array<integer,object<EE_Model_Relation_Base>> of property $_model_relations.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
124
			'Question_Group'=>new EE_HABTM_Relation('Question_Group_Question'),
125
			'Question_Option'=>new EE_Has_Many_Relation(),
126
			'Answer'=>new EE_Has_Many_Relation(),
127
			'WP_User' => new EE_Belongs_To_Relation(),
128
			//for QST_order column
129
			'Question_Group_Question'=>new EE_Has_Many_Relation()
130
		);
131
		//this model is generally available for reading
132
		$this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public();
133
		$this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Reg_Form('QST_system');
134
		$this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Reg_Form('QST_system');
135
		$this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Reg_Form('QST_system');
136
		parent::__construct( $timezone );
137
	}
138
139
	/**
140
	 * Returns the list of allowed question types, which are normally: 'TEXT','TEXTAREA','RADIO_BTN','DROPDOWN','CHECKBOX','DATE'
141
	 * but they can be extended
142
	 * @return string[]
143
	 */
144
	public function allowed_question_types(){
145
		return $this->_allowed_question_types;
146
	}
147
	/**
148
	 * Gets all the question types in the same category
149
	 * @param string $question_type one of EEM_Question::allowed_question_types(
150
	 * @return string[] like EEM_Question::allowed_question_types()
151
	 */
152
	public function question_types_in_same_category( $question_type ) {
153
		$question_types = array( $question_type );
154
		foreach( $this->_question_type_categories as $category => $question_types_in_category ) {
155
			if( in_array( $question_type, $question_types_in_category ) ) {
156
				$question_types = $question_types_in_category;
157
				break;
158
			}
159
		}
160
161
		return array_intersect_key( $this->allowed_question_types(), array_flip( $question_types ) );
162
	}
163
164
	/**
165
	 * Determines if the given question type is in the given question type category
166
	 * @param string $question_type one of EEM_Question::allowed_question_types()
167
	 * @param string $category one of the top-level keys of EEM_Question::question_type_categories()
168
	 * @return boolean
169
	 */
170
	public function question_type_is_in_category( $question_type, $category ) {
171
		if( ! isset( $this->_question_type_categories[ $category ] ) ) {
172
			return false;
173
		}
174
		return in_array( $question_type, $this->_question_type_categories[ $category ] );
175
	}
176
177
	/**
178
	 * Returns the question type categories 2d array
179
	 * @return array see EEM_Question::_question_type_categories
180
	 */
181
	public function question_type_categories() {
182
		return $this->_question_type_categories;
183
	}
184
185
	/**
186
	 * Returns an array of all the QST_system values that can be allowed in the system question group
187
	 * identified by $system_question_group_id
188
	 * @param string $system_question_group_id QSG_system
189
	 * @return array of system question names (QST_system)
190
	 */
191
	public function allowed_system_questions_in_system_question_group( $system_question_group_id ) {
192
		$question_system_ids = array();
193
		switch( $system_question_group_id ) {
194
			case EEM_Question_Group::system_personal:
195
				$question_system_ids = array(
196
					EEM_Attendee::system_question_fname,
197
					EEM_Attendee::system_question_lname,
198
					EEM_Attendee::system_question_email,
199
					EEM_Attendee::system_question_phone
200
				);
201
				break;
202
			case EEM_Question_Group::system_address:
203
				$question_system_ids = array(
204
					EEM_Attendee::system_question_address,
205
					EEM_Attendee::system_question_address2,
206
					EEM_Attendee::system_question_city,
207
					EEM_Attendee::system_question_state,
208
					EEM_Attendee::system_question_country,
209
					EEM_Attendee::system_question_zip,
210
					EEM_Attendee::system_question_phone
211
				);
212
				break;
213
		}
214
		return apply_filters( 'FHEE__EEM_Question__system_questions_allowed_in_system_question_group__return', $question_system_ids, $system_question_group_id );
215
	}
216
217
	/**
218
	 * Returns an array of all the QST_system values that are required in the system question group
219
	 * identified by $system_question_group_id
220
	 * @param string $system_question_group_id QSG_system
221
	 * @return array of system question names (QST_system)
222
	 */
223
	public function required_system_questions_in_system_question_group( $system_question_group_id ) {
224
		$question_system_ids = null;
0 ignored issues
show
Unused Code introduced by
$question_system_ids is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
225
		switch( $system_question_group_id ) {
226
			case EEM_Question_Group::system_personal:
227
				$question_system_ids =  array(
228
					EEM_Attendee::system_question_fname,
229
					EEM_Attendee::system_question_email,
230
				);
231
				break;
232
			default:
233
				$question_system_ids = array();
234
		}
235
		return apply_filters( 'FHEE__EEM_Question__system_questions_required_in_system_question_group', $question_system_ids, $system_question_group_id );
236
	}
237
238
239
240
	/**
241
	 * Gets an array for converting between QST_system and QST_IDs for system questions. Eg, if you want to know
242
	 * which system question QST_ID corresponds to the QST_system 'city', use EEM_Question::instance()->get_Question_ID_from_system_string('city');
243
	 * @param $QST_system
244
	 * @return int of QST_ID for the question that corresponds to that QST_system
245
	 */
246
	public function get_Question_ID_from_system_string( $QST_system ){
247
		 return $this->get_var( array( array( 'QST_system' => $QST_system ) ) );
248
	}
249
250
251
252
	/**
253
	 * searches the db for the question with the latest question order and returns that value.
254
	 * @access public
255
	 * @return int
256
	 */
257 View Code Duplication
	public function get_latest_question_order() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
		$columns_to_select = array(
259
			'max_order' => array("MAX(QST_order)","%d")
260
		);
261
		$max = $this->_get_all_wpdb_results( array(), ARRAY_A, $columns_to_select );
262
		return $max[0]['max_order'];
263
	}
264
265
	/**
266
	 * Returns an array where keys are system question QST_system values,
267
	 * and values are the highest question max the admin can set on the question
268
	 * (aka the "max max"; eg, a site admin can change the zip question to have a max
269
	 * of 5, but no larger than 12)
270
	 * @return array
271
	 */
272
	public function system_question_maxes() {
273
		return array(
274
			'fname' => 45,
275
			'lname' => 45,
276
			'address' => 255,
277
			'address2' => 255,
278
			'city' => 45,
279
			'zip' => 12,
280
			'email' => 255,
281
			'phone' => 45,
282
		);
283
	}
284
285
	/**
286
	 * Given a QST_system value, gets the question's largest allowable max input.
287
	 * @see Registration_Form_Admin_Page::system_question_maxes()
288
	 * @param string $system_question_value
289
	 * @return int|float
290
	 */
291
	public function absolute_max_for_system_question( $system_question_value ) {
292
		$maxes = $this->system_question_maxes();
293
		if( isset( $maxes[ $system_question_value ] ) ) {
294
			return $maxes[ $system_question_value ];
295
		} else {
296
			return EE_INF;
297
		}
298
	}
299
300
301
302
303
304
}
305
// End of file EEM_Question.model.php
306
// Location: /includes/models/EEM_Question.model.php
307