Skip to content

Commit f6ed468

Browse files
committed
Add a way of displaying the number of times an assistant has given a lesson using colours
1 parent 81dff63 commit f6ed468

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

assignment.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from sqlalchemy import func
12
from decorators import login_required, check_access_level
23
from db import db, User, Course, PreferenceAssignment, Teacher, Researcher, Organization, \
34
ResearcherSupervisor, Role, AssignmentDraft, AssignmentPublished
@@ -11,6 +12,7 @@
1112

1213
@assignment_bp.route('/assignments', methods=['GET'])
1314
@login_required
15+
@check_access_level(Role.ADMIN)
1416
def assignments():
1517
return render_template('assignment.html')
1618

@@ -72,6 +74,7 @@ def load_data():
7274

7375
@assignment_bp.route('/publish_assignments', methods=['POST'])
7476
@login_required
77+
@check_access_level(Role.ADMIN)
7578
def publish_assignments():
7679
data = request.get_json()
7780
if not data:
@@ -130,3 +133,25 @@ def publish_assignments():
130133
except Exception as e:
131134
db.session.rollback()
132135
return jsonify({"error": f"Failed to publish assignments: {str(e)}"}), 500
136+
137+
138+
def count_course_assignments():
139+
# Counts the number of times each user has taught each course
140+
assignment_counts = db.session.query(
141+
AssignmentDraft.user_id,
142+
AssignmentDraft.course_id,
143+
func.count(AssignmentDraft.course_id).label('count')
144+
).group_by(
145+
AssignmentDraft.user_id,
146+
AssignmentDraft.course_id
147+
).all()
148+
149+
return assignment_counts
150+
151+
152+
@assignment_bp.route('/course_assignments_count', methods=['GET'])
153+
@login_required
154+
@check_access_level(Role.ADMIN)
155+
def get_course_assignments_count():
156+
results = count_course_assignments()
157+
return jsonify(results=[{'user_id': user_id, 'course_id': course_id, 'count': count} for user_id, course_id, count in results])

static/scripts/assignment_table.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ fetch('/assignment/load_data')
184184
}
185185
}
186186

187+
function getCountCourseAssignment() {
188+
return fetch('/assignment/course_assignments_count')
189+
.then(response => response.json())
190+
.then(data => {
191+
return data.results;
192+
})
193+
.catch(error => console.log(error))
194+
}
195+
187196
const table = new Handsontable(document.getElementById("handsontable"), {
188197
data: data,
189198
fixedColumnsLeft: lenFixedHeaders,
@@ -277,11 +286,35 @@ fetch('/assignment/load_data')
277286
});
278287
}
279288
},
280-
afterRenderer: function (TD, row, col, prop, value, cellProperties) {
289+
afterRenderer: async function (TD, row, col, prop, value, cellProperties) {
281290
if ((col >= lenFixedHeaders && row >= lenFixedRowsText) && (row % 2 === 1) && (value !== '')) {
282291
TD.style.fontWeight = 'bold'; // Bold text
283292
TD.style.textAlign = 'left'; // Left alignment
284293
}
294+
295+
if (row >= lenFixedRowsText && col >= lenFixedHeaders && (row % 2 === 0)) {
296+
const assignmentCount = await getCountCourseAssignment();
297+
298+
console.log("Assignment count", assignmentCount);
299+
300+
const user_id = this.getDataAtRowProp(row, 'researchers.id'); // Retrieves user ID
301+
const course_id = this.getDataAtCol(col)[0]; // Retrieves the course ID
302+
303+
const assignment = assignmentCount.find(item => item.user_id === user_id && item.course_id === course_id);
304+
305+
if (assignment) {
306+
// Coloration basée sur le nombre d'affectations
307+
const count = assignment.count;
308+
if (count === 1) {
309+
TD.style.backgroundColor = '#5DADE2';
310+
} else if (count === 2) {
311+
TD.style.backgroundColor = '#87CEEB';
312+
} else if (count >= 3) {
313+
TD.style.backgroundColor = '#0A74DA';
314+
}
315+
}
316+
}
317+
285318
//(row%2) === 1 to avoid empty lines
286319
if (row >= lenFixedRowsText && (row % 2) === 0 && col < lenFixedHeaders) {
287320
const rowValue = this.getDataAtRow(row);

0 commit comments

Comments
 (0)