-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathscanning.js
180 lines (166 loc) · 6.68 KB
/
scanning.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
M.plagiarism_programming = M.plagiarism_programming || {}
M.plagiarism_programming = {
/*
* Entry function!
* Check the current status of the assignment.
* Status = pending => display button "Start scanning"
* Status = finished => display button "Rescan"
* Status = uploading, scanning, downloading: display the current progress of scanning
* Note that user can always navigate away while the scanning in progress.
* However, the scanning will continue. If users return to the page again,
* they will see the bar displayed if the scanning still proceed.
*/
initialise : function(Y, cmid, checkprogress) {
this.Y = Y;
Y.one('#plagiarism_programming_scan').on('click', function(e) {
e.preventDefault();
var time = new Date().getTime() % 100000000;
M.plagiarism_programming.initiate_scanning(cmid, time);
M.plagiarism_programming.monitor_scanning(cmid, time);
});
if (checkprogress.jplag || checkprogress.moss) {
M.plagiarism_programming.enable_scanning(false); //disable the scanning button
M.plagiarism_programming.monitor_scanning(cmid,null); //show the process of unfinished scanning
}
},
/**
* Display the progressbar at a progress for the tool
* @param progress a number between 0 and 100, indicating the percentage of the progress
* @param tool either 'jplag' or 'moss'
**/
display_progress : function(progress, tool) {
var Y = M.plagiarism_programming.Y;
if (!Y.one('#'+tool+'_tool')) {
return;
}
if (!M.plagiarism_programming['progressbar_'+tool]) {
// if not yet created, create the progress bar
M.plagiarism_programming['progressbar_'+tool] = new Y.ProgressBar({
contentBox: '#'+tool+'_tool',
width:'200px',
height:'10px',
minValue:0,
maxValue:100,
value:progress,
barColor: 'blue',
border: '1px solid black'
});
M.plagiarism_programming['progressbar_'+tool].render();
} else {
M.plagiarism_programming['progressbar_'+tool].set('value', progress);
}
},
/**
* Removing the progress bar
**/
remove_progress : function(tool) {
var Y = M.plagiarism_programming.Y;
if (M.plagiarism_programming['progressbar_'+tool]) {
M.plagiarism_programming['progressbar_'+tool].destroy();
M.plagiarism_programming['progressbar_'+tool] = null;
}
if (!Y.one('#'+tool+'_tool')) {
var node = Y.Node.create('<div id="'+tool+'_tool"></div>')
Y.one('#'+tool+'_status').get('parentNode').insert(node, 'after');
}
},
/*
* Called when button clicked
* If the scanning is in progress, set up the timer and the progress bar
* to monitor upload, scanning, download, finish
*/
initiate_scanning : function(cmid, time) {
var Y = this.Y;
M.plagiarism_programming.enable_scanning(false);
// show the progress bar
M.plagiarism_programming.display_progress(0,'jplag');
M.plagiarism_programming.display_progress(0,'moss');
// make the request for scanning
Y.io(M.cfg.wwwroot+'/plagiarism/programming/start_scanning.php', {
method: 'POST',
data: {
cmid: cmid,
task: 'scan',
time: time
}
});
},
/**
* Check the status of the scanning on server and display the progress in the progress bar
* @param cmid: the course module id
* @param time: the time initiating the scanning (optional) - this parameter is just to synchronise the time
**/
monitor_scanning : function(cmid, time) {
var Y = M.plagiarism_programming.Y;
time = (time) ? time: 0;
Y.io(M.cfg.wwwroot+'/plagiarism/programming/start_scanning.php', {
method: 'GET',
data: {
cmid: cmid,
task: 'check',
time: time
},
arguments: [cmid, time],
on: {
success: M.plagiarism_programming.display_monitor_info
}
});
},
/**
* Display the progress information. This is the callback function of monitor_progress
* @param id: ajax request id (not used)
* @param o: the response
**/
display_monitor_info : function(id, o, arguments) {
var Y = M.plagiarism_programming.Y;
var status = Y.JSON.parse(o.responseText);
var finished = true;
for (var tool in status) {
var stage = Y.one('#'+tool+'_status');
stage.setContent(M.plagiarism_programming.convert_status_message(status[tool].stage));
M.plagiarism_programming.display_progress(1*status[tool].progress, tool);
if (status[tool].stage!='finished' && status[tool].stage!='error') {
finished = false;
} else if (status[tool].link) {
stage.setContent(status[tool].link);
} else if (status[tool].stage=='error') {
stage.setContent('Error: '+status[tool].message);
}
if (status[tool].stage=='finished' || status[tool].stage=='error') {
M.plagiarism_programming.remove_progress(tool);
}
}
if (!finished) {
setTimeout('M.plagiarism_programming.monitor_scanning('+arguments[0]+','+arguments[1]+')',2000);
} else {
M.plagiarism_programming.enable_scanning(true);
}
},
/**
* Convert status to an informative message. The message are passed from the code
**/
convert_status_message : function(status) {
if (status=='pending' || status=='uploading' || status=='scanning' || status=='downloading') {
return M.str.plagiarism_programming[status];
} else {
return status;
}
},
/**
* Enable/disable the scanning button. The button should be disabled when scanning is in progress
* or cannot be performed
**/
enable_scanning : function(is_on) {
var Y = M.plagiarism_programming.Y;
var button = Y.one('#plagiarism_programming_scan');
var message = Y.one('#scan_message');
if (is_on) { // turn scanning on
button.set('disabled', false);
button.set('value', 'Rescan');
message.hide();
} else {
button.set('disabled', true);
message.show();
}
}
}