file ongoing.html was added on branch MOODLE_15_STABLE on 2005-07-07 16:14:36 +0000
[moodle.git] / mod / quiz / view.php
blobf796dd056b3ce61e8ac0df6fe2542e2644898ec9
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 $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or
11 $q = optional_param('q', 0, PARAM_INT); // quiz ID
12 $edit = optional_param('edit', '');
15 if ($id) {
16 if (! $cm = get_record("course_modules", "id", $id)) {
17 error("There is no coursemodule with id $id");
20 if (! $course = get_record("course", "id", $cm->course)) {
21 error("Course is misconfigured");
24 if (! $quiz = get_record("quiz", "id", $cm->instance)) {
25 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 require_login($course->id, false, $cm);
41 $isteacher = isteacher($course->id);
43 // if no questions have been set up yet redirect to edit.php
44 if (!$quiz->questions and isteacheredit($course->id)) {
45 redirect('edit.php?quizid='.$quiz->id);
48 add_to_log($course->id, "quiz", "view", "view.php?id=$cm->id", $quiz->id, $cm->id);
50 $timenow = time();
52 // Initialize $PAGE, compute blocks
54 $PAGE = page_create_instance($quiz->id);
55 $pageblocks = blocks_setup($PAGE);
56 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210);
58 // Print the page header
60 if (!empty($edit) && $PAGE->user_allowed_editing()) {
61 if ($edit == 'on') {
62 $USER->editing = true;
63 } else if ($edit == 'off') {
64 $USER->editing = false;
68 $PAGE->print_header($course->shortname.': %fullname%');
70 echo '<table id="layout-table"><tr>';
72 if(!empty($CFG->showblocksonmodpages) && (blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) {
73 echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
74 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
75 echo '</td>';
78 echo '<td id="middle-column">';
80 $available = ($quiz->timeopen < $timenow and $timenow < $quiz->timeclose) || $isteacher;
82 // Print the main part of the page
84 // Print heading and tabs for teacher
85 if ($isteacher) {
86 $currenttab = 'info';
87 include('tabs.php');
89 print_heading(format_string($quiz->name));
91 if (trim(strip_tags($quiz->intro))) {
92 print_simple_box(format_text($quiz->intro), "center");
95 if ($quiz->attempts > 1) {
96 echo "<p align=\"center\">".get_string("attemptsallowed", "quiz").": $quiz->attempts</p>";
97 echo "<p align=\"center\">".get_string("grademethod", "quiz").": ".$QUIZ_GRADE_METHOD[$quiz->grademethod]."</p>";
98 } else {
99 echo "<br />";
101 if ($available) {
102 if ($quiz->timelimit) {
103 echo "<p align=\"center\">".get_string("quiztimelimit","quiz", format_time($quiz->timelimit * 60))."</p>";
105 echo "<p align=\"center\">".get_string("quizavailable", "quiz", userdate($quiz->timeclose));
106 } else if ($timenow < $quiz->timeopen) {
107 echo "<p align=\"center\">".get_string("quiznotavailable", "quiz", userdate($quiz->timeopen));
108 } else {
109 echo "<p align=\"center\">".get_string("quizclosed", "quiz", userdate($quiz->timeclose));
113 // This is all the teacher will get
114 if ($isteacher) {
116 if ($attemptcount = count_records('quiz_attempts', 'quiz', $quiz->id, 'preview', 0)) {
118 $strviewallanswers = get_string("viewallanswers", "quiz", $attemptcount);
119 $usercount = count_records_select('quiz_attempts', "quiz = '$quiz->id' AND preview = '0'", 'COUNT(DISTINCT userid)');
120 $strusers = $course->students;
122 notify("<a href=\"report.php?mode=overview&amp;id=$cm->id\">$strviewallanswers ($usercount $strusers)</a>");
124 print_footer($course);
125 exit;
128 if (isguest()) {
130 $wwwroot = $CFG->wwwroot.'/login/index.php';
131 if (!empty($CFG->loginhttps)) {
132 $wwwroot = str_replace('http','https', $wwwroot);
135 notice_yesno(get_string('guestno', 'quiz').'<br /><br />'.get_string('liketologin'),
136 $wwwroot, $_SERVER['HTTP_REFERER']);
137 print_footer($course);
138 exit;
141 if ($attempts = quiz_get_user_attempts($quiz->id, $USER->id)) {
142 $numattempts = count($attempts);
143 } else {
144 $numattempts = 0;
147 $unfinished = false;
148 if ($unfinishedattempt = quiz_get_user_attempt_unfinished($quiz->id, $USER->id)) {
149 $attempts[] = $unfinishedattempt;
150 $unfinished = true;
153 $strattempt = get_string("attempt", "quiz");
154 $strtimetaken = get_string("timetaken", "quiz");
155 $strtimecompleted = get_string("timecompleted", "quiz");
156 $strgrade = get_string("grade");
157 $strmarks = get_string('marks', 'quiz');
158 $strbestgrade = $QUIZ_GRADE_METHOD[$quiz->grademethod];
160 $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";
162 $mygrade = quiz_get_best_grade($quiz, $USER->id);
164 /// Now print table with existing attempts
166 if ($numattempts) {
168 /// prepare table header
169 if ($quiz->grade and $quiz->sumgrades) { // Grades used so have more columns in table
170 if ($quiz->grade <> $quiz->sumgrades) {
171 $table->head = array($strattempt, $strtimetaken, $strtimecompleted, "$strmarks / $quiz->sumgrades", "$strgrade / $quiz->grade");
172 $table->align = array("center", "center", "left", "right", "right");
173 $table->size = array("", "", "", "", "");
174 } else {
175 $table->head = array($strattempt, $strtimetaken, $strtimecompleted, "$strgrade / $quiz->grade");
176 $table->align = array("center", "center", "left", "right");
177 $table->size = array("", "", "", "");
180 } else { // No grades are being used
181 $table->head = array($strattempt, $strtimetaken, $strtimecompleted);
182 $table->align = array("center", "center", "left");
183 $table->size = array("", "", "");
186 /// One row for each attempt
187 foreach ($attempts as $attempt) {
189 /// prepare strings for time taken and date completed
190 $timetaken = '';
191 $datecompleted = '';
192 if ($attempt->timefinish > 0) { // attempt has finished
193 $timetaken = format_time($attempt->timefinish - $attempt->timestart);
194 $datecompleted = userdate($attempt->timefinish);
195 } else if ($available) { // The student can continue this attempt, so put appropriate link
196 $timetaken = format_time(time() - $attempt->timestart);
197 $strconfirmstartattempt = addslashes(get_string("confirmstartattempt","quiz"));
198 $datecompleted = "\n".'<script language="javascript" type="text/javascript">';
199 $datecompleted .= "\n<!--\n"; // -->
200 if (!empty($quiz->popup)) {
201 $datecompleted .= "var windowoptions = 'left=0, top=0, height='+window.screen.height+
202 ', width='+window.screen.width+', channelmode=yes, fullscreen=yes, scrollbars=yes, '+
203 'resizeable=no, directories=no, toolbar=no, titlebar=no, location=no, status=no, '+
204 'menubar=no';\n";
205 $jslink = 'javascript:';
206 if ($quiz->timelimit) {
207 $jslink .= "if (confirm('$strconfirmstartattempt')) ";
209 $jslink .= "var popup = window.open(\\'attempt.php?id=$cm->id\\', \\'quizpopup\\', windowoptions);";
210 } else {
211 $jslink = "attempt.php?id=$cm->id";
214 $linktext = get_string('continueattemptquiz', 'quiz');
215 $datecompleted .= "document.write('<a href=\"$jslink\" alt=\"$linktext\">$linktext</a>');";
216 $datecompleted .= "\n-->\n";
217 $datecompleted .= '</script>';
218 $datecompleted .= '<noscript>';
219 $datecompleted .= '<strong>'.get_string('noscript', 'quiz').'</strong>';
220 $datecompleted .= '</noscript>';
221 } else { // attempt was not completed but is also not available any more.
222 $timetaken = format_time($quiz->timeclose - $attempt->timestart);
223 $datecompleted = userdate($quiz->timeclose);
226 /// prepare strings for attempt number, mark and grade
227 if ($quiz->grade and $quiz->sumgrades) {
228 $attemptmark = round($attempt->sumgrades,$quiz->decimalpoints);
229 $attemptgrade = round(($attempt->sumgrades/$quiz->sumgrades)*$quiz->grade,$quiz->decimalpoints);
231 // highlight the highest grade if appropriate
232 if ($attemptgrade == $mygrade and ($quiz->grademethod == QUIZ_GRADEHIGHEST)) {
233 $attemptgrade = "<span class=\"highlight\">$attemptgrade</span>";
236 // if attempt is closed and review is allowed then make attemptnumber and
237 // mark and grade into links to review page
238 if (quiz_review_allowed($quiz) and $attempt->timefinish > 0) {
239 if ($quiz->popup) { // need to link to popup window
240 $attemptmark = link_to_popup_window ("/mod/quiz/review.php?q=$quiz->id&amp;attempt=$attempt->id", 'quizpopup', round($attempt->sumgrades,$quiz->decimalpoints), '+window.screen.height+', '+window.screen.width+', '', $windowoptions, true);
241 $attemptgrade = link_to_popup_window ("/mod/quiz/review.php?q=$quiz->id&amp;attempt=$attempt->id", 'quizpopup', $attemptgrade, '+window.screen.height+', '+window.screen.width+', '', $windowoptions, true);
242 $attempt->attempt = link_to_popup_window ("/mod/quiz/review.php?q=$quiz->id&amp;attempt=$attempt->id", 'quizpopup', "#$attempt->attempt", '+window.screen.height+', '+window.screen.width+', '', $windowoptions, true);
243 } else {
244 $attemptmark = "<a href=\"review.php?q=$quiz->id&amp;attempt=$attempt->id\">".round($attempt->sumgrades,$quiz->decimalpoints).'</a>';
245 $attemptgrade = "<a href=\"review.php?q=$quiz->id&amp;attempt=$attempt->id\">$attemptgrade</a>";
246 $attempt->attempt = "<a href=\"review.php?q=$quiz->id&amp;attempt=$attempt->id\">#$attempt->attempt</a>";
250 if ($quiz->grade <> $quiz->sumgrades) {
251 $table->data[] = array( $attempt->attempt,
252 $timetaken,
253 $datecompleted,
254 $attemptmark, $attemptgrade);
255 } else {
256 $table->data[] = array( $attempt->attempt,
257 $timetaken,
258 $datecompleted,
259 $attemptgrade);
261 } else { // No grades are being used
262 if (quiz_review_allowed($quiz)) {
263 if($attempt->timefinish > 0) {
264 $attempt->attempt = "<a href=\"review.php?q=$quiz->id&amp;attempt=$attempt->id\">#$attempt->attempt</a>";
265 } else {
266 $attempt->attempt = "<a href=\"attempt.php?id=$id\">#$attempt->attempt</a>";
269 $table->data[] = array( $attempt->attempt,
270 $timetaken,
271 $datecompleted);
274 print_table($table);
277 if (!$quiz->questions) {
278 print_heading(get_string("noquestions", "quiz"));
279 } else {
280 if ($numattempts < $quiz->attempts or !$quiz->attempts) {
281 if ($available) {
282 $options["id"] = $cm->id;
283 if ($numattempts and $quiz->grade) {
284 print_heading("$strbestgrade: $mygrade / $quiz->grade.");
286 $strconfirmstartattempt = addslashes(get_string("confirmstartattempt","quiz"));
287 echo "<br />";
288 echo "</p>";
289 echo "<div align=\"center\">";
291 include("view_js.php");
293 echo "</div>\n";
295 } else {
296 print_heading(get_string("nomoreattempts", "quiz"));
297 if ($quiz->grade) {
298 print_heading(get_string("yourfinalgradeis", "quiz", "$mygrade / $quiz->grade"));
300 print_continue('../../course/view.php?id='.$course->id);
303 // Finish the page
304 echo '</td></tr></table>';
306 print_footer($course);
308 function quiz_review_allowed($quiz) {
309 // If not even responses are to be shown in review then we
310 // don't allow any review
311 if (!($quiz->review & QUIZ_REVIEW_RESPONSES)) {
312 return false;
314 if ((time() < $quiz->timeclose) and !($quiz->review & QUIZ_REVIEW_OPEN)) {
315 return false;
317 if ((time() > $quiz->timeclose) and !($quiz->review & QUIZ_REVIEW_CLOSED)) {
318 return false;
320 return true;