report-all-stat.ts 22.8 KB
Newer Older
1 2 3 4 5 6
import { Component, OnInit, OnDestroy, ElementRef, ViewChild, Input } from "@angular/core";
import { Injectable } from "@angular/core";
import { AppSettings } from '../../app.settings';
import { HelperDataService } from '../../services/helper-data-service';
import { Observable} from "rxjs/Observable";
import { Http, Headers, RequestOptions} from '@angular/http';
7
import { NgRedux, select } from '@angular-redux/store';
8 9 10 11 12 13 14
import { IAppState } from '../../store/store';
import { Router, ActivatedRoute, Params} from '@angular/router';
import { BehaviorSubject, Subscription } from 'rxjs/Rx';
import { ILoginInfo } from '../../store/logininfo/logininfo.types';
import { Ng2SmartTableModule, LocalDataSource } from 'ng2-smart-table';
import {reportsSchema, TableColumn} from './reports-schema';
import { LOGININFO_INITIAL_STATE } from '../../store/logininfo/logininfo.initial-state';
15
import { DIDE_ROLE, PDE_ROLE, MINISTRY_ROLE } from '../../constants';
16
import {csvCreator} from './csv-creator';
17
import {chartCreator} from './chart-creator';
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

import {
    FormBuilder,
    FormGroup,
    FormControl,
    FormArray,
    Validators,
} from '@angular/forms';


import { API_ENDPOINT } from '../../app.settings';

@Component({
    selector: 'report-all-stat',
    template: `

  <div>

36

37 38 39 40 41 42
        <div
          class = "loading" *ngIf="validCreator == 0" >
        </div>

        <form [formGroup]="formGroup"  #form>

43 44 45
          <!--<h5> >Επιλογή Φίλτρων <br><br></h5>-->
          <br>
          <button type="button" class="btn-link" (click)="toggleRegionFilter()" >
46 47 48 49 50

          <div *ngIf = "userLoggedIn == 'supervisor'">    > Φίλτρο Επιλογής Περιφ/κής Δ/νσης - Δ/νσης Εκπ/σης - Σχολείου </div>
          <div *ngIf = "userLoggedIn == 'dide'" >    > Φίλτρο Επιλογής Σχολείου </div>
          <div *ngIf = "userLoggedIn == 'pde'">    > Φίλτρο Επιλογής Δ/νσης Εκπ/σης - Σχολείου </div>

51
          </button>
52

53
          <div  class="col-md-11 offset-md-1">
54 55
                <label *ngIf = "enableRegionFilter && userLoggedIn == 'supervisor'"> Περιφερειακή Διεύθυνση </label>
                <select #regsel class="form-control" (change)="checkregion(regsel)" *ngIf = "enableRegionFilter" [value] = "regionSelected" [hidden] = "userLoggedIn != 'supervisor'" formControlName="region">
56
                  <option value="0"></option>
57
                  <option *ngFor="let RegionSelection$  of RegionSelections$ | async; let i=index" [value] = "RegionSelection$.id"> {{RegionSelection$.name}} </option>
58 59
                </select>
          </div>
60
          <div class="col-md-11 offset-md-1">
61 62
                <label *ngIf="(showAdminList | async) && enableRegionFilter && userLoggedIn != 'dide'">Διεύθυνση Εκπαίδευσης</label>
                <select #admsel class="form-control"  *ngIf="(showAdminList | async) && enableRegionFilter" (change)="checkadminarea(admsel)" [hidden] = "userLoggedIn == 'dide'" formControlName="adminarea">
63
                  <option value="0"></option>
64 65 66
                  <option *ngFor="let AdminAreaSelection$  of AdminAreaSelections$ | async; let i=index" [value] = "AdminAreaSelection$.id"> {{AdminAreaSelection$.name}}</option>
                </select>
          </div>
67
          <div class="col-md-11 offset-md-1">
68 69
                <label *ngIf="(showAdminList | async) && enableRegionFilter">Σχολείο</label>
                <select #schsel class="form-control"  *ngIf="(showAdminList | async) && enableRegionFilter" (change)="checkschool(schsel)" formControlName="schoollist">
70
                  <option value="0"></option>
71 72 73
                  <option *ngFor="let SchoolSelection$  of SchoolSelections$ | async; let i=index" [value] = "SchoolSelection$.epal_id"> {{SchoolSelection$.epal_name}} </option>
                </select>
          </div>
74

75 76 77 78 79 80
          <div *ngIf = "reportId == 3 || reportId == 5" >
            <button type="button" class="btn-link" (click)="toggleCourseFilter()" >
                  > Φίλτρο Επιλογής Τομέα / Ειδικότητας
            </button>
          </div>

81 82 83 84 85 86 87
          <div  class="col-md-11 offset-md-1">
                <label for="classid" *ngIf = "enableCourseFilter" >Τάξη</label><br/>
                <select #class_sel  class="form-control" (change)="checkclass(class_sel)" *ngIf = "enableCourseFilter" formControlName="classid" >
                  <option value="0" ></option>
                  <option value="1" >Α' Λυκείου</option>
                  <option value="2" >Β' Λυκείου</option>
                  <option value="3" >Γ' Λυκείου</option>
88
                  <option value="4" >Δ' Λυκείου</option>
89 90 91
                </select>
          </div>
          <div class="col-md-11 offset-md-1">
92 93 94
                <label *ngIf="(showSectorList | async) && enableCourseFilter && (classSelected == 2 || classSelected == 3 || classSelected == 4) ">Τομέας</label>
                <select #secsel class="form-control"  *ngIf="(showSectorList | async) && enableCourseFilter && (classSelected == 2 || classSelected == 3 || classSelected == 4)"
                      (change)="checksector(secsel)" formControlName="sector">
95 96 97 98 99
                  <option value="0"></option>
                  <option *ngFor="let SectorSelection$  of SectorSelections$ | async; let i=index" [value] = "SectorSelection$.id"> {{SectorSelection$.name}}</option>
                </select>
          </div>
          <div class="col-md-11 offset-md-1">
100 101 102
                <label *ngIf="(showCourseList | async) && enableCourseFilter && (classSelected == 3 || classSelected == 4)">Ειδικότητα</label>
                <select #coursel class="form-control"  *ngIf="(showCourseList | async) && enableCourseFilter && (classSelected == 3 || classSelected == 4)"
                      (change)="checkcourse(coursel)" formControlName="course">
103 104 105 106 107
                  <option value="0"></option>
                  <option *ngFor="let CourseSelection$  of CourseSelections$ | async; let i=index" [value] = "CourseSelection$.id"> {{CourseSelection$.name}}</option>
                </select>
          </div>
          <br>
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
          <button type="submit" class="btn btn-alert"  (click)="createReport(regsel)" [hidden]="minedu_userName == ''" >
          <i class="fa fa-file-text"></i>
              Δημιουργία Αναφοράς
          </button>
          <button type="submit" class="btn btn-alert pull-right"  (click)="navigateBack()" [hidden]="minedu_userName == ''" >
              Επιστροφή
          </button>
          <br><br>
        </form>

        <div *ngIf="validCreator == 1 ">
          <input #search class="search" type="text" placeholder="Αναζήτηση..." (keydown.enter)="onSearch(search.value)">
          <div class="smart-table-container" reportScroll>
            <ng2-smart-table [settings]="settings" [source]="source"></ng2-smart-table>
          </div>
        </div>

        <button type="button" class="alert alert-info pull-right" (click)="export2Csv()" [hidden]="validCreator != 1">
        <i class="fa fa-download"></i>
            <br>Εξαγωγή σε csv
        </button>
129
        <button type="button" class="alert alert-info pull-left" (click)="createDiagram()" [hidden]="validCreator != 1 || schSelected == 0 || (reportId != 2 ) ">
130 131 132 133 134 135 136
        <i class="fa fa-bar-chart"></i>
            Διάγραμμα
        </button>

        <div class="d3-chart" *ngIf = "validCreator == 1" #chart>
        </div>
        <br><br><br><br><br>
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

    </div>

   `
})

@Injectable() export default class ReportAllStat implements OnInit, OnDestroy {

    public formGroup: FormGroup;
    loginInfo$: BehaviorSubject<ILoginInfo>;
    loginInfoSub: Subscription;
    private generalReport$: BehaviorSubject<any>;
    private RegionSelections$: BehaviorSubject<any>;
    private AdminAreaSelections$: BehaviorSubject<any>;
    private SchoolSelections$: BehaviorSubject<any>;
152 153
    private SectorSelections$: BehaviorSubject<any>;
    private CourseSelections$: BehaviorSubject<any>;
154
    private RegionRetrieve$: BehaviorSubject<any>;
155 156 157 158
    private generalReportSub: Subscription;
    private RegionSelectionsSub: Subscription;
    private AdminAreaSelectionsSub: Subscription;
    private SchoolSelectionsSub: Subscription;
159 160
    private SectorSelectionsSub: Subscription;
    private CourseSelectionsSub: Subscription;
161
    private RegionRetrieveSub: Subscription;
162 163 164 165 166 167 168 169 170
    private apiEndPoint = API_ENDPOINT;
    private minedu_userName: string;
    private minedu_userPassword: string;
    private distStatus = "READY";
    private data;
    private validCreator: number;
    private reportId: number;
    private source: LocalDataSource;
    private showAdminList: BehaviorSubject<boolean>;
171 172
    private showSectorList: BehaviorSubject<boolean>;
    private showCourseList: BehaviorSubject<boolean>;
173
    private regionSelected: number;
174 175
    private adminAreaSelected: number;
    private schSelected: number;
176 177 178
    private classSelected: number;
    private sectorSelected: number;
    private courseSelected: number;
179
    private distribFinalized: number;
180
    private enableRegionFilter: boolean;
181
    private enableCourseFilter: boolean;
182
    private userLoggedIn: string;
183

184
    columnMap: Map<string, TableColumn> = new Map<string, TableColumn>();
185
    @Input() settings: any;
186
    private reportSchema = new reportsSchema();
187 188 189 190

    //csvObj:csvCreator ;
    private csvObj = new csvCreator();

191 192 193 194 195 196
    private createGraph: boolean;
    //d3 creator
    private chartObj = new chartCreator();
    @ViewChild('chart') public chartContainer: ElementRef;
    private d3data: Array<any>;

197 198 199 200 201 202 203 204 205
    //private repid: number;
    private routerSub: any;

    constructor(private fb: FormBuilder,
        private _ngRedux: NgRedux<IAppState>,
        private _hds: HelperDataService,
        private activatedRoute: ActivatedRoute,
        private router: Router) {

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        this.formGroup = this.fb.group({
            region: ['', []],
            adminarea: ['', []],
            schoollist: ['', []],
            classid: ['', []],
            sector: ['', []],
            course: ['', []],
        });

        this.loginInfo$ = new BehaviorSubject(LOGININFO_INITIAL_STATE);
        this.generalReport$ = new BehaviorSubject([{}]);
        this.RegionSelections$ = new BehaviorSubject([{}]);
        this.AdminAreaSelections$ = new BehaviorSubject([{}]);
        this.SchoolSelections$ = new BehaviorSubject([{}]);
        this.SectorSelections$ = new BehaviorSubject([{}]);
        this.CourseSelections$ = new BehaviorSubject([{}]);
        this.RegionRetrieve$ = new BehaviorSubject([{}]);
        this.minedu_userName = '';
        this.validCreator = -1;
        this.showAdminList = new BehaviorSubject(false);
        this.showSectorList = new BehaviorSubject(false);
        this.showCourseList = new BehaviorSubject(false);
        this.regionSelected = 0;
        this.adminAreaSelected = 0;
        this.schSelected = 0;
        this.classSelected = 0;
        this.sectorSelected = 0;
        this.courseSelected = 0;
        this.enableRegionFilter = false;
        this.enableCourseFilter = false;
        this.createGraph = false;
237 238 239 240 241

    }

    ngOnInit() {

242 243 244 245 246
        this.loginInfoSub = this._ngRedux.select(state => {
            if (state.loginInfo.size > 0) {
                state.loginInfo.reduce(({}, loginInfoToken) => {
                    this.minedu_userName = loginInfoToken.minedu_username;
                    this.minedu_userPassword = loginInfoToken.minedu_userpassword;
247

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
                    this.userLoggedIn = loginInfoToken.auth_role;
                    if (loginInfoToken.auth_role == PDE_ROLE || loginInfoToken.auth_role == DIDE_ROLE) {
                        let regId = -1;
                        this.minedu_userName = loginInfoToken.auth_token;
                        this.minedu_userPassword = loginInfoToken.auth_token;
                        if (loginInfoToken.auth_role == PDE_ROLE || loginInfoToken.auth_role == DIDE_ROLE) {

                            this.RegionRetrieveSub = this._hds.getUserRegistryNo(this.minedu_userName, this.minedu_userPassword).subscribe(data => {
                                this.RegionRetrieve$.next(data);
                                this.data = data;
                                regId = this.data['id'];

                                if (loginInfoToken.auth_role == PDE_ROLE) {
                                    this.regionSelected = regId;
                                    this.showAdminList.next(true);

                                    this.checkregion(this.regionSelected);
                                }
                                else if (loginInfoToken.auth_role == DIDE_ROLE) {
                                    this.adminAreaSelected = regId;
                                    this.showAdminList.next(false);
                                    this.checkadminarea(this.adminAreaSelected);
                                }
                            },
                                error => {
                                    this.RegionRetrieve$.next([{}]);
                                });
                        }
                    }
                    return loginInfoToken;
                }, {});
            }
            return state.loginInfo;
        }).subscribe(this.loginInfo$);

        this.routerSub = this.activatedRoute.params.subscribe(params => {
            this.reportId = +params['reportId'];
        });


        this.showFilters();
289 290 291 292 293

    }

    ngOnDestroy() {

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
        if (this.loginInfoSub)
            this.loginInfoSub.unsubscribe();
        if (this.generalReportSub)
            this.generalReportSub.unsubscribe();
        if (this.RegionSelectionsSub)
            this.RegionSelectionsSub.unsubscribe();
        if (this.AdminAreaSelectionsSub)
            this.AdminAreaSelectionsSub.unsubscribe();
        if (this.SchoolSelectionsSub)
            this.SchoolSelectionsSub.unsubscribe();
        if (this.SectorSelectionsSub)
            this.SectorSelectionsSub.unsubscribe();
        if (this.CourseSelectionsSub)
            this.CourseSelectionsSub.unsubscribe();

        if (this.loginInfo$)
310
            this.loginInfo$.unsubscribe();
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
        if (this.generalReport$)
            this.generalReport$.unsubscribe();
        if (this.RegionSelections$)
            this.RegionSelections$.unsubscribe();
        if (this.AdminAreaSelections$)
            this.AdminAreaSelections$.unsubscribe();
        if (this.SchoolSelections$)
            this.SchoolSelections$.unsubscribe();
        if (this.SectorSelections$)
            this.SectorSelections$.unsubscribe();
        if (this.CourseSelections$)
            this.CourseSelections$.unsubscribe();
        if (this.showAdminList)
            this.showAdminList.unsubscribe();
        if (this.showSectorList)
            this.showSectorList.unsubscribe();
        if (this.showCourseList)
            this.showCourseList.unsubscribe();
        if (this.RegionRetrieveSub)
            this.RegionRetrieveSub.unsubscribe();
        if (this.RegionRetrieve$)
            this.RegionRetrieve$.unsubscribe();
333 334 335 336

    }


337
    createReport(regionSel) {
338

339 340
        this.validCreator = 0;
        this.createGraph = false;
341

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
        let route;
        if (this.reportId === 1) {
            route = "/ministry/general-report/";
            this.settings = this.reportSchema.genReportSchema;
        }
        else if (this.reportId === 2) {
            route = "/ministry/report-completeness/";
            this.settings = this.reportSchema.reportCompletenessSchema;
        }
        else if (this.reportId === 3) {
            route = "/ministry/report-all-stat/";
            this.distribFinalized = 1;
            this.settings = this.reportSchema.reportAllStatSchema;
        }
        else if (this.reportId === 5) {
            route = "/ministry/report-all-stat/";
            this.distribFinalized = 0;
            this.settings = this.reportSchema.reportAllStatSchema;
        }

        let regSel = 0, admSel = 0, schSel = 0;
        if (this.enableRegionFilter) {
            //if (regionSel.value != 0)
            //regSel = regionSel.value;
            regSel = this.regionSelected;
            admSel = this.adminAreaSelected;
            schSel = this.schSelected;
        }
370

371 372 373 374 375 376
        let clSel = 0, secSel = 0, courSel = 0;
        if (this.enableCourseFilter) {
            clSel = this.classSelected;
            secSel = this.sectorSelected;
            courSel = this.courseSelected;
        }
377

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
        if (this.userLoggedIn == PDE_ROLE) {
            regSel = this.regionSelected;
        }
        else if (this.userLoggedIn == DIDE_ROLE)
            admSel = this.adminAreaSelected;

        this.generalReportSub = this._hds.makeReport(this.minedu_userName, this.minedu_userPassword, route, regSel, admSel, schSel, clSel, secSel, courSel, this.distribFinalized).subscribe(data => {
            this.generalReport$.next(data);
            this.data = data;

            for (let i = 0; i < this.data.length; i++) {
                this.data[i].num = Number(data[i].num);
                this.data[i].percentage = Number(data[i].percentage);

                this.data[i].percTotal = Number(data[i].percTotal);
                this.data[i].percA = Number(data[i].percA);
                this.data[i].percB = Number(data[i].percB);
                this.data[i].percC = Number(data[i].percC);
            }
            this.validCreator = 1;
            this.source = new LocalDataSource(this.data);
            this.columnMap = new Map<string, TableColumn>();

            //pass parametes to csv class object
            this.csvObj.columnMap = this.columnMap;
            this.csvObj.source = this.source;
            this.csvObj.settings = this.settings;
            //this.prepareColumnMap();
            this.csvObj.prepareColumnMap();
407
        },
408 409 410 411
            error => {
                this.generalReport$.next([{}]);
                console.log("Error Getting generalReport");
            });
412

413
    }
414

415 416 417
    navigateBack() {
        this.router.navigate(['/ministry/minister-reports']);
    }
418

419
    showFilters() {
420

421 422 423 424 425 426 427
        this.RegionSelectionsSub = this._hds.getRegions(this.minedu_userName, this.minedu_userPassword).subscribe(data => {
            this.RegionSelections$.next(data);
        },
            error => {
                this.RegionSelections$.next([{}]);
                console.log("Error Getting RegionSelections");
            });
428

429
    }
430

431
    toggleRegionFilter() {
432

433
        this.enableRegionFilter = !this.enableRegionFilter;
434

435
    }
436

437
    toggleCourseFilter() {
438

439 440
        this.enableCourseFilter = !this.enableCourseFilter;
    }
441

442
    checkregion(regionId) {
443

444 445 446 447
        if (typeof regionId.value != "undefined")
            this.regionSelected = regionId.value;
        this.adminAreaSelected = 0;
        this.schSelected = 0;
448

449 450 451 452 453 454 455 456
        this.AdminAreaSelectionsSub = this._hds.getAdminAreas(this.minedu_userName, this.minedu_userPassword, this.regionSelected).subscribe(data => {
            this.AdminAreaSelections$.next(data);
            this.showAdminList.next(true);
        },
            error => {
                this.AdminAreaSelections$.next([{}]);
                console.log("Error Getting AdminAreaSelections");
            });
457

458 459 460 461 462 463 464 465 466
        this.SchoolSelectionsSub = this._hds.getSchoolsPerRegion(this.minedu_userName, this.minedu_userPassword, this.regionSelected).subscribe(data => {
            this.SchoolSelections$.next(data);
            this.showAdminList.next(true);
        },
            error => {
                this.SchoolSelections$.next([{}]);
                console.log("Error Getting SchoolSelections");
            });
    }
467

468
    checkadminarea(adminId) {
469

470
        this.schSelected = 0;
471

472 473 474 475
        if (typeof adminId.value != "undefined")
            this.adminAreaSelected = adminId.value;
        this.SchoolSelectionsSub = this._hds.getSchoolsPerAdminArea(this.minedu_userName, this.minedu_userPassword, this.adminAreaSelected).subscribe(data => {
            this.SchoolSelections$.next(data);
476
        },
477 478 479 480 481 482 483 484
            error => {
                this.SchoolSelections$.next([{}]);
                console.log("Error Getting SchoolSelections");
            },
            () => {
                console.log("Success Getting SchoolSelectionsSub");
                this.showAdminList.next(true);
            }
485

486 487
        );
    }
488

489
    checkschool(schId) {
490

491
        this.schSelected = schId.value;
492

493
    }
494

495
    checkclass(classId) {
496

497 498 499
        this.classSelected = classId.value;
        this.sectorSelected = 0;
        this.courseSelected = 0;
500

501 502 503 504 505 506 507 508 509 510
        if (this.classSelected == 2 || this.classSelected == 3 || this.classSelected == 4) {
            this.SectorSelectionsSub = this._hds.getSectors(this.minedu_userName, this.minedu_userPassword, this.classSelected).subscribe(data => {
                this.SectorSelections$.next(data);
                this.showSectorList.next(true);
            },
                error => {
                    this.SectorSelections$.next([{}]);
                    console.log("Error Getting SectorSelections");
                });
        } //end if
511 512


513
    }
514

515
    checksector(sectorId) {
516

517 518
        this.courseSelected = 0;
        this.sectorSelected = sectorId.value;
519

520 521
        this.CourseSelectionsSub = this._hds.getCourses(this.minedu_userName, this.minedu_userPassword, this.sectorSelected).subscribe(data => {
            this.CourseSelections$.next(data);
522
            this.showCourseList.next(true);
523 524 525 526 527 528
        },
            error => {
                this.CourseSelections$.next([{}]);
                console.log("Error Getting CourseSelections");
            });
    }
529

530
    checkcourse(courseId) {
531

532
        this.courseSelected = courseId.value;
533

534
    }
535 536 537



538
    onSearch(query: string = '') {
539

540 541
        this.csvObj.onSearch(query);
    }
542

543
    export2Csv() {
544

545
        this.csvObj.export2Csv();
546

547
    }
548 549


550 551 552 553 554 555 556 557 558 559
    createDiagram() {
        if (!this.createGraph) {
            this.generateGraphData();
            this.chartObj.d3data = this.d3data;
            this.chartObj.chartContainer = this.chartContainer;
            this.chartObj.createChart();
            this.chartObj.updateChart();
            this.createGraph = true;
        }
    }
560

561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
    generateGraphData() {

        this.d3data = [];

        if (this.reportId === 2) {
            let labelsX = [];
            labelsX.push("Σχολείο");
            labelsX.push("Α\' τάξη");
            labelsX.push("Β\' τάξη");
            labelsX.push("Γ\' τάξη");
            labelsX.push("Δ\' τάξη");

            this.d3data.push([
                labelsX[0],
                this.data[0].percTotal,
            ]);
            this.d3data.push([
                labelsX[1],
                this.data[0].percA,
            ]);
            this.d3data.push([
                labelsX[2],
                this.data[0].percB,
            ]);
            this.d3data.push([
                labelsX[3],
                this.data[0].percC,
            ]);
            this.d3data.push([
                labelsX[4],
                this.data[0].percD,
            ]);
593

594
        }
595

596
    }
597 598 599 600 601 602 603






}