<commit_before>/*jshint newcap: false */
'use strict';

module.exports =
angular.module('quill-grammar.services.firebase.grammarActivity', [
  'firebase',
  'underscore',
  require('./../../../../.tmp/config.js').name,
  require('./../rule.js').name,
  require('./../localStorage.js').name,
  require('./question.js').name,
])
/*@ngInject*/
.factory('GrammarActivity', function (firebaseUrl, $firebaseObject, _, RuleService, $q, Question, SentenceLocalStorage, ConceptsFBService) {
  function GrammarActivity(data) {
    if (data) {
      _.extend(this, data);
    }
    return this;
  }
  GrammarActivity.ref = new Firebase(firebaseUrl + '/v2/grammarActivities');

  GrammarActivity.DEFAULT_QUESTION_QUANTITY = 3;

  // 'Class' methods

  GrammarActivity.getById = function (id) {
    return $firebaseObject(GrammarActivity.ref.child(id)).$loaded().then(function (data) {
      return new GrammarActivity(data);
    }).then(loadQuestionsForFirebaseActivity);
  };

  /*
   * Create a grammar activity from a custom set of rule IDs and a passage ID.
   *
   * This should only be used during play session, not for
   * creating activities to save to firebase.
   *
   * Returns a promise to match the API of GrammarActivity.getById().
   */
  GrammarActivity.fromPassageResults = function (ruleNumbers, passageId) {
    return ConceptsFBService.getByRuleNumbers(ruleNumbers).then(function (concepts) {
      var activity = new GrammarActivity({
        passageId: passageId,
        concepts: concepts
      });
      var quantities = _.times(concepts.length, function () {return GrammarActivity.DEFAULT_QUESTION_QUANTITY; });
      activity.questions = loadQuestionsFromConcepts(concepts, quantities);
      return activity;
    }, function onError (err) {
      console.error('Error loading concepts from Firebase', err);
    });
  };

  function loadQuestionsForFirebaseActivity(activity) {
    var quantities = _.pluck(activity.concepts, 'quantity');
    var conceptIds = _.keys(activity.concepts);
    return ConceptsFBService.getByIds(conceptIds).then(function (concepts) {
      activity.concepts = concepts;
      return loadQuestionsFromConcepts(concepts, quantities);
    }).then(function (questions) {
      activity.questions = questions;
      return activity;
    });
  }

  /*
   * Return a list of questions to use in the play session.
   *
   * 1st arg is an array of concepts that have already been loaded
   * from Firebase.
   * 2nd arg is a array of integers where each entry corresponds
   * to the number of questions to return for the corresponding concept
   * in the first list.
   *
   * Returns a list of questions.
   */
  function loadQuestionsFromConcepts(concepts, quantities) {
    var questionsData = _.chain(concepts)
      .map(function (concept, i) {
        _.each(concept.questions, function (question) {
          question.conceptUid = concept.concept_level_0.uid;
          question.conceptIndex = i; // For lookup in getConceptForQuestion()
        });
        return concept.questions;
      })
      .map(function (questionSets, i) {
        return _.sample(questionSets, quantities[i]);
      })<commit_msg>Add error message when no concepts found for proofreading activity<commit_after>/*jshint newcap: false */
'use strict';

module.exports =
angular.module('quill-grammar.services.firebase.grammarActivity', [
  'firebase',
  'underscore',
  require('./../../../../.tmp/config.js').name,
  require('./../rule.js').name,
  require('./../localStorage.js').name,
  require('./question.js').name,
])
/*@ngInject*/
.factory('GrammarActivity', function (firebaseUrl, $firebaseObject, _, RuleService, $q, Question, SentenceLocalStorage, ConceptsFBService) {
  function GrammarActivity(data) {
    if (data) {
      _.extend(this, data);
    }
    return this;
  }
  GrammarActivity.ref = new Firebase(firebaseUrl + '/v2/grammarActivities');

  GrammarActivity.DEFAULT_QUESTION_QUANTITY = 3;

  // 'Class' methods

  GrammarActivity.getById = function (id) {
    return $firebaseObject(GrammarActivity.ref.child(id)).$loaded().then(function (data) {
      return new GrammarActivity(data);
    }).then(loadQuestionsForFirebaseActivity);
  };

  /*
   * Create a grammar activity from a custom set of rule IDs and a passage ID.
   *
   * This should only be used during play session, not for
   * creating activities to save to firebase.
   *
   * Returns a promise to match the API of GrammarActivity.getById().
   */
  GrammarActivity.fromPassageResults = function (ruleNumbers, passageId) {
    return ConceptsFBService.getByRuleNumbers(ruleNumbers).then(function (concepts) {
      var activity = new GrammarActivity({
        passageId: passageId,
        concepts: concepts
      });
      var quantities = _.times(concepts.length, function () {return GrammarActivity.DEFAULT_QUESTION_QUANTITY; });
      activity.questions = loadQuestionsFromConcepts(concepts, quantities);
      return activity;
    }, function onError (err) {
      console.error('Error loading concepts from Firebase', err);
    });
  };

  function loadQuestionsForFirebaseActivity(activity) {
    var quantities = _.pluck(activity.concepts, 'quantity');
    var conceptIds = _.keys(activity.concepts);
    return ConceptsFBService.getByIds(conceptIds).then(function (concepts) {
      activity.concepts = concepts;
      return loadQuestionsFromConcepts(concepts, quantities);
    }).then(function (questions) {
      activity.questions = questions;
      return activity;
    });
  }

  /*
   * Return a list of questions to use in the play session.
   *
   * 1st arg is an array of concepts that have already been loaded
   * from Firebase.
   * 2nd arg is a array of integers where each entry corresponds
   * to the number of questions to return for the corresponding concept
   * in the first list.
   *
   * Returns a list of questions.
   */
  function loadQuestionsFromConcepts(concepts, quantities) {
    if (!concepts.length) {
      throw new Error('Cannot load questions for this activity: no concepts found');
    }
    var questionsData = _.chain(concepts)
      .map(function (concept, i) {
        _.each(concept.questions, function (question) {
          question.conceptUid = concept.concept_level_0.uid;
          question.conceptIndex = i; // For lookup in getConceptForQuestion()
        });
        return concept.questions;
      })
      .map(function (questionSets, i) {
        return _.sample(questionSets, quantities[i]);
      })
{'fraction_non_alphanumeric': 0.11581222977146387, 'fraction_numerical': 0.0015441630636195182, 'mean_word_length': 3.3763513513513512, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2390366', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1838, 'n_words': 484}