def test_solution(self):
        
        self.qsa1.question_set=3
        self.qsa1.save()
        self.qsa2.question_set=7
        self.qsa2.save()
        self.qsa3.question_set=3
        self.qsa3.save()
        self.qsa4.question_set=7
        self.qsa4.save()
        
        valid_options=[[self.q1,self.q2],[self.q2,self.q1],[self.q1,self.q4],[self.q4,self.q1],
                       [self.q3,self.q2],[self.q2,self.q3],[self.q3,self.q4],[self.q4,self.q3]]


        qs = [self.q1, self.q2, self.q3, self.q4]

        for j in range(3):
            question_list = get_question_list(self.asmt, rng=self.rng,
                                              seed=get_new_seed(self.rng))
            question_list = render_question_list(
                self.asmt, rng=self.rng, question_list = question_list,
                assessment_seed=get_new_seed(self.rng), solution=True)
            questions = [ql['question'] for ql in question_list]

            self.assertTrue(questions in valid_options)
            
            for (i,question_dict) in enumerate(question_list):
                self.assertEqual(
                    question_dict['question_data']['rendered_text'],
                    "Question number %i solution."
                    % (qs.index(question_dict['question'])+1))
    def test_no_question_groups_all_orders(self):
        self.qsa4.delete()
        
        qs = [self.q1, self.q2, self.q3, self.q4]
        valid_orders = []
        orders_used = []
        for i in range(3):
            for j in range(3):
                if i==j:
                    continue
                for k in range(3):
                    if k==i or k==j:
                        continue
                    valid_orders.append([qs[i], qs[j], qs[k]])
                    orders_used.append(False)

        for i in range(200):
            question_list = get_question_list(self.asmt, rng=self.rng,
                                              seed=get_new_seed(self.rng))
            question_list = render_question_list(
                self.asmt, rng=self.rng, question_list = question_list,
                assessment_seed=get_new_seed(self.rng))
            for question_dict in question_list:
                self.assertEqual(question_dict['relative_weight'],1/3)
                self.assertFalse(question_dict['previous_same_group'])
            questions = [ql['question'] for ql in question_list]
            self.assertTrue(questions in valid_orders)
            one_used = valid_orders.index(questions)
            orders_used[one_used]=True
            
            if False not in orders_used:
                break

        self.assertTrue(False not in orders_used)
    def test_multiple_in_question_set(self):
        
        self.qsa1.question_set=3
        self.qsa1.save()
        self.qsa2.question_set=7
        self.qsa2.save()
        self.qsa3.question_set=3
        self.qsa3.save()
        self.qsa4.question_set=7
        self.qsa4.save()
        
        valid_options=[[self.q1,self.q2],[self.q2,self.q1],[self.q1,self.q4],[self.q4,self.q1],
                       [self.q3,self.q2],[self.q2,self.q3],[self.q3,self.q4],[self.q4,self.q3]]

        options_used = [False, False, False, False,
                        False, False, False, False]

        for j in range(200):
            question_list = get_question_list(self.asmt, rng=self.rng,
                                              seed=get_new_seed(self.rng))
            question_list = render_question_list(
                self.asmt, rng=self.rng, question_list = question_list,
                assessment_seed=get_new_seed(self.rng))
            questions = [ql['question'] for ql in question_list]
            
            self.assertTrue(questions in valid_options)
            
            one_used = valid_options.index(questions)
            options_used[one_used]=True
            
            if False not in options_used:
                break

        self.assertTrue(False not in options_used)
    def test_with_weight(self):
        
        self.qsa1.question_set=3
        self.qsa1.save()
        self.qsa4.question_set=2
        self.qsa4.save()

        self.asmt.questionsetdetail_set.create(question_set=3,
                                               weight = 5)
        self.asmt.questionsetdetail_set.create(question_set=2,
                                               weight = 7.3)

        valid_options = [{self.q2,self.q1},{self.q2,self.q3},{self.q4,self.q1},{self.q4,self.q3}]

        options_used = [False, False, False, False]

        for i in range(100):
            question_list = get_question_list(self.asmt, rng=self.rng,
                                              seed=get_new_seed(self.rng))
            
            relative_weights = {ql['relative_weight'] for ql in question_list}
            self.assertEqual(relative_weights, {5/12.3, 7.3/12.3})

            questions = {ql['question'] for ql in question_list}
            self.assertTrue(questions in valid_options)

            one_used = valid_options.index(questions)
            options_used[one_used]=True
            
            if False not in options_used:
                break

        self.assertTrue(False not in options_used)
    def test_multiple_questions_per_question_set(self):
        self.qsa1.question_set=2
        self.qsa1.save()
        self.qsa3.question_set=4
        self.qsa3.save()

        valid_options = [{self.q1,self.q3},{self.q1,self.q4},{self.q2,self.q3},{self.q2,self.q4}]

        options_used = [False, False, False, False]

        for i in range(100):
            question_list = get_question_list(self.asmt, rng=self.rng,
                                              seed=get_new_seed(self.rng))
            questions = {ql['question'] for ql in question_list}
            self.assertTrue(questions in valid_options)

            one_used = valid_options.index(questions)
            options_used[one_used]=True
            
            if False not in options_used:
                break

        self.assertTrue(False not in options_used)


        self.qsa1.question_set=4
        self.qsa1.save()

        valid_options = [{self.q2,self.q1},{self.q2,self.q3},{self.q2,self.q4}]

        options_used = [False, False, False]

        for i in range(100):
            question_list = get_question_list(self.asmt, rng=self.rng,
                                              seed=get_new_seed(self.rng))
            questions = {ql['question'] for ql in question_list}
            self.assertTrue(questions in valid_options)

            one_used = valid_options.index(questions)
            options_used[one_used]=True
            
            if False not in options_used:
                break

        self.assertTrue(False not in options_used)
    def test_groups_fixed_order(self):
        self.asmt.fixed_order=True
        self.asmt.save()

        self.asmt.questionsetdetail_set.create(question_set=1,
                                               group="apple")
        self.asmt.questionsetdetail_set.create(question_set=4,
                                               group="apple")
        for i in range(3):
            question_list = get_question_list(self.asmt, rng=self.rng,
                                              seed=get_new_seed(self.rng))
            question_list = render_question_list(
                self.asmt, rng=self.rng, question_list = question_list,
                assessment_seed=get_new_seed(self.rng))
            questions = [ql['question'] for ql in question_list]
            self.assertEqual(questions, [self.q1,self.q2,self.q3,self.q4])
            psg = [ql['previous_same_group'] for ql in question_list]
            self.assertEqual(psg, [False,False,False,False])
            groups = [ql['group'] for ql in question_list]
            self.assertEqual(groups[0], "apple")
            self.assertEqual(groups[3], "apple")


        self.asmt.questionsetdetail_set.create(question_set=2,
                                               group="appl")
        self.asmt.questionsetdetail_set.create(question_set=3,
                                               group="appl")

        for i in range(3):
            question_list = get_question_list(self.asmt, rng=self.rng,
                                              seed=get_new_seed(self.rng))
            question_list = render_question_list(
                self.asmt, rng=self.rng, question_list = question_list,
                assessment_seed=get_new_seed(self.rng))
            questions = [ql['question'] for ql in question_list]
            self.assertEqual(questions, [self.q1,self.q2,self.q3,self.q4])
            psg = [ql['previous_same_group'] for ql in question_list]
            self.assertEqual(psg, [False,False,True,False])
            groups = [ql['group'] for ql in question_list]
            self.assertEqual(groups, ["apple", "appl", "appl", "apple"])
Exemplo n.º 7
0
def create_new_assessment_attempt(assessment, thread_content, courseuser,
                                  student_record):

    from micourses.models import AVAILABLE, NOT_YET_AVAILABLE
    assessment_availability = thread_content.return_availability(student_record)

    # treat assessment not set up for recording as not available
    if not thread_content.record_scores:
        assessment_availability = NOT_YET_AVAILABLE

    valid_attempt=assessment_availability==AVAILABLE

    if assessment.single_version:
       seed='1'
       version = ''
    else:
        if valid_attempt:
            attempt_number = student_record.attempts.filter(valid=True)\
                                                    .count()+1
            version = str(attempt_number)
        else:
            attempt_number = student_record.attempts.filter(valid=False)\
                                                    .count()+1
            version = "x%s" % attempt_number

        if thread_content.individualize_by_student:
            version = "%s_%s" % \
                        (courseuser.user.username, version)
        seed = "sd%s_%s" % (thread_content.id, version)
        version = re.sub("_", " ", version)

    # create the new attempt
    with transaction.atomic(), reversion.create_revision():
        new_attempt = student_record.attempts.create(
            seed=seed, valid=valid_attempt, version=version)

    from micourses.render_assessments import get_question_list
    question_list = get_question_list(assessment, seed=seed,
                                      thread_content=thread_content)

    # create the content question sets and question attempts
    with transaction.atomic(), reversion.create_revision():
        for (i,q_dict) in enumerate(question_list):
            ca_question_set = new_attempt.question_sets.create(
                question_number=i+1, question_set=q_dict['question_set'])
            qa=ca_question_set.question_attempts.create(
                question=q_dict['question'],
                seed=q_dict['seed'], valid=valid_attempt)
            q_dict["question_attempt"] = qa

    return {'new_attempt': new_attempt, 'question_list': question_list,
            'version': version, 'assessment_seed': seed,}
    def test_one_question_per_question_set(self):
        
        for i in range(10):
            question_list = get_question_list(self.asmt, rng=self.rng,
                                              seed=get_new_seed(self.rng))
            questions = {ql['question'] for ql in question_list}
            self.assertEqual(questions, {self.q1, self.q2, self.q3, self.q4})
            
            question_sets = sorted([ql['question_set'] for ql in question_list])
            self.assertEqual(question_sets, [1,2,3,4])

            relative_weights = [ql['relative_weight'] for ql in question_list]
            self.assertEqual(relative_weights, [1/4,1/4,1/4,1/4])
            
            for ql in question_list:
                seed = ql['seed']
                self.assertTrue(int(seed) >= 0)
                self.assertTrue(int(seed) <= 1000000000000)
    def test_no_question_groups_fixed_order(self):
        self.asmt.fixed_order=True
        self.asmt.save()

        qs = [self.q1, self.q2, self.q3, self.q4]
        for j in range(10):
            question_list = get_question_list(self.asmt, rng=self.rng,
                                              seed=get_new_seed(self.rng))
            question_list = render_question_list(
                self.asmt, rng=self.rng, question_list = question_list,
                assessment_seed=get_new_seed(self.rng))
            for (i,question_dict) in enumerate(question_list):
                self.assertEqual(question_dict['question'], qs[i])
                self.assertEqual(question_dict['question_set'], i+1)
                self.assertEqual(question_dict['relative_weight'],1/4)
                qseed = int(question_dict['seed'])
                self.assertTrue(qseed >= 0 and qseed < 100000000000)
                self.assertEqual(question_dict['group'],"")
                self.assertEqual(
                    question_dict['question_data']['rendered_text'],
                    "Question number %s text." % (i+1))
                self.assertFalse(question_dict['previous_same_group'])
Exemplo n.º 10
0
    def test_groups_random_order(self):
        self.asmt.questionsetdetail_set.create(question_set=1,
                                               group="apple")
        self.asmt.questionsetdetail_set.create(question_set=4,
                                               group="apple")
        qs = [self.q1, self.q2, self.q3, self.q4]
        for j in range(10):
            question_list = get_question_list(self.asmt, rng=self.rng,
                                              seed=get_new_seed(self.rng))
            question_list = render_question_list(
                self.asmt, rng=self.rng, question_list = question_list,
                assessment_seed=get_new_seed(self.rng))
            hit_first_group_member=False
            expected_next_group_member=None
            for (i,question_dict) in enumerate(question_list):
                if hit_first_group_member:
                    self.assertTrue(question_dict['previous_same_group'])
                    self.assertEqual(question_dict['group'],'apple')
                    self.assertEqual(qs.index(question_dict['question'])+1, 
                                     expected_next_group_member)
                    hit_first_group_member = False
                        
                else:
                    self.assertFalse(question_dict['previous_same_group'])
                    if question_dict['group'] == 'apple':
                        hit_first_group_member = True
                        if qs.index(question_dict['question']) == 0:
                            expected_next_group_member = 4
                        else:
                            expected_next_group_member = 1

                self.assertEqual(
                    question_dict['question_data']['rendered_text'],
                    "Question number %i text." % (qs.index(question_dict['question'])+1))


        self.asmt.questionsetdetail_set.create(question_set=2,
                                               group="appl")
        self.asmt.questionsetdetail_set.create(question_set=3,
                                               group="appl")

        for j in range(10):
            question_list = get_question_list(self.asmt, rng=self.rng,
                                              seed=get_new_seed(self.rng))
            question_list = render_question_list(
                self.asmt, rng=self.rng, question_list = question_list,
                assessment_seed=get_new_seed(self.rng))
            hit_first_group_member=False
            expected_next_group_member=None
            for (i,question_dict) in enumerate(question_list):
                if hit_first_group_member:
                    self.assertTrue(question_dict['previous_same_group'])
                    self.assertEqual(question_dict['group'],group_found)
                    self.assertEqual(qs.index(question_dict['question'])+1, 
                                     expected_next_group_member)
                    hit_first_group_member = False
                        
                else:
                    self.assertFalse(question_dict['previous_same_group'])
                    group_found = question_dict['group']
                    if group_found == 'apple':
                        hit_first_group_member = True
                        if qs.index(question_dict['question']) == 0:
                            expected_next_group_member = 4
                        else:
                            expected_next_group_member = 1
                    elif group_found == 'appl':
                        hit_first_group_member = True
                        if qs.index(question_dict['question']) == 1:
                            expected_next_group_member = 3
                        else:
                            expected_next_group_member = 2
                        
                self.assertEqual(
                    question_dict['question_data']['rendered_text'],
                    "Question number %s text." % (qs.index(question_dict['question'])+1))
Exemplo n.º 11
0
    def form_valid(self, form):

        include_list= form.cleaned_data['include_list']
        avoid_list= form.cleaned_data['avoid_list']
        seed = form.cleaned_data['seed']
        version = form.cleaned_data['version_description']

        if seed == "":
            seed = str(timezone.now())

        assessment_datetime = form.cleaned_data['assessment_datetime']

        current_tz = timezone.get_current_timezone()
        assessment_datetime = current_tz.normalize(
            assessment_datetime.astimezone(current_tz))
        assessment_date = assessment_datetime.date()

        
        include_dict={}
        if include_list:
            for item in include_list.split(","):
                ind = int(item)
                include_dict[ind] = include_dict.get(ind,0)+1

        avoid_dict={}
        if avoid_list:
            for item in avoid_list.split(","):
                ind = int(item)
                avoid_dict[ind] = avoid_dict.get(ind,0)+1

        from micourses.models import ContentAttempt
                
        for cca_id in self.coursewide_attempts_include:
            try:
                cca = ContentAttempt.objects.get(id=cca_id)
            except ContentAttempt.DoesNotExist:
                continue

            if cca.record.content != self.thread_content:
                continue

            for qs in cca.question_sets.all():
                try:
                    qa = qs.question_attempts.latest()
                except ObjectDoesNotExist:
                    continue
                ind = qa.question.id
                include_dict[ind] = include_dict.get(ind,0)+1

        for cca_id in self.coursewide_attempts_avoid:
            try:
                cca = ContentAttempt.objects.get(id=cca_id)
            except ContentAttempt.DoesNotExist:
                continue

            if cca.record.content != self.thread_content:
                continue

            for qs in cca.question_sets.all():
                try:
                    qa = qs.question_attempts.latest()
                except ObjectDoesNotExist:
                    continue
                ind = qa.question.id
                avoid_dict[ind] = avoid_dict.get(ind,0)+1


        if include_dict or avoid_dict:
            new_seed = self.assessment.include_avoid_question_seed(
                include_dict=include_dict, avoid_dict=avoid_dict, 
                start_seed=seed)
        else:
            from mitesting.utils import get_new_seed
            new_seed=get_new_seed(seed=seed)


        if not version:
            version = new_seed[-3:]

        try:
            course_record = self.thread_content.contentrecord_set\
                                    .get(enrollment = None)
        except ObjectDoesNotExist:
            with transaction.atomic(), reversion.create_revision():
                course_record = self.thread_content.contentrecord_set\
                                    .create(enrollment = None)

        # create new course attempt
        # in case get deadlock, try five times
        for trans_i in range(5):
            try:
                with transaction.atomic(), reversion.create_revision():
                    course_attempt = course_record.attempts.create(
                        seed=new_seed, valid=True,
                        attempt_began = assessment_datetime,
                        version=version)

                    from micourses.render_assessments import get_question_list
                    question_list = get_question_list(
                        self.assessment, seed=new_seed,
                        thread_content=self.thread_content)

                    # create the content question sets and question attempts
                    with transaction.atomic(), reversion.create_revision():
                        for (i,q_dict) in enumerate(question_list):
                            ca_question_set = \
                                course_attempt.question_sets.create(
                                    question_number=i+1,
                                    question_set=q_dict['question_set'])
                            qa=ca_question_set.question_attempts.create(
                                question=q_dict['question'],
                                seed=q_dict['seed'])
                            q_dict['question_attempt'] = qa

            except OperationalError:
                if trans_i==4:
                    raise
            else:
                break

        new_url = "%s?content_attempt=%s&date=%s" % \
                  (reverse('miassess:assessment', 
                           kwargs={'course_code': self.course.code,
                                   'assessment_code': self.assessment.code}),
                   course_attempt.id, assessment_datetime)


        return HttpResponseRedirect(new_url)
Exemplo n.º 12
0
    def determine_version_attempt(self, user, seed, content_attempt_id, 
                                  question_attempt_ids):

        """
        Determine what version of assessment to generate.
        
        For enrolled students, find or create
        content attempts and question attempts.

        Set the following variables that give information about user
        and the assessment's role in course:
        self.course_enrollment
        self.thread_content
        self.current_attempt
        
        Set the following variables that specify the version of assessment:
        self.assessment_seed
        self.version
        self.question_list

        question_list is a list of dictionaries with the following info 
        about each question
        - question: the question chosen for the question_set
        - seed: the seed for the question
        - question_set (optional): the question set from which the question
          was chosen
        - question_attempt (optional): the question attempt in which
          to record responses.


        ------------------
        
        Behavior based on status of user as follows.

        Anonymous user behavior:
        just set seed via GET.  If seed is not specified, use seed=1.
        If single version, ignore seed from GET and use seed=1.
        Generate new seed and make link at bottom.
        Even if resample question sets, reloading page will reset with questions
        from that given assessment seed.

        Logged in user who isn't in course: 
        same as anonymous user behavior

        Logged in user who is an active student of course:
        If assessment is not in course thread, same as anonymous user behavior
        Otherwise
        - Ignore seed from GET
        - Determine availability of content 
        - Find latest content attempt
          If content attempt validity does not match, 
          treat as no matching attempt (available=valid)
          Obtain
          * assessment seed from content attempt 
          * list of questions sets in order (from content attempt question sets)
          * the latest question attempt for each question set
          * from each question attempt, determine
            + question
            + seed 
            + whether or not solution viewed
          If missing data (e.g., assessment seed or question attempts),
          then treat as though don't have content attempt and create new one
        - If no matching content attempt, 
          then create new content attempt and question attempts.
          Generate assessment seed as follows:
          * If not yet released, set seed to be attempt number.
          * Otherwise, create assessment seed from 
            + thread content id and attempt number
            + plus username if assessment is individualized by student
            Exception: set seed=1 if assessment marked as single version
          Use assessment seed to generate
          * list of question sets in order
          * question and seed
          Save 
          * assessment seed to content attempt
          * list of question sets in order to content attempt question sets
          * questions and their seeds to question attempt
          If not yet released or past due, mark content attempt
          and question attempts as invalid.


        Logged in user who is a withdrawn student of course:
        Treat like active student, only mark as withdrawn so can
        display message when submitting responses.


        Looged in user who is instructor of course:
        If assessment is not in course thread, same as anonymous user behavior
        Otherwise:
        If content_attempt id is in GET
        - generate assessment based on that content attempt,
          using the latest question attempts for each question set
        - if, in addition, GET contains list of question attempt ids, 
          then use those instead, assuming have one for each question set
          (if don't have one for each question set or question attempts don't
          belong to content attempt, then ignore and use latest question attempts.)
        - if content attempt doesn't have associated question attempts for
          all question sets, then ignore content_attempt id
        If no valid content_attempt id, but seed is in GET
        - use that to generate assessment (even if single version)
        Do not record any responses if generated from content attempt id or seed
        If no valid content attempt id and  seed is not in GET, 
        then treat as student of course


        """

        # sets the following variables
        self.course_enrollment=None
        self.thread_content=None
        self.assessment_seed= None
        self.version = ''
        self.current_attempt=None
        self.question_list = []

        from micourses.render_assessments import get_question_list
        from micourses.models import AVAILABLE,  NOT_YET_AVAILABLE


        #################################
        # first, determine status of user
        #################################

        # if course user doesn't exist, then is anonymous user
        # as logged in users should have a courseuser
        try:
            courseuser = user.courseuser
        except AttributeError:
            courseuser = None

        # check if enrolled in course
        current_role=None
        if courseuser:
            try:
                self.course_enrollment = self.assessment.course\
                        .courseenrollment_set.get(student=courseuser)
            except ObjectDoesNotExist:
                pass
            else:
                current_role = courseuser.get_current_role(course=self.assessment.course)

        ###############################################
        # first, determine thread_content of assessment
        ###############################################

        # thread_content will be None
        # if assessment is not in thread and number in thread is 1
        try:
            self.thread_content=self.assessment.determine_thread_content(
                self.number_in_thread)
        except ObjectDoesNotExist:
            raise Http404("No assessment found") 

        
        ########################################################
        # generic behavior if not in course or no thread content
        ########################################################

        
        if not (self.course_enrollment and self.thread_content):
            if self.assessment.single_version:
                self.assessment_seed='1'
                self.version = ''
            else:
                if seed is None:
                    self.assessment_seed='1'
                else:
                    self.assessment_seed=seed
                self.version = str(self.assessment_seed)[-4:]


            self.question_list = get_question_list(self.assessment, 
                                                   seed=self.assessment_seed)

            return


        #########################################
        # instructor behavior with content attempt or seed specified
        #########################################

        if current_role == INSTRUCTOR_ROLE or current_role == DESIGNER_ROLE:

            content_attempt=None
            if content_attempt_id is not None:
                from micourses.models import ContentAttempt
                try:
                    content_attempt = ContentAttempt.objects.get(
                        id=content_attempt_id)
                except ObjectDoesNotExist:
                    pass
                else:
                    if content_attempt.record.content != self.thread_content:
                        content_attempt=None
                if content_attempt is None:
                    raise ValueError("Content attempt %s does not exist for %s." % \
                        (content_attempt_id, self.thread_content.get_title()))

            # if found valid content attempt, 
            # attempt to find valid question attempts
            if content_attempt:
                question_attempts = []

                if question_attempt_ids:
                    question_attempt_id_list = question_attempt_ids.split(",")

                    from micourses.models import QuestionAttempt

                    for qa_id in question_attempt_id_list:
                        try:
                            qa=QuestionAttempt.objects.get(id=qa_id.strip())
                        except QuestionAttempt.DoesNotExist:
                            question_attempts=[]
                            break
                        else:
                            question_attempts.append(qa)

                from micourses.render_assessments import get_question_list_from_attempt

                self.question_list = get_question_list_from_attempt(
                    assessment=self.assessment, content_attempt=content_attempt,
                    question_attempts=question_attempts)
                            
                if self.question_list:
                    self.current_attempt=content_attempt

                    # set assessment seed and version string
                    self.assessment_seed = self.current_attempt.seed
                    self.version = self.current_attempt.version

                    return
                else:
                    raise ValueError("Invalid content attempt %s for %s.<br/>Question attempts don't match." % \
                        (content_attempt_id, self.thread_content.get_title()))


            # if don't have valid question list, then generate from seed, if set
            if seed is not None:
                self.assessment_seed = seed
                self.question_list = get_question_list(
                    self.assessment, seed=self.assessment_seed,
                    thread_content=self.thread_content)
                self.version=str(seed)

                return


        #########################################
        # enrolled student behavior
        # (also instructor with no seed or content attempt)
        #########################################

        try:
            student_record = self.thread_content.contentrecord_set\
                                    .get(enrollment = self.course_enrollment)
        except ObjectDoesNotExist:
            with transaction.atomic(), reversion.create_revision():
                student_record = self.thread_content.contentrecord_set\
                                    .create(enrollment = self.course_enrollment)

        assessment_availability = self.thread_content.return_availability(student_record)

        # treat assessment not set up for recording as not available
        if not self.thread_content.record_scores:
            assessment_availability = NOT_YET_AVAILABLE

        try:
            latest_attempt = student_record.attempts.latest()
        except ObjectDoesNotExist:
            latest_attempt = None
        else:
            # will use the latest attempt in the following cases
            # 1. assessment is not yet available and attempt is not valid
            # 2. assessment is available and the attempt is valid
            # 3. assessment is past due (OK for valid or invalid attempt)

            # This means that invalid past due responses will be added
            # to a valid attempt.
            # However, to prevent free practice on an attempt before it
            # becomes available,
            # if an assessment is changed
            # to become not yet available after an valid attempt has begun,
            # then create a new, invalid, attempt.
            # This algorithm will make the original valid
            # attempt no longer available, even when the assessment
            # becomes available again.
            # (This situation is not foolproof, as one could get this
            # free practice if the assessment was not reloaded.)
        
            if assessment_availability==NOT_YET_AVAILABLE:
                if latest_attempt.valid:
                    latest_attempt=False
            elif assessment_availability==AVAILABLE:
                if not latest_attempt.valid:
                    latest_attempt=False

        self.current_attempt=None
        if latest_attempt:
            # Verify latest attempt has the right number of
            # of question sets with question attempts
            # If so, set as current attempt and populate
            # question list from that attempt
            
            from micourses.render_assessments import get_question_list_from_attempt
            self.question_list = get_question_list_from_attempt(
                assessment=self.assessment, 
                content_attempt=latest_attempt)

            # if found question_list, use latest attempt as current attempt
            if self.question_list:
                self.current_attempt = latest_attempt
                
                # set assessment seed and version string
                self.assessment_seed = latest_attempt.seed
                self.version = latest_attempt.version

                return
                
        # If didn't find a current attempt to use, generate new attempt
        if not self.current_attempt:

            from micourses.utils import create_new_assessment_attempt
            with transaction.atomic():
                new_attempt_info = create_new_assessment_attempt(
                    assessment=self.assessment,
                    thread_content=self.thread_content,
                    courseuser = courseuser,
                    student_record = student_record)

            self.current_attempt = new_attempt_info['new_attempt']
            self.question_list = new_attempt_info['question_list']
            self.assessment_seed = new_attempt_info['assessment_seed']
            self.version = new_attempt_info['version']