PageRenderTime 29ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/grade/export/txt/grade_export_txt.php

https://github.com/kpike/moodle
PHP | 120 lines | 77 code | 25 blank | 18 comment | 7 complexity | b30277fc7d2696899ee7ed7837d3288b MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. require_once($CFG->dirroot.'/grade/export/lib.php');
  17. class grade_export_txt extends grade_export {
  18. public $plugin = 'txt';
  19. public $separator; // default separator
  20. public function grade_export_txt($course, $groupid=0, $itemlist='', $export_feedback=false, $updatedgradesonly = false, $displaytype = GRADE_DISPLAY_TYPE_REAL, $decimalpoints = 2, $separator='comma') {
  21. $this->grade_export($course, $groupid, $itemlist, $export_feedback, $updatedgradesonly, $displaytype, $decimalpoints);
  22. $this->separator = $separator;
  23. }
  24. public function __construct($course, $groupid=0, $itemlist='', $export_feedback=false, $updatedgradesonly = false, $displaytype = GRADE_DISPLAY_TYPE_REAL, $decimalpoints = 2, $separator='comma') {
  25. parent::__construct($course, $groupid, $itemlist, $export_feedback, $updatedgradesonly, $displaytype, $decimalpoints);
  26. $this->separator = $separator;
  27. }
  28. public function get_export_params() {
  29. $params = parent::get_export_params();
  30. $params['separator'] = $this->separator;
  31. return $params;
  32. }
  33. public function print_grades() {
  34. global $CFG;
  35. $export_tracking = $this->track_exports();
  36. $strgrades = get_string('grades');
  37. switch ($this->separator) {
  38. case 'comma':
  39. $separator = ",";
  40. break;
  41. case 'tab':
  42. default:
  43. $separator = "\t";
  44. }
  45. /// Print header to force download
  46. if (strpos($CFG->wwwroot, 'https://') === 0) { //https sites - watch out for IE! KB812935 and KB316431
  47. @header('Cache-Control: max-age=10');
  48. @header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
  49. @header('Pragma: ');
  50. } else { //normal http - prevent caching at all cost
  51. @header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
  52. @header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
  53. @header('Pragma: no-cache');
  54. }
  55. header("Content-Type: application/download\n");
  56. $downloadfilename = clean_filename("{$this->course->shortname} $strgrades");
  57. header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\"");
  58. /// Print names of all the fields
  59. echo get_string("firstname").$separator.
  60. get_string("lastname").$separator.
  61. get_string("idnumber").$separator.
  62. get_string("institution").$separator.
  63. get_string("department").$separator.
  64. get_string("email");
  65. foreach ($this->columns as $grade_item) {
  66. echo $separator.$this->format_column_name($grade_item);
  67. /// add a feedback column
  68. if ($this->export_feedback) {
  69. echo $separator.$this->format_column_name($grade_item, true);
  70. }
  71. }
  72. echo "\n";
  73. /// Print all the lines of data.
  74. $geub = new grade_export_update_buffer();
  75. $gui = new graded_users_iterator($this->course, $this->columns, $this->groupid);
  76. $gui->init();
  77. while ($userdata = $gui->next_user()) {
  78. $user = $userdata->user;
  79. echo $user->firstname.$separator.$user->lastname.$separator.$user->idnumber.$separator.$user->institution.$separator.$user->department.$separator.$user->email;
  80. foreach ($userdata->grades as $itemid => $grade) {
  81. if ($export_tracking) {
  82. $status = $geub->track($grade);
  83. }
  84. echo $separator.$this->format_grade($grade);
  85. if ($this->export_feedback) {
  86. echo $separator.$this->format_feedback($userdata->feedbacks[$itemid]);
  87. }
  88. }
  89. echo "\n";
  90. }
  91. $gui->close();
  92. $geub->close();
  93. exit;
  94. }
  95. }