Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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      throw new \moodle_exception('invalidcoursemodule');
  32  }
  33  
  34  $cm = cm_info::create($cm);
  35  
  36  if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
  37      throw new \moodle_exception('coursemisconf');
  38  }
  39  
  40  $PAGE->set_url('/mod/survey/view.php', array('id' => $id));
  41  require_login($course, false, $cm);
  42  $context = context_module::instance($cm->id);
  43  
  44  require_capability('mod/survey:participate', $context);
  45  
  46  if (! $survey = $DB->get_record("survey", array("id" => $cm->instance))) {
  47      throw new \moodle_exception('invalidsurveyid', 'survey');
  48  }
  49  
  50  if (! $template = $DB->get_record("survey", array("id" => $survey->template))) {
  51      throw new \moodle_exception('invalidtmptid', 'survey');
  52  }
  53  
  54  $showscales = ($template->name != 'ciqname');
  55  
  56  // Check the survey hasn't already been filled out.
  57  $surveyalreadydone = survey_already_done($survey->id, $USER->id);
  58  if ($surveyalreadydone) {
  59      // Trigger course_module_viewed event and completion.
  60      survey_view($survey, $course, $cm, $context, 'graph');
  61  } else {
  62      survey_view($survey, $course, $cm, $context, 'form');
  63  }
  64  
  65  $strsurvey = get_string("modulename", "survey");
  66  $PAGE->set_title($survey->name);
  67  $PAGE->set_heading($course->fullname);
  68  // No need to show the description if the survey is done and a graph page is to be shown.
  69  if ($surveyalreadydone && $showscales) {
  70      $PAGE->activityheader->set_description('');
  71  } else {
  72      // If the survey has empty description, display the default one.
  73      $trimmedintro = trim($survey->intro);
  74      if (empty($trimmedintro)) {
  75          $tempo = $DB->get_field("survey", "intro", array("id" => $survey->template));
  76          $PAGE->activityheader->set_description(get_string($tempo, "survey"));
  77      }
  78  }
  79  $PAGE->add_body_class('limitedwidth');
  80  
  81  echo $OUTPUT->header();
  82  
  83  // Check to see if groups are being used in this survey.
  84  if ($groupmode = groups_get_activity_groupmode($cm)) {   // Groups are being used.
  85      $currentgroup = groups_get_activity_group($cm);
  86  } else {
  87      $currentgroup = 0;
  88  }
  89  $groupingid = $cm->groupingid;
  90  
  91  if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
  92      $currentgroup = 0;
  93  }
  94  
  95  if (!$cm->visible) {
  96      notice(get_string("activityiscurrentlyhidden"));
  97  }
  98  
  99  if (!is_enrolled($context)) {
 100      echo $OUTPUT->notification(get_string("guestsnotallowed", "survey"));
 101  }
 102  
 103  if ($surveyalreadydone) {
 104      $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
 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->spacer(array('height' => 30, 'width' => 1), true);  // Should be done with CSS instead.
 123  
 124          $questions = survey_get_questions($survey);
 125          foreach ($questions as $question) {
 126  
 127              if ($question->type == 0 or $question->type == 1) {
 128                  if ($answer = survey_get_user_answer($survey->id, $question->id, $USER->id)) {
 129                      $table = new html_table();
 130                      $table->head = array(get_string($question->text, "survey"));
 131                      $table->align = array ("left");
 132                      $table->data[] = array(s($answer->answer1));// No html here, just plain text.
 133                      echo html_writer::table($table);
 134                      echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true);
 135                  }
 136              }
 137          }
 138      }
 139  
 140      echo $OUTPUT->footer();
 141      exit;
 142  }
 143  
 144  echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
 145  echo '<div>';
 146  echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
 147  echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
 148  
 149  echo '<div>'. get_string('allquestionrequireanswer', 'survey'). '</div>';
 150  
 151  // Get all the major questions in order.
 152  $questions = survey_get_questions($survey);
 153  
 154  global $qnum;  // TODO: ugly globals hack for survey_print_*().
 155  $qnum = 0;
 156  foreach ($questions as $question) {
 157  
 158      if ($question->type >= 0) {
 159  
 160          $question = survey_translate_question($question);
 161  
 162          if ($question->multi) {
 163              survey_print_multi($question);
 164          } else {
 165              survey_print_single($question);
 166          }
 167      }
 168  }
 169  
 170  if (!is_enrolled($context)) {
 171      echo '</div>';
 172      echo "</form>";
 173      echo $OUTPUT->footer();
 174      exit;
 175  }
 176  
 177  $PAGE->requires->js_call_amd('mod_survey/validation', 'ensureRadiosChosen', array('surveyform'));
 178  
 179  echo '<br />';
 180  echo '<input type="submit" class="btn btn-primary" value="'. get_string("submit"). '" />';
 181  echo '</div>';
 182  echo "</form>";
 183  
 184  echo $OUTPUT->footer();