| 1 |
<?php
|
| 2 |
|
| 3 |
// Copyright 2005 http://2bits.com
|
| 4 |
|
| 5 |
// Profile fields to be included in the email
|
| 6 |
function _get_profile_fields() {
|
| 7 |
return array(
|
| 8 |
'profile_last_name',
|
| 9 |
'profile_first_name',
|
| 10 |
'profile_city',
|
| 11 |
'profile_state');
|
| 12 |
}
|
| 13 |
|
| 14 |
function topic_help($section) {
|
| 15 |
switch ($section) {
|
| 16 |
case 'admin/modules#description':
|
| 17 |
return t('Allows posting of topic and gathering answers to them.');
|
| 18 |
case 'node/add#topic':
|
| 19 |
return t('Allows posting of topic and gathering answers to them.');
|
| 20 |
}
|
| 21 |
}
|
| 22 |
|
| 23 |
function topic_node_name($node) {
|
| 24 |
return t('topic');
|
| 25 |
}
|
| 26 |
|
| 27 |
function topic_access($op, $node) {
|
| 28 |
global $user;
|
| 29 |
|
| 30 |
switch($op) {
|
| 31 |
case 'view':
|
| 32 |
if (user_access('edit own topic') && ($user->uid == $node->uid)) {
|
| 33 |
$access = true;
|
| 34 |
}
|
| 35 |
else {
|
| 36 |
$access = user_access('view topic');
|
| 37 |
}
|
| 38 |
break;
|
| 39 |
|
| 40 |
case 'create':
|
| 41 |
$access = user_access('create topic');
|
| 42 |
break;
|
| 43 |
|
| 44 |
case 'update':
|
| 45 |
case 'delete':
|
| 46 |
if (user_access('edit own topic') && ($user->uid == $node->uid)) {
|
| 47 |
$access = true;
|
| 48 |
}
|
| 49 |
break;
|
| 50 |
}
|
| 51 |
return $access;
|
| 52 |
}
|
| 53 |
|
| 54 |
function topic_perm() {
|
| 55 |
return array('create topic', 'edit own topic', 'view topic', 'create answer');
|
| 56 |
}
|
| 57 |
|
| 58 |
function topic_link($type, $node = 0, $main) {
|
| 59 |
$links = array();
|
| 60 |
|
| 61 |
if ($type == 'node' && $node->type == 'topic') {
|
| 62 |
if (user_access('create answer')) {
|
| 63 |
if ($node->answer_status) {
|
| 64 |
if (!_user_answered($node->nid)) {
|
| 65 |
$links[] = l(t('answer this topic'), 'topic/' . $node->nid . '/answer');
|
| 66 |
}
|
| 67 |
else {
|
| 68 |
$links[] = l(t('edit your answers'), 'topic/' . $node->nid . '/answer');
|
| 69 |
}
|
| 70 |
}
|
| 71 |
}
|
| 72 |
else {
|
| 73 |
$links[] = l(t('login'), 'user/login') . ' or ' .
|
| 74 |
l(t('register'), 'user/register') . ' to answer';
|
| 75 |
}
|
| 76 |
}
|
| 77 |
|
| 78 |
return $links;
|
| 79 |
}
|
| 80 |
|
| 81 |
function topic_menu($may_cache) {
|
| 82 |
$items = array();
|
| 83 |
if (is_numeric(arg(1))) {
|
| 84 |
$nid = arg(1);
|
| 85 |
}
|
| 86 |
|
| 87 |
$items[] = array(
|
| 88 |
'path' => 'node/add/topic',
|
| 89 |
'title' => t('topic'),
|
| 90 |
'access' => user_access('create topic')
|
| 91 |
);
|
| 92 |
|
| 93 |
$items[] = array(
|
| 94 |
'path' => "topic/$nid/answer",
|
| 95 |
'title' => t('topic answer'),
|
| 96 |
'callback' => 'topic_answer',
|
| 97 |
'access' => user_access('create answer'),
|
| 98 |
'type' => MENU_CALLBACK
|
| 99 |
);
|
| 100 |
|
| 101 |
return $items;
|
| 102 |
}
|
| 103 |
|
| 104 |
function topic_form(&$node) {
|
| 105 |
// In order to be able to attach taxonomy terms to this node, we need
|
| 106 |
// to display the appropriate form elements.
|
| 107 |
if (function_exists('taxonomy_node_form')) {
|
| 108 |
$output .= implode('', taxonomy_node_form('topic', $node));
|
| 109 |
}
|
| 110 |
|
| 111 |
// Now we define the form elements specific to our node type.
|
| 112 |
$output .= form_textarea(t('Body'), 'body', $node->body, 60, 20);
|
| 113 |
|
| 114 |
$output .= form_select(t('Number of allowed answers'), 'num_answers',
|
| 115 |
$node->num_answers,drupal_map_assoc(array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)),
|
| 116 |
t('Number of answers to display for users for this topic.'),
|
| 117 |
$extra = 0, $multiple = FALSE, $required = TRUE);
|
| 118 |
|
| 119 |
$output .= form_select(t('Number of Days available'), 'num_days',
|
| 120 |
$node->num_days, drupal_map_assoc(array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,30,60,90)),
|
| 121 |
t('Number of days users are allowed to submit answers to this topic, counting from the node creation date.'),
|
| 122 |
$extra = 0, $multiple = FALSE, $required = TRUE);
|
| 123 |
|
| 124 |
$output .= form_radios(t('Submit Answers'), 'answer_status',
|
| 125 |
$node->answer_status, array(t('Disabled'), t('Enabled')));
|
| 126 |
|
| 127 |
$output .= filter_form('format', $node->format);
|
| 128 |
|
| 129 |
return $output;
|
| 130 |
}
|
| 131 |
|
| 132 |
function topic_answer() {
|
| 133 |
global $user;
|
| 134 |
$nid = (int)arg(1);
|
| 135 |
$edit = $_POST['edit'];
|
| 136 |
$topic = _topic_load($nid);
|
| 137 |
|
| 138 |
if (!$topic->answer_status) {
|
| 139 |
drupal_set_message('This topic has already been closed.');
|
| 140 |
drupal_goto("node/$nid");
|
| 141 |
}
|
| 142 |
|
| 143 |
$node = node_load(array('nid' => $topic->nid));
|
| 144 |
$output .= "<strong>$node->title</strong><br/>";
|
| 145 |
$output .= "<br/>$node->body<br/>";
|
| 146 |
|
| 147 |
if ($edit) {
|
| 148 |
$answered = _user_answered($nid);
|
| 149 |
// Save data
|
| 150 |
for($i=1; $i<$topic->num_answers+1; $i++) {
|
| 151 |
$answer = 'answer_' . $i;
|
| 152 |
if ($answered) {
|
| 153 |
db_query('UPDATE {topic_answer}
|
| 154 |
SET answer = "%s"
|
| 155 |
WHERE aid = %d
|
| 156 |
AND uid = %d
|
| 157 |
AND nid = %d',
|
| 158 |
$edit[$answer], $i, $user->uid, $nid);
|
| 159 |
}
|
| 160 |
else {
|
| 161 |
db_query('INSERT INTO {topic_answer} VALUES (%d, %d, %d, "%s")',
|
| 162 |
$i, $user->uid, $nid, $edit[$answer]);
|
| 163 |
}
|
| 164 |
}
|
| 165 |
drupal_set_message('Your answers have been saved');
|
| 166 |
drupal_goto("node/$nid");
|
| 167 |
}
|
| 168 |
|
| 169 |
if (_user_answered($nid)) {
|
| 170 |
// User has answered
|
| 171 |
$existing = _load_answers($nid);
|
| 172 |
}
|
| 173 |
|
| 174 |
for($i=1; $i<$topic->num_answers+1; $i++) {
|
| 175 |
$answer = 'answer_' . $i;
|
| 176 |
if ($existing) {
|
| 177 |
$edit[$answer] = $existing[$i-1];
|
| 178 |
}
|
| 179 |
$output .= form_textarea(t('Answer %num', array('%num'=>$i)), check_plain($answer), filter_xss($edit[$answer]), 55, 10);
|
| 180 |
}
|
| 181 |
$output .= form_submit(t('Save'));
|
| 182 |
|
| 183 |
print theme('page', form($output));
|
| 184 |
}
|
| 185 |
|
| 186 |
function topic_view(&$node, $teaser = FALSE, $page = FALSE) {
|
| 187 |
$order_info = theme('topic_display', $node);
|
| 188 |
$node = node_prepare($node, $teaser);
|
| 189 |
}
|
| 190 |
|
| 191 |
function topic_nodeapi(&$node, $op, $arg) {
|
| 192 |
|
| 193 |
switch($op) {
|
| 194 |
case 'insert':
|
| 195 |
db_query('INSERT INTO {topic} VALUES (%d, %d, %d, %d, %d)',
|
| 196 |
$node->nid, $node->num_answers, $node->num_days, $node->answer_status, 0);
|
| 197 |
break;
|
| 198 |
|
| 199 |
case 'update':
|
| 200 |
db_query('UPDATE {topic} SET num_answers = %d, num_days = %d, answer_status = %d WHERE nid = %d',
|
| 201 |
$node->num_answers, $node->num_days, $node->answer_status, $node->nid);
|
| 202 |
break;
|
| 203 |
|
| 204 |
case 'delete':
|
| 205 |
db_query('DELETE FROM {topic} WHERE nid = %d', $node->nid);
|
| 206 |
db_query('DELETE FROM {topic_answer} WHERE nid = %d', $node->nid);
|
| 207 |
break;
|
| 208 |
}
|
| 209 |
}
|
| 210 |
|
| 211 |
function topic_user($op, &$edit, &$user, $category = '') {
|
| 212 |
switch($op) {
|
| 213 |
case 'delete':
|
| 214 |
db_query('DELETE FROM {topic_answer} WHERE uid = %d', $user->uid);
|
| 215 |
break;
|
| 216 |
}
|
| 217 |
}
|
| 218 |
|
| 219 |
function topic_load($node) {
|
| 220 |
return _topic_load($node->nid);
|
| 221 |
}
|
| 222 |
|
| 223 |
function _topic_load($nid) {
|
| 224 |
$ret = array();
|
| 225 |
$result = db_query('SELECT * FROM {topic} WHERE nid = %d', $nid);
|
| 226 |
if ($result) {
|
| 227 |
$ret = db_fetch_object($result);
|
| 228 |
}
|
| 229 |
return $ret;
|
| 230 |
}
|
| 231 |
|
| 232 |
function topic_cron() {
|
| 233 |
// Find topics that are enabled, but should be disabled
|
| 234 |
|
| 235 |
$topic_result = db_query('SELECT n.nid, n.uid FROM {topic} t, {node} n
|
| 236 |
WHERE t.nid = n.nid
|
| 237 |
AND answer_status = 1
|
| 238 |
AND processed = 0
|
| 239 |
AND n.created + (num_days*24*60*60) < %d', time());
|
| 240 |
while($topic = db_fetch_object($topic_result)) {
|
| 241 |
// Close the topic
|
| 242 |
db_query('UPDATE {topic} SET answer_status = 0 WHERE nid = %d', $topic->nid);
|
| 243 |
|
| 244 |
$node = node_load(array('nid' => $topic->nid));
|
| 245 |
$body = $node->title . "\n\n";
|
| 246 |
$body .= $node->body . "\n==================\n";
|
| 247 |
|
| 248 |
$answer_uid_result = db_query('SELECT uid, COUNT(*) FROM {topic_answer}
|
| 249 |
WHERE nid = %d
|
| 250 |
GROUP BY uid', $topic->nid);
|
| 251 |
while($answer_uid = db_fetch_object($answer_uid_result)) {
|
| 252 |
|
| 253 |
$answer_result = db_query('SELECT answer FROM {topic_answer}
|
| 254 |
WHERE nid = %d
|
| 255 |
AND uid = %d', $topic->nid, $answer_uid->uid);
|
| 256 |
while($answer = db_fetch_object($answer_result)) {
|
| 257 |
$body .= "\n";
|
| 258 |
$body .= _format_user($answer_uid->uid);
|
| 259 |
$body .= $answer->answer;
|
| 260 |
$body .= "\n";
|
| 261 |
}
|
| 262 |
|
| 263 |
// Remove HTML tags, and format for 72 characters per line
|
| 264 |
$body = wordwrap(strip_tags($body),72);
|
| 265 |
|
| 266 |
// Get the email of the topic creator, so we email him
|
| 267 |
$mail = _get_email($topic->uid);
|
| 268 |
$from = variable_get('site_mail', ini_get('sendmail'));
|
| 269 |
$subject = "Topic $topic->nid: $node->title";
|
| 270 |
$header = "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from";
|
| 271 |
|
| 272 |
user_mail($mail, $subject, $body, $header);
|
| 273 |
|
| 274 |
// Mark the topic as processed
|
| 275 |
}
|
| 276 |
db_query('UPDATE {topic} SET processed = 1 WHERE nid = %d', $topic->nid);
|
| 277 |
}
|
| 278 |
}
|
| 279 |
|
| 280 |
function _get_email($uid = 0) {
|
| 281 |
return db_result(db_query('SELECT mail FROM {users} WHERE uid = %d', $uid));
|
| 282 |
}
|
| 283 |
|
| 284 |
function _format_user($uid = 0) {
|
| 285 |
$info = _get_user($uid);
|
| 286 |
$info .= ' (';
|
| 287 |
foreach(_get_profile($uid) as $profile) {
|
| 288 |
$info .= $profile . ', ';
|
| 289 |
}
|
| 290 |
$info .= ') ';
|
| 291 |
|
| 292 |
return $info;
|
| 293 |
}
|
| 294 |
|
| 295 |
function _get_profile($uid = 0) {
|
| 296 |
$profile_result = array();
|
| 297 |
foreach(_get_profile_fields() as $profile_field) {
|
| 298 |
$value = db_result(db_query("SELECT pv.value FROM {profile_fields} pf, {profile_values} pv
|
| 299 |
WHERE pv.fid = pf.fid
|
| 300 |
AND pf.name = '%s'
|
| 301 |
AND pv.uid = %d", $profile_field, $uid));
|
| 302 |
$profile_result[] = $value;
|
| 303 |
}
|
| 304 |
|
| 305 |
return $profile_result;
|
| 306 |
}
|
| 307 |
|
| 308 |
function _get_user($uid = 0) {
|
| 309 |
return db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $uid));
|
| 310 |
}
|
| 311 |
|
| 312 |
function _load_answers($nid = 0) {
|
| 313 |
global $user;
|
| 314 |
$answer = array();
|
| 315 |
|
| 316 |
$result = db_query('SELECT answer FROM {topic_answer} WHERE nid = %d AND uid = %d', $nid, $user->uid);
|
| 317 |
while ($row = db_fetch_object($result)) {
|
| 318 |
$answer[] = $row->answer;
|
| 319 |
}
|
| 320 |
|
| 321 |
return $answer;
|
| 322 |
}
|
| 323 |
|
| 324 |
function _user_answered($nid) {
|
| 325 |
global $user;
|
| 326 |
$num = (int)db_result(db_query('SELECT COUNT(*) FROM {topic_answer} WHERE nid = %d AND uid = %d', $nid, $user->uid));
|
| 327 |
return $num;
|
| 328 |
}
|
| 329 |
|
| 330 |
function topic_update_1() {
|
| 331 |
return _system_update_utf8(array('topic', 'topic_answer'));
|
| 332 |
}
|