forked from ubccapico/team-formation-script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_data.py
More file actions
111 lines (96 loc) · 3.57 KB
/
fetch_data.py
File metadata and controls
111 lines (96 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
import canvasapi
import click
from team_formation import config
from team_formation.data_helpers import process_canvas_course, process_canvas_sections, \
process_canvas_students
from team_formation.prompts import course_prompt
@click.command()
@click.option('--store_data',
default=False,
is_flag=True,
help='Store data fetched into `.csv` files. [default: False]',
envvar='CANVAS_STORE_DATA_LOCALLY')
@click.option('--course_id',
help='Canvas Course ID.',
type=int,
envvar='CANVAS_COURSE_ID')
@click.option('--token',
prompt=False,
default=config.TOKEN,
hide_input=True,
help='Canvas API token.',
required=True,
envvar='CANVAS_API_TOKEN')
@click.option('--url',
default='https://canvas.ubc.ca',
# default='https://ubc.test.instructure.com',
help='Canvas Url. [default: https://canvas.ubc.ca]',
# help='Canvas Url. [default: https://ubc.test.instructure.com]',
required=True,
envvar='CANVAS_BASE_URL')
def fetch_data(url, token, course_id, store_data):
config.STORE_DATA_LOCALLY = store_data
_fetch_data(url, token, course_id)
def _fetch_data(url, token, course_id):
canvas = canvasapi.Canvas(url, token)
data = {}
if not course_id:
# prompt user to select a course they have access to (paginated)
course_id = course_prompt(canvas)
# get course by id
course = canvas.get_course(course_id, include=['total_students'])
data['course_df'] = process_canvas_course(course)
sections = course.get_sections(include=['students'], per_page=config.PER_PAGE)
data['sections_df'] = process_canvas_sections(sections)
students = course.get_users(enrollment_type=['student'], enrollment_stat=['active'], per_page=config.PER_PAGE)
data['students_df'] = process_canvas_students(students)
modules = course.get_modules()
for module in modules:
module_items =module.get_module_items()
module_items = [module_item for module_item in module_items]
for mi in module_items:
print(mi)
return (course, data)
def fetch_student_survey_data(course, student_id, quiz_id):
survey = course.get_quiz(quiz_id)
students = course.get_users(enrollment_state=['active']);
student = None;
for s in students:
if(s.id == student_id):
student = s;
break
submissions = survey.get_submissions()
submission = None
for x in submissions:
if(x.user_id == student_id):
submission = x
break;
q_data=[]
if(submission is None):
return None
for y in submission.get_submission_events():
if(y.event_type == 'question_answered'):
q_data += y.event_data
read_data = [];
for z in q_data:
# print(z);
q = survey.get_question(int(z['quiz_question_id'])).question_text
for a in survey.get_question(int(z['quiz_question_id'])).answers:
#handle multiple dropdowns separately
if type(z['answer']) is dict:
for e in z['answer']:
if z['answer'][e] is None:
continue;
elif z['answer'] is not None and a['id'] == int(z['answer']):
# print({'Question':q,'Answer':a['text']})
read_data.append({'Question':q,'Answer':a['text']});
break;
if(student is None):
name = "Test"
else:
name = student.name
return {'student_name':name, 'quiz':survey.title, 'answers': read_data}
# def fetch_all_survey_data(course, quiz_id):
if __name__ == '__main__':
fetch_data()