CVS edit link for tstoeckler

I have written a simple utility module which provides a block showing the current time. The time zone and date formats (it requires Date API and Date Timezone (http://drupal.org/project/date)) are configurable through the block configure UI.
I am aware of the World Clock module (http://drupal.org/project/worldclock) but have personally come to the conclusion that that module solves a different use-case, therefore I did not try to contribute to World Clock but wrote my own module. Even though a super-abstracted Clock module is theoretically possible, I find a simple block showing the time is a common enough use-case to provide a drop-in-and-it-works solution for.
The module is fully functional at this stage, though if this gets accepted plans include SimpleTest coverage and some JS-magic.

Comments

tstoeckler’s picture

StatusFileSize
new51.91 KB
new2.55 KB

Here is the module and a screenshot of the block configure screen.

tstoeckler’s picture

StatusFileSize
new1.56 KB

How did that file sneak in there...

sun’s picture

Status: Postponed (maintainer needs more info) » Needs work

Overall, this module almost looks too small to deserve an own project.

However, considering that this is a trivial use-case and there may be demand for it, this looks ok - although there are some localization issues with strings in t().

The block rendering function clock_content() should be a theme function and get the display settings already prepared as settings array.

Additionally, I expected some jQuery/JavaScript that updates the clock on the page (optionally / configurable). Probably a good target for future development.

Another possible enhancement in the future would be to support clock "widgets", i.e. JavaScript/Canvas/Flash-driven graphical elements (instead of text).

I'd also like to see an open communication and collaboration with the wordclock module maintainer(s).

Furthermore, I'd be interested to see a kind of "mini-widget" that could be exposed to Administration menu (admin_menu), so one could display a small clock in the menu.

sun’s picture

Also, there are plenty of coding style and coding standards issues. Please run this code through Coder and alternatively the coder_format script.

tstoeckler’s picture

StatusFileSize
new2.69 KB

Thanks for the quick review!!!

1. t(): I had thought it would be better to wrap the whole thing, including the variables in one t(), but then coder threw up some errors, so I fixed that. Will change depending on what's the right way. Will have a look at some localization documentation, which I admittedly haven't done yet.

2. theme function: Have no clue how to do this, will look into some docs.

3. jQuery to update the clock: Definitely a must have, I thought I would get by without it initially :)...

4. Graphical widgets: See 2. I like the idea, though.

5. Contact the world clock maintainers: Will have a look at exactly what the World clock module does behind the scenes and then submit an issue there.

6. admin_menu integration: See 2. If I've gotten this far, though, should be doable...

By the way, in the version I uploaded, the if statements are constructed wrong, so the forms don't show properly depending on the settings.
Fixed that. Uploading simply for sake of completeness.

So I've got enough to do now, so, depending on the outcome of 5., either see you in 2 months or bye bye!

tstoeckler’s picture

Oh, also:
I ran this code through Coder (and got no errors...), so if you can tell me how you got those errors, that'd be great.
Thanks.

[edit: forget about it. found the coder_format script (great stuff!!!) and applied it. I think that's what you meant. I didn't attach a new version, I'll just remember to run that script beforehand next time.]

avpaderno’s picture

  1.             'short' => t('Short date format') . ' (' . $short_date_format_formatted . ')',
                'medium' => t('Medium date format') . ' (' . $medium_date_format_formatted . ')',
                'long' => t('Long date format') . ' (' . $long_date_format_formatted . ')',
                'custom' => t('Custom date format'),
    

    Use a placeholder.

  2.   if ($clock_timezone_type == 'site') {
        $clock_timezone = variable_get('date_default_timezone_name', 'UTC');
      }
      if ($clock_timezone_type == 'user') {
        $clock_timezone = NULL;
      }
      if ($clock_timezone_type == 'custom') {
        $clock_timezone = variable_get('clock_custom_timezone', 'UTC');
      }
    

    This is a minor optimization, but the code should be rewritten, as one value excludes the other.
    It would also be better to use integers for that setting, as the setting values are part of a set.

What reported by sun about the theme function is still true.

avpaderno’s picture

Issue tags: +Module review
sun’s picture

@tstoeckler: I know that you are an active member of the Drupal community... can I trust you and we simply move forward here or do you want to be on the safe side of life and go with us through this process to make sure your CVS stuff has a value for users in the end? :)

tstoeckler’s picture

@sun: I like being trusted. Thanks.

Actually I was just about to write a little note, in which I would have proposed to not make this issue reliant on a possible collaboration with the Worldclock module, as when I was looking into that module, I realized, quite simply, that Worldclock, to deliver its specific use-case needs to be able to set up multiple times in one block, which is not feasible with just the block-config UI; on the other hand, not using the block config UI for the simple module I wrote but inventing an own one, defeats the whole purpose of being a "simple" utility module. Good that I don't have to make that argument now :)

Also: Done 1. Will start 2 today (and hopefully also finish 2 today) and then prepare for becoming a jQuery-master in 3. and 4. which will probably take me a while, though.

tstoeckler’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new1.76 KB

While this is still pending: Attaching a new version.

1. Done. I used the placeholders in the t() function.

2. I implemented a theme function. It was the first time I did this, so let me know if I did something wrong. It does still work, though, I tested that. While I was at it, I also surrounded the clock with a span class="clock".

From #7:

This is a minor optimization, but the code should be rewritten, as one value excludes the other.

I changed that into a switch statement, which makes it look much nicer. Is that what you meant?

Also changing status, as per #9 ?!

avpaderno’s picture

Status: Reviewed & tested by the community » Needs review

@tstoeckler: The applicant is not surely the person who can change the status to reviewed & tested by the community.

I don't see anything wrong in the code, except I don't understand why it loads data from the other module database table, when it could use date_api_date_formats(); there are date formats that are not found in the database.

tstoeckler’s picture

Re #12.
Sorry, I suppose I misinterpreted #9.

I didn't use date_api_date_formats() because I did not see a way to get only the custom formats. I do not want to get all formats, because I want to display the preferred "short", "medium" and "long" formats the user has set on /admin/settings/date-time. Otherwise those settings are kind of worthless.

there are date formats that are not found in the database

Really? Could you elaborate on that? Because if I use Date API's nice admin UI for custom date formats, they all show up nicely in the {date_formats} table.

Thanks for being so perseverent, I'm still learning all these things...

sun’s picture

Status: Needs review » Fixed
function clock_theme() {
  return array(
    'clock' => array(
      'arguments' => array(
        'clock_timezone' => 'UTC',
        'clock_date_format_type' => 'short',
        'clock_custom_date_format' => 'g:i a',
      ),
...
function theme_clock($timezone = 'UTC', $format = 'g:i a') {

Those theme function arguments should match. The keys specified in the registry should map to the function argument variable names. Furthermore, I think (!) you do not need to specify default values, because theme() will already assign them according to the theme registry. (However, it's possible that this is a D7-only feature, so please test...)

  switch ($clock_timezone_type) {
    // Site time zone
    case '1':
      $clock_timezone = variable_get('date_default_timezone_name', 'UTC');
    // User time zone
    case '2':
      $clock_timezone = NULL;
    // Custom time zone
    case '3':
      $clock_timezone = variable_get('clock_custom_timezone', 'UTC');
  }

Those cases are missing break; statements - which means that the last ('3') will always be in effect.

  $output .= '<span class="clock">' . $clock_formatted . '</span>';

You likely want to convert this into a DIV.

function clock_block($op = 'list', $delta = 0, $edit = array()) {
    case 'view':
      $block['subject'] = 'Clock';
      $block['content'] = clock_content();
      return $block;
  }
}

/*
 * Return the content of the clock block.
 */
function clock_content() {
  return theme_clock($clock_timezone, $clock_date_format_type, $clock_custom_date_format);
}

You can directly invoke the theme function from hook_block().

However, the rest of the code looks just fine, and we are not really here to teach best practice coding, just prevent horrible code from hitting CVS contributions. Thus, I'll move forward and mark this fixed.

tstoeckler’s picture

Thanks.

Now let's get this baby into CVS!

Status: Fixed » Closed (fixed)
Issue tags: -Module review

Automatically closed -- issue fixed for 2 weeks with no activity.

avpaderno’s picture

Component: Miscellaneous » new project application
Issue summary: View changes
Status: Closed (fixed) » Fixed

I am giving credits to the users who participated in this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.