MDL-71981 user: escape identity fields if writer supports HTML.
[moodle.git] / admin / user / user_bulk_download.php
CommitLineData
244bb396 1<?php
12142298
BH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
444598e9 16
12142298
BH
17/**
18 * Bulk export user into any dataformat
19 *
20 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
21 * @copyright 2007 Petr Skoda
22 * @package core
23 */
24
25define('NO_OUTPUT_BUFFERING', true);
444598e9 26require_once('../../config.php');
27require_once($CFG->libdir.'/adminlib.php');
12142298 28require_once($CFG->dirroot.'/user/profile/lib.php');
444598e9 29
12142298 30$dataformat = optional_param('dataformat', '', PARAM_ALPHA);
444598e9 31
32admin_externalpage_setup('userbulk');
bf006d2c 33require_capability('moodle/user:update', context_system::instance());
444598e9 34
444598e9 35if (empty($SESSION->bulk_users)) {
12142298 36 redirect(new moodle_url('/admin/user/user_bulk.php'));
444598e9 37}
38
12142298 39if ($dataformat) {
444598e9 40 $fields = array('id' => 'id',
c79106c4 41 'username' => 'username',
42 'email' => 'email',
43 'firstname' => 'firstname',
44 'lastname' => 'lastname',
45 'idnumber' => 'idnumber',
46 'institution' => 'institution',
47 'department' => 'department',
48 'phone1' => 'phone1',
49 'phone2' => 'phone2',
50 'city' => 'city',
c79106c4 51 'country' => 'country');
52
5682b9a5
MG
53 $extrafields = profile_get_user_fields_with_data(0);
54 foreach ($extrafields as $formfield) {
55 $fields['profile_field_'.$formfield->get_shortname()] = 'profile_field_'.$formfield->get_shortname();
444598e9 56 }
57
9a5abd1b 58 $filename = clean_filename(get_string('users'));
444598e9 59
12142298
BH
60 $downloadusers = new ArrayObject($SESSION->bulk_users);
61 $iterator = $downloadusers->getIterator();
444598e9 62
526f5ecc
PH
63 \core\dataformat::download_data($filename, $dataformat, $fields, $iterator, function($userid, $supportshtml)
64 use ($extrafields, $fields) {
65
12142298 66 global $DB;
8844cb82 67
12142298
BH
68 if (!$user = $DB->get_record('user', array('id' => $userid))) {
69 return null;
70 }
5682b9a5 71 profile_load_data($user);
9a5abd1b 72 $userprofiledata = array();
12142298 73 foreach ($fields as $field => $unused) {
9a5abd1b
AG
74 // Custom user profile textarea fields come in an array
75 // The first element is the text and the second is the format.
76 // We only take the text.
77 if (is_array($user->$field)) {
12142298 78 $userprofiledata[$field] = reset($user->$field);
526f5ecc
PH
79 } else if ($supportshtml) {
80 $userprofiledata[$field] = s($user->$field);
9a5abd1b 81 } else {
12142298 82 $userprofiledata[$field] = $user->$field;
9a5abd1b 83 }
444598e9 84 }
12142298
BH
85 return $userprofiledata;
86 });
87
88 exit;
444598e9 89}
12142298
BH
90
91echo $OUTPUT->header();
92echo $OUTPUT->heading(get_string('download', 'admin'));
93echo $OUTPUT->download_dataformat_selector(get_string('userbulkdownload', 'admin'), 'user_bulk_download.php');
94echo $OUTPUT->footer();
95