我想使用angularjs代码(通过调用web-service)获取moodle中登录用户的用户ID。
答案 0 :(得分:0)
我会这样做:
1 - 在moodle中创建一个本地插件:https://docs.moodle.org/dev/Local_plugins
2 - 在该插件中添加一个脚本,该脚本返回以json格式登录的用户ID:
<?php
require_once('../../config.php');
require_login(); // We need login
$item = new stdClass();
$item->user = $USER->id;
// Print response
header('Expires: 0');
header('Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
header('Content-Type: application/json; charset=utf-8');
echo json_encode($item);
3 - 从您的角度,对新脚本进行ajax调用:
<div ng-app="myApp" ng-controller="myCtrl">
<p>Logged user id:</p>
<h1>{{myUser}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get('/local/myplugin/ajax.php')
.then(function(response) {
$scope.myUser = response.user;
});
});
</script>