Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
moodle-local_hshexport
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elc
moodle-local_hshexport
Commits
70fd9c9e
Commit
70fd9c9e
authored
4 months ago
by
Elke Kreim
Browse files
Options
Downloads
Patches
Plain Diff
Refactor form handling
parent
247a8990
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
classes/form/evaluserexportform.php
+14
-7
14 additions, 7 deletions
classes/form/evaluserexportform.php
classes/local/helpers/download.php
+26
-3
26 additions, 3 deletions
classes/local/helpers/download.php
export.php
+40
-22
40 additions, 22 deletions
export.php
with
80 additions
and
32 deletions
classes/form/evaluserexport.php
→
classes/form/evaluserexport
form
.php
+
14
−
7
View file @
70fd9c9e
...
@@ -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'
,
'coursecontext
id
'
,
$coursecontext
->
id
);
$mform
->
setType
(
'coursecontext'
,
PARAM_INT
);
$mform
->
set
Type
(
'coursecontext
id
'
,
PARAM_INT
);
$mform
->
set
Constant
(
'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
(
fals
e
,
$submitlabel
);
$this
->
add_action_buttons
(
tru
e
,
$submitlabel
);
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
classes/local/helpers/download.php
+
26
−
3
View file @
70fd9c9e
...
@@ -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
This diff is collapsed.
Click to expand it.
export.php
+
40
−
22
View file @
70fd9c9e
...
@@ -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
evaluserexport
form
(
'#'
,
$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
();
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment