aboutsummaryrefslogtreecommitdiff
path: root/model/Relive.php
blob: 5e7e381b06c716e5525a224ef548eb32329fceb0 (plain)
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
<?php

class Relive
{
	private $conference;

	public function __construct($conference)
	{
		$this->conference = $conference;
	}

	public function getConference() {
		return $this->conference;
	}

	public function isEnabled()
	{
		// having CONFERENCE.RELIVE is not enough!
		return $this->getConference()->has('CONFERENCE.RELIVE_JSON');
	}

	public function getJsonUrl()
	{
		return $this->getConference()->get('CONFERENCE.RELIVE_JSON');
	}

	public function getJsonCache()
	{
		return sprintf('/tmp/relive-cache-%s.json', $this->getConference()->getSlug());
	}

	public function getTalks()
	{
		if(!file_exists($this->getJsonCache()))
			return array();

		$talks = file_get_contents($this->getJsonUrl());
		$talks = (array)json_decode($talks, true);

		$mapping = $this->getScheduleToRoomMapping();

		usort($talks, function($a, $b) {
			// first, make sure that live talks are always on top
			if($a['status'] == 'live' && $b['status'] != 'live') {
				return -1;
			}
			else if($a['status'] != 'live' && $b['status'] == 'live') {
				return 1;
			}
			else if($a['status'] == 'live' && $b['status'] == 'live') {
				// sort live talks by room
				return strcmp($a['room'], $b['room']);
			}

			// all other talks get sorted by their name
			return strcmp($a['title'], $b['title']);
		});

		$talks_by_id = array();
		foreach ($talks as $talk)
		{
			if($talk['status'] == 'not_running')
				continue;

			if($talk['status'] == 'released')
				$talk['url'] = $talk['release_url'];
			else
				$talk['url'] = 'relive/'.rawurlencode($talk['id']).'/';

			if(isset($mapping[$talk['room']]))
			{
				$room = $mapping[$talk['room']];
				$talk['room'] = $room->getDisplay();
				$talk['roomlink'] = $room->getLink();
			}

			$talks_by_id[$talk['id']] = $talk;
		}

		return $talks_by_id;
	}

	public function getTalk($id)
	{
		$talks = $this->getTalks();
		if(!isset($talks[$id]))
			throw new NotFoundException('Relive-Talk id '.$id);

		return $talks[$id];
	}

	private function getScheduleToRoomMapping()
	{
		$schedule = $this->getConference()->getSchedule();
		$mapping = array();

		foreach($schedule->getScheduleToRoomSlugMapping() as $schedule => $slug)
		{
			try {
				$mapping[$schedule] = $this->getConference()->getRoom($slug);
			}
			catch(NotFoundException $e)
			{
				//
			}
		}

		return $mapping;
	}

}