160 lines
4.8 KiB
JavaScript
Raw Normal View History

2016-08-01 19:30:32 +08:00
import { Meteor } from 'meteor/meteor';
2016-08-07 00:32:19 -04:00
import { Mongo } from 'meteor/mongo';
2016-08-01 19:30:32 +08:00
2016-08-08 05:39:20 -04:00
_uuid4 = function(cc) {
var rr = Math.random() * 16 | 0;
return (cc === 'x' ? rr : (rr & 0x3 | 0x8)).toString(16);
2016-08-12 12:10:21 -04:00
};
2016-08-07 15:21:39 -04:00
2016-08-10 17:42:07 -04:00
worktype = ["test", "quiz", "project", "normal"];
2016-08-08 20:55:02 -04:00
Meteor.methods({
'genCode': function() {
2016-08-10 07:54:25 -04:00
return 'xxxxxx'.replace(/[x]/g, _uuid4);
2016-08-08 20:55:02 -04:00
},
2016-08-10 07:54:25 -04:00
'createSchool': function(schoolname) {
2016-08-10 14:05:45 -04:00
// if superadmin, no need for approval
2016-08-12 12:10:21 -04:00
if (Meteor.user() !== null &&
schools.findOne({name:input.school}) !== null &&
schools.findOne({status: false, creator: Meteor.userId()}) !== null) {
schools.insert({name: schoolname, status: false, creator: Meteor.userId()});
2016-08-10 07:54:25 -04:00
}
2016-08-10 14:05:45 -04:00
},
'deleteSchool': function(schoolid) {
// alanning:roles implementation here
2016-08-12 12:10:21 -04:00
schools.remove({_id: schoolid});
2016-08-10 07:54:25 -04:00
},
2016-08-08 20:55:02 -04:00
'createClass': function(input) {
2016-08-10 14:05:45 -04:00
// if superadmin, no need for approval
2016-08-09 16:56:30 -04:00
classes.schema.validate(input);
2016-08-12 12:10:21 -04:00
if(Meteor.user() !== null &&
classes.find({status:false, admin:Meteor.userId()}).fetch().length < 5 &&
2016-08-12 12:10:21 -04:00
schools.findOne({name:input.school}) !== null) {
2016-08-10 14:14:55 -04:00
input.status = false;
2016-08-09 16:56:30 -04:00
input.subscribers = 0;
2016-08-12 12:10:21 -04:00
input.admin = Meteor.userId();
2016-08-09 16:56:30 -04:00
if (input.privacy) {
2016-08-09 17:10:08 -04:00
Meteor.call('genCode', function(error, result) {
input.code = result;
});
2016-08-09 16:56:30 -04:00
} else {
input.code = "";
}
if (input.category != "class" && input.category != "club") {
input.category = "other";
}
2016-08-12 12:10:21 -04:00
input.moderators = [];
input.banned = [];
input.blockEdit = [];
2016-08-08 20:55:02 -04:00
classes.insert(input);
2016-08-10 13:48:46 -04:00
Meteor.call('joinClass',classes.findOne(input)._id, input.code, function(error,result){});
2016-08-09 16:56:30 -04:00
return 1;
} else {
return 0;
}
2016-08-08 20:55:02 -04:00
},
2016-08-10 14:05:45 -04:00
'deleteClass': function(classid) {
found = classes.findOne({_id: classid});
// Add roles
2016-08-12 12:10:21 -04:00
if (Meteor.user() !== null && found !== null && found.admin === Meteor.user()._id) {
classes.remove({_id: classid});
2016-08-10 14:05:45 -04:00
}
2016-08-10 17:42:07 -04:00
},
'createWork': function(input) {
2016-08-12 12:10:21 -04:00
ref = new Date();
month = ref.getMonth + 1;
ref = new Date(ref.getFullYear()+ "-" + month.toString() + "-" + ref.getDate()).getTime();
2016-08-10 17:42:07 -04:00
work.schema.validate(input);
2016-08-12 12:10:21 -04:00
found = Meteor.findOne({_id: input.class});
2016-08-12 12:10:21 -04:00
if (Meteor.user() !== null &&
found !== null &&
found.subscribers.indexOf(Meteor.userId()) != -1 &&
found.banned.indexOf(Meteor.userId()) === -1 &&
found.blockEdit.indexOf(Meteor.userId()) === -1 &&
input.dueDate.getTime() >= ref && worktype.indexOf(type) != -1 &&
input.name.length <= 50) {
2016-08-10 17:42:07 -04:00
input.submittor = Meteor.userId();
input.confirmations = [Meteor.userId()];
input.reports = [];
input.done = [];
input.numberdone = 0;
2016-08-10 18:26:23 -04:00
work.insert(input);
2016-08-10 17:42:07 -04:00
}
},
'deleteWork': function(workid) {
2016-08-10 17:43:31 -04:00
// Add security here
work.remove({_id: workid});
2016-08-10 14:05:45 -04:00
},
2016-08-08 20:55:02 -04:00
'editProfile': function(change) {
current = Meteor.user().profile;
current.school = change.school;
current.grade = change.grade;
current.description = change.description;
current.avatar = change.avatar;
current.banner = change.banner;
2016-08-12 12:10:21 -04:00
current.preferences = change.preferences;
if (schools.findOne({name:current.school}) !== null &&
Number.isInteger(current.grade) &&
current.grade >= 9 && current.grade <= 12 &&
current.description.length <= 50) {
2016-08-08 20:55:02 -04:00
Meteor.users.update({_id: Meteor.userId()}, {$set: {profile: current}});
return 1;
} else {
return 0;
}
},
'joinClass': function(input) {
change = input[0];
pass = input[1];
if(Meteor.user().profile.classes === undefined) {
curr = Meteor.user().profile;
curr.classes = [];
2016-08-12 12:10:21 -04:00
Meteor.users.update({_id: Meteor.userId()}, {$set: {profile: curr}});
}
prof = Meteor.user().profile;
found = classes.findOne({_id: change, status: true});
2016-08-12 12:10:21 -04:00
if (Meteor.user() !== null &&
found !== null &&
pass === found.code &&
!found.banned.includes(Meteor.userId()) &&
!prof.classes.includes(change)) {
2016-08-08 20:39:15 -04:00
current = Meteor.user().profile;
2016-08-08 20:55:02 -04:00
current.classes.append(change);
Meteor.users.update({_id: Meteor.userId()}, {$set: {profile: current}});
return 1;
} else {
return 0;
}
2016-08-09 18:03:31 -04:00
},
'leaveClass': function(change) {
2016-08-12 12:10:21 -04:00
if (Meteor.user() !== null) {
profile = Meteor.user().profile;
index = profile.classes.indexOf(change);
2016-08-09 18:03:31 -04:00
if (index >= 0) {
if (classes.findOne({_id: change}).admin != Meteor.userId()) {
2016-08-09 18:23:02 -04:00
profile.classes.splice(index, 1);
Meteor.users.update({_id: Meteor.userId()}, {$set: {profile: current}});
2016-08-12 12:10:21 -04:00
return 1;
2016-08-09 18:23:02 -04:00
} else {
2016-08-12 12:10:21 -04:00
throw "You are currently the admin of this class. Transfer ownership in order to leave this class.";
2016-08-09 18:23:02 -04:00
}
2016-08-09 18:03:31 -04:00
}
}
2016-08-08 20:55:02 -04:00
}
});
function has(array, has) {
for(var i = 0; i < array.length; i++) {
if(array[i] === has) return true;
}
return false;
2016-08-12 12:10:21 -04:00
}