Completed
Push — master ( 1abfb3...169323 )
by Daniel Neis
02:07
created

featuredcourses_form   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 59
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A definition() 0 53 3
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 32 and the first side effect is on line 25.

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
 * The list of featured courses form.
19
 *
20
 * @package    block_featuredcourses
21
 * @copyright  Daniel Neis <[email protected]>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
25
defined('MOODLE_INTERNAL') || die;
26
27
require_once($CFG->libdir.'/formslib.php');
28
29
/**
30
 * The form for handling featured courses.
31
 */
32
class featuredcourses_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...
33
34
    /**
35
     * Form definition.
36
     */
37
    public function definition() {
38
        global $CFG;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
39
40
        $mform = $this->_form;
41
        $availablecourses  = $this->_customdata['availablecourses'];
42
        $featuredcourses  = $this->_customdata['featuredcourses'];
43
        $availablecourseslist = array();
44
        foreach ($availablecourses as $c) {
45
            $availablecourseslist[$c->id] = $c->shortname . ' : ' . $c->fullname;
46
        }
47
48
        // Forms to edit existing featured courses.
49
        foreach ($featuredcourses as $c) {
50
            $mform->addElement('header', 'featured',
51
                               get_string('featuredcourse', 'block_featuredcourses', $c->shortname . ' - '. $c->fullname));
52
53
            $mform->addElement('hidden', 'featured['.$c->id.'][id]', null);
54
            $mform->setType('featured['.$c->id.'][id]', PARAM_INT);
55
            $mform->setConstant('featured['.$c->id.'][id]', $c->id);
56
57
            $mform->addElement('text', 'featured['.$c->id.'][sortorder]', get_string('sortorder', 'block_featuredcourses'));
58
            $mform->addRule('featured['.$c->id.'][sortorder]',
59
                            get_string('missingsortorder', 'block_featuredcourses'), 'required', null, 'client');
60
            $mform->setType('featured['.$c->id.'][sortorder]', PARAM_INT);
61
            $mform->setDefault('featured['.$c->id.'][sortorder]', $c->sortorder);
62
63
            $mform->addElement('static', 'link',
64
                               get_string('deletelink', 'block_featuredcourses',
65
                                          $CFG->wwwroot.'/blocks/featuredcourses/delete_featuredcourse.php?courseid='.$c->id));
66
67
        }
68
69
        // Add a new featured course.
70
        $mform->addElement('header', 'add', get_string('addfeaturedcourse', 'block_featuredcourses'));
71
72
        $mform->addElement('checkbox', 'doadd', get_string('doadd', 'block_featuredcourses'));
73
74
        $mform->addElement('select', 'newfeatured[courseid]',
75
                           get_string('courseid', 'block_featuredcourses'), $availablecourseslist);
76
        $mform->addRule('newfeatured[courseid]', get_string('missingcourseid', 'block_featuredcourses'),
77
                        'required', null, 'client');
78
        $mform->disabledIf('newfeatured[courseid]', 'doadd', 'notchecked');
79
80
        $mform->addElement('text', 'newfeatured[sortorder]', get_string('sortorder', 'block_featuredcourses'));
81
        $mform->addRule('newfeatured[sortorder]', get_string('missingsortorder', 'block_featuredcourses'),
82
                        'required', null, 'client');
83
        $mform->setType('newfeatured[sortorder]', PARAM_INT);
84
        $mform->disabledIf('newfeatured[sortorder]', 'doadd', 'notchecked');
85
86
        $mform->addElement('submit', 'save', get_string('savechanges'));
87
88
        $mform->closeHeaderBefore('save');
89
    }
90
}
91