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  
   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  $strallquestions = get_string("allquestions", "survey");
  90  $strselectedquestions = get_string("selectedquestions", "survey");
  91  $strseemoredetail = get_string("seemoredetail", "survey");
  92  $strnotes = get_string("notes", "survey");
  93  
  94  switch ($action) {
  95      case 'download':
  96          $PAGE->navbar->add(get_string('downloadresults', 'survey'));
  97          break;
  98      case 'summary':
  99          if ($survey->template == SURVEY_CIQ) {
 100              throw new moodle_exception('cannotviewreport');
 101          }
 102      case 'scales':
 103          if ($survey->template == SURVEY_CIQ) {
 104              throw new moodle_exception('cannotviewreport');
 105          }
 106      case 'questions':
 107          $PAGE->navbar->add($strreport);
 108          $PAGE->navbar->add(${'str' . $action});
 109          break;
 110      case 'students':
 111          $PAGE->navbar->add($strreport);
 112          $PAGE->navbar->add(get_string('participants'));
 113          break;
 114      case '':
 115          $PAGE->navbar->add($strreport);
 116          $PAGE->navbar->add($strsummary);
 117          break;
 118      default:
 119          $PAGE->navbar->add($strreport);
 120          break;
 121  }
 122  
 123  $PAGE->set_title("$course->shortname: " . format_string($survey->name));
 124  $PAGE->set_heading($course->fullname);
 125  echo $OUTPUT->header();
 126  echo $OUTPUT->heading(format_string($survey->name));
 127  
 128  // Check to see if groups are being used in this survey.
 129  $groupmode = groups_get_activity_groupmode($cm);
 130  if ($groupmode != NOGROUPS) {
 131      $menuaction = $action == "student" ? "students" : $action;
 132  
 133      // Get the current activity group, confirm user can access.
 134      $currentgroup = groups_get_activity_group($cm, true);
 135      if ($currentgroup === 0 && $groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context)) {
 136          throw new moodle_exception('notingroup');
 137      }
 138  
 139      groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/survey/report.php?id=$cm->id&amp;action=$menuaction&amp;qid=$qid");
 140  } else {
 141      $currentgroup = 0;
 142  }
 143  
 144  $params = array(
 145          'objectid' => $survey->id,
 146          'context' => $context,
 147          'courseid' => $course->id,
 148          'relateduserid' => $student,
 149          'other' => array('action' => $action, 'groupid' => $currentgroup)
 150  );
 151  $event = \mod_survey\event\report_viewed::create($params);
 152  $event->trigger();
 153  
 154  if ($currentgroup) {
 155      $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $currentgroup, null, false);
 156  } else if (!empty($cm->groupingid)) {
 157      $groups = groups_get_all_groups($courseid, 0, $cm->groupingid);
 158      $groups = array_keys($groups);
 159      $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $groups, null, false);
 160  } else {
 161      $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', '', null, false);
 162      $group = false;
 163  }
 164  
 165  $groupingid = $cm->groupingid;
 166  
 167  echo $OUTPUT->box_start("generalbox boxaligncenter");
 168  if ($showscales) {
 169      echo "<a href=\"report.php?action=summary&amp;id=$id\">$strsummary</a>";
 170      echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=scales&amp;id=$id\">$strscales</a>";
 171      echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=questions&amp;id=$id\">$strquestions</a>";
 172      echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=students&amp;id=$id\">" . get_string('participants') . "</a>";
 173      if (has_capability('mod/survey:download', $context)) {
 174          echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=download&amp;id=$id\">$strdownload</a>";
 175      }
 176      if (empty($action)) {
 177          $action = "summary";
 178      }
 179  } else {
 180      echo "<a href=\"report.php?action=questions&amp;id=$id\">$strquestions</a>";
 181      echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=students&amp;id=$id\">" . get_string('participants') . "</a>";
 182      if (has_capability('mod/survey:download', $context)) {
 183          echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"report.php?action=download&amp;id=$id\">$strdownload</a>";
 184      }
 185      if (empty($action)) {
 186          $action = "questions";
 187      }
 188  }
 189  echo $OUTPUT->box_end();
 190  
 191  echo $OUTPUT->spacer(array('height' => 30, 'width' => 30, 'br' => true)); // Should be done with CSS instead.
 192  
 193  // Print the menu across the top.
 194  
 195  $virtualscales = false;
 196  
 197  switch ($action) {
 198  
 199      case "summary":
 200          echo $OUTPUT->heading($strsummary, 3);
 201  
 202          if (survey_count_responses($survey->id, $currentgroup, $groupingid)) {
 203              echo "<div class='reportsummary'><a href=\"report.php?action=scales&amp;id=$id\">";
 204              survey_print_graph("id=$id&amp;group=$currentgroup&amp;type=overall.png");
 205              echo "</a></div>";
 206          } else {
 207              echo $OUTPUT->notification(get_string("nobodyyet", "survey"));
 208          }
 209          break;
 210  
 211      case "scales":
 212          echo $OUTPUT->heading($strscales, 3);
 213  
 214          if (!$results = survey_get_responses($survey->id, $currentgroup, $groupingid)) {
 215              echo $OUTPUT->notification(get_string("nobodyyet", "survey"));
 216  
 217          } else {
 218  
 219              $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
 220              $questionorder = explode(",", $survey->questions);
 221  
 222              foreach ($questionorder as $key => $val) {
 223                  $question = $questions[$val];
 224                  if ($question->type < 0) {  // We have some virtual scales.  Just show them.
 225                      $virtualscales = true;
 226                      break;
 227                  }
 228              }
 229  
 230              foreach ($questionorder as $key => $val) {
 231                  $question = $questions[$val];
 232                  if ($question->multi) {
 233                      if (!empty($virtualscales) && $question->type > 0) {  // Don't show non-virtual scales if virtual.
 234                          continue;
 235                      }
 236                      echo "<p class=\"centerpara\"><a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
 237                      survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=multiquestion.png");
 238                      echo "</a></p><br />";
 239                  }
 240              }
 241          }
 242  
 243          break;
 244  
 245      case "questions":
 246  
 247          if ($qid) {     // Just get one multi-question.
 248              $questions = $DB->get_records_select("survey_questions", "id in ($qid)");
 249              $questionorder = explode(",", $qid);
 250  
 251              if ($scale = $DB->get_records("survey_questions", array("multi" => $qid))) {
 252                  $scale = array_pop($scale);
 253                  echo $OUTPUT->heading("$scale->text - $strselectedquestions", 3);
 254              } else {
 255                  echo $OUTPUT->heading($strselectedquestions, 3);
 256              }
 257  
 258          } else {        // Get all top-level questions.
 259              $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
 260              $questionorder = explode(",", $survey->questions);
 261  
 262              echo $OUTPUT->heading($strquestions, 3);
 263          }
 264  
 265          if (!$results = survey_get_responses($survey->id, $currentgroup, $groupingid)) {
 266              echo $OUTPUT->notification(get_string("nobodyyet", "survey"));
 267  
 268          } else {
 269  
 270              foreach ($questionorder as $key => $val) {
 271                  $question = $questions[$val];
 272                  if ($question->type < 0) {  // We have some virtual scales.  DON'T show them.
 273                      $virtualscales = true;
 274                      break;
 275                  }
 276              }
 277  
 278              foreach ($questionorder as $key => $val) {
 279                  $question = $questions[$val];
 280  
 281                  if ($question->type < 0) {  // We have some virtual scales.  DON'T show them.
 282                      continue;
 283                  }
 284                  $question->text = get_string($question->text, "survey");
 285  
 286                  if ($question->multi) {
 287                      echo $OUTPUT->heading($question->text . ':', 4);
 288  
 289                      $subquestions = survey_get_subquestions($question);
 290                      foreach ($subquestions as $subquestion) {
 291                          if ($subquestion->type > 0) {
 292                              echo "<p class=\"centerpara\">";
 293                              echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$subquestion->id\">";
 294                              survey_print_graph("id=$id&amp;qid=$subquestion->id&amp;group=$currentgroup&amp;type=question.png");
 295                              echo "</a></p>";
 296                          }
 297                      }
 298                  } else if ($question->type > 0) {
 299                      echo "<p class=\"centerpara\">";
 300                      echo "<a title=\"$strseemoredetail\" href=\"report.php?action=question&amp;id=$id&amp;qid=$question->id\">";
 301                      survey_print_graph("id=$id&amp;qid=$question->id&amp;group=$currentgroup&amp;type=question.png");
 302                      echo "</a></p>";
 303  
 304                  } else {
 305                      $table = new html_table();
 306                      $table->head = array($question->text);
 307                      $table->align = array("left");
 308  
 309                      $contents = '<table cellpadding="15" width="100%">';
 310  
 311                      if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup, "sa.time ASC")) {
 312                          foreach ($aaa as $a) {
 313                              $contents .= "<tr>";
 314                              $contents .= '<td class="fullnamecell">' . fullname($a) . '</td>';
 315                              $contents .= '<td valign="top">' . s($a->answer1) . '</td>';
 316                              $contents .= "</tr>";
 317                          }
 318                      }
 319                      $contents .= "</table>";
 320  
 321                      $table->data[] = array($contents);
 322  
 323                      echo html_writer::table($table);
 324  
 325                      echo $OUTPUT->spacer(array('height' => 30)); // Should be done with CSS instead.
 326                  }
 327              }
 328          }
 329  
 330          break;
 331  
 332      case "question":
 333          if (!$question = $DB->get_record("survey_questions", array("id" => $qid))) {
 334              throw new moodle_exception('cannotfindquestion', 'survey');
 335          }
 336          $question->text = get_string($question->text, "survey");
 337  
 338          $answers = explode(",", get_string($question->options, "survey"));
 339  
 340          echo $OUTPUT->heading("$strquestion: $question->text", 3);
 341  
 342          $strname = get_string("name", "survey");
 343          $strtime = get_string("time", "survey");
 344          $stractual = get_string("actual", "survey");
 345          $strpreferred = get_string("preferred", "survey");
 346          $strdateformat = get_string("strftimedatetime");
 347  
 348          $table = new html_table();
 349          $table->head = array("", $strname, $strtime, $stractual, $strpreferred);
 350          $table->align = array("left", "left", "left", "left", "right");
 351          $table->size = array(35, "", "", "", "");
 352  
 353          if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup)) {
 354              foreach ($aaa as $a) {
 355                  if ($a->answer1) {
 356                      $answer1 = "$a->answer1 - " . $answers[$a->answer1 - 1];
 357                  } else {
 358                      $answer1 = "&nbsp;";
 359                  }
 360                  if ($a->answer2) {
 361                      $answer2 = "$a->answer2 - " . $answers[$a->answer2 - 1];
 362                  } else {
 363                      $answer2 = "&nbsp;";
 364                  }
 365                  $table->data[] = array(
 366                          $OUTPUT->user_picture($a, array('courseid' => $course->id)),
 367                          "<a href=\"report.php?id=$id&amp;action=student&amp;student=$a->userid\">" . fullname($a) . "</a>",
 368                          userdate($a->time),
 369                          s($answer1), s($answer2));
 370  
 371              }
 372          }
 373  
 374          echo html_writer::table($table);
 375  
 376          break;
 377  
 378      case "students":
 379  
 380          echo $OUTPUT->heading(get_string("analysisof", "survey", get_string('participants')), 3);
 381  
 382          if (!$results = survey_get_responses($survey->id, $currentgroup, $groupingid)) {
 383              echo $OUTPUT->notification(get_string("nobodyyet", "survey"));
 384          } else {
 385              survey_print_all_responses($cm->id, $results, $course->id);
 386          }
 387  
 388          break;
 389  
 390      case "student":
 391          $user = core_user::get_user($student, '*', MUST_EXIST);
 392          if ($currentgroup && !array_key_exists($user->id, $users)) {
 393              throw new moodle_exception('usernotavailable', 'error');
 394          }
 395  
 396          echo $OUTPUT->heading(get_string("analysisof", "survey", fullname($user)), 3);
 397  
 398          if ($notes != '' and confirm_sesskey()) {
 399              if (survey_get_analysis($survey->id, $user->id)) {
 400                  if (!survey_update_analysis($survey->id, $user->id, $notes)) {
 401                      echo $OUTPUT->notification(get_string("errorunabletosavenotes", "survey"), "notifyproblem");
 402                  } else {
 403                      echo $OUTPUT->notification(get_string("savednotes", "survey"), "notifysuccess");
 404                  }
 405              } else {
 406                  if (!survey_add_analysis($survey->id, $user->id, $notes)) {
 407                      echo $OUTPUT->notification(get_string("errorunabletosavenotes", "survey"), "notifyproblem");
 408                  } else {
 409                      echo $OUTPUT->notification(get_string("savednotes", "survey"), "notifysuccess");
 410                  }
 411              }
 412          }
 413  
 414          echo "<p class=\"centerpara\">";
 415          echo $OUTPUT->user_picture($user, array('courseid' => $course->id));
 416          echo "</p>";
 417  
 418          $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
 419          $questionorder = explode(",", $survey->questions);
 420  
 421          if ($showscales) {
 422              // Print overall summary.
 423              echo "<p class=\"centerpara\">";
 424              survey_print_graph("id=$id&amp;sid=$student&amp;type=student.png");
 425              echo "</p>";
 426  
 427              // Print scales.
 428  
 429              foreach ($questionorder as $key => $val) {
 430                  $question = $questions[$val];
 431                  if ($question->type < 0) {  // We have some virtual scales.  Just show them.
 432                      $virtualscales = true;
 433                      break;
 434                  }
 435              }
 436  
 437              foreach ($questionorder as $key => $val) {
 438                  $question = $questions[$val];
 439                  if ($question->multi) {
 440                      if ($virtualscales && $question->type > 0) {  // Don't show non-virtual scales if virtual.
 441                          continue;
 442                      }
 443                      echo "<p class=\"centerpara\">";
 444                      echo "<a title=\"$strseemoredetail\" href=\"report.php?action=questions&amp;id=$id&amp;qid=$question->multi\">";
 445                      survey_print_graph("id=$id&amp;qid=$question->id&amp;sid=$student&amp;type=studentmultiquestion.png");
 446                      echo "</a></p><br />";
 447                  }
 448              }
 449          }
 450  
 451          // Print non-scale questions.
 452  
 453          foreach ($questionorder as $key => $val) {
 454              $question = $questions[$val];
 455              if ($question->type == 0 or $question->type == 1) {
 456                  if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) {
 457                      $table = new html_table();
 458                      $table->head = array(get_string($question->text, "survey"));
 459                      $table->align = array("left");
 460                      if (!empty($question->options) && $answer->answer1 > 0) {
 461                          $answers = explode(',', get_string($question->options, 'survey'));
 462                          if ($answer->answer1 <= count($answers)) {
 463                              $table->data[] = array(s($answers[$answer->answer1 - 1])); // No html here, just plain text.
 464                          } else {
 465                              $table->data[] = array(s($answer->answer1)); // No html here, just plain text.
 466                          }
 467                      } else {
 468                          $table->data[] = array(s($answer->answer1)); // No html here, just plain text.
 469                      }
 470                      echo html_writer::table($table);
 471                      echo $OUTPUT->spacer(array('height' => 30));
 472                  }
 473              }
 474          }
 475  
 476          if ($rs = survey_get_analysis($survey->id, $user->id)) {
 477              $notes = $rs->notes;
 478          } else {
 479              $notes = "";
 480          }
 481          echo "<hr noshade=\"noshade\" size=\"1\" />";
 482          echo "<div class='studentreport'>";
 483          echo "<form action=\"report.php\" method=\"post\">";
 484          echo "<h3>$strnotes:</h3>";
 485          echo "<blockquote>";
 486          echo "<textarea class=\"form-control\" name=\"notes\" rows=\"10\" cols=\"60\">";
 487          p($notes);
 488          echo "</textarea><br />";
 489          echo "<input type=\"hidden\" name=\"action\" value=\"student\" />";
 490          echo "<input type=\"hidden\" name=\"sesskey\" value=\"" . sesskey() . "\" />";
 491          echo "<input type=\"hidden\" name=\"student\" value=\"$student\" />";
 492          echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
 493          echo "<input type=\"submit\" class=\"btn btn-primary\" value=\"" . get_string("savechanges") . "\" />";
 494          echo "</blockquote>";
 495          echo "</form>";
 496          echo "</div>";
 497  
 498          break;
 499  
 500      case "download":
 501          echo $OUTPUT->heading($strdownload, 3);
 502  
 503          require_capability('mod/survey:download', $context);
 504  
 505          $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
 506          if ($numusers > 0) {
 507              echo html_writer::tag('p', get_string("downloadinfo", "survey"), array('class' => 'centerpara'));
 508  
 509              echo $OUTPUT->container_start('reportbuttons');
 510              $options = array();
 511              $options["id"] = "$cm->id";
 512              $options["group"] = $currentgroup;
 513  
 514              $options["type"] = "ods";
 515              echo $OUTPUT->single_button(new moodle_url("download.php", $options), get_string("downloadods"));
 516  
 517              $options["type"] = "xls";
 518              echo $OUTPUT->single_button(new moodle_url("download.php", $options), get_string("downloadexcel"));
 519  
 520              $options["type"] = "txt";
 521              echo $OUTPUT->single_button(new moodle_url("download.php", $options), get_string("downloadtext"));
 522              echo $OUTPUT->container_end();
 523  
 524          } else {
 525              echo html_writer::tag('p', get_string("nobodyyet", "survey"), array('class' => 'centerpara'));
 526          }
 527  
 528          break;
 529  
 530      default:
 531          throw new moodle_exception('cannotviewreport');
 532  
 533  }
 534  echo $OUTPUT->footer();