[Tutor] Validation loop

Norman Khine norman at khine.net
Sat Sep 13 13:55:56 CEST 2008

I am writting a little module that validates if a member has completed 
all the previous exams before they can access the current exam.

The way, I thought it may work is to:

Search all previous exams from the previous topics and check to see if 
the user has passed them, if not, then they cannot access this exam.

This works to a point, in that, I only make a check in the topic before 
the current topic if the user has passed the exam.

The problem is if, for example, I have this:

+ exam1
+ no-exam
+ exam3

In topic-2 I don't have an exam, so my function returns true and the 
user can access both exam-1 and exam-3

Whereas I don't want them to access exam-3.

Can you please look at my function and point me in the right direction.

     # Exam Access Control
     def can_take_exam(self, user, object):
         Who is not allowed to take the exam:
         1) If the user has not passed the exam from all the previous 
         if self.is_admin(user, object):
             return True
         if not self.is_member(user, object):
             return False

         # Has the user already passed this exam?
         passed = object.get_result()[0]
         if passed:
             return False
         topic = object.parent
         prev_topic = topic.get_prev_topic()

         # First topic, user can take exam
         if prev_topic is None:
             return True

         # Previous topic has no exam
         exam = prev_topic.get_exam()
         if exam is None:
             return True

         # Has the user passed the previous exam?
         passed = exam.get_result()[0]
         return bool(passed)

I was thinking maybe to put in the list all the previous topics and then 
through this for each exam and if all are passed then the user can take the
current exam, if not then no.

So I tried this:

         topics = object.parent.get_topics()
         for i, topic in enumerate(topics):
             all_previous_topics = topics[:i]
             if all_previous_topics is not None:
                 return False
                 for x in all_previous_topics:
                     exam = x.get_exam(user.name)
                     if exam is not None:
                         passed = exam.get_result(user.name)[0]
                         if passed:
                             return bool(passed)

But this, makes all exams non accessable.

Any pointers much appreciated.



More information about the Tutor mailing list