Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This file is responsible for displaying the survey
  19   *
  20   * @package   mod_survey
  21   * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once("../../config.php");
  26  require_once ("lib.php");
  27  
  28  $id = required_param('id', PARAM_INT);    // Course Module ID.
  29  
  30  if (! $cm = get_coursemodule_from_id('survey', $id)) {
  31      print_error('invalidcoursemodule');
  32  }
  33  
  34  if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
  35      print_error('coursemisconf');
  36  }
  37  
  38  $PAGE->set_url('/mod/survey/view.php', array('id' => $id));
  39  require_login($course, false, $cm);
  40  $context = context_module::instance($cm->id);
  41  
  42  require_capability('mod/survey:participate', $context);
  43  
  44  if (! $survey = $DB->get_record("survey", array("id" => $cm->instance))) {
  45      print_error('invalidsurveyid', 'survey');
  46  }
  47  $trimmedintro = trim($survey->intro);
  48  if (empty($trimmedintro)) {
  49      $tempo = $DB->get_field("survey", "intro", array("id" => $survey->template));
  50      $survey->intro = get_string($tempo, "survey");
  51  }
  52  
  53  if (! $template = $DB->get_record("survey", array("id" => $survey->template))) {
  54      print_error('invalidtmptid', 'survey');
  55  }
  56  
  57  $showscales = ($template->name != 'ciqname');
  58  
  59  // Check the survey hasn't already been filled out.
  60  $surveyalreadydone = survey_already_done($survey->id, $USER->id);
  61  if ($surveyalreadydone) {
  62      // Trigger course_module_viewed event and completion.
  63      survey_view($survey, $course, $cm, $context, 'graph');
  64  } else {
  65      survey_view($survey, $course, $cm, $context, 'form');
  66  }
  67  
  68  $strsurvey = get_string("modulename", "survey");
  69  $PAGE->set_title($survey->name);
  70  $PAGE->set_heading($course->fullname);
  71  echo $OUTPUT->header();
  72  echo $OUTPUT->heading(format_string($survey->name));
  73  
  74  // Check to see if groups are being used in this survey.
  75  if ($groupmode = groups_get_activity_groupmode($cm)) {   // Groups are being used.
  76      $currentgroup = groups_get_activity_group($cm);
  77  } else {
  78      $currentgroup = 0;
  79  }
  80  $groupingid = $cm->groupingid;
  81  
  82  if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
  83      $currentgroup = 0;
  84  }
  85  
  86  $currentactivitygroup = groups_get_activity_group($cm, true);
  87  if (has_capability('mod/survey:readresponses', $context) &&
  88          !($currentactivitygroup === 0 && $groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context))) {
  89  
  90      $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
  91      echo "<div class=\"reportlink\"><a href=\"report.php?id=$cm->id\">".
  92            get_string("viewsurveyresponses", "survey", $numusers)."</a></div>";
  93  } else if (!$cm->visible) {
  94      notice(get_string("activityiscurrentlyhidden"));
  95  }
  96  
  97  if (!is_enrolled($context)) {
  98      echo $OUTPUT->notification(get_string("guestsnotallowed", "survey"));
  99  }
 100  
 101  if ($surveyalreadydone) {
 102  
 103      $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
 104  
 105      if ($showscales) {
 106          // Ensure that graph.php will allow the user to see the graph.
 107          if (has_capability('mod/survey:readresponses', $context) || !$groupmode || groups_is_member($currentgroup)) {
 108  
 109              echo $OUTPUT->box(get_string("surveycompleted", "survey"));
 110              echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
 111  
 112              echo '<div class="resultgraph">';
 113              survey_print_graph("id=$cm->id&amp;sid=$USER->id&amp;group=$currentgroup&amp;type=student.png");
 114              echo '</div>';
 115          } else {
 116              echo $OUTPUT->box(get_string("surveycompletednograph", "survey"));
 117              echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
 118          }
 119  
 120      } else {
 121  
 122          echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox', 'intro');
 123          echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true);  // Should be done with CSS instead.
 124  
 125          $questions = survey_get_questions($survey);
 126          foreach ($questions as $question) {
 127  
 128              if ($question->type == 0 or $question->type == 1) {
 129                  if ($answer = survey_get_user_answer($survey->id, $question->id, $USER->id)) {
 130                      $table = new html_table();
 131                      $table->head = array(get_string($question->text, "survey"));
 132                      $table->align = array ("left");
 133                      $table->data[] = array(s($answer->answer1));// No html here, just plain text.
 134                      echo html_writer::table($table);
 135                      echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true);
 136                  }
 137              }
 138          }
 139      }
 140  
 141      echo $OUTPUT->footer();
 142      exit;
 143  }
 144  
 145  echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
 146  echo '<div>';
 147  echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
 148  echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
 149  
 150  echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox boxaligncenter bowidthnormal', 'intro');
 151  echo '<div>'. get_string('allquestionrequireanswer', 'survey'). '</div>';
 152  
 153  // Get all the major questions in order.
 154  $questions = survey_get_questions($survey);
 155  
 156  global $qnum;  // TODO: ugly globals hack for survey_print_*().
 157  $qnum = 0;
 158  foreach ($questions as $question) {
 159  
 160      if ($question->type >= 0) {
 161  
 162          $question = survey_translate_question($question);
 163  
 164          if ($question->multi) {
 165              survey_print_multi($question);
 166          } else {
 167              survey_print_single($question);
 168          }
 169      }
 170  }
 171  
 172  if (!is_enrolled($context)) {
 173      echo '</div>';
 174      echo "</form>";
 175      echo $OUTPUT->footer();
 176      exit;
 177  }
 178  
 179  $PAGE->requires->js_call_amd('mod_survey/validation', 'ensureRadiosChosen', array('surveyform'));
 180  
 181  echo '<br />';
 182  echo '<input type="submit" class="btn btn-primary" value="'.get_string("clicktocontinue", "survey").'" />';
 183  echo '</div>';
 184  echo "</form>";
 185  
 186  echo $OUTPUT->footer();
 187  
 188