diff --git admin/user/user_bulk_download.php admin/user/user_bulk_download.php index 9d1a61a..6732914 100644 --- admin/user/user_bulk_download.php +++ admin/user/user_bulk_download.php @@ -92,25 +92,26 @@ function user_download_ods($fields) { } $row = 1; - foreach ($SESSION->bulk_users as $userid) { - if (!$user = $DB->get_record('user', array('id'=>$userid))) { - continue; - } - $col = 0; - profile_load_data($user); - foreach ($fields as $field=>$unused) { - $worksheet[0]->write($row, $col, $user->$field); - $col++; - } - $row++; - } + sort($SESSION->bulk_users); + $users_chunked = array_chunk($SESSION->bulk_users, 1000); + foreach ($users_chunked as $user_ids) { + $users = user_download_prepare($user_ids); + foreach ($users as $user) { + $col = 0; + foreach ($fields as $field=>$unused) { + $worksheet[0]->write($row, $col, $user->{$field}); + $col++; + } + $row++; + } + } $workbook->close(); die; } function user_download_xls($fields) { - global $CFG, $SESSION, $DB; + global $CFG, $SESSION; require_once("$CFG->libdir/excellib.class.php"); require_once($CFG->dirroot.'/user/profile/lib.php'); @@ -129,19 +130,27 @@ function user_download_xls($fields) { $col++; } - $row = 1; - foreach ($SESSION->bulk_users as $userid) { - if (!$user = $DB->get_record('user', array('id'=>$userid))) { - continue; - } - $col = 0; - profile_load_data($user); - foreach ($fields as $field=>$unused) { - $worksheet[0]->write($row, $col, $user->$field); - $col++; - } - $row++; - } + $row = 1; + sort($SESSION->bulk_users); + $users_chunked = array_chunk($SESSION->bulk_users, 1000); + foreach ($users_chunked as $user_ids) { + + $users = user_download_prepare($user_ids); + if (empty($users)) { + $workbook->close(); + die; + } + + foreach ($users as $user) { + $col = 0; + foreach ($fields as $field=>$unused) { + $value = (isset($user->{$field})) ? $user->{$field} : ""; + $worksheet[0]->write($row, $col, $value); + $col++; + } + $row++; + } + } $workbook->close(); die; @@ -159,25 +168,62 @@ function user_download_csv($fields) { $csvexport->set_filename($filename); $csvexport->add_data($fields); - foreach ($SESSION->bulk_users as $userid) { - $row = array(); - if (!$user = $DB->get_record('user', array('id'=>$userid))) { - continue; - } - profile_load_data($user); - $userprofiledata = array(); - foreach ($fields as $field=>$unused) { - // Custom user profile textarea fields come in an array - // The first element is the text and the second is the format. - // We only take the text. - if (is_array($user->$field)) { - $userprofiledata[] = reset($user->$field); - } else { - $userprofiledata[] = $user->$field; - } - } - $csvexport->add_data($userprofiledata); + sort($SESSION->bulk_users); + $users_chunked = array_chunk($SESSION->bulk_users, 1000); + foreach ($users_chunked as $user_ids) { + $users = user_download_prepare($user_ids); + foreach ($users as $user) { + $userprofiledata = array(); + foreach ($fields as $field=>$unused) { + // Custom user profile textarea fields come in an array + // The first element is the text and the second is the format. + // We only take the text. + if (is_array($user->{$field})) { + $userprofiledata[] = reset($user->{$field}); + } else { + $userprofiledata[] = $user->{$field}; + } + } + $csvexport->add_data($userprofiledata); + } } $csvexport->download_file(); die; } + +function user_download_prepare($user_ids) { + global $DB; + + if (empty($user_ids)) { + return false; + } + + // Fetch basic user data first. + list ($in_or_equal, $params) = $DB->get_in_or_equal($user_ids, SQL_PARAMS_NAMED, 'id'); + $sql = "SELECT * FROM {user} WHERE id " . $in_or_equal . " ORDER BY id"; + $users = $DB->get_records_sql($sql, $params); + if (empty($users)) { + return false; + } + + // Then fetch the custom profile fields, if any. + $pseudo_id = $DB->sql_concat('uid.userid', "'-'", 'uif.shortname'); + list ($in_or_equal, $params) = $DB->get_in_or_equal($user_ids, SQL_PARAMS_NAMED, 'userid'); + $sql = "SELECT + ". $pseudo_id ." AS id, + uid.userid, + uif.shortname, + uid.data + FROM {user_info_data} AS uid + LEFT JOIN {user_info_field} AS uif ON uif.id = uid.fieldid + WHERE uid.userid " . $in_or_equal ." + ORDER BY uid.userid"; + $users_data_raw = $DB->get_records_sql($sql, $params); + // Append this data to $users. + foreach ($users_data_raw as $udr) { + $users[$udr->userid]->{'profile_field_'.$udr->shortname} = $udr->data; + } + unset($users_data_raw); // saves some memory + + return $users; +}