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 {
/** @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.
*
* @return string
* @return array
*/
private function set_roles($data) {
if (property_exists($data, 'roles')) {
$roleids = array_filter(array_values($data->roles));
private function set_roles($roles) {
$roleids = array_filter($roles);
$roles = array_map('intval', $roleids);
return $roles;
} else {
return [];
}
}
/**
......@@ -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;
}
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment