forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.php
413 lines (357 loc) · 16.6 KB
/
index.php
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?PHP // $Id$
// This page lists all the instances of hotpot in a particular course
require_once("../../config.php");
require_once("../../course/lib.php");
require_once("lib.php");
$id = required_param('id', PARAM_INT); // course
if (!$course = $DB->get_record('course', array('id'=>$id))) {
print_error('invalidcourseid');
}
require_login($course->id);
$coursecontext = get_context_instance(CONTEXT_COURSE, $id);
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
add_to_log($course->id, "hotpot", "view all", "index.php?id=$course->id", "");
// Moodle 1.4+ requires sesskey to be passed in forms
if (isset($USER->sesskey)) {
$sesskey = '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'" />';
} else {
$sesskey = '';
}
// get message strings for titles
$strmodulenameplural = get_string("modulenameplural", "hotpot");
$strmodulename = get_string("modulename", "hotpot");
// string translation array for single and double quotes
$quotes = array("'"=>"\'", '"'=>'"');
// Print the header
$title = format_string($course->shortname) . ": $strmodulenameplural";
$heading = $course->fullname;
$navlinks = array();
$navlinks[] = array('name' => $strmodulenameplural, 'link' => '', 'type' => 'activity');
$navigation = build_navigation($navlinks);
print_header($title, $heading, $navigation, "", "", true, "", navmenu($course));
$next_url = "$CFG->wwwroot/course/view.php?id=$course->id";
// get display section, if any
$section = optional_param('section', 0, PARAM_ALPHANUM);
if ($section=='all') {
// do nothing
} else {
$section = intval($section);
}
if ($section) {
$displaysection = course_set_display($course->id, $section);
} else {
if (isset($USER->display[$course->id])) {
$displaysection = $USER->display[$course->id];
} else {
$displaysection = 0;
}
}
// Get all hotpot instances in this course
$hotpots = array();
if ($hotpot_instances = hotpot_get_all_instances_in_course('hotpot', $course)) {
foreach ($hotpot_instances as $hotpot_instance) {
if ($displaysection>0 && $hotpot_instance->section>0 && $displaysection<>$hotpot_instance->section) {
// do nothing (user is not diplaying this section)
} else {
$hotpots[$hotpot_instance->id] = $hotpot_instance;
}
}
}
if (empty($hotpots)) {
notice(get_string('thereareno', 'moodle', $strmodulenameplural), $next_url);
exit;
}
$hotpotids = implode(',', array_keys($hotpots));
if (has_capability('mod/hotpot:grade', $sitecontext)) {
// array of hotpots to be regraded
$regrade_hotpots = array();
// do we need to regrade any or all of the hotpots?
$regrade = optional_param('regrade', 0, PARAM_SEQUENCE);
if ($regrade) {
// add valid hotpot ids to the regrade array
$regrade = explode(',', $regrade);
foreach ($regrade as $id) {
if (isset($hotpots[$id])) {
$regrade_hotpots[$id] = &$hotpots[$id];
}
}
$regrade = implode(',', array_keys($regrade_hotpots));
}
if ($regrade) {
$confirm = optional_param('confirm', 0, PARAM_BOOL);
if (!$confirm) {
print_simple_box_start("center", "60%", "#FFAAAA", 20, "noticebox");
if (count($regrade_hotpots)==1) {
print_heading(get_string('regradecheck', 'hotpot', $regrade_hotpots[$regrade]->name));
} else {
print_heading(get_string('regradecheck', 'hotpot', ''));
print '<ul>';
foreach ($regrade_hotpots as $hotpot) {
print "<li>$hotpot->name</li>";
}
print '</ul>';
}
print ''
. '<div align="center"><table border="0"><tr><td>'
. '<form target="_parent" method="post" action="'.$ME.'">'
. '<input type="hidden" name="id" value="'.$course->id.'" />'
. '<input type="hidden" name="regrade" value="'.$regrade.'" />'
. '<input type="hidden" name="confirm" value="1" />'
. $sesskey
. '<input type="submit" value="'.get_string("yes").'" />'
. '</form>'
. '</td><td> </td><td>'
. '<form target="_parent" method="post" action="'.$ME.'">'
. '<input type="hidden" name="id" value="'.$course->id.'" />'
. $sesskey
. '<input type="submit" value="'.get_string("no").'" />'
. '</form>'
. '</td></tr></table></div>'
;
print_simple_box_end();
print_footer($course);
exit;
} else { // regrade has been confirmed, so proceed
// start hotpot counter and timer
$hotpotstart = microtime();
$hotpotcount = 0;
// regrade attempts for these hotpots
foreach ($regrade_hotpots as $hotpot) {
notify("<b>$hotpot->name</b>");
// delete questions and responses for this hotpot
if ($records = $DB->get_records('hotpot_questions', array('hotpot'=>$hotpot->id), '', 'id,hotpot')) {
$questionids = implode(',', array_keys($records));
hotpot_delete_and_notify('hotpot_questions', "id IN ($questionids)", array(), get_string('question', 'quiz'));
hotpot_delete_and_notify('hotpot_responses', "question IN ($questionids)", array(), get_string('answer', 'quiz'));
}
// start attempt counter and timer
$attemptstart = microtime();
$attemptcount = 0;
// regrade attempts, if any, for this hotpot
if ($attempts = $DB->get_records('hotpot_attempts', array('hotpot'=>$hotpot->id))) {
foreach ($attempts as $attempt) {
$attempt->details = $DB->get_field('hotpot_details', 'details', array('attempt'=>$attempt->id));
if ($attempt->details) {
hotpot_add_attempt_details($attempt);
if (! $DB->update_record('hotpot_attempts', $attempt)) {
print_error('cannotupdateattempt', 'hotpot', $next_url, $DB->get_last_error());
}
}
$attemptcount++;
}
}
if ($attemptcount) {
$msg = get_string('added', 'moodle', "$attemptcount x ".get_string('attempts', 'quiz'));
if (!empty($CFG->hotpot_showtimes)) {
$msg .= ' ('.format_time(sprintf("%0.2f", microtime_diff($attemptstart, microtime()))).')';
}
notify($msg);
}
$hotpotcount++;
} // end foreach $hotpots
if ($hotpotcount) {
$msg = get_string('regrade', 'quiz').": $hotpotcount x ".get_string('modulenameplural', 'hotpot');
if (!empty($CFG->hotpot_showtimes)) {
$msg .= ' ('.format_time(sprintf("%0.2f", microtime_diff($hotpotstart, microtime()))).')';
}
notify($msg);
}
notify(get_string('regradecomplete', 'quiz'));
} // end if $confirm
} // end regrade
// get duplicate hotpot-name questions
// - JMatch LHS is longer than 255 bytes
// - JQuiz question text is longer than 255 bytes
// - other unidentified situations ?!
$regrade_hotpots = array();
$concat_field = $DB->sql_concat('hotpot', "'_'", 'name');
if ($concat_field) {
$records = $DB->get_records_sql("
SELECT $concat_field, COUNT(*), hotpot, name
FROM {hotpot_questions}
WHERE hotpot IN ($hotpotids)
GROUP BY hotpot, name
HAVING COUNT(*) >1
");
if ($records) {
foreach ($records as $record) {
$regrade_hotpots[$record->hotpot] = 1;
}
ksort($regrade_hotpots);
$regrade_hotpots = array_keys($regrade_hotpots);
}
}
}
// start timer
$start = microtime();
// get total number of attempts, users and details for these hotpots
$params = array();
$tables = "{hotpot_attempts} a";
$fields = "
a.hotpot AS hotpot,
COUNT(DISTINCT a.clickreportid) AS attemptcount,
COUNT(DISTINCT a.userid) AS usercount,
MAX(a.score) AS maxscore
";
$select = "a.hotpot IN ($hotpotids)";
if (has_capability('mod/hotpot:viewreport', $coursecontext)) {
// do nothing (=get all users)
} else {
// restrict results to this user only
$select .= " AND a.userid=:userid";
$params['userid'] = $USER->id;
}
$usejoin = 0;
if (has_capability('mod/hotpot:grade', get_context_instance(CONTEXT_SYSTEM)) && $usejoin) {
// join attempts table and details table
$tables .= ",{hotpot_details} d";
$fields .= ',COUNT(DISTINCT d.id) AS detailcount';
$select .= " AND a.id=d.attempt";
// this may take about twice as long as getting the gradecounts separately :-(
// so this operation could be done after getting the $totals from the attempts table
}
$totals = $DB->get_records_sql("SELECT $fields FROM $tables WHERE $select GROUP BY a.hotpot", $params);
if (has_capability('mod/hotpot:grade', get_context_instance(CONTEXT_SYSTEM)) && empty($usejoin)) {
foreach ($hotpots as $hotpot) {
$totals[$hotpot->id]->detailcount = 0;
if ($ids = $DB->get_records('hotpot_attempts', array('hotpot'=>$hotpot->id))) {
$ids = join(',', array_keys($ids));
$totals[$hotpot->id]->detailcount = $DB->count_records_select('hotpot_details', "attempt IN ($ids)");
}
}
}
// message strings for main table
$strusers = get_string('users');
$strupdate = get_string('update');
$strregrade = get_string('regrade', 'hotpot');
$strneverclosed = get_string('neverclosed', 'hotpot');
$strregraderequired = get_string('regraderequired', 'hotpot');
// column headings and attributes
$table->head = array();
$table->align = array();
if (!empty($CFG->hotpot_showtimes)) {
print '<H3>'.sprintf("%0.3f", microtime_diff($start, microtime())).' secs'."</H3>\n";
}
switch ($course->format) {
case 'weeks' :
$title = get_string("week");
break;
case 'topics' :
$title = get_string("topic");
break;
default :
$title = '';
break;
}
if ($title) {
array_push($table->head, $title);
array_push($table->align, "center");
}
if (has_capability('moodle/course:manageactivities', $coursecontext)) {
array_push($table->head, $strupdate);
array_push($table->align, "center");
}
array_push($table->head,
get_string("name"),
get_string("quizcloses", "quiz"),
get_string("bestgrade", "quiz"),
get_string("attempts", "quiz")
);
array_push($table->align,
"left", "left", "center", "left"
);
if (has_capability('mod/hotpot:grade', $coursecontext)) {
array_push($table->head, $strregrade);
array_push($table->align, "center");
}
$currentsection = -1;
foreach ($hotpots as $hotpot) {
$printsection = "";
if ($hotpot->section != $currentsection) {
if ($hotpot->section) {
$printsection = $hotpot->section;
if ($course->format=='weeks' || $course->format=='topics') {
// Show the zoom boxes
if ($displaysection==$hotpot->section) {
$strshowall = get_string('showall'.$course->format);
$printsection .= '<br /><a href="index.php?id='.$course->id.'&section=all" title="'.$strshowall.'"><img src="'.$CFG->pixpath.'/i/all.gif" style="height:25px; width:16px; border:0px" alt="'.$strshowall.'" /></a><br />';
} else {
$strshowone = get_string('showonly'.preg_replace('|s$|', '', $course->format, 1), '', $hotpot->section);
$printsection .= '<br /><a href="index.php?id='.$course->id.'&section='.$hotpot->section.'" title="'.$strshowone.'"><img src="'.$CFG->pixpath.'/i/one.gif" class="icon" alt="'.$strshowone.'" /></a><br />';
}
}
}
if ($currentsection>=0) {
$table->data[] = 'hr';
}
$currentsection = $hotpot->section;
}
$class = ($hotpot->visible) ? '' : 'class="dimmed" ';
$quizname = '<a '.$class.'href="view.php?id='.$hotpot->coursemodule.'">'.$hotpot->name.'</a>';
$quizclose = empty($hotpot->timeclose) ? $strneverclosed : userdate($hotpot->timeclose);
// are there any totals for this hotpot?
if (empty($totals[$hotpot->id]->attemptcount)) {
$report = " ";
$bestscore = " ";
} else {
$cm = get_coursemodule_from_instance('hotpot', $hotpot->id);
// report number of attempts and users
$report = get_string("viewallreports","quiz", $totals[$hotpot->id]->attemptcount);
if (has_capability('mod/hotpot:viewreport', get_context_instance(CONTEXT_MODULE, $cm->id))) {
$report .= " (".$totals[$hotpot->id]->usercount." $strusers)";
}
$report = '<a href="report.php?hp='.$hotpot->id.'">'.$report.'</a>';
// get best score
if (is_numeric($totals[$hotpot->id]->maxscore)) {
$weighting = $hotpot->grade / 100;
$precision = hotpot_get_precision($hotpot);
$bestscore = round($totals[$hotpot->id]->maxscore * $weighting, $precision)." / $hotpot->grade";
} else {
$bestscore = " ";
}
}
if (has_capability('mod/hotpot:grade', $sitecontext)) {
if (in_array($hotpot->id, $regrade_hotpots)) {
$report .= ' <font color="red">'.$strregraderequired.'</font>';
}
}
$data = array ();
if ($course->format=="weeks" || $course->format=="topics") {
array_push($data, $printsection);
}
if (has_capability('moodle/course:manageactivities', $coursecontext)) {
$updatebutton = ''
. '<form '.$CFG->frametarget.' method="get" action="'.$CFG->wwwroot.'/course/mod.php">'
. '<input type="hidden" name="update" value="'.$hotpot->coursemodule.'" />'
. $sesskey
. '<input type="submit" value="'.$strupdate.'" />'
. '</form>'
;
array_push($data, $updatebutton);
}
array_push($data, $quizname, $quizclose, $bestscore, $report);
if (has_capability('mod/hotpot:grade', $sitecontext)) {
if (empty($totals[$hotpot->id]->detailcount)) {
// no details records for this hotpot, so disable regrade
$regradebutton = ' ';
} else {
$strregradecheck = get_string('regradecheck', 'hotpot', strtr($hotpot->name, $quotes));
$regradebutton = ''
. '<form target="_parent" method="post" action="'.$ME.'" onsubmit="var x=window.confirm('."'$strregradecheck'".');this.confirm.value=x;return x;">'
. '<input type="hidden" name="id" value="'.$course->id.'" />'
. '<input type="hidden" name="regrade" value="'.$hotpot->id.'" />'
. '<input type="hidden" name="confirm" value="" />'
. $sesskey
. '<input type="submit" value="'.$strregrade.'" />'
. '</form>'
;
}
array_push($data, $regradebutton);
}
$table->data[] = $data;
}
echo "<br />";
print_table($table);
// Finish the page
print_footer($course);
?>