]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/DialogManagedLVAFeedback.py
modules/TACommandAnnounceAll.py
[pub/jan/mattermost-bot.git] / modules / DialogManagedLVAFeedback.py
1 # Mattermost Bot module.
2 #  Copyright (c) 2016-2022 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 #  published under MIT-License
4
5 from inspect import cleandoc
6 import csv
7 import os
8
9
10 # pylint: disable=wrong-import-position
11 from AbstractCommand import AbstractCommand
12 class DialogManagedLVAFeedback(AbstractCommand):
13     TRIGGER = "lva-feedback"
14     CONFIG = {"display_name": "somebot-command", "auto_complete": False,
15               "auto_complete_hint": "",
16              }
17     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "preview-spams channel and asks for feedback. spam manuall!. [BOT_ADMIN]"
18
19
20     def __init__(self, team_id, datadir):
21         super().__init__(team_id)
22
23         self.datadir = datadir
24         self.channels = dict()
25
26         with open(self.datadir+'channels.csv', newline='') as csvfile:
27             self.channels = {row[0]:row for row in csv.reader(csvfile, delimiter=',', quotechar='"')}
28
29
30     def on_POST(self, request, data):
31         self._require_bot_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.)
32
33         msg = cleandoc("""
34             ## :pencil:``COVID course feedback 2.0!``:rocket:
35             The next term is going to be another distance-learning :dl: one.
36             We want your feedback in order to find out which courses need to be improved asap to achieve the excellence we are accustomed to, or better.
37             The feedback will be aggregated, anonymised and discussed with the deans of study affairs and possibly with the vice rector for teaching matter and/or the in-house lawyers.
38
39             #### :point_right: This Feedback form will run up until 2021-02-07 23:59 :point_left:
40
41             What we want to know:
42             + Feedback for this course.
43             + How did you like the modalities of this course? (Would you want it to be applied to others?)
44             + This course's modalities - describe them in a few words. (Also consider updating the info in VoWi - its a work resource for us and we use it to represent you)
45             + Feedback about this survey for :fsinf:. (This will not be shared outside of fsinf)
46
47             :information_source: *We are your legaly appointed representatives - we will never forward any personal info about who wrote what without explicit permission. But please dont make us read a list of profanities.* :)
48             """)
49
50         att = [{
51             "actions": [{"name": ":pencil: Give/edit your feedback", "integration": {"url": self.URL+"/interactive"}}]
52             }]
53
54         print(self.URL)
55         request.cmd_respond_text_chan(msg, {"attachments":att})
56
57
58     def on_POST_interactive(self, request, data):
59         #import pprint
60         #print("on_POST_interactive")
61         #pprint.pprint(data)
62
63 #        request.respond(200, {"ephemeral_text": "## ``The submission period ended. We are now evaluating the responses!`` :)"})
64
65         callback_id = "res-"+data["team_id"]+"-"+data["channel_id"]+"-"+data["user_id"]
66
67         feedback_course = self._response_load(self.datadir+callback_id, "-feedback_course.txt")
68         feedback_modus = self._response_load(self.datadir+callback_id, "-feedback_modus.txt")
69         feedback_modus_like = self._response_load(self.datadir+callback_id, "-feedback_modus_like.txt", "0")
70         feedback_fsinf = self._response_load(self.datadir+callback_id, "-feedback_fsinf.txt")
71
72         dialog = {
73             "callback_id": callback_id,
74             "title": "COVID Course feedback 2.0",
75             "submit_label":"Submit",
76             "elements":[{
77                 "display_name": "Your Comment/Feedback about the course (Max. 3000 characters)",
78                 "placeholder": "Type here. Max. 3000 characters.",
79                 "name": "feedback_course",
80                 "type": "textarea",
81                 "help_text": "We will aggregate and forward your feedback to the deans of study affairs, the vice rector for teaching and possibly others that are deemed able to fix something. You can edit/blank it later at any time.",
82                 "optional": True,
83                 "default": feedback_course
84             },{
85                 "display_name": "Describe the modalities in a few words (Max. 3000 characters)",
86                 "placeholder": "Type here. Max. 3000 characters.",
87                 "name": "feedback_modus",
88                 "type": "textarea",
89                 "help_text": "You can edit/blank it later at any time.",
90                 "optional": True,
91                 "default": feedback_modus
92             },{
93                 "display_name": "How did you like the modalities of this course? (Do you want it to be applied to others?)",
94                 "placeholder": "Type here. Max. 3000 characters.",
95                 "name": "feedback_modus_like",
96                 "type": "radio",
97                 "options": [
98                     {"text": "I loved it", "value": "2" },
99                     {"text": "I liked it", "value": "1" },
100                     {"text": "I'm indifferent/Don't want to answer (default)", "value": "0" },
101                     {"text": "I disliked it", "value": "-1" },
102                     {"text": "I hated it", "value": "-2" },
103                 ],
104                 "help_text": "You can edit it later at any time.",
105                 "optional": True,
106                 "default": feedback_modus_like
107             },{
108                 "display_name": "Feedback (about this survey) for FSInf (Max. 3000 characters)",
109                 "placeholder": "Type here. Max. 3000 characters.",
110                 "name": "feedback_fsinf",
111                 "type": "textarea",
112                 "help_text": "We will not forward this text to anyone. You can edit/blank it later at any time.",
113                 "optional": True,
114                 "default": feedback_fsinf
115             }]
116         }
117
118         request.respond(200, {})
119         self.bot.api.open_dialog(data["trigger_id"], self.URL+"/dialog", dialog)
120
121
122     def on_POST_dialog(self, request, data):
123         #import pprint
124         #print("on_POST_dialog")
125         #pprint.pprint(data)
126
127         c = self.bot.api.get_channel(data["channel_id"])
128
129         self._response_store(self.datadir+data["callback_id"], "-feedback_course.txt", data["submission"]["feedback_course"], c["name"])
130         self._response_store(self.datadir+data["callback_id"], "-feedback_modus.txt", data["submission"]["feedback_modus"], c["name"])
131         self._response_store(self.datadir+data["callback_id"], "-feedback_modus_like.txt", data["submission"]["feedback_modus_like"], c["name"])
132         self._response_store(self.datadir+data["callback_id"], "-feedback_fsinf.txt", data["submission"]["feedback_fsinf"], c["name"])
133
134         request.respond(200, {})
135
136
137     def _response_load(self, path, filename, default=""):
138         path = path+filename
139         ret = default
140         if os.path.isfile(path):
141             with open(path, "r") as f:
142                 ret = f.read()
143         return ret
144
145
146     def _response_store(self, path, filename, data, channelname=""):
147         path = path+filename
148         data = str(data).strip()
149         if data == "" or data == "None" or data == "0":
150             if os.path.isfile(path):
151                 os.remove(path)
152                 self.bot.debug_chan("``lva-feedback::"+channelname+filename+"`` result deleted.")
153         else:
154             with open(path, "w") as f:
155                 f.write(data)
156             self.bot.debug_chan("``lva-feedback::"+channelname+filename+"`` result stored.\n```\n"+data+"\n```")