Skip to content

Commit 4743766

Browse files
committed
Added discussion generation
1 parent d7dba5a commit 4743766

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

canvas_mock_data.py

+58-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ def get_sub_types():
158158
return list(online | offline)
159159

160160
course = canvas.get_course(course_id)
161-
162161
num_assignments = fake.random_int(min_assignments, max_assignments)
163162

164163
for i in range(num_assignments):
@@ -169,3 +168,61 @@ def get_sub_types():
169168
'description': fake.text(max_nb_chars=fake.random_int(100, 500)),
170169
'published': fake.boolean(75)
171170
})
171+
172+
173+
def generate_discussions(course_id, min_discussions=3, max_discussions=7):
174+
"""
175+
Create discussion topics in a course.
176+
177+
:param course_id: The Canvas Course to create discussions in.
178+
:type course_id: int
179+
:param min_discussions: The minimum number of discussions to create.
180+
:type min_discussions: int
181+
:param max_discussions: The maximum number of discussions to create.
182+
:type max_discussions: int
183+
"""
184+
course = canvas.get_course(course_id)
185+
num_discussions = fake.random_int(min_discussions, max_discussions)
186+
187+
for i in range(num_discussions):
188+
course.create_discussion_topic(
189+
title=fake.bs().title(),
190+
message=fake.text(max_nb_chars=fake.random_int(100, 500)),
191+
published=True
192+
)
193+
194+
195+
def generate_discussion_entries(course_id, chance_to_skip=25, students_only=True):
196+
"""
197+
Create several entries in all Discussion Topics by various users in
198+
the course.
199+
200+
:param course_id: The Canvas Course to add entries to
201+
:type course_id: int
202+
:param chance_to_skip: Percent chance (between 0 and 100, inclusive)
203+
to not create a particular entry for a user. Makes response
204+
patterns look more realistic.
205+
:type chance_to_skip: int
206+
:param students_only: Only create entries from students. Otherwise,
207+
allow non-student replies. Defaults to "True".
208+
:type students_only: bool
209+
"""
210+
course = canvas.get_course(course_id)
211+
212+
if students_only:
213+
users = course.get_users(enrollment_type=['student'])
214+
else:
215+
users = course.get_users()
216+
217+
# Have each user make one reply
218+
for user in users:
219+
# Reply to each discussion
220+
for discussion in course.get_discussion_topics():
221+
# Sometimes don't post based on chance to skip
222+
if fake.boolean(100 - chance_to_skip):
223+
paragraphs = fake.paragraphs(nb=fake.random_int(2, 5))
224+
paragraphs = ['<p>{}</p>'.format(p) for p in paragraphs]
225+
discussion.post_entry(
226+
message='\n'.join(paragraphs),
227+
as_user_id=user.id
228+
)

0 commit comments

Comments
 (0)