Skip to content
Snippets Groups Projects
Commit cab3a230 authored by Elke Kreim's avatar Elke Kreim
Browse files

Take course shortname as lv code if no code is set

parent 0a2879b2
No related branches found
No related tags found
No related merge requests found
...@@ -36,7 +36,9 @@ class exporter { ...@@ -36,7 +36,9 @@ class exporter {
/** @var int ID of a course. */ /** @var int ID of a course. */
private int $courseid; private int $courseid;
/** @var string Identifier for the evaluation of a course. */ /** @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. */ /** @var int[] Array of role ids. */
private array $roles; private array $roles;
/** @var string Name of the file to download. */ /** @var string Name of the file to download. */
...@@ -47,45 +49,37 @@ class exporter { ...@@ -47,45 +49,37 @@ class exporter {
/** /**
* Creates an exporter object. * Creates an exporter object.
* *
* @param stdClass $data * @param int $courseid
* @param string $ccode
* @param string $codereplacement
* @param array $roles
*/ */
public function __construct($data) { public function __construct($courseid=0, $code=null, $codereplacement=null, $roles=[]) {
$this->courseid = $this->set_courseid($data); $this->courseid = $this->set_courseid($courseid);
$this->coursecode = $this->set_coursecode($data); $this->code = $code;
$this->roles = $this->set_roles($data); $this->codereplacement = $codereplacement;
$this->roles = $this->set_roles($roles);
$this->filename = $this->set_filename(); $this->filename = $this->set_filename();
$this->content = $this->set_content(); $this->content = $this->set_content();
} }
private function set_courseid($data) { private function set_courseid($id) {
if (property_exists($data, 'courseid')) { if ($id != 0) {
return $data->courseid; return $id;
} else { } else {
throw new \moodle_exception('Error: no course id given.'); 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) { private function set_roles($roles) {
if (property_exists($data, 'roles')) { $roleids = array_filter($roles);
$roleids = array_filter(array_values($data->roles));
$roles = array_map('intval', $roleids); $roles = array_map('intval', $roleids);
return $roles; return $roles;
} else {
return [];
}
} }
/** /**
...@@ -103,11 +97,19 @@ class exporter { ...@@ -103,11 +97,19 @@ class exporter {
return \csv_export_writer::print_array($rows, $delimiter, '"', true); 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. * Create filename.
* *
* This method creates a filename for the file to be downloaded. The filename consists of a given prefix, * 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. * short name of the course.
* *
* @return string * @return string
...@@ -115,18 +117,7 @@ class exporter { ...@@ -115,18 +117,7 @@ class exporter {
private function set_filename() { private function set_filename() {
// Use course shortname if no course code is given. // Use course shortname if no course code is given.
$filename = $this->coursecode; $filename = $this->get_base_filename();
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);
}
// Extend filename with prefix and timestamp. // Extend filename with prefix and timestamp.
$filename = $this->add_prefix($filename); $filename = $this->add_prefix($filename);
$filename = $this->add_timestamp($filename); $filename = $this->add_timestamp($filename);
...@@ -166,10 +157,10 @@ class exporter { ...@@ -166,10 +157,10 @@ class exporter {
$rows = []; $rows = [];
// If no code is set, use alternative
$code = $this->code ? $this->code : $this->codereplacement;
foreach ($users as $user) { foreach ($users as $user) {
if ($this->coursecode) { $row['code'] = $code;
$row['coursecode'] = $this->coursecode;
}
$row['email'] = $user->email; $row['email'] = $user->email;
$rows[] = $row; $rows[] = $row;
} }
......
...@@ -61,7 +61,11 @@ if ($mform->is_cancelled()) { ...@@ -61,7 +61,11 @@ if ($mform->is_cancelled()) {
$returnurl = new moodle_url('/user/index.php', ['id' => $course->id]); $returnurl = new moodle_url('/user/index.php', ['id' => $course->id]);
redirect($returnurl); redirect($returnurl);
} else if ($formdata) { } 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(); $exporter->send_file();
} else { } else {
$mform->set_data($formdata); $mform->set_data($formdata);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment