[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/choice/ -> view.php (source)

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1