block_featuredcourses::get_content()   C
last analyzed

Complexity

Conditions 10
Paths 5

Size

Total Lines 76
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 5.7198
c 0
b 0
f 0
cc 10
eloc 52
nc 5
nop 0

How to fix   Long Method    Complexity   

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
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 27 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
 * Featured coures block main class.
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
class block_featuredcourses extends block_base {
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...
28
29
    public function init() {
30
        $this->title = get_string('pluginname', 'block_featuredcourses');
31
    }
32
33
    public function get_content() {
34
        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...
35
36
        if ($this->content !== null) {
37
            return $this->content;
38
        }
39
40
        if (empty($this->instance)) {
41
            $this->content = '';
42
            return $this->content;
43
        }
44
45
        $this->content = new stdClass();
46
        $this->content->items = array();
47
        $this->content->icons = array();
48
        $this->content->footer = '';
49
        $this->content->text = '';
50
51
        // The user/index.php expect course context, so get one if page has module context.
52
        $currentcontext = $this->page->context->get_course_context(false);
53
54
        if (empty($currentcontext)) {
55
            return $this->content;
56
        }
57
        if ($this->page->course->id == SITEID) {
58
            $courses = self::get_featured_courses();
59
            require_once($CFG->libdir. '/coursecatlib.php');
60
            $chelper = new coursecat_helper();
61
            foreach ($courses as $course) {
62
63
                $course = new course_in_list($course);
64
65
                $this->content->text .= '<div class="container-fluid coursebox">';
66
67
                $content = '';
68
69
                $coursename = $chelper->get_course_formatted_name($course);
70
                $coursenamelink = html_writer::link(new moodle_url('/course/view.php', array('id' => $course->id)),
71
                                                    $coursename, array('class' => $course->visible ? '' : 'dimmed'));
72
                $content .= html_writer::tag('div', $coursenamelink, array('class' => 'coursename'));
73
74
                if ($course->has_summary()) {
75
                    $content .= html_writer::start_tag('div', array('class' => 'summary'));
76
                    $content .= $chelper->get_course_formatted_summary($course,
77
                            array('overflowdiv' => true, 'noclean' => true, 'para' => false));
78
                    $content .= html_writer::end_tag('div');
79
                }
80
81
                // Display course overview files.
82
                $contentimages = $contentfiles = '';
83
                foreach ($course->get_course_overviewfiles() as $file) {
84
                    $isimage = $file->is_valid_image();
85
                    $url = file_encode_url("{$CFG->wwwroot}/pluginfile.php",
86
                            '/'. $file->get_contextid(). '/'. $file->get_component(). '/'.
87
                            $file->get_filearea(). $file->get_filepath(). $file->get_filename(), !$isimage);
88
                    if ($isimage) {
89
                        $contentimages .= html_writer::tag('div',
90
                                html_writer::empty_tag('img', array('src' => $url, 'style' => 'max-height: 150px')),
91
                                array('class' => 'courseimage'));
92
                    } else {
93
                        $image = $this->output->pix_icon(file_file_icon($file, 24), $file->get_filename(), 'moodle');
94
                        $filename = html_writer::tag('span', $image, array('class' => 'fp-icon')).
95
                                html_writer::tag('span', $file->get_filename(), array('class' => 'fp-filename'));
96
                        $contentfiles .= html_writer::tag('span',
97
                                html_writer::link($url, $filename),
98
                                array('class' => 'coursefile fp-filename-icon'));
99
                    }
100
                }
101
                $content .= $contentimages. $contentfiles;
102
103
                $this->content->text .= $content. '</div>';
104
            }
105
        }
106
107
        return $this->content;
108
    }
109
110
    public function applicable_formats() {
111
        return array('all' => true);
112
    }
113
114
    public function instance_allow_multiple() {
115
          return false;
116
    }
117
118
    public function has_config() {
119
        return false;
120
    }
121
122
    public function cron() {
123
        return true;
124
    }
125
126
    public static function get_featured_courses() {
127
        global $DB;
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...
128
129
        $sql = 'SELECT c.id, c.shortname, c.fullname, fc.sortorder
130
                  FROM {block_featuredcourses} fc
131
                  JOIN {course} c
132
                    ON (c.id = fc.courseid)
133
              ORDER BY sortorder';
134
        return $DB->get_records_sql($sql);
135
    }
136
137
    public static function delete_featuredcourse($courseid) {
138
        global $DB;
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...
139
        return $DB->delete_records('block_featuredcourses', array('courseid' => $courseid));
140
    }
141
}
142