2016-04-06 185 views
0

我在我的Moodle中有兩個頁面。第一個是註冊頁面,第二個是課程頁面。在他們每個人我有一個按鈕,其中打印PDF。在註冊頁面有麪包,這看起來像:從頁面上的鏈接獲取ID

Startpage->課程 - > Miscellaneous->學習課程 1-> Enrolment->註冊選項

Learning Course 1有是一個鏈接,它是:

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

如何獲得編號爲此鏈接鏈接?我需要id才能將課程信息轉換爲PDF。

我建立的功能,以獲取有關課程級別的ID和代碼工作:

$('#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'] 

     }); 

當試圖從報名網址

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

它不起作用。

ID是必要的,以獲得從課程的信息爲PDF格式,其中有:

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

註冊頁面並沒有被打印只是載荷和PDF,所以我想PDF不會從課程中獲得ID?我是新來的Moodle和Javascript,所以任何幫助將不勝感激。

+0

的【如何檢索JavaScript的GET參數?](http://stackoverflow.com/questions/5448545/how-to-retrieve-get-parameters-from-javascript) – Eihwaz

回答

1

您可以使用Moodle單鍵功能而不是Javascript。

$printpdfurl = new moodle_url('/local/getinfo/getinfocourse.php', array('id' => $id)); 
echo $OUTPUT->single_button($printpdfurl, get_string('printpdf', 'local_getinfo')); 
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 
+0

可能的複製使用'consolelog '即使使用我的代碼,我也能得到id,但仍然無法工作......在打印pdf時,**所需的參數**是'$ courseid = required_pa​​ram('id',PARAM_INT);' – StartVader

+0

也Moodle在控制檯中向我顯示下列消息:「moodle-block_navigation:初始化導航塊樹」 – StartVader