2016-04-06 133 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