~ubuntu-branches/ubuntu/natty/schooltool.gradebook/natty

« back to all changes in this revision

Viewing changes to src/schooltool/gradebook/browser/gradebook_overview.js.pt

  • Committer: Bazaar Package Importer
  • Author(s): Gediminas Paulauskas
  • Date: 2011-02-24 16:53:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110224165353-hi69ckyal3b8dyns
Tags: upstream-0.9.0~a1
ImportĀ upstreamĀ versionĀ 0.9.0~a1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<tal:tag condition="view/update" />
 
2
/* This is the Javascript to be included when rendering the gradebook overview. */
 
3
var numstudents = <tal:block replace="python: len(view.students)"/> 
 
4
var students = new Array(numstudents);
 
5
<tal:loop repeat="row view/table">
 
6
     students[<tal:block replace="repeat/row/index"/>] = '<tal:block replace="python: view.breakJSString(row['student']['id'])"/>';
 
7
     </tal:loop>
 
8
 
 
9
var numactivities = <tal:block replace="python: len(view.scorableActivities())"/> 
 
10
var activities = new Array(numactivities);
 
11
<tal:loop repeat="activity view/activities">
 
12
     activities[<tal:block replace="repeat/activity/index"/>] = '<tal:block replace="activity/hash"/>';
 
13
     </tal:loop>
 
14
 
 
15
var numscores = <tal:block replace="python: len(view.scores)"/>;
 
16
var scores = new Object();
 
17
<tal:loop repeat="activity view/scores">
 
18
    scores['<tal:block replace="activity" />'] = new Array(<tal:block replace="python: view.scores[activity]" />);
 
19
</tal:loop>
 
20
 
 
21
var numdescriptions = <tal:block replace="python: len(view.descriptions)"/> 
 
22
var descriptions = new Array(numdescriptions);
 
23
<tal:loop repeat="activity view/descriptions">
 
24
     descriptions['<tal:block replace="activity/act_hash"/>'] = '<tal:block replace="activity/description"/>';
 
25
     </tal:loop>
 
26
 
 
27
var edited = false;
 
28
var currentCell;
 
29
var currentDesc = '';
 
30
 
 
31
function setNotEdited()
 
32
{
 
33
    edited = false;
 
34
}
 
35
 
 
36
function checkChanges()
 
37
{
 
38
    if (!edited)
 
39
        return;
 
40
    warningText = '<tal:block replace="view/warningText" />';
 
41
    saveFlag = window.confirm(warningText);
 
42
    if (saveFlag == true)
 
43
        {
 
44
        button = document.getElementsByName('UPDATE_SUBMIT')[0];
 
45
        button.click();
 
46
        }
 
47
    else
 
48
        return true;
 
49
}
 
50
 
 
51
function onLoadHandler()
 
52
{
 
53
    // highlight error values
 
54
    for (a = 0; a < numactivities; a++)
 
55
    {
 
56
        activity = activities[a];
 
57
        for (s = 0; s < numstudents; s++)
 
58
        {
 
59
            name = activity + '_' + students[s];
 
60
            value = document.getElementById(name).value;
 
61
            setBackgroundColor(name, activity, value, true);
 
62
        }
 
63
    }
 
64
}
 
65
 
 
66
window.onload = onLoadHandler;
 
67
window.onunload = checkChanges;
 
68
 
 
69
 
 
70
function handleCellFocus(cell, activity)
 
71
{
 
72
    currentCell = cell;
 
73
    cell.select();
 
74
    setDescription(activity);
 
75
}
 
76
 
 
77
function setDescription(activity)
 
78
{
 
79
    desc = document.getElementById('description');
 
80
    currentDesc = descriptions[activity];
 
81
    desc.innerHTML = currentDesc;
 
82
}
 
83
 
 
84
function tempDescription(activity)
 
85
{
 
86
    desc = document.getElementById('description');
 
87
    desc.innerHTML = descriptions[activity];
 
88
}
 
89
 
 
90
function restoreDescription()
 
91
{
 
92
    desc = document.getElementById('description');
 
93
    desc.innerHTML = currentDesc;
 
94
}
 
95
 
 
96
function spreadsheetBehaviour(e)
 
97
{
 
98
    var keynum;
 
99
    if(window.event) // IE
 
100
    {
 
101
        keynum = e.keyCode;
 
102
    }
 
103
    else if(e.which) // Netscape/Firefox/Opera
 
104
    {
 
105
        keynum = e.which;
 
106
    }
 
107
 
 
108
    var my_name = currentCell.id;
 
109
    var s, a, done;
 
110
    done = new Boolean(false);
 
111
 
 
112
    for (s = 0; s != numstudents; s++)
 
113
    {
 
114
        for (a = 0; a != numactivities; a++)
 
115
        {
 
116
            try_name = activities[a] + '_' + students[s]
 
117
            if (try_name == my_name)
 
118
            {
 
119
                done = true;
 
120
                break;
 
121
            }
 
122
        }
 
123
        if (done == true)
 
124
            break;
 
125
    }
 
126
 
 
127
    var i_stayed_put = new Boolean(true);
 
128
    if (keynum == 37) // left arrow
 
129
    {
 
130
        if (a != 0) { a--; i_stayed_put = false;}
 
131
    }
 
132
    if (keynum == 39) // right arrow
 
133
    {
 
134
        if (a != numactivities - 1) {a++; i_stayed_put = false;}
 
135
    }
 
136
    if (keynum == 38) // up arrow
 
137
    {
 
138
        if (s != 0) {s--; i_stayed_put = false;}
 
139
    }
 
140
    if ((keynum == 40) || (keynum == 13)) // down arrow or enter
 
141
    {
 
142
        if (s != numstudents - 1) {s++; i_stayed_put = false;}
 
143
    }
 
144
 
 
145
    if (i_stayed_put == true)
 
146
        return true;
 
147
    var newname = activities[a] + '_' + students[s];
 
148
    var el = document.getElementsByName(newname)[0]
 
149
    el.focus();
 
150
    return false;
 
151
}
 
152
 
 
153
function checkValid(e, name)
 
154
{
 
155
    var activity = name.split('_')[0];
 
156
    if(activity == "fd")
 
157
        activity = name.split('_')[1];
 
158
    if (e == null)
 
159
        return true;
 
160
 
 
161
    var keynum;
 
162
    if(window.event) // IE
 
163
        { 
 
164
            keynum = e.keyCode;
 
165
        }
 
166
    else if(e.which) // Netscape/Firefox/Opera
 
167
        {
 
168
            keynum = e.which;
 
169
        }
 
170
    if (keynum < 48 || (keynum > 57 && keynum < 65) || (keynum > 90 && keynum < 97) || keynum > 122)
 
171
        {
 
172
            return true;
 
173
        }
 
174
 
 
175
    edited = true;
 
176
    var element = document.getElementById(name);
 
177
    var elementCell = document.getElementById(name+'_cell');
 
178
    var value = element.value;
 
179
 
 
180
    return setBackgroundColor(name, activity, value, false);
 
181
}
 
182
 
 
183
function setBackgroundColor(name, activity, value, errors_only)
 
184
{
 
185
    changeBackgroundColor(name+'_cell', 'default_bg');
 
186
 
 
187
    if (value == '')
 
188
        return true;
 
189
 
 
190
    // handle validation of discrete score system
 
191
    var actScores = scores[activity];
 
192
    if (actScores[0] == 'd')
 
193
    {
 
194
        for(var index in actScores)
 
195
        {
 
196
            if (index > 0 && value == actScores[index])
 
197
            {
 
198
                if (!errors_only)
 
199
                    changeBackgroundColor(name+'_cell', 'changed_bg');
 
200
                return true;
 
201
            }
 
202
        }  
 
203
        changeBackgroundColor(name+'_cell', 'error_bg');
 
204
        return false;   
 
205
    }
 
206
 
 
207
    // handle validation of ranged score system
 
208
    else
 
209
    {
 
210
        var min = parseInt(actScores[1]);
 
211
        var max = parseInt(actScores[2]);
 
212
        var intValue = parseInt(value);
 
213
        var regex = /[0-9]+$/;
 
214
        if (!value.match(regex) || intValue < min)
 
215
        {
 
216
            changeBackgroundColor(name+'_cell', 'error_bg');
 
217
            return false;   
 
218
        }
 
219
        if (errors_only)
 
220
            return true;
 
221
        if (intValue > max)
 
222
        {
 
223
            changeBackgroundColor(name+'_cell', 'warning_bg');
 
224
            return true;
 
225
        }
 
226
    }
 
227
 
 
228
    changeBackgroundColor(name+'_cell', 'changed_bg');
 
229
    return true;    
 
230
}
 
231
 
 
232
function performFillDown(activity) {
 
233
    var fd = document.getElementById('fd_'+activity);
 
234
    if (fd.value=='') return false;
 
235
    changeBackgroundColor('fd_'+activity+'_cell', 'default_bg');
 
236
    if (checkValid(null, 'fd_'+activity) == false)  {
 
237
        changeBackgroundColor('fd_'+activity+'_cell', 'warning_bg');
 
238
            return false;
 
239
        }
 
240
    for(j=0;j!=numstudents;j++) {
 
241
        name = activity+'_'+students[j];
 
242
        document.getElementById(name).value = fd.value;
 
243
        setBackgroundColor(name, activity, fd.value, false);
 
244
    }
 
245
    document.getElementById('fd_'+activity).value = '';
 
246
    document.getElementById('fdbtn_'+activity).style.display = 'none';
 
247
    changeBackgroundColor('fd_'+activity+'_cell', 'default_bg');
 
248
    edited = true;
 
249
}
 
250
 
 
251
function checkFillDown(activity)
 
252
{
 
253
    var fd = document.getElementById('fd_'+activity);
 
254
    if (checkValid(null, 'fd_'+activity))
 
255
        {
 
256
            var el = document.getElementById('fdbtn_'+activity);
 
257
            el.style.display='block';
 
258
            if (document.getElementById('fd_'+activity).value=='')  {
 
259
                changeBackgroundColor('fd_'+activity+'_cell', 'default_bg');
 
260
                el.style.display='none';
 
261
            }
 
262
        }
 
263
    else
 
264
        {
 
265
            document.getElementById('fdbtn_'+activity).style.display='none';
 
266
            if (document.getElementById('fd_'+activity).value=='')  {
 
267
                changeBackgroundColor('fd_'+activity+'_cell', 'default_bg');
 
268
                }
 
269
        }
 
270
}
 
271
 
 
272
function changeBackgroundColor(id, class)    {
 
273
    obj = document.getElementById(id);
 
274
    $(obj).removeClass('default_bg');
 
275
    $(obj).removeClass('changed_bg');
 
276
    $(obj).removeClass('warning_bg');
 
277
    $(obj).removeClass('error_bg');
 
278
    $(obj).addClass(class);
 
279
}