array( 'name' => t('Sondage'), 'module' => 'mod_sondage', 'description' => t("module gestion des sondages."), ) ); } /** * Definition des groupes pour les permissions */ function mod_sondage_perm() { return array('Admin sondage', 'participation sondage'); } /** * definition des accès lies au groupes */ function mod_sondage_access($op, $node) { global $user; if ($op == 'create') { // Only users with permission to do so may create this node type. return user_access('Admin sondage'); } // Users who create a node may edit or delete it later, assuming they have the // necessary permissions. if ($op == 'update' || $op == 'delete') { if (user_access('Admin sondage') && ($user->uid == $node->uid)) { return TRUE; } } } //************************************************************************************* //******************* MENU *********************************************** //************************************************************************************* function mod_sondage_menu($may_cache) { global $user; $items = array(); if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) != "edit") { $nid = arg(1); $node = node_load(arg(1)); if ($node->type == 'sondage') { $items[] = array( 'path' => 'node/'. arg(1) .'/'.arg(2), 'title' => t('vote'), 'callback' => 'mod_sondage_vote', 'callback arguments' => array($node, arg(2)), 'type' => MENU_CALLBACK ); } } if ($may_cache) { $items[] = array( 'path' => 'admin/frontpage/attribution', 'title' => t('Gestion Sondage'), 'callback' => 'drupal_get_form', 'callback arguments' => '_mod_sondage_form_attibution', 'access' => user_access('Admin sondage'), ); } if ($may_cache) { $items[] = array( 'path' => 'admin/export', 'title' => t('Export'), ); $items[] = array( 'path' => 'admin/export/sondage.csv', 'title' => t('Sondage'), 'callback' => 'drupal_get_form', 'callback arguments' => array('_mod_sondage_form_export'), 'access' => user_access('Admin sondage'), ); } return $items; } //************************************************************************************* //******************* API *********************************************** //************************************************************************************* function mod_sondage_nodeapi(&$node, $op, $teaser, $page) { if ($node->type == 'sondage') { switch ($op) { case 'load': $sondage = array(); $reponse = array(); $row = db_query("SELECT * FROM {sondage} WHERE nid=%d",$node->nid); $row2 = db_query("SELECT * FROM {reponse} WHERE nid=%d",$node->nid); $sondage = db_fetch_array($row); while ($data = db_fetch_object($row2)) { array_push($reponse, $data); } return array('sondage' => $sondage, 'reponse'=>$reponse ); break; case 'insert': // le node $sql_ins_sondage ="INSERT INTO {sondage} (sid, nid, name, description, question) VALUES (NULL, '%d', '%s', '%s', '%s')"; db_query($sql_ins_sondage, $node->nid, $node->title,$node->body, $node->sondage['question']); foreach ($node->reponse as $reponse => $value){ if (!empty($value)){ $sql_ins_reponse ="INSERT INTO {reponse} (rid, nid, reponse, nbvote) VALUES (NULL, '%d', '%s', '%d')"; db_query($sql_ins_reponse, $node->nid, $value,'0'); } } break; case 'update': // le node $sql_upd_sondage ="UPDATE {sondage} SET name='%s', description='%s', question='%s' WHERE nid='%d'"; db_query($sql_upd_sondage, $node->title,$node->body, $node->sondage['question'], $node->nid); foreach ($node->reponse as $reponse => $value){ if (!empty($reponse)){ $sql_upd_reponse ="UPDATE {reponse} SET reponse='%s' WHERE rid='%d'"; db_query($sql_upd_reponse, $value, $reponse); }else{ $sql_ins_reponse ="INSERT INTO {reponse} (rid, nid, reponse, nbvote) VALUES (NULL, '%d', '%s', '%d')"; db_query($sql_ins_reponse, $node->nid, $value,'0'); } } break; case 'delete': db_query('DELETE FROM {sondage} WHERE nid = %d', $node->nid); db_query('DELETE FROM {reponse} WHERE nid = %d', $node->nid); break; } } } /** * Gere l'affichage d'un sondage */ function mod_sondage_view($node, $teaser = FALSE, $page = FALSE) { for ($i='0'; $i < '4'; $i++){ if (!empty($node->reponse[$i]->reponse)) { $node->content['reponse'.$i] = array( '#value' => "".$node->reponse[$i]->reponse."
", '#weight' => 1, ); } } $node->content['bouton'] = array( '#value' => '', '#weight' => 2, ); return $node; } //************************************************************************************* //******************* FORM *********************************************** //************************************************************************************* function mod_sondage_form($node, $form_values=NULL) { $sondage_data=$node->sondage; $reponse_data=$node->reponse; $form['block'] = array( '#type' => 'fieldset', '#title' => 'Descriptif', '#weight' => -9 ); $form['block']['title'] = array( '#type' => 'textfield', '#title' => 'Titre', '#required' => TRUE, '#default_value' => $node->title, ); $form['block']['body'] = array( '#type' => 'textarea', '#title' => 'Description', '#default_value' => $node->body, '#rows' => 10, '#required' => FALSE ); $form['sondage']= array( '#type' => 'fieldset', '#title' => 'Sondage', '#tree' => TRUE, '#weight' => -1, '#collapsible' => TRUE, '#collapsed' => FALSE ); $form['sondage']['question'] = array( '#type' => 'textfield', '#title' => 'Question', '#required' => TRUE, '#default_value' => $sondage_data['question'], ); $form['reponse']= array( '#type' => 'fieldset', '#title' => 'Reponse', '#tree' => TRUE, '#weight' => 0, '#collapsible' => TRUE, '#collapsed' => FALSE ); if (isset($reponse_data)){ for ($i='0'; $i < '4'; $i++){ $title = 1 + $i; $form['reponse'][$reponse_data[$i]->rid] = array( '#type' => 'textfield', '#title' => 'reponse '.$title, '#required' => ($title == '1') ? TRUE : FALSE, '#default_value' => $reponse_data[$i]->reponse, ); } }else{ for ($i='1'; $i < '5'; $i++){ $form['reponse']['reponse_'.$i] = array( '#type' => 'textfield', '#title' => 'reponse '.$i, '#required' => ($i == '1') ? TRUE : FALSE, '#default_value' => '', ); } } return $form; } /** * Traitement d'un vote */ function mod_sondage_vote($node, $id) { $nid = $_POST['id']; $rid = $_POST['vote']; if (isset($_POST['vote']) && isset($_POST["submit"])){ return theme('sondage_captcha', $nid, $rid); } if (isset($_POST["resultat"])) { $node = node_load($nid); return theme('sondage_result', $node); } if (isset($_COOKIE['captcha-control'], $_POST['captcha'])) { if($_COOKIE['captcha-control'] == $_POST['captcha']) { _mod_sondage_increase_vote($rid); ob_start(); setcookie("id[$nid]", $nid, time()+86400, "/"); /* expire dans une journée */ ob_end_clean(); drupal_goto("/node/".$nid); }else{ $erreur = 'Invalide
'; return theme('sondage_captcha', $nid, $rid, $erreur); } } } //************************************************************************************* //******************* BLOC *********************************************** //************************************************************************************* function mod_sondage_block($op = 'list', $delta = 0, $edit = array()) { switch($op) { case 'list': $blocks[0]['info'] = t('Sondage Attribution'); return $blocks; case 'configure': $form = array(); return $form; case 'view': default: $block['content'] = mod_sondage_contents(); return $block; } } /** * Creation du contenu du block */ function mod_sondage_contents(){ if ( arg(0) == 'taxonomy' && arg(1) == 'term' ) { $tid=arg(2); } elseif( arg(0) == 'all') { if (arg(1)) { $tid=arg(1); } else { $tid = NULL ; } } elseif( (arg(0) == 'node') && (is_numeric(arg(1))) ) { $term=taxonomy_node_get_terms_by_vocabulary(arg(1),1); if(is_array($term)) { $term=current($term); $tid=$term->tid; } else { $tid=$term->tid; } } else { $tid = NULL ; } $mod_sondage_attribution = unserialize(variable_get('mod_sondage_attribution','')); if(is_numeric($tid)){ $nid = $mod_sondage_attribution['blocksondage_'.$tid]; return theme('sondage_block', $nid); }else{ $nid = $mod_sondage_attribution['blockhome']; return theme('sondage_block_home', $nid); } } /** * Inclus les fonctions annexe, et les themes */ require_once(drupal_get_path('module','mod_sondage'). '/mod_sondage_fonctions_annexe.inc'); include(drupal_get_path('module','mod_sondage'). '/mod_sondage_theme.inc.php');