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 graph for survey reports
  20   *
  21   * @package   mod_survey
  22   * @copyright 2021 Sujith Haridasan <sujith@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("$CFG->libdir/graphlib.php");
  28  require_once ("lib.php");
  29  
  30  $id = required_param('id', PARAM_INT);    // Course Module ID.
  31  $type = required_param('type', PARAM_FILE);  // Graph Type.
  32  $group = optional_param('group', 0, PARAM_INT);  // Group ID.
  33  $sid = optional_param('sid', false, PARAM_INT);  // Student ID.
  34  $qid = optional_param('qid', 0, PARAM_INT);  // Group ID.
  35  
  36  $url = new moodle_url('/mod/survey/graph.php', array('id' => $id, 'type' => $type));
  37  if ($group !== 0) {
  38      $url->param('group', $group);
  39  }
  40  if ($sid !== false) {
  41      $url->param('sid', $sid);
  42  }
  43  if ($qid !== 0) {
  44      $url->param('qid', $qid);
  45  }
  46  $PAGE->set_url($url);
  47  
  48  if (!$cm = get_coursemodule_from_id('survey', $id)) {
  49      throw new \moodle_exception('invalidcoursemodule');
  50  }
  51  
  52  if (!$course = $DB->get_record("course", array("id" => $cm->course))) {
  53      throw new \moodle_exception('coursemisconf');
  54  }
  55  
  56  if ($sid) {
  57      if (!$user = $DB->get_record("user", array("id" => $sid))) {
  58          throw new \moodle_exception('invaliduserid');
  59      }
  60  }
  61  
  62  require_login($course, false, $cm);
  63  
  64  $groupmode = groups_get_activity_groupmode($cm);   // Groups are being used.
  65  $context = context_module::instance($cm->id);
  66  
  67  if (!has_capability('mod/survey:readresponses', $context)) {
  68      if ($type != "student.png" or $sid != $USER->id) {
  69          throw new \moodle_exception('nopermissiontoshow');
  70      } else if ($groupmode and !groups_is_member($group)) {
  71          throw new \moodle_exception('nopermissiontoshow');
  72      }
  73  }
  74  
  75  if (!$survey = $DB->get_record("survey", array("id" => $cm->instance))) {
  76      throw new \moodle_exception('invalidsurveyid', 'survey');
  77  }
  78  
  79  // Check to see if groups are being used in this survey.
  80  if ($group) {
  81      $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $group, null, false);
  82  } else if (!empty($cm->groupingid)) {
  83      $groups = groups_get_all_groups($courseid, 0, $cm->groupingid);
  84      $groups = array_keys($groups);
  85      $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', $groups, null, false);
  86  } else {
  87      $users = get_users_by_capability($context, 'mod/survey:participate', '', '', '', '', '', null, false);
  88      $group = false;
  89  }
  90  
  91  $stractual = get_string("actual", "survey");
  92  $stractualclass = get_string("actualclass", "survey");
  93  
  94  $strpreferred = get_string("preferred", "survey");
  95  $strpreferredclass = get_string("preferredclass", "survey");
  96  
  97  if ($sid || isset($user)) {
  98      $stractualstudent = get_string("actualstudent", "survey", fullname($user));
  99      $strpreferredstudent = get_string("preferredstudent", "survey", fullname($user));
 100  }
 101  
 102  $virtualscales = false; // Set default value for case clauses.
 103  
 104  switch ($type) {
 105  
 106      case "question.png":
 107  
 108          $question = $DB->get_record("survey_questions", array("id" => $qid));
 109          $question->text = wordwrap(get_string($question->text, "survey"), SURVEY_QLENGTH_WRAP);
 110          $question->options = get_string($question->options, "survey");
 111  
 112          $options = explode(",", $question->options);
 113  
 114          foreach ($options as $key => $unused) {
 115              $buckets1[$key] = 0;
 116              $buckets2[$key] = 0;
 117          }
 118  
 119          if ($aaa = $DB->get_records('survey_answers', array('survey' => $cm->instance, 'question' => $qid))) {
 120              foreach ($aaa as $aa) {
 121                  if (!$group or isset($users[$aa->userid])) {
 122                      if ($a1 = $aa->answer1) {
 123                          $buckets1[$a1 - 1]++;
 124                      }
 125                      if ($a2 = $aa->answer2) {
 126                          $buckets2[$a2 - 1]++;
 127                      }
 128                  }
 129              }
 130          }
 131  
 132          $maxbuckets1 = max($buckets1);
 133          $maxbuckets2 = max($buckets2);
 134          $maxbuckets = ($maxbuckets1 > $maxbuckets2) ? $maxbuckets1 : $maxbuckets2;
 135  
 136          $graph = new graph($SURVEY_GWIDTH, $SURVEY_GHEIGHT);
 137          $graph->parameter['title'] = "$question->text";
 138  
 139          $graph->x_data = $options;
 140  
 141          $graph->y_data['answers1'] = $buckets1;
 142          $graph->y_format['answers1'] = array('colour' => 'ltblue', 'bar' => 'fill', 'legend' => $stractual, 'bar_size' => 0.4);
 143          $graph->y_data['answers2'] = $buckets2;
 144          $graph->y_format['answers2'] = array('colour' => 'ltorange', 'bar' => 'fill', 'legend' => $strpreferred, 'bar_size' => 0.2);
 145  
 146          $graph->parameter['legend'] = 'outside-top';
 147          $graph->parameter['legend_border'] = 'black';
 148          $graph->parameter['legend_offset'] = 4;
 149  
 150          if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
 151              $graph->y_order = array('answers1', 'answers2');
 152          } else if ($maxbuckets1 > 0.0) {
 153              $graph->y_order = array('answers1');
 154          } else {
 155              $graph->y_order = array('answers2');
 156          }
 157  
 158          $graph->parameter['y_axis_gridlines'] = $maxbuckets + 1;
 159          $graph->parameter['y_resolution_left'] = 1;
 160          $graph->parameter['y_decimal_left'] = 0;
 161          $graph->parameter['x_axis_angle'] = 20;
 162          $graph->parameter['shadow'] = 'none';
 163  
 164          $graph->y_tick_labels = null;
 165          $graph->offset_relation = null;
 166  
 167          $graph->draw_stack();
 168  
 169          break;
 170  
 171      case "multiquestion.png":
 172  
 173          $question = $DB->get_record("survey_questions", array("id" => $qid));
 174          $question->text = get_string($question->text, "survey");
 175          $question->options = get_string($question->options, "survey");
 176  
 177          $options = explode(",", $question->options);
 178          $questionorder = explode(",", $question->multi);
 179  
 180          $qqq = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi));
 181  
 182          foreach ($questionorder as $i => $val) {
 183              $names[$i] = get_string($qqq["$val"]->shorttext, "survey");
 184              $buckets1[$i] = 0;
 185              $buckets2[$i] = 0;
 186              $count1[$i] = 0;
 187              $count2[$i] = 0;
 188              $indexof[$val] = $i;
 189              $stdev1[$i] = 0;
 190              $stdev2[$i] = 0;
 191          }
 192  
 193          $aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($question->multi)))",
 194                  array($cm->instance));
 195  
 196          if ($aaa) {
 197              foreach ($aaa as $a) {
 198                  if (!$group or isset($users[$a->userid])) {
 199                      $index = $indexof[$a->question];
 200                      if ($a->answer1) {
 201                          $buckets1[$index] += $a->answer1;
 202                          $count1[$index]++;
 203                      }
 204                      if ($a->answer2) {
 205                          $buckets2[$index] += $a->answer2;
 206                          $count2[$index]++;
 207                      }
 208                  }
 209              }
 210          }
 211  
 212          foreach ($questionorder as $i => $val) {
 213              if ($count1[$i]) {
 214                  $buckets1[$i] = (float) $buckets1[$i] / (float) $count1[$i];
 215              }
 216              if ($count2[$i]) {
 217                  $buckets2[$i] = (float) $buckets2[$i] / (float) $count2[$i];
 218              }
 219          }
 220  
 221          if ($aaa) {
 222              foreach ($aaa as $a) {
 223                  if (!$group or isset($users[$a->userid])) {
 224                      $index = $indexof[$a->question];
 225                      if ($a->answer1) {
 226                          $difference = (float) ($a->answer1 - $buckets1[$index]);
 227                          $stdev1[$index] += ($difference * $difference);
 228                      }
 229                      if ($a->answer2) {
 230                          $difference = (float) ($a->answer2 - $buckets2[$index]);
 231                          $stdev2[$index] += ($difference * $difference);
 232                      }
 233                  }
 234              }
 235          }
 236  
 237          foreach ($questionorder as $i => $val) {
 238              if ($count1[$i]) {
 239                  $stdev1[$i] = sqrt((float) $stdev1[$i] / ((float) $count1[$i]));
 240              }
 241              if ($count2[$i]) {
 242                  $stdev2[$i] = sqrt((float) $stdev2[$i] / ((float) $count2[$i]));
 243              }
 244              $buckets1[$i] = $buckets1[$i] - 1;
 245              $buckets2[$i] = $buckets2[$i] - 1;
 246          }
 247  
 248          $maxbuckets1 = max($buckets1);
 249          $maxbuckets2 = max($buckets2);
 250  
 251          $graph = new graph($SURVEY_GWIDTH, $SURVEY_GHEIGHT);
 252          $graph->parameter['title'] = "$question->text";
 253  
 254          $graph->x_data = $names;
 255          $graph->y_data['answers1'] = $buckets1;
 256          $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
 257                  'shadow_offset' => 4, 'legend' => $stractual);
 258          $graph->y_data['answers2'] = $buckets2;
 259          $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
 260                  'shadow_offset' => 4, 'legend' => $strpreferred);
 261          $graph->y_data['stdev1'] = $stdev1;
 262          $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
 263                  'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3);
 264          $graph->y_data['stdev2'] = $stdev2;
 265          $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
 266                  'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2);
 267          $graph->offset_relation['stdev1'] = 'answers1';
 268          $graph->offset_relation['stdev2'] = 'answers2';
 269  
 270          $graph->parameter['bar_size'] = 0.15;
 271  
 272          $graph->parameter['legend'] = 'outside-top';
 273          $graph->parameter['legend_border'] = 'black';
 274          $graph->parameter['legend_offset'] = 4;
 275  
 276          $graph->y_tick_labels = $options;
 277  
 278          if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
 279              $graph->y_order = array('stdev1', 'answers1', 'stdev2', 'answers2');
 280          } else if ($maxbuckets1 > 0.0) {
 281              $graph->y_order = array('stdev1', 'answers1');
 282          } else {
 283              $graph->y_order = array('stdev2', 'answers2');
 284          }
 285  
 286          $graph->parameter['y_max_left'] = count($options) - 1;
 287          $graph->parameter['y_axis_gridlines'] = count($options);
 288          $graph->parameter['y_resolution_left'] = 1;
 289          $graph->parameter['y_decimal_left'] = 1;
 290          $graph->parameter['x_axis_angle'] = 20;
 291  
 292          $graph->draw();
 293  
 294          break;
 295  
 296      case "overall.png":
 297  
 298          $qqq = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
 299  
 300          foreach ($qqq as $key => $qq) {
 301              if ($qq->multi) {
 302                  $qqq[$key]->text = get_string($qq->text, "survey");
 303                  $qqq[$key]->options = get_string($qq->options, "survey");
 304                  if ($qq->type < 0) {
 305                      $virtualscales = true;
 306                  }
 307              }
 308          }
 309          foreach ($qqq as $qq) {         // If any virtual, then use JUST virtual, else use JUST nonvirtual.
 310              if ($qq->multi) {
 311                  if ($virtualscales && $qq->type < 0) {
 312                      $question[] = $qq;
 313                  } else if (!$virtualscales && $qq->type > 0) {
 314                      $question[] = $qq;
 315                  }
 316              }
 317          }
 318          $numquestions = count($question);
 319  
 320          $options = explode(",", $question[0]->options);
 321          $numoptions = count($options);
 322  
 323          for ($i = 0; $i < $numquestions; $i++) {
 324              $names[$i] = $question[$i]->text;
 325              $buckets1[$i] = 0.0;
 326              $buckets2[$i] = 0.0;
 327              $stdev1[$i] = 0.0;
 328              $stdev2[$i] = 0.0;
 329              $count1[$i] = 0;
 330              $count2[$i] = 0;
 331              $subquestions = $question[$i]->multi;   // Otherwise next line doesn't work.
 332              $aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($subquestions)))",
 333                      array($cm->instance));
 334  
 335              if ($aaa) {
 336                  foreach ($aaa as $a) {
 337                      if (!$group or isset($users[$a->userid])) {
 338                          if ($a->answer1) {
 339                              $buckets1[$i] += $a->answer1;
 340                              $count1[$i]++;
 341                          }
 342                          if ($a->answer2) {
 343                              $buckets2[$i] += $a->answer2;
 344                              $count2[$i]++;
 345                          }
 346                      }
 347                  }
 348              }
 349  
 350              if ($count1[$i]) {
 351                  $buckets1[$i] = (float) $buckets1[$i] / (float) $count1[$i];
 352              }
 353              if ($count2[$i]) {
 354                  $buckets2[$i] = (float) $buckets2[$i] / (float) $count2[$i];
 355              }
 356  
 357              // Calculate the standard devaiations.
 358              if ($aaa) {
 359                  foreach ($aaa as $a) {
 360                      if (!$group or isset($users[$a->userid])) {
 361                          if ($a->answer1) {
 362                              $difference = (float) ($a->answer1 - $buckets1[$i]);
 363                              $stdev1[$i] += ($difference * $difference);
 364                          }
 365                          if ($a->answer2) {
 366                              $difference = (float) ($a->answer2 - $buckets2[$i]);
 367                              $stdev2[$i] += ($difference * $difference);
 368                          }
 369                      }
 370                  }
 371              }
 372  
 373              if ($count1[$i]) {
 374                  $stdev1[$i] = sqrt((float) $stdev1[$i] / ((float) $count1[$i]));
 375              }
 376              if ($count2[$i]) {
 377                  $stdev2[$i] = sqrt((float) $stdev2[$i] / ((float) $count2[$i]));
 378              }
 379  
 380              $buckets1[$i] = $buckets1[$i] - 1;         // Hack because there should not be ANY 0 values in the data.
 381              $buckets2[$i] = $buckets2[$i] - 1;
 382  
 383          }
 384  
 385          $maxbuckets1 = max($buckets1);
 386          $maxbuckets2 = max($buckets2);
 387  
 388          $graph = new graph($SURVEY_GWIDTH, $SURVEY_GHEIGHT);
 389          $graph->parameter['title'] = strip_tags(format_string($survey->name, true));
 390  
 391          $graph->x_data = $names;
 392  
 393          $graph->y_data['answers1'] = $buckets1;
 394          $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
 395                  'shadow_offset' => 4, 'legend' => $stractual);
 396          $graph->y_data['answers2'] = $buckets2;
 397          $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
 398                  'shadow_offset' => 4, 'legend' => $strpreferred);
 399  
 400          $graph->y_data['stdev1'] = $stdev1;
 401          $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
 402                  'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.3);
 403          $graph->y_data['stdev2'] = $stdev2;
 404          $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
 405                  'shadow_offset' => '4', 'legend' => 'none', 'bar_size' => 0.2);
 406          $graph->offset_relation['stdev1'] = 'answers1';
 407          $graph->offset_relation['stdev2'] = 'answers2';
 408  
 409          $graph->parameter['legend'] = 'outside-top';
 410          $graph->parameter['legend_border'] = 'black';
 411          $graph->parameter['legend_offset'] = 4;
 412  
 413          $graph->y_tick_labels = $options;
 414  
 415          if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
 416              $graph->y_order = array('stdev1', 'answers1', 'stdev2', 'answers2');
 417          } else if ($maxbuckets1 > 0.0) {
 418              $graph->y_order = array('stdev1', 'answers1');
 419          } else {
 420              $graph->y_order = array('stdev2', 'answers2');
 421          }
 422  
 423          $graph->parameter['y_max_left'] = $numoptions - 1;
 424          $graph->parameter['y_axis_gridlines'] = $numoptions;
 425          $graph->parameter['y_resolution_left'] = 1;
 426          $graph->parameter['y_decimal_left'] = 1;
 427          $graph->parameter['x_axis_angle'] = 0;
 428          $graph->parameter['x_inner_padding'] = 6;
 429  
 430          $graph->draw();
 431  
 432          break;
 433  
 434      case "student.png":
 435  
 436          $qqq = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
 437  
 438          foreach ($qqq as $key => $qq) {
 439              if ($qq->multi) {
 440                  $qqq[$key]->text = get_string($qq->text, "survey");
 441                  $qqq[$key]->options = get_string($qq->options, "survey");
 442                  if ($qq->type < 0) {
 443                      $virtualscales = true;
 444                  }
 445              }
 446          }
 447          foreach ($qqq as $qq) {         // If any virtual, then use JUST virtual, else use JUST nonvirtual.
 448              if ($qq->multi) {
 449                  if ($virtualscales && $qq->type < 0) {
 450                      $question[] = $qq;
 451                  } else if (!$virtualscales && $qq->type > 0) {
 452                      $question[] = $qq;
 453                  }
 454              }
 455          }
 456          $numquestions = count($question);
 457  
 458          $options = explode(",", $question[0]->options);
 459          $numoptions = count($options);
 460  
 461          for ($i = 0; $i < $numquestions; $i++) {
 462              $names[$i] = $question[$i]->text;
 463              $buckets1[$i] = 0.0;
 464              $buckets2[$i] = 0.0;
 465              $count1[$i] = 0;
 466              $count2[$i] = 0;
 467              $studbuckets1[$i] = 0.0;
 468              $studbuckets2[$i] = 0.0;
 469              $studcount1[$i] = 0;
 470              $studcount2[$i] = 0;
 471              $stdev1[$i] = 0.0;
 472              $stdev2[$i] = 0.0;
 473  
 474              $subquestions = $question[$i]->multi;   // Otherwise next line doesn't work.
 475              $aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($subquestions)))",
 476                      array($cm->instance));
 477  
 478              if ($aaa) {
 479                  foreach ($aaa as $a) {
 480                      if (!$group or isset($users[$a->userid])) {
 481                          if ($a->userid == $sid) {
 482                              if ($a->answer1) {
 483                                  $studbuckets1[$i] += $a->answer1;
 484                                  $studcount1[$i]++;
 485                              }
 486                              if ($a->answer2) {
 487                                  $studbuckets2[$i] += $a->answer2;
 488                                  $studcount2[$i]++;
 489                              }
 490                          }
 491                          if ($a->answer1) {
 492                              $buckets1[$i] += $a->answer1;
 493                              $count1[$i]++;
 494                          }
 495                          if ($a->answer2) {
 496                              $buckets2[$i] += $a->answer2;
 497                              $count2[$i]++;
 498                          }
 499                      }
 500                  }
 501              }
 502  
 503              if ($count1[$i]) {
 504                  $buckets1[$i] = (float) $buckets1[$i] / (float) $count1[$i];
 505              }
 506              if ($count2[$i]) {
 507                  $buckets2[$i] = (float) $buckets2[$i] / (float) $count2[$i];
 508              }
 509              if ($studcount1[$i]) {
 510                  $studbuckets1[$i] = (float) $studbuckets1[$i] / (float) $studcount1[$i];
 511              }
 512              if ($studcount2[$i]) {
 513                  $studbuckets2[$i] = (float) $studbuckets2[$i] / (float) $studcount2[$i];
 514              }
 515  
 516              // Calculate the standard devaiations.
 517              foreach ($aaa as $a) {
 518                  if (!$group or isset($users[$a->userid])) {
 519                      if ($a->answer1) {
 520                          $difference = (float) ($a->answer1 - $buckets1[$i]);
 521                          $stdev1[$i] += ($difference * $difference);
 522                      }
 523                      if ($a->answer2) {
 524                          $difference = (float) ($a->answer2 - $buckets2[$i]);
 525                          $stdev2[$i] += ($difference * $difference);
 526                      }
 527                  }
 528              }
 529  
 530              if ($count1[$i]) {
 531                  $stdev1[$i] = sqrt((float) $stdev1[$i] / ((float) $count1[$i]));
 532              }
 533              if ($count2[$i]) {
 534                  $stdev2[$i] = sqrt((float) $stdev2[$i] / ((float) $count2[$i]));
 535              }
 536  
 537              $buckets1[$i] = $buckets1[$i] - 1;         // Hack because there should not be ANY 0 values in the data.
 538              $buckets2[$i] = $buckets2[$i] - 1;
 539              $studbuckets1[$i] = $studbuckets1[$i] - 1;
 540              $studbuckets2[$i] = $studbuckets2[$i] - 1;
 541  
 542          }
 543  
 544          $maxbuckets1 = max($buckets1);
 545          $maxbuckets2 = max($buckets2);
 546  
 547          $graph = new graph($SURVEY_GWIDTH, $SURVEY_GHEIGHT);
 548          $graph->parameter['title'] = strip_tags(format_string($survey->name, true));
 549  
 550          $graph->x_data = $names;
 551  
 552          $graph->y_data['answers1'] = $buckets1;
 553          $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
 554                  'shadow_offset' => 0.1, 'legend' => $stractualclass);
 555          $graph->y_data['answers2'] = $buckets2;
 556          $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
 557                  'shadow_offset' => 0.1, 'legend' => $strpreferredclass);
 558          $graph->y_data['studanswers1'] = $studbuckets1;
 559          $graph->y_format['studanswers1'] = array('colour' => 'blue', 'line' => 'line', 'point' => 'square',
 560                  'shadow_offset' => 4, 'legend' => $stractualstudent);
 561          $graph->y_data['studanswers2'] = $studbuckets2;
 562          $graph->y_format['studanswers2'] = array('colour' => 'orange', 'line' => 'line', 'point' => 'square',
 563                  'shadow_offset' => 4, 'legend' => $strpreferredstudent);
 564          $graph->y_data['stdev1'] = $stdev1;
 565          $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
 566                  'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.3);
 567          $graph->y_data['stdev2'] = $stdev2;
 568          $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
 569                  'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.2);
 570          $graph->offset_relation['stdev1'] = 'answers1';
 571          $graph->offset_relation['stdev2'] = 'answers2';
 572  
 573          $graph->y_tick_labels = $options;
 574  
 575          $graph->parameter['bar_size'] = 0.15;
 576  
 577          $graph->parameter['legend'] = 'outside-top';
 578          $graph->parameter['legend_border'] = 'black';
 579          $graph->parameter['legend_offset'] = 4;
 580  
 581          if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
 582              $graph->y_order = array('stdev1', 'stdev2', 'answers1', 'answers2', 'studanswers1', 'studanswers2');
 583          } else if ($maxbuckets1 > 0.0) {
 584              $graph->y_order = array('stdev1', 'answers1', 'studanswers1');
 585          } else {
 586              $graph->y_order = array('stdev2', 'answers2', 'studanswers2');
 587          }
 588  
 589          $graph->parameter['y_max_left'] = $numoptions - 1;
 590          $graph->parameter['y_axis_gridlines'] = $numoptions;
 591          $graph->parameter['y_resolution_left'] = 1;
 592          $graph->parameter['y_decimal_left'] = 1;
 593          $graph->parameter['x_axis_angle'] = 20;
 594  
 595          $graph->draw();
 596          break;
 597  
 598      case "studentmultiquestion.png":
 599  
 600          $question = $DB->get_record("survey_questions", array("id" => $qid));
 601          $question->text = get_string($question->text, "survey");
 602          $question->options = get_string($question->options, "survey");
 603  
 604          $options = explode(",", $question->options);
 605          $questionorder = explode(",", $question->multi);
 606  
 607          $qqq = $DB->get_records_list("survey_questions", "id", explode(',', $question->multi));
 608  
 609          foreach ($questionorder as $i => $val) {
 610              $names[$i] = get_string($qqq[$val]->shorttext, "survey");
 611              $buckets1[$i] = 0;
 612              $buckets2[$i] = 0;
 613              $count1[$i] = 0;
 614              $count2[$i] = 0;
 615              $indexof[$val] = $i;
 616              $studbuckets1[$i] = 0.0;
 617              $studbuckets2[$i] = 0.0;
 618              $studcount1[$i] = 0;
 619              $studcount2[$i] = 0;
 620              $stdev1[$i] = 0.0;
 621              $stdev2[$i] = 0.0;
 622          }
 623  
 624          $aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($question->multi)))",
 625                  array($cm->instance));
 626  
 627          if ($aaa) {
 628              foreach ($aaa as $a) {
 629                  if (!$group or isset($users[$a->userid])) {
 630                      $index = $indexof[$a->question];
 631                      if ($a->userid == $sid) {
 632                          if ($a->answer1) {
 633                              $studbuckets1[$index] += $a->answer1;
 634                              $studcount1[$index]++;
 635                          }
 636                          if ($a->answer2) {
 637                              $studbuckets2[$index] += $a->answer2;
 638                              $studcount2[$index]++;
 639                          }
 640                      }
 641                      if ($a->answer1) {
 642                          $buckets1[$index] += $a->answer1;
 643                          $count1[$index]++;
 644                      }
 645                      if ($a->answer2) {
 646                          $buckets2[$index] += $a->answer2;
 647                          $count2[$index]++;
 648                      }
 649                  }
 650              }
 651          }
 652  
 653          foreach ($questionorder as $i => $val) {
 654              if ($count1[$i]) {
 655                  $buckets1[$i] = (float) $buckets1[$i] / (float) $count1[$i];
 656              }
 657              if ($count2[$i]) {
 658                  $buckets2[$i] = (float) $buckets2[$i] / (float) $count2[$i];
 659              }
 660              if ($studcount1[$i]) {
 661                  $studbuckets1[$i] = (float) $studbuckets1[$i] / (float) $studcount1[$i];
 662              }
 663              if ($studcount2[$i]) {
 664                  $studbuckets2[$i] = (float) $studbuckets2[$i] / (float) $studcount2[$i];
 665              }
 666          }
 667  
 668          foreach ($aaa as $a) {
 669              if (!$group or isset($users[$a->userid])) {
 670                  $index = $indexof[$a->question];
 671                  if ($a->answer1) {
 672                      $difference = (float) ($a->answer1 - $buckets1[$index]);
 673                      $stdev1[$index] += ($difference * $difference);
 674                  }
 675                  if ($a->answer2) {
 676                      $difference = (float) ($a->answer2 - $buckets2[$index]);
 677                      $stdev2[$index] += ($difference * $difference);
 678                  }
 679              }
 680          }
 681  
 682          foreach ($questionorder as $i => $val) {
 683              if ($count1[$i]) {
 684                  $stdev1[$i] = sqrt((float) $stdev1[$i] / ((float) $count1[$i]));
 685              }
 686              if ($count2[$i]) {
 687                  $stdev2[$i] = sqrt((float) $stdev2[$i] / ((float) $count2[$i]));
 688              }
 689              $buckets1[$i] = $buckets1[$i] - 1;         // Hack because there should not be ANY 0 values in the data.
 690              $buckets2[$i] = $buckets2[$i] - 1;
 691              $studbuckets1[$i] = $studbuckets1[$i] - 1;
 692              $studbuckets2[$i] = $studbuckets2[$i] - 1;
 693          }
 694  
 695          $maxbuckets1 = max($buckets1);
 696          $maxbuckets2 = max($buckets2);
 697  
 698          $graph = new graph($SURVEY_GWIDTH, $SURVEY_GHEIGHT);
 699          $graph->parameter['title'] = "$question->text";
 700  
 701          $graph->x_data = $names;
 702          $graph->y_data['answers1'] = $buckets1;
 703          $graph->y_format['answers1'] = array('colour' => 'ltblue', 'line' => 'line', 'point' => 'square',
 704                  'shadow_offset' => 0.1, 'legend' => $stractualclass);
 705          $graph->y_data['answers2'] = $buckets2;
 706          $graph->y_format['answers2'] = array('colour' => 'ltorange', 'line' => 'line', 'point' => 'square',
 707                  'shadow_offset' => 0.1, 'legend' => $strpreferredclass);
 708          $graph->y_data['studanswers1'] = $studbuckets1;
 709          $graph->y_format['studanswers1'] = array('colour' => 'blue', 'line' => 'line', 'point' => 'square',
 710                  'shadow_offset' => 4, 'legend' => $stractualstudent);
 711          $graph->y_data['studanswers2'] = $studbuckets2;
 712          $graph->y_format['studanswers2'] = array('colour' => 'orange', 'line' => 'line', 'point' => 'square',
 713                  'shadow_offset' => 4, 'legend' => $strpreferredstudent);
 714          $graph->y_data['stdev1'] = $stdev1;
 715          $graph->y_format['stdev1'] = array('colour' => 'ltltblue', 'bar' => 'fill',
 716                  'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.3);
 717          $graph->y_data['stdev2'] = $stdev2;
 718          $graph->y_format['stdev2'] = array('colour' => 'ltltorange', 'bar' => 'fill',
 719                  'shadow_offset' => 0.1, 'legend' => 'none', 'bar_size' => 0.2);
 720          $graph->offset_relation['stdev1'] = 'answers1';
 721          $graph->offset_relation['stdev2'] = 'answers2';
 722  
 723          $graph->parameter['bar_size'] = 0.15;
 724  
 725          $graph->parameter['legend'] = 'outside-top';
 726          $graph->parameter['legend_border'] = 'black';
 727          $graph->parameter['legend_offset'] = 4;
 728  
 729          $graph->y_tick_labels = $options;
 730  
 731          if (($maxbuckets1 > 0.0) && ($maxbuckets2 > 0.0)) {
 732              $graph->y_order = array('stdev1', 'stdev2', 'answers1', 'answers2', 'studanswers1', 'studanswers2');
 733          } else if ($maxbuckets1 > 0.0) {
 734              $graph->y_order = array('stdev1', 'answers1', 'studanswers1');
 735          } else {
 736              $graph->y_order = array('stdev2', 'answers2', 'studanswers2');
 737          }
 738  
 739          $graph->parameter['y_max_left'] = count($options) - 1;
 740          $graph->parameter['y_axis_gridlines'] = count($options);
 741          $graph->parameter['y_resolution_left'] = 1;
 742          $graph->parameter['y_decimal_left'] = 1;
 743          $graph->parameter['x_axis_angle'] = 20;
 744  
 745          $graph->draw();
 746  
 747          break;
 748  
 749      default:
 750          break;
 751  }
 752  
 753  exit;