Search moodle.org's
Developer Documentation


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

Search This Site: