I need to be able to sort videos in views by viewcount, so I can make blocks with 'popular videos'. This data is saved within the field_video_video_data.

Currently I take the following approach:

1. Store viewcount in a separate column
- add a columns for viewcount using this approach: http://drupal.org/node/1681920

2. Update video data
It seems that the data is only updated when the node is saved.
Since viewcount will change over time I need some way to update this at least daily.
I can think of 2 different options:
- writing some custom code that uses cron.
- use the rules module to update the field

I am not an experienced php coder for drupal, so I will choose whatever works.
But any input/feedback/guidance on my approach is welcome.

Comments

workplaysleep’s picture

I wrote a simple module to daily update the data on all my nodes of the type video.
This together with the extra column for the viewcount works for me.
Probably not the most elegant code/solution but it works for me.

/**
	 * Implements hook_cron_queue_info()
	 */
	function updatevideodata_cron_queue_info() {
		// Create queue
		$queues['processVideoData'] = array(
			'worker callback' => 'processVideoDataQueue',
			'time' => 60,// This is the max run time per cron run in seconds.
		);
		return $queues;
	}

	/**
	 * Implements hook_cron()
	 */
	function updatevideodata_cron() {
		// execute once per day
		if (!executedToday()) {
			$my_queue = DrupalQueue::get('processVideoData');
			
			// Get nodes of type ''video'
			$query = new EntityFieldQuery;
			$query->entityCondition('entity_type', 'node')->entityCondition('bundle', 'video');
			$result = $query->execute();
		
			// $result['node'] contains a list of nids where the title matches
			if (!empty($result['node']) ) {
				// Add all nodes (nid only) to queue
				foreach($result['node'] as $item) {
					$my_queue->createItem($item->nid);
				}
			}
			
			setCheckedDateTime();
		}
	}

	/**
	 * Worker callback defined in hook_cron_queue_info()
	 */
	function processVideoDataQueue($nid) {
		$node = node_load($nid);
		// triggers presave 'video_embed_field_field_presave' in video_embed_field.field.inc
		field_attach_presave('node', $node);
		// update fields without changing node changedate/author
		field_attach_update('node', $node);
		  
		// log to watchdog
		$dt = variable_get('zplay_last_checked');
		watchdog('processVideoData',t('UPDATE nid:'.$nid.' date:'.$dt) );
	}
	
	
	/**
	 * setDateTime
	*/
	function setCheckedDateTime() {
		variable_set('zplay_last_checked', createCheckedDateTime());
	}
	
	/**
	 * executedToday
	*/
	function executedToday() {
		return (variable_get('zplay_last_checked') !== createCheckedDateTime() ? false : true);
	}
	
	/**
	 * createCheckedDateTime
	*/
	function createCheckedDateTime() {
		$today = getdate();
		//$dtString = $today['year'].$today['mon'].$today['mday'].'-'.$today['hours'].$today['minutes'].'-'.$today['seconds'];
		$dtString = $today['year'].$today['mon'].$today['mday'];
		return $dtString;
	}
workplaysleep’s picture

Issue summary: View changes

removed some unnecessary words