Skip to content

Instantly share code, notes, and snippets.

@edtsech
Last active July 5, 2019 14:27
Show Gist options
  • Save edtsech/6bea82206bb33f14917214ed135c4ce0 to your computer and use it in GitHub Desktop.
Save edtsech/6bea82206bb33f14917214ed135c4ce0 to your computer and use it in GitHub Desktop.
@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
}
@edtsech
Copy link
Author

edtsech commented Jul 5, 2019

TopicModel or TopicModelAsStudent ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment