Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   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.
   8  //
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Bulk export user into any dataformat
  19   *
  20   * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
  21   * @copyright  2007 Petr Skoda
  22   * @package    core
  23   */
  24  
  25  define('NO_OUTPUT_BUFFERING', true);
  26  require_once('../../config.php');
  27  require_once($CFG->libdir.'/adminlib.php');
  28  require_once($CFG->dirroot.'/user/profile/lib.php');
  29  
  30  $dataformat = optional_param('dataformat', '', PARAM_ALPHA);
  31  
  32  admin_externalpage_setup('userbulk');
  33  require_capability('moodle/user:update', context_system::instance());
  34  
  35  if (empty($SESSION->bulk_users)) {
  36      redirect(new moodle_url('/admin/user/user_bulk.php'));
  37  }
  38  
  39  if ($dataformat) {
  40      $fields = array('id'        => 'id',
  41                      'username'  => 'username',
  42                      'email'     => 'email',
  43                      'firstname' => 'firstname',
  44                      'lastname'  => 'lastname',
  45                      'idnumber'  => 'idnumber',
  46                      'institution' => 'institution',
  47                      'department' => 'department',
  48                      'phone1'    => 'phone1',
  49                      'phone2'    => 'phone2',
  50                      'city'      => 'city',
  51                      'url'       => 'url',
  52                      'icq'       => 'icq',
  53                      'skype'     => 'skype',
  54                      'aim'       => 'aim',
  55                      'yahoo'     => 'yahoo',
  56                      'msn'       => 'msn',
  57                      'country'   => 'country');
  58  
  59      if ($extrafields = $DB->get_records('user_info_field')) {
  60          foreach ($extrafields as $n => $field) {
  61              $fields['profile_field_'.$field->shortname] = 'profile_field_'.$field->shortname;
  62              require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
  63          }
  64      }
  65  
  66      $filename = clean_filename(get_string('users'));
  67  
  68      $downloadusers = new ArrayObject($SESSION->bulk_users);
  69      $iterator = $downloadusers->getIterator();
  70  
  71      \core\dataformat::download_data($filename, $dataformat, $fields, $iterator, function($userid, $supportshtml)
  72              use ($extrafields, $fields) {
  73  
  74          global $DB;
  75  
  76          if (!$user = $DB->get_record('user', array('id' => $userid))) {
  77              return null;
  78          }
  79          foreach ($extrafields as $field) {
  80              $newfield = 'profile_field_'.$field->datatype;
  81              $formfield = new $newfield($field->id, $user->id);
  82              $formfield->edit_load_user_data($user);
  83          }
  84          $userprofiledata = array();
  85          foreach ($fields as $field => $unused) {
  86              // Custom user profile textarea fields come in an array
  87              // The first element is the text and the second is the format.
  88              // We only take the text.
  89              if (is_array($user->$field)) {
  90                  $userprofiledata[$field] = reset($user->$field);
  91              } else if ($supportshtml) {
  92                  $userprofiledata[$field] = s($user->$field);
  93              } else {
  94                  $userprofiledata[$field] = $user->$field;
  95              }
  96          }
  97          return $userprofiledata;
  98      });
  99  
 100      exit;
 101  }
 102  
 103  echo $OUTPUT->header();
 104  echo $OUTPUT->heading(get_string('download', 'admin'));
 105  echo $OUTPUT->download_dataformat_selector(get_string('userbulkdownload', 'admin'), 'user_bulk_download.php');
 106  echo $OUTPUT->footer();
 107