Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

courses: surveys required to finish (fixes #7788, #7890) #7961

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h3 class="margin-lr-3 ellipsis-title"><ng-container i18n>Step</ng-container> {{
class="margin-lr-10"
*ngIf="stepDetail?.survey?.questions.length && isUserEnrolled"
(click)="goToExam('survey')" i18n>
Take Survey
{{ surveyCompleted ? 'Retake Survey' : 'Take Survey' }}
</a>
<button *ngIf="canManage && (stepDetail?.exam?.questions.length || stepDetail?.survey?.questions.length)" class="margin-lr-10" color="accent" mat-raised-button [matMenuTriggerFor]="previewMenu" (click)="menuTriggerButtonClick()" i18n>
Preview
Expand Down Expand Up @@ -66,7 +66,7 @@ <h3 class="margin-lr-3 ellipsis-title"><ng-container i18n>Step</ng-container> {{
<span>{{stepNum}}/{{maxStep}}</span>
<button mat-icon-button *ngIf="maxStep !== 1" [disabled]="stepNum === 1" (click)="changeStep(-1)"><mat-icon>navigate_before</mat-icon></button>
<button mat-icon-button *ngIf="stepNum !== maxStep" [disabled]="stepNum === maxStep || (!parent && stepDetail?.exam?.questions.length > 0 && !attempts)" (click)="changeStep(1)"><mat-icon>navigate_next</mat-icon></button>
<button mat-raised-button *ngIf="stepNum === maxStep && maxStep !== 1" [disabled]="!parent && stepDetail?.exam?.questions.length > 0 && !attempts" (click)="backToCourseDetail()" i18n>Finish</button>
<button mat-raised-button *ngIf="stepNum === maxStep && maxStep !== 1" [disabled]="!parent && stepDetail?.exam?.questions.length > 0 && !attempts || !surveyCompleted" (click)="backToCourseDetail()" i18n>Finish</button>
</div>
</mat-toolbar>
<div class="view-container view-full-height" [ngClass]="{'grid-view': showChat, 'flex-view': !isGridView && !showChat}" *ngIf="stepDetail?.description || resource?._attachments; else emptyRecord">
Expand Down
48 changes: 35 additions & 13 deletions src/app/courses/step-view-courses/courses-step-view.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { DialogsAnnouncementComponent, includedCodes, challengeCourseId, challen
})

export class CoursesStepViewComponent implements OnInit, OnDestroy {

onDestroy$ = new Subject<void>();
stepNum = 0;
stepDetail: any = { stepTitle: '', description: '', resources: [] };
Expand All @@ -40,6 +39,9 @@ export class CoursesStepViewComponent implements OnInit, OnDestroy {
showChat = false;
isOpenai = false;
isLoading = true;
surveyAnswers: any[] = [];
surveySubmission: any = null;
surveyCompleted = false;
@ViewChild(MatMenuTrigger) previewButton: MatMenuTrigger;

constructor(
Expand Down Expand Up @@ -86,20 +88,40 @@ export class CoursesStepViewComponent implements OnInit, OnDestroy {

getSubmission() {
this.submissionsService.submissionUpdated$.pipe(takeUntil(this.onDestroy$))
.subscribe(({ submission, attempts, bestAttempt = { grade: 0 } }) => {
this.examStart = (this.submissionsService.nextQuestion(submission, submission.answers.length - 1, 'passed') + 1) || 1;
this.examText = submission.answers.length > 0 ? 'continue' : attempts === 0 ? 'take' : 'retake';
this.attempts = attempts;
const examPercent = (bestAttempt.grade / this.stepDetail.exam.totalMarks) * 100;
this.examPassed = examPercent >= this.stepDetail.exam.passingPercentage;
if (!this.parent && this.progress.passed !== this.examPassed) {
this.coursesService.updateProgress({
courseId: this.courseId, stepNum: this.stepNum, passed: this.examPassed
});
}
});
.subscribe(({ submission, attempts, bestAttempt = { grade: 0 } }) => {
this.examStart = (this.submissionsService.nextQuestion(submission, submission.answers.length - 1, 'passed') + 1) || 1;
this.examText = submission.answers.length > 0 ? 'continue' : attempts === 0 ? 'take' : 'retake';
this.attempts = attempts;
const examPercent = (bestAttempt.grade / this.stepDetail.exam.totalMarks) * 100;
this.examPassed = examPercent >= this.stepDetail.exam.passingPercentage;

// Log submissions for the survey
console.log('Fetching submissions related to survey ID:', this.stepDetail.survey._id);

this.submissionsService.getSubmissionsBySurveyId(this.stepDetail.survey._id).pipe(takeUntil(this.onDestroy$))
.subscribe((submissions) => {
console.log('Submissions related to survey:', this.stepDetail.survey._id);
submissions.forEach((submission: any) => { // Type assertion to avoid TypeScript error
console.log('Submission (child) for survey:', submission);

// Check if the submission's status is 'complete' and the usernames match
if (submission.status === 'complete' && submission.user.name === this.userService.get().name) {
this.surveyCompleted = true;
console.log('Survey is complete and usernames match.');
}
});
});

// Update progress if necessary
if (!this.parent && this.progress.passed !== this.examPassed) {
this.coursesService.updateProgress({
courseId: this.courseId, stepNum: this.stepNum, passed: this.examPassed
});
}
});
}


ngOnDestroy() {
this.onDestroy$.next();
this.onDestroy$.complete();
Expand Down
20 changes: 20 additions & 0 deletions src/app/submissions/submissions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,4 +350,24 @@ export class SubmissionsService {
(includeAnswers ? exportText(this.getPDFAnswerText(submission, questionIndex, answerIndexes), questionIndex, 'Response') : '');
}

// Add this method inside your SubmissionsService

getSubmissionsBySurveyId(surveyId: string) {
// Construct the query to fetch submissions based on the survey ID
const query = findDocuments({
parentId: { "$regex": `^${surveyId}` } // Match submissions where parentId starts with the surveyId
});

// Call the existing getSubmissions method to fetch the submissions
return this.getSubmissions(query).pipe(
tap((submissions) => {
// Log all submissions related to the survey
console.log('Submissions for survey:', surveyId);
submissions.forEach(submission => {
console.log('Submission (child) for survey:', submission);
});
})
);
}

}
Loading