Last active
July 5, 2019 14:27
-
-
Save edtsech/6bea82206bb33f14917214ed135c4ce0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Field({ | |
type: () => TopicEnrolmentModel, | |
description: `creates a new enrolment if retries are allowed` | |
}) | |
async reEnrol( | |
@Context ctx: UserSessionModel | |
) { | |
const tv = await this.$relatedQuery('currentTopicVersion') | |
if (tv.allow_retry) { | |
const enrolment = await this.createEnrolment(ctx.user, tv) | |
return enrolment | |
} else { | |
return null | |
} | |
} | |
async selfEnrol(user: UserModel) { | |
const tv = await this.$relatedQuery('currentTopicVersion') | |
if (!tv) { | |
throw new InvalidOperation(`topic ${this.id} is not published`) // no user can self enrol to an unpublished topic | |
} | |
const enrolment = await this.createEnrolment(user, tv) | |
return enrolment | |
} | |
private async createEnrolment(user: UserModel, topicVersion: TopicVersionModel) { | |
const enrolment = await this.$relatedQuery<TopicEnrolmentModel>( | |
'enrolments' | |
).insertAndFetch({ | |
topic_version_id: this.production_topic_version_id, | |
user_id: user.id, | |
topic_id: this.id, | |
start_date: new Date(), | |
shared_by: null, | |
available_points: topicVersion.number_quizzes, | |
pass_mark: topicVersion.pass_mark, | |
is_assigned: false | |
}) | |
return enrolment | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TopicModel or TopicModelAsStudent ?