[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/choice/ -> report.php (source)

   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          // Trigger the report downloaded event.
  76          $eventdata = array();
  77          $eventdata['context'] = $context;
  78          $eventdata['courseid'] = $course->id;
  79          $eventdata['other']['content'] = 'choicereportcontentviewed';
  80          $eventdata['other']['format'] = $download;
  81          $eventdata['other']['choiceid'] = $choice->id;
  82          $event = \mod_choice\event\report_downloaded::create($eventdata);
  83          $event->trigger();
  84  
  85      }
  86  
  87      // Check if we want to include responses from inactive users.
  88      $onlyactive = $choice->includeinactive ? false : true;
  89  
  90      $users = choice_get_response_data($choice, $cm, $groupmode, $onlyactive);
  91  
  92      if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
  93          require_once("$CFG->libdir/odslib.class.php");
  94  
  95      /// Calculate file name
  96          $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.ods';
  97      /// Creating a workbook
  98          $workbook = new MoodleODSWorkbook("-");
  99      /// Send HTTP headers
 100          $workbook->send($filename);
 101      /// Creating the first worksheet
 102          $myxls = $workbook->add_worksheet($strresponses);
 103  
 104      /// Print names of all the fields
 105          $myxls->write_string(0,0,get_string("lastname"));
 106          $myxls->write_string(0,1,get_string("firstname"));
 107          $myxls->write_string(0,2,get_string("idnumber"));
 108          $myxls->write_string(0,3,get_string("group"));
 109          $myxls->write_string(0,4,get_string("choice","choice"));
 110  
 111      /// generate the data for the body of the spreadsheet
 112          $i=0;
 113          $row=1;
 114          if ($users) {
 115              foreach ($users as $option => $userid) {
 116                  $option_text = choice_get_option_text($choice, $option);
 117                  foreach($userid as $user) {
 118                      $myxls->write_string($row,0,$user->lastname);
 119                      $myxls->write_string($row,1,$user->firstname);
 120                      $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
 121                      $myxls->write_string($row,2,$studentid);
 122                      $ug2 = '';
 123                      if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
 124                          foreach ($usergrps as $ug) {
 125                              $ug2 = $ug2. $ug->name;
 126                          }
 127                      }
 128                      $myxls->write_string($row,3,$ug2);
 129  
 130                      if (isset($option_text)) {
 131                          $myxls->write_string($row,4,format_string($option_text,true));
 132                      }
 133                      $row++;
 134                      $pos=4;
 135                  }
 136              }
 137          }
 138          /// Close the workbook
 139          $workbook->close();
 140  
 141          exit;
 142      }
 143  
 144      //print spreadsheet if one is asked for:
 145      if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
 146          require_once("$CFG->libdir/excellib.class.php");
 147  
 148      /// Calculate file name
 149          $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls';
 150      /// Creating a workbook
 151          $workbook = new MoodleExcelWorkbook("-");
 152      /// Send HTTP headers
 153          $workbook->send($filename);
 154      /// Creating the first worksheet
 155          $myxls = $workbook->add_worksheet($strresponses);
 156  
 157      /// Print names of all the fields
 158          $myxls->write_string(0,0,get_string("lastname"));
 159          $myxls->write_string(0,1,get_string("firstname"));
 160          $myxls->write_string(0,2,get_string("idnumber"));
 161          $myxls->write_string(0,3,get_string("group"));
 162          $myxls->write_string(0,4,get_string("choice","choice"));
 163  
 164  
 165      /// generate the data for the body of the spreadsheet
 166          $i=0;
 167          $row=1;
 168          if ($users) {
 169              foreach ($users as $option => $userid) {
 170                  $option_text = choice_get_option_text($choice, $option);
 171                  foreach($userid as $user) {
 172                      $myxls->write_string($row,0,$user->lastname);
 173                      $myxls->write_string($row,1,$user->firstname);
 174                      $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
 175                      $myxls->write_string($row,2,$studentid);
 176                      $ug2 = '';
 177                      if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
 178                          foreach ($usergrps as $ug) {
 179                              $ug2 = $ug2. $ug->name;
 180                          }
 181                      }
 182                      $myxls->write_string($row,3,$ug2);
 183                      if (isset($option_text)) {
 184                          $myxls->write_string($row,4,format_string($option_text,true));
 185                      }
 186                      $row++;
 187                  }
 188              }
 189              $pos=4;
 190          }
 191          /// Close the workbook
 192          $workbook->close();
 193          exit;
 194      }
 195  
 196      // print text file
 197      if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
 198          $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt';
 199  
 200          header("Content-Type: application/download\n");
 201          header("Content-Disposition: attachment; filename=\"$filename\"");
 202          header("Expires: 0");
 203          header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
 204          header("Pragma: public");
 205  
 206          /// Print names of all the fields
 207  
 208          echo get_string("lastname")."\t".get_string("firstname") . "\t". get_string("idnumber") . "\t";
 209          echo get_string("group"). "\t";
 210          echo get_string("choice","choice"). "\n";
 211  
 212          /// generate the data for the body of the spreadsheet
 213          $i=0;
 214          if ($users) {
 215              foreach ($users as $option => $userid) {
 216                  $option_text = choice_get_option_text($choice, $option);
 217                  foreach($userid as $user) {
 218                      echo $user->lastname;
 219                      echo "\t".$user->firstname;
 220                      $studentid = " ";
 221                      if (!empty($user->idnumber)) {
 222                          $studentid = $user->idnumber;
 223                      }
 224                      echo "\t". $studentid."\t";
 225                      $ug2 = '';
 226                      if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
 227                          foreach ($usergrps as $ug) {
 228                              $ug2 = $ug2. $ug->name;
 229                          }
 230                      }
 231                      echo $ug2. "\t";
 232                      if (isset($option_text)) {
 233                          echo format_string($option_text,true);
 234                      }
 235                      echo "\n";
 236                  }
 237              }
 238          }
 239          exit;
 240      }
 241      // Show those who haven't answered the question.
 242      if (!empty($choice->showunanswered)) {
 243          $choice->option[0] = get_string('notanswered', 'choice');
 244          $choice->maxanswers[0] = 0;
 245      }
 246  
 247      $results = prepare_choice_show_results($choice, $course, $cm, $users);
 248      $renderer = $PAGE->get_renderer('mod_choice');
 249      echo $renderer->display_result($results, has_capability('mod/choice:readresponses', $context));
 250  
 251     //now give links for downloading spreadsheets.
 252      if (!empty($users) && has_capability('mod/choice:downloadresponses',$context)) {
 253          $downloadoptions = array();
 254          $options = array();
 255          $options["id"] = "$cm->id";
 256          $options["download"] = "ods";
 257          $button =  $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadods"));
 258          $downloadoptions[] = html_writer::tag('li', $button, array('class'=>'reportoption'));
 259  
 260          $options["download"] = "xls";
 261          $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadexcel"));
 262          $downloadoptions[] = html_writer::tag('li', $button, array('class'=>'reportoption'));
 263  
 264          $options["download"] = "txt";
 265          $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadtext"));
 266          $downloadoptions[] = html_writer::tag('li', $button, array('class'=>'reportoption'));
 267  
 268          $downloadlist = html_writer::tag('ul', implode('', $downloadoptions));
 269          $downloadlist .= html_writer::tag('div', '', array('class'=>'clearfloat'));
 270          echo html_writer::tag('div',$downloadlist, array('class'=>'downloadreport'));
 271      }
 272      echo $OUTPUT->footer();
 273  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1