/blocks/glossary_random/block_glossary_random.php

https://github.com/viggof/moodle · PHP · 188 lines · 135 code · 41 blank · 12 comment · 24 complexity · ea4b3f23e098d754741ef080c4719ce5 MD5 · raw file

  1. <?php
  2. define('BGR_RANDOMLY', '0');
  3. define('BGR_LASTMODIFIED', '1');
  4. define('BGR_NEXTONE', '2');
  5. class block_glossary_random extends block_base {
  6. function init() {
  7. $this->title = get_string('pluginname','block_glossary_random');
  8. }
  9. function specialization() {
  10. global $CFG, $DB;
  11. require_once($CFG->libdir . '/filelib.php');
  12. $this->course = $this->page->course;
  13. // load userdefined title and make sure it's never empty
  14. if (empty($this->config->title)) {
  15. $this->title = get_string('pluginname','block_glossary_random');
  16. } else {
  17. $this->title = $this->config->title;
  18. }
  19. if (empty($this->config->glossary)) {
  20. return false;
  21. }
  22. if (!isset($this->config->nexttime)) {
  23. $this->config->nexttime = 0;
  24. }
  25. //check if it's time to put a new entry in cache
  26. if (time() > $this->config->nexttime) {
  27. // place glossary concept and definition in $pref->cache
  28. if (!$numberofentries = $DB->count_records('glossary_entries',
  29. array('glossaryid'=>$this->config->glossary, 'approved'=>1))) {
  30. $this->config->cache = get_string('noentriesyet','block_glossary_random');
  31. $this->instance_config_commit();
  32. }
  33. // Get module and context, to be able to rewrite urls
  34. if (! $cm = get_coursemodule_from_instance("glossary", $this->config->glossary, $this->course->id)) {
  35. return false;
  36. }
  37. $glossaryctx = get_context_instance(CONTEXT_MODULE, $cm->id);
  38. $limitfrom = 0;
  39. $limitnum = 1;
  40. switch ($this->config->type) {
  41. case BGR_RANDOMLY:
  42. $i = rand(1,$numberofentries);
  43. $limitfrom = $i-1;
  44. $SORT = 'ASC';
  45. break;
  46. case BGR_NEXTONE:
  47. if (isset($this->config->previous)) {
  48. $i = $this->config->previous + 1;
  49. } else {
  50. $i = 1;
  51. }
  52. if ($i > $numberofentries) { // Loop back to beginning
  53. $i = 1;
  54. }
  55. $limitfrom = $i-1;
  56. $SORT = 'ASC';
  57. break;
  58. default: // BGR_LASTMODIFIED
  59. $i = $numberofentries;
  60. $limitfrom = 0;
  61. $SORT = 'DESC';
  62. break;
  63. }
  64. if ($entry = $DB->get_records_sql("SELECT id, concept, definition, definitionformat, definitiontrust
  65. FROM {glossary_entries}
  66. WHERE glossaryid = ? AND approved = 1
  67. ORDER BY timemodified $SORT", array($this->config->glossary), $limitfrom, $limitnum)) {
  68. $entry = reset($entry);
  69. if (empty($this->config->showconcept)) {
  70. $text = '';
  71. } else {
  72. $text = "<h3>".format_string($entry->concept,true)."</h3>";
  73. }
  74. $options = new stdClass();
  75. $options->trusted = $entry->definitiontrust;
  76. $options->overflowdiv = true;
  77. $entry->definition = file_rewrite_pluginfile_urls($entry->definition, 'pluginfile.php', $glossaryctx->id, 'mod_glossary', 'entry', $entry->id);
  78. $text .= format_text($entry->definition, $entry->definitionformat, $options);
  79. $this->config->nexttime = usergetmidnight(time()) + DAYSECS * $this->config->refresh;
  80. $this->config->previous = $i;
  81. } else {
  82. $text = get_string('noentriesyet','block_glossary_random');
  83. }
  84. // store the text
  85. $this->config->cache = $text;
  86. $this->instance_config_commit();
  87. }
  88. }
  89. function instance_allow_multiple() {
  90. // Are you going to allow multiple instances of each block?
  91. // If yes, then it is assumed that the block WILL USE per-instance configuration
  92. return true;
  93. }
  94. function get_content() {
  95. global $USER, $CFG, $DB;
  96. if (empty($this->config->glossary)) {
  97. $this->content->text = get_string('notyetconfigured','block_glossary_random');
  98. $this->content->footer = '';
  99. return $this->content;
  100. }
  101. $glossaryid = $this->config->glossary;
  102. $course = $this->page->course;
  103. require_once($CFG->dirroot.'/course/lib.php');
  104. $modinfo = get_fast_modinfo($course);
  105. if (!isset($modinfo->instances['glossary'][$glossaryid])) {
  106. // we can get here if the glossary has been deleted, so
  107. // unconfigure the glossary from the block..
  108. $this->config->glossary = 0;
  109. $this->config->cache = '';
  110. $this->instance_config_commit();
  111. $this->content->text = get_string('notyetconfigured','block_glossary_random');
  112. $this->content->footer = '';
  113. return $this->content;
  114. }
  115. $cm = $modinfo->instances['glossary'][$glossaryid];
  116. if (empty($this->config->cache)) {
  117. $this->config->cache = '';
  118. }
  119. if ($this->content !== NULL) {
  120. return $this->content;
  121. }
  122. $this->content = new stdClass;
  123. $this->content->text = $this->config->cache;
  124. // place link to glossary in the footer if the glossary is visible
  125. //Obtain the visible property from the instance
  126. if ($cm->uservisible) {
  127. if (has_capability('mod/glossary:write', get_context_instance(CONTEXT_MODULE, $cm->id))) {
  128. $this->content->footer = '<a href="'.$CFG->wwwroot.'/mod/glossary/edit.php?cmid='.$cm->id
  129. .'" title="'.$this->config->addentry.'">'.$this->config->addentry.'</a><br />';
  130. } else {
  131. $this->content->footer = '';
  132. }
  133. $this->content->footer .= '<a href="'.$CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id
  134. .'" title="'.$this->config->viewglossary.'">'.$this->config->viewglossary.'</a>';
  135. // otherwise just place some text, no link
  136. } else {
  137. $this->content->footer = $this->config->invisible;
  138. }
  139. return $this->content;
  140. }
  141. function hide_header() {
  142. if (empty($this->config->title)) {
  143. return true;
  144. }
  145. return false;
  146. }
  147. }