diff --git a/action_download.php b/action_download.php new file mode 100644 index 0000000000000000000000000000000000000000..75d16ebe38cae96a098874174b4b796d9aeb2064 --- /dev/null +++ b/action_download.php @@ -0,0 +1,47 @@ +<?php +// This file is part of Moodle - http://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. + +/** + * Wrapper script redirecting user operations to correct destination. + * + * @copyright 1999 Martin Dougiamas http://dougiamas.com + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package local_hshexport + */ + +use local_hshexport\local\helpers\download; + +require(__DIR__ . '/../../config.php'); + +$PAGE->set_url('/local/hshexport/action_download.php'); + +if (!confirm_sesskey()) { + throw new \moodle_exception('confirmsesskeybad'); +} + +$course_code = optional_param('course_code', null, PARAM_ALPHANUMEXT); +$roleids = required_param_array('select_roles', PARAM_INT); +$courseid = required_param('courseid', PARAM_INT); +$coursecontextid = required_param('coursecontextid', PARAM_INT); + +$filename = download::get_evaluation_filename($courseid, $course_code); +$rows = local_hshexport\local\helpers\user_query::get_users_by_role($courseid, $coursecontextid, $roleids); + +if ($course_code != null) { + $rows = local_hshexport\local\helpers\download::get_rows($rows, $course_code); +} + +\core\dataformat::download_data($filename, 'csv', [], $rows); diff --git a/classes/form/evaluserexport.php b/classes/form/evaluserexport.php new file mode 100644 index 0000000000000000000000000000000000000000..7af2a8b715368afcea43970cdacbc7b81330f6a5 --- /dev/null +++ b/classes/form/evaluserexport.php @@ -0,0 +1,75 @@ +<?php +// This file is part of Moodle - https://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see <https://www.gnu.org/licenses/>. + +/** + * Plugin version and other meta-data are defined here. + * + * @package local_hshexport + * @copyright 2024 Elke Kreim elke.kreim@hs-hannover.de + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace local_hshexport\form; + +use moodleform; + +defined('MOODLE_INTERNAL') || die(); + +class evaluserexport extends moodleform { + + public function definition() + { + $mform = $this->_form; + + $introtext = get_string('evaluserexport_form_into', 'local_hshexport'); + $mform->addElement('html', '<div id="intor">' . $introtext . '</div>'); + + $courseid = $this->_customdata['courseid']; + $coursecontext = $this->_customdata['coursecontext']; + $viewableroles = get_profile_roles($coursecontext); + $roles = role_fix_names($viewableroles, $coursecontext, ROLENAME_ALIAS); + $options = []; + foreach ($roles as $role) { + $options[$role->id] = $role->localname; + } + + $mform->addElement('text', 'course_code', get_string('course_code', 'local_hshexport'), 'maxlength="100" size="50" placeholder="BBA-422-01"'); + $mform->setType('course_code', PARAM_ALPHANUMEXT); + $mform->addHelpButton('course_code', 'course_code', 'local_hshexport'); + $mform->setDefault('course_code', null); + + $roles = role_fix_names($viewableroles, $coursecontext, ROLENAME_ALIAS); + $options = []; + foreach ($roles as $role) { + $options[$role->id] = $role->localname; + } + $select = $mform->addElement('select', 'select_roles', get_string('select_roles', 'local_hshexport'), $options); + $select->setMultiple(true); + $mform->addHelpButton('select_roles', 'select_roles', 'local_hshexport'); + $mform->addRule('select_roles', get_string('noselectedroles', 'local_hshexport'), 'required', 'null', true); + + $mform->addElement('hidden', 'courseid', $courseid); + $mform->setType('courseid', PARAM_INT); + + $mform->addElement('hidden', 'coursecontextid', $coursecontext->id); + $mform->setType('coursecontextid', PARAM_INT); + + $submitlabel = get_string('csvdownload', 'local_hshexport'); + $this->add_action_buttons(false, $submitlabel); + + } + +} \ No newline at end of file diff --git a/classes/local/helpers/download.php b/classes/local/helpers/download.php new file mode 100644 index 0000000000000000000000000000000000000000..9176edcb91ec5c027626524678ddc37ce8c2b2c8 --- /dev/null +++ b/classes/local/helpers/download.php @@ -0,0 +1,78 @@ +<?php +// This file is part of Moodle - https://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see <https://www.gnu.org/licenses/>. + +namespace local_hshexport\local\helpers; + +use core\context; + +/** + * Plugin version and other meta-data are defined here. + * + * @package local_hshexport + * @copyright 2024 Elke Kreim elke.kreim@hs-hannover.de + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +class download { + public static function get_evaluation_filename(int $courseid = 0, string $course_code = null): string + { + global $DB; + + define('FILENAME_PREFIX', 'TN_'); + + $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); + $context = context\course::instance($course->id, MUST_EXIST); + + if ($context->contextlevel != CONTEXT_COURSE) { + throw new \moodle_exception('invalidcontext'); + } + + $timestamp = download::get_file_timestamp(); + + if ($course_code != null) { + $filename = FILENAME_PREFIX . $course_code . '_' . $timestamp; + } else { + $filename = FILENAME_PREFIX . $course->shortname . '_' . $timestamp; + } + return $filename; + } + + public static function get_file_timestamp(): string + { + $now = date_create('now'); + $timestamp = ($now->format('Ymd')); + + return $timestamp; + } + + public static function get_rows($users, $course_code = null): array + { + if ($course_code === null) { + return $users; + } + + $rows = []; + + foreach ($users as $user) { + $row = [ + 'course_code' => $course_code, + 'email' => $user->email + ]; + array_push($rows, $row); + } + return $rows; + } +} \ No newline at end of file diff --git a/classes/local/helpers/user_query.php b/classes/local/helpers/user_query.php new file mode 100644 index 0000000000000000000000000000000000000000..e13091479a06f6f1a287469b61bb037756af79f1 --- /dev/null +++ b/classes/local/helpers/user_query.php @@ -0,0 +1,76 @@ +<?php +// This file is part of Moodle - https://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see <https://www.gnu.org/licenses/>. + +/** + * Plugin version and other meta-data are defined here. + * + * @package local_hshexport + * @copyright 2024 Elke Kreim elke.kreim@hs-hannover.de + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace local_hshexport\local\helpers; + +use core\context; + +class user_query { + + const ENROLEMENT_STATUS = 0; + const USER_DELETED = 0; + const USER_SUSPENDED = 0; + + public static function get_users_by_role(int $courseid = 0, int $contextid = 0, array $roles = null): array { + global $DB; + + $users = []; + + if ($contextid) { + $context = context::instance_by_id($contextid, MUST_EXIST); + if ($context->contextlevel != CONTEXT_COURSE) { + throw new \moodle_exception('invalidcontext'); + } + $course = $DB->get_record('course', array('id' => $context->instanceid), '*', MUST_EXIST); + } else { + $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); + $context = context\course::instance($course->id, MUST_EXIST); + } + + [$inrolesql, $params] = $DB->get_in_or_equal($roles, SQL_PARAMS_NAMED); + + $sql = "SELECT DISTINCT u.email + FROM {user} u + JOIN {role_assignments} ra ON u.id=ra.userid + JOIN mdl_user_enrolments e ON e.userid=u.id + JOIN mdl_role r ON ra.roleid=r.id + JOIN mdl_context c ON ra.contextid=c.id + WHERE c.contextlevel= :contextlevel + AND e.status = :enrolment_status + AND u.deleted = :user_deleted + AND u.suspended = :user_suspended + AND instanceid = :courseid + AND r.id {$inrolesql}"; + + $params['contextlevel'] = CONTEXT_COURSE; + $params['enrolment_status'] = self::ENROLEMENT_STATUS; + $params['user_deleted'] = self::USER_DELETED; + $params['user_suspended'] = self::USER_SUSPENDED; + $params['courseid'] = $courseid; + + $users = $DB->get_records_sql($sql, $params); + + return $users; + } +} diff --git a/index.php b/index.php new file mode 100644 index 0000000000000000000000000000000000000000..6a0956ba9e9f86e1305f209af75fae0e51583e15 --- /dev/null +++ b/index.php @@ -0,0 +1,54 @@ +<?php +// This file is part of Moodle - https://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see <https://www.gnu.org/licenses/>. + +/** + * Plugin version and other meta-data are defined here. + * + * @package local_hshexport + * @copyright 2024 Elke Kreim elke.kreim@hs-hannover.de + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +use local_hshexport\form\evaluserexport; + + +require(__DIR__ . '/../../config.php'); + +$courseid = required_param('id', PARAM_INT); +$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); +$title = get_string('pluginname', 'local_hshexport'); + +require_login($course, true); + +$PAGE->set_url('/local/hshexport/index.php', array('id' => $course->id)); +$PAGE->set_title($title); + +$coursecontext = context_course::instance($course->id); +$PAGE->set_context($coursecontext); +require_capability('local/hshexport:canexport', $coursecontext); + +echo $OUTPUT->header(); +echo $OUTPUT->heading($title); + +$customdata = [ + 'courseid' => $courseid, + 'coursecontext' => $coursecontext +]; + +$mform = new evaluserexport('action_download.php', $customdata); + +$mform->display(); +echo $OUTPUT->footer(); diff --git a/lang/de/local_hshexport.php b/lang/de/local_hshexport.php new file mode 100644 index 0000000000000000000000000000000000000000..7d7fd2151a8f8a15bccc2c5c37278771e18eebe0 --- /dev/null +++ b/lang/de/local_hshexport.php @@ -0,0 +1,40 @@ +<?php +// This file is part of Moodle - https://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see <https://www.gnu.org/licenses/>. + +/** + * Plugin strings are defined here. + * + * @package local_hshexport + * @category string + * @copyright 2024 Elke Kreim elke.kreim@hs-hannover.de + * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$string['hshexport:canexport'] = 'Kann Nutzer für Evasys als csv exportieren'; +$string['pluginname'] = 'HSH Nutzer Export für Evaluation'; +$string['nav_course'] = 'Link in Kursnavigation'; +$string['nav_participants'] = 'Link in Auswahlbutton der Teilnehmerliste'; +$string['navigation'] = 'Platzierung des Links'; +$string['navigation_desc'] = 'Der Ort, an dem der Link für das Formular zum Export von Nutzern zur Evaluation innerhalb eines Kurses hinzugefügt wird.'; +$string['course_code'] = 'LV-Kennung'; +$string['course_code_help'] = 'Eine LV-Kennung muss eindeutig und einmalig sein, z.B. eine Modulkurzbezeichnung. Sie ist die eindeutige Kennung, unter der die Evaluation durchgeführt wird. Beispiele: BBA-422-01 oder 010-103'; +$string['select_roles'] = 'Rollen'; +$string['select_roles_help'] = 'Nutzer mit dieser Rolle bzw. diesen Rollen exportieren.'; +$string['csvdownload'] = 'CSV herunterladen'; +$string['noselectedroles'] = 'Mindestens eine Rolle muss ausgewählt werden.'; +$string['evaluserexport_form_into'] = 'Nutzen Sie dieses Formular um eine CSV-Datei mit den Nutzern der ausgewählten Rollen für die Evaluation zu erzeugen.'; diff --git a/lang/en/local_hshexport.php b/lang/en/local_hshexport.php index f8145ab23a1f0fd3be8d7b558efa1da6f190fd06..0d43f4a5534131c61993cdbccfe0efd0bd132da6 100644 --- a/lang/en/local_hshexport.php +++ b/lang/en/local_hshexport.php @@ -30,4 +30,11 @@ $string['pluginname'] = 'HSH User export'; $string['nav_course'] = 'Navigation node in course navigation'; $string['nav_participants'] = 'Navigation node in participants page jump menu'; $string['navigation'] = 'Navigation node placement'; -$string['navigation_desc'] = 'The location where the navigation node for user bulk enrolment will be added within a course.'; +$string['navigation_desc'] = 'The location where the navigation node for evaluation user export form will be added within a course.'; +$string['course_code'] = 'Course code'; +$string['course_code_help'] = 'A course code must be unique and unambiguous, e.g. a module abbreviation. It is the unique identifier under which the evaluation is carried out. Examples BBA-422-01 or 010-103'; +$string['select_roles'] = 'Roles'; +$string['select_roles_help'] = 'Export users with this role or these roles.'; +$string['csvdownload'] = 'Download csv'; +$string['noselectedroles'] = 'At least one role has to be selected.'; +$string['evaluserexport_form_into'] = 'Use this form to generate a CSV file with users of selected roles for evaluation purposes.'; \ No newline at end of file diff --git a/lib.php b/lib.php index f0cba1c011b5ced1e0070f0ab787a675e6e2f5e2..e125dd3e6d43cb6d505bfc7e85a64e3b9997372a 100644 --- a/lib.php +++ b/lib.php @@ -23,4 +23,32 @@ */ define('LOCALHSHEXPORT_NAV_COURSE', 'navcourse'); -define('LOCALHSHEXPORT_NAV_PARTICIPANTS', 'navpart'); \ No newline at end of file +define('LOCALHSHEXPORT_NAV_PARTICIPANTS', 'navpart'); + + +function local_hshexport_extend_navigation_course($navigation, $course, $context) { + if (has_capability('local/hshexport:canexport', $context)) { + // Create the navigation node. + $url = new moodle_url('/local/hshexport/index.php', ['id' => $course->id]); + $hshexportnode = navigation_node::create(get_string('pluginname', 'local_hshexport'), $url, + navigation_node::TYPE_SETTING, null, 'local_hshexport', new pix_icon('i/users', '')); + + // Get the navigation node placement setting. + $navigationplacement = get_config('local_hshexport', 'navigation'); + + // If the admin wanted to add the navigation node to the participants page jump menu. + if ($navigationplacement == LOCALBULKENROL_NAV_PARTICIPANTS) { + $usersnode = $navigation->get('users'); + if (isset($hshexportnode) && !empty($usersnode)) { + $usersnode->add_node($hshexportnode); + } + } + + // If the admin wanted to add the navigation node to the course navigation. + if ($navigationplacement == LOCALBULKENROL_NAV_COURSE) { + if (isset($hshexportnode)) { + $navigation->add_node($hshexportnode); + } + } + } +}