diff --git a/classes/local/exporter.php b/classes/local/exporter.php
index c236650a23019620cf5942bf8088ec05fa71d3aa..acaa32d2a06db7d214df14a947c330e082f486f3 100644
--- a/classes/local/exporter.php
+++ b/classes/local/exporter.php
@@ -36,7 +36,9 @@ class exporter {
     /** @var int ID of a course. */
     private int $courseid;
     /** @var string Identifier for the evaluation of a course. */
-    private string $coursecode;
+    private string $code;
+    /** @var string identifier for the course. */
+    private string $codereplacement;
     /** @var int[] Array of role ids. */
     private array $roles;
     /** @var string Name of the file to download. */
@@ -47,45 +49,37 @@ class exporter {
     /**
      * Creates an exporter object.
      *
-     * @param stdClass $data
+     * @param int $courseid
+     * @param string $ccode
+     * @param string $codereplacement
+     * @param array $roles
      */
-    public function __construct($data) {
-        $this->courseid = $this->set_courseid($data);
-        $this->coursecode = $this->set_coursecode($data);
-        $this->roles = $this->set_roles($data);
+    public function __construct($courseid=0, $code=null, $codereplacement=null, $roles=[]) {
+        $this->courseid = $this->set_courseid($courseid);
+        $this->code = $code;
+        $this->codereplacement = $codereplacement;
+        $this->roles = $this->set_roles($roles);
         $this->filename = $this->set_filename();
         $this->content = $this->set_content();
     }
 
-    private function set_courseid($data) {
-        if (property_exists($data, 'courseid')) {
-            return $data->courseid;
+    private function set_courseid($id) {
+        if ($id != 0) {
+            return $id;
         } else {
             throw new \moodle_exception('Error: no course id given.');
         }
     }
 
-    private function set_coursecode($data) {
-        if (property_exists($data, 'coursecode')) {
-            return $data->coursecode;
-        } else {
-            return null;
-        }
-    }
-
     /**
-     * Create array of  content in csv format.
+     * Create array of content in csv format.
      *
-     * @return string
+     * @return array
      */
-    private function set_roles($data) {
-        if (property_exists($data, 'roles')) {
-            $roleids = array_filter(array_values($data->roles));
-            $roles = array_map('intval', $roleids);
-            return $roles;
-        } else {
-            return [];
-        }
+    private function set_roles($roles) {
+        $roleids = array_filter($roles);
+        $roles = array_map('intval', $roleids);
+        return $roles;
     }
 
     /**
@@ -103,11 +97,19 @@ class exporter {
         return \csv_export_writer::print_array($rows, $delimiter, '"', true);
     }
 
+    private function get_base_filename() {
+        $base = $this->code;
+        if (!$base) {
+            $base = str_replace(',', '-', $this->codereplacement);
+        }
+        return $base;
+    }
+
     /**
      * Create filename.
      *
      * This method creates a filename for the file to be downloaded. The filename consists of a given prefix,
-     * a coursecode, a timestamp and the appending extension. If no coursecode is set this part is replaced by the
+     * a code, a timestamp and the appending extension. If no code is set this part is replaced by the
      * short name of the course.
      *
      * @return string
@@ -115,18 +117,7 @@ class exporter {
     private function set_filename() {
 
         // Use course shortname if no course code is given.
-        $filename = $this->coursecode;
-        if (!$filename) {
-            global $DB;
-            $course = $DB->get_record('course', ['id' => $this->courseid], '*', MUST_EXIST);
-            $context = context\course::instance($course->id, MUST_EXIST);
-
-            if ($context->contextlevel != CONTEXT_COURSE) {
-                throw new \moodle_exception('Error: this is not an appropriate course context.');
-            }
-            $filename = str_replace(',', '-', $course->shortname);
-        }
-
+        $filename = $this->get_base_filename();
         // Extend filename with prefix and timestamp.
         $filename = $this->add_prefix($filename);
         $filename = $this->add_timestamp($filename);
@@ -166,10 +157,10 @@ class exporter {
 
         $rows = [];
 
+        // If no code is set, use alternative
+        $code = $this->code ? $this->code : $this->codereplacement;
         foreach ($users as $user) {
-            if ($this->coursecode) {
-                $row['coursecode'] = $this->coursecode;
-            }
+            $row['code'] = $code;
             $row['email'] = $user->email;
             $rows[] = $row;
         }
diff --git a/export.php b/export.php
index 89f05f7c5d3df57ac143ecc619121ef0de53608b..d75c691340e1a62a5fa4c36f81bed4bdb60a1cb3 100644
--- a/export.php
+++ b/export.php
@@ -61,7 +61,11 @@ if ($mform->is_cancelled()) {
     $returnurl = new moodle_url('/user/index.php', ['id' => $course->id]);
     redirect($returnurl);
 } else if ($formdata) {
-    $exporter = new exporter($formdata);
+    $courseid = $formdata->courseid;
+    $code = $formdata->coursecode;
+    $codereplacement = $course->shortname;
+    $roles = $formdata->roles;
+    $exporter = new exporter($courseid, $code, $codereplacement, $roles);
     $exporter->send_file();
 } else {
     $mform->set_data($formdata);