00001 <?php
00002
00012 class CSERDreview extends CSERDModelBase {
00015 protected $questionnaire;
00016
00017 public static $ATTRIBUTES = array(
00018 'reviewId' => array(
00019 'nullable' => false,
00020 'commit' => SModel2::COMMIT_NEVER,
00021 'type' => 'int'
00022 ),
00023 'state' => array(
00024 'type' => 'string'
00025 ),
00026 'active' => array(
00027 'type' => 'int',
00028 'default' => null
00029 ),
00030 'type' => array(
00031 'nullable' => false,
00032 'type' => 'string'
00033 ),
00034 'cserdId' => array(
00035 'type' => 'int',
00036 'default' => null
00037 ),
00038 'authorId' => array(
00039 'nullable' => false,
00040 'commit' => SModel2::COMMIT_FIRST,
00041 'type' => 'int',
00042 'foreign' => array('table' => 'user', 'on' => 'userId', 'object' => 'author')
00043 ),
00044 'ownerId' => array(
00045 'nullable' => false,
00046 'commit' => SModel2::COMMIT_FIRST,
00047 'type' => 'int',
00048 'foreign' => array('table' => 'user', 'on' => 'userId', 'object' => 'owner')
00049 ),
00050 'created' => array(
00051 'nullable' => false,
00052 'commit' => SModel2::COMMIT_CHANGED,
00053 'type' => 'time',
00054 'default' => ''
00055 ),
00056 'modified' => array(
00057 'nullable' => false,
00058 'type' => 'time',
00059 'default' => '0000-00-00 00:00:00'
00060 ),
00061 'solicited' => array(
00062 'type' => 'int',
00063 'default' => null
00064 ),
00065 'cache' => array(
00066 'type' => 'string'
00067 ),
00068 'project' => array(
00069 'type' => 'string'
00070 ),
00071 'author' => array(
00072 'commit' => SModel2::COMMIT_FOREIGN,
00073 'type' => 'CSERDuser'
00074 ),
00075 'owner' => array(
00076 'commit' => SModel2::COMMIT_FOREIGN,
00077 'type' => 'CSERDuser'
00078 )
00079 );
00080
00090 public function __construct($constraints = null, $multi = false) {
00091 parent::__construct();
00092
00093 $this->registerAttributes(self::$ATTRIBUTES);
00094 $this->registerTableName('review');
00095 $this->registerPrimaryKey('reviewId', true);
00096
00097 if($constraints !== null)
00098 $this->populate($constraints, $multi);
00099 }
00100
00110 public static function getAttributesStatic() {
00111 return self::$ATTRIBUTES;
00112 }
00113
00119 public static function getTableNameStatic() {
00120 return 'review';
00121 }
00122
00134 public static function retrieve($constraints = array(), $checkOnly = false) {
00135 $list = self::getList($constraints, array(1), array(), $checkOnly);
00136 if(!$list || count($list) == 0)
00137 return $checkOnly ? false : null;
00138 else if($checkOnly)
00139 return $list > 0;
00140 else
00141 return $list[0];
00142 }
00143
00152 public static function exists($constraints = array()) {
00153 return self::retrieve($constraints, true);
00154 }
00155
00168 public static function retrieveMulti($constraints = array(), $checkOnly = false, $depth = 1) {
00169 $list = self::getListMulti($constraints, array(1), array(), $checkOnly, $depth);
00170 if(!$list || count($list) == 0)
00171 return $checkOnly ? false : null;
00172 else if($checkOnly)
00173 return $list > 0;
00174 else
00175 return $list[0];
00176 }
00177
00186 public static function existsMulti($constraints = array()) {
00187 return self::retrieveMulti($constraints, true);
00188 }
00189
00203 public static function getList($constraints = array(), $limit = array(), $order = array(), $count = false) {
00204 return parent::getListBase(self::getDBI(), $constraints, $limit, $order, $count, 'CSERDreview', true);
00205 }
00206
00222 public static function getListMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00223 return parent::getListBaseMulti(self::getDBI(), $constraints, $limit, $order, $count,
00224 'CSERDreview', true, $depth);
00225 }
00226
00227
00236 public function getParentAuthor() {
00237 return CSERDuser::retrieve(array('userId' => $this->authorId));
00238 }
00239
00249 public function getParentAuthorMulti($depth = 1) {
00250 return CSERDuser::retrieveMulti(array('Base.userId' => $this->authorId), false, $depth);
00251 }
00252
00261 public function getParentOwner() {
00262 return CSERDuser::retrieve(array('userId' => $this->ownerId));
00263 }
00264
00274 public function getParentOwnerMulti($depth = 1) {
00275 return CSERDuser::retrieveMulti(array('Base.userId' => $this->ownerId), false, $depth);
00276 }
00277
00294 public function listreviewSubjects($constraints = array(), $limit = array(), $order = array(), $count = false) {
00295 return parent::getListBase(self::getDBI(),
00296 array_merge($constraints, array('reviewId' => $this->reviewId)),
00297 $limit, $order, $count, 'CSERDreviewSubject', true);
00298 }
00299
00318 public function listreviewSubjectsMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00319 return parent::getListBaseMulti(self::getDBI(),
00320 array_merge($constraints, array('Base.reviewId' => $this->reviewId)),
00321 $limit, $order, $count, 'CSERDreviewSubject', true, $depth);
00322 }
00323
00338 public function addreviewSubject($subject) {
00339 $obj = new CSERDreviewSubject();
00340
00341 if(is_object($subject))
00342 $obj->subject = $subject;
00343 else
00344 $obj->subjectId = $subject;
00345 $obj->reviewId = $this->reviewId;
00346
00347 if(!$obj->commit())
00348 return null;
00349
00350 return $obj;
00351 }
00352
00369 public function listcomments($constraints = array(), $limit = array(), $order = array(), $count = false) {
00370 return parent::getListBase(self::getDBI(),
00371 array_merge($constraints, array('reviewId' => $this->reviewId)),
00372 $limit, $order, $count, 'CSERDcomment', true);
00373 }
00374
00393 public function listcommentsMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00394 return parent::getListBaseMulti(self::getDBI(),
00395 array_merge($constraints, array('Base.reviewId' => $this->reviewId)),
00396 $limit, $order, $count, 'CSERDcomment', true, $depth);
00397 }
00398
00416 public function addcomment($owner, $author, $comment = '', $timestamp = '') {
00417 $obj = new CSERDcomment();
00418
00419 if($comment != '')
00420 $obj->comment = $comment;
00421 $obj->timestamp = $timestamp;
00422 $obj->reviewId = $this->reviewId;
00423 if(is_object($owner))
00424 $obj->owner = $owner;
00425 else
00426 $obj->ownerId = $owner;
00427 if(is_object($author))
00428 $obj->author = $author;
00429 else
00430 $obj->authorId = $author;
00431
00432 if(!$obj->commit())
00433 return null;
00434
00435 return $obj;
00436 }
00437
00454 public function listresponses($constraints = array(), $limit = array(), $order = array(), $count = false) {
00455 return parent::getListBase(self::getDBI(),
00456 array_merge($constraints, array('reviewId' => $this->reviewId)),
00457 $limit, $order, $count, 'CSERDresponse', true);
00458 }
00459
00478 public function listresponsesMulti($constraints = array(), $limit = array(), $order = array(), $count = false, $depth = 1) {
00479 return parent::getListBaseMulti(self::getDBI(),
00480 array_merge($constraints, array('Base.reviewId' => $this->reviewId)),
00481 $limit, $order, $count, 'CSERDresponse', true, $depth);
00482 }
00483
00499 public function addresponse($question, $response = '') {
00500 $obj = new CSERDresponse();
00501
00502 if(is_object($question))
00503 $obj->question = $question;
00504 else
00505 $obj->questionId = $question;
00506 $obj->reviewId = $this->reviewId;
00507 if($response != '')
00508 $obj->response = $response;
00509
00510 if(!$obj->commit())
00511 return null;
00512
00513 return $obj;
00514 }
00515
00516 protected function setQuestionnaire($questionnaire) {
00517 $this->questionnaire = $questionnaire;
00518 }
00519
00520 public function getQuestionnaire() {
00521 if (!isset($this->questionnaire)){
00522 $q = new CSERDquestionnaire($this->reviewId);
00523 if ($q === false) {
00524 print("Unable to complete populate(): attempt to populate Questionnaire "
00525 . "object for the review failed.");
00526
00527 return false;
00528 }
00529 $this->setQuestionnaire($q);
00530 }
00531
00532 return $this->questionnaire;
00533
00534 }
00535
00536
00537 public function updateCache($q = '') {
00538
00539 if ($q === '') {
00540 $q = new CSERDquestionnaire($this->reviewId);
00541 if ($q === false) {
00542 return false;
00543 }
00544 $this->questionnaire = $q;
00545 }
00546 $this->cache = HTMLQuestionnaireEncoder::encode($q);
00547
00548
00549 return true;
00550 }
00551
00552 public function updateDateModified() {
00553 $this->modified = DateParser::currentDateMysql();
00554 $this->commit();
00555 }
00556
00557
00558
00559
00560
00561
00562
00563 public function submitReview() {
00564 if ($this->state != 'private') {
00565 print("Attempt to call submitReview() on Review '" . $this->reviewId .
00566 "' failed: current workflow state is " . $this->state .
00567 ". Transition requires 'private'.");
00568 return false;
00569 }
00570 $this->state = 'pending';
00571 $this->updateDateModified();
00572 $this->commit();
00573 $cserdId = $this->cserdId;
00574 if (!SDRServiceCache::updateAverageReviewRating($cserdId))
00575 return false;
00576 return true;
00577 }
00578
00579
00580
00581 public function rejectReview() {
00582 if ($this->state != 'pending') {
00583 print("Attempt to call rejectReview() on Review $reviewId failed: "
00584 . "current workflow state is " . $this->state . ". Transition requires 'pending'.");
00585 return false;
00586 }
00587 $this->updateDateModified();
00588 $this->state = 'returned';
00589 $this->commit();
00590 $cserdId = $this->cserdId;
00591 if (!SDRServiceCache::updateAverageReviewRating($cserdId))
00592 return false;
00593 return true;
00594 }
00595
00596
00597
00598 public function publishReview() {
00599 if ($this->state != 'pending') {
00600 print("Attempt to call publishReview() on Review '" . $this->reviewId .
00601 "' failed: current workflow state is " . $this->state .
00602 ". Transition requires 'pending'.");
00603 return false;
00604 }
00605 $this->updateDateModified();
00606 $this->state = 'published';
00607 $this->commit();
00608
00609 $cserdId = $this->cserdId;
00610 if (!SDRServiceCache::updateAverageReviewRating($cserdId)) {
00611 return false;
00612 }
00613 return true;
00614 }
00615
00616
00617
00618 public function unpublishReview() {
00619 if ($this->state != 'published') {
00620 print("Attempt to call unpublishReview() on Review '" . $this->reviewId .
00621 "' failed: current workflow state is " . $this->state .
00622 ". Transition requires 'published'.");
00623 return false;
00624 }
00625 $this->updateDateModified();
00626 $this->state = 'pending';
00627 $this->commit();
00628 $cserdId = $this->cserdId;
00629 if (!SDRServiceCache::updateAverageReviewRating($cserdId))
00630 return false;
00631 return true;
00632 }
00633
00634 public function deleteReview() {
00635 if ($this->state != 'published') {
00636 print("Attempt to call unpublishReview() on Review '" . $this->reviewId .
00637 "' failed: current workflow state is " . $this->state .
00638 ". Transition requires 'published'.");
00639 return false;
00640 }
00641 $this->updateDateModified();
00642 $this->active = 'false';
00643 $this->commit();
00644 $cserdId = $this->cserdId;
00645 if (!SDRServiceCache::updateAverageReviewRating($cserdId))
00646 return false;
00647 return true;
00648 }
00649
00650
00651 public function isValidReview()
00652 {
00653
00654
00655 return (
00656 ($this->state != "") &&
00657 ($this->authorId != "") &&
00658 ($this->ownerId != "") &&
00659 ($this->active === true || $this->active === false) &&
00660 ($this->cserdId != "") &&
00661 ($this->type != ""));
00662 }
00663
00664 }
00665
00666 ?>