Skip to content

Commit

Permalink
Fixed #75 - ability to vote for any resource
Browse files Browse the repository at this point in the history
  • Loading branch information
bezumkin committed Mar 21, 2014
1 parent ad6308b commit 6217c27
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 13 deletions.
1 change: 1 addition & 0 deletions core/components/tickets/docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changelog for Tickets.
1.4.0 beta2
==========
- [#76] Fixed auto publication of tickets.
- [#75] Ability to vote for any resource.
- [#74] Fixed ticket panel for new versions of MODX.
- [#72] Fixed handling TVs.
- [#64] Fixed forced processing of introtext by Jevix.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
$properties = is_string($row['properties'])
? $modx->fromJSON($row['properties'])
: $row['properties'];
if (!empty($properties['tickets'])) {
$properties = $properties['tickets'];
}
if (empty($properties['process_tags'])) {
foreach ($row as $field => $value) {
$row[$field] = str_replace(array('[',']'), array('[',']'), $value);
Expand Down
71 changes: 62 additions & 9 deletions core/components/tickets/elements/snippets/snippet.ticket_meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,75 @@
$pdoFetch = new $pdoClass($modx, $scriptProperties);
$pdoFetch->addTime('pdoTools loaded');

/** @var Ticket $ticket */
/** @var Ticket|modResource $ticket */
$ticket = !empty($id) && $id != $modx->resource->id
? $modx->getObject('Ticket', $id)
? $modx->getObject('modResource', $id)
: $modx->resource;

if (!($ticket instanceof Ticket)) {
return 'This resource is not instance of Ticket class.';
}
$class = $ticket instanceof Ticket
? 'Ticket'
: 'modResource';

$data = $ticket->toArray();
$vote = $pdoFetch->getObject('TicketVote', array('id' => $ticket->id, 'class' => 'Ticket', 'createdby' => $modx->user->id), array('select' => 'value', 'sortby' => 'id'));
if (!empty($vote)) {
$data['vote'] = $vote['value'];
}

if ($class != 'Ticket') {
// Rating
if (!$modx->user->id || $modx->user->id == $ticket->createdby) {
$data['voted'] = 0;
}
else {
$q = $modx->newQuery('TicketVote');
$q->where(array('id' => $ticket->id, 'createdby' => $modx->user->id, 'class' => 'Ticket'));
$q->select('`value`');
$tstart = microtime(true);
if ($q->prepare() && $q->stmt->execute()) {
$modx->startTime += microtime(true) - $tstart;
$modx->executedQueries ++;
$voted = $q->stmt->fetchColumn();
if ($voted > 0) {$voted = 1;}
elseif ($voted < 0) {$voted = -1;}
$data['voted'] = $voted;
}
}
$data['can_vote'] = $data['voted'] === false && $modx->user->id && $modx->user->id != $ticket->createdby;

$data = array_merge($ticket->getProperties('tickets'), $data);
if (!isset($data['rating'])) {
$data['rating'] = $data['rating_total'] = $data['rating_plus'] = $data['rating_minus'] = 0;
}

// Views
$data['views'] = 0;
$q = $modx->newQuery('modResource', $ticket->id);
$q->leftJoin('TicketView','TicketView', "`TicketView`.`parent` = `modResource`.`id`");
$q->select('COUNT(`TicketView`.`parent`) as `views`');
$tstart = microtime(true);
if ($q->prepare() && $q->stmt->execute()) {
$modx->startTime += microtime(true) - $tstart;
$modx->executedQueries ++;
$data['views'] = (integer) $q->stmt->fetchColumn();
}

// Comments
$data['comments'] = 0;
$q = $modx->newQuery('TicketThread', array('name' => 'resource-'.$ticket->id));
$q->leftJoin('TicketComment','TicketComment', "`TicketThread`.`id` = `TicketComment`.`thread` AND `TicketComment`.`published` = 1");
$q->select('COUNT(`TicketComment`.`id`) as `comments`');
$tstart = microtime(true);
if ($q->prepare() && $q->stmt->execute()) {
$modx->startTime += microtime(true) - $tstart;
$modx->executedQueries ++;
$data['comments'] = (integer) $q->stmt->fetchColumn();
}

// Date ago
$data['date_ago'] = $Tickets->dateFormat($data['createdon']);
}

if ($data['rating'] > 0) {
$data['rating'] = '+'.$data['rating'];
$data['rating_positive'] = 1;
Expand Down Expand Up @@ -58,9 +112,9 @@
$data['inactive'] = (integer) !empty($data['cant_vote']);

if (!empty($getSection)) {
$fields = $modx->getFieldMeta('TicketsSection');
unset($fields['content']);
$section = $pdoFetch->getObject('TicketsSection', $ticket->parent, array('select' => implode(',', array_keys($fields))));
$fields = $modx->getFieldMeta('modResource');
unset($fields['content'], $fields['id']);
$section = $pdoFetch->getObject('modResource', $ticket->parent, array('select' => implode(',', array_keys($fields))));
foreach ($section as $k => $v) {
$data['section.'.$k] = $v;
}
Expand All @@ -76,7 +130,6 @@
'modUser' => 'username',
)
));

$data = array_merge($data, $user);
}
$data['id'] = $ticket->id;
Expand Down
2 changes: 1 addition & 1 deletion core/components/tickets/model/tickets/ticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public function getRating() {
else {
$voted = $this->getVote();
if ($voted > 0) {$voted = 1;}
elseif ($voted < 0) {$array['voted'] = -1;}
elseif ($voted < 0) {$voted = -1;}
$array['voted'] = $voted;
}

Expand Down
48 changes: 45 additions & 3 deletions core/components/tickets/processors/web/ticket/vote.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
class TicketVoteProcessor extends modObjectCreateProcessor {
/** @var TicketVote $object */
public $object;
/* @var Ticket $ticket */
/* @var Ticket|modResource $ticket */
private $ticket;
public $objectType = 'TicketVote';
public $classKey = 'TicketVote';
Expand All @@ -17,7 +17,7 @@ public function beforeSet() {
if (!$this->modx->user->isAuthenticated($this->modx->context->key)) {
return $this->modx->lexicon('permission_denied');
}
elseif (!$this->ticket = $this->modx->getObject('Ticket', $id)) {
elseif (!$this->ticket = $this->modx->getObject('modResource', $id)) {
return $this->modx->lexicon('ticket_err_ticket');
}
elseif ($this->ticket->createdby == $this->modx->user->id) {
Expand Down Expand Up @@ -62,11 +62,53 @@ public function beforeSave() {

/** {@inheritDoc} */
public function cleanup() {
$rating = $this->ticket->updateRating();
if ($this->ticket instanceof Ticket) {
$rating = $this->ticket->updateRating();
}
else {
$rating = array('rating' => 0, 'rating_plus' => 0, 'rating_minus' => 0);

$q = $this->modx->newQuery('TicketVote', array('id' => $this->ticket->id, 'class' => 'Ticket'));
$q->innerJoin('modUser', 'modUser', '`modUser`.`id` = `TicketVote`.`createdby`');
$q->select('value');
$tstart = microtime(true);
if ($q->prepare() && $q->stmt->execute()) {
$this->modx->startTime += microtime(true) - $tstart;
$this->modx->executedQueries ++;
$rows = $q->stmt->fetchAll(PDO::FETCH_COLUMN);
foreach ($rows as $value) {
$rating['rating'] += $value;
if ($value > 0) {
$rating['rating_plus'] += $value;
}
elseif ($value < 0) {
$rating['rating_minus'] += $value;
}
}
$this->ticket->setProperties($rating, 'tickets', true);
$this->ticket->save();
$this->clearCache();
}
}

return $this->success('', $rating);
}


/**
* Clear ticket cache
*/
public function clearCache() {
$context = $this->ticket->get('context_key');
$this->ticket->_contextKey = $context;

/** @var xPDOFileCache $cache */
$cache = $this->modx->cacheManager->getCacheProvider($this->modx->getOption('cache_resource_key', null, 'resource'));
$key = $this->ticket->getCacheKey();
$cache->delete($key, array('deleteTop' => false));
$cache->delete($key);
}

}

return 'TicketVoteProcessor';

0 comments on commit 6217c27

Please sign in to comment.