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)
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
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
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
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
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)
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
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)
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)
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