Completed
Push — develop ( 2bfc38...b5fecd )
by Seth
02:09
created

Toolbox::averageAssignmentCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace smtech\GradingAnalytics;
4
5
use smtech\LTI\Configuration\Option;
6
7
class Toolbox extends \smtech\StMarksReflexiveCanvasLTI\Toolbox
8
{
9
    protected $courseHistory = [];
10
11
    const DEPT = 0;
12
    const SCHOOL = 1;
13
14
    protected $snapshots = [[], []];
15
16
    const AVERAGE_TURN_AROUND = 0;
17
    const AVERAGE_ASSIGNMENT_COUNT = 1;
18
19
    protected $numbers = [];
20
21
    public function getGenerator()
22
    {
23
        parent::getGenerator();
24
25
        $this->generator->setOptionProperty(
26
            Option::COURSE_NAVIGATION(),
27
            'visibility',
28
            'admins'
29
        );
30
        $this->generator->setOptionProperty(
31
            Option::ACCOUNT_NAVIGATION(),
32
            'visibility',
33
            'admins'
34
        );
35
36
        return $this->generator;
37
    }
38
39
    private $GRAPH_DATA_COUNT = 0;
40
    public function graphWidth($dataCount = false)
41
    {
42
        if ($dataCount) {
43
            $this->GRAPH_DATA_COUNT = $dataCount;
0 ignored issues
show
Documentation Bug introduced by
The property $GRAPH_DATA_COUNT was declared of type integer, but $dataCount is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
44
        }
45
        return max(GRAPH_MIN_WIDTH, $this->GRAPH_DATA_COUNT * GRAPH_BAR_WIDTH);
46
    }
47
48
    public function graphHeight($dataCount = false)
49
    {
50
        if ($dataCount) {
51
            $this->GRAPH_DATA_COUNT = $dataCount;
0 ignored issues
show
Documentation Bug introduced by
The property $GRAPH_DATA_COUNT was declared of type integer, but $dataCount is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
52
        }
53
        return $this->graphWidth() * GRAPH_ASPECT_RATIO;
54
    }
55
56
    public function getMostCurrentCourseTimestamp($courseId)
57
    {
58
        return $this->getCourseHistory($courseId)[0]['timestamp'];
59
    }
60
61
    public function getDepartmentId($courseId)
62
    {
63
        return substr($this->getCourseHistory($courseId)[0]['course[account_id]'], 0, 10);
64
    }
65
66
    public function getCourseHistory($courseId)
67
    {
68
        if (empty($this->courseHistory)) {
69
            if ($response = $this->mysql_query("
70
                SELECT * FROM `course_statistics`
71
                    WHERE
72
                        `course[id]` = '$courseId'
73
                    ORDER BY
74
                        `timestamp` DESC
75
            ")) {
76
                while ($row = $response->fetch_assoc()) {
77
                    $this->courseHistory[] = $row;
78
                }
79
            }
80
        }
81
        return $this->courseHistory;
82
    }
83
84
    public function getCourseSnapshot($courseId)
85
    {
86
        $history = $this->getCourseHistory($courseId);
87
        if (!empty($history)) {
88
            return $history[0];
89
        }
90
        return false;
91
    }
92
93
    public function getDepartmentSnapshot($courseId)
94
    {
95
        return $this->getSnapshot($courseId, self::DEPT);
96
    }
97
98
    public function getSchoolSnapshot($courseId)
99
    {
100
        return $this->getSnapshot($courseId, self::SCHOOL);
101
    }
102
103
    public function getSnapshot($courseId, $domain = self::DEPT)
104
    {
105
        if (empty($this->snapshots[$domain])) {
106
            if ($response = $this->mysql_query("
107
                SELECT * FROM `course_statistics`
108
                    WHERE
109
                        " . ($domain === self::DEPT ? "`course[account_id]` = '" . $this->getDepartmentId($courseId) . "' AND" : '') . "
110
                        `timestamp` LIKE '" . $this->getMostCurrentCourseTimestamp($courseId) . "%'
111
                    GROUP BY
112
                        `course_id`
113
                    ORDER BY
114
                        `timestamp` DESC
115
            ")) {
116
                while ($row = $response->fetch_assoc()) {
117
                    $this->schoolSnapshot[] = $row;
0 ignored issues
show
Bug introduced by
The property schoolSnapshot does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
118
119
                    /* average turn-around */
120
                    $totalTurnAround += $row['average_grading_turn_around'] * $row['student_count'] * $row['graded_assignment_count'];
0 ignored issues
show
Bug introduced by
The variable $totalTurnAround does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
121
                    $divisorTurnAroud += $row['student_count'] * $row['graded_assignment_count'];
0 ignored issues
show
Bug introduced by
The variable $divisorTurnAroud does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
122
123
                    /* average assignment count */
124
                    $totalAssignmentCount += $row['assignments_due_count'] + $row['dateless_assignment_count'];
0 ignored issues
show
Bug introduced by
The variable $totalAssignmentCount does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
125
                }
126
                $this->numbers[self::DEPT][self::AVERAGE_TURN_AROUND] = $totalTurnAround / $divisorTurnAround;
0 ignored issues
show
Bug introduced by
The variable $divisorTurnAround does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
127
                $this->numbers[self::DEPT][self::AVERAGE_ASSIGNMENT_COUNT] = $total / $response->num_rows;
0 ignored issues
show
Bug introduced by
The variable $total does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
128
            }
129
        }
130
        return $this->schoolSnapshot;
131
    }
132
133
    public function averageTurnAround($courseId, $domain = self::DEPT)
134
    {
135
        $this->getSnapshot($courseId, $domain);
136
        return $this->numbers[$domain][self::AVERAGE_TURN_AROUND];
137
    }
138
139
    public function averageAssignmentCount($courseId, $domain = self::DEPT)
140
    {
141
        $this->getSnapshot($courseId, $domain);
142
        return $this->numbers[$domain][self::AVERAGE_ASSIGNMENT_COUNT];
143
    }
144
}
145