<commit_before>var path = require('path');
var viewPath = path.join( __dirname + "/");
var fs = require('fs');
var multer = require('multer');

var _ = require('lodash');

var preferencesDB = require( __components + 'Preferences/preferenceDB.js');
var profileDB = require( __components + 'StudentProfile/studentProfileDB.js');
var defaultTool = require( __components + 'Preferences/setupDefaultToolType.js');

var limits = { fileSize: 51200 };
var fileFilter = function(req, file, cb) {
    var filetype = /png/;
    var mimetype = filetype.test(file.mimetype);
    var extension = filetype.test(path.extname(file.originalname).toLowerCase());
    if (mimetype && extension) {
        return cb(null, true);
    } else {
        cb("Error: Only " + filetype + "files are allowed.", false);
    }
};
var storage = multer.diskStorage({
    destination: function(req, file, cb) {
        var userId = req.user.id;
        var basepath = './app/shared/img/users/';
        cb(null, basepath + req.user.id + '/');
    },
    filename: function(req, file, cb) {
        cb(null, 'avatar.png');
    },
});
var upload = multer({
    storage: storage,
    limits: limits,
    fileFilter: fileFilter
});

module.exports = function( app ) {
    app.route("/preferences")

    .get( function(req,res,next) {
        res.render( viewPath + "preferences", { title: 'Preferences', message:'ok'})
    });

    app.get('/preferences/data.json', function(req,res) {
        var preferencesFile = './config/preferencesList.json';
        fs.readFile( preferencesFile, 'utf-8', function( err, data ) {
            if( err ) {
                //Unable to get support tools file, larger problem here.
                Logger.error(err);
                res.end();
            }
            else {
                var jsonObj = JSON.parse(data);
                var preferences = jsonObj['preferences'];

                preferencesDB.getStudentPreferencesByToolType(req.user.id, "Option", function( err, preferencesDB ) {
                    if(!err)
                        updateJsonWithDbValues(preferencesDB, preferences.options );

                    profileDB.getStudentProfile(req.user.id, function( perr, profile ) {
                        setupProfile(preferences.options, profile);
                        res.json(preferences);
                    });
                });
            }
        });
    });

    app.post('/preferences', function(req, res, next) {
        console.log("RECEIVED", req.body);
        var userId = req.user.id;
        var pref = req.body["pref-toolSelect"];
        var studentName = req.body['student-name'];
        var studentBio = req.body['student-bio'];

        console.log(pref);

        if (req.file)
            upload.single('student-avatar');

        /*
        upload(req, res, function(err) {
            if (err) {
                console.log("Error uploading file: " + err);
                return res.end("Error uploading file: ", err);
            }
            console.log('uploaded new avatar');
        });
        */

        preferencesDB.setStudentPreferences(userId, "Option", "pref-toolSelect", pref , function(err,result){
            if(!err)
                defaultTool.setupDefaultTool(req, pref);
            else
                console.log("ERROR SETTING DEFAULT TOOLS");

            profileDB.setStudentProfile(userId, studentName, studentBio, function(err, presult) {
                if(err)
                    console.log("ERROR SETTING STUDENT PROFILE");

                 //TODO pop or message
                res.location( "/");
                res.redirect( "/" );
            });
        });
    });

    /**
     * Update default preferences with DB values for specific user.
     * @param  {[type]} preferencesDB      [description]
     * @param  {[type]} preferencesOptions [description]
     * @return {[type]}                    [description]
     */
    function updateJsonWithDbValues( preferencesDB, preferencesOptions) {
        var prefPrefix = "pref-"
        for( var i = 0; i < preferencesDB.length; i++ ) {

            var optionName = preferencesDB[i].toolName;

            if( _.startsWith(optionName,prefPrefix) ) {
                var r = _.find(preferencesOptions,_.matchesProperty('name',optionName));
                 if( r ){
                    if(r.type == "checkbox")
                        r['prefValue'] = preferencesDB[i].toolValue == "on";
                    else if(r.type == "select")
                        r['prefValue'] = preferencesDB[i].toolValue;
                }
            }
        }
    }

    /**
     * Setup student profile with basic information name, bio, img
     * @param  {[type]} preferenceOptions [description]
     * @param  {[type]} profile           [description]
     * @return {[type]}                   [description]
     */
    function setupProfile( preferenceOptions, profile ) {
        if( profile && profile.length > 0) {
            var prefix = "student-"
            var keys = ['name', 'avatarFileName','bio'];
            for( var i = 0; i < keys.length; i++ ){
                var r = _.find(preferenceOptions,_.matchesProperty('name',prefix+keys[i]));
                if( r && r.type == "text") {
                    r['prefValue'] =  profile[0][keys[i]] ? profile[0][keys[i]] : "";
                }
            }
        }
    }
}
<commit_msg>Fixing path information for student profile/preferences<commit_after>var path = require('path');
var viewPath = path.join( __dirname + "/");
var fs = require('fs');
var multer = require('multer');

var _ = require('lodash');

var preferencesDB = require( __components + 'Preferences/preferenceDB.js');
var profileDB = require( __components + 'StudentProfile/studentProfileDB.js');
var defaultTool = require( __components + 'Preferences/setupDefaultToolType.js');

var limits = { fileSize: 51200 };
var fileFilter = function(req, file, cb) {
    var filetype = /png/;
    var mimetype = filetype.test(file.mimetype);
    var extension = filetype.test(path.extname(file.originalname).toLowerCase());
    console.log(extension);
    if (mimetype && extension) {
        return cb(null, true);
    } else {
        cb("Error: Only " + filetype + "files are allowed.", false);
    }
};
var storage = multer.diskStorage({
    destination: function(req, file, cb) {
        var userId = req.user.id;
        var basepath = 'app/shared/img/user/';
        var submissionFolder = path.join(basepath, req.user.id.toString());
        cb(null, submissionFolder);
    },
    filename: function(req, file, cb) {
        console.log("FILE:",file)
        cb(null, 'avatar.png');
    },
});
var upload = multer({
    storage: storage,
    limits: limits,
    fileFilter: fileFilter
});

//var upload = multer({ dest: 'app/shared/img/users/' })

module.exports = function( app ) {
    app.route("/preferences")

    .get( function(req,res,next) {
        res.render( viewPath + "preferences", { title: 'Preferences', message:'ok'})
    });

    app.get('/preferences/data.json', function(req,res) {
        var preferencesFile = './config/preferencesList.json';
        fs.readFile( preferencesFile, 'utf-8', function( err, data ) {
            if( err ) {
                //Unable to get support tools file, larger problem here.
                Logger.error(err);
                res.end();
            }
            else {
                var jsonObj = JSON.parse(data);
                var preferences = jsonObj['preferences'];

                preferencesDB.getStudentPreferencesByToolType(req.user.id, "Option", function( err, preferencesDB ) {
                    if(!err)
                        updateJsonWithDbValues(preferencesDB, preferences.options );

                    profileDB.getStudentProfile(req.user.id, function( perr, profile ) {
                        setupProfile(preferences.options, profile);
                        res.json(preferences);
                    });
                });
            }
        });
    });

    app.post('/preferences', upload.single('student-avatar'), function(req, res, next) {
        console.log("RECEIVED", req.body);
        var userId = req.user.id;
        var pref = req.body["pref-toolSelect"];
        var studentName = req.body['student-name'];
        var studentBio = req.body['student-bio'];

        if( studentName && studentBio && pref) {

            preferencesDB.setStudentPreferences(userId, "Option", "pref-toolSelect", pref , function(err,result){
                if(!err)
                    defaultTool.setupDefaultTool(req, pref);
                else
                    console.log("ERROR SETTING DEFAULT TOOLS");

                profileDB.setStudentProfile(userId, studentName, studentBio, function(err, presult) {
                    if(err)
                        console.log("ERROR SETTING STUDENT PROFILE");

                     //TODO pop or message
                    res.location( "/");
                    res.redirect( "/" );
                });
            });
        }
        else {
            console.log("ERROR ERROR");
            res.end();
        }

    });

    /**
     * Update default preferences with DB values for specific user.
     * @param  {[type]} preferencesDB      [description]
     * @param  {[type]} preferencesOptions [description]
     * @return {[type]}                    [description]
     */
    function updateJsonWithDbValues( preferencesDB, preferencesOptions) {
        var prefPrefix = "pref-"
        for( var i = 0; i < preferencesDB.length; i++ ) {

            var optionName = preferencesDB[i].toolName;

            if( _.startsWith(optionName,prefPrefix) ) {
                var r = _.find(preferencesOptions,_.matchesProperty('name',optionName));
                 if( r ){
                    if(r.type == "checkbox")
                        r['prefValue'] = preferencesDB[i].toolValue == "on";
                    else if(r.type == "select")
                        r['prefValue'] = preferencesDB[i].toolValue;
                }
            }
        }
    }

    /**
     * Setup student profile with basic information name, bio, img
     * @param  {[type]} preferenceOptions [description]
     * @param  {[type]} profile           [description]
     * @return {[type]}                   [description]
     */
    function setupProfile( preferenceOptions, profile ) {
        if( profile && profile.length > 0) {
            var prefix = "student-"
            var keys = ['name', 'avatarFileName','bio'];
            for( var i = 0; i < keys.length; i++ ){
                var r = _.find(preferenceOptions,_.matchesProperty('name',prefix+keys[i]));
                if( r && r.type == "text") {
                    r['prefValue'] =  profile[0][keys[i]] ? profile[0][keys[i]] : "";
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.1371554982025993, 'fraction_numerical': 0.0020278366669739148, 'mean_word_length': 1.9033984479529034, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '2554037', 'n_tokens_mistral': 3008, 'n_tokens_neox': 2898, 'n_words': 653}