Completed
Branch dev (3b3c60)
by
unknown
44:42 queued 33:59
created

EEM_Question_Group::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 94

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 94
rs 8.1309
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Question Group Model
5
 *
6
 * @package         Event Espresso
7
 * @subpackage      includes/models/
8
 * @author          Michael Nelson
9
 */
10
class EEM_Question_Group extends EEM_Soft_Delete_Base
11
{
12
13
    const system_personal = 1;
14
15
    const system_address = 2;
16
17
    /**
18
     * private instance of the EEM_Question_Group object
19
     *
20
     * @var EEM_Question_Group
21
     */
22
    protected static $_instance = null;
23
24
25
    /**
26
     * EEM_Question_Group constructor.
27
     *
28
     * @param string|null $timezone
29
     */
30
    protected function __construct($timezone = null)
31
    {
32
        $this->singular_item = esc_html__('Question Group', 'event_espresso');
33
        $this->plural_item   = esc_html__('Question Groups', 'event_espresso');
34
35
        $this->_tables          = [
36
            'Question_Group' => new EE_Primary_Table('esp_question_group', 'QSG_ID'),
37
        ];
38
        $this->_fields          = [
39
            'Question_Group' => [
40
                'QSG_ID'              => new EE_Primary_Key_Int_Field(
41
                    'QSG_ID',
42
                    esc_html__('Question Group ID', 'event_espresso')
43
                ),
44
                'QSG_name'            => new EE_Plain_Text_Field(
45
                    'QSG_name',
46
                    esc_html__('Question Group Name', 'event_espresso'),
47
                    false,
48
                    ''
49
                ),
50
                'QSG_identifier'      => new EE_Plain_Text_Field(
51
                    'QSG_identifier',
52
                    esc_html__('Text ID for question Group', 'event_espresso'),
53
                    false,
54
                    ''
55
                ),
56
                'QSG_desc'            => new EE_Post_Content_Field(
57
                    'QSG_desc',
58
                    esc_html__('Description of Question Group', 'event_espresso'),
59
                    true,
60
                    ''
61
                ),
62
                'QSG_order'           => new EE_Integer_Field(
63
                    'QSG_order',
64
                    esc_html__('Order in which to show the question group', 'event_espresso'),
65
                    true,
66
                    0
67
                ),
68
                'QSG_show_group_name' => new EE_Boolean_Field(
69
                    'QSG_show_group_name',
70
                    esc_html__(
71
                        'Flag indicating whether to show the group\'s name on the registration page',
72
                        'event_espresso'
73
                    ),
74
                    false,
75
                    true
76
                ),
77
                'QSG_show_group_desc' => new EE_Boolean_Field(
78
                    'QSG_show_group_desc',
79
                    esc_html__(
80
                        'Flag indicating whether to show the group\s description on the registration page',
81
                        'event_espresso'
82
                    ), false,
83
                    false
84
                ),
85
                'QSG_wp_user'         => new EE_WP_User_Field(
86
                    'QSG_wp_user',
87
                    esc_html__('Question Group Creator ID', 'event_espresso'),
88
                    false
89
                ),
90
                'QSG_system'          => new EE_Integer_Field(
91
                    'QSG_system',
92
                    esc_html__(
93
                        'Indicate IF this is a system group and if it is what system group it corresponds to.',
94
                        'event_espresso'
95
                    ), false,
96
                    0
97
                ),
98
                'QSG_deleted'         => new EE_Trashed_Flag_Field(
99
                    'QSG_deleted',
100
                    esc_html__('Flag indicating this question group was deleted', 'event_espresso'),
101
                    false,
102
                    false
103
                ),
104
            ],
105
        ];
106
        $this->_model_relations = [
107
            'Question'             => new EE_HABTM_Relation('Question_Group_Question'),
108
            'Event'                => new EE_HABTM_Relation('Event_Question_Group'),
109
            'Event_Question_Group' => new EE_Has_Many_Relation(),
110
            'WP_User'              => new EE_Belongs_To_Relation(),
111
        ];
112
        // this model is generally available for reading
113
        $this->_cap_restriction_generators[ EEM_Base::caps_read ]       =
114
            new EE_Restriction_Generator_Public();
115
        $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] =
116
            new EE_Restriction_Generator_Reg_Form('QSG_system');
117
        $this->_cap_restriction_generators[ EEM_Base::caps_edit ]       =
118
            new EE_Restriction_Generator_Reg_Form('QSG_system');
119
        $this->_cap_restriction_generators[ EEM_Base::caps_delete ]     =
120
            new EE_Restriction_Generator_Reg_Form('QSG_system');
121
122
        parent::__construct($timezone);
123
    }
124
125
126
    /**
127
     * searches the db for the question group with the latest question order and returns that value.
128
     *
129
     * @return int
130
     * @throws EE_Error
131
     */
132
    public function get_latest_question_group_order()
133
    {
134
        $max = $this->_get_all_wpdb_results(
135
            [],
136
            ARRAY_A,
137
            [
138
                'max_order' => ["MAX(QSG_order)", "%d"],
139
            ]
140
        );
141
        return $max[0]['max_order'];
142
    }
143
}
144