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] [Versions 400 and 401]

   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          throw new \moodle_exception("invalidcoursemodule");
  23      }
  24  
  25      if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
  26          throw new \moodle_exception("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          throw new \moodle_exception('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      $groupmode = groups_get_activity_groupmode($cm);
  67  
  68      if (!$download) {
  69          $PAGE->set_title(format_string($choice->name).": $strresponses");
  70          $PAGE->set_heading($course->fullname);
  71          $PAGE->activityheader->set_attrs([
  72              'hidecompletion' => true,
  73              'description' => ''
  74          ]);
  75          echo $OUTPUT->header();
  76          echo $OUTPUT->heading($strresponses);
  77  
  78          /// Check to see if groups are being used in this choice
  79          if ($groupmode) {
  80              groups_get_activity_group($cm, true);
  81              $groupsactivitymenu = groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/report.php?id=' . $id,
  82                  true);
  83              echo html_writer::div($groupsactivitymenu, 'mb-2');
  84          }
  85      } else {
  86          // Trigger the report downloaded event.
  87          $eventdata = array();
  88          $eventdata['context'] = $context;
  89          $eventdata['courseid'] = $course->id;
  90          $eventdata['other']['content'] = 'choicereportcontentviewed';
  91          $eventdata['other']['format'] = $download;
  92          $eventdata['other']['choiceid'] = $choice->id;
  93          $event = \mod_choice\event\report_downloaded::create($eventdata);
  94          $event->trigger();
  95  
  96      }
  97  
  98      // Check if we want to include responses from inactive users.
  99      $onlyactive = $choice->includeinactive ? false : true;
 100  
 101      $users = choice_get_response_data($choice, $cm, $groupmode, $onlyactive);
 102  
 103      // TODO Does not support custom user profile fields (MDL-70456).
 104      $extrafields = \core_user\fields::get_identity_fields($context, false);
 105  
 106      if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
 107          require_once("$CFG->libdir/odslib.class.php");
 108  
 109      /// Calculate file name
 110          $shortname = format_string($course->shortname, true, array('context' => $context));
 111          $choicename = format_string($choice->name, true, array('context' => $context));
 112          $filename = clean_filename("$shortname " . strip_tags($choicename)) . '.ods';
 113      /// Creating a workbook
 114          $workbook = new MoodleODSWorkbook("-");
 115      /// Send HTTP headers
 116          $workbook->send($filename);
 117      /// Creating the first worksheet
 118          $myxls = $workbook->add_worksheet($strresponses);
 119  
 120      /// Print names of all the fields
 121          $i = 0;
 122          $myxls->write_string(0, $i++, get_string("lastname"));
 123          $myxls->write_string(0, $i++, get_string("firstname"));
 124  
 125          // Add headers for extra user fields.
 126          foreach ($extrafields as $field) {
 127              $myxls->write_string(0, $i++, \core_user\fields::get_display_name($field));
 128          }
 129  
 130          $myxls->write_string(0, $i++, get_string("group"));
 131          $myxls->write_string(0, $i++, get_string("choice", "choice"));
 132  
 133          // Generate the data for the body of the spreadsheet.
 134          $row = 1;
 135          if ($users) {
 136              foreach ($users as $option => $userid) {
 137                  if (!($choice->showunanswered  == 0 && $option == 0)) {
 138                      $option_text = choice_get_option_text($choice, $option);
 139                      foreach ($userid as $user) {
 140                          $i = 0;
 141                          $myxls->write_string($row, $i++, $user->lastname);
 142                          $myxls->write_string($row, $i++, $user->firstname);
 143                          foreach ($extrafields as $field) {
 144                              $myxls->write_string($row, $i++, $user->$field);
 145                          }
 146                          $ug2 = '';
 147                          if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
 148                              foreach ($usergrps as $ug) {
 149                                  $ug2 = $ug2 . $ug->name;
 150                              }
 151                          }
 152                          $myxls->write_string($row, $i++, $ug2);
 153  
 154                          if (isset($option_text)) {
 155                              $myxls->write_string($row, $i++, format_string($option_text, true));
 156                          }
 157                          $row++;
 158                      }
 159                  }
 160              }
 161          }
 162          /// Close the workbook
 163          $workbook->close();
 164  
 165          exit;
 166      }
 167  
 168      //print spreadsheet if one is asked for:
 169      if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
 170          require_once("$CFG->libdir/excellib.class.php");
 171  
 172      /// Calculate file name
 173          $shortname = format_string($course->shortname, true, array('context' => $context));
 174          $choicename = format_string($choice->name, true, array('context' => $context));
 175          $filename = clean_filename("$shortname " . strip_tags($choicename)) . '.xls';
 176      /// Creating a workbook
 177          $workbook = new MoodleExcelWorkbook("-");
 178      /// Send HTTP headers
 179          $workbook->send($filename);
 180      /// Creating the first worksheet
 181          $myxls = $workbook->add_worksheet($strresponses);
 182  
 183      /// Print names of all the fields
 184          $i = 0;
 185          $myxls->write_string(0, $i++, get_string("lastname"));
 186          $myxls->write_string(0, $i++, get_string("firstname"));
 187  
 188          // Add headers for extra user fields.
 189          foreach ($extrafields as $field) {
 190              $myxls->write_string(0, $i++, \core_user\fields::get_display_name($field));
 191          }
 192  
 193          $myxls->write_string(0, $i++, get_string("group"));
 194          $myxls->write_string(0, $i++, get_string("choice", "choice"));
 195  
 196          // Generate the data for the body of the spreadsheet.
 197          $row = 1;
 198          if ($users) {
 199              foreach ($users as $option => $userid) {
 200                  $i = 0;
 201                  if (!($choice->showunanswered  == 0 && $option == 0)) {
 202                      $option_text = choice_get_option_text($choice, $option);
 203                      foreach($userid as $user) {
 204                          $i = 0;
 205                          $myxls->write_string($row, $i++, $user->lastname);
 206                          $myxls->write_string($row, $i++, $user->firstname);
 207                          foreach ($extrafields as $field) {
 208                              $myxls->write_string($row, $i++, $user->$field);
 209                          }
 210                          $ug2 = '';
 211                          if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
 212                              foreach ($usergrps as $ug) {
 213                                  $ug2 = $ug2 . $ug->name;
 214                              }
 215                          }
 216                          $myxls->write_string($row, $i++, $ug2);
 217                          if (isset($option_text)) {
 218                              $myxls->write_string($row, $i++, format_string($option_text, true));
 219                          }
 220                          $row++;
 221                      }
 222                  }
 223              }
 224          }
 225          /// Close the workbook
 226          $workbook->close();
 227          exit;
 228      }
 229  
 230      // print text file
 231      if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
 232          $shortname = format_string($course->shortname, true, array('context' => $context));
 233          $choicename = format_string($choice->name, true, array('context' => $context));
 234          $filename = clean_filename("$shortname " . strip_tags($choicename)) . '.txt';
 235  
 236          header("Content-Type: application/download\n");
 237          header("Content-Disposition: attachment; filename=\"$filename\"");
 238          header("Expires: 0");
 239          header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
 240          header("Pragma: public");
 241  
 242          /// Print names of all the fields
 243  
 244          echo get_string("lastname") . "\t" . get_string("firstname") . "\t";
 245  
 246          // Add headers for extra user fields.
 247          foreach ($extrafields as $field) {
 248              echo \core_user\fields::get_display_name($field) . "\t";
 249          }
 250  
 251          echo get_string("group"). "\t";
 252          echo get_string("choice","choice"). "\n";
 253  
 254          /// generate the data for the body of the spreadsheet
 255          $i=0;
 256          if ($users) {
 257              foreach ($users as $option => $userid) {
 258                  if (!($choice->showunanswered  == 0 && $option == 0)) {
 259                      $option_text = choice_get_option_text($choice, $option);
 260                      foreach($userid as $user) {
 261                          echo $user->lastname . "\t";
 262                          echo $user->firstname . "\t";
 263                          foreach ($extrafields as $field) {
 264                              echo $user->$field . "\t";
 265                          }
 266                          $ug2 = '';
 267                          if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
 268                              foreach ($usergrps as $ug) {
 269                                  $ug2 = $ug2. $ug->name;
 270                              }
 271                          }
 272                          echo $ug2. "\t";
 273                          if (isset($option_text)) {
 274                              echo format_string($option_text,true);
 275                          }
 276                          echo "\n";
 277                      }
 278                  }
 279              }
 280          }
 281          exit;
 282      }
 283      $results = prepare_choice_show_results($choice, $course, $cm, $users);
 284      $renderer = $PAGE->get_renderer('mod_choice');
 285      echo $renderer->display_result($results, true);
 286  
 287     //now give links for downloading spreadsheets.
 288      if (!empty($users) && has_capability('mod/choice:downloadresponses',$context)) {
 289          $downloadoptions = array();
 290          $options = array();
 291          $options["id"] = "$cm->id";
 292          $options["download"] = "ods";
 293          $button =  $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadods"));
 294          $downloadoptions[] = html_writer::tag('li', $button, array('class' => 'reportoption list-inline-item'));
 295  
 296          $options["download"] = "xls";
 297          $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadexcel"));
 298          $downloadoptions[] = html_writer::tag('li', $button, array('class' => 'reportoption list-inline-item'));
 299  
 300          $options["download"] = "txt";
 301          $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadtext"));
 302          $downloadoptions[] = html_writer::tag('li', $button, array('class' => 'reportoption list-inline-item'));
 303  
 304          $downloadlist = html_writer::tag('ul', implode('', $downloadoptions), array('class' => 'list-inline inline'));
 305          $downloadlist .= html_writer::tag('div', '', array('class' => 'clearfloat'));
 306          echo html_writer::tag('div',$downloadlist, array('class' => 'downloadreport mt-1'));
 307      }
 308      echo $OUTPUT->footer();
 309