View source
<?php
class quiz_views_handler_argument_quiz_nid extends views_handler_argument_numeric {
var $corresponding_vids = array();
var $phrase_broken = FALSE;
function construct() {
parent::construct();
$this->nid_field = $this->definition['nid field'];
}
function option_definition() {
$options = parent::option_definition();
$options['which_vid'] = array(
'default' => 'latest',
);
return $options;
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['title']['#weight'] = -2;
$form['which_vid'] = array(
'#type' => 'select',
'#title' => t('Quiz revision(s) to use'),
'#options' => array(
'latest' => t('Latest: most recent version of the quiz ONLY.'),
'initial' => t('Initial: original version of the quiz ONLY.'),
'all' => t('All: ALL versions of the quiz.'),
),
'#description' => t('The validator will transform the incoming quiz node id(s) into one or more quiz node revisions, depending on your selection.'),
'#default_value' => $this->options['which_vid'],
'#weight' => -1,
);
}
function title_query() {
$titles = array();
$placeholders = implode(', ', array_fill(0, count($this->corresponding_vids), '%d'));
$result = db_query("SELECT n.title FROM {node_revisions} n WHERE n.vid IN ({$placeholders})", $this->corresponding_vids);
while ($term = db_fetch_object($result)) {
$titles[] = check_plain($term->title);
}
return $titles;
}
function query() {
$this
->ensure_my_table();
$this
->break_phrase();
if ($this
->subselect_vids()) {
$placeholders = implode(', ', array_fill(0, count($this->corresponding_vids), '%d'));
$this->query
->add_where(0, "{$this->table_alias}.{$this->nid_field} IN ({$placeholders})", $this->corresponding_vids);
}
else {
$this->query
->add_where(0, "{$this->table_alias}.{$this->nid_field} = %d", $this->corresponding_vids);
}
}
function set_argument($arg) {
$this->argument = $arg;
if ($this
->validate_arg($arg)) {
$this
->break_phrase();
$this
->subselect_vids();
return TRUE;
}
return FALSE;
}
function break_phrase() {
if (!$this->phrase_broken) {
if (!empty($this->options['break_phrase'])) {
views_break_phrase($this->argument, $this);
}
else {
$this->value = array(
$this->argument,
);
}
$this->phrase_broken = TRUE;
}
}
function subselect_vids() {
$from = 'FROM {quiz_node_properties} qnp';
$field = 'qnp.vid';
if (count($this->value) > 1) {
$use_group_by = TRUE;
$operator = empty($this->options['not']) ? 'IN' : 'NOT IN';
$placeholders = implode(', ', array_fill(0, count($this->value), '%d'));
$where = "WHERE qnp.nid {$operator} ({$placeholders})";
}
else {
$use_group_by = !empty($this->options['not']);
$operator = empty($this->options['not']) ? '=' : '!=';
$where = "WHERE qnp.nid {$operator} %d";
}
switch ($this->options['which_vid']) {
case 'initial':
case 'latest':
$operation = $this->options['which_vid'] == 'initial' ? 'MIN' : 'MAX';
$where .= $use_group_by ? ' GROUP BY qnp.nid' : '';
$result = db_query("SELECT {$operation}(qnp.vid) AS vid {$from} {$where}", $this->value);
break;
case 'all':
$result = db_query("SELECT qnp.vid AS vid {$from} {$where}", $this->value);
break;
}
$this->corresponding_vids = array();
while ($item = db_fetch_object($result)) {
$this->corresponding_vids[] = $item->vid;
}
return count($this->corresponding_vids) > 1;
}
}