Code Duplication    Length = 49-52 lines in 2 locations

core/db_models/EEM_Registration.model.php 2 locations

@@ 366-417 (lines=52) @@
363
	 * @return stdClass[] with properties Registration_REG_date and a column for each registration status as the STS_ID
364
	 *                    (i.e. RAP)
365
	 */
366
	public function get_registrations_per_day_and_per_status_report( $period = '-1 month' ) {
367
		global $wpdb;
368
		$registration_table = $wpdb->prefix . 'esp_registration';
369
		$event_table = $wpdb->posts;
370
		$sql_date = date("Y-m-d H:i:s", strtotime($period) );
371
372
		//prepare the query interval for displaying offset
373
		$query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset( $this->get_timezone(), 'dates.REG_date' );
374
375
		//inner date query
376
		$inner_date_query = "SELECT DISTINCT REG_date from $registration_table ";
377
		$inner_where = " WHERE";
378
		//exclude events not authored by user if permissions in effect
379
		if ( ! EE_Registry::instance()->CAP->current_user_can( 'ee_read_others_registrations', 'reg_per_event_report' ) ) {
380
			$inner_date_query .= "LEFT JOIN $event_table ON ID = EVT_ID";
381
			$inner_where .= " post_author = " . get_current_user_id() . " AND";
382
		}
383
		$inner_where .= " REG_date >= '$sql_date'";
384
		$inner_date_query .= $inner_where;
385
386
		//start main query
387
		$select = "SELECT DATE($query_interval) as Registration_REG_date, ";
388
		$join = '';
389
		$join_parts = array();
390
		$select_parts = array();
391
392
		//loop through registration stati to do parts for each status.
393
		foreach ( EEM_Registration::reg_status_array() as $STS_ID => $STS_code ) {
394
			if ( $STS_ID === EEM_Registration::status_id_incomplete ) {
395
				continue;
396
			}
397
			$select_parts[] = "COUNT($STS_code.REG_ID) as $STS_ID";
398
			$join_parts[] = "$registration_table AS $STS_code ON $STS_code.REG_date = dates.REG_date AND $STS_code.STS_ID = '$STS_ID'";
399
		}
400
401
		//setup the selects
402
		$select .= implode(', ', $select_parts );
403
		$select .= " FROM ($inner_date_query) AS dates LEFT JOIN ";
404
405
		//setup the joins
406
		$join .= implode( " LEFT JOIN ", $join_parts );
407
408
		//now let's put it all together
409
		$query = $select . $join . ' GROUP BY Registration_REG_date';
410
411
		//and execute it
412
		$results = $wpdb->get_results(
413
			$query,
414
			ARRAY_A
415
		);
416
		return $results;
417
	}
418
419
420
@@ 464-512 (lines=49) @@
461
	 * @return stdClass[] with properties `Registration_Event` and a column for each registration status as the STS_ID
462
	 *                    (i.e. RAP)
463
	 */
464
	public function get_registrations_per_event_and_per_status_report( $period = '-1 month' ) {
465
		global $wpdb;
466
		$registration_table = $wpdb->prefix . 'esp_registration';
467
		$event_table = $wpdb->posts;
468
		$sql_date = date("Y-m-d H:i:s", strtotime($period) );
469
470
		//inner date query
471
		$inner_date_query = "SELECT DISTINCT EVT_ID, REG_date from $registration_table ";
472
		$inner_where = " WHERE";
473
		//exclude events not authored by user if permissions in effect
474
		if ( ! EE_Registry::instance()->CAP->current_user_can( 'ee_read_others_registrations', 'reg_per_event_report' ) ) {
475
			$inner_date_query .= "LEFT JOIN $event_table ON ID = EVT_ID";
476
			$inner_where .= " post_author = " . get_current_user_id() . " AND";
477
		}
478
		$inner_where .= " REG_date >= '$sql_date'";
479
		$inner_date_query .= $inner_where;
480
481
		//build main query
482
		$select = "SELECT Event.post_title as Registration_Event, ";
483
		$join = '';
484
		$join_parts = array();
485
		$select_parts = array();
486
487
		//loop through registration stati to do parts for each status.
488
		foreach ( EEM_Registration::reg_status_array() as $STS_ID => $STS_code ) {
489
			if ( $STS_ID === EEM_Registration::status_id_incomplete ) {
490
				continue;
491
			}
492
			$select_parts[] = "COUNT($STS_code.REG_ID) as $STS_ID";
493
			$join_parts[] = "$registration_table AS $STS_code ON $STS_code.EVT_ID = dates.EVT_ID AND $STS_code.STS_ID = '$STS_ID' AND $STS_code.REG_date = dates.REG_date";
494
		}
495
496
		//setup the selects
497
		$select .= implode( ', ', $select_parts );
498
		$select .= " FROM ($inner_date_query) AS dates LEFT JOIN $event_table as Event ON Event.ID = dates.EVT_ID LEFT JOIN ";
499
500
		//setup remaining joins
501
		$join .= implode( " LEFT JOIN ", $join_parts );
502
503
		//now put it all together
504
		$query = $select . $join . ' GROUP BY Registration_Event';
505
506
		//and execute
507
		$results = $wpdb->get_results(
508
			$query,
509
			ARRAY_A
510
		);
511
		return $results;
512
	}
513
514
515
	/**