PageRenderTime 39ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/glossary/rsslib.php

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