00001 <?php
00002
00018 class SnapContentMediaInteractivateApplet extends SnapContent {
00019
00023 private $hasUploaded = false;
00024
00032 protected function doDelete() {
00033 if(!$this->validate())
00034 return true;
00035
00036 $jar = new SnapExternalFile($this->version, $this->get('jar'));
00037 $jar->delete();
00038
00039 return true;
00040 }
00041
00049 protected function doCopy() {
00050 $newCM = new SnapContentMediaInteractivateApplet($this->version);
00051
00052 if($this->get('jar') != '') {
00053 $jar = new SnapExternalFile($this->version, $this->get('jar'));
00054 $cp = $jar->copy();
00055 if(!$cp) {
00056 $this->setError("Failed to copy old jarfile: <ul><li>" . implode('</li><li>', $jar->getError()) . '</li></ul>');
00057 return null;
00058 }
00059
00060 $newCM->set('width', $this->get('width'));
00061 $newCM->set('height', $this->get('height'));
00062 $newCM->set('appclass', $this->get('appclass'));
00063 $newCM->set('jar', $cp->getArray());
00064 }
00065
00066 return $newCM;
00067 }
00068
00076 protected function validate() {
00077 $values = $this->getAll();
00078 if(!isset($values['width']) || !isset($values['height'])
00079 || !isset($values['appclass']) || !isset($values['jar']))
00080 return false;
00081 else
00082 return true;
00083 }
00084
00093 protected function doGetHTML($params) {
00094 if(!$this->validate())
00095 return false;
00096
00097 $resourceId = $this->version->getResourceId();
00098
00099 $jar = new SnapExternalFile($this->version, $this->get('jar'));
00100 $jarurl = $jar->getURL();
00101
00102 $switcherPath = 'http://www.shodor.org/media/content//interactivate/switcher';
00103 $activityFullName = "Interactivate Java-based activity";
00104 $activityFullDescription = "Interactivate activities provide an engaging mathematics learning environment for elementary through undergraduate education.";
00105
00106 $loginInfo = isset($params['loginInfo']) ? $params['loginInfo'] : false;
00107
00108 if(isset($_REQUEST['shortName'])){
00109 $shortName = $_REQUEST['shortName'];
00110 $less = new InterActivity($shortName, array('iplus' => $loginInfo));
00111 $ct = $less->getContent();
00112 }
00113 else
00114 $ct = '';
00115
00116 if (!is_string($ct)) {
00117
00118
00119 $patterns = array("/'/", "/\n/", "/\t/", "/\r/", '/"/');
00120 $replacements = array("\'", "\\n", "\\t", "\\r", '\"');
00121
00122 if (isset($ct['name'])) {
00123 $activityFullName = preg_replace($patterns, $replacements, $ct['name']);
00124 }
00125 if (isset($ct['description'])) {
00126 $activityFullDescription = preg_replace($patterns, $replacements, $ct['description']);
00127 }
00128 }
00129
00130
00131 if($loginInfo == false) {
00132 $width = $this->get('width');
00133 $height = $this->get('height');
00134 $appclass = $this->get('appclass');
00135 $html = <<<END_HTML
00136 <div id="interactivateAppletDiv_$resourceId">
00137 <script type="text/javascript">
00138 var AppClass = '$appclass';
00139 var AppWidth = '$width';
00140 var AppHeight = '$height';
00141 var AppArchive = '$jarurl';
00142 var InteractivatePlus = 'false';
00143 var StudentId = '';
00144 var Server = '';
00145 var Port = '';
00146 var Uri = '';
00147 var ActivityFullName = '$activityFullName';
00148 var ActivityFullDescription = '$activityFullDescription';
00149 </script>
00150 <script type="text/javascript" src="$switcherPath">
00151 </script>
00152 </div>
00153
00154 <div id="noJava" style="display:none;">
00155
00156 </div>
00157 END_HTML;
00158 }
00159 else {
00160
00161
00162
00163 $width = $this->get('width');
00164 $height = $this->get('height');
00165 $appclass = $this->get('appclass');
00166
00167 $studentId = $loginInfo['uid'];
00168 $server = $_SERVER['SERVER_NAME'];
00169 $port = $_SERVER['SERVER_PORT'];
00170 $uri = $_SERVER['REQUEST_URI'];
00171
00172 $html = <<<END_HTML
00173 <div id="interactivateAppletDiv_$resourceId">
00174 <script type="text/javascript">
00175 var AppClass = '$appclass';
00176 var AppWidth = '$width';
00177 var AppHeight = '$height';
00178 var AppArchive = '$jarurl';
00179 var StudentId = '$studentId';
00180 var Server = '$server';
00181 var Port = '$port';
00182 var Uri = '$uri';
00183 var InteractivatePlus = 'true';
00184 var ActivityFullName = '$activityFullName';
00185 var ActivityFullDescription = '$activityFullDescription';
00186 </script>
00187 <script type="text/javascript" src="$switcherPath">
00188 </script>
00189 </div>
00190
00191 <div id="noJava" style="display:none;">
00192
00193 </div>
00194 END_HTML;
00195
00196 }
00197
00198 return $html;
00199 }
00200
00216 public function uploadApplet($tmpFile, $fileName, $appclass, $width, $height) {
00217 if(!is_readable($tmpFile)) {
00218 $this->setError("File '$tmpFile' does not exist "
00219 . "or is not accessible");
00220 return false;
00221 }
00222
00223 if(!preg_match("/[a-zA-Z0-9_]+\.class/", $appclass)) {
00224 $this->setError("Invalid applet class name: '$appclass'");
00225 return false;
00226 }
00227 if(!ctype_digit((string) $height) || $height <= 0) {
00228 $this->setError("Invalid height: '$height'");
00229 return false;
00230 }
00231 if(!ctype_digit((string) $width) || $width <= 0) {
00232 $this->setError("Invalid width: '$width'");
00233 return false;
00234 }
00235
00236 $jar = new SnapExternalFile($this->version);
00237 if(!$jar->create($tmpFile, $fileName)) {
00238 $this->setError('Could not create jarfile: <ul><li>' . implode('</li><li>', $jar->getError()) . '</li></ul>');
00239 return false;
00240 }
00241
00242 $this->set('width', $width);
00243 $this->set('height', $height);
00244 $this->set('appclass', $appclass);
00245 $this->set('jar', $jar->getArray());
00246
00247 return true;
00248 }
00249
00260 private function copyFrom($oldCM) {
00261 if($this->hasUploaded)
00262 return true;
00263
00264 $oldJarfile = new SnapExternalFile($this->version, $oldCM->get('jar'));
00265 $newJarfile = $oldJarfile->copy();
00266 if(!$newJarfile) {
00267 $this->setError("Could not copy jarfile: <ul><li>" . implode('</li><li>', $oldJarFile->getError()) . '</li></ul>');
00268 return false;
00269 }
00270
00271 $this->set('jar', $newJarfile->getArray());
00272 $this->set('width', $oldCM->getWidth());
00273 $this->set('height', $oldCM->getHeight());
00274 $this->set('appclass', $oldCM->getAppClass());
00275
00276 $this->hasUploaded = true;
00277
00278 return true;
00279 }
00280
00292 public function setAppClass($oldCM, $appclass) {
00293 if(!preg_match("/[a-zA-Z0-9_]+\.class/", $appclass)) {
00294 $this->setError("Invalid applet class name: '$appclass'");
00295 return false;
00296 }
00297 if(!$this->copyFrom($oldCM))
00298 return false;
00299 $this->set('appclass', $appclass);
00300 return true;
00301 }
00302
00314 public function setWidth($oldCM, $width) {
00315 if(!ctype_digit((string) $width) || $width <= 0) {
00316 $this->setError("Invalid width: '$width'");
00317 return false;
00318 }
00319 if(!$this->copyFrom($oldCM))
00320 return false;
00321 $this->set('width', $width);
00322 return true;
00323 }
00324
00336 public function setHeight($oldCM, $height) {
00337 if(!ctype_digit((string) $height) || $height <= 0) {
00338 $this->setError("Invalid height: '$height'");
00339 return false;
00340 }
00341 if(!$this->copyFrom($oldCM))
00342 return false;
00343 $this->set('height', $height);
00344 return true;
00345 }
00346
00351 public function getWidth() {
00352 if(!$this->validate())
00353 return false;
00354
00355 return $this->get('width');
00356 }
00357
00362 public function getHeight() {
00363 if(!$this->validate())
00364 return false;
00365
00366 return $this->get('height');
00367 }
00368
00373 public function getAppClass() {
00374 if(!$this->validate())
00375 return false;
00376
00377 return $this->get('appclass');
00378 }
00379
00384 public function getJarURL() {
00385 if(!$this->validate())
00386 return false;
00387 $file = new SnapExternalFile($this->version, $this->get('jar'));
00388 $url = $file->getURL();
00389 return $url;
00390 }
00391
00398 public function getJarPath() {
00399 if(!$this->validate())
00400 return false;
00401 $file = new SnapExternalFile($this->version, $this->get('jar'));
00402 $path = $file->getPath();
00403 return $path;
00404 }
00405
00413 public function getJarName() {
00414 if(!$this->validate())
00415 return false;
00416 $file = new SnapExternalFile($this->version, $this->get('jar'));
00417 $path = $file->getName();
00418 return $path;
00419 }
00420
00428 public function getSize() {
00429 if(!$this->validate())
00430 return false;
00431 $file = new SnapExternalFile($this->version, $this->get('jar'));
00432 return filesize($file->getPath());
00433 }
00434
00439 public function checkMedia() {
00440 if($this->get('jar') == '')
00441 return true;
00442 if(!$this->validate())
00443 return false;
00444 $jar = new SnapExternalFile($this->version, $this->get('jar'));
00445 $path = $jar->getPath();
00446 if(!is_readable($path) || !is_file($path))
00447 return false;
00448 else
00449 return true;
00450 }
00451
00456 public function updateFileList() {
00457 if(!$this->validate())
00458 return 'no files to insert';
00459 SnapDBI::startTransaction();
00460 $jar = new SnapExternalFile($this->version, $this->get('jar'));
00461 if(!$jar->updateDB()) {
00462 SnapDBI::cancelTransaction();
00463 return 'database failure';
00464 }
00465 else {
00466 SnapDBI::commitTransaction();
00467 return true;
00468 }
00469 }
00470 }
00471
00472 ?>