You are here

recommender.admin.inc in Recommender API 6.3

File

recommender.admin.inc
View source
<?php

function recommender_settings_form() {
  $form = array();
  $result = db_query('SELECT name, title, cron FROM {recommender_app} ORDER BY name');
  $rows = array();
  while ($row = db_fetch_object($result)) {
    $rows[] = $row;
  }
  if (count($rows) == 0) {
    $form['note'] = array(
      '#value' => t('No recommender modules available. Please install helper modules first. A list of helper modules can be found at !url', array(
        '!url' => l('http://drupal.org/project/recommender', 'http://drupal.org/project/recommender'),
      )),
    );
    return $form;
  }

  // the settings fieldset
  $form['settings'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Settings'),
    '#description' => t('Configure settings for Recommender API based modules.'),
  );
  $cron_options = array(
    'null' => t('Never'),
    '0' => t('Next cron'),
    '1800' => t('Every 30 minutes'),
    '3600' => t('Hourly'),
    '21600' => t('Every 6 hours'),
    '43200' => t('Every 12 hours'),
    '86400' => t('Daily'),
    '259200' => t('Every 3 days'),
    '604800' => t('Weekly'),
  );
  foreach ($rows as $row) {
    $rows[] = $row;
    $form['settings']['cron_freq'][$row->name] = array(
      '#title' => $row->title,
      '#description' => t('Please specify cron frequency to run this recommender'),
      '#type' => 'select',
      '#default_value' => $row->cron === NULL ? 'null' : $row->cron,
      '#options' => $cron_options,
    );
  }
  $form['settings']['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save settings'),
    '#name' => 'save',
  );

  // the "run" fieldset
  $form['run'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Run recommender'),
    '#description' => t('Compute recommendations could be time consuming. Please be patient. The actual computation happens on the recommender server. Refer to !doc for more details.', array(
      '!doc' => l('the latest README documentation', 'http://drupalcode.org/project/recommender.git/blob_plain/refs/heads/6.x-1.x:/README.html'),
    )),
  );
  $options = array();
  foreach ($rows as $row) {
    $options[$row->name] = $row->title;
  }
  $form['run']['apps'] = array(
    '#title' => t('Choose one or more recommender applications to run now:'),
    '#default_value' => variable_get('recommender_apps', array()),
    '#type' => 'checkboxes',
    //'#description' => t('Please select which recommender to run.'),
    '#options' => $options,
  );
  $form['run']['run'] = array(
    '#type' => 'submit',
    '#value' => t('Run recommender now'),
    '#name' => 'run',
  );

  // the history table.
  $recent_command = async_command_retrieve_command_list('recommender');
  $history_list = theme('async_command_list', array(
    'title' => t('Recent execution history'),
    'command_list' => $recent_command,
    'cols' => array(
      'uid',
      'command',
      'created',
      'changed',
      'status',
      'message',
    ),
  ));
  $form['history'] = array(
    '#value' => $history_list,
  );
  return $form;
}
function recommender_settings_form_submit($form, &$form_state) {

  // save settings
  if ($form_state['clicked_button']['#name'] == 'save') {
    $result = db_query("SELECT name FROM {recommender_app} ORDER BY name");
    while ($row = db_fetch_object($result)) {
      $name = $row->name;
      $cron = $form_state['values'][$name];
      if ($cron == 'null') {
        $cron = NULL;
      }
      db_query("UPDATE {recommender_app} SET cron=%d WHERE name='%s'", $cron, $name);
    }
    drupal_set_message(t("The settings have been saved."));
  }
  else {
    if ($form_state['clicked_button']['#name'] == 'run') {
      $apps = $form_state['values']['apps'];
      variable_set('recommender_apps', $apps);

      // trigger recommender_run()
      foreach ($apps as $app_name) {
        if (!empty($app_name)) {
          recommender_create_command($app_name);
        }
      }
    }
  }
}