def delete_registered_problem(courseId, problemId):
    error = None
    
    targetProblem = dao.query(RegisteredProblems).\
                        filter(and_(RegisteredProblems.courseId == courseId,
                                    RegisteredProblems.problemId == problemId)).\
                        first()
    
    dao.delete(targetProblem)
        
    try:
        dao.commit()
    except exc.SQLAlchemyError:
        error = 'Error has been occurred while searching the problem to delete'
        
    return error
def class_manage_user():
    error = None
    
    try:
        ownCourses = dao.query(RegisteredCourses).\
                         filter(RegisteredCourses.courseAdministratorId == session[SessionResources().const.MEMBER_ID]).\
                         all()
    except:
        error = 'Error has been occurred while searching own courses'
        return render_template('/class_manage_user.html',
                               error=error, 
                               SETResources = SETResources,
                               SessionResources = SessionResources,
                               LanguageResources = LanguageResources,
                               ownCourses=[],
                               ownUsers=[],
                               allUsers=[],
                               colleges=[],
                               departments=[])

    # all registered users
    allUsers = (dao.query(DepartmentsDetailsOfMembers,
                          Members).\
                    join(Members,
                         Members.memberId == DepartmentsDetailsOfMembers.memberId).\
                    filter(Members.authority == SETResources().const.USER).\
                    order_by(DepartmentsDetailsOfMembers.memberId)).\
               all()

    colleges = dao.query(Colleges).\
                   all()
    departments = dao.query(Departments).\
                      all()
                      
    allUsersToData = []
    userIndex = 1
    loopIndex = 0
    for departmentsDetailsOfMember,eachUser in allUsers:
        if loopIndex == 0:
            allUsersToData.append([departmentsDetailsOfMember.memberId,
                                   eachUser.memberName,
                                   departmentsDetailsOfMember.collegeIndex,
                                   departmentsDetailsOfMember.departmentIndex])
        else:
            if eachUser.memberId == allUsersToData[userIndex-1][0]:               
                allUsersToData[userIndex-1].append(departmentsDetailsOfMember.collegeIndex)
                allUsersToData[userIndex-1].append(departmentsDetailsOfMember.departmentIndex)
            else:
                allUsersToData.append([departmentsDetailsOfMember.memberId,
                                       eachUser.memberName,
                                       departmentsDetailsOfMember.collegeIndex,
                                       departmentsDetailsOfMember.departmentIndex])
                userIndex += 1
        loopIndex += 1

    ownUsers = []
    for ownCourse in ownCourses:
        try:
            ownUsersOfCourse = (dao.query(Registrations,
                                          Members,
                                          Colleges,
                                          Departments).\
                                    join(Members,
                                         Registrations.memberId == Members.memberId).\
                                    join(DepartmentsDetailsOfMembers,
                                         DepartmentsDetailsOfMembers.memberId == Registrations.memberId).\
                                    join(Colleges,
                                         Colleges.collegeIndex == DepartmentsDetailsOfMembers.collegeIndex).\
                                    join(Departments,
                                         Departments.departmentIndex == DepartmentsDetailsOfMembers.departmentIndex).\
                                    filter(Registrations.courseId == ownCourse.courseId).\
                                    order_by(Members.memberId)).\
                               all()
        except:
            error = 'Error has been occurred while searching own users'
            return render_template('/class_manage_user.html',
                                   error=error, 
                                   SETResources = SETResources,
                                   SessionResources = SessionResources,
                                   LanguageResources = LanguageResources,
                                   ownCourses=ownCourses,
                                   ownUsers=[],
                                   allUsers=[],
                                   colleges=[],
                                   departments=[])
            
        for ownUser in ownUsersOfCourse:
            ownUsers.append(ownUser)
                
    ownUsersToData = []
    userIndex = 1
    loopIndex = 0
    for registration,eachUser,college,department in ownUsers:
        if loopIndex == 0:
            ownUsersToData.append([registration.courseId,
                                   registration.memberId,
                                   eachUser.memberName,
                                   college.collegeName,
                                   department.departmentName])
        else:
            if registration.memberId == ownUsersToData[userIndex-1][1] and registration.courseId == ownUsersToData[userIndex-1][0]:
                ownUsersToData[userIndex-1].append(college.collegeName)
                ownUsersToData[userIndex-1].append(department.departmentName)
            else:
                ownUsersToData.append([registration.courseId,
                                       registration.memberId,
                                       eachUser.memberName,
                                       college.collegeName,
                                       department.departmentName])
                userIndex += 1  
        loopIndex += 1
    
    if request.method == 'POST':
        for form in request.form:
            if 'delete' in form:
                courseId,memberId = form[7:].split('_')
                targetUser = dao.query(Registrations).\
                                 filter(and_(Registrations.courseId == courseId,
                                             Registrations.memberId == memberId)).\
                                 first()
                dao.delete(targetUser)
                dao.commit()
            
        return redirect(url_for('.class_manage_user'))
    
    return render_template('/class_manage_user.html',
                           error=error, 
                           SETResources = SETResources,
                           SessionResources = SessionResources,
                           LanguageResources = LanguageResources,
                           ownCourses=ownCourses,
                           ownUsers=ownUsersToData,
                           allUsers=allUsersToData,
                           colleges=colleges,
                           departments=departments)