summaryrefslogtreecommitdiffstats
path: root/modules-available/locationinfo/inc/coursebackend/coursebackend_hisinone.inc.php
blob: 55d5ed4b00f89ae3640b69ab6b8ddfd22c2cac9e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php

class CourseBackend_HisInOne extends ICalCourseBackend
{

	public function setCredentialsInternal(array $data): bool
	{
		if (empty($data['baseUrl'])) {
			$this->addError("No url is given", true);
			return false;
		}

		$this->init($this->mangleProperty('baseUrl', $data['baseUrl']),
			$data['verifyCert'], $data['verifyHostname']);

		return true;
	}

	public function getCredentialDefinitions(): array
	{
		return [
			new BackendProperty('baseUrl', 'string'),
			new BackendProperty('verifyCert', 'bool', true),
			new BackendProperty('verifyHostname', 'bool', true)
		];
	}

	public function mangleProperty(string $prop, $value)
	{
		if ($prop === 'baseUrl') {
			// Update form SOAP to iCal url
			if (preg_match(',^(http.*?)/qisserver,', $value, $out)) {
				$value = $out[1] . '/qisserver/pages/cm/exa/timetable/roomScheduleCalendarExport.faces?roomId=%ID%';
			} elseif (preg_match(',(.*[/=])\d*$', $value, $out)) {
				$value = $out[1] . '%ID%';
			} elseif (substr_count($value, '/') <= 3) {
				if (substr($value, -1) !== '/') {
					$value .= '/';
				}
				$value .= 'qisserver/pages/cm/exa/timetable/roomScheduleCalendarExport.faces?roomId=%ID%';
			}
		}
		return $value;
	}

	protected function toTitle(ICalEvent $event): string
	{
		$title = parent::toTitle($event);
		// His in one seems to prefix *some* (but *not* all) of the lectures by their ID/("Nummer")
		// No clue what that format is supposed to be, this regex is some guesswork after observing this for a while
		return preg_replace('#^[0-9][0-9A-ZÄÖÜ]{3,9}-[A-Za-z0-9/_ÄÖÜäöüß.-]{4,30}\s+#u', '', $title);
	}

	public function checkConnection(): bool
	{
		if (!$this->isOK())
			return false;
		// Unfortunately HisInOne returns an internal server error if you pass an invalid roomId.
		// So we just try a bunch and see if anything works. Even if this fails, using
		// the backend should work, given the URL is actually correct.
		foreach ([60, 100, 5, 10, 50, 110, 200, 210, 250, 300, 333, 500, 1000, 2000] as $roomId) {
			if ($this->downloadIcal($roomId) !== null)
				return true;
		}
		return false;
	}

	public function getCacheTime(): int
	{
		return 30 * 60;
	}

	public function getRefreshTime(): int
	{
		return 60 * 60;
	}


	public function getDisplayName(): string
	{
		return "HisInOne";
	}

}