[ Index ]

PHP Cross Reference of Unnamed Project

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1