Completed
Branch BUG-8784-add_meta_boxes_hook (f2e044)
by
unknown
147:53 queued 130:39
created

RegistrationsReport   F

Complexity

Total Complexity 48

Size/Duplication

Total Lines 396
Duplicated Lines 27.78 %

Coupling/Cohesion

Components 2
Dependencies 22

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 48
lcom 2
cbo 22
dl 110
loc 396
rs 3.894
c 5
b 1
f 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get_filename_from_event() 8 11 3
A _get_questions_for_report() 0 12 2
F get_csv_data_for() 85 218 35
B create_job() 0 32 3
B count_units_to_process() 5 26 2
A cleanup_job() 8 8 1
B continue_job() 4 24 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like RegistrationsReport often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use RegistrationsReport, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 *
4
 * Class RegistrationsReport
5
 *
6
 * Generates the registrations report for the specified event,
7
 * or for all events
8
 *
9
 * @package         Event Espresso
10
 * @subpackage    batch
11
 * @author				Mike Nelson
12
 * @since		 	   4.8.26
13
 *
14
 */
15
namespace EventEspressoBatchRequest\JobHandlers;
16
17
use EventEspressoBatchRequest\JobHandlerBaseClasses\JobHandlerFile;
18
use EventEspressoBatchRequest\Helpers\BatchRequestException;
19
use EventEspressoBatchRequest\Helpers\JobParameters;
20
use EventEspressoBatchRequest\Helpers\JobStepResponse;
21
22
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) {
23
	exit( 'No direct script access allowed' );
24
}
25
26
27
28
class RegistrationsReport extends JobHandlerFile {
29
30
	/**
31
	 * Performs any necessary setup for starting the job. This is also a good
32
	 * place to setup the $job_arguments which will be used for subsequent HTTP requests
33
	 * when continue_job will be called
34
	 * @param JobParameters $job_parameters
35
	 * @throws BatchRequestException
36
	 * @return JobStepResponse
37
	 */
38
	public function create_job( JobParameters $job_parameters ) {
39
		$event_id = intval( $job_parameters->request_datum( 'EVT_ID', '0' ) );
40
		if( ! \EE_Capabilities::instance()->current_user_can( 'ee_read_registrations', 'generating_report' ) ) {
41
			throw new BatchRequestException(
42
				__( 'You do not have permission to view registrations', 'event_espresso')
43
			);
44
		}
45
		$filepath = $this->create_file_from_job_with_name(
46
			$job_parameters->job_id(),
47
			$this->get_filename_from_event( $event_id )
48
		);
49
		$job_parameters->add_extra_data( 'filepath', $filepath );
50
		$question_data_for_columns = $this->_get_questions_for_report( $event_id );
51
		$job_parameters->add_extra_data( 'questions_data', $question_data_for_columns );
52
		$job_parameters->set_job_size( $this->count_units_to_process( $event_id ) );
53
		//we should also set the header columns
54
		$csv_data_for_row = $this->get_csv_data_for(
55
			$event_id,
56
			0,
57
			1,
58
			$job_parameters->extra_datum( 'questions_data' ) );
0 ignored issues
show
Bug introduced by
It seems like $job_parameters->extra_datum('questions_data') targeting EventEspressoBatchReques...rameters::extra_datum() can also be of type string; however, EventEspressoBatchReques...ort::get_csv_data_for() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
59
		\EE_Registry::instance()->load_helper( 'Export' );
60
		\EEH_Export::write_data_array_to_csv( $filepath, $csv_data_for_row, true );
61
		//if we actually processed a row there, record it
62
		if( $job_parameters->job_size() ) {
63
			$job_parameters->mark_processed( 1 );
64
		}
65
		return new JobStepResponse(
66
			$job_parameters,
67
			__( 'Registrations report started successfully...', 'event_espresso' )
68
		);
69
	}
70
71
72
73
	/**
74
	 * Creates teh filename form the event id (or lack thereof)
75
	 * @param int $event_id
76
	 * @return string
77
	 */
78
	protected function get_filename_from_event( $event_id ) {
79 View Code Duplication
		if( $event_id ){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
80
			$event_slug =  \EEM_Event::instance()->get_var( array( array( 'EVT_ID' => $event_id ) ), 'EVT_slug' );
81
			if( ! $event_slug ) {
82
				$event_slug = __( 'unknown', 'event_espresso' );
83
			}
84
		}else{
85
			$event_slug = __( 'all', 'event_espresso' );
86
		}
87
		return sprintf( "registrations-for-%s.csv", $event_slug );
88
	}
89
90
	/**
91
	 * Gets the questions which are to be used for this report, so they
92
	 * can be remembered for later
93
	 * @param int|null $event_id
94
	 * @return array of wpdb results for questions which are to be used for this report
95
	 */
96
	protected function _get_questions_for_report( $event_id ) {
97
		$question_query_params = array(
98
			array(
99
				'Answer.ANS_ID' => array( 'IS_NOT_NULL' ),
100
			),
101
			'group_by' => array( 'QST_ID' )
102
		);
103
		if( $event_id ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $event_id of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
104
			$question_query_params[0]['Answer.Registration.EVT_ID'] = $event_id;
105
		}
106
		return \EEM_Question::instance()->get_all_wpdb_results( $question_query_params );
107
	}
108
109
110
111
	/**
112
	 * Performs another step of the job
113
	 *
114
	 * @param JobParameters $job_parameters
115
	 * @param int           $batch_size
116
	 * @return JobStepResponse
117
	 * @throws \EE_Error
118
	 */
119
	public function continue_job( JobParameters $job_parameters, $batch_size = 50 ) {
120
		$csv_data = $this->get_csv_data_for(
121
			$job_parameters->request_datum( 'EVT_ID', '0'),
0 ignored issues
show
Documentation introduced by
$job_parameters->request_datum('EVT_ID', '0') is of type string|array, but the function expects a integer|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...
122
			$job_parameters->units_processed(),
123
			$batch_size,
124
			$job_parameters->extra_datum( 'questions_data' ) );
0 ignored issues
show
Bug introduced by
It seems like $job_parameters->extra_datum('questions_data') targeting EventEspressoBatchReques...rameters::extra_datum() can also be of type string; however, EventEspressoBatchReques...ort::get_csv_data_for() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
125
		\EE_Registry::instance()->load_helper( 'Export' );
126
		\EEH_Export::write_data_array_to_csv( $job_parameters->extra_datum( 'filepath' ), $csv_data, false );
0 ignored issues
show
Bug introduced by
It seems like $job_parameters->extra_datum('filepath') targeting EventEspressoBatchReques...rameters::extra_datum() can also be of type array; however, EEH_Export::write_data_array_to_csv() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
127
		$units_processed = count( $csv_data );
128
		$job_parameters->mark_processed( $units_processed );
129
		$extra_response_data = array(
130
			'file_url' => ''
131
		);
132 View Code Duplication
		if( $units_processed < $batch_size ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
133
			$job_parameters->set_status( JobParameters::status_complete );
134
			$extra_response_data[ 'file_url' ] = $this->get_url_to_file( $job_parameters->extra_datum( 'filepath' ) );
0 ignored issues
show
Bug introduced by
It seems like $job_parameters->extra_datum('filepath') targeting EventEspressoBatchReques...rameters::extra_datum() can also be of type array; however, EventEspressoBatchReques...File::get_url_to_file() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
135
		}
136
		return new JobStepResponse(
137
				$job_parameters,
138
				sprintf(
139
					__( 'Wrote %1$s rows to report CSV file...', 'event_espresso' ),
140
					count( $csv_data ) ),
141
				$extra_response_data );
142
	}
143
144
	/**
145
	 * Gets the csv data for a batch of registrations
146
	 * @param int|null $event_id
147
	 * @param int $offset
148
	 * @param int $limit
149
	 * @param array $questions_for_these_regs_rows results of $wpdb->get_results( $something, ARRAY_A) when querying for questions
150
	 * @return array top-level keys are numeric, next-level keys are column headers
151
	 *
152
	 */
153
	function get_csv_data_for( $event_id, $offset, $limit, $questions_for_these_regs_rows ) {
154
		\EE_Registry::instance()->load_helper( 'Export' );
155
		$reg_fields_to_include = array(
156
			'TXN_ID',
157
			'ATT_ID',
158
			'REG_ID',
159
			'REG_date',
160
			'REG_code',
161
			'REG_count',
162
			'REG_final_price',
163
		);
164
		$att_fields_to_include = array(
165
			'ATT_fname',
166
			'ATT_lname',
167
			'ATT_email',
168
			'ATT_address',
169
			'ATT_address2',
170
			'ATT_city',
171
			'STA_ID',
172
			'CNT_ISO',
173
			'ATT_zip',
174
			'ATT_phone',
175
		);
176
177
		$registrations_csv_ready_array = array();
178
		$reg_model = \EE_Registry::instance()->load_model('Registration');
179
		$query_params = apply_filters(
180
			'FHEE__EE_Export__report_registration_for_event',
181
			array(
182
				array(
183
					'OR' => array(
184
						//don't include registrations from failed or abandoned transactions...
185
						'Transaction.STS_ID' => array( 'NOT IN', array( \EEM_Transaction::failed_status_code, \EEM_Transaction::abandoned_status_code ) ),
186
						//unless the registration is approved, in which case include it regardless of transaction status
187
						'STS_ID' => \EEM_Registration::status_id_approved
188
						),
189
					'Ticket.TKT_deleted' => array( 'IN', array( true, false ) )
190
					),
191
				'order_by' => array('Transaction.TXN_ID'=>'asc','REG_count'=>'asc'),
192
				'force_join' => array( 'Transaction', 'Ticket', 'Attendee' ),
193
				'limit' => array( $offset, $limit ),
194
			),
195
			$event_id
196
		);
197 View Code Duplication
		if( $event_id ){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
Bug Best Practice introduced by
The expression $event_id of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
198
			$query_params[0]['EVT_ID'] =  $event_id;
199
		}else{
200
			$query_params[ 'force_join' ][] = 'Event';
201
		}
202
		$registration_rows = $reg_model->get_all_wpdb_results( $query_params );
0 ignored issues
show
Bug introduced by
The method get_all_wpdb_results cannot be called on $reg_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...
203
		//get all questions which relate to someone in this group
204
		$registration_ids = array();
205
		foreach( $registration_rows as $reg_row ) {
206
			$registration_ids[] = intval( $reg_row[ 'Registration.REG_ID'] );
207
		}
208
209
		foreach($registration_rows as $reg_row){
210
			if ( is_array( $reg_row ) ) {
211
				$reg_csv_array = array();
212 View Code Duplication
				if( ! $event_id ){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
Bug Best Practice introduced by
The expression $event_id of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
213
					//get the event's name and Id
214
					$reg_csv_array[ __( 'Event', 'event_espresso' ) ] = sprintf( __( '%1$s (%2$s)', 'event_espresso' ), \EEH_Export::prepare_value_from_db_for_display( \EEM_Event::instance(), 'EVT_name', $reg_row[ 'Event_CPT.post_title'] ), $reg_row[ 'Event_CPT.ID' ] );
215
				}
216
				$is_primary_reg = $reg_row[ 'Registration.REG_count' ] == '1' ? true : false;
217
				/*@var $reg_row EE_Registration */
218
				foreach($reg_fields_to_include as $field_name){
219
					$field = $reg_model->field_settings_for($field_name);
0 ignored issues
show
Bug introduced by
The method field_settings_for cannot be called on $reg_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...
220
					if($field_name == 'REG_final_price'){
221
						$value = \EEH_Export::prepare_value_from_db_for_display( $reg_model, $field_name, $reg_row[ 'Registration.REG_final_price'], 'localized_float' );
0 ignored issues
show
Documentation introduced by
$reg_model is of type boolean, but the function expects a object<EEM_Base>.

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...
222
					}elseif( $field_name == 'REG_count' ){
223
						$value = sprintf( __( '%s of %s', 'event_espresso' ), \EEH_Export::prepare_value_from_db_for_display( $reg_model, 'REG_count', $reg_row['Registration.REG_count'] ), \EEH_Export::prepare_value_from_db_for_display( $reg_model, 'REG_group_size', $reg_row['Registration.REG_group_size' ] ) );
0 ignored issues
show
Documentation introduced by
$reg_model is of type boolean, but the function expects a object<EEM_Base>.

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...
224
					}elseif( $field_name == 'REG_date' ) {
225
						$value = \EEH_Export::prepare_value_from_db_for_display( $reg_model, $field_name, $reg_row[ 'Registration.REG_date'], 'no_html' );
0 ignored issues
show
Documentation introduced by
$reg_model is of type boolean, but the function expects a object<EEM_Base>.

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...
226
					}else{
227
						$value = \EEH_Export::prepare_value_from_db_for_display( $reg_model, $field_name, $reg_row[ $field->get_qualified_column() ] );
0 ignored issues
show
Documentation introduced by
$reg_model is of type boolean, but the function expects a object<EEM_Base>.

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...
228
					}
229
					$reg_csv_array[\EEH_Export::get_column_name_for_field($field)] = $value;
230
					if($field_name == 'REG_final_price'){
231
						//add a column named Currency after the final price
232
						$reg_csv_array[__("Currency", "event_espresso")] = \EE_Config::instance()->currency->code;
233
					}
234
				}
235
				//get pretty status
236
				$stati = \EEM_Status::instance()->localized_status( array(
237
					$reg_row[ 'Registration.STS_ID' ] => __( 'unknown', 'event_espresso' ),
238
					$reg_row[ 'Transaction.STS_ID' ] => __( 'unknown', 'event_espresso' ) ),
239
						FALSE,
240
						'sentence' );
241
				$reg_csv_array[__("Registration Status", 'event_espresso')] = $stati[ $reg_row[ 'Registration.STS_ID' ] ];
242
				//get pretty transaction status
243
				$reg_csv_array[__("Transaction Status", 'event_espresso')] = $stati[ $reg_row[ 'Transaction.STS_ID' ] ];
244
				$reg_csv_array[ __( 'Transaction Amount Due', 'event_espresso' ) ] = $is_primary_reg ? \EEH_Export::prepare_value_from_db_for_display( \EEM_Transaction::instance(), 'TXN_total', $reg_row[ 'Transaction.TXN_total' ], 'localized_float' ) : '0.00';
245
				$reg_csv_array[ __( 'Amount Paid', 'event_espresso' )] = $is_primary_reg ? \EEH_Export::prepare_value_from_db_for_display( \EEM_Transaction::instance(), 'TXN_paid', $reg_row[ 'Transaction.TXN_paid' ], 'localized_float' ) : '0.00';
246
				$payment_methods = array();
247
				$gateway_txn_ids_etc = array();
248
				$payment_times = array();
249 View Code Duplication
				if( $is_primary_reg && $reg_row[ 'Transaction.TXN_ID' ] ){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
250
					$payments_info = \EEM_Payment::instance()->get_all_wpdb_results(
251
							array(
252
								array(
253
									'TXN_ID' => $reg_row[ 'Transaction.TXN_ID' ],
254
									'STS_ID' => \EEM_Payment::status_id_approved
255
								),
256
								'force_join' => array( 'Payment_Method' ),
257
258
							),
259
							ARRAY_A,
260
							'Payment_Method.PMD_admin_name as name, Payment.PAY_txn_id_chq_nmbr as gateway_txn_id, Payment.PAY_timestamp as payment_time' );
261
262
					foreach( $payments_info as $payment_method_and_gateway_txn_id ){
263
						$payment_methods[] = isset( $payment_method_and_gateway_txn_id[ 'name' ] ) ? $payment_method_and_gateway_txn_id[ 'name' ] : __( 'Unknown', 'event_espresso' );
264
						$gateway_txn_ids_etc[] = isset( $payment_method_and_gateway_txn_id[ 'gateway_txn_id' ] ) ? $payment_method_and_gateway_txn_id[ 'gateway_txn_id' ] : '';
265
						$payment_times[] = isset( $payment_method_and_gateway_txn_id[ 'payment_time' ] ) ? $payment_method_and_gateway_txn_id[ 'payment_time' ] : '';
266
					}
267
268
				}
269
				$reg_csv_array[ __( 'Payment Date(s)', 'event_espresso' ) ] = implode( ',', $payment_times );
270
				$reg_csv_array[ __( 'Payment Method(s)', 'event_espresso' ) ] = implode( ",", $payment_methods );
271
				$reg_csv_array[ __( 'Gateway Transaction ID(s)', 'event_espresso' )] = implode( ',', $gateway_txn_ids_etc );
272
273
				//get whether or not the user has checked in
274
				$reg_csv_array[__("Check-Ins", "event_espresso")] = $reg_model->count_related( $reg_row[ 'Registration.REG_ID'] , 'Checkin' );
0 ignored issues
show
Bug introduced by
The method count_related cannot be called on $reg_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...
275
				//get ticket of registration and its price
276
				$ticket_model = \EE_Registry::instance()->load_model('Ticket');
277
				if( $reg_row[ 'Ticket.TKT_ID'] ) {
278
					$ticket_name = \EEH_Export::prepare_value_from_db_for_display( $ticket_model, 'TKT_name', $reg_row[ 'Ticket.TKT_name' ] );
0 ignored issues
show
Documentation introduced by
$ticket_model is of type boolean, but the function expects a object<EEM_Base>.

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...
279
					$datetimes_strings = array();
280 View Code Duplication
					foreach( \EEM_Datetime::instance()->get_all_wpdb_results( array( array( 'Ticket.TKT_ID' => $reg_row[ 'Ticket.TKT_ID' ] ), 'order_by' => array( 'DTT_EVT_start' => 'ASC' ), 'default_where_conditions' => 'none' ) ) as $datetime){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
281
						$datetimes_strings[] = \EEH_Export::prepare_value_from_db_for_display( \EEM_Datetime::instance(), 'DTT_EVT_start', $datetime[ 'Datetime.DTT_EVT_start'] );
282
					}
283
284
				} else {
285
					$ticket_name = __( 'Unknown', 'event_espresso' );
286
					$datetimes_strings = array( __( 'Unknown', 'event_espresso' ) );
287
				}
288
				$reg_csv_array[$ticket_model->field_settings_for('TKT_name')->get_nicename()] = $ticket_name;
0 ignored issues
show
Bug introduced by
The method field_settings_for cannot be called on $ticket_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...
289
				$reg_csv_array[__("Datetimes of Ticket", "event_espresso")] = implode(", ", $datetimes_strings);
290
				//get datetime(s) of registration
291
292
				//add attendee columns
293
				foreach($att_fields_to_include as $att_field_name){
294
					$field_obj = \EEM_Attendee::instance()->field_settings_for($att_field_name);
295 View Code Duplication
					if( $reg_row[ 'Attendee_CPT.ID' ]){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
296
						if($att_field_name == 'STA_ID'){
297
							$value = \EEM_State::instance()->get_var( array( array( 'STA_ID' => $reg_row[ 'Attendee_Meta.STA_ID' ] ) ), 'STA_name' );
298
						}elseif($att_field_name == 'CNT_ISO'){
299
							$value = \EEM_Country::instance()->get_var( array( array( 'CNT_ISO' => $reg_row[ 'Attendee_Meta.CNT_ISO' ] ) ), 'CNT_name' );
300
						}else{
301
							$value = \EEH_Export::prepare_value_from_db_for_display( \EEM_Attendee::instance(), $att_field_name, $reg_row[ $field_obj->get_qualified_column() ] );
302
						}
303
					}else{
304
						$value = '';
305
					}
306
307
					$reg_csv_array[ \EEH_Export::get_column_name_for_field($field_obj) ] = $value;
308
				}
309
310
				//make sure each registration has the same questions in the same order
311 View Code Duplication
				foreach($questions_for_these_regs_rows as $question_row){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
312
					if( ! isset($reg_csv_array[$question_row[ 'Question.QST_admin_label']])){
313
						$reg_csv_array[$question_row[ 'Question.QST_admin_label' ] ] = null;
314
					}
315
				}
316
				$answers = \EEM_Answer::instance()->get_all_wpdb_results(
317
					array(
318
						array( 'REG_ID' => $reg_row[ 'Registration.REG_ID' ] ),
319
						'force_join' => array( 'Question' )
320
					)
321
				);
322
				//now fill out the questions THEY answered
323
				foreach( $answers as $answer_row ){
324 View Code Duplication
					if( $answer_row[ 'Question.QST_ID' ] ){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
325
						$question_label = \EEH_Export::prepare_value_from_db_for_display(
326
							\EEM_Question::instance(),
327
							'QST_admin_label',
328
							$answer_row[ 'Question.QST_admin_label' ]
329
						);
330
					} else {
331
						$question_label = sprintf( __( 'Question $s', 'event_espresso' ), $answer_row[ 'Answer.QST_ID' ] );
332
					}
333 View Code Duplication
					if ( isset( $answer_row[ 'Question.QST_type' ] )
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
334
						 && $answer_row[ 'Question.QST_type' ] == \EEM_Question::QST_type_state
335
					) {
336
						$reg_csv_array[ $question_label ] = \EEM_State::instance()->get_state_name_by_ID(
337
							$answer_row[ 'Answer.ANS_value' ]
338
						);
339
					} else {
340
						$reg_csv_array[ $question_label ] = \EEH_Export::prepare_value_from_db_for_display(
341
							\EEM_Answer::instance(),
342
							'ANS_value',
343
							$answer_row[ 'Answer.ANS_value' ]
344
						);
345
					}
346
				}
347
				$registrations_csv_ready_array[] = apply_filters(
348
					'FHEE__EE_Export__report_registrations__reg_csv_array',
349
					$reg_csv_array, $reg_row
350
				);
351
			}
352
		}
353
		//if we couldn't export anything, we want to at least show the column headers
354 View Code Duplication
		if ( empty( $registrations_csv_ready_array ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
355
			$reg_csv_array = array();
356
			$model_and_fields_to_include = array(
357
				'Registration' => $reg_fields_to_include,
358
				'Attendee'     => $att_fields_to_include
359
			);
360
			foreach ( $model_and_fields_to_include as $model_name => $field_list ) {
361
				$model = \EE_Registry::instance()->load_model( $model_name );
362
				foreach ( $field_list as $field_name ) {
363
					$field = $model->field_settings_for( $field_name );
0 ignored issues
show
Bug introduced by
The method field_settings_for cannot be called on $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...
364
					$reg_csv_array[ \EEH_Export::get_column_name_for_field( $field ) ] = null;
365
				}
366
			}
367
			$registrations_csv_ready_array[] = $reg_csv_array;
368
		}
369
		return $registrations_csv_ready_array;
370
	}
371
372
373
374
	/**
375
	 * Counts total unit to process
376
	 *
377
	 * @param int $event_id
378
	 * @return int
379
	 */
380
	public function count_units_to_process( $event_id ) {
381
		//use the legacy filter
382
		$query_params = apply_filters(
383
			'FHEE__EE_Export__report_registration_for_event',
384
			array(
385
				array(
386
					'OR' => array(
387
						//don't include registrations from failed or abandoned transactions...
388
						'Transaction.STS_ID' => array( 'NOT IN', array( \EEM_Transaction::failed_status_code, \EEM_Transaction::abandoned_status_code ) ),
389
						//unless the registration is approved, in which case include it regardless of transaction status
390
						'STS_ID' => \EEM_Registration::status_id_approved
391
						),
392
					'Ticket.TKT_deleted' => array( 'IN', array( true, false ) )
393
					),
394
				'order_by' => array('Transaction.TXN_ID'=>'asc','REG_count'=>'asc'),
395
				'force_join' => array( 'Transaction', 'Ticket', 'Attendee' )
396
			),
397
			$event_id
398
		);
399 View Code Duplication
		if( $event_id ){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
400
			$query_params[0]['EVT_ID'] =  $event_id;
401
		} else {
402
			$query_params[ 'force_join' ][] = 'Event';
403
		}
404
		return \EEM_Registration::instance()->count( $query_params );
405
	}
406
407
408
409
	/**
410
	 * Performs any clean-up logic when we know the job is completed.
411
	 * In this case, we delete the temporary file
412
	 * @param JobParameters $job_parameters
413
	 * @return boolean
414
	 */
415 View Code Duplication
	public function cleanup_job( JobParameters $job_parameters ){
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...
416
		$this->_file_helper->delete(
417
			\EEH_File::remove_filename_from_filepath( $job_parameters->extra_datum( 'filepath' ) ),
0 ignored issues
show
Bug introduced by
It seems like $job_parameters->extra_datum('filepath') targeting EventEspressoBatchReques...rameters::extra_datum() can also be of type array; however, EEH_File::remove_filename_from_filepath() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
418
			true,
419
			'd'
420
		);
421
		return new JobStepResponse( $job_parameters, __( 'Cleaned up temporary file', 'event_espresso' ) );
422
	}
423
}
424
425
426