/lib/classes/dataformat/base.php

https://gitlab.com/unofficial-mirrors/moodle · PHP · 137 lines · 40 code · 15 blank · 82 comment · 3 complexity · 5b4666a153504cb3d92991154837347e 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. /**
  17. * Base class for dataformat.
  18. *
  19. * @package core
  20. * @subpackage dataformat
  21. * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net)
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. namespace core\dataformat;
  25. /**
  26. * Base class for dataformat.
  27. *
  28. * @package core
  29. * @subpackage dataformat
  30. * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net)
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. abstract class base {
  34. /** @var $mimetype */
  35. protected $mimetype = "text/plain";
  36. /** @var $extension */
  37. protected $extension = ".txt";
  38. /** @var $filename */
  39. protected $filename = '';
  40. /**
  41. * Get the file extension
  42. *
  43. * @return string file extension
  44. */
  45. public function get_extension() {
  46. return $this->extension;
  47. }
  48. /**
  49. * Set download filename base
  50. *
  51. * @param string $filename
  52. */
  53. public function set_filename($filename) {
  54. $this->filename = $filename;
  55. }
  56. /**
  57. * Set the title of the worksheet inside a spreadsheet
  58. *
  59. * For some formats this will be ignored.
  60. *
  61. * @param string $title
  62. */
  63. public function set_sheettitle($title) {
  64. }
  65. /**
  66. * Output file headers to initialise the download of the file.
  67. */
  68. public function send_http_headers() {
  69. if (defined('BEHAT_SITE_RUNNING')) {
  70. // For text based formats - we cannot test the output with behat if we force a file download.
  71. return;
  72. }
  73. if (is_https()) {
  74. // HTTPS sites - watch out for IE! KB812935 and KB316431.
  75. header('Cache-Control: max-age=10');
  76. header('Pragma: ');
  77. } else {
  78. // Normal http - prevent caching at all cost.
  79. header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
  80. header('Pragma: no-cache');
  81. }
  82. header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
  83. header("Content-Type: $this->mimetype\n");
  84. $filename = $this->filename . $this->get_extension();
  85. header("Content-Disposition: attachment; filename=\"$filename\"");
  86. }
  87. /**
  88. * Write the start of the file.
  89. */
  90. public function start_output() {
  91. // Override me if needed.
  92. }
  93. /**
  94. * Write the start of the sheet we will be adding data to.
  95. *
  96. * @param array $columns
  97. */
  98. public function start_sheet($columns) {
  99. // Override me if needed.
  100. }
  101. /**
  102. * Write a single record
  103. *
  104. * @param array $record
  105. * @param int $rownum
  106. */
  107. abstract public function write_record($record, $rownum);
  108. /**
  109. * Write the end of the sheet containing the data.
  110. *
  111. * @param array $columns
  112. */
  113. public function close_sheet($columns) {
  114. // Override me if needed.
  115. }
  116. /**
  117. * Write the end of the file.
  118. */
  119. public function close_output() {
  120. // Override me if needed.
  121. }
  122. }