Code Duplication    Length = 49-52 lines in 2 locations

core/db_models/EEM_Registration.model.php 2 locations

@@ 409-460 (lines=52) @@
406
	 * @return stdClass[] with properties Registration_REG_date and a column for each registration status as the STS_ID
407
	 *                    (i.e. RAP)
408
	 */
409
	public function get_registrations_per_day_and_per_status_report( $period = '-1 month' ) {
410
		global $wpdb;
411
		$registration_table = $wpdb->prefix . 'esp_registration';
412
		$event_table = $wpdb->posts;
413
		$sql_date = date("Y-m-d H:i:s", strtotime($period) );
414
415
		//prepare the query interval for displaying offset
416
		$query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset( $this->get_timezone(), 'dates.REG_date' );
417
418
		//inner date query
419
		$inner_date_query = "SELECT DISTINCT REG_date from $registration_table ";
420
		$inner_where = " WHERE";
421
		//exclude events not authored by user if permissions in effect
422
		if ( ! EE_Registry::instance()->CAP->current_user_can( 'ee_read_others_registrations', 'reg_per_event_report' ) ) {
423
			$inner_date_query .= "LEFT JOIN $event_table ON ID = EVT_ID";
424
			$inner_where .= " post_author = " . get_current_user_id() . " AND";
425
		}
426
		$inner_where .= " REG_date >= '$sql_date'";
427
		$inner_date_query .= $inner_where;
428
429
		//start main query
430
		$select = "SELECT DATE($query_interval) as Registration_REG_date, ";
431
		$join = '';
432
		$join_parts = array();
433
		$select_parts = array();
434
435
		//loop through registration stati to do parts for each status.
436
		foreach ( EEM_Registration::reg_status_array() as $STS_ID => $STS_code ) {
437
			if ( $STS_ID === EEM_Registration::status_id_incomplete ) {
438
				continue;
439
			}
440
			$select_parts[] = "COUNT($STS_code.REG_ID) as $STS_ID";
441
			$join_parts[] = "$registration_table AS $STS_code ON $STS_code.REG_date = dates.REG_date AND $STS_code.STS_ID = '$STS_ID'";
442
		}
443
444
		//setup the selects
445
		$select .= implode(', ', $select_parts );
446
		$select .= " FROM ($inner_date_query) AS dates LEFT JOIN ";
447
448
		//setup the joins
449
		$join .= implode( " LEFT JOIN ", $join_parts );
450
451
		//now let's put it all together
452
		$query = $select . $join . ' GROUP BY Registration_REG_date';
453
454
		//and execute it
455
		$results = $wpdb->get_results(
456
			$query,
457
			ARRAY_A
458
		);
459
		return $results;
460
	}
461
462
463
@@ 507-555 (lines=49) @@
504
	 * @return stdClass[] with properties `Registration_Event` and a column for each registration status as the STS_ID
505
	 *                    (i.e. RAP)
506
	 */
507
	public function get_registrations_per_event_and_per_status_report( $period = '-1 month' ) {
508
		global $wpdb;
509
		$registration_table = $wpdb->prefix . 'esp_registration';
510
		$event_table = $wpdb->posts;
511
		$sql_date = date("Y-m-d H:i:s", strtotime($period) );
512
513
		//inner date query
514
		$inner_date_query = "SELECT DISTINCT EVT_ID, REG_date from $registration_table ";
515
		$inner_where = " WHERE";
516
		//exclude events not authored by user if permissions in effect
517
		if ( ! EE_Registry::instance()->CAP->current_user_can( 'ee_read_others_registrations', 'reg_per_event_report' ) ) {
518
			$inner_date_query .= "LEFT JOIN $event_table ON ID = EVT_ID";
519
			$inner_where .= " post_author = " . get_current_user_id() . " AND";
520
		}
521
		$inner_where .= " REG_date >= '$sql_date'";
522
		$inner_date_query .= $inner_where;
523
524
		//build main query
525
		$select = "SELECT Event.post_title as Registration_Event, ";
526
		$join = '';
527
		$join_parts = array();
528
		$select_parts = array();
529
530
		//loop through registration stati to do parts for each status.
531
		foreach ( EEM_Registration::reg_status_array() as $STS_ID => $STS_code ) {
532
			if ( $STS_ID === EEM_Registration::status_id_incomplete ) {
533
				continue;
534
			}
535
			$select_parts[] = "COUNT($STS_code.REG_ID) as $STS_ID";
536
			$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";
537
		}
538
539
		//setup the selects
540
		$select .= implode( ', ', $select_parts );
541
		$select .= " FROM ($inner_date_query) AS dates LEFT JOIN $event_table as Event ON Event.ID = dates.EVT_ID LEFT JOIN ";
542
543
		//setup remaining joins
544
		$join .= implode( " LEFT JOIN ", $join_parts );
545
546
		//now put it all together
547
		$query = $select . $join . ' GROUP BY Registration_Event';
548
549
		//and execute
550
		$results = $wpdb->get_results(
551
			$query,
552
			ARRAY_A
553
		);
554
		return $results;
555
	}
556
557
558
	/**