Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/user/user_bulk_download.php
Go to the documentation of this file.
00001 <?php
00006 require_once('../../config.php');
00007 require_once($CFG->libdir.'/adminlib.php');
00008 
00009 $format = optional_param('format', '', PARAM_ALPHA);
00010 
00011 require_login();
00012 admin_externalpage_setup('userbulk');
00013 require_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM));
00014 
00015 $return = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php';
00016 
00017 if (empty($SESSION->bulk_users)) {
00018     redirect($return);
00019 }
00020 
00021 if ($format) {
00022     $fields = array('id'        => 'id',
00023                     'username'  => 'username',
00024                     'email'     => 'email',
00025                     'firstname' => 'firstname',
00026                     'lastname'  => 'lastname',
00027                     'idnumber'  => 'idnumber',
00028                     'institution' => 'institution',
00029                     'department' => 'department',
00030                     'phone1'    => 'phone1',
00031                     'phone2'    => 'phone2',
00032                     'city'      => 'city',
00033                     'url'       => 'url',
00034                     'icq'       => 'icq',
00035                     'skype'     => 'skype',
00036                     'aim'       => 'aim',
00037                     'yahoo'     => 'yahoo',
00038                     'msn'       => 'msn',
00039                     'country'   => 'country');
00040 
00041     if ($extrafields = $DB->get_records('user_info_field')) {
00042         foreach ($extrafields as $n=>$v){
00043             $fields['profile_field_'.$v->shortname] = 'profile_field_'.$v->shortname;
00044         }
00045     }
00046 
00047     switch ($format) {
00048         case 'csv' : user_download_csv($fields);
00049         case 'ods' : user_download_ods($fields);
00050         case 'xls' : user_download_xls($fields);
00051 
00052     }
00053     die;
00054 }
00055 
00056 echo $OUTPUT->header();
00057 echo $OUTPUT->heading(get_string('download', 'admin'));
00058 
00059 echo $OUTPUT->box_start();
00060 echo '<ul>';
00061 echo '<li><a href="user_bulk_download.php?format=csv">'.get_string('downloadtext').'</a></li>';
00062 echo '<li><a href="user_bulk_download.php?format=ods">'.get_string('downloadods').'</a></li>';
00063 echo '<li><a href="user_bulk_download.php?format=xls">'.get_string('downloadexcel').'</a></li>';
00064 echo '</ul>';
00065 echo $OUTPUT->box_end();
00066 
00067 echo $OUTPUT->continue_button($return);
00068 
00069 echo $OUTPUT->footer();
00070 
00071 function user_download_ods($fields) {
00072     global $CFG, $SESSION, $DB;
00073 
00074     require_once("$CFG->libdir/odslib.class.php");
00075     require_once($CFG->dirroot.'/user/profile/lib.php');
00076 
00077     $filename = clean_filename(get_string('users').'.ods');
00078 
00079     $workbook = new MoodleODSWorkbook('-');
00080     $workbook->send($filename);
00081 
00082     $worksheet = array();
00083 
00084     $worksheet[0] =& $workbook->add_worksheet('');
00085     $col = 0;
00086     foreach ($fields as $fieldname) {
00087         $worksheet[0]->write(0, $col, $fieldname);
00088         $col++;
00089     }
00090 
00091     $row = 1;
00092     foreach ($SESSION->bulk_users as $userid) {
00093         if (!$user = $DB->get_record('user', array('id'=>$userid))) {
00094             continue;
00095         }
00096         $col = 0;
00097         profile_load_data($user);
00098         foreach ($fields as $field=>$unused) {
00099             $worksheet[0]->write($row, $col, $user->$field);
00100             $col++;
00101         }
00102         $row++;
00103     }
00104 
00105     $workbook->close();
00106     die;
00107 }
00108 
00109 function user_download_xls($fields) {
00110     global $CFG, $SESSION, $DB;
00111 
00112     require_once("$CFG->libdir/excellib.class.php");
00113     require_once($CFG->dirroot.'/user/profile/lib.php');
00114 
00115     $filename = clean_filename(get_string('users').'.xls');
00116 
00117     $workbook = new MoodleExcelWorkbook('-');
00118     $workbook->send($filename);
00119 
00120     $worksheet = array();
00121 
00122     $worksheet[0] =& $workbook->add_worksheet('');
00123     $col = 0;
00124     foreach ($fields as $fieldname) {
00125         $worksheet[0]->write(0, $col, $fieldname);
00126         $col++;
00127     }
00128 
00129     $row = 1;
00130     foreach ($SESSION->bulk_users as $userid) {
00131         if (!$user = $DB->get_record('user', array('id'=>$userid))) {
00132             continue;
00133         }
00134         $col = 0;
00135         profile_load_data($user);
00136         foreach ($fields as $field=>$unused) {
00137             $worksheet[0]->write($row, $col, $user->$field);
00138             $col++;
00139         }
00140         $row++;
00141     }
00142 
00143     $workbook->close();
00144     die;
00145 }
00146 
00147 function user_download_csv($fields) {
00148     global $CFG, $SESSION, $DB;
00149 
00150     require_once($CFG->dirroot.'/user/profile/lib.php');
00151 
00152     $filename = clean_filename(get_string('users').'.csv');
00153 
00154     header("Content-Type: application/download\n");
00155     header("Content-Disposition: attachment; filename=$filename");
00156     header("Expires: 0");
00157     header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
00158     header("Pragma: public");
00159 
00160     $delimiter = get_string('listsep', 'langconfig');
00161     $encdelim  = '&#'.ord($delimiter);
00162 
00163     $row = array();
00164     foreach ($fields as $fieldname) {
00165         $row[] = str_replace($delimiter, $encdelim, $fieldname);
00166     }
00167     echo implode($delimiter, $row)."\n";
00168 
00169     foreach ($SESSION->bulk_users as $userid) {
00170         $row = array();
00171         if (!$user = $DB->get_record('user', array('id'=>$userid))) {
00172             continue;
00173         }
00174         profile_load_data($user);
00175         foreach ($fields as $field=>$unused) {
00176             $row[] = str_replace($delimiter, $encdelim, $user->$field);
00177         }
00178         echo implode($delimiter, $row)."\n";
00179     }
00180     die;
00181 }
 All Data Structures Namespaces Files Functions Variables Enumerations