Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

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

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