2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Bulk export user into any dataformat
20 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
21 * @copyright 2007 Petr Skoda
27 define('NO_OUTPUT_BUFFERING', true);
28 require_once('../../config.php');
29 require_once($CFG->libdir
.'/adminlib.php');
30 require_once($CFG->dirroot
.'/user/profile/lib.php');
32 $dataformat = optional_param('dataformat', '', PARAM_ALPHA
);
34 admin_externalpage_setup('userbulk');
35 require_capability('moodle/user:update', context_system
::instance());
37 $returnurl = optional_param('returnurl', '', PARAM_LOCALURL
);
38 $return = new moodle_url($returnurl ?
: '/admin/user/user_bulk.php');
40 if (empty($SESSION->bulk_users
)) {
45 $originfields = array('id' => 'id',
46 'username' => 'username',
48 'firstname' => 'firstname',
49 'lastname' => 'lastname',
50 'idnumber' => 'idnumber',
51 'institution' => 'institution',
52 'department' => 'department',
56 'country' => 'country');
58 $extrafields = profile_get_user_fields_with_data(0);
60 foreach ($extrafields as $formfield) {
61 $profilefields[fields
::PROFILE_FIELD_PREFIX
. $formfield->get_shortname()] = fields
::PROFILE_FIELD_PREFIX
.
62 $formfield->get_shortname();
65 $filename = clean_filename(get_string('users'));
67 $downloadusers = new ArrayObject($SESSION->bulk_users
);
68 $iterator = $downloadusers->getIterator();
70 \core\dataformat
::download_data($filename, $dataformat, array_merge($originfields, $profilefields), $iterator,
71 function($userid, $supportshtml) use ($originfields) {
75 if (!$user = $DB->get_record('user', array('id' => $userid))) {
79 $userprofiledata = array();
80 foreach ($originfields as $field) {
81 // Custom user profile textarea fields come in an array
82 // The first element is the text and the second is the format.
83 // We only take the text.
84 if (is_array($user->$field)) {
85 $userprofiledata[$field] = reset($user->$field);
86 } else if ($supportshtml) {
87 $userprofiledata[$field] = s($user->$field);
89 $userprofiledata[$field] = $user->$field;
94 // Formatting extra field if transform is true.
95 $extrafields = profile_get_user_fields_with_data($userid);
96 foreach ($extrafields as $field) {
97 $fieldkey = fields
::PROFILE_FIELD_PREFIX
. $field->get_shortname();
98 if ($field->is_transform_supported()) {
99 $userprofiledata[$fieldkey] = $field->display_data();
101 $userprofiledata[$fieldkey] = $field->data
;
105 return $userprofiledata;
111 $PAGE->set_primary_active_tab('siteadminnode');
112 $PAGE->set_secondary_active_tab('users');
114 echo $OUTPUT->header();
115 echo $OUTPUT->heading(get_string('download', 'admin'));
116 echo $OUTPUT->download_dataformat_selector(get_string('userbulkdownload', 'admin'), 'user_bulk_download.php');
117 echo $OUTPUT->footer();