summaryrefslogtreecommitdiffstats
path: root/modules-available/locationinfo/inc/coursebackend/coursebackend_davinci.inc.php
blob: 6c6db23ca296d9c57b432302d613f4f038d1226a (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

class Coursebackend_Davinci extends CourseBackend
{

	private $location;

	public function setCredentials($json, $location, $serverID)
	{
		$this->location = $location . "/DAVINCIIS.dll?";
		$this->serverID = $serverID;
		//Davinci doesn't have credentials
		$this->checkConnection();
		return $this->error;
	}

	public function checkConnection()
	{
		if ($this->location != "") {
			$this->fetchSchedulesInternal('B206');
			return $this->error;
		}
		$this->error = false;
		$this->errormsg = "No url is given";
		return $this->error;
	}

	public function getCredentials()
	{
		$return = array();
		return $return;
	}

	public function getDisplayName()
	{
		return 'Davinci';
	}

	public function getCacheTime()
	{
		return 0;
	}

	public function getRefreshTime()
	{
		return 0;
	}

	private function toArray($response)
	{

		$cleanresponse = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);
		$xml = new SimpleXMLElement($cleanresponse);
		$array = json_decode(json_encode((array)$xml), true);
		return $array;
	}

	private function fetchArray($roomId)
	{
		$startDate = new DateTime('monday this week');
		$endDate = new DateTime('sunday');
		$url = $this->location . "content=xml&type=room&name=" . $roomId . "&startdate=" . $startDate->format('d.m.Y') . "&enddate=" . $endDate->format('d.m.Y');
		$ch = curl_init();
		$options = array(
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_FOLLOWLOCATION => true,
			CURLOPT_SSL_VERIFYHOST => false,
			CURLOPT_SSL_VERIFYPEER => false,
			CURLOPT_URL => $url,
		);

		curl_setopt_array($ch, $options);
		$output = curl_exec($ch);
		if ($output === false) {
			$this->error = true;
			$this->errormsg = 'Curl error: ' . curl_error($ch);
			return 'Curl error: ' . curl_error($ch);
		} else {
			$this->error = false;
			$this->errormsg = "";
			///Operation completed successfully
		}
		curl_close($ch);
		return $this->toArray($output);

	}

	public function fetchSchedulesInternal($roomIds)
	{
		$schedules = [];
		try {
			foreach ($roomIds as $sroomId) {
				$return = $this->fetchArray($sroomId);
				$lessons = $return['Lessons']['Lesson'];
				$timetable = [];
				foreach ($lessons as $lesson) {
					$date = $lesson['Date'];
					$date = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6, 2);
					$start = $lesson['Start'];
					$start = substr($start, 0, 2) . ':' . substr($start, 2, 2);
					$end = $lesson['Finish'];
					$end = substr($end, 0, 2) . ':' . substr($end, 2, 2);
					$subject = $lesson['Subject'];
					$json = array(
						'title' => $subject,
						'start' => $date . " " . $start . ':00',
						'end' => $date . " " . $end . ':00'
					);
					array_push($timetable, $json);
				}
				$timetable = json_encode($timetable);
				$schedules[$sroomId] = $timetable;
			}
		} catch (Exception $e) {
			$this->error = true;
			$this->errormsg = "url returns a wrong xml";
		}

		return $schedules;
	}
}