Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * This file is responsible for producing the survey reports
  20   *
  21   * @package   mod_survey
  22   * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once("../../config.php");
  27  require_once ("lib.php");
  28  
  29  // Check that all the parameters have been provided.
  30  
  31  $id = required_param('id', PARAM_INT);           // Course Module ID.
  32  $action = optional_param('action', '', PARAM_ALPHA); // What to look at.
  33  $qid = optional_param('qid', 0, PARAM_RAW);       // Question IDs comma-separated list.
  34  $student = optional_param('student', 0, PARAM_INT);   // Student ID.
  35  $notes = optional_param('notes', '', PARAM_RAW);    // Save teachers notes.
  36  
  37  $qids = explode(',', $qid);
  38  $qids = clean_param_array($qids, PARAM_INT);
  39  $qid = implode(',', $qids);
  40  
  41  if (!$cm = get_coursemodule_from_id('survey', $id)) {
  42      throw new moodle_exception('invalidcoursemodule');
  43  }
  44  
  45  if (!$course = $DB->get_record("course", array("id" => $cm->course))) {
  46      throw new moodle_exception('coursemisconf');
  47  }
  48  
  49  $url = new moodle_url('/mod/survey/report.php', array('id' => $id));
  50  if ($action !== '') {
  51      $url->param('action', $action);
  52  }
  53  if ($qid !== 0) {
  54      $url->param('qid', $qid);
  55  }
  56  if ($student !== 0) {
  57      $url->param('student', $student);
  58  }
  59  if ($notes !== '') {
  60      $url->param('notes', $notes);
  61  }
  62  $PAGE->set_url($url);
  63  
  64  require_login($course, false, $cm);
  65  
  66  $context = context_module::instance($cm->id);
  67  
  68  require_capability('mod/survey:readresponses', $context);
  69  
  70  if (!$survey = $DB->get_record("survey", array("id" => $cm->instance))) {
  71      throw new moodle_exception('invalidsurveyid', 'survey');
  72  }
  73  
  74  if (!$template = $DB->get_record("survey", array("id" => $survey->template))) {
  75      throw new moodle_exception('invalidtmptid', 'survey');
  76  }
  77  
  78  $showscales = ($template->name != 'ciqname');
  79  
  80  $strreport = get_string("report", "survey");
  81  $strsurvey = get_string("modulename", "survey");
  82  $strsurveys = get_string("modulenameplural", "survey");
  83  $strsummary = get_string("summary", "survey");
  84  $strscales = get_string("scales", "survey");
  85  $strquestion = get_string("question", "survey");
  86  $strquestions = get_string("questions", "survey");
  87  $strdownload = get_string("download", "survey");
  88  $strallscales = get_string("allscales", "survey");
  89  $strselectedquestions = get_string("selectedquestions", "survey");
  90  $strseemoredetail = get_string("seemoredetail", "survey");
  91  $strnotes = get_string("notes", "survey");
  92  
  93  $PAGE->set_title("$course->shortname: " . format_string($survey->name));
  94  $PAGE->set_heading($course->fullname);
  95  $PAGE->activityheader->set_attrs([
  96          'hidecompletion' => true,
  97          'description' => ''
  98  ]);
  99  
 100  // Activate the secondary nav tab.
 101  navigation_node::override_active_url(new moodle_url('/mod/survey/report.php', ['id' => $id, 'action' => 'summary']));
 102  
 103  $actionbar = new \mod_survey\output\actionbar($id, $action, $url);
 104  echo $OUTPUT->header();
 105  $renderer = $PAGE->get_renderer('mod_survey');
 106  echo $renderer->response_actionbar($actionbar);
 107  
 108  // Check to see if groups are being used in this survey.
 109  $groupmode = groups_get_activity_groupmode($cm);
 110  if ($groupmode != NOGROUPS) {
 111      $menuaction = $action == "student" ? "students" : $action;
 112  
 113      // Get the current activity group, confirm user can access.
 114      $currentgroup = groups_get_activity_group($cm, true);
 115      if ($currentgroup === 0 && $groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) {
 116          throw new moodle_exception('notingroup');
 117      }
 118  
 119      $groupsactivitymenu = groups_print_activity_menu($cm, new moodle_url('/mod/survey/report.php',
 120              ['id' => $cm->id, 'action' => $menuaction, 'qid' => $qid]), true);
 121  } else {
 122      $currentgroup = 0;
 123      $groupsactivitymenu = null;
 124  }
 125  
 126  $params = array(
 127          'objectid' => $survey->id,
 128          'context' => $context,
 129          'courseid' => $course->id,
 130          'relateduserid' => $student,
 131          'other' => array('action' => $action, 'groupid' => $currentgroup)
 132  );
 133  $event = \mod_survey\event\report_viewed::create($params);
 134  $event->trigger();
 135  
 136  if ($currentgroup) {
 137      $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $currentgroup, null, false);
 138  } else if (!empty($cm->groupingid)) {
 139      $groups = groups_get_all_groups($courseid, 0, $cm->groupingid);
 140      $groups = array_keys($groups);
 141      $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $groups, null, false);
 142  } else {
 143      $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', '', null, false);
 144      $group = false;
 145  }
 146  
 147  $groupingid = $cm->groupingid;
 148  
 149  // Print the menu across the top.
 150  
 151  $virtualscales = false;
 152  
 153  switch ($action) {
 154  
 155      case "summary":
 156          // If survey type is Critical incidents then we don't show summary report.
 157          if ($survey->template == SURVEY_CIQ) {
 158              throw new moodle_exception('cannotviewreport');
 159          }
 160          echo $OUTPUT->heading($strsummary, 3);
 161  
 162          if ($groupsactivitymenu) {
 163              echo html_writer::div($groupsactivitymenu, 'mb-2');
 164          }
 165  
 166          if (survey_count_responses($survey->id, $currentgroup, $groupingid)) {
 167              echo "<div class='reportsummary'><a href=\"report.php?action=scales&amp;id=$id\">";
 168              survey_print_graph("id=$id&amp;group=$currentgroup&amp;type=overall.png");
 169              echo "</a></div>";
 170          } else {
 171              echo $OUTPUT->notification(get_string("nobodyyet", "survey"), 'info', false);
 172          }
 173          break;
 174  
 175      case "scales":
 176          // If survey type is Critical incidents then we don't show scales report.
 177          if ($survey->template == SURVEY_CIQ) {
 178              throw new moodle_exception('cannotviewreport');
 179          }
 180          echo $OUTPUT->heading($strscales, 3);
 181  
 182          if ($groupsactivitymenu) {
 183              echo html_writer::div($groupsactivitymenu, 'mb-2');
 184          }
 185  
 186          if (!$results = survey_get_responses($survey->id, $currentgroup, $groupingid)) {
 187              echo $OUTPUT->notification(get_string("nobodyyet", "survey"), 'info', false);
 188  
 189          } else {
 190  
 191              $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
 192              $questionorder = explode(",", $survey->questions);
 193  
 194              foreach ($questionorder as $key => $val) {
 195                  $question = $questions[$val];
 196                  if ($question->type < 0) {  // We have some virtual scales.  Just show them.
 197                      $virtualscales = true;
 198                      break;
 199                  }
 200              }
 201  
 202              foreach ($questionorder as $key => $val) {
 203                  $question = $questions[$val];
 204                  if ($question->multi) {
 205                      if (!empty($virtualscales) && $question->type > 0) {  // Don't show non-virtual scales if virtual.
 206                          continue;
 207                      }
 208                      echo "<p class=\"centerpara\"><a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
 209                      survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=multiquestion.png");
 210                      echo "</a></p><br />";
 211                  }
 212              }
 213          }
 214  
 215          break;
 216  
 217      case "questions":
 218  
 219          if ($qid) {     // Just get one multi-question.
 220              $questions = $DB->get_records_select("survey_questions", "id in ($qid)");
 221              $questionorder = explode(",", $qid);
 222  
 223              if ($scale = $DB->get_records("survey_questions", array("multi" => $qid))) {
 224                  $scale = array_pop($scale);
 225                  echo $OUTPUT->heading("$scale->text - $strselectedquestions", 3);
 226              } else {
 227                  echo $OUTPUT->heading($strselectedquestions, 3);
 228              }
 229  
 230          } else {        // Get all top-level questions.
 231              $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
 232              $questionorder = explode(",", $survey->questions);
 233  
 234              echo $OUTPUT->heading($strquestions, 3);
 235          }
 236  
 237          if ($groupsactivitymenu) {
 238              echo html_writer::div($groupsactivitymenu, 'mb-2');
 239          }
 240  
 241          if (!$results = survey_get_responses($survey->id, $currentgroup, $groupingid)) {
 242              echo $OUTPUT->notification(get_string("nobodyyet", "survey"), 'info', false);
 243  
 244          } else {
 245  
 246              foreach ($questionorder as $key => $val) {
 247                  $question = $questions[$val];
 248                  if ($question->type < 0) {  // We have some virtual scales.  DON'T show them.
 249                      $virtualscales = true;
 250                      break;
 251                  }
 252              }
 253  
 254              foreach ($questionorder as $key => $val) {
 255                  $question = $questions[$val];
 256  
 257                  if ($question->type < 0) {  // We have some virtual scales.  DON'T show them.
 258                      continue;
 259                  }
 260                  $question->text = get_string($question->text, "survey");
 261  
 262                  if ($question->multi) {
 263                      echo $OUTPUT->heading($question->text . ':', 4);
 264  
 265                      $subquestions = survey_get_subquestions($question);
 266                      foreach ($subquestions as $subquestion) {
 267                          if ($subquestion->type > 0) {
 268                              echo "<p class=\"centerpara\">";
 269                              echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$subquestion->id\">";
 270                              survey_print_graph("id=$id&amp;qid=$subquestion->id&amp;group=$currentgroup&amp;type=question.png");
 271                              echo "</a></p>";
 272                          }
 273                      }
 274                  } else if ($question->type > 0) {
 275                      echo "<p class=\"centerpara\">";
 276                      echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$question->id\">";
 277                      survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=question.png");
 278                      echo "</a></p>";
 279  
 280                  } else {
 281                      $table = new html_table();
 282                      $table->head = array($question->text);
 283                      $table->align = array("left");
 284  
 285                      $contents = '<table cellpadding="15" width="100%">';
 286  
 287                      if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup, "sa.time ASC")) {
 288                          foreach ($aaa as $a) {
 289                              $contents .= "<tr>";
 290                              $contents .= '<td class="fullnamecell">' . fullname($a) . '</td>';
 291                              $contents .= '<td valign="top">' . s($a->answer1) . '</td>';
 292                              $contents .= "</tr>";
 293                          }
 294                      }
 295                      $contents .= "</table>";
 296  
 297                      $table->data[] = array($contents);
 298  
 299                      echo html_writer::table($table);
 300  
 301                      echo $OUTPUT->spacer(array('height' => 30)); // Should be done with CSS instead.
 302                  }
 303              }
 304          }
 305  
 306          break;
 307  
 308      case "question":
 309          if (!$question = $DB->get_record("survey_questions", array("id" => $qid))) {
 310              throw new \moodle_exception('cannotfindquestion', 'survey');
 311          }
 312          $question->text = get_string($question->text, "survey");
 313  
 314          $answers = explode(",", get_string($question->options, "survey"));
 315  
 316          echo $OUTPUT->heading("$strquestion: $question->text", 3);
 317  
 318          if ($groupsactivitymenu) {
 319              echo html_writer::div($groupsactivitymenu, 'mb-2');
 320          }
 321  
 322          $strname = get_string("name", "survey");
 323          $strtime = get_string("time", "survey");
 324          $stractual = get_string("actual", "survey");
 325          $strpreferred = get_string("preferred", "survey");
 326          $strdateformat = get_string("strftimedatetime");
 327  
 328          $table = new html_table();
 329          $table->head = array("", $strname, $strtime, $stractual, $strpreferred);
 330          $table->align = array("left", "left", "left", "left", "right");
 331          $table->size = array(35, "", "", "", "");
 332  
 333          if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup)) {
 334              foreach ($aaa as $a) {
 335                  if ($a->answer1) {
 336                      $answer1 = "$a->answer1 - " . $answers[$a->answer1 - 1];
 337                  } else {
 338                      $answer1 = "&nbsp;";
 339                  }
 340                  if ($a->answer2) {
 341                      $answer2 = "$a->answer2 - " . $answers[$a->answer2 - 1];
 342                  } else {
 343                      $answer2 = "&nbsp;";
 344                  }
 345                  $table->data[] = array(
 346                          $OUTPUT->user_picture($a, array('courseid' => $course->id)),
 347                          "<a href=\"report.php?id=$id&amp;action=student&amp;student=$a->userid\">" . fullname($a) . "</a>",
 348                          userdate($a->time),
 349                          s($answer1), s($answer2));
 350  
 351              }
 352          }
 353  
 354          echo html_writer::table($table);
 355  
 356          break;
 357  
 358      case "students":
 359  
 360          echo $OUTPUT->heading(get_string("analysisof", "survey", get_string('participants')), 3);
 361  
 362          if ($groupsactivitymenu) {
 363              echo html_writer::div($groupsactivitymenu, 'mb-2');
 364          }
 365  
 366          if (!$results = survey_get_responses($survey->id, $currentgroup, $groupingid)) {
 367              echo $OUTPUT->notification(get_string("nobodyyet", "survey"), 'info', false);
 368          } else {
 369              survey_print_all_responses($cm->id, $results, $course->id);
 370          }
 371  
 372          break;
 373  
 374      case "student":
 375          $user = core_user::get_user($student, '*', MUST_EXIST);
 376          if ($currentgroup && !array_key_exists($user->id, $users)) {
 377              throw new moodle_exception('usernotavailable', 'error');
 378          }
 379  
 380          echo $OUTPUT->heading(get_string("analysisof", "survey", fullname($user)), 3);
 381  
 382          if ($groupsactivitymenu) {
 383              echo html_writer::div($groupsactivitymenu, 'mb-2');
 384          }
 385  
 386          if ($notes != '' and confirm_sesskey()) {
 387              if (survey_get_analysis($survey->id, $user->id)) {
 388                  if (!survey_update_analysis($survey->id, $user->id, $notes)) {
 389                      echo $OUTPUT->notification(get_string("errorunabletosavenotes", "survey"), "notifyproblem");
 390                  } else {
 391                      echo $OUTPUT->notification(get_string("savednotes", "survey"), "notifysuccess");
 392                  }
 393              } else {
 394                  if (!survey_add_analysis($survey->id, $user->id, $notes)) {
 395                      echo $OUTPUT->notification(get_string("errorunabletosavenotes", "survey"), "notifyproblem");
 396                  } else {
 397                      echo $OUTPUT->notification(get_string("savednotes", "survey"), "notifysuccess");
 398                  }
 399              }
 400          }
 401  
 402          echo "<p class=\"centerpara\">";
 403          echo $OUTPUT->user_picture($user, array('courseid' => $course->id));
 404          echo "</p>";
 405  
 406          $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
 407          $questionorder = explode(",", $survey->questions);
 408  
 409          if ($showscales) {
 410              // Print overall summary.
 411              echo "<p class=\"centerpara\">";
 412              survey_print_graph("id=$id&amp;sid=$student&amp;type=student.png");
 413              echo "</p>";
 414  
 415              // Print scales.
 416  
 417              foreach ($questionorder as $key => $val) {
 418                  $question = $questions[$val];
 419                  if ($question->type < 0) {  // We have some virtual scales.  Just show them.
 420                      $virtualscales = true;
 421                      break;
 422                  }
 423              }
 424  
 425              foreach ($questionorder as $key => $val) {
 426                  $question = $questions[$val];
 427                  if ($question->multi) {
 428                      if ($virtualscales && $question->type > 0) {  // Don't show non-virtual scales if virtual.
 429                          continue;
 430                      }
 431                      echo "<p class=\"centerpara\">";
 432                      echo "<a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
 433                      survey_print_graph("id=$id&amp;qid=$question->id&amp;sid=$student&amp;type=studentmultiquestion.png");
 434                      echo "</a></p><br />";
 435                  }
 436              }
 437          }
 438  
 439          // Print non-scale questions.
 440  
 441          foreach ($questionorder as $key => $val) {
 442              $question = $questions[$val];
 443              if ($question->type == 0 or $question->type == 1) {
 444                  if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
 445                      $table = new html_table();
 446                      $table->head = array(get_string($question->text, "survey"));
 447                      $table->align = array("left");
 448                      if (!empty($question->options) && $answer->answer1 > 0) {
 449                          $answers = explode(',', get_string($question->options, 'survey'));
 450                          if ($answer->answer1 <= count($answers)) {
 451                              $table->data[] = array(s($answers[$answer->answer1 - 1])); // No html here, just plain text.
 452                          } else {
 453                              $table->data[] = array(s($answer->answer1)); // No html here, just plain text.
 454                          }
 455                      } else {
 456                          $table->data[] = array(s($answer->answer1)); // No html here, just plain text.
 457                      }
 458                      echo html_writer::table($table);
 459                      echo $OUTPUT->spacer(array('height' => 30));
 460                  }
 461              }
 462          }
 463  
 464          if ($rs = survey_get_analysis($survey->id, $user->id)) {
 465              $notes = $rs->notes;
 466          } else {
 467              $notes = "";
 468          }
 469          echo "<hr noshade=\"noshade\" size=\"1\" />";
 470          echo "<div class='studentreport'>";
 471          echo "<form action=\"report.php\" method=\"post\">";
 472          echo "<h3>$strnotes:</h3>";
 473          echo "<blockquote>";
 474          echo "<textarea class=\"form-control\" name=\"notes\" rows=\"10\" cols=\"60\">";
 475          p($notes);
 476          echo "</textarea><br />";
 477          echo "<input type=\"hidden\" name=\"action\" value=\"student\" />";
 478          echo "<input type=\"hidden\" name=\"sesskey\" value=\"" . sesskey() . "\" />";
 479          echo "<input type=\"hidden\" name=\"student\" value=\"$student\" />";
 480          echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
 481          echo "<input type=\"submit\" class=\"btn btn-primary\" value=\"" . get_string("savechanges") . "\" />";
 482          echo "</blockquote>";
 483          echo "</form>";
 484          echo "</div>";
 485  
 486          break;
 487  
 488      case "download":
 489          echo $OUTPUT->heading($strdownload, 3);
 490  
 491          if ($groupsactivitymenu) {
 492              echo html_writer::div($groupsactivitymenu, 'mb-2');
 493          }
 494  
 495          require_capability('mod/survey:download', $context);
 496  
 497          $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
 498          if ($numusers > 0) {
 499              echo html_writer::tag('p', get_string("downloadinfo", "survey"), array('class' => 'centerpara'));
 500  
 501              echo $OUTPUT->container_start('reportbuttons');
 502              $options = array();
 503              $options["id"] = "$cm->id";
 504              $options["group"] = $currentgroup;
 505  
 506              $options["type"] = "ods";
 507              echo $OUTPUT->single_button(new moodle_url("download.php", $options), get_string("downloadods"));
 508  
 509              $options["type"] = "xls";
 510              echo $OUTPUT->single_button(new moodle_url("download.php", $options), get_string("downloadexcel"));
 511  
 512              $options["type"] = "txt";
 513              echo $OUTPUT->single_button(new moodle_url("download.php", $options), get_string("downloadtext"));
 514              echo $OUTPUT->container_end();
 515  
 516          } else {
 517              echo $OUTPUT->notification(get_string("nobodyyet", "survey"), 'info', false);
 518          }
 519  
 520          break;
 521  
 522      default:
 523          throw new moodle_exception('cannotviewreport');
 524  
 525  }
 526  echo $OUTPUT->footer();