javascriptcursus/php/assignments.php

<?php // Require meekro DB voor de database connectie require_once "meekrodb.2.3.class.php"; /* define("DB_HOST", "localhost"); // Locatie van MySQL host define("DB_NAME", "phpserver_js"); // Naam van de database define("DB_USER", "phpserver_js"); // Gebruikersnaam define("DB_PASS", "Terpen16!"); // Wachtwoord define("DB_PREFIX", "in_js_"); // Prefix die voor de tabel staat die door deze site gebruikt wordt */ // Database gegevens: DB::$host = "localhost"; // Locatie van MySQL host DB::$dbName = "serverphp_js"; // Naam van de database DB::$user = "serverphp_js"; // Gebruikersnaam DB::$password = "Terpen16!"; // Wachtwoord define("DB_PREFIX", "in_js_"); // Prefix die voor elke tabel staat die door deze site gebruikt wordt // Enum codePlace: define("codePlace_wholeDocument", 0); define("codePlace_head", 1); define("codePlace_body", 2); class answer { // Public: public $text; public $js; public $html; public $htmlPlace; public function __construct($text, $js, $html, $htmlPlace = codePlace_body) { $this->text = $text; $this->js = $js; $this->html = $html; $this->htmlPlace = $htmlPlace; } }; class assignment { // Public: public $id; public $assignment; public $answer; public $chapter; public $subChapter; public $exampleCode; public $viewAnswer; public $nr; public $display; public $orderNr; public function __construct($id, $assignment, $answer, $chapter, $subChapter, $exampleCode, $viewAnswer, $nr, $display, $orderNr) { $this->id = $id; $this->assignment = $assignment; $this->answer = $answer; $this->chapter = $chapter; $this->subChapter = $subChapter; $this->exampleCode = $exampleCode; $this->viewAnswer = $viewAnswer; $this->nr = $nr; $this->display = $display; $this->orderNr = $orderNr; } public function save() { if(!is_numeric($this->id)) $this->id = 0; if($this->id == 0) return $this->insert(); else return $this->update(); } public function delete() { if(!is_numeric($this->id)) return false; DB::delete(DB_PREFIX.'assignments', 'id=%i', $this->id); } // Private: private function insert() { if(!is_numeric($this->orderNr)) $this->orderNr = 1; $oldErrorHandler = DB::$error_handler; $oldThrowExceptionValue = DB::$throw_exception_on_error; DB::$error_handler = false; DB::$throw_exception_on_error = true; $success = true; try { DB::insert(DB_PREFIX.'assignments', array ( 'assignment' => $this->assignment, 'answer_text' => $this->answer->text, 'answer_js' => $this->answer->js, 'answer_html' => $this->answer->html, 'answer_codeplace' => $this->answer->htmlPlace, 'view_answer' => $this->viewAnswer, 'chapter' => $this->chapter, 'subchapter' => $this->subChapter, 'example' => $this->exampleCode, 'nr' => $this->orderNr, 'display' => $this->display )); } catch(MeekroDBException $e) { $success = false; } DB::$error_handler = $oldErrorHandler; DB::$throw_exception_on_error = $oldThrowExceptionValue; return $success; } private function update() { if(!is_numeric($this->orderNr)) $this->orderNr = 1; $oldErrorHandler = DB::$error_handler; $oldThrowExceptionValue = DB::$throw_exception_on_error; DB::$error_handler = false; DB::$throw_exception_on_error = true; $success = true; try { DB::update(DB_PREFIX.'assignments', array ( 'assignment' => $this->assignment, 'answer_text' => $this->answer->text, 'answer_js' => $this->answer->js, 'answer_html' => $this->answer->html, 'answer_codeplace' => $this->answer->htmlPlace, 'view_answer' => $this->viewAnswer, 'chapter' => $this->chapter, 'subchapter' => $this->subChapter, 'example' => $this->exampleCode, 'nr' => $this->orderNr, 'display' => $this->display ), 'id=%i', $this->id); } catch(MeekroDBException $e) { $success = false; } DB::$error_handler = $oldErrorHandler; DB::$throw_exception_on_error = $oldThrowExceptionValue; return $success; } }; class assignmentsClient { // Public: public function getAssignment($id, $onlyDisplayable = false) { if(!is_numeric($id)) return false; $result = DB::queryFirstRow('SELECT * FROM '.DB_PREFIX.'assignments WHERE id=%i'.($onlyDisplayable ? ' AND display = 1' : ''), $id); if($result) { if($result['display']) return $this->arrayToAssignment($result, DB::queryFirstField('SELECT COUNT(id) FROM '.DB_PREFIX.'assignments WHERE display = 1 AND chapter = %i AND nr < %i', $result['chapter'], $result['nr']) + 1); else return $this->arrayToAssignment($result, 0); } return false; } public function getAssignmentsByChapter($chapter, $onlyDisplayable = false) { if(!is_numeric($chapter)) return false; $result = DB::query('SELECT * FROM '.DB_PREFIX.'assignments WHERE chapter=%i'.($onlyDisplayable ? ' AND display = 1' : '').' ORDER BY nr', $chapter); $nr = 1; if($result) { $out = array(); foreach($result as $tmp) { if($tmp['display']) array_push($out, $this->arrayToAssignment($tmp, $nr++)); else array_push($out, $this->arrayToAssignment($tmp, 0)); } return $out; } return array(); } public function getAllAssignments($onlyDisplayable = false) { $result = DB::query('SELECT * FROM '.DB_PREFIX.'assignments'.($onlyDisplayable ? ' WHERE display = 1' : '').' ORDER BY chapter, nr'); $nr = 1; if($result) { $out = array(); foreach($result as $tmp) { if($tmp['display']) array_push($out, $this->arrayToAssignment($tmp, $nr++)); else array_push($out, $this->arrayToAssignment($tmp, 0)); } return $out; } return array(); } public function setViewAnswers($viewIds) { // Hide all answers if there are none to be shown if(count($viewIds) == 0) { $oldErrorHandler = DB::$error_handler; $oldThrowExceptionValue = DB::$throw_exception_on_error; DB::$error_handler = false; DB::$throw_exception_on_error = true; $success = true; try { DB::query('UPDATE '.DB_PREFIX.'assignments SET view_answer = 0'); } catch(MeekroDBException $e) { $success = false; } DB::$error_handler = $oldErrorHandler; DB::$throw_exception_on_error = $oldThrowExceptionValue; return $success; } // Hide and show the right answers $oldErrorHandler = DB::$error_handler; $oldThrowExceptionValue = DB::$throw_exception_on_error; DB::$error_handler = false; DB::$throw_exception_on_error = true; $success = true; try { DB::update(DB_PREFIX.'assignments', array('view_answer' => 1), 'id IN %li', $viewIds); DB::update(DB_PREFIX.'assignments', array('view_answer' => 0), 'id NOT IN %li', $viewIds); } catch(MeekroDBException $e) { $success = false; } DB::$error_handler = $oldErrorHandler; DB::$throw_exception_on_error = $oldThrowExceptionValue; return $success; } public function viewAnswersTillChapter($chapter, $subChapter, $view = true) { if(!is_numeric($chapter) || !is_numeric($subChapter)) return false; $oldErrorHandler = DB::$error_handler; $oldThrowExceptionValue = DB::$throw_exception_on_error; DB::$error_handler = false; DB::$throw_exception_on_error = true; $success = true; try { DB::update(DB_PREFIX.'assignments', array('view_answer' => $view), 'chapter < %i0 OR (chapter = %i0 AND subchapter <= %i1)', $chapter, $subChapter); } catch(MeekroDBException $e) { $success = false; } DB::$error_handler = $oldErrorHandler; DB::$throw_exception_on_error = $oldThrowExceptionValue; } // Private: private static $con = false; private static $conFailed = false; private function arrayToAssignment($a, $nr) { return new assignment($a['id'], $a['assignment'], new answer($a['answer_text'], $a['answer_js'], $a['answer_html'], $a['answer_codeplace']), $a['chapter'], $a['subchapter'], $a['example'], $a['view_answer'], $nr, $a['display'], $a['nr']); } }; ?>

Resultaat

Made by Thijs Aarnoudse