def create_free_quiz(quiz_name, quiz_language_id, quiz_short_desc): """ Checks for errors and creates a free quiz if no errors found """ # Check for errors if not quiz_name or not quiz_language_id or not quiz_short_desc: raise ValueError("Property in body is empty") # Check types if type(quiz_name) != str or type(quiz_language_id) != int or type( quiz_short_desc) != str: raise ValueError("Types in body are incorrect") if len(quiz_name) > 20 or len(quiz_short_desc) > 20: raise ValueError("One field in body is too long") # Also check that the language_id actually exists query = """ SELECT language_id FROM languages WHERE language_id = %s """ languages = db.query(query, (quiz_language_id)) if not languages: raise ValueError("Language ID does not exist") query = """ INSERT INTO quizzes (quiz_name, quiz_language_id, quiz_short_desc) VALUES (%s, %s, %s) """ db.insert_query(query, (quiz_name, quiz_language_id, quiz_short_desc))
def add_class(course_id, class_id): """ Adds a class to a course """ query = """ INSERT INTO classes_courses (cc_class_id, cc_course_id) VALUES (%s, %s) """ db.insert_query(query, (class_id, course_id)) return
def insert_course(teacher_id, name): """ Inserts a course and returns its id """ query = """ INSERT INTO courses (course_teacher_id, course_name) VALUES (%s, %s) """ course_id = db.insert_query(query, (teacher_id, name)) return course_id
def insert_class(teacher_id, name): """ Inserts a class and returns its id """ query = """ INSERT INTO classes (class_teacher_id, class_name) VALUES (%s, %s) """ class_id = db.insert_query(query, (teacher_id, name)) return class_id
def insert_teacher(name, email, teacher_hash): """ Inserts a new teacher into the database """ query = """ INSERT INTO teachers (teacher_name, teacher_email, teacher_hash) VALUES (%s, %s, %s) """ teacher_id = db.insert_query(query, (name, email, teacher_hash)) return teacher_id
def insert_attempt(question_id, student_id): """ Inserts a users question attempt """ query = """ INSERT INTO attempts VALUES (DEFAULT, %s, %s) """ attempt_id = db.insert_query(query, (question_id, student_id)) return attempt_id
def add_question(quiz_id, description): """ Adds a question to a quiz """ query = """ INSERT INTO questions (question_quiz_id, question_description) VALUES (%s, %s) """ question_id = db.insert_query(query, (quiz_id, description)) return question_id
def insert_quiz(course_id, name, start_date, end_date, description, language): """ Inserts a quiz and returns its id """ query = """ INSERT INTO quizzes (quiz_course_id, quiz_name, quiz_start_date, quiz_end_date, quiz_language_id, quiz_short_desc) VALUES (%s, %s, %s, %s, %s, %s) """ quiz_id = db.insert_query( query, (course_id, name, start_date, end_date, language, description)) return quiz_id
def create_test_case(question_id, test_input, test_expected): """ Creates a test case """ if not test_input or not test_expected: raise ValueError("Property in body is empty") if type(test_input) != str or type(test_expected) != str: raise ValueError("test_input or test_expected were not a string") if len(test_input) > 500 or len(test_expected) > 500: raise ValueError("Length of property was too long") query = """ INSERT INTO tests (test_question_id, test_input, test_expected) VALUES (%s, %s, %s) """ test_id = db.insert_query(query, (question_id, test_input, test_expected)) return test_id