I have created a virtual bathroom pass where the students enter their names and click a button to sign out and then back in. This is so we can track the frequent flyers and the same student doesn’t go to the washroom 6 periods of the day. The code technician showed me I can add it as a button to see that on the app but is there a way to make it a live spreadsheet or anything like that?
Hello @kevmur,
does this program actually pertain to code.org? if we assume this is on there and is using Applab as the storage center I’d recommend just having the button alert you that the student is a frequent flyer than needing to see all the data at the given time however you can access the CDO data paths if that’s what your asking for
Using the CDO data API
you can use the /datablock_storage/:id/:method
to interact with the CDO data even if the account isn’t yours
/*
@routes
@POST
* set_key_value
@desc
* sets a key to the corresponding value
* create_record
@desc
* creates a given record to the corresponding table
* create_table
@desc
* initalizies a given table name in the dataset
* add_column
@desc
* adds a column to a given table
* add_shared_table
@desc
* would add any shared tables in the given library manifest
* import_csv
@desc
* would import a csv datasheet into the dataset
@PUT
* populate_key_values
@desc
* populates multiple keys within a given project
* populate_tables
@desc
* populates multiple tables within a given project
* update_record
@desc
* updates a given record in a table
* rename_column
@desc
* changes the given column name in a table
* coerce_column
@desc
* coerces a column of data to a <number>, <boolean>, or <string> type
@GET
* get_key_value
@desc
* gets an established key from the database
* get_key_values
@desc
* gets all key values in the established database
* get_column
@desc
* gets a specific column specified in a given table
* get_columns_for_table
@desc
* gives all availible columns on a given table
* export_csv
@desc
* exports a given table to a csv file
* read_records
@desc
* gets all records/entries from a given table
* get_table_names
@desc
* returns all availible table names on a given project
* get_library_manifest
@desc
* would return the inbuilt availible librarys that can be shared
* project_has_data
@desc
* checks to see if the project has any data
@DELETE
* delete_key_value
* clear_table
* delete_record
* delete_column
* delete_table
* clear_all_data
*/
Note that you need an account signed in to interact with project data and that have not documented the paths with the necessary parameters to use so you will have to do a bit of research of your own if you want to use this route
However if you want to see the given data for any student without using this you may want to consider the further solutions i have provided below
View the Content within the App
you can try a much simpler approach something like this would do if you want to see it within the app
onEvent("userData_btn", "clicked", function() {
var records = readRecords("bathroomUsers" + Date().slice(0,15), function(v){
// parse data here
var text = "";
for(var i = 0; i < v.length; i++) {
var user = v[i];
text += user.name + ": " + user.used + "\n";
}
setText("users", text);
})
})
I don’t want the app to display it i just want to have the csv file
you are also able to export the data as a csv if you wish to keep a physical log outside of CDO since it does have limited space per project not sure exactly of what you want but this will also be included
You don’t have to be the owner of the project to export the tables off of CDO just make sure you have a CDO account so that you can properly interact with the storage API to do this it’d be easiest if you know the table you want within the project otherwise your going to have to run a check to see what table your trying to extract from the project. you will most likely have to test for get_table_names
path as referenced with the earlier data API there is no parameters necessary besides the project id which is always required with the API
once you have located the table you want it’s time to export it from the project. To do this we are going to use this datablock path
https://studio.code.org/datablock_storage/:id/export_csv?table_name=:name
by doing this you should be able to view any table on CDO as an excel table
I know this is a lot but i hope one of these solutions helps
Varrience
Thanks for all the info and ideas. We have to know when they are going so the red flag wouldn’t be as useful because we more need to know when they just did since if they go 4,5,and try 6 we have to text to find out. This way it becomes easier since you can say you just went this time, this time and this for this long…
// demo of how table should be formatted
/*
name // name of the person going
times // store users in and out times
*/
var id = getAttribute("divApplab", "baseURI").match(new RegExp("(?<=(applab|gamelab)/)[^/]+|^[^/]+(?!/)$"))[0];
var db = {};
var dbn = "bathroomPool";
onEvent("bathroomBtn", "click", function () {
var name = getText("signoutBox");
startWebRequest("https://studio.code.org/datablock_storage/" + id + "/read_records?table_name=" + dbn + "&cb=" + Math.floor(Math.random() * 5e5), function (s, t, c) {
if (s == 200 && t == "application/json" && c !== "[]") {
var data = JSON.parse(c);
var users = Object.keys(data);
for (var i in users) {
var user = data[users[i]];
var times = user.times = JSON.parse(user.times);
var lastTime = times[Object.keys(times).length - 1];
if (user.name === name.toLowerCase()) {
// update user if there current time is in or out and update it
if ("in" in lastTime) { // adds new out time
console.log(times)
} else { // finishes in time and calculates the total time out
lastTime.in = Date.now();
var total = lastTime.in - lastTime.out;
// ideally a break won't exceed an hour so we will format minutes and seconds
var minutes = total / 6e4;
var seconds = (minutes - parseInt(minutes)) * 60;
lastTime.total = (minutes > 0 ? parseInt(minutes) + " minutes " : "") + (seconds > 0 ? parseInt(seconds) + " seconds" : "")
lastTime.out = String(new Date(lastTime.out).valueOf()).slice(16,24);
lastTime.inms = lastTime.in; // allows for checking last time they went out
lastTime.in = String(new Date(lastTime.in).valueOf()).slice(16,24);
lastTime.dif = total;
user.times = JSON.stringify(times);
updateRecordSync(dbn, user)
}
return;
}
}
addNewUser(name);
} else {
addNewUser(name);
}
})
})
function addNewUser(name) {
createRecord(dbn, {
name: name.toLowerCase(),
times: JSON.stringify([{
out: Date.now()
}])
})
}
would this be closer to what your looking for? it’s not finished yet though you can check inms to check the last time they came in the rest is formatting if you want to make UI to display the data so the person allowing them knows
I’ll share the link later if you interested
Thanks, this is great. I will see what tweeks I need to make to what I have and hopefully get it to work.
App Lab - Code.org I have finished the project and am satisfied with the results best of luck with tweaking it
Varrience
This is great. I am going to adjust mine so I can have that second button that will show the times that the student went. I’m also going to change the sign in and out to one button so it can be easier to use. This is the current one I have and I made a copy so you can see the code.
Your free to just use my template if you wish (just leave credits somewhere)
your code as you have it now it will be pretty hard to assemble all the user logs times since your adding them separately vs having it as 1 entry so you’d have to perform a search on the db to get all in and out times on it just make sure to leave my credit somewhere if you decide to use it
or if you plan on learning from my example i welcome it just a minor nitpick that i have with your current project at the moment but if that’s how you want it structured that’s fine
Varrience
I agree and its indeed helpful tho