00001 <?php
00002
00012 abstract class TSDService extends SObject {
00062 public static function getAlignments($req) {
00063 switch($req->getMode()) {
00064 case TSD::MODE_LIVE:
00065 $where = ' AND version = \'LIVE\'';
00066 break;
00067 case TSD::MODE_DEV:
00068 $where = ' AND version = \'DEV\'';
00069 break;
00070 case TSD::MODE_DEV_ACTIVE:
00071 $where = ' AND version = \'DEV\' AND status <> \'DELETED\'';
00072 break;
00073 case TSD::MODE_DEV_UNAPPROVED:
00074 $where = ' AND version = \'DEV\' AND status <> \'APPROVED\'';
00075 break;
00076 case TSD::MODE_NEW:
00077 $where = ' AND version = \'DEV\' AND status = \'NEW\'';
00078 break;
00079 case TSD::MODE_CHANGED:
00080 $where = ' AND version = \'DEV\' AND status = \'CHANGED\'';
00081 break;
00082 case TSD::MODE_DELETED:
00083 $where = ' AND version = \'DEV\' AND status = \'DELETED\'';
00084 break;
00085 case TSD::MODE_APPROVED:
00086 $where = ' AND version = \'DEV\' AND status = \'APPROVED\'';
00087 break;
00088 case TSD::MODE_ALL:
00089 $where = '';
00090 break;
00091 }
00092 $resourceId = $req->getResourceIds();
00093 $multiResource = false;
00094 if(count($resourceId) > 0) {
00095 $where2 = '';
00096 $multiResource = array();
00097 foreach($resourceId as $resId) {
00098 $multiResource[] = $resId;
00099 $where2 .= "$resId,";
00100 }
00101 if($where2 != '')
00102 $where2 = ' (resourceId IN (' . substr($where2, 0, -1) . ')) ';
00103 if($where != '')
00104 $where = ' WHERE ' . $where2 . $where;
00105 else
00106 $where = ' WHERE ' . $where2;
00107 }
00108 else if($where != '')
00109 $where = ' WHERE ' . substr($where, 4);
00110 $limitClause = '';
00111 if($req->getLimit() !== false || $req->getOffset() !== false) {
00112 $limit = intval($req->getLimit());
00113 $offset = intval($req->getOffset());
00114 if($offset !== false && $limit !== false)
00115 $limitClause = " LIMIT $offset, $limit";
00116 else if($limit !== false)
00117 $limitClause = " LIMIT $limit";
00118 }
00119 $types = $req->getTypes();
00120 $types['alignments'] = true;
00121 return self::treeListing('alignments', $types, $where, '', $multiResource, $req->getOperation(), $limitClause);
00122 }
00123
00157 public static function getRelationships($req, $asXML = true) {
00158 $resWhere = '';
00159 $resourceId = $req->getResourceIds();
00160 if(count($resourceId) > 0) {
00161 $whereA = '';
00162 $whereB = '';
00163 foreach($resourceId as $id) {
00164 $whereA .= "$id,";
00165 $whereB .= "$id,";
00166 #$resWhere .= " sourceId = $id OR destId = $id OR ";
00167 }
00168 $whereA = substr($whereA, 0, -1);
00169 $whereB = substr($whereB, 0, -1);
00170 $resWhere = "( sourceId IN ($whereA) OR destId IN ($whereB) )";
00171 }
00172 else
00173 $resWhere = ' TRUE ';
00174 $query = 'SELECT sourceId, destId, description, version, status FROM TSDRelation WHERE (' . $resWhere . ')';
00175 switch($req->getMode()) {
00176 case TSD::MODE_LIVE:
00177 $query .= ' AND version = \'LIVE\'';
00178 break;
00179 case TSD::MODE_DEV:
00180 $query .= ' AND version = \'DEV\'';
00181 break;
00182 case TSD::MODE_DEV_ACTIVE:
00183 $query .= ' AND version = \'DEV\' AND status <> \'DELETED\'';
00184 break;
00185 case TSD::MODE_DEV_UNAPPROVED:
00186 $query .= ' AND version = \'DEV\' AND status <> \'APPROVED\'';
00187 break;
00188 case TSD::MODE_NEW:
00189 $query .= ' AND version = \'DEV\' AND status = \'NEW\'';
00190 break;
00191 case TSD::MODE_CHANGED:
00192 $query .= ' AND version = \'DEV\' AND status = \'CHANGED\'';
00193 break;
00194 case TSD::MODE_DELETED:
00195 $query .= ' AND version = \'DEV\' AND status = \'DELETED\'';
00196 break;
00197 case TSD::MODE_APPROVED:
00198 $query .= ' AND version = \'DEV\' AND status = \'APPROVED\'';
00199 break;
00200 case TSD::MODE_ALL:
00201 $query .= '';
00202 break;
00203 }
00204 if($req->getLimit() !== false || $req->getOffset() !== false) {
00205 $limit = intval($req->getLimit());
00206 $offset = intval($req->getOffset());
00207 if($offset !== false && $limit !== false)
00208 $query .= " LIMIT $offset, $limit";
00209 else if($limit !== false)
00210 $query .= " LIMIT $limit";
00211 }
00212 $result = TSD::getDBI()->query($query);
00213 if(count(TSD::getDBI()->getError()) > 0) {
00214 self::setStaticError('Database error(s): ' . implode('; ', TSD::getDBI()->getError()));
00215 return null;
00216 }
00217 if(count($resourceId) > 0) {
00218 $infoArray = array();
00219 $xmlArray = array();
00220 foreach($resourceId as $id) {
00221 $xmlArray[$id] = '<relationships><resourceId>' . $id . '</resourceId>';
00222 $infoArray[$id] = array();
00223 }
00224 foreach($result as $r) {
00225 $r['version'] = strtolower($r['version']);
00226 $r['status'] = strtolower($r['status']);
00227 if(in_array($r['sourceId'], $resourceId)) {
00228 $id = $r['sourceId'];
00229 $oid = $r['destId'];
00230 }
00231 else if(in_array($r['destId'], $resourceId)) {
00232 $id = $r['destId'];
00233 $oid = $r['sourceId'];
00234 }
00235 $infoArray[$id][$oid] = array();
00236 $infoArray[$id][$oid]['sourceId'] = $r['sourceId'];
00237 $infoArray[$id][$oid]['destId'] = $r['destId'];
00238 $infoArray[$id][$oid]['description'] = $r['description'];
00239 $infoArray[$id][$oid]['version'] = $r['version'];
00240 $infoArray[$id][$oid]['status'] = $r['status'];
00241 $xml = $xmlArray[$id];
00242 $xml .= "<relationship><resourceId>$oid</resourceId>";
00243 $xml .= "<sourceId>$r[sourceId]</sourceId><destId>$r[destId]</destId>";
00244 $xml .= '<description>' . htmlspecialchars($r['description']) . '</description>';
00245 $xml .= '<version>' . $r['version'] . '</version>';
00246 $xml .= '<status>' . $r['status'] . '</status>';
00247 $xml .= '</relationship>';
00248 $xmlArray[$id] = $xml;
00249 }
00250 foreach($resourceId as $id)
00251 $xmlArray[$id] .= '</relationships>';
00252 if($asXML)
00253 return $xmlArray;
00254 else
00255 return $infoArray;
00256 }
00257 else {
00258 $xml = '<relationships>';
00259 $infoArray = array();
00260 foreach($result as $r) {
00261 $r['version'] = strtolower($r['version']);
00262 $r['status'] = strtolower($r['status']);
00263 $info = array();
00264 $info['sourceId'] = $r['sourceId'];
00265 $info['destId'] = $r['destId'];
00266 $info['description'] = $r['description'];
00267 $info['version'] = $r['version'];
00268 $info['status'] = $r['status'];
00269 $xml .= '<relationship>';
00270 $xml .= "<sourceId>$r[sourceId]</sourceId><destId>$r[destId]</destId>";
00271 $xml .= '<description>' . htmlspecialchars($r['description']) . '</description>';
00272 $xml .= "<status>$r[status]</status>";
00273 $xml .= "<version>$r[version]</version>";
00274 $xml .= '</relationship>';
00275 $infoArray[] = $info;
00276 }
00277 $xml .= '</relationships>';
00278 if($asXML)
00279 return $xml;
00280 else
00281 return $infoArray;
00282 }
00283 }
00284
00298 public static function getResourcesAligned($type, $id, $mode = TSDRequest::MODE_APPROVED, $offset = false, $limit = false) {
00299 switch($type) {
00300 case 'section':
00301 $query = 'SELECT resourceId FROM TSDTextbookAlignment WHERE sectionId = ' . $id;
00302 break;
00303 case 'objective':
00304 $query = 'SELECT resourceId FROM TSDStandardAlignment WHERE objectiveId = ' . $id;
00305 break;
00306 case 'word':
00307 $query = 'SELECT resourceId FROM TSDDictionaryAlignment WHERE word = \'' . mysql_escape_string($id) . '\'';
00308 break;
00309 default:
00310 self::setStaticError('Invalid type: ' . $type);
00311 return null;
00312 }
00313
00314 switch($mode) {
00315 case TSD::MODE_LIVE:
00316 $query .= ' AND status = \'APPROVED\'';
00317 break;
00318 case TSD::MODE_DEV:
00319 $query .= ' AND status = \'APPROVED\'';
00320 break;
00321 case TSD::MODE_DEV_ACTIVE:
00322 $query .= ' AND status <> \'DELETED\'';
00323 break;
00324 case TSD::MODE_DEV_UNAPPROVED:
00325 $query .= ' AND status <> \'APPROVED\'';
00326 break;
00327 case TSD::MODE_NEW:
00328 $query .= ' AND status = \'NEW\'';
00329 break;
00330 case TSD::MODE_CHANGED:
00331 $query .= ' AND status = \'CHANGED\'';
00332 break;
00333 case TSD::MODE_DELETED:
00334 $query .= ' AND status = \'DELETED\'';
00335 break;
00336 case TSD::MODE_APPROVED:
00337 $query .= ' AND status = \'APPROVED\'';
00338 break;
00339 case TSD::MODE_ALL:
00340 $query .= '';
00341 break;
00342 }
00343 if($limit !== false || $offset !== false) {
00344 if($offset !== false && $limit !== false)
00345 $query .= " LIMIT $offset, $limit";
00346 else if($limit !== false)
00347 $query .= " LIMIT $limit";
00348 }
00349 $DBI = TSD::getDBI();
00350 $result = $DBI->query($query);
00351 if(count($DBI->getError()) > 0) {
00352 self::setStaticError('Database error(s): ' . implode('; ', $DBI->getError()));
00353 return null;
00354 }
00355 $resources = array();
00356 foreach($result as $r)
00357 $resources[] = $r['resourceId'];
00358 return $resources;
00359 }
00360
00371 public static function getAlignmentsByStatus($req, $keyOn = 'resource', $asXML = false) {
00372 $mode = $req->getMode();
00373 $where = ' WHERE TRUE';
00374
00375 switch($mode) {
00376 case TSD::MODE_LIVE:
00377 $where = ' WHERE status = \'APPROVED\'';
00378 break;
00379 case TSD::MODE_DEV:
00380 $where = ' WHERE status = \'APPROVED\'';
00381 break;
00382 case TSD::MODE_DEV_ACTIVE:
00383 $where = ' WHERE status <> \'DELETED\'';
00384 break;
00385 case TSD::MODE_DEV_UNAPPROVED:
00386 $where = ' WHERE status <> \'APPROVED\'';
00387 break;
00388 case TSD::MODE_NEW:
00389 $where = ' WHERE status = \'NEW\'';
00390 break;
00391 case TSD::MODE_CHANGED:
00392 $where = ' WHERE status = \'CHANGED\'';
00393 break;
00394 case TSD::MODE_DELETED:
00395 $where = ' WHERE status = \'DELETED\'';
00396 break;
00397 case TSD::MODE_APPROVED:
00398 $where = ' WHERE status = \'APPROVED\'';
00399 break;
00400 case TSD::MODE_ALL:
00401 $where = ' WHERE TRUE';
00402 break;
00403 }
00404
00405 $types = $req->getTypes();
00406 $from = array();
00407 if(in_array(TSDRequest::TYPE_TEXTBOOKS, $types))
00408 $from[] = "SELECT resourceId, reason, version, status, 'standard' AS type FROM TSDStandardAlignment $where";
00409 if(in_array(TSDRequest::TYPE_STANDARDS, $types))
00410 $from[] = "SELECT resourceId, reason, version, status, 'standard' AS type FROM TSDStandardAlignment $where";
00411 if(in_array(TSDRequest::TYPE_DICTIONARY, $types))
00412 $from[] = "SELECT resourceId, reason, version, status, 'word' AS type FROM TSDDictionaryAlignment $where";
00413 $from = implode(' UNION ', $from);
00414
00415 $query = "SELECT resourceId, reason, version, status, type FROM ($from) AS unionSet";
00416 if(intval($req->getLimit()) !== false || intval($req->getOffset()) !== false) {
00417 $limit = intval($req->getLimit());
00418 $offset = intval($req->getOffset());
00419 if($offset !== false && $limit !== false)
00420 $query .= " LIMIT $offset, $limit";
00421 else if($limit !== false)
00422 $query .= " LIMIT $limit";
00423 }
00424
00425 $DBI = TSD::getDBI();
00426 $results = $DBI->query($query);
00427 if(count($DBI->getError()) > 0) {
00428 self::setStaticError('Database error(s): ' . implode('; ', $DBI->getError()));
00429 return null;
00430 }
00431
00432 switch(strtolower($keyOn)) {
00433 default:
00434 case 'resource':
00435 if($asXML) {
00436 $xmlArray = array();
00437 foreach($results as $r) {
00438 if(!isset($xmlArray[$r['resourceId']]))
00439 $xmlArray[$r['resourceId']] = '<resource><resourceId>' . $r['resourceId'] . '</resourceId>';
00440 $xmlArray[$r['resourceId']] .= '<alignment>'
00441 . '<resourceId>' . $r['resourceId'] . ' </resourceId>'
00442 . '<reason>' . htmlspecialchars($r['reason']) . '</reason>'
00443 . '<status>' . strtolower($r['status']) . '</status>'
00444 . '<version>' . strtolower($r['version']) . '</version>'
00445 . '<type>' . $r['type'] . '</type></alignment>';
00446 }
00447 foreach($xmlArray as $id => $c)
00448 $xmlArray[$id] .= '</resource>';
00449 $out = '<alignments>' . implode('', $xmlArray) . '</alignments>';
00450 }
00451 else {
00452 $out = array();
00453 foreach($results as $r) {
00454 if(!isset($out[$r['resourceId']]))
00455 $out[$r['resourceId']] = array();
00456 $out[$r['resourceId']][] = array(
00457 'resourceId' => $r['resourceId'],
00458 'reason' => $r['reason'],
00459 'status' => strtolower($r['status']),
00460 'version' => strtolower($r['version']),
00461 'type' => $r['type']
00462 );
00463 }
00464 }
00465 break;
00466 case 'result':
00467 if($asXML) {
00468 $out = '<alignments>';
00469 foreach($results as $r)
00470 $out .= '<alignment>'
00471 . '<resourceId>' . $r['resourceId'] . ' </resourceId>'
00472 . '<reason>' . htmlspecialchars($r['reason']) . '</reason>'
00473 . '<status>' . strtolower($r['status']) . '</status>'
00474 . '<version>' . strtolower($r['version']) . '</version>'
00475 . '<type>' . $r['type'] . '</type></alignment>';
00476 $out .= '</alignments>';
00477 }
00478 else
00479 $out = $results;
00480 break;
00481 }
00482
00483 return $out;
00484 }
00485
00496 public static function buildConstraints($req, $limit, $blah) {
00497 $mode = $req->getMode();
00498 $where = '';
00499
00500 switch($mode) {
00501 case TSD::MODE_LIVE:
00502 $where = ' status = \'APPROVED\'';
00503 break;
00504 case TSD::MODE_DEV:
00505 $where = ' status = \'APPROVED\'';
00506 break;
00507 case TSD::MODE_DEV_ACTIVE:
00508 $where = ' status <> \'DELETED\'';
00509 break;
00510 case TSD::MODE_DEV_UNAPPROVED:
00511 $where = ' status <> \'APPROVED\'';
00512 break;
00513 case TSD::MODE_NEW:
00514 $where = ' status = \'NEW\'';
00515 break;
00516 case TSD::MODE_CHANGED:
00517 $where = ' status = \'CHANGED\'';
00518 break;
00519 case TSD::MODE_DELETED:
00520 $where = ' status = \'DELETED\'';
00521 break;
00522 case TSD::MODE_APPROVED:
00523 $where = ' status = \'APPROVED\'';
00524 break;
00525 case TSD::MODE_ALL:
00526 $where = '';
00527 break;
00528 }
00529
00530 $query = "SELECT DISTINCT limit{$limit}.cserdId FROM $blah AS limit{$limit} LEFT JOIN";
00531
00532 switch($req->getOperation()) {
00533 case TSDRequest::OP_FILTER_ALIGNED:
00534 $type = $req->getObjectType();
00535 $id = $req->getObjectId();
00536 switch($type) {
00537 case 'objective':
00538 if($where != '')
00539 $where = ' AND ' . $where;
00540 $query .= " TSDStandardAlignment ON limit{$limit}.cserdId = TSDStandardAlignment.resourceId"
00541 . " WHERE objectiveId = $id $where";
00542 break;
00543 case 'section':
00544 if($where != '')
00545 $where = ' AND ' . $where;
00546 $query .= " TSDTextbookAlignment ON limit{$limit}.cserdId = TSDTextbookAlignment.resourceId"
00547 . " WHERE sectionId = $id $where";
00548 break;
00549 case 'word':
00550 if($where != '')
00551 $where = ' AND ' . $where;
00552 $query .= " TSDDictionaryAlignment ON limit{$limit}.cserdId = TSDDictionaryAlignment.resourceId"
00553 . " WHERE word = '" . mysql_escape_string($id) . "' $where";
00554 break;
00555 }
00556 break;
00557 case TSDRequest::OP_FILTER_RELATED:
00558 $resourceId = $req->getResourceIds();
00559 $where2 = ' WHERE (sourceId IS NOT NULL AND destId IS NOT NULL) AND ((sourceId = \'' . $resourceId[0]
00560 . '\' OR destId = \'' . $resourceId[0] . '\')';
00561 if($where != '')
00562 $where2 .= ' AND ' . $where;
00563 $where2 .= ')';
00564 $query .= " TSDRelation ON limit{$limit}.cserdId = TSDRelation.sourceId OR limit{$limit}.cserdId = TSDRelation.destId $where2";
00565 $query = "SELECT cserdId FROM ($query) AS RelatedTo WHERE cserdId <> $resourceId[0] ";
00566 break;
00567 case TSDRequest::OP_FILTER_MODE:
00568 $types = $req->getTypes();
00569 $from = array();
00570 if($types[TSDRequest::TYPE_RELATIONS]) {
00571 $from[] = "SELECT sourceId AS cserdId FROM TSDRelation WHERE $where";
00572 $from[] = "SELECT destId AS cserdId FROM TSDRelation WHERE $where";
00573 }
00574 if($types[TSDRequest::TYPE_TEXTBOOKS])
00575 $from[] = "SELECT resourceId AS cserdId FROM TSDStandardAlignment WHERE $where";
00576 if($types[TSDRequest::TYPE_STANDARDS])
00577 $from[] = "SELECT resourceId AS cserdId FROM TSDTextbookAlignment WHERE $where";
00578 if($types[TSDRequest::TYPE_DICTIONARY])
00579 $from[] = "SELECT resourceId AS cserdId FROM TSDDictionaryAlignment WHERE $where";
00580 $from = implode(' UNION ', $from);
00581 $query = $from;
00582 break;
00583 }
00584 return $query . ' ';
00585 }
00586
00621 public static function getTree($rootType, $rootId = '', $recursive = true) {
00622 switch($rootType) {
00623 case 'book':
00624 case 'chapter':
00625 case 'section':
00626 return self::treeListing('tree', array('textbooks' => true), $rootId == '' ? '' :
00627 " WHERE TSD" . ucfirst($rootType) . ".id = '$rootId' ", $recursive ? '' : $rootType);
00628 case 'organization':
00629 case 'grade':
00630 case 'category':
00631 case 'objective':
00632 return self::treeListing('tree', array('standards' => true), $rootId == '' ? '' :
00633 " WHERE TSD" . ucfirst($rootType) . ".id = '$rootId' ",
00634 $recursive ? '' : $rootType);
00635 case 'word':
00636 return self::treeListing('tree', array('dictionary' => true), $rootId == '' ? '' :
00637 " WHERE word = '" . mysql_escape_string($rootId) . "' ", $recursive ? '' : $rootType);
00638 }
00639 return null;
00640 }
00641
00662 private static function treeListing($root, $types, $where, $depth = '', $multiResource = false, $op = '', $limitClause = '') {
00663 $DBI = TSD::getDBI();
00664
00665 if($multiResource) {
00666 $resources = array();
00667 foreach($multiResource as $k)
00668 $resources[$k] = "<$root><resourceId>$k</resourceId>";
00669 }
00670 else
00671 $xml = "<$root>";
00672 if(isset($types['standards']) && $types['standards'] == true) {
00673 if(isset($types['alignments'])) {
00674 $ob = ', TSDStandardAlignment.resourceId';
00675 $first = 'TSDStandardAlignment.resourceId, TSDStandardAlignment.reason, TSDStandardAlignment.version, '
00676 . 'TSDStandardAlignment.status,';
00677 $basis = <<<END_QUERY
00678 FROM TSDStandardAlignment
00679 LEFT JOIN TSDObjective ON TSDObjective.id = TSDStandardAlignment.objectiveId
00680 END_QUERY;
00681 }
00682 else {
00683 $ob = '';
00684 $first = '';
00685 $basis = ' FROM TSDObjective ';
00686 }
00687 if($op == 'count')
00688 $query = 'SELECT COUNT(objectiveId), resourceId ';
00689 else
00690 $query = <<<END_QUERY
00691 SELECT
00692 $first
00693 TSDObjective.id AS objective_id,
00694 TSDCategory.id AS category_id,
00695 TSDGrade.id AS grade_id,
00696 TSDOrganization.id AS organization_id,
00697 TSDObjective.description AS objective_description,
00698 TSDCategory.name AS category_name,
00699 TSDGrade.name AS grade_name,
00700 TSDOrganization.name AS organization_name,
00701 TSDOrganization.url AS organization_url,
00702 TSDOrganization.state AS organization_state,
00703 TSDOrganization.address AS organization_address,
00704 TSDOrganization.description AS organization_description
00705 END_QUERY;
00706 $query .= <<<END_QUERY
00707 $basis
00708 LEFT JOIN TSDCategory ON TSDCategory.id = TSDObjective.categoryId
00709 LEFT JOIN TSDGrade ON TSDGrade.id = TSDCategory.gradeId
00710 LEFT JOIN TSDOrganization ON TSDOrganization.id = TSDGrade.organizationId
00711 $where
00712 END_QUERY;
00713 if($op == 'count')
00714 $query .= ' GROUP BY resourceId';
00715 else
00716 $query .= " ORDER BY resourceId, TSDOrganization.id, TSDGrade.id, TSDCategory.id, TSDObjective.id $ob";
00717 $query .= $limitClause;
00718 $result = $DBI->query($query);
00719 if(count($DBI->getError()) > 0) {
00720 self::setStaticError('Database error(s): ' . implode('; ', $DBI->getError()));
00721 return null;
00722 }
00723
00724 $res = 0;
00725 for($i = 0; $i < count($result); ) {
00726 if($multiResource && $res != $result[$i]['resourceId']) {
00727 if($res != 0)
00728 $resources[$res] = $xml;
00729 $res = $result[$i]['resourceId'];
00730 $xml = $resources[$res];
00731 if($op == 'count') {
00732 $xml .= "<standards>{$result[$i]['COUNT(objectiveId)']}</standards>";
00733 $i++;
00734 }
00735 }
00736 if($op != 'count') {
00737 $xml .= "<organization><id>{$result[$i]['organization_id']}</id><name>"
00738 . htmlspecialchars($result[$i]['organization_name']) . "</name><url>"
00739 . htmlspecialchars($result[$i]['organization_url']) . "</url><state>"
00740 . htmlspecialchars($result[$i]['organization_state']) . "</state><description>"
00741 . htmlspecialchars($result[$i]['organization_description']) . "</description>";
00742 $org = $result[$i]['organization_id'];
00743 while($depth == 'organization' && $i < count($result) && $result[$i]['organization_id'] == $org
00744 && (!$multiResource || $result[$i]['resourceId'] == $res))
00745 $i++;
00746 while($i < count($result) && $result[$i]['organization_id'] == $org
00747 && (!$multiResource || $result[$i]['resourceId'] == $res))
00748 {
00749 $xml .= "<grade><id>{$result[$i]['grade_id']}</id><name>"
00750 . htmlspecialchars($result[$i]['grade_name']) . "</name>";
00751 $grade = $result[$i]['grade_id'];
00752 while($depth == 'grade' && $i < count($result) && $result[$i]['grade_id'] == $grade
00753 && (!$multiResource || $result[$i]['resourceId'] == $res))
00754 $i++;
00755 while($i < count($result) && $result[$i]['grade_id'] == $grade
00756 && (!$multiResource || $result[$i]['resourceId'] == $res))
00757 {
00758 $xml .= "<category><id>{$result[$i]['category_id']}</id><name>"
00759 . htmlspecialchars($result[$i]['category_name']) . "</name>";
00760 $category = $result[$i]['category_id'];
00761 while($depth == 'category' && $i < count($result) && $result[$i]['category_id'] == $category
00762 && (!$multiResource || $result[$i]['resourceId'] == $res))
00763 $i++;
00764 while($i < count($result) && $result[$i]['category_id'] == $category
00765 && (!$multiResource || $result[$i]['resourceId'] == $res))
00766 {
00767 $xml .= "<objective><id>{$result[$i]['objective_id']}</id><name>"
00768 . htmlspecialchars($result[$i]['objective_description']) . "</name>";
00769 $objective = $result[$i]['objective_id'];
00770 while($i < count($result) && $result[$i]['objective_id'] == $objective
00771 && (!$multiResource || $result[$i]['resourceId'] == $res))
00772 {
00773 if(isset($types['alignments']))
00774 $xml .= '<alignment><resourceId>' . $result[$i]['resourceId'] . '</resourceId>'
00775 . '<reason>' . htmlspecialchars($result[$i]['reason']) . '</reason>'
00776 . '<version>' . strtolower($result[$i]['version']) . '</version>'
00777 . '<status>' . strtolower($result[$i]['status']) . '</status></alignment>';
00778 $i++;
00779 }
00780 $xml .= "</objective>";
00781 }
00782 $xml .= "</category>";
00783 }
00784 $xml .= "</grade>";
00785 }
00786 $xml .= "</organization>";
00787 }
00788 }
00789 if($multiResource && $res != 0) {
00790 $resources[$res] = $xml;
00791 }
00792 }
00793
00794 if(isset($types['textbooks']) && $types['textbooks'] == true) {
00795 if(isset($types['alignments'])) {
00796 $ob = ', TSDTextbookAlignment.resourceId';
00797 $first = 'TSDTextbookAlignment.resourceId, TSDTextbookAlignment.reason, TSDTextbookAlignment.version, '
00798 . 'TSDTextbookAlignment.status, ';
00799 $basis = <<<END_QUERY
00800 FROM TSDTextbookAlignment
00801 LEFT JOIN TSDSection ON TSDSection.id = TSDTextbookAlignment.sectionId
00802 END_QUERY;
00803 }
00804 else {
00805 $ob = '';
00806 $first = '';
00807 $basis = ' FROM TSDSection ';
00808 }
00809
00810 if($op == 'count')
00811 $query = 'SELECT COUNT(sectionId), resourceId ';
00812 else
00813 $query = <<<END_QUERY
00814 SELECT
00815 $first
00816 TSDSection.id AS section_id,
00817 TSDChapter.id AS chapter_id,
00818 TSDBook.id AS book_id,
00819 TSDSection.name AS section_name,
00820 TSDSection.description AS section_description,
00821 TSDChapter.name AS chapter_name,
00822 TSDBook.title AS book_title,
00823 TSDBook.gradeOrCourse AS book_gradeOrCourse
00824 END_QUERY;
00825 $query .= <<<END_QUERY
00826 $basis
00827 LEFT JOIN TSDChapter ON TSDChapter.id = TSDSection.chapterId
00828 LEFT JOIN TSDBook ON TSDBook.id = TSDChapter.bookId
00829 $where
00830 END_QUERY;
00831 if($op == 'count')
00832 $query .= ' GROUP BY resourceId ';
00833 else
00834 $query .= "ORDER BY resourceId, TSDBook.id, TSDChapter.id, TSDSection.id $ob ";
00835 $query .= $limitClause;
00836 $result2 = $DBI->query($query);
00837 if(count($DBI->getError()) > 0) {
00838 self::setStaticError('Database error(s): ' . implode('; ', $DBI->getError()));
00839 return null;
00840 }
00841
00842 $res = 0;
00843 for($i = 0; $i < count($result2); ) {
00844 if($multiResource && $res != $result2[$i]['resourceId']) {
00845 if($res != 0)
00846 $resources[$res] = $xml;
00847 $res = $result2[$i]['resourceId'];
00848 $xml = $resources[$res];
00849 if($op == 'count') {
00850 $xml .= "<textbooks>{$result2[$i]['COUNT(sectionId)']}</textbooks>";
00851 $i++;
00852 }
00853 }
00854 if($op != 'count') {
00855 $xml .= "<book><id>{$result2[$i]['book_id']}</id><title>"
00856 . htmlspecialchars($result2[$i]['book_title']) . "</title><gradeOrCourse>"
00857 . htmlspecialchars($result2[$i]['book_gradeOrCourse']) . "</gradeOrCourse>";
00858 $book = $result2[$i]['book_id'];
00859 while($depth == 'book' && $i < count($result2) && $result2[$i]['book_id'] == $book
00860 && (!$multiResource || $result2[$i]['resourceId'] == $res))
00861 $i++;
00862 while($i < count($result2) && $result2[$i]['book_id'] == $book
00863 && (!$multiResource || $result2[$i]['resourceId'] == $res))
00864 {
00865 $xml .= "<chapter><id>{$result2[$i]['chapter_id']}</id><name>"
00866 . htmlspecialchars($result2[$i]['chapter_name']) . "</name>";
00867 $chapter = $result2[$i]['chapter_id'];
00868 while($depth == 'chapter' && $i < count($result2) && $result2[$i]['chapter_id'] == $chapter
00869 && (!$multiResource || $result2[$i]['resourceId'] == $res))
00870 $i++;
00871 while($i < count($result2) && $result2[$i]['chapter_id'] == $chapter
00872 && (!$multiResource || $result2[$i]['resourceId'] == $res))
00873 {
00874 $xml .= "<section><id>{$result2[$i]['section_id']}</id><name>"
00875 . htmlspecialchars($result2[$i]['section_name']) . "</name><description>"
00876 . htmlspecialchars($result2[$i]['section_description']) . "</description>";
00877 $section = $result2[$i]['section_id'];
00878 while($i < count($result2) && $result2[$i]['section_id'] == $section) {
00879 if(isset($types['alignments']))
00880 $xml .= '<alignment><resourceId>' . $result2[$i]['resourceId'] . '</resourceId>'
00881 . '<reason>' . htmlspecialchars($result2[$i]['reason']) . '</reason>'
00882 . '<version>' . strtolower($result2[$i]['version']) . '</version>'
00883 . '<status>' . strtolower($result2[$i]['status']) . '</status></alignment>';
00884 $i++;
00885 }
00886 $xml .= "</section>";
00887 }
00888 $xml .= "</chapter>";
00889 }
00890 $xml .= "</book>";
00891 }
00892 }
00893 if($multiResource && $res != 0) {
00894 $resources[$res] = $xml;
00895 }
00896 }
00897
00898 if(isset($types['dictionary']) && $types['dictionary'] == true) {
00899 if(isset($types['alignments'])) {
00900 $ob = ', TSDDictionaryAlignment.resourceId';
00901 $reason = 'resourceId, reason, version, status, ';
00902 $basis = ' TSDDictionaryAlignment LEFT JOIN TSDDictionary ON TSDDictionaryAlignment.word = TSDDictionary.word ';
00903 }
00904 else {
00905 $ob = '';
00906 $reason = '';
00907 $basis = 'TSDDictionary';
00908 }
00909 if($op == 'count')
00910 $query = "SELECT COUNT(TSDDictionary.word), resourceId FROM ";
00911 else
00912 $query = "SELECT $reason TSDDictionary.word, TSDDictionary.definition FROM ";
00913 $query .= "$basis $where ";
00914 if($op == 'count')
00915 $query .= ' GROUP BY resourceId ';
00916 else
00917 $query .= " ORDER BY resourceId, TSDDictionary.word $ob";
00918 $query .= $limitClause;
00919 $result = $DBI->query($query);
00920 if(count($DBI->getError()) > 0) {
00921 self::setStaticError('Database error(s): ' . implode('; ', $DBI->getError()));
00922 return null;
00923 }
00924
00925 $res = 0;
00926 for($i = 0; $i < count($result); ) {
00927 if($multiResource && $res != $result[$i]['resourceId']) {
00928 if($res != 0)
00929 $resources[$res] = $xml;
00930 $res = $result[$i]['resourceId'];
00931 $xml = $resources[$res];
00932 if($op == 'count') {
00933 $xml .= "<dictionary>{$result[$i]['COUNT(TSDDictionary.word)']}</dictionary>";
00934 $i++;
00935 }
00936 }
00937 if($op != 'count') {
00938 $r = $result[$i];
00939 $xml .= "<word><word>" . htmlspecialchars($r['word']) . "</word><definition>" . htmlspecialchars($r['definition'])
00940 . "</definition>";
00941 $word = $result[$i]['word'];
00942 while($i < count($result) && $result[$i]['word'] == $word
00943 && (!$multiResource || $result[$i]['resourceId'] == $res))
00944 {
00945 if(isset($types['alignments']))
00946 $xml .= '<alignment><resourceId>' . $r['resourceId'] . '</resourceId>'
00947 . '<reason>' . htmlspecialchars($result[$i]['reason']) . '</reason>'
00948 . '<version>' . strtolower($result[$i]['version']) . '</version>'
00949 . '<status>' . strtolower($result[$i]['status']) . '</status></alignment>';
00950 $i++;
00951 }
00952 $xml .= '</word>';
00953 }
00954 }
00955 if($multiResource && $res != 0) {
00956 $resources[$res] = $xml;
00957 }
00958 }
00959
00960 if(!$multiResource) {
00961 $xml .= "</$root>";
00962 return $xml;
00963 }
00964 else {
00965 foreach($resources as $id => $res)
00966 $resources[$id] .= "</$root>";
00967 return $resources;
00968 }
00969 }
00970 }
00971
00972 ?>