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  
   3      require_once("../../config.php");
   4      require_once ("lib.php");
   5  
   6      $id         = required_param('id', PARAM_INT);   //moduleid
   7      $download   = optional_param('download', '', PARAM_ALPHA);
   8      $action     = optional_param('action', '', PARAM_ALPHANUMEXT);
   9      $attemptids = optional_param_array('attemptid', array(), PARAM_INT); // Get array of responses to delete or modify.
  10      $userids    = optional_param_array('userid', array(), PARAM_INT); // Get array of users whose choices need to be modified.
  11  
  12      $url = new moodle_url('/mod/choice/report.php', array('id'=>$id));
  13      if ($download !== '') {
  14          $url->param('download', $download);
  15      }
  16      if ($action !== '') {
  17          $url->param('action', $action);
  18      }
  19      $PAGE->set_url($url);
  20  
  21      if (! $cm = get_coursemodule_from_id('choice', $id)) {
  22          print_error("invalidcoursemodule");
  23      }
  24  
  25      if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
  26          print_error("coursemisconf");
  27      }
  28  
  29      require_login($course, false, $cm);
  30  
  31      $context = context_module::instance($cm->id);
  32  
  33      require_capability('mod/choice:readresponses', $context);
  34  
  35      if (!$choice = choice_get_choice($cm->instance)) {
  36          print_error('invalidcoursemodule');
  37      }
  38  
  39      $strchoice = get_string("modulename", "choice");
  40      $strchoices = get_string("modulenameplural", "choice");
  41      $strresponses = get_string("responses", "choice");
  42  
  43      $eventdata = array();
  44      $eventdata['objectid'] = $choice->id;
  45      $eventdata['context'] = $context;
  46      $eventdata['courseid'] = $course->id;
  47      $eventdata['other']['content'] = 'choicereportcontentviewed';
  48  
  49      $event = \mod_choice\event\report_viewed::create($eventdata);
  50      $event->trigger();
  51  
  52      if (data_submitted() && has_capability('mod/choice:deleteresponses', $context) && confirm_sesskey()) {
  53          if ($action === 'delete') {
  54              // Delete responses of other users.
  55              choice_delete_responses($attemptids, $choice, $cm, $course);
  56              redirect("report.php?id=$cm->id");
  57          }
  58          if (preg_match('/^choose_(\d+)$/', $action, $actionmatch)) {
  59              // Modify responses of other users.
  60              $newoptionid = (int)$actionmatch[1];
  61              choice_modify_responses($userids, $attemptids, $newoptionid, $choice, $cm, $course);
  62              redirect("report.php?id=$cm->id");
  63          }
  64      }
  65  
  66      if (!$download) {
  67          $PAGE->navbar->add($strresponses);
  68          $PAGE->set_title(format_string($choice->name).": $strresponses");
  69          $PAGE->set_heading($course->fullname);
  70          echo $OUTPUT->header();
  71          echo $OUTPUT->heading(format_string($choice->name), 2, null);
  72          /// Check to see if groups are being used in this choice
  73          $groupmode = groups_get_activity_groupmode($cm);
  74          if ($groupmode) {
  75              groups_get_activity_group($cm, true);
  76              groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/report.php?id='.$id);
  77          }
  78      } else {
  79          $groupmode = groups_get_activity_groupmode($cm);
  80  
  81          // Trigger the report downloaded event.
  82          $eventdata = array();
  83          $eventdata['context'] = $context;
  84          $eventdata['courseid'] = $course->id;
  85          $eventdata['other']['content'] = 'choicereportcontentviewed';
  86          $eventdata['other']['format'] = $download;
  87          $eventdata['other']['choiceid'] = $choice->id;
  88          $event = \mod_choice\event\report_downloaded::create($eventdata);
  89          $event->trigger();
  90  
  91      }
  92  
  93      // Check if we want to include responses from inactive users.
  94      $onlyactive = $choice->includeinactive ? false : true;
  95  
  96      $users = choice_get_response_data($choice, $cm, $groupmode, $onlyactive);
  97  
  98      // TODO Does not support custom user profile fields (MDL-70456).
  99      $extrafields = \core_user\fields::get_identity_fields($context, false);
 100  
 101      if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
 102          require_once("$CFG->libdir/odslib.class.php");
 103  
 104      /// Calculate file name
 105          $shortname = format_string($course->shortname, true, array('context' => $context));
 106          $choicename = format_string($choice->name, true, array('context' => $context));
 107          $filename = clean_filename("$shortname " . strip_tags($choicename)) . '.ods';
 108      /// Creating a workbook
 109          $workbook = new MoodleODSWorkbook("-");
 110      /// Send HTTP headers
 111          $workbook->send($filename);
 112      /// Creating the first worksheet
 113          $myxls = $workbook->add_worksheet($strresponses);
 114  
 115      /// Print names of all the fields
 116          $i = 0;
 117          $myxls->write_string(0, $i++, get_string("lastname"));
 118          $myxls->write_string(0, $i++, get_string("firstname"));
 119  
 120          // Add headers for extra user fields.
 121          foreach ($extrafields as $field) {
 122              $myxls->write_string(0, $i++, \core_user\fields::get_display_name($field));
 123          }
 124  
 125          $myxls->write_string(0, $i++, get_string("group"));
 126          $myxls->write_string(0, $i++, get_string("choice", "choice"));
 127  
 128          // Generate the data for the body of the spreadsheet.
 129          $row = 1;
 130          if ($users) {
 131              foreach ($users as $option => $userid) {
 132                  $option_text = choice_get_option_text($choice, $option);
 133                  foreach ($userid as $user) {
 134                      $i = 0;
 135                      $myxls->write_string($row, $i++, $user->lastname);
 136                      $myxls->write_string($row, $i++, $user->firstname);
 137                      foreach ($extrafields as $field) {
 138                          $myxls->write_string($row, $i++, $user->$field);
 139                      }
 140                      $ug2 = '';
 141                      if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
 142                          foreach ($usergrps as $ug) {
 143                              $ug2 = $ug2 . $ug->name;
 144                          }
 145                      }
 146                      $myxls->write_string($row, $i++, $ug2);
 147  
 148                      if (isset($option_text)) {
 149                          $myxls->write_string($row, $i++, format_string($option_text, true));
 150                      }
 151                      $row++;
 152                  }
 153              }
 154          }
 155          /// Close the workbook
 156          $workbook->close();
 157  
 158          exit;
 159      }
 160  
 161      //print spreadsheet if one is asked for:
 162      if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
 163          require_once("$CFG->libdir/excellib.class.php");
 164  
 165      /// Calculate file name
 166          $shortname = format_string($course->shortname, true, array('context' => $context));
 167          $choicename = format_string($choice->name, true, array('context' => $context));
 168          $filename = clean_filename("$shortname " . strip_tags($choicename)) . '.xls';
 169      /// Creating a workbook
 170          $workbook = new MoodleExcelWorkbook("-");
 171      /// Send HTTP headers
 172          $workbook->send($filename);
 173      /// Creating the first worksheet
 174          $myxls = $workbook->add_worksheet($strresponses);
 175  
 176      /// Print names of all the fields
 177          $i = 0;
 178          $myxls->write_string(0, $i++, get_string("lastname"));
 179          $myxls->write_string(0, $i++, get_string("firstname"));
 180  
 181          // Add headers for extra user fields.
 182          foreach ($extrafields as $field) {
 183              $myxls->write_string(0, $i++, \core_user\fields::get_display_name($field));
 184          }
 185  
 186          $myxls->write_string(0, $i++, get_string("group"));
 187          $myxls->write_string(0, $i++, get_string("choice", "choice"));
 188  
 189          // Generate the data for the body of the spreadsheet.
 190          $row = 1;
 191          if ($users) {
 192              foreach ($users as $option => $userid) {
 193                  $i = 0;
 194                  $option_text = choice_get_option_text($choice, $option);
 195                  foreach($userid as $user) {
 196                      $i = 0;
 197                      $myxls->write_string($row, $i++, $user->lastname);
 198                      $myxls->write_string($row, $i++, $user->firstname);
 199                      foreach ($extrafields as $field) {
 200                          $myxls->write_string($row, $i++, $user->$field);
 201                      }
 202                      $ug2 = '';
 203                      if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
 204                          foreach ($usergrps as $ug) {
 205                              $ug2 = $ug2 . $ug->name;
 206                          }
 207                      }
 208                      $myxls->write_string($row, $i++, $ug2);
 209                      if (isset($option_text)) {
 210                          $myxls->write_string($row, $i++, format_string($option_text, true));
 211                      }
 212                      $row++;
 213                  }
 214              }
 215          }
 216          /// Close the workbook
 217          $workbook->close();
 218          exit;
 219      }
 220  
 221      // print text file
 222      if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
 223          $shortname = format_string($course->shortname, true, array('context' => $context));
 224          $choicename = format_string($choice->name, true, array('context' => $context));
 225          $filename = clean_filename("$shortname " . strip_tags($choicename)) . '.txt';
 226  
 227          header("Content-Type: application/download\n");
 228          header("Content-Disposition: attachment; filename=\"$filename\"");
 229          header("Expires: 0");
 230          header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
 231          header("Pragma: public");
 232  
 233          /// Print names of all the fields
 234  
 235          echo get_string("lastname") . "\t" . get_string("firstname") . "\t";
 236  
 237          // Add headers for extra user fields.
 238          foreach ($extrafields as $field) {
 239              echo \core_user\fields::get_display_name($field) . "\t";
 240          }
 241  
 242          echo get_string("group"). "\t";
 243          echo get_string("choice","choice"). "\n";
 244  
 245          /// generate the data for the body of the spreadsheet
 246          $i=0;
 247          if ($users) {
 248              foreach ($users as $option => $userid) {
 249                  $option_text = choice_get_option_text($choice, $option);
 250                  foreach($userid as $user) {
 251                      echo $user->lastname . "\t";
 252                      echo $user->firstname . "\t";
 253                      foreach ($extrafields as $field) {
 254                          echo $user->$field . "\t";
 255                      }
 256                      $ug2 = '';
 257                      if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
 258                          foreach ($usergrps as $ug) {
 259                              $ug2 = $ug2. $ug->name;
 260                          }
 261                      }
 262                      echo $ug2. "\t";
 263                      if (isset($option_text)) {
 264                          echo format_string($option_text,true);
 265                      }
 266                      echo "\n";
 267                  }
 268              }
 269          }
 270          exit;
 271      }
 272      $results = prepare_choice_show_results($choice, $course, $cm, $users);
 273      $renderer = $PAGE->get_renderer('mod_choice');
 274      echo $renderer->display_result($results, true);
 275  
 276     //now give links for downloading spreadsheets.
 277      if (!empty($users) && has_capability('mod/choice:downloadresponses',$context)) {
 278          $downloadoptions = array();
 279          $options = array();
 280          $options["id"] = "$cm->id";
 281          $options["download"] = "ods";
 282          $button =  $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadods"));
 283          $downloadoptions[] = html_writer::tag('li', $button, array('class' => 'reportoption list-inline-item'));
 284  
 285          $options["download"] = "xls";
 286          $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadexcel"));
 287          $downloadoptions[] = html_writer::tag('li', $button, array('class' => 'reportoption list-inline-item'));
 288  
 289          $options["download"] = "txt";
 290          $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadtext"));
 291          $downloadoptions[] = html_writer::tag('li', $button, array('class' => 'reportoption list-inline-item'));
 292  
 293          $downloadlist = html_writer::tag('ul', implode('', $downloadoptions), array('class' => 'list-inline inline'));
 294          $downloadlist .= html_writer::tag('div', '', array('class' => 'clearfloat'));
 295          echo html_writer::tag('div',$downloadlist, array('class' => 'downloadreport mt-1'));
 296      }
 297      echo $OUTPUT->footer();
 298