moodle-block_mytreestudyplan/block_gradelevel.php

103 lines
3.0 KiB
PHP
Raw Normal View History

<?php
require_once($CFG->libdir.'/gradelib.php');
require_once($CFG->dirroot.'/grade/querylib.php');
require_once($CFG->dirroot.'/blocks/gradelevel/lib.php');
use block_gradelevel;
class block_gradelevel extends block_base {
public $levelset;
public function init() {
global $PAGE;
global $COURSE;
$this->title = get_config('gradelevel', 'blocktitle');
if(empty($this->title))
{
$this->title = get_string('title', 'block_gradelevel');
}
// include javascript and run badge renderer when page loading is complete
$PAGE->requires->js_call_amd('block_gradelevel/renderbadge', 'init');
// find or create the levelset for this course
$this->levelset = block_gradelevel_levelset::find_by_course($COURSE->id);
if(empty($this->levelset))
{
// create a new levelset with the name of this course and attach
$this->levelset = block_gradelevel_levelset::create_new($COURSE->shortname);
$this->levelset->attach_course($COURSE->id);
}
}
// The PHP tag and the curly bracket for the class definition
// will only be closed after there is another function added in the next section.
public function html_attributes() {
$attributes = parent::html_attributes(); // Get default values
$attributes['class'] .= ' block_'. $this->name(); // Append our class to class attribute
return $attributes;
}
public function get_content() {
global $USER;
if ($this->content !== null) {
return $this->content;
}
$this->content = new stdClass;
// below can be a single call to $this->levelset->get_user_leveldata() once the need for debugging (fixed point total) is gone
$pointstotal = $this->levelset->get_levelset_grade($USER->id);
$level_info = $this->levelset->calculate_level($pointstotal);
$this->content->text = $this->render_badge($level_info->badge_color,$level_info->progress,$this->levelset->getIcon(),$level_info->level);
if($level_info->levelup_total > 0)
{
$this->content->footer = "<div class='pointinfo'>".get_string('levelup_at','block_gradelevel')." <span class='currentpoints'>{$level_info->points_in_level}</span>/<span class='leveluppoints'>{$level_info->levelup_total}</span></div>";
}
else
{
$this->content->footer = "<div class='pointinfo complete'>".get_string('levelup_done','block_gradelevel')."</div>";
}
return $this->content;
}
public function hide_header() { return false; }
public function has_config() { return true; }
private function render_badge($basecolor,$progress,$image=null,$level="1"){
global $CFG;
if(strncmp($image,"data: ",6) != 0)
{
$image_url = $CFG->wwwroot.$image;
}
else
{
$image_url = $image;
}
$html = "<figure class='levelbadge' data-color='{$basecolor}' data-progress='{$progress}' data-width='150' data-height='150' data-level='{$level}'>";
if(!empty($image))
{
$html .= "<img style='display:none' src='{$image_url}' />";
}
$html .= "</figure>";
return $html;
}
}