• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

cms/modules/quiz.lib.php

Go to the documentation of this file.
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 
00067 require_once('quiz/quizedit.php');
00068 require_once('quiz/quizview.php');
00069 require_once('quiz/quizcorrect.php');
00070 require_once('quiz/iquiz.php');
00071 
00072 $quizTypes = getQuizTypes();
00073 for ($i = 0; $i < count($quizTypes); ++$i)
00074         require_once('quiz/' . $quizTypes[$i] . 'quiz.php');
00075 
00076 class quiz implements module {
00077         private $moduleComponentId;
00078         private $userId;
00079         private $action;
00080 
00086         public function getHtml($userId, $moduleComponentId, $action) {
00087                 $this->userId = $userId;
00088                 $this->moduleComponentId = $moduleComponentId;
00089                 $this->action = $action;
00090 
00091                 switch ($action) {
00092                         case 'view':
00093                                 return $this->actionView();
00094                         case 'edit':
00095                                 return $this->actionEdit();
00096                         case 'correct':
00097                                 return $this->actionCorrect();
00098                 }
00099         }
00100         
00105         private function isValidId($id) {
00106                 return isset($id) && is_numeric($id) && $id > 0;
00107         }
00108         
00114         public function actionView() {
00115                 $quizOpen = checkQuizOpen($this->moduleComponentId);
00116                 if ($quizOpen != 0) {
00117                         displayerror($quizOpen < 0 ? 'This quiz has not opened yet. Please check back later.' : 'This quiz has expired.');
00118                         return '';
00119                 }
00120 
00121                 if (!checkQuizSetup($this->moduleComponentId)) {
00122                         displayerror('This quiz has not been properly set up with the required number of questions. The quiz module cannot continue.');
00123                         return '';
00124                 }
00125 
00126                 $quizRow = getQuizRow($this->moduleComponentId);
00127                 $quizType = ucfirst($quizRow['quiz_quiztype']) . 'Quiz';
00128                 
00129                 $quizObject = new $quizType($this->moduleComponentId);
00130                 if (!($quizObject instanceof IQuiz)) {
00131                         displayerror('Error. This type of quiz has not been implemented correctly.');
00132                         return '';
00133                 }
00134 
00135                 if (checkUserFirstAttempt($this->moduleComponentId, $this->userId)) {
00136                         // user is attempting for the first time
00137                         $quizObject->initQuiz($this->userId);
00138                 }
00139 
00140                 if (isset($_GET['subaction']) && $_GET['subaction'] == 'keepalive') {
00141                         echo "Ok";
00142                         exit();
00143                 }
00144 
00145                 // Quiz is initialized. Call getQuizPage.
00146                 return $quizObject->getQuizPage($this->userId);
00147         }
00148         
00153         public function actionEdit() {
00154                 // dataSource: the $dataSource argument to get*Form() functions specifies where to persist data for the form from.
00155                 // if a submit was in progress, and the submit was successful, we set dataSource to db.
00156                 // else, we set dataSource to POST, because we need to present the user's entered values, rather than existing values
00157                 // so that he/she may make changes and submit again, with least hassle.
00158 
00159                 if (isset($_GET['subaction'])) {
00160                         switch ($_GET['subaction']) {
00161                                 case 'addsections':
00162                                         if (!$this->isValidId($_POST['txtSectionCount'])) {
00163                                                 displayerror('Error. No count specified.');
00164                                         }
00165                                         else {
00166                                                 $count = escape($_POST['txtSectionCount']);
00167                                                 if (addSections($this->moduleComponentId, $count) !== false)
00168                                                         displayinfo('Section(s) added successfully.');
00169                                         }
00170                                 break;
00171 
00172                                 case 'editsection':
00173                                         $dataSource = 'db';
00174                                         if (!$this->isValidId($_GET['sectionid'])) {
00175                                                 displayerror('Error. Invalid section id specified.');
00176                                         }
00177                                         elseif (isset($_POST['btnSubmit'])) {
00178                                                 $dataSource = 'POST';
00179                                                 if (submitSectionEditForm($this->moduleComponentId, intval($_GET['sectionid']))) {
00180                                                         displayinfo('Section properties saved successfully.');
00181                                                         $dataSource = 'db';
00182                                                 }
00183                                         }
00184                                         return getSectionEditForm($this->moduleComponentId, intval($_GET['sectionid']), $dataSource);
00185                                 break;
00186 
00187                                 case 'deletesection':
00188                                         if (!$this->isValidId($_POST['hdnSectionId'])) {
00189                                                 displayerror('Error. Invalid section id specified.');
00190                                         }
00191                                         elseif (deleteSection($this->moduleComponentId, intval($_POST['hdnSectionId']))) {
00192                                                 displayinfo('The specified section was successfully deleted.');
00193                                         }
00194                                 break;
00195 
00196                                 case 'movesection':
00197                                         if (!$this->isValidId($_GET['sectionid'])) {
00198                                                 displayerror('Error. Invalid section id specified.');
00199                                         }
00200                                         elseif (!isset($_GET['direction']) || ($_GET['direction'] != 'up' && $_GET['direction'] != 'down')) {
00201                                                 displayerror('Error. No or invalid direction specified. Could not move section.');
00202                                         }
00203                                         elseif (moveSection($this->moduleComponentId, intval($_GET['sectionid']))) {
00204                                                 displayinfo('The specified section was successfully moved.');
00205                                         }
00206                                 break;
00207 
00208                                 case 'addquestions':
00209                                         if (!$this->isValidId($_GET['sectionid'])) {
00210                                                 displayerror('Error. No or invalid section id specified. Could not add question.');
00211                                         }
00212                                         elseif (!$this->isValidId($_POST['txtQuestionCount'])) {
00213                                                 displayerror('Error. No or invalid count specified. Could not add question.');
00214                                         }
00215                                         else {
00216                                                 $count = intval($_POST['txtQuestionCount']);
00217                                                 $insertIds = addQuestions($this->moduleComponentId, intval($_GET['sectionid']), $count);
00218                                                 if ($insertIds !== false)
00219                                                         displayinfo('New question(s) added successfully.');
00220                                         }
00221                                 break;
00222 
00223                                 case 'editquestion':
00224                                         $dataSource = 'db';
00225                                         if (!$this->isValidId($_GET['sectionid']) || !$this->isValidId($_GET['questionid'])) {
00226                                                 displayerror('Error. Invalid section or question specified.');
00227                                         }
00228                                         elseif (isset($_POST['btnSubmit'])) {
00229                                                 $dataSource = 'POST';
00230                                                 if (submitQuestionEditForm($this->moduleComponentId, intval($_GET['sectionid']), intval($_GET['questionid']))) {
00231                                                         displayinfo('Question properties saved successfully.');
00232                                                         $dataSource = 'db';
00233                                                 }
00234                                         }
00235                                         return getQuestionEditForm($this->moduleComponentId, intval($_GET['sectionid']), intval($_GET['questionid']), $dataSource);
00236                                 break;
00237 
00238                                 case 'deletequestion':
00239                                         if (!$this->isValidId($_POST['hdnSectionId']) || !$this->isValidId($_POST['hdnQuestionId'])) {
00240                                                 displayerror('Error. Invalid section or question specified.');
00241                                         }
00242                                         elseif (deleteQuestion($this->moduleComponentId, intval($_POST['hdnSectionId']), intval($_POST['hdnQuestionId']))) {
00243                                                 displayinfo('Question successfully deleted.');
00244                                         }
00245                                 break;
00246 
00247                                 case 'movequestion':
00248                                         if (!$this->isValidId($_GET['sectionid'])) {
00249                                                 displayerror('Error. Invalid section id specified.');
00250                                         }
00251                                         elseif (!$this->isValidId($_GET['questionid'])) {
00252                                                 displayerror('Error. Invalid question id specified.');
00253                                         }
00254                                         elseif (!isset($_GET['direction']) || ($_GET['direction'] != 'up' && $_GET['direction'] != 'down')) {
00255                                                 displayerror('Error. No or invalid direction specified. Could not move section.');
00256                                         }
00257                                         elseif (moveQuestion($this->moduleComponentId, intval($_GET['sectionid']), intval($_GET['questionid']), $_GET['direction'])) {
00258                                                 displayinfo('The specified question was successfully moved.');
00259                                         }
00260                                 break;
00261                         }
00262                 }
00263 
00264                 if (isset($_POST['btnSetWeightMarks'])) {
00265                         if(setWeightMark(intval($_POST['quizId']), intval($_POST['weight']), intval($_POST['pos']), intval($_POST['neg']))) {
00266                                 displayinfo('Weight - Marks saved.');
00267                         } else {
00268                                 displayerror('Error in changing weight mark');
00269                         }
00270                 }
00271                 $dataSource = 'db';
00272                 if (isset($_POST['btnSubmit'])) {
00273                         $dataSource = 'POST';
00274                         if (submitQuizEditForm($this->moduleComponentId))
00275                                 $dataSource = 'db';
00276                 }
00277 
00278                 return getQuizEditForm($this->moduleComponentId, $dataSource);
00279         }
00280         
00286         public function actionCorrect() {
00287 
00288                 if (isset($_POST['btnSetMark'])) {
00289                         $quizid = escape($_POST['quizid']);
00290                         $sectionid = escape($_POST['sectionid']);
00291                         $questionid = escape($_POST['questionid']);
00292                         $userid = escape($_POST['userid']);
00293                         $mark = escape($_POST['mark']);
00294                         $condition = "`page_modulecomponentid` = '$quizid' AND `quiz_sectionid` = '$sectionid' AND `quiz_questionid` = '$questionid' AND `user_id` = '$userid'";
00295                         $result = mysql_query("SELECT `quiz_submittedanswer` FROM `quiz_answersubmissions` WHERE $condition");
00296                         if($row = mysql_fetch_array($result)) {
00297                                 $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')"));
00298                                 if($_POST['mark'] > $result['question_positivemarks'] || $_POST['mark'] < -1 * $result['question_negativemarks'])
00299                                         displaywarning('Mark out of range for this question, so mark not set');
00300                                 else {
00301                                         mysql_query("UPDATE `quiz_answersubmissions` SET `quiz_marksallotted` = $mark WHERE $condition");
00302                                         updateSectionMarks($quizid);
00303                                         displayinfo('Mark set');
00304                                 }
00305                         }
00306                         else
00307                                 displayerror('Unable to set value');
00308                 }
00309 
00310                 if (isset($_GET['useremail'])) {
00311                         $userId = getUserIdFromEmail($_GET['useremail']);
00312                         if ($userId)
00313                                 return getQuizCorrectForm($this->moduleComponentId, $userId);
00314                         else
00315                                 displayerror('Error. Could not find user.');
00316                 }
00317                 elseif (isset($_POST['btnDeleteUser']) && isset($_POST['hdnUserId']) && is_numeric($_POST['hdnUserId'])) {
00318                         $quizObject = $this->getNewQuizObject();
00319                         if ($quizObject !== false)
00320                                 $quizObject->deleteEntries(intval($_POST['hdnUserId']));
00321                 }
00322 
00323                 return getQuizUserListHtml($this->moduleComponentId);
00324         }
00325 
00331         public function createModule($moduleComponentId) {
00332                 $insertQuery = "INSERT INTO `quiz_descriptions`(`page_modulecomponentid`,`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" .
00333                                         "('{$moduleComponentId}','New Quiz', 'Quiz under construction', 'Quiz under construction', 'simple', '00:30', '20', '10', 0, 1, 0, 1, 0)";
00334                 if (!mysql_query($insertQuery)) {
00335                         displayerror('Database Error. Could not create quiz. ' . $insertQuery . ' ' . mysql_error());
00336                 }
00337         }
00338 
00344         public function copyModule($moduleComponentId,$newId) {
00345                 return true;
00346         }
00347 
00353         public function deleteModule($moduleComponentId) {
00354                 return true;
00355         }
00356 
00361         private function getNewQuizObject() {
00362                 $quizRow = getQuizRow($this->moduleComponentId);
00363                 $quizType = $quizRow['quiz_quiztype'];
00364                 $quizObjectType = ucfirst($quizType) . 'Quiz';
00365                 if (!class_exists($quizObjectType)) {
00366                         displayerror('Error. This type of quiz has not been implemented yet.');
00367                         return false;
00368                 }
00369 
00370                 $quizObject = new $quizObjectType($this->moduleComponentId);
00371                 return $quizObject;
00372         }
00373 }

Generated on Mon Mar 14 2011 05:35:30 for Pragyan CMS by  doxygen 1.7.1