<commit_before>var HttpError = require('../components/http-error'),
    User = require('../models/user'),
    validator = require('validator'),
    async = require('asyncawait/async'),
    await = require('asyncawait/await'),
    Course = require('../models/course'),
    Submission = require('../models/submission'),
    Quiz = require('../models/quiz'),
    _ = require('underscore'),
    gcm = require('node-gcm');

var env = process.env.NODE_ENV || 'development';
var config = config = require('../config/config')[env];
var gcmSender = new gcm.Sender(config.gcmApiKey);

function formatQuiz(quiz, dateSolved) {
    return {
        id: quiz.id,
        title: quiz.title,
        description: quiz.description || null,
        course_id: quiz.course_id,
        date_created: quiz.date_created || null,
        date_due: quiz.date_due || null,
        date_closed: quiz.date_closed || null,
        date_published: quiz.date_published || null,
        date_solved: dateSolved || null
    }
}

function validateQuiz(quiz) {
    var errors = [];
    if (!quiz) {
        errors.push('Invalid or missing request content.');
    }
    if (!quiz.course_id) {
        errors.push('Invalid or missing course_id.');
    }
    if (!validator.isLength(quiz.title, 1)) {
        errors.push('Invalid or missing title.');
    }
    if (errors.length > 0) {
        return { success: false, errors: errors.join() };
    }
    return { success: true, errors: null };
}

exports.listQuizzes = async(function (req, res, next) {
    var courseId = req.query.course_id;
    var options = courseId ? { course_id: courseId } : {};
    var quizzes = await(Quiz.find(options).exec());
    if (!quizzes) {
        return res.json([]);
    }
    return res.json(quizzes.map(function (quiz) {
        return formatQuiz(quiz);
    }));
});

exports.createQuiz = function (req, res, next) {
    var quizData = req.body;
    var result = validateQuiz(quizData);
    if (!result.success) {
        return next(new HttpError(400, result.errors));<commit_msg>quiz solved<commit_after>var HttpError = require('../components/http-error'),
    User = require('../models/user'),
    validator = require('validator'),
    async = require('asyncawait/async'),
    await = require('asyncawait/await'),
    Course = require('../models/course'),
    Submission = require('../models/submission'),
    Quiz = require('../models/quiz'),
    _ = require('underscore'),
    gcm = require('node-gcm');

var env = process.env.NODE_ENV || 'development';
var config = config = require('../config/config')[env];
var gcmSender = new gcm.Sender(config.gcmApiKey);

function formatQuiz(quiz, dateSolved) {
    return {
        id: quiz.id,
        title: quiz.title,
        description: quiz.description || null,
        course_id: quiz.course_id,
        date_created: quiz.date_created || null,
        date_due: quiz.date_due || null,
        date_closed: quiz.date_closed || null,
        date_published: quiz.date_published || null,
        date_solved: dateSolved || null
    }
}

function validateQuiz(quiz) {
    var errors = [];
    if (!quiz) {
        errors.push('Invalid or missing request content.');
    }
    if (!quiz.course_id) {
        errors.push('Invalid or missing course_id.');
    }
    if (!validator.isLength(quiz.title, 1)) {
        errors.push('Invalid or missing title.');
    }
    if (errors.length > 0) {
        return { success: false, errors: errors.join() };
    }
    return { success: true, errors: null };
}

exports.listQuizzes = async(function (req, res, next) {
    var courseId = req.query.course_id;
    var options = courseId ? { course_id: courseId } : {};
    var quizzes = await(Quiz.find(options).exec());
    if (!quizzes) {
        return res.json([]);
    }
    return res.json(await(quizzes.map(async(function (quiz) {
        var submission = await(Submission.findOne({
            user_id: req.user.id,
            quiz_id: quiz.id
        }).exec());
        var dateSolved = null;
        if (submission) {
            dateSolved = submission.date_submitted;
        }
        return formatQuiz(quiz, dateSolved);
    }))));
});

exports.createQuiz = function (req, res, next) {
    var quizData = req.body;
    var result = validateQuiz(quizData);
    if (!result.success) {
        return next(new HttpError(400, result.errors));
{'fraction_non_alphanumeric': 0.15200368493781669, 'fraction_numerical': 0.0023030861354214646, 'mean_word_length': 2.7863993025283347, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2441664', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1368, 'n_words': 294}