Example #1
0
def result():
    # read html template
    source = open('templates/administerTest.html','rU')
    html = source.read()
    source.close()

    fromQS = request.form
    name = fromQS['name']
    testChoice = fromQS['testChoice']

    # create personalized dictionary
    allStudents = csvToDict.csvToDict("client/students/"+testChoice)
    # print allStudents
    studentDict = allStudents[name]
    # print studentDict
    questionNumber = int(studentDict['questionNumber'])
    # print questionNumber

    # get test details
    testCsv = csvToDict.csvToDict("client/tests/"+testChoice)
    # print testCsv

    # for received answer
    score = int(studentDict['score'])
    continuation = "answer" in fromQS # if continuation of the recursive program, could've tested for any hidden input sent later
    if continuation and questionNumber < len(testCsv)+1:
        lastQuestion = fromQS['lastQuestion']
        # see if question is new
        if lastQuestion == testCsv[str(questionNumber)]['question']:
            # validate answer
            if fromQS['answer'] == testCsv[str(questionNumber)]['correctAnswer']:
                score += 1
            # update question number
            questionNumber += 1
            # update csv file
            testModule.update_csv(name,testChoice,questionNumber,score)

    if questionNumber < len(testCsv)+1:
        # get question details
        questionDetails = testCsv[str(questionNumber)]
        # update html
        html = html.replace("question_placeholder",questionDetails['question'])
        html = html.replace("a_placeholder",questionDetails['a'])
        html = html.replace("b_placeholder",questionDetails['b'])
        html = html.replace("c_placeholder",questionDetails['c'])
        html = html.replace("d_placeholder",questionDetails['d'])
        html = html.replace("lastQuestion_placeholder",questionDetails['question'])
        html = html.replace("number_placeholder",str(questionNumber))
        html = html.replace("name_placeholder",name)
        html = html.replace("testChoice_placeholder",testChoice)
        # produce html
        #print(html)
        return html
    else:
        #print(testModule.handleCompletion(name,score,questionNumber-1))
        return testModule.handleCompletion(name,score,questionNumber-1)
Example #2
0
    def create(questionList):
        testContent = 'number,question,a,b,c,d,correctAnswer\n'
        field = 0
        while field < len(questionList):
            testContent += str((field + 6) / 6) + ","
            testContent += questionList[field] + ","
            testContent += questionList[field + 1] + ","
            testContent += questionList[field + 2] + ","
            testContent += questionList[field + 3] + ","
            testContent += questionList[field + 4] + ","
            testContent += questionList[field + 5] + "\n"
            field += 6
        # write questions into test file
        dest = open('client/tests/' + name, 'w')
        dest.write(testContent[:len(testContent) - 1])
        dest.close()

        studentContent = 'student,questionNumber,score\n'
        from app import csvToDict
        studentDatabase = csvToDict.csvToDict('client/data/accounts.csv')
        for student in studentDatabase:
            studentContent += student + ",1,0\n"
        dest = open('client/students/' + name, 'w')
        dest.write(studentContent)
        dest.close()
Example #3
0
 def redify():
     # determine if end of test
     testCsv = csvToDict.csvToDict("client/tests/" + testChoice)
     notEndOfTest = questionNumber < len(testCsv)
     if notEndOfTest:
         return ' style="color:red";'
     return ""
def result():
    fromQS = request.form

    student = fromQS["registerName"]
    password = fromQS["registerPassword"]
    # print student, password

    # check validity of student name
    valid = not "," in student
    from app import csvToDict
    studentDatabase = csvToDict.csvToDict('client/data/accounts.csv')
    for name in studentDatabase:
        valid = valid and not name == student

    # read template
    source = open('templates/after_login.html', 'rU')  # need the same template
    html = source.read()
    source.close()
    body = """

    <p align="right">
    <b><a href="login" style="text-decorations:none; color:inherit;">Log out</a></b>
    </p>
    """

    # update body
    if valid:
        addAccount(student, password)
        body += '<div align="center"><h1> <u>student_placeholder</u> is now registered </h1>\n<h2>password: <u>' + password + '</u></h2></div>'
        html = html.replace("title_placeholder", "Student Registered")
    else:
        body += '<div align="center"><h1> "student_placeholder" is an invalid name </h1></div>'
        html = html.replace("title_placeholder", "Failed Registration")
    body = body.replace("student_placeholder", student)

    # produce html
    html = html.replace("body_template", body)
    # print(html)
    return html
Example #5
0
def result():
    fromQS = request.form
    testChoice = fromQS['testChoice']

    # dealing with resetting data
    def reset(name):
        testModule.update_csv(name, testChoice, 1, 0)

    for name in fromQS:
        if name == "reset":
            if isinstance(fromQS[name], list):
                position = 0
                while position < len(fromQS[name]):
                    # print fromQS[name][position].value
                    reset(fromQS[name][position])
                    position += 1
            else:
                # print fromQS[name].value
                reset(fromQS[name])

    csvDict = csvToDict.csvToDict('client/students/' + testChoice)
    csvList = csvDict.keys()
    csvList = sorted(csvList)

    # print csvList

    # successful teacher login
    def success(testChoice):
        def fraction(name):
            if questionNumber != 0:  # for users that didn't take the test
                return csvDict[name]["score"] + "/" + str(
                    int(csvDict[name]["questionNumber"]) - 1)
            else:
                return " - "

        def percent(name):
            if questionNumber != 0:  # for users that didn't take the test
                return str(
                    int(
                        int(csvDict[name]["score"]) / float(questionNumber) *
                        100))
            else:
                return " - "

        def redify():
            # determine if end of test
            testCsv = csvToDict.csvToDict("client/tests/" + testChoice)
            notEndOfTest = questionNumber < len(testCsv)
            if notEndOfTest:
                return ' style="color:red";'
            return ""

        # create table
        table = ""
        for name in csvList[1:]:  # to splice out empty string
            questionNumber = (int(csvDict[name]["questionNumber"]) - 1)
            table += '''  <tr''' + redify() + '''>
        <td align="center">
          ''' + name + '''
        </td>
        <td align="center">
          ''' + fraction(name) + '''
        </td>
        <td align="center">
          ''' + percent(name) + '''
        </td>
        <td align="center">
          <input type="checkbox" name="reset" value="''' + name + '''">
        </td>
      </tr>\n'''

        # produce html
        return table

    # read template
    source = open("templates/teacherStats.html", 'rU')
    template = source.read()
    source.close()

    # change and produce html
    html = template.replace("table_placeholder", success(testChoice))
    html = html.replace("testChoice_value", testChoice)

    scorePercents = []
    del csvDict[""]
    for name in csvDict:
        # from redify function above
        totalNumberQ = len(csvToDict.csvToDict("client/tests/" + testChoice))
        if int(csvDict[name]
               ["questionNumber"]) - 1 == totalNumberQ:  # if completed test
            scorePercents.append(
                int(int(csvDict[name]["score"]) / float(totalNumberQ) * 100))

    mean = str(statModule.mean(scorePercents))
    median = str(statModule.median(scorePercents))
    mode = str(statModule.mode(scorePercents))
    maxValue = str(statModule.customMax(scorePercents))
    minValue = str(statModule.customMin(scorePercents))

    html = html.replace("mean_placeholder", mean)
    html = html.replace("median_placeholder", median)
    html = html.replace("mode_placeholder", mode)
    html = html.replace("max_placeholder", maxValue)
    html = html.replace("min_placeholder", minValue)

    # print(html)
    return html
Example #6
0
def result():
    fromQS = request.form
    if "student" in fromQS and fromQS['student'] != "":  # if student logs in
        accounts = csvToDict.csvToDict('client/data/accounts.csv')
        # adapted from hw54 (login page)
        name = fromQS['studentName']
        try:
            password = fromQS['studentPassword']
            success = accounts[name]['password'] == password
            if success:
                title = "Welcome student!"
            elif password == "":
                title = "Missing Password"
            else:
                title = "Invalid Password"
        except:
            title = "Error"
    elif "name" in fromQS and fromQS[
            'name'] != "":  # if student finishes a test and wants to take another one
        accounts = csvToDict.csvToDict('client/data/accounts.csv')
        name = fromQS['name']
        title = "Welcome student!"
    else:  # if "teacher" in fromQS, if teacher logs in
        accounts = csvToDict.csvToDict('client/data/teachers.csv')
        action = fromQS['teacherAction']
        try:
            password = fromQS['teacherPassword']
            success = accounts[action]['password'] == password
            if success:
                title = "Welcome teacher!"
            elif password == "":
                title = "Missing Password"
            else:
                title = "Invalid Password"
        except:
            title = "Error"

    # read html template
    source = open("templates/after_login.html", 'rU')
    template = source.read()
    source.close()
    html = template.replace("title_placeholder", title)

    # update html

    if title == "Missing Password":
        body = '<h2> Please enter a password. </h2> <a href="login"> Retry </a>'
        html = html.replace("body_template", body)
    elif title == "Invalid Password":
        body = '<h2> Wrong password. Please try again. </h2> <a href="login"> Retry </a>'
        html = html.replace("body_template", body)
    elif title == "Error":
        body = '<h2> Error. Please try again. </h2> <a href="login"> Retry </a>'
        html = html.replace("body_template", body)
    else:  # if successful login
        if title == "Welcome student!":
            source = open("templates/studentLogin.html", 'rU')
            body = source.read()
            source.close()
            html = html.replace("body_template", body)
            html = html.replace("name_placeholder", name)
            inputs = '<h1> Please select a test. </h1>' + loginModule.htmlChoices(
                loginModule.testChoices())
            html = html.replace("inputs_placeholder", inputs)
        else:  # if title == "Welcome teacher!"
            source = open("templates/teacherLogin.html", 'rU')
            body = source.read()
            source.close()
            html = html.replace("body_template", body)
            inputs = '''<p align="right">
    <b><a href="login" style="text-decorations:none; color:inherit;">Log out</a></b>
    </p>'''
            if action == "View Scores":
                html = html.replace("action_placeholder", "teacherStats")
                inputs += '\n<h1> What scores do you want to view? </h1>\n' + loginModule.htmlChoices(
                    loginModule.testChoices())
            elif action == "Register a Student":
                html = html.replace("action_placeholder", "registerStudent")
                inputs += '\n<h1> Who do you want to register? </h1>\n' + loginModule.formNamePassword(
                )
            elif action == "Add Test":
                html = html.replace("action_placeholder", "createTest")
                inputs += '\n<h1> Please create the test form. </h1>\n' + loginModule.testGeneralInfo(
                )
            # add more options
            html = html.replace("inputs_placeholder", inputs)

    # produce html
    # print(html)
    return html