0

我想使用 angularjs 代码(通过调用 web 服务)获取 moodle 中登录用户的用户 ID。

4

1 回答 1

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>
于 2016-04-01T11:19:15.123 回答