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