Skip to content

Instantly share code, notes, and snippets.

@mcserep
Last active January 23, 2025 16:23
Show Gist options
  • Save mcserep/049f4343e433ffe64dcaa67b725d95a3 to your computer and use it in GitHub Desktop.
Save mcserep/049f4343e433ffe64dcaa67b725d95a3 to your computer and use it in GitHub Desktop.
Canvas LMS script to automatically check the assignment submissions, looking for suspicious activity.
"""MIT License
Copyright (c) 2024-2025 Mate Cserep, https://mcserep.web.elte.hu/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
### SETTINGS ###
# Base URL of your Canvas instance
base_url = 'https://canvas.elte.hu'
# Create a new token at the '<base_url>/profile/settings' page.
access_token = 'INSERT YOUR ACCESS TOKEN'
# Course and assignment ID. You can get it from the URL upon opening the assignment in your browser.
# The format will be: '<base_url>/courses/<course_id>/assignments/<quiz_id>'
course_id = 12345
assignment_id = 12345
### PROGRAM ###
import requests
# main
page = 1
try:
response = requests.get(f'{base_url}/api/v1/courses/{course_id}/assignments/{assignment_id}', {
'access_token': access_token,
'per_page': 50,
'page': page
})
response.raise_for_status()
assignment = response.json()
catch_count = 0
print('STUDENTS WITH SUSPICIOUS ACTIVITY:')
while True:
response = requests.get(f'{base_url}/api/v1/courses/{course_id}/assignments/{assignment_id}/submissions', {
'access_token': access_token,
'per_page': 50,
'page': page,
'include[]': ['user', 'submission_comments']
})
response.raise_for_status()
submissions = response.json()
page += 1
if len(submissions) == 0:
break
for submission in submissions:
if submission['submitted_at'] == None:
continue
if not submission['grade_matches_current_submission']:
print(submission['user']['name'])
print('Activity: Submission after grading')
print('Submitted at: ' + submission['submitted_at'])
print('Graded at: ' + submission['graded_at'])
print()
catch_count += 1
for comment in submission['submission_comments']:
if 'attachments' in comment and len(comment['attachments']) > 0 and comment['author_id'] == submission['user_id']:
print(submission['user']['name'])
print('Activity: Attachment in submission comments')
print('Comment last modified: ' + (comment['edited_at'] if comment['edited_at'] != None else comment['created_at']))
print()
catch_count += 1
break
if catch_count == 0:
print('None.')
except requests.exceptions.RequestException as err:
print('Canvas communication error occurred, HTTP response code: {0}'.format(err.response.status_code))
print('Response: {0}'.format(err.response.text))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment