$roles = $SESSION->customdata['selectedroles'];
                                 } else {
                                     throw new moodle_exception('emptyroles', 'tool_downloaddata', $returnurl);
                                 }
                                 $overrides = array();
                                 if ($options['useoverrides']) {
                                     try {
                                         $overrides = tool_downloaddata_process_overrides($formdata->overrides);
                                     } catch (Exception $e) {
                                         $e->link = $returnurl;
                                         throw $e;
                                     }
                                 }
                                 $processor = new tool_downloaddata_processor($options, $fields, $roles, $overrides);
                                 try {
                                     $processor->prepare();
                                 } catch (Exception $e) {
                                     $e->link = $returnurl;
                                     throw $e;
                                 }
                                 $processor->download();
                             }
                         }
                     }
                 }
             }
         }
     }
 }
 unset($_POST);
 $SESSION->customdata['format'] = $formdata->format;
 /**
  * Tests downloading multiple users with variable number of roles to csv.
  */
 public function test_download_multiple_users_to_csv()
 {
     global $DB;
     $this->resetAfterTest(true);
     $course1 = $this->getDataGenerator()->create_course();
     $course2 = $this->getDataGenerator()->create_course();
     $user1 = $this->getDataGenerator()->create_user();
     $user2 = $this->getDataGenerator()->create_user();
     $roleid1 = $this->getDataGenerator()->create_role();
     $roleid2 = $this->getDataGenerator()->create_role();
     $roles = get_all_roles();
     foreach ($roles as $r) {
         if ($roleid1 == $r->id) {
             $role1 = $r;
         } else {
             if ($roleid2 == $r->id) {
                 $role2 = $r;
             }
         }
     }
     $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $role1->id);
     $this->getDataGenerator()->enrol_user($user2->id, $course1->id, $role1->id);
     $this->getDataGenerator()->enrol_user($user2->id, $course2->id, $role2->id);
     $fields = array('username');
     $options = self::$optionsuserscsv;
     // Created new roles, cannot use self::$allroles.
     $roles = tool_downloaddata_processor::get_all_valid_roles();
     $options['sortbycategorypath'] = true;
     $processor = new tool_downloaddata_processor($options, $fields, $roles);
     $processor->prepare();
     $csv = $processor->get_file_object();
     $expectedoutput = array('username,course1,role1,course2,role2', $user1->username . ',' . $course1->shortname . ',' . $role1->shortname . ',,', $user2->username . ',' . $course1->shortname . ',' . $role1->shortname . ',' . $course2->shortname . ',' . $role2->shortname);
     $expectedoutput = implode("\n", $expectedoutput);
     $output = rtrim($csv->print_csv_data(true));
     // Sorting the output lines as the user order is random.
     $output = explode("\n", $output);
     sort($output);
     $output = implode("\n", $output);
     $output = rtrim($output);
     $this->assertEquals($expectedoutput, $output);
 }