Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / quiz / view.php
blobafb5ce99875f67cd1093206d300ce6537f83911f
1 <?php // $Id$
3 // This page prints a particular instance of quiz
5 require_once("../../config.php");
6 require_once("locallib.php");
7 require_once($CFG->libdir.'/blocklib.php');
8 require_once('pagelib.php');
10 if (!empty($THEME->customcorners)) {
11 require_once($CFG->dirroot.'/lib/custom_corners_lib.php');
14 $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or
15 $q = optional_param('q', 0, PARAM_INT); // quiz ID
16 $edit = optional_param('edit', -1, PARAM_BOOL);
18 if ($id) {
19 if (! $cm = get_coursemodule_from_id('quiz', $id)) {
20 error("There is no coursemodule with id $id");
22 if (! $course = get_record("course", "id", $cm->course)) {
23 error("Course is misconfigured");
25 if (! $quiz = get_record("quiz", "id", $cm->instance)) {
26 error("The quiz with id $cm->instance corresponding to this coursemodule $id is missing");
28 } else {
29 if (! $quiz = get_record("quiz", "id", $q)) {
30 error("There is no quiz with id $q");
32 if (! $course = get_record("course", "id", $quiz->course)) {
33 error("The course with id $quiz->course that the quiz with id $q belongs to is missing");
35 if (! $cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id)) {
36 error("The course module for the quiz with id $q is missing");
40 // Check login and get context.
41 require_login($course->id, false, $cm);
42 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
44 // if no questions have been set up yet redirect to edit.php
45 if (!$quiz->questions and has_capability('mod/quiz:manage', $context)) {
46 redirect('edit.php?cmid='.$cm->id);
49 add_to_log($course->id, "quiz", "view", "view.php?id=$cm->id", $quiz->id, $cm->id);
51 // Initialize $PAGE, compute blocks
52 $PAGE = page_create_instance($quiz->id);
53 $pageblocks = blocks_setup($PAGE);
54 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210);
56 // Print the page header
57 if ($edit != -1 and $PAGE->user_allowed_editing()) {
58 $USER->editing = $edit;
61 //only check pop ups if the user is not a teacher, and popup is set
63 $bodytags = (has_capability('mod/quiz:attempt', $context) && $quiz->popup)?'onload="popupchecker(\'' . get_string('popupblockerwarning', 'quiz') . '\');"':'';
64 $PAGE->print_header($course->shortname.': %fullname%','',$bodytags);
66 echo '<table id="layout-table"><tr>';
68 if(!empty($CFG->showblocksonmodpages) && (blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) {
69 echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
70 if (!empty($THEME->customcorners)) print_custom_corners_start();
71 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
72 if (!empty($THEME->customcorners)) print_custom_corners_end();
73 echo '</td>';
76 echo '<td id="middle-column">';
77 if (!empty($THEME->customcorners)) print_custom_corners_start();
79 // Print the main part of the page
81 // Print heading and tabs (if there is more than one).
82 $currenttab = 'info';
83 include('tabs.php');
85 // Print quiz name
87 print_heading(format_string($quiz->name));
89 if (has_capability('mod/quiz:view', $context)) {
91 // Print quiz description
92 if (trim(strip_tags($quiz->intro))) {
93 $formatoptions->noclean = true;
94 print_box(format_text($quiz->intro, FORMAT_MOODLE, $formatoptions), 'generalbox', 'intro');
97 echo '<div class="quizinfo">';
99 // Print information about number of attempts and grading method.
100 if ($quiz->attempts > 1) {
101 echo "<p>".get_string("attemptsallowed", "quiz").": $quiz->attempts</p>";
103 if ($quiz->attempts != 1) {
104 echo "<p>".get_string("grademethod", "quiz").": ".$QUIZ_GRADE_METHOD[$quiz->grademethod]."</p>";
107 // Print information about timings.
108 $timenow = time();
109 $available = ($quiz->timeopen < $timenow and ($timenow < $quiz->timeclose or !$quiz->timeclose));
110 if ($available) {
111 if ($quiz->timelimit) {
112 echo "<p>".get_string("quiztimelimit","quiz", format_time($quiz->timelimit * 60))."</p>";
114 quiz_view_dates($quiz);
115 } else if ($timenow < $quiz->timeopen) {
116 echo "<p>".get_string("quiznotavailable", "quiz", userdate($quiz->timeopen))."</p>";
117 } else {
118 echo "<p>".get_string("quizclosed", "quiz", userdate($quiz->timeclose))."</p>";
120 echo '</div>';
121 } else {
122 $available = false;
125 // Show number of attempts summary to those who can view reports.
126 if (has_capability('mod/quiz:viewreports', $context)) {
127 if ($a->attemptnum = count_records('quiz_attempts', 'quiz', $quiz->id, 'preview', 0)) {
128 $a->studentnum = count_records_select('quiz_attempts', "quiz = '$quiz->id' AND preview = '0'", 'COUNT(DISTINCT userid)');
129 $a->studentstring = $course->students;
131 notify("<a href=\"report.php?mode=overview&amp;id=$cm->id\">".get_string('numattempts', 'quiz', $a).'</a>');
135 // Guests can't do a quiz, so offer them a choice of logging in or going back.
137 // TODO, work out what to do about this under roles and permissions.
138 // You have to be logged in to do a quiz, because attempts are tied to
139 // userid, and so if guests were allowed to attempt quizzes, all guests
140 // would see all attempts, and it would be confusing.
142 // So for courses that allow guest access, it is good to offer people an easy
143 // way to log in at this point if they have got this far before logging in.
144 if (isguestuser()) {
145 $loginurl = $CFG->wwwroot.'/login/index.php';
146 if (!empty($CFG->loginhttps)) {
147 $loginurl = str_replace('http:','https:', $loginurl);
150 notice_yesno('<p>' . get_string('guestsno', 'quiz') . "</p>\n\n</p>" .
151 get_string('liketologin') . '</p>', $loginurl, get_referer(false));
154 if (has_capability('mod/quiz:attempt', $context)) {
156 // Get this user's attempts.
157 $attempts = quiz_get_user_attempts($quiz->id, $USER->id);
158 $unfinished = false;
159 if ($unfinishedattempt = quiz_get_user_attempt_unfinished($quiz->id, $USER->id)) {
160 $attempts[] = $unfinishedattempt;
161 $unfinished = true;
163 $numattempts = count($attempts);
165 $mygrade = quiz_get_best_grade($quiz, $USER->id);
167 // Get some strings.
168 $strattempt = get_string("attempt", "quiz");
169 $strtimetaken = get_string("timetaken", "quiz");
170 $strtimecompleted = get_string("timecompleted", "quiz");
171 $strgrade = get_string("grade");
172 $strmarks = get_string('marks', 'quiz');
173 $strfeedback = get_string('feedback', 'quiz');
175 // Print table with existing attempts
176 if ($attempts) {
178 print_heading('Summary of your previous attempts');
180 // Work out which columns we need, taking account what data is available in each attempt.
181 list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts, $context);
183 $gradecolumn = $someoptions->scores && $quiz->grade && $quiz->sumgrades;
184 $markcolumn = $gradecolumn && ($quiz->grade != $quiz->sumgrades);
185 $overallstats = $alloptions->scores;
187 $feedbackcolumn = quiz_has_feedback($quiz->id);
188 $overallfeedback = $feedbackcolumn && $alloptions->overallfeedback;
190 // prepare table header
191 $table->class = 'generaltable quizattemptsummary';
192 $table->head = array($strattempt, $strtimecompleted);
193 $table->align = array("center", "left");
194 $table->size = array("", "");
195 if ($markcolumn) {
196 $table->head[] = "$strmarks / $quiz->sumgrades";
197 $table->align[] = 'center';
198 $table->size[] = '';
200 if ($gradecolumn) {
201 $table->head[] = "$strgrade / $quiz->grade";
202 $table->align[] = 'center';
203 $table->size[] = '';
205 if ($feedbackcolumn) {
206 $table->head[] = $strfeedback;
207 $table->align[] = 'left';
208 $table->size[] = '';
210 if (isset($quiz->showtimetaken)) {
211 $table->head[] = $strtimetaken;
212 $table->align[] = 'left';
213 $table->size[] = '';
216 // One row for each attempt
217 foreach ($attempts as $attempt) {
218 $attemptoptions = quiz_get_reviewoptions($quiz, $attempt, $context);
219 $row = array();
221 // Add the attempt number, making it a link, if appropriate.
222 if ($attempt->preview) {
223 $row[] = make_review_link(get_string('preview', 'quiz'), $quiz, $attempt);
224 } else {
225 $row[] = make_review_link($attempt->attempt, $quiz, $attempt);
228 // prepare strings for time taken and date completed
229 $timetaken = '';
230 $datecompleted = '';
231 if ($attempt->timefinish > 0) {
232 // attempt has finished
233 $timetaken = format_time($attempt->timefinish - $attempt->timestart);
234 $datecompleted = userdate($attempt->timefinish);
235 } else if ($available) {
236 // The attempt is still in progress.
237 $timetaken = format_time(time() - $attempt->timestart);
238 $datecompleted = '';
239 } else if ($quiz->timeclose) {
240 // The attempt was not completed but is also not available any more becuase the quiz is closed.
241 $timetaken = format_time($quiz->timeclose - $attempt->timestart);
242 $datecompleted = userdate($quiz->timeclose);
243 } else {
244 // Something wheird happened.
245 $timetaken = '';
246 $datecompleted = '';
248 $row[] = $datecompleted;
250 if ($markcolumn) {
251 if ($attemptoptions->scores) {
252 $row[] = make_review_link(round($attempt->sumgrades, $quiz->decimalpoints), $quiz, $attempt);
253 } else {
254 $row[] = '';
258 // Ouside the if because we may be showing feedback but not grades.
259 $attemptgrade = quiz_rescale_grade($attempt->sumgrades, $quiz);
261 if ($gradecolumn) {
262 if ($attemptoptions->scores) {
263 $formattedgrade = $attemptgrade;
264 // highlight the highest grade if appropriate
265 if ($overallstats && $numattempts > 1 && !is_null($mygrade) && $attemptgrade == $mygrade && $quiz->grademethod == QUIZ_GRADEHIGHEST) {
266 $table->rowclass[$attempt->attempt] = 'bestrow';
269 $row[] = make_review_link($formattedgrade, $quiz, $attempt);
270 } else {
271 $row[] = '';
275 if ($feedbackcolumn) {
276 if ($attemptoptions->overallfeedback) {
277 $row[] = quiz_feedback_for_grade($attemptgrade, $quiz->id);
278 } else {
279 $row[] = '';
283 if (isset($quiz->showtimetaken)) {
284 $row[] = $timetaken;
287 $table->data[$attempt->attempt] = $row;
288 } // End of loop over attempts.
289 print_table($table);
292 // Print information about the student's best score for this quiz if possible.
293 $moreattempts = $unfinished || $numattempts < $quiz->attempts || $quiz->attempts == 0;
294 if (!$moreattempts) {
295 print_heading(get_string("nomoreattempts", "quiz"));
298 if ($numattempts && $quiz->sumgrades && !is_null($mygrade)) {
299 if ($overallstats) {
300 if ($available && $moreattempts) {
301 $a = new stdClass;
302 $a->method = $QUIZ_GRADE_METHOD[$quiz->grademethod];
303 $a->mygrade = $mygrade;
304 $a->quizgrade = $quiz->grade;
305 print_heading(get_string('gradesofar', 'quiz', $a));
306 } else {
307 print_heading(get_string('yourfinalgradeis', 'quiz', "$mygrade / $quiz->grade"));
311 if ($overallfeedback) {
312 echo '<p class="quizgradefeedback">'.quiz_feedback_for_grade($mygrade, $quiz->id).'</p>';
316 // Print a button to start/continue an attempt, if appropriate.
317 if (!$quiz->questions) {
318 print_heading(get_string("noquestions", "quiz"));
320 } else if ($available && $moreattempts) {
321 echo "<br />";
322 echo "<div class=\"quizattempt\">";
324 if ($unfinished) {
325 if (has_capability('mod/quiz:preview', $context)) {
326 $buttontext = get_string('continuepreview', 'quiz');
327 } else {
328 $buttontext = get_string('continueattemptquiz', 'quiz');
330 } else {
332 // Work out the appropriate button caption.
333 if (has_capability('mod/quiz:preview', $context)) {
334 $buttontext = get_string('previewquiznow', 'quiz');
335 } else if ($numattempts == 0) {
336 $buttontext = get_string('attemptquiznow', 'quiz');
337 } else {
338 $buttontext = get_string('reattemptquiz', 'quiz');
341 // Work out if the quiz is temporarily unavailable because of the delay option.
342 if (!empty($attempts)) {
343 $tempunavailable = '';
344 $lastattempt = end($attempts);
345 $lastattempttime = $lastattempt->timefinish;
346 if ($numattempts == 1 && $quiz->delay1 && $timenow <= $lastattempttime + $quiz->delay1) {
347 $tempunavailable = get_string('temporaryblocked', 'quiz') .
348 ' <strong>'. userdate($lastattempttime + $quiz->delay1). '</strong>';
349 } else if ($numattempts > 1 && $quiz->delay2 && $timenow <= $lastattempttime + $quiz->delay2) {
350 $tempunavailable = get_string('temporaryblocked', 'quiz') .
351 ' <strong>'. userdate($lastattempttime + $quiz->delay2). '</strong>';
354 // If so, display a message and prevent the start button from appearing.
355 if ($tempunavailable) {
356 print_simple_box($tempunavailable, "center");
357 print_continue($CFG->wwwroot . '/course/view.php?id=' . $course->id);
358 $buttontext = '';
363 // Actually print the start button.
364 if ($buttontext) {
365 $buttontext = htmlspecialchars($buttontext, ENT_QUOTES);
367 // Do we need a confirm javascript alert?
368 if ($unfinished) {
369 $strconfirmstartattempt = '';
370 } else if ($quiz->timelimit && $quiz->attempts) {
371 $strconfirmstartattempt = addslashes(get_string('confirmstartattempttimelimit','quiz', $quiz->attempts));
372 } else if ($quiz->timelimit) {
373 $strconfirmstartattempt = addslashes(get_string('confirmstarttimelimit','quiz'));
374 } else if ($quiz->attempts) {
375 $strconfirmstartattempt = addslashes(get_string('confirmstartattemptlimit','quiz', $quiz->attempts));
376 } else {
377 $strconfirmstartattempt = '';
380 // Prepare options depending on whether the quiz should be a popup.
381 if (!empty($quiz->popup)) {
382 $window = 'quizpopup';
383 $windowoptions = "left=0, top=0, height='+window.screen.height+', " .
384 "width='+window.screen.width+', channelmode=yes, fullscreen=yes, " .
385 "scrollbars=yes, resizeable=no, directories=no, toolbar=no, " .
386 "titlebar=no, location=no, status=no, menubar=no";
387 } else {
388 $window = '_self';
389 $windowoptions = '';
392 // Determine the URL to use.
393 $attempturl = "attempt.php?id=$cm->id";
394 if (!empty($CFG->usesid) && !isset($_COOKIE[session_name()])) {
395 $attempturl = sid_process_url($attempturl);
398 echo '<input type="button" value="'.$buttontext.'" onclick="javascript:';
399 if ($strconfirmstartattempt) {
400 echo "if (confirm('".addslashes_js($strconfirmstartattempt)."')) ";
402 echo "window.open('$attempturl','$window','$windowoptions');", '" />';
405 <noscript>
406 <div>
407 <?php print_heading(get_string('noscript', 'quiz')); ?>
408 </div>
409 </noscript>
410 <?php
413 echo "</div>\n";
414 } else {
415 print_continue($CFG->wwwroot . '/course/view.php?id=' . $course->id);
419 // Should we not be seeing if we need to print right-hand-side blocks?
421 // Finish the page.
422 if (!empty($THEME->customcorners)) print_custom_corners_end();
423 echo '</td></tr></table>';
424 print_footer($course);
426 // Utility functions =================================================================
428 function quiz_review_allowed($quiz) {
429 return true;
432 /** Make some text into a link to review the quiz, if that is appropriate. */
433 function make_review_link($linktext, $quiz, $attempt) {
434 // If not even responses are to be shown in review then we don't allow any review
435 if (!($quiz->review & QUIZ_REVIEW_RESPONSES)) {
436 return $linktext;
439 // If the quiz is still open, are reviews allowed?
440 if ((!$quiz->timeclose or time() < $quiz->timeclose) and !($quiz->review & QUIZ_REVIEW_OPEN)) {
441 // If not, don't link.
442 return $linktext;
445 // If the quiz is closed, are reviews allowed?
446 if (($quiz->timeclose and time() > $quiz->timeclose) and !($quiz->review & QUIZ_REVIEW_CLOSED)) {
447 // If not, don't link.
448 return $linktext;
451 // If the attempt is still open, don't link.
452 if (!$attempt->timefinish) {
453 return $linktext;
456 $url = "review.php?q=$quiz->id&amp;attempt=$attempt->id";
457 if ($quiz->popup) {
458 $windowoptions = "left=0, top=0, channelmode=yes, fullscreen=yes, scrollbars=yes, resizeable=no, directories=no, toolbar=no, titlebar=no, location=no, status=no, menubar=no";
459 return link_to_popup_window('/mod/quiz/' . $url, 'quizpopup', $linktext, '+window.screen.height+', '+window.screen.width+', '', $windowoptions, true);
460 } else {
461 return "<a href='$url'>$linktext</a>";