You are hereBlog / gianluca's blog / Codice sorgente degli "MP3 più ascoltati sul sito"
Codice sorgente degli "MP3 più ascoltati sul sito"

Per chi fosse interessato, questo è il codice del modulo da me realizzato che visualizza il blocco sulla destra "MP3 inseriti di recente".
<?php
define('GB_AUDIO_BLOCK_RANK_DEFAULT', 9999999);
/**
* Implementation of hook_block().
*/
function gb_audio_block_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Most listened MP3');
return $blocks;
case 'configure':
break;
case 'save':
break;
case 'view': default:
$block['title'] = t('Most listened MP3');
switch ($delta) {
case 0:
$block['content'] = _gb_audio_block_contents(10);
break;
}
return $block;
}
}
/**
* Implementation of hook_perm().
*/
function gb_audio_block_perm() {
return array('administer gb_audio_block');
}
function gb_audio_block_cron() {
set_time_limit(0);
$lastrun = variable_get('gb_audio_block_lastrun', 0);
$nextrun = $lastrun + 24 * 60 * 60 * variable_get('gb_audio_block_interval', 30);
if (time() > $nextrun) {
$sql = "SELECT nid FROM {audio} ORDER BY play_count + download_count DESC";
$res = db_query($sql);
$pos = 0;
while ($row = db_fetch_object($res)) {
$pos++;
$sql2 = "SELECT nid FROM {gb_audio_block} WHERE nid = %d";
$res2 = db_fetch_object(db_query($sql2, $row->nid));
if (is_null($res2->nid)) {
$sql3 = "INSERT INTO {gb_audio_block} (nid, curr_rank, prec_rank) VALUES(%d, %d, %d)";
$res3 = db_query($sql3, $row->nid, $pos, GB_AUDIO_BLOCK_RANK_DEFAULT);
} else {
$sql3 = "UPDATE {gb_audio_block} SET nid = %d, prec_rank = curr_rank, curr_rank = %d)";
$res3 = db_query($sql3, $row->nid, $pos);
}
}
$t = time();
variable_set('gb_audio_block_lastrun', $t);
}
}
function gb_audio_block_get_voto_star($voto) {
if (empty($voto)) return "";
global $base_url;
$img_path = $base_url . "/" . drupal_get_path('module', 'gb_audio_block')
.'/images';
$stelle = intval($voto / 20);
$mezza = $voto - ($stelle * 20);
$output = "<div class='container-inline'>";
for ($i = 0; $i < $stelle; $i++) {
$output .= "<img style='float: left;' src='$img_path/fullstar.png'>";
}
if ($mezza > 5) {
$output .= "<img style='float: left;' src='$img_path/halfstar.png'>";
}
$output .= "</div>";
return $output;
}
function _gb_audio_block_contents($limite) {
global $base_url;
$full_path = $base_url . "/" . drupal_get_path('module', 'gb_audio_block')
.'/gb_audio_block.css';
$img_path = $base_url . "/" . drupal_get_path('module', 'gb_audio_block')
.'/images';
drupal_add_css($full_path);
$sql = "SELECT t.value AS titolo, v.voto AS voto, art.value AS autore, a.nid AS nid,
gb.prec_rank AS prec_rank
FROM {audio} AS a LEFT JOIN
(SELECT content_id, value AS voto FROM {votingapi_cache}
WHERE function = 'average' GROUP BY content_id) AS v ON v.content_id = a.nid
, {node} as n
LEFT JOIN {gb_audio_block} AS gb ON n.nid = gb.nid, {audio_metadata} AS t,
{audio_metadata} AS art
WHERE n.vid = a.vid AND t.vid = a.vid AND t.tag = 'title'
AND art.vid = a.vid AND art.tag = 'artist'
ORDER BY play_count + download_count DESC
LIMIT 0, %d";
$res = db_query($sql, $limite);
$i = 0;
$rows = array();
while ($row = db_fetch_object($res)) {
$i++;
if (empty($row->prec_rank) ||
($row->prec_rank == GB_AUDIO_BLOCK_RANK_DEFAULT)) {
$img = "<em>n.e.</em>";
} else if ($i == $row->prec_rank) {
$img = "<img src='$img_path/stabile.gif'>";
} else if ($i > $row->prec_rank) {
$img = "<img src='$img_path/discesa.gif'><br>($row->prec_rank)";
} else if ($i < $row->prec_rank) {
$img = "<img src='$img_path/salita.gif'><br>($row->prec_rank)";
}
$rows[] = array("<b>$i</b><br>$img", l($row->titolo, "node/" . $row->nid) .
"<BR><EM>$row->autore</EM><br>" . gb_audio_block_get_voto_star($row->voto));
}
$output = theme('table', array(), $rows);
return $output;
}
?>




Ciao, dovresti allegare il file MYSQL, con le tabelle utilizzate
Ciao.
Con il nuovo "schema API" di Drupal 6 non è più necessario scrivere il DDL MySQL.
La struttura delle tabelle viene creata automaticamente in fase installazione da drupal in funzione della definizione fatta nell'hook "schema".
Di seguito il .install del mio modulo contenente la definizione dello schema:
<?php
function gb_audio_block_install() {
set_time_limit(0);
drupal_install_schema('gb_audio_block');
}
function gb_audio_block_schema() {
$schema['gb_audio_block'] = array(
'fields' => array(
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE),
'curr_rank' => array(
'type' => 'int',
'length' => 10,
'default' => 9999999,
'not null' => TRUE),
'prec_rank' => array(
'type' => 'int',
'length' => 10,
'default' => 9999999,
'not null' => TRUE),
),
'primary key' => array('nid'),
);
return $schema;
}
function gb_audio_block_uninstall() {
set_time_limit(0);
drupal_uninstall_schema('gb_audio_block');
variable_del('gb_audio_block_lastrun');
variable_del('gb_audio_block_interval');
}
?>
Ciao!
Invia nuovo commento