Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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  use core_user\fields;
  26  
  27  define('NO_OUTPUT_BUFFERING', true);
  28  require_once('../../config.php');
  29  require_once($CFG->libdir.'/adminlib.php');
  30  require_once($CFG->dirroot.'/user/profile/lib.php');
  31  
  32  $dataformat = optional_param('dataformat', '', PARAM_ALPHA);
  33  
  34  admin_externalpage_setup('userbulk');
  35  require_capability('moodle/user:update', context_system::instance());
  36  
  37  if (empty($SESSION->bulk_users)) {
  38      redirect(new moodle_url('/admin/user/user_bulk.php'));
  39  }
  40  
  41  if ($dataformat) {
  42      $originfields = array('id'        => 'id',
  43                      'username'  => 'username',
  44                      'email'     => 'email',
  45                      'firstname' => 'firstname',
  46                      'lastname'  => 'lastname',
  47                      'idnumber'  => 'idnumber',
  48                      'institution' => 'institution',
  49                      'department' => 'department',
  50                      'phone1'    => 'phone1',
  51                      'phone2'    => 'phone2',
  52                      'city'      => 'city',
  53                      'country'   => 'country');
  54  
  55      $extrafields = profile_get_user_fields_with_data(0);
  56      $profilefields = [];
  57      foreach ($extrafields as $formfield) {
  58          $profilefields[fields::PROFILE_FIELD_PREFIX . $formfield->get_shortname()] = fields::PROFILE_FIELD_PREFIX .
  59              $formfield->get_shortname();
  60      }
  61  
  62      $filename = clean_filename(get_string('users'));
  63  
  64      $downloadusers = new ArrayObject($SESSION->bulk_users);
  65      $iterator = $downloadusers->getIterator();
  66  
  67      \core\dataformat::download_data($filename, $dataformat, array_merge($originfields, $profilefields), $iterator,
  68              function($userid, $supportshtml) use ($originfields) {
  69  
  70          global $DB;
  71  
  72          if (!$user = $DB->get_record('user', array('id' => $userid))) {
  73              return null;
  74          }
  75  
  76          $userprofiledata = array();
  77          foreach ($originfields as $field) {
  78              // Custom user profile textarea fields come in an array
  79              // The first element is the text and the second is the format.
  80              // We only take the text.
  81              if (is_array($user->$field)) {
  82                  $userprofiledata[$field] = reset($user->$field);
  83              } else if ($supportshtml) {
  84                  $userprofiledata[$field] = s($user->$field);
  85              } else {
  86                  $userprofiledata[$field] = $user->$field;
  87              }
  88          }
  89  
  90  
  91          // Formatting extra field if transform is true.
  92          $extrafields = profile_get_user_fields_with_data($userid);
  93          foreach ($extrafields as $field) {
  94              $fieldkey = fields::PROFILE_FIELD_PREFIX . $field->get_shortname();
  95              if ($field->is_transform_supported()) {
  96                  $userprofiledata[$fieldkey] = $field->display_data();
  97              } else {
  98                  $userprofiledata[$fieldkey] = $field->data;
  99              }
 100          }
 101  
 102          return $userprofiledata;
 103      });
 104  
 105      exit;
 106  }
 107  
 108  $PAGE->set_primary_active_tab('siteadminnode');
 109  $PAGE->set_secondary_active_tab('users');
 110  
 111  echo $OUTPUT->header();
 112  echo $OUTPUT->heading(get_string('download', 'admin'));
 113  echo $OUTPUT->download_dataformat_selector(get_string('userbulkdownload', 'admin'), 'user_bulk_download.php');
 114  echo $OUTPUT->footer();
 115