Completed
Push — master ( 1a78df...e4a2dc )
by Seth
01:53
created

submissions.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 15 and the first side effect is on line 3.

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
3
require_once 'common.inc.php';
4
5
use smtech\ReflexiveCanvasLTI\LTI\ToolProvider;
6
use Battis\DataUtilities;
7
8
/**
9
 * This is a quasi temporary "unborking" while a few support tickets are
10
 * working through the queue. See the fix-me notations below.
11
 *
12
 * @param string $previewUrl
13
 * @return string
14
 */
15
function unborkPreviewUrl($previewUrl)
1 ignored issue
show
unborkPreviewUrl uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
16
{
17
    if (!preg_match('%^https?://.*%', $previewUrl)) {
18
        /*
1 ignored issue
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
19
         * FIXME: per [case 01584858 ](https://cases.canvaslms.com/CommunityConsole?id=500A000000UPwauIAD) attachments are not well-documented and the Crocodoc attachments include an incomplete preview URL that works… but not in an IFRAME
20
         */
21
        return $_SESSION[CANVAS_INSTANCE_URL] . $previewUrl;
22
    } elseif (preg_match('%^(.*version=)(\d+)(.*)$%', $previewUrl, $match)) {
23
        /*
1 ignored issue
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
24
         * FIXME: per [case 01584819](https://cases.canvaslms.com/CommunityConsole?id=500A000000UPwSlIAL) preview URLs are generated "off by one" and are really zero-indexed.
25
         */
26
        return $match[1] . ($match[2] - 1) . $match[3];
27
    } else {
28
        /* whatevs, it is what it is… */
29
        return $previewUrl;
30
    }
31
}
32
33
$enrollments = $toolbox->api_get(
34
    'courses/' . $_SESSION[ToolProvider::class]['canvas']['course_id'] . '/enrollments',
35
    [
36
        'user_id' => $_SESSION[ToolProvider::class]['canvas']['user_id']
37
    ]
38
);
39
40
$isStudent = false;
41
foreach ($enrollments as $enrollment) {
42
    if ($enrollment['type'] == 'StudentEnrollment') {
43
        $isStudent = true;
44
    }
45
    if (empty($user)) {
46
        $user = $enrollment['user'];
47
    }
48
}
49
50
$assignments = [];
51
if ($isStudent) {
52
    $submissions = $toolbox->api_get(
53
        'courses/' . $_SESSION[ToolProvider::class]['canvas']['course_id'] . '/students/submissions',
54
        [
55
            'as_user_id' => $user['id'],
56
            'student_ids' => [$user['id']],
57
            'include' => ['submission_history']
58
        ]
59
    );
60
61
    foreach ($submissions as $submission) {
62
        $assignment = $toolbox->api_get(
63
            'courses/' . $_SESSION[ToolProvider::class]['canvas']['course_id'] . "/assignments/{$submission['assignment_id']}"
64
        );
65
        if (!in_array('not_graded', $assignment['submission_types'])) {
66
            $assignmentData['assignment'] = $assignment;
67
            foreach($submission['submission_history'] as $version) {
68
                if (!empty($version['submitted_at'])) {
69
                    $versionData = [
70
                        'id' => $version['id'],
71
                        'attempt' => $version['attempt'],
72
                        'submitted_at' => $version['submitted_at']
73
                    ];
74
                    if ($version['submission_type'] == 'online_text_entry') {
75
                        $versionData['body'] = $version['body'];
76
                    } else {
77
                        if (empty($version['attachments'])) {
78
                            $versionData['type'] = DataUtilities::titleCase(str_replace('_', ' ', $version['submission_type']));
79
                            $versionData['preview_url'] = unborkPreviewUrl($version['preview_url']);
80
                        } else {
81
                            foreach ($version['attachments'] as $attachment) {
82
                                $versionData['attachments'][$attachment['id']] = [
83
                                        'name' => $attachment['display_name'],
84
                                        'preview_url' => unborkPreviewUrl($attachment['preview_url'])
85
                                    ];
86
                            }
87
                        }
88
                    }
89
                    if (!empty($versionData['body']) || !empty($versionData['preview_url']) || !empty($versionData['attachments'])) {
90
                        $assignmentData['submissions'][$versionData['attempt']] = $versionData;
91
                    }
92
                }
93
            }
94
            if (!empty($assignmentData['submissions'])) {
95
                $assignments[$submission['assignment_id']] = $assignmentData;
96
            }
97
            unset($assignmentData);
98
            unset($versionData);
99
        }
100
    }
101
}
102
103
$toolbox->smarty_assign([
104
    'name' => 'See All Submissions',
105
    'category' => $user['name'],
106
    'assignments' => $assignments,
107
]);
108
if (empty($assignments)) {
109
    $toolbox->smarty_display('no_submissions.tpl');
110
} else {
111
    $toolbox->smarty_display('submissions.tpl');
112
}
113