Skip to content

Commit

Permalink
Changing the .gitignore
Browse files Browse the repository at this point in the history
Adding behavior to manage custom order of quiz questions
  • Loading branch information
lorenzo committed Apr 3, 2009
1 parent 9a92341 commit 98a53e1
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 23 deletions.
4 changes: 1 addition & 3 deletions .gitignore
@@ -1,5 +1,3 @@
.DS_Store
tmp/cache/models/*
tmp/cache/persitent/*
tmp/cache/views/*
tmp/**/*
config/database.php
2 changes: 1 addition & 1 deletion config/core.php
Expand Up @@ -43,7 +43,7 @@
* In production mode, flash messages redirect after a time interval.
* In development mode, you need to click the flash message to continue.
*/
Configure::write('debug', 0);
Configure::write('debug', 2);
/**
* To configure CakePHP *not* to use mod_rewrite and to
* use CakePHP pretty URLs, remove these .htaccess
Expand Down
285 changes: 285 additions & 0 deletions plugins/quiz/models/behaviors/sortable.php
@@ -0,0 +1,285 @@
<?php
/**
* Copyright (c) 2008, Dardo Sordi
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) 2008, Dardo Sordi
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @author Dardo Sordi
*/

class SortableBehavior extends ModelBehavior {

var $__config = array();

function setup(&$model, $config = array()) {
$default = array('field' => 'order', 'enabled' => true, 'cluster' => false);
$this->__config[$model->name] = array_merge($default, (array)$config);
}

function _config(&$model, $config = array()) {
if (is_array($config)) {
if (!empty($config)) {
$this->__config[$model->name] = array_merge($this->__config[$model->name], $config);
}
} else {
return $this->__config[$model->name][$config];
}

return $this->__config[$model->name];
}

function _fullField(&$model) {
return $model->name . '.' . $this->_config($model, 'field');
}

function beforeFind(&$model, $conditions = array()) {
extract($this->_config($model));
if (!$enabled) {
return $conditions;
}
if (is_string($conditions['fields']) && strpos($conditions['fields'], 'COUNT') === 0) {
return $conditions;
}
$order = $conditions['order'];
if (is_array($conditions['order'])) {
$order = current($conditions['order']);
}
if (empty($order)) {
$conditions['order'] = array(array($this->_fullField($model) => 'ASC'));

if ($cluster !== false) {
$conditions['order'] = array_merge(array($cluster => 'ASC'), $conditions['order']);
}
}
return $conditions;
}

function beforeSave(&$model) {
extract($this->_config($model));
if ($enabled) {
$fixPosition = false;
$isInsert = !$model->id;
if (isset($model->data[$model->name][$field]) && !empty($model->data[$model->name][$field])) {
$newPosition = $model->data[$model->name][$field];
} else {
$newPosition = null;
}
$clusterId = $this->_clusterId($model);
$model->data[$model->name][$field] = $this->lastPosition($model, $clusterId) + 1;
$model->__fixPosition = $newPosition;
}
return true;
}

function afterSave(&$model, $created) {
extract($this->_config($model));
if (!$enabled) {
return true;
}
$position = $model->data[$model->name][$field];
if ($model->__fixPosition) {
$position = $model->__fixPosition;
$model->__fixPosition = null;
$this->setPosition($model, $model->id, $position);
}
}

function beforeDelete(&$model) {
extract($this->_config($model));
if ($enabled) {
$model->__fixPosition = $this->position($model);
}
return true;
}

function afterDelete(&$model) {
extract($this->_config($model));
if (!$enabled) {
return true;
}
$fullField = $this->_fullField($model);
$position = $model->__fixPosition;
$model->__fixPosition = null;
$model->updateAll(array($fullField => "$fullField - 1"), array("$fullField >=" => $position));
}

function moveTop(&$model, $id = null) {
$this->disableSortable($model);
if ($id) {
$model->id = $id;
}
extract($this->_config($model));
$position = $this->position($model);
$clusterId = $this->_clusterId($model);
$fullField = $this->_fullField($model);

if ($position > 1) {
$newPosition = 1;
$conditions = $this->_conditions($model, $clusterId, array("$fullField <=" => $position, "$fullField >=" => $newPosition));
$model->updateAll(array($fullField => "$fullField + 1"), $conditions);
$model->saveField($field, $newPosition);
}
$this->enableSortable($model);
return true;
}

function moveUp(&$model, $id = null, $step = 1) {
$this->disableSortable($model);
if ($id) {
$model->id = $id;
}
extract($this->_config($model));
$position = $this->position($model);
$clusterId = $this->_clusterId($model);
$fullField = $this->_fullField($model);

if ($position > 1) {
$newPosition = $position - $step;
if ($newPosition < 1) {
$newPosition = 1;
}
$conditions = $this->_conditions($model, $clusterId, array("$fullField <=" => $position, "$fullField >=" => $newPosition));
$model->updateAll(array($fullField => "$fullField + 1"), $conditions);
$model->saveField($field, $newPosition);
}
$this->enableSortable($model);
return true;
}

function moveDown(&$model, $id = null, $step = 1) {
$this->disableSortable($model);
if ($id) {
$model->id = $id;
}
extract($this->_config($model));
$position = $this->position($model);
$clusterId = $this->_clusterId($model);
$id = $model->id;
$model->id = null;
$last = $this->lastPosition($model, $clusterId);
$fullField = $this->_fullField($model);

if ($position < $last) {
$newPosition = $position + $step;
if ($newPosition > $last) {
$newPosition = $last;
}
$conditions = $this->_conditions($model, $clusterId, array("$fullField >=" => $position, "$fullField <=" => $newPosition));
$model->updateAll(array($fullField => "$fullField - 1"), $conditions);
$model->id = $id;
$model->saveField($field, $newPosition);
}
$this->enableSortable($model);
return true;
}

function moveBottom(&$model, $id = null) {
$this->disableSortable($model);
if ($id) {
$model->id = $id;
}
extract($this->_config($model));

$position = $this->position($model);
$clusterId = $this->_clusterId($model);
$id = $model->id;
$model->id = null;
$last = $this->lastPosition($model, $clusterId);
$fullField = $this->_fullField($model);

if ($position < $last) {
$newPosition = $last;
$conditions = $this->_conditions($model, $clusterId, array("$fullField >=" => $position, "$fullField <=" => $newPosition));
$model->updateAll(array($fullField => "$fullField - 1"), $conditions);
$model->id = $id;
$model->saveField($field, $newPosition);
}
$this->enableSortable($model);
return true;
}

function setPosition(&$model, $id = null, $destination) {
$this->disableSortable($model);
if ($id) {
$model->id = $id;
}
extract($this->_config($model));
$position = $this->position($model);
$id = $model->id;
$model->id = null;
$delta = $position - $destination;

if ($position > $destination) {
$this->moveUp($model, $id, $delta);
} elseif ($position < $destination) {
$this->moveDown($model, $id, -$delta);
}
$this->enableSortable($model);
return true;
}

function disableSortable(&$model) {
$this->_config($model, array('enabled' => false));
}

function enableSortable(&$model) {
$this->_config($model, array('enabled' => true));
}

function position(&$model, $id = null) {
if ($id) {
$model->id = $id;
}
return $model->field($this->_config($model, 'field'));
}

function _clusterId(&$model, $id = null) {
$cluster = $this->_config($model, 'cluster');

if ($cluster === false) {
return null;
}
if ($id) {
$model->id = $id;
}
return $model->field($cluster);
}

function lastPosition(&$model, $clusterId = null) {
$id = $model->id;
$model->id = null;
$field = $this->_config($model, 'field');
$fields = array($field);
$order = array($field => 'DESC');
$conditions = $this->_conditions($model, $clusterId);
$last = $model->find('first', compact('fields', 'order', 'conditions'));
$model->id = $id;

if (!empty($last)) {
return current(current($last));
}

return false;
}

function _conditions(&$model, $clusterId = null, $conditions = array()) {
$cluster = $this->_config($model, 'cluster');

if (($cluster !== false) && !is_null($clusterId)) {
$conditions = array_merge($conditions, array($cluster => $clusterId));
}

return $conditions;
}

function findByPosition(&$model, $position, $clusterId = null) {
$field = $this->_fullField($model);
return $model->find($this->_conditions($model, $clusterId, array($field => $position)));
}

}

?>
22 changes: 3 additions & 19 deletions plugins/quiz/models/quiz_question.php
Expand Up @@ -25,24 +25,8 @@ class QuizQuestion extends QuizAppModel {
'counterCache' => ''
)
);

function getLastPostion($quiz_id) {
$result = $this->find('first',array(
'fields' => array('MAX(position) AS last'),
'conditions' => array('quiz_id' => $quiz_id),
'recursive' => -1
)
);
return $result[0]['last'];
}

function beforeSave() {
if (!isset($this->data[$this->alias]['position']) && isset($this->data[$this->alias]['quiz_id'])) {
$lastPosition = $this->getLastPostion($this->data[$this->alias]['quiz_id']);
$this->data[$this->alias]['position'] = $lastPosition + 1;
}
return true;
}

var $actsAs = array('Quiz.Sortable' => array('field' => 'position'));

}
?>
?>

0 comments on commit 98a53e1

Please sign in to comment.