####################################################################################################################################################  
#                                                                                                                                                  #
# This file has been generated by Amit Patankar:                                                                                                   #
#     Created by              : amit.patankar                                                                                                      #    
#     Created on              : 14-07-2013                                                                                                         #
#     Directory               : /Desktop/                                                                                                          #
#     Purpose                 : This structure holds the attributes of each Answer Sheet.                                                          #
#                                                                                                                                                  #
####################################################################################################################################################

from Values import *
from Key import *
from Scored import *
from Summary import *
from Data import *
from Graph import *
import csv
from datetime import date

#Base User Class
class User(object):

    #This is the default constructor with all variables defined.
    def __init__(self, name, c):
        self.name = name
        self.c = c #class
        self.tests_taken = [] #array of scored tests for data analysis
        self.data = Data_Holder()
        self.scores = []  
        self.intended_date = default_intended_date #stored as string MM/DD/YYYY
        self.intended_score = default_intended_score #stored as integer from 0-2400


    def get_intended_date(self):
        today = date.today()
        datestring = self.intended_date.split('/')
        if len(datestring[2]) == 2:
            datestring[2] = '20' + datestring[2]
        test_date = date(int(datestring[2]), int(datestring[0]), int(datestring[1]))
        diff = abs(test_date - today)
        return diff.days

    #return 3 scores, math, reading, writing- from most recent test. 
    def recent_scores(self):
        s1 = []
        writing_scores = []
        reading_scores = []
        math_scores = []
        overall_score_difference = 0
        writing_score_difference = 0
        reading_score_difference = 0
        math_score_difference = 0
        if len(self.tests_taken) >= 1:

            for test in self.tests_taken:
                s1.append([date_converter(test.date),test.score_summary.total_score()])
                writing_scores.append([date_converter(test.date), test.score_summary.section_scores[WRITING_TYPE]])
                reading_scores.append([date_converter(test.date), test.score_summary.section_scores[READING_TYPE]])
                math_scores.append([date_converter(test.date), test.score_summary.section_scores[MATH_TYPE]])
            
                
            if len(self.tests_taken) > 1:
                overall_score_difference = (s1[-1][1]) - (s1[0][1])
                writing_score_difference = (writing_scores[-1][1]) - (writing_scores[0][1])
                reading_score_difference = (reading_scores[-1][1]) - (reading_scores[0][1])
                math_score_difference =  (math_scores[-1][1]) - (math_scores[0][1])
                if(overall_score_difference < 0):
                    overall_score_difference = 0
                if(math_score_difference < 0):
                    math_score_difference = 0
                if reading_score_difference<0:
                    reading_score_difference = 0
                if writing_score_difference < 0:
                    writing_score_difference = 0  
                return math_scores[-1][1], reading_scores[-1][1], writing_scores[-1][1], s1[-1][1], math_score_difference, writing_score_difference, reading_score_difference, overall_score_difference 
            else:
                return math_scores[0][1], reading_scores[0][1], writing_scores[0][1], s1[0][1], 0, 0, 0, 0

        else: # no tests taken yet 
            return 0,0,0,0,0,0,0,0

    def build(self):
        mkdir(user_directory(self.name, self.c))
        self.save_user()

    def grade(self, answers):
        t = Test(answers.id)
        ts = t.grade(answers)
        ss = Score_Summary(ts)
        #disable printing in console
        #print (ss)
        sections = {}
        sections[WRITING_TYPE] = ts.reports[WRITING_TYPE].incorrect_questions
        sections[MATH_TYPE] = ts.reports[MATH_TYPE].incorrect_questions
        sections[READING_TYPE] = ts.reports[READING_TYPE].incorrect_questions
        st = Scored_Test(answers.id)
        #pass Scored Test the Answer Test Date,Essay,Type
        st.recreate(sections)
        st.date = answers.date
        st.essay = answers.essay
        st.type = answers.type
        #put the scored test in the history page after it has been graded
        self.tests_taken.append(st)
        #add this new test to data sections
        self.stats_from_test(st)
        self.grade_HTML(ts, ss, st.type)


    def save_user(self):
        FILE = open(user_filename(self.name, self.c), "w")
        lines = []
        lines.append("Name:" + self.name + endl + endl)
        lines.append("Intended_Date: " + self.intended_date + endl)
        lines.append("Intended_Score: " + str(self.intended_score) + endl)
        for test in self.tests_taken:
            lines.append(str(test))
        FILE.writelines(lines)          
        FILE.close()


    def recreate_user(self, filename):
        with open(filename) as f:
            lines = f.readlines()
            assert(self.name in lines[0]) #verifies name
            for line in lines:
                line = line.strip()
                if line == '':
                    continue
                elif 'Intended_Date:' in line:
                    self.intended_date = line.split(' ')[1]
                elif 'Intended_Score:' in line:
                    self.intended_score = int(line.split(' ')[1])
                elif 'TEST_ID' in line:
                    test_id = line.split(' ')[1]
                    sections = {}
                    current_test = Scored_Test(test_id)
                elif 'TYPE' in line:
                    current_test.type = line.split(' ')[1]
                elif 'ESSAY' in line:
                    current_test.essay = int(line.split(' ')[1])
                elif 'DATE' in line:
                    current_test.date = line.split(' ')[1]
                elif 'WRITING:' in line:
                    current_TYPE = WRITING_TYPE
                elif 'MATH:' in line:
                    current_TYPE = MATH_TYPE
                elif 'READING:' in line:
                    current_TYPE = READING_TYPE
                elif 'Missed Questions:' in line:
                    data = string_to_array(line.split(":")[1])
                    sections[current_TYPE] = data
                elif SECTION_SEP in line:
                    current_test.recreate(sections)
                    sections = {}
                    self.tests_taken.append(current_test)
        for test in self.tests_taken:
            self.stats_from_test(test)
        for test in self.tests_taken:
            if test.type == str(QUICK_SECTIONS):
                self.tests_taken.remove(test)


    def stats_from_test(self, test):
        with open(test_directory(test.test_id) + DIR_SEP + KEYFILE, 'rU') as f:
            reader = csv.reader(f)
            key = {}
            for row in reader:
                if row != KEY_VECTOR or row[0] == '':
                    index = int(row[0])
                    section_type = int(row[1])
                    size = int(row[2])
                    key[index] = (section_type, size)
        for index in key.keys():
            self.stats_from_file(test, key, index)


    def stats_from_file(self, test, key, index):
        section_type = key[index][0]
        if not file_exists(test_directory(test.test_id) + DIR_SEP + "Section " + str(index) + CSV):
            return
        ifile  = open(test_directory(test.test_id) + DIR_SEP + "Section " + str(index) + CSV, "rU")
        reader = csv.reader(ifile)

        for row in reader:
            if row != LABEL_VECTOR or row[0] == '':
                number = int(row[NUMBER_INDEX])
                difficulty = int(row[DIFFICULTY_INDEX])
                type_list = row[TYPE_INDEX].split(',')
                main_type_list = return_main_types(type_list)
                type_list += main_type_list
                if index_exists(test.missed_questions_index, index) and index_exists(test.missed_questions_index[index], number):
                    attempt = test.missed_questions_index[index][number]
                    if attempt == BLANK_ENTRY:
                        for qtype in type_list:
                            self.data.data[section_type].stats["L" + str(difficulty)].add_blank()
                            self.data.data[section_type].stats[qtype].add_blank()
                            test.data.data[section_type].stats["L" + str(difficulty)].add_blank()
                            test.data.data[section_type].stats[qtype].add_blank()
                        #complete data processing
                        #add the level and type blanks
                    elif attempt == OMIT_ENTRY:
                        continue                
                    else:
                        for qtype in type_list:
                            self.data.data[section_type].stats["L" + str(difficulty)].add_miss()
                            self.data.data[section_type].stats[qtype].add_miss()
                            test.data.data[section_type].stats["L" + str(difficulty)].add_miss()
                            test.data.data[section_type].stats[qtype].add_miss()
                else:
                    for qtype in type_list:
                        self.data.data[section_type].stats["L" + str(difficulty)].add_correct()
                        self.data.data[section_type].stats[qtype].add_correct()
                        test.data.data[section_type].stats["L" + str(difficulty)].add_correct()
                        test.data.data[section_type].stats[qtype].add_correct()

        ifile.close()

    def average_scores(self):
        if len(self.tests_taken) == 0:
            return [0,0,0,0,0]
        writing_score = 0
        reading_score = 0
        math_score = 0
        essay_score = 0
        total_score = 0
        for test in self.tests_taken:
            writing_score += test.score_summary.section_scores[WRITING_TYPE]
            reading_score += test.score_summary.section_scores[READING_TYPE]
            math_score += test.score_summary.section_scores[MATH_TYPE]
            essay_score += test.essay
        total_score = (writing_score + reading_score + math_score) // len(self.tests_taken)
        reading_score = reading_score // len(self.tests_taken)
        writing_score = writing_score // len(self.tests_taken)
        math_score = math_score // len(self.tests_taken)
        essay_score = essay_score // len(self.tests_taken)
        return [total_score, reading_score, writing_score, math_score, essay_score]

    def reset_account(self):
        self.tests_taken = [] #array of scored tests for data analysis
        self.data = Data_Holder()
        self.save_user()

    def cram(self):
        #returns a dictionary indexed by type and key as tuple of lowest type and level
        #calculate lowest data has in terms of performance for each section
        array = TYPE_ARRAY
        output = {}
        for section_type in array:
            letter,size = get_section_type_size(section_type)
            current_min = 1
            current_code = None
            percent = None
            for i in range (1, MAIN_TYPE_SIZE_ARRAY[section_type] + 1):
                code = letter + str(i)
                qs = self.data.data[section_type].stats[code]
                if qs.t != 0:
                    new_point = div(qs.c, qs.t)
                    if new_point < current_min:
                        current_min = new_point
                        current_code = code
                        percent = percentage(new_point)
            output[section_type] = (current_code, percent)
        return output

    def positivecram(self):
        """
        Returns a dictionary indexed by type and key as tuple of high type and level
        """
        types = TYPE_ARRAY
        output = {}
        for section_type in types:
            letter,size = get_section_type_size(section_type)
            current_max = -1
            current_code = None
            percent = None
            for i in range (1, MAIN_TYPE_SIZE_ARRAY[section_type] + 1):
                code = letter + str(i)
                qs = self.data.data[section_type].stats[code]
                new_point = div(qs.c, qs.t)
                if new_point > current_max or current_max < 0:
                    current_max = new_point
                    current_code = code
                    percent = percentage(new_point)
            output[section_type] = (current_code, percent)
        return output


    def make_gauge(self,numgagues, gdata, labels):
        number_of_gauges = numgagues
        data = gdata
        title = labels
        index = 0
        lines = []
        lines.append('<script>' + endl)
        lines.append('var')
        for number in range(0,number_of_gauges):
            if(number < (number_of_gauges-1)):
                lines.append(' g' + str(number) + ',')
            else:
                lines.append(' g' + str(number))

        lines.append(';' + endl)
        lines.append('window.onload= function(){' + endl)
        for number in range(0,number_of_gauges):
            lines.append('var g' + str(index) + '= new JustGage({' + endl)
            lines.append('id: ' + '"' + 'g' + str(number) + '"' + ',' + endl)
            lines.append('value: ' + str(gdata[index]) + ',' + endl)
            lines.append('min: 0,' + endl)
            if index == 0:
                lines.append('max: 2400,' + endl)
            else:
                lines.append('max: 800,' + endl)
            lines.append('title: '+ '"' + str(labels[index]) + '"' + ',' + endl)
            lines.append('label: '+'"'+ 'Points' + '"' + ',' + endl)
            lines.append('levelColors: [' + endl)
            lines.append('"' + '#ff3b3b' + '"' + ',' + endl)
            lines.append('"' + '#ffff27' + '"' + ',' + endl)
            lines.append('"' + '#62d184' + '"' + ',' + endl)
            lines.append('],' + endl)
            lines.append('startAnimationTime: 2000,' + endl)
            lines.append('startAnimationType: ' + '"' + 'bounce' + '"' + endl) 
            lines.append('});' + endl)
            index = index + 1

        lines.append('};' + endl)
        lines.append('</script>' + endl)
        return lines    

    def simple_HTML(self):
        FILE = open(user_directory(self.name, self.c) + DIR_SEP + "simple_report" + ".html", "w")
        lines = []

        index = 1
        scores = self.average_scores()
        s1 = []
        pointlabels = []  #form codes for graph points 

        #calculate all scores
        for test in self.tests_taken:
            s1.append(test.score_summary.total_score())
            pointlabels.append(date_converter(test.date) +  SPACE + str(test.test_id))
            index += 1

        #graph js
        g = Graph("Overall Score Performance", 1, s1, None, None, pointlabels)

        #HTML opener
        lines.append('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">' + endl)
        lines.append('<html xmlns="http://www.w3.org/1999/xhtml">' + endl)
        lines.append('<head>')
        lines.append('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' + endl)
        lines.append('{% load staticfiles %}' + endl)
        lines.append('<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">' + endl)
        lines.append('<link rel="stylesheet" type="text/css" href=' + '"' + "{% static 'style.css' %}" + '"' + '/>' + endl)
        lines.append('<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>' + endl)
        lines.append('<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>' + endl)
        lines.append('<script src=' + '"' + "{% static 'raphael.2.1.0.min.js' %}" + '"></script>' + endl)
        lines.append('<script src=' + '"' + "{% static 'justgage.1.0.1.js' %}" + '"></script>' + endl)
        lines.append('<link rel="stylesheet" type="text/css" href=' + '"' + "{% static 'style.css' %}" + '"' + '/>' + endl)
        
        lines.append('<style>' + endl)
        lines.append("#g0 {" + endl)
        lines.append("width:470px; height:200px;" + endl)
        lines.append("display: inline-block; " + endl)
        lines.append("margin: 1em;")
        lines.append("}" + endl)

        lines.append("#g1, #g2, #g3 {" + endl)
        lines.append("width:137px; height:100px;" + endl)
        lines.append("display: inline-block; " + endl)
        lines.append("margin: 1em;")
        lines.append("}" + endl)

        lines.append('</style>' + endl)
        lines.append('<title>Simple Score Report</title>' + endl)
        lines += g.head()
        lines.append('</head>' + endl)
        lines.append('<body>' + endl)
        #lines.append('<style>' + endl)
        #lines.append('html,' + endl)
        #lines.append('body {' + endl)
        #lines.append('background-image: url("{% static' +  " 'mysite/css/images/overlay.svg' " +  '%}");')
        #lines.append('background-size: 100%;' + endl)
        #lines.append('}' + endl)
        #lines.append('</style>' + endl)        
        lines.append('<div id="page">' + endl)
        lines.append('<div id="header">' + endl)
        lines.append('<img src=' + '"' + "{% static 'ml.png' %}" + '" alt="Excelerate" style="float: right; width: 35%; margin-right: 35%;"/>' +
            '<a href="javascript:history.go(-1)">  <img src=' + '"' + "{% static 'back_rev.png' %}" + '" alt="Home" style="float: left; width: 15%; margin-left: 5%;"> </a>' + endl)
        lines.append('<p style="clear: both;">' + endl)
        #lines.append('<a href="javascript:history.go(-1)"> <img src=' + '"' + "{% static 'back_rev.png' %}" + '" width="20%" alt="Home"> </a>' + endl)
        #lines.append('</div>' + endl)
        #lines.append('</div>' + endl)
        lines.append('<div id="content">' + endl)
        lines.append('<div id="container">' + endl)
        lines.append('<div id="main">' + endl)
        lines.append('<div id="menu">' + endl)
        lines.append('<h2 style="text-align:center;">Simple Report: ' + self.name + '</h2>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div id="text">' + endl)
        

        #Qualitative Performance
        lines.append('<h1>Qualitative Performance</h1>' + endl)
        lines.append('<p><b>Overall Aptitude:</b><font color = "' + overall_qualitative_color(scores[0]) + '"> ' + overall_qualitative(scores[0]) + '</font></p>' + endl)
        lines.append('<p><b>Writing Performance:</b><font color = "' + qualitative_color(scores[2]) + '"> ' + qualitative(scores[2]) + '</font></p>' + endl)
        lines.append('<p><b>Reading Performance:</b><font color = "' + qualitative_color(scores[1]) + '"> ' + qualitative(scores[1]) + '</font></p>' + endl)
        lines.append('<p><b>Math Performance:</b><font color = "' + qualitative_color(scores[3]) + '"> ' + qualitative(scores[3]) + '</font></p>' + endl)
        #lines.append('<p><b>Essay Performance:</b> Unknown</p>' + endl)
        lines.append('<p><i>Here are the details regarding the scores. Average scores are between 550 to 670. All scores above average are recorded as proficient and all scores below are noted as poor. Unknown scores have no records.</i></p>' + endl)
        lines.append('<hr color="#BBBBBB" size="2" width="100%">' + endl)

        #Average Results
        lines.append('<h1>Average Results</h1>' + endl)
        #lines.append('<p><b>Total Score:</b><font color = "' + overall_qualitative_color(scores[0]) + '">  ' + str(scores[0]) + '/2400</font></p>' + endl)
        #lines.append('<p><b>Average Writing Score:</b><font color = "' + qualitative_color(scores[2]) + '">  ' + str(scores[2]) + '/800</font></p>' + endl)
        #lines.append('<p><b>Average Reading Score:</b><font color = "' + qualitative_color(scores[1]) + '">  ' + str(scores[1]) + '/800</font></p>' + endl)
        #lines.append('<p><b>Average Math Score:</b><font color = "' + qualitative_color(scores[3]) + '">  ' + str(scores[3]) + '/800</font></p>' + endl)
    
        lines.append('<br>' + endl)
        lines.append('<div  id = "g0"></div>' + endl)
        lines.append('<div  id = "g1"></div>' + endl)
        lines.append('<div  id = "g2"></div>' + endl)
        lines.append('<div  id = "g3"></div>' + endl)


        lines += self.make_gauge(4, scores,['Average Overall Score', 'Average Writing Score', 'Average Reading Score', 'Average Math Score'])

        #lines.append('<p><b>Average Essay Score: </b>' + str(scores[4]) + '/12</p>' + endl)
        lines.append('<p><b>Tests Taken:</b> ' + str(len(self.tests_taken)) + '</p>' + endl)
        lines.append('<hr color="#BBBBBB" size="2" width="100%">' + endl)

        #Previous Test History
        """lines.append('<h1>Previous Test History</h1>' + endl)
        for test in self.tests_taken:
            lines.append('<p><b>Test ID:</b> ' + test.test_id + '</p>' + endl)
            lines.append('<p><b>Total:</b> ' + str(test.score_summary.total_score())+'</p>' + endl)
            lines.append('<p><b>Writing:</b> ' + str(test.score_summary.section_scores[WRITING_TYPE]) +'</p>' + endl)
            lines.append('<p><b>Reading:</b> ' + str(test.score_summary.section_scores[READING_TYPE]) +'</p>' + endl)
            lines.append('<p><b>Math:</b> ' + str(test.score_summary.section_scores[MATH_TYPE]) +'</p>' + endl)
            lines.append('<br>' + endl)
        lines.append('<hr color="#BBBBBB" size="2" width="100%">' + endl)"""

        #Overall Graph
        lines.append('<h1>Previous Test History</h1>' + endl)
        lines += g.html(False, False, True)
        lines.append('<br><hr color="#BBBBBB" size="2" width="100%">' + endl)


        #Cram
        cram = self.cram()
        positivecram = self.positivecram()
        lines.append('<h1>Quick Advice</h1>' + endl)
        #if a 2400 is scored
        if(len(self.tests_taken) != 0 and cram[READING_TYPE][0] == None and cram[WRITING_TYPE][0] == None and cram[MATH_TYPE][0] == None):
            lines.append("<h2 style = " + ' " ' + "color: #348cb2" + ' " ' + "><i>Congratulations for getting a perfect multiple choice score on your most recent test!</i></h2>" + endl)
            lines.append("<h3><i>Check in every once in a while and practice to maintain your amazing: " + str(self.tests_taken[-1].score_summary.total_score()) + "!</i></h3>" + endl)

        if (cram[READING_TYPE][0] != None):
            type_name = READING_TYPE_DICT[cram[READING_TYPE][0]]
            best_type = READING_TYPE_DICT[positivecram[READING_TYPE][0]]
            lines.append("<h3><i>Reading Highlights:</i></h3>" + endl)
            lines.append(paropen + "Your strongest reading section is " + best_type + " as you are scoring " + positivecram[READING_TYPE][1] + " in these questions.")
            if best_type != type_name:
                lines.append(paropen + "Your weakest reading section is " + type_name + " as you are scoring " + cram[READING_TYPE][1] + " in these questions.")
             
        if (cram[WRITING_TYPE][0] != None):
            lines.append("<h3><i>Writing Highlights:</i></h3>" + endl)
            type_name = WRITING_TYPE_DICT[cram[WRITING_TYPE][0]]
            best_type = WRITING_TYPE_DICT[positivecram[WRITING_TYPE][0]]
            lines.append(paropen + "Your strongest writing section is " + best_type + " as you are scoring " + positivecram[WRITING_TYPE][1] + " in these questions.")
            if best_type != type_name:
                lines.append(paropen + "Your weakest writing section is " + type_name + " as you are scoring " + cram[WRITING_TYPE][1] + " in these questions.")
        
        if (cram[MATH_TYPE][0] != None):
            lines.append("<h3><i>Math Highlights:</i></h3>" + endl)
            type_name = MATH_TYPE_DICT[cram[MATH_TYPE][0]]
            best_type = MATH_TYPE_DICT[positivecram[MATH_TYPE][0]]
            lines.append(paropen + "Your strongest math section is " + best_type + " as you are scoring " + positivecram[MATH_TYPE][1] + " in these questions.")
            if best_type != type_name:
                lines.append(paropen + "Your weakest math section is " + type_name + " as you are scoring " + cram[MATH_TYPE][1] + " in these questions.")

      
        #Footer
        lines.append('<br>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div class="clear"></div>' + endl)
        lines.append('<div id="footer">' + endl)
        lines.append('<p><a>' + self.name + ' Simple Report</a></p>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines += g.body()
        lines.append('</body>' + endl)
        lines.append('</html>' + endl)
        lines.append(endl)

        FILE.writelines(lines)
        FILE.close()

    def section_HTML(self, section_type):
        section_type_name = section_name(section_type)
        FILE = open(user_directory(self.name, self.c) + DIR_SEP + section_type_name.lower() + "_report" + ".html", "w")
        lines = []

        index = 1
        graph_index = 1
        scores = self.average_scores()
        s1 = []
        graphs = []
        pointlabels = []
        #calculate all scores
        for test in self.tests_taken:
            s1.append(test.score_summary.section_scores[section_type])
            pointlabels.append(date_converter(test.date) + SPACE + str(test.test_id))
            index += 1

        g = Graph(section_type_name + " Score Performance", graph_index, s1, None, None, str(pointlabels))
        type_dict = section_type_dict(section_type)
        del pointlabels[:]
        for i in range (1, MAIN_TYPE_SIZE_ARRAY[section_type] + 1):
            key = section_type_name[0] + str(i)
            data = []
            graph_index += 1
            for test in self.tests_taken:
                if test.data.data[section_type].stats[key].t != 0:
                    data.append(div(test.data.data[section_type].stats[key].c, test.data.data[section_type].stats[key].t) * 100)
                    pointlabels.append(date_converter(test.date) + SPACE + str(test.test_id))
            if len(data) > 0:
                graphs.append(Graph(type_dict[key], graph_index, data,None, None, str(pointlabels)))
                del pointlabels[:]




        #HTML opener
        lines.append('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">' + endl)
        lines.append('<html xmlns="http://www.w3.org/1999/xhtml">' + endl)
        lines.append('<head>')
        lines.append('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' + endl)
        lines.append('{% load staticfiles %}' + endl)
        lines.append('<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">' + endl)
        lines.append('<link rel="stylesheet" type="text/css" href=' + '"' + "{% static 'style.css' %}" + '"' + '/>' + endl)
        lines.append('<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>' + endl)
        lines.append('<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>' + endl)
        lines.append('<title>' + section_type_name + ' Score Report</title>' + endl)
        lines += g.head()
        lines.append('</head>' + endl)
        lines.append('<body>' + endl)
        lines.append('<style>' + endl)
        lines.append('html,' + endl)
        lines.append('body {' + endl)
        lines.append('background-image: url("{% static' +  " 'mysite/css/images/overlay.svg' " +  '%}");')
        lines.append('background-size: 100%;' + endl)
        lines.append('}' + endl)
        lines.append('</style>' + endl)   
        lines.append('<div id="page">' + endl)
        lines.append('<div id="header">' + endl)
        lines.append('<img src=' + '"' + "{% static 'ml.png' %}" + '" alt="Excelerate" style="float: right; width: 35%; margin-right: 35%;"/>' +
            '<a href="javascript:history.go(-1)">   <img src=' + '"' + "{% static 'back_rev.png' %}" + '" alt="Home" style="float: left; width: 15%; margin-left: 5%;"> </a>' + endl)
        lines.append('<p style="clear: both;">' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div id="content">' + endl)
        lines.append('<div id="container">' + endl)
        lines.append('<div id="main">' + endl)
        lines.append('<div id="menu">' + endl)
        lines.append('<h2 style="text-align:center;">' + section_type_name + SPACE + 'Analytics Report: ' + self.name + '</h2>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div id="text">' + endl)
        


        #Average Results
        lines.append('<h1>' + section_type_name + ' Analytics</h1>' + endl)
        lines.append('<hr color="#BBBBBB" size="2" width="100%">' + endl)


        #Overall Graph
        lines += g.html(False, True, False)
        lines.append("<p>This is your performance in the " + section_type_name + " section of the last " + str(len(self.tests_taken)) + " tests you have taken. The more comprehensive analysis of questions correct in each type is found below.</p>")


        #Average Results

        #Print the type analysis as well
        i = 1
        for graph in graphs:
            graph_index += 1
            main_type = section_type_name[0] + str(i)
            TYPE_DICT = section_type_dict(section_type)
            lines.append('<h1>'+ TYPE_DICT[main_type] + ' Analytics</h1>' + endl)
            lines.append('<hr color="#BBBBBB" size="2" width="100%">' + endl)
            lines += graph.html(True, True, False)
            lines.append('<div id="chart' + str(graph_index) + '" style="height:245px; width:245px;"></div>' + endl)
            lines.append('<script>' + endl)
            lines.append('var chart' + str(graph_index) + ' = c3.generate({' + endl)
            lines.append('size:{ height:245, width:245},' + endl)
            lines.append('bindto: ' + "'" + '#chart' + str(graph_index) + "'" + ',' + endl)
            lines.append('data: {' + endl)
            lines.append('labels: true,' + endl)
            lines.append('columns: [' + endl)
            lines.append('[' + "'" + 'Correct' +"'," + str(self.data.data[section_type].stats[section_type_name[0]+ str(i)].c) + "]," +  "['" + 'Missed' +"'," + str(self.data.data[section_type].stats[section_type_name[0]+ str(i)].m) + "]," + "['" + 'Blank' +"'," + str(self.data.data[section_type].stats[section_type_name[0]+ str(i)].b) + "]"+ endl)
            lines.append('],' + endl)
            lines.append('type:' + "'" + 'donut' + "'" + endl)
            lines.append('},' + endl)
            lines.append('color:{ pattern: [' + "'" + '#66FF66' + "'," + "'" + '#FF3300' + "'," + "'" + '#e5e544' + "']" + endl)
            lines.append('},')
            lines.append('donut: {' + endl)
            #lines.append('title:' + "'" + str(type_dict[section_type_name[0]+ str(i)]) + "'" + endl)
            lines.append('} });' + endl)
            lines.append('</script>' + endl)
            lines.append(endl)
            lines.append('<p><b><font color = "' + self.data.data[section_type].stats[section_type_name[0] + str(i)].color() +'">' + type_dict[section_type_name[0] + str(i)] + "</b> " + str(self.data.data[section_type].stats[section_type_name[0]+str(i)]) + '</p>')
            #lines.append("<h3>" + '<font color = "#000000">' + "<i>" + 'Category Subtype' + " Analytics:</i></h3>" + endl + endl)
            lines.append('<div data-role="collapsible"> <h3><font color = "#093175">' + TYPE_DICT[main_type] + ' In-Depth Analytics</font></h3>')
            lines.append(endl)
            for subtype in SUB_TYPE_LIST[main_type]:
                if self.data.data[section_type].stats[subtype].t != 0:
                    lines.append('<p><b><font color = "' + self.data.data[section_type].stats[subtype].color() +'">' + TYPE_DICT[subtype] + '</b> ' + str(self.data.data[section_type].stats[subtype]) + '</p>')
                    lines.append('<hr color="#4169EF" size="1" width="100%">' + endl)
                    lines.append('<br>')
            lines.append('</div>' + endl)
            lines.append('<hr color="#4169EF" size="1" width="90%">' + endl)
            lines.append("<br>" + endl)     
            i+=1 
            #lines.append("<br>" + endl)

        #Footer
        lines.append('<br>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div class="clear"></div>' + endl)
        lines.append('<div id="footer">' + endl)
        lines.append('<p><a>' + self.name + SPACE + section_type_name + SPACE +' Report</a></p>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines += g.body()
        lines.append('</body>' + endl)
        lines.append('</html>' + endl)
        lines.append(endl)

        FILE.writelines(lines)
        FILE.close()



    def advanced_HTML(self):

        FILE = open(user_directory(self.name, self.c) + DIR_SEP + "advanced_report" + ".html", "w")
        scores = self.average_scores()
        s1 = []
        writing_scores = []
        reading_scores = []
        math_scores = []
        overall_score_difference = 0
        writing_score_difference = 0
        reading_score_difference = 0
        math_score_difference = 0
        graph_index = 0

        if len(self.tests_taken) > 1:

            for test in self.tests_taken:
                s1.append([date_converter(test.date),test.score_summary.total_score()])
                writing_scores.append([date_converter(test.date), test.score_summary.section_scores[WRITING_TYPE]])
                reading_scores.append([date_converter(test.date), test.score_summary.section_scores[READING_TYPE]])
                math_scores.append([date_converter(test.date), test.score_summary.section_scores[MATH_TYPE]])
        if len(self.tests_taken) > 1:        
            overall_score_difference = (s1[-1][1]) - (s1[0][1])
            writing_score_difference = (writing_scores[-1][1]) - (writing_scores[0][1])
            reading_score_difference = (reading_scores[-1][1]) - (reading_scores[0][1])
            math_score_difference =  (math_scores[-1][1]) - (math_scores[0][1])



        lines = []

        scores = self.average_scores()

        #HTML opener
        lines.append('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">' + endl)
        lines.append('<html xmlns="http://www.w3.org/1999/xhtml">' + endl)
        lines.append('<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>' + endl)
        lines.append('<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>' + endl)
        lines.append('<head>')
        lines.append('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' + endl)
        lines.append('{% load staticfiles %}' + endl)
        lines.append('<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">' + endl)
        lines.append('<link rel="stylesheet" type="text/css" href=' + '"' + "{% static 'style.css' %}" + '"' + '/>' + endl)
        lines.append('<script src=' + '"' + "{% static 'raphael.2.1.0.min.js' %}" + '"></script>' + endl)
        lines.append('<script src=' + '"' + "{% static 'justgage.1.0.1.js' %}" + '"></script>' + endl)
          #load c3.css
        lines.append('<link rel="stylesheet" type= "text/css"  href=' + '"' + "{% static 'c3.css' %}" +  ' "/> ' + endl)
        #load d3.js and c3.js 
        lines.append('<script charset="utf-8"  src=' + '"' + "{% static 'd3.min.js' %}" + '"></script>  ' + endl)
        lines.append('<script src=' + '"' + "{% static 'c3.min.js' %}" + '"></script>  ' + endl)
        lines.append('<title>Advanced Score Report</title>' + endl)
        lines.append('</head>' + endl)
        lines.append('<body>' + endl)
        lines.append('<style>' + endl)

        lines.append("#g0 {" + endl)
        lines.append("width:470px; height:200px;" + endl)
        lines.append("display: inline-block; " + endl)
        lines.append("margin: 1em;")
        lines.append("}" + endl)

        lines.append("#g1, #g2, #g3 {" + endl)
        lines.append("width:137px; height:100px;" + endl)
        lines.append("display: inline-block; " + endl)
        lines.append("margin: 1em;")
        lines.append("}" + endl)

    
        lines.append('html,' + endl)
        lines.append('body {' + endl)
        lines.append('background-image: url("{% static' +  " 'mysite/css/images/overlay.svg' " +  '%}");')
        lines.append('background-size: 100%;' + endl)
        lines.append('}' + endl)
        lines.append('</style>' + endl)   
        lines.append('<div id="page">' + endl)
        lines.append('<div id="header">' + endl)
        lines.append('<img src=' + '"' + "{% static 'ml.png' %}" + '" alt="Excelerate" style="float: right; width: 35%; margin-right: 35%;"/>' +
            '<a href="javascript:history.go(-1)">   <img src=' + '"' + "{% static 'back_rev.png' %}" + '" alt="Home" style="float: left; width: 15%; margin-left: 5%;"> </a>' + endl)
        lines.append('<p style="clear: both;">' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div id="content">' + endl)
        lines.append('<div id="container">' + endl)
        lines.append('<div id="main">' + endl)
        lines.append('<div id="menu">' + endl)
        lines.append('<h2 style="text-align:center;">Advanced Report: ' + self.name + '</h2>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div id="text">' + endl)
            

        #Average Results
        #lines.append('<h1>Average Results</h1>' + endl)
        #lines.append('<p><b>Total Score:</b><font color = "' + overall_qualitative_color(scores[0]) + '">  ' + str(scores[0]) + '/2400</font></p>' + endl)
        #lines.append('<p><b>Average Writing Score:</b><font color = "' + qualitative_color(scores[2]) + '">  ' + str(scores[2]) + '/800</font></p>' + endl)
        #lines.append('<p><b>Average Reading Score:</b><font color = "' + qualitative_color(scores[1]) + '">  ' + str(scores[1]) + '/800</font></p>' + endl)
        #lines.append('<p><b>Average Math Score:</b><font color = "' + qualitative_color(scores[3]) + '">  ' + str(scores[3]) + '/800</font></p>' + endl)
        lines.append('<div  id = "g0"></div>' + endl)
        lines.append('<div  id = "g1"></div>' + endl)
        lines.append('<div  id = "g2"></div>' + endl)
        lines.append('<div  id = "g3"></div>' + endl)


        lines += self.make_gauge(4, scores,['Average Overall Score', 'Average Writing Score', 'Average Reading Score', 'Average Math Score'])

        #lines.append('<p><b>Average Essay Score: </b>' + str(scores[4]) + '/12</p>' + endl)
        lines.append('<p><b>Tests Taken:</b> ' + str(len(self.tests_taken)) + '</p>' + endl)
        if overall_score_difference > 0:
            lines.append('You have gone from '+ str(s1[0][1]) + ' points on your first practice test to ' + str(s1[-1][1]) + ' points on your most recent test and improved your overall score by ' + str(overall_score_difference) + ' points' + endl)


        lines.append('<hr color="#BBBBBB" size="2" width="100%">' + endl)
       

        #Section Analysis
        lines.append(endl)
        lines.append("<h1>Writing Performance Analysis</h1>" + endl)
        lines.append('<p><font color = "#093175">Type: Total Questions | Questions Correct | Questions Missed | Questions Blank</font></p><br>' + endl)

        lines.append(endl)

        #3 weakest tier two subjects per section
        missed_qs_writing = []
        missed_qs_reading = []
        missed_qs_math = []

        #Writing Analytics
        if writing_score_difference > 0:
            lines.append('You have gone from '+ str(writing_scores[0][1]) + ' points since your first practice test to ' + str(writing_scores[-1][1]) + ' points on your most recent test and improved your writing score by ' + str(writing_score_difference) + ' points' + endl)
        for i in range(1, WRITING_TYPES + 1):
            main_type = 'W' + str(i)
            for subtype in SUB_TYPE_LIST[main_type]:
                missed_qs_writing.append([self.data.data[WRITING_TYPE].stats[subtype], subtype])
            missed_qs_writing = sorted(missed_qs_writing, key = lambda missed: missed[:][0].m, reverse = True)
            lines.append('<div data-role="collapsible">')
            lines.append("<h2>" + '<font color = "#093175">' + "<i>" + WRITING_TYPE_DICT[main_type] + " Analytics</i></font></h2>" + endl + endl)
            lines.append(endl)
            index = 0 #index for top 3 worst categories 
            for question in missed_qs_writing:
                if self.data.data[WRITING_TYPE].stats[str(question[1])].t != 0 and index <=2:

                    lines.append('<div id="chart' + str(graph_index) + '" style="height:245px; width:245px;"></div>' + endl)
                    lines.append('<script>' + endl)
                    lines.append('var chart' + str(graph_index) + ' = c3.generate({' + endl)
                    lines.append('size:{ height:245, width:245},' + endl)
                    lines.append('bindto: ' + "'" + '#chart' + str(graph_index) + "'" + ',' + endl)
                    lines.append('data: {' + endl)
                    lines.append('labels: true,' + endl)
                    lines.append('columns: [' + endl)
                    lines.append('[' + "'" + 'Correct' +"'," + str(self.data.data[WRITING_TYPE].stats[question[1]].c) + "]," +  "['" + 'Missed' +"'," + str(self.data.data[WRITING_TYPE].stats[question[1]].m) + "]," + "['" + 'Blank' +"'," + str(self.data.data[WRITING_TYPE].stats[question[1]].b) + "]"+ endl)
                    lines.append('],' + endl)
                    lines.append('type:' + "'" + 'donut' + "'" + endl)
                    lines.append('},' + endl)
                    lines.append('color:{ pattern: [' + "'" + '#66FF66' + "'," + "'" + '#FF3300' + "'," + "'" + '#e5e544' + "']" + endl)
                    lines.append('},')
                    lines.append('donut: {' + endl)
                    #lines.append('title:' + "'" + 'Question Stats' + "'" + endl)
                    lines.append('} });' + endl)
                    lines.append('</script>' + endl)
                    lines.append(endl)
                    graph_index += 1
                    index += 1
                    lines.append('<p><b><font color = "' + self.data.data[WRITING_TYPE].stats[str(question[1])].color() +'">' + WRITING_TYPE_DICT[str(question[1])] + '</b> ' + str(self.data.data[WRITING_TYPE].stats[str(question[1])]) + '</p>')
                    lines.append('<p><a ' + "href=" + '"{% static ' + "'" + lesson_filename(str(question[1])) + "'"  + ' %}"' + ' target="_blank"><img src="' + "{% static 'book.png' %}" +'" width="8%" alt="Excelerate" ' +  '/></a></p>' + endl)                    
                    if str(question[1]) in WRITING_TIP_DICT.keys():
                        lines.append('<div data-role="collapsible">')
                        lines.append("<h2>" + '<font color = "#093175">' + "<i>Quick Tips</i></font></h2>" + endl)
                        lines.append('<p><b>' + WRITING_TYPE_DICT[str(question[1])] + ":</b> " + WRITING_TIP_DICT[str(question[1])] + '</p>')
                        lines.append('</div>')
                        lines.append(endl)
                    lines.append('<hr color="#4169EF" size="1" width="100%">' + endl)
            del missed_qs_writing[:]
            #lines.append('<hr color="#4169EF" size="1" width="90%">' + endl)
            lines.append('</div>' + endl)
        lines.append("<br>" + endl)     

        lines.append('<hr color="#BBBBBB" size="2" width="100%">' + endl)
        lines.append("<h1>Reading Performance Analysis</h1>" + endl)
        lines.append('<p><font color = "#093175">Type: Total Questions | Questions Correct | Questions Missed | Questions Blank</font></p><br>' + endl)
        lines.append(endl)

        #Reading Analytics
        if reading_score_difference > 0:
            lines.append('You have gone from '+ str(reading_scores[0][1]) + ' points on your first practice test to ' + str(reading_scores[-1][1]) + ' points on your most recent test and improved your reading score by ' + str(reading_score_difference) + ' points' + endl)
        for i in range(1, READING_TYPES + 1):
            main_type = 'R' + str(i)
            for subtype in SUB_TYPE_LIST[main_type]:
                missed_qs_reading.append([self.data.data[READING_TYPE].stats[subtype], subtype])
            missed_qs_reading = sorted(missed_qs_reading, key = lambda missed: missed[:][0].m, reverse = True)
            lines.append('<div data-role="collapsible">')
            lines.append("<h2>" + '<font color = "#093175">' + "<i>" + READING_TYPE_DICT[main_type] + " Analytics</i></h2>" + endl + endl)
            lines.append(endl)
            index = 0 #index for top 3 worst categories
            for question in missed_qs_reading:
                if self.data.data[READING_TYPE].stats[str(question[1])].t != 0 and index <=2:
                    lines.append('<div id="chart' + str(graph_index) + '" style="height:245px; width:245px;"></div>' + endl)
                    lines.append('<script>' + endl)
                    lines.append('var chart' + str(graph_index) + ' = c3.generate({' + endl)
                    lines.append('size:{ height:245, width:245},' + endl)
                    lines.append('bindto: ' + "'" + '#chart' + str(graph_index) + "'" + ',' + endl)
                    lines.append('data: {' + endl)
                    lines.append('labels: true,' + endl)
                    lines.append('columns: [' + endl)
                    lines.append('[' + "'" + 'Correct' +"'," + str(self.data.data[READING_TYPE].stats[question[1]].c) + "]," +  "['" + 'Missed' +"'," + str(self.data.data[READING_TYPE].stats[question[1]].m) + "]," + "['" + 'Blank' +"'," + str(self.data.data[READING_TYPE].stats[question[1]].b) + "]"+ endl)
                    lines.append('],' + endl)
                    lines.append('type:' + "'" + 'donut' + "'" + endl)
                    lines.append('},' + endl)
                    lines.append('color:{ pattern: [' + "'" + '#66FF66' + "'," + "'" + '#FF3300' + "'," + "'" + '#e5e544' + "']" + endl)
                    lines.append('},')
                    lines.append('donut: {' + endl)
                    #lines.append('title:' + "'" + 'Question Stats' + "'" + endl)
                    lines.append('} });' + endl)
                    lines.append('</script>' + endl)
                    lines.append(endl)
                    graph_index += 1
                    index += 1
                    lines.append('<p><b><font color = "' + self.data.data[READING_TYPE].stats[str(question[1])].color() +'">' + READING_TYPE_DICT[str(question[1])] + '</b> ' + str(self.data.data[READING_TYPE].stats[subtype]) + '</p>')
                    lines.append('<p><a ' + "href=" + '"{% static ' + "'" + lesson_filename(str(question[1])) + "'"  + ' %}"' + ' target="_blank"><img src="' + "{% static 'book.png' %}" +'" width="8%" alt="Excelerate" ' +  '/></a></p>' + endl)
                    if str(question[1]) in READING_TIP_DICT.keys():
                        lines.append('<div data-role="collapsible">')
                        lines.append("<h2>" + '<font color = "#093175">' + "<i>Quick Tips</i></font></h2>" + endl)
                        lines.append('<p><b>' + READING_TYPE_DICT[str(question[1])] + ":</b> " + READING_TIP_DICT[str(question[1])] + '</p>')
                        lines.append('</div>')
                        lines.append(endl)
                    lines.append('<hr color="#4169EF" size="1" width="100%">' + endl)
            del missed_qs_reading[:]
            #lines.append('<hr color="#4169EF" size="1" width="90%">' + endl)
            lines.append('</div>' + endl)
        lines.append("<br>" + endl)


        lines.append('<hr color="#BBBBBB" size="2" width="100%">' + endl)
        lines.append("<h1>Math Performance Analysis</h1>" + endl)
        lines.append('<p><font color = "#093175">Type: Total Questions | Questions Correct | Questions Missed | Questions Blank</font></p><br>' + endl)
        lines.append(endl)
       

        #Math Analytics
        if math_score_difference > 0:
            lines.append('You have gone from '+ str(math_scores[0][1]) + ' points on your first practice test to ' + str(math_scores[-1][1]) + ' points on your most recent test and improved your math score by ' + str(math_score_difference) + ' points' + endl)
        for i in range(1, MATH_TYPES + 1):
            main_type = 'M' + str(i)
            for subtype in SUB_TYPE_LIST[main_type]:
                missed_qs_math.append([self.data.data[MATH_TYPE].stats[subtype], subtype])
            missed_qs_math = sorted(missed_qs_math, key = lambda missed: missed[:][0].m, reverse = True)
            lines.append('<div data-role="collapsible">')
            lines.append("<h2>" + '<font color = "#093175">' + "<i>" + MATH_TYPE_DICT[main_type] + " Analytics</i></h2>" + endl + endl)
            lines.append(endl)
            index = 0 #index for top 3 worst categories
            for question in missed_qs_math:
                if self.data.data[MATH_TYPE].stats[str(question[1])].t != 0 and index <=2:
                    lines.append('<div id="chart' + str(graph_index) + '" style="height:245px; width:245px;"></div>' + endl)
                    lines.append('<script>' + endl)
                    lines.append('var chart' + str(graph_index) + ' = c3.generate({' + endl)
                    lines.append('size:{ height:245, width:245},' + endl)
                    lines.append('bindto: ' + "'" + '#chart' + str(graph_index) + "'" + ',' + endl)
                    lines.append('data: {' + endl)
                    lines.append('labels: true,' + endl)
                    lines.append('columns: [' + endl)
                    lines.append('[' + "'" + 'Correct' +"'," + str(self.data.data[MATH_TYPE].stats[question[1]].c) + "]," +  "['" + 'Missed' +"'," + str(self.data.data[MATH_TYPE].stats[question[1]].m) + "]," + "['" + 'Blank' +"'," + str(self.data.data[MATH_TYPE].stats[question[1]].b) + "]"+ endl)
                    lines.append('],' + endl)
                    lines.append('type:' + "'" + 'donut' + "'" + endl)
                    lines.append('},' + endl)
                    lines.append('color:{ pattern: [' + "'" + '#66FF66' + "'," + "'" + '#FF3300' + "'," + "'" + '#e5e544' + "']" + endl)
                    lines.append('},')
                    lines.append('donut: {' + endl)
                    #lines.append('title:' + "'" + 'Question Stats' + "'" + endl)
                    lines.append('} });' + endl)
                    lines.append('</script>' + endl)
                    lines.append(endl)
                    graph_index += 1
                    index += 1
                    lines.append('<p><b><font color = "' + self.data.data[MATH_TYPE].stats[str(question[1])].color() +'">' + MATH_TYPE_DICT[str(question[1])] + '</b> ' + str(self.data.data[MATH_TYPE].stats[str(question[1])]) + '</p>')
                    lines.append('<p><a ' + "href=" + '"{% static ' + "'" + lesson_filename(str(question[1])) + "'"  + ' %}"' + ' target="_blank"><img src="' + "{% static 'book.png' %}" +'" width="8%" alt="Excelerate" ' +  '/></a></p>' + endl)                    
                    if str(question[1]) in MATH_TIP_DICT.keys():
                        lines.append('<div data-role="collapsible">')
                        lines.append("<h2>" + '<font color = "#093175">' + "<i>Quick Tips</i></font></h2>" + endl)
                        lines.append('<p><b>' + MATH_TYPE_DICT[str(question[1])] + ":</b> " + MATH_TIP_DICT[str(question[1])] + '</p>')
                        lines.append('</div>')
                        lines.append(endl)
                    lines.append('<hr color="#4169EF" size="1" width="100%">' + endl)
            del missed_qs_math[:]
            #lines.append('<hr color="#4169EF" size="1" width="90%">' + endl)
            lines.append('</div>' + endl)
        lines.append("<br>" + endl) 
        lines.append('<hr color="#BBBBBB" size="2" width="100%">' + endl)

        #Footer
        lines.append('<br>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div class="clear"></div>' + endl)
        lines.append('<div id="footer">' + endl)
        lines.append('<p><a>' + self.name + ' Advanced Report</a></p>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)


        lines.append('</body>' + endl)
        lines.append('</html>' + endl)
        lines.append(endl)

        FILE.writelines(lines)
        FILE.close()


    def graph_HTML(self):
        graphs = []
        s1 = []
        writing_scores = []
        reading_scores = []
        math_scores = []
        graph_index = 1
        index = 1
        l = {} #overall class average dictionary
        m = {} #math class average dictionary
        r = {} #reading " " "
        w = {} #writing " " "
        overall_rep_flag = True
        pointlabels = []
        #calculate all scores
        for test in self.tests_taken:
            s1.append(test.score_summary.total_score())
            writing_scores.append(test.score_summary.section_scores[WRITING_TYPE])
            reading_scores.append(test.score_summary.section_scores[READING_TYPE])
            math_scores.append(test.score_summary.section_scores[MATH_TYPE])
            pointlabels.append([date_converter(test.date),str(test.test_id)])
            index += 1

        # calculate class averages for each of the 4 graphs Overall, M W R Sections
        filename = class_directory('Elite') + DIR_SEP + "average.txt"
        with open(filename) as f:
            array = f.readlines()
            total = 0
            current_date = None
            for line in array:
                line = line.strip()
                if line == '':
                    continue
                if 'TEST_DATE' in line:
                    current_date = line.split(' ')[1]

                if 'WRITING:' in line:
                    total += int(line.split(' ')[1])
                    w[current_date] = total # get writing class average for 'current_date'

                if 'MATH:' in line:
                    total += int(line.split(' ')[1])
                    m[current_date] = int(line.split(' ')[1])
                        
                if 'READING:' in line:
                    total += int(line.split(' ')[1])
                    r[current_date] = int(line.split(' ')[1])
                        
                if SECTION_SEP in line:
                    l[current_date] = total
                    total = 0
        op= []
        math_class_average = []
        reading_class_average = []
        writing_class_average = []

        #CLASS AVERAGE GRAPH
        """for entry in s1:
            new_entry = [entry[0], l[entry[0]]]
            math_new_entry = [entry[0], m[entry[0]]] # get math class average for test on that date
            reading_new_entry = [entry[0], r[entry[0]]]
            writing_new_entry = [entry[0], w[entry[0]]]
            op.append(new_entry)
            math_class_average.append(math_new_entry)
            reading_class_average.append(reading_new_entry)
            writing_class_average.append(writing_new_entry)"""

        #graph js
        g = Graph("Overall Score Performance", graph_index, s1, op, overall_rep_flag, pointlabels)
        graph_index += 1
        wg = Graph("Writing Score Performance", graph_index, writing_scores, writing_class_average, None, pointlabels)
        graph_index += 1
        rg = Graph("Reading Score Performance", graph_index, reading_scores, reading_class_average, None, pointlabels)
        graph_index += 1
        mg = Graph("Math Score Performance", graph_index, math_scores, math_class_average, None, pointlabels)
        graph_index += 1
        graphs.append(g)
        graphs.append(wg)
        graphs.append(rg)
        graphs.append(mg)


        FILE = open(user_directory(self.name, self.c) + DIR_SEP + "graph_report" + ".html", "w")
        lines = []

        scores = self.average_scores()

        #HTML opener
        lines.append('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">' + endl)
        lines.append('<html xmlns="http://www.w3.org/1999/xhtml">' + endl)
        lines.append('<head>')
        lines.append('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' + endl)
        lines.append('{% load staticfiles %}' + endl)
        lines.append('<link rel="stylesheet" type="text/css" href=' + '"' + "{% static 'style.css' %}" + '"' + '/>' + endl)
        lines.append('<title>Graph Report</title>' + endl)
        lines += g.head()
        lines.append('</head>' + endl)
        lines.append('<body>' + endl)
        lines.append('<div id="page">' + endl)
        lines.append('<div id="header">' + endl)
        lines.append('<img src=' + '"' + "{% static 'ml.png' %}" + '" alt="Excelerate" style="float: right; width: 35%; margin-right: 35%;"/>' +
            '<a href="{% url ' + "'" +'login:dashboard' + "'" + '%}">  <img src=' + '"' + "{% static 'back_rev.png' %}" + '" alt="Home" style="float: left; width: 15%; margin-left: 5%;"> </a>' + endl)
        lines.append('<p style="clear: both;">' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div id="content">' + endl)
        lines.append('<div id="container">' + endl)
        lines.append('<div id="main">' + endl)
        lines.append('<div id="menu">' + endl)
        lines.append('<h2 style="text-align:center;">Graph Report: ' + self.name + '</h2>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div id="text">' + endl)
            

        #Average Results
        lines.append('<h1>Score Graphs</h1>' + endl)
        lines.append('<hr color="#BBBBBB" size="2" width="100%">' + endl)


        index = 0
        #Graph js
        for graph in graphs:
            lines += graph.html(False, True, False)
            index += 1

            if index == 2:
                lines.append('<div style="page-break-after: always;">' '</div>')
            lines.append('<br><hr color="#4169EF" size="1" width="90%">' + endl)
            lines.append(endl)





        #Footer
        lines.append('<br>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div class="clear"></div>' + endl)
        lines.append('<div id="footer">' + endl)
        #lines.append('<p><a>' + self.name + ' Advanced Report</a></p>' + endl)
        lines.append('<p><img src="' + "{% static 'Mini Logo.png' %}" '" width="8%" alt="Excelerate" /></p>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)

        #scripts
        lines += g.body()


        lines.append('</body>' + endl)
        lines.append('</html>' + endl)
        lines.append(endl)

        FILE.writelines(lines)
        FILE.close()

    def c_graph_HTML(self, schoolscoreO, schoolscoreM, schoolscoreR, schoolscoreW, schoolname, schoolcolor, schoolbgcolor):
        graphs = []
        s1 = []
        writing_scores = []
        reading_scores = []
        math_scores = []
        graph_index = 1
        index = 1
        School_Score_O = schoolscoreO
        School_Score_M = schoolscoreM
        School_Score_R = schoolscoreR
        School_Score_W = schoolscoreW
        School_Name = schoolname
        School_Color = schoolcolor
        School_Bgcolor = schoolbgcolor
        #calculate all scores
        for test in self.tests_taken:
            s1.append([index,test.score_summary.total_score()])
            writing_scores.append([index, test.score_summary.section_scores[WRITING_TYPE]])
            reading_scores.append([index, test.score_summary.section_scores[READING_TYPE]])
            math_scores.append([index, test.score_summary.section_scores[MATH_TYPE]])
            index += 1

        #graph js
        g = C_Graph("Overall Score Performance vs " + str(School_Name) + " Admission Average" , graph_index, s1, School_Score_O, School_Name, School_Color, School_Bgcolor)
        graph_index += 1
        wg = C_Graph("Writing Score Performance vs " + str(School_Name) + " Admission Average" , graph_index, writing_scores, School_Score_W, School_Name, School_Color, School_Bgcolor)
        graph_index += 1
        rg = C_Graph("Reading Score Performance vs " + str(School_Name) + " Admission Average" , graph_index, reading_scores, School_Score_R, School_Name, School_Color, School_Bgcolor)
        graph_index += 1
        mg = C_Graph("Math Score Performance vs " + str(School_Name) + " Admission Average" , graph_index, math_scores, School_Score_M, School_Name, School_Color, School_Bgcolor)
        graph_index += 1
        graphs.append(g)
        graphs.append(wg)
        graphs.append(rg)
        graphs.append(mg)


        FILE = open(user_directory(self.name, self.c) + DIR_SEP + "college_graph_report" + ".html", "w")
        lines = []

        scores = self.average_scores()

        #HTML opener
        lines.append('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">' + endl)
        lines.append('<html xmlns="http://www.w3.org/1999/xhtml">' + endl)
        lines.append('<head>')
        lines.append('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' + endl)
        lines.append('<link rel="stylesheet" type="text/css" href="../../../HTML/style.css" />' + endl)
        lines.append('<title>College Tracker</title>' + endl)
        lines += g.head()
        lines.append('</head>' + endl)
        lines.append('<body>' + endl)
        lines.append('<div id="page">' + endl)
        lines.append('<div id="header">' + endl)
        lines.append('<img src="../../../HTML/EliteLogo.png" width="35%" alt="Excelerate" />' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div id="content">' + endl)
        lines.append('<div id="container">' + endl)
        lines.append('<div id="main">' + endl)
        lines.append('<div id="menu">' + endl)
        lines.append('<h2 style="text-align:center;"> Graph Report: ' + self.name + '</h2>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div id="text">' + endl)
            

        #Average Results
        lines.append('<h1 style = "text-align:center;" > ' + str(School_Name) + ' Graphs</h1>' + endl)
        lines.append('<hr color="#BBBBBB" size="2" width="100%">' + endl)

        #Graph js
        for graph in graphs:
            lines += graph.html()
            index += 1
            if index == 2:
                lines.append('<div style="page-break-after: always;">' '</div>')

            #lines.append('<h2> ' + 'Students accepted to' + str(School_Name) + 'score an average of' + str(schoolscoreO) + ' </h2>' + endl)
            #lines.append('<br><hr color="#4169EF" size="1" width="90%">' + endl)
            if index < 4:
                lines.append(endl)


        #Footer
        lines.append('<br>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        #lines.append('<p><img src="../../../HTML/Mini Logo.png" width="10%" alt="Excelerate" /></p>' + endl)
        lines.append('<div class="clear"></div>' + endl)
        lines.append('<div id="footer">' + endl)
        #lines.append('<p><a>' + self.name + ' Advanced Report</a></p>' + endl)
        lines.append('<p><img src="' + "{% static 'Mini Logo.png' %}" '" width="8%" alt="Excelerate" /></p>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        #scripts
        lines += g.body()


        lines.append('</body>' + endl)
        lines.append('</html>' + endl)
        lines.append(endl)

        FILE.writelines(lines)
        FILE.close()

    def grade_HTML(self, ts, ss, test_type, reverse = '-3'):
        graphs = []
        s1 = []
        writing_scores = []
        reading_scores = []
        math_scores = []
        graph_index = 1
        index = 1
        Q_index = 0
        Q_type = []
        SQ_type = {}
        sq_index = 0
        #calculate all scores
        for test in self.tests_taken:
            s1.append([index,test.score_summary.total_score()])
            writing_scores.append([index, test.score_summary.section_scores[WRITING_TYPE]])
            reading_scores.append([index, test.score_summary.section_scores[READING_TYPE]])
            math_scores.append([index, test.score_summary.section_scores[MATH_TYPE]])
            index += 1



        FILE = open(user_directory(self.name, self.c) + DIR_SEP + "grade" + ".html", "w")
        lines = []

        scores = self.average_scores()

        #HTML opener
        lines.append('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">' + endl)
        lines.append('<html xmlns="http://www.w3.org/1999/xhtml">' + endl)
        lines.append('<head>')
        lines.append('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' + endl)
        lines.append('{% load staticfiles %}' + endl)
        lines.append('<link rel="stylesheet" type="text/css" href=' + '"' + "{% static 'style.css' %}" + '"' + '/>' + endl)
        lines.append('<script src=' + '"' + "{% static 'raphael.2.1.0.min.js' %}" + '"></script>' + endl)
        lines.append('<script src=' + '"' + "{% static 'justgage.1.0.1.js' %}" + '"></script>' + endl)
        #load c3.css
        lines.append('<link rel="stylesheet" type= "text/css"  href=' + '"' + "{% static 'c3.css' %}" +  ' "/> ' + endl)
        #load d3.js and c3.js 
        lines.append('<script charset="utf-8"  src=' + '"' + "{% static 'd3.min.js' %}" + '"></script>  ' + endl)
        lines.append('<script src=' + '"' + "{% static 'c3.min.js' %}" + '"></script>  ' + endl)

        lines.append('<style>' + endl)
        lines.append("#g0 {" + endl)
        lines.append("width:470px; height:200px;" + endl)
        lines.append("display: inline-block; " + endl)
        lines.append("margin: 1em;")
        lines.append("}" + endl)

        lines.append("#g1, #g2, #g3 {" + endl)
        lines.append("width:137px; height:100px;" + endl)
        lines.append("display: inline-block; " + endl)
        lines.append("margin: 1em;")
        lines.append("}" + endl)
        lines.append('</style>' + endl)

        lines.append('<title>Test Report</title>' + endl)
        lines.append('</head>' + endl)
        lines.append('<body>' + endl)
        lines.append('<style>' + endl)
        lines.append('html,' + endl)
        lines.append('body {' + endl)
        lines.append('background-image: url("{% static' +  " 'mysite/css/images/overlay.svg' " +  '%}");')
        lines.append('background-size: 100%;' + endl)
        lines.append('}' + endl)
        lines.append('</style>' + endl)     
        lines.append('<div id="page">' + endl)
        lines.append('<div id="header">' + endl)
        lines.append('<img src=' + '"' + "{% static 'ml.png' %}" + '" alt="Excelerate" style="float: right; width: 35%; margin-right: 35%;"/>' +
            '<a href="{% url ' + "'" +'login:dashboard' + "'" + '%}">  <img src=' + '"' + "{% static 'back_rev.png' %}" + '" alt="Home" style="float: left; width: 15%; margin-left: 5%;"> </a>' + endl)
        lines.append('<p style="clear: both;">' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div id="content">' + endl)
        lines.append('<div id="container">' + endl)
        lines.append('<div id="main">' + endl)
        lines.append('<div id="menu">' + endl)
        lines.append('<h2 style="text-align:center;">Test Review: ' + self.name + '</h2>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div id="text">' + endl)
            

        #Average Results
        if str(test_type) == str(FULL_TEST):
            lines.append('<h1>Graded Test Report</h1>' + endl)
            #lines.append(str(ss))
            lines.append('<br>' + endl)
            lines.append('<div  id = "g0"></div>' + endl)
            lines.append('<div  id = "g1"></div>' + endl)
            lines.append('<div  id = "g2"></div>' + endl)
            lines.append('<div  id = "g3"></div>' + endl)
            lines += self.make_gauge(4, ss.get_donut_data() ,['Overall Score', 'Writing Score', 'Reading Score', 'Math Score'])
            lines.append('<hr color="#BBBBBB" size="2" width="100%">' + endl)
            lines.append('<h1>Section Breakdown</h1>' + endl)
            lines.append('<div id="chart1' + '" style="height:245px; width:245px;"></div>' + endl)
            lines.append('<script>' + endl)
            lines.append('var chart1' + ' = c3.generate({' + endl)
            lines.append('size:{ height:245, width:245},' + endl)
            lines.append('bindto: ' + "'" + '#chart1' + "'" + ',' + endl)
            lines.append('data: {' + endl)
            lines.append('labels: true,' + endl)
            lines.append('columns: [' + endl)
            lines.append('[' + "'" + 'Correct' +"'," + str(ts.get_summary(WRITING_TYPE).qa) + "]," +  "['" + 'Missed' +"'," + str(ts.get_summary(WRITING_TYPE).qm) + "]," + "['" + 'Blank' +"'," + str(ts.get_summary(WRITING_TYPE).qb) + "]"+ endl)
            lines.append('],' + endl)
            lines.append('type:' + "'" + 'donut' + "'" + endl)
            lines.append('},' + endl)
            lines.append('color:{ pattern: [' + "'" + '#66FF66' + "'," + "'" + '#FF3300' + "'," + "'" + '#e5e544' + "']" + endl)
            lines.append('},')
            lines.append('donut: {' + endl)
            #lines.append('title:' + "'" + 'Writing' + "'" + endl)
            lines.append('} });' + endl)
            lines.append('</script>' + endl)
            lines.append(endl)
            lines.append(str(ts.get_summary(WRITING_TYPE)) + endl)
            lines.append('<br>' + endl)
            lines.append('<hr color="#BBBBBB" size="2" width="100%">' + endl)
            lines.append('<br>' + endl)
            lines.append('<div id="chart2' + '" style="height:245px; width:245px;"></div>' + endl)
            lines.append('<script>' + endl)
            lines.append('var chart2' + ' = c3.generate({' + endl)
            lines.append('size:{ height:245, width:245},' + endl)
            lines.append('bindto: ' + "'" + '#chart2' + "'" + ',' + endl)
            lines.append('data: {' + endl)
            lines.append('labels: true,' + endl)
            lines.append('columns: [' + endl)
            lines.append('[' + "'" + 'Correct' +"'," + str(ts.get_summary(READING_TYPE).qa) + "]," +  "['" + 'Missed' +"'," + str(ts.get_summary(READING_TYPE).qm) + "]," + "['" + 'Blank' +"'," + str(ts.get_summary(READING_TYPE).qb) + "]"+ endl)
            lines.append('],' + endl)
            lines.append('type:' + "'" + 'donut' + "'" + endl)
            lines.append('},' + endl)
            lines.append('color:{ pattern: [' + "'" + '#66FF66' + "'," + "'" + '#FF3300' + "'," + "'" + '#e5e544' + "']" + endl)
            lines.append('},')
            lines.append('donut: {' + endl)
            #lines.append('title:' + "'" + 'Reading' + "'" + endl)
            lines.append('} });' + endl)
            lines.append('</script>' + endl)
            lines.append(endl)
            lines.append(str(ts.get_summary(READING_TYPE)) + endl)
            lines.append('<br>' + endl)
            lines.append('<hr color="#BBBBBB" size="2" width="100%">' + endl)
            lines.append('<br>' + endl)
            lines.append('<div id="chart3' + '" style="height:245px; width:245px;"></div>' + endl)
            lines.append('<script>' + endl)
            lines.append('var chart3'+ ' = c3.generate({' + endl)
            lines.append('size:{ height:245, width:245},' + endl)
            lines.append('bindto: ' + "'" + '#chart3' + "'" + ',' + endl)
            lines.append('data: {' + endl)
            lines.append('labels: true,' + endl)
            lines.append('columns: [' + endl)
            lines.append('[' + "'" + 'Correct' +"'," + str(ts.get_summary(MATH_TYPE).qa) + "]," +  "['" + 'Missed' +"'," + str(ts.get_summary(MATH_TYPE).qm) + "]," + "['" + 'Blank' +"'," + str(ts.get_summary(MATH_TYPE).qb) + "]"+ endl)
            lines.append('],' + endl)
            lines.append('type:' + "'" + 'donut' + "'" + endl)
            lines.append('},' + endl)
            lines.append('color:{ pattern: [' + "'" + '#66FF66' + "'," + "'" + '#FF3300' + "'," + "'" + '#e5e544' + "']" + endl)
            lines.append('},')
            lines.append('donut: {' + endl)
            #lines.append('title:' + "'" + 'Math' + "'" + endl)
            lines.append('} });' + endl)
            lines.append('</script>' + endl)
            lines.append(endl)
            lines.append(str(ts.get_summary(MATH_TYPE)) + endl)
            lines.append('<br>' + endl)
            lines.append('<hr color="#BBBBBB" size="2" width="100%">' + endl)
            lines.append('<br>' + endl)
        lines.append('<h1>Question Review Section</h1>' + endl)
        lines.append('<p style = "text-align: justify;color: #348cb2" >Below are a list of questions for you to review. <br> Please take the time to go back and redo/review the question <br> Reviewing missed questions is an excellent way to practice and improve your score</p>')
        
        for section_type in TYPE_ARRAY:
            lines.append('<h2 style = "text-align: Left;color: #348cb2">' + section_name(section_type)  + '</h3>'+ endl)

            for item in ts.reports[section_type].incorrect_questions:
                if str(item[1]) == OMIT_ENTRY:
                    continue

                lines.append('<br>' + endl)
                section = item[0].split('_')[1]
                question = item[0].split('_')[2]
                lines.append('<h3 style = "text-align: Left;color: #FF8C00">Section: ' + str(section)  + '</h3>'+ endl)
                lines.append('<h3 style = "text-align: Left;color: #348cb2">Question: ' + str(question)  + '</h3>'+ endl)
                lines.append('<h3 style = "text-align: Left;color: #FF8C00" >Your Answer: ' + str(item[1])  + '</h3>'+ endl)

                #add functionality to these checkboxes to keep track of review usage later on
                lines.append('<input style = "text-align: center;color: #FF8C00" type="checkbox" id=" none" name="none" value="none">Question Review Complete</input>' + endl)
                lines.append('<br>' + endl)
                lines.append('<br>' + endl)

        """for item in MISSED_QUESTIONS_TYPE_LIST:


            del Q_type[:]

            if(item[0] == 'M'):
                for i in range (0, len(item.split(','))):
                    temp = MATH_TYPE_DICT[item.split(',')[i]]
                    Q_type.append(temp)

            elif(item[0] == 'R'):
                for i in range (0, len(item.split(','))):
                    temp = READING_TYPE_DICT[item.split(',')[i]]
                    Q_type.append(temp)

            elif(item[0] == 'W'):
                for i in range (0, len(item.split(','))):
                    temp = WRITING_TYPE_DICT[item.split(',')[i]]
                    Q_type.append(temp)

            
            SQ_type[sq_index] = str(Q_type) 
            sq_index += 1

        
        

        for item in MISSED_QUESTIONS_USRANSWER_LIST:
            if str(MISSED_QUESTIONS_USRANSWER_LIST[Q_index][0]) == OMIT_ENTRY:
                Q_index += 1
                continue
            lines.append('<br>' + endl)
            lines.append('<h3 style = "text-align: Left;color: #FF8C00">Section: ' + str(MISSED_QUESTIONS_SECTION_LIST[Q_index])  + '</h3>'+ endl)
            lines.append('<h3 style = "text-align: Left;color: #348cb2">Question: ' + str(MISSED_QUESTIONS_LIST[Q_index])  + '</h3>'+ endl)
            lines.append('<h3>Question Type Breakdown: ' + str(SQ_type[Q_index]) + ' </h3>' + endl)
            lines.append('<h3 style = "text-align: Left;color: #FF8C00" >Your Answer: ' + str(MISSED_QUESTIONS_USRANSWER_LIST[Q_index][0])  + '</h3>'+ endl)
            lines.append('<h3 style = "text-align: Left;color: #348cb2">Correct Answer: ' + str(MISSED_QUESTIONS_USRANSWER_LIST[Q_index][1])  + '</h3>'+ endl)
            lines.append('<h3 style = "text-align: Left;color: #FF8C00" >Flip to page: ' + str(MISSED_QUESTIONS_PGNUM_LIST[Q_index])  + ' and redo/review the question</h3>'+ endl)
            #add functionality to these checkboxes to keep track of review usage later on
            lines.append('<input style = "text-align: center;color: #FF8C00" type="checkbox" id=" none" name="none" value="none">Question Review Complete</input>' + endl)
            lines.append('<br>' + endl)
            lines.append('<br>' + endl)
            Q_index += 1
            """
        #Footer
        lines.append('<br>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)
        lines.append('<div class="clear"></div>' + endl)
        lines.append('<div id="footer">' + endl)
        #lines.append('<p><a>' + self.name + ' Test Report</a></p>' + endl)
        lines.append('<p><img src="' + "{% static 'Mini Logo.png' %}" '" width="8%" alt="Excelerate" /></p>' + endl)
        lines.append('</div>' + endl)
        lines.append('</div>' + endl)



        lines.append('</body>' + endl)
        lines.append('</html>' + endl)
        lines.append(endl)

        FILE.writelines(lines)
        FILE.close()

    def simple_report(self):
        FILE = open(user_directory(self.name, self.c) + DIR_SEP + "simple_report" + ".txt", "w")
        lines = []

        #Name
        line = "Name: " + self.name  + endl
        lines.append(line)
        lines.append(endl)
        lines.append(SECTION_SEP)

        #Scores
        scores = self.average_scores()
        lines.append(endl)
        lines.append("Scores:" + endl)
        lines.append("Average Total Score: " + str(scores[0]) + "/2400" + endl)
        lines.append("Average Writing Score: " + str(scores[2]) + "/800" + endl)
        lines.append("Average Reading Score: " + str(scores[1]) + "/800" + endl)
        lines.append("Average Math Score: " + str(scores[3]) + "/800" + endl)
        lines.append("Average Essay Score: " + "??/12" + endl)
        lines.append(endl)
        lines.append("Tests Taken: " + str(len(self.tests_taken)))
        lines.append(endl)
        lines.append(SECTION_SEP)
        lines.append(endl + endl)

        for test in self.tests_taken:
            lines.append(str(test.score_summary))
            lines.append(endl)

        FILE.writelines(lines)
        FILE.close()


    def advanced_report(self):
        FILE = open(user_directory(self.name, self.c)+ DIR_SEP + "advanced_report" + ".txt", "w")
        lines = []

        #Name
        line = "Name: " + self.name  + endl
        lines.append(line)
        lines.append(endl)
        lines.append(SECTION_SEP)

        #Scores
        scores = self.average_scores()
        lines.append(endl)
        lines.append("Scores:" + endl)
        lines.append("Average Total Score: " + str(scores[0]) + "/2400" + endl)
        lines.append("Average Writing Score: " + str(scores[2]) + "/800" + endl)
        lines.append("Average Reading Score: " + str(scores[1]) + "/800" + endl)
        lines.append("Average Math Score: " + str(scores[3]) + "/800" + endl)
        lines.append("Average Essay Score: " + "??/12" + endl)
        lines.append(endl)
        lines.append("Tests Taken: " + str(len(self.tests_taken)))
        lines.append(endl)
        lines.append(SECTION_SEP)

        #Section Analysis
        lines.append(endl)
        lines.append("Section Performance Analysis:" + endl)
        lines.append("Type: Total Questions | Questions Correct | Questions Missed | Questions Blank" + endl)
        lines.append(endl)
        lines.append( "_______" + endl + endl)
        
        lines.append("Writing Analytics:" + endl)
        for i in range(1, WRITING_TYPES + 1):
            lines.append(WRITING_TYPE_DICT["W" + str(i)] + ": " + str(self.data.data[WRITING_TYPE].stats["W"+str(i)]))
            lines.append(endl)
        lines.append( "_______" + endl + endl)      

        lines.append("Math Analytics:" + endl)
        for i in range(1, MATH_TYPES + 1):
            lines.append(MATH_TYPE_DICT["M" + str(i)] + ": " + str(self.data.data[MATH_TYPE].stats["M"+str(i)]))
            lines.append(endl)
        lines.append( "_______" + endl + endl)
        
        lines.append("Reading Analytics:" + endl)
        for i in range(1, READING_TYPES + 1):
            lines.append(READING_TYPE_DICT["R" + str(i)] + ": " + str(self.data.data[READING_TYPE].stats["R"+str(i)]))
            lines.append(endl)

        lines.append(SECTION_SEP)

        #Difficulty Analysis
        lines.append(endl)
        lines.append("Difficulty Analytics:" + endl)
        lines.append(endl)
        lines.append("Writing:" + endl)
        for i in range(1,6):
            lines.append("Level " + str(i) + ": " + str(self.data.data[WRITING_TYPE].stats["L" + str(i)]))
            lines.append(endl)
        lines.append( "_______" + endl + endl)

        lines.append(endl)
        lines.append("Math:" + endl)
        for i in range(1,6):
            lines.append("Level " + str(i) + ": " + str(self.data.data[MATH_TYPE].stats["L" + str(i)]))
            lines.append(endl)
        lines.append( "_______" + endl + endl)

        lines.append(endl)
        lines.append("Reading:" + endl)
        for i in range(1,6):
            lines.append("Level " + str(i) + ": " + str(self.data.data[READING_TYPE].stats["L" + str(i)]))
            lines.append(endl)

        lines.append(endl)

        FILE.writelines(lines)
        FILE.close()


    def update_intended_score(self, score):
        self.intended_score = int(score)

    def update_intended_date(self, date):
        self.intended_date = date

    def delete_test(self, test_index, test_id_2_remove, test_date_2_remove, filepath):
        data = []
        with open(filepath) as f:
            lines = f.readlines()
            assert(self.name in lines[0]) #verifies name
            flag = False
            begin_flag = False
            done = False
            count = int(test_index) - 1

            #brings you past the beginning information
            for x in range(len(lines)):
                line = lines[x]
                if (begin_flag == False):
                    if ("Intended_Score" in line):
                        begin_flag = True
                    data.append(line)

                else:
                    if (done):
                        data.append(line)
                        continue

                    #getting to the correct index
                    if (count != 0):
                        if (line.strip() == SECTION_SEP):
                            count -= 1
                        data.append(line)

                    #verifying section and then deleting
                    else:
                        if (flag == True) and (line.strip() != SECTION_SEP):
                            continue
                        if (flag == True) and (line.strip() == SECTION_SEP):
                            flag = False
                            done = True
                            continue
                        if 'TEST_ID' in line:
                            test_id = line.split(' ')[1].strip()
                            if (test_id == test_id_2_remove) and (lines[x+3].strip() == test_date_2_remove):
                                flag = True
                                continue
                        data.append(line)

        with open(filepath, "wb") as nf:
            for newlines in data:
                nf.write(newlines)

