Skip to content

Commit

Permalink
Step navigation is adjusted to stay within the assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan-CTL committed Nov 20, 2023
1 parent 3fd9374 commit ad9208a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
17 changes: 17 additions & 0 deletions econplayground/assignment/migrations/0011_remove_step_next_step.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.7 on 2023-11-20 18:44

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('assignment', '0010_stepresult_scorepath'),
]

operations = [
migrations.RemoveField(
model_name='step',
name='next_step',
),
]
26 changes: 17 additions & 9 deletions econplayground/assignment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,15 @@ class Step(MP_Node):
Question, on_delete=models.SET_NULL,
blank=True, null=True)

next_step = models.ForeignKey(
'self', on_delete=models.SET_NULL,
blank=True, null=True)

@property
def is_last_step(self) -> bool:
return self.next_step is None and \
self.get_next() is None and \
self.get_next_intervention() is None
return (self.depth == 2 and self.get_next_sibling() is None) or \
(self.depth == 3 and self.get_next() is None)

def get_prev(self) -> Self:
"""Return the previous child, or the prev sibling, or None."""
if self.is_root_node:
return None

node = self.get_prev_sibling()
if node:
Expand All @@ -197,7 +194,11 @@ def get_prev(self) -> Self:
return child

if not node:
return self.get_parent()
parent = self.get_parent()
if parent and not parent.is_root_node:
return parent
else:
return None

return node

Expand All @@ -207,6 +208,13 @@ def get_next(self) -> Self:
This is probably the result of a correct answer on the student
side.
"""

# In case you find yourself in a root node, go to the first child.
if self.is_root_node:
child = self.get_first_child()
if child:
return child

node = self.get_next_sibling()
if node:
return node
Expand All @@ -215,7 +223,7 @@ def get_next(self) -> Self:
if parent:
node = parent.get_next_sibling()

if node:
if node and not node.is_root_node:
return node

child = self.get_first_child()
Expand Down
3 changes: 1 addition & 2 deletions econplayground/assignment/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,12 @@ def test_step_question_management(self):
r = self.client.post(
reverse('tree_edit', kwargs={'pk': self.assignment.pk}), {
'action': 'save',
'step_next_' + str(step.pk): new_step.pk,
}, follow=True)
self.assertEqual(r.status_code, 200)
self.assertContains(r, 'Steps updated.')
self.assertEqual(Step.objects.count(), 3)
step.refresh_from_db()
self.assertEqual(step.next_step, new_step)
self.assertEqual(step.get_next(), new_step)

def test_build_linear_assignment(self):
r = self.client.post(
Expand Down

0 comments on commit ad9208a

Please sign in to comment.