Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   1  <?php
   2  
   3  require_once("../../config.php");
   4  require_once ("lib.php");
   5  require_once($CFG->libdir . '/completionlib.php');
   6  
   7  $id         = required_param('id', PARAM_INT);                 // Course Module ID
   8  $action     = optional_param('action', '', PARAM_ALPHANUMEXT);
   9  $attemptids = optional_param_array('attemptid', array(), PARAM_INT); // Get array of responses to delete or modify.
  10  $userids    = optional_param_array('userid', array(), PARAM_INT); // Get array of users whose choices need to be modified.
  11  $notify     = optional_param('notify', '', PARAM_ALPHA);
  12  
  13  $url = new moodle_url('/mod/choice/view.php', array('id'=>$id));
  14  if ($action !== '') {
  15      $url->param('action', $action);
  16  }
  17  $PAGE->set_url($url);
  18  
  19  if (! $cm = get_coursemodule_from_id('choice', $id)) {
  20      print_error('invalidcoursemodule');
  21  }
  22  $cm = cm_info::create($cm);
  23  
  24  if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
  25      print_error('coursemisconf');
  26  }
  27  
  28  require_course_login($course, false, $cm);
  29  
  30  if (!$choice = choice_get_choice($cm->instance)) {
  31      print_error('invalidcoursemodule');
  32  }
  33  
  34  $strchoice = get_string('modulename', 'choice');
  35  $strchoices = get_string('modulenameplural', 'choice');
  36  
  37  $context = context_module::instance($cm->id);
  38  
  39  list($choiceavailable, $warnings) = choice_get_availability_status($choice);
  40  
  41  if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate
  42          and $choiceavailable) {
  43      $answercount = $DB->count_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
  44      if ($answercount > 0) {
  45          $choiceanswers = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id),
  46              '', 'id');
  47          $todelete = array_keys($choiceanswers);
  48          choice_delete_responses($todelete, $choice, $cm, $course);
  49          redirect("view.php?id=$cm->id");
  50      }
  51  }
  52  
  53  $PAGE->set_title($choice->name);
  54  $PAGE->set_heading($course->fullname);
  55  
  56  /// Submit any new data if there is any
  57  if (data_submitted() && !empty($action) && confirm_sesskey()) {
  58      $timenow = time();
  59      if (has_capability('mod/choice:deleteresponses', $context)) {
  60          if ($action === 'delete') {
  61              // Some responses need to be deleted.
  62              choice_delete_responses($attemptids, $choice, $cm, $course);
  63              redirect("view.php?id=$cm->id");
  64          }
  65          if (preg_match('/^choose_(\d+)$/', $action, $actionmatch)) {
  66              // Modify responses of other users.
  67              $newoptionid = (int)$actionmatch[1];
  68              choice_modify_responses($userids, $attemptids, $newoptionid, $choice, $cm, $course);
  69              redirect("view.php?id=$cm->id");
  70          }
  71      }
  72  
  73      // Redirection after all POSTs breaks block editing, we need to be more specific!
  74      if ($choice->allowmultiple) {
  75          $answer = optional_param_array('answer', array(), PARAM_INT);
  76      } else {
  77          $answer = optional_param('answer', '', PARAM_INT);
  78      }
  79  
  80      if (!$choiceavailable) {
  81          $reason = current(array_keys($warnings));
  82          throw new moodle_exception($reason, 'choice', '', $warnings[$reason]);
  83      }
  84  
  85      if ($answer && is_enrolled($context, null, 'mod/choice:choose')) {
  86          choice_user_submit_response($answer, $choice, $USER->id, $course, $cm);
  87          redirect(new moodle_url('/mod/choice/view.php',
  88              array('id' => $cm->id, 'notify' => 'choicesaved', 'sesskey' => sesskey())));
  89      } else if (empty($answer) and $action === 'makechoice') {
  90          // We cannot use the 'makechoice' alone because there might be some legacy renderers without it,
  91          // outdated renderers will not get the 'mustchoose' message - bad luck.
  92          redirect(new moodle_url('/mod/choice/view.php',
  93              array('id' => $cm->id, 'notify' => 'mustchooseone', 'sesskey' => sesskey())));
  94      }
  95  }
  96  
  97  // Completion and trigger events.
  98  choice_view($choice, $course, $cm, $context);
  99  
 100  echo $OUTPUT->header();
 101  echo $OUTPUT->heading(format_string($choice->name), 2, null);
 102  
 103  // Render the activity information.
 104  $completiondetails = \core_completion\cm_completion_details::get_instance($cm, $USER->id);
 105  $activitydates = \core\activity_dates::get_dates_for_module($cm, $USER->id);
 106  echo $OUTPUT->activity_information($cm, $completiondetails, $activitydates);
 107  
 108  if ($notify and confirm_sesskey()) {
 109      if ($notify === 'choicesaved') {
 110          echo $OUTPUT->notification(get_string('choicesaved', 'choice'), 'notifysuccess');
 111      } else if ($notify === 'mustchooseone') {
 112          echo $OUTPUT->notification(get_string('mustchooseone', 'choice'), 'notifyproblem');
 113      }
 114  }
 115  
 116  /// Display the choice and possibly results
 117  $eventdata = array();
 118  $eventdata['objectid'] = $choice->id;
 119  $eventdata['context'] = $context;
 120  
 121  /// Check to see if groups are being used in this choice
 122  $groupmode = groups_get_activity_groupmode($cm);
 123  
 124  if ($groupmode) {
 125      groups_get_activity_group($cm, true);
 126      groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/view.php?id='.$id);
 127  }
 128  
 129  // Check if we want to include responses from inactive users.
 130  $onlyactive = $choice->includeinactive ? false : true;
 131  
 132  $allresponses = choice_get_response_data($choice, $cm, $groupmode, $onlyactive);   // Big function, approx 6 SQL calls per user.
 133  
 134  
 135  if (has_capability('mod/choice:readresponses', $context)) {
 136      choice_show_reportlink($allresponses, $cm);
 137  }
 138  
 139  echo '<div class="clearer"></div>';
 140  
 141  if ($choice->intro) {
 142      echo $OUTPUT->box(format_module_intro('choice', $choice, $cm->id), 'generalbox', 'intro');
 143  }
 144  
 145  $timenow = time();
 146  $current = choice_get_my_response($choice);
 147  //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.
 148  if (isloggedin() && (!empty($current)) &&
 149      (empty($choice->allowupdate) || ($timenow > $choice->timeclose)) ) {
 150      $choicetexts = array();
 151      foreach ($current as $c) {
 152          $choicetexts[] = format_string(choice_get_option_text($choice, $c->optionid));
 153      }
 154      echo $OUTPUT->box(get_string("yourselection", "choice") . ": " . implode('; ', $choicetexts), 'generalbox', 'yourselection');
 155  }
 156  
 157  /// Print the form
 158  $choiceopen = true;
 159  if ((!empty($choice->timeopen)) && ($choice->timeopen > $timenow)) {
 160      if ($choice->showpreview) {
 161          echo $OUTPUT->box(get_string('previewing', 'choice'), 'generalbox alert');
 162      } else {
 163          echo $OUTPUT->footer();
 164          exit;
 165      }
 166  } else if ((!empty($choice->timeclose)) && ($timenow > $choice->timeclose)) {
 167      $choiceopen = false;
 168  }
 169  
 170  if ( (!$current or $choice->allowupdate) and $choiceopen and is_enrolled($context, NULL, 'mod/choice:choose')) {
 171  
 172      // Show information on how the results will be published to students.
 173      $publishinfo = null;
 174      switch ($choice->showresults) {
 175          case CHOICE_SHOWRESULTS_NOT:
 176              $publishinfo = get_string('publishinfonever', 'choice');
 177              break;
 178  
 179          case CHOICE_SHOWRESULTS_AFTER_ANSWER:
 180              if ($choice->publish == CHOICE_PUBLISH_ANONYMOUS) {
 181                  $publishinfo = get_string('publishinfoanonafter', 'choice');
 182              } else {
 183                  $publishinfo = get_string('publishinfofullafter', 'choice');
 184              }
 185              break;
 186  
 187          case CHOICE_SHOWRESULTS_AFTER_CLOSE:
 188              if ($choice->publish == CHOICE_PUBLISH_ANONYMOUS) {
 189                  $publishinfo = get_string('publishinfoanonclose', 'choice');
 190              } else {
 191                  $publishinfo = get_string('publishinfofullclose', 'choice');
 192              }
 193              break;
 194  
 195          default:
 196              // No need to inform the user in the case of CHOICE_SHOWRESULTS_ALWAYS since it's already obvious that the results are
 197              // being published.
 198              break;
 199      }
 200  
 201      // Show info if necessary.
 202      if (!empty($publishinfo)) {
 203          echo $OUTPUT->notification($publishinfo, 'info');
 204      }
 205  
 206      // They haven't made their choice yet or updates allowed and choice is open.
 207      $options = choice_prepare_options($choice, $USER, $cm, $allresponses);
 208      $renderer = $PAGE->get_renderer('mod_choice');
 209      echo $renderer->display_options($options, $cm->id, $choice->display, $choice->allowmultiple);
 210      $choiceformshown = true;
 211  } else {
 212      $choiceformshown = false;
 213  }
 214  
 215  if (!$choiceformshown) {
 216      $sitecontext = context_system::instance();
 217  
 218      if (isguestuser()) {
 219          // Guest account
 220          echo $OUTPUT->confirm(get_string('noguestchoose', 'choice').'<br /><br />'.get_string('liketologin'),
 221                       get_login_url(), new moodle_url('/course/view.php', array('id'=>$course->id)));
 222      } else if (!is_enrolled($context)) {
 223          // Only people enrolled can make a choice
 224          $SESSION->wantsurl = qualified_me();
 225          $SESSION->enrolcancel = get_local_referer(false);
 226  
 227          $coursecontext = context_course::instance($course->id);
 228          $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
 229  
 230          echo $OUTPUT->box_start('generalbox', 'notice');
 231          echo '<p align="center">'. get_string('notenrolledchoose', 'choice') .'</p>';
 232          echo $OUTPUT->container_start('continuebutton');
 233          echo $OUTPUT->single_button(new moodle_url('/enrol/index.php?', array('id'=>$course->id)), get_string('enrolme', 'core_enrol', $courseshortname));
 234          echo $OUTPUT->container_end();
 235          echo $OUTPUT->box_end();
 236  
 237      }
 238  }
 239  
 240  // print the results at the bottom of the screen
 241  if (choice_can_view_results($choice, $current, $choiceopen)) {
 242      $results = prepare_choice_show_results($choice, $course, $cm, $allresponses);
 243      $renderer = $PAGE->get_renderer('mod_choice');
 244      $resultstable = $renderer->display_result($results);
 245      echo $OUTPUT->box($resultstable);
 246  
 247  } else if (!$choiceformshown) {
 248      echo $OUTPUT->box(get_string('noresultsviewable', 'choice'));
 249  }
 250  
 251  echo $OUTPUT->footer();