2323
2424# select acid, sid from code as T where timestamp = (select max(timestamp) from code where sid=T.sid and acid=T.acid);
2525
26+ class ChapterGet :
27+ # chapnum_map={}
28+ # sub_chapters={}
29+ # subchap_map={}
30+ # subchapnum_map={}
31+ # subchapNum_map={}
32+ def __init__ (self ,chapters ):
33+
34+ self .Cmap = {}
35+ self .Smap = {} #dictionary organized by chapter and section labels
36+ self .SAmap = {} #organized just by section label
37+ for chapter in chapters :
38+ label = chapter .chapter_label
39+ self .Cmap [label ]= chapter
40+ sub_chapters = db (db .sub_chapters .chapter_id == chapter .id ).select (db .sub_chapters .ALL ) #FIX: get right course_id, too
41+ #NOTE: sub_chapters table doesn't have a course name column in it, kind of a problem
42+ self .Smap [label ]= {}
43+
44+ for sub_chapter in sub_chapters :
45+ self .Smap [label ][sub_chapter .sub_chapter_label ]= sub_chapter
46+ self .SAmap [sub_chapter .sub_chapter_label ]= sub_chapter
47+ def ChapterNumber (self ,label ):
48+ """Given the label of a chapter, return its number"""
49+ try :
50+ return self .Cmap [label ].chapter_num
51+ except KeyError :
52+ return ""
53+ def ChapterName (self ,label ):
54+ try :
55+ return self .Cmap [label ].chapter_name
56+ except KeyError :
57+ return label
58+ def SectionName (self ,chapter ,section ):
59+ try :
60+ return self .Smap [chapter ][section ].sub_chapter_name
61+ except KeyError :
62+ return section
63+ def SectionNumber (self ,chapter ,section = None ):
64+ try :
65+ if section == None :
66+ lookup = self .SAmap
67+ section = chapter
68+ else :
69+ lookup = self .Smap [chapter ]
70+
71+ return lookup [section ].sub_chapter_num
72+ except KeyError :
73+ return 999
74+
2675@auth .requires_login ()
2776def index ():
2877 selected_chapter = None
@@ -36,11 +85,10 @@ def index():
3685
3786 course = db (db .courses .id == auth .user .course_id ).select ().first ()
3887 assignments = db (db .assignments .course == course .id ).select (db .assignments .ALL , orderby = db .assignments .name )
88+ chapters = db (db .chapters .course_id == course .base_course ).select (orderby = db .chapters .chapter_num )
89+
3990 logger .debug ("getting chapters for {}" .format (auth .user .course_name ))
40- chapters = db (db .chapters .course_id == course .base_course ).select ()
41- chap_map = {}
42- for chapter in chapters :
43- chap_map [chapter .chapter_label ] = chapter .chapter_name
91+ chapget = ChapterGet (chapters )
4492 for chapter in chapters .find (lambda chapter : chapter .chapter_label == request .vars ['chapter' ]):
4593 selected_chapter = chapter
4694 if selected_chapter is None :
@@ -61,12 +109,16 @@ def index():
61109
62110 if data_analyzer .questions [problem_id ]:
63111 chtmp = data_analyzer .questions [problem_id ].chapter
112+ schtmp = data_analyzer .questions [problem_id ].subchapter
64113 entry = {
65114 "id" : problem_id ,
66115 "text" : metric .problem_text ,
67116 "chapter" : chtmp ,
68- "chapter_title" : chap_map .get (chtmp ,chtmp ),
69- "sub_chapter" : data_analyzer .questions [problem_id ].subchapter ,
117+ "chapter_title" : chapget .ChapterName (chtmp ),
118+ "chapter_number" : chapget .ChapterNumber (chtmp ),
119+ "sub_chapter" : schtmp ,
120+ "sub_chapter_number" : chapget .SectionNumber (chtmp ,schtmp ),
121+ "sub_chapter_title" : chapget .SectionName (chtmp ,schtmp ),
70122 "correct" : stats [2 ],
71123 "correct_mult_attempt" : stats [3 ],
72124 "incomplete" : stats [1 ],
@@ -79,6 +131,8 @@ def index():
79131 "text" : metric .problem_text ,
80132 "chapter" : "unknown" ,
81133 "sub_chapter" : "unknown" ,
134+ "sub_chapter_number" : 0 ,
135+ "sub_chapter_title" :"unknown" ,
82136 "chapter_title" : "unknown" ,
83137 "correct" : stats [2 ],
84138 "correct_mult_attempt" : stats [3 ],
@@ -89,14 +143,20 @@ def index():
89143 questions .append (entry )
90144 logger .debug ("ADDING QUESTION %s " , entry ["chapter" ])
91145
92- logger .debug ("getting questsions" )
93- questions = sorted (questions , key = itemgetter ("chapter" ))
146+ logger .debug ("getting questions" )
147+ try :
148+ questions = sorted (questions , key = itemgetter ("chapter" ,"sub_chapter_number" ))
149+ except :
150+ logger .error ("FAILED TO SORT {}" .format (questions ))
94151 logger .debug ("starting sub_chapter loop" )
95152 for sub_chapter , metric in six .iteritems (progress_metrics .sub_chapters ):
96153 sections .append ({
97154 "id" : metric .sub_chapter_label ,
98155 "text" : metric .sub_chapter_text ,
99156 "name" : metric .sub_chapter_name ,
157+ "number" : chapget .SectionNumber (selected_chapter .chapter_label ,metric .sub_chapter_label ),
158+ #FIX: Using selected_chapter here might be a kludge
159+ #Better if metric contained chapter numbers associated with sub_chapters
100160 "readPercent" : metric .get_completed_percent (),
101161 "startedPercent" : metric .get_started_percent (),
102162 "unreadPercent" : metric .get_not_started_percent ()
0 commit comments