block_badges.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. * Block for displaying earned local badges to users
  18. *
  19. * @package block_badges
  20. * @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. * @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. require_once($CFG->libdir . "/badgeslib.php");
  26. /**
  27. * Displays recent badges
  28. */
  29. class block_badges extends block_base {
  30. public function init() {
  31. $this->title = get_string('pluginname', 'block_badges');
  32. }
  33. public function instance_allow_multiple() {
  34. return true;
  35. }
  36. public function has_config() {
  37. return false;
  38. }
  39. public function instance_allow_config() {
  40. return true;
  41. }
  42. public function applicable_formats() {
  43. return array(
  44. 'admin' => false,
  45. 'site-index' => true,
  46. 'course-view' => true,
  47. 'mod' => false,
  48. 'my' => true
  49. );
  50. }
  51. public function specialization() {
  52. if (empty($this->config->title)) {
  53. $this->title = get_string('pluginname', 'block_badges');
  54. } else {
  55. $this->title = $this->config->title;
  56. }
  57. }
  58. public function get_content() {
  59. global $USER, $PAGE, $CFG;
  60. if ($this->content !== null) {
  61. return $this->content;
  62. }
  63. if (empty($this->config)) {
  64. $this->config = new stdClass();
  65. }
  66. // Number of badges to display.
  67. if (!isset($this->config->numberofbadges)) {
  68. $this->config->numberofbadges = 10;
  69. }
  70. // Create empty content.
  71. $this->content = new stdClass();
  72. $this->content->text = '';
  73. if (empty($CFG->enablebadges)) {
  74. $this->content->text .= get_string('badgesdisabled', 'badges');
  75. return $this->content;
  76. }
  77. $courseid = $this->page->course->id;
  78. if ($courseid == SITEID) {
  79. $courseid = null;
  80. }
  81. if ($badges = badges_get_user_badges($USER->id, $courseid, 0, $this->config->numberofbadges)) {
  82. $output = $this->page->get_renderer('core', 'badges');
  83. $this->content->text = $output->print_badges_list($badges, $USER->id, true);
  84. } else {
  85. $this->content->text .= get_string('nothingtodisplay', 'block_badges');
  86. }
  87. return $this->content;
  88. }
  89. }