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
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)
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