Passed
Push — master ( 65060b...2b54a2 )
by Julito
10:15
created

OutcomeForm::build_add_form()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 13
rs 10
c 1
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
class OutcomeForm extends EvalForm
5
{
6
    /**
7
     * Builds a form containing form items based on a given parameter.
8
     *
9
     * @param int        $form_type         1=add, 2=edit,3=move,4=result_add
10
     * @param Evaluation $evaluation_object the category object
11
     * @param obj        $result_object     the result object
12
     * @param string     $form_name
13
     * @param string     $method
14
     * @param string     $action
15
     */
16
    public function __construct(
17
        $evaluation_object,
18
        $result_object,
19
        $form_name,
20
        $method = 'post',
21
        $action = null,
22
        $extra1 = null,
23
        $extra2 = null
24
    ) {
25
        parent::__construct(
26
            -1,
27
            $evaluation_object,
28
            $result_object,
29
            $form_name,
30
            $method,
31
            $action,
32
            $extra1,
33
            $extra2
34
        );
35
36
        $this->build_add_form();
37
        $this->setDefaults();
38
    }
39
40
    /**
41
     * Builds a basic form that is used in add and edit.
42
     *
43
     * @param int $edit
44
     *
45
     * @throws Exception
46
     */
47
    private function build_basic_form($edit = 0)
48
    {
49
        $this->addElement('header', get_plugin_lang('NewOutcomeFormTitle'));
0 ignored issues
show
Bug introduced by
The call to get_plugin_lang() has too few arguments starting with pluginName. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $this->addElement('header', /** @scrutinizer ignore-call */ get_plugin_lang('NewOutcomeFormTitle'));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
50
        $this->addElement('hidden', 'hid_user_id');
51
        $this->addElement('hidden', 'hid_course_code');
52
53
        $this->addText(
54
            'name',
55
            get_lang('EvaluationName'),
56
            true,
57
            [
58
                'maxlength' => '50',
59
                'id' => 'evaluation_title',
60
            ]
61
        );
62
63
        $cat_id = $this->evaluation_object->get_category_id();
64
65
        $session_id = api_get_session_id();
66
        $course_code = api_get_course_id();
67
        $all_categories = Category:: load(null, null, $course_code, null, null, $session_id, false);
68
69
        if (count($all_categories) == 1) {
70
            $this->addElement('hidden', 'hid_category_id', $cat_id);
71
        } else {
72
            $select_gradebook = $this->addElement(
73
                'select',
74
                'hid_category_id',
75
                get_lang('SelectGradebook'),
76
                [],
77
                ['id' => 'hid_category_id']
78
            );
79
            $this->addRule('hid_category_id', get_lang('ThisFieldIsRequired'), 'nonzero');
80
            $default_weight = 0;
81
            if (!empty($all_categories)) {
82
                foreach ($all_categories as $my_cat) {
83
                    if ($my_cat->get_course_code() == api_get_course_id()) {
84
                        $grade_model_id = $my_cat->get_grade_model_id();
85
                        if (empty($grade_model_id)) {
86
                            if ($my_cat->get_parent_id() == 0) {
87
                                $default_weight = $my_cat->get_weight();
88
                                $select_gradebook->addoption(get_lang('Default'), $my_cat->get_id());
89
                                $cats_added[] = $my_cat->get_id();
90
                            } else {
91
                                $select_gradebook->addoption($my_cat->get_name(), $my_cat->get_id());
92
                                $cats_added[] = $my_cat->get_id();
93
                            }
94
                        } else {
95
                            $select_gradebook->addoption(get_lang('Select'), 0);
96
                        }
97
                        if ($this->evaluation_object->get_category_id() == $my_cat->get_id()) {
98
                            $default_weight = $my_cat->get_weight();
99
                        }
100
                    }
101
                }
102
            }
103
        }
104
105
        $this->addFloat(
106
            'weight_mask',
107
            [
108
                get_lang('Weight'),
109
                null,
110
                ' [0 .. <span id="max_weight">'.$all_categories[0]->get_weight().'</span>] ',
111
            ],
112
            true,
113
            [
114
                'size' => '4',
115
                'maxlength' => '5',
116
            ]
117
        );
118
119
        if ($edit) {
120
            if (!$this->evaluation_object->has_results()) {
121
                $this->addText(
122
                    'max',
123
                    get_lang('QualificationNumeric'),
124
                    true,
125
                    [
126
                        'maxlength' => '5',
127
                    ]
128
                );
129
            } else {
130
                $this->addText(
131
                    'max',
132
                    [get_lang('QualificationNumeric'), get_lang('CannotChangeTheMaxNote')],
133
                    false,
134
                    [
135
                        'maxlength' => '5',
136
                        'disabled' => 'disabled',
137
                    ]
138
                );
139
            }
140
        } else {
141
            $this->addText(
142
                'max',
143
                get_lang('QualificationNumeric'),
144
                true,
145
                [
146
                    'maxlength' => '5',
147
                ]
148
            );
149
            $default_max = api_get_setting('gradebook_default_weight');
150
            $defaults['max'] = isset($default_max) ? $default_max : 100;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$defaults was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaults = array(); before regardless.
Loading history...
151
            $this->setDefaults($defaults);
152
        }
153
154
        $this->addElement('textarea', 'description', get_lang('Description'));
155
        $this->addRule('hid_category_id', get_lang('ThisFieldIsRequired'), 'required');
156
        $this->addElement('checkbox', 'visible', null, get_lang('Visible'));
157
        $this->addRule('max', get_lang('OnlyNumbers'), 'numeric');
158
        $this->addRule(
159
            'max',
160
            get_lang('NegativeValue'),
161
            'compare',
162
            '>=',
163
            'server',
164
            false,
165
            false,
166
            0
167
        );
168
        $setting = api_get_setting('tool_visible_by_default_at_creation');
169
        $visibility_default = 1;
170
        if (isset($setting['gradebook']) && $setting['gradebook'] == 'false') {
171
            $visibility_default = 0;
172
        }
173
        $this->setDefaults(['visible' => $visibility_default]);
174
    }
175
176
    /**
177
     *
178
     */
179
    protected function build_add_form()
180
    {
181
        $this->setDefaults(
182
            [
183
                'hid_user_id' => $this->evaluation_object->get_user_id(),
184
                'hid_category_id' => $this->evaluation_object->get_category_id(),
185
                'hid_course_code' => $this->evaluation_object->get_course_code(),
186
                'created_at' => api_get_utc_datetime(),
187
            ]
188
        );
189
        $this->build_basic_form();
190
191
        $this->addButtonCreate(get_lang('AddAssessment'), 'submit');
192
    }
193
}