<commit_before>(function() {
  'use strict';
  // Polls controller
  angular
    .module('polls')
    .controller('PollInputController', PollInputController);

  PollInputController.$inject = [
    '$scope',
    '$state',
    '$window',
    'Authentication',
    'pollResolve',
    'PollsService',
    'PollsApi',
    'TagsService',
    '$bsModal',
    '$bsAside',
    '$bsPopover',
    'OptsService',
    'Socket',
    'CategorysService'
  ];

  function PollInputController(
    $scope,
    $state,
    $window,
    Authentication,
    poll,
    Polls,
    PollsApi,
    Tags,
    $bsModal,
    $bsAside,
    $bsPopover,
    Opts,
    Socket,
    Categorys
  ) {
    var vm = this;

    vm.authentication = Authentication;
    vm.poll = poll;
    vm.poll.close = vm.poll.close ? moment(vm.poll.close) : vm.poll.close;
    vm.poll.tags = [];
    vm.categorys = Categorys.query();
    vm.bk_poll = _.clone(poll);
    vm.error = null;
    vm.form = {};
    vm.opts = [];

    function init() {
      if (vm.poll._id) {
        loadOpts();
        loadTags();
        initSocket();
      }
    }

    init();
    // Init Socket
    function initSocket() {
      if (!Socket.socket) {
        Socket.connect();
      }
      Socket.emit('subscribe', {
        pollId: vm.poll._id,
        userId: vm.authentication.user._id
      });

      Socket.on('poll_delete', res => {
        alert('This poll has been deleted. Please back to list screen.');
        $state.go('poll.list');
      });
      Socket.on('opts_request', res => {
        console.log('opts_request');
        loadOpts();
      });
      $scope.$on('$destroy', function() {
        Socket.emit('unsubscribe', {
          pollId: vm.poll._id,
          userId: vm.authentication.user._id
        });
        Socket.removeListener('poll_delete');
        Socket.removeListener('opts_request');
      });
    }

    function loadOpts() {
      PollsApi.findOpts(vm.poll._id)
        .then(res => {
          vm.opts = res.data || [];
        })
        .catch(err => {
          alert('error' + err);
        });
    }

    function loadTags() {
      PollsApi.findTags(vm.poll._id)
        .then(res => {
          angular.forEach(res.data, (polltag, index) => {
            vm.poll.tags.push(polltag.tag);
          });
        })
        .catch(err => {
          alert('error' + err);
        });
    }

    function isCanUpdate() {
      const update = moment(vm.poll.updated);
      const now = moment(new Date());
      var duration = moment.duration(now.diff(update)).asHours();
      return duration >= 1;
    }
    // Function
    vm.remove = () => {
      if ($window.confirm('Are you sure you want to delete?')) {
        vm.poll.$remove(() => {
          Socket.emit('poll_delete', { pollId: vm.poll._id });
          $state.go('polls.list');
        });
      }
    };

    vm.save = () => {
      if (!vm.validateBody || !vm.validateCategory || !vm.validateTitle) {
        return alert('Vui lòng xem lại bạn chưa nhập đủ thông tin');
      }

      var isNew = !vm.poll._id ? true : false;
      vm.poll.opts = vm.opts;
      if (vm.poll._id) {
        if (!isCanUpdate()) {
          return alert(
            'Bạn không thể update poll trong vòng một giờ kể từ lần update trước.'
          );
        }
        vm.poll.$update(successCallback, errorCallback);
      } else {
        vm.poll.$save(successCallback, errorCallback);
      }

      function successCallback(res) {
        if (isNew) {
          Socket.emit('poll_create');
        } else {
          Socket.emit('poll_update', { pollId: vm.poll._id });
        }
        $state.go('polls.view', {
          pollId: res._id
        });
      }

      function errorCallback(res) {<commit_msg>Commited from HaNhatLenh at Wed, Jun 28, 2017 10:50:26 PM<commit_after>(function() {
  'use strict';
  // Polls controller
  angular
    .module('polls')
    .controller('PollInputController', PollInputController);

  PollInputController.$inject = [
    '$scope',
    '$state',
    '$window',
    'Authentication',
    'pollResolve',
    'PollsService',
    'PollsApi',
    'TagsService',
    '$bsModal',
    '$bsAside',
    '$bsPopover',
    'OptsService',
    'Socket',
    'CategorysService'
  ];

  function PollInputController(
    $scope,
    $state,
    $window,
    Authentication,
    poll,
    Polls,
    PollsApi,
    Tags,
    $bsModal,
    $bsAside,
    $bsPopover,
    Opts,
    Socket,
    Categorys
  ) {
    var vm = this;

    vm.authentication = Authentication;
    vm.poll = poll;
    vm.poll.close = vm.poll.close ? moment(vm.poll.close) : vm.poll.close;
    vm.poll.tags = [];
    vm.categorys = Categorys.query();
    vm.bk_poll = _.clone(poll);
    vm.error = null;
    vm.form = {};
    vm.opts = [];

    function init() {
      if (vm.poll._id) {
        loadOpts();
        loadTags();
        initSocket();
      }
    }

    init();
    // Init Socket
    function initSocket() {
      if (!Socket.socket) {
        Socket.connect();
      }
      Socket.emit('subscribe', {
        pollId: vm.poll._id,
        userId: vm.authentication.user._id
      });

      Socket.on('poll_delete', res => {
        alert('This poll has been deleted. Please back to list screen.');
        $state.go('poll.list');
      });
      Socket.on('opts_request', res => {
        console.log('opts_request');
        loadOpts();
      });
      $scope.$on('$destroy', function() {
        Socket.emit('unsubscribe', {
          pollId: vm.poll._id,
          userId: vm.authentication.user._id
        });
        Socket.removeListener('poll_delete');
        Socket.removeListener('opts_request');
      });
    }

    function loadOpts() {
      PollsApi.findOpts(vm.poll._id)
        .then(res => {
          vm.opts = res.data || [];
        })
        .catch(err => {
          alert('error' + err);
        });
    }

    function loadTags() {
      PollsApi.findTags(vm.poll._id)
        .then(res => {
          angular.forEach(res.data, (polltag, index) => {
            vm.poll.tags.push(polltag.tag);
          });
        })
        .catch(err => {
          alert('error' + err);
        });
    }

    function isCanUpdate() {
      const update = moment(vm.poll.updated);
      const now = moment(new Date());
      var duration = moment.duration(now.diff(update)).asHours();
      return duration >= 1;
    }
    // Function
    vm.remove = () => {
      if ($window.confirm('Are you sure you want to delete?')) {
        vm.poll.$remove(() => {
          Socket.emit('poll_delete', { pollId: vm.poll._id });
          $state.go('polls.list');
        });
      }
    };

    vm.save = () => {
      if (!vm.validateBody() || !vm.validateCategory() || !vm.validateTitle()) {
        return alert('Vui lòng xem lại bạn chưa nhập đủ thông tin');
      }

      var isNew = !vm.poll._id ? true : false;
      vm.poll.opts = vm.opts;
      if (vm.poll._id) {
        if (!isCanUpdate()) {
          return alert(
            'Bạn không thể update poll trong vòng một giờ kể từ lần update trước.'
          );
        }
        vm.poll.$update(successCallback, errorCallback);
      } else {
        vm.poll.$save(successCallback, errorCallback);
      }

      function successCallback(res) {
        if (isNew) {
          Socket.emit('poll_create');
        } else {
          Socket.emit('poll_update', { pollId: vm.poll._id });
        }
        $state.go('polls.view', {
          pollId: res._id
        });
      }

      function errorCallback(res) {
{'fraction_non_alphanumeric': 0.15699840340606705, 'fraction_numerical': 0.0018626929217668972, 'mean_word_length': 1.8603500761035008, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '15248', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2428, 'n_words': 445}