00001 <?php
00002 if(!defined('__PRAGYAN_CMS'))
00003 {
00004 header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
00005 echo "<h1>403 Forbidden<h1><h4>You are not authorized to access the page.</h4>";
00006 echo '<hr/>'.$_SERVER['SERVER_SIGNATURE'];
00007 exit(1);
00008 }
00009 require_once('quiz/quizedit.php');
00010 require_once('quiz/quizview.php');
00011 require_once('quiz/quizcorrect.php');
00012 require_once('quiz/iquiz.php');
00013
00014 $quizTypes = getQuizTypes();
00015 for ($i = 0; $i < count($quizTypes); ++$i)
00016 require_once('quiz/' . $quizTypes[$i] . 'quiz.php');
00017
00018 class quiz implements module {
00019 private $moduleComponentId;
00020 private $userId;
00021 private $action;
00022
00023 public function getHtml($userId, $moduleComponentId, $action) {
00024 $this->userId = $userId;
00025 $this->moduleComponentId = $moduleComponentId;
00026 $this->action = $action;
00027
00028 switch ($action) {
00029 case 'view':
00030 return $this->actionView();
00031 case 'edit':
00032 return $this->actionEdit();
00033 case 'correct':
00034 return $this->actionCorrect();
00035 }
00036 }
00037
00038 private function isValidId($id) {
00039 return isset($id) && is_numeric($id) && $id > 0;
00040 }
00041
00042 public function actionView() {
00043 $quizOpen = checkQuizOpen($this->moduleComponentId);
00044 if ($quizOpen != 0) {
00045 displayerror($quizOpen < 0 ? 'This quiz has not opened yet. Please check back later.' : 'This quiz has expired.');
00046 return '';
00047 }
00048
00049 if (!checkQuizSetup($this->moduleComponentId)) {
00050 displayerror('This quiz has not been properly set up with the required number of questions. The quiz module cannot continue.');
00051 return '';
00052 }
00053
00054 $quizRow = getQuizRow($this->moduleComponentId);
00055 $quizType = ucfirst($quizRow['quiz_quiztype']) . 'Quiz';
00056
00057 $quizObject = new $quizType($this->moduleComponentId);
00058 if (!($quizObject instanceof IQuiz)) {
00059 displayerror('Error. This type of quiz has not been implemented correctly.');
00060 return '';
00061 }
00062
00063 if (checkUserFirstAttempt($this->moduleComponentId, $this->userId)) {
00064
00065 $quizObject->initQuiz($this->userId);
00066 }
00067
00068 if (isset($_GET['subaction']) && $_GET['subaction'] == 'keepalive') {
00069 echo "Ok";
00070 exit();
00071 }
00072
00073
00074 return $quizObject->getQuizPage($this->userId);
00075 }
00076
00077 public function actionEdit() {
00078
00079
00080
00081
00082
00083 if (isset($_GET['subaction'])) {
00084 switch ($_GET['subaction']) {
00085 case 'addsections':
00086 if (!$this->isValidId($_POST['txtSectionCount'])) {
00087 displayerror('Error. No count specified.');
00088 }
00089 else {
00090 $count = escape($_POST['txtSectionCount']);
00091 if (addSections($this->moduleComponentId, $count) !== false)
00092 displayinfo('Section(s) added successfully.');
00093 }
00094 break;
00095
00096 case 'editsection':
00097 $dataSource = 'db';
00098 if (!$this->isValidId($_GET['sectionid'])) {
00099 displayerror('Error. Invalid section id specified.');
00100 }
00101 elseif (isset($_POST['btnSubmit'])) {
00102 $dataSource = 'POST';
00103 if (submitSectionEditForm($this->moduleComponentId, intval($_GET['sectionid']))) {
00104 displayinfo('Section properties saved successfully.');
00105 $dataSource = 'db';
00106 }
00107 }
00108 return getSectionEditForm($this->moduleComponentId, intval($_GET['sectionid']), $dataSource);
00109 break;
00110
00111 case 'deletesection':
00112 if (!$this->isValidId($_POST['hdnSectionId'])) {
00113 displayerror('Error. Invalid section id specified.');
00114 }
00115 elseif (deleteSection($this->moduleComponentId, intval($_POST['hdnSectionId']))) {
00116 displayinfo('The specified section was successfully deleted.');
00117 }
00118 break;
00119
00120 case 'movesection':
00121 if (!$this->isValidId($_GET['sectionid'])) {
00122 displayerror('Error. Invalid section id specified.');
00123 }
00124 elseif (!isset($_GET['direction']) || ($_GET['direction'] != 'up' && $_GET['direction'] != 'down')) {
00125 displayerror('Error. No or invalid direction specified. Could not move section.');
00126 }
00127 elseif (moveSection($this->moduleComponentId, intval($_GET['sectionid']))) {
00128 displayinfo('The specified section was successfully moved.');
00129 }
00130 break;
00131
00132 case 'addquestions':
00133 if (!$this->isValidId($_GET['sectionid'])) {
00134 displayerror('Error. No or invalid section id specified. Could not add question.');
00135 }
00136 elseif (!$this->isValidId($_POST['txtQuestionCount'])) {
00137 displayerror('Error. No or invalid count specified. Could not add question.');
00138 }
00139 else {
00140 $count = intval($_POST['txtQuestionCount']);
00141 $insertIds = addQuestions($this->moduleComponentId, intval($_GET['sectionid']), $count);
00142 if ($insertIds !== false)
00143 displayinfo('New question(s) added successfully.');
00144 }
00145 break;
00146
00147 case 'editquestion':
00148 $dataSource = 'db';
00149 if (!$this->isValidId($_GET['sectionid']) || !$this->isValidId($_GET['questionid'])) {
00150 displayerror('Error. Invalid section or question specified.');
00151 }
00152 elseif (isset($_POST['btnSubmit'])) {
00153 $dataSource = 'POST';
00154 if (submitQuestionEditForm($this->moduleComponentId, intval($_GET['sectionid']), intval($_GET['questionid']))) {
00155 displayinfo('Question properties saved successfully.');
00156 $dataSource = 'db';
00157 }
00158 }
00159 return getQuestionEditForm($this->moduleComponentId, intval($_GET['sectionid']), intval($_GET['questionid']), $dataSource);
00160 break;
00161
00162 case 'deletequestion':
00163 if (!$this->isValidId($_POST['hdnSectionId']) || !$this->isValidId($_POST['hdnQuestionId'])) {
00164 displayerror('Error. Invalid section or question specified.');
00165 }
00166 elseif (deleteQuestion($this->moduleComponentId, intval($_POST['hdnSectionId']), intval($_POST['hdnQuestionId']))) {
00167 displayinfo('Question successfully deleted.');
00168 }
00169 break;
00170
00171 case 'movequestion':
00172 if (!$this->isValidId($_GET['sectionid'])) {
00173 displayerror('Error. Invalid section id specified.');
00174 }
00175 elseif (!$this->isValidId($_GET['questionid'])) {
00176 displayerror('Error. Invalid question id specified.');
00177 }
00178 elseif (!isset($_GET['direction']) || ($_GET['direction'] != 'up' && $_GET['direction'] != 'down')) {
00179 displayerror('Error. No or invalid direction specified. Could not move section.');
00180 }
00181 elseif (moveQuestion($this->moduleComponentId, intval($_GET['sectionid']), intval($_GET['questionid']), $_GET['direction'])) {
00182 displayinfo('The specified question was successfully moved.');
00183 }
00184 break;
00185 }
00186 }
00187
00188 if (isset($_POST['btnSetWeightMarks'])) {
00189 if(setWeightMark(intval($_POST['quizId']), intval($_POST['weight']), intval($_POST['pos']), intval($_POST['neg']))) {
00190 displayinfo('Weight - Marks saved.');
00191 } else {
00192 displayerror('Error in changing weight mark');
00193 }
00194 }
00195 $dataSource = 'db';
00196 if (isset($_POST['btnSubmit'])) {
00197 $dataSource = 'POST';
00198 if (submitQuizEditForm($this->moduleComponentId))
00199 $dataSource = 'db';
00200 }
00201
00202 return getQuizEditForm($this->moduleComponentId, $dataSource);
00203 }
00204
00205 public function actionCorrect() {
00206
00207 if (isset($_POST['btnSetMark'])) {
00208 $quizid = escape($_POST['quizid']);
00209 $sectionid = escape($_POST['sectionid']);
00210 $questionid = escape($_POST['questionid']);
00211 $userid = escape($_POST['userid']);
00212 $mark = escape($_POST['mark']);
00213 $condition = "`page_modulecomponentid` = '$quizid' AND `quiz_sectionid` = '$sectionid' AND `quiz_questionid` = '$questionid' AND `user_id` = '$userid'";
00214 $result = mysql_query("SELECT `quiz_submittedanswer` FROM `quiz_answersubmissions` WHERE $condition");
00215 if($row = mysql_fetch_array($result)) {
00216 $result = mysql_fetch_array(mysql_query("SELECT `question_positivemarks`, `question_negativemarks` FROM `quiz_weightmarks` WHERE `page_modulecomponentid` = '$quizid' AND `question_weight` = (SELECT `quiz_questionweight` FROM `quiz_questions` WHERE `page_modulecomponentid` = '$quizid' AND `quiz_sectionid` = '$sectionid' AND `quiz_questionid` = '$questionid')"));
00217 if($_POST['mark'] > $result['question_positivemarks'] || $_POST['mark'] < -1 * $result['question_negativemarks'])
00218 displaywarning('Mark out of range for this question, so mark not set');
00219 else {
00220 mysql_query("UPDATE `quiz_answersubmissions` SET `quiz_marksallotted` = $mark WHERE $condition");
00221 updateSectionMarks($quizid);
00222 displayinfo('Mark set');
00223 }
00224 }
00225 else
00226 displayerror('Unable to set value');
00227 }
00228
00229 if (isset($_GET['useremail'])) {
00230 $userId = getUserIdFromEmail($_GET['useremail']);
00231 if ($userId)
00232 return getQuizCorrectForm($this->moduleComponentId, $userId);
00233 else
00234 displayerror('Error. Could not find user.');
00235 }
00236 elseif (isset($_POST['btnDeleteUser']) && isset($_POST['hdnUserId']) && is_numeric($_POST['hdnUserId'])) {
00237 $quizObject = $this->getNewQuizObject();
00238 if ($quizObject !== false)
00239 $quizObject->deleteEntries(intval($_POST['hdnUserId']));
00240 }
00241
00242 return getQuizUserListHtml($this->moduleComponentId);
00243 }
00244
00245
00246 public function createModule(&$moduleComponentId) {
00247 $insertQuery = "INSERT INTO `quiz_descriptions`(`quiz_title`, `quiz_headertext`, `quiz_submittext`, `quiz_quiztype`, `quiz_testduration`, `quiz_questionspertest`, `quiz_questionsperpage`, `quiz_timeperpage`, `quiz_allowsectionrandomaccess`, `quiz_mixsections`, `quiz_showquiztimer`, `quiz_showpagetimer`) VALUES" .
00248 "('New Quiz', 'Quiz under construction', 'Quiz under construction', 'simple', '00:30', '20', '10', 0, 1, 0, 1, 0)";
00249 if (!mysql_query($insertQuery)) {
00250 displayerror('Database Error. Could not create quiz. ' . $insertQuery . ' ' . mysql_error());
00251 return false;
00252 }
00253
00254 $moduleComponentId = mysql_insert_id();
00255
00256 $insertIds = addSections($moduleComponentId, 1);
00257 return count($insertIds) == 1;
00258 }
00259
00260 public function copyModule($moduleComponentId) {
00261 }
00262
00263 public function deleteModule($moduleComponentId) {
00264 $tableNames = array('quiz_descriptions', 'quiz_sections', 'quiz_questions', 'quiz_objectiveoptions', 'quiz_userattempts', 'quiz_answersubmissions', 'quiz_weightmarks');
00265 $allOk = true;
00266 for ($i = 0; $i < count($tableNames); ++$i) {
00267 $deleteQuery = "DELETE FROM `{$tableNames[$i]}` WHERE `page_modulecomponentid` = $moduleComponentId";
00268 $allOk = (mysql_query($deleteQuery) ? true : false) && $allOk;
00269 }
00270 if (!$allOk)
00271 displayerror('Database Error. Could not remove all entries related to the module.');
00272 return $allOk;
00273 }
00274
00275 private function getNewQuizObject() {
00276 $quizRow = getQuizRow($this->moduleComponentId);
00277 $quizType = $quizRow['quiz_quiztype'];
00278 $quizObjectType = ucfirst($quizType) . 'Quiz';
00279 if (!class_exists($quizObjectType)) {
00280 displayerror('Error. This type of quiz has not been implemented yet.');
00281 return false;
00282 }
00283
00284 $quizObject = new $quizObjectType($this->moduleComponentId);
00285 return $quizObject;
00286 }
00287 }