Passed
Push — master ( b0b4c3...57268f )
by Yannick
09:19 queued 13s
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 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
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
    protected function build_add_form()
41
    {
42
        $this->setDefaults(
43
            [
44
                'hid_user_id' => $this->evaluation_object->get_user_id(),
45
                'hid_category_id' => $this->evaluation_object->get_category_id(),
46
                'hid_course_code' => $this->evaluation_object->get_course_code(),
47
                'created_at' => api_get_utc_datetime(),
48
            ]
49
        );
50
        $this->build_basic_form();
51
52
        $this->addButtonCreate(get_lang('AddAssessment'), 'submit');
53
    }
54
55
    /**
56
     * Builds a basic form that is used in add and edit.
57
     *
58
     * @param int $edit
59
     *
60
     * @throws Exception
61
     */
62
    private function build_basic_form($edit = 0)
63
    {
64
        $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

64
        $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...
65
        $this->addElement('hidden', 'hid_user_id');
66
        $this->addElement('hidden', 'hid_course_code');
67
68
        $this->addText(
69
            'name',
70
            get_lang('EvaluationName'),
71
            true,
72
            [
73
                'maxlength' => '50',
74
                'id' => 'evaluation_title',
75
            ]
76
        );
77
78
        $cat_id = $this->evaluation_object->get_category_id();
79
80
        $session_id = api_get_session_id();
81
        $course_code = api_get_course_id();
82
        $all_categories = Category:: load(null, null, $course_code, null, null, $session_id, false);
83
84
        if (count($all_categories) == 1) {
85
            $this->addElement('hidden', 'hid_category_id', $cat_id);
86
        } else {
87
            $select_gradebook = $this->addElement(
88
                'select',
89
                'hid_category_id',
90
                get_lang('SelectGradebook'),
91
                [],
92
                ['id' => 'hid_category_id']
93
            );
94
            $this->addRule('hid_category_id', get_lang('ThisFieldIsRequired'), 'nonzero');
95
            $default_weight = 0;
96
            if (!empty($all_categories)) {
97
                foreach ($all_categories as $my_cat) {
98
                    if ($my_cat->get_course_code() == api_get_course_id()) {
99
                        $grade_model_id = $my_cat->get_grade_model_id();
100
                        if (empty($grade_model_id)) {
101
                            if ($my_cat->get_parent_id() == 0) {
102
                                $default_weight = $my_cat->get_weight();
103
                                $select_gradebook->addOption(get_lang('Default'), $my_cat->get_id());
104
                                $cats_added[] = $my_cat->get_id();
105
                            } else {
106
                                $select_gradebook->addOption($my_cat->get_name(), $my_cat->get_id());
107
                                $cats_added[] = $my_cat->get_id();
108
                            }
109
                        } else {
110
                            $select_gradebook->addOption(get_lang('Select'), 0);
111
                        }
112
                        if ($this->evaluation_object->get_category_id() == $my_cat->get_id()) {
113
                            $default_weight = $my_cat->get_weight();
114
                        }
115
                    }
116
                }
117
            }
118
        }
119
120
        $this->addFloat(
121
            'weight_mask',
122
            [
123
                get_lang('Weight'),
124
                null,
125
                ' [0 .. <span id="max_weight">'.$all_categories[0]->get_weight().'</span>] ',
126
            ],
127
            true,
128
            [
129
                'size' => '4',
130
                'maxlength' => '5',
131
            ]
132
        );
133
134
        if ($edit) {
135
            if (!$this->evaluation_object->has_results()) {
136
                $this->addText(
137
                    'max',
138
                    get_lang('QualificationNumeric'),
139
                    true,
140
                    [
141
                        'maxlength' => '5',
142
                    ]
143
                );
144
            } else {
145
                $this->addText(
146
                    'max',
147
                    [get_lang('QualificationNumeric'), get_lang('CannotChangeTheMaxNote')],
148
                    false,
149
                    [
150
                        'maxlength' => '5',
151
                        'disabled' => 'disabled',
152
                    ]
153
                );
154
            }
155
        } else {
156
            $this->addText(
157
                'max',
158
                get_lang('QualificationNumeric'),
159
                true,
160
                [
161
                    'maxlength' => '5',
162
                ]
163
            );
164
            $default_max = api_get_setting('gradebook_default_weight');
165
            $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...
166
            $this->setDefaults($defaults);
167
        }
168
169
        $this->addElement('textarea', 'description', get_lang('Description'));
170
        $this->addRule('hid_category_id', get_lang('ThisFieldIsRequired'), 'required');
171
        $this->addElement('checkbox', 'visible', null, get_lang('Visible'));
172
        $this->addRule('max', get_lang('OnlyNumbers'), 'numeric');
173
        $this->addRule(
174
            'max',
175
            get_lang('NegativeValue'),
176
            'compare',
177
            '>=',
178
            'server',
179
            false,
180
            false,
181
            0
182
        );
183
        $setting = api_get_setting('tool_visible_by_default_at_creation');
184
        $visibility_default = 1;
185
        if (isset($setting['gradebook']) && $setting['gradebook'] == 'false') {
186
            $visibility_default = 0;
187
        }
188
        $this->setDefaults(['visible' => $visibility_default]);
189
    }
190
}
191