<commit_before>/**
*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Question, Quiz} from './models';
import {User} from '../user/models';
import {longPollers} from '../long-pollers/views';
import EventStream from '../event-stream';

export const quiz = new Quiz();
export const presentationListeners = new EventStream();
longPollers.broadcast(quiz.getState());
presentationListeners.broadcast(quiz.getState());


export function adminStateJson(req, res) {
  Question.find().then(questions => {
    questions = questions.map(q => q.toObject());
    for (const question of questions) {
      question.active = quiz.activeQuestion && quiz.activeQuestion._id.equals(question._id);
      if (question.active) {
        question.closed = !quiz.acceptingAnswers;
        question.revealingAnswers = quiz.revealingAnswers;
        question.showingLiveResults = quiz.showingLiveResults;
      }
    }
    res.json({
      questions,
      showingLeaderboard: quiz.showingLeaderboard,
      showingVideo: quiz.showingVideo,
      showingBlackout: quiz.showingBlackout
    });
  });
}

export function deleteQuestionJson(req, res) {
  Question.findByIdAndRemove(req.body.id).then(() => {
    adminStateJson(req, res);
    return;
  }).catch(err => {
    res.status(500).json({err: err.message});
  });
}

export function updateQuestionJson(req, res) {
  const update = {
    title: req.body.title,
    text: req.body.text,
    code: req.body.code,
    codeType: req.body.codeType,
    multiple: !!req.body.multiple,
    scored: !!req.body.scored,
    answers: req.body.answers
  };

  if (!Array.isArray(update.answers)) {
    update.answers = [];
  }

  // remove answers without text
  update.answers = update.answers.filter(answer => String(answer.text).trim());

  if (!update.answers.length) {
    res.json({err: "No answers provided"});
    return;
  }

  let p;

  if (req.body.id) {
    p = Question.findByIdAndUpdate(req.body.id, update, {new: true});
  }
  else {
    p = new Question(update).save();
  }

  p.then(newQuestion => {
    if (!newQuestion) throw Error('No record found');
    adminStateJson(req, res);
    return;
  }).catch(err => {
    res.status(500).json({err: err.message});
  });
}

export function setQuestionJson(req, res) {
  Question.findById(req.body.id).then(question => {
    if (!question) {
      res.status(404).json({err: "Question not found"});
      return;
    }

    quiz.setQuestion(question);
    presentationListeners.broadcast(quiz.getState());
    longPollers.broadcast(quiz.getState());

    adminStateJson(req, res);
  }).catch(err => {
    res.status(500).json({err: err.message});
  });
}

export function closeQuestionJson(req, res) {
  Question.findById(req.body.id).then(question => {
    if (!question) {
      res.status(404).json({err: "Question not found"});
      return;
    }

    if (!quiz.activeQuestion || !question.equals(quiz.activeQuestion)) {
      res.status(404).json({err: "This isn't the active question"});
      return;
    }

    quiz.closeForAnswers();
    presentationListeners.broadcast(quiz.getState());
    longPollers.broadcast(quiz.getState());

    adminStateJson(req, res);
  }).catch(err => {
    res.status(500).json({err: err.message});<commit_msg>Reset the averages on new question<commit_after>/**
*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {Question, Quiz} from './models';
import {User} from '../user/models';
import {longPollers} from '../long-pollers/views';
import EventStream from '../event-stream';

export const quiz = new Quiz();
export const presentationListeners = new EventStream();
longPollers.broadcast(quiz.getState());
presentationListeners.broadcast(quiz.getState());


export function adminStateJson(req, res) {
  Question.find().then(questions => {
    questions = questions.map(q => q.toObject());
    for (const question of questions) {
      question.active = quiz.activeQuestion && quiz.activeQuestion._id.equals(question._id);
      if (question.active) {
        question.closed = !quiz.acceptingAnswers;
        question.revealingAnswers = quiz.revealingAnswers;
        question.showingLiveResults = quiz.showingLiveResults;
      }
    }
    res.json({
      questions,
      showingLeaderboard: quiz.showingLeaderboard,
      showingVideo: quiz.showingVideo,
      showingBlackout: quiz.showingBlackout
    });
  });
}

export function deleteQuestionJson(req, res) {
  Question.findByIdAndRemove(req.body.id).then(() => {
    adminStateJson(req, res);
    return;
  }).catch(err => {
    res.status(500).json({err: err.message});
  });
}

export function updateQuestionJson(req, res) {
  const update = {
    title: req.body.title,
    text: req.body.text,
    code: req.body.code,
    codeType: req.body.codeType,
    multiple: !!req.body.multiple,
    scored: !!req.body.scored,
    answers: req.body.answers
  };

  if (!Array.isArray(update.answers)) {
    update.answers = [];
  }

  // remove answers without text
  update.answers = update.answers.filter(answer => String(answer.text).trim());

  if (!update.answers.length) {
    res.json({err: "No answers provided"});
    return;
  }

  let p;

  if (req.body.id) {
    p = Question.findByIdAndUpdate(req.body.id, update, {new: true});
  }
  else {
    p = new Question(update).save();
  }

  p.then(newQuestion => {
    if (!newQuestion) throw Error('No record found');
    adminStateJson(req, res);
    return;
  }).catch(err => {
    res.status(500).json({err: err.message});
  });
}

export function setQuestionJson(req, res) {
  Question.findById(req.body.id).then(question => {
    if (!question) {
      res.status(404).json({err: "Question not found"});
      return;
    }

    quiz.setQuestion(question);
    presentationListeners.broadcast(
      Object.assign({averages: undefined}, quiz.getState())
    );
    longPollers.broadcast(quiz.getState());

    adminStateJson(req, res);
  }).catch(err => {
    res.status(500).json({err: err.message});
  });
}

export function closeQuestionJson(req, res) {
  Question.findById(req.body.id).then(question => {
    if (!question) {
      res.status(404).json({err: "Question not found"});
      return;
    }

    if (!quiz.activeQuestion || !question.equals(quiz.activeQuestion)) {
      res.status(404).json({err: "This isn't the active question"});
      return;
    }

    quiz.closeForAnswers();
    presentationListeners.broadcast(quiz.getState());
    longPollers.broadcast(quiz.getState());

    adminStateJson(req, res);
  }).catch(err => {
    res.status(500).json({err: err.message});
{'fraction_non_alphanumeric': 0.14532374100719425, 'fraction_numerical': 0.007586657946370177, 'mean_word_length': 3.5593321407274896, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6052064', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2354, 'n_words': 549}