00001 <?php
00002
00011 class TKSnapInteractivateLessonEditor extends TKSnapEditor {
00012 private $title = '';
00013 private $abstract = '';
00014 private $objectives = array();
00015 private $prereqs = array();
00016 private $preps = '';
00017 private $keyTerms = array();
00018 private $sections = array();
00019 private $alternate = '';
00020 private $followUpIntro = '';
00021 private $followUps = array();
00022
00031 private function grabResponse($noWarn = false) {
00032 $json = json_decode(utf8_encode($_REQUEST['result']));
00033 $ret = true;
00034 $dupCount = array();
00035 $warnings = '';
00036
00037 $this->title = $json->title;
00038 $this->abstract = $json->abstract;
00039 $this->objectives = $json->objectives;
00040 $this->prereqs = array();
00041 $cats = $json->categories;
00042 foreach($cats as $cat => $prereqs) {
00043 $this->prereqs[$cat] = array();
00044 $prereqs = $prereqs;
00045 foreach($prereqs as $pre)
00046 $this->prereqs[$cat][] = $pre;
00047 }
00048 $this->preps = $json->preps;
00049 $this->keyTerms = $json->keyterms;
00050 $this->sections = array();
00051 $sections = $json->sections;
00052 foreach($sections as $section) {
00053 $sectionName = $section[0];
00054 $sectionContent = $section[1];
00055 if(isset($this->sections[$sectionName])) {
00056 $ret = false;
00057 if(!isset($dupCount[$sectionName]))
00058 $dupCount[$sectionName] = 1;
00059 $warnings .= "<li>Duplicate section name: <b>$sectionName</b> (renamed to <b>$sectionName (DUPLICATE $dupCount[$sectionName])</b>)</li>";
00060 $this->sections["$sectionName (DUPLICATE $dupCount[$sectionName])"] = $sectionContent;
00061 $dupCount[$sectionName]++;
00062 }
00063 else
00064 $this->sections[$sectionName] = $sectionContent;
00065 }
00066 $this->alternate = $json->alternate;
00067 $this->followUpIntro = $json->followupintro;
00068 $this->followUps = $json->followups;
00069
00070 if($warnings)
00071 $warnings = "<p><b>Warnings:</b><ul>$warnings</ul>Document has been saved with renamed duplicate sections.</p>";
00072 if(!$noWarn)
00073 $this->addText($warnings);
00074
00075 return $ret;
00076 }
00077
00087 protected function showEditor($cm, $failed) {
00088 if($failed) {
00089 $this->grabResponse(true);
00090 }
00091 else {
00092 $this->title = $cm->getTitle();
00093 $this->abstract = $cm->getAbstract();
00094 $this->objectives = $cm->getObjectives();
00095 $this->prereqs = $cm->getPrerequisites();
00096 $this->preps = $cm->getPreparations();
00097 $this->keyTerms = $cm->getKeyTerms();
00098 $this->sections = $cm->getSections();
00099 $this->alternate = $cm->getAlternate();
00100 $this->followUpIntro = $cm->getFollowUpIntro();
00101 $this->followUps = $cm->getFollowUps();
00102 }
00103
00104 $json = array(
00105 'title' => $this->title,
00106 'abstract' => $this->abstract,
00107 'objectives' => $this->objectives,
00108 'categories' => array(),
00109 'preps' => $this->preps,
00110 'keyterms' => $this->keyTerms,
00111 'sections' => array(),
00112 'alternate' => $this->alternate,
00113 'followupintro' => $this->followUpIntro,
00114 'followups' => $this->followUps
00115 );
00116
00117 foreach($this->prereqs as $cat => $pre)
00118 $json['categories'][$cat] = $pre;
00119 foreach($this->sections as $name => $content)
00120 $json['sections'][] = array($name, $content);
00121
00122 $json = htmlspecialchars(json_encode($json), ENT_NOQUOTES);
00123
00124 if(SAutoLoad::isModuleUsed('tsd')) {
00125 $kw = TSDWord::getList();
00126 $keywords = array();
00127 foreach($kw as $w)
00128 $keywords[] = addslashes($w->word);
00129 $keywords = implode("', '", $keywords);
00130 if($keywords != '')
00131 $keywords = "'$keywords'";
00132 }
00133 else
00134 $keywords = '';
00135
00136 $js = <<<END_JAVASCRIPT
00137 SWAT.use('InteractivateLessonEditor');
00138
00139 function initializeEditor() {
00140 Lesson.KeyTerms = [ $keywords ];
00141 var lesson = new Lesson();
00142 lesson.unserialize($json)
00143 var editor = new LessonEditor(lesson);
00144 $('editorContainer').insert(editor);
00145 window.lesson = lesson;
00146 }
00147
00148 END_JAVASCRIPT;
00149
00150 global $prm;
00151 $prm->setOption('prototype', true);
00152 $prm->setOption('swatjs', true);
00153 $prm->loadJS($js);
00154 $prm->loadJS('initializeEditor()', 'OnLoad');
00155
00156 $this->addText('<div id="editorContainer"></div>');
00157 }
00158
00167 protected function processSave($oldCM) {
00168 if(!$this->grabResponse())
00169 return false;
00170
00171 $cmNew = new SnapContentInteractivateLesson($this->version);
00172
00173 $cmNew->setTitle($this->title);
00174 $cmNew->setAbstract($this->abstract);
00175 $cmNew->clearObjectives();
00176 foreach($this->objectives as $obj)
00177 $cmNew->addObjective($obj);
00178 $cmNew->clearPrerequisites();
00179 foreach($this->prereqs as $cat => $pre) {
00180 $cmNew->addPrerequisiteCategory($cat);
00181 foreach($pre as $p)
00182 $cmNew->addPrerequisite($cat, $p);
00183 }
00184 $cmNew->setPreparations($this->preps);
00185 $cmNew->clearKeyTerms();
00186 foreach($this->keyTerms as $term)
00187 $cmNew->addKeyTerm($term);
00188 $cmNew->clearSections();
00189 foreach($this->sections as $name => $content)
00190 $cmNew->addSection($name, $content);
00191 $cmNew->setAlternate($this->alternate);
00192 $cmNew->clearFollowUps();
00193 $cmNew->setFollowUpIntro($this->followUpIntro);
00194 foreach($this->followUps as $fu)
00195 $cmNew->addFollowUp($fu);
00196
00197
00198 if(count($cmNew->getLoadErrors()) > 0) {
00199 $this->addText('<p><b>Could not save because of the following errors</b>: <ul><li>'
00200 . implode($cmNew->getLoadErrors(), '</li><li>') . '</li></ul></p>');
00201 return false;
00202 }
00203
00204
00205 $cmNew->getXML();
00206
00207
00208 if(count($cmNew->getError()) > 0) {
00209 $this->addText('<p><b>Fatal Error</b>: Could not save document: <ul><li>' . implode($cmNew->getError(), '</li><li>')
00210 . '</li></ul></p>');
00211 return false;
00212 }
00213
00214 return $cmNew;
00215 }
00216 }
00217
00218 ?>