00001 <?php
00002
00017 class SnapContentInteractivateLesson extends SnapContent {
00018 protected $NEED_TRANSLATION = true;
00019
00020 protected $currentXML = null;
00021 protected $loadErrors = array();
00022
00036 protected function prepareAndValidateXML($content, $field, $pre = '', $post = '') {
00037 $content = ltrim(rtrim($content));
00038 $pre = '<XMLResource><section>' . $pre;
00039 $post = $post . '</section></XMLResource>';
00040 $res = XmlFormatter::prepareXML($content, $field, true);
00041 if(is_array($res)) {
00042 $this->loadErrors = array_merge($this->loadErrors, $res);
00043 return false;
00044 }
00045 $res = SXMLHelper::validateXML($in = "$pre$content$post", true);
00046 if(is_array($res)) {
00047 $res = XmlFormatter::prepareXML($content, $field, false);
00048 $res = SXMLHelper::validateXML($in = "$pre$content$post", true);
00049 }
00050 if(is_array($res)) {
00051 foreach($res as $err)
00052 $this->loadErrors[] = "In field <b>$field</b>: $err";
00053 return false;
00054 }
00055 $ret = SXMLHelper::formatXML($content);
00056 if(!$ret)
00057 $ret = $content;
00058 return $ret;
00059 }
00060
00066 public function setTitle($title) {
00067 $this->set('title', $title);
00068 }
00069
00075 public function setAbstract($abstract) {
00076 $abstract = $this->prepareAndValidateXML($abstract, 'Abstract');
00077 if($abstract === false)
00078 return false;
00079 else
00080 $this->set('abstract', $abstract);
00081 }
00082
00086 public function clearObjectives() {
00087 $this->set('objectives', array());
00088 }
00089
00095 public function addObjective($obj) {
00096 $obj = $this->prepareAndValidateXML($obj, 'Objective', '<ul><li>', '</li></ul>');
00097 if($obj === false)
00098 return false;
00099 $objectives = $this->get('objectives');
00100 if(!is_array($objectives))
00101 $objectives = array($obj);
00102 else
00103 $objectives[] = $obj;
00104 $this->set('objectives', $objectives);
00105 }
00106
00110 public function clearPrerequisites() {
00111 $this->set('prereqs', array());
00112 }
00113
00121 public function addPrerequisiteCategory($cat) {
00122 $cat = ltrim(rtrim($cat));
00123 $cat = $this->prepareAndValidateXML($cat, 'Prerequisite Category', '<ul><li>', '</li></ul>');
00124 if($cat === false)
00125 return false;
00126 $prereqs = $this->get('prereqs');
00127 if(!is_array($prereqs))
00128 $prereqs = array($cat => array());
00129 else if(!isset($prereqs[$cat]) || !is_array($prereqs[$cat]))
00130 $prereqs[$cat] = array();
00131 $this->set('prereqs', $prereqs);
00132 return true;
00133 }
00134
00142 public function addPrerequisite($cat, $pre) {
00143 $cat = ltrim(rtrim($cat));
00144 $pre = ltrim(rtrim($pre));
00145 $pre = $this->prepareAndValidateXML($pre, 'Prerequisite', '<ul><li>', '</li></ul>');
00146 if($pre === false)
00147 return false;
00148 $cat = $this->prepareAndValidateXML($cat, 'Prerequisite Category', '<ul><li>', '</li></ul>');
00149 if($cat === false)
00150 return false;
00151 $prereqs = $this->get('prereqs');
00152 if(!is_array($prereqs))
00153 $prereqs = array($cat => array($pre));
00154 else if(!isset($prereqs[$cat]))
00155 $prereqs[$cat] = array($pre);
00156 else
00157 $prereqs[$cat][] = $pre;
00158 $this->set('prereqs', $prereqs);
00159 return true;
00160 }
00161
00167 public function setPreparations($content) {
00168 $content = $this->prepareAndValidateXML($content, 'Teacher Preparations');
00169 if($content === false)
00170 return false;
00171 else
00172 $this->set('preps', $content);
00173 return true;
00174 }
00175
00179 public function clearKeyTerms() {
00180 $this->set('keyTerms', array());
00181 }
00182
00188 public function addKeyTerm($term) {
00189 $word = new TSDWord($term);
00190 if($word->definition == '') {
00191 $this->loadErrors[] = "<b>Key Term</b> '$term' is not a valid key term";
00192 return false;
00193 }
00194 $keyTerms = $this->get('keyTerms');
00195 if(!is_array($keyTerms))
00196 $keyTerms = array($term);
00197 else
00198 $keyTerms[] = $term;
00199 $this->set('keyTerms', $keyTerms);
00200 return true;
00201 }
00202
00206 public function clearSections() {
00207 $this->set('sections', array());
00208 }
00209
00210
00218 public function addSection($name, $content) {
00219 $name = $this->prepareAndValidateXML($name, 'Section Name', '<p><span>', '</span></p>');
00220 if($name === false)
00221 return false;
00222 $content = $this->prepareAndValidateXML($content, "Section '$name'");
00223 if($content === false)
00224 return false;
00225 $sections = $this->get('sections');
00226 if(!is_array($sections))
00227 $sections = array($name => $content);
00228 else
00229 $sections[$name] = $content;
00230 $this->set('sections', $sections);
00231 return true;
00232 }
00233
00239 public function setAlternate($content) {
00240 $content = $this->prepareAndValidateXML($content, 'Alternate Outline');
00241 if($content === false)
00242 return false;
00243 $this->set('alternate', $content);
00244 return true;
00245 }
00246
00250 public function clearFollowUps() {
00251 $this->set('followUp', array());
00252 }
00253
00258 public function setFollowUpIntro($content) {
00259 $content = $this->prepareAndValidateXML($content, 'Follow Up Intro');
00260 if($content === false)
00261 return false;
00262 $this->set('followUpIntro', $content);
00263 return true;
00264 }
00265
00270 public function addFollowUp($content) {
00271 $content = $this->prepareAndValidateXML($content, 'Follow Up', '<ul><li>', '</li></ul>');
00272 if($content === false)
00273 return false;
00274 $followUp = $this->get('followUp');
00275 $followUp[] = $content;
00276 $this->set('followUp', $followUp);
00277 return true;
00278 }
00279
00290 private function grabBlockSection($xml, $xpath, $trimTag = 'p') {
00291 $xpath .= '/' . $trimTag;
00292 $sectionXML = $xml->xpath($xpath);
00293 if(!$sectionXML)
00294 return '';
00295 $section = '';
00296 foreach($sectionXML as $p) {
00297 if($trimTag != '')
00298 $section .= trim(substr($p->asXML(), strlen($trimTag) + 2, -(strlen($trimTag) + 3)));
00299 else
00300 $section .= trim($p->asXML());
00301 }
00302 return $section;
00303 }
00304
00314 private function grabListItems($xml, $xpath) {
00315 $items = $xml->xpath($xpath);
00316 if(!$items)
00317 return array();
00318 $ret = array();
00319 foreach($items as $item) {
00320 $ret[] = trim(substr($item->asXML(), 4, -5));
00321 }
00322 return $ret;
00323 }
00324
00333 public function loadFromXML($content) {
00334 $xml = simplexml_load_string($content);
00335
00336 $title = $xml->xpath('/XMLResource/section[@mapping="title"]/name');
00337 if($title)
00338 $title = (string)$title[0];
00339 else
00340 $title = '';
00341
00342 $abstract = $this->grabBlockSection($xml, '/XMLResource/section[@mapping="abstract"]');
00343
00344 $objectives = $this->grabListItems($xml, '/XMLResource/section[@mapping="objectives"]/p/ul/li');
00345
00346 $prereqXML = $xml->xpath('/XMLResource/section[@mapping="prerequisites"]/p/ul/li');
00347 $prereqs = array();
00348 if($prereqXML)
00349 foreach($prereqXML as $p) {
00350 $cat = trim(preg_replace('/^\s*<li>(.*)<ul>.*<\/ul>\s*<\/li>\s*$/ms', '$1', $p->asXML()));
00351 $prereqs[$cat] = array();
00352 $items = $this->grabListItems($p, '*/li');
00353 foreach($items as $item)
00354 $prereqs[$cat][] = $item;
00355 }
00356
00357 $kts = $xml->xpath('/XMLResource/section[@mapping="keyTerms"]/resourceData/word');
00358 $keyTerms = array();
00359 if($kts)
00360 foreach($kts as $k) {
00361 $keyTerms[] = trim((string)$k);
00362 }
00363
00364 $sects = $xml->xpath('/XMLResource/section[@mapping="outline"]/section[@mapping="section"]');
00365 $sections = array();
00366 if($sects)
00367 foreach($sects as $sect) {
00368 $name = trim((string)$sect->resourceData->name);
00369 $content = $this->grabBlockSection($sect, 'section[@mapping="content"]');
00370 $sections[$name] = $content;
00371 }
00372
00373 $teacherPrep = $this->grabBlockSection($xml, '/XMLResource/section[@mapping="teacherPreparations"]');
00374
00375 $alternate = $this->grabBlockSection($xml, '/XMLResource/section[@mapping="alternateOutline"]');
00376
00377 $followupintro = $xml->xpath('/XMLResource/section[@mapping="followUp"]/p');
00378 if($followupintro) {
00379 $cnt = 0;
00380 foreach($followupintro as $foo)
00381 $cnt++;
00382 if($cnt > 1) {
00383 $followUpBlock = $followupintro[$cnt - 1];
00384 $fui = '';
00385 for($i = 0; $i < $cnt - 1; $i++) {
00386 $fui .= (string)$followupintro[$i]->asXML();
00387 }
00388 $followupintro = trim(substr($fui, 3, -4));
00389 }
00390 else
00391 $followUpBlock = $xml->xpath('/XMLResource/section[@mapping="followUp"]/p');
00392 $followups = $this->grabListItems($followUpBlock, 'ul/li');
00393 }
00394 else {
00395 $followupintro = '';
00396 $followups = array();
00397 }
00398
00399 $this->setTitle($title);
00400 $this->setAbstract($abstract);
00401 foreach($objectives as $obj)
00402 $this->addObjective($obj);
00403 foreach($prereqs as $cat => $pres) {
00404 $this->addPrerequisiteCategory($cat);
00405 foreach($pres as $pre)
00406 $this->addPrerequisite($cat, $pre);
00407 }
00408 foreach($keyTerms as $kt)
00409 $this->addKeyTerm($kt);
00410 foreach($sections as $name => $content)
00411 $this->addSection($name, $content);
00412 $this->setPreparations($teacherPrep);
00413 $this->setAlternate($alternate);
00414 $this->setFollowUpIntro($followupintro);
00415 foreach($followups as $fu)
00416 $this->addFollowUp($fu);
00417 }
00418
00426 public function getXML() {
00427 if(!$this->currentXML) {
00428 $ret = $this->generateXML();
00429 if(is_array($ret)) {
00430 foreach($ret as $entry)
00431 $this->setError($entry);
00432 return false;
00433 }
00434 $this->currentXML = $ret;
00435 }
00436
00437 return $this->currentXML;
00438 }
00439
00447 protected function generateXML() {
00448 $errors = array();
00449
00450 $title = trim($this->get('title'));
00451 $abstract = trim($this->prepareAndValidateXML($this->get('abstract'), 'Abstract'));
00452 $objectives = (is_array($this->get('objectives')) ? $this->get('objectives') : array());
00453 $prereqs = (is_array($this->get('prereqs')) ? $this->get('prereqs') : array());
00454 $preps = trim($this->prepareAndValidateXML($this->get('preps'), 'Teacher Preparation'));
00455 $keyTerms = (is_array($this->get('keyTerms')) ? $this->get('keyTerms') : array());
00456 $sections = (is_array($this->get('sections')) ? $this->get('sections') : array());
00457 $alternate = trim($this->prepareAndValidateXML($this->get('alternate'), 'Alternate Outline'));
00458 $followUp = (is_array($this->get('followUp')) ? $this->get('followUp') : array());
00459 $fuIntro = trim($this->prepareAndValidateXML($this->get('followUpIntro'), 'Follow Up Intro'));
00460
00461 $sectionStr = '';
00462 foreach($sections as $nm => $sect) {
00463 $ret = $this->prepareAndValidateXML($sect, $nm);
00464 if(is_array($ret))
00465 $errors = array_merge($errors, $ret);
00466 else {
00467 $sectionStr .= '<section mapping="section"><resourceData><name>' . htmlspecialchars($nm)
00468 . '</name></resourceData><section mapping="content">' . $ret . '</section></section>';
00469 }
00470 }
00471 if(count($sections) > 0)
00472 $sectionStr = "<section mapping=\"outline\">$sectionStr</section>";
00473
00474 $objStr = '';
00475 foreach($objectives as $obj)
00476 $objStr .= '<li>' . htmlspecialchars($obj) . '</li>';
00477 if(count($objectives) > 0)
00478 $objStr = <<<END_XML
00479 <section mapping="objectives">
00480 <p>Upon completion of this lesson, students will:
00481 <ul>$objStr</ul>
00482 </p>
00483 </section>
00484
00485 END_XML;
00486
00487 $preStr = '';
00488 foreach($prereqs as $cat => $ps) {
00489 $preStr .= '<li>' . $cat . (substr($cat, -1) == ':' ? '' : ':');
00490 if(count($ps) > 0) {
00491 $preStr .= '<ul>';
00492 foreach($ps as $pre)
00493 $preStr .= '<li>' . $pre . '</li>';
00494 $preStr .= '</ul>';
00495 }
00496 $preStr .= '</li>';
00497 }
00498 if(count($prereqs) > 0)
00499 $preStr = "<section mapping=\"prerequisites\"><ul>$preStr</ul></section>";
00500
00501 $ktStr = '';
00502 foreach($keyTerms as $term)
00503 $ktStr .= '<word>' . htmlspecialchars($term) . '</word>';
00504 if(count($keyTerms) > 0)
00505 $ktStr = "<section mapping=\"keyTerms\"><resourceData>$ktStr</resourceData></section>";
00506
00507 $fuStr = '';
00508 foreach($followUp as $fu) {
00509 $ret = $this->prepareAndValidateXML($fu, 'Suggested Follow-up');
00510 if(is_array($ret))
00511 $errors = array_merge($errors, $ret);
00512 else
00513 $fuStr .= '<li>' . $ret . '</li>';
00514 }
00515
00516 if(count($followUp) > 0)
00517 $fuStr = '<section mapping="followUp">' . $fuIntro . '<ul>' . $fuStr . '</ul></section>';
00518 else if($fuIntro != '')
00519 $fuStr = '<section mapping="followUp">' . $fuIntro . '</section>';
00520
00521 if($abstract != '')
00522 $abstract = "<section mapping=\"abstract\">$abstract</section>";
00523 if($alternate != '')
00524 $alternate = "<section mapping=\"alternateOutline\">$alternate</section>";
00525
00526 if($preps != '')
00527 $preps = "<section mapping=\"teacherPreparations\">$preps</section>";
00528
00529 if(count($errors) > 0)
00530 return $errors;
00531
00532 $xmlString = <<<END_XML
00533 <XMLResource>
00534 <section mapping="title">
00535 <name>$title</name>
00536 </section>
00537 $abstract
00538 $objStr
00539 <section mapping="standards">
00540 <p>{{{PLACEHOLDER}}}</p>
00541 </section>
00542 <section mapping="textbooks">
00543 <p>{{{PLACEHOLDER}}}</p>
00544 </section>
00545 $preStr
00546 $preps
00547 $ktStr
00548 $sectionStr
00549 $alternate
00550 $fuStr
00551 </XMLResource>
00552 END_XML;
00553
00554
00555
00556 $ret = XmlFormatter::finalGenerateXML($xmlString, true);
00557
00558 return $ret;
00559 }
00560
00568 public function getTitle() {
00569 return $this->get('title');
00570 }
00571
00579 public function getAbstract() {
00580 return $this->get('abstract');
00581 }
00582
00590 public function getObjectives() {
00591 $o = $this->get('objectives');
00592 return is_array($o) ? $o : array();
00593 }
00594
00602 public function getPrerequisites() {
00603 $o = $this->get('prereqs');
00604 return is_array($o) ? $o : array();
00605 }
00606
00614 public function getPreparations() {
00615 return $this->get('preps');
00616 }
00617
00625 public function getKeyTerms() {
00626 $o = $this->get('keyTerms');
00627 return is_array($o) ? $o : array();
00628 }
00629
00637 public function getSections() {
00638 $o = $this->get('sections');
00639 return is_array($o) ? $o : array();
00640 }
00641
00649 public function getAlternate() {
00650 return $this->get('alternate');
00651 }
00652
00660 public function getFollowUps() {
00661 $o = $this->get('followUp');
00662 return is_array($o) ? $o : array();
00663 }
00664
00672 public function getFollowUpIntro() {
00673 return $this->get('followUpIntro');
00674 }
00675
00683 public function getLoadErrors() {
00684 return $this->loadErrors;
00685 }
00686
00694 protected function validate() {
00695 return true;
00696 }
00697
00706 protected function doGetHTML($params) {
00707 return $this->getAll();
00708 }
00709
00717 public function checkMedia() {
00718 return true;
00719 }
00720
00728 public function updateFileList() {
00729 return true;
00730 }
00731
00735 public static function initialValue() {
00736 return "<XMLResource>\n</XMLResource>\n";
00737 }
00738 }
00739
00740 ?>