Skip to content

Commit a3a03ba

Browse files
Add singleton provider for evasys soap client
1 parent 69018a4 commit a3a03ba

File tree

3 files changed

+58
-35
lines changed

3 files changed

+58
-35
lines changed

classes/evasys_inviter.php

+1-17
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class evasys_inviter {
3232
private $soapclient;
3333

3434
private function __construct() {
35-
$this->soapclient = $this->init_soap_client();
35+
$this->soapclient = evasys_soap_client::get();
3636
}
3737

3838
public static function get_instance() {
@@ -259,22 +259,6 @@ public static function get_enrolled_student_email_adresses_from_usernames($moodl
259259
return $emailadresses;
260260
}
261261

262-
public function init_soap_client() {
263-
$soapclient = new \SoapClient(get_config('block_evasys_sync', 'evasys_wsdl_url'), [
264-
'trace' => 1,
265-
'exceptions' => 0,
266-
'location' => get_config('block_evasys_sync', 'evasys_soap_url')
267-
]);
268-
269-
$headerbody = new \SoapVar([
270-
new \SoapVar(get_config('block_evasys_sync', 'evasys_username'), XSD_STRING, null, null, 'Login', null),
271-
new \SoapVar(get_config('block_evasys_sync', 'evasys_password'), XSD_STRING, null, null, 'Password', null),
272-
], SOAP_ENC_OBJECT);
273-
$header = new \SOAPHEADER('soap', 'Header', $headerbody);
274-
$soapclient->__setSoapHeaders($header);
275-
return $soapclient;
276-
}
277-
278262
public function set_close_task($surveyid) {
279263
$time = new \DateTime();
280264
$time->add(new \DateInterval("PT10S"));

classes/evasys_soap_client.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
// This file is part of the Moodle plugin block_evasys_sync
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace block_evasys_sync;
18+
19+
defined('MOODLE_INTERNAL') || die();
20+
21+
/**
22+
* Singleton for evasys soap client.
23+
* @package block_evasys_sync
24+
* @copyright 2022 Justus Dieckmann WWU
25+
*/
26+
class evasys_soap_client {
27+
28+
private static $instance = null;
29+
30+
public static function get(): \SoapClient {
31+
if (!self::$instance) {
32+
self::$instance = self::create_soap_client();
33+
}
34+
return self::$instance;
35+
}
36+
37+
private static function create_soap_client(): \SoapClient {
38+
$soapclient = new \SoapClient(get_config('block_evasys_sync', 'evasys_wsdl_url'), [
39+
'trace' => 1,
40+
'exceptions' => 0,
41+
'location' => get_config('block_evasys_sync', 'evasys_soap_url')
42+
]);
43+
44+
$headerbody = new \SoapVar([
45+
new \SoapVar(get_config('block_evasys_sync', 'evasys_username'), XSD_STRING, null, null, 'Login', null),
46+
new \SoapVar(get_config('block_evasys_sync', 'evasys_password'), XSD_STRING, null, null, 'Password', null),
47+
], SOAP_ENC_OBJECT);
48+
$header = new \SOAPHEADER('soap', 'Header', $headerbody);
49+
$soapclient->__setSoapHeaders($header);
50+
return $soapclient;
51+
}
52+
53+
}

classes/evasys_synchronizer.php

+4-18
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class evasys_synchronizer {
3030

3131
public function __construct($courseid) {
3232
$this->courseid = $courseid;
33-
$this->init_soap_client();
33+
$this->soapclient = evasys_soap_client::get();
3434
$this->blockcontext = \context_course::instance($courseid); // TODO Course context or block context? Check caps.
3535
$this->courseinformation = $this->get_course_information();
3636
}
@@ -67,21 +67,6 @@ public function get_allocated_courses() {
6767
return $this->evasyscourses;
6868
}
6969

70-
private function init_soap_client() {
71-
$this->soapclient = new \SoapClient(get_config('block_evasys_sync', 'evasys_wsdl_url'), [
72-
'trace' => 1,
73-
'exceptions' => 0,
74-
'location' => get_config('block_evasys_sync', 'evasys_soap_url')
75-
]);
76-
77-
$headerbody = new \SoapVar([
78-
new \SoapVar(get_config('block_evasys_sync', 'evasys_username'), XSD_STRING, null, null, 'Login', null),
79-
new \SoapVar(get_config('block_evasys_sync', 'evasys_password'), XSD_STRING, null, null, 'Password', null),
80-
], SOAP_ENC_OBJECT);
81-
$header = new \SOAPHEADER('soap', 'Header', $headerbody);
82-
$this->soapclient->__setSoapHeaders($header);
83-
}
84-
8570
private function get_course_information() {
8671
$result = [];
8772
foreach ($this->get_allocated_courses() as $course) {
@@ -101,7 +86,7 @@ private function get_course_information() {
10186
* @return array of surveys with additional information
10287
*/
10388
public function get_surveys($courseid) {
104-
if ($this->courseinformation[$courseid] === null) {
89+
if (!isset($this->courseinformation[$courseid]) || $this->courseinformation[$courseid] === null) {
10590
return array();
10691
}
10792
if (!isset($this->courseinformation[$courseid]->m_oSurveyHolder->m_aSurveys->Surveys)) {
@@ -190,7 +175,8 @@ private function get_form_name($formid) {
190175
}
191176

192177
public function get_amount_participants($courseid) {
193-
if ($this->courseinformation[$courseid] === null || !property_exists($this->courseinformation[$courseid]->m_aoParticipants, "Persons")) {
178+
if (!isset($this->courseinformation[$courseid]) || $this->courseinformation[$courseid] === null
179+
|| !property_exists($this->courseinformation[$courseid]->m_aoParticipants, "Persons")) {
194180
return 0;
195181
}
196182
if (is_object($this->courseinformation[$courseid]->m_aoParticipants->Persons)) {

0 commit comments

Comments
 (0)