This took a while to get working, but I got there eventually. It's used in OnTime to calculate the number of hours and minutes a user has clicked on. I have rows of horizontal lines which show hours. It's easy to extract a time from a click position, but generally people don't need that much precision and unless the grid is quite large (bigger than 1440 pixels), you won't have 1px per minute anyway.
This snippet will find the nearsest 30 minute interval, so if a user clicks ⅓ of the way between the 4th and 5th division, this would round to 4 hours and 30 minutes.
double clickPosition = ((double)e.Y + -this.AutoScrollPosition.Y) / heightPerHour;
int hours = (int)clickPosition;
int mins = (int)((clickPosition - (double)hours) * 60);
int roundedMinutes = (int)Math.Round((double)mins / 30, 0) * 30;
double fractionalHours = hours + (double)(roundedMinutes / 60.0);
This is designed for a control which autoscrolls, so the actual position of the click is determined first. heightPerHour is the height of each row (each hour).
I thought it would be nifty to show my last Twitter update as a profile field next to all of my posts in the Lime49 forums. It took a bit longer than expected, but eventually I got it working. These instructions are specific to Dreamhost, but you could change the slightly to work on other hosts.
-
You need to change your PHPBB template to include the status next to post matching a certain criteria. I'm the only administrator on the forum, so I set a template flag which is set for administrators, but no-one else.
-
Open viewtopic.php and add the template switch will determine whether the field will be shown. I used my User ID so it wouldn’t be shown for anyone else, but you could use another variable from PHPBB.
Find the section:
'S_CUSTOM_FIELDS' => (isset($cp_row['row']) && sizeof($cp_row['row'])) ? true : false,
'S_TOPIC_POSTER' => ($topic_data['topic_poster'] == $poster_id) ? true : false,
'S_IGNORE_POST' => ($row['hide_post']) ? true : false,
'L_IGNORE_POST' => ($row['hide_post']) ? sprintf($user->lang['POST_BY_FOE'], get_username_string('full', $poster_id, $row['username'], $row['user_colour'], $row['post_username']), '<a href="' . $viewtopic_url . "&p={$row['post_id']}&view=show#p{$row['post_id']}" . '">', '</a>') : '',
And at the end add:
'POSTER_ADMIN' => ($poster_id == 2),
-
Now open stylename\template\viewtopic_body.html and add the following to the top:
<!-- PHP -->
$twitterStatus = file_get_contents('/home/hjennerway/lime49.com/forums/twitter.php');
<!-- ENDPHP -->
-
Still in viewtopic_body.html, add this to the profile section (where the status message will be shown).:
<!-- IF postrow.POSTER_ADMIN -->
<br />
<dd><strong>Currently:</strong>
<span class="twitter"><!-- PHP -->echo $twitterStatus;<!-- ENDPHP --></span>
</dd>
<!-- ENDIF -->
-
Log in to the admin panel and purge the cache.
-
By default, PHP is not enabled in templates. You can enable it in the Security Settings section on the General tab.
-
Now you need a cronjob to connect to twitter, parse your last update and write it to a file. Create a new php file and modify this script to point to your PHPBB installation. Save it as twittercron.php
-
Add a line to crontab to run the script every six hours (or more, depending on how often you update your status). You'll need to change the path to the location where PHP is installed, which you can using which php.
* */6 * * * php5.cgi /path/to/twittercron.php 1>&2 &>/dev/null
This code saves your twitter status to a file every six hours. Them stores the contents of the file in a variable, which is shown in the template.