3 * script for downloading of user lists
6 require_once('../../config.php');
7 require_once($CFG->libdir
.'/adminlib.php');
9 $format = optional_param('format', '', PARAM_ALPHA
);
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
)) {
22 $fields = array('id' => 'id',
23 'username' => 'username',
25 'firstname' => 'firstname',
26 'lastname' => 'lastname',
27 'idnumber' => 'idnumber',
28 'institution' => 'institution',
29 'department' => 'department',
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
;
48 case 'csv' : user_download_csv($fields);
49 case 'ods' : user_download_ods($fields);
50 case 'xls' : user_download_xls($fields);
56 echo $OUTPUT->header();
57 echo $OUTPUT->heading(get_string('download', 'admin'));
59 echo $OUTPUT->box_start();
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>';
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);
84 $worksheet[0] = $workbook->add_worksheet('');
86 foreach ($fields as $fieldname) {
87 $worksheet[0]->write(0, $col, $fieldname);
92 foreach ($SESSION->bulk_users
as $userid) {
93 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
97 profile_load_data($user);
98 foreach ($fields as $field=>$unused) {
99 $worksheet[0]->write($row, $col, $user->$field);
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('');
124 foreach ($fields as $fieldname) {
125 $worksheet[0]->write(0, $col, $fieldname);
130 foreach ($SESSION->bulk_users
as $userid) {
131 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
135 profile_load_data($user);
136 foreach ($fields as $field=>$unused) {
137 $worksheet[0]->write($row, $col, $user->$field);
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) {
161 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
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);
173 $userprofiledata[] = $user->$field;
176 $csvexport->add_data($userprofiledata);
178 $csvexport->download_file();