/* update gradphile demo data
Demo data generated for Huxley university department of liberal arts is ok,
but all timestamps and dates should be updated to current year when site is rebuilt.
check last academic year in which site was built on admin page (i.e. site built in 2017-12-11, then last built in academic year 2017)
So look at all fields in allstudents.json that meet these conditions:
NOT derived,
are of type: yyyy, date (mm/dd/yyyy), term (yyyy/[1,6,9])
increment yyyy by given amount to match current year (i.e. if this is academic year 2018, and site was last built in 2017, then increment by all year occurences by +1)
TODO: 2/9/2020 : make a minimum file with all dates set relative to 2000, then can just update by adding (current_year - 2000) to all year terms
*/
fs = require('fs');
bc = require('../bc_utilities.js');
var readYAML = require('read-yaml');
// require('../logging')(__filename);
var plan_fields = [ 'info','requirements'];
var updatable_value_types = ["yyyy","date","term"];
var current_year = (new Date()).getFullYear();
var incr = current_year - 2017;
console.log("\n Incrementing by " + incr + " years to update from 2017 to " + current_year + "\n");
/* ------------------------------------------------------------------------------ */
function increment_yyyy(value,incr) {
var year = parseInt(value,10);
year = year + incr;
console.log(value + " -> " + year);
return year.toString();
};
function increment_date(value,incr) {
// date (mm/dd/yyyy)
var parts = value.split('/');
var year = parseInt(parts[2]);
year = year + incr;
console.log(value + " -> " + (parts[0] + "/" + parts[1] + "/" + year));
return (parts[0] + "/" + parts[1] + "/" + year);
};
function increment_term(value,incr) {
// term (yyyy/[1,6,9])
var parts = value.split('/');
var year = parseInt(parts[0]);
year = year + incr;
console.log(value + " -> " + year + "/" + parts[1]);
return (year + "/" + parts[1]);
};
/* ------------------------------------------------------------------------------ */
/** given a value of value_type, increment year by given incr
value_type should be: yyyy, date (mm/dd/yyyy), or term (yyyy/[1,6,9])
*/
function increment_year(value,value_type,incr) {
if (null == value) { return value; }
if ("yyyy" == value_type) {
return increment_yyyy(value,incr);
}
else if ("date" == value_type) {
return increment_date(value,incr);
}
else if ("term" == value_type) {
return increment_term(value,incr);
}
return value;
}
/* ------------------------------------------------------------------------------ */
function increment_plans(student,incr) {
var plan_keys = Object.keys(student['academic_plans']);
for (var p=0;p<plan_keys.length;p++) {
plan = student['academic_plans'][plan_keys[p]];
plan.plan_code = plan_keys[p];
console.log(plan.plan_code);
plan_schema = academic_plan_codes[plan.plan_code];
for (var f=0;f < plan_fields.length;f++) {
var schema = plan_schema[plan_fields[f]];
var keys = Object.keys(schema);
for (var i=0;i<keys.length;i++) {
key = keys[i];
if (typeof plan[key] == "undefined") {
console.log("UNDEFINED PLAN KEY IN STUDENT: " + plan.plan_code +" "+ key);
}
else {
var derived = (typeof schema[key].source != "undefined" && schema[key].source.includes('derived'));
if (!derived) {
if (-1 != updatable_value_types.indexOf(schema[key].value_type)) {
plan[key] = increment_year(plan[key],
schema[key].value_type,
incr);
console.log(student.fsuid +": "
+ plan.plan_code + " "
+ key + " " + plan[key]);
}
}
} // key defined
} // next key
} // next plan_field
/*
var keys = Object.keys(plan);
for (var i=0;i<keys.length;i++) {
key = keys[i];
var fields = "undefined";
if (typeof plan_schema['info'][key] != "undefined") {
fields = 'info';
} else if (typeof plan_schema['requirements'][key] != "undefined") {
fields = 'requirements';
}
if (fields != "undefined") {
if (typeof plan_schema[fields][key] == "undefined") {
console.log("UNDEFINED PLAN KEY IN STUDENT: " + plan.plan_code +" " + fields + " "+ key);
}
else {
var derived = plan_schema[fields][key].source.includes('derived');
if (!derived) {
if (-1 != updatable_value_types.indexOf(plan_schema[fields][key].value_type)) {
plan[key] = increment_year(plan[key],
plan_schema[fields][key].value_type,
incr);
console.log(student.fsuid +": "
+ plan.plan_code + " "
+ key + " " + plan[key]);
}
}
} // key defined
} // fields undefined
else {
console.log("UNDEFINED FIELD KEY: " + plan.plan_code +" " + fields + " "+ key);
}
} // next key
*/
} // next plan
}
/* ------------------------------------------------------------------------------ */
function increment_student(student,incr) {
var keys = Object.keys(student);
for (var i=0;i<keys.length;i++) {
key = keys[i];
if (typeof student_schema[key] == "undefined") {
// console.log("UNDEFINED KEY IN STUDENT: " + key);
}
else {
var derived = student_schema[key].source.includes('derived');
if (!derived) {
if (-1 != updatable_value_types.indexOf(student_schema[key].value_type)) {
student[key] = increment_year(student[key],
student_schema[key].value_type,
incr);
console.log(student.fsuid +": " + key + " " + student[key]);
}
}
}
}
increment_plans(student,incr);
}
/* ------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------ */
// read in academic_plan_codes.yaml
// read in student_schema.yaml
var academic_plan_codes = readYAML.sync(__dirname+'/demo_source_data/dept/academic_plan_codes.yaml');
// remove the base academic plan code
delete academic_plan_codes['ACADEMICPLAN'];
/* turn plan codes in maps */
plan_codes = Object.keys(academic_plan_codes);
for (var p=0;p< plan_codes.length;p++) {
plan = academic_plan_codes[plan_codes[p]];
for (var f=0;f<plan_fields.length;f++) {
field_array = plan[plan_fields[f]];
plan[plan_fields[f]] = field_array.reduce(function(map, obj) {
map[obj.key] = obj;
return map;
}, {});
}
}
var student_schema_array = readYAML.sync(__dirname+'/../jekyll/_data/schemata/student_schema.yaml');
// turn schema into map
var student_schema = student_schema_array.reduce(function(map, obj) {
map[obj.key] = obj;
return map;
}, {});
// read in LIBARTS_dallstudents.json
var students = bc.readJSONfromAppDir(__dirname,'LIBARTS_allstudents_2017.json');
// increment all the students
for (var s=0; s < students.length;s++) {
increment_student(students[s],incr);
}
// save the new students file
// var time_now = new Date();
// var month = (time_now.getMonth()+1);
// if (month < 10) { month = '0' + month; }
// var date = time_now.getDate();
// if (date < 10) { date = '0' + date; }
// var hour = time_now.getHours();
// if (hour < 10) { hour = '0' + hour; }
// var minute = time_now.getMinutes();
// if (minute < 10) { minute = '0' + minute; }
//
// var time_string = "" + time_now.getFullYear() + month + date + "_" + hour + minute;
// var file_name = '/LIBARTS_allstudents_Current_Year.json';
var file_name = "./demo_source_data/students/allstudents_old_format.json";
// bc.backupFile(file_name);
fs.writeFileSync(file_name,JSON.stringify(students, null, "\t"));
//.copyFileSync(__dirname + file_name, __dirname + file_name + "_"+time_string+'.bak');
// don't forget to run refactor_plans.js on the resulting file!
//console.log('\n\n' + 'DON'T FORGET TO RUN REFACTOR_PLANS.JS ON THE NEW FILE!');
console.log("\n\nYou can now copy " + file_name + " to the demo_source_data/students/ folder as 'allstudents_old_format.json'\n\n");