PageRenderTime 46ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/choice/report.php

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