<commit_before>/**
 *
 * A complete Quiz engine.
 *
 * A quiz is a game in which the player attempts to find the answer to questions from multiple possible answers.
 * To create a quiz, use the `admin/quiz` component in an admin page, which will let you create a new Quiz (which is a particular type of achievement).
 *
 * Then use this quiz's ID as a parameter for your component.
 *
 * @name Quiz
 * @param {String} id The id of the quiz you want to display
 * @template {intro}    Show the title and the description of the quiz. And secondarily the identity component if the user is not connected..
 * @template {question} Show a question and its answers.
 * @template {answer}   A partial template used in the `question` template. It shows the name and the description of the answer.
 * @template {finished} Say to the user that the quiz is finish.
 * @template {result}   Show to the user his score.
 * @datasource {quiz} A collection of all the questions and their possible answers.
 * @example <div data-hull-component="games/quiz@hull"  data-hull-id="5130a76ed4384e508f000009"></div>
 */

Hull.component({

  requiredOptions: ['id'],

  templates: [
    'intro',
    'question',
    'answer',
    'finished',
    'result'
  ],

  answers: {},

  datasources: {
    quiz: function() {
      return this.quiz || this.sandbox.data.api.model(this.id);
    },
    badge: function() {
      return this.badge || this.api('me/badges/' + this.id);
    }
  },

  trackingData: function() {
    var data = { type: 'quiz' };
    var quiz = this.data.quiz;
    if (quiz && quiz.get) { data.name = quiz.get('name'); }
    return data;
  },

  initialize: function() {
    this.sandbox.on('hull.model.' + this.id + '.change', function() {
      this.render();
    }.bind(this));
    this.sandbox.on('model.hull.me.change', function() {
      if(this.loggedIn()) {this.actions.start.apply(this); }
    }.bind(this));
    this.currentQuestionIndex = 0;
    this.answers = {};
  },

  beforeRender: function(data) {
    this.quiz   = data.quiz;
    this.badge  = data.badge || {};
    if (!this.isInitialized) { this.track('init'); }

    if (data.me.id != this.currentUserId) {
      this.template = "intro";
      this.reset();
      return data;
    }

    data.result             = this.getResult(data);

    if (this.started) {
      data.questions        = this.getQuestions(data);
      data.current          = this.getCurrent(data);
    }
    return data;
  },

  afterRender: function(data) {
    this.sandbox.emit('hull.quiz.' + this.id, data);
  },

  startQuiz: function() {
    this.reset();
    this.startedAt = new Date();
    this.started = true;
    this.render('question');
  },

  reset: function() {
    this.quiz = null;
    this.badge = null;
    this.started = false;
    this.submitted = false;
    this.answers = {};
    this.currentQuestionIndex = 0;
    this.currentUserId = this.api.model('me').id;
  },

  // TODO : Refactor this please !!!!
  getTemplate: function(tpl, data) {
    if (tpl) {
      return tpl;
    }
    if (!this.loggedIn()) {
      return "intro";
    } else if (this.submitted && data.result) {
      return "result";
    } else if (data.current) {
      if (data.current.question) {
        return "question";
      } else {
        return "finished";
      }
    } else if (data.result) {
      return "result";
    }
    return "intro";
  },


  getCurrent: function(data) {
    this.currentQuestion = data.questions[this.currentQuestionIndex];
    return {
      index:            this.currentQuestionIndex,
      indexDisplayable: this.currentQuestionIndex+1,
      question:         this.currentQuestion,
      next:             data.questions[this.currentQuestionIndex + 1],
      previous:         data.questions[this.currentQuestionIndex - 1]
    };
  },<commit_msg>Another Quiz fix<commit_after>/**
 *
 * A complete Quiz engine.
 *
 * A quiz is a game in which the player attempts to find the answer to questions from multiple possible answers.
 * To create a quiz, use the `admin/quiz` component in an admin page, which will let you create a new Quiz (which is a particular type of achievement).
 *
 * Then use this quiz's ID as a parameter for your component.
 *
 * @name Quiz
 * @param {String} id The id of the quiz you want to display
 * @template {intro}    Show the title and the description of the quiz. And secondarily the identity component if the user is not connected..
 * @template {question} Show a question and its answers.
 * @template {answer}   A partial template used in the `question` template. It shows the name and the description of the answer.
 * @template {finished} Say to the user that the quiz is finish.
 * @template {result}   Show to the user his score.
 * @datasource {quiz} A collection of all the questions and their possible answers.
 * @example <div data-hull-component="games/quiz@hull"  data-hull-id="5130a76ed4384e508f000009"></div>
 */

Hull.component({

  requiredOptions: ['id'],

  templates: [
    'intro',
    'question',
    'answer',
    'finished',
    'result'
  ],

  answers: {},

  datasources: {
    quiz: function() {
      return this.quiz || this.sandbox.data.api.model(this.id);
    },
    badge: function() {
      return this.badge || this.api('me/badges/' + this.id);
    }
  },

  trackingData: function() {
    var data = { type: 'quiz' };
    var quiz = this.data.quiz;
    if (quiz && quiz.get) { data.name = quiz.get('name'); }
    return data;
  },

  initialize: function() {
    this.sandbox.on('hull.model.' + this.id + '.change', function() {
      this.render();
    }.bind(this));
    this.sandbox.on('model.hull.me.change', function() {
      if(this.loggedIn()) {this.actions.start.apply(this); }
    }.bind(this));
    this.currentQuestionIndex = 0;
    this.answers = {};
  },

  beforeRender: function(data) {
    this.quiz   = data.quiz;
    this.badge  = data.badge || {};
    if (!this.isInitialized) { this.track('init'); }

    if (data.me.id != this.currentUserId) {
      this.template = "intro";
      this.reset();
      return data;
    }

    data.result             = this.getResult(data);

    if (this.started) {
      data.questions        = this.getQuestions(data);
      data.current          = this.getCurrent(data);
    }
    return data;
  },

  afterRender: function(data) {
    this.sandbox.emit('hull.quiz.' + this.id, data);
  },

  startQuiz: function() {
    this.reset();
    this.startedAt = new Date();
    this.started = true;
    this.render('question');
  },

  reset: function() {
    this.quiz = null;
    this.badge = null;
    this.started = false;
    this.submitted = false;
    this.answers = {};
    this.currentQuestionIndex = 0;
    this.currentUserId = this.api.model('me').id;
  },

  // TODO : Refactor this please !!!!
  getTemplate: function(tpl, data) {
    if (tpl) {
      return tpl;
    }
    if (!this.loggedIn()) {
      return "intro";
    } else if (this.submitted && data.badge && data.badge.id) {
      return "result";
    } else if (data.current) {
      if (data.current.question) {
        return "question";
      } else {
        return "finished";
      }
    } else if (data.badge) {
      return "result";
    }
    return "intro";
  },


  getCurrent: function(data) {
    this.currentQuestion = data.questions[this.currentQuestionIndex];
    return {
      index:            this.currentQuestionIndex,
      indexDisplayable: this.currentQuestionIndex+1,
      question:         this.currentQuestion,
      next:             data.questions[this.currentQuestionIndex + 1],
      previous:         data.questions[this.currentQuestionIndex - 1]
    };
  },
{'fraction_non_alphanumeric': 0.13751633986928105, 'fraction_numerical': 0.006274509803921568, 'mean_word_length': 2.6038624587847385, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '503429', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2378, 'n_words': 657}