~azzar1/unity/add-show-desktop-key

1099.1.213 by Nick Chadwick
Modifief exercise view, so that exercises can now be viewed in an
1
/* IVLE - Informatics Virtual Learning Environment
2
 * Copyright (C) 2007-2009 The University of Melbourne
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 2 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
 *
18
 * Author: Nick Chadwick
19
 */
20
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
21
/* Show and hide the given page element */
1099.1.214 by Nick Chadwick
The skeleton for editing an exercise has now been created. The next
22
function tog(something)
23
{
1099.7.1 by William Grant
Toggle things in exercise admin with slideToggle.
24
  $('#' + something).slideToggle("slow");
1099.1.213 by Nick Chadwick
Modifief exercise view, so that exercises can now be viewed in an
25
}
1099.1.216 by Nick Chadwick
Started adding in add and save options in the exercise edit view, to
26
1099.1.229 by Nick Chadwick
Fixed a slight oversight, which meant there was no way to add a new
27
function add_exercise()
28
{
29
    var exercise_id = $('#exercise_id').val();
30
    var exercise_name = $('#exercise_name').val();
31
    var exercise_num_rows = $('#exercise_num_rows').val();
32
    var exercise_desc = $('#exercise_desc').val();
33
    var exercise_partial = $('#exercise_partial').val();
34
    var exercise_solution = $('#exercise_solution').val();
35
    var exercise_include = $('#exercise_include').val();
36
37
    var callback = function(xhr)
38
    {
39
        var testresponse;
40
        try
41
        {
42
            testresponse = JSON.parse(xhr.responseText);
43
            alert('Exercise Created');
44
            window.location = '/+exercises';
45
        }
46
        catch (ex)
47
        {
48
            alert('Error: Could not add exercise');
49
            return;
50
        }
51
    }
52
    
53
    update_path = "api/+exercises";
54
    
55
    var args = {'identifier': exercise_id , 'name': exercise_name, 
56
                'description': exercise_desc, 'partial': exercise_partial,
57
                'solution': exercise_solution, 'include': exercise_include,
58
                'num_rows': exercise_num_rows, 'ivle.op': 'add_exercise'};
59
    ajax_call(callback, update_path, "", args, 'POST');
60
    
61
}
62
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
63
/* Edit the exercise values */
64
function edit_exercise()
1099.1.216 by Nick Chadwick
Started adding in add and save options in the exercise edit view, to
65
{
66
67
    var exercise_id = $('#exercise_id').val();
68
    var exercise_name = $('#exercise_name').val();
69
    var exercise_num_rows = $('#exercise_num_rows').val();
1099.1.221 by Nick Chadwick
added in extra parts to the exercise edit view. Now almost all
70
    var exercise_desc = $('#exercise_desc').val();
1099.1.216 by Nick Chadwick
Started adding in add and save options in the exercise edit view, to
71
    var exercise_partial = $('#exercise_partial').val();
72
    var exercise_solution = $('#exercise_solution').val();
73
    var exercise_include = $('#exercise_include').val();
74
    
75
    var callback = function(xhr)
76
    {
77
        var testresponse;
78
        try
79
        {
80
            testresponse = JSON.parse(xhr.responseText);
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
81
            alert('Exercise Saved');
1099.1.216 by Nick Chadwick
Started adding in add and save options in the exercise edit view, to
82
            return;
83
        }
84
        catch(ex)
85
        {
86
            alert('Error updating Exercise');
87
            return;
88
        }
89
    }
90
    
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
91
    update_path = "api/+exercises/" + exercise;
1099.1.216 by Nick Chadwick
Started adding in add and save options in the exercise edit view, to
92
    
93
    var args = {'name': exercise_name, 'description': exercise_desc, 
94
                'partial': exercise_partial, 'solution': exercise_solution,
95
                'include': exercise_include, 'num_rows': exercise_num_rows,
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
96
                'ivle.op': 'edit_exercise'};
97
    ajax_call(callback, update_path, "", args, 'POST');
98
}
99
1099.6.4 by Nick Chadwick
Exercise UI is now ready to be merged into trunk.
100
/* Modify, add and delete suites */
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
101
function edit_suite(suiteid)
102
{
1099.7.8 by William Grant
Suite editing widgets are templated.
103
    var desc = $('#test_suite_' + suiteid + '_description').val();
104
    var func = $('#test_suite_' + suiteid + '_function').val();
105
    var stdin = $('#test_suite_' + suiteid + '_stdin').val();
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
106
    
107
    var callback = function(xhr)
108
    {
109
        var testresponse;
110
        try
111
        {
112
            testresponse = JSON.parse(xhr.responseText);
113
            alert('Suite Saved');
114
            window.location.reload();
115
            return;
116
        }
117
        catch(ex)
118
        {
119
            alert('Error Saving Test Suite');
120
            return;
121
        }
122
    }
123
    
124
    var args = {'ivle.op': 'edit_suite', 'description': desc, 'function': func,
125
                'stdin': stdin, 'suiteid': suiteid};
126
    update_path = "api/+exercises/" + exercise;
127
    ajax_call(callback, update_path, "", args, 'POST');
128
}
129
130
function add_suite()
131
{
1428.1.1 by William Grant
Rename new_test_suite_* to test_suite_new_*, so the scheme matches existing test suites.
132
    var desc = $('#test_suite_new_description').val();
133
    var func = $('#test_suite_new_function').val();
134
    var stdin = $('#test_suite_new_stdin').val();
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
135
    
136
    var callback = function(xhr)
137
    {
138
        var testresponse;
139
        try
140
        {
141
            testresponse = JSON.parse(xhr.responseText);
142
            alert('Suite Created Sucessfully');
143
            window.location.reload();
144
            return;
145
        }
146
        catch(ex)
147
        {
148
            alert('Error Creating Test Suite');
149
        }
150
    }
151
    
152
    var args = {'ivle.op': 'add_suite', 'description': desc, 'function': func,
153
                'stdin': stdin};
154
    update_path = "api/+exercises/" + exercise;
155
    ajax_call(callback, update_path, "", args, 'POST');
156
}
157
1099.6.4 by Nick Chadwick
Exercise UI is now ready to be merged into trunk.
158
function delete_suite(suiteid)
159
{
160
    var callback = function(xhr)
161
    {
162
        var testresponse;
163
        try
164
        {
165
            testresponse = JSON.parse(xhr.responseText);
166
            alert('Suite Deleted.');
167
            window.location.reload()
168
            return;
169
        }
170
        catch (ex)
171
        {
172
            alert('Could not delete suite.');
173
        }
174
        
175
    }
176
    
177
    var args = {'ivle.op': 'delete_suite', 'suiteid': suiteid}
178
    update_path = "api/+exercises/" + exercise;
179
    ajax_call(callback, update_path, "", args, 'POST');
180
}
181
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
182
/* Modify and add Variables */
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
183
function edit_var(varid, suiteid)
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
184
{
1409 by William Grant
Unbreak variable adding and editing.
185
    var var_name = $('#var_name_' + varid).val();
186
    var var_val = $('#var_val_' + varid).val();
187
    var var_type = $('#var_type_' + varid).val();
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
188
    var argno = $('#var_argno_' + varid).val();
189
190
    var callback = function(xhr)
191
    {
192
        var testresponse;
193
        try
194
        {
195
            testresponse = JSON.parse(xhr.responseText);
196
            alert('Variable Added Sucessfully');
197
            window.location.reload();
198
            return;
199
        }
200
        catch(ex)
201
        {
202
            alert('Error Creating Test Suite');
203
            return;
204
        }
205
    }
206
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
207
    var args = {'ivle.op': 'edit_var', 'var_name': var_name,
208
                'var_val': var_val, 'var_type': var_type, 'argno': argno,
209
                'varid': varid, 'suiteid': suiteid};
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
210
    
211
    update_path = "api/+exercises/" + exercise;
212
    ajax_call(callback, update_path, "", args, 'POST')
213
214
}
215
216
function add_var(suiteid)
217
{
1099.1.242 by Nick Chadwick
Fixed a problem with exercise editor, which wasn't editing or adding
218
    var var_name = $('#new_var_name_' + suiteid).val();
219
    var var_val = $('#new_var_val_' + suiteid).val();
220
    var var_type = $('#new_var_type_' + suiteid).val();
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
221
    var argno = $('#new_var_argno_' + suiteid).val();
222
    
223
    var callback = function(xhr)
224
    {
225
        var testresponse;
226
        try
227
        {
228
            testresponse = JSON.parse(xhr.responseText);
229
            alert('Variable Added Sucessfully');
230
            window.location.reload();
231
            return;
232
        }
233
        catch(ex)
234
        {
1099.6.4 by Nick Chadwick
Exercise UI is now ready to be merged into trunk.
235
            alert('Error Adding Variable.');
1099.1.217 by Nick Chadwick
working on making the exercise editor complete
236
            return;
237
        }
238
    }
239
    
240
    var args = {'ivle.op': 'add_var', 'var_name': var_name, 
241
                'var_val': var_val, 'var_type': var_type, 
242
                'argno': argno, 'suiteid': suiteid};
243
    update_path = "api/+exercises/" + exercise;
1099.1.216 by Nick Chadwick
Started adding in add and save options in the exercise edit view, to
244
    ajax_call(callback, update_path, "", args, 'POST');
245
}
1099.1.221 by Nick Chadwick
added in extra parts to the exercise edit view. Now almost all
246
1099.6.4 by Nick Chadwick
Exercise UI is now ready to be merged into trunk.
247
function delete_var(varid, suiteid)
248
{
249
    var callback = function(xhr)
250
    {
251
        var testresponse;
252
        try
253
        {
254
            testresponse = JSON.parse(xhr.responseText);
255
            alert('Variable Deleted.');
256
            window.location.reload();
257
            return;
258
        }
259
        catch(ex)
260
        {
261
            alert('Error Deleting Variable');
262
            return;
263
        }
264
    }
265
    var args = {'ivle.op': 'delete_var', 'suiteid': suiteid, 'varid': varid};
266
    update_path = "api/+exercises/" + exercise;
267
    ajax_call(callback, update_path, "", args, 'POST');
268
269
}
270
1099.1.221 by Nick Chadwick
added in extra parts to the exercise edit view. Now almost all
271
/* Add and edit test case parts */
272
273
function add_test_case(suiteid)
274
{
1099.7.9 by William Grant
Test case addition/editing widgets are templated.
275
    var passmsg = $("#new_test_case_" + suiteid + "_pass").val();
276
    var failmsg = $("#new_test_case_" + suiteid + "_fail").val();
1099.1.221 by Nick Chadwick
added in extra parts to the exercise edit view. Now almost all
277
    
278
    var callback = function(xhr)
279
    {
280
        var testresponse;
281
        try
282
        {
283
            testresponse = JSON.parse(xhr.responseText);
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
284
            alert('Test Case Added Sucessfully');
1099.1.221 by Nick Chadwick
added in extra parts to the exercise edit view. Now almost all
285
            window.location.reload();
286
            return;
287
        }
288
        catch(ex)
289
        {
290
            alert('Error Creating Test Suite');
291
            return;
292
        }
293
    }
294
    
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
295
    var args = {'ivle.op': 'add_testcase', 'passmsg': passmsg, 
1394.1.5 by William Grant
Drop TestSuite file match default from the UI -- we don't support file tests any more.
296
                'failmsg': failmsg, 'suiteid': suiteid};
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
297
    update_path = "api/+exercises/" + exercise;
298
    ajax_call(callback, update_path, "", args, 'POST');
299
    
300
}
301
302
function edit_test_case(testid, suiteid)
303
{
1099.7.9 by William Grant
Test case addition/editing widgets are templated.
304
    var passmsg = $("#test_case_" + testid + "_" + suiteid + "_pass").val();
305
    var failmsg = $("#test_case_" + testid + "_" + suiteid + "_fail").val();
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
306
    
307
    var callback = function(xhr)
308
    {
309
        var testresponse;
310
        try
311
        {
312
            testresponse = JSON.parse(xhr.responseText);
313
            alert('Test Case Modified Sucessfully');
314
            return;
315
        }
316
        catch(ex)
317
        {
318
            alert('Error Saving Test Case');
319
            return;
320
        }
321
    }
322
    
323
    var args = {'ivle.op': 'edit_testcase', 'passmsg': passmsg, 
1394.1.5 by William Grant
Drop TestSuite file match default from the UI -- we don't support file tests any more.
324
                'failmsg': failmsg, 'testid':testid, 'suiteid': suiteid};
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
325
    update_path = "api/+exercises/" + exercise;
326
    ajax_call(callback, update_path, "", args, 'POST');
327
}
328
1099.6.4 by Nick Chadwick
Exercise UI is now ready to be merged into trunk.
329
function delete_testcase(testid, suiteid)
330
{
331
    var callback = function(xhr)
332
    {
333
        var testresponse;
334
        try
335
        {
336
            testresponse = JSON.parse(xhr.responseText);
337
            alert('Test Case Deleted.');
338
            window.location.reload();
339
            return;
340
        }
341
        catch(ex)
342
        {
343
            alert('Error Deleting Test Case');
344
            return;
345
        }
346
    }
347
    
348
    var args = {'ivle.op': 'delete_testcase', 'testid': testid, 
349
                'suiteid': suiteid};
350
    update_path = "api/+exercises/" + exercise;
351
    ajax_call(callback, update_path, "", args, 'POST');
352
}
353
354
/* Functions to add, edit, and delete test case parts */
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
355
function edit_test_part(partid, testid, suiteid)
356
{
1099.7.14 by William Grant
Template out bits of the TestCasePart editor.
357
    var part_type = $("#test_part_" + partid + "_part_type").val();
1420 by William Grant
Redo the exercise test part type (check/norm) selection, with radio buttons. Also add an exact match option, as yet unfunctional.
358
    var test_type = $("input[name='test_part_" + partid + "_test_type']:checked").val();
1099.7.14 by William Grant
Template out bits of the TestCasePart editor.
359
    var data = $("#test_part_" + partid + "_data").val();
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
360
    
361
    var callback = function(xhr)
362
    {
363
        var testresponse;
364
        try
365
        {
366
            testresponse = JSON.parse(xhr.responseText);
367
            alert("Test Part Modified");
368
        }
369
        catch (ex)
370
        {
371
            alert("Error Adding Test Part");
372
            return;
373
        }
374
    }
375
    
376
    var args = {'ivle.op': 'edit_testpart', 'part_type': part_type, 
1394.1.7 by William Grant
Drop TestCasePart filename UI.
377
                'test_type': test_type, 'data': data, 'partid': partid,
378
                'testid': testid, 'suiteid': suiteid};
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
379
    update_path = "api/+exercises/" + exercise;
380
    ajax_call(callback, update_path, "", args, 'POST');
381
}
382
383
function add_test_part(testid, suiteid)
384
{
1099.7.38 by William Grant
Template bits of the test case part addition form.
385
    var part_type = $("#test_part_new_part_type_" + testid).val();
1420 by William Grant
Redo the exercise test part type (check/norm) selection, with radio buttons. Also add an exact match option, as yet unfunctional.
386
    var test_type = $("input[name='test_part_new_" + testid + "_test_type']:checked").val();
1428 by William Grant
Enable, disable, and set default code in the test part code widget when the test type is changed.
387
    var data = $("#test_part_new_" + testid + "_data").val();
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
388
389
    var savebutton = $("#new_test_part_save_" + testid);
390
    savebutton.attr('value', 'Saving...');
391
    savebutton.attr('disabled', 'disabled');
392
    
393
    var callback = function(xhr)
394
    {
395
        var testresponse;
396
        var test_part_id;
397
        var test_parts = $("#test_case_parts_" + testid);
398
        try
399
        {
400
            testresponse = JSON.parse(xhr.responseText);
401
            savebutton.attr('value', 'Saving...');
402
            savebutton.removeAttr('disabled');
403
            
404
            alert("Test Part Added");
405
            window.location.reload();
406
            return;
407
        }
408
        catch (ex)
409
        {
410
            alert("Error Adding Test Part");
411
            return;
412
        }
413
    }
414
    
415
    var args = {'ivle.op': 'add_testpart', 'part_type': part_type, 
1394.1.7 by William Grant
Drop TestCasePart filename UI.
416
                'test_type': test_type, 'data': data, 'testid': testid,
417
                'suiteid': suiteid};
1099.6.1 by Nick Chadwick
Exercise-ui is now able to create an entire exercise.
418
    update_path = "api/+exercises/" + exercise;
419
    ajax_call(callback, update_path, "", args, 'POST');
420
}
421
1099.6.4 by Nick Chadwick
Exercise UI is now ready to be merged into trunk.
422
function delete_testpart(partid, testid, suiteid)
423
{
424
    var callback = function(xhr)
425
    {
426
        try
427
        {
428
            testresponse = JSON.parse(xhr.responseText);
429
            alert("Test Part Deleted.");
430
            window.location.reload();
431
            return;
432
        }
433
        catch (ex)
434
        {
435
            alert("Error Deleting Test Part");
436
            return;
437
        }
438
    }
439
    
440
    var args = {'ivle.op': 'delete_testpart', 'partid': partid, 
441
                'testid': testid, 'suiteid': suiteid};
442
    update_path = "api/+exercises/" + exercise;
443
    ajax_call(callback, update_path, "", args, 'POST');
444
}
1428 by William Grant
Enable, disable, and set default code in the test part code widget when the test type is changed.
445
446
function enable_test_part_function(partid, which)
447
{
448
    var elem = $("#test_part_" + partid + "_data");
449
    if (which)
450
        elem.removeAttr("disabled");
451
    else
452
        elem.attr("disabled", "disabled");
453
}
454
455
/* Set a test part's code to a reasonable default. This clobbers any
456
 * code that is present.
457
 */
458
function set_test_part_function(partid, test_type)
459
{
460
    var defaults = new Object();
461
    defaults.match = "";
462
    defaults.norm = "lambda x: x";
463
    defaults.check = "lambda solution, attempt: solution == attempt";
464
465
    $("#test_part_" + partid + "_data").text(defaults[test_type]);
466
}
467
468
/* When a test part's test type (norm/check/match) is changed, enable
469
 * or disable the code textarea and set some example code.
470
 */
471
function test_part_type_changed(partid)
472
{
473
    var name = "test_part_" + partid + "_test_type";
474
    var enable = true;
475
    var test_type = $("input[name='" + name + "']:checked").val();
476
    var sample_code = "";
477
478
    if (test_type == "match")
479
        enable = false;
480
481
    enable_test_part_function(partid, enable);
482
    set_test_part_function(partid, test_type);
483
};
1428.1.2 by William Grant
Make the test suite function widget a little nicer, by adding a checkbox and more description.
484
1428.1.3 by William Grant
Apply the function input changes to the stdin input, and add a linebreak after the 'Description' label.
485
/* When a test suite attribute checkbox is toggled, enable or disable
486
 * and clear the textbox.
1428.1.2 by William Grant
Make the test suite function widget a little nicer, by adding a checkbox and more description.
487
 */
1428.1.3 by William Grant
Apply the function input changes to the stdin input, and add a linebreak after the 'Description' label.
488
function test_suite_checkbox_toggled(which, suiteid)
1428.1.2 by William Grant
Make the test suite function widget a little nicer, by adding a checkbox and more description.
489
{
1428.1.3 by William Grant
Apply the function input changes to the stdin input, and add a linebreak after the 'Description' label.
490
    var name = "test_suite_" + suiteid + "_" + which;
1428.1.2 by William Grant
Make the test suite function widget a little nicer, by adding a checkbox and more description.
491
    var textbox_elem = $("#" + name);
492
493
    if ($("#" + name + "_enabled").is(":checked"))
494
        textbox_elem.removeAttr("disabled")
495
    else
496
    {
497
        textbox_elem.attr("disabled", "disabled");
498
        textbox_elem.val("");
499
    }
500
};