00001 <?php
00002
00011 class TSDText extends TSDLeaf {
00012 public static $ATTRIBUTES = array(
00013 'book_title' => false,
00014 'book_id' => false,
00015 'book_gradeOrCourse' => false,
00016 'chapter_name' => false,
00017 'chapter_id' => false,
00018 'section_name' => false,
00019 'section_id' => false,
00020 'resourceId' => false,
00021 'reason' => false,
00022 'sectionId' => false,
00023 'status' => false,
00024 'version' => false
00025 );
00026
00035 public function __construct($id = '') {
00036 if(is_array($id)) {
00037 $arr = $id;
00038 $id = '';
00039 }
00040 parent::__construct('Text', self::$ATTRIBUTES, 'section_id', $id, 'TSDTextbookAlignment', 'sectionId');
00041 if(isset($arr)) {
00042 foreach($arr as $elt) {
00043 switch(get_class($elt)) {
00044 case 'TSDBook':
00045 $this->setAttr('book_title', $elt->title);
00046 $this->setAttr('book_id', $elt->id);
00047 $this->setAttr('book_gradeOrCourse', $elt->gradeOrCourse);
00048 break;
00049 case 'TSDChapter':
00050 $this->setAttr('chapter_name', $elt->name);
00051 $this->setAttr('chapter_id', $elt->id);
00052 break;
00053 case 'TSDSection':
00054 $this->setAttr('section_name', $elt->name);
00055 $this->setAttr('section_id', $elt->id);
00056 break;
00057 }
00058 }
00059 $this->setAttr('reason', '');
00060 }
00061 }
00062
00070 public function isValid() {
00071 return true;
00072 }
00073
00081 protected function populate() {
00082 $query = <<<END_QUERY
00083 SELECT TSDBook.title AS book_title,
00084 TSDBook.gradeOrCourse AS book_gradeOrCourse,
00085 TSDChapter.name AS chapter_name,
00086 TSDSection.name AS section_name,
00087 TSDSection.id AS section_id
00088 FROM TSDSection
00089 LEFT JOIN TSDChapter ON TSDChapter.id = TSDSection.chapterId
00090 LEFT JOIN TSDBook ON TSDBook.id = TSDChapter.bookId
00091 WHERE
00092 TSDSection.id = '$this->section_id'
00093 END_QUERY;
00094
00095 $DBI = TSD::getDBI();
00096
00097 $result = $DBI->query($query);
00098 if(count($DBI->getError()) > 0) {
00099 $this->setError('Failed to populate: ' . implode('; ', $DBI->getError()));
00100 return false;
00101 }
00102
00103 if(count($result) == 0)
00104 return true;
00105 if(count($result) > 1)
00106 $this->setWarning("Multiple results for key 'id'; using first result");
00107
00108 foreach($result[0] as $a => $v)
00109 $this->setAttr($a, $v);
00110
00111 $this->inDB = true;
00112
00113 return true;
00114 }
00115
00128 protected static function getListImpl($constraints, $limit, $order, $cnt, $mode = 'normal') {
00129 $DBI = TSD::getDBI();
00130 if(!$DBI) {
00131 self::setStaticError('Cannot connect to TSD!');
00132 return null;
00133 }
00134
00135 if($mode == 'normal')
00136 $attrs = array_keys(self::$ATTRIBUTES);
00137 else
00138 $attrs = array_merge(array_keys(self::$ATTRIBUTES), array('resourceId', 'reason', 'approved', 'deleteFlag'));
00139
00140 $constraintC = self::criteriaToString($constraints, $attrs);
00141 if($constraintC === false)
00142 return null;
00143 $limitC = self::limitToString($limit);
00144 if($limitC === false)
00145 return null;
00146 $orderC = self::orderToString($order, $attrs);
00147 if($orderC === false)
00148 return null;
00149
00150 if($cnt)
00151 $query = 'SELECT COUNT(*) ';
00152 else {
00153 $query = <<<END_QUERY
00154 SELECT TSDBook.title AS book_title,
00155 TSDBook.gradeOrCourse AS book_gradeOrCourse,
00156 TSDBook.id AS book_id,
00157 TSDChapter.name AS chapter_name,
00158 TSDChapter.id AS chapter_id,
00159 TSDSection.name AS section_name,
00160 TSDSection.id AS section_id
00161
00162 END_QUERY;
00163 if($mode != 'normal')
00164 $query .= ', TSDTextbookAlignment.resourceId AS resourceId, TSDTextbookAlignment.reason AS reason, '
00165 . 'TSDTextbookAlignment.version AS version, TSDTextbookAlignment.status AS status ';
00166 }
00167
00168 $query .= <<<END_QUERY
00169 FROM TSDSection
00170 LEFT JOIN TSDChapter ON TSDChapter.id = TSDSection.chapterId
00171 LEFT JOIN TSDBook ON TSDBook.id = TSDChapter.bookId
00172 END_QUERY;
00173 if($mode != 'normal')
00174 $query .= ' LEFT JOIN TSDTextbookAlignment ON TSDSection.id = TSDTextbookAlignment.sectionId ';
00175
00176 if($constraintC != '') {
00177 $constraintC = str_replace('section_name', 'TSDSection.name', $constraintC);
00178 $constraintC = str_replace('chapter_name', 'TSDChapter.name', $constraintC);
00179 $constraintC = str_replace('chapter_id', 'TSDChapter.id', $constraintC);
00180 $constraintC = str_replace('book_title', 'TSDBook.title', $constraintC);
00181 $constraintC = str_replace('book_id', 'TSDBook.id', $constraintC);
00182 $constraintC = str_replace('book_gradeOrCourse', 'TSDBook.gradeOrCourse', $constraintC);
00183 $constraintC = str_replace('section_id', 'TSDSection.id', $constraintC);
00184 $query .= ' WHERE ' . $constraintC;
00185 }
00186 $query .= ' ' . $limitC . ' ' . $orderC;
00187
00188 $result = $DBI->query($query);
00189 if(count($DBI->getError()) > 0) {
00190 self::setStaticError('Error doing query: ' . implode('; ', $DBI->getError()));
00191 return null;
00192 }
00193
00194 $ret = array();
00195 foreach($result as $r) {
00196 $obj = new TSDText();
00197 foreach($r as $a => $v)
00198 $obj->setAttr($a, $v);
00199 $obj->inDB = true;
00200 $ret[] = $obj;
00201 }
00202
00203 return $ret;
00204 }
00205
00217 public static function getList($constraints = array(), $limit = array(), $order = array(), $cnt = false) {
00218 return self::getListImpl($constraints, $limit, $order, $cnt);
00219 }
00220
00230 public static function getResourceAlignments($urlOrId, $mode = TSD::MODE_LIVE) {
00231 if(($id = self::getCWISID($urlOrId)) === false)
00232 return false;
00233 switch($mode) {
00234 case TSD::MODE_LIVE:
00235 $modeArray = array('AND', 'resourceId' => $id, 'version' => TSD::VERSION_LIVE);
00236 break;
00237 case TSD::MODE_DEV:
00238 $modeArray = array('AND', 'resourceId' => $id, 'version' => TSD::VERSION_DEV);
00239 break;
00240 case TSD::MODE_DEV_ACTIVE:
00241 $modeArray = array('AND', 'resourceId' => $id, 'version' => TSD::VERSION_DEV,
00242 '<>', 'status' => TSD::STATUS_DELETED);
00243 break;
00244 case TSD::MODE_NEW:
00245 $modeArray = array('AND', 'resourceId' => $id, 'version' => TSD::VERSION_DEV, 'status' => TSD::STATUS_NEW);
00246 break;
00247 case TSD::MODE_CHANGED:
00248 $modeArray = array('AND', 'resourceId' => $id, 'version' => TSD::VERSION_DEV, 'status' => TSD::STATUS_CHANGED);
00249 break;
00250 case TSD::MODE_DELETED:
00251 $modeArray = array('AND', 'resourceId' => $id, 'version' => TSD::VERSION_DEV, 'status' => TSD::STATUS_DELETED);
00252 break;
00253 case TSD::MODE_APPROVED:
00254 $modeArray = array('AND', 'resourceId' => $id, 'version' => TSD::VERSION_DEV, 'status' => TSD::STATUS_APPROVED);
00255 break;
00256 case TSD::MODE_ALL:
00257 $modeArray = array();
00258 break;
00259 }
00260 return self::getListImpl($modeArray, array(), array(), false, 'alignments');
00261 }
00262
00270 public function commit() {
00271 $this->setError('You cannot commit TSDText objects!');
00272 return false;
00273 }
00274
00282 public function getParent() {
00283 return null;
00284 }
00285
00293 public function getIdentifier() {
00294 return $this->section_name;
00295 }
00296
00308 public function listChildren($constraints = array(), $limit = array(), $order = array(), $cnt = false) {
00309 return null;
00310 }
00311
00323 public function listValidChildren($constraints = array(), $limit = array(), $order = array(), $cnt = false) {
00324 return null;
00325 }
00326 }
00327
00328 ?>