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

Create plugin with form for user export

parent d172a1a1
No related branches found
No related tags found
No related merge requests found
<?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);
<?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
<?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
<?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;
}
}
<?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();
<?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.';
...@@ -30,4 +30,11 @@ $string['pluginname'] = 'HSH User export'; ...@@ -30,4 +30,11 @@ $string['pluginname'] = 'HSH User export';
$string['nav_course'] = 'Navigation node in course navigation'; $string['nav_course'] = 'Navigation node in course navigation';
$string['nav_participants'] = 'Navigation node in participants page jump menu'; $string['nav_participants'] = 'Navigation node in participants page jump menu';
$string['navigation'] = 'Navigation node placement'; $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
...@@ -24,3 +24,31 @@ ...@@ -24,3 +24,31 @@
define('LOCALHSHEXPORT_NAV_COURSE', 'navcourse'); define('LOCALHSHEXPORT_NAV_COURSE', 'navcourse');
define('LOCALHSHEXPORT_NAV_PARTICIPANTS', 'navpart'); 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);
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment