def create_quiz_form_db(json_data, sql=None):

    new_id = ''
    try:
        max_id = connect_and_query("select max(id) as max_id from exams")
        new_id = int(max_id[0].get("max_id")) + 1
        title = json_data.get('quiz_name')
        desc = json_data.get('quiz_description', '')
        ids = json_data.get('item_ids')
        user_profile = json_data.get('user_profile', {})
        options = json_data.get('options', {})
        # create exam in DB
        values = (str(new_id), title, desc)
        connect_and_execute(insert_sqls[3], values)

        # get items
        if not sql:
            sql = queries[11].format(','.join(map(str, ids)))
        results = connect_and_query(sql)

        # do actual creation in a thread
        if json_data:
            thread = threading.Thread(target=create_quiz_thread,
                                      args=(title, desc, results, options,
                                            user_profile, new_id))
            thread.start()

        return {"quiz": {'quiz_name': title, 'exam_id': new_id}}

    except Exception as exc:
        logger.error("Quiz creation DB exception: " + str(exc))
        return {"quiz": {}, 'error': str(exc)}
def create_quiz(subject='Islam', topic=None):
    q = queries[9]
    sql = q.format(subject, 50)
    if topic:
        sql = queries[10].format(subject, topic, 15)

    results = connect_and_query(sql)
    # print(json.dumps(results, indent=4))
    items, tags = process_items(results)
    user = json.loads(results[0].get('metadata', {}))
    user = user.get('user_profile')
    pt = "We are compiling the results for 25 Quizzes. " \
         "Please complete any missed ones before the 29th of Ramadan.\n\n" \
         "We are very grateful for your overwhelming response, " \
         "support and feedback to our daily Ramadan quizzes. " \
         "Ramadan will shortly be over but our striving to gain knowledge " \
         "should continue. We will soon be introducing a feature to allow " \
         "you to contribute your own questions and create your own quizzes. " \
         "Please look out for more updates on this soon."

    creds = GoogleCredentials().get_credential()
    options = {'email': 0, 'name': 1, 'show_correct': 1}
    params = [
        'Ramadan 2020 Quiz Review 7',
        "All " + topic + " Questions (" + str(len(results)) + "). "
        "See all quizzes here: http://muslimscholars.info/quiz/\n\n" + pt,
        user, items, options
    ]
    return run_app_script(creds, function_name='createQuiz', params=params)
Ejemplo n.º 3
0
def get_items_db(json_data):
    subject = json_data.get('subject')
    topic = json_data.get('topic')
    keyword = json_data.get('keyword')
    limit = json_data.get('limit', 50)
    # keyword = json_data.get('keyword')
    user_id = json_data.get('user_id', "")

    sql = queries[9].format(subject, limit)
    if len(user_id) > 2:
        sql = queries[7].format(subject, limit, user_id)
    if topic:
        sql = queries[10].format(subject, topic, limit)
        if len(user_id) > 2:
            sql = queries[8].format(subject, topic, limit, user_id)
    if keyword:
        sql = queries[18].format(subject, keyword, limit)
        if len(user_id) > 2:
            sql = queries[19].format(subject, keyword, limit, user_id)

    # print(sql)
    results = connect_and_query(sql)
    results = sorted(results, key=lambda pos: pos['id'])

    for result in results:
        result['choices'] = json.loads(result.get('choices', {}))
        result['metadata'] = json.loads(result.get('metadata', {}))
        try:
            result['topic'] = json.loads(result['topic'])
            result['sub_topics'] = json.loads(result['sub_topics'])
        except:
            pass
        result['answer'] = json.loads(result.get('answer', {}))

    return {'items': results, 'total_items': len(results)}
Ejemplo n.º 4
0
def get_quiz_form_db(json_data):
    user_id = json_data.get('user_id')
    user_profile = json_data.get('user_profile')
    limit = json_data.get('limit', 500)

    user_email = user_profile.get('email')
    user_id = user_profile.get('googleId')

    # get all items by user
    sql = queries[12].format(user_email, limit)
    # get all items for admin
    if user_email in get_config("admin_users") and \
            user_id in get_config("admin_user_ids"):
        sql = queries[16].format(limit)

    items = connect_and_query(sql)
    for item in items:
        item['choices'] = json.loads(item.get('choices', {}))
        item['metadata'] = json.loads(item.get('metadata', {}))
        try:
            item['topic'] = json.loads(item['topic'])
            item['sub_topics'] = json.loads(item['sub_topics'])
        except:
            pass

    # get all quizzes by user
    sql = queries[13].format(user_email, limit)
    # for admin, get all quizzes
    if user_email in get_config("admin_users") and \
            user_id in get_config("admin_user_ids"):
        sql = queries[17].format(limit)

    exams = connect_and_query(sql)
    for item in exams:
        item['metadata'] = json.loads(item.get('metadata') or '{}')
        item['user_profile'] = json.loads(item.get('user_profile') or '{}')
        #print(item.get('analysis'))
        item['analysis'] = json.loads(item.get('analysis') or '{}')

    return {"items": items,
            "exams": exams,
            "items_count": len(items),
            "exams_count": len(exams)}
Ejemplo n.º 5
0
def search_quiz(json_data):
    id = json_data.get('id')
    name = json_data.get('name')
    keyword = json_data.get('keyword')
    results = []
    if id and name:
        sql = queries[14].format(id, name)
        results = connect_and_query(sql)
    if keyword:
        sql = queries[15].format(keyword)
        results = connect_and_query(sql)

    for result in results:
        result['user_profile'] = json.loads(result.get('user_profile') or '{}')
        result['metadata'] = json.loads(result.get('metadata') or '{}')

    if len(results) > 0:
        return {"quiz": results, "quiz_count": len(results)}
    else:
        return {"quiz": "Not found!", "quiz_count": 0}