2009-09-23 06:34:39 +00:00
|
|
|
<?php
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2003-01-05 14:19:20 +00:00
|
|
|
require_once("../../config.php");
|
|
|
|
require_once("lib.php");
|
2009-05-08 09:00:41 +00:00
|
|
|
require_once($CFG->libdir . '/completionlib.php');
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2006-04-23 21:07:44 +00:00
|
|
|
$id = required_param('id', PARAM_INT); // Course Module ID
|
|
|
|
$action = optional_param('action', '', PARAM_ALPHA);
|
2011-08-17 16:27:15 +02:00
|
|
|
$attemptids = optional_param_array('attemptid', array(), PARAM_INT); // array of attempt ids for delete action
|
2008-06-04 06:42:54 +00:00
|
|
|
|
2010-01-16 15:39:56 +00:00
|
|
|
$url = new moodle_url('/mod/choice/view.php', array('id'=>$id));
|
2009-09-23 06:34:39 +00:00
|
|
|
if ($action !== '') {
|
|
|
|
$url->param('action', $action);
|
|
|
|
}
|
|
|
|
$PAGE->set_url($url);
|
|
|
|
|
2006-08-08 22:09:55 +00:00
|
|
|
if (! $cm = get_coursemodule_from_id('choice', $id)) {
|
2008-05-23 07:03:17 +00:00
|
|
|
print_error('invalidcoursemodule');
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2008-06-04 06:42:54 +00:00
|
|
|
if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
|
2008-05-23 07:03:17 +00:00
|
|
|
print_error('coursemisconf');
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2005-02-16 10:40:48 +00:00
|
|
|
require_course_login($course, false, $cm);
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2002-09-08 09:42:07 +00:00
|
|
|
if (!$choice = choice_get_choice($cm->instance)) {
|
2008-05-23 07:03:17 +00:00
|
|
|
print_error('invalidcoursemodule');
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
2008-06-04 06:42:54 +00:00
|
|
|
|
2006-10-23 03:00:30 +00:00
|
|
|
$strchoice = get_string('modulename', 'choice');
|
|
|
|
$strchoices = get_string('modulenameplural', 'choice');
|
|
|
|
|
2012-07-26 14:57:34 +08:00
|
|
|
$context = context_module::instance($cm->id);
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2010-03-31 07:41:31 +00:00
|
|
|
if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate) {
|
2008-06-04 06:42:54 +00:00
|
|
|
if ($answer = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id))) {
|
|
|
|
$DB->delete_records('choice_answers', array('id' => $answer->id));
|
2010-10-14 14:58:43 +00:00
|
|
|
|
|
|
|
// Update completion state
|
|
|
|
$completion = new completion_info($course);
|
|
|
|
if ($completion->is_enabled($cm) && $choice->completionsubmit) {
|
|
|
|
$completion->update_state($cm, COMPLETION_INCOMPLETE);
|
|
|
|
}
|
2008-02-15 02:43:38 +00:00
|
|
|
}
|
2007-05-03 00:04:53 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2014-02-05 14:47:23 +08:00
|
|
|
$PAGE->set_title($choice->name);
|
2010-07-05 22:42:46 +00:00
|
|
|
$PAGE->set_heading($course->fullname);
|
2005-03-30 17:32:23 +00:00
|
|
|
|
2011-04-06 18:05:43 +01:00
|
|
|
// Mark viewed by user (if required)
|
|
|
|
$completion = new completion_info($course);
|
|
|
|
$completion->set_module_viewed($cm);
|
|
|
|
|
2009-02-09 09:51:06 +00:00
|
|
|
/// Submit any new data if there is any
|
2010-11-19 03:42:33 +00:00
|
|
|
if (data_submitted() && is_enrolled($context, NULL, 'mod/choice:choose') && confirm_sesskey()) {
|
2002-08-12 08:56:36 +00:00
|
|
|
$timenow = time();
|
2006-08-14 05:55:40 +00:00
|
|
|
if (has_capability('mod/choice:deleteresponses', $context)) {
|
2009-08-10 04:58:52 +00:00
|
|
|
if ($action == 'delete') { //some responses need to be deleted
|
2010-10-14 14:58:43 +00:00
|
|
|
choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses.
|
2006-02-23 01:20:24 +00:00
|
|
|
redirect("view.php?id=$cm->id");
|
|
|
|
}
|
2006-10-23 07:01:18 +00:00
|
|
|
}
|
|
|
|
$answer = optional_param('answer', '', PARAM_INT);
|
|
|
|
|
|
|
|
if (empty($answer)) {
|
2004-02-04 13:40:20 +00:00
|
|
|
redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice'));
|
2001-11-22 06:23:56 +00:00
|
|
|
} else {
|
2010-10-14 14:58:43 +00:00
|
|
|
choice_user_submit_response($answer, $choice, $USER->id, $course, $cm);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
2010-07-05 23:02:17 +00:00
|
|
|
echo $OUTPUT->header();
|
2014-08-15 15:53:10 +08:00
|
|
|
echo $OUTPUT->heading(format_string($choice->name), 2, null);
|
2009-08-18 05:13:36 +00:00
|
|
|
echo $OUTPUT->notification(get_string('choicesaved', 'choice'),'notifysuccess');
|
2010-07-05 23:02:17 +00:00
|
|
|
} else {
|
|
|
|
echo $OUTPUT->header();
|
2014-08-15 15:53:10 +08:00
|
|
|
echo $OUTPUT->heading(format_string($choice->name), 2, null);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2005-03-30 17:32:23 +00:00
|
|
|
|
|
|
|
/// Display the choice and possibly results
|
2013-09-19 13:39:44 +08:00
|
|
|
$eventdata = array();
|
|
|
|
$eventdata['objectid'] = $choice->id;
|
|
|
|
$eventdata['context'] = $context;
|
|
|
|
|
|
|
|
$event = \mod_choice\event\course_module_viewed::create($eventdata);
|
2014-02-25 11:43:13 +08:00
|
|
|
$event->add_record_snapshot('course_modules', $cm);
|
|
|
|
$event->add_record_snapshot('course', $course);
|
2013-09-19 13:39:44 +08:00
|
|
|
$event->trigger();
|
2007-07-05 04:40:48 +00:00
|
|
|
|
|
|
|
/// Check to see if groups are being used in this choice
|
2007-08-27 21:52:35 +00:00
|
|
|
$groupmode = groups_get_activity_groupmode($cm);
|
2008-06-04 06:42:54 +00:00
|
|
|
|
2008-03-03 04:42:01 +00:00
|
|
|
if ($groupmode) {
|
|
|
|
groups_get_activity_group($cm, true);
|
2009-11-30 17:12:48 +00:00
|
|
|
groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/view.php?id='.$id);
|
2008-03-03 04:42:01 +00:00
|
|
|
}
|
|
|
|
$allresponses = choice_get_response_data($choice, $cm, $groupmode); // Big function, approx 6 SQL calls per user
|
|
|
|
|
2008-06-04 06:42:54 +00:00
|
|
|
|
2006-08-14 05:55:40 +00:00
|
|
|
if (has_capability('mod/choice:readresponses', $context)) {
|
2008-03-03 04:42:01 +00:00
|
|
|
choice_show_reportlink($allresponses, $cm);
|
2001-11-22 06:23:56 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 04:40:48 +00:00
|
|
|
echo '<div class="clearer"></div>';
|
|
|
|
|
2009-04-20 18:45:31 +00:00
|
|
|
if ($choice->intro) {
|
2009-08-10 04:58:52 +00:00
|
|
|
echo $OUTPUT->box(format_module_intro('choice', $choice, $cm->id), 'generalbox', 'intro');
|
2005-03-13 16:17:55 +00:00
|
|
|
}
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2012-03-23 08:56:13 +05:30
|
|
|
$timenow = time();
|
2008-01-25 06:31:15 +00:00
|
|
|
$current = false; // Initialise for later
|
2012-03-23 08:56:13 +05:30
|
|
|
//if user has already made a selection, and they are not allowed to update it or if choice is not open, show their selected answer.
|
2010-03-31 07:41:31 +00:00
|
|
|
if (isloggedin() && ($current = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id))) &&
|
2012-03-23 08:56:13 +05:30
|
|
|
(empty($choice->allowupdate) || ($timenow > $choice->timeclose)) ) {
|
2010-07-23 03:55:06 +00:00
|
|
|
echo $OUTPUT->box(get_string("yourselection", "choice", userdate($choice->timeopen)).": ".format_string(choice_get_option_text($choice, $current->optionid)), 'generalbox', 'yourselection');
|
2005-04-15 01:40:22 +00:00
|
|
|
}
|
2004-07-01 12:52:15 +00:00
|
|
|
|
2005-03-30 17:32:23 +00:00
|
|
|
/// Print the form
|
2008-08-11 04:19:34 +00:00
|
|
|
$choiceopen = true;
|
2007-10-22 21:25:06 +00:00
|
|
|
if ($choice->timeclose !=0) {
|
|
|
|
if ($choice->timeopen > $timenow ) {
|
2010-05-03 17:35:21 +00:00
|
|
|
echo $OUTPUT->box(get_string("notopenyet", "choice", userdate($choice->timeopen)), "generalbox notopenyet");
|
2009-08-06 14:20:08 +00:00
|
|
|
echo $OUTPUT->footer();
|
2007-10-22 21:25:06 +00:00
|
|
|
exit;
|
|
|
|
} else if ($timenow > $choice->timeclose) {
|
2010-05-03 17:35:21 +00:00
|
|
|
echo $OUTPUT->box(get_string("expired", "choice", userdate($choice->timeclose)), "generalbox expired");
|
2008-08-11 04:19:34 +00:00
|
|
|
$choiceopen = false;
|
2007-10-22 21:25:06 +00:00
|
|
|
}
|
2004-07-01 12:52:15 +00:00
|
|
|
}
|
|
|
|
|
2010-03-31 07:41:31 +00:00
|
|
|
if ( (!$current or $choice->allowupdate) and $choiceopen and is_enrolled($context, NULL, 'mod/choice:choose')) {
|
2004-07-01 12:52:15 +00:00
|
|
|
// They haven't made their choice yet or updates allowed and choice is open
|
2005-02-16 10:40:48 +00:00
|
|
|
|
2010-07-23 03:55:06 +00:00
|
|
|
$options = choice_prepare_options($choice, $USER, $cm, $allresponses);
|
2010-08-20 02:40:13 +00:00
|
|
|
$renderer = $PAGE->get_renderer('mod_choice');
|
2010-07-23 03:55:06 +00:00
|
|
|
echo $renderer->display_options($options, $cm->id, $choice->display);
|
2006-10-23 03:00:30 +00:00
|
|
|
$choiceformshown = true;
|
|
|
|
} else {
|
|
|
|
$choiceformshown = false;
|
2004-07-01 12:52:15 +00:00
|
|
|
}
|
|
|
|
|
2006-10-23 03:00:30 +00:00
|
|
|
if (!$choiceformshown) {
|
2012-07-26 14:57:34 +08:00
|
|
|
$sitecontext = context_system::instance();
|
2006-10-23 03:00:30 +00:00
|
|
|
|
2010-03-31 07:41:31 +00:00
|
|
|
if (isguestuser()) {
|
|
|
|
// Guest account
|
2009-08-20 08:43:12 +00:00
|
|
|
echo $OUTPUT->confirm(get_string('noguestchoose', 'choice').'<br /><br />'.get_string('liketologin'),
|
2010-08-20 02:40:13 +00:00
|
|
|
get_login_url(), new moodle_url('/course/view.php', array('id'=>$course->id)));
|
2010-03-31 07:41:31 +00:00
|
|
|
} else if (!is_enrolled($context)) {
|
|
|
|
// Only people enrolled can make a choice
|
2012-04-22 17:17:27 +02:00
|
|
|
$SESSION->wantsurl = qualified_me();
|
2010-07-13 23:25:19 +00:00
|
|
|
$SESSION->enrolcancel = (!empty($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
|
2006-10-23 03:00:30 +00:00
|
|
|
|
2012-07-26 14:57:34 +08:00
|
|
|
$coursecontext = context_course::instance($course->id);
|
2011-09-07 11:46:28 +12:00
|
|
|
$courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
|
|
|
|
|
2009-08-18 05:13:36 +00:00
|
|
|
echo $OUTPUT->box_start('generalbox', 'notice');
|
2010-07-21 10:11:18 +00:00
|
|
|
echo '<p align="center">'. get_string('notenrolledchoose', 'choice') .'</p>';
|
2009-08-20 08:43:12 +00:00
|
|
|
echo $OUTPUT->container_start('continuebutton');
|
2011-09-07 11:46:28 +12:00
|
|
|
echo $OUTPUT->single_button(new moodle_url('/enrol/index.php?', array('id'=>$course->id)), get_string('enrolme', 'core_enrol', $courseshortname));
|
2009-08-20 08:43:12 +00:00
|
|
|
echo $OUTPUT->container_end();
|
2010-08-20 02:40:13 +00:00
|
|
|
echo $OUTPUT->box_end();
|
2006-10-23 03:00:30 +00:00
|
|
|
|
|
|
|
}
|
2003-01-01 06:34:13 +00:00
|
|
|
}
|
2005-02-16 10:40:48 +00:00
|
|
|
|
2006-10-31 19:00:55 +00:00
|
|
|
// print the results at the bottom of the screen
|
|
|
|
if ( $choice->showresults == CHOICE_SHOWRESULTS_ALWAYS or
|
2010-08-20 02:40:13 +00:00
|
|
|
($choice->showresults == CHOICE_SHOWRESULTS_AFTER_ANSWER and $current) or
|
|
|
|
($choice->showresults == CHOICE_SHOWRESULTS_AFTER_CLOSE and !$choiceopen)) {
|
2006-10-31 19:00:55 +00:00
|
|
|
|
2010-07-23 03:55:06 +00:00
|
|
|
if (!empty($choice->showunanswered)) {
|
|
|
|
$choice->option[0] = get_string('notanswered', 'choice');
|
|
|
|
$choice->maxanswers[0] = 0;
|
|
|
|
}
|
2010-08-20 02:40:13 +00:00
|
|
|
$results = prepare_choice_show_results($choice, $course, $cm, $allresponses);
|
|
|
|
$renderer = $PAGE->get_renderer('mod_choice');
|
2010-07-23 03:55:06 +00:00
|
|
|
echo $renderer->display_result($results);
|
2010-08-20 02:40:13 +00:00
|
|
|
|
|
|
|
} else if (!$choiceformshown) {
|
2009-08-18 05:13:36 +00:00
|
|
|
echo $OUTPUT->box(get_string('noresultsviewable', 'choice'));
|
2008-06-04 06:42:54 +00:00
|
|
|
}
|
2006-10-31 19:00:55 +00:00
|
|
|
|
2009-08-06 14:20:08 +00:00
|
|
|
echo $OUTPUT->footer();
|