Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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  $cm = cm_info::create($cm);
  35  
  36  if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
  37      print_error('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      print_error('invalidsurveyid', 'survey');
  48  }
  49  $trimmedintro = trim($survey->intro);
  50  if (empty($trimmedintro)) {
  51      $tempo = $DB->get_field("survey", "intro", array("id" => $survey->template));
  52      $survey->intro = get_string($tempo, "survey");
  53  }
  54  
  55  if (! $template = $DB->get_record("survey", array("id" => $survey->template))) {
  56      print_error('invalidtmptid', 'survey');
  57  }
  58  
  59  $showscales = ($template->name != 'ciqname');
  60  
  61  // Check the survey hasn't already been filled out.
  62  $surveyalreadydone = survey_already_done($survey->id, $USER->id);
  63  if ($surveyalreadydone) {
  64      // Trigger course_module_viewed event and completion.
  65      survey_view($survey, $course, $cm, $context, 'graph');
  66  } else {
  67      survey_view($survey, $course, $cm, $context, 'form');
  68  }
  69  
  70  $strsurvey = get_string("modulename", "survey");
  71  $PAGE->set_title($survey->name);
  72  $PAGE->set_heading($course->fullname);
  73  echo $OUTPUT->header();
  74  echo $OUTPUT->heading(format_string($survey->name));
  75  
  76  // Render the activity information.
  77  $completiondetails = \core_completion\cm_completion_details::get_instance($cm, $USER->id);
  78  $activitydates = \core\activity_dates::get_dates_for_module($cm, $USER->id);
  79  echo $OUTPUT->activity_information($cm, $completiondetails, $activitydates);
  80  
  81  // Check to see if groups are being used in this survey.
  82  if ($groupmode = groups_get_activity_groupmode($cm)) {   // Groups are being used.
  83      $currentgroup = groups_get_activity_group($cm);
  84  } else {
  85      $currentgroup = 0;
  86  }
  87  $groupingid = $cm->groupingid;
  88  
  89  if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
  90      $currentgroup = 0;
  91  }
  92  
  93  $currentactivitygroup = groups_get_activity_group($cm, true);
  94  if (has_capability('mod/survey:readresponses', $context) &&
  95          !($currentactivitygroup === 0 && $groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context))) {
  96  
  97      $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
  98      echo "<div class=\"reportlink\"><a href=\"report.php?id=$cm->id\">".
  99            get_string("viewsurveyresponses", "survey", $numusers)."</a></div>";
 100  } else if (!$cm->visible) {
 101      notice(get_string("activityiscurrentlyhidden"));
 102  }
 103  
 104  if (!is_enrolled($context)) {
 105      echo $OUTPUT->notification(get_string("guestsnotallowed", "survey"));
 106  }
 107  
 108  if ($surveyalreadydone) {
 109  
 110      $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
 111  
 112      if ($showscales) {
 113          // Ensure that graph.php will allow the user to see the graph.
 114          if (has_capability('mod/survey:readresponses', $context) || !$groupmode || groups_is_member($currentgroup)) {
 115  
 116              echo $OUTPUT->box(get_string("surveycompleted", "survey"));
 117              echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
 118  
 119              echo '<div class="resultgraph">';
 120              survey_print_graph("id=$cm->id&amp;sid=$USER->id&amp;group=$currentgroup&amp;type=student.png");
 121              echo '</div>';
 122          } else {
 123              echo $OUTPUT->box(get_string("surveycompletednograph", "survey"));
 124              echo $OUTPUT->box(get_string("peoplecompleted", "survey", $numusers));
 125          }
 126  
 127      } else {
 128  
 129          echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox', 'intro');
 130          echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true);  // Should be done with CSS instead.
 131  
 132          $questions = survey_get_questions($survey);
 133          foreach ($questions as $question) {
 134  
 135              if ($question->type == 0 or $question->type == 1) {
 136                  if ($answer = survey_get_user_answer($survey->id, $question->id, $USER->id)) {
 137                      $table = new html_table();
 138                      $table->head = array(get_string($question->text, "survey"));
 139                      $table->align = array ("left");
 140                      $table->data[] = array(s($answer->answer1));// No html here, just plain text.
 141                      echo html_writer::table($table);
 142                      echo $OUTPUT->spacer(array('height' => 30, 'width' => 1), true);
 143                  }
 144              }
 145          }
 146      }
 147  
 148      echo $OUTPUT->footer();
 149      exit;
 150  }
 151  
 152  echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
 153  echo '<div>';
 154  echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
 155  echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
 156  
 157  echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox boxaligncenter bowidthnormal', 'intro');
 158  echo '<div>'. get_string('allquestionrequireanswer', 'survey'). '</div>';
 159  
 160  // Get all the major questions in order.
 161  $questions = survey_get_questions($survey);
 162  
 163  global $qnum;  // TODO: ugly globals hack for survey_print_*().
 164  $qnum = 0;
 165  foreach ($questions as $question) {
 166  
 167      if ($question->type >= 0) {
 168  
 169          $question = survey_translate_question($question);
 170  
 171          if ($question->multi) {
 172              survey_print_multi($question);
 173          } else {
 174              survey_print_single($question);
 175          }
 176      }
 177  }
 178  
 179  if (!is_enrolled($context)) {
 180      echo '</div>';
 181      echo "</form>";
 182      echo $OUTPUT->footer();
 183      exit;
 184  }
 185  
 186  $PAGE->requires->js_call_amd('mod_survey/validation', 'ensureRadiosChosen', array('surveyform'));
 187  
 188  echo '<br />';
 189  echo '<input type="submit" class="btn btn-primary" value="'.get_string("clicktocontinue", "survey").'" />';
 190  echo '</div>';
 191  echo "</form>";
 192  
 193  echo $OUTPUT->footer();
 194  
 195