Themes

Overriding mod renderer

 
Picture of Oliver Trampleasure
Overriding mod renderer
 

Hi everyone

I think I'm doing something silly... I've been following the tutorial on overriding renderers and had a google but can't work it out.

I'm trying to override the renderer for a module (studentquiz) but it doesn't seem to be working.

Below is what I've done so far... I've just copy and pasted a function in and then deleted a few lines from it - expecting to see the changes after purging the caches but nothing is happening. Have tried various setups but nothing is working.

Any help would be greatly appreciated!


Many thanks


Ollie


-----

/theme/THEMENAME/config.php has the line:

$THEME->rendererfactory = 'theme_overridden_renderer_factory';

-----

/theme/THEMENAME/renderers.php is as follows:


<?php

defined('MOODLE_INTERNAL') || die;

include_once($CFG->dirroot . "/mod/studentquiz/renderer.php");

class theme_THEMENAME_mod_studentquiz_renderer extends mod_studentquiz_renderer {

   // place your overridden methods (functions) here.

   public function render_stat_block($report) {

      ...

   }

}


 
Average of ratings: -
It's only an avatar...
Re: Overriding mod renderer
Core developersDocumentation writersParticularly helpful MoodlersPlugin developersTesters

Hi Oliver,

You did not tell me the Moodle version or your theme name and version?

This info is needed to answers the question.

Im not familiar with that particular mod/student_quiz, so it could be lots of things that are missing, like changes to your themes lib.php if there is one or adding a lib.php if there isn't one!

It would have been better if you had added the acctual code in the renderer.

Thanks,

Mary

 
Average of ratings: -
Picture of Oliver Trampleasure
Re: Overriding mod renderer
 

Apologies

  • Moodle: v3.2
  • Theme: Photo v1.3 clone that I've basically just added a load of custom scss to.

Below is the full renderer code, with the two functions I've tweaked.


Thanks for your help

---

<?php


include_once($CFG->dirroot . "/mod/studentquiz/renderer.php");


class theme_medtastic_mod_studentquiz_renderer extends mod_studentquiz_renderer {


   // place your overridden methods (functions) here.


   public function render_stat_block($report) {

       // TODO: Refactor: use mod_studentquiz_report_record_type!

       $userstats = $report->get_user_stats();

       $sqstats = $report->get_studentquiz_stats();

       if (!$userstats) {

           $bc = new block_contents();

           $bc->attributes['id'] = 'mod_studentquiz_statblock';

           $bc->attributes['role'] = 'navigation';

           $bc->attributes['aria-labelledby'] = 'mod_studentquiz_navblock_title';

           $bc->title = html_writer::span(get_string('statistic_block_title', 'studentquiz'));

           $bc->content = get_string('please_enrole_message', 'studentquiz');

           return $bc;

       }

       $bc = new block_contents();

       $bc->attributes['id'] = 'mod_studentquiz_statblock';

       $bc->attributes['role'] = 'navigation';

       $bc->attributes['aria-labelledby'] = 'mod_studentquiz_navblock_title';

       $bc->title = html_writer::span(get_string('statistic_block_title', 'studentquiz'));

       $info1 = new stdClass();

       $info1->total = $sqstats->questions_available;

       $info1->group = $userstats->last_attempt_exists;

       $info1->one = $userstats->last_attempt_correct;

       $info2 = new stdClass();

       $info2->total = $userstats->questions_created;

       $info2->group = 0;

       $info2->one = $userstats->questions_approved;

       $bc->content = html_writer::div($this->render_progress_bar($info1), '', array('style' => 'width:inherit'))

            . html_writer::div(

               get_string('statistic_block_progress_last_attempt_correct', 'studentquiz')

               .html_writer::span('<b class="stat last-attempt-correct">' .$userstats->last_attempt_correct .'</b>', '',

                   array('style' => 'float: right;color:#5cb85c;')))

           . html_writer::div(

               get_string('statistic_block_progress_last_attempt_incorrect', 'studentquiz')

               .html_writer::span('<b class="stat last-attempt-incorrect">' .$userstats->last_attempt_incorrect .'</b>', '',

                   array('style' => 'float: right;color:#d9534f;')))

           . html_writer::div(

               get_string('statistic_block_progress_never', 'studentquiz')

               .html_writer::span(

                   '<b class="stat never-answered">' . ($sqstats->questions_available - $userstats->last_attempt_exists) .'</b>',

                   '', array('style' => 'float: right;color:#f0ad4e;')))

           . html_writer::div(

               get_string('statistic_block_progress_available', 'studentquiz')

               .html_writer::span('<b class="stat questions-available">' .$sqstats->questions_available .'</b>', '',

                   array('style' => 'float: right;')));

       return $bc;

   }


   public function render_progress_bar($info, $texttotal=null, $bicolor=false) {


       // Check input.

       $validinput = true;

       if (!isset($info->total)) {

           $validinput = false;

       }


       if (!isset($info->group)) {

           $validinput = false;

       }


       if (!isset($info->one)) {

           $validinput = false;

       }


       // Stylings.

       $rgbstroke = 'rgb(1, 22, 39)';

       $rgbyellow = 'rgb(255, 159, 28)';

       $rgbgreen = 'rgb(46, 196, 182)';

       $rgbred = 'rgb(231, 29, 54)';

       $rgbred = 'rgb(231, 29, 54)';

       $rgbgrey = 'rgb(253, 255, 252)';

       $barstroke = 'stroke-width:0.1;stroke:' . $rgbstroke .';';

       $svgdims = array('width' => '100%', 'height' => 20);

       $bardims = array('height' => '100%', 'rx' => 5, 'ry' => 5);

       $idblue = 'blue';

       $idgreen = 'green';

       $idred = 'red';

       $gradientdims = array('cx' => '50%', 'cy' => '50%', 'r' => '50%', 'fx' => '50%', 'fy' => '50%');

       $stopcolorgreen = html_writer::tag('stop', null,

           array('offset' => '100%', 'style' => 'stop-color:' . $rgbgreen . ';stop-opacity:1'));

       $stopcolorred = html_writer::tag('stop', null,

           array('offset' => '100%', 'style' => 'stop-color:' . $rgbred . ';stop-opacity:1'));

       $stopcolorblue = html_writer::tag('stop', null,

           array('offset' => '100%', 'style' => 'stop-color:' . $rgbblue . ';stop-opacity:1'));

       $gradientblue = html_writer::tag('radialGradient', $stopcolorblue . $stopcolorblue,

           array_merge($gradientdims, array('id' => $idblue)));

       $gradientred = html_writer::tag('radialGradient', $stopcolorred . $stopcolorred,

           array_merge($gradientdims, array('id' => $idred)));

       $gradientgreen = html_writer::tag('radialGradient', $stopcolorgreen . $stopcolorgreen,

           array_merge($gradientdims, array('id' => $idgreen)));

       $gradients = array($gradientred, $gradientgreen, $gradientblue);

       $defs = html_writer::tag('defs', implode($gradients));


       // Background bar.

       if ($bicolor) {

           $barbackground = html_writer::tag('rect', null, array_merge($bardims,

               array('width' => '100%', 'style' => $barstroke . 'fill:' . $rgbgrey )));

       } else {

           $barbackground = html_writer::tag('rect', null, array_merge($bardims,

               array('width' => '100%', 'style' => $barstroke . 'fill:' . $rgbyellow)));

       }


       // Return empty bar if no questions are in StudentQuiz.

       if (!$validinput || $info->total <= 0) {

           return html_writer::tag('svg', $barbackground, $svgdims);

       }


       // Calculate Percentages to display.

       $percentgroup = round(100 * ($info->group / $info->total));

       $percentone = round(100 * ($info->one / $info->total));


       if (!empty($texttotal)) {

           $text = '<text xml:space="preserve" text-anchor="start" font-family="Helvetica, Arial, sans-serif"'

            .' font-size="12" font-weight="bold" id="svg_text" x="50%" y="50%" alignment-baseline="middle"'

            .' text-anchor="middle" stroke-width="0" stroke="#000" fill="#000000">' . $texttotal . '</text>';

       } else {

           $text = '';

       }


       // Return stacked bars.

       $bars = array($barbackground);

       if ($bicolor) {

           $bars[] = html_writer::tag('rect', null, array_merge($bardims,

               array('width' => $percentone . '%', 'style' => $barstroke . 'fill:url(#' . $idblue .')')));

       } else {

           $bars[] = html_writer::tag('rect', null, array_merge($bardims,

               array('width' => $percentgroup . '%', 'style' => $barstroke . 'fill:url(#' . $idred .')')));

           $bars[] = html_writer::tag('rect', null, array_merge($bardims,

               array('width' => $percentone . '%', 'style' => $barstroke . 'fill:url(#' . $idgreen .')')));

       }

       return html_writer::tag('svg', $defs . implode($bars) . $text, $svgdims);

   }


}


 
Average of ratings: -
It's only an avatar...
Re: Overriding mod renderer
Core developersDocumentation writersParticularly helpful MoodlersPlugin developersTesters

Thanks Oliver, 

I don't seem to be able to find this particular plugin I order to compare code.

Can you point me to the place where you downloaded it from please?

Cheers

Mary

 
Average of ratings: -
Picture of Oliver Trampleasure
Re: Overriding mod renderer
 

https://moodle.org/plugins/mod_studentquiz

I'm using the latest version.


Ta

 
Average of ratings: -
It's only an avatar...
Re: Overriding mod renderer
Core developersDocumentation writersParticularly helpful MoodlersPlugin developersTesters

Thanks Oliver, I'll look through it later today all being well.

Will get back to ASP

Cheers

Mary

 
Average of ratings: -
It's only an avatar...
Re: Overriding mod renderer
Core developersDocumentation writersParticularly helpful MoodlersPlugin developersTesters

Hi Ollie,

There appears to be an error in the renderers.php of your theme.

Can you please check it using the CodeChecker plugin?

Thanks

Mary

 
Average of ratings: -
Picture of Oliver Trampleasure
Re: Overriding mod renderer
 

This gets a thumbs up from the CodeChecker.

Thanks for your help Mary, let me know if you need anything else from me.

----


<?php

// This file is part of Moodle - http://moodle.org/

//

// Moodle is free software: you can redistribute it and/or modify

// it under the terms of the GNU General Public License as published by

// the Free Software Foundation, either version 3 of the License, or

// (at your option) any later version.

//

// Moodle is distributed in the hope that it will be useful,

// but WITHOUT ANY WARRANTY; without even the implied warranty of

// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

// GNU General Public License for more details.

//

// You should have received a copy of the GNU General Public License

// along with Moodle.  If not, see <http://www.gnu.org/licenses/>;.


/**

 * This is a one-line short description of the file.

 *

 * You can have a rather longer description of the file as well,

 * if you like, and it can span multiple lines.

 *

 * @package    THEMENAME

 * @category   theme

 * @copyright  2017 XXX

 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

 */


defined('MOODLE_INTERNAL') || die();


require_once($CFG->dirroot . "/mod/studentquiz/renderer.php");


class theme_THEMENAME_mod_studentquiz_renderer extends mod_studentquiz_renderer {


    public function render_stat_block($report) {

        // TODO: Refactor: use mod_studentquiz_report_record_type!

        $userstats = $report->get_user_stats();

        $sqstats = $report->get_studentquiz_stats();

        if (!$userstats) {

            $bc = new block_contents();

            $bc->attributes['id'] = 'mod_studentquiz_statblock';

            $bc->attributes['role'] = 'navigation';

            $bc->attributes['aria-labelledby'] = 'mod_studentquiz_navblock_title';

            $bc->title = html_writer::span(get_string('statistic_block_title', 'studentquiz'));

            $bc->content = get_string('please_enrole_message', 'studentquiz');

            return $bc;

        }

        $bc = new block_contents();

        $bc->attributes['id'] = 'mod_studentquiz_statblock';

        $bc->attributes['role'] = 'navigation';

        $bc->attributes['aria-labelledby'] = 'mod_studentquiz_navblock_title';

        $bc->title = html_writer::span(get_string('statistic_block_title', 'studentquiz'));

        $info1 = new stdClass();

        $info1->total = $sqstats->questions_available;

        $info1->group = $userstats->last_attempt_exists;

        $info1->one = $userstats->last_attempt_correct;

        $info2 = new stdClass();

        $info2->total = $userstats->questions_created;

        $info2->group = 0;

        $info2->one = $userstats->questions_approved;

        $bc->content = html_writer::div($this->render_progress_bar($info1), '', array('style' => 'width:inherit'))

             . html_writer::div(

                get_string('statistic_block_progress_last_attempt_correct', 'studentquiz')

                .html_writer::span('<b class="stat last-attempt-correct">' .$userstats->last_attempt_correct .'</b>', '',

                    array('style' => 'float: right;color:#5cb85c;')))

            . html_writer::div(

                get_string('statistic_block_progress_last_attempt_incorrect', 'studentquiz')

                .html_writer::span('<b class="stat last-attempt-incorrect">' .$userstats->last_attempt_incorrect .'</b>', '',

                    array('style' => 'float: right;color:#d9534f;')))

            . html_writer::div(

                get_string('statistic_block_progress_never', 'studentquiz')

                .html_writer::span(

                    '<b class="stat never-answered">' . ($sqstats->questions_available - $userstats->last_attempt_exists) .'</b>',

                    '', array('style' => 'float: right;color:#f0ad4e;')));

        return $bc;

    }


    public function render_progress_bar($info, $texttotal=null, $bicolor=false) {


        // Check input.

        $validinput = true;

        if (!isset($info->total)) {

            $validinput = false;

        }


        if (!isset($info->group)) {

            $validinput = false;

        }


        if (!isset($info->one)) {

            $validinput = false;

        }


        // Stylings.

        $rgbstroke = 'rgb(1, 22, 39)';

        $rgbyellow = 'rgb(255, 159, 28)';

        $rgbgreen = 'rgb(46, 196, 182)';

        $rgbblue = 'rgb(2, 117, 216)';

        $rgbred = 'rgb(231, 29, 54)';

        $rgbgrey = 'rgb(253, 255, 252)';

        $barstroke = 'stroke-width:0.1;stroke:' . $rgbstroke .';';

        $svgdims = array('width' => '100%', 'height' => 20);

        $bardims = array('height' => '100%', 'rx' => 5, 'ry' => 5);

        $idblue = 'blue';

        $idgreen = 'green';

        $idred = 'red';

        $gradientdims = array('cx' => '50%', 'cy' => '50%', 'r' => '50%', 'fx' => '50%', 'fy' => '50%');

        $stopcolorgreen = html_writer::tag('stop', null,

            array('offset' => '100%', 'style' => 'stop-color:' . $rgbgreen . ';stop-opacity:1'));

        $stopcolorred = html_writer::tag('stop', null,

            array('offset' => '100%', 'style' => 'stop-color:' . $rgbred . ';stop-opacity:1'));

        $stopcolorblue = html_writer::tag('stop', null,

            array('offset' => '100%', 'style' => 'stop-color:' . $rgbblue . ';stop-opacity:1'));

        $gradientblue = html_writer::tag('radialGradient', $stopcolorblue . $stopcolorblue,

            array_merge($gradientdims, array('id' => $idblue)));

        $gradientred = html_writer::tag('radialGradient', $stopcolorred . $stopcolorred,

            array_merge($gradientdims, array('id' => $idred)));

        $gradientgreen = html_writer::tag('radialGradient', $stopcolorgreen . $stopcolorgreen,

            array_merge($gradientdims, array('id' => $idgreen)));

        $gradients = array($gradientred, $gradientgreen, $gradientblue);

        $defs = html_writer::tag('defs', implode($gradients));


        // Background bar.

        if ($bicolor) {

            $barbackground = html_writer::tag('rect', null, array_merge($bardims,

                array('width' => '100%', 'style' => $barstroke . 'fill:' . $rgbgrey )));

        } else {

            $barbackground = html_writer::tag('rect', null, array_merge($bardims,

                array('width' => '100%', 'style' => $barstroke . 'fill:' . $rgbyellow)));

        }


        // Return empty bar if no questions are in StudentQuiz.

        if (!$validinput || $info->total <= 0) {

            return html_writer::tag('svg', $barbackground, $svgdims);

        }


        // Calculate Percentages to display.

        $percentgroup = round(100 * ($info->group / $info->total));

        $percentone = round(100 * ($info->one / $info->total));


        if (!empty($texttotal)) {

            $text = '<text xml:space="preserve" text-anchor="start" font-family="Helvetica, Arial, sans-serif"'

             .' font-size="12" font-weight="bold" id="svg_text" x="50%" y="50%" alignment-baseline="middle"'

             .' text-anchor="middle" stroke-width="0" stroke="#000" fill="#000000">TEST' . $texttotal . '</text>';

        } else {

            $text = '';

        }


        // Return stacked bars.

        $bars = array($barbackground);

        if ($bicolor) {

            $bars[] = html_writer::tag('rect', null, array_merge($bardims,

                array('width' => $percentone . '%', 'style' => $barstroke . 'fill:url(#' . $idblue .')')));

        } else {

            $bars[] = html_writer::tag('rect', null, array_merge($bardims,

                array('width' => $percentgroup . '%', 'style' => $barstroke . 'fill:#e71d36)')));

            $bars[] = html_writer::tag('rect', null, array_merge($bardims,

                array('width' => $percentone . '%', 'style' => $barstroke . 'fill:#2ec4b6')));

        }

        return html_writer::tag('svg', $defs . implode($bars) . $text, $svgdims);

    }


}


 
Average of ratings: -
It's only an avatar...
Re: Overriding mod renderer
Core developersDocumentation writersParticularly helpful MoodlersPlugin developersTesters

Hi Ollie, 

Have you tried enabeling Debugging?

Site Administration > Development > Debugging > DEVELOPER mode

It may help as it will show you were things are going wrong.

Hope this helps?

Mary

 
Average of ratings: -