<commit_before>angular.module('cityquest.controllers', ['cityquest.services', 'ngCordova'])

    /*
    Controller that either forwards us to a load_new controller, or load_existing
    */
    .controller ('CityquestSplashCtrl',
    function ($scope, $rootScope) {
        if (localStorage.getItem ('quest') !== null) {
            /* Load existing */
            $scope.quest = JSON.parse (localStorage.getItem ('quest'));
            $rootScope.quest = $scope.quest;
            window.location = "#/landing";
        } else {
            /* Load new */
            window.location = "#/load";
        }
    })

    /*
    Controller that fetches the quest data
    */
    .controller ('CityquestFetchCtrl',
    function ($scope, quest, progress, $cordovaFileTransfer, $ionicPlatform, $timeout, $cordovaFile) {
        $scope.images_downloaded = false;
        $scope.errormsg = null;
        /* Image downloader */
        $scope.downloadImages = function () {
            /* Create directory */
            $scope.quest.image_dir = cordova.file.dataDirectory + 'cityquest/' + $scope.quest.details.id.toString () + '/';
            $cordovaFile.checkDir (cordova.file.dataDirectory + 'cityquest', $scope.quest.details.id.toString ())
                .then (function (success) {

            }, function (error) {
                $cordovaFile.createDir (cordova.file.dataDirectory + 'cityquest', $scope.quest.details.id.toString (), false)
                    .then (function (success) {
                }, function (error) {
                    $scope.errormsg = JSON.stringify (error);
                })
            });
            /* Cache header image */
            var target = $scope.quest.image_dir + $scope.quest.details.id.toString ();
            var source = $scope.quest.details.remote_imageFile;
            $cordovaFileTransfer.download (source, target, {}, true)
                .then (function (result) {
            }, function (error) {
                $scope.errormsg = JSON.stringify (error);
            }, function (progressed) {
                $timeout (function () {
                    $scope.downloadProgress = (progressed.loaded / progressed.total) * 100;
                })
            });
            /* Cache items */
            for (var i = 0; i < $scope.quest.details.items.length; i++) {
                target = $scope.quest.image_dir + $scope.quest.details.id.toString () + '.' + $scope.quest.details.items[i].itemid;
                source = $scope.quest.details.items[i].remote_image;
                $cordovaFileTransfer.download (source, target, {}, true)
                    .then (function (result) {
                }, function (error) {
                    $scope.errormsg = JSON.stringify (error);
                }, function (progressed) {
                    $timeout (function () {
                        $scope.downloadProgress = (progressed.loaded / progressed.total) * 100;
                    })
                });
                /* Cache hints */
                if (typeof ($scope.quest.details.items[i].hints) != 'undefined') {
                    for (var j = 0; j < $scope.quest.details.items[i].hints.length; j++) {
                        target = $scope.quest.image_dir + $scope.quest.details.id.toString () + '.' + $scope.quest.details.items[i].hints[j].hint_id;
                        source = $scope.quest.details.items[i].hints[j].remote_image;
                        $cordovaFileTransfer.download (source, target, {}, true)
                            .then (function (result) {
                        }, function (error) {
                            $scope.errormsg = JSON.stringify (error);
                        }, function (progressed) {
                            $scope.hdownloadProgress = (progressed.loaded / progressed.total) * 100;
                        })
                    }
                }
            }
            /* Cache map */
            target = $scope.quest.image_dir + "map" + $scope.quest.details.id.toString ();
            console.log (target);
            source = 'http://cityquest.be/' + $scope.quest.details.map.url;
            $cordovaFileTransfer.download (source, target, {}, true)
                .then (function (result) {
            }, function (error) {
                $scope.errormsg = JSON.stringify (error);
            }, function (progressed) {
                $timeout (function () {
                    $scope.downloadProgress = (progressed.loaded / progressed.total) * 100;
                })
            });
            console.log ($scope.errormsg);
        };

        /* Check if the items array is already parsed */
        if (typeof (quest.details.items) == 'string') {
            quest.details.items = JSON.parse (quest.details.items);
        }
        $scope.quest = quest;
        $scope.progress = progress;
        /* Download the images for the Quest */
        $ionicPlatform.ready (function () {
            $scope.downloadImages ();
            $scope.images_downloaded = true;
        });
        /* If images_downloaded is true, redirect */
        $scope.$watch ('images_downloaded', function (newValue) {
            if (newValue == true) {
                //$scope.ff = cordova.file.dataDirectory + $scope.quest.details.items[0].itemid;
                localStorage.setItem ('quest', JSON.stringify ($scope.quest));
                if (typeof ($scope.progress) != 'undefined' && $scope.progress != null) {
                    localStorage.setItem ('progress', JSON.stringify ($scope.progress));
                }
            }
            window.location = "#/landing";
        });
    })

    /*
     Controller that allows the user to enter a key in the form and redirects to the fetching controller, responsible for downloading the quest
     */
    .controller('CityquestLoadCtrl', function ($scope) {
        $scope.loading = false;
        $scope.load = function () {
        };
        $scope.loadQuest = function (key){
            window.alert ('Loading quest ...');
            if (key == "" || key == null) {
                alert ("Error: your key must not be empty");
                $scope.loading = false;
            } else {
                $scope.loading = true;
                window.location = '#/fetch/' + key;
            }<commit_msg>Check whether the quest exists<commit_after>angular.module('cityquest.controllers', ['cityquest.services', 'ngCordova'])

    /*
    Controller that either forwards us to a load_new controller, or load_existing
    */
    .controller ('CityquestSplashCtrl',
    function ($scope, $rootScope) {
        if (localStorage.getItem ('quest') !== null) {
            /* Load existing */
            $scope.quest = JSON.parse (localStorage.getItem ('quest'));
            $rootScope.quest = $scope.quest;
            window.location = "#/landing";
        } else {
            /* Load new */
            window.location = "#/load";
        }
    })

    /*
    Controller that fetches the quest data
    */
    .controller ('CityquestFetchCtrl',
    function ($scope, quest, progress, $cordovaFileTransfer, $ionicPlatform, $timeout, $cordovaFile) {
        $scope.images_downloaded = false;
        $scope.errormsg = null;
        /* Image downloader */
        $scope.downloadImages = function () {
            /* Create directory */
            $scope.quest.image_dir = cordova.file.dataDirectory + 'cityquest/' + $scope.quest.details.id.toString () + '/';
            $cordovaFile.checkDir (cordova.file.dataDirectory + 'cityquest', $scope.quest.details.id.toString ())
                .then (function (success) {

            }, function (error) {
                $cordovaFile.createDir (cordova.file.dataDirectory + 'cityquest', $scope.quest.details.id.toString (), false)
                    .then (function (success) {
                }, function (error) {
                    $scope.errormsg = JSON.stringify (error);
                })
            });
            /* Cache header image */
            var target = $scope.quest.image_dir + $scope.quest.details.id.toString ();
            var source = $scope.quest.details.remote_imageFile;
            $cordovaFileTransfer.download (source, target, {}, true)
                .then (function (result) {
            }, function (error) {
                $scope.errormsg = JSON.stringify (error);
            }, function (progressed) {
                $timeout (function () {
                    $scope.downloadProgress = (progressed.loaded / progressed.total) * 100;
                })
            });
            /* Cache items */
            for (var i = 0; i < $scope.quest.details.items.length; i++) {
                target = $scope.quest.image_dir + $scope.quest.details.id.toString () + '.' + $scope.quest.details.items[i].itemid;
                source = $scope.quest.details.items[i].remote_image;
                $cordovaFileTransfer.download (source, target, {}, true)
                    .then (function (result) {
                }, function (error) {
                    $scope.errormsg = JSON.stringify (error);
                }, function (progressed) {
                    $timeout (function () {
                        $scope.downloadProgress = (progressed.loaded / progressed.total) * 100;
                    })
                });
                /* Cache hints */
                if (typeof ($scope.quest.details.items[i].hints) != 'undefined') {
                    for (var j = 0; j < $scope.quest.details.items[i].hints.length; j++) {
                        target = $scope.quest.image_dir + $scope.quest.details.id.toString () + '.' + $scope.quest.details.items[i].hints[j].hint_id;
                        source = $scope.quest.details.items[i].hints[j].remote_image;
                        $cordovaFileTransfer.download (source, target, {}, true)
                            .then (function (result) {
                        }, function (error) {
                            $scope.errormsg = JSON.stringify (error);
                        }, function (progressed) {
                            $scope.hdownloadProgress = (progressed.loaded / progressed.total) * 100;
                        })
                    }
                }
            }
            /* Cache map */
            target = $scope.quest.image_dir + "map" + $scope.quest.details.id.toString ();
            console.log (target);
            source = 'http://cityquest.be/' + $scope.quest.details.map.url;
            $cordovaFileTransfer.download (source, target, {}, true)
                .then (function (result) {
            }, function (error) {
                $scope.errormsg = JSON.stringify (error);
            }, function (progressed) {
                $timeout (function () {
                    $scope.downloadProgress = (progressed.loaded / progressed.total) * 100;
                })
            });
            console.log ($scope.errormsg);
        };

        /* Check if the items array is already parsed */
        if (typeof (quest.details.items) == 'string') {
            quest.details.items = JSON.parse (quest.details.items);
        }
        $scope.quest = quest;
        $scope.progress = progress;
        /* Download the images for the Quest */
        $ionicPlatform.ready (function () {
            $scope.downloadImages ();
            $scope.images_downloaded = true;
        });
        /* If images_downloaded is true, redirect */
        $scope.$watch ('images_downloaded', function (newValue) {
            if (newValue == true) {
                //$scope.ff = cordova.file.dataDirectory + $scope.quest.details.items[0].itemid;
                localStorage.setItem ('quest', JSON.stringify ($scope.quest));
                if (typeof ($scope.progress) != 'undefined' && $scope.progress != null) {
                    localStorage.setItem ('progress', JSON.stringify ($scope.progress));
                }
            }
            window.location = "#/landing";
        });
    })

    /*
     Controller that allows the user to enter a key in the form and redirects to the fetching controller, responsible for downloading the quest
     */
    .controller('CityquestLoadCtrl', function ($scope, $http) {
        $scope.loading = false;
        $scope.load = function () {
        };
        $scope.loadQuest = function (key){
            window.alert ('Loading quest ...');
            if (key == "" || key == null) {
                alert ("Error: your key must not be empty");
                $scope.loading = false;
            } else {
                /* GET to see if item exists */
                $http.get ('http://cityquest.be/en/api/key/' + key).
                    success (function (data, status) {
                    // Continue
                    $scope.loading = true;
                    window.location = '#/fetch/' + key;
                }).
                    error (function (data, status) {
                    // Error
                    alert ("Error: this quest does not seem to exist. Perhaps your key is wrong or the quest is not yet published.");
                    $scope.loading = false;
                });
            }
{'fraction_non_alphanumeric': 0.132786387116378, 'fraction_numerical': 0.0022789425706472195, 'mean_word_length': 1.5647769335671147, 'pattern_counts': {'":': 0, '<': 7, '<?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': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5178856', 'n_tokens_mistral': 3450, 'n_tokens_neox': 3287, 'n_words': 731}