This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
exercise-details.component.ts 3.46 KiB
Newer Older
import { Component, OnInit, Input, OnDestroy } from '@angular/core';
import { Exercise, IExerciseType } from 'app/shared/model/exercise.model';
Michael Breu's avatar
Michael Breu committed
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';
import { AccountService } from 'app/core/auth/account.service';
import { Account } from 'app/core/user/account.model';
import { SearchService } from 'app/search/service/search-service.ts';
import { HttpResponse } from '@angular/common/http';
import { JhiAlertService } from 'ng-jhipster';

  selector: 'jhi-exercise-details',
  templateUrl: './exercise-details.component.html',
  styleUrls: ['./exercise-details.component.scss'],
export class ExerciseDetailsComponent implements OnInit, OnDestroy {
  @Input() exercise: Exercise | undefined;
  account: Account | null = null;
  authSubscription?: Subscription;
  constructor(
    private accountService: AccountService,
    protected pluginService: PluginService,
    private searchService: SearchService,
    private jhiAlertService: JhiAlertService
  ) {}
  ngOnInit(): void {
    this.authSubscription = this.accountService.getAuthenticationState().subscribe(account => (this.account = account));
  }
Michael Breu's avatar
Michael Breu committed

  public get exerciseType(): typeof IExerciseType {
    return IExerciseType;
  }
  public isAuthenticated(): boolean {
    return this.accountService.isAuthenticated();
  }
Michael Breu's avatar
Michael Breu committed

Daniel Rainer's avatar
Daniel Rainer 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')
    );
  }
  public download(): void {
    this.exportExercise(Number(this.exercise!.originalResult.project.project_id));
  }
  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's avatar
Michael Breu committed
        }
      () => alert('Unable to export exercise. Please log in to export, or check your git lab permissions.')
    );
  }

  openLink(link: string): void {
    window.open(link);
  }

  ngOnDestroy(): void {
    if (this.authSubscription) {
      this.authSubscription.unsubscribe();
  /**
   * correct missing image urls
   */
  correctImageURL(event: Event): void {
    if (event.srcElement) {
      event.srcElement['src'] = '/content/img/gitLab.png';
Michael Breu's avatar
Michael Breu committed
  
  selectREADME(): void {
Michael Breu's avatar
Michael Breu committed
  }