Completed
Branch BUG-9179-default-price-please (cae472)
by
unknown
85:30 queued 68:14
created

EE_Question::max()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php if ( !defined( 'EVENT_ESPRESSO_VERSION' ) ) {
2
	exit( 'No direct script access allowed' );
3
}
4
/**
5
 * Event Espresso
6
 *
7
 * Event Registration and Management Plugin for WordPress
8
 *
9
 * @ package 		Event Espresso
10
 * @ author 		Event Espresso
11
 * @ copyright 	(c) 2008-2011 Event Espresso  All Rights Reserved.
12
 * @ license 		{@link http://eventespresso.com/support/terms-conditions/}   * see Plugin Licensing *
13
 * @ link 				{@link http://www.eventespresso.com}
14
 * @ since 			4.0
15
 *
16
 */
17
18
19
20
/**
21
 * EE_Question class
22
 *
23
 * @package 			Event Espresso
24
 * @subpackage 	includes/classes/EE_Answer.class.php
25
 * @author 				Mike Nelson
26
 *
27
 * ------------------------------------------------------------------------
28
 */
29
class EE_Question extends EE_Soft_Delete_Base_Class implements EEI_Duplicatable {
30
31
	/**
32
	 *
33
	 * @param array $props_n_values  incoming values
34
	 * @param string $timezone  incoming timezone (if not set the timezone set for the website will be
35
	 *                          		used.)
36
	 * @param array $date_formats  incoming date_formats in an array where the first value is the
37
	 *                             		    date_format and the second value is the time format
38
	 * @return EE_Question
39
	 */
40
	public static function new_instance( $props_n_values = array(), $timezone = null, $date_formats = array() ) {
41
		$has_object = parent::_check_for_object( $props_n_values, __CLASS__ );
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (_check_for_object() instead of new_instance()). Are you sure this is correct? If so, you might want to change this to $this->_check_for_object().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
42
		return $has_object ? $has_object : new self( $props_n_values, false, $timezone, $date_formats );
43
	}
44
45
46
47
	/**
48
	 * @param array $props_n_values  incoming values from the database
49
	 * @param string $timezone  incoming timezone as set by the model.  If not set the timezone for
50
	 *                          		the website will be used.
51
	 * @return EE_Question
52
	 */
53
	public static function new_instance_from_db( $props_n_values = array(), $timezone = null ) {
54
		return new self( $props_n_values, TRUE, $timezone );
55
	}
56
57
58
59
	/**
60
	 *        Set    Question display text
61
	 *
62
	 * @access        public
63
	 * @param string $QST_display_text
64
	 */
65
	public function set_display_text( $QST_display_text = '' ) {
66
		$this->set( 'QST_display_text', $QST_display_text );
67
	}
68
69
70
71
	/**
72
	 *        Set    Question admin text
73
	 *
74
	 * @access        public
75
	 * @param        string $QST_admin_label
76
	 */
77
	public function set_admin_label( $QST_admin_label = '' ) {
78
		$this->set( 'QST_admin_label', $QST_admin_label );
79
	}
80
81
82
83
	/**
84
	 *        Set    system name
85
	 *
86
	 * @access        public
87
	 * @param        mixed $QST_system
88
	 */
89
	public function set_system_ID( $QST_system = '' ) {
90
		$this->set( 'QST_system', $QST_system );
91
	}
92
93
94
95
	/**
96
	 *        Set    question's type
97
	 *
98
	 * @access        public
99
	 * @param        string $QST_type
100
	 */
101
	public function set_question_type( $QST_type = '' ) {
102
		$this->set( 'QST_type', $QST_type );
103
	}
104
105
106
107
	/**
108
	 *        Sets whether this question must be answered when presented in a form
109
	 *
110
	 * @access        public
111
	 * @param        bool $QST_required
112
	 */
113
	public function set_required( $QST_required = FALSE ) {
114
		$this->set( 'QST_required', $QST_required );
115
	}
116
117
118
119
	/**
120
	 *        Set    Question display text
121
	 *
122
	 * @access        public
123
	 * @param        string $QST_required_text
124
	 */
125
	public function set_required_text( $QST_required_text = '' ) {
126
		$this->set( 'QST_required_text', $QST_required_text );
127
	}
128
129
130
131
	/**
132
	 *        Sets the order of this question when placed in a sequence of questions
133
	 *
134
	 * @access        public
135
	 * @param        int $QST_order
136
	 */
137
	public function set_order( $QST_order = 0 ) {
138
		$this->set( 'QST_order', $QST_order );
139
	}
140
141
142
143
	/**
144
	 *        Sets whether the question is admin-only
145
	 *
146
	 * @access        public
147
	 * @param        bool $QST_admin_only
148
	 */
149
	public function set_admin_only( $QST_admin_only = FALSE ) {
150
		$this->set( 'QST_admin_only', $QST_admin_only );
151
	}
152
153
154
155
	/**
156
	 *        Sets the wordpress user ID on the question
157
	 *
158
	 * @access        public
159
	 * @param        int $QST_wp_user
160
	 */
161
	public function set_wp_user( $QST_wp_user = 1 ) {
162
		$this->set( 'QST_wp_user', $QST_wp_user );
163
	}
164
165
166
167
	/**
168
	 *        Sets whether the question has been deleted
169
	 *        (we use this boolean instead of actually
170
	 *        deleting it because when users delete this question
171
	 *        they really want to remove the question from future
172
	 *        forms, BUT keep their old answers which depend
173
	 *        on this record actually existing.
174
	 *
175
	 * @access        public
176
	 * @param 	bool $QST_deleted
177
	 */
178
	public function set_deleted( $QST_deleted = FALSE ) {
179
		$this->set( 'QST_deleted', $QST_deleted );
180
	}
181
182
183
184
	/**
185
	 * returns the text for displaying the question to users
186
	 * @access public
187
	 * @return string
188
	 */
189
	public function display_text() {
190
		return $this->get( 'QST_display_text' );
191
	}
192
193
194
195
	/**
196
	 * returns the text for the administrative label
197
	 * @access public
198
	 * @return string
199
	 */
200
	public function admin_label() {
201
		return $this->get( 'QST_admin_label' );
202
	}
203
204
205
206
	/**
207
	 * returns the attendee column name for this question
208
	 * @access public
209
	 * @return string
210
	 */
211
	public function system_ID() {
212
		return $this->get( 'QST_system' );
213
	}
214
215
216
217
	/**
218
	 * returns either a string of 'text', 'textfield', etc.
219
	 * @access public
220
	 * @return boolean
221
	 */
222
	public function required() {
223
		return $this->get( 'QST_required' );
224
	}
225
226
227
228
	/**
229
	 * returns the text which should be displayed when a user
230
	 * doesn't answer this question in a form
231
	 * @access public
232
	 * @return string
233
	 */
234
	public function required_text() {
235
		return $this->get( 'QST_required_text' );
236
	}
237
238
239
240
	/**
241
	 * returns the type of this question
242
	 * @access public
243
	 * @return string
244
	 */
245
	public function type() {
246
		return $this->get( 'QST_type' );
247
	}
248
249
250
251
	/**
252
	 * returns an integer showing where this question should
253
	 * be placed in a sequence of questions
254
	 * @access public
255
	 * @return int
256
	 */
257
	public function order() {
258
		return $this->get( 'QST_order' );
259
	}
260
261
262
263
	/**
264
	 * returns whether this question should only appears to admins,
265
	 * or to everyone
266
	 * @access public
267
	 * @return boolean
268
	 */
269
	public function admin_only() {
270
		return $this->get( 'QST_admin_only' );
271
	}
272
273
274
275
	/**
276
	 * returns the id the wordpress user who created this question
277
	 * @access public
278
	 * @return int
279
	 */
280
	public function wp_user() {
281
		return $this->get( 'QST_wp_user' );
282
	}
283
284
285
286
	/**
287
	 * returns whether this question has been marked as 'deleted'
288
	 * @access public
289
	 * @return boolean
290
	 */
291
	public function deleted() {
292
		return $this->get( 'QST_deleted' );
293
	}
294
295
296
297
	/**
298
	 * Gets an array of related EE_Answer  to this EE_Question
299
	 * @return EE_Answer[]
300
	 */
301
	public function answers() {
302
		return $this->get_many_related( 'Answer' );
303
	}
304
305
306
307
	/**
308
	 * Boolean check for if there are answers on this question in th db
309
	 * @return boolean true = has answers, false = no answers.
310
	 */
311
	public function has_answers() {
312
		return $this->count_related( 'Answer' ) > 0 ? TRUE : FALSE;
313
	}
314
315
316
317
	/**
318
	 * gets an array of EE_Question_Group which relate to this question
319
	 * @return EE_Question_Group[]
320
	 */
321
	public function question_groups() {
322
		return $this->get_many_related( 'Question_Group' );
323
	}
324
325
326
327
	/**
328
	 * Returns all the options for this question. By default, it returns only the not-yet-deleted ones.
329
	 * @param boolean      $notDeletedOptionsOnly            1
330
	 *                                                       whether to return ALL options, or only the ones which have not yet been deleleted
331
	 * @param string|array $selected_value_to_always_include , when retrieving options to an ANSWERED question,
332
	 *                                                       we want to usually only show non-deleted options AND the value that was selected for the answer,
333
	 *                                                       whether it was trashed or not.
334
	 * @return EE_Question_Option[]
335
	 */
336
	public function options( $notDeletedOptionsOnly = TRUE, $selected_value_to_always_include = NULL ) {
337
		if ( ! $this->ID() ) {
338
			return array();
339
		}
340
		$query_params = array();
341
		if ( $selected_value_to_always_include ) {
342
			if ( is_array( $selected_value_to_always_include ) ) {
343
				$query_params[ 0 ][ 'OR*options-query' ][ 'QSO_value' ] = array( 'IN', $selected_value_to_always_include );
344
			} else {
345
				$query_params[ 0 ][ 'OR*options-query' ][ 'QSO_value' ] = $selected_value_to_always_include;
346
			}
347
		}
348
		if ( $notDeletedOptionsOnly ) {
349
			$query_params[ 0 ][ 'OR*options-query' ][ 'QSO_deleted' ] = FALSE;
350
		}
351
		//order by QSO_order
352
		$query_params[ 'order_by' ] = array( 'QSO_order' => 'ASC' );
353
		return $this->get_many_related( 'Question_Option', $query_params );
354
	}
355
356
357
358
	/**
359
	 * returns an array of EE_Question_Options which relate to this question
360
	 * @return \EE_Question_Option[]
361
	 */
362
	public function temp_options() {
363
		return $this->_model_relations[ 'Question_Option' ];
364
	}
365
366
367
368
	/**
369
	 * Adds an option for this question. Note: if the option were previously associated with a different
370
	 * Question, that relationship will be overwritten.
371
	 * @param EE_Question_Option $option
372
	 * @return boolean success
373
	 */
374
	public function add_option( EE_Question_Option $option ) {
375
		return $this->_add_relation_to( $option, 'Question_Option' );
376
	}
377
378
379
380
	/**
381
	 * Adds an option directly to this question without saving to the db
382
	 * @param EE_Question_Option $option
383
	 * @return boolean success
384
	 */
385
	public function add_temp_option( EE_Question_Option $option ) {
386
		$this->_model_relations[ 'Question_Option' ][ ] = $option;
387
		return TRUE;
388
	}
389
390
391
392
	/**
393
	 * Marks the option as deleted.
394
	 * @param EE_Question_Option $option
395
	 * @return boolean success
396
	 */
397
	public function remove_option( EE_Question_Option $option ) {
398
		return $this->_remove_relation_to( $option, 'Question_Option' );
399
	}
400
401
402
403
	/**
404
	 * @return bool
405
	 */
406
	public function is_system_question() {
407
		$system_ID = $this->get( 'QST_system' );
408
		return ! empty( $system_ID ) ? TRUE : FALSE;
409
	}
410
411
412
413
	/**
414
	 * The purpose of this method is set the question order this question order to be the max out of all questions
415
	 *
416
	 * @access public
417
	 * @return void
418
	 */
419
	public function set_order_to_latest() {
420
		$latest_order = $this->get_model()->get_latest_question_order();
0 ignored issues
show
Bug introduced by
The method get_latest_question_order cannot be called on $this->get_model() (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
421
		$latest_order ++;
422
		$this->set( 'QST_order', $latest_order );
423
	}
424
425
426
427
	/**
428
	 * Retrieves the list of allowed question types from the model.
429
	 * @return string[]
430
	 */
431
	private function _allowed_question_types() {
432
		$questionModel = $this->get_model();
433
		/* @var $questionModel EEM_Question */
434
		return $questionModel->allowed_question_types();
435
	}
436
437
	/**
438
	 * Duplicates this question and its question options
439
	 * @return \EE_Question
440
	 */
441
	public function duplicate( $options = array() ) {
442
		$new_question = clone $this;
443
		$new_question->set( 'QST_ID', null );
444
		$new_question->set_display_text( sprintf( __( '%s **Duplicate**', 'event_espresso' ), $this->display_text() ) );
445
		$new_question->set_admin_label( sprintf( __( '%s **Duplicate**', 'event_espresso' ), $this->admin_label() ) );
446
		$new_question->set_system_ID( null );
447
		$new_question->set_wp_user( get_current_user_id() );
448
                //if we're duplicating a trashed question, assume we don't want the new one to be trashed
449
                $new_question->set_deleted( false );
450
		$success = $new_question->save();
451
		if( $success ) {
452
			//we don't totally want to duplicate the question options, because we want them to be for the NEW question
453
			foreach( $this->options() as $question_option ) {
454
				$question_option->duplicate( array( 'QST_ID' => $new_question->ID() ) );
455
			}
456
			return $new_question;
457
		} else {
458
			return null;
459
		}
460
	}
461
	
462
	/**
463
	 * Returns the question's maximum allowed response size
464
	 * @return int|float
465
	 */
466
	public function max() {
467
		return $this->get( 'QST_max' );
468
	}
469
	
470
	/**
471
	 * Sets the question's maximum allowed response size
472
	 * @param int|float $new_max
473
	 * @return int|float
474
	 */
475
	public function set_max( $new_max ) {
476
		return $this->set( 'QST_max', $new_max );
477
	}
478
479
480
}
481