Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 29] [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       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_ALPHA);
       9  $attemptids = optional_param_array('attemptid', array(), PARAM_INT); // array of attempt ids for delete action
      10  $notify     = optional_param('notify', '', PARAM_ALPHA);
      11  
      12  $url = new moodle_url('/mod/choice/view.php', array('id'=>$id));
      13  if ($action !== '') {
      14      $url->param('action', $action);
      15  }
      16  $PAGE->set_url($url);
      17  
      18  if (! $cm = get_coursemodule_from_id('choice', $id)) {
      19      print_error('invalidcoursemodule');
      20  }
      21  
      22  if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
      23      print_error('coursemisconf');
      24  }
      25  
      26  require_course_login($course, false, $cm);
      27  
      28  if (!$choice = choice_get_choice($cm->instance)) {
      29      print_error('invalidcoursemodule');
      30  }
      31  
      32  $strchoice = get_string('modulename', 'choice');
      33  $strchoices = get_string('modulenameplural', 'choice');
      34  
      35  $context = context_module::instance($cm->id);
      36  
      37  list($choiceavailable, $warnings) = choice_get_availability_status($choice);
      38  
      39  if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, 'mod/choice:choose') and $choice->allowupdate
      40          and $choiceavailable) {
      41      $answercount = $DB->count_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
      42      if ($answercount > 0) {
      43          $DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
      44  
      45          // Update completion state
      46          $completion = new completion_info($course);
      47          if ($completion->is_enabled($cm) && $choice->completionsubmit) {
      48              $completion->update_state($cm, COMPLETION_INCOMPLETE);
      49          }
      50          redirect("view.php?id=$cm->id");
      51      }
      52  }
      53  
      54  $PAGE->set_title($choice->name);
      55  $PAGE->set_heading($course->fullname);
      56  
      57  // Mark viewed by user (if required)
      58  $completion = new completion_info($course);
      59  $completion->set_module_viewed($cm);
      60  
      61  /// Submit any new data if there is any
      62  if (data_submitted() && is_enrolled($context, NULL, 'mod/choice:choose') && confirm_sesskey()) {
      63      $timenow = time();
      64      if (has_capability('mod/choice:deleteresponses', $context) && $action == 'delete') {
      65          //some responses need to be deleted
      66          choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses.
      67          redirect("view.php?id=$cm->id");
      68      }
      69  
      70      // Redirection after all POSTs breaks block editing, we need to be more specific!
      71      if ($choice->allowmultiple) {
      72          $answer = optional_param_array('answer', array(), PARAM_INT);
      73      } else {
      74          $answer = optional_param('answer', '', PARAM_INT);
      75      }
      76  
      77      if (!$choiceavailable) {
      78          $reason = current(array_keys($warnings));
      79          throw new moodle_exception($reason, 'choice', '', $warnings[$reason]);
      80      }
      81  
      82      if ($answer) {
      83          choice_user_submit_response($answer, $choice, $USER->id, $course, $cm);
      84          redirect(new moodle_url('/mod/choice/view.php',
      85              array('id' => $cm->id, 'notify' => 'choicesaved', 'sesskey' => sesskey())));
      86      } else if (empty($answer) and $action === 'makechoice') {
      87          // We cannot use the 'makechoice' alone because there might be some legacy renderers without it,
      88          // outdated renderers will not get the 'mustchoose' message - bad luck.
      89          redirect(new moodle_url('/mod/choice/view.php',
      90              array('id' => $cm->id, 'notify' => 'mustchooseone', 'sesskey' => sesskey())));
      91      }
      92  }
      93  
      94  echo $OUTPUT->header();
      95  echo $OUTPUT->heading(format_string($choice->name), 2, null);
      96  
      97  if ($notify and confirm_sesskey()) {
      98      if ($notify === 'choicesaved') {
      99          echo $OUTPUT->notification(get_string('choicesaved', 'choice'), 'notifysuccess');
     100      } else if ($notify === 'mustchooseone') {
     101          echo $OUTPUT->notification(get_string('mustchooseone', 'choice'), 'notifyproblem');
     102      }
     103  }
     104  
     105  /// Display the choice and possibly results
     106  $eventdata = array();
     107  $eventdata['objectid'] = $choice->id;
     108  $eventdata['context'] = $context;
     109  
     110  $event = \mod_choice\event\course_module_viewed::create($eventdata);
     111  $event->add_record_snapshot('course_modules', $cm);
     112  $event->add_record_snapshot('course', $course);
     113  $event->trigger();
     114  
     115  /// Check to see if groups are being used in this choice
     116  $groupmode = groups_get_activity_groupmode($cm);
     117  
     118  if ($groupmode) {
     119      groups_get_activity_group($cm, true);
     120      groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/view.php?id='.$id);
     121  }
     122  $allresponses = choice_get_response_data($choice, $cm, $groupmode);   // Big function, approx 6 SQL calls per user
     123  
     124  
     125  if (has_capability('mod/choice:readresponses', $context)) {
     126      choice_show_reportlink($allresponses, $cm);
     127  }
     128  
     129  echo '<div class="clearer"></div>';
     130  
     131  if ($choice->intro) {
     132      echo $OUTPUT->box(format_module_intro('choice', $choice, $cm->id), 'generalbox', 'intro');
     133  }
     134  
     135  $timenow = time();
     136  $current = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id));
     137  //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.
     138  if (isloggedin() && (!empty($current)) &&
     139      (empty($choice->allowupdate) || ($timenow > $choice->timeclose)) ) {
     140      $choicetexts = array();
     141      foreach ($current as $c) {
     142          $choicetexts[] = format_string(choice_get_option_text($choice, $c->optionid));
     143      }
     144      echo $OUTPUT->box(get_string("yourselection", "choice", userdate($choice->timeopen)).": ".implode('; ', $choicetexts), 'generalbox', 'yourselection');
     145  }
     146  
     147  /// Print the form
     148  $choiceopen = true;
     149  if ($choice->timeclose !=0) {
     150      if ($choice->timeopen > $timenow ) {
     151          echo $OUTPUT->box(get_string("notopenyet", "choice", userdate($choice->timeopen)), "generalbox notopenyet");
     152          echo $OUTPUT->footer();
     153          exit;
     154      } else if ($timenow > $choice->timeclose) {
     155          echo $OUTPUT->box(get_string("expired", "choice", userdate($choice->timeclose)), "generalbox expired");
     156          $choiceopen = false;
     157      }
     158  }
     159  
     160  if ( (!$current or $choice->allowupdate) and $choiceopen and is_enrolled($context, NULL, 'mod/choice:choose')) {
     161  // They haven't made their choice yet or updates allowed and choice is open
     162  
     163      $options = choice_prepare_options($choice, $USER, $cm, $allresponses);
     164      $renderer = $PAGE->get_renderer('mod_choice');
     165      echo $renderer->display_options($options, $cm->id, $choice->display, $choice->allowmultiple);
     166      $choiceformshown = true;
     167  } else {
     168      $choiceformshown = false;
     169  }
     170  
     171  if (!$choiceformshown) {
     172      $sitecontext = context_system::instance();
     173  
     174      if (isguestuser()) {
     175          // Guest account
     176          echo $OUTPUT->confirm(get_string('noguestchoose', 'choice').'<br /><br />'.get_string('liketologin'),
     177                       get_login_url(), new moodle_url('/course/view.php', array('id'=>$course->id)));
     178      } else if (!is_enrolled($context)) {
     179          // Only people enrolled can make a choice
     180          $SESSION->wantsurl = qualified_me();
     181          $SESSION->enrolcancel = get_local_referer(false);
     182  
     183          $coursecontext = context_course::instance($course->id);
     184          $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
     185  
     186          echo $OUTPUT->box_start('generalbox', 'notice');
     187          echo '<p align="center">'. get_string('notenrolledchoose', 'choice') .'</p>';
     188          echo $OUTPUT->container_start('continuebutton');
     189          echo $OUTPUT->single_button(new moodle_url('/enrol/index.php?', array('id'=>$course->id)), get_string('enrolme', 'core_enrol', $courseshortname));
     190          echo $OUTPUT->container_end();
     191          echo $OUTPUT->box_end();
     192  
     193      }
     194  }
     195  
     196  // print the results at the bottom of the screen
     197  if ( $choice->showresults == CHOICE_SHOWRESULTS_ALWAYS or
     198      ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_ANSWER and $current) or
     199      ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_CLOSE and !$choiceopen)) {
     200  
     201      if (!empty($choice->showunanswered)) {
     202          $choice->option[0] = get_string('notanswered', 'choice');
     203          $choice->maxanswers[0] = 0;
     204      }
     205      $results = prepare_choice_show_results($choice, $course, $cm, $allresponses);
     206      $renderer = $PAGE->get_renderer('mod_choice');
     207      echo $renderer->display_result($results);
     208  
     209  } else if (!$choiceformshown) {
     210      echo $OUTPUT->box(get_string('noresultsviewable', 'choice'));
     211  }
     212  
     213  echo $OUTPUT->footer();
    

    Search This Site: