-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoodle.py
630 lines (492 loc) · 20.5 KB
/
Moodle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
237
238
239
240
241
242
243
244
245
246
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
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
370
371
372
373
374
375
376
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import pyautogui
import pyperclip
from time import sleep
from numpy import std, mean
import numpy as np
import re
import random
import argparse
import datetime
import os
import shutil
from credentials import creds
from Aux import identify_outliers, typeText, saveToGSheets, progressBar
import Classcraft
FILENAME = []
DEFECTIVETHRESHOLD = 10
DRIVER = None
class Submission():
"""Class describing an instance of submitted work."""
def __init__(self, student: str, address: str = None, markOut: int = None, markIn: int = None):
self.address = address
self.markIn = markIn
self.markInOld = None
self.markOut = markOut
if markOut:
if markIn:
self.markSum = markIn + markOut
else:
self.markSum = markOut
else:
self.markSum = None
self.student = student
self.marks = {}
self.givenMarks = {}
self.markMean = 0
self.markStdDev = 0
self.defective = False
self.slacker = False
self.course = None
self.courseAcr = None
self.assignment = None
def calculateForOutliers(self):
try:
# self.markMean = mean([v['mark']
# for v in self.marks.values() if v['mark'] is not None])
self.markStdDev = std([v['mark']
for v in self.marks.values() if v['mark'] is not None])
except:
pass
if self.markStdDev > DEFECTIVETHRESHOLD:
self.defective = True
def calculateForSlackers(self):
if self.marks and len([k for k, v in self.givenMarks.items() if v['mark'] is None]) > len(self.givenMarks) * 0.5:
# print([k for k, v in self.givenMarks.items() if v['mark'] is None])
self.slacker = True
self.markIn = int(self.markIn * 0.7)
try:
self.markSum = self.markIn + self.markOut
except Exception as e:
# print(f"Have not calculated slack penalty for {self.student}.")
self.markSum = self.markIn
# pass
def addAssessment(self, student: str, mark: int, link: str, given: bool = False):
if given:
self.givenMarks[link] = {'student': student, 'mark': mark}
else:
self.marks[link] = {'student': student, 'mark': mark}
def prepareDataForSheets(self):
return {
'in': self.markIn,
'out': self.markOut,
'sum': self.markSum,
'received': self.marks
}
def __str__(self) -> str:
if not self.marks:
return f"{self.student} did not submit their assignment."
if not [k for k, v in self.marks.items() if v['mark'] is not None]:
return f"{self.student} submitted their assignment, but was not yet graded."
if not self.markSum:
assessmentsReceived = [
f"{v['student']} ({v['mark']})" for v in self.marks.values() if v['mark'] is not None]
assessmentsGiven = [
f"{v['student']} ({v['mark']})" for v in self.givenMarks.values() if v['mark'] is not None]
return f"{self.student}\n\twas graded by: {', '.join(assessmentsReceived) if assessmentsReceived else 'nobody'}\n\tgave grades to: {', '.join(assessmentsGiven) if assessmentsGiven else 'nobody'}"
assessmentsReceived = [
f"{v['student']} ({v['mark']})" for v in self.marks.values() if v['mark'] is not None]
assessmentsGiven = [
f"{v['student']} ({v['mark']})" for v in self.givenMarks.values() if v['mark'] is not None]
return f"{self.student} (received: {f'{self.markInOld} -> ' if self.markInOld else ''}{self.markIn}{'!' if self.slacker else ''}, given: {self.markOut}, sum: {self.markSum})\n\t{'may have been incorrectly' if self.defective else 'was'} graded by: {', '.join(assessmentsReceived) if assessmentsReceived else 'nobody'}\n\tgave grades to: {', '.join(assessmentsGiven) if assessmentsGiven else 'nobody'}"
def doMoodleLogin(usr: str, password: str):
try:
DRIVER.find_element_by_xpath(
'//*[@id="page-wrapper"]/nav/ul[2]/li[3]/div/span/a').click()
DRIVER.find_element_by_id("username").send_keys(
usr,
Keys.TAB,
password,
Keys.ENTER)
except Exception as e:
pass
def getSubmissionBasic():
rows = DRIVER.find_elements_by_xpath(
'//table[contains(@class, "grading-report")]/tbody/tr'
)
course = DRIVER.find_element_by_xpath(
'//*[@id="page-header"]//h1'
).text
courseAcr = DRIVER.find_element_by_xpath(
f'//*[@id="page-navbar"]//a[@title="{course}"]'
).text
assignment = DRIVER.find_element_by_xpath(
'//*[@id="region-main"]//h2[1]'
).text
submissions = []
total = len(rows)
for _, row in progressBar(rows, prefix = 'Getting info:', suffix = 'info on submissions collected.', length = 20):
try:
student = row.find_element_by_xpath(
'.//td[contains(@class,"participant")]//span'
).text
submissions.append(Submission(student=student))
submissions[-1].course = course
submissions[-1].courseAcr = courseAcr
submissions[-1].assignment = assignment
except:
pass
try:
received = row.find_element_by_xpath(
'./td[contains(@class, "receivedgrade")][./div[@class="assessmentdetails"]]'
)
try:
mark = int(received.find_element_by_xpath(
'.//span[@class="grade"]'
).text)
except Exception as e:
mark = None
student = received.find_element_by_xpath(
'.//span[@class="fullname"]'
).text
link = received.find_element_by_xpath(
'.//a[@class="grade"]'
).get_attribute("href")
submissions[-1].addAssessment(student, mark, link)
except Exception as e:
pass
try:
given = row.find_element_by_xpath(
'./td[contains(@class, "givengrade")][./div[@class="assessmentdetails"]]'
)
try:
mark = int(given.find_element_by_xpath(
'.//span[@class="grade"]'
).text)
except Exception as e:
mark = None
student = given.find_element_by_xpath(
'.//span[@class="fullname"]'
).text
link = given.find_element_by_xpath(
'.//a[@class="grade"]'
).get_attribute("href")
submissions[-1].addAssessment(student, mark, link, given=True)
except Exception as e:
pass
try:
link = row.find_element_by_xpath(
'.//td[contains(@class,"submission")]/a'
).get_attribute("href")
submissions[-1].address = link
except Exception as e:
continue
try:
mark = row.find_element_by_xpath(
'.//td[contains(@class, "submissiongrade")]'
)
try:
markIn = int(mark.text)
except Exception as e:
markIn = int(mark.find_element_by_xpath('./ins').text)
markInOld = int(mark.find_element_by_xpath('./del').text)
submissions[-1].markInOld = markInOld
submissions[-1].markIn = markIn
submissions[-1].markSum = markIn
except Exception as e:
# markIn = None
# markOut = None
continue
try:
markOut = int(row.find_element_by_xpath(
'.//td[contains(@class, "gradinggrade")]'
).text)
submissions[-1].markOut = markOut
submissions[-1].markSum += markOut
except Exception as e:
# markOut = None
pass
return submissions
def downloadFiles(sub: Submission):
DRIVER.get(sub.address)
link = DRIVER.find_element_by_xpath(
'//div[@class="submission-full"]//div[@class="attachments"]//a'
)
ActionChains(DRIVER).context_click(link).perform()
downloadedName = f'{sub.courseAcr}TempDl'
pyautogui.press('k', interval=0.5)
typeText(downloadedName)
pyautogui.press('enter', interval=0.5)
# shutil.move(f"~/Downloads/{sub.courseAcr} - {sub.assignment} - {sub.student}.*", dst)
# downloadedPath = os.path.join("~", 'Downloads', f"{downloadedName}.zip")
downloadedPath = os.path.join("/home", "bogdan", 'Downloads', f"{downloadedName}.zip")
sleep(3)
try:
shutil.unpack_archive(
downloadedPath,
f'{sub.courseAcr} {sub.assignment}/{sub.student}'
)
except Exception as e:
print(f'{sub.student}\'s submission not downloaded.')
os.remove(downloadedPath)
DRIVER.back()
def getSubmissionFiles(submissions: list):
# if not os.path.isdir(f'{submissions[0].courseAcr}{submissions[0].assignment}'
os.makedirs(f'{submissions[0].courseAcr} {submissions[0].assignment}', exist_ok=True)
for _, sub in progressBar(submissions, prefix = 'Downloading files:', suffix = 'files downloaded.', length = 20):
if not sub.address:
continue
downloadFiles(sub)
def repairDefective(outliers: list):
for _, k in progressBar(outliers, prefix = 'Repairing outliers:', suffix = 'outlying assessments\' weights set to 0.', length = 20):
DRIVER.get(k)
try:
Select(DRIVER.find_element_by_id("id_weight")).select_by_value("0")
DRIVER.find_element_by_id("id_feedbackreviewer_editoreditable").send_keys(
"Težinska vrijednost ocjene postavljena je na 0 zato što dodijeljena ocjena previše odskače od ostalih procjena iste zadaće."
)
except Exception as e:
pass
DRIVER.find_element_by_id("id_save").click()
def identifyDefective(subs: list, repair: bool = False):
for sub in subs:
sub.calculateForOutliers()
f = open(FILENAME[0], "a")
print(f"\nIdentifying assignments with potentially defective assessment.")
f.write(f"\n\n\nAssignments with potentially defective assessment ({len([s for s in subs if s.defective])}):\n")
defective = []
for sub in [s for s in subs if s.defective]:
f.write(f"\n{sub}")
outliers = identify_outliers([v['mark']
for v in sub.marks.values() if v['mark'] is not None])
outliers = {k: v for k, v in sub.marks.items() if v['mark'] in outliers}
for k, v in outliers.items():
f.write(f"\n\t\tSuspect: {v['student']}: {k}")
defective.append(k)
if repair:
repairDefective(defective)
DRIVER.find_element_by_id("id_submit").click()
f.close()
def suggestSample(subs: list):
f = open(FILENAME[0], "a")
# print(f"\nRandom check suggestion:")
f.write(f"\n\n\nRandom sample:\n")
suggestion = random.sample(
[sub.student for sub in subs if sub.marks],
int(len(subs) * 0.15)
)
# print("\n".join(suggestion))
f.write("\n".join(suggestion))
f.close()
def getTopMarks(subs: list) -> list:
subs = [sub for sub in subs if sub.marks]
f = open(FILENAME[0], "a")
print(f"\nCalculating top 10%.")
f.write(f"\n\n\nTop 10%:\n")
top = subs[:int(len(subs) * 0.1 if len(subs) * 0.1 > 1 else 1)]
f.write("\n".join([sub.__str__() for sub in top]))
f.close()
return top
def getLowestMarks(subs: list) -> list:
subs = [sub for sub in subs if sub.marks]
f = open(FILENAME[0], "a")
print(f"\nCalculating bottom 10%.")
f.write(f"\n\n\nBottom 10%:\n")
btm = subs[int(len(subs) * 0.9):]
f.write("\n".join([sub.__str__() for sub in btm]))
f.close()
return btm
def getWorkshopLink(courseLink: str):
DRIVER.get(courseLink)
try:
doMoodleLogin(creds['user'], creds['password'])
except Exception as e:
print(e)
pass
return DRIVER.find_element_by_xpath(
'//a[text()="Laboratorijske vježbe"]/../../..//li[contains(@class, "activity workshop")][last()]//a'
).get_attribute("href")
def showPlenty():
e = DRIVER.find_element_by_xpath(
'//select[@class="custom-select singleselect"][@name="perpage"]'
)
try:
Select(e).select_by_value("300")
except Exception as e:
pass
def saveSubmissionsInfo(submissions: list, initial: bool = True):
courseAcr = submissions[0].courseAcr
assignment = submissions[0].assignment
print(f"\n{'-'*13}\nReport for: {courseAcr} - {assignment} - N: {len(submissions)}\n{'-'*13}")
if not len(FILENAME):
FILENAME.append(f"{courseAcr} - {assignment} - {datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}.txt")
if initial:
f = open(FILENAME[0], "w")
f.write(f"{courseAcr} - {assignment} - N: {len(submissions)}\n\n\n")
else:
f = open(FILENAME[0], "a")
f.write(f"\n\n\n{'-'*13}\nRevised assessments\n\n\n")
for sub in submissions:
f.write(f"\n{sub}")
f.close()
def penaliseSlackers(slackers: dict):
for k, v in slackers.items():
DRIVER.get(k)
try:
Select(DRIVER.find_element_by_id("id_gradeover")).select_by_value(f"{v['mark']}")
DRIVER.find_element_by_id("id_feedbackauthor_editoreditable").send_keys(
"Primijenjena penalizacija (nova ocjena = izvorna ocjena * 0.7) zato što nije napravljena procjena za više od dva rada."
)
except Exception as e:
pass
DRIVER.find_element_by_id("id_save").click()
def identifySlackers(subs: list, repair: bool = False):
for sub in subs:
sub.calculateForSlackers()
f = open(FILENAME[0], "a")
print(f"\nIdentifying slackers.")
f.write(f"\n\n\nStudents who submitted their assignments, and received grades, but assessed none:\n")
slackers = {s.address: {'mark': s.markIn, 'student': s.student} for s in subs if s.slacker}
f.write('\n'.join([s['student'] for s in slackers.values()]))
if repair:
penaliseSlackers(slackers)
f.close()
def scoreClasscraft(submissions: list, feedback: dict):
f = open(FILENAME[0], "a")
f.write(f"\n\n\n{'-'*13}\nClasscraft Input\n\n\n")
f.write(f"\n{feedback}\n")
f.close()
Classcraft.DRIVER = DRIVER
Classcraft.main(
course=submissions[0].course,
feedback=feedback
)
def main(link: str, identify: bool = False, suggest: bool = False, scoring: bool = False, classcraft: bool = False, download: bool = False, gSheets: bool=False, repair: bool=False, slackers: bool=False):
print(f"{'#'*13}\nNow performing on {link}\n{'#'*13}")
DRIVER.get(link)
try:
doMoodleLogin(creds['user'], creds['password'])
except Exception as e:
print(e)
pass
showPlenty()
submissions = getSubmissionBasic()
saveSubmissionsInfo(submissions)
if download:
getSubmissionFiles(submissions)
if identify:
# identify those submissions where there are outliers, and repair if identified
identifyDefective(submissions, repair=repair)
if repair:
submissions = []
# get the new state of submissions
submissions = getSubmissionBasic()
saveSubmissionsInfo(submissions, initial=False)
if slackers:
identifySlackers(submissions, repair=repair)
if suggest:
suggestSample(submissions)
if scoring:
forScoring = sorted([s for s in submissions if s.markIn is not None], key=lambda x: x.markIn, reverse=True)
top = getTopMarks(forScoring)
btm = getLowestMarks(forScoring)
if classcraft:
feedback = {
sub.student: {
'positive': True,
'behaviour': 1
} for sub in top
}
feedback.update({
sub.student: {
'positive': False,
'behaviour': 3
} for sub in btm
})
scoreClasscraft(submissions, feedback)
if gSheets:
saveToGSheets(
{
sub.student: sub.prepareDataForSheets() for sub in submissions
},
course=submissions[0].courseAcr,
assignment=submissions[0].assignment)
FILENAME = []
submissions = []
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="A script for opening ELF in a browser and collecting and providing feedback on workshops.\n\nE.g.:\n- Moodle.py -w link_to_workshop [ --options ]\n- Moodle.py -c link_to_course [ --options ]"
)
parser.add_argument("-c", "--courses", type=str, nargs="*",
default=None, help="A list of link(s) of the course(s)")
parser.add_argument("-w", "--workshops", type=str, nargs="*",
default=None, help="A list of link(s) to specific workshop(s)")
parser.add_argument("--select", dest="select", action="store_true",
help="A random subset of N * 0.15 assignments will be selected")
parser.add_argument("--dl", dest="download", action="store_true",
help="Download assignment attachments")
parser.add_argument("--analyse-all", dest="analyse", action="store_true",
help="Set --outliers and --slackers")
parser.add_argument("--outliers", dest="outliers", action="store_true",
help="Outlying assessments will be identified")
parser.add_argument("--slackers", dest="slackers", action="store_true",
help="Students who received grades, but gave no grades, will be identified; requires --outliers")
parser.add_argument("--repair", dest="repair", action="store_true",
help="Repair outlying assessments by giving them weight=0 and slackers by giving them received grade * 0.7; requires --outliers or --slackers")
parser.add_argument("--scores-full", dest="scoresFull", action="store_true",
help="Set --scores and --ccraft and --gsheets")
parser.add_argument("--scores", dest="scores", action="store_true",
help="Top N * 0.1 and bottom N * 0.1 assignments will be given")
parser.add_argument("--ccraft", dest="ccraft", action="store_true",
help="Top and bottom scores will be acknowledged in Classcraft")
parser.add_argument("--gsheets", dest="gSheets", action="store_true",
help="Store grades to GSheets")
parser.add_argument("--gui", dest="gui", action="store_true",
help="Show GUI. Cancels --dl if set")
parser.set_defaults(
select=False,
download=False,
outliers=False,
slackers=False,
repair=False,
scores=False,
ccraft=False,
gSheets=False,
gui=False,
analyse=False,
scoresFull=False,
)
args = parser.parse_args()
if args.analyse:
args.outliers = True
args.slackers = True
if args.scoresFull:
args.scores = True
args.ccraft = True
args.gSheets = True
opt = Options()
# opt.binary_location = "/opt/vivaldi/vivaldi"
if not args.gui:
opt.add_argument("--headless")
args.download = False
opt.add_argument("--incognito")
DRIVER = webdriver.Chrome(options=opt, executable_path="./chromedriver")
if args.workshops:
for w in args.workshops:
main(w,
suggest=args.select,
download=args.download,
identify=args.outliers,
slackers=args.slackers,
repair=args.repair,
scoring=args.scores,
classcraft=args.ccraft,
gSheets=args.gSheets)
else:
for c in args.courses:
link = getWorkshopLink(c)
main(link,
suggest=args.select,
download=args.download,
identify=args.outliers,
slackers=args.slackers,
repair=args.repair,
scoring=args.scores,
classcraft=args.ccraft,
gSheets=args.gSheets)
DRIVER.quit()