Newer
Older

Eduard Frankford
committed
import { Component, OnInit, Input, OnDestroy } from '@angular/core';
import { Exercise, IExerciseType } from 'app/shared/model/exercise.model';
import { Person } from 'app/shared/model/person.model';

Eduard Frankford
committed
import { Subscription } from 'rxjs';
import { PluginActionInfo } from 'app/shared/model/search/search-result-dto.model';
import { PluginService } from 'app/shared/service/plugin-service';
import { ShoppingBasketInfo, ShoppingBasketRedirectInfoDTO } from 'app/shared/model/basket/shopping-basket-info.model';

Eduard Frankford
committed
import { AccountService } from 'app/core/auth/account.service';
import { Account } from 'app/core/user/account.model';

Michael Breu
committed
import { SearchService } from 'app/search/service/search-service.ts';
import { HttpResponse } from '@angular/common/http';
import { JhiAlertService } from 'ng-jhipster';

Michael Breu
committed
selector: 'jhi-exercise-details',
templateUrl: './exercise-details.component.html',
styleUrls: ['./exercise-details.component.scss'],

Eduard Frankford
committed
export class ExerciseDetailsComponent implements OnInit, OnDestroy {

Michael Breu
committed
@Input() exercise: Exercise | undefined;
account: Account | null = null;
authSubscription?: Subscription;

Michael Breu
committed
constructor(
private accountService: AccountService,
protected pluginService: PluginService,
private searchService: SearchService,
private jhiAlertService: JhiAlertService
) {}

Michael Breu
committed
ngOnInit(): void {
this.authSubscription = this.accountService.getAuthenticationState().subscribe(account => (this.account = account));
}

Michael Breu
committed
public get exerciseType(): typeof IExerciseType {
return IExerciseType;
}

Eduard Frankford
committed

Michael Breu
committed
public isAuthenticated(): boolean {
return this.accountService.isAuthenticated();
}

Michael Breu
committed
public getPersonName(person: Person): string {
return person.name;
}

Michael Breu
committed
public getPersonDetails(person: Person): string {
return person.name + ', ' + person.affiliation;
}

Michael Breu
committed
public getPersonDetailsWithEmail(person: Person): string {
return "<a class='text-dark' href='mailto:" + person.email + "'>" + person.name + ', ' + person.affiliation + '</a>';

Michael Breu
committed
}

Eduard Frankford
committed

Michael Breu
committed
public arrayToString(array: string[]): string {
let result = '';
let i = 1;
array.forEach(element => {
if (array.length > 1 && array.length !== i) {
result += element + ', ';
} else {
result += element;
}
if (i % 5 === 0) {
result += '<br>';
}
i++;
});
return result;
}

Eduard Frankford
committed
public startAction(action: PluginActionInfo, exercise: Exercise): void {
const basketInfo: ShoppingBasketInfo = {
plugin: action.plugin,
action: action.action,
itemInfos: [exercise.originalResult],
};
this.pluginService.getRedirectLink(basketInfo).subscribe(
(redirectInfo: ShoppingBasketRedirectInfoDTO) => {
// alert('redirecting to ' + redirectInfo.redirectURL);
// location.href = redirectInfo.redirectURL;
window.open(redirectInfo.redirectURL, action.action);
},
() => alert('Search failed')
);
}

Michael Breu
committed
public download(): void {
this.exportExercise(Number(this.exercise!.originalResult.project.project_id));
}

Eduard Frankford
committed

Michael Breu
committed
exportExercise(projectId: number) {
return this.searchService.exportExercise(projectId).subscribe(
(response: HttpResponse<Blob>) => {
this.jhiAlertService.success('artemisApp.programmingExercise.export.successMessage');
if (response.body) {
const zipFile = new Blob([response.body], { type: 'application/zip' });
const url = window.URL.createObjectURL(zipFile);
const link = document.createElement('a');
link.setAttribute('href', url);
link.setAttribute('download', response.headers.get('filename')!);
document.body.appendChild(link); // Required for FF
link.click();
window.URL.revokeObjectURL(url);

Michael Breu
committed
},
() => alert('Unable to export exercise. Please log in to export.')

Michael Breu
committed
);
}
public getViews(): number {
return 5;
}
openLink(link: string): void {
window.open(link);
}
ngOnDestroy(): void {
if (this.authSubscription) {
this.authSubscription.unsubscribe();

Eduard Frankford
committed
}

Michael Breu
committed
}

Michael Breu
committed
/**
* correct missing image urls
*/
correctImageURL(event: Event): void {
if (event.srcElement) {
event.srcElement['src'] = '/content/img/gitLab.png';

Michael Breu
committed
}