-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollcall.js
More file actions
298 lines (239 loc) · 7 KB
/
rollcall.js
File metadata and controls
298 lines (239 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*jshint debug:false, noarg:true, noempty:true, eqeqeq:true, bitwise:true, undef:true, curly:true, browser: true, devel: true, jquery:false, strict:true */
/*global Backbone, Skeletor, _, jQuery, Rollcall, exports, require */
(function () {
"use strict";
var Backbone, Skeletor, Drowsy, jQuery, _,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) {return i;} } return -1; },
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty;
// This detects if we are in a browser or node.js environment and
// ensures dependencies are available in the required way
if (typeof exports !== "undefined" && exports !== null) {
jQuery = require("jquery");
_ = require("underscore");
Backbone = require("backbone");
Backbone.$ = jQuery;
Drowsy = require("backbone.drowsy").Drowsy;
Skeletor = {};
exports.Skeletor = Skeletor;
} else {
window.Skeletor = window.Skeletor || {};
Skeletor = window.Skeletor;
jQuery = window.$;
_ = window._;
Drowsy = window.Drowsy;
}
// a Rollcall 2.0 client
var Rollcall = function (url, db) {
this.server = new Drowsy.Server(url);
this.db = this.server.database(db);
// User model
this.User = this.db.Document('users').extend({
addTag: function (tag) {
var tags = _.clone(this.get('tags'));
// if no classes array exists add it
if (!tags) {
tags = [];
}
tags.push(tag);
this.set('tags', _.uniq(tags));
},
removeTag: function (tag) {
var tags = this.get('tags');
this.set('tags', _.without(tags, tag));
},
addCohort: function (c) {
var classes = _.clone(this.get('cohorts'));
// if no cohorts array exists add it
if (!cohorts) {
cohorts = [];
}
cohorts.push(c);
this.set('cohorts', _.uniq(cohorts));
},
removeCohort: function (c) {
var cohorts = this.get('cohorts');
this.set('cohorts', _.without(cohorts, c));
},
isTeacher: function() {
if (this.get('user_role') === 'teacher') {
return true;
} else {
return false;
}
}
});
this.Users = this.db.Collection('users').extend({
model: this.User
});
/*
* Group model
*/
this.Group = this.db.Document('groups').extend({
addGroup: function (group) {
var groups = _.clone(this.get('groups'));
groups.push(group);
this.set('groups', _.uniq(group));
}
});
this.Groups = this.db.Collection('groups').extend({
model: this.Group
});
// Run model
this.Run = this.db.Document('runs').extend({
});
this.Runs = this.db.Collection('runs').extend({
model: this.Run
});
/*
* Model for Cohorts
*/
this.Cohort = this.db.Document('cohorts').extend({
addDiscussion: function (discussionId) {
var discussions = _.clone(this.get('discussions'));
// if no discussions array exists add it
if (!discussions) {
discussions = [];
}
discussions.push(discussionId);
this.set('discussions', _.uniq(discussions));
},
removeDiscussion: function (discussionId) {
var discussions = this.get('discussions');
this.set('discussions', _.without(discussions, discussionId));
}
});
this.Cohorts = this.db.Collection('cohorts').extend({
model: this.Cohort
});
/*
* Model for Discussions
*/
this.Discussion = this.db.Document('discussions').extend({
});
this.Discussions = this.db.Collection('discussions').extend({
model: this.Discussion
});
};
Rollcall.prototype.users = function(selector) {
selector = selector || {};
var users = new this.Users();
var usersPromise = users.fetch({
data: {
selector: JSON.stringify(selector),
strict: false
}
});
return usersPromise.then(function () {
return users;
});
};
Rollcall.prototype.authenticate = function(username, password) {
var authenticatePromise = this.user(username)
.then(function (user) {
if (!user) {
return false;
}
if (user.get('password') !== password) {
return false;
}
return true;
});
return authenticatePromise;
};
Rollcall.prototype.identify = function(username) {
var identifyPromise = this.user(username)
.then(function (user) {
if (!user) {
return null;
}
return user;
});
return identifyPromise;
};
Rollcall.prototype.usersWithTags = function(tags) {
tags = tags || [];
var selector = {"tags":{"$all": tags}};
return this.users(selector);
};
Rollcall.prototype.usersWithClasses = function(classes) {
classes = classes || [];
var selector = {"classes":{"$all": classes}};
return this.users(selector);
};
Rollcall.prototype.user = function(username) {
return this.users({"username": username})
.then(function (users) {
return users.at(0);
});
};
Rollcall.prototype.userExists = function(username) {
return this.user(username)
.then(function (u) {
if (u) {
return true;
} else {
return false;
}
});
};
Rollcall.prototype.usersWithUserRole = function (userRole) {
userRole = userRole || '';
var selector = {"user_role":userRole};
return this.users(selector);
};
Rollcall.prototype.group = function(groupname) {
return this.groups({"groupname": groupname})
.then(function (groups) {
return groups.at(0);
});
};
Rollcall.prototype.groups = function(selector) {
selector = selector || {};
var groups = new this.Groups();
var groupsPromise = groups.fetch({
data: {
selector: JSON.stringify(selector),
strict: false
}
});
return groupsPromise.then(function () {
return groups;
});
};
Rollcall.prototype.groupsWithTags = function(tags) {
tags = tags || [];
var selector = {"tags":{"$all": tags}};
return this.groups(selector);
};
/*
* Added run stuff - unsure and therefor experimental
*/
Rollcall.prototype.runs = function(selector) {
selector = selector || {};
var runs = new this.Runs();
var runsPromise = runs.fetch({
data: {
selector: JSON.stringify(selector),
strict: false
}
});
return runsPromise.then(function () {
return runs;
});
};
Rollcall.prototype.selectFromCollection = function(selector, collectionName) {
selector = selector || {};
var collection = new this[collectionName]();
var promise = collection.fetch({
data: {
selector: JSON.stringify(selector),
strict: false
}
});
return promise.then(function () {
return collection;
});
};
this.Rollcall = Rollcall;
}).call(this);