Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
Team6CMT313_Assessment2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Wen-Hsuan Hu
Team6CMT313_Assessment2
Commits
c016d3b8
Commit
c016d3b8
authored
4 months ago
by
Kris Hu
Browse files
Options
Downloads
Patches
Plain Diff
update attemptFormative logic, enable to storet data to TestAttempt and QuestionAttempt tables
parent
1fe028c2
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/routes.py
+53
-8
53 additions, 8 deletions
app/routes.py
with
53 additions
and
8 deletions
app/routes.py
+
53
−
8
View file @
c016d3b8
...
...
@@ -844,9 +844,9 @@ def attemptTest():
@routes_bp.route
(
'
/attemptFormative/<int:id>
'
,
methods
=
[
'
GET
'
,
'
POST
'
])
def
attemptFormative
(
id
):
student_id
=
session
[
"
user_id
"
]
test_id
=
id
if
request
.
method
==
'
POST
'
:
# answer s
n
et from front end
# answer se
n
t from front end
answers
=
request
.
form
.
to_dict
(
flat
=
True
)
result
=
{
...
...
@@ -854,6 +854,9 @@ def attemptFormative(id):
"
fib
"
:
[]
}
total_questions
=
0
correct_answers
=
0
for
key
,
user_answer
in
answers
.
items
():
if
'
_
'
in
key
:
qtype
,
qid
=
key
.
split
(
'
_
'
)
...
...
@@ -862,7 +865,6 @@ def attemptFormative(id):
if
qtype
==
'
mcq
'
:
row
=
MCQ
.
query
.
get
(
qid
)
if
row
:
# Corresponding A/B/C/D to the actual option content
options_map
=
{
'
A
'
:
row
.
option_a
,
'
B
'
:
row
.
option_b
,
...
...
@@ -870,30 +872,73 @@ def attemptFormative(id):
'
D
'
:
row
.
option_d
}
# Get the content actually selected by the user
user_option_value
=
options_map
.
get
(
user_answer
.
strip
().
upper
(),
''
)
is_correct
=
user_answer
.
strip
().
upper
()
==
row
.
answer
.
strip
().
upper
()
result
[
"
mcq
"
].
append
({
"
question_id
"
:
qid
,
"
your_answer
"
:
user_option_value
,
"
answer
"
:
options_map
.
get
(
row
.
answer
.
strip
().
upper
(),
''
),
"
is_correct
"
:
user_answer
.
strip
().
upper
()
==
row
.
answer
.
strip
().
upper
()
,
"
is_correct
"
:
is_correct
,
"
feedback
"
:
row
.
feedback
if
row
.
feedback
!=
''
else
''
})
total_questions
+=
1
if
is_correct
:
correct_answers
+=
1
elif
qtype
==
'
fib
'
:
# sql = text("SELECT * FROM FIB WHERE id = :id")
# row = db.engine.execute(sql, {"id": qid}).fetchone()
row
=
FIB
.
query
.
get
(
qid
)
if
row
:
is_correct
=
user_answer
.
strip
().
lower
()
==
row
.
answer
.
strip
().
lower
()
result
[
"
fib
"
].
append
({
"
question_id
"
:
qid
,
"
your_answer
"
:
user_answer
,
"
answer
"
:
row
.
answer
,
"
is_correct
"
:
user_answer
.
strip
().
lower
()
==
row
.
answer
.
strip
().
lower
()
,
"
is_correct
"
:
is_correct
,
"
feedback
"
:
row
.
feedback
if
row
.
feedback
!=
''
else
''
})
total_questions
+=
1
if
is_correct
:
correct_answers
+=
1
# Calculate score
score
=
round
((
correct_answers
/
total_questions
)
*
100
,
2
)
if
total_questions
>
0
else
0.0
# Save to TestAttempt
new_attempt
=
TestAttempt
(
student_id
=
student_id
,
test_id
=
test_id
,
score
=
score
,
time_taken
=
0
# Set time_taken to 0, you can modify this if needed
)
new_attempt
.
attempt_date
=
datetime
.
now
()
db
.
session
.
add
(
new_attempt
)
db
.
session
.
commit
()
# Get the latest TestAttempt (top 1 by id descending)
latest_attempt
=
TestAttempt
.
query
.
filter_by
(
student_id
=
student_id
,
test_id
=
test_id
).
order_by
(
TestAttempt
.
id
.
desc
()).
first
()
# Insert QuestionAttempt records
for
mcq_data
in
result
[
"
mcq
"
]:
qa
=
QuestionAttempt
(
test_attempt_id
=
latest_attempt
.
id
,
student_answer
=
mcq_data
[
"
your_answer
"
],
mcq_id
=
mcq_data
[
"
question_id
"
]
)
db
.
session
.
add
(
qa
)
for
fib_data
in
result
[
"
fib
"
]:
qa
=
QuestionAttempt
(
test_attempt_id
=
latest_attempt
.
id
,
student_answer
=
fib_data
[
"
your_answer
"
],
fib_id
=
fib_data
[
"
question_id
"
]
)
db
.
session
.
add
(
qa
)
db
.
session
.
commit
()
# Save to session, redirect to feedback page
session
[
"
formative_result
"
]
=
result
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment