report-all-stat.ts 21.9 KB
Newer Older
1
import { NgRedux } from "@angular-redux/store";
2
import { Injectable } from "@angular/core";
3 4 5 6
import { Component, ElementRef, Input, OnDestroy, OnInit, ViewChild } from "@angular/core";
import { FormBuilder, FormGroup } from "@angular/forms";
import { ActivatedRoute, Router } from "@angular/router";
import { LocalDataSource } from "ng2-smart-table";
7
import { BehaviorSubject, Subscription } from "rxjs/Rx";
8

9
import { API_ENDPOINT } from "../../app.settings";
10 11 12 13 14 15 16 17
import { DIDE_ROLE, PDE_ROLE } from "../../constants";
import { HelperDataService } from "../../services/helper-data-service";
import { LOGININFO_INITIAL_STATE } from "../../store/logininfo/logininfo.initial-state";
import { ILoginInfoRecords } from "../../store/logininfo/logininfo.types";
import { IAppState } from "../../store/store";
import { ChartCreator } from "./chart-creator";
import { CsvCreator } from "./csv-creator";
import { ReportsSchema, TableColumn } from "./reports-schema";
18 19

@Component({
20
    selector: "report-all-stat",
21 22 23 24
    template: `

  <div>

25

26
        <div
27
          class = "loading" *ngIf="validCreator === 0" >
28 29 30 31
        </div>

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

32 33 34
          <!--<h5> >Επιλογή Φίλτρων <br><br></h5>-->
          <br>
          <button type="button" class="btn-link" (click)="toggleRegionFilter()" >
35

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

40
          </button>
41

42
          <div  class="col-md-11 offset-md-1">
43 44
                <label *ngIf = "enableRegionFilter && userLoggedIn === 'supervisor'"> Περιφερειακή Διεύθυνση </label>
                <select #regsel class="form-control" (change)="checkregion(regsel)" *ngIf = "enableRegionFilter" [value] = "regionSelected" [hidden] = "userLoggedIn !== 'supervisor'" formControlName="region">
45
                  <option value="0"></option>
46
                  <option *ngFor="let RegionSelection$  of RegionSelections$ | async; let i=index" [value] = "RegionSelection$.id"> {{RegionSelection$.name}} </option>
47 48
                </select>
          </div>
49
          <div class="col-md-11 offset-md-1">
50 51
                <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">
52
                  <option value="0"></option>
53 54 55
                  <option *ngFor="let AdminAreaSelection$  of AdminAreaSelections$ | async; let i=index" [value] = "AdminAreaSelection$.id"> {{AdminAreaSelection$.name}}</option>
                </select>
          </div>
56
          <div class="col-md-11 offset-md-1">
57 58
                <label *ngIf="(showAdminList | async) && enableRegionFilter">Σχολείο</label>
                <select #schsel class="form-control"  *ngIf="(showAdminList | async) && enableRegionFilter" (change)="checkschool(schsel)" formControlName="schoollist">
59
                  <option value="0"></option>
60 61 62
                  <option *ngFor="let SchoolSelection$  of SchoolSelections$ | async; let i=index" [value] = "SchoolSelection$.epal_id"> {{SchoolSelection$.epal_name}} </option>
                </select>
          </div>
63

64
          <div *ngIf = "reportId === 3 || reportId === 5" >
65 66 67 68 69
            <button type="button" class="btn-link" (click)="toggleCourseFilter()" >
                  > Φίλτρο Επιλογής Τομέα / Ειδικότητας
            </button>
          </div>

70 71 72 73 74 75 76
          <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>
77
                  <option value="4" >Δ' Λυκείου</option>
78 79 80
                </select>
          </div>
          <div class="col-md-11 offset-md-1">
81 82
                <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)"
83
                      (change)="checksector(secsel)" formControlName="sector">
84 85 86 87 88
                  <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">
89 90
                <label *ngIf="(showCourseList | async) && enableCourseFilter && (classSelected === 3 || classSelected === 4)">Ειδικότητα</label>
                <select #coursel class="form-control"  *ngIf="(showCourseList | async) && enableCourseFilter && (classSelected === 3 || classSelected === 4)"
91
                      (change)="checkcourse(coursel)" formControlName="course">
92 93 94 95 96
                  <option value="0"></option>
                  <option *ngFor="let CourseSelection$  of CourseSelections$ | async; let i=index" [value] = "CourseSelection$.id"> {{CourseSelection$.name}}</option>
                </select>
          </div>
          <br>
97
          <button type="submit" class="btn btn-alert"  (click)="createReport(regsel)" [hidden]="minedu_userName === ''" >
98 99 100
          <i class="fa fa-file-text"></i>
              Δημιουργία Αναφοράς
          </button>
101
          <button type="submit" class="btn btn-alert pull-right"  (click)="navigateBack()" [hidden]="minedu_userName === ''" >
102 103 104 105 106
              Επιστροφή
          </button>
          <br><br>
        </form>

107
        <div *ngIf="validCreator === 1 ">
108 109 110 111 112 113
          <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>

114
        <button type="button" class="alert alert-info pull-right" (click)="export2Csv()" [hidden]="validCreator !== 1">
115 116 117
        <i class="fa fa-download"></i>
            <br>Εξαγωγή σε csv
        </button>
118
        <button type="button" class="alert alert-info pull-left" (click)="createDiagram()" [hidden]="validCreator !== 1 || schSelected === 0 || (reportId !== 2 ) ">
119 120 121 122
        <i class="fa fa-bar-chart"></i>
            Διάγραμμα
        </button>

123
        <div class="d3-chart" *ngIf = "validCreator === 1" #chart>
124 125
        </div>
        <br><br><br><br><br>
126 127 128 129 130 131 132 133

    </div>

   `
})

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

134
    private formGroup: FormGroup;
135
    private loginInfo$: BehaviorSubject<ILoginInfoRecords>;
136
    private loginInfoSub: Subscription;
137 138 139 140
    private generalReport$: BehaviorSubject<any>;
    private RegionSelections$: BehaviorSubject<any>;
    private AdminAreaSelections$: BehaviorSubject<any>;
    private SchoolSelections$: BehaviorSubject<any>;
141 142
    private SectorSelections$: BehaviorSubject<any>;
    private CourseSelections$: BehaviorSubject<any>;
143
    private RegionRetrieve$: BehaviorSubject<any>;
144 145 146 147
    private generalReportSub: Subscription;
    private RegionSelectionsSub: Subscription;
    private AdminAreaSelectionsSub: Subscription;
    private SchoolSelectionsSub: Subscription;
148 149
    private SectorSelectionsSub: Subscription;
    private CourseSelectionsSub: Subscription;
150
    private RegionRetrieveSub: Subscription;
151 152 153 154
    private apiEndPoint = API_ENDPOINT;
    private minedu_userName: string;
    private minedu_userPassword: string;
    private distStatus = "READY";
155
    private data: any;
156 157 158 159
    private validCreator: number;
    private reportId: number;
    private source: LocalDataSource;
    private showAdminList: BehaviorSubject<boolean>;
160 161
    private showSectorList: BehaviorSubject<boolean>;
    private showCourseList: BehaviorSubject<boolean>;
162
    private regionSelected: number;
163 164
    private adminAreaSelected: number;
    private schSelected: number;
165 166 167
    private classSelected: number;
    private sectorSelected: number;
    private courseSelected: number;
168
    private distribFinalized: number;
169
    private enableRegionFilter: boolean;
170
    private enableCourseFilter: boolean;
171
    private userLoggedIn: string;
172

173
    columnMap: Map<string, TableColumn> = new Map<string, TableColumn>();
174
    @Input() settings: any;
175
    private reportSchema = new ReportsSchema();
176

177 178
    // csvObj:CsvCreator ;
    private csvObj = new CsvCreator();
179

180
    private createGraph: boolean;
181 182 183
    // d3 creator
    private chartObj = new ChartCreator();
    @ViewChild("chart") public chartContainer: ElementRef;
184 185
    private d3data: Array<any>;

186
    // private repid: number;
187 188 189 190 191 192 193 194
    private routerSub: any;

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

195
        this.formGroup = this.fb.group({
196 197 198 199 200 201
            region: ["", []],
            adminarea: ["", []],
            schoollist: ["", []],
            classid: ["", []],
            sector: ["", []],
            course: ["", []],
202 203 204 205 206 207 208 209 210 211
        });

        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([{}]);
212
        this.minedu_userName = "";
213 214 215 216 217 218 219 220 221 222 223 224 225
        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;
226 227 228 229 230

    }

    ngOnInit() {

231
        this.loginInfoSub = this._ngRedux.select("loginInfo")
232
            .map(loginInfo => <ILoginInfoRecords>loginInfo)
233 234
            .subscribe(loginInfo => {
                if (loginInfo.size > 0) {
235 236 237
                    loginInfo.reduce(({}, loginInfoObj) => {
                        this.minedu_userName = loginInfoObj.minedu_username;
                        this.minedu_userPassword = loginInfoObj.minedu_userpassword;
238

239 240
                        this.userLoggedIn = loginInfoObj.auth_role;
                        if (loginInfoObj.auth_role === PDE_ROLE || loginInfoObj.auth_role === DIDE_ROLE) {
241
                            let regId = -1;
242 243 244
                            this.minedu_userName = loginInfoObj.auth_token;
                            this.minedu_userPassword = loginInfoObj.auth_token;
                            if (loginInfoObj.auth_role === PDE_ROLE || loginInfoObj.auth_role === DIDE_ROLE) {
245 246 247 248 249 250

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

251
                                    if (loginInfoObj.auth_role === PDE_ROLE) {
252 253 254 255 256
                                        this.regionSelected = regId;
                                        this.showAdminList.next(true);

                                        this.checkregion(this.regionSelected);
                                    }
257
                                    else if (loginInfoObj.auth_role === DIDE_ROLE) {
258 259 260 261 262 263 264 265 266
                                        this.adminAreaSelected = regId;
                                        this.showAdminList.next(false);
                                        this.checkadminarea(this.adminAreaSelected);
                                    }
                                },
                                    error => {
                                        this.RegionRetrieve$.next([{}]);
                                    });
                            }
267
                        }
268
                        return loginInfoObj;
269 270 271 272
                    }, {});
                }
                this.loginInfo$.next(loginInfo);
            }, error => console.log("error selecting loginInfo"));
273 274

        this.routerSub = this.activatedRoute.params.subscribe(params => {
275
            this.reportId = +params["reportId"];
276 277 278
        });

        this.showFilters();
279 280 281 282
    }

    ngOnDestroy() {

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
        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.RegionRetrieveSub)
            this.RegionRetrieveSub.unsubscribe();
299 300
    }

301
    createReport(regionSel) {
302

303 304
        this.validCreator = 0;
        this.createGraph = false;
305

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
        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) {
328 329
            // if (regionSel.value !== 0)
            // regSel = regionSel.value;
330 331 332 333
            regSel = this.regionSelected;
            admSel = this.adminAreaSelected;
            schSel = this.schSelected;
        }
334

335 336 337 338 339 340
        let clSel = 0, secSel = 0, courSel = 0;
        if (this.enableCourseFilter) {
            clSel = this.classSelected;
            secSel = this.sectorSelected;
            courSel = this.courseSelected;
        }
341

342
        if (this.userLoggedIn === PDE_ROLE) {
343 344
            regSel = this.regionSelected;
        }
345
        else if (this.userLoggedIn === DIDE_ROLE)
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
            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>();

365
            // pass parametes to csv class object
366 367 368
            this.csvObj.columnMap = this.columnMap;
            this.csvObj.source = this.source;
            this.csvObj.settings = this.settings;
369
            // this.prepareColumnMap();
370
            this.csvObj.prepareColumnMap();
371
        },
372 373 374 375 376
            error => {
                this.generalReport$.next([{}]);
                console.log("Error Getting generalReport");
            });
    }
377

378
    navigateBack() {
379
        this.router.navigate(["/ministry/minister-reports"]);
380
    }
381

382
    showFilters() {
383

384 385 386 387 388 389 390 391
        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");
            });
    }
392

393
    toggleRegionFilter() {
394

395
        this.enableRegionFilter = !this.enableRegionFilter;
396

397
    }
398

399
    toggleCourseFilter() {
400

401 402
        this.enableCourseFilter = !this.enableCourseFilter;
    }
403

404
    checkregion(regionId) {
405

406
        if (typeof regionId.value !== "undefined")
407 408 409
            this.regionSelected = regionId.value;
        this.adminAreaSelected = 0;
        this.schSelected = 0;
410

411 412 413 414 415 416 417 418
        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");
            });
419

420 421 422 423 424 425 426 427 428
        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");
            });
    }
429

430
    checkadminarea(adminId) {
431

432
        this.schSelected = 0;
433

434
        if (typeof adminId.value !== "undefined")
435 436 437
            this.adminAreaSelected = adminId.value;
        this.SchoolSelectionsSub = this._hds.getSchoolsPerAdminArea(this.minedu_userName, this.minedu_userPassword, this.adminAreaSelected).subscribe(data => {
            this.SchoolSelections$.next(data);
438
        },
439 440 441 442 443 444 445 446
            error => {
                this.SchoolSelections$.next([{}]);
                console.log("Error Getting SchoolSelections");
            },
            () => {
                console.log("Success Getting SchoolSelectionsSub");
                this.showAdminList.next(true);
            }
447

448 449
        );
    }
450

451 452 453
    checkschool(schId) {
        this.schSelected = schId.value;
    }
454

455 456 457 458
    checkclass(classId) {
        this.classSelected = classId.value;
        this.sectorSelected = 0;
        this.courseSelected = 0;
459

460
        if (this.classSelected === 2 || this.classSelected === 3 || this.classSelected === 4) {
461 462 463 464 465 466 467 468
            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");
                });
469
        } // end if
470
    }
471

472 473 474
    checksector(sectorId) {
        this.courseSelected = 0;
        this.sectorSelected = sectorId.value;
475

476 477
        this.CourseSelectionsSub = this._hds.getCourses(this.minedu_userName, this.minedu_userPassword, this.sectorSelected).subscribe(data => {
            this.CourseSelections$.next(data);
478
            this.showCourseList.next(true);
479 480 481 482 483 484
        },
            error => {
                this.CourseSelections$.next([{}]);
                console.log("Error Getting CourseSelections");
            });
    }
485

486 487 488
    checkcourse(courseId) {
        this.courseSelected = courseId.value;
    }
489

490
    onSearch(query: string = "") {
491 492
        this.csvObj.onSearch(query);
    }
493

494 495 496
    export2Csv() {
        this.csvObj.export2Csv();
    }
497 498


499 500 501 502 503 504 505 506 507 508
    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;
        }
    }
509

510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
    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,
            ]);
        }
    }
542
}