I want to get userid of logged in user in moodle using angularjs code (by calling web-service).
Asked
Active
Viewed 1,350 times
1 Answers
0
I would do this:
1 - Create a local plugin in moodle: https://docs.moodle.org/dev/Local_plugins
2 - Add a script in that plugin that returns id of user logged in json format:
<?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 - From your angular, make a ajax call to the new script:
<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>

icsbcn
- 11
- 6