Example #1
0
def choose_unit_route(request, subject_id, unit_id):
    """
  Updates the learner's information based on the unit they have chosen.

  NEXT STATE
  POST Chosen Unit
    -> GET Learn Card
  """

    # TODO-3 simplify this method. should it be broken up or moved to model?
    db_conn = request['db_conn']
    current_user = get_current_user(request)
    if not current_user:
        return abort(401, 'NcrRkqIlSW-BkcWLBkwORQ')
    unit = get_latest_accepted_unit(db_conn, unit_id)
    if not unit:
        return abort(404, 'PIti_qwAQ7WXwQwNZertxw')
    # If the unit isn't in the subject...
    context = get_learning_context(current_user)
    subject_ids = [
        convert_uuid_to_slug(subject['entity_id'])
        for subject in list_subjects_by_unit_recursive(db_conn, unit_id)
    ]
    context_subject_id = context.get('subject', {}).get('entity_id')
    if context_subject_id not in subject_ids:
        return 400, {
            'errors': [{
                'message': 'Unit not in subject.',
                'ref': 'r32fH0eCRZCivJoh-hKwZQ',
            }]
        }
    # Or, the unit doesn't need to be learned...
    status = judge(db_conn, unit, current_user)
    if status == "done":
        return 400, {
            'errors': [{
                'message': 'Unit not needed.',
                'ref': 'YTU27E63Rfiy3Rqmqd6Bew',
            }]
        }
    # Choose a card for the learner to learn
    card = choose_card(db_conn, current_user, unit)
    if card:
        next_ = {
            'method':
            'GET',
            'path':
            '/s/cards/{card_id}/learn'.format(
                card_id=convert_uuid_to_slug(card['entity_id'])),
        }
    else:
        next_ = {}
    set_learning_context(current_user,
                         unit=unit,
                         card=card if card else None,
                         next=next_)
    return 200, {'next': next_}
Example #2
0
def choose_unit_route(request, subject_id, unit_id):
    """
    Updates the learner's information based on the unit they have chosen.

    NEXT STATE
    POST Chosen Unit
        -> GET Learn Card
    """

    # TODO-3 simplify this method. should it be broken up or moved to model?
    db_conn = request['db_conn']

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    unit = Unit.get_latest_accepted(db_conn, unit_id)
    if not unit:
        return abort(404)

    # If the unit isn't in the subject...
    context = get_learning_context(current_user)
    subject_ids = [
        subject['entity_id']
        for subject in Subject.list_by_unit_id(db_conn, unit_id)
    ]
    if context.get('subject', {}).get('entity_id') not in subject_ids:
        return abort(400)

    status = judge(db_conn, unit, current_user)
    # Or, the unit doesn't need to be learned...
    if status == "done":
        return abort(400)

    # Choose a card for the learner to learn
    card = choose_card(db_conn, current_user, unit)

    if card:
        next_ = {
            'method': 'GET',
            'path':
            '/s/cards/{card_id}/learn'.format(card_id=card['entity_id']),
        }
    else:
        next_ = {}

    set_learning_context(current_user,
                         unit=unit.data,
                         card=card.data if card else None,
                         next=next_)

    return 200, {'next': next_}
Example #3
0
def choose_unit_route(request, set_id, unit_id):
    """
    Updates the learner's information based on the unit they have chosen.

    NEXT STATE
    POST Chosen Unit
        -> GET Learn Card
    """

    # TODO-3 simplify this method. should it be broken up or moved to model?
    db_conn = request['db_conn']

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    unit = Unit.get_latest_accepted(db_conn, unit_id)
    if not unit:
        return abort(404)

    # If the unit isn't in the set...
    context = get_learning_context(current_user)
    set_ids = [set_['entity_id']
               for set_ in Set.list_by_unit_id(db_conn, unit_id)]
    if context.get('set', {}).get('entity_id') not in set_ids:
        return abort(400)

    status = judge(db_conn, unit, current_user)
    # Or, the unit doesn't need to be learned...
    if status == "done":
        return abort(400)

    # Choose a card for the learner to learn
    card = choose_card(db_conn, current_user, unit)

    if card:
        next_ = {
            'method': 'GET',
            'path': '/s/cards/{card_id}/learn'
                    .format(card_id=card['entity_id']),
        }
    else:
        next_ = {}

    set_learning_context(
        current_user,
        unit=unit.data,
        card=card.data if card else None,
        next=next_
    )

    return 200, {'next': next_}
Example #4
0
def get_set_tree_route(request, set_id):
    """
    Render the tree of units that exists within a set.

    Contexts:
    - Search set, preview units in set
    - Pre diagnosis
    - Learner view progress in set
    - Set complete

    NEXT STATE
    GET View Set Tree
        -> GET Choose Set    ...when set is complete
        -> GET Choose Unit   ...when in learn or review mode
        -> GET Learn Card    ...when in diagnosis
            (Unit auto chosen)

    TODO-2 merge with get_set_units_route
    TODO-2 simplify this method
    """

    db_conn = request['db_conn']

    set_ = Set.get(db_conn, entity_id=set_id)

    if not set_:
        return abort(404)

    units = set_.list_units(db_conn)

    # For the menu, it must return the name and ID of the set
    output = {
        'set': set_.deliver(),
        'units': [u.deliver() for u in units],
    }

    current_user = get_current_user(request)

    if not current_user:
        return 200, output

    context = get_learning_context(current_user) if current_user else {}
    buckets = traverse(db_conn, current_user, set_)
    output['buckets'] = {
        'diagnose': [u['entity_id'] for u in buckets['diagnose']],
        'review': [u['entity_id'] for u in buckets['review']],
        'learn': [u['entity_id'] for u in buckets['learn']],
        'done': [u['entity_id'] for u in buckets['done']],
    }

    # If we are just previewing, don't update anything
    if set_id != context.get('set', {}).get('entity_id'):
        return 200, output

    # When in diagnosis, choose the unit and card automagically.
    if buckets['diagnose']:
        unit = buckets['diagnose'][0]
        card = choose_card(db_conn, current_user, unit)
        next_ = {
            'method': 'GET',
            'path':
            '/s/cards/{card_id}/learn'.format(card_id=card['entity_id']),
        }
        set_learning_context(current_user,
                             next=next_,
                             unit=unit.data,
                             card=card.data)

    # When in learn or review mode, lead me to choose a unit.
    elif buckets['review'] or buckets['learn']:
        next_ = {
            'method': 'GET',
            'path': '/s/sets/{set_id}/units'.format(set_id=set_id),
        }
        set_learning_context(current_user, next=next_)

    # If the set is complete, lead the learner to choose another set.
    else:
        next_ = {
            'method': 'GET',
            'path':
            '/s/users/{user_id}/sets'.format(user_id=current_user['id']),
        }
        set_learning_context(current_user, next=next_, unit=None, set=None)

    output['next'] = next_
    return 200, output
Example #5
0
def respond_to_card_route(request, card_id):
    """
    Record and process a learner's response to a card.

    NEXT STATE
    POST Respond Card
        -> GET Learn Card      ...when not ready
        -> GET Choose Unit     ...when ready, but still units
        -> GET View Set Tree   ...when ready and done
    """

    # TODO-3 simplify this method.
    #      perhaps smaller methods or move to model layer?

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    card = get_card_by_kind(card_id)
    if not card:
        return abort(404)

    # Make sure the card is the current one
    context = current_user.get_learning_context()
    if context.get('card', {}).get('entity_id') != card['entity_id']:
        return abort(400)

    r = seq_update(current_user, card, request['params'].get('response'))
    errors, response, feedback = (r.get('errors'), r.get('response'),
                                  r.get('feedback'))
    if errors:
        return 400, {
            'errors': errors,
            'ref': 'wtyOJPoy4bh76OIbYp8mS3LP',
        }

    set_ = Set(context.get('set'))
    unit = Unit(context.get('unit'))

    status = judge(unit, current_user)

    # If we are done with this current unit...
    if status == "done":
        buckets = traverse(current_user, set_)

        # If there are units to be diagnosed...
        if buckets['diagnose']:
            unit = buckets['diagnose'][0]
            next_card = choose_card(current_user, unit)
            next_ = {
                'method':
                'GET',
                'path':
                '/s/cards/{card_id}/learn'.format(
                    card_id=next_card['entity_id']),
            }
            current_user.set_learning_context(card=next_card.data,
                                              unit=unit.data,
                                              next=next_)

        # If there are units to be learned or reviewed...
        elif buckets['learn'] or buckets['review']:
            next_ = {
                'method': 'GET',
                'path':
                '/s/sets/{set_id}/units'.format(set_id=set_['entity_id']),
            }
            current_user.set_learning_context(card=None, unit=None, next=next_)

        # If we are out of units...
        else:
            next_ = {
                'method': 'GET',
                'path':
                '/s/sets/{set_id}/tree'.format(set_id=set_['entity_id']),
            }
            current_user.set_learning_context(card=None, unit=None, next=next_)

    # If we are still reviewing, learning or diagnosing this unit...
    else:
        next_card = choose_card(current_user, unit)
        if next_card:
            next_ = {
                'method':
                'GET',
                'path':
                '/s/cards/{card_id}/learn'.format(
                    card_id=next_card['entity_id']),
            }
            current_user.set_learning_context(card=next_card.data, next=next_)
        else:
            next_ = {}
            current_user.set_learning_context(next=next_)

    return 200, {
        'response': response.deliver(),
        'feedback': feedback,
        'next': next_,
    }
Example #6
0
def respond_to_card_route(request, card_id):
    """
    Record and process a learner's response to a card.

    NEXT STATE
    POST Respond Card
        -> GET Learn Card      ...when not ready
        -> GET Choose Unit     ...when ready, but still units
        -> GET View Set Tree   ...when ready and done
    """

    # TODO-3 simplify this method.
    #      perhaps smaller methods or move to model layer?

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    card = get_card_by_kind(card_id)
    if not card:
        return abort(404)

    # Make sure the card is the current one
    context = current_user.get_learning_context()
    if context.get('card', {}).get('entity_id') != card['entity_id']:
        return abort(400)

    r = seq_update(current_user, card, request['params'].get('response'))
    errors, response, feedback = (r.get('errors'), r.get('response'),
                                  r.get('feedback'))
    if errors:
        return 400, {
            'errors': errors,
            'ref': 'wtyOJPoy4bh76OIbYp8mS3LP',
        }

    set_ = Set(context.get('set'))
    unit = Unit(context.get('unit'))

    status = judge(unit, current_user)

    # If we are done with this current unit...
    if status == "done":
        buckets = traverse(current_user, set_)

        # If there are units to be diagnosed...
        if buckets['diagnose']:
            unit = buckets['diagnose'][0]
            next_card = choose_card(current_user, unit)
            next_ = {
                'method': 'GET',
                'path': '/s/cards/{card_id}/learn'
                        .format(card_id=next_card['entity_id']),
            }
            current_user.set_learning_context(
                card=next_card.data, unit=unit.data, next=next_)

        # If there are units to be learned or reviewed...
        elif buckets['learn'] or buckets['review']:
            next_ = {
                'method': 'GET',
                'path': '/s/sets/{set_id}/units'
                        .format(set_id=set_['entity_id']),
            }
            current_user.set_learning_context(card=None, unit=None, next=next_)

        # If we are out of units...
        else:
            next_ = {
                'method': 'GET',
                'path': '/s/sets/{set_id}/tree'
                        .format(set_id=set_['entity_id']),
            }
            current_user.set_learning_context(card=None, unit=None, next=next_)

    # If we are still reviewing, learning or diagnosing this unit...
    else:
        next_card = choose_card(current_user, unit)
        if next_card:
            next_ = {
                'method': 'GET',
                'path': '/s/cards/{card_id}/learn'
                        .format(card_id=next_card['entity_id']),
            }
            current_user.set_learning_context(card=next_card.data, next=next_)
        else:
            next_ = {}
            current_user.set_learning_context(next=next_)

    return 200, {
        'response': response.deliver(),
        'feedback': feedback,
        'next': next_,
    }
Example #7
0
def get_set_tree_route(request, set_id):
    """
    Render the tree of units that exists within a set.

    Contexts:
    - Search set, preview units in set
    - Pre diagnosis
    - Learner view progress in set
    - Set complete

    NEXT STATE
    GET View Set Tree
        -> GET Choose Set    ...when set is complete
        -> GET Choose Unit   ...when in learn or review mode
        -> GET Learn Card    ...when in diagnosis
            (Unit auto chosen)

    TODO-2 merge with get_set_units_route
    TODO-2 simplify this method
    """

    db_conn = request['db_conn']

    set_ = Set.get(db_conn, entity_id=set_id)

    if not set_:
        return abort(404)

    units = set_.list_units(db_conn)

    # For the menu, it must return the name and ID of the set
    output = {
        'set': set_.deliver(),
        'units': [u.deliver() for u in units],
    }

    current_user = get_current_user(request)

    if not current_user:
        return 200, output

    context = get_learning_context(current_user) if current_user else {}
    buckets = traverse(db_conn, current_user, set_)
    output['buckets'] = {
        'diagnose': [u['entity_id'] for u in buckets['diagnose']],
        'review': [u['entity_id'] for u in buckets['review']],
        'learn': [u['entity_id'] for u in buckets['learn']],
        'done': [u['entity_id'] for u in buckets['done']],
    }

    # If we are just previewing, don't update anything
    if set_id != context.get('set', {}).get('entity_id'):
        return 200, output

    # When in diagnosis, choose the unit and card automagically.
    if buckets['diagnose']:
        unit = buckets['diagnose'][0]
        card = choose_card(db_conn, current_user, unit)
        next_ = {
            'method': 'GET',
            'path': '/s/cards/{card_id}/learn'
                    .format(card_id=card['entity_id']),
        }
        set_learning_context(
            current_user,
            next=next_, unit=unit.data, card=card.data)

    # When in learn or review mode, lead me to choose a unit.
    elif buckets['review'] or buckets['learn']:
        next_ = {
            'method': 'GET',
            'path': '/s/sets/{set_id}/units'
                    .format(set_id=set_id),
        }
        set_learning_context(current_user, next=next_)

    # If the set is complete, lead the learner to choose another set.
    else:
        next_ = {
            'method': 'GET',
            'path': '/s/users/{user_id}/sets'
                    .format(user_id=current_user['id']),
        }
        set_learning_context(current_user, next=next_, unit=None, set=None)

    output['next'] = next_
    return 200, output
Example #8
0
def respond_to_card_route(request, card_id):
    """
  Record and process a learner's response to a card.

  NEXT STATE
  POST Respond Card
    -> GET Learn Card    ...when not ready
    -> GET Choose Unit   ...when ready, but still units
    -> GET View Subject Tree   ...when ready and done
  """

    # TODO-3 simplify this method.
    #    perhaps smaller methods or move to model layer?
    db_conn = request['db_conn']
    current_user = get_current_user(request)
    if not current_user:
        return abort(401, 'XDVEHHLRSZqQNJW4Zi_iqw')
    card = get_latest_accepted_card(db_conn, card_id)
    if not card:
        return abort(404, 'TQZ3SmAhS1qBd274C9DG0w')
    # Make sure the card is the current one
    context = get_learning_context(current_user)
    context_card_id = context.get('card', {}).get('entity_id')
    if context_card_id != convert_uuid_to_slug(card['entity_id']):
        return 400, {
            'errors': [{
                'message': 'Not the current card.',
                'ref': 'XfmF52NmQnK_bbaxx-p8dg',
            }]
        }
    result = seq_update(db_conn, current_user, card,
                        request['params'].get('response'))
    errors, response, feedback = (result.get('errors'), result.get('response'),
                                  result.get('feedback'))
    if errors:
        return 400, {
            'errors': errors,
            'ref': 'HfuW7_B-TByy8yh4FwgdrA',
        }

    subject = context.get('subject')
    unit = context.get('unit')

    status = judge(db_conn, unit, current_user)

    # If we are done with this current unit...
    if status == "done":
        buckets = traverse(db_conn, current_user, subject)

        # If there are units to be diagnosed...
        if buckets.get('diagnose'):
            unit = buckets['diagnose'][0]
            next_card = choose_card(db_conn, current_user, unit)
            next_ = {
                'method':
                'GET',
                'path':
                '/s/cards/{card_id}/learn'.format(
                    card_id=convert_uuid_to_slug(next_card['entity_id'])),
            }
            set_learning_context(current_user,
                                 card=next_card.data,
                                 unit=unit,
                                 next=next_)

        # If there are units to be learned or reviewed...
        elif buckets.get('learn') or buckets.get('review'):
            next_ = {
                'method':
                'GET',
                'path':
                '/s/subjects/{subject_id}/units'.format(
                    subject_id=convert_uuid_to_slug(subject['entity_id'])),
            }
            set_learning_context(current_user,
                                 card=None,
                                 unit=None,
                                 next=next_)

        # If we are out of units...
        else:
            next_ = {
                'method':
                'GET',
                'path':
                '/s/subjects/{subject_id}/tree'.format(
                    subject_id=convert_uuid_to_slug(subject['entity_id'])),
            }
            set_learning_context(current_user,
                                 card=None,
                                 unit=None,
                                 next=next_)

    # If we are still reviewing, learning or diagnosing this unit...
    else:
        next_card = choose_card(db_conn, current_user, unit)
        if next_card:
            next_ = {
                'method':
                'GET',
                'path':
                '/s/cards/{card_id}/learn'.format(
                    card_id=convert_uuid_to_slug(next_card['entity_id'])),
            }
            set_learning_context(current_user, card=next_card, next=next_)
        else:
            next_ = {}
            set_learning_context(current_user, next=next_)

    return 200, {
        'response': deliver_response(response),
        'feedback': feedback,
        'next': next_,
    }