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
00010
00011
00012
00019 function getQuestionTypeCounts($quizId, $sectionId) {
00020 $countQuery = "SELECT `quiz_questiontype`, COUNT(*) FROM `quiz_questions` WHERE `page_modulecomponentid` = $quizId AND `quiz_sectionid` = $sectionId GROUP BY `quiz_questiontype`";
00021 $countResult = mysql_query($countQuery);
00022 $result = array();
00023 while ($countRow = mysql_fetch_row($countResult))
00024 $result[$countRow[0]] = $countRow[1];
00025 $questionTypes = array_keys(getQuestionTypes());
00026 for ($i = 0; $i < count($questionTypes); ++$i)
00027 if (!array_key_exists($questionTypes[$i], $result))
00028 $result[$questionTypes[$i]] = 0;
00029 return $result;
00030 }
00031
00037 function checkQuizSetup($quizId) {
00038 $sectionList = getSectionList($quizId);
00039 if (count($sectionList) == 0)
00040 return false;
00041
00042 $questionTypes = array_keys(getQuestionTypes());
00043 for ($i = 0; $i < count($sectionList); ++$i) {
00044 $questionCounts = getQuestionTypeCounts($quizId, $sectionList[$i]['quiz_sectionid']);
00045 for ($j = 0; $j < count($questionTypes); ++$j)
00046 if ($questionCounts[$questionTypes[$j]] < $sectionList[$i]['quiz_section' . $questionTypes[$j] . 'count'])
00047 return false;
00048 }
00049 return true;
00050 }
00051
00052 function checkQuizOpen($quizId) {
00053 $quizQuery = "SELECT IF(NOW() < `quiz_startdatetime`, -1, IF(NOW() > `quiz_enddatetime`, 1, 0)) FROM `quiz_descriptions` WHERE `page_modulecomponentid` = $quizId";
00054 $quizResult = mysql_query($quizQuery);
00055 $quizRow = mysql_fetch_row($quizResult);
00056 if (!$quizRow) {
00057 displayerror('Error. Could not find information about the given quiz.');
00058 return 1;
00059 }
00060 return $quizRow[0];
00061 }
00062
00063 function checkUserFirstAttempt($quizId, $userId) {
00064 $attemptQuery = "SELECT COUNT(*) FROM `quiz_userattempts` WHERE `page_modulecomponentid` = $quizId AND `user_id` = $userId";
00065 $attemptResult = mysql_query($attemptQuery);
00066 $attemptRow = mysql_fetch_row($attemptResult);
00067 return $attemptRow[0] == 0;
00068 }
00069
00070 function sectionBelongsToQuiz($quizId, $sectionId) {
00071 $sectionQuery = "SELECT COUNT(*) FROM `quiz_sections` WHERE `page_modulecomponentid` = $quizId AND `quiz_sectionid` = $sectionId";
00072 $sectionResult = mysql_query($sectionQuery);
00073 $sectionRow = mysql_fetch_row($sectionResult);
00074 return $sectionRow[0] == 1;
00075 }
00076
00077 function startSection($quizId, $sectionId, $userId) {
00078 $attemptQuery = "INSERT INTO `quiz_userattempts`(`page_modulecomponentid`, `quiz_sectionid`, `user_id`, `quiz_attemptstarttime`) VALUES " .
00079 "($quizId, $sectionId, $userId, NOW())";
00080 if (!mysql_query($attemptQuery)) {
00081 displayerror('Database Error. Could not mark section as started.');
00082 return false;
00083 }
00084 return true;
00085 }
00086
00087 function getFirstSectionId($quizId) {
00088 $sectionQuery = "SELECT MIN(`quiz_sectionid`) FROM `quiz_sections` WHERE `page_modulecomponentid` = $quizId";
00089 $sectionResult = mysql_query($sectionQuery);
00090 if (!$sectionResult)
00091 return -1;
00092 $sectionRow = mysql_fetch_row($sectionResult);
00093 return $sectionRow[0];
00094 }
00095
00096 function getAttemptRow($quizId, $sectionId, $userId) {
00097 $attemptQuery = "SELECT * FROM `quiz_userattempts` WHERE `page_modulecomponentid` = $quizId AND `quiz_sectionid` = $sectionId AND `user_id` = $userId";
00098 $attemptResult = mysql_query($attemptQuery);
00099 return mysql_fetch_assoc($attemptResult);
00100 }