У меня есть плагин блока moodle, где я пытаюсь включить javascript на страницу.
Как бы я ни пытался, javascript, вызываемый js_init_call, всегда выдает ошибку вместо запуска. Я пытался передать полный массив в js_init_call и пробовал сокращенную форму, которая предполагает, что файл называется module.js.
Если файл JavaScript не существует, я получаю сообщение об ошибке Moodle, жалующееся на отсутствие файла. Если он действительно существует, я получаю сообщение об ошибке javascript, как указано выше. Что я делаю не так?
block_foo.php:
<?php
class block_foo extends block_base {
public function init() {
$this->title = get_string('foo', 'block_foo');
}
public function applicable_formats(){
return array('course-view' => true);
}
public function get_content() {
global $PAGE;
if ($this->content !== null) {
return $this->content;
}
if(!isloggedin()){
//if(!is_enrolled()){
return false;
}
$this->content = new stdClass;
$this->content->text = 'asdf';
$this->content->footer = '';
$jsmodule = array(
'name' => 'block_foo',
'fullpath' => '/blocks/foo/foo.js',
'requires' => array(),
'strings' => array()
);
$PAGE->requires->js_init_call('M.block_foo.init', null, false, $jsmodule);
//This style of call doesn't work either... (if the js is named module.js)
//$PAGE->requires->js_init_call('M.block_foo.init', null);
return $this->content;
}
function hide_header(){
return true;
}
}
module.js или foo.js:
M.block_foo = {};
M.block_foo.init = function(){
alert("I was called, yay");
$var = 1234;
$var = $var + 3;
};