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

submissions.php (2 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
2
3
require_once 'common.inc.php';
4
5
use smtech\ReflexiveCanvasLTI\LTI\ToolProvider;
6
7
/**
8
 * This is a quasi temporary "unborking" while a few support tickets are
9
 * working through the queue. See the fix-me notations below.
10
 *
11
 * @param string $previewUrl
12
 * @return string
13
 */
14
function unborkPreviewUrl($previewUrl)
15
{
16
    if (!preg_match('%^https?://.*%', $previewUrl)) {
17
        /*
0 ignored issues
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...
18
         * 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
19
         */
20
        return $_SESSION[CANVAS_INSTANCE_URL] . $previewUrl;
21
    } elseif (preg_match('%^(.*version=)(\d+)(.*)$%', $previewUrl, $match)) {
22
        /*
0 ignored issues
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...
23
         * FIXME: per [case 01584819](https://cases.canvaslms.com/CommunityConsole?id=500A000000UPwSlIAL) preview URLs are generated "off by one" and are really zero-indexed.
24
         */
25
        return $match[1] . ($match[2] - 1) . $match[3];
26
    } else {
27
        /* whatevs, it is what it is… */
28
        return $previewUrl;
29
    }
30
}
31
32
$enrollments = $toolbox->api_get(
33
    'courses/' . $_SESSION[ToolProvider::class]['canvas']['course_id'] . '/enrollments',
34
    [
35
        'user_id' => $_SESSION[ToolProvider::class]['canvas']['user_id']
36
    ]
37
);
38
39
$isStudent = false;
40
foreach ($enrollments as $enrollment) {
41
    if ($enrollment['type'] == 'StudentEnrollment') {
42
        $isStudent = true;
43
    }
44
    if (empty($user)) {
45
        $user = $enrollment['user'];
46
    }
47
}
48
49
$assignments = [];
50
if ($isStudent) {
51
    $submissions = $toolbox->api_get(
52
        'courses/' . $_SESSION[ToolProvider::class]['canvas']['course_id'] . '/students/submissions',
53
        [
54
            'as_user_id' => $user['id'],
55
            'student_ids' => [$user['id']],
56
            'include' => ['submission_history']
57
        ]
58
    );
59
60
    foreach ($submissions as $submission) {
61
        $assignment = $toolbox->api_get(
62
            'courses/' . $_SESSION[ToolProvider::class]['canvas']['course_id'] . "/assignments/{$submission['assignment_id']}"
63
        );
64
        if (!in_array('not_graded', $assignment['submission_types'])) {
65
            $assignmentData['assignment'] = $assignment;
66
            foreach($submission['submission_history'] as $version) {
67
                if (!empty($version['submitted_at'])) {
68
                    $versionData = [
69
                        'id' => $version['id'],
70
                        'attempt' => $version['attempt'],
71
                        'submitted_at' => $version['submitted_at']
72
                    ];
73
                    if ($version['submission_type'] == 'online_text_entry') {
74
                        $versionData['body'] = $version['body'];
75
                    } else {
76
                        if (empty($version['attachments'])) {
77
                            $versionData['preview_url'] = unborkPreviewUrl($version['preview_url']);
78
                        } else {
79
                            foreach ($version['attachments'] as $attachment) {
80
                                $versionData['attachments'][$attachment['id']] = unborkPreviewUrl($attachment['preview_url']);
81
                            }
82
                        }
83
                    }
84
                    if (!empty($versionData['body']) || !empty($versionData['preview_url']) || !empty($versionData['attachments'])) {
85
                        $assignmentData['submissions'][$versionData['attempt']] = $versionData;
86
                    }
87
                }
88
            }
89
            if (!empty($assignmentData['submissions'])) {
90
                $assignments[$submission['assignment_id']] = $assignmentData;
91
            }
92
            unset($assignmentData);
93
            unset($versionData);
94
        }
95
    }
96
}
97
98
$toolbox->smarty_assign([
99
    'name' => 'See All Submissions',
100
    'category' => $user['name'],
101
    'assignments' => $assignments,
102
]);
103
if (empty($assignments)) {
104
    $toolbox->smarty_display('no_submissions.tpl');
105
} else {
106
    $toolbox->smarty_display('submissions.tpl');
107
}
108