Beispiel #1
0
def joinClassTeacher():
    body = request.json

    #nambahin userid ke classes-file
    classesData = readFile(classesFileLocation)

    for class_ in classesData:
        if body["classid"] == class_["classid"]:
            if (body["userid"]
                    not in class_["teachers"]) and (body["userid"]
                                                    not in class_["students"]):
                class_["teachers"].append(body["userid"])
            else:
                return "Join failed! Anda sudah terdaftar!"

    classesFile = writeFile(classesFileLocation, classesData)

    #nambahin classid ke users-file
    usersData = readFile(usersFileLocation)

    for user in usersData:
        if body["userid"] == user["userid"]:
            if (body["classid"] not in user["classes_as_student"]) and (
                    body["classid"] not in user["classes_as_teacher"]):
                user["classes_as_teacher"].append(body["classid"])
            else:
                return "Join failed! Anda sudah terdaftar!"

    usersFile = writeFile(usersFileLocation, usersData)

    return "Success"
Beispiel #2
0
def deleteClassWork(id):
    response = {}
    response["message"] = "Error. Classwork is already deleted!"
    response["data"] = []

    #read-write data classwork
    classesWorkData = readFile(classesWorkFileLocation)

    for classWork in range(len(classesWorkData)):
        if id == classesWorkData[classWork]["workid"]:
            response["message"] = "Classwork {} deleted successfuly!".format(
                id)
            response["data"] = classesWorkData[classWork]
            del classesWorkData[classWork]
            break
        # return "Error. Classwork sudah tidak ada!"

    #write data classwork
    writeFile(classesWorkFileLocation, classesWorkData)

    #read data class
    classesData = readFile(classesFileLocation)

    for class_ in classesData:
        if id in class_["classwork"]:
            class_["classwork"].remove(id)
            break

    #write data class
    writeFile(classesFileLocation, classesData)

    return jsonify(response)
Beispiel #3
0
def joinClass():
    body = request.json

    #nambahin userid ke classes-file
    classesData = readFile(classesFileLocation)

    for class_ in classesData:
        if body["classid"] == class_["classid"]:
            if (body["userid"]
                    not in class_["students"]) and (body["userid"]
                                                    not in class_["teachers"]):
                class_["students"].append(body["userid"])

    writeFile(classesFileLocation, classesData)

    #nambahin classid ke users-file
    usersData = readFile(usersFileLocation)

    for user in usersData:
        if body["userid"] == user["userid"]:
            if (body["classid"] not in user["classes_as_student"]) and (
                    body["classid"] not in user["classes_as_teacher"]):
                user["classes_as_student"].append(body["classid"])

    writeFile(usersFileLocation, usersData)

    response = {}
    response["message"] = "User {} joined to {} class".format(
        body["userid"], body["classid"])
    response["data"] = body

    return jsonify(response)
Beispiel #4
0
def register():
    response = {}
    response["message"] = "Username sudah ada, ganti username"
    response["data"] = []

    body = request.json
    userData = readFile(usersFileLocation)

    for user in userData:
        if body["username"] in user["username"]:
            return jsonify(response)
        else:
            pass

    body["password"] = encrypt(3, body["password"])

    response["message"] = "Registration successfully"
    response["data"] = body

    userData.append(body)

    #siapin file buat di write
    writeFile(usersFileLocation, userData)

    return jsonify(response)
Beispiel #5
0
def createClassWork():
    body = request.json

    response = {}
    response["message"] = "{} is wrong class id!".format(body["class"])
    response["data"] = []

    #read data
    classWorkData = readFile(classesWorkFileLocation)
    classesData = classList().json

    for class_ in classesData:
        if class_["classid"] == body["class"]:
            class_["classwork"].append(body["workid"])
            classWorkData.append(body)

            response["message"] = "Classwork {} created successfully!".format(
                body["workid"])
            response["data"] = body

            #write data
            writeFile(classesWorkFileLocation, classWorkData)
            writeFile(classesFileLocation, classesData)

    return jsonify(response)
Beispiel #6
0
def outClass(id):
    body = request.json

    response = {}
    response["message"] = "Fail! User is not in the class!"
    response["data"] = []

    #read classes-file
    classesData = readFile(classesFileLocation)

    #read users-file
    usersData = readFile(usersFileLocation)

    for class_ in classesData:
        if id == class_["classid"]:
            for user in usersData:
                if body["userid"] in class_["students"]:
                    response[
                        "message"] = "User {} out from the class successfully!".format(
                            body["userid"])
                    response["data"] = body
                    class_["students"].remove(body["userid"])
                    break

    writeFile(classesFileLocation, classesData)

    for user in usersData:
        if body["userid"] == user["userid"]:
            if id in user["classes_as_student"]:
                user["classes_as_student"].remove(id)
                break

    writeFile(usersFileLocation, usersData)

    return jsonify(response)
Beispiel #7
0
def updateUserId(id):
    usersData = getUser().json
    body = request.json

    for user in usersData:
        if id == user["userid"]:
            user["email"] = body["email"]
            user["fullname"] = body["fullname"]
            user["password"] = body["password"]

    #update ke file users
    writeFile(usersFileLocation, usersData)

    return "Update Success"
Beispiel #8
0
def updateClassWork(id):
    body = request.json

    #read data classwork
    classesWorkData = readFile(classesWorkFileLocation)

    for classWork in classesWorkData:
        if id == classWork["workid"]:
            classWork["question"] = body["question"]

    #write data classwork
    writeFile(classesWorkFileLocation, classesWorkData)

    response = {}
    response["message"] = "Update question on work id {} success!".format(id)
    response["data"] = body

    return jsonify(response)
Beispiel #9
0
def updateClassId(id):
    classesData = classList().json
    body = request.json

    response = {}
    response["message"] = "Unknown error. No changes"
    response["data"] = []

    for class_ in classesData:
        if id == class_["classid"]:
            class_["classname"] = body["classname"]
            response["message"] = "Class update successfully!"
            response["data"] = body
            break

    #update ke file class
    writeFile(classesFileLocation, classesData)

    return jsonify(response)
Beispiel #10
0
def assignClassWork(id):
    body = request.json

    #siapin data classwork
    classWorkData = readFile(classesWorkFileLocation)

    #siapin data user
    usersData = readFile(usersFileLocation)

    for classwork in classWorkData:
        if id == classwork["workid"]:
            for user in usersData:
                if body["userid"] == user["userid"]:
                    classwork["answer"].append(body)

    writeFile(classesWorkFileLocation, classWorkData)
    response = {}
    response["message"] = "Answer sent successfully!"
    response["body"] = body

    return jsonify(response)
Beispiel #11
0
def createClass():
    body = request.json
    body["students"] = []
    body["classwork"] = []

    response = {}
    response["message"] = "Create Class Success"
    response["data"] = []

    #baca file classes
    classesData = readFile(classesFileLocation)

    # check apakah kelas sudah ada
    classidAlreadyExist = False
    for class_ in classesData:
        if class_["classid"] == body["classid"]:
            response["message"] = "Class ID {} is already exist".format(
                body["classid"])
            classidAlreadyExist = True
            break

    if not classidAlreadyExist:
        #tambah ke file classes
        classesData.append(body)
        writeFile(classesFileLocation, classesData)

        #baca file users
        usersData = readFile(usersFileLocation)
        for user in usersData:
            if user["userid"] == body["teachers"]:
                user["classes_as_teacher"].append(body["classid"])

        #tambah classid ke file users
        writeFile(usersFileLocation, usersData)

        response["data"] = body

    return jsonify(response)
Beispiel #12
0
def deleteClass(id):
    response = {}
    response["message"] = "Error. Kelas sudah tidak ada!"
    response["data"] = []

    #read data class
    classesData = readFile(classesFileLocation)

    for class_ in range(len(classesData)):
        if id == classesData[class_]["classid"]:
            response["message"] = "Class {} deleted successfully!".format(id)
            response["data"] = classesData[class_]
            del classesData[class_]
            break
        # return "Error. Kelas sudah tidak ada!"

    #write data class
    writeFile(classesFileLocation, classesData)

    #read data classwork
    classesWorkData = readFile(classesWorkFileLocation)

    for classWork in range(len(classesWorkData)):
        if id == classesWorkData[classWork]["class"]:
            del classesWorkData[classWork]
            break

    #write data classWork
    writeFile(classesWorkFileLocation, classesWorkData)

    #read data user
    usersData = readFile(usersFileLocation)

    for user in usersData:
        if id in user["classes_as_student"]:
            user["classes_as_student"].remove(id)
            break

    #write data users
    writeFile(usersFileLocation, usersData)

    return jsonify(response)