<commit_before>/**
 * Copyright (C) 2011 Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

define([
  'i18n!message_students',
  'jquery' /* $ */,
  'jsx/shared/helpers/numberHelper',
  'jquery.instructure_forms' /* formSubmit */,
  'jqueryui/dialog',
  'jquery.instructure_misc_plugins', /* showIf */
], function (I18n, $, numberHelper) {
  var $message_students_dialog = $("#message_students_dialog");
  var $sendButton = $message_students_dialog.find(".send_button");
  var currentSettings = {};
  var checkSendable = function() {
    disableSend(
      $message_students_dialog.find("#body").val().length == 0 ||
      $message_students_dialog.find(".student:not(.blank):visible").length == 0
    );
  }

  window.messageStudents = function(settings) {
    currentSettings = settings;
    $message_students_dialog.find(".message_types").empty();
    for(var idx=0, l=settings.options.length; idx < l; idx++) {
      var $option = $("<option/>");
      var option = settings.options[idx];
      $option.val(idx).text(option.text);
      $message_students_dialog.find(".message_types").append($option);
    }

    var title = settings.title,
        $li = $message_students_dialog.find("ul li.blank:first"),
        $ul = $message_students_dialog.find("ul"),
        students_hash = {};

    $message_students_dialog.find("ul li:not(.blank)").remove();

    for (var i = 0; i < settings.students.length; i++) {
      var $student = $li.clone(true).removeClass('blank');

      $student.find('.name').text(settings.students[i].name);
      $student.find('.score').text(settings.students[i].score);
      var remove_text = I18n.t("Remove %{student} from recipients", {student: settings.students[i].name});
      var $remove_button = $student.find('.remove-button');
      $remove_button.attr('title', remove_text).append($("<span class='screenreader-only'></span>").text(remove_text));
      $remove_button.click(function(event) {
        event.preventDefault();
        // hide the selected student
        var $s = $(this).closest('li');
        $s.hide('fast', checkSendable);
        // focus the next visible student, or the subject field if that was the last one in the list
        var $next = $s.nextAll(':visible:first');
        if ($next.length) {
          $('button', $next).focus();
        } else {
          $('#message_assignment_recipients #subject').focus();
        }
      });

      $student.data('id', settings.students[i].id);
      $student.user_data = settings.students[i];

      $ul.append($student.show());
      students_hash[settings.students[i].id] = $student;
    }

    $ul.show();

    $message_students_dialog.data('students_hash', students_hash),
    $message_students_dialog.find(".asset_title").text(title);
    $message_students_dialog.find(".out_of").showIf(settings.points_possible != null);
    $message_students_dialog.find(".send_button").text(I18n.t("send_message", "Send Message"));
    $message_students_dialog.find(".points_possible").text(I18n.n(settings.points_possible));
    $message_students_dialog.find("[name=context_code]").val(settings.context_code);

    $message_students_dialog.find("textarea").val("");
    $message_students_dialog.find("select")[0].selectedIndex = 0;
    $message_students_dialog.find("select").change();
    $message_students_dialog.dialog({
      width: 600,
      modal: true
    }).dialog('open').dialog('option', 'title', I18n.t("message_student", "Message Students for %{course_name}", {course_name: title}));
  };

  $(document).ready(function() {
    $message_students_dialog.find("button").click(function(e) {
      var btn = $(e.target);
      if (btn.hasClass("disabled")) {
        e.preventDefault();
        e.stopPropagation();
      }
    });
    $("#message_assignment_recipients").formSubmit({
      processData: function(data) {
        var ids = [];
        $(this).find(".student:visible").each(function() {
          ids.push($(this).data('id'));
        });
        if(ids.length == 0) { return false; }
        data['recipients'] = ids.join(",");
        return data;
      },
      beforeSubmit: function(data) {
        disableButtons(true)
        $(this).find(".send_button").text(I18n.t("Sending Message..."));
      },
      success: function(data) {
        $.flashMessage(I18n.t("Message sent!"));
        disableButtons(false);
        $(this).find(".send_button").text(I18n.t("Send Message"));
        $("#message_students_dialog").dialog('close');
      },
      error: function(data) {
        disableButtons(false);
        $(this).find(".send_button").text(I18n.t("Sending Message Failed, please try again"));
      }
    });

    var showStudentsMessageSentTo = function() {
      var idx = parseInt($message_students_dialog.find("select").val(), 10) || 0;
      var option = currentSettings.options[idx];
      var students_hash = $message_students_dialog.data('students_hash');
      var cutoff = numberHelper.parse($message_students_dialog.find('.cutoff_score').val());
      if (isNaN(cutoff)) {
        cutoff = null;
      }
      var student_ids = null;
      var students_list = [];
      for(var idx in students_hash) {
        students_list.push(students_hash[idx]);
      }
      if(students_hash) {
        if(option && option.callback) {
          student_ids = option.callback.call(window.messageStudents, cutoff, students_list);
        } else if(currentSettings.callback) {
          student_ids = currentSettings.callback.call(window.messageStudents, option.text, cutoff, students_list);
        }
      }
      student_ids = student_ids || [];

      if (currentSettings.subjectCallback) {
        $message_students_dialog.find("[name=subject]").val(currentSettings.subjectCallback(option.text, cutoff));
      }
      $message_students_dialog.find(".cutoff_holder").showIf(option.cutoff);

      $message_students_dialog.find(".student_list").toggleClass('show_score', !!(option.cutoff || option.score));
      disableButtons(student_ids.length === 0);

      var student_ids_hash = {};
      for(var idx in student_ids) {
        if (student_ids.hasOwnProperty(idx)) {
          student_ids_hash[parseInt(student_ids[idx], 10) || 0] = true;
        }
      }
      for(var idx in students_hash) {
        students_hash[idx].showIf(student_ids_hash[idx]);
      }
    };

    var focusOnCutoffScore = function() {
      var idx = parseInt($message_students_dialog.find("select").val(), 10) || 0;
      var option = currentSettings.options[idx];
      if (option.cutoff) {
        $(".cutoff_score").focus();
      }
    };

    var closeDialog = function() {
      $message_students_dialog.dialog('close');
    };

    $message_students_dialog.find(".cancel_button").click(closeDialog);
    $message_students_dialog.find("select").change(showStudentsMessageSentTo)
      .change(focusOnCutoffScore)
      .change(checkSendable);
    $message_students_dialog.find(".cutoff_score").bind('change blur keyup', showStudentsMessageSentTo)
      .bind('change blur keyup', checkSendable);
    $message_students_dialog.find("#body").bind('change blur keyup', checkSendable);
  });

  function disableButtons(disabled, buttons) {<commit_msg>remove focus when selecting submission criteria<commit_after>/**
 * Copyright (C) 2011 Instructure, Inc.
 *
 * This file is part of Canvas.
 *
 * Canvas is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, version 3 of the License.
 *
 * Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

define([
  'i18n!message_students',
  'jquery' /* $ */,
  'jsx/shared/helpers/numberHelper',
  'jquery.instructure_forms' /* formSubmit */,
  'jqueryui/dialog',
  'jquery.instructure_misc_plugins', /* showIf */
], function (I18n, $, numberHelper) {
  var $message_students_dialog = $("#message_students_dialog");
  var $sendButton = $message_students_dialog.find(".send_button");
  var currentSettings = {};
  var checkSendable = function() {
    disableSend(
      $message_students_dialog.find("#body").val().length == 0 ||
      $message_students_dialog.find(".student:not(.blank):visible").length == 0
    );
  }

  window.messageStudents = function(settings) {
    currentSettings = settings;
    $message_students_dialog.find(".message_types").empty();
    for(var idx=0, l=settings.options.length; idx < l; idx++) {
      var $option = $("<option/>");
      var option = settings.options[idx];
      $option.val(idx).text(option.text);
      $message_students_dialog.find(".message_types").append($option);
    }

    var title = settings.title,
        $li = $message_students_dialog.find("ul li.blank:first"),
        $ul = $message_students_dialog.find("ul"),
        students_hash = {};

    $message_students_dialog.find("ul li:not(.blank)").remove();

    for (var i = 0; i < settings.students.length; i++) {
      var $student = $li.clone(true).removeClass('blank');

      $student.find('.name').text(settings.students[i].name);
      $student.find('.score').text(settings.students[i].score);
      var remove_text = I18n.t("Remove %{student} from recipients", {student: settings.students[i].name});
      var $remove_button = $student.find('.remove-button');
      $remove_button.attr('title', remove_text).append($("<span class='screenreader-only'></span>").text(remove_text));
      $remove_button.click(function(event) {
        event.preventDefault();
        // hide the selected student
        var $s = $(this).closest('li');
        $s.hide('fast', checkSendable);
        // focus the next visible student, or the subject field if that was the last one in the list
        var $next = $s.nextAll(':visible:first');
        if ($next.length) {
          $('button', $next).focus();
        } else {
          $('#message_assignment_recipients #subject').focus();
        }
      });

      $student.data('id', settings.students[i].id);
      $student.user_data = settings.students[i];

      $ul.append($student.show());
      students_hash[settings.students[i].id] = $student;
    }

    $ul.show();

    $message_students_dialog.data('students_hash', students_hash),
    $message_students_dialog.find(".asset_title").text(title);
    $message_students_dialog.find(".out_of").showIf(settings.points_possible != null);
    $message_students_dialog.find(".send_button").text(I18n.t("send_message", "Send Message"));
    $message_students_dialog.find(".points_possible").text(I18n.n(settings.points_possible));
    $message_students_dialog.find("[name=context_code]").val(settings.context_code);

    $message_students_dialog.find("textarea").val("");
    $message_students_dialog.find("select")[0].selectedIndex = 0;
    $message_students_dialog.find("select").change();
    $message_students_dialog.dialog({
      width: 600,
      modal: true
    }).dialog('open').dialog('option', 'title', I18n.t("message_student", "Message Students for %{course_name}", {course_name: title}));
  };

  $(document).ready(function() {
    $message_students_dialog.find("button").click(function(e) {
      var btn = $(e.target);
      if (btn.hasClass("disabled")) {
        e.preventDefault();
        e.stopPropagation();
      }
    });
    $("#message_assignment_recipients").formSubmit({
      processData: function(data) {
        var ids = [];
        $(this).find(".student:visible").each(function() {
          ids.push($(this).data('id'));
        });
        if(ids.length == 0) { return false; }
        data['recipients'] = ids.join(",");
        return data;
      },
      beforeSubmit: function(data) {
        disableButtons(true)
        $(this).find(".send_button").text(I18n.t("Sending Message..."));
      },
      success: function(data) {
        $.flashMessage(I18n.t("Message sent!"));
        disableButtons(false);
        $(this).find(".send_button").text(I18n.t("Send Message"));
        $("#message_students_dialog").dialog('close');
      },
      error: function(data) {
        disableButtons(false);
        $(this).find(".send_button").text(I18n.t("Sending Message Failed, please try again"));
      }
    });

    var showStudentsMessageSentTo = function() {
      var idx = parseInt($message_students_dialog.find("select").val(), 10) || 0;
      var option = currentSettings.options[idx];
      var students_hash = $message_students_dialog.data('students_hash');
      var cutoff = numberHelper.parse($message_students_dialog.find('.cutoff_score').val());
      if (isNaN(cutoff)) {
        cutoff = null;
      }
      var student_ids = null;
      var students_list = [];
      for(var idx in students_hash) {
        students_list.push(students_hash[idx]);
      }
      if(students_hash) {
        if(option && option.callback) {
          student_ids = option.callback.call(window.messageStudents, cutoff, students_list);
        } else if(currentSettings.callback) {
          student_ids = currentSettings.callback.call(window.messageStudents, option.text, cutoff, students_list);
        }
      }
      student_ids = student_ids || [];

      if (currentSettings.subjectCallback) {
        $message_students_dialog.find("[name=subject]").val(currentSettings.subjectCallback(option.text, cutoff));
      }
      $message_students_dialog.find(".cutoff_holder").showIf(option.cutoff);

      $message_students_dialog.find(".student_list").toggleClass('show_score', !!(option.cutoff || option.score));
      disableButtons(student_ids.length === 0);

      var student_ids_hash = {};
      for(var idx in student_ids) {
        if (student_ids.hasOwnProperty(idx)) {
          student_ids_hash[parseInt(student_ids[idx], 10) || 0] = true;
        }
      }
      for(var idx in students_hash) {
        students_hash[idx].showIf(student_ids_hash[idx]);
      }
    };

    var closeDialog = function() {
      $message_students_dialog.dialog('close');
    };

    $message_students_dialog.find(".cancel_button").click(closeDialog);
    $message_students_dialog.find("select").change(showStudentsMessageSentTo).change(checkSendable);
    $message_students_dialog.find(".cutoff_score").bind('change blur keyup', showStudentsMessageSentTo)
      .bind('change blur keyup', checkSendable);
    $message_students_dialog.find("#body").bind('change blur keyup', checkSendable);
  });

  function disableButtons(disabled, buttons) {
{'fraction_non_alphanumeric': 0.15616223630069884, 'fraction_numerical': 0.005682189275684149, 'mean_word_length': 3.5008818342151677, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, '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': '6186814', 'n_tokens_mistral': 4656, 'n_tokens_neox': 4560, 'n_words': 829}