Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Data provider tests.
  19   *
  20   * @package    mod_survey
  21   * @category   test
  22   * @copyright  2018 Frédéric Massart
  23   * @author     Frédéric Massart <fred@branchup.tech>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  namespace mod_survey\privacy;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  global $CFG;
  30  
  31  use core_privacy\tests\provider_testcase;
  32  use core_privacy\local\request\approved_contextlist;
  33  use core_privacy\local\request\approved_userlist;
  34  use core_privacy\local\request\transform;
  35  use core_privacy\local\request\writer;
  36  use mod_survey\privacy\provider;
  37  
  38  require_once($CFG->dirroot . '/mod/survey/lib.php');
  39  
  40  /**
  41   * Data provider testcase class.
  42   *
  43   * @package    mod_survey
  44   * @category   test
  45   * @copyright  2018 Frédéric Massart
  46   * @author     Frédéric Massart <fred@branchup.tech>
  47   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  48   */
  49  class provider_test extends provider_testcase {
  50  
  51      public function setUp(): void {
  52          global $PAGE;
  53          $this->resetAfterTest();
  54          $PAGE->get_renderer('core');
  55      }
  56  
  57      public function test_get_contexts_for_userid() {
  58          $dg = $this->getDataGenerator();
  59  
  60          $c1 = $dg->create_course();
  61          $c2 = $dg->create_course();
  62          $cm1a = $dg->create_module('survey', ['template' => 1, 'course' => $c1]);
  63          $cm1b = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
  64          $cm1c = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
  65          $cm2a = $dg->create_module('survey', ['template' => 1, 'course' => $c2]);
  66          $cm2b = $dg->create_module('survey', ['template' => 1, 'course' => $c2]);
  67          $u1 = $dg->create_user();
  68          $u2 = $dg->create_user();
  69  
  70          $this->create_answer($cm1a->id, 1, $u1->id);
  71          $this->create_answer($cm1a->id, 1, $u2->id);
  72          $this->create_answer($cm1b->id, 1, $u2->id);
  73          $this->create_answer($cm2a->id, 1, $u1->id);
  74          $this->create_analysis($cm2b->id, $u1->id);
  75          $this->create_analysis($cm1c->id, $u2->id);
  76  
  77          $contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
  78          $this->assertCount(3, $contextids);
  79          $this->assertTrue(in_array(\context_module::instance($cm1a->cmid)->id, $contextids));
  80          $this->assertTrue(in_array(\context_module::instance($cm2a->cmid)->id, $contextids));
  81          $this->assertTrue(in_array(\context_module::instance($cm2b->cmid)->id, $contextids));
  82  
  83          $contextids = provider::get_contexts_for_userid($u2->id)->get_contextids();
  84          $this->assertCount(3, $contextids);
  85          $this->assertTrue(in_array(\context_module::instance($cm1a->cmid)->id, $contextids));
  86          $this->assertTrue(in_array(\context_module::instance($cm1b->cmid)->id, $contextids));
  87          $this->assertTrue(in_array(\context_module::instance($cm1c->cmid)->id, $contextids));
  88      }
  89  
  90      /**
  91       * Test for provider::test_get_users_in_context().
  92       */
  93      public function test_get_users_in_context() {
  94          $dg = $this->getDataGenerator();
  95          $component = 'mod_survey';
  96  
  97          $c1 = $dg->create_course();
  98          $c2 = $dg->create_course();
  99          $cm1a = $dg->create_module('survey', ['template' => 1, 'course' => $c1]);
 100          $cm1b = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
 101          $cm2 = $dg->create_module('survey', ['template' => 1, 'course' => $c2]);
 102          $cm1acontext = \context_module::instance($cm1a->cmid);
 103          $cm1bcontext = \context_module::instance($cm1b->cmid);
 104          $cm2context = \context_module::instance($cm2->cmid);
 105  
 106          $u1 = $dg->create_user();
 107          $u2 = $dg->create_user();
 108          $bothusers = [$u1->id, $u2->id];
 109          sort($bothusers);
 110  
 111          $this->create_answer($cm1a->id, 1, $u1->id);
 112          $this->create_answer($cm1b->id, 1, $u1->id);
 113          $this->create_answer($cm1b->id, 1, $u2->id);
 114          $this->create_answer($cm2->id, 1, $u2->id);
 115          $this->create_analysis($cm2->id, $u1->id);
 116  
 117          // Cm1a should only contain u1.
 118          $userlist = new \core_privacy\local\request\userlist($cm1acontext, $component);
 119          provider::get_users_in_context($userlist);
 120  
 121          $this->assertCount(1, $userlist);
 122          $this->assertEquals([$u1->id], $userlist->get_userids());
 123  
 124          // Cm1b should contain u1 and u2 (both have answers).
 125          $userlist = new \core_privacy\local\request\userlist($cm1bcontext, $component);
 126          provider::get_users_in_context($userlist);
 127  
 128          $this->assertCount(2, $userlist);
 129          $actual = $userlist->get_userids();
 130          sort($actual);
 131          $this->assertEquals($bothusers, $actual);
 132  
 133          // Cm2 should contain u1 (analysis) and u2 (answer).
 134          $userlist = new \core_privacy\local\request\userlist($cm2context, $component);
 135          provider::get_users_in_context($userlist);
 136  
 137          $this->assertCount(2, $userlist);
 138          $actual = $userlist->get_userids();
 139          sort($actual);
 140          $this->assertEquals($bothusers, $actual);
 141      }
 142  
 143      public function test_delete_data_for_all_users_in_context() {
 144          global $DB;
 145          $dg = $this->getDataGenerator();
 146  
 147          $c1 = $dg->create_course();
 148          $cm1a = $dg->create_module('survey', ['template' => 1, 'course' => $c1]);
 149          $cm1b = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
 150          $cm1c = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
 151          $u1 = $dg->create_user();
 152          $u2 = $dg->create_user();
 153  
 154          $this->create_answer($cm1a->id, 1, $u1->id);
 155          $this->create_answer($cm1a->id, 1, $u2->id);
 156          $this->create_answer($cm1b->id, 1, $u2->id);
 157          $this->create_answer($cm1c->id, 1, $u1->id);
 158          $this->create_analysis($cm1a->id, $u1->id);
 159          $this->create_analysis($cm1b->id, $u1->id);
 160          $this->create_analysis($cm1a->id, $u2->id);
 161          $this->create_analysis($cm1c->id, $u2->id);
 162  
 163          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 164          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
 165          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 166          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1b->id]));
 167          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 168          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1b->id]));
 169          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 170          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
 171  
 172          // Deleting the course does nothing.
 173          provider::delete_data_for_all_users_in_context(\context_course::instance($c1->id));
 174          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 175          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
 176          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 177          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1b->id]));
 178          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 179          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1b->id]));
 180          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 181          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
 182  
 183          provider::delete_data_for_all_users_in_context(\context_module::instance($cm1c->cmid));
 184          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 185          $this->assertFalse($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
 186          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 187          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1b->id]));
 188          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 189          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1b->id]));
 190          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 191          $this->assertFalse($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
 192  
 193          provider::delete_data_for_all_users_in_context(\context_module::instance($cm1a->cmid));
 194          $this->assertFalse($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 195          $this->assertFalse($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
 196          $this->assertFalse($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 197          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1b->id]));
 198          $this->assertFalse($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 199          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1b->id]));
 200          $this->assertFalse($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 201          $this->assertFalse($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
 202      }
 203  
 204      public function test_delete_data_for_user() {
 205          global $DB;
 206          $dg = $this->getDataGenerator();
 207  
 208          $c1 = $dg->create_course();
 209          $cm1a = $dg->create_module('survey', ['template' => 1, 'course' => $c1]);
 210          $cm1b = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
 211          $cm1c = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
 212          $u1 = $dg->create_user();
 213          $u2 = $dg->create_user();
 214  
 215          $this->create_answer($cm1a->id, 1, $u1->id);
 216          $this->create_answer($cm1a->id, 1, $u2->id);
 217          $this->create_answer($cm1b->id, 1, $u2->id);
 218          $this->create_answer($cm1c->id, 1, $u1->id);
 219          $this->create_analysis($cm1a->id, $u1->id);
 220          $this->create_analysis($cm1b->id, $u1->id);
 221          $this->create_analysis($cm1a->id, $u2->id);
 222          $this->create_analysis($cm1c->id, $u2->id);
 223  
 224          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 225          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
 226          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 227          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1b->id]));
 228          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 229          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1b->id]));
 230          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 231          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
 232  
 233          provider::delete_data_for_user(new approved_contextlist($u1, 'mod_survey', [
 234              \context_course::instance($c1->id)->id,
 235              \context_module::instance($cm1a->cmid)->id,
 236              \context_module::instance($cm1b->cmid)->id,
 237          ]));
 238          $this->assertFalse($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 239          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
 240          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 241          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1b->id]));
 242          $this->assertFalse($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 243          $this->assertFalse($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1b->id]));
 244          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 245          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
 246      }
 247  
 248      /**
 249       * Test for provider::delete_data_for_users().
 250       */
 251      public function test_delete_data_for_users() {
 252          global $DB;
 253          $dg = $this->getDataGenerator();
 254          $component = 'mod_survey';
 255  
 256          $c1 = $dg->create_course();
 257          $cm1a = $dg->create_module('survey', ['template' => 1, 'course' => $c1]);
 258          $cm1b = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
 259          $cm1c = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
 260          $cm1acontext = \context_module::instance($cm1a->cmid);
 261          $cm1bcontext = \context_module::instance($cm1b->cmid);
 262  
 263          $u1 = $dg->create_user();
 264          $u2 = $dg->create_user();
 265  
 266          $this->create_answer($cm1a->id, 1, $u1->id);
 267          $this->create_answer($cm1a->id, 1, $u2->id);
 268          $this->create_analysis($cm1a->id, $u1->id);
 269          $this->create_analysis($cm1a->id, $u2->id);
 270          $this->create_answer($cm1b->id, 1, $u2->id);
 271          $this->create_analysis($cm1b->id, $u1->id);
 272          $this->create_answer($cm1c->id, 1, $u1->id);
 273          $this->create_analysis($cm1c->id, $u2->id);
 274  
 275          // Confirm data exists before deletion.
 276          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 277          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
 278          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 279          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1b->id]));
 280          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 281          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1b->id]));
 282          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 283          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
 284  
 285          // Ensure only approved user data is deleted.
 286          $approveduserids = [$u1->id];
 287          $approvedlist = new approved_userlist($cm1acontext, $component, $approveduserids);
 288          provider::delete_data_for_users($approvedlist);
 289  
 290          $this->assertFalse($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 291          $this->assertFalse($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
 292          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 293          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
 294  
 295          $approveduserids = [$u1->id, $u2->id];
 296          $approvedlist = new approved_userlist($cm1bcontext, $component, $approveduserids);
 297          provider::delete_data_for_users($approvedlist);
 298  
 299          $this->assertFalse($DB->record_exists('survey_answers', ['survey' => $cm1b->id]));
 300          $this->assertFalse($DB->record_exists('survey_analysis', ['survey' => $cm1b->id]));
 301  
 302          $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
 303          $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
 304      }
 305  
 306      public function test_export_data_for_user() {
 307          global $DB;
 308          $dg = $this->getDataGenerator();
 309  
 310          $templates = $DB->get_records_menu('survey', array('template' => 0), 'name', 'name, id');
 311  
 312          $c1 = $dg->create_course();
 313          $s1a = $dg->create_module('survey', ['template' => $templates['attlsname'], 'course' => $c1]);
 314          $s1b = $dg->create_module('survey', ['template' => $templates['ciqname'], 'course' => $c1]);
 315          $s1c = $dg->create_module('survey', ['template' => $templates['collesapname'], 'course' => $c1]);
 316          $u1 = $dg->create_user();
 317          $u2 = $dg->create_user();
 318  
 319          $s1actx = \context_module::instance($s1a->cmid);
 320          $s1bctx = \context_module::instance($s1b->cmid);
 321          $s1cctx = \context_module::instance($s1c->cmid);
 322  
 323          $this->answer_survey($s1a, $u1, $c1, $s1actx);
 324          $this->answer_survey($s1b, $u1, $c1, $s1bctx);
 325          $this->create_analysis($s1a->id, $u1->id, 'Hello,');
 326  
 327          $this->answer_survey($s1a, $u2, $c1, $s1actx);
 328          $this->answer_survey($s1c, $u2, $c1, $s1cctx);
 329          $this->create_analysis($s1b->id, $u2->id, 'World!');
 330  
 331          provider::export_user_data(new approved_contextlist($u1, 'mod_survey', [$s1actx->id, $s1bctx->id, $s1cctx->id]));
 332  
 333          $data = writer::with_context($s1actx)->get_data([]);
 334          $this->assertNotEmpty($data);
 335          $this->assert_exported_answers($data->answers, $u1, $s1a);
 336          $data = writer::with_context($s1actx)->get_related_data([], 'survey_analysis');
 337          $this->assertEquals('Hello,', $data->notes);
 338  
 339          $data = writer::with_context($s1bctx)->get_data([]);
 340          $this->assertNotEmpty($data);
 341          $this->assert_exported_answers($data->answers, $u1, $s1b);
 342          $data = writer::with_context($s1bctx)->get_related_data([], 'survey_analysis');
 343          $this->assertEmpty($data);
 344  
 345          $data = writer::with_context($s1cctx)->get_data([]);
 346          $this->assertEmpty($data);
 347          $data = writer::with_context($s1cctx)->get_related_data([], 'survey_analysis');
 348          $this->assertEmpty($data);
 349  
 350          writer::reset();
 351          provider::export_user_data(new approved_contextlist($u2, 'mod_survey', [$s1actx->id, $s1bctx->id, $s1cctx->id]));
 352  
 353          $data = writer::with_context($s1actx)->get_data([]);
 354          $this->assertNotEmpty($data);
 355          $this->assert_exported_answers($data->answers, $u2, $s1a);
 356          $data = writer::with_context($s1actx)->get_related_data([], 'survey_analysis');
 357          $this->assertEmpty($data);
 358  
 359          $data = writer::with_context($s1bctx)->get_data([]);
 360          $this->assertEmpty($data);
 361          $data = writer::with_context($s1bctx)->get_related_data([], 'survey_analysis');
 362          $this->assertEquals('World!', $data->notes);
 363  
 364          $data = writer::with_context($s1cctx)->get_data([]);
 365          $this->assertNotEmpty($data);
 366          $this->assert_exported_answers($data->answers, $u2, $s1c);
 367          $data = writer::with_context($s1cctx)->get_related_data([], 'survey_analysis');
 368          $this->assertEmpty($data);
 369      }
 370  
 371      /**
 372       * Answer a survey in a predictable manner.
 373       *
 374       * @param stdClass $survey The survey.
 375       * @param stdClass $user The user.
 376       * @param stdClass $course The course.
 377       * @param context_module $context The module context.
 378       * @return void
 379       */
 380      protected function answer_survey($survey, $user, $course, \context_module $context) {
 381          global $USER;
 382  
 383          $userid = $user->id;
 384          $questions = survey_get_questions($survey);
 385          $answer = function(&$answers, $q) use ($userid) {
 386              $key = 'q' . ($q->type == 2 ? 'P' : '') . $q->id;
 387  
 388              if ($q->type < 1) {
 389                  $a = "A:{$q->id}:{$userid}";
 390                  $answers[$key] = $a;
 391  
 392              } else if ($q->type < 3) {
 393                  $options = explode(',', get_string($q->options, 'mod_survey'));
 394                  $answers[$key] = ($q->id + $userid) % count($options) + 1;
 395  
 396              } else {
 397                  $options = explode(',', get_string($q->options, 'mod_survey'));
 398                  $answers["q{$q->id}"] = ($q->id + $userid) % count($options) + 1;
 399                  $answers["qP{$q->id}"] = ($q->id + $userid + 1) % count($options) + 1;
 400              }
 401  
 402          };
 403  
 404          foreach ($questions as $q) {
 405              if ($q->type < 0) {
 406                  continue;
 407              } else if ($q->type > 0 && $q->multi) {
 408                  $subquestions = survey_get_subquestions($q);
 409                  foreach ($subquestions as $sq) {
 410                      $answer($answers, $sq);
 411                  }
 412              } else {
 413                  $answer($answers, $q);
 414              }
 415          }
 416  
 417          $origuser = $USER;
 418          $this->setUser($user);
 419          survey_save_answers($survey, $answers, $course, $context);
 420          $this->setUser($origuser);
 421      }
 422  
 423      /**
 424       * Assert the answers provided to a survey.
 425       *
 426       * @param array $answers The answers.
 427       * @param object $user The user.
 428       * @param object $survey The survey.
 429       * @return void
 430       */
 431      protected function assert_exported_answers($answers, $user, $survey) {
 432          global $DB;
 433  
 434          $userid = $user->id;
 435          $questionids = explode(',', $survey->questions);
 436          $topquestions = $DB->get_records_list('survey_questions', 'id', $questionids, 'id');
 437          $questions = [];
 438  
 439          foreach ($topquestions as $q) {
 440              if ($q->type < 0) {
 441                  continue;
 442              } else if ($q->type > 0 && $q->multi) {
 443                  $questionids = explode(',', $q->multi);
 444                  $subqs = $DB->get_records_list('survey_questions', 'id', $questionids, 'id');
 445              } else {
 446                  $subqs = [$q];
 447              }
 448              foreach ($subqs as $sq) {
 449                  $questions[] = $sq;
 450              }
 451          }
 452  
 453          $this->assertCount(count($questions), $answers);
 454  
 455          $answer = reset($answers);
 456          foreach ($questions as $question) {
 457              $qtype = $question->type;
 458              $question = survey_translate_question($question);
 459              $options = $qtype > 0 ? explode(',', $question->options) : '-';
 460              $this->assertEquals($question->text, $answer['question']['text']);
 461              $this->assertEquals($question->shorttext, $answer['question']['shorttext']);
 462              $this->assertEquals($question->intro, $answer['question']['intro']);
 463              $this->assertEquals($options, $answer['question']['options']);
 464  
 465              if ($qtype < 1) {
 466                  $this->assertEquals("A:{$question->id}:{$userid}", $answer['answer']['actual']);
 467  
 468              } else if ($qtype == 1 || $qtype == 2) {
 469                  $chosen = ($question->id + $userid) % count($options);
 470                  $key = $qtype == 1 ? 'actual' : 'preferred';
 471                  $this->assertEquals($options[$chosen], $answer['answer'][$key]);
 472  
 473              } else {
 474                  $chosen = ($question->id + $userid) % count($options);
 475                  $this->assertEquals($options[$chosen], $answer['answer']['actual']);
 476                  $chosen = ($question->id + $userid + 1) % count($options);
 477                  $this->assertEquals($options[$chosen], $answer['answer']['preferred']);
 478              }
 479  
 480              // Grab next answer, if any.
 481              $answer = next($answers);
 482          }
 483  
 484      }
 485  
 486      /**
 487       * Create analysis.
 488       *
 489       * @param int $surveyid The survey ID.
 490       * @param int $userid The user ID.
 491       * @param string $notes The nodes.
 492       * @return stdClass
 493       */
 494      protected function create_analysis($surveyid, $userid, $notes = '') {
 495          global $DB;
 496          $record = (object) [
 497              'survey' => $surveyid,
 498              'userid' => $userid,
 499              'notes' => $notes
 500          ];
 501          $record->id = $DB->insert_record('survey_analysis', $record);
 502          return $record;
 503      }
 504  
 505      /**
 506       * Create answer.
 507       *
 508       * @param int $surveyid The survey ID.
 509       * @param int $questionid The question ID.
 510       * @param int $userid The user ID.
 511       * @param string $answer1 The first answer field.
 512       * @param string $answer2 The second answer field.
 513       * @return stdClass
 514       */
 515      protected function create_answer($surveyid, $questionid, $userid, $answer1 = '', $answer2 = '') {
 516          global $DB;
 517          $record = (object) [
 518              'survey' => $surveyid,
 519              'question' => $questionid,
 520              'userid' => $userid,
 521              'answer1' => $answer1,
 522              'answer2' => $answer2,
 523              'time' => time()
 524          ];
 525          $record->id = $DB->insert_record('survey_answers', $record);
 526          return $record;
 527      }
 528  
 529  }