From dca8394630432e85bfdfde725579d970ce3e084d Mon Sep 17 00:00:00 2001 From: Andrew Hancox Date: Tue, 7 Mar 2017 09:59:55 +0000 Subject: [PATCH] MDL-58172 mod_choice: Make export choice respect id fields --- mod/choice/report.php | 98 +++++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 40 deletions(-) diff --git a/mod/choice/report.php b/mod/choice/report.php index ef144972680..8c0293bfe10 100644 --- a/mod/choice/report.php +++ b/mod/choice/report.php @@ -95,6 +95,8 @@ $users = choice_get_response_data($choice, $cm, $groupmode, $onlyactive); + $extrafields = get_extra_user_fields($context); + if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) { require_once("$CFG->libdir/odslib.class.php"); @@ -108,36 +110,42 @@ $myxls = $workbook->add_worksheet($strresponses); /// Print names of all the fields - $myxls->write_string(0,0,get_string("lastname")); - $myxls->write_string(0,1,get_string("firstname")); - $myxls->write_string(0,2,get_string("idnumber")); - $myxls->write_string(0,3,get_string("group")); - $myxls->write_string(0,4,get_string("choice","choice")); + $i = 0; + $myxls->write_string(0, $i++, get_string("lastname")); + $myxls->write_string(0, $i++, get_string("firstname")); - /// generate the data for the body of the spreadsheet - $i=0; - $row=1; + // Add headers for extra user fields. + foreach ($extrafields as $field) { + $myxls->write_string(0, $i++, get_user_field_name($field)); + } + + $myxls->write_string(0, $i++, get_string("group")); + $myxls->write_string(0, $i++, get_string("choice", "choice")); + + // Generate the data for the body of the spreadsheet. + $row = 1; if ($users) { foreach ($users as $option => $userid) { $option_text = choice_get_option_text($choice, $option); - foreach($userid as $user) { - $myxls->write_string($row,0,$user->lastname); - $myxls->write_string($row,1,$user->firstname); - $studentid=(!empty($user->idnumber) ? $user->idnumber : " "); - $myxls->write_string($row,2,$studentid); + foreach ($userid as $user) { + $i = 0; + $myxls->write_string($row, $i++, $user->lastname); + $myxls->write_string($row, $i++, $user->firstname); + foreach ($extrafields as $field) { + $myxls->write_string($row, $i++, $user->$field); + } $ug2 = ''; if ($usergrps = groups_get_all_groups($course->id, $user->id)) { foreach ($usergrps as $ug) { - $ug2 = $ug2. $ug->name; + $ug2 = $ug2 . $ug->name; } } - $myxls->write_string($row,3,$ug2); + $myxls->write_string($row, $i++, $ug2); if (isset($option_text)) { - $myxls->write_string($row,4,format_string($option_text,true)); + $myxls->write_string($row, $i++, format_string($option_text, true)); } $row++; - $pos=4; } } } @@ -161,38 +169,44 @@ $myxls = $workbook->add_worksheet($strresponses); /// Print names of all the fields - $myxls->write_string(0,0,get_string("lastname")); - $myxls->write_string(0,1,get_string("firstname")); - $myxls->write_string(0,2,get_string("idnumber")); - $myxls->write_string(0,3,get_string("group")); - $myxls->write_string(0,4,get_string("choice","choice")); + $i = 0; + $myxls->write_string(0, $i++, get_string("lastname")); + $myxls->write_string(0, $i++, get_string("firstname")); + // Add headers for extra user fields. + foreach ($extrafields as $field) { + $myxls->write_string(0, $i++, get_user_field_name($field)); + } - /// generate the data for the body of the spreadsheet - $i=0; - $row=1; + $myxls->write_string(0, $i++, get_string("group")); + $myxls->write_string(0, $i++, get_string("choice", "choice")); + + // Generate the data for the body of the spreadsheet. + $row = 1; if ($users) { foreach ($users as $option => $userid) { + $i = 0; $option_text = choice_get_option_text($choice, $option); foreach($userid as $user) { - $myxls->write_string($row,0,$user->lastname); - $myxls->write_string($row,1,$user->firstname); - $studentid=(!empty($user->idnumber) ? $user->idnumber : " "); - $myxls->write_string($row,2,$studentid); + $i = 0; + $myxls->write_string($row, $i++, $user->lastname); + $myxls->write_string($row, $i++, $user->firstname); + foreach ($extrafields as $field) { + $myxls->write_string($row, $i++, $user->$field); + } $ug2 = ''; if ($usergrps = groups_get_all_groups($course->id, $user->id)) { foreach ($usergrps as $ug) { - $ug2 = $ug2. $ug->name; + $ug2 = $ug2 . $ug->name; } } - $myxls->write_string($row,3,$ug2); + $myxls->write_string($row, $i++, $ug2); if (isset($option_text)) { - $myxls->write_string($row,4,format_string($option_text,true)); + $myxls->write_string($row, $i++, format_string($option_text, true)); } $row++; } } - $pos=4; } /// Close the workbook $workbook->close(); @@ -211,7 +225,13 @@ /// Print names of all the fields - echo get_string("lastname")."\t".get_string("firstname") . "\t". get_string("idnumber") . "\t"; + echo get_string("lastname") . "\t" . get_string("firstname") . "\t"; + + // Add headers for extra user fields. + foreach ($extrafields as $field) { + echo get_user_field_name($field) . "\t"; + } + echo get_string("group"). "\t"; echo get_string("choice","choice"). "\n"; @@ -221,13 +241,11 @@ foreach ($users as $option => $userid) { $option_text = choice_get_option_text($choice, $option); foreach($userid as $user) { - echo $user->lastname; - echo "\t".$user->firstname; - $studentid = " "; - if (!empty($user->idnumber)) { - $studentid = $user->idnumber; + echo $user->lastname . "\t"; + echo $user->firstname . "\t"; + foreach ($extrafields as $field) { + echo $user->$field . "\t"; } - echo "\t". $studentid."\t"; $ug2 = ''; if ($usergrps = groups_get_all_groups($course->id, $user->id)) { foreach ($usergrps as $ug) {