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

Refactor form handling

parent 247a8990
Branches
No related tags found
No related merge requests found
...@@ -28,22 +28,29 @@ use moodleform; ...@@ -28,22 +28,29 @@ use moodleform;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
class evaluserexport extends moodleform { global $CFG;
require_once($CFG->libdir.'/formslib.php');
class evaluserexportform extends moodleform {
public function definition() public function definition()
{ {
$mform = $this->_form; $mform = $this->_form;
$courseid = $this->_customdata['courseid'];
$coursecontext = $this->_customdata['coursecontext'];
$introtext = get_string('evaluserexport_form_into', 'local_hshexport'); $introtext = get_string('evaluserexport_form_into', 'local_hshexport');
$mform->addElement('html', '<div id="intor">' . $introtext . '</div>'); $mform->addElement('html', '<div id="intor">' . $introtext . '</div>');
$courseid = $this->_customdata['courseid']; $mform->addElement('hidden', 'courseid', null);
$mform->addElement('hidden', 'courseid', $courseid);
$mform->setType('courseid', PARAM_INT); $mform->setType('courseid', PARAM_INT);
$mform->setConstant('courseid', $courseid);
$coursecontext = $this->_customdata['coursecontext']; $mform->addElement('hidden', 'coursecontext', null);
$mform->addElement('hidden', 'coursecontextid', $coursecontext->id); $mform->setType('coursecontext', PARAM_INT);
$mform->setType('coursecontextid', PARAM_INT); $mform->setConstant('coursecontext', $coursecontext->id);
$placeholder = get_string('course_code_placeholder', 'local_hshexport'); $placeholder = get_string('course_code_placeholder', 'local_hshexport');
$mform->addElement('text', 'course_code', get_string('course_code', 'local_hshexport'), 'maxlength="150" size="50" placeholder="'.$placeholder.'"'); $mform->addElement('text', 'course_code', get_string('course_code', 'local_hshexport'), 'maxlength="150" size="50" placeholder="'.$placeholder.'"');
...@@ -74,6 +81,6 @@ class evaluserexport extends moodleform { ...@@ -74,6 +81,6 @@ class evaluserexport extends moodleform {
); );
$submitlabel = get_string('csvdownload', 'local_hshexport'); $submitlabel = get_string('csvdownload', 'local_hshexport');
$this->add_action_buttons(false, $submitlabel); $this->add_action_buttons(true, $submitlabel);
} }
} }
\ No newline at end of file
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
namespace local_hshexport\local\helpers; namespace local_hshexport\local\helpers;
use core\context; use core\context;
use local_hshexport\local\helpers\user_query;
/** /**
* Plugin version and other meta-data are defined here. * Plugin version and other meta-data are defined here.
...@@ -27,13 +28,14 @@ use core\context; ...@@ -27,13 +28,14 @@ use core\context;
*/ */
class download { class download {
public static function get_evaluation_filename(int $courseid = 0, string $course_code = null): string public static function get_evaluation_filename($data): string
{ {
global $DB; global $DB;
define('FILENAME_PREFIX', 'TN_'); define('FILENAME_PREFIX', 'TN_');
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); $courseid = $data->courseid;
$course = $DB->get_record('course', ['id' => $courseid], '*', MUST_EXIST);
$context = context\course::instance($course->id, MUST_EXIST); $context = context\course::instance($course->id, MUST_EXIST);
if ($context->contextlevel != CONTEXT_COURSE) { if ($context->contextlevel != CONTEXT_COURSE) {
...@@ -41,11 +43,13 @@ class download { ...@@ -41,11 +43,13 @@ class download {
} }
$timestamp = download::get_file_timestamp(); $timestamp = download::get_file_timestamp();
$filename = $course_code;
// use course shortname if no course_code is given // use course shortname if no course_code is given
$course_code = $data->course_code;
if ($course_code == null) { if ($course_code == null) {
$filename = str_replace(',', '-', $course->shortname); $filename = str_replace(',', '-', $course->shortname);
} else {
$filename = $course_code;
} }
// extend filename with prefix and date // extend filename with prefix and date
...@@ -87,4 +91,23 @@ class download { ...@@ -87,4 +91,23 @@ class download {
\csv_export_writer::download_array($filename, $rows, $delimiter, $enclosure); \csv_export_writer::download_array($filename, $rows, $delimiter, $enclosure);
} }
public static function csv_file($data): void
{
global $CFG;
require_once($CFG->libdir.'/csvlib.class.php');
$course_code = $data->course_code;
if (!$course_code) {
$course = get_course($data->courseid);
$course_code = $course->shortname;
}
$filename = self::get_evaluation_filename($data);
$roles = array_filter(array_values($data->roles));
$roleids = array_map('intval', $roles);
$users = user_query::get_users_by_role($data->courseid, $data->coursecontext, $roleids);
$rows = self::get_rows($users, $course_code);
\csv_export_writer::download_array($filename, $rows, $delimiter='semicolon');
}
} }
\ No newline at end of file
...@@ -22,52 +22,70 @@ ...@@ -22,52 +22,70 @@
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
use local_hshexport\form\evaluserexport; use local_hshexport\form\evaluserexportform;
use local_hshexport\local\helpers\download;
require(__DIR__ . '/../../config.php'); require(__DIR__ . '/../../config.php');
require_once($CFG->libdir.'/csvlib.class.php'); require_once($CFG->libdir.'/csvlib.class.php');
$courseid = required_param('id',PARAM_INT); $courseid = required_param('id',PARAM_INT);
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$course = $DB->get_record('course', ['id' => $courseid], '*', MUST_EXIST);
$title = get_string('pluginname', 'local_hshexport'); $title = get_string('pluginname', 'local_hshexport');
require_login($course, true); require_login($course, true);
$PAGE->set_url('/local/hshexport/export.php', array('id' => $course->id)); $PAGE->set_url('/local/hshexport/export.php', ['id' => $course->id]);
$PAGE->set_title($title); $PAGE->set_title($title);
$coursecontext = context_course::instance($course->id); $coursecontext = context_course::instance($course->id);
$PAGE->set_context($coursecontext); $PAGE->set_context($coursecontext);
require_capability('local/hshexport:canexport', $coursecontext); require_capability('local/hshexport:canexport', $coursecontext);
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
$customdata = [ $customdata = [
'courseid' => $courseid, 'courseid' => $courseid,
'coursecontext' => $coursecontext 'coursecontext' => $coursecontext,
]; ];
$mform = new evaluserexport('#', $customdata); $mform = new evaluserexportform('#', $customdata);
if ($formdata = $mform->get_data()) { $formdata = $mform->get_data();
// removes not selected roles from array
$roleids = array_values(array_filter($formdata->roles));
$roles_param = urlencode(json_encode($roleids));
$params = [
'id' => $formdata->courseid,
'context' => $formdata->coursecontextid,
'roles' => $roles_param,
'code' => $formdata->course_code
];
redirect(new moodle_url('/local/hshexport/download.php', $params));
if ($mform->is_cancelled()) {
$returnurl = new moodle_url('/user/index.php', ['id' => $course->id]);
redirect($returnurl);
} elseif ($formdata) {
download::csv_file($formdata);
} else { } else {
$mform->set_data($formdata); $mform->set_data($formdata);
} }
//
// // removes not selected roles from array
// $roleids = array_values(array_filter($formdata->roles));
// $roles_param = urlencode(json_encode($roleids));
//
// $params = [
// 'id' => $formdata->courseid,
// 'context' => $formdata->coursecontextid,
// 'roles' => $roles_param,
// 'code' => $formdata->course_code
// ];
//
//// redirect(new moodle_url('/local/hshexport/download.php', $params));
//
//} else {
// $mform->set_data($formdata);
//}
echo $OUTPUT->header();
echo $OUTPUT->heading($title);
$mform->display(); $mform->display();
echo $OUTPUT->footer(); echo $OUTPUT->footer();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment