<commit_before>"use strict";
/*global alert: true, console: true, ODSA, PARAMS */

/** This file should only be referenced by AVs (not modules) */

(function ($) {
  var settings = {},
      odsaUtils = {};

  // Eliminate dependence on OpenDSA framework by creating stubbed function definitions
  if (typeof ODSA === "undefined") {
    // If MODULE_ORIGIN is not specified, assume they are on the same domain
    if (!settings.MODULE_ORIGIN) {
      settings.MODULE_ORIGIN = location.protocol + '//' + location.host;
    }

    // Define the console object if it doesn't exist to support IE without developer tools
    if (!(window.console && console.log)) {
      console = {
        log: function () {},
        debug: function () {},
        info: function () {},
        warn: function () {},
        error: function () {}
      };
    }

    odsaUtils.serverEnabled = function () {return false; };
    odsaUtils.roundPercent = function (number) {
      return Math.round(number * 100) / 100;
    };
    odsaUtils.inDebugMode = function () {
      return localStorage.DEBUG_MODE === 'true';
    };

    odsaUtils.sendEventData = function () {};
    odsaUtils.logUserAction = function (type, desc, exerName, eventUiid) {};
    odsaUtils.logEvent = function (data) {};

    window.ODSA = {};
    window.ODSA.SETTINGS = settings;
    window.ODSA.UTILS = odsaUtils;

    console.warn('odsaUtils.js was not included, using fallback function definitions');
  } else {
    settings = ODSA.SETTINGS;
    odsaUtils = ODSA.UTILS;
  }

  /**
   * A timestamp when the user started looking at the page
   */
  var focusTime = +new Date();

  /**
   * The total amount of time the user has spent on the current exercise instance
   */
  var totalTime = 0;

  /**
   * Stores the empty contents of the avcontainer, used for reset
   */
  var emptyContent = '';

  /**
   * Set a flag indicating the user cannot receive credit for the
   * current exercise instance after viewing the model answer
   */
  var allowCredit = true;

  /**
   * A unique instance identifier, used to group interaction events from a single instance
   */
  var uiid = +new Date();

  //*****************************************************************************
  //*************                    AV FUNCTIONS                   *************
  //*****************************************************************************

  // Controls whether the AV submits its own event data or allows its parent page to handle event data
  var selfLoggingEnabled = PARAMS.selfLoggingEnabled === 'false' ? false : true;

  /**
   * Generates a JSAV event to log the initial state of an AV or exercise
   *   - initData - A JSON object that contains the initial state of an exercise
   *     Conventions:
   *       - The key for automatically generated data should have a prefix 'gen_'
   *         - Ex: an automatically generated array would be 'gen_array'
   *       - The key for user generated data should have a prefix 'user_'
   *         - Ex: Array data the user enters in the textbox should have a key 'user_array'
   */
  function logExerciseInit(initData) {
    // Reset the uiid (unique instance identifier)
    uiid = +new Date();
    totalTime = 0;

    var data = {av: settings.AV_NAME, type: 'odsa-exercise-init', desc: JSON.stringify(initData)};
    $("body").trigger("jsav-log-event", [data]);
  }

  /**
   * Generates a JSAV event which triggers the code to give a user credit for an exercise
   */
  function awardCompletionCredit() {
    var data = {av: settings.AV_NAME, type: 'odsa-award-credit'};
    $("body").trigger("jsav-log-event", [data]);
  }

  /**
   * Resets the AV to its initial state
   */
  function reset(flag) {
    // Replace the contents of the avcontainer with the save initial state
    $('.avcontainer').unbind().html(emptyContent);

    // Clear the array values field, when no params given and reset button hit
    if (flag !== true && !$('#arrayValues').prop("disabled")) {
      $('#arrayValues').val("");
    }
  }

  /**
   * Loads a JSON file that contains language and / or code translations
   */
  function getConfig(url) {
    var configurationFile;
    var result = {};

    // the selected language and code
    var lang = JSAV_EXERCISE_OPTIONS.lang || JSAV_OPTIONS.lang || "en";
    var code = JSAV_EXERCISE_OPTIONS.code || JSAV_OPTIONS.code;

    // download the entire configurationFileiguration file
    $.ajax({
      url: url,
      async: false,
      dataType: "json",
      success: function (data) { configurationFile = data; }
    });

    // URL where the language information can be downloaded from
    var langUrl = '';

    // set the language object for the result
    if (typeof configurationFile.translations === "string") {
      langUrl = configurationFile.translations.replace('{lang}', lang);
    } else if (typeof configurationFile.translations[lang] === "object") {
      // return the language object in the configurationFileiguration file
      result.language = configurationFile.translations[lang];
    } else if (typeof configurationFile.translations[lang] === "string") {
      langUrl = url.split("/");
      langUrl.pop();
      langUrl = langUrl.join("/");
      if (langUrl !== "") {
        langUrl += "/";
      }
      langUrl += configurationFile.translations[lang];
    }

    // If langUrl is initialized, download the language data
    if (langUrl !== '') {
      // download the language object from the url
      var langData;

      $.ajax({
        url: langUrl,
        async: false,
        dataType: "json",
        success: function (data) { langData = data; }
      });

      result.language = langData;
    }

    // set the code object for the result
    if (configurationFile.code) {
      result.code = configurationFile.code[code];
    }

    return result;
  }

  /**
   * Reset the text of the exercise title, instructions label and instructions to the specified language
   */
  function setTitleAndInstructions(container, translation) {
    if (typeof translation === 'undefined') {
      console.error('There is no translation support for language ' + JSAV_OPTIONS.lang);
    }

    var container = $(container);
    var elem;
    for (var field in translation) {
      if (translation.hasOwnProperty(field)) {
        elem = container.find(field);
        if (elem.size() > 0) {
          if (elem.is('input')) {
            elem.attr('value', translation[field]);
          } else {
            elem.html(translation[field]);
          }
        }
      }
    }

    //var $title = container.find(".exerciseTitle");
    //var $instructLabel = container.find(".instructLabel");
    //var $instructions = container.find(".instructions");

    //if ($title.length !== 0) {
    //  $title.html(translation.exerciseTitle);
    //}
    //if ($instructLabel.length !== 0) {
    //  $instructLabel.html(translation.instructLabel);
    //}
    //if ($instructions.length !== 0) {
    //  $instructions.html(translation.instructions);
    //}
  }

  // Add AV utility functions to the global ODSA object
  var av = {};
  av.logExerciseInit = logExerciseInit;<commit_msg>Edited setTitleAndInstructions so that it does not use fields beginning with 'av_' as jQuery selectors.<commit_after>"use strict";
/*global alert: true, console: true, ODSA, PARAMS */

/** This file should only be referenced by AVs (not modules) */

(function ($) {
  var settings = {},
      odsaUtils = {};

  // Eliminate dependence on OpenDSA framework by creating stubbed function definitions
  if (typeof ODSA === "undefined") {
    // If MODULE_ORIGIN is not specified, assume they are on the same domain
    if (!settings.MODULE_ORIGIN) {
      settings.MODULE_ORIGIN = location.protocol + '//' + location.host;
    }

    // Define the console object if it doesn't exist to support IE without developer tools
    if (!(window.console && console.log)) {
      console = {
        log: function () {},
        debug: function () {},
        info: function () {},
        warn: function () {},
        error: function () {}
      };
    }

    odsaUtils.serverEnabled = function () {return false; };
    odsaUtils.roundPercent = function (number) {
      return Math.round(number * 100) / 100;
    };
    odsaUtils.inDebugMode = function () {
      return localStorage.DEBUG_MODE === 'true';
    };

    odsaUtils.sendEventData = function () {};
    odsaUtils.logUserAction = function (type, desc, exerName, eventUiid) {};
    odsaUtils.logEvent = function (data) {};

    window.ODSA = {};
    window.ODSA.SETTINGS = settings;
    window.ODSA.UTILS = odsaUtils;

    console.warn('odsaUtils.js was not included, using fallback function definitions');
  } else {
    settings = ODSA.SETTINGS;
    odsaUtils = ODSA.UTILS;
  }

  /**
   * A timestamp when the user started looking at the page
   */
  var focusTime = +new Date();

  /**
   * The total amount of time the user has spent on the current exercise instance
   */
  var totalTime = 0;

  /**
   * Stores the empty contents of the avcontainer, used for reset
   */
  var emptyContent = '';

  /**
   * Set a flag indicating the user cannot receive credit for the
   * current exercise instance after viewing the model answer
   */
  var allowCredit = true;

  /**
   * A unique instance identifier, used to group interaction events from a single instance
   */
  var uiid = +new Date();

  //*****************************************************************************
  //*************                    AV FUNCTIONS                   *************
  //*****************************************************************************

  // Controls whether the AV submits its own event data or allows its parent page to handle event data
  var selfLoggingEnabled = PARAMS.selfLoggingEnabled === 'false' ? false : true;

  /**
   * Generates a JSAV event to log the initial state of an AV or exercise
   *   - initData - A JSON object that contains the initial state of an exercise
   *     Conventions:
   *       - The key for automatically generated data should have a prefix 'gen_'
   *         - Ex: an automatically generated array would be 'gen_array'
   *       - The key for user generated data should have a prefix 'user_'
   *         - Ex: Array data the user enters in the textbox should have a key 'user_array'
   */
  function logExerciseInit(initData) {
    // Reset the uiid (unique instance identifier)
    uiid = +new Date();
    totalTime = 0;

    var data = {av: settings.AV_NAME, type: 'odsa-exercise-init', desc: JSON.stringify(initData)};
    $("body").trigger("jsav-log-event", [data]);
  }

  /**
   * Generates a JSAV event which triggers the code to give a user credit for an exercise
   */
  function awardCompletionCredit() {
    var data = {av: settings.AV_NAME, type: 'odsa-award-credit'};
    $("body").trigger("jsav-log-event", [data]);
  }

  /**
   * Resets the AV to its initial state
   */
  function reset(flag) {
    // Replace the contents of the avcontainer with the save initial state
    $('.avcontainer').unbind().html(emptyContent);

    // Clear the array values field, when no params given and reset button hit
    if (flag !== true && !$('#arrayValues').prop("disabled")) {
      $('#arrayValues').val("");
    }
  }

  /**
   * Loads a JSON file that contains language and / or code translations
   */
  function getConfig(url) {
    var configurationFile;
    var result = {};

    // the selected language and code
    var lang = JSAV_EXERCISE_OPTIONS.lang || JSAV_OPTIONS.lang || "en";
    var code = JSAV_EXERCISE_OPTIONS.code || JSAV_OPTIONS.code;

    // download the entire configurationFileiguration file
    $.ajax({
      url: url,
      async: false,
      dataType: "json",
      success: function (data) { configurationFile = data; }
    });

    // URL where the language information can be downloaded from
    var langUrl = '';

    // set the language object for the result
    if (typeof configurationFile.translations === "string") {
      langUrl = configurationFile.translations.replace('{lang}', lang);
    } else if (typeof configurationFile.translations[lang] === "object") {
      // return the language object in the configurationFileiguration file
      result.language = configurationFile.translations[lang];
    } else if (typeof configurationFile.translations[lang] === "string") {
      langUrl = url.split("/");
      langUrl.pop();
      langUrl = langUrl.join("/");
      if (langUrl !== "") {
        langUrl += "/";
      }
      langUrl += configurationFile.translations[lang];
    }

    // If langUrl is initialized, download the language data
    if (langUrl !== '') {
      // download the language object from the url
      var langData;

      $.ajax({
        url: langUrl,
        async: false,
        dataType: "json",
        success: function (data) { langData = data; }
      });

      result.language = langData;
    }

    // set the code object for the result
    if (configurationFile.code) {
      result.code = configurationFile.code[code];
    }

    return result;
  }

  /**
   * Reset the text of the exercise title, instructions label and instructions to the specified language
   */
  function setTitleAndInstructions(container, translation) {
    if (typeof translation === 'undefined') {
      console.error('There is no translation support for language ' + JSAV_OPTIONS.lang);
    }

    container = $(container);
    var elem;
    for (var field in translation) {
      if (translation.hasOwnProperty(field) && field.indexOf("av_") !== 0) {
        elem = container.find(field);
        if (elem.size() > 0) {
          if (elem.is('input')) {
            elem.attr('value', translation[field]);
          } else {
            elem.html(translation[field]);
          }
        }
      }
    }

    //var $title = container.find(".exerciseTitle");
    //var $instructLabel = container.find(".instructLabel");
    //var $instructions = container.find(".instructions");

    //if ($title.length !== 0) {
    //  $title.html(translation.exerciseTitle);
    //}
    //if ($instructLabel.length !== 0) {
    //  $instructLabel.html(translation.instructLabel);
    //}
    //if ($instructions.length !== 0) {
    //  $instructions.html(translation.instructions);
    //}
  }

  // Add AV utility functions to the global ODSA object
  var av = {};
  av.logExerciseInit = logExerciseInit;
{'fraction_non_alphanumeric': 0.14391835601221883, 'fraction_numerical': 0.0017356289919466814, 'mean_word_length': 2.7011819116135665, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3163551', 'n_tokens_mistral': 4099, 'n_tokens_neox': 3938, 'n_words': 1276}