0

I have two pages in my Moodle. The first is the enrolment page and the second is the course page. On each of them I have a button, which prints PDF. On the enrolment page there are breadcrumbs, which look like that:

Startpage->Course->Miscellaneous->Learning Course 1->Enrolment->Enrolment Options

Under Learning Course 1 there is a link, which is:

http://localhost/mymoodle/course/view.php?id=6

How to get the id from this link? I need the id in order to get the course information into PDF.

I build up the functionality to get the id on course level and the code works:

$('#page-enrol-index .printpdf').click(function() {
        //jquery extend function
        $.extend(
        {
            redirectPost: function(location, args)
            {
                var form = '';
                $.each( args, function( key, value ) {
                    form += '<input type="hidden" name="'+key+'" value="'+value+'">';
                });
                $('<form action="'+location+'" method="POST">'+form+'</form>').appendTo('body').submit();
            }
        });

        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }

        //create the pdf
        $.redirectPost("http://localhost/mymoodle/local/getinfo/getinfocourse.php", {
            id: vars['id']

        });

When trying to get the id it from the enrolment url

http://localhost/mymoodle/enrol/index.php?id=6

It won't work.

The id is needed in order to get the information from the course for the pdf, where there is:

$courseid = required_param('id', PARAM_INT);

The enrolment page just loads and the PDF isn't being printed, so I guess the PDF doesn't get the id from the course? I am new to Moodle and Javascript, so any help will be appreciated.

StartVader
  • 151
  • 1
  • 13
  • Possible duplicate of [How to retrieve GET parameters from javascript?](http://stackoverflow.com/questions/5448545/how-to-retrieve-get-parameters-from-javascript) – Eihwaz Apr 06 '16 at 09:16

2 Answers2

1

You could use the Moodle single button function instead of Javascript.

$printpdfurl = new moodle_url('/local/getinfo/getinfocourse.php', array('id' => $id));
echo $OUTPUT->single_button($printpdfurl, get_string('printpdf', 'local_getinfo'));
Russell England
  • 9,436
  • 1
  • 27
  • 41
0
function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)", "i"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

// query string: 'http://localhost/mymoodle/course/view.php?id=6'
var id = getParameterByName('id'); // 6
Nic
  • 217
  • 2
  • 16
  • Using `consolelog` I get the id even with my code, but still won't work... In the printing of the pdf the **required parameter** is `$courseid = required_param('id', PARAM_INT);` – StartVader Apr 06 '16 at 09:34
  • Also Moodle shows me in the Console the follwing message: `moodle-block_navigation: Initialising navigation block tree` – StartVader Apr 06 '16 at 09:57