summaryrefslogtreecommitdiffstats
path: root/lib/class/democratic.class.php
blob: b4b632010c65b6b6b0232572ced24bac31f85b97 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
<?php
/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */
/*
 * Democratic Class
 *
 * PHP version 5
 *
 * LICENSE: GNU General Public License, version 2 (GPLv2)
 * Copyright (c) 2001 - 2011 Ampache.org All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License v2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * @category	Democratic
 * @package	Ampache
 * @author	Karl Vollmer <vollmer@ampache.org>
 * @copyright	2001 - 2011 Ampache.org
 * @license	http://opensource.org/licenses/gpl-2.0 GPLv2
 * @version	PHP 5.2
 * @link	http://www.ampache.org/
 * @since	File available since Release 1.0
 */

/**
 * Democratic Class
 *
 * This class handles democratic play, which is a fancy
 * name for voting based playback. This extends the tmpplaylist
 *
 * @category	Democratic
 * @package	Ampache
 * @author	Karl Vollmer <vollmer@ampache.org>
 * @copyright	2001 - 2011 Ampache.org
 * @license	http://opensource.org/licenses/gpl-2.0 GPLv2
 * @version	Release: 3.6
 * @link	http://www.ampache.org/
 * @since	Class available since Release 1.0
 */
class Democratic extends tmpPlaylist {

	public $name;
	public $cooldown;
	public $level;
	public $user;
	public $primary;
	public $base_playlist;

	// Build local, buy local
	public $tmp_playlist;
	public $object_ids = array();
	public $vote_ids = array();
	public $user_votes = array();

	/**
	 * constructor
	 * We need a constructor for this class. It does it's own thing now
	 */
	public function __construct($id='') {

		if (!$id) { return false; }

		$info = $this->get_info($id);

		foreach ($info as $key=>$value) {
			$this->$key = $value;
		}

	} // constructor

	/**
	 * build_vote_cache
	 * This builds a vote cache of the objects we've got in the playlist
	 */
	public static function build_vote_cache($ids) {

		if (!is_array($ids) OR !count($ids)) { return false; }

		$idlist = '(' . implode(',',$ids) . ')';

		$sql = "SELECT `object_id`,COUNT(`user`) AS `count` FROM user_vote WHERE `object_id` IN $idlist GROUP BY `object_id`";
		$db_results = Dba::read($sql);

		while ($row = Dba::fetch_assoc($db_results)) {
			parent::add_to_cache('democratic_vote',$row['object_id'],$row['count']);
		}

		return true;

	} // build_vote_cache

	/**
	 * is_enabled
	 * This function just returns true / false if the current democraitc playlist
	 * is currently enabled / configured
	 */
	public function is_enabled() {

		if ($this->tmp_playlist) { return true; }

		return false;

	} // is_enabled

	/**
	 * set_parent
	 * This returns the tmpPlaylist for this democratic play instance
	 */
	public function set_parent() {

		$demo_id = Dba::escape($this->id);

		$sql = "SELECT * FROM `tmp_playlist` WHERE `session`='$demo_id'";
		$db_results = Dba::read($sql);

		$row = Dba::fetch_assoc($db_results);

		$this->tmp_playlist = $row['id'];


	} // set_parent

	/**
	 * set_user_preferences
	 * This sets up a (or all) user(s) to use democratic play. This sets their play method
	 * and playlist method (clear on send) If no user is passed it does it for everyone and
	 * also locks down the ability to change to admins only
	 */
	public static function set_user_preferences($user=NULL) {

		//FIXME: Code in single user stuff

		$preference_id = Preference::id_from_name('play_type');
		Preference::update_level($preference_id,'75');
		Preference::update_all($preference_id,'democratic');

		$allow_demo = Preference::id_from_name('allow_democratic_playback');
		Preference::update_all($allow_demo,'1');

		$play_method = Preference::id_from_name('playlist_method');
		Preference::update_all($play_method,'clear');

		return true;

	} // set_user_preferences

	/**
	 * format
	 * This makes the objects variables all purrty so that they can be displayed
	 */
	public function format() {

		$this->f_cooldown	= $this->cooldown . ' ' . _('minutes');
		$this->f_primary	= $this->primary ? _('Primary') : '';

		switch ($this->level) {
			case '5':
				$this->f_level = _('Guest');
			break;
			case '25':
				$this->f_level = _('User');
			break;
			case '50':
				$this->f_level = _('Content Manager');
			break;
			case '75':
				$this->f_level = _('Catalog Manager');
			break;
			case '100':
				$this->f_level = _('Admin');
			break;
		}

	} // format

	/**
	 * get_playlists
	 * This returns all of the current valid 'Democratic' Playlists
	 * that have been created.
	 */
	public static function get_playlists() {

		// Pull all tmp playlsits with a session of < 0 (as those are fake)
		// This is kind of hackish, should really think about tweaking the db
		// and doing this right.
		$sql = "SELECT `id` FROM `democratic` ORDER BY `name`";
		$db_results = Dba::read($sql);

		$results = array();

		while ($row = Dba::fetch_assoc($db_results)) {
			$results[] = $row['id'];
		}

		return $results;

	} // get_playlists

	/**
	 * get_current_playlist
	 * This returns the curren users current playlist, or if specified
	 * this current playlist of the user
	 */
	public static function get_current_playlist() {

		$democratic_id = Config::get('democratic_id');

		if (!$democratic_id) {
			$level = Dba::escape($GLOBALS['user']->access);
			$sql = "SELECT `id` FROM `democratic` WHERE `level` <= '$level' " .
				" ORDER BY `level` DESC,`primary` DESC";
			$db_results = Dba::read($sql);
			$row = Dba::fetch_assoc($db_results);
			$democratic_id = $row['id'];
		}

		$object = new Democratic($democratic_id);

		return $object;

	} // get_current_playlist

	/**
	 * get_items
	 * This returns an array of all object_ids currently in this tmpPlaylist. This
	 * has gotten a little more complicated because of type, the values are an array
	 * 0 being ID 1 being TYPE
	 * FIXME: This is too complex, it makes my brain hurt
	 * [VOTE COUNT]
	 * 	[DATE OF NEWEST VOTE]
	 *		[ROW ID]
	 * 			[OBJECT_ID]
	 *			[OBJECT_TYPE]
	 *
	 * Sorting does the following
	 * sort largest VOTE COUNT to top
	 * sort smallest DATE OF NEWEST VOTE]
	 */
	public function get_items() {

		$order	  = "ORDER BY `user_vote`.`date` ASC, `tmp_playlist_data`.`track` ASC";
		$vote_join 	= "INNER JOIN `user_vote` ON `user_vote`.`object_id`=`tmp_playlist_data`.`id`";

		/* Select all objects from this playlist */
		$sql = "SELECT `user_vote`.`object_id` AS `vote_id`,`user_vote`.`user`,`tmp_playlist_data`.`id`,`tmp_playlist_data`.`object_type`, `user_vote`.`date`, `tmp_playlist_data`.`object_id` " .
			"FROM `tmp_playlist_data` $vote_join " .
			"WHERE `tmp_playlist_data`.`tmp_playlist`='" . Dba::escape($this->tmp_playlist) . "' $order";
		$db_results = Dba::read($sql);

		/* Define the array */
		$items = array();
		$votes = array();
		$object_ids = array();

		// Itterate and build the sortable array
		while ($results = Dba::fetch_assoc($db_results)) {

			// Extra set of data for caching!
			$this->object_ids[] = $results['object_id'];
			$this->vote_ids[] = $results['vote_id'];

			// First build a variable that holds the number of votes for an object
			$name		= 'vc_' . $results['object_id'];

			// Check if the vote is older then our current vote for this object
			if ($votes[$results['object_id']] < $results['date'] OR !isset($votes[$results['object_id']])) {
				$votes[$results['object_id']] = $results['date'];
			}


			// Append one to the vote
			${$name}++;
			$primary_key 	= ${$name};
			$secondary_key	= $votes[$results['object_id']];
			$items[$primary_key][$secondary_key][$results['id']] = array('object_id'=>$results['object_id'],'object_type'=>$results['object_type'],'id'=>$results['id']);
		} // gather data

		// Sort highest voted stuff to the top
		krsort($items);

		$sorted_items = array();

		// re-collapse the array
		foreach ($items as $vote_count=>$date_array) {
			ksort($date_array);
			foreach ($date_array as $object_array) {
				foreach ($object_array as $key=>$sorted_array) {
					$sorted_items[$key] = $sorted_array;
				}
			}
		}

		return $sorted_items;

	} // get_items

	/**
	 * play_url
	 * This returns the special play URL for democratic play, only open to ADMINs
	 */
	public function play_url() {

		$link = Stream::get_base_url() . 'uid=' . scrub_out($GLOBALS['user']->id) . '&demo_id=' . scrub_out($this->id);

		return $link;

	} // play_url

	/**
	 * get_next_object
	 * This returns the next object in the tmp_playlist most of the time this
	 * will just be the top entry, but if there is a base_playlist and no
	 * items in the playlist then it returns a random entry from the base_playlist
	 */
	public function get_next_object($offset='') {

		$offset = $offset ? intval($offset) : '0';

		// We have to get all because of the pysco sorting
		$items = self::get_items();

		if (count($items) > $offset) {
			$array = array_slice($items,$offset,1);
			$item = array_shift($array);
			$results['object_id'] = $item['object_id'];
		}

		/* If nothing was found and this is a voting playlist then get from base_playlist */
		if (!$results['object_id']) {

			/* Check for a playlist */
			if ($this->base_playlist) {
				/* We need to pull a random one from the base_playlist */
				$base_playlist = new Playlist($this->base_playlist);
				$data = $base_playlist->get_random_items(1);
				$results['object_id'] = $data['0']['object_id'];
			}
			else {
				$sql = "SELECT `id` as `object_id` FROM `song` WHERE `enabled`='1' ORDER BY RAND() LIMIT 1";
				$db_results = Dba::read($sql);
				$results = Dba::fetch_assoc($db_results);
			}
		}

		return $results['object_id'];

	} // get_next_object

	/**
	 * get_uid_from_object_id
	 * This takes an object_id and an object type and returns the ID for the row
	 */
	public function get_uid_from_object_id($object_id,$object_type='') {

		$object_id	= Dba::escape($object_id);
		$object_type	= $object_type ? Dba::escape($object_type) : 'song';
		$tmp_id		= Dba::escape($this->tmp_playlist);

		$sql = "SELECT `tmp_playlist_data`.`id` FROM `tmp_playlist_data` WHERE `object_type`='$object_type' AND " .
			"`tmp_playlist`='$tmp_id' AND `object_id`='$object_id'";
		$db_results = Dba::read($sql);

		$row = Dba::fetch_assoc($db_results);

		return $row['id'];

	} // get_uid_from_object_id

	/**
	 * get_cool_songs
	 * This returns all of the song_ids for songs that have happened within the last 'cooldown'
	 * for this user.
	 */
	public function get_cool_songs() {

		// Convert cooldown time to a timestamp in the past
		$cool_time = time() - ($this->cooldown * 60);

		$song_ids = Stats::get_object_history($GLOBALS['user']->id,$cool_time);

		return $song_ids;

	} // get_cool_songs

	/**
	 * vote
	 * This function is called by users to vote on a system wide playlist
	 * This adds the specified objects to the tmp_playlist and adds a 'vote'
	 * by this user, naturally it checks to make sure that the user hasn't
	 * already voted on any of these objects
	 */
	public function vote($items) {

		/* Itterate through the objects if no vote, add to playlist and vote */
		foreach ($items as $element) {
			$type = array_shift($element);
			$object_id = array_shift($element);
			if (!$this->has_vote($object_id,$type)) {
				$this->add_vote($object_id,$type);
			}
		} // end foreach

	} // vote

	/**
	 * has_vote
	 * This checks to see if the current user has already voted on this object
	 */
	public function has_vote($object_id,$type='') {

		$tmp_id		= Dba::escape($this->tmp_playlist);
		$object_id 	= Dba::escape($object_id);
		$type		= $type ? Dba::escape($type) : 'song';
		$user_id	= Dba::escape($GLOBALS['user']->id);

		/* Query vote table */
		$sql = "SELECT tmp_playlist_data.object_id FROM `user_vote` " .
			"INNER JOIN tmp_playlist_data ON tmp_playlist_data.id=user_vote.object_id " .
			"WHERE user_vote.user='$user_id' AND tmp_playlist_data.object_type='$type' " .
			"AND tmp_playlist_data.object_id='$object_id' " .
			"AND tmp_playlist_data.tmp_playlist='$tmp_id'";
		$db_results = Dba::read($sql);

		/* If we find  row, they've voted!! */
		if (Dba::num_rows($db_results)) {
			return true;
		}

		return false;

	} // has_vote

	/**
	 * add_vote
	 * This takes a object id and user and actually inserts the row
	 */
	public function add_vote($object_id,$object_type='') {

		$object_id	= Dba::escape($object_id);
		$tmp_playlist	= Dba::escape($this->tmp_playlist);
		$object_type	= $object_type ? Dba::escape($object_type) : 'song';
		$media = new $object_type($object_id);
		$track = isset($media->track) ? "'" . intval($media->track) . "'" : "NULL";

		/* If it's on the playlist just vote */
		$sql = "SELECT `id` FROM `tmp_playlist_data` " .
			"WHERE `tmp_playlist_data`.`object_id`='$object_id' AND `tmp_playlist_data`.`tmp_playlist`='$tmp_playlist'";
		$db_results = Dba::write($sql);

		/* If it's not there, add it and pull ID */
		if (!$results = Dba::fetch_assoc($db_results)) {
			$sql = "INSERT INTO `tmp_playlist_data` (`tmp_playlist`,`object_id`,`object_type`,`track`) " .
				"VALUES ('$tmp_playlist','$object_id','$object_type',$track)";
			$db_results = Dba::write($sql);
			$results['id'] = Dba::insert_id();
		}

		/* Vote! */
		$time = time();
		$sql = "INSERT INTO user_vote (`user`,`object_id`,`date`) " .
			"VALUES ('" . Dba::escape($GLOBALS['user']->id) . "','" . $results['id'] . "','$time')";
		$db_results = Dba::write($sql);

		return true;

	} // add_vote

	/**
	 * remove_vote
	 * This is called to remove a vote by a user for an object, it uses the object_id
	 * As that's what we'll have most the time, no need to check if they've got an existing
	 * vote for this, just remove anything that is there
	 */
	public function remove_vote($row_id) {

		$object_id	= Dba::escape($row_id);
		$user_id	= Dba::escape($GLOBALS['user']->id);

		$sql = "DELETE FROM `user_vote` WHERE `object_id`='$object_id' AND `user`='$user_id'";
		$db_results = Dba::write($sql);

		/* Clean up anything that has no votes */
		self::prune_tracks();

		return true;

	} // remove_vote

	/**
	 * delete_votes
	 * This removes the votes for the specified object on the current playlist
	 */
	public function delete_votes($row_id) {

		$row_id		= Dba::escape($row_id);

		$sql = "DELETE FROM `user_vote` WHERE `object_id`='$row_id'";
		$db_results = Dba::write($sql);

		$sql = "DELETE FROM `tmp_playlist_data` WHERE `id`='$row_id'";
		$db_results = Dba::write($sql);

		return true;

	} // delete_votes

	/**
	 * delete_from_oid
	 * This takes an OID and type and removes the object from the democratic playlist
	 */
	public function delete_from_oid($oid,$object_type) { 
		
		$row_id = $this->get_uid_from_object_id($oid,$object_type); 
		if ($row_id) { 
			debug_event('Democratic','Removing Votes for ' . $oid . ' of type ' . $object_type,'5'); 
			$this->delete_votes($row_id); 
		} 
		else { debug_event('Democratic','Unable to find Votes for ' . $oid . ' of type ' . $object_type,'3'); } 

		return true; 

	} // delete_from_oid 

	/**
	 * delete
	 * This deletes a democratic playlist
	 */
	public static function delete($democratic_id) {

		$democratic_id = Dba::escape($democratic_id);

		$sql = "DELETE FROM `democratic` WHERE `id`='$democratic_id'";
		$db_results = Dba::write($sql);

		$sql = "DELETE FROM `tmp_playlist` WHERE `session`='$democratic_id'";
		$db_results = Dba::write($sql);

		self::prune_tracks();

		return true;

	} // delete

	/**
	 * update
	 * This updates an existing democratic playlist item. It takes a key'd array just like the create
	 */
	public function update($data) {

		$name = Dba::escape($data['name']);
		$base = Dba::escape($data['democratic']);
		$cool = Dba::escape($data['cooldown']);
		$id = Dba::escape($this->id);

		$sql = "UPDATE `democratic` SET `name`='$name', `base_playlist`='$base',`cooldown`='$cool' WHERE `id`='$id'";
		$db_results = Dba::write($sql);

		return true;

	} // update

	/**
	 * create
	 * This is the democratic play create function it inserts this into the democratic table
	 */
	public static function create($data) {

		// Clean up the input
		$name 	= Dba::escape($data['name']);
		$base 	= Dba::escape($data['democratic']);
		$cool	= Dba::escape($data['cooldown']);
		$level	= Dba::escape($data['level']);
		$default = Dba::escape($data['make_default']);
		$user	= Dba::escape($GLOBALS['user']->id);

		$sql = "INSERT INTO `democratic` (`name`,`base_playlist`,`cooldown`,`level`,`user`,`primary`) " .
			"VALUES ('$name','$base','$cool','$level','$user','$default')";
		$db_results = Dba::write($sql);

		if ($db_results) {
			$insert_id = Dba::insert_id();
			parent::create(array(
				'session_id'  => $insert_id,
				'type'        => 'vote',
				'object_type' => 'song'
			));
		}

		return $db_results;

	} // create

	/**
	 * prune_tracks
	 * This replaces the normal prune tracks and correctly removes the votes
	 * as well
	 */
	public static function prune_tracks() {

		// This deletes data without votes, if it's a voting democratic playlist
		$sql = "DELETE FROM `tmp_playlist_data` USING `tmp_playlist_data` " .
			"LEFT JOIN `user_vote` ON `tmp_playlist_data`.`id`=`user_vote`.`object_id` " .
			"LEFT JOIN `tmp_playlist` ON `tmp_playlist`.`id`=`tmp_playlist_data`.`tmp_playlist` " .
			"WHERE `user_vote`.`object_id` IS NULL AND `tmp_playlist`.`type` = 'vote'";
		$db_results = Dba::write($sql);

		return true;

	} // prune_tracks

	/**
	 * clear
	 * This is really just a wrapper function, it clears the entire playlist
	 * including all votes etc.
	 */
	public function clear() {

		$tmp_id = Dba::escape($this->tmp_playlist);

		/* Clear all votes then prune */
		$sql = "DELETE FROM `user_vote` USING `user_vote` " .
			"LEFT JOIN `tmp_playlist_data` ON `user_vote`.`object_id` = `tmp_playlist_data`.`id` " .
			"WHERE `tmp_playlist_data`.`tmp_playlist`='$tmp_id'";
		$db_results = Dba::write($sql);

		// Prune!
		self::prune_tracks();

		// Clean the votes
		self::clear_votes();

		return true;

	} // clear_playlist

	/**
	 * clean_votes
	 * This removes in left over garbage in the votes table
	 */
	public function clear_votes() {

		$sql = "DELETE FROM `user_vote` USING `user_vote` " .
			"LEFT JOIN `tmp_playlist_data` ON `user_vote`.`object_id`=`tmp_playlist_data`.`id` " .
			"WHERE `tmp_playlist_data`.`id` IS NULL";
		$db_results = Dba::write($sql);

		return true;

	} // clear_votes

	/**
	 * get_vote
	 * This returns the current count for a specific song on this tmp_playlist
	 */
	public function get_vote($object_id) {

		if (parent::is_cached('democratic_vote',$object_id)) {
			return parent::get_from_cache('democratic_vote',$object_id);
		}

		$object_id = Dba::escape($object_id);

		$sql = "SELECT COUNT(`user`) AS `count` FROM user_vote " .
			"WHERE `object_id`='$object_id'";
		$db_results = Dba::read($sql);

		$results = Dba::fetch_assoc($db_results);

		return $results['count'];

	} // get_vote

	/**
	 * get_voters
	 * This returns the users that voted for the specified object
	 * This is an array of user ids
	 */
	public function get_voters($object_id) {

		return parent::get_from_cache('democratic_voters',$object_id);

	} // get_voters


} // Democratic class
?>