Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  if (has_capability('mod/survey:readresponses', $context)) {
  87      $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
  88      echo "<div class=\"reportlink\"><a href=\"report.php?id=$cm->id\">".
  89            get_string("viewsurveyresponses", "survey", $numusers)."</a></div>";
  90  } else if (!$cm->visible) {
  91      notice(get_string("activityiscurrentlyhidden"));
  92  }
  93  
  94  if (!is_enrolled($context)) {
  95      echo $OUTPUT->notification(get_string("guestsnotallowed", "survey"));
  96  }
  97  
  98  if ($surveyalreadydone) {
  99  
 100      $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
 101  
 102      if ($showscales) {
 103          // Ensure that graph.php will allow the user to see the graph.
 104          if (has_capability('mod/survey:readresponses', $context) || !$groupmode || groups_is_member($currentgroup)) {
 105  
 106              echo $OUTPUT->box(get_string("surveycompleted", "survey"));
 107              echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
 108  
 109              echo '<div class="resultgraph">';
 110              survey_print_graph("id=$cm->id&amp;sid=$USER->id&amp;group=$currentgroup&amp;type=student.png");
 111              echo '</div>';
 112          } else {
 113              echo $OUTPUT->box(get_string("surveycompletednograph", "survey"));
 114              echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
 115          }
 116  
 117      } else {
 118  
 119          echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox', 'intro');
 120          echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true);  // Should be done with CSS instead.
 121  
 122          $questions = survey_get_questions($survey);
 123          foreach ($questions as $question) {
 124  
 125              if ($question->type == 0 or $question->type == 1) {
 126                  if ($answer = survey_get_user_answer($survey->id, $question->id, $USER->id)) {
 127                      $table = new html_table();
 128                      $table->head = array(get_string($question->text, "survey"));
 129                      $table->align = array ("left");
 130                      $table->data[] = array(s($answer->answer1));// No html here, just plain text.
 131                      echo html_writer::table($table);
 132                      echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true);
 133                  }
 134              }
 135          }
 136      }
 137  
 138      echo $OUTPUT->footer();
 139      exit;
 140  }
 141  
 142  echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
 143  echo '<div>';
 144  echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
 145  echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
 146  
 147  echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox boxaligncenter bowidthnormal', 'intro');
 148  echo '<div>'. get_string('allquestionrequireanswer', 'survey'). '</div>';
 149  
 150  // Get all the major questions in order.
 151  $questions = survey_get_questions($survey);
 152  
 153  global $qnum;  // TODO: ugly globals hack for survey_print_*().
 154  $qnum = 0;
 155  foreach ($questions as $question) {
 156  
 157      if ($question->type >= 0) {
 158  
 159          $question = survey_translate_question($question);
 160  
 161          if ($question->multi) {
 162              survey_print_multi($question);
 163          } else {
 164              survey_print_single($question);
 165          }
 166      }
 167  }
 168  
 169  if (!is_enrolled($context)) {
 170      echo '</div>';
 171      echo "</form>";
 172      echo $OUTPUT->footer();
 173      exit;
 174  }
 175  
 176  $PAGE->requires->js_call_amd('mod_survey/validation', 'ensureRadiosChosen', array('surveyform'));
 177  
 178  echo '<br />';
 179  echo '<input type="submit" class="btn btn-primary" value="'.get_string("clicktocontinue", "survey").'" />';
 180  echo '</div>';
 181  echo "</form>";
 182  
 183  echo $OUTPUT->footer();
 184  
 185