API for exam correction

master
Markus Bauer 2023-06-16 15:47:33 +02:00
parent 881c3a451d
commit 484dabe4c2
2 changed files with 70 additions and 1 deletions

View File

@ -61,6 +61,18 @@ class CakeCMS:
print('[', r.status_code, ']')
return r
def post_exams_api(self, uri, data, course=None, **kwargs):
if self.token:
self.session.headers['X-CMS-API-TOKEN'] = self.token
url = self.url + '/examination_backend/' + (self.course if course is None else course) + '/' + uri
headers = {'X-Exams-Tenant': self.course if course is None else course, 'X-CSRF-CHECK': '1'}
if self.debug:
print('> POST', url, ' ', end='')
r = self.session.post(url, json=data, headers=headers, **kwargs)
if self.debug:
print('[', r.status_code, ']')
return r
def assert_success(self, result):
if 'error' in result and result['error'] == 'exception':
if self.debug: print('<!', result['type'], ':', result['message'])
@ -273,6 +285,24 @@ class CakeCMS:
"""
return self.post('testingresults/change', {'Testingresult': {'student_id': student_id, 'testing_id': testing_id, 'points': points}}, course=course).json()
def testingresults_retrieve(self, testing_id, student_id=None, user_id=None, course=None):
"""
Retrieve the points of a student.
:param testing_id:
:param student_id:
:param user_id:
:param course:
:return:
"""
params = {}
if student_id is not None:
params['studentId'] = str(student_id)
elif user_id is not None:
params['userId'] = str(user_id)
else:
raise Exception('Need either student_id or user_id')
return self.get('testingresults/retrieve/' + str(testing_id), course=course, named_params=params).json()
def materials_index(self, course=None):
"""
Get a list of all accessible material categories and files.
@ -324,3 +354,9 @@ class CakeCMS:
def exam_solution_download_file(self, downloadname, course=None):
return self.get_exams_api('files/' + (course if course else self.course) + '/solution/' + downloadname, course=course)
def exam_solutions_set_correction(self, solution_id, path, value, course=None):
return self.post_exams_api('solution/correct', {'id': solution_id, 'key': path, 'value': value}, course=course).json()
def exam_solutions_set_comment(self, solution_id, comment, course=None):
return self.post_exams_api('solution/correct', {'id': solution_id, 'comment': comment}, course=course).json()

View File

@ -2,6 +2,7 @@ import json
import cakecms
# insert your credentials here ...
HOST = 'http://localhost/cakecms'
TOKEN = '2q4q3dsLrnFsXf8AMFSKEZj3'
COURSE = '...'
@ -15,9 +16,10 @@ cms.course = COURSE
exams = cms.exams_index()
# list solutions for an exam
print('Retrieving data for exam', exams[0]['name'])
exam_id = exams[0]['id']
solutions = cms.exam_solutions_index(exam_id)
print(solutions)
print(json.dumps(solutions['solutionList'], indent=4))
# list the responses of a solution
for solution in solutions['solutionList']:
@ -33,3 +35,34 @@ print(json.dumps(all_solutions, indent=4))
## How to retrieve uploaded files:
#x = cms.exam_solution_download_file('668aae926f91f95177d90ecba0cbf924a301e73934f4a827760384fed868621e.pdf')
#print(repr(x), x.content[:10])
# === CORRECTION ===
# We'll now focus on one solution:
solution = all_solutions['solutions'][-1]
# update corrections (remarks in the exam)
cms.exam_solutions_set_correction(solution['id'], 'answer.richtext1', 'This comment will appear at some input widget')
cms.exam_solutions_set_correction(solution['id'], '__bottom', 'This comment will appear at the end')
cms.exam_solutions_set_comment(solution['id'], 'This comment is invisible for students')
# check that everything worked
solution = cms.exam_solutions_get(solution['id'])
print('Correction:', solution['correctionData'])
print('Comment:', solution['comment'])
# get exam points
testing_id = solution['exam']['testingId']
print('Testing ID: ', testing_id)
if testing_id:
testings = cms.testingresults_retrieve(testing_id, user_id=solution['userId']) # attention: user ID here
student_id = testings['student_id'] # but we use student ID to save results
for testing in testings['testings']:
print('- ', testing['Testing']['name'], ' : ', testing['Testingresult']['points'], '/', testing['Testing']['max_points'], 'points')
# set exam points - give half of the points for second exercise
testing = testings['testings'][2]
points = testing['Testing']['max_points'] / 2
result = cms.testingresults_change(testing['Testing']['id'], student_id, points)
print('OK' if result else 'FAILED')