Skip to content

Commit c062370

Browse files
committed
Add problems
1 parent 68a0f4c commit c062370

4 files changed

Lines changed: 112 additions & 1 deletion

File tree

lib/course.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var utils = require('./utils');
88

99
var Base = require('./base');
1010
var About = require('./about');
11+
var Problem = require('./problem');
1112

1213
// ## //
1314

@@ -321,6 +322,19 @@ var Course = Base.extend({
321322
return self._saveAboutFields();
322323
})
323324
.return(this);
325+
},
326+
327+
listProblems: function () {
328+
var _id = this._get('_id');
329+
330+
return models.Problem
331+
.findAsync({
332+
'_id.org': _id.org,
333+
'_id.course': _id.course
334+
})
335+
.then(function (problems) {
336+
return problems.map(Problem.load);
337+
});
324338
}
325339
});
326340

lib/models/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,19 @@ var CourseSchema = require('./course');
1010
var AboutSchema = require('./about');
1111
var ChapterSchema = require('./chapter');
1212
var SequentialSchema = require('./sequential');
13+
var ProblemSchema = require('./problem');
1314

1415
// ## //
1516

16-
var Module, Course, About, Chapter, Sequential;
17+
var Module, Course, About, Chapter, Sequential, Problem;
1718

1819
var setup = function (connection) {
1920
Module = connection.model('Module', ModuleSchema);
2021
Course = Module.discriminator('course', CourseSchema);
2122
About = Module.discriminator('about', AboutSchema);
2223
Chapter = Module.discriminator('chapter', ChapterSchema);
2324
Sequential = Module.discriminator('sequential', SequentialSchema);
25+
Problem = Module.discriminator('problem', ProblemSchema);
2426
};
2527

2628
// ## //
@@ -32,3 +34,4 @@ exports.__defineGetter__('Course', function () { return Course; });
3234
exports.__defineGetter__('About', function () { return About; });
3335
exports.__defineGetter__('Chapter', function () { return Chapter; });
3436
exports.__defineGetter__('Sequential', function () { return Sequential; });
37+
exports.__defineGetter__('Problem', function () { return Problem; });

lib/models/problem.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
var ModuleSchema = require('./module');
4+
5+
// ## //
6+
7+
var ProblemSchema = ModuleSchema.extend({
8+
metadata: {
9+
markdown: String,
10+
'max_attempts': Number
11+
},
12+
definition: {
13+
data: {
14+
data: String
15+
}
16+
}
17+
});
18+
19+
// ## //
20+
21+
module.exports = ProblemSchema;

lib/problem.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
3+
var Base = require('./base');
4+
var models = require('./models');
5+
6+
// ## //
7+
8+
/*
9+
** Constructor
10+
*/
11+
12+
var Problem = Base.extend({
13+
initialize: function (model) {
14+
this._model = model || new models.Problem();
15+
},
16+
17+
/*
18+
** Attributes
19+
*/
20+
21+
attributes: {
22+
xml: {
23+
get: function () {
24+
return this._get('definition.data.data');
25+
},
26+
set: function (value) {
27+
return this._set('definition.data.data', value);
28+
}
29+
},
30+
31+
input: {
32+
get: function () {
33+
return this._get('metadata.markdown');
34+
},
35+
set: function (value) {
36+
return this._set('metadata.markdown', value);
37+
}
38+
},
39+
40+
attempts: {
41+
get: function () {
42+
return this._get('metadata.max_attempts');
43+
},
44+
set: function (value) {
45+
return this._set('metadata.max_attempts', value);
46+
}
47+
48+
}
49+
}
50+
});
51+
52+
Problem.load = function (model, parent) {
53+
var problem = new Problem(model);
54+
problem.parent = parent;
55+
return problem;
56+
};
57+
58+
Problem.get = function (id, options) {
59+
return models.Problem
60+
.findOneAsync({
61+
'_id.name': id
62+
})
63+
.then(function (model) {
64+
if (model) {
65+
return Problem.load(model, null, options);
66+
}
67+
return null;
68+
});
69+
};
70+
71+
// ## //
72+
73+
module.exports = Problem;

0 commit comments

Comments
 (0)