Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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                      'country'   => 'country');
  52  
  53      $extrafields = profile_get_user_fields_with_data(0);
  54      foreach ($extrafields as $formfield) {
  55          $fields['profile_field_'.$formfield->get_shortname()] = 'profile_field_'.$formfield->get_shortname();
  56      }
  57  
  58      $filename = clean_filename(get_string('users'));
  59  
  60      $downloadusers = new ArrayObject($SESSION->bulk_users);
  61      $iterator = $downloadusers->getIterator();
  62  
  63      \core\dataformat::download_data($filename, $dataformat, $fields, $iterator, function($userid, $supportshtml)
  64              use ($extrafields, $fields) {
  65  
  66          global $DB;
  67  
  68          if (!$user = $DB->get_record('user', array('id' => $userid))) {
  69              return null;
  70          }
  71          profile_load_data($user);
  72          $userprofiledata = array();
  73          foreach ($fields as $field => $unused) {
  74              // Custom user profile textarea fields come in an array
  75              // The first element is the text and the second is the format.
  76              // We only take the text.
  77              if (is_array($user->$field)) {
  78                  $userprofiledata[$field] = reset($user->$field);
  79              } else if ($supportshtml) {
  80                  $userprofiledata[$field] = s($user->$field);
  81              } else {
  82                  $userprofiledata[$field] = $user->$field;
  83              }
  84          }
  85          return $userprofiledata;
  86      });
  87  
  88      exit;
  89  }
  90  
  91  echo $OUTPUT->header();
  92  echo $OUTPUT->heading(get_string('download', 'admin'));
  93  echo $OUTPUT->download_dataformat_selector(get_string('userbulkdownload', 'admin'), 'user_bulk_download.php');
  94  echo $OUTPUT->footer();
  95