]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/course-reserves/course-associate-users.component.ts
LP1849212 Associtate and Disassociate Course With Instructors
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / course-reserves / course-associate-users.component.ts
1 import {Component, Input, ViewChild, OnInit, TemplateRef} from '@angular/core';\r
2 import {Observable, Observer, of} from 'rxjs';\r
3 import {DialogComponent} from '@eg/share/dialog/dialog.component';\r
4 import {AuthService} from '@eg/core/auth.service';\r
5 import {NetService} from '@eg/core/net.service';\r
6 import {EventService} from '@eg/core/event.service';\r
7 import {OrgService} from '@eg/core/org.service';\r
8 import {PcrudService} from '@eg/core/pcrud.service';\r
9 import {Pager} from '@eg/share/util/pager';\r
10 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';\r
11 import {GridDataSource} from '@eg/share/grid/grid';\r
12 import {GridComponent} from '@eg/share/grid/grid.component';\r
13 import {IdlObject, IdlService} from '@eg/core/idl.service';\r
14 import {StringComponent} from '@eg/share/string/string.component';\r
15 import {ToastService} from '@eg/share/toast/toast.service';\r
16 import {CourseService} from '@eg/staff/share/course.service';\r
17 \r
18 @Component({\r
19     selector: 'eg-course-associate-users-dialog',\r
20     templateUrl: './course-associate-users.component.html'\r
21 })\r
22 \r
23 export class CourseAssociateUsersComponent extends DialogComponent {\r
24 \r
25     @ViewChild('usersGrid', {static: true}) usersGrid: GridComponent;\r
26     @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;\r
27     @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;\r
28     @ViewChild('successString', { static: true }) successString: StringComponent;\r
29     @ViewChild('failedString', { static: true }) failedString: StringComponent;\r
30     @ViewChild('differentLibraryString', { static: true }) differentLibraryString: StringComponent;\r
31     @Input() table_name = "Course Users";\r
32     @Input() userRoleInput: String;\r
33     \r
34     idl_class = "acmcu";\r
35     new_usr:any;\r
36     currentCourse: IdlObject;\r
37     users: any[];\r
38     gridDataSource: GridDataSource;\r
39 \r
40     constructor(\r
41         private auth: AuthService,\r
42         private idl: IdlService,\r
43         private net: NetService,\r
44         private pcrud: PcrudService,\r
45         private org: OrgService,\r
46         private evt: EventService,\r
47         private modal: NgbModal,\r
48         private toast: ToastService,\r
49         private courseSvc: CourseService\r
50     ) {\r
51         super(modal);\r
52         this.gridDataSource = new GridDataSource();\r
53     }\r
54 \r
55     ngOnInit() {\r
56     }\r
57 \r
58     /**\r
59      * Takes the user id and creates a course user based around it.\r
60      * @param user_input The inputted user Id.\r
61      */\r
62     associateUsers(user_input) {\r
63         if (user_input) {\r
64             let user = this.idl.create('acmcu');\r
65             user.course(this.currentCourse.id());\r
66             user.usr(this.new_usr);\r
67             user.usr_role(user_input);\r
68             this.pcrud.create(user).subscribe(\r
69             val => {\r
70                console.debug('created: ' + val);\r
71                this.successString.current().then(str => this.toast.success(str));\r
72             }, err => {\r
73                 this.failedString.current().then(str => this.toast.danger(str));\r
74             })\r
75         }\r
76     }\r
77 \r
78     /**\r
79      * Delete a user based on the id selected from the grid.\r
80      * @param users \r
81      */\r
82     deleteSelected(users) {\r
83         let user_ids = [];\r
84         users.forEach(user => {\r
85             this.gridDataSource.data.splice(this.gridDataSource.data.indexOf(user, 0), 1);\r
86             user_ids.push(user.id())\r
87         });\r
88         this.pcrud.remove(users).subscribe(user => {\r
89             this.pcrud.autoApply(user).subscribe(\r
90                 val => {\r
91                     console.debug('deleted: ' + val);\r
92                     this.deleteSuccessString.current().then(str => this.toast.success(str));\r
93                 },\r
94                 err => {\r
95                     this.deleteFailedString.current()\r
96                         .then(str => this.toast.danger(str));\r
97                 }\r
98             );\r
99         });\r
100     }\r
101 \r
102 }