| 1 |
<?php
|
| 2 |
|
| 3 |
function promise_perm() {
|
| 4 |
|
| 5 |
return array('access promise','create promise','sign promise','manage own promise');
|
| 6 |
|
| 7 |
} // promise_perm
|
| 8 |
|
| 9 |
function promise_help($section) {
|
| 10 |
|
| 11 |
switch ($section) {
|
| 12 |
case 'node/add#promise':
|
| 13 |
return t('Add a promise here. You can assign a due date.');
|
| 14 |
} // switch
|
| 15 |
|
| 16 |
} // promise_help
|
| 17 |
|
| 18 |
function promise_access($op, $node) {
|
| 19 |
|
| 20 |
global $user;
|
| 21 |
|
| 22 |
if ($op == 'create') {
|
| 23 |
return user_access('create promise');
|
| 24 |
} // if
|
| 25 |
|
| 26 |
if ($op == 'update' || $op == 'delete') {
|
| 27 |
if ( user_access('manage own promise') && ($user->uid == $node->uid ) )
|
| 28 |
{
|
| 29 |
return true;
|
| 30 |
} // if
|
| 31 |
} // if
|
| 32 |
|
| 33 |
return true;
|
| 34 |
|
| 35 |
} // promise_access
|
| 36 |
|
| 37 |
function promise_menu($may_cache) {
|
| 38 |
|
| 39 |
$items = array();
|
| 40 |
|
| 41 |
if ($may_cache) {
|
| 42 |
$items[] = array(
|
| 43 |
'path' => 'node/add/promise',
|
| 44 |
'title' => t('Promise'),
|
| 45 |
'access' => user_access('create promise')
|
| 46 |
);
|
| 47 |
$items[] = array(
|
| 48 |
'path' => 'promises',
|
| 49 |
'title' => t('Promises'),
|
| 50 |
'callback' => 'promise_page',
|
| 51 |
'access' => user_access('access promise'),
|
| 52 |
'type' => MENU_SUGGESTED_ITEM
|
| 53 |
);
|
| 54 |
} // if
|
| 55 |
|
| 56 |
return $items;
|
| 57 |
|
| 58 |
} // promise_menu
|
| 59 |
|
| 60 |
function promise_page($a = NULL) {
|
| 61 |
|
| 62 |
if (is_numeric($a)) { // $a is a user ID
|
| 63 |
|
| 64 |
return promise_page_user($a);
|
| 65 |
|
| 66 |
} // if
|
| 67 |
else if ( $a == 'successful' )
|
| 68 |
{
|
| 69 |
|
| 70 |
return promise_page_successful();
|
| 71 |
|
| 72 |
} // else if
|
| 73 |
|
| 74 |
} // promise_page
|
| 75 |
|
| 76 |
function promise_page_user($uid) {
|
| 77 |
|
| 78 |
$account = user_load(array((is_numeric($uid) ? 'uid' : 'name') => $uid, 'status' => 1));
|
| 79 |
|
| 80 |
if ($account->uid) {
|
| 81 |
|
| 82 |
drupal_set_title($title = t("@name's Promises", array('@name' => $account->name)));
|
| 83 |
|
| 84 |
$result = db_query("SELECT n.* FROM {node} n
|
| 85 |
LEFT JOIN {promise} p
|
| 86 |
ON p.nid = n.nid
|
| 87 |
LEFT JOIN {promise_signed} s
|
| 88 |
ON s.nid = n.nid
|
| 89 |
WHERE p.nid IS NOT NULL
|
| 90 |
AND ( s.uid = %d OR n.uid = %d )" , $account->uid , $account->uid );
|
| 91 |
|
| 92 |
while ($node = db_fetch_object($result)) {
|
| 93 |
|
| 94 |
$output.= node_view(node_load($node->nid), 1);
|
| 95 |
|
| 96 |
} // while
|
| 97 |
|
| 98 |
return $output;
|
| 99 |
|
| 100 |
} // if
|
| 101 |
else {
|
| 102 |
|
| 103 |
drupal_not_found();
|
| 104 |
|
| 105 |
} // else
|
| 106 |
|
| 107 |
} // promise_page_user
|
| 108 |
|
| 109 |
function promise_page_successful() {
|
| 110 |
|
| 111 |
drupal_set_title($title = t("Successful Promises"));
|
| 112 |
|
| 113 |
$result = db_query("SELECT n.*, p.quota, COUNT(s.id) AS signed FROM {node} n
|
| 114 |
LEFT JOIN {promise} p
|
| 115 |
ON p.nid = n.nid
|
| 116 |
LEFT JOIN {promise_signed} s
|
| 117 |
ON s.nid = n.nid
|
| 118 |
WHERE p.nid IS NOT NULL
|
| 119 |
GROUP BY n.nid");
|
| 120 |
|
| 121 |
$output = '';
|
| 122 |
|
| 123 |
while ($node = db_fetch_object($result)) {
|
| 124 |
|
| 125 |
if ( $node->quota <= $node->signed )
|
| 126 |
$output.= node_view(node_load($node->nid), 1);
|
| 127 |
|
| 128 |
} // while
|
| 129 |
|
| 130 |
if ( $output == '' )
|
| 131 |
$output = '<div>No Successful Promises Yet</div>';
|
| 132 |
|
| 133 |
return $output;
|
| 134 |
|
| 135 |
} // promise_page_successful
|
| 136 |
|
| 137 |
function promise_node_info() {
|
| 138 |
return array(
|
| 139 |
'promise' => array(
|
| 140 |
'name' => t('promise'),
|
| 141 |
'module' => 'promise',
|
| 142 |
'base' => 'promise'
|
| 143 |
)
|
| 144 |
);
|
| 145 |
} // promise_node_info
|
| 146 |
|
| 147 |
function promise_form(&$node) {
|
| 148 |
|
| 149 |
if($node->title==""){
|
| 150 |
$title = $_REQUEST['promisetitle'];
|
| 151 |
} else {
|
| 152 |
$title = $node->title;
|
| 153 |
}
|
| 154 |
|
| 155 |
// summary / title
|
| 156 |
$form['title'] = array(
|
| 157 |
'#type' => 'textfield',
|
| 158 |
'#title' => t('I Promise To'),
|
| 159 |
'#required' => TRUE,
|
| 160 |
'#default_value' => $title,
|
| 161 |
'#weight' => -5,
|
| 162 |
'#description' => 'Promise summary',
|
| 163 |
);
|
| 164 |
|
| 165 |
// description / body
|
| 166 |
|
| 167 |
$form['body_filter']['body'] = array(
|
| 168 |
'#type' => 'textarea',
|
| 169 |
'#title' => t('Any Extra Information about Your Promise'),
|
| 170 |
'#required' => FALSE,
|
| 171 |
'#default_value' => $node->body,
|
| 172 |
'#rows' => 20
|
| 173 |
);
|
| 174 |
|
| 175 |
$form['body_filter']['filter'] = filter_form($node->format);
|
| 176 |
|
| 177 |
/**
|
| 178 |
* This should be changed to a calendar input widget
|
| 179 |
*/
|
| 180 |
|
| 181 |
if($node->duedate == '31/12/1969'){
|
| 182 |
$duedate = '31/12/2008';
|
| 183 |
} else {
|
| 184 |
$duedate = $node->duedate;
|
| 185 |
}
|
| 186 |
|
| 187 |
$form['duedate'] = array(
|
| 188 |
'#type' => 'textfield',
|
| 189 |
'#title' => t('People Must Promise By'),
|
| 190 |
'#required' => TRUE,
|
| 191 |
'#default_value' => $duedate,
|
| 192 |
'#weight' => 0,
|
| 193 |
'#description' => 'IMPORTANT: The date format is currently dd/mm/yyyy. That will change in the 1.1 release',
|
| 194 |
'#attributes' => array('class' => 'jscalendar')
|
| 195 |
);
|
| 196 |
|
| 197 |
|
| 198 |
$form['quota'] = array(
|
| 199 |
'#type' => 'textfield',
|
| 200 |
'#title' => t('If This Number of People Promise to Do the Same'),
|
| 201 |
'#required' => TRUE,
|
| 202 |
'#default_value' => $node->quota,
|
| 203 |
'#weight' => 0,
|
| 204 |
'#description' => 'How many people need to sign on by due date?'
|
| 205 |
);
|
| 206 |
|
| 207 |
return $form;
|
| 208 |
|
| 209 |
} // promise_form
|
| 210 |
|
| 211 |
function promise_insert($node) {
|
| 212 |
|
| 213 |
global $user;
|
| 214 |
|
| 215 |
preg_match('/^(\d\d)\/(\d\d)\/(\d\d\d\d)$/', $node->duedate, $m);
|
| 216 |
$ts = mktime(0, 0, 0, $m[2], $m[1], $m[3]);
|
| 217 |
$src = "node/" . $node->nid;
|
| 218 |
$dst = "promise/" . $node->nid;
|
| 219 |
$uid = $user->uid;
|
| 220 |
|
| 221 |
db_query("INSERT INTO {promise} (nid, duedate, quota) VALUES (%d, %d, %d)", $node->nid, $ts, $node->quota);
|
| 222 |
db_query("INSERT INTO {promise_signed} (nid, uid, signed) VALUES (%d, %d, %d)", $node->nid, $uid, time());
|
| 223 |
db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", $src, $dst);
|
| 224 |
|
| 225 |
} // promise_insert
|
| 226 |
|
| 227 |
function promise_sign(&$node) {
|
| 228 |
|
| 229 |
global $user;
|
| 230 |
$nid = arg(1);
|
| 231 |
|
| 232 |
if ($node = node_load($nid)) {
|
| 233 |
|
| 234 |
if ( time() < $node->duedate ) {
|
| 235 |
|
| 236 |
// Record the signing by this user
|
| 237 |
if ($user->uid) {
|
| 238 |
|
| 239 |
db_query('INSERT INTO {promise_signed} (nid, uid, signed) VALUES (%d, %d, %d)', $node->nid, $user->uid, time());
|
| 240 |
|
| 241 |
} // if
|
| 242 |
else {
|
| 243 |
|
| 244 |
drupal_set_message(t("You must login to sign a promise."), 'error');
|
| 245 |
|
| 246 |
} // else
|
| 247 |
|
| 248 |
cache_clear_all();
|
| 249 |
drupal_set_message(t('Your signature was recorded.'));
|
| 250 |
|
| 251 |
} // if
|
| 252 |
else {
|
| 253 |
|
| 254 |
drupal_set_message(t("Sorry, this promise has closed."), 'error');
|
| 255 |
|
| 256 |
} // else
|
| 257 |
|
| 258 |
drupal_goto('node/'. $nid);
|
| 259 |
|
| 260 |
} // if
|
| 261 |
else {
|
| 262 |
|
| 263 |
drupal_not_found();
|
| 264 |
|
| 265 |
} // else
|
| 266 |
|
| 267 |
} // promise_sign
|
| 268 |
|
| 269 |
function promise_delete($node) {
|
| 270 |
|
| 271 |
db_query("DELETE FROM {promise_signed} WHERE nid = %d", $node->nid);
|
| 272 |
|
| 273 |
} // promise_delete
|
| 274 |
|
| 275 |
function promise_user($op, &$edit, &$user) {
|
| 276 |
|
| 277 |
if ($op == 'delete') {
|
| 278 |
|
| 279 |
db_query('UPDATE {promise_signed} SET uid = 0 WHERE uid = %d', $user->uid);
|
| 280 |
|
| 281 |
} // if
|
| 282 |
|
| 283 |
} // promise_user
|
| 284 |
|
| 285 |
function promise_load($node) {
|
| 286 |
|
| 287 |
$t = db_fetch_object(db_query('SELECT duedate, quota FROM {promise} WHERE nid = %d', $node->nid));
|
| 288 |
|
| 289 |
return $t;
|
| 290 |
|
| 291 |
} // promise_load
|
| 292 |
|
| 293 |
function promise_prepare(&$node) {
|
| 294 |
|
| 295 |
$node->duedate = date('d/m/Y', $node->duedate);
|
| 296 |
|
| 297 |
} // promise_prepare
|
| 298 |
|
| 299 |
function promise_update($node) {
|
| 300 |
|
| 301 |
preg_match('/^(\d\d)\/(\d\d)\/(\d\d\d\d)$/', $node->duedate, $m);
|
| 302 |
$ts = mktime(0, 0, 0, $m[2], $m[1], $m[3]);
|
| 303 |
|
| 304 |
db_query("UPDATE {promise} set duedate=%d , quota=%d WHERE nid=%d", $ts, $node->quota, $node->nid);
|
| 305 |
|
| 306 |
} // promise_update
|
| 307 |
|
| 308 |
function promise_view(&$node, $teaser = FALSE, $page = FALSE) {
|
| 309 |
|
| 310 |
global $user;
|
| 311 |
|
| 312 |
if ($_POST['op'] == t('Promise')) {
|
| 313 |
|
| 314 |
promise_sign($node);
|
| 315 |
|
| 316 |
} // if
|
| 317 |
|
| 318 |
$signed_already_result = db_query('SELECT * FROM {promise_signed} WHERE nid = %d', $node->nid);
|
| 319 |
|
| 320 |
$signed = array();
|
| 321 |
|
| 322 |
while ( $signed_already = db_fetch_object( $signed_already_result ) )
|
| 323 |
$signed[$signed_already->uid] = $signed_already;
|
| 324 |
|
| 325 |
$node->content['body'] = array( '#value' => '' );
|
| 326 |
|
| 327 |
$node->content['body']['#value'].= '<table border="0"><tr><td valign="top" width="50">';
|
| 328 |
//OurTahoe.org specific digg-like functionality
|
| 329 |
//$node->content['body']['#value'].= '<div style="clear:both;float:left;margin:0 10px 10px 0">';
|
| 330 |
//$node->content['body']['#value'].= '<script>promise_id = "' . $node->nid . '";';
|
| 331 |
//$node->content['body']['#value'].= '</script>';
|
| 332 |
//$node->content['body']['#value'].= '<script src="http://www.ourtahoe.org/custompages/promise/js/promisethis.js"></script></div>';
|
| 333 |
$node->content['body']['#value'].= '</td><td valign="top">';
|
| 334 |
|
| 335 |
$node->content['body']['#value'].= t( $node->body );
|
| 336 |
|
| 337 |
$node->content['body']['#value'].= '</td></table>';
|
| 338 |
|
| 339 |
$node->content['body']['#value'].= '<div class="promise-view-duedate">';
|
| 340 |
|
| 341 |
//subtract the number of users committed from the quota
|
| 342 |
|
| 343 |
$newquota = ( ($node->quota) - count($signed) );
|
| 344 |
$node->content['body']['#value'].= $newquota;
|
| 345 |
|
| 346 |
//grammar check
|
| 347 |
if($newquota==1){
|
| 348 |
$people="person";
|
| 349 |
} else {
|
| 350 |
$people="people";
|
| 351 |
} // if
|
| 352 |
|
| 353 |
$node->content['body']['#value'].= t(' more ' . $people . ' must promise before: %duedate', array('%duedate' => format_date($node->duedate, 'custom', 'm/d/Y')));
|
| 354 |
$node->content['body']['#value'].= '</div>';
|
| 355 |
|
| 356 |
if (
|
| 357 |
( ! isset( $signed[ $user->uid ] ) ) &&
|
| 358 |
( time() <= $node->duedate )
|
| 359 |
)
|
| 360 |
{
|
| 361 |
|
| 362 |
$node->content['body']['#value'].= drupal_get_form('promise_view_signed', $node);
|
| 363 |
|
| 364 |
} // if
|
| 365 |
else if ( time() > $node->duedate )
|
| 366 |
{
|
| 367 |
|
| 368 |
$node->content['body']['#value'].= '<p>The deadline for this promise has passed.</p>';
|
| 369 |
|
| 370 |
} // else if
|
| 371 |
|
| 372 |
if ( count( $signed ) > 0 )
|
| 373 |
{
|
| 374 |
|
| 375 |
$node->content['body']['#value'].= '<p><b>' . t( 'OurTahoe.org Users Already Committed to This Promise:' ) . '</b>';
|
| 376 |
$node->content['body']['#value'].= '<ul class="signed">';
|
| 377 |
|
| 378 |
foreach( $signed as $signer )
|
| 379 |
{
|
| 380 |
|
| 381 |
$account = user_load(array('uid' => $signer->uid));
|
| 382 |
$node->content['body']['#value'].= '<li><a href="/user/' . $signer->uid . '">' . $account->name . '</a></li>';
|
| 383 |
|
| 384 |
} // foreach
|
| 385 |
|
| 386 |
$node->content['body']['#value'].= '</ul></p>';
|
| 387 |
|
| 388 |
} // if
|
| 389 |
|
| 390 |
return $node;
|
| 391 |
|
| 392 |
} // promise_view
|
| 393 |
|
| 394 |
function promise_view_signed($node) {
|
| 395 |
|
| 396 |
$form['nid'] = array('#type' => 'hidden', '#value' => $node->nid);
|
| 397 |
$form['sign'] = array('#type' => 'submit', '#value' => t('Promise'));
|
| 398 |
$form['#action'] = url('node/'. $node->nid);
|
| 399 |
return $form;
|
| 400 |
|
| 401 |
} // promise_view_signed
|
| 402 |
|
| 403 |
?>
|