Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/quiz/view.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00028 require_once(dirname(__FILE__) . '/../../config.php');
00029 require_once($CFG->libdir.'/gradelib.php');
00030 require_once($CFG->dirroot.'/mod/quiz/locallib.php');
00031 require_once($CFG->libdir . '/completionlib.php');
00032 
00033 $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or
00034 $q = optional_param('q',  0, PARAM_INT);  // quiz ID
00035 
00036 if ($id) {
00037     if (!$cm = get_coursemodule_from_id('quiz', $id)) {
00038         print_error('invalidcoursemodule');
00039     }
00040     if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
00041         print_error('coursemisconf');
00042     }
00043 } else {
00044     if (!$quiz = $DB->get_record('quiz', array('id' => $q))) {
00045         print_error('invalidquizid', 'quiz');
00046     }
00047     if (!$course = $DB->get_record('course', array('id' => $quiz->course))) {
00048         print_error('invalidcourseid');
00049     }
00050     if (!$cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id)) {
00051         print_error('invalidcoursemodule');
00052     }
00053 }
00054 
00055 // Check login and get context.
00056 require_login($course->id, false, $cm);
00057 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00058 require_capability('mod/quiz:view', $context);
00059 
00060 // Cache some other capabilities we use several times.
00061 $canattempt = has_capability('mod/quiz:attempt', $context);
00062 $canreviewmine = has_capability('mod/quiz:reviewmyattempts', $context);
00063 $canpreview = has_capability('mod/quiz:preview', $context);
00064 
00065 // Create an object to manage all the other (non-roles) access rules.
00066 $timenow = time();
00067 $quizobj = quiz::create($cm->instance, $USER->id);
00068 $accessmanager = new quiz_access_manager($quizobj, $timenow,
00069         has_capability('mod/quiz:ignoretimelimits', $context, null, false));
00070 $quiz = $quizobj->get_quiz();
00071 
00072 // Log this request.
00073 add_to_log($course->id, 'quiz', 'view', 'view.php?id=' . $cm->id, $quiz->id, $cm->id);
00074 
00075 $completion = new completion_info($course);
00076 $completion->set_module_viewed($cm);
00077 
00078 // Initialize $PAGE, compute blocks
00079 $PAGE->set_url('/mod/quiz/view.php', array('id' => $cm->id));
00080 
00081 // Get this user's attempts.
00082 $attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'finished', true);
00083 $lastfinishedattempt = end($attempts);
00084 $unfinished = false;
00085 if ($unfinishedattempt = quiz_get_user_attempt_unfinished($quiz->id, $USER->id)) {
00086     $attempts[] = $unfinishedattempt;
00087     $unfinished = true;
00088 }
00089 $numattempts = count($attempts);
00090 
00091 // Work out the final grade, checking whether it was overridden in the gradebook.
00092 if (!$canpreview) {
00093     $mygrade = quiz_get_best_grade($quiz, $USER->id);
00094 } else if ($lastfinishedattempt) {
00095     // Users who can preview the quiz don't get a proper grade, so work out a
00096     // plausible value to display instead, so the page looks right.
00097     $mygrade = quiz_rescale_grade($lastfinishedattempt->sumgrades, $quiz, false);
00098 } else {
00099     $mygrade = null;
00100 }
00101 
00102 $mygradeoverridden = false;
00103 $gradebookfeedback = '';
00104 
00105 $grading_info = grade_get_grades($course->id, 'mod', 'quiz', $quiz->id, $USER->id);
00106 if (!empty($grading_info->items)) {
00107     $item = $grading_info->items[0];
00108     if (isset($item->grades[$USER->id])) {
00109         $grade = $item->grades[$USER->id];
00110 
00111         if ($grade->overridden) {
00112             $mygrade = $grade->grade + 0; // Convert to number.
00113             $mygradeoverridden = true;
00114         }
00115         if (!empty($grade->str_feedback)) {
00116             $gradebookfeedback = $grade->str_feedback;
00117         }
00118     }
00119 }
00120 
00121 $title = $course->shortname . ': ' . format_string($quiz->name);
00122 $PAGE->set_title($title);
00123 $PAGE->set_heading($course->fullname);
00124 $output = $PAGE->get_renderer('mod_quiz');
00125 
00126 /*
00127  * Create view object for use within renderers file
00128  */
00129 $viewobj = new mod_quiz_view_object();
00130 $viewobj->attempts = $attempts;
00131 $viewobj->accessmanager = $accessmanager;
00132 $viewobj->canreviewmine = $canreviewmine;
00133 
00134 // Print table with existing attempts
00135 if ($attempts) {
00136     // Work out which columns we need, taking account what data is available in each attempt.
00137     list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts, $context);
00138 
00139     $viewobj->attemptcolumn = $quiz->attempts != 1;
00140 
00141     $viewobj->gradecolumn = $someoptions->marks >= question_display_options::MARK_AND_MAX &&
00142             quiz_has_grades($quiz);
00143     $viewobj->markcolumn = $viewobj->gradecolumn && ($quiz->grade != $quiz->sumgrades);
00144     $viewobj->overallstats = $lastfinishedattempt && $alloptions->marks >= question_display_options::MARK_AND_MAX;
00145 
00146     $viewobj->feedbackcolumn = quiz_has_feedback($quiz) && $alloptions->overallfeedback;
00147 } else {
00148     $viewobj->attemptcolumn = 1;
00149 }
00150 
00151 $viewobj->timenow = $timenow;
00152 $viewobj->numattempts = $numattempts;
00153 $viewobj->mygrade = $mygrade;
00154 $viewobj->moreattempts = $unfinished ||
00155         !$accessmanager->is_finished($numattempts, $lastfinishedattempt);
00156 $viewobj->mygradeoverridden = $mygradeoverridden;
00157 $viewobj->gradebookfeedback = $gradebookfeedback;
00158 $viewobj->lastfinishedattempt = $lastfinishedattempt;
00159 $viewobj->canedit = has_capability('mod/quiz:manage', $context);
00160 $viewobj->editurl = new moodle_url('/mod/quiz/edit.php', array('cmid' => $cm->id));
00161 $viewobj->backtocourseurl = new moodle_url('/course/view.php', array('id' => $course->id));
00162 $viewobj->startattempturl = $quizobj->start_attempt_url();
00163 $viewobj->startattemptwarning = $quizobj->confirm_start_attempt_message($unfinished);
00164 $viewobj->popuprequired = $accessmanager->attempt_must_be_in_popup();
00165 $viewobj->popupoptions = $accessmanager->get_popup_options();
00166 
00167 // Display information about this quiz.
00168 $viewobj->infomessages = $viewobj->accessmanager->describe_rules();
00169 if ($quiz->attempts != 1) {
00170     $viewobj->infomessages[] = get_string('gradingmethod', 'quiz',
00171             quiz_get_grading_option_name($quiz->grademethod));
00172 }
00173 
00174 // Determine wheter a start attempt button should be displayed.
00175 $viewobj->quizhasquestions = (bool) quiz_clean_layout($quiz->questions, true);
00176 $viewobj->preventmessages = array();
00177 if (!$viewobj->quizhasquestions) {
00178     $viewobj->buttontext = '';
00179 
00180 } else {
00181     if ($unfinished) {
00182         if ($canattempt) {
00183             $viewobj->buttontext = get_string('continueattemptquiz', 'quiz');
00184         } else if ($canpreview) {
00185             $viewobj->buttontext = get_string('continuepreview', 'quiz');
00186         }
00187 
00188     } else {
00189         if ($canattempt) {
00190             $viewobj->preventmessages = $viewobj->accessmanager->prevent_new_attempt(
00191                     $viewobj->numattempts, $viewobj->lastfinishedattempt);
00192             if ($viewobj->preventmessages) {
00193                 $viewobj->buttontext = '';
00194             } else if ($viewobj->numattempts == 0) {
00195                 $viewobj->buttontext = get_string('attemptquiznow', 'quiz');
00196             } else {
00197                 $viewobj->buttontext = get_string('reattemptquiz', 'quiz');
00198             }
00199 
00200         } else if ($canpreview) {
00201             $viewobj->buttontext = get_string('previewquiznow', 'quiz');
00202         }
00203     }
00204 
00205     // If, so far, we think a button should be printed, so check if they will be
00206     // allowed to access it.
00207     if ($viewobj->buttontext) {
00208         if (!$viewobj->moreattempts) {
00209             $viewobj->buttontext = '';
00210         } else if ($canattempt
00211                 && $viewobj->preventmessages = $viewobj->accessmanager->prevent_access()) {
00212             $viewobj->buttontext = '';
00213         }
00214     }
00215 }
00216 
00217 echo $OUTPUT->header();
00218 
00219 if (isguestuser()) {
00220     // Guests can't do a quiz, so offer them a choice of logging in or going back.
00221     echo $output->view_page_guest($course, $quiz, $cm, $context, $viewobj->infomessages);
00222 } else if (!isguestuser() && !($canattempt || $canpreview
00223           || $viewobj->canreviewmine)) {
00224     // If they are not enrolled in this course in a good enough role, tell them to enrol.
00225     echo $output->view_page_notenrolled($course, $quiz, $cm, $context, $viewobj->infomessages);
00226 } else {
00227     echo $output->view_page($course, $quiz, $cm, $context, $viewobj);
00228 }
00229 
00230 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations