[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/admin/user/ -> user_bulk_download.php (source)

   1  <?php
   2  /**
   3  * script for downloading of user lists
   4  */
   5  
   6  require_once('../../config.php');
   7  require_once($CFG->libdir.'/adminlib.php');
   8  
   9  $format = optional_param('format', '', PARAM_ALPHA);
  10  
  11  require_login();
  12  admin_externalpage_setup('userbulk');
  13  require_capability('moodle/user:update', context_system::instance());
  14  
  15  $return = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php';
  16  
  17  if (empty($SESSION->bulk_users)) {
  18      redirect($return);
  19  }
  20  
  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');
  40  
  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;
  44          }
  45      }
  46  
  47      switch ($format) {
  48          case 'csv' : user_download_csv($fields);
  49          case 'ods' : user_download_ods($fields);
  50          case 'xls' : user_download_xls($fields);
  51  
  52      }
  53      die;
  54  }
  55  
  56  echo $OUTPUT->header();
  57  echo $OUTPUT->heading(get_string('download', 'admin'));
  58  
  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();
  66  
  67  echo $OUTPUT->continue_button($return);
  68  
  69  echo $OUTPUT->footer();
  70  
  71  function user_download_ods($fields) {
  72      global $CFG, $SESSION, $DB;
  73  
  74      require_once("$CFG->libdir/odslib.class.php");
  75      require_once($CFG->dirroot.'/user/profile/lib.php');
  76  
  77      $filename = clean_filename(get_string('users').'.ods');
  78  
  79      $workbook = new MoodleODSWorkbook('-');
  80      $workbook->send($filename);
  81  
  82      $worksheet = array();
  83  
  84      $worksheet[0] = $workbook->add_worksheet('');
  85      $col = 0;
  86      foreach ($fields as $fieldname) {
  87          $worksheet[0]->write(0, $col, $fieldname);
  88          $col++;
  89      }
  90  
  91      $row = 1;
  92      foreach ($SESSION->bulk_users as $userid) {
  93          if (!$user = $DB->get_record('user', array('id'=>$userid))) {
  94              continue;
  95          }
  96          $col = 0;
  97          profile_load_data($user);
  98          foreach ($fields as $field=>$unused) {
  99              $worksheet[0]->write($row, $col, $user->$field);
 100              $col++;
 101          }
 102          $row++;
 103      }
 104  
 105      $workbook->close();
 106      die;
 107  }
 108  
 109  function user_download_xls($fields) {
 110      global $CFG, $SESSION, $DB;
 111  
 112      require_once("$CFG->libdir/excellib.class.php");
 113      require_once($CFG->dirroot.'/user/profile/lib.php');
 114  
 115      $filename = clean_filename(get_string('users').'.xls');
 116  
 117      $workbook = new MoodleExcelWorkbook('-');
 118      $workbook->send($filename);
 119  
 120      $worksheet = array();
 121  
 122      $worksheet[0] = $workbook->add_worksheet('');
 123      $col = 0;
 124      foreach ($fields as $fieldname) {
 125          $worksheet[0]->write(0, $col, $fieldname);
 126          $col++;
 127      }
 128  
 129      $row = 1;
 130      foreach ($SESSION->bulk_users as $userid) {
 131          if (!$user = $DB->get_record('user', array('id'=>$userid))) {
 132              continue;
 133          }
 134          $col = 0;
 135          profile_load_data($user);
 136          foreach ($fields as $field=>$unused) {
 137              $worksheet[0]->write($row, $col, $user->$field);
 138              $col++;
 139          }
 140          $row++;
 141      }
 142  
 143      $workbook->close();
 144      die;
 145  }
 146  
 147  function user_download_csv($fields) {
 148      global $CFG, $SESSION, $DB;
 149  
 150      require_once($CFG->dirroot.'/user/profile/lib.php');
 151      require_once($CFG->libdir . '/csvlib.class.php');
 152  
 153      $filename = clean_filename(get_string('users'));
 154  
 155      $csvexport = new csv_export_writer();
 156      $csvexport->set_filename($filename);
 157      $csvexport->add_data($fields);
 158  
 159      foreach ($SESSION->bulk_users as $userid) {
 160          $row = array();
 161          if (!$user = $DB->get_record('user', array('id'=>$userid))) {
 162              continue;
 163          }
 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;
 174              }
 175          }
 176          $csvexport->add_data($userprofiledata);
 177      }
 178      $csvexport->download_file();
 179      die;
 180  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1