1 define(function(require) {
5 var _ = require('libs/underscore'),
7 order: require('libs/text!./order.xml'),
8 choice: require('libs/text!./choice.xml'),
9 'choice.single': require('libs/text!./choiceSingle.xml'),
10 'choice.true-or-false': require('libs/text!./choiceTrueOrFalse.xml'),
11 gap: require('libs/text!./gaps.xml'),
12 replace: require('libs/text!./replace.xml')
15 var Item = function(node, exerciseNode) {
16 Object.defineProperty(this, 'text', {
19 var firstNode = node.contents()[0];
20 if(firstNode && firstNode.nodeType === Node.TEXT_NODE) {
21 return firstNode.getText();
27 this.exerciseNode = exerciseNode;
29 _.extend(Item.prototype, {
30 setText: function(text) {
32 var contents = this.node.contents();
33 if(contents.length === 1 && contents[0].nodeType === Node.TEXT_NODE) {
34 contents[0].setText(text);
36 contents.forEach(function(childNode) {
39 contents.append({text: text});
45 getAnswer: function() {
46 var toret = parseInt(this.node.getAttr('answer'), 10);
52 setAnswer: function(answer) {
53 answer = parseInt(answer, 10);
55 if(!_.isNumber(answer)) {
59 this.exerciseNode.object.getItems()
60 .sort(function(item1, item2) {
61 if(item1.getAnswer() > item2.getAnswer()) {
66 .some(function(item) {
67 if(item.getAnswer() === prev && !item.node.sameNode(this.node)) {
68 item.node.setAttr('answer', prev+1);
72 this.node.setAttr('answer', answer);
77 var isItemsList = function(node) {
78 return node.is('list.orderable');
82 var extension = {wlxmlClass: {'exercise.order': {
84 isContextRoot: function(node) {
85 return this.object.isItemNode(node) || this.sameNode(node);
87 getItems: function() {
91 this.contents().some(function(node) {
92 if(isItemsList(node)) {
94 .filter(function(node) {
95 return node.is('item.answer');
97 .forEach(function(node) {
98 toret.push(new Item(node, exerciseNode));
105 isItemNode: function(node) {
110 this.contents().some(function(node) {
111 if(isItemsList(node)) {
116 return list && list.sameNode(node.parent());
118 getDescription: function() {
120 this.contents().some(function(node) {
121 if(isItemsList(node)) {
130 addItem: function(text) {
132 this.contents().some(function(node) {
133 if(isItemsList(node)) {
134 var itemNode = this.document.createDocumentNode({tagName: 'div', attrs: {'class': 'item.answer', answer: this.object.getItems().length+1}});
135 toret = itemNode.append({text: text});
136 node.append(itemNode);
142 setDescription: function(text) {
143 this.contents().some(function(node) {
146 textNode = node.contents()[0];
147 if(!(textNode && textNode.nodeType === Node.TEXT_NODE)) {
148 node.prepend({text:text});
150 textNode.setText(text);
159 var choiceMethods = {
160 getChoiceList: function() {
161 return this.contents()
162 .filter(function(n) { return this.object.isChoiceList(n); }.bind(this))[0];
164 isChoiceList: function(node) {
165 return node.is('list') && this.sameNode(node.parent());
167 isChoiceListItem: function(node) {
168 return this.object.isChoiceList(node.parent()) && node.is('item.answer');
172 extension.wlxmlClass['exercise.choice'] = {
174 setAnswer: function(itemNode, answer) {
175 if(!this.object.isChoiceListItem(itemNode)) {
178 itemNode.setAttr('answer', answer ? 'true' : 'false');
181 methods: choiceMethods
184 extension.wlxmlClass['exercise.choice.single'] = {
186 markAsAnswer: function(itemNode) {
187 if(!this.object.isChoiceListItem(itemNode)) {
190 this.object.getChoiceList().contents()
191 .filter(function(node) { return node.is('item.answer'); })
192 .forEach(function(node) {
193 node.setAttr('answer', node.sameNode(itemNode) ? 'true' : 'false');
197 methods: choiceMethods
200 extension.wlxmlClass['exercise.choice.true-or-false'] = {
202 setAnswer: function(itemNode, answer) {
203 if(!this.object.isChoiceListItem(itemNode)) {
206 itemNode.setAttr('answer', answer ? 'true' : 'false');
209 methods: choiceMethods
212 extension.document = {
214 edumedCreateExerciseNode: function(klass) {
216 return this.createDocumentNode(templates[klass]);