work functions

This commit is contained in:
yamanq 2016-08-12 21:52:14 -04:00
parent f2fa1fdc20
commit 5dc07fb7c3
2 changed files with 64 additions and 23 deletions

View File

@ -31,11 +31,9 @@ work.schema = new SimpleSchema({
dueDate: {type: Date}, dueDate: {type: Date},
creator: {type: String}, creator: {type: String},
comments: {type: [String]}, comments: {type: [String]},
submittor: {type: String, optional: true},
confirmations: {type: [String], optional: true}, confirmations: {type: [String], optional: true},
reports: {type: [String], optional: true}, reports: {type: [String], optional: true},
attachments: {type: [String], optional: true}, attachments: {type: [String], optional: true},
done: {type: [String], optional: true}, done: {type: [String], optional: true},
numberdone: {type: Number, optional: true},
type: {type: String} type: {type: String}
}); });

View File

@ -15,6 +15,8 @@ superadmins = [
"ksjdragon@gmail.com" "ksjdragon@gmail.com"
]; ];
worktype = ["test", "quiz", "project", "normal"];
for (var i = 0; i < superadmins.length; i++) { for (var i = 0; i < superadmins.length; i++) {
var superadmin = superadmins[i]; var superadmin = superadmins[i];
if (Meteor.users.findOne({ if (Meteor.users.findOne({
@ -75,7 +77,6 @@ Meteor.publish('work', function() {
}); });
worktype = ["test", "quiz", "project", "normal"];
Meteor.methods({ Meteor.methods({
'genCode': function() { 'genCode': function() {
return 'xxxxxx'.replace(/[x]/g, _uuid4); return 'xxxxxx'.replace(/[x]/g, _uuid4);
@ -91,9 +92,9 @@ Meteor.methods({
}) !== null) { }) !== null) {
if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) { if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin'])) {
stat = true; var stat = true;
} else { } else {
stat = false; var stat = false;
} }
schools.insert({ schools.insert({
name: schoolname, name: schoolname,
@ -147,7 +148,7 @@ Meteor.methods({
} }
}, },
'deleteClass': function(classid) { 'deleteClass': function(classid) {
found = classes.findOne({ var found = classes.findOne({
_id: classid _id: classid
}); });
if (Meteor.user() !== null && found !== null && (found.admin === Meteor.user()._id || Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']))) { if (Meteor.user() !== null && found !== null && (found.admin === Meteor.user()._id || Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']))) {
@ -157,11 +158,12 @@ Meteor.methods({
} }
}, },
'createWork': function(input) { 'createWork': function(input) {
ref = new Date(); var ref = new Date();
month = ref.getMonth + 1; var month = ref.getMonth + 1;
ref = new Date(ref.getFullYear() + "-" + month.toString() + "-" + ref.getDate()).getTime(); ref = new Date(ref.getFullYear() + "-" + month.toString() + "-" + ref.getDate()).getTime();
input.creator = Meteor.userId();
work.schema.validate(input); work.schema.validate(input);
found = Meteor.findOne({ var found = Meteor.findOne({
_id: input.class _id: input.class
}); });
@ -173,28 +175,69 @@ Meteor.methods({
input.dueDate.getTime() >= ref && worktype.indexOf(type) != -1 && input.dueDate.getTime() >= ref && worktype.indexOf(type) != -1 &&
input.name.length <= 50) { input.name.length <= 50) {
input.submittor = Meteor.userId();
input.confirmations = [Meteor.userId()]; input.confirmations = [Meteor.userId()];
input.reports = []; input.reports = [];
input.done = []; input.done = [];
input.numberdone = 0; input.numberdone = 0;
input.creator = Meteor.userId();
input.comments = []; input.comments = [];
work.insert(input); work.insert(input);
} }
}, },
'editWork': function(change) {
var ref = new Date();
var month = ref.getMonth + 1;
ref = new Date(ref.getFullYear() + "-" + month.toString() + "-" + ref.getDate()).getTime();
var currentclass = classes.findOne({_id: work.findOne({_id: workId}).class});
var authorized = currentclass.moderators.push(currentclass.admin);
if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']) {
Meteor.update({_id: change._id}, {$set: change});
} else if (authorized.indexOf(Meteor.userId()) != -1) {
if (change.name.length <= 50 && worktype.indexOf(type) != -1) {
Meteor.update({_id: change._id}, {$set: {name: change.name, dueDate: change.dueDate, comments: change.comments, attachments: change.attachments, type: change.type}});
}
} else if (Meteor.userId() === work.findOne({_id: change._id}).creator) {
if (change.name.length <= 50 && worktype.indexOf(type) != -1 && input.dueDate.getTime() >= ref) {
Meteor.update({_id: change._id}, {$set: {name: change.name, dueDate: change.dueDate, attachments: change.attachments, type: change.type}});
}
} else {
throw "Unauthorized."
}
},
'addComment': function(input) {
var comment = input[0];
var workobject = work.findOne({_id: input[1]});
var currentclass = classes.findOne({_id: workobject.class});
if (typeof comment === "string" && comment.length <= 200 &&
currentclass.subscribers.indexOf(Meteor.userId()) != -1 &&
currentclass.blockEdit.indexOf(Meteor.userId()) === -1) {
comments = workobject.comments.push(comment);
work.update({_id: input[1]}, {$set: {comments: comments}});
}
},
'markDone': function(workId) {
var workobject = work.findOne({_id: workId});
var currentclass = classes.findOne({_id: workobject.class});
if (currentclass.subscribers.indexOf(Meteor.userId()) != -1 &&
workobject.done.indexOf(Meteor.userId()) === -1) {
currentdone = workobject.done.push(Meteor.userId());
work.update({_id: input[1]}, {$set: {done: currentdone}});
}
},
'deleteWork': function(workId) { 'deleteWork': function(workId) {
var currentclass = classes.findOne({_id: work.findOne({_id: workId}).class}); var currentclass = classes.findOne({_id: work.findOne({_id: workId}).class});
var authorized = currentclass.moderators.push(currentclass.admin); var authorized = currentclass.moderators.push(currentclass.admin);
if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']) || authorized.indexOf(Meteor.userId()) != -1) { if (Roles.userIsInRole(Meteor.userId(), ['superadmin', 'admin']) ||
work.remove({ authorized.indexOf(Meteor.userId()) != -1) {
_id: workid
}); work.remove({
_id: workId
});
} }
}, },
'editProfile': function(change) { 'editProfile': function(change) {
current = Meteor.user().profile; var current = Meteor.user().profile;
current.school = change.school; current.school = change.school;
current.grade = change.grade; current.grade = change.grade;
current.description = change.description; current.description = change.description;
@ -223,10 +266,10 @@ Meteor.methods({
} }
}, },
'joinClass': function(input) { 'joinClass': function(input) {
change = input[0]; var change = input[0];
pass = input[1]; var pass = input[1];
prof = Meteor.user().profile; var prof = Meteor.user().profile;
found = classes.findOne({ var found = classes.findOne({
_id: change, _id: change,
status: true status: true
}); });
@ -236,7 +279,7 @@ Meteor.methods({
found.banned.indexOf(Meteor.userId()) === -1 && found.banned.indexOf(Meteor.userId()) === -1 &&
prof.classes.indexOf(change) === -1) { prof.classes.indexOf(change) === -1) {
current = Meteor.user().profile; var current = Meteor.user().profile;
current.classes.push(change); current.classes.push(change);
Meteor.users.update({ Meteor.users.update({
_id: Meteor.userId() _id: Meteor.userId()
@ -252,8 +295,8 @@ Meteor.methods({
}, },
'leaveClass': function(change) { 'leaveClass': function(change) {
if (Meteor.user() !== null) { if (Meteor.user() !== null) {
profile = Meteor.user().profile; var profile = Meteor.user().profile;
index = profile.classes.indexOf(change); var index = profile.classes.indexOf(change);
if (index >= 0) { if (index >= 0) {
if (classes.findOne({ if (classes.findOne({
_id: change _id: change