moodle-block_mytreestudyplan/block_gradelevel.php
2019-10-09 22:08:44 +02:00

90 lines
3.0 KiB
PHP

<?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);
}
// 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 $CFG;
global $USER;
global $COURSE;
if ($this->content !== null) {
return $this->content;
}
$this->content = new stdClass;
if(empty($this->levelset))
{
$this->content->text = "<figure class='dummybadge'><img height='150px' width='150px' src='{$CFG->wwwroot}/blocks/gradelevel/pix/brokenbadge.svg' /></figure>";
$this->content->footer = get_string("unattached_course",'block_gradelevel');
}
else {
// below can be a single call to $this->levelset->get_user_leveldata() once the need for debugging (fixed point total) is gone
$pointstotal = round($this->levelset->get_levelset_grade($USER->id),1);
$level_info = $this->levelset->calculate_level($pointstotal);
$this->content->text = $this->levelset->render_badge($pointstotal);
if($level_info->levelup_total > 0)
{
$this->content->footer = "<div class='pointinfo'>".get_string('levelup_at','block_gradelevel')." <span class='currentpoints'>{$pointstotal}</span>/<span class='leveluppoints'>{$level_info->next_at}</span></div>";
}
else
{
$this->content->footer = "<div class='pointinfo complete'>".get_string('levelup_done','block_gradelevel')."</div>";
}
$coursecontext = context_course::instance($COURSE->id);
if(has_capability('block/gradelevel:viewresults', $coursecontext))
{
$this->content->footer .= "\n<div class='teachermode'><a href='{$CFG->wwwroot}/blocks/gradelevel/view-results.php?courseid={$COURSE->id}'>".get_string('teacher_view_results','block_gradelevel')."</a></div>";
}
}
return $this->content;
}
public function hide_header() { return !get_config('gradelevel', 'showtitle'); }
public function has_config() { return true; }
}