<commit_before>var mongoose = require('mongoose')
    , User = mongoose.model('User')
    , Account = mongoose.model('Account')
    , Assignment = mongoose.model('Assignment')
    , treemill = require('treemill') 

//API route for ajax requests to change the visual 
//representation of the data they uploaded.
exports.updateVistype = function (req, res, next) {
    
    console.log('update vistype for ' + req.user.email + 
        ' '+req.params.assignmentID)

    Assignment
        .findOne({
            email:req.user.email,
            assignmentID: req.params.assignmentID
        })
        .exec(function (err, assignmentResult) {
            if (err) return next(err)
            if (!assignmentResult) 
                return next("could not find assignment")
            //validate the vis type is implemented..
            vistypes = ["nodelink", "tree", "queue"]
            if (vistypes.indexOf(req.params.value) == -1) 
                return next("specified vistype is not implemented")
            assignmentResult.vistype=req.params.value 
            assignmentResult.save()
            res.send("OK")
        })
}

//API route to toggle the visibility of an assignment 
//between private and public.
exports.updateVisibility = function (req, res, next) {
    Assignment
        .findOne({
            email:req.user.email,
            assignmentID: req.params.assignmentID    
        })
        .exec(function (err, assignmentResult) {
            if (err) return next(err)
            if (!assignmentResult) 
                return next("could not find assignment")
            assignmentResult.shared=req.params.value 
            assignmentResult.save()
            res.send("OK")
        })
}

//API route for uploading assignment data. If the 
//assignment already exists it will be replaced.
exports.upload = function (req, res, next) {
    
    try { rawBody = JSON.parse(req.body) } 
    catch (e) { 
        console.log(e)
        return next("invalid syntax for raw body of request")
    }

    var assignmentID = req.params.assignmentID;
    var visualizationType = rawBody.visual;
    if(visualizationType != "tree")
	visualizationType = "nodelink";
    
    //get username from apikey 
    User.findOne({
        apikey:req.query.apikey
    })    
    .exec(function (err, user) {    
        if (err) return next (err)    
        if (!user) return next ("could not find user by apikey: " + 
                        req.query.apikey)

        //if username found, upload or replace 
        replaceAssignment(res, user, assignmentID) 
    })

    function replaceAssignment (res, user, assignmentID) {
        
        //if this assignment is #.0, remove all sub assignments from #
        if(assignmentID.split('.')[1] == "0") {
            Assignment.remove({
                assignmentID: {$gte: Math.floor(parseFloat(assignmentID)), $lt: Math.floor(parseFloat(assignmentID) + 1) },
                email: user.email        
            })
            .exec(function (err, resp) {
            })
        }
        
        //remove previously uploaded assignment if exists
        Assignment.remove({
            assignmentID: assignmentID,
            email: user.email        
        })
        .exec(function (err, resp) {

            //create a new assignment in the database
            assignment = new Assignment()
            assignment.email = user.email
            assignment.vistype = visualizationType
            assignment.data = rawBody
            assignment.assignmentID = assignmentID
            assignment.schoolID = req.params.schoolID;
            assignment.classID = req.params.classID;
            assignment.save()
            
            //log new assignment
            //console.log("assignment added: "+user.email+
              //  " "+assignmentID)
            
            //report to client
            User.findOne({
                email: user.email
            }).exec(function (err, resp) {
                res.json({"msg":assignmentID+"/"+resp.username}) 
            })
        })
    }
}

var sessionUser = null;

exports.next = null

exports.show = function (req, res, next) {
    //var query = Assignment.find({'username': 'test'});
    //console.log(query);

    this.next = next 
    var assignmentID = req.params.assignmentID
    var schoolID = req.params.schoolID
    var classID = req.params.classID
    var username = req.params.username
    var apikey = req.query.apikey 
    sessionUser = null
    if (typeof req.user != "undefined") sessionUser = req.user
    
    User
        .findOne({username: username})
        .exec(function(err, usr){
            if (err) return next(err) 
            if (!usr) 
                return next("couldn't find the username "+username) 
           
            getAssignment(req, res, next, usr.email, function (assign) {
               
                //Test whether user has permission to view vis
                testByUser(res, req, username, assign, function (){
                    testByKey(res, apikey, username, assign, null)
                })
            })
        })

    function getAssignment (req, res, next, email, cb) {
        assignmentID = req.params.assignmentID
        next = next
        
        //db.products.find( { qty: { $gt: 25 } }, { item: 1, qty: 1 } )
        Assignment
            .find({
                email: email, 
                assignmentID: {$gte: Math.floor(parseFloat(assignmentID)), $lt: Math.floor(parseFloat(assignmentID) + 1)}
                
            })
            .exec(function(err, assignments) {
                if (err) return next(err);
                if (!assignments || assignments.length == 0) {
                         return next("Could not find assignment " + assignmentID);   
                } else if (assignments.length == 1) {
                    //console.log(assignments[0]);
                    return renderVis(res, assignments[0]);
                } else 
                    return renderMultiVis(res, assignments);
            });
           // console.log(assignmentID, username);
        <commit_msg>Added ordering for multi-assignment vis<commit_after>var mongoose = require('mongoose')
    , User = mongoose.model('User')
    , Account = mongoose.model('Account')
    , Assignment = mongoose.model('Assignment')
    , treemill = require('treemill') 

//API route for ajax requests to change the visual 
//representation of the data they uploaded.
exports.updateVistype = function (req, res, next) {
    
    console.log('update vistype for ' + req.user.email + 
        ' '+req.params.assignmentID)

    Assignment
        .findOne({
            email:req.user.email,
            assignmentID: req.params.assignmentID
        })
        .exec(function (err, assignmentResult) {
            if (err) return next(err)
            if (!assignmentResult) 
                return next("could not find assignment")
            //validate the vis type is implemented..
            vistypes = ["nodelink", "tree", "queue"]
            if (vistypes.indexOf(req.params.value) == -1) 
                return next("specified vistype is not implemented")
            assignmentResult.vistype=req.params.value 
            assignmentResult.save()
            res.send("OK")
        })
}

//API route to toggle the visibility of an assignment 
//between private and public.
exports.updateVisibility = function (req, res, next) {
    Assignment
        .findOne({
            email:req.user.email,
            assignmentID: req.params.assignmentID    
        })
        .exec(function (err, assignmentResult) {
            if (err) return next(err)
            if (!assignmentResult) 
                return next("could not find assignment")
            assignmentResult.shared=req.params.value 
            assignmentResult.save()
            res.send("OK")
        })
}

//API route for uploading assignment data. If the 
//assignment already exists it will be replaced.
exports.upload = function (req, res, next) {
    
    try { rawBody = JSON.parse(req.body) } 
    catch (e) { 
        console.log(e)
        return next("invalid syntax for raw body of request")
    }

    var assignmentID = req.params.assignmentID;
    var visualizationType = rawBody.visual;
    if(visualizationType != "tree")
	visualizationType = "nodelink";
    
    //get username from apikey 
    User.findOne({
        apikey:req.query.apikey
    })    
    .exec(function (err, user) {    
        if (err) return next (err)    
        if (!user) return next ("could not find user by apikey: " + 
                        req.query.apikey)

        //if username found, upload or replace 
        replaceAssignment(res, user, assignmentID) 
    })

    function replaceAssignment (res, user, assignmentID) {
        
        //if this assignment is #.0, remove all sub assignments from #
        if(assignmentID.split('.')[1] == "0") {
            Assignment.remove({
                assignmentID: {$gte: Math.floor(parseFloat(assignmentID)), $lt: Math.floor(parseFloat(assignmentID) + 1) },
                email: user.email        
            })
            .exec(function (err, resp) {
            })
        }
        
        //remove previously uploaded assignment if exists
        Assignment.remove({
            assignmentID: assignmentID,
            email: user.email        
        })
        .exec(function (err, resp) {

            //create a new assignment in the database
            assignment = new Assignment()
            assignment.email = user.email
            assignment.vistype = visualizationType
            assignment.data = rawBody
            assignment.assignmentID = assignmentID
            assignment.schoolID = req.params.schoolID;
            assignment.classID = req.params.classID;
            assignment.save()
            
            //log new assignment
            //console.log("assignment added: "+user.email+
              //  " "+assignmentID)
            
            //report to client
            User.findOne({
                email: user.email
            }).exec(function (err, resp) {
                res.json({"msg":assignmentID+"/"+resp.username}) 
            })
        })
    }
}

var sessionUser = null;

exports.next = null

exports.show = function (req, res, next) {
    //var query = Assignment.find({'username': 'test'});
    //console.log(query);

    this.next = next 
    var assignmentID = req.params.assignmentID
    var schoolID = req.params.schoolID
    var classID = req.params.classID
    var username = req.params.username
    var apikey = req.query.apikey 
    sessionUser = null
    if (typeof req.user != "undefined") sessionUser = req.user
    
    User
        .findOne({username: username})
        .exec(function(err, usr){
            if (err) return next(err) 
            if (!usr) 
                return next("couldn't find the username "+username) 
           
            getAssignment(req, res, next, usr.email, function (assign) {
               
                //Test whether user has permission to view vis
                testByUser(res, req, username, assign, function (){
                    testByKey(res, apikey, username, assign, null)
                })
            })
        })

    function getAssignment (req, res, next, email, cb) {
        assignmentID = req.params.assignmentID
        next = next
        
        //db.products.find( { qty: { $gt: 25 } }, { item: 1, qty: 1 } )
        Assignment
            .find({
                email: email, 
                assignmentID: {$gte: Math.floor(parseFloat(assignmentID)), $lt: Math.floor(parseFloat(assignmentID) + 1)}
                
            })
            .sort({ 
                assignmentID: 1 
            })
            .exec(function(err, assignments) {
                if (err) return next(err);
                if (!assignments || assignments.length == 0) {
                         return next("Could not find assignment " + assignmentID);   
                } else if (assignments.length == 1) {
                    //console.log(assignments[0]);
                    return renderVis(res, assignments[0]);
                } else 
                    return renderMultiVis(res, assignments);
            });
           // console.log(assignmentID, username);
        
{'fraction_non_alphanumeric': 0.10903931901094446, 'fraction_numerical': 0.0023510336441021486, 'mean_word_length': 1.714191419141914, 'pattern_counts': {'":': 2, '<': 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': '7634225', 'n_tokens_mistral': 3356, 'n_tokens_neox': 3258, 'n_words': 843}