コード例 #1
0
def createClass():
    body = request.json
    body["students"] = []
    body["classworks"] = []

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

    classesData = readFile(classFileLocation)

    # check class id apakah 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:
        classesData.append(body)
        writeFile(classFileLocation, classesData)

        usersData = readFile(userFileLocation)
        for user in usersData:
            if user["userid"] == body["teacher"]:
                user["classes_as_teacher"].append(body["classid"])

        writeFile(userFileLocation, usersData)

        response["data"] = body

    return jsonify(response)
コード例 #2
0
def outFromClass(id):
    body = request.json

    # hapus student di kelas
    classesData = readFile(classFileLocation)
    for class_ in classesData:
        if class_["classid"] == id:
            if body["userid"] in class_["students"]:
                class_["students"].remove(body["userid"])
                writeFile(classFileLocation, classesData)
            else:
                return "User ID {} tidak ada di Class ID {}".format(
                    body["userid"], id)
            break

    # hapus kelas di student
    usersData = readFile(userFileLocation)
    for user in usersData:
        if user["userid"] == body["userid"]:
            user["classes_as_student"].remove(id)
            writeFile(userFileLocation, usersData)
            break

    thisUser = getUser(body["userid"])
    return thisUser
コード例 #3
0
def joinClass():
    body = request.json

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

    for class_ in classesData:
        if class_["classid"] == body["classid"]:  # kalau kelasnya ketemu
            if body["userid"] not in class_[
                    "students"]:  # kalau belum join ke kelas ini sebelumnya
                class_["students"].append(body["userid"])
                break

    writeFile(classFileLocation, classesData)

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

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

    writeFile(userFileLocation, usersData)

    # return data user setelah join
    thisClass = getClass(body["classid"])
    return thisClass
コード例 #4
0
def updateClasswork(id):
    body = request.json

    classworksData = readFile(classworkFileLocation)

    for classwork in classworksData:
        if classwork["classworkid"] == id:
            classwork["question"] = body["question"]

    writeFile(classworkFileLocation, classworksData)

    thisClasswork = getClasswork(id)
    return thisClasswork
コード例 #5
0
def updateClass(id):
    body = request.json

    classesData = readFile(classFileLocation)

    for class_ in classesData:
        if class_["classid"] == id:  # kalau user yang mau diupdate ketemu
            class_["classname"] = body["classname"]
            break

    writeFile(classFileLocation, classesData)

    classData = getClass(id)
    return classData
コード例 #6
0
def register():
    response = {}

    usersData = readFile(userFileLocation)

    body = request.json
    body["classes_as_student"] = []
    body["classes_as_teacher"] = []
    body['password'] = enkripsi(body['password'])
    usersData.append(body)

    writeFile(userFileLocation, usersData)

    response["message"] = "Register successful"
    response["data"] = body
    return jsonify(response)
コード例 #7
0
def updateUser(id):
    body = request.json

    usersData = readFile(userFileLocation)

    for user in usersData:
        if user["userid"] == id:  # kalau user yang mau diupdate ketemu
            user["username"] = body["username"]
            user["password"] = body["password"]
            user["fullname"] = body["fullname"]
            user["email"] = body["email"]
            break

    writeFile(userFileLocation, usersData)

    userData = getUser(id)
    return userData
コード例 #8
0
def createClasswork():
    classworksData = readFile(classworkFileLocation)

    body = request.json
    body["answers"] = []

    classworksData.append(body)

    writeFile(classworkFileLocation, classworksData)

    classesData = readFile(classFileLocation)

    for class_ in classesData:
        if class_["classid"] == body["classid"]:
            class_["classworks"].append(body["classworkid"])

    writeFile(classFileLocation, classesData)

    return jsonify(body)
コード例 #9
0
def deleteClasswork(id):
    ## delete di file classwork
    classworksData = readFile(classworkFileLocation)
    for i in range(len(classworksData)):
        if classworksData[i]["classworkid"] == id:
            del classworksData[i]  # hapus classwork
            break

    writeFile(classworkFileLocation, classworksData)

    ## delete classwork di class
    classesData = readFile(classFileLocation)
    print(classesData)
    for class_ in classesData:
        if id in class_["classworks"]:
            class_["classworks"].remove(id)
            break

    writeFile(classFileLocation, classesData)

    return "Classwork ID {} has been deleted".format(id)
コード例 #10
0
def assignClasswork(id):
    body = request.json

    classworksData = readFile(classworkFileLocation)

    studentAnswerFound = False
    for classwork in classworksData:
        if classwork["classworkid"] == id:  # kalau ketemu classworknya
            for answer in classwork[
                    "answers"]:  # cari apakah student udah pernah assign sebelumnya
                if answer["userid"] == body[
                        "userid"]:  # kalau udah pernah assign, ganti answernya aja
                    answer["answer"] = body["answer"]
                    studentAnswerFound = True  # jawaban student ketemu (pernah assign), break
                    break
            if not studentAnswerFound:  # kalau student belum pernah assign
                classwork["answers"].append(body)  # append ke answers
            break

    writeFile(classworkFileLocation, classworksData)

    thisClasswork = getClasswork(id)
    return thisClasswork