<commit_before>'use strict';

angular.module('SpTransp.Requests')
    .controller('RequestsCtrl', RequestsCtrl);

function RequestsCtrl($scope, $location, $routeParams, RequestsModel) {
    var requests = this;

    const TYPE_BEING_VALIDATED = 'beingValidated';
    const TYPE_GRANTED_OR_REFUSED = 'grantedOrRefused';

    $scope.itemsPerPageOptions = [10, 20, 40, 60];

    $scope.totalNbOfRequests = 0;
    $scope.totalNbOfPages = 0;
    $scope.nbOfElementsPerPage = $scope.itemsPerPageOptions[1];
    // Sur la dernier page le nombre d'elements affiches n'est pas forcement le
    // meme que le nombre d'elements par page
    $scope.nbOfElementsDisplayed = 0;
    $scope.currentPageIndex = 0;
    $scope.paginationLabel = "";

    requests.searchType = $routeParams['searchType'];

    requests.requests = [];
    $scope.title = "";


    $scope.getRange = function(size) {
        var array = new Array();
        for (var i=0; i<size; i++) {
            array.push(i+1);
        }
        return array;
    };

    requests.getRequestsBeingValidated = function() {
        $scope.title = "Requests being validated";
        RequestsModel.getRequestsBeingValidated($scope.nbOfElementsPerPage, $scope.currentPageIndex)
            .then(function(result) {
                requests.requests = requests.analyzePaginatedResult(result);
        });
    };

    requests.getRequestsGrantedOrRefused = function() {
        $scope.title = "Requests granted or refused";
        RequestsModel.getRequestsGrantedOrRefused($scope.nbOfElementsPerPage, $scope.currentPageIndex)
            .then(function(result) {
                requests.requests = requests.analyzePaginatedResult(result);
                requests.requests.sort(function(req1, req2) {
                        return req2.updateDateForComparison - req1.updateDateForComparison;
                    });
        });
    };

    requests.analyzePaginatedResult = function(result) {
        var listOfRequests = result[0];
        $scope.totalNbOfRequests = result[1];
        $scope.totalNbOfPages = result[3];
        $scope.nbOfElementsPerPage = result[4];
        $scope.currentPageIndex = result[5];
        $scope.nbOfElementsDisplayed = result[8];

        var indexOfFirstRequestDisplayed = $scope.currentPageIndex * $scope.nbOfElementsPerPage;
        var indexOfLastRequestDisplayed = indexOfFirstRequestDisplayed + $scope.nbOfElementsDisplayed;

        $scope.paginationLabel = (indexOfFirstRequestDisplayed + 1) + " - "
            + indexOfLastRequestDisplayed + " of " + $scope.totalNbOfRequests;

        return listOfRequests;
    };

    requests.loadRequests = function() {
        if (requests.searchType == TYPE_BEING_VALIDATED) requests.getRequestsBeingValidated();
        else if (requests.searchType == TYPE_GRANTED_OR_REFUSED) requests.getRequestsGrantedOrRefused();
        else $location.path('requests/' + TYPE_BEING_VALIDATED);
    };

    $scope.changePage = function(pageIndex) {
        $scope.currentPageIndex = pageIndex;
        requests.loadRequests();
    }

    $scope.changeItemsPerPage = function(itemPerPage) {
        $scope.currentPageIndex = 0;
        $scope.nbOfElementsPerPage = itemPerPage;
        requests.loadRequests();
    };

    $scope.selectRequest = function(requestReference) {
        $location.path('request/' + requestReference);
    };<commit_msg>Fixing bug with requests pagination<commit_after>'use strict';

angular.module('SpTransp.Requests')
    .controller('RequestsCtrl', RequestsCtrl);

function RequestsCtrl($scope, $location, $routeParams, RequestsModel) {
    var requests = this;

    const TYPE_BEING_VALIDATED = 'beingValidated';
    const TYPE_GRANTED_OR_REFUSED = 'grantedOrRefused';

    $scope.itemsPerPageOptions = [10, 20, 40, 60];

    $scope.totalNbOfRequests = 0;
    $scope.totalNbOfPages = 0;
    $scope.nbOfElementsPerPage = $scope.itemsPerPageOptions[1];
    // Sur la dernier page le nombre d'elements affiches n'est pas forcement le
    // meme que le nombre d'elements par page
    $scope.nbOfElementsDisplayed = 0;
    $scope.currentPageIndex = 0;
    $scope.paginationLabel = "";

    requests.searchType = $routeParams['searchType'];

    requests.requests = [];
    $scope.title = "";


    $scope.getRange = function(size) {
        var array = new Array();
        for (var i=0; i<size; i++) {
            array.push(i+1);
        }
        return array;
    };

    requests.getRequestsBeingValidated = function() {
        $scope.title = "Requests being validated";
        RequestsModel.getRequestsBeingValidated($scope.nbOfElementsPerPage, $scope.currentPageIndex)
            .then(function(result) {
                requests.requests = requests.analyzePaginatedResult(result);
        });
    };

    requests.getRequestsGrantedOrRefused = function() {
        $scope.title = "Requests granted or refused";
        RequestsModel.getRequestsGrantedOrRefused($scope.nbOfElementsPerPage, $scope.currentPageIndex)
            .then(function(result) {
                requests.requests = requests.analyzePaginatedResult(result);
                requests.requests.sort(function(req1, req2) {
                        return req2.updateDateForComparison - req1.updateDateForComparison;
                    });
        });
    };

    requests.analyzePaginatedResult = function(result) {
        var listOfRequests = result[0];
        $scope.totalNbOfRequests = result[1];
        $scope.totalNbOfPages = result[2];
        $scope.nbOfElementsPerPage = result[4];
        $scope.currentPageIndex = result[5];
        $scope.nbOfElementsDisplayed = result[8];

        //$log.debug(result);
        //$log.debug($scope.totalNbOfRequests);
        //$log.debug($scope.totalNbOfPages);
        //$log.debug($scope.nbOfElementsPerPage);
        //$log.debug($scope.currentPageIndex);
        //$log.debug($scope.nbOfElementsDisplayed);

        var indexOfFirstRequestDisplayed = $scope.currentPageIndex * $scope.nbOfElementsPerPage;
        var indexOfLastRequestDisplayed = indexOfFirstRequestDisplayed + $scope.nbOfElementsDisplayed;

        $scope.paginationLabel = (indexOfFirstRequestDisplayed + 1) + " - "
            + indexOfLastRequestDisplayed + " of " + $scope.totalNbOfRequests;

        return listOfRequests;
    };

    requests.loadRequests = function() {
        if (requests.searchType == TYPE_BEING_VALIDATED) requests.getRequestsBeingValidated();
        else if (requests.searchType == TYPE_GRANTED_OR_REFUSED) requests.getRequestsGrantedOrRefused();
        else $location.path('requests/' + TYPE_BEING_VALIDATED);
    };

    $scope.changePage = function(pageIndex) {
        $scope.currentPageIndex = pageIndex;
        requests.loadRequests();
    }

    $scope.changeItemsPerPage = function(itemPerPage) {
        $scope.currentPageIndex = 0;
        $scope.nbOfElementsPerPage = itemPerPage;
        requests.loadRequests();
    };

    $scope.selectRequest = function(requestReference) {
        $location.path('request/' + requestReference);
    };
{'fraction_non_alphanumeric': 0.11352966282543747, 'fraction_numerical': 0.0076824583866837385, 'mean_word_length': 3.014848657909766, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5196650', 'n_tokens_mistral': 2076, 'n_tokens_neox': 2033, 'n_words': 332}