1 |
efrain |
1 |
/**
|
|
|
2 |
* JavaScript for form editing relativedate conditions.
|
|
|
3 |
*
|
|
|
4 |
* @module moodle-availability_relativedate-form
|
|
|
5 |
*/
|
|
|
6 |
M.availability_relativedate = M.availability_relativedate || {};
|
|
|
7 |
|
|
|
8 |
// Class M.availability_relativedate.form @extends M.core_availability.plugin.
|
|
|
9 |
M.availability_relativedate.form = Y.Object(M.core_availability.plugin);
|
|
|
10 |
|
|
|
11 |
// Time fields available for selection.
|
|
|
12 |
M.availability_relativedate.form.timeFields = null;
|
|
|
13 |
|
|
|
14 |
// Start field available for selection.
|
|
|
15 |
M.availability_relativedate.form.startFields = null;
|
|
|
16 |
|
|
|
17 |
// A section or a module.
|
|
|
18 |
M.availability_relativedate.form.isSection = null;
|
|
|
19 |
|
|
|
20 |
// Optional warnings that can be displayed.
|
|
|
21 |
M.availability_relativedate.form.warningStrings = null;
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Initialises this plugin.
|
|
|
26 |
*
|
|
|
27 |
* @method initInner
|
|
|
28 |
* @param {array} timeFields Collection of time fields
|
|
|
29 |
* @param {array} startFields Collection of start fields
|
|
|
30 |
* @param {boolean} isSection Is this a section
|
|
|
31 |
* @param {array} warningStrings Collection of warning strings
|
|
|
32 |
* @param {array} activitySelector Collection of activity fields
|
|
|
33 |
*/
|
|
|
34 |
M.availability_relativedate.form.initInner = function(timeFields, startFields, isSection, warningStrings, activitySelector) {
|
|
|
35 |
this.timeFields = timeFields;
|
|
|
36 |
this.startFields = startFields;
|
|
|
37 |
this.isSection = isSection;
|
|
|
38 |
this.warningStrings = warningStrings;
|
|
|
39 |
this.activitySelector = activitySelector;
|
|
|
40 |
};
|
|
|
41 |
|
|
|
42 |
M.availability_relativedate.form.getNode = function(json) {
|
|
|
43 |
var html = '<span class="availability-relativedate">';
|
|
|
44 |
var fieldInfo;
|
|
|
45 |
var i = 0;
|
|
|
46 |
var j = 0;
|
|
|
47 |
|
|
|
48 |
for (i = 0; i < this.warningStrings.length; i++) {
|
|
|
49 |
html += '<div class="alert alert-warning alert-block fade in " role="alert">' + this.warningStrings[i] + '</div>';
|
|
|
50 |
}
|
|
|
51 |
html += '<label><select name="relativenumber">';
|
|
|
52 |
for (i = 1; i < 60; i++) {
|
|
|
53 |
html += '<option value="' + i + '">' + i + '</option>';
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
html += '</select></label> ';
|
|
|
57 |
html += '<label><select name="relativednw">';
|
|
|
58 |
for (i = 0; i < this.timeFields.length; i++) {
|
|
|
59 |
fieldInfo = this.timeFields[i];
|
|
|
60 |
html += '<option value="' + fieldInfo.field + '">' + fieldInfo.display + '</option>';
|
|
|
61 |
}
|
|
|
62 |
html += '</select></label> ';
|
|
|
63 |
html += '<label><select name="relativestart">';
|
|
|
64 |
|
|
|
65 |
for (i = 0; i < this.startFields.length; i++) {
|
|
|
66 |
fieldInfo = this.startFields[i];
|
|
|
67 |
html += '<option value="' + fieldInfo.field + '">' + fieldInfo.display + '</option>';
|
|
|
68 |
}
|
|
|
69 |
html += '</select></label>';
|
|
|
70 |
html += '<label><select name="relativecoursemodule"' + (json.s != 7 ? ' style="display: none;"' : '') + '>';
|
|
|
71 |
|
|
|
72 |
var defaultCourseModuleId = 0;
|
|
|
73 |
|
|
|
74 |
for (i = 0; i < this.activitySelector.length; i++) {
|
|
|
75 |
html += '<option disabled>' + this.activitySelector[i].name + '</option>';
|
|
|
76 |
for (j = 0; j < this.activitySelector[i].coursemodules.length; j++) {
|
|
|
77 |
html += '<option value="' + this.activitySelector[i].coursemodules[j].id + '"';
|
|
|
78 |
if (this.activitySelector[i].coursemodules[j].completionenabled == 0) {
|
|
|
79 |
html += ' disabled';
|
|
|
80 |
} else {
|
|
|
81 |
if (!defaultCourseModuleId) {
|
|
|
82 |
defaultCourseModuleId = this.activitySelector[i].coursemodules[j].id;
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
html += '>' + this.activitySelector[i].coursemodules[j].name + '</option>';
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
html += '</select></label>';
|
|
|
89 |
var node = Y.Node.create('<span>' + html + '</span>');
|
|
|
90 |
|
|
|
91 |
// Set initial values if specified.
|
|
|
92 |
i = 1;
|
|
|
93 |
if (json.n !== undefined) {
|
|
|
94 |
i = json.n;
|
|
|
95 |
}
|
|
|
96 |
node.one('select[name=relativenumber]').set('value', i);
|
|
|
97 |
|
|
|
98 |
i = 2;
|
|
|
99 |
if (json.d !== undefined) {
|
|
|
100 |
i = json.d;
|
|
|
101 |
}
|
|
|
102 |
node.one('select[name=relativednw]').set('value', i);
|
|
|
103 |
|
|
|
104 |
i = 1;
|
|
|
105 |
if (json.s !== undefined) {
|
|
|
106 |
i = json.s;
|
|
|
107 |
}
|
|
|
108 |
node.one('select[name=relativestart]').set('value', i);
|
|
|
109 |
|
|
|
110 |
i = defaultCourseModuleId;
|
|
|
111 |
if (json.m !== undefined) {
|
|
|
112 |
i = json.m;
|
|
|
113 |
}
|
|
|
114 |
node.one('select[name=relativecoursemodule]').set('value', i);
|
|
|
115 |
|
|
|
116 |
// Add event handlers (first time only).
|
|
|
117 |
if (!M.availability_relativedate.form.addedEvents) {
|
|
|
118 |
M.availability_relativedate.form.addedEvents = true;
|
|
|
119 |
var root = Y.one('.availability-field');
|
|
|
120 |
var updateForm = function(input) {
|
|
|
121 |
var ancestorNode = input.ancestor('span.availability_relativedate');
|
|
|
122 |
var op = ancestorNode.one('select[name=relativestart]');
|
|
|
123 |
if (op.get('value') == '7') {
|
|
|
124 |
ancestorNode.one('select[name=relativecoursemodule]').set('style', '');
|
|
|
125 |
} else {
|
|
|
126 |
ancestorNode.one('select[name=relativecoursemodule]').set('style', 'display: none;');
|
|
|
127 |
}
|
|
|
128 |
M.core_availability.form.update();
|
|
|
129 |
};
|
|
|
130 |
|
|
|
131 |
root.delegate('change', function() {
|
|
|
132 |
updateForm(this);
|
|
|
133 |
}, '.availability_relativedate select');
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
return node;
|
|
|
137 |
};
|
|
|
138 |
|
|
|
139 |
M.availability_relativedate.form.fillValue = function(value, node) {
|
|
|
140 |
value.n = Number(node.one('select[name=relativenumber]').get('value'));
|
|
|
141 |
value.d = Number(node.one('select[name=relativednw]').get('value'));
|
|
|
142 |
value.s = Number(node.one('select[name=relativestart]').get('value'));
|
|
|
143 |
value.m = 0;
|
|
|
144 |
if (value.s == 7) {
|
|
|
145 |
value.m = Number(node.one('select[name=relativecoursemodule]').get('value'));
|
|
|
146 |
}
|
|
|
147 |
};
|
|
|
148 |
|
|
|
149 |
M.availability_relativedate.form.fillErrors = function(errors, node) {
|
|
|
150 |
var value = {};
|
|
|
151 |
this.fillValue(value, node);
|
|
|
152 |
};
|