enrol_pagseguro_edit_form::validation()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 3 Features 0
Metric Value
cc 5
eloc 8
c 4
b 3
f 0
nc 5
nop 2
dl 0
loc 16
rs 8.8571
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 27.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17
/**
18
 * Adds new instance of enrol_pagseguro to specified course
19
 * or edits current instance.
20
 *
21
 * @package    enrol
22
 * @subpackage pagseguro
23
 * @copyright  2010 Petr Skoda  {@link http://skodak.org}
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
27
defined('MOODLE_INTERNAL') || die();
28
29
require_once($CFG->libdir.'/formslib.php');
30
31
class enrol_pagseguro_edit_form extends moodleform {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
32
33
    public function definition() {
34
        $mform = $this->_form;
35
36
        list($instance, $plugin, $context) = $this->_customdata;
37
38
        $mform->addElement('header', 'header', get_string('pluginname', 'enrol_pagseguro'));
39
40
        $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'));
41
        $mform->setType('name', PARAM_TEXT);
42
43
        $options = array(ENROL_INSTANCE_ENABLED  => get_string('yes'),
44
                         ENROL_INSTANCE_DISABLED => get_string('no'));
45
        $mform->addElement('select', 'status', get_string('status', 'enrol_pagseguro'), $options);
46
        $mform->setDefault('status', $plugin->get_config('status'));
47
48
        $mform->addElement('text', 'cost', get_string('cost', 'enrol_pagseguro'), array('size' => 4));
49
        $mform->setType('cost', PARAM_RAW);
50
        $mform->setDefault('cost', $plugin->get_config('cost'));
51
52
        $mform->addElement('select', 'currency', get_string('currency', 'enrol_pagseguro'),
53
                           \get_string_manager()->get_list_of_currencies());
54
        $mform->setDefault('currency', $plugin->get_config('currency'));
55
56
        if ($instance->id) {
57
            $roles = get_default_enrol_roles($context, $instance->roleid);
58
        } else {
59
            $roles = get_default_enrol_roles($context, $plugin->get_config('roleid'));
60
        }
61
        $mform->addElement('select', 'roleid', get_string('assignrole', 'enrol_pagseguro'), $roles);
62
        $mform->setDefault('roleid', $plugin->get_config('roleid'));
63
64
        $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_pagseguro'), array('optional' => true, 'defaultunit' => 86400));
65
        $mform->setDefault('enrolperiod', $plugin->get_config('enrolperiod'));
66
        $mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_pagseguro');
67
68
        $mform->addElement('date_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_pagseguro'), array('optional' => true));
69
        $mform->setDefault('enrolstartdate', 0);
70
        $mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_pagseguro');
71
72
        $mform->addElement('date_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_pagseguro'), array('optional' => true));
73
        $mform->setDefault('enrolenddate', 0);
74
        $mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_pagseguro');
75
76
        $mform->addElement('hidden', 'id');
77
        $mform->setType('id', PARAM_INT);
78
        $mform->addElement('hidden', 'courseid');
79
        $mform->setType('courseid', PARAM_INT);
80
81
        $this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol')));
82
83
        $this->set_data($instance);
84
    }
85
86
    public function validation($data, $files) {
87
        $errors = parent::validation($data, $files);
88
89
        if ($data['status'] == ENROL_INSTANCE_ENABLED) {
90
            if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
91
                $errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_pagseguro');
92
            }
93
94
            if (!is_numeric($data['cost'])) {
95
                $errors['cost'] = get_string('costerror', 'enrol_pagseguro');
96
97
            }
98
        }
99
100
        return $errors;
101
    }
102
}
103