Skip to content

Commit

Permalink
myLife: print achievements (fixes #7436) (#7438)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Mutugiii and dogi authored Apr 25, 2024
1 parent 10d7bc2 commit 8534382
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "planet",
"license": "AGPL-3.0",
"version": "0.14.25",
"version": "0.14.26",
"myplanet": {
"latest": "v0.14.92",
"latest": "v0.14.97",
"min": "v0.14.0"
},
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
<span *ngIf="user?.firstName; else elseBlock">{{ user.firstName}} {{user.middleName}} {{user.lastName }}</span>
<ng-template #elseBlock>{{ user.name }}</ng-template>
<span class="toolbar-fill"></span>
<a mat-raised-button color="accent" routerLink="update" *ngIf="ownAchievements"><mat-icon>mode_edit</mat-icon>
<a mat-raised-button color="primary" style="margin-right: 1rem;" *ngIf="ownAchievements" (click)="generatePDF()">
<span i18n>Print Achievements</span>
</a> <br>
<a mat-raised-button color="accent" routerLink="update" *ngIf="ownAchievements">
<span *ngIf="achievementNotFound" i18n>Add Achievements</span>
<span *ngIf="!achievementNotFound" i18n>Edit Achievements</span>
</a>
Expand Down
82 changes: 82 additions & 0 deletions src/app/users/users-achievements/users-achievements.component.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { format } from 'date-fns';
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
import { CouchService } from '../../shared/couchdb.service';
Expand All @@ -10,6 +11,10 @@ import { StateService } from '../../shared/state.service';
import { CoursesService } from '../../courses/courses.service';
import { CertificationsService } from '../../manager-dashboard/certifications/certifications.service';

const pdfMake = require('pdfmake/build/pdfmake');
const pdfFonts = require('pdfmake/build/vfs_fonts');
pdfMake.vfs = pdfFonts.pdfMake.vfs;

@Component({
templateUrl: './users-achievements.component.html',
styleUrls: [ './users-achievements.scss' ],
Expand Down Expand Up @@ -107,4 +112,81 @@ export class UsersAchievementsComponent implements OnInit {
});
}

generatePDF() {
const formattedBirthDate = format(new Date(this.user.birthDate), 'MMM d, y, h:mm:ss a');
let contentArray = [
{
text: `${`${this.user.firstName}'s achievements`}`,
style: 'header',
alignment: 'center',
},
{
text: `
${this.user.firstName} ${this.user.middleName ? this.user.middleName : ''} ${this.user.lastName}
${this.user.birthDate ? `Birthdate: ${this.user.birthDate}` : ''}
${formattedBirthDate ? `Birthplace: ${formattedBirthDate}` : ''}
`,
alignment: 'center',
},
];

const optionals = [];
if (this.achievements.purpose) {
optionals.push(
{ text: 'My Purpose', style: 'subHeader', alignment: 'center' },
{ text: this.achievements.purpose, alignment: 'left', margin: [ 20, 5 ] }
);
}

if (this.achievements.goals) {
optionals.push(
{ text: 'My Goals', style: 'subHeader', alignment: 'center' },
{ text: this.achievements.goals, alignment: 'left', margin: [ 20, 5 ] }
);
}

if (this.achievements.achievements && this.achievements.achievements.length > 0) {
optionals.push(
{ text: 'My Achievements', style: 'subHeader', alignment: 'center' },
...this.achievements.achievements.map((achievement) => {
return [
{ text: achievement.title, bold: true, margin: [ 20, 5 ] },
{ text: achievement.description, marginLeft: 40 },
];
})
);
}

if (this.certifications && this.certifications.length > 0) {
optionals.push(
{ text: 'My Certifications', style: 'subHeader', alignment: 'center' },
...this.certifications.map((certification) => {
return [
{ text: certification.title, bold: true, margin: [ 20, 5 ] },
{ text: certification.description, marginLeft: 40 },
];
})
);
}

contentArray = contentArray.concat(optionals);

const documentDefinition = {
content: contentArray,
styles: {
header: {
fontSize: 18,
bold: true,
},
subHeader: {
fontSize: 16,
bold: true
}
},
};

pdfMake
.createPdf(documentDefinition)
.download(`${this.user.name} achievements.pdf`);
}
}

0 comments on commit 8534382

Please sign in to comment.