<commit_before>'use strict';

function cdManageDojoUsersCtrl($scope, $state, $q, cdDojoService, alertService, tableUtils, usSpinnerService, 
  cdBadgesService, $translate, cdUsersService, initUserTypes, currentUser, utilsService) {

  var dojoId = $state.params.id;
  var usersDojosLink = [];
  $scope.itemsPerPage = 10;
  $scope.userTypes = [];
  $scope.userPermissions = [];
  $scope.selectedUserPermissions = {};
  $scope.canUpdateUserPermissions = false;
  $scope.canRemoveUsers = false;
  $scope.userPermissionsModel = {};
  $scope.isDojoAdmin = false;
  $scope.badgeModel = {};
  $scope.awardBadgeButtonModel = {};
  $scope.manageDojoUsersPageTitle = $translate.instant('Manage Dojo Users');
  $scope.invite = {};
  $scope.queryModel = {};

  var user = currentUser.data;
  $scope.currentUser = user;
  //Updating user permissions and user types require the same permissions.
  //Remove users also requires the same permissions,
  //therefore we can check if the user can update user permissions & delete users by checking the result from the
  //canUpdateUser method.
  canUpdateUser(function (result) {
    $scope.canUpdateUserPermissions = result;
    $scope.canRemoveUsers = result;
  });

  cdBadgesService.listBadges(function (response) {
    $scope.badges = response.badges;
  });

  $scope.pageChanged = function () {
    $scope.loadPage(false);
  };

  $scope.loadPage = function(resetFlag) {
    var loadPageData = tableUtils.loadPage(resetFlag, $scope.itemsPerPage, $scope.pageNo, $scope.filterQuery, $scope.sort);
    $scope.pageNo = loadPageData.pageNo;
    $scope.myDojos = [];
    $scope.queryModel.sort = $scope.queryModel.sort ? $scope.queryModel.sort: {name: -1};

    cdDojoService.getUsersDojos({userId: user.id, dojoId: dojoId}, function (usersDojos) {
      var userDojo = usersDojos[0]
      user.userTypes = userDojo.userTypes;
      var inviteUserTypes = angular.copy(initUserTypes.data);
      if(_.contains(user.userTypes, 'mentor')) {
        inviteUserTypes = _.without(inviteUserTypes, _.findWhere(inviteUserTypes, {name: 'champion'}));
      } else if(_.contains(user.userTypes, 'parent-guardian')) {
        inviteUserTypes = _.chain(inviteUserTypes)
          .without(_.findWhere(inviteUserTypes, {name: 'champion'}))
          .without(_.findWhere(inviteUserTypes, {name: 'mentor'}))
          .value();
      } else if(_.contains(user.userTypes, 'attendee-o13')) {
        $scope.userTypes = _.chain(inviteUserTypes)
          .without(_.findWhere(inviteUserTypes, {name: 'champion'}))
          .without(_.findWhere(inviteUserTypes, {name: 'mentor'}))
          .without(_.findWhere(inviteUserTypes, {name: 'parent-guardian'}))
          .value();
      } else if(_.contains(user.userTypes, 'attendee-u13')) {
        inviteUserTypes = [];
      }
      $scope.userTypes = inviteUserTypes;

      cdDojoService.getUserPermissions(function (response) {
        $scope.userPermissions = response;
      });

      cdDojoService.getUsersDojos({dojoId:dojoId, limit$: $scope.itemsPerPage, skip$: loadPageData.skip}, function (response) {
        usersDojosLink = response;
        cdDojoService.loadDojoUsers({dojoId:dojoId, limit$: $scope.itemsPerPage, skip$: loadPageData.skip, sort$: $scope.queryModel.sort}, function (response) {
          _.each(response, function (user) {
            var thisUsersDojoLink = _.findWhere(usersDojosLink, {userId:user.id});
            user.types = thisUsersDojoLink.userTypes;
            user.frontEndTypes = _.map(thisUsersDojoLink.userTypes, function (userType) {
              var userTypeFound = _.find(initUserTypes.data, function (initUserType) {
                return userType === initUserType.name;
              });
              return $translate.instant(userTypeFound.title);
            });
            user.permissions = thisUsersDojoLink.userPermissions;
            user.isMentor = _.contains(user.types, 'mentor');
            user.isDojoOwner = (thisUsersDojoLink.owner === 1) ? true : false;
            user.backgroundChecked = thisUsersDojoLink.backgroundChecked;
            user.userDojoId = thisUsersDojoLink.id;
            $scope.selectedUserPermissions[user.id] = user.permissions;
            $scope.userPermissionsModel[user.id] = {};

            _.each(user.permissions, function (permission) {
              $scope.userPermissionsModel[user.id][permission.name] = true;
            });
          });
          $scope.users = response;
          //Query the loadDojoUsers service without the limit to get the total number of users.
          cdDojoService.loadDojoUsers({dojoId: dojoId}, function (response) {
            $scope.totalItems = response.length;
          });
        });
      });
    });
  };

  $scope.updateMentorBackgroundCheck = function (user) {
    var userDojo = {
      id: user.userDojoId,
      backgroundChecked: user.backgroundChecked
    };

    cdDojoService.saveUsersDojos(userDojo, null, function (err) {
      alertService.showError(JSON.stringify(err));
    });
  }

  $scope.updateUserPermissions = function(user, permission) {
    var hasPermission = false;
    canUpdateUser(function (result) {
      hasPermission = result;
      if (hasPermission) {
        var query = {dojoId:dojoId};
        delete permission.$$hashKey;
        var userDojoLink = _.findWhere(usersDojosLink, {userId:user.id});
        if($scope.userPermissionsModel[user.id][permission.name]) {
          //Add to user permissions
          if(!userDojoLink.userPermissions) userDojoLink.userPermissions = [];
          userDojoLink.userPermissions.push(permission);<commit_msg>updating loadDojoUsers to have no limit$<commit_after>'use strict';

function cdManageDojoUsersCtrl($scope, $state, $q, cdDojoService, alertService, tableUtils, usSpinnerService, 
  cdBadgesService, $translate, cdUsersService, initUserTypes, currentUser, utilsService) {

  var dojoId = $state.params.id;
  var usersDojosLink = [];
  $scope.itemsPerPage = 10;
  $scope.userTypes = [];
  $scope.userPermissions = [];
  $scope.selectedUserPermissions = {};
  $scope.canUpdateUserPermissions = false;
  $scope.canRemoveUsers = false;
  $scope.userPermissionsModel = {};
  $scope.isDojoAdmin = false;
  $scope.badgeModel = {};
  $scope.awardBadgeButtonModel = {};
  $scope.manageDojoUsersPageTitle = $translate.instant('Manage Dojo Users');
  $scope.invite = {};
  $scope.queryModel = {};

  var user = currentUser.data;
  $scope.currentUser = user;
  //Updating user permissions and user types require the same permissions.
  //Remove users also requires the same permissions,
  //therefore we can check if the user can update user permissions & delete users by checking the result from the
  //canUpdateUser method.
  canUpdateUser(function (result) {
    $scope.canUpdateUserPermissions = result;
    $scope.canRemoveUsers = result;
  });

  cdBadgesService.listBadges(function (response) {
    $scope.badges = response.badges;
  });

  $scope.pageChanged = function () {
    $scope.loadPage(false);
  };

  $scope.loadPage = function(resetFlag) {
    var loadPageData = tableUtils.loadPage(resetFlag, $scope.itemsPerPage, $scope.pageNo, $scope.filterQuery, $scope.sort);
    $scope.pageNo = loadPageData.pageNo;
    $scope.myDojos = [];
    $scope.queryModel.sort = $scope.queryModel.sort ? $scope.queryModel.sort: {name: -1};

    cdDojoService.getUsersDojos({userId: user.id, dojoId: dojoId}, function (usersDojos) {
      var userDojo = usersDojos[0]
      user.userTypes = userDojo.userTypes;
      var inviteUserTypes = angular.copy(initUserTypes.data);
      if(_.contains(user.userTypes, 'mentor')) {
        inviteUserTypes = _.without(inviteUserTypes, _.findWhere(inviteUserTypes, {name: 'champion'}));
      } else if(_.contains(user.userTypes, 'parent-guardian')) {
        inviteUserTypes = _.chain(inviteUserTypes)
          .without(_.findWhere(inviteUserTypes, {name: 'champion'}))
          .without(_.findWhere(inviteUserTypes, {name: 'mentor'}))
          .value();
      } else if(_.contains(user.userTypes, 'attendee-o13')) {
        $scope.userTypes = _.chain(inviteUserTypes)
          .without(_.findWhere(inviteUserTypes, {name: 'champion'}))
          .without(_.findWhere(inviteUserTypes, {name: 'mentor'}))
          .without(_.findWhere(inviteUserTypes, {name: 'parent-guardian'}))
          .value();
      } else if(_.contains(user.userTypes, 'attendee-u13')) {
        inviteUserTypes = [];
      }
      $scope.userTypes = inviteUserTypes;

      cdDojoService.getUserPermissions(function (response) {
        $scope.userPermissions = response;
      });

      cdDojoService.getUsersDojos({dojoId:dojoId, limit$: $scope.itemsPerPage, skip$: loadPageData.skip}, function (response) {
        usersDojosLink = response;
        cdDojoService.loadDojoUsers({dojoId:dojoId, limit$: $scope.itemsPerPage, skip$: loadPageData.skip, sort$: $scope.queryModel.sort}, function (response) {
          _.each(response, function (user) {
            var thisUsersDojoLink = _.findWhere(usersDojosLink, {userId:user.id});
            user.types = thisUsersDojoLink.userTypes;
            user.frontEndTypes = _.map(thisUsersDojoLink.userTypes, function (userType) {
              var userTypeFound = _.find(initUserTypes.data, function (initUserType) {
                return userType === initUserType.name;
              });
              return $translate.instant(userTypeFound.title);
            });
            user.permissions = thisUsersDojoLink.userPermissions;
            user.isMentor = _.contains(user.types, 'mentor');
            user.isDojoOwner = (thisUsersDojoLink.owner === 1) ? true : false;
            user.backgroundChecked = thisUsersDojoLink.backgroundChecked;
            user.userDojoId = thisUsersDojoLink.id;
            $scope.selectedUserPermissions[user.id] = user.permissions;
            $scope.userPermissionsModel[user.id] = {};

            _.each(user.permissions, function (permission) {
              $scope.userPermissionsModel[user.id][permission.name] = true;
            });
          });
          $scope.users = response;
          //Query the loadDojoUsers service without the limit to get the total number of users.
          cdDojoService.loadDojoUsers({dojoId: dojoId, limit$: 'NULL'}, function (response) {
            $scope.totalItems = response.length;
          });
        });
      });
    });
  };

  $scope.updateMentorBackgroundCheck = function (user) {
    var userDojo = {
      id: user.userDojoId,
      backgroundChecked: user.backgroundChecked
    };

    cdDojoService.saveUsersDojos(userDojo, null, function (err) {
      alertService.showError(JSON.stringify(err));
    });
  }

  $scope.updateUserPermissions = function(user, permission) {
    var hasPermission = false;
    canUpdateUser(function (result) {
      hasPermission = result;
      if (hasPermission) {
        var query = {dojoId:dojoId};
        delete permission.$$hashKey;
        var userDojoLink = _.findWhere(usersDojosLink, {userId:user.id});
        if($scope.userPermissionsModel[user.id][permission.name]) {
          //Add to user permissions
          if(!userDojoLink.userPermissions) userDojoLink.userPermissions = [];
          userDojoLink.userPermissions.push(permission);
{'fraction_non_alphanumeric': 0.12654623120049835, 'fraction_numerical': 0.0016018510278544096, 'mean_word_length': 3.2216378662659655, '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': '254013', 'n_tokens_mistral': 3370, 'n_tokens_neox': 3375, 'n_words': 606}