utils.py 5.02 KB
Newer Older
Daniel W Bond's avatar
Daniel W Bond committed
1 2
from mainapp.models import Course # import more

3
### common functionality
4

5 6 7 8 9 10 11 12 13 14 15
def programCourses(program):

    programCourses = []
    requirements = program.requirements
    for requirement in requirements:
        for coursegroup in requirement.coursegroup:
            for course in coursegroup.courses:
                programCourses.append(course)

    return programCourses

16 17 18 19 20 21
### creating a trajectory

#def maxProgramsAllowed():
#    """ the maximum programs allowed, e.g. 2 majors and 3 minors tops """
#   return True

22
### automatically building a trajectory
Daniel W Bond's avatar
Daniel W Bond committed
23

24 25 26
def assignedWeights(weightedCourse, programCourses):
    """ assign weights to all courses in a program for automation """
    assignedWeights = {}
27

28 29
    for weightedCourse in programCourses:
        weightedCourseCounter = 0
30
        reqs = set()
31 32 33 34
        for prereq in weightedCourse.preq:
            reqs.add(prereq)
        for coreq in weightedCourse.coreq:
            reqs.add(coreq)
35 36
        for req in reqs:
            for course in programCourses:
37
                childReqList = []
38
                if req is course:
39 40
                    weightedCourseCounter += 1
                    childReqList.append(course)
41
                    assignedWeights(course, childReqList)
42
                
43
        assignedWeights[weighedCourse] = coursecounter
44 45
        
    return assignWeights
Daniel W Bond's avatar
Daniel W Bond committed
46

47 48 49 50 51 52 53 54 55 56 57 58 59
def customAssignedWeights(assignedWeights, selectedCourses):
    """ remove courses that a student has not selected from the weighted
        courses """
    customAssignedWeights = assignedWeights

    for course in selectedCourses:
        del customAssignedWeights[course]

    return customAssignedWeights

def shortestPath(assignedWeights):
    return True

60
### editing a trajectory
Daniel W Bond's avatar
Daniel W Bond committed
61

62 63 64 65 66 67
def requirementsFulfilled(alreadyTaken, program):
    """ for *A* program, return a list of all requirements fulfilled """

    requirementsFulfilled = []
    alreadyTaken = set(alreadyTaken)
    requirements = program.requirements
68

69
    for requirement in requirements:
70 71 72 73 74 75
        for coursegroup in requirement.coursegroup:
            courseRequirements = set(coursegroup.courses)
            requirementCoursesTaken = intersection(courseRequirements, alreadyTaken)
        
            if len(requirementCoursesTaken) is requirement.coursegroup.numneeded:
                requirementsFulfilled.append(requirement)
76 77

    return requirementsFulfilled
Daniel W Bond's avatar
Daniel W Bond committed
78

79 80 81 82
#def alreadyTaken():
#    """ return all of the courses that a student has already taken so far
#        in the trajectory """
#    return True
83

84 85 86
def remainingReqCourses(alreadyTaken, program):
    """ returns the remaining required courses for a program given
    the already taken courses """
Daniel W Bond's avatar
Daniel W Bond committed
87

88
    alreadyTaken = set(alreadyTaken)
89
    programCourses = set( programCourses(program) )
Daniel W Bond's avatar
Daniel W Bond committed
90

91 92 93
    remainingReqCourses = intersection(alreadyTaken, programCourses)

    return remainingReqCourses
Daniel W Bond's avatar
Daniel W Bond committed
94

95
def nextCourses(remainingReqCourses, alreadyTaken):
Daniel W Bond's avatar
Daniel W Bond committed
96
    """ which courses you can take next based on prereqs and coreqs, given
97
        already taken courses and remaining required courses """
98 99 100
    nextCourses = []
    for course in remainingReqCourses:
        reqs = set()
101 102 103 104
        for prereq in course.preq:
            reqs.add(prereq)
        for coreq in course.coreq:
            reqs.add(coreq)
105 106 107
        for req in reqs:
            if req in previousCourses:
                nextCourses.append(course)
Daniel W Bond's avatar
Daniel W Bond committed
108

109
    return nextCourses
Daniel W Bond's avatar
Daniel W Bond committed
110

111 112 113 114 115 116
# IMPLEMENT THIS IN JS!
#def maxCreditsAllowed():
#    """ the maximum credits allowed for a semester-- returns different values
#        to warn or disallow if maximum credits are touched or exceeded """

def findDependancies(deletedCourse, semester):
Daniel W Bond's avatar
Daniel W Bond committed
117 118
    """ if a student removes a course while editing, find all courses
        that have require the removed courses """
119 120 121
    foundDependancies = []
    for suspectCourse in semester.nextSemester.courses:
        reqs = set()
122 123 124 125
        for prereq in suspectCourse.preq:
            reqs.add(prereq)
        for coreq in suspectCourse.coreq:
            reqs.add(coreq)
126 127 128
            for req in reqs:
                if req is deletedCourse:
                    req.append(foundDependancies)
129 130

    findDependancies(deletedCourse, semester.nextSemester)
131 132

    return foundDependancies
Daniel W Bond's avatar
Daniel W Bond committed
133

Daniel W Bond's avatar
Daniel W Bond committed
134
def enoughCredits(previousCourses, numRequired):
Daniel W Bond's avatar
Daniel W Bond committed
135
    """ checks if enough credits have been taken to graduate """
Daniel W Bond's avatar
Daniel W Bond committed
136 137 138 139 140 141 142 143 144
    enoughCredits = False
    numTaken = 0
    for course in previousCourses:
        numTaken += course.credits
    
    if numTaken >= numRequired:
        enoughCredits = True

    return enoughCredits
Daniel W Bond's avatar
Daniel W Bond committed
145

146
### student page
Daniel W Bond's avatar
Daniel W Bond committed
147

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
def allTrajectories(topSemester):
    """ returns all paths to each end node of a trajectories tree
        for the user page, do this over all trees """
    # traverse the tree

    # find the end leafs

    # find the shortest path to each leaf

    # add the list of that semesters to the allTrajectories list
    
    # return said list of lists

    #endLeafs = []
    #for
    allTrajectories  = []
    #for
    #    singleTrajectory = []
    #    singleTrajectory.append(semester)
    #    allTrajectories.append(singleTrajectory)

    return allTrajectories