/mod/glossary/rsslib.php

https://github.com/tmuras/moodle-debian · PHP · 158 lines · 109 code · 27 blank · 22 comment · 23 complexity · 196d8d14f0b2adf336b14f4cba9f5906 MD5 · raw file

  1. <?php
  2. //This file adds support to rss feeds generation
  3. //This function is the main entry point to glossary
  4. //rss feeds generation.
  5. function glossary_rss_get_feed($context, $args) {
  6. global $CFG, $DB;
  7. if (empty($CFG->glossary_enablerssfeeds)) {
  8. debugging("DISABLED (module configuration)");
  9. return null;
  10. }
  11. $status = true;
  12. //check capabilities
  13. //glossary module doesn't require any capabilities to view glossary entries (aside from being logged in)
  14. if (!is_enrolled($context) && !isguestuser()) {
  15. return null;
  16. }
  17. $glossaryid = clean_param($args[3], PARAM_INT);
  18. $glossary = $DB->get_record('glossary', array('id' => $glossaryid), '*', MUST_EXIST);
  19. if (!rss_enabled_for_mod('glossary', $glossary)) {
  20. return null;
  21. }
  22. $sql = glossary_rss_get_sql($glossary);
  23. //get the cache file info
  24. $filename = rss_get_file_name($glossary, $sql);
  25. $cachedfilepath = rss_get_file_full_name('mod_glossary', $filename);
  26. //Is the cache out of date?
  27. $cachedfilelastmodified = 0;
  28. if (file_exists($cachedfilepath)) {
  29. $cachedfilelastmodified = filemtime($cachedfilepath);
  30. }
  31. //if the cache is more than 60 seconds old and there's new stuff
  32. $dontrecheckcutoff = time()-60;
  33. if ( $dontrecheckcutoff > $cachedfilelastmodified && glossary_rss_newstuff($glossary, $cachedfilelastmodified)) {
  34. if (!$recs = $DB->get_records_sql($sql, array(), 0, $glossary->rssarticles)) {
  35. return null;
  36. }
  37. $items = array();
  38. $formatoptions = new stdClass();
  39. $formatoptions->trusttext = true;
  40. foreach ($recs as $rec) {
  41. $item = new stdClass();
  42. $user = new stdClass();
  43. $item->title = $rec->entryconcept;
  44. if ($glossary->rsstype == 1) {//With author
  45. $user->firstname = $rec->userfirstname;
  46. $user->lastname = $rec->userlastname;
  47. $item->author = fullname($user);
  48. }
  49. $item->pubdate = $rec->entrytimecreated;
  50. $item->link = $CFG->wwwroot."/mod/glossary/showentry.php?courseid=".$glossary->course."&eid=".$rec->entryid;
  51. $item->description = format_text($rec->entrydefinition,$rec->entryformat,$formatoptions,$glossary->course);
  52. $items[] = $item;
  53. }
  54. //First all rss feeds common headers
  55. $header = rss_standard_header(format_string($glossary->name,true),
  56. $CFG->wwwroot."/mod/glossary/view.php?g=".$glossary->id,
  57. format_string($glossary->intro,true));
  58. //Now all the rss items
  59. if (!empty($header)) {
  60. $articles = rss_add_items($items);
  61. }
  62. //Now all rss feeds common footers
  63. if (!empty($header) && !empty($articles)) {
  64. $footer = rss_standard_footer();
  65. }
  66. //Now, if everything is ok, concatenate it
  67. if (!empty($header) && !empty($articles) && !empty($footer)) {
  68. $rss = $header.$articles.$footer;
  69. //Save the XML contents to file.
  70. $status = rss_save_file('mod_glossary', $filename, $rss);
  71. }
  72. }
  73. if (!$status) {
  74. $cachedfilepath = null;
  75. }
  76. return $cachedfilepath;
  77. }
  78. function glossary_rss_get_sql($glossary, $time=0) {
  79. //do we only want new items?
  80. if ($time) {
  81. $time = "AND e.timecreated > $time";
  82. } else {
  83. $time = "";
  84. }
  85. if ($glossary->rsstype == 1) {//With author
  86. $sql = "SELECT e.id AS entryid,
  87. e.concept AS entryconcept,
  88. e.definition AS entrydefinition,
  89. e.definitionformat AS entryformat,
  90. e.definitiontrust AS entrytrust,
  91. e.timecreated AS entrytimecreated,
  92. u.id AS userid,
  93. u.firstname AS userfirstname,
  94. u.lastname AS userlastname
  95. FROM {glossary_entries} e,
  96. {user} u
  97. WHERE e.glossaryid = {$glossary->id} AND
  98. u.id = e.userid AND
  99. e.approved = 1 $time
  100. ORDER BY e.timecreated desc";
  101. } else {//Without author
  102. $sql = "SELECT e.id AS entryid,
  103. e.concept AS entryconcept,
  104. e.definition AS entrydefinition,
  105. e.definitionformat AS entryformat,
  106. e.definitiontrust AS entrytrust,
  107. e.timecreated AS entrytimecreated,
  108. u.id AS userid
  109. FROM {glossary_entries} e,
  110. {user} u
  111. WHERE e.glossaryid = {$glossary->id} AND
  112. u.id = e.userid AND
  113. e.approved = 1 $time
  114. ORDER BY e.timecreated desc";
  115. }
  116. return $sql;
  117. }
  118. /**
  119. * If there is new stuff in since $time this returns true
  120. * Otherwise it returns false.
  121. *
  122. * @param object $glossary the glossary activity object
  123. * @param int $time timestamp
  124. * @return bool
  125. */
  126. function glossary_rss_newstuff($glossary, $time) {
  127. global $DB;
  128. $sql = glossary_rss_get_sql($glossary, $time);
  129. $recs = $DB->get_records_sql($sql, null, 0, 1);//limit of 1. If we get even 1 back we have new stuff
  130. return ($recs && !empty($recs));
  131. }