CakeCMS-Python-API/examples/get-exam-solutions.py

69 lines
2.4 KiB
Python

import json
import cakecms
# insert your credentials here ...
HOST = 'http://localhost/cakecms'
TOKEN = '2q4q3dsLrnFsXf8AMFSKEZj3'
COURSE = '...'
# create an instance
cms = cakecms.CakeCMS(HOST, TOKEN)
cms.debug = True
cms.course = COURSE
# list exams
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(json.dumps(solutions['solutionList'], indent=4))
# list the responses of a solution
for solution in solutions['solutionList']:
content = cms.exam_solutions_get(solution['id'])
print('-'*80)
print('#'+str(solution['id'])+': ', json.dumps(content['data'], indent=4))
# Or: get all at once:
print('='*80)
all_solutions = cms.exam_solutions_get_all(exam_id)
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')