Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/choice/view.php
Go to the documentation of this file.
00001 <?php
00002 
00003     require_once("../../config.php");
00004     require_once("lib.php");
00005     require_once($CFG->libdir . '/completionlib.php');
00006 
00007     $id         = required_param('id', PARAM_INT);                 // Course Module ID
00008     $action     = optional_param('action', '', PARAM_ALPHA);
00009     $attemptids = optional_param_array('attemptid', array(), PARAM_INT); // array of attempt ids for delete action
00010 
00011     $url = new moodle_url('/mod/choice/view.php', array('id'=>$id));
00012     if ($action !== '') {
00013         $url->param('action', $action);
00014     }
00015     $PAGE->set_url($url);
00016 
00017     if (! $cm = get_coursemodule_from_id('choice', $id)) {
00018         print_error('invalidcoursemodule');
00019     }
00020 
00021     if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
00022         print_error('coursemisconf');
00023     }
00024 
00025     require_course_login($course, false, $cm);
00026 
00027     if (!$choice = choice_get_choice($cm->instance)) {
00028         print_error('invalidcoursemodule');
00029     }
00030 
00031     $strchoice = get_string('modulename', 'choice');
00032     $strchoices = get_string('modulenameplural', 'choice');
00033 
00034     if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) {
00035         print_error('badcontext');
00036     }
00037 
00038     if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate) {
00039         if ($answer = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id))) {
00040             $DB->delete_records('choice_answers', array('id' => $answer->id));
00041 
00042             // Update completion state
00043             $completion = new completion_info($course);
00044             if ($completion->is_enabled($cm) && $choice->completionsubmit) {
00045                 $completion->update_state($cm, COMPLETION_INCOMPLETE);
00046             }
00047         }
00048     }
00049 
00050     $PAGE->set_title(format_string($choice->name));
00051     $PAGE->set_heading($course->fullname);
00052 
00053     // Mark viewed by user (if required)
00054     $completion = new completion_info($course);
00055     $completion->set_module_viewed($cm);
00056 
00058     if (data_submitted() && is_enrolled($context, NULL, 'mod/choice:choose') && confirm_sesskey()) {
00059         $timenow = time();
00060         if (has_capability('mod/choice:deleteresponses', $context)) {
00061             if ($action == 'delete') { //some responses need to be deleted
00062                 choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses.
00063                 redirect("view.php?id=$cm->id");
00064             }
00065         }
00066         $answer = optional_param('answer', '', PARAM_INT);
00067 
00068         if (empty($answer)) {
00069             redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice'));
00070         } else {
00071             choice_user_submit_response($answer, $choice, $USER->id, $course, $cm);
00072         }
00073         echo $OUTPUT->header();
00074         echo $OUTPUT->notification(get_string('choicesaved', 'choice'),'notifysuccess');
00075     } else {
00076         echo $OUTPUT->header();
00077     }
00078 
00079 
00081     add_to_log($course->id, "choice", "view", "view.php?id=$cm->id", $choice->id, $cm->id);
00082 
00084     $groupmode = groups_get_activity_groupmode($cm);
00085 
00086     if ($groupmode) {
00087         groups_get_activity_group($cm, true);
00088         groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/view.php?id='.$id);
00089     }
00090     $allresponses = choice_get_response_data($choice, $cm, $groupmode);   // Big function, approx 6 SQL calls per user
00091 
00092 
00093     if (has_capability('mod/choice:readresponses', $context)) {
00094         choice_show_reportlink($allresponses, $cm);
00095     }
00096 
00097     echo '<div class="clearer"></div>';
00098 
00099     if ($choice->intro) {
00100         echo $OUTPUT->box(format_module_intro('choice', $choice, $cm->id), 'generalbox', 'intro');
00101     }
00102 
00103     $current = false;  // Initialise for later
00104     //if user has already made a selection, and they are not allowed to update it, show their selected answer.
00105     if (isloggedin() && ($current = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id))) &&
00106         empty($choice->allowupdate) ) {
00107         echo $OUTPUT->box(get_string("yourselection", "choice", userdate($choice->timeopen)).": ".format_string(choice_get_option_text($choice, $current->optionid)), 'generalbox', 'yourselection');
00108     }
00109 
00111     $choiceopen = true;
00112     $timenow = time();
00113     if ($choice->timeclose !=0) {
00114         if ($choice->timeopen > $timenow ) {
00115             echo $OUTPUT->box(get_string("notopenyet", "choice", userdate($choice->timeopen)), "generalbox notopenyet");
00116             echo $OUTPUT->footer();
00117             exit;
00118         } else if ($timenow > $choice->timeclose) {
00119             echo $OUTPUT->box(get_string("expired", "choice", userdate($choice->timeclose)), "generalbox expired");
00120             $choiceopen = false;
00121         }
00122     }
00123 
00124     if ( (!$current or $choice->allowupdate) and $choiceopen and is_enrolled($context, NULL, 'mod/choice:choose')) {
00125     // They haven't made their choice yet or updates allowed and choice is open
00126 
00127         $options = choice_prepare_options($choice, $USER, $cm, $allresponses);
00128         $renderer = $PAGE->get_renderer('mod_choice');
00129         echo $renderer->display_options($options, $cm->id, $choice->display);
00130         $choiceformshown = true;
00131     } else {
00132         $choiceformshown = false;
00133     }
00134 
00135     if (!$choiceformshown) {
00136         $sitecontext = get_context_instance(CONTEXT_SYSTEM);
00137 
00138         if (isguestuser()) {
00139             // Guest account
00140             echo $OUTPUT->confirm(get_string('noguestchoose', 'choice').'<br /><br />'.get_string('liketologin'),
00141                          get_login_url(), new moodle_url('/course/view.php', array('id'=>$course->id)));
00142         } else if (!is_enrolled($context)) {
00143             // Only people enrolled can make a choice
00144             $SESSION->wantsurl = $FULLME;
00145             $SESSION->enrolcancel = (!empty($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
00146 
00147             $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00148             $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
00149 
00150             echo $OUTPUT->box_start('generalbox', 'notice');
00151             echo '<p align="center">'. get_string('notenrolledchoose', 'choice') .'</p>';
00152             echo $OUTPUT->container_start('continuebutton');
00153             echo $OUTPUT->single_button(new moodle_url('/enrol/index.php?', array('id'=>$course->id)), get_string('enrolme', 'core_enrol', $courseshortname));
00154             echo $OUTPUT->container_end();
00155             echo $OUTPUT->box_end();
00156 
00157         }
00158     }
00159 
00160     // print the results at the bottom of the screen
00161     if ( $choice->showresults == CHOICE_SHOWRESULTS_ALWAYS or
00162         ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_ANSWER and $current) or
00163         ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_CLOSE and !$choiceopen)) {
00164 
00165         if (!empty($choice->showunanswered)) {
00166             $choice->option[0] = get_string('notanswered', 'choice');
00167             $choice->maxanswers[0] = 0;
00168         }
00169         $results = prepare_choice_show_results($choice, $course, $cm, $allresponses);
00170         $renderer = $PAGE->get_renderer('mod_choice');
00171         echo $renderer->display_result($results);
00172 
00173     } else if (!$choiceformshown) {
00174         echo $OUTPUT->box(get_string('noresultsviewable', 'choice'));
00175     }
00176 
00177     echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations