block_sloodle_backpack.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Defines the Sloodle menu block class.
  4. *
  5. * @package sloodle
  6. * @contributor Paul Preibisch
  7. * @contributor Edmund Edgar
  8. */
  9. /** Include the current Sloodle configuration, if possible. */
  10. @include_once($CFG->dirroot .'/mod/sloodle/sl_config.php');
  11. if (defined('SLOODLE_LIBROOT')) {
  12. /** Inlcude the current general Sloodle functionality. */
  13. require_once(SLOODLE_LIBROOT.'/general.php');
  14. /** Include the Sloodle course functionality. */
  15. require_once(SLOODLE_LIBROOT.'/course.php');
  16. }
  17. /** Define the Sloodle Menu Block version. */
  18. define('BACKPACKS_VERSION', 1.0);
  19. /**
  20. * Defines the block class.
  21. * @package sloodle
  22. */
  23. class block_sloodle_backpack extends block_base {
  24. /**
  25. * Perform block initialisation.
  26. * @return void
  27. */
  28. function init() {
  29. global $CFG;
  30. $this->title = get_string('blockname', 'block_sloodle_backpack');
  31. $this->content_type = BLOCK_TYPE_TEXT;
  32. $this->version = 2011070900;
  33. }
  34. /**
  35. * Indicates whether or not this module has a global configuration page.
  36. * @return bool True if there is a global configuration page, or false otherwise.
  37. */
  38. function has_config() {
  39. return false;
  40. }
  41. /**
  42. * Indicates whether or not to hide the header of this block.
  43. * @return bool True to hide the header, or false to show it.
  44. */
  45. function hide_header() {
  46. return false;
  47. }
  48. /**
  49. * Defines *and* returns the content of this block.
  50. * @return object
  51. */
  52. function get_content() {
  53. global $CFG, $COURSE, $USER;
  54. // Construct the content
  55. $this->content = new stdClass;
  56. $this->content->text = '';
  57. $this->content->footer = '';
  58. // If the user is not logged in or if they are using guest access, then we can't show anything
  59. if (!isloggedin() || isguestuser()) {
  60. return $this->content;
  61. }
  62. // Get the context instance for this course
  63. $course_context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
  64. // Get the Sloodle course data
  65. $sloodle_course = new SloodleCourse();
  66. if (!$sloodle_course->load((int)$COURSE->id)) {
  67. $this->content->text = get_string('failedloadcourse', 'block_sloodle_backpack');
  68. return $this->content;
  69. }
  70. // Attempt to find a Sloodle user for the Moodle user
  71. $dbquery = " SELECT * FROM {$CFG->prefix}sloodle_users
  72. WHERE userid = ? AND NOT (avname = '' AND uuid = '')
  73. ";
  74. $dbresult = sloodle_get_records_sql_params($dbquery,array($USER->id));
  75. $sl_avatar_name = "";
  76. if (!is_array($dbresult) || count($dbresult) == 0) $userresult = FALSE;
  77. else if (count($dbresult) > 1) $userresult = "Multiple avatars associated with your Moodle account.";
  78. else {
  79. $userresult = TRUE;
  80. reset($dbresult);
  81. $cur = current($dbresult);
  82. $sl_avatar_name = $cur->avname;
  83. }
  84. if ($userresult === TRUE) {
  85. // Success
  86. // Make sure there was a name
  87. if (empty($sl_avatar_name)) $sl_avatar_name = '('.get_string('backpacks:nameunknown', 'sloodle').')';
  88. } else if (!is_string($userresult)) {
  89. // No avatar linked yet
  90. $this->content->text .= '<center><span style="font-style:italic;">('.get_string('backpacks:noavatar', 'sloodle').')</span></center>';
  91. }
  92. //retreive all currencies and user info
  93. $this->content->text .= '<div>';
  94. $sql = "select p.currencyid as currencyid, sum(p.amount) as balance, c.name, c.imageurl as imageurl from {$CFG->prefix}sloodle_award_points p inner join {$CFG->prefix}sloodle_currency_types c on p.currencyid=c.id inner join {$CFG->prefix}sloodle_award_rounds r on p.roundid=r.id where p.userid=? AND r.courseid=? group by p.currencyid ORDER BY c.displayorder DESC";
  95. $items = sloodle_get_records_sql_params($sql, array($USER->id, $COURSE->id));
  96. foreach ($items as $item){
  97. $this->content->text .= "<div>";
  98. if ($item->imageurl===null) {
  99. $this->content->text .= "<img src=\"".SLOODLE_WWWROOT."/lib/media/blank16.png\" width=\"16\" height=\"16\" >";
  100. }else {
  101. $this->content->text .= "<img src=\"{$item->imageurl}\" width=\"16\" height=\"16\" >";
  102. }
  103. $this->content->text .= "&nbsp{$item->name}";
  104. $this->content->text .= '<span style="float:right">';
  105. $this->content->text .= "{$item->balance}";
  106. $this->content->text .= '</span>';
  107. $this->content->text .= "</div>";
  108. }
  109. $this->content->text .= '</div>';
  110. $this->content->text .= "<br>";
  111. return $this->content;
  112. }
  113. }
  114. ?>