MDL-38373 Use correct PEAR->loadExtension()
[moodle.git] / admin / user / user_bulk_download.php
blob5d6ea51d1944f1fc4e9cbb3339191156c9b683b8
1 <?php
2 /**
3 * script for downloading of user lists
4 */
6 require_once('../../config.php');
7 require_once($CFG->libdir.'/adminlib.php');
9 $format = optional_param('format', '', PARAM_ALPHA);
11 require_login();
12 admin_externalpage_setup('userbulk');
13 require_capability('moodle/user:update', context_system::instance());
15 $return = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php';
17 if (empty($SESSION->bulk_users)) {
18 redirect($return);
21 if ($format) {
22 $fields = array('id' => 'id',
23 'username' => 'username',
24 'email' => 'email',
25 'firstname' => 'firstname',
26 'lastname' => 'lastname',
27 'idnumber' => 'idnumber',
28 'institution' => 'institution',
29 'department' => 'department',
30 'phone1' => 'phone1',
31 'phone2' => 'phone2',
32 'city' => 'city',
33 'url' => 'url',
34 'icq' => 'icq',
35 'skype' => 'skype',
36 'aim' => 'aim',
37 'yahoo' => 'yahoo',
38 'msn' => 'msn',
39 'country' => 'country');
41 if ($extrafields = $DB->get_records('user_info_field')) {
42 foreach ($extrafields as $n=>$v){
43 $fields['profile_field_'.$v->shortname] = 'profile_field_'.$v->shortname;
47 switch ($format) {
48 case 'csv' : user_download_csv($fields);
49 case 'ods' : user_download_ods($fields);
50 case 'xls' : user_download_xls($fields);
53 die;
56 echo $OUTPUT->header();
57 echo $OUTPUT->heading(get_string('download', 'admin'));
59 echo $OUTPUT->box_start();
60 echo '<ul>';
61 echo '<li><a href="user_bulk_download.php?format=csv">'.get_string('downloadtext').'</a></li>';
62 echo '<li><a href="user_bulk_download.php?format=ods">'.get_string('downloadods').'</a></li>';
63 echo '<li><a href="user_bulk_download.php?format=xls">'.get_string('downloadexcel').'</a></li>';
64 echo '</ul>';
65 echo $OUTPUT->box_end();
67 echo $OUTPUT->continue_button($return);
69 echo $OUTPUT->footer();
71 function user_download_ods($fields) {
72 global $CFG, $SESSION, $DB;
74 require_once("$CFG->libdir/odslib.class.php");
75 require_once($CFG->dirroot.'/user/profile/lib.php');
77 $filename = clean_filename(get_string('users').'.ods');
79 $workbook = new MoodleODSWorkbook('-');
80 $workbook->send($filename);
82 $worksheet = array();
84 $worksheet[0] = $workbook->add_worksheet('');
85 $col = 0;
86 foreach ($fields as $fieldname) {
87 $worksheet[0]->write(0, $col, $fieldname);
88 $col++;
91 $row = 1;
92 foreach ($SESSION->bulk_users as $userid) {
93 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
94 continue;
96 $col = 0;
97 profile_load_data($user);
98 foreach ($fields as $field=>$unused) {
99 $worksheet[0]->write($row, $col, $user->$field);
100 $col++;
102 $row++;
105 $workbook->close();
106 die;
109 function user_download_xls($fields) {
110 global $CFG, $SESSION, $DB;
112 require_once("$CFG->libdir/excellib.class.php");
113 require_once($CFG->dirroot.'/user/profile/lib.php');
115 $filename = clean_filename(get_string('users').'.xls');
117 $workbook = new MoodleExcelWorkbook('-');
118 $workbook->send($filename);
120 $worksheet = array();
122 $worksheet[0] = $workbook->add_worksheet('');
123 $col = 0;
124 foreach ($fields as $fieldname) {
125 $worksheet[0]->write(0, $col, $fieldname);
126 $col++;
129 $row = 1;
130 foreach ($SESSION->bulk_users as $userid) {
131 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
132 continue;
134 $col = 0;
135 profile_load_data($user);
136 foreach ($fields as $field=>$unused) {
137 $worksheet[0]->write($row, $col, $user->$field);
138 $col++;
140 $row++;
143 $workbook->close();
144 die;
147 function user_download_csv($fields) {
148 global $CFG, $SESSION, $DB;
150 require_once($CFG->dirroot.'/user/profile/lib.php');
151 require_once($CFG->libdir . '/csvlib.class.php');
153 $filename = clean_filename(get_string('users'));
155 $csvexport = new csv_export_writer();
156 $csvexport->set_filename($filename);
157 $csvexport->add_data($fields);
159 foreach ($SESSION->bulk_users as $userid) {
160 $row = array();
161 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
162 continue;
164 profile_load_data($user);
165 $userprofiledata = array();
166 foreach ($fields as $field=>$unused) {
167 // Custom user profile textarea fields come in an array
168 // The first element is the text and the second is the format.
169 // We only take the text.
170 if (is_array($user->$field)) {
171 $userprofiledata[] = reset($user->$field);
172 } else {
173 $userprofiledata[] = $user->$field;
176 $csvexport->add_data($userprofiledata);
178 $csvexport->download_file();
179 die;