-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExamTool.py
4600 lines (3641 loc) · 175 KB
/
ExamTool.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# !/usr/bin/env python3
# coding:utf-8
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GtkSource', '3.0')
import json
from datetime import datetime
from Database.ExamDB import ExamDB
from Parser.LatexParser import LatexParser
from Parser.BibParser import BibParser
from ExamClasses.SettingsClass import Settings
from ExamClasses.ExamClass import Exam
from ExamClasses.QuestionClass import Question
from ExamClasses.AuthorClass import Author
from ExamClasses.CourseClass import Course
from ExamClasses.CourseGoalClass import ILO
from ExamClasses.DeclarationClass import Declaration
from ExamClasses.BibliographyClass import Bibliography
from ExamClasses.PreambleClasses import DocumentClass
from ExamClasses.PreambleClasses import Instructions
from ExamClasses.Profile import Profile
from ExamClasses.StudentExamGrade import StudentExamGrade
from Statistics.GenerateExamStatistics import GenerateExamStatistics
from Statistics.GenerateQuestionStatistics import GenerateQuestionStatistics
from HTMLTemplate.HTMLTemplate import HTMLTemplate
from HTMLTemplate.JSON_gen import JSON_gen
from ExamClasses.PackageClass import Package
from ExamClasses.DefPackageClass import PreamblePackages
from Parser.ExamResultTemplate import ExamResultTemplate
from Parser.ExamResultParser import ExamResultParser
from GUI.Messagedialogue import MessageDialogWindow
from GUI.GuiHandlers import Handler
from QuestionGenerators.QuestionGeneratorByGoal import GenerateQuestionsByGoal
import io
import os
import subprocess
import re
import random
from fnmatch import fnmatch
# noinspection PyUnresolvedReferences
from gi.repository import Gtk, GtkSource, GObject, Gdk, GLib
import webbrowser
__author__ = "Lennart Franked"
__email__ = "[email protected]"
__version__ = "0.8"
"""
"""
# noinspection PyAttributeOutsideInit,PyUnusedLocal
class Gui:
def __init__(self):
# Init GUI
GObject.type_register(GtkSource.View)
self.ExamDB = ExamDB()
# Load glade-file
self.builder = Gtk.Builder()
self.builder.add_from_file("GUI/ExamGUIGlade.glade")
self.builder.connect_signals(Handler(self))
self._retrieve_objects()
self._set_globals()
self._load_config()
self._connected_to_database = False
# Connect to database
self._connected_to_database = self._db_settings_exist()
# Setup syntax highlighting for text viewer
self._setup_syntax_highlighting()
self.new_exam()
self.window = self.builder.get_object("MainWindow")
# Create the tree views
self._create_author_list()
self._create_course_list()
self._create_ilo_list()
self._create_docclass_list()
self._create_instructions_list()
self._create_question_id_list()
self._create_question_id_in_exam_list()
self._create_declaration_list()
self._create_bibliography_list()
self._create_preamble_package_list()
self._create_package_id_list()
self._setup_profile_combo_box()
self._setup_combo_box()
self._create_not_in_same_exam_list()
self._create_old_exam_list()
self._create_quest_package_req_list()
# Retrieve default data from DB
self.get_init_data_from_db()
self.window.show_all()
Gtk.main()
def get_init_data_from_db(self):
"""
Runs at start to retrieve initial data from the database, such as
document classes, available instructions, authors, courses, profiles,
gradelimits and preambles.
"""
if self._connected_to_database:
self._get_docclass()
self._get_instructions()
self._get_authors()
self._get_courses()
self._get_profile_list()
self._get_gradelimits()
self._get_preamble_package_list()
def _set_globals(self):
"""
Run at start, setting all global parameters needed.
"""
self.Settings = None
self.Exam = None
self._exam_saved = False
self.preamble = {"Instruction": None,
"Document_Class": None,
"default_package_id": None,
}
self.edited_course_list_path = []
self.edited_ilo_path = []
self.edited_author_list_path = []
self.edited_package_list_path = []
self.edited_document_class_list_path = []
self.edited_instruction_list_path = []
self.edited_declaration_list_path = []
self.edited_bibliography_list_path = []
self.edited_not_in_same_exam_list_path = []
self.grade_entries = [
[self.fx_entry, "Fx"],
[self.pass_entry, "Pass"], [self.d_entry, "D"],
[self.c_entry, "C"], [self.b_entry, "B"],
[self.a_entry, "A"]
]
self.question_filter_data = None
self.question_drafts = set()
self.selected_question_id = []
self.loaded_question = None
self.bibliography_drafts = set()
self.selected_bibliography_id = []
self.loaded_bibliography = {"path": None,
"bibliography": None,
}
self.package_used = set()
self.package_drafts = set()
self.selected_package = []
self.preamble_package_drafts = set()
self.selected_preamble_package = []
self.loaded_preamble_package = []
self.authors_used = set()
self.selected_author = None
self.authors_selector = self.authors_view.get_selection()
self.selected_question_package_requirement = None
self.exam_to_load = None
self.selected_old_exam = None
self.selected_course = [None, None]
self._course_code_used = None
self.loaded_profile = None
def _load_config(self):
"""
Run at start. Reads the configuration file located in program path,
includes amongst other, the location to the database.
"""
self.Settings = Settings()
self.Settings.load_config()
self.select_program_path_btn.set_current_folder(self.Settings.get_program_path())
self.select_database_btn.set_filename(self.Settings.get_database_path())
return True
def _db_settings_exist(self):
"""
Run at start. Connects to the SQLite database.
"""
if os.path.isfile(self.Settings.get_database_path()):
self.dbQuery = self.ExamDB.connect_to_db(self.Settings.get_database_path())
if self.dbQuery:
return True
return False
def _setup_profile_combo_box(self):
"""
Run at start. Loads the combo box that will list available exam
profiles.
"""
_cell = Gtk.CellRendererText()
self.profile_combo_box.pack_start(_cell, True)
self.profile_combo_box.add_attribute(_cell, 'text', 2)
self.profile_combo_box.set_id_column(2)
def _get_profile_list(self):
"""
Retrieves the list of available profiles.
"""
self.profile_list.clear()
_available_profiles = self.ExamDB.get_available_profiles()
for profile in _available_profiles:
self.profile_list.append(profile)
# Check if there is a course selected.
if self.selected_course is not None:
if self.selected_course[1] is not None:
_course_code = self.selected_course[1].get_course_code()
_course_ver = self.selected_course[1].get_course_version()
_profile_name = ('%s%s' % (_course_code, _course_ver))
self.profile_combo_box.set_active_id(_profile_name)
# If not apply default.
else:
self.profile_combo_box.set_active_id('default0.0')
def save_profile(self):
"""
Takes the changes done by user and saves it to the database.
"""
if self.loaded_profile:
self.loaded_profile.set_default_packages(
self.preamble["default_package_id"])
self.loaded_profile.set_document_class(
self.preamble['Document_Class'])
self.loaded_profile.set_instructions(self.preamble["Instruction"])
_profile_exam_aids_start_iter = self.profile_exam_aids.get_start_iter()
_profile_exam_aids_end_iter = self.profile_exam_aids.get_end_iter()
self.loaded_profile.set_exam_aids(
self.profile_exam_aids.get_text(_profile_exam_aids_start_iter,
_profile_exam_aids_end_iter,
True)
)
self.loaded_profile.set_type_of_exam(
self.profile_type_of_exam_entry.get_text())
self.loaded_profile.set_time_limit(
self.profile_exam_time_entry.get_text())
self.loaded_profile.set_authors(self.profile_author_entry.get_text())
self.loaded_profile.set_language(
self.profile_language_entry.get_text())
self.loaded_profile.set_number_of_questions(
self.profile_number_of_questions_entry.get_text())
self.loaded_profile.set_ilo(
self.profile_course_goals_entry.get_text())
self.loaded_profile.set_allow_same_tags(self.allow_same_tags.get_active())
self.save_gradelimits_to_profile()
self.loaded_profile.set_connector(self.dbQuery.get_connector())
self.loaded_profile.update_database()
return None
def profile_combo_changed(self):
"""
Triggers when the profile have been changed. Will load the selected
profile.
"""
_iter = self.profile_combo_box.get_active_iter()
if _iter is not None:
course_code = self.profile_list[_iter][0]
course_version = self.profile_list[_iter][1]
self._get_profile(course_code, course_version)
self.load_profile()
def _get_profile(self, course_code, course_version):
"""
Called by profile_combo_changed(). Retrieves the profile from the
database that belongs to the current course.
:param course_code: The course code of the profile to load
:param course_version: The course version of the course of the profile
to load.
"""
# Remove previously loaded profile if there is one.
if self.loaded_profile:
del self.loaded_profile
self.loaded_profile = Profile(course_code, course_version)
if course_code:
self.loaded_profile.load_from_database(*self.ExamDB.get_profile(course_code,
course_version))
def load_profile(self):
"""
Called by profile_combo_changed() after the profile has been retrieved.
Takes the retrieved profile and changes the settings accordingly.
"""
if not self.loaded_profile:
self._get_profile(None, None)
self.set_gradelimits_from_profile()
# Setup documentclass
_iter = self._search_store(self.document_class_list,
self.loaded_profile.get_document_class())
if _iter is not None:
self.docclass_selector.select_iter(_iter)
self.document_class_view.row_activated(
self.document_class_list.get_path(_iter),
self.document_class_view.get_column(0)
)
# Instructions
_iter = self._search_store(self.instruction_list,
self.loaded_profile.get_instructions())
if _iter is not None:
self.instruction_selector.select_iter(_iter)
self.instruction_view.row_activated(
self.instruction_list.get_path(_iter),
self.instruction_view.get_column(0))
# Default Packages
_iter = self._search_store(self.preamble_package_list,
self.loaded_profile.get_default_packages())
if _iter is not None:
self.preamble_package_combo.set_active_iter(_iter)
# Examaids
if self.loaded_profile.get_exam_aids() is None:
self.profile_exam_aids.set_text('')
else:
self.profile_exam_aids.set_text(self.loaded_profile.get_exam_aids())
# Type of Exam
if self.loaded_profile.get_type_of_exam() is None:
self.profile_type_of_exam_entry.set_text('')
else:
self.profile_type_of_exam_entry.set_text(self.loaded_profile.get_type_of_exam())
# Timelimit
if self.loaded_profile.get_time_limit() is None:
self.profile_exam_time_entry.set_text('')
else:
self.profile_exam_time_entry.set_text(self.loaded_profile.get_time_limit())
# Authors
self.authors_used = set() # Reset list of authors
if self.loaded_profile.get_authors() is None:
self.profile_author_entry.set_text('')
else:
self.profile_author_entry.set_text(self.loaded_profile.get_authors())
for _a in re.split('\s|,', self.loaded_profile.get_authors()):
self.authors_used.add(_a)
# Language
if self.loaded_profile.get_language() is None:
self.profile_language_entry.set_text('')
else:
self.profile_language_entry.set_text(self.loaded_profile.get_language())
# Number of questions
if self.loaded_profile.get_number_of_questions() is None:
self.profile_number_of_questions_entry.set_text('')
else:
self.profile_number_of_questions_entry.set_text(str(self.loaded_profile.get_number_of_questions()))
# Exam date
if self.loaded_profile.get_exam_date() is None:
self.exam_date_entry.set_text(datetime.today().strftime('%Y-%m-%d'))
else:
self.exam_date_entry.set_text(self.loaded_profile.get_exam_date())
# Course goals
if self.loaded_profile.get_ilo() is not None:
for _i in self.loaded_profile.get_ilo().split(' '):
# Check to handle case where no ILO is loaded.
_ilo_id = self.ExamDB.get_ilo_by_id(_i)
if _ilo_id:
self.Exam.append_ilo((_i, _ilo_id[0][3]))
self.profile_course_goals_entry.set_text(self.loaded_profile.get_ilo())
if self.loaded_profile.get_allow_same_tags():
self.allow_same_tags.set_active(self.loaded_profile.get_allow_same_tags())
@staticmethod
def _search_store(_model, _value, _value2=None):
"""
Static method used for searching GTK stores for a certain value.
:param _model: The store that contains the searched value
:param _value: The value that is searched for
:param _value: Should this be removed???
:return: Returns the iterator to the location of that value in _model
"""
_iter = _model.get_iter_first()
while _iter:
if _model.get_value(_iter, 0) == _value:
if _value2 is not None:
if _model.get_value(_iter, 1) == _value2:
return _iter
else:
return _iter
_iter = _model.iter_next(_iter)
def _retrieve_objects(self):
"""
Retrieving all the objects used to get user input about exam
information.
"""
# ExamInformation
self.course_code_label = self.builder.get_object("course_label")
self.exam_date_entry = self.builder.get_object("exam_date_label")
self.calendar = self.builder.get_object("calendar")
self.cal_popover = self.builder.get_object("cal_popover")
self.save_dialogue = self.builder.get_object("savedialog")
self.settings_windows = self.builder.get_object("settings")
self.select_database_btn = self.builder.get_object("select_database_btn")
self.select_program_path_btn = self.builder.get_object("select_program_path_btn")
self.create_database_btn = self.builder.get_object("create_database_btn")
self.database_entry = self.builder.get_object("dbentry")
self.preamble_package_combo = self.builder.get_object(
"preamblePackageCombo")
self.course_list = self.builder.get_object("courselist")
self.select_course = self.builder.get_object("select_course_popover")
self.course_view = self.builder.get_object("courseview")
self.ilo_list = self.builder.get_object("coursegoallist")
self.select_course_goal = self.builder.get_object("selectcoursegoal")
self.ilo_tree_view = self.builder.get_object("coursegoaltreeview")
self.document_class_entry = self.builder.get_object("docclassentry")
self.select_document_class = self.builder.get_object("selectdocclass")
self.document_class_view = self.builder.get_object("docclassview")
self.document_class_list = self.builder.get_object("docclasslist")
self.author_list = self.builder.get_object("authorlist")
self.select_authors = self.builder.get_object("selectauthors")
self.authors_view = self.builder.get_object("authorsview")
self.select_instructions = (
self.builder.get_object("selectinstructions")
)
self.instruction_entry = self.builder.get_object("instructionEntry")
self.instruction_list = self.builder.get_object("instructionslist")
self.add_instruction = self.builder.get_object("addinstructions")
self.instruction_view = self.builder.get_object("instructionview")
self.properties_notebook = self.builder.get_object("propnotebook")
self.grade_limit_window = \
self.builder.get_object("gradelimitwindow")
self.grade_comment_buffer = self.builder.get_object("grade_comment_buffer")
self.grade_limit_pass_fail_switch = self.builder.get_object("grade_limit_pass_fail_switch")
self.grade_limit_in_percent_switch = self.builder.get_object("grade_limit_in_percent_switch")
self.ilo_based_grading_switch = self.builder.get_object("ilo_based_grading_switch")
self.fx_entry = self.builder.get_object("Fxentry")
self.pass_entry = self.builder.get_object("Passentry")
self.d_entry = self.builder.get_object("Dentry")
self.c_entry = self.builder.get_object("Centry")
self.b_entry = self.builder.get_object("Bentry")
self.a_entry = self.builder.get_object("Aentry")
self.exam_summary_buffer = self.builder.get_object("exam_summary_buffer")
self.manage_questions_window = self.builder.get_object("manage_questions_window")
self.manage_questions_btn = self.builder.get_object("manage_questions_btn")
self.question_id_list = self.builder.get_object("question_id_list")
self.question_id_viewer = self.builder.get_object("question_id_viewer")
self.question_id_in_exam_list = self.builder.get_object("question_id_in_exam_list")
self.question_id_in_exam_viewer = self.builder.get_object("question_id_in_exam_viewer")
self.student_answer_entry = self.builder.get_object("student_answer_entry")
self.question_average_score_entry = self.builder.get_object("question_average_score_entry")
self.question_last_used_entry = self.builder.get_object("question_last_used_entry")
self.question_viewer = self.builder.get_object("questionviewer")
self.force_edit_btn = self.builder.get_object("force_edit_btn")
self.allow_same_tags = self.builder.get_object("allow_same_tags")
self.question_package_requirement = self.builder.get_object(
"show_question_package_requirement")
self.scrolled_window = self.builder.get_object("scrolledwindow")
self.question_package_requirement_list = \
self.builder.get_object("question_package_requirement_list")
self.question_package_requirement_view = \
self.builder.get_object("question_package_requirement_view")
self.select_question_folder_window = self.builder.get_object(
"selectquestionfolderwin")
self.question_language_entry = (
self.builder.get_object("question_language_entry")
)
self.usable_check = self.builder.get_object("usablecheck")
self.question_save_draft_button = self.builder.get_object("qwsavebtn")
self.question_filter_entry = self.builder.get_object("question_filter_entry")
self.question_filter_completion = self.builder.get_object("question_filter_completion")
self.question_filter_completion_store = self.builder.get_object("question_filter_store")
self.show_declarations = self.builder.get_object("showdeclarations")
self.declaration_list = self.builder.get_object("declarationlist")
self.declaration_view = self.builder.get_object("declarationview")
self.bibliography_list = self.builder.get_object("bibliographylist")
self.show_bibliography = self.builder.get_object("showBibliography")
self.not_in_same_exam_view = self.builder.get_object("notinsameexamview")
self.not_in_same_exam_window = self.builder.get_object("notinsameexamwin")
self.not_in_same_exam_list = self.builder.get_object("notinsameexamlist")
self.confirmation_dialogue = self.builder.get_object("confirmdia")
self.load_exam_window = self.builder.get_object("loadExamWindow")
self.old_exam_viewer = self.builder.get_object("oldexamviewer")
self.old_exam_scrolled_window = self.builder.get_object("old_exam_scrolled_window")
self.bibliography_editor_window = (
self.builder.get_object("bibliographyeditorwin")
)
self.bibliography_list = self.builder.get_object("bibliographylist")
self.bibliography_id_viewer = self.builder.get_object("bibIDViewer")
self.package_id_view = self.builder.get_object("packageIDView")
self.package_manager_window = self.builder.get_object(
"packageManagerWindow")
self.preamble_package_viewer = self.builder.get_object(
"preamblePackageViewer")
self.preamble_package_list = self.builder.get_object(
"preamblepackagelist")
self.package_list = self.builder.get_object("packagelist")
self.select_file_dialogue = self.builder.get_object("selectFileDialogue")
self.profile_combo_box = self.builder.get_object("profileComboBox")
self.profile_list = self.builder.get_object("profileList")
self.profile_author_entry = self.builder.get_object("profileAuthorEntry")
self.profile_course_goals_entry = (
self.builder.get_object("profileILOEntry")
)
self.profile_exam_aids = self.builder.get_object(
"exam_aids_buffer")
self.profile_type_of_exam_entry = self.builder.get_object(
"profileTypeOfExamEntry")
self.profile_exam_time_entry = self.builder.get_object(
"profileExamTimeEntry")
self.profile_language_entry = self.builder.get_object(
"profileLanguageEntry")
self.profile_number_of_questions_entry = self.builder.get_object(
"profileNumQuestionsEntry")
self.generate_reports_window = self.builder.get_object("generate_reports_window")
def _setup_syntax_highlighting(self):
"""
Setup syntax highlighing. Start by creating a GTK source buffer to store
the LaTeX-code. Next create a language manager that defines what
language will be stored in the source buffer. Define language to 'latex'
and connect this language manager to the source buffer.
Finally get the source viewer from glade and connect to source buffer to
it.
"""
self.buffer_latex = GtkSource.Buffer()
self.language_manager = GtkSource.LanguageManager()
self.buffer_latex.set_language(self.language_manager.get_language('latex'))
self.exam_viewer = self.builder.get_object("examviewer")
self.exam_viewer.set_buffer(self.buffer_latex)
self.buffer_question = GtkSource.Buffer()
self.buffer_question.connect('begin-user-action',
self.question_has_been_modified)
self.buffer_question.set_language(
self.language_manager.get_language('latex'))
self.question_viewer.set_buffer(self.buffer_question)
self.buffer_bibliography = GtkSource.Buffer()
self.buffer_bibliography.connect('begin-user-action',
self.bibliography_has_been_modified)
self.buffer_bibliography.set_language(
self.language_manager.get_language('bibtex'))
self.bibliography_viewer = self.builder.get_object("bibliographyviewer")
self.bibliography_viewer.set_buffer(self.buffer_bibliography)
def new_exam(self):
"""
Empty all the variables and clear the user input.
"""
if not self._connected_to_database:
return False
if self.Exam:
del self.Exam # Remove old exam-object
if self.selected_course:
self.selected_course = None
self._exam_saved = False
self.Exam = Exam()
self.Exam.set_connector(self.dbQuery.get_connector())
self.course_code_label.set_text("Course")
self.profile_type_of_exam_entry.set_text("")
self.profile_author_entry.set_text("")
self.exam_date_entry.set_text("Exam date")
self.profile_language_entry.set_text("")
self.profile_exam_time_entry.set_text("")
self.profile_exam_aids.set_text("")
self.profile_number_of_questions_entry.set_text(str(0))
self.profile_course_goals_entry.set_text("")
self.buffer_latex.set_text("")
self.profile_combo_box.set_active_id("default0.0")
self.reset_gradelimits()
self.exam_summary_buffer.set_text("")
return True
def _course_loaded(self):
if self.course_code_label.get_text():
for r in self.course_list: # Search to see if course code exist
if r[0] == self.course_code_label.get_text():
return True
else:
print("Please enter course code")
return False
def reveal(self):
"""
This method reveals or unreveals the user-input field in the GUI to
give it that lean fancy look.
"""
_arrow = self.builder.get_object("expand")
_revealer = self.builder.get_object("revealer")
_revealer.set_reveal_child(True)
if _revealer.get_child_revealed():
_revealer.set_reveal_child(False)
_arrow.set_label("<")
else:
_revealer.set_reveal_child(True)
_arrow.set_label(">")
def reveal_profile(self):
"""
This method reveals or unreveals the Profile settings §in the GUI to
give it that lean fancy look.
"""
_arrow = self.builder.get_object("expandProfile")
_revealer = self.builder.get_object("revealProfile")
_revealer.set_reveal_child(False)
if _revealer.get_child_revealed():
_revealer.set_reveal_child(False)
_arrow.set_label(">")
size = self.window.get_size()
self.window.resize(1, size[1])
else:
_revealer.set_reveal_child(True)
_arrow.set_label("<")
@staticmethod
def _cell_renderer_text_generator(treeview, columns, function=None):
"""
Generates the list for treeview. Input for the function is the treeview
followed by a list of list that should be according to the following
format:
[ID, isEditable, isSortable, isVisible] Finally the function that should be
called when column is edited.
"""
_position = 0
for _column in columns:
cell = {"ID": _column[0],
"Editable": _column[1],
"Sortable": _column[2],
"Visible": _column[3]
}
_text_renderer = Gtk.CellRendererText()
_column_text = Gtk.TreeViewColumn(
cell["ID"], _text_renderer, text=_position)
if cell["Sortable"]:
_column_text.set_sort_column_id(0)
_column_text.set_visible(cell["Visible"])
_text_renderer.props.wrap_width = 500
_text_renderer.props.wrap_mode = Gtk.WrapMode.WORD
_text_renderer.set_property("editable", cell["Editable"])
if function:
_text_renderer.connect("edited", function, _position)
treeview.append_column(_column_text)
_position += 1
return True
@staticmethod
def compare(model, row1, row2, __data=None):
# Custom sort function to handle issue with lexikograpgical sorting.
# Sorts based on trailing integers in a string.
relabel = re.compile(r'([0-9]*$)')
sort_column, _ = model.get_sort_column_id()
value1 = model.get_value(row1, sort_column)
sortvalue1 = relabel.findall(value1)[0]
# Fix TypeError if value becomes '' by setting it to 0.
if sortvalue1 == '':
sortvalue1 = 0
value2 = model.get_value(row2, sort_column)
sortvalue2 = relabel.findall(value2)[0]
if sortvalue2 == '':
sortvalue2 = 0
if int(sortvalue1) < int(sortvalue2):
return -1
elif sortvalue1 == sortvalue2:
return 0
else:
return 1
@staticmethod
def compare_date(model, row1, row2, __data=None):
"""
Comparison func to sort old exams based on date. Latest exams should be displayed first.
:param model: Treemodel
:param row1: First row to compare
:param row2: Second row to compare
:param __data: User data
:return: -1 if row1 > row2, 0 if row1 = row2, 1 if row1 < row2
"""
_val1 = model.get_value(row1, 2)
_val2 = model.get_value(row2, 2)
if _val1 == _val2:
return 0
if _val1 > _val2:
return -1
else:
return 1
def on_list_key_press(self, widget, event):
_messagedia = MessageDialogWindow()
_keyname = Gdk.keyval_name(event.keyval)
if _keyname:
_func = getattr(self, '%s_dialogue' % widget.get_name(), None)
if _keyname == 'Delete':
_func(_messagedia)
def save_question(self, widget, event):
# Allow the use of Ctrl-s to save question, instead of having to press button.
_keyname = Gdk.keyval_name(event.keyval)
if event.state & Gdk.ModifierType.CONTROL_MASK:
if _keyname == 's':
self.save_question_as_draft()
self.save_question_to_db()
def bibliographyeditor_dialogue(self, messagedia):
if (self.bibliography_list is None) or \
(self.loaded_bibliography["bibliography"] is None):
return
_bib_id = self.loaded_bibliography["bibliography"].get_bibliography_id()
_bib_used = self.ExamDB.bibliography_used(_bib_id)
# Check if bibliography is used in question. This might not work particularly well, if reference is used in
# multiple questions. Fix this!
if _bib_used:
messagedia.information_dialogue(
'Unable to delete %s.' % _bib_id[0],
'This bibliography is used for question %s' % _bib_used[0][1])
else:
if messagedia.confirmation_dialogue(
'Are you sure?', 'This will permanently remove '
'%s' % _bib_id):
self.loaded_bibliography["bibliography"].set_connector(self.dbQuery.get_connector())
self.loaded_bibliography["bibliography"].remove_from_database()
# Remove bib from draft.
for _b in self.bibliography_drafts.copy():
if _b.get_bibliography_id() == _bib_id:
self.bibliography_drafts.discard(_b)
self.get_bibliography()
def course_viewer_dialogue(self, messagedia):
_list = self.selected_course[0][0]
_path = self.selected_course[0][1]
if _list is None:
return
if self.selected_course[1] is None: # Only draft
_list.remove(_path)
else:
if self.selected_course[1].get_in_database():
if self.ExamDB.course_used(
self.selected_course[1].get_course_code(),
self.selected_course[1].get_course_version()):
messagedia.information_dialogue(
'Unable to delete %s.' %
self.selected_course[1].get_course_code(),
'An exam for this course exist')
else:
if messagedia.confirmation_dialogue(
'Are you sure?', 'This will permanently remove '
'%s' % (self.selected_course[1].get_course_code())):
self.selected_course[1].set_connector(
self.dbQuery.get_connector())
if self.course_code_label.get_text() == self._course_code_used:
self.course_code_label.set_text('Course')
self.selected_course[1].remove_from_database()
self._get_courses()
self.load_profile()
def authorsview_dialogue(self, messagedia):
_list = self.selected_author[0][0]
_path = self.selected_author[0][1]
if _list is None:
return
if self.selected_author[1] is None:
_list.remove(_path)
else:
if self.selected_author[1].get_in_database():
if self.ExamDB.author_used(
self.selected_author[1].get_id()):
messagedia.information_dialogue(
'Unable to delete %s.' %
self.selected_author[1].get_id(),
'This author is associated with an existing '
'ExamClass.')
else:
if messagedia.confirmation_dialogue(
'Are you sure?', 'This will permanently remove '
'entry for %s' % self.selected_author[1].get_id()):
self.selected_author[1].set_connector(
self.dbQuery.get_connector())
self.selected_author[1].remove_from_database()
self._get_authors()
def coursegoalview_dialogue(self, messagedia):
_list = self.selected_ilo[0][0]
_path = self.selected_ilo[0][1]
if _list is None:
return
if self.selected_ilo[1] is None:
_list.remove(_path)
else:
if self.selected_ilo[1].get_in_database():
if self.ExamDB.have_ilo_been_used(
self.selected_ilo[1].get_goal()):
messagedia.information_dialogue(
'Unable to delete %s.' %
self.selected_ilo[1].get_goal(),
'This author is associated with an existing'
'ExamClass.')
else:
if messagedia.confirmation_dialogue(
'Are you sure?', 'This will permanently remove'
'entry for %s' % self.selected_ilo[1].get_goal()):
self.selected_ilo[1].remove_from_database()
def docclassview_dialogue(self, messagedia):
_list = self.selected_docclass[0][0]
_path = self.selected_docclass[0][1]
if _list is None:
return
if self.selected_docclass[1] is None:
_list.remove(_path)
else:
if self.selected_docclass[1].get_in_database():
if self.ExamDB.docclass_used(
self.selected_docclass[1].get_document_class_id()):
messagedia.information_dialogue(
'Unable to delete %s.' %
self.selected_docclass[1].get_document_class_id(),
'This documentclass is associated with an existing '
'ExamClass.')
else:
if messagedia.confirmation_dialogue(
'Are you sure?', 'This will permanently remove '
'entry for %s' % self.selected_docclass[1].get_document_class_id()):
self.selected_docclass[1].remove_from_database()
_list.remove(_path)
def instructionview_dialogue(self, messagedia):
_list = self.selected_instruction[0][0]
_path = self.selected_instruction[0][1]
if _list is None:
return
if self.selected_instruction[1] is None:
_list.remove(_path)
else:
if self.selected_instruction[1].get_in_database():
if self.ExamDB.instruction_used(
self.selected_instruction[1].get_instruction_id()):
messagedia.information_dialogue(
'Unable to delete %s.' %
self.selected_instruction[1].get_instruction_id(),
'This instruction is associated with an existing ExamClass.')
else:
if messagedia.confirmation_dialogue(
'Are you sure?',
'This will permanently remove entry for %s' %
self.selected_instruction[1].get_instruction_id()
):
self.selected_instruction[1].remove_from_database()
_list.remove(_path)
def declarationview_dialogue(self, messagedia):
_list = self.selected_declaration[0][0]
_path = self.selected_declaration[0][1]
if _list is None:
return
if self.selected_declaration[1] is None:
_list.remove(_path)
else:
if self.selected_declaration[1].get_in_database():
_question = self.ExamDB.declaration_used(
self.selected_declaration[1].get_id())
if _question:
messagedia.information_dialogue(
'Unable to delete %s.' %
self.selected_declaration[1].get_id(),
'This declaration is associated with questions %s.' %