Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
LabNbook
LabNbook-Moodle
Commits
c8b13236
Commit
c8b13236
authored
Feb 21, 2019
by
Francois Gannaz
Browse files
class mod_labnbook_session to store and query LnN info
parent
2d3747c2
Changes
1
Hide whitespace changes
Inline
Side-by-side
classes/session.php
0 → 100644
View file @
c8b13236
<?php
/**
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
*/
/* @var $DB moodle_database */
/**
* Queries on LabNbook info for the current user.
*/
class
mod_labnbook_session
{
private
static
$singleton
;
/**
* @var string
*/
private
$url
;
/**
* @var string
*/
private
$secret
;
/**
* @var int
*/
private
$userId
;
/**
* @var array
*/
private
$missions
=
[];
private
$identifyByCasAuth
=
true
;
private
$identifyByStudentNumber
=
true
;
private
$autoCreateUser
=
true
;
public
static
function
load
()
{
if
(
empty
(
self
::
$singleton
))
{
self
::
$singleton
=
new
self
;
}
return
self
::
$singleton
;
}
public
function
__construct
()
{
global
$CFG
,
$USER
;
$this
->
url
=
rtrim
(
$CFG
->
labnbook_url
,
'/'
);
$this
->
secret
=
$CFG
->
labnbook_key
;
$this
->
userId
=
$this
->
fetchUserId
(
$USER
->
id
);
}
/**
* Search the LabNbook user ID in the cache (object attribute),
* then in the user session,
* then the DB,
* then through the LabNbook REST API (first GET, then PUT).
*
* @return int
*/
public
function
getUserId
()
{
if
(
!
isset
(
$this
->
userId
))
{
$this
->
userId
=
$this
->
fetchUserId
();
}
return
$this
->
userId
;
}
public
function
getMissions
()
{
if
(
!
isset
(
$this
->
missions
))
{
$this
->
missions
=
$this
->
fetchMissions
();
}
return
$this
->
missions
;
}
/**
* @global moodle_database $DB
* @global object $SESSION
* @global object $USER
* @param boolean $force
* @return int
*/
protected
function
fetchUserId
(
$force
=
false
)
{
global
$DB
,
$SESSION
,
$USER
;
if
(
$force
||
!
isset
(
$SESSION
->
labnbook_userid
))
{
$userId
=
$DB
->
get_field
(
"mod_labnbook_user"
,
"labnbook_userid"
,
[
'userid'
=>
$USER
->
id
]);
if
(
!
$userId
&&
$this
->
identifyByStudentNumber
&&
$USER
->
idnumber
)
{
$userId
=
$this
->
fetch
(
'GET'
,
"/user/identify"
,
null
,
[
'idnumber'
=>
$USER
->
idnumber
]);
}
if
(
!
$userId
&&
$this
->
identifyByCasAuth
&&
$USER
->
auth
===
'cas'
)
{
$userId
=
$this
->
fetch
(
'GET'
,
"/user/identify"
,
null
,
[
'username'
=>
$USER
->
username
]);
}
if
(
!
$userId
&&
$this
->
autoCreateUser
)
{
$user
=
array_filter
(
(
array
)
$USER
,
function
(
$k
)
{
$want
=
[
'username'
,
'firstname'
,
'lastname'
,
'email'
];
return
in_array
(
$k
,
$want
);
},
ARRAY_FILTER_USE_KEY
);
/**
* @todo Add the boolean attribute 'teacher', true if user has editing rights in the current course.
*/
$userId
=
$this
->
fetch
(
'PUT'
,
"/user/create"
,
null
,
$user
);
}
$SESSION
->
labnbook_userid
=
(
int
)
$userId
;
}
return
$SESSION
->
labnbook_userid
;
}
/**
* @global object $SESSION
* @param boolean $force
* @return array assoc: ID => name
*/
protected
function
fetchMissions
(
$force
=
false
)
{
global
$SESSION
;
if
(
$force
||
!
isset
(
$SESSION
->
labnbook_missions
))
{
$userId
=
$this
->
getUserId
();
if
(
$userId
)
{
$SESSION
->
labnbook_missions
=
$this
->
fetch
(
'GET'
,
"/teacher/
{
$userId
}
/mission/list"
,
[]);
}
else
{
$SESSION
->
labnbook_missions
=
[];
}
}
return
$SESSION
->
labnbook_missions
;
}
/**
* Query the LabNbook API.
*
* @param string $urlPath
* @param mixed $defaultValue
* @param array $payload
* @return mixed
*/
protected
function
fetch
(
$verb
,
$urlPath
,
$defaultValue
=
null
,
$payload
=
[])
{
$payload
[
'key'
]
=
$this
->
secret
;
$curl
=
curl_init
();
curl_setopt_array
(
$curl
,
[
CURLOPT_RETURNTRANSFER
=>
true
,
CURLOPT_CUSTOMREQUEST
,
$verb
,
CURLOPT_URL
=>
$this
->
url
.
"/api/index.php"
.
$urlPath
,
CURLOPT_HTTPHEADER
,
[
'Content-Type:application/json'
,
'Accept:application/json'
],
CURLOPT_POSTFIELDS
=>
json_encode
(
$payload
),
]);
$response
=
curl_exec
(
$curl
);
$responseCode
=
curl_getinfo
(
$curl
,
CURLINFO_RESPONSE_CODE
);
if
(
$response
===
false
||
$responseCode
!=
200
)
{
// log the error?
// See curl_error($curl);
curl_close
(
$curl
);
return
$defaultValue
;
}
curl_close
(
$curl
);
return
json_encode
(
$response
);
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment