var mongoose = require('mongoose');
var Question = require('./QuestionModel');
var User = require('./UserModel');


var quizSchema = new mongoose.Schema({
    title: { type : String},
    topic: String,
    keywords: [String],
    author: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    },
    questions:[{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Question'
    }],
    registeredUsers: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }],
    activeUsers: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }],
    active: {type:Boolean, default:false},
    correctAnswers: { type: Number, default: 0 },

});



quizSchema.statics.createQuiz = function(info, callback) {
    var new_quiz = new this(info);
    return new_quiz.save(callback);
}

quizSchema.statics.editQuiz = function(info, callback) {

}

quizSchema.statics.addUser = function(quizId,userId, callback) {
    this.update({_id:quizId},{$push:{registeredUsers:userId}},callback);
}

quizSchema.statics.removeUser = function(userId, callback) {
    params.quiz.registeredUsers.remove(userId);
    params.quiz.save(callback);
}

quizSchema.statics.addActiveUser = function(userId, callback) {
    params.quiz.activeUsers.push(userId);
    params.quiz.save(callback);
}

quizSchema.statics.getQuizSubscribe=function(userId, callback) {
    return this.find({registeredUsers: userId},'title topic author',function(err,quiz){
        console.log("quiz:" + quiz);
        var author_array=[]
        console.log("autore questionario" + quiz.author);
        var i=quiz.length;
        quiz.forEach(function(elem){
            console.log(elem.author);
            User.getUser(elem.author,function(err,author){
                console.log(author);
                author_array.push(author.name)
            })
            i--;
            if (i==0){
                quiz.author=author_array;
                callback(null,quiz)
            }
        })
    });
}

quizSchema.statics.getPersonalQuizzes = function(author, callback) {
    return this.find({ author: author}, callback);
}

quizSchema.statics.searchQuiz=function(tosearch, callback){
    return this.find({'title':  new RegExp(tosearch, "i") },'title', callback);
};

quizSchema.statics.getQuiz=function(quizId,userId,callback){
    return this.findOne({'_id':quizId,'activeUsers':userId},'title keywords topic questions active',function (err, quiz){
        if (quiz)
            if (quiz.active){
                var questions_quiz=[];
                quiz.questions.forEach(function(elem) {
                        Question.getQuestion(elem,function(err,question){
                            questions_quiz.push(question);
                            if(questions_quiz.length==quiz.questions.length){
                                var qR = questions_quiz.reverse();
                                quiz.questions=qR;
                                return callback(err,quiz);
                            }
                        })
                });
    
            }
            else
                return callback(new Error("Questionario non abilitato"))
        else
            return callback(new Error("Utente non autorizzato"))
    })
};



var Quiz = mongoose.model('Quiz', quizSchema);
module.exports = Quiz;

@@ -1472,131 +1472,8 @@
 z)%7B%0A
-        console.log(%22quiz:%22 + quiz);%0A        var author_array=%5B%5D%0A        console.log(%22autore questionario%22 + quiz.author);%0A
     
@@ -1533,48 +1533,16 @@
 elem
-)%7B%0A            console.log(elem.author);
+,index)%7B
 %0A   
@@ -1617,63 +1617,27 @@
     
-console.log(author);%0A                author_array.push(
+quiz%5Bindex%5D.author=
 auth
@@ -1643,17 +1643,16 @@
 hor.name
-)
 %0A       
@@ -1703,50 +1703,8 @@
 0)%7B%0A
-                quiz.author=author_array;%0A
     

Create quiz ok