freemail_moodle_importer.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /*
  3. This is a base class for email processors.
  4. It needs to be extended by a specific moodle importer, stored in the moodle_importers directory.
  5. */
  6. abstract class sloodle_freemail_moodle_importer {
  7. var $_title;
  8. var $_body;
  9. var $_images = array();
  10. var $_userid;
  11. var $_user = null;
  12. // Return true to say that this processor can process the email.
  13. // For example, if we have a rule that blog subjects have to begin with "b:", we'll check for that.
  14. function can_process() {
  15. return false;
  16. }
  17. // Return the user ID
  18. function get_user_id() {
  19. return $this->_userid;
  20. }
  21. function set_user_id($u) {
  22. $this->_userid = $u;
  23. }
  24. // Return the body body
  25. function get_body() {
  26. return $this->_body;
  27. }
  28. function set_body($m) {
  29. $this->_body = $m;
  30. }
  31. function set_title($s) {
  32. $this->_title = $s;
  33. }
  34. function get_title() {
  35. return $this->_title;
  36. }
  37. function add_image($filename, $imgdata) {
  38. $this->_images[$filename] = $imgdata;
  39. }
  40. function get_images() {
  41. return $this->_images;
  42. }
  43. // Fetch, cache and return a Moodle user record, loading if necessary.
  44. // Return null on failure.
  45. function user($reload = false) {
  46. if (!$userid = intval($this->_userid)) {
  47. return null;
  48. }
  49. if ( !$reload && !is_null($this->_user) ) {
  50. return $this->_user;
  51. }
  52. global $DB;
  53. $this->_user = $DB->get_record('user', array('id'=>$userid));
  54. return $this->_user;
  55. }
  56. // Notify the user that their content has been imported.
  57. // In most cases this is probably OK as is.
  58. // but you'll want to override user_notification_title() and user_notification_text()
  59. // ...to customized the content of the email.
  60. // In the SLOODLE case we'll do some exotic stuff like sending an in-world instant message, so this will be overloaded..
  61. public function notify_user() {
  62. if (!$user = $this->user()) {
  63. return false;
  64. }
  65. if (!$subject = $this->user_notification_title()) {
  66. return false;
  67. }
  68. if (!$messagetext = $this->user_notification_text()) {
  69. return false;
  70. }
  71. $supportuser = generate_email_supportuser();
  72. email_to_user($user, $supportuser, $subject, $messagetext);
  73. }
  74. // The title of the notification sent to the user to say their content has been imported.
  75. // You will probably want to overload this.
  76. function user_notification_title() {
  77. return 'Content has been imported';
  78. }
  79. // The title of the notification sent to the user to say their content has been imported.
  80. // You will probably want to overload this to give them specific instructions about where to find their content
  81. // ...and if it's been imported in draft form, where to go to publish it.
  82. function user_notification_text() {
  83. return 'Your content has been imported into Moodle.'."\n";
  84. }
  85. static function available_moodle_importers() {
  86. $importer_dir = dirname(__FILE__).'/moodle_importers';
  87. if (!$dh = opendir($importer_dir)) {
  88. return false;
  89. }
  90. $importers = array();
  91. while (($importer_file = readdir($dh)) !== false) {
  92. //print "looking at $importer_file";
  93. if (preg_match('/^(sloodle_freemail_\w+_moodle_importer).php$/', $importer_file, $matches)) {
  94. $clsname = $matches[1];
  95. require_once($importer_dir.'/'.$importer_file);
  96. if (!class_exists($clsname)) {
  97. continue;
  98. }
  99. if (!$clsname::is_available()) {
  100. continue;
  101. }
  102. $importers[] = new $clsname;
  103. }
  104. }
  105. return $importers;
  106. }
  107. static function config_options() {
  108. }
  109. }