'Extract the Data
@ToDo: Read the filters from the HTML view'

def _extract(self, r, **attr):

    db = current.db
    s3db = current.s3db
    status_from_activities = current.deployment_settings.get_project_status_from_activities()
    project_id = r.id
    vars_get = r.get_vars.get
    start_date = vars_get('date__ge')
    end_date = vars_get('date__le')
    goal_ids = vars_get('goal_id__belongs', [])
    indicator_ids = vars_get('indicator_id__belongs', [])
    activity_ids = vars_get('activity_id__belongs', [])
    if ((not start_date) and (not end_date) and (not goal_ids) and (not indicator_ids) and (not activity_ids)):
        record = r.record
        end_date = current.request.utcnow
        end_date = end_date.date()
        project = {'start_date': None, 'end_date': end_date}
        if status_from_activities:
            project.update(actual_progress=record.actual_progress_by_activities, planned_progress=record.planned_progress_by_activities)
        else:
            project.update(current_status=record.current_status_by_indicators, overall_status=record.overall_status_by_indicators)
        goals = {}
        table = s3db.project_goal
        query = ((table.project_id == project_id) & (table.deleted == False))
        fields = [table.id, table.code, table.name]
        if status_from_activities:
            fields += [table.actual_progress_by_activities, table.planned_progress_by_activities]
        else:
            fields += [table.current_status_by_indicators, table.overall_status_by_indicators]
        rows = db(query).select(*fields)
        if status_from_activities:
            for row in rows:
                goals[row.id] = dict(code=row.code, name=row.name, outcomes={}, actual_progress=row.actual_progress_by_activities, planned_progress=row.planned_progress_by_activities)
        else:
            for row in rows:
                goals[row.id] = dict(code=row.code, name=row.name, outcomes={}, current_status=row.current_status_by_indicators, overall_status=row.overall_status_by_indicators)
        table = s3db.project_outcome
        query = ((table.project_id == project_id) & (table.deleted == False))
        fields = [table.id, table.goal_id, table.code, table.name]
        if status_from_activities:
            fields += [table.actual_progress_by_activities, table.planned_progress_by_activities]
        else:
            fields += [table.current_status_by_indicators, table.overall_status_by_indicators]
        rows = db(query).select(*fields)
        if status_from_activities:
            for row in rows:
                goals[row.goal_id]['outcomes'][row.id] = dict(code=row.code, name=row.name, outputs={}, actual_progress=row.actual_progress_by_activities, planned_progress=row.planned_progress_by_activities)
        else:
            for row in rows:
                goals[row.goal_id]['outcomes'][row.id] = dict(code=row.code, name=row.name, outputs={}, current_status=row.current_status_by_indicators, overall_status=row.overall_status_by_indicators)
        table = s3db.project_output
        query = ((table.project_id == project_id) & (table.deleted == False))
        fields = [table.id, table.goal_id, table.outcome_id, table.code, table.name]
        if status_from_activities:
            fields += [table.actual_progress_by_activities, table.planned_progress_by_activities]
        else:
            fields += [table.current_status_by_indicators, table.overall_status_by_indicators]
        rows = db(query).select(*fields)
        if status_from_activities:
            for row in rows:
                goals[row.goal_id]['outcomes'][row.outcome_id]['outputs'][row.id] = dict(code=row.code, name=row.name, indicators={}, actual_progress=row.actual_progress_by_activities, planned_progress=row.planned_progress_by_activities)
        else:
            for row in rows:
                goals[row.goal_id]['outcomes'][row.outcome_id]['outputs'][row.id] = dict(code=row.code, name=row.name, indicators={}, current_status=row.current_status_by_indicators, overall_status=row.overall_status_by_indicators)
        table = s3db.project_indicator
        query = ((table.project_id == project_id) & (table.deleted == False))
        fields = [table.id, table.goal_id, table.outcome_id, table.output_id, table.code, table.name]
        if status_from_activities:
            fields += [table.actual_progress_by_activities, table.planned_progress_by_activities]
        else:
            fields += [table.current_status_by_indicators, table.overall_status_by_indicators]
        rows = db(query).select(*fields)
        if status_from_activities:
            for row in rows:
                indicator_id = row.id
                goal_id = row.goal_id
                outcome_id = row.outcome_id
                output_id = row.output_id
                goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id] = dict(code=row.code, name=row.name, criteria={}, actual_progress=row.actual_progress_by_activities, planned_progress=row.planned_progress_by_activities)
        else:
            for row in rows:
                indicator_id = row.id
                goal_id = row.goal_id
                outcome_id = row.outcome_id
                output_id = row.output_id
                goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id] = dict(code=row.code, name=row.name, current_status=row.current_status_by_indicators, overall_status=row.overall_status_by_indicators)
        if status_from_activities:
            table = s3db.project_indicator_criteria
            query = ((table.project_id == project_id) & (table.deleted == False))
            rows = db(query).select(table.id, table.goal_id, table.outcome_id, table.output_id, table.indicator_id, table.name, table.actual_progress_by_activities, table.planned_progress_by_activities)
            for row in rows:
                criteria_id = row.id
                goal_id = row.goal_id
                outcome_id = row.outcome_id
                output_id = row.output_id
                indicator_id = row.indicator_id
                goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id]['criteria'][criteria_id] = dict(name=row.name, activities={}, actual_progress=row.actual_progress_by_activities, planned_progress=row.planned_progress_by_activities)
            table = s3db.project_criteria_activity
            atable = s3db.project_activity
            ltable = s3db.project_criteria_activity_activity
            query = ((((table.project_id == project_id) & (table.deleted == False)) & (ltable.criteria_activity_id == table.id)) & (ltable.activity_id == atable.id))
            rows = db(query).select(ltable.activity_id, atable.name, table.goal_id, table.outcome_id, table.output_id, table.indicator_id, table.criteria_id, table.actual_progress, table.planned_progress)
            for row in rows:
                activity_id = row[ltable.activity_id]
                name = row[atable.name]
                row = row['project_criteria_activity']
                goal_id = row.goal_id
                outcome_id = row.outcome_id
                output_id = row.output_id
                indicator_id = row.indicator_id
                criteria_id = row.criteria_id
                goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id]['criteria'][criteria_id]['activities'][activity_id] = dict(name=name, actual_progress=row.actual_progress, planned_progress=row.planned_progress)
        return (project, goals)
    if current.deployment_settings.get_project_status_from_activities():
        if start_date:
            start_date = s3_decode_iso_datetime(start_date)
            start_date = start_date.date()
        if end_date:
            end_date = s3_decode_iso_datetime(end_date)
        else:
            end_date = current.request.utcnow
        end_date = end_date.date()
        project = {'start_date': start_date, 'end_date': end_date, 'actual_progress': 0, 'planned_progress': 0}
        goals = {}
        outcomes = {}
        outputs = {}
        indicators = {}
        criteria = {}
        limitby = None
        table = s3db.project_criteria_activity
        atable = s3db.project_activity
        ltable = s3db.project_criteria_activity_activity
        if activity_ids:
            activity_ids = activity_ids.split(',')
            if (len(activity_ids) == 1):
                query = (ltable.activity_id == activity_ids[0])
                limitby = (0, 1)
            else:
                query = ltable.activity_id.belongs(activity_ids)
        elif indicator_ids:
            if (len(indicator_ids) == 1):
                query = ((table.indicator_id == indicator_ids[0]) & (table.deleted == False))
            else:
                query = (table.indicator_id.belongs(indicator_ids) & (table.deleted == False))
        elif goal_ids:
            if (len(goal_ids) == 1):
                query = ((table.goal_id == goal_ids[0]) & (table.deleted == False))
            else:
                query = (table.goal_id.belongs(goal_ids) & (table.deleted == False))
        else:
            query = ((table.project_id == project_id) & (table.deleted == False))
        query &= (ltable.criteria_activity_id == table.id)
        query &= (atable.id == ltable.activity_id)
        if start_date:
            query &= (atable.date >= start_date)
        query &= (atable.end_date <= end_date)
        rows = db(query).select(ltable.activity_id, atable.name, table.goal_id, table.outcome_id, table.output_id, table.indicator_id, table.criteria_id, table.actual_progress, table.planned_progress, table.weighting)
        for row in rows:
            activity_id = row[ltable.activity_id]
            activity_name = row[atable.name]
            row = row['project_criteria_activity']
            goal_id = row.goal_id
            outcome_id = row.outcome_id
            output_id = row.output_id
            indicator_id = row.indicator_id
            criteria_id = row.criteria_id
            actual_progress = row.actual_progress
            planned_progress = row.planned_progress
            if (goal_id in goals):
                if (outcome_id in goals[goal_id]['outcomes']):
                    if (output_id in goals[goal_id]['outcomes'][outcome_id]['outputs']):
                        if (indicator_id in goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators']):
                            if (criteria_id in goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id]['criteria']):
                                goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id]['criteria'][criteria_id]['activities'][activity_id] = dict(name=activity_name, actual_progress=actual_progress, planned_progress=planned_progress)
                            else:
                                goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id]['criteria'][criteria_id] = {'activities': {activity_id: {'name': activity_name, 'actual_progress': actual_progress, 'planned_progress': planned_progress}}}
                        else:
                            goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id] = {'criteria': {criteria_id: {'activities': {activity_id: {'name': activity_name, 'actual_progress': actual_progress, 'planned_progress': planned_progress}}}}}
                    else:
                        goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id] = {'indicators': {indicator_id: {'criteria': {criteria_id: {'activities': {activity_id: {'name': activity_name, 'actual_progress': actual_progress, 'planned_progress': planned_progress}}}}}}}
                else:
                    goals[goal_id]['outcomes'][outcome_id] = {'outputs': {output_id: {'indicators': {indicator_id: {'criteria': {criteria_id: {'activities': {activity_id: {'name': activity_name, 'actual_progress': actual_progress, 'planned_progress': planned_progress}}}}}}}}}
            else:
                goals[goal_id] = {'actual_progress': 0, 'planned_progress': 0, 'outcomes': {outcome_id: {'outputs': {output_id: {'indicators': {indicator_id: {'criteria': {criteria_id: {'activities': {activity_id: {'name': activity_name, 'actual_progress': actual_progress, 'planned_progress': planned_progress}}}}}}}}}}}
            weighting = row.weighting
            if (actual_progress is None):
                actual_progress = 0
            if (planned_progress is None):
                planned_progress = 0
            actual_progress = (actual_progress * weighting)
            planned_progress = (planned_progress * weighting)
            if (criteria_id not in criteria):
                criteria[criteria_id] = {'actual_progress': actual_progress, 'planned_progress': planned_progress}
            else:
                c = criteria[criteria_id]
                c['actual_progress'] += actual_progress
                c['planned_progress'] += planned_progress
        table = s3db.project_indicator_criteria
        if indicator_ids:
            if (len(indicator_ids) == 1):
                query = ((table.indicator_id == indicator_ids[0]) & (table.deleted == False))
            else:
                query = (table.indicator_id.belongs(indicator_ids) & (table.deleted == False))
        elif goal_ids:
            if (len(goal_ids) == 1):
                query = ((table.goal_id == goal_ids[0]) & (table.deleted == False))
            else:
                query = (table.goal_id.belongs(goal_ids) & (table.deleted == False))
        else:
            query = ((table.project_id == project_id) & (table.deleted == False))
        rows = db(query).select(table.id, table.goal_id, table.outcome_id, table.output_id, table.indicator_id, table.name, table.weighting)
        for row in rows:
            criteria_id = row.id
            criteria_name = row.name
            goal_id = row.goal_id
            outcome_id = row.outcome_id
            output_id = row.output_id
            indicator_id = row.indicator_id
            if (criteria_id in criteria):
                actual_progress = criteria[criteria_id]['actual_progress']
                planned_progress = criteria[criteria_id]['planned_progress']
            else:
                actual_progress = 0
                planned_progress = 0
            if (criteria_id in goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id]['criteria']):
                goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id]['criteria'][criteria_id].update(name=criteria_name, actual_progress=actual_progress, planned_progress=planned_progress)
            else:
                goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id]['criteria'][criteria_id] = {'activities': {}, 'name': criteria_name, 'actual_progress': actual_progress, 'planned_progress': planned_progress}
            weighting = row.weighting
            actual_progress = (actual_progress * weighting)
            planned_progress = (planned_progress * weighting)
            if (indicator_id not in indicators):
                indicators[indicator_id] = {'actual_progress': actual_progress, 'planned_progress': planned_progress}
            else:
                i = indicators[indicator_id]
                i['actual_progress'] += actual_progress
                i['planned_progress'] += planned_progress
        table = s3db.project_indicator
        limitby = None
        if indicator_ids:
            if (len(indicator_ids) == 1):
                limitby = (0, 1)
                query = ((table.id == indicator_ids[0]) & (table.deleted == False))
            else:
                query = (table.id.belongs(indicator_ids) & (table.deleted == False))
        elif goal_ids:
            if (len(goal_ids) == 1):
                query = ((table.goal_id == goal_ids[0]) & (table.deleted == False))
            else:
                query = (table.goal_id.belongs(goal_ids) & (table.deleted == False))
        else:
            query = ((table.project_id == project_id) & (table.deleted == False))
        rows = db(query).select(table.id, table.goal_id, table.outcome_id, table.output_id, table.code, table.name, table.weighting, limitby=limitby)
        for row in rows:
            indicator_id = row.id
            indicator_name = row.name
            goal_id = row.goal_id
            outcome_id = row.outcome_id
            output_id = row.output_id
            if (indicator_id in indicators):
                actual_progress = indicators[indicator_id]['actual_progress']
                planned_progress = indicators[indicator_id]['planned_progress']
            else:
                actual_progress = 0
                planned_progress = 0
            if (indicator_id in goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators']):
                goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id].update(code=row.code, name=indicator_name, actual_progress=actual_progress, planned_progress=planned_progress)
            else:
                goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id] = {'criteria': {}, 'code': row.code, 'name': indicator_name, 'actual_progress': actual_progress, 'planned_progress': planned_progress}
            weighting = row.weighting
            actual_progress = (actual_progress * weighting)
            planned_progress = (planned_progress * weighting)
            if (output_id not in outputs):
                outputs[output_id] = {'actual_progress': actual_progress, 'planned_progress': planned_progress}
            else:
                o = outputs[output_id]
                o['actual_progress'] += actual_progress
                o['planned_progress'] += planned_progress
        table = s3db.project_output
        if goal_ids:
            if (len(goal_ids) == 1):
                query = ((table.goal_id == goal_ids[0]) & (table.deleted == False))
            else:
                query = (table.goal_id.belongs(goal_ids) & (table.deleted == False))
        else:
            query = ((table.project_id == project_id) & (table.deleted == False))
        rows = db(query).select(table.id, table.goal_id, table.outcome_id, table.code, table.name, table.weighting)
        for row in rows:
            output_id = row.id
            output_name = row.name
            goal_id = row.goal_id
            outcome_id = row.outcome_id
            if (output_id in outputs):
                actual_progress = outputs[output_id]['actual_progress']
                planned_progress = outputs[output_id]['planned_progress']
            else:
                actual_progress = 0
                planned_progress = 0
            if (output_id in goals[goal_id]['outcomes'][outcome_id]['outputs']):
                goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id].update(code=row.code, name=output_name, actual_progress=actual_progress, planned_progress=planned_progress)
            else:
                goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id] = {'indicators': {}, 'code': row.code, 'name': output_name, 'actual_progress': actual_progress, 'planned_progress': planned_progress}
            weighting = row.weighting
            actual_progress = (actual_progress * weighting)
            planned_progress = (planned_progress * weighting)
            if (outcome_id not in outcomes):
                outcomes[outcome_id] = {'actual_progress': actual_progress, 'planned_progress': planned_progress}
            else:
                o = outcomes[outcome_id]
                o['actual_progress'] += actual_progress
                o['planned_progress'] += planned_progress
        table = s3db.project_outcome
        if goal_ids:
            if (len(goal_ids) == 1):
                query = ((table.goal_id == goal_ids[0]) & (table.deleted == False))
            else:
                query = (table.goal_id.belongs(goal_ids) & (table.deleted == False))
        else:
            query = ((table.project_id == project_id) & (table.deleted == False))
        rows = db(query).select(table.id, table.goal_id, table.code, table.name, table.weighting)
        for row in rows:
            outcome_id = row.id
            outcome_name = row.name
            goal_id = row.goal_id
            if (outcome_id in outcomes):
                actual_progress = outcomes[outcome_id]['actual_progress']
                planned_progress = outcomes[outcome_id]['planned_progress']
            else:
                actual_progress = 0
                planned_progress = 0
            if (outcome_id in goals[goal_id]['outcomes']):
                goals[goal_id]['outcomes'][outcome_id].update(code=row.code, name=outcome_name, actual_progress=actual_progress, planned_progress=planned_progress)
            else:
                goals[goal_id]['outcomes'][outcome_id] = {'outputs': {}, 'code': row.code, 'name': outcome_name, 'actual_progress': actual_progress, 'planned_progress': planned_progress}
            weighting = row.weighting
            actual_progress = (actual_progress * weighting)
            planned_progress = (planned_progress * weighting)
            if (goal_id not in goals):
                goals[goal_id] = {'actual_progress': actual_progress, 'planned_progress': planned_progress}
            else:
                g = goals[goal_id]
                g['actual_progress'] += actual_progress
                g['planned_progress'] += planned_progress
        table = s3db.project_goal
        limitby = None
        if goal_ids:
            if (len(goal_ids) == 1):
                limitby = (0, 1)
                query = ((table.id == goal_ids[0]) & (table.deleted == False))
            else:
                query = (table.id.belongs(goal_ids) & (table.deleted == False))
        else:
            query = ((table.project_id == project_id) & (table.deleted == False))
        rows = db(query).select(table.id, table.code, table.name, table.weighting, limitby=None)
        for row in rows:
            goal_id = row.id
            goal_name = row.name
            if (goal_id in goals):
                actual_progress = outcomes[goal_id]['actual_progress']
                planned_progress = outcomes[goal_id]['planned_progress']
                goals[goal_id].update(code=row.code, name=goal_name, actual_progress=actual_progress, planned_progress=planned_progress)
            else:
                actual_progress = 0
                planned_progress = 0
                goals[goal_id] = {'outcomes': {}, 'name': goal_name, 'code': row.code, 'actual_progress': actual_progress, 'planned_progress': planned_progress}
            weighting = row.weighting
            actual_progress = (actual_progress * weighting)
            planned_progress = (planned_progress * weighting)
            project['actual_progress'] += actual_progress
            project['planned_progress'] += planned_progress
        return (project, goals)
    limitby = None
    goals = {}
    table = s3db.project_goal
    if goal_ids:
        goal_ids = goal_ids.split(',')
        if (len(goal_ids) == 1):
            query = (table.id == goal_ids[0])
            limitby = (0, 1)
        else:
            query = table.id.belongs(goal_ids)
    else:
        query = ((table.project_id == project_id) & (table.deleted == False))
    fields = [table.id, table.code, table.name]
    if status_from_activities:
        fields += [table.actual_progress_by_activities, table.planned_progress_by_activities]
    else:
        fields += [table.current_status_by_indicators, table.overall_status_by_indicators]
    rows = db(query).select(limitby=limitby, *fields)
    if status_from_activities:
        for row in rows:
            goals[row.id] = dict(code=row.code, name=row.name, outcomes={}, actual_progress=row.actual_progress_by_activities, planned_progress=row.planned_progress_by_activities)
    else:
        for row in rows:
            goals[row.id] = dict(code=row.code, name=row.name, outcomes={}, current_status=row.current_status_by_indicators, overall_status=row.overall_status_by_indicators)
    table = s3db.project_outcome
    if goal_ids:
        if (len(goal_ids) == 1):
            query = (table.goal_id == goal_ids[0])
        else:
            query = table.goal_id.belongs(goal_ids)
    else:
        query = (table.project_id == project_id)
    query &= (table.deleted == False)
    fields = [table.id, table.goal_id, table.code, table.name]
    if status_from_activities:
        fields += [table.actual_progress_by_activities, table.planned_progress_by_activities]
    else:
        fields += [table.current_status_by_indicators, table.overall_status_by_indicators]
    rows = db(query).select(*fields)
    if status_from_activities:
        for row in rows:
            goals[row.goal_id]['outcomes'][row.id] = dict(code=row.code, name=row.name, outputs={}, actual_progress=row.actual_progress_by_activities, planned_progress=row.planned_progress_by_activities)
    else:
        for row in rows:
            goals[row.goal_id]['outcomes'][row.id] = dict(code=row.code, name=row.name, outputs={}, current_status=row.current_status_by_indicators, overall_status=row.overall_status_by_indicators)
    table = s3db.project_output
    if goal_ids:
        if (len(goal_ids) == 1):
            query = (table.goal_id == goal_ids[0])
        else:
            query = table.goal_id.belongs(goal_ids)
    else:
        query = (table.project_id == project_id)
    query &= (table.deleted == False)
    fields = [table.id, table.goal_id, table.outcome_id, table.code, table.name]
    if status_from_activities:
        fields += [table.actual_progress_by_activities, table.planned_progress_by_activities]
    else:
        fields += [table.current_status_by_indicators, table.overall_status_by_indicators]
    rows = db(query).select(*fields)
    if status_from_activities:
        for row in rows:
            goals[row.goal_id]['outcomes'][row.outcome_id]['outputs'][row.id] = dict(code=row.code, name=row.name, indicators={}, actual_progress=row.actual_progress_by_activities, planned_progress=row.planned_progress_by_activities)
    else:
        for row in rows:
            goals[row.goal_id]['outcomes'][row.outcome_id]['outputs'][row.id] = dict(code=row.code, name=row.name, indicators={}, current_status=row.current_status_by_indicators, overall_status=row.overall_status_by_indicators)
    limitby = None
    indicators = {}
    table = s3db.project_indicator
    if indicator_ids:
        indicator_ids = indicator_ids.split(',')
        if (len(indicator_ids) == 1):
            query = (table.id == indicator_ids[0])
            limitby = (0, 1)
        else:
            query = table.id.belongs(indicator_ids)
    elif goal_ids:
        if (len(goal_ids) == 1):
            query = ((table.goal_id == goal_ids[0]) & (table.deleted == False))
        else:
            query = (table.goal_id.belongs(goal_ids) & (table.deleted == False))
    else:
        query = ((table.project_id == project_id) & (table.deleted == False))
    fields = [table.id, table.goal_id, table.outcome_id, table.output_id, table.code, table.name]
    if status_from_activities:
        fields += [table.actual_progress_by_activities, table.planned_progress_by_activities]
    else:
        fields += [table.current_status_by_indicators, table.overall_status_by_indicators]
    rows = db(query).select(limitby=limitby, *fields)
    if (not indicator_ids):
        iappend = indicator_ids.append
    else:
        iappend = (lambda i: i)
    if status_from_activities:
        for row in rows:
            indicator_id = row.id
            iappend(indicator_id)
            goal_id = row.goal_id
            outcome_id = row.outcome_id
            output_id = row.output_id
            indicators[indicator_id] = dict(goal=goal_id, outcome=outcome_id, output=output_id)
            goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id] = dict(code=row.code, name=row.name, actual_progress=row.actual_progress_by_activities, planned_progress=row.planned_progress_by_activities)
    else:
        for row in rows:
            indicator_id = row.id
            iappend(indicator_id)
            goal_id = row.goal_id
            outcome_id = row.outcome_id
            output_id = row.output_id
            indicators[indicator_id] = dict(goal=goal_id, outcome=outcome_id, output=output_id)
            goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id] = dict(code=row.code, name=row.name, current_status=row.current_status_by_indicators, overall_status=row.overall_status_by_indicators)
    if status_from_activities:
        limitby = None
        table = s3db.project_criteria_activity
        atable = s3db.project_activity
        ltable = s3db.project_criteria_activity_activity
        if activity_ids:
            activity_ids = activity_ids.split(',')
            if (len(activity_ids) == 1):
                query = (ltable.activity_id == activity_ids[0])
                limitby = (0, 1)
            else:
                query = ltable.activity_id.belongs(activity_ids)
        elif indicator_ids:
            if (len(indicator_ids) == 1):
                query = ((table.indicator_id == indicator_ids[0]) & (table.deleted == False))
            else:
                query = (table.indicator_id.belongs(indicator_ids) & (table.deleted == False))
        elif goal_ids:
            if (len(goal_ids) == 1):
                query = ((table.goal_id == goal_ids[0]) & (table.deleted == False))
            else:
                query = (table.goal_id.belongs(goal_ids) & (table.deleted == False))
        else:
            query = ((table.project_id == project_id) & (table.deleted == False))
        query &= (ltable.criteria_activity_id == table.id)
        rows = db(query).select(ltable.activity_id, table.goal_id, table.outcome_id, table.output_id, table.indicator_id, atable.name, table.actual_progress, table.planned_progress, limitby=limitby)
        for row in rows:
            activity_id = row[ltable.activity_id]
            name = row[atable.name]
            row = row['project_criteria_activity']
            goal_id = row.goal_id
            outcome_id = row.outcome_id
            output_id = row.output_id
            indicator_id = row.indicator_id
            indicators[indicator_id] = dict(goal=goal_id, outcome=outcome_id, output=output_id)
            goals[goal_id]['outcomes'][outcome_id]['outputs'][output_id]['indicators'][indicator_id] = dict(name=name, actual_progress=row.actual_progress_by_activities, planned_progress=row.planned_progress_by_activities)
    return (project, goals)
