/modules/quiz.php

https://github.com/Jessicasoon/ProjectKentRidgeV2 · PHP · 1383 lines · 986 code · 166 blank · 231 comment · 155 complexity · fb7509bcc9bbd9e8c5b99ca2af656e6a MD5 · raw file

  1. <?php
  2. /**************************************************************************
  3. Quiz Class: this class contains all the quiz operations on the database
  4. -Function exist: return false when quizzes do not exist or are not published
  5. -Function create: insert into table the newly created quiz then return back the quiz id for other purpose
  6. -Funtion update: update information of the quiz such as description, catrgory, picture, etc.
  7. -Function delete($memberID): check whether the current user is member-> delete questions, results and images
  8. -Function addResult: create a new result
  9. -Function updateResult: create a new result
  10. -Function removeResult: remove a new result
  11. -Function addQuestion: create a new question
  12. -Function update a question
  13. -Function remove a question
  14. -Function addQuestion: create a new option
  15. -Function update an option
  16. -Function remove an option
  17. -Function numQuestions: return the number of question in this quiz
  18. -Function isPublished: return the publish status of the quiz
  19. -Function checkPublish: check if quiz is ready to be published
  20. -Function: publish the quiz
  21. -Function: re-publish the quiz
  22. -Function: archive the quiz
  23. -Function: get the rating value by a member
  24. -Function getResults: get the list of results belonging to this quiz
  25. -Function getQuestions: get the list of questions belonging to this quiz
  26. -Function getOptions: get the list of options belonging to a question
  27. -Function getAttempts: get the list of attempts for this quiz
  28. -Function awardPoints: award points based on like(1), dislike(-1) or neutral(0)
  29. -Function creator: return the text name of the creator
  30. -Function category: return the quiz topic
  31. -Function isOwner: check if user is owner
  32. -Function hasTaken: check if a user has taken quiz
  33. -Function bindImagekey: bind a unikey with this quiz
  34. ****************************************************************/
  35. if(!class_exists("Quiz")){
  36. class Quiz{
  37. // Quiz data
  38. public $quiz_id = NULL;
  39. public $quiz_name = NULL;
  40. public $quiz_description = NULL;
  41. public $fk_quiz_cat = NULL;
  42. public $quiz_picture = NULL;
  43. public $creation_date = NULL;
  44. public $fk_member_id = NULL;
  45. public $quiz_score = NULL;
  46. public $likes = NULL;
  47. public $dislikes = NULL;
  48. public $isPublished = NULL;
  49. public $quiz_key = NULL;
  50. public $quiz_not_available = NULL;
  51. function __construct($quiz_id = NULL){
  52. if($quiz_id != NULL && $quiz_id != ""){
  53. require('quizrooDB.php');
  54. // populate class with quiz data
  55. $queryQuiz = sprintf("SELECT * FROM q_quizzes WHERE quiz_id = %d", GetSQLValueString($quiz_id, "int"));
  56. $getQuiz = mysql_query($queryQuiz, $quizroo) or die(mysql_error());
  57. $row_getQuiz = mysql_fetch_assoc($getQuiz);
  58. $totalRows_getQuiz = mysql_num_rows($getQuiz);
  59. if($totalRows_getQuiz != 0){
  60. $this->quiz_id = $quiz_id;
  61. $this->quiz_name = $row_getQuiz['quiz_name'];
  62. $this->quiz_description = $row_getQuiz['quiz_description'];
  63. $this->fk_quiz_cat = $row_getQuiz['fk_quiz_cat'];
  64. $this->quiz_picture = $row_getQuiz['quiz_picture'];
  65. $this->creation_date = $row_getQuiz['creation_date'];
  66. $this->fk_member_id = $row_getQuiz['fk_member_id'];
  67. $this->quiz_score = $row_getQuiz['quiz_score'];
  68. $this->likes = $row_getQuiz['likes'];
  69. $this->dislikes = $row_getQuiz['dislikes'];
  70. $this->isPublished = $row_getQuiz['isPublished'];
  71. $this->quiz_key = $row_getQuiz['quiz_key'];
  72. return true;
  73. }else{
  74. $this->quiz_not_available = true;
  75. return false;
  76. }
  77. }else{
  78. $this->quiz_not_available = true;
  79. return false;
  80. }
  81. }
  82. function exists(){
  83. if($this->quiz_not_available){
  84. return false;
  85. }else{
  86. if($this->isPublished == 3){
  87. return false;
  88. }else{
  89. return true;
  90. }
  91. }
  92. }
  93. // create a new quiz
  94. function create($title, $description, $cat, $picture, $member_id, $key){
  95. require('quizrooDB.php');
  96. // insert into the quiz table (protect each insert from HTML Injection)
  97. $insertSQL = sprintf("INSERT INTO q_quizzes(`quiz_name`, `quiz_description`, `fk_quiz_cat`, `quiz_picture`, `fk_member_id`, `quiz_key`) VALUES (%s, %s, %d, %s, %d, %s)",
  98. htmlentities(GetSQLValueString($title, "text")),
  99. htmlentities(GetSQLValueString($description, "text")),
  100. GetSQLValueString($cat, "int"),
  101. GetSQLValueString($picture, "text"),
  102. GetSQLValueString($member_id, "int"),
  103. GetSQLValueString($key, "text"));
  104. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  105. // find the quiz id
  106. $querySQL = "SELECT LAST_INSERT_ID() AS insertID, creation_date FROM q_quizzes WHERE quiz_id = LAST_INSERT_ID()";
  107. $resultID = mysql_query($querySQL, $quizroo) or die(mysql_error());
  108. $row_resultID = mysql_fetch_assoc($resultID);
  109. $this->quiz_id = $row_resultID['insertID'];
  110. $this->quiz_name = htmlentities($title);
  111. $this->quiz_description = htmlentities($description);
  112. $this->fk_quiz_cat = $cat;
  113. $this->quiz_picture = $picture;
  114. $this->creation_date = $row_resultID['creation_date'];
  115. $this->fk_member_id = $member_id;
  116. $this->quiz_key = $key;
  117. mysql_free_result($resultID);
  118. // update the keystore
  119. $this->bindImagekey($key);
  120. return $this->quiz_id;
  121. }
  122. // update the quiz
  123. function update($title, $description, $cat, $picture, $memberID){
  124. require('quizrooDB.php');
  125. // check if is member
  126. if($this->isOwner($memberID)){
  127. // insert into the quiz table
  128. $insertSQL = sprintf("UPDATE q_quizzes SET `quiz_name`=%s, `quiz_description`=%s, `fk_quiz_cat`=%d, `quiz_picture`=%s WHERE `quiz_id` = %d",
  129. htmlentities(GetSQLValueString($title, "text")),
  130. htmlentities(GetSQLValueString($description, "text")),
  131. GetSQLValueString($cat, "int"),
  132. GetSQLValueString($picture, "text"),
  133. GetSQLValueString($this->quiz_id, "int"));
  134. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  135. return $this->quiz_id;
  136. }else{
  137. return false;
  138. }
  139. }
  140. // delete the quiz
  141. function deleteMulti($memberID){
  142. require('quizrooDB.php');
  143. // check if is member
  144. if($this->isOwner($memberID)){
  145. // remove the questions
  146. $questionList = explode(',', $this->getQuestions());
  147. foreach($questionList as $question){
  148. if($question != ""){
  149. $this->removeQuestionMulti($question, $memberID);
  150. }
  151. }
  152. // remove the results
  153. $resultList = explode(',', $this->getResultsMulti());
  154. foreach($resultList as $result){
  155. if($result != ""){
  156. $this->removeResultMulti($result, $memberID);
  157. }
  158. }
  159. // clean images
  160. if($this->quiz_key != ""){
  161. foreach(glob("../quiz_images/".$this->quiz_key."*") as $filename){
  162. unlink($filename);
  163. }
  164. }
  165. // remove the question
  166. $insertSQL = sprintf("DELETE FROM q_quizzes WHERE `quiz_id` = %d", GetSQLValueString($this->quiz_id, "int"));
  167. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  168. $removed_id = $this->quiz_id;
  169. $this->quiz_id = NULL;
  170. $this->quiz_name = NULL;
  171. $this->quiz_description = NULL;
  172. $this->fk_quiz_cat = NULL;
  173. $this->quiz_picture = NULL;
  174. $this->creation_date = NULL;
  175. $this->fk_member_id = NULL;
  176. $this->quiz_score = NULL;
  177. $this->likes = NULL;
  178. $this->dislikes = NULL;
  179. $this->isPublished = NULL;
  180. $this->quiz_key = NULL;
  181. return $removed_id;
  182. }else{
  183. return false;
  184. }
  185. }
  186. // delete the quiz HAVE TO CHANGE YL
  187. function deleteTest($memberID){
  188. require('quizrooDB.php');
  189. // check if is member
  190. if($this->isOwner($memberID)){
  191. // remove the questions
  192. $questionList = explode(',', $this->getQuestions());
  193. foreach($questionList as $question){
  194. if($question != ""){
  195. $this->removeQuestionTest($question, $memberID);
  196. }
  197. }
  198. // remove the results
  199. $resultList = explode(',', $this->getResultsTest());
  200. foreach($resultList as $result){
  201. if($result != ""){
  202. $this->removeResultTest($result, $memberID);
  203. }
  204. }
  205. // clean images
  206. if($this->quiz_key != ""){
  207. foreach(glob("../quiz_images/".$this->quiz_key."*") as $filename){
  208. unlink($filename);
  209. }
  210. }
  211. // remove the question
  212. $insertSQL = sprintf("DELETE FROM q_quizzes WHERE `quiz_id` = %d", GetSQLValueString($this->quiz_id, "int"));
  213. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  214. $removed_id = $this->quiz_id;
  215. $this->quiz_id = NULL;
  216. $this->quiz_name = NULL;
  217. $this->quiz_description = NULL;
  218. $this->fk_quiz_cat = NULL;
  219. $this->quiz_picture = NULL;
  220. $this->creation_date = NULL;
  221. $this->fk_member_id = NULL;
  222. $this->quiz_score = NULL;
  223. $this->likes = NULL;
  224. $this->dislikes = NULL;
  225. $this->isPublished = NULL;
  226. $this->quiz_key = NULL;
  227. return $removed_id;
  228. }else{
  229. return false;
  230. }
  231. }
  232. // delete the quiz
  233. function delete_NOTUSED($memberID){
  234. require('quizrooDB.php');
  235. // check if is member
  236. if($this->isOwner($memberID)){
  237. // remove the questions
  238. $questionList = explode(',', $this->getQuestions());
  239. foreach($questionList as $question){
  240. if($question != ""){
  241. $this->removeQuestion_NOTUSED($question, $memberID);
  242. }
  243. }
  244. // remove the results
  245. $resultList = explode(',', $this->getResults_NOTUSED());
  246. foreach($resultList as $result){
  247. if($result != ""){
  248. $this->removeResult_NOTUSED($result, $memberID);
  249. }
  250. }
  251. // clean images
  252. if($this->quiz_key != ""){
  253. foreach(glob("../quiz_images/".$this->quiz_key."*") as $filename){
  254. unlink($filename);
  255. }
  256. }
  257. // remove the question
  258. $insertSQL = sprintf("DELETE FROM q_quizzes WHERE `quiz_id` = %d", GetSQLValueString($this->quiz_id, "int"));
  259. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  260. $removed_id = $this->quiz_id;
  261. $this->quiz_id = NULL;
  262. $this->quiz_name = NULL;
  263. $this->quiz_description = NULL;
  264. $this->fk_quiz_cat = NULL;
  265. $this->quiz_picture = NULL;
  266. $this->creation_date = NULL;
  267. $this->fk_member_id = NULL;
  268. $this->quiz_score = NULL;
  269. $this->likes = NULL;
  270. $this->dislikes = NULL;
  271. $this->isPublished = NULL;
  272. $this->quiz_key = NULL;
  273. return $removed_id;
  274. }else{
  275. return false;
  276. }
  277. }
  278. // create a new result
  279. function addResultMulti($result_title, $result_description, $result_picture, $memberID){
  280. require('quizrooDB.php');
  281. // check if is member
  282. if($this->isOwner($memberID)){
  283. // Insert the result
  284. $insertSQL = sprintf("INSERT INTO q_results_multi(`result_title`, `result_description`, `result_picture`, `fk_quiz_id`) VALUES (%s, %s, %s, %d)",
  285. htmlentities(GetSQLValueString($result_title, "text")),
  286. htmlentities(GetSQLValueString($result_description, "text")),
  287. GetSQLValueString($result_picture, "text"),
  288. GetSQLValueString($this->quiz_id, "int"));
  289. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  290. // find the result id
  291. $querySQL = "SELECT LAST_INSERT_ID() AS insertID";
  292. $resultID = mysql_query($querySQL, $quizroo) or die(mysql_error());
  293. $row_resultID = mysql_fetch_assoc($resultID);
  294. mysql_free_result($resultID);
  295. return $row_resultID['insertID'];
  296. }else{
  297. return false;
  298. }
  299. }
  300. // create a new result HAVE TO CHANGE YL
  301. function addResultTest($result_title, $result_description, $result_picture, $result_max, $result_min, $memberID){
  302. require('quizrooDB.php');
  303. // check if is member
  304. if($this->isOwner($memberID)){
  305. // Insert the result
  306. $insertSQL = sprintf("INSERT INTO q_results_test(`result_title`, `result_description`, `result_picture`, `range_max`, `range_min`, `fk_quiz_id`) VALUES (%s, %s, %s, %d, %d, %d)",
  307. htmlentities(GetSQLValueString($result_title, "text")),
  308. htmlentities(GetSQLValueString($result_description, "text")),
  309. GetSQLValueString($result_picture, "text"),
  310. GetSQLValueString($result_max, "int"),
  311. GetSQLValueString($result_min, "int"),
  312. GetSQLValueString($this->quiz_id, "int"));
  313. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  314. // find the result id
  315. $querySQL = "SELECT LAST_INSERT_ID() AS insertID";
  316. $resultID = mysql_query($querySQL, $quizroo) or die(mysql_error());
  317. $row_resultID = mysql_fetch_assoc($resultID);
  318. mysql_free_result($resultID);
  319. return $row_resultID['insertID'];
  320. }else{
  321. return false;
  322. }
  323. }
  324. // create a new result
  325. function addResult_NOTUSED($result_title, $result_description, $result_picture, $memberID){
  326. require('quizrooDB.php');
  327. // check if is member
  328. if($this->isOwner($memberID)){
  329. // Insert the result
  330. $insertSQL = sprintf("INSERT INTO q_results(`result_title`, `result_description`, `result_picture`, `fk_quiz_id`) VALUES (%s, %s, %s, %d)",
  331. htmlentities(GetSQLValueString($result_title, "text")),
  332. htmlentities(GetSQLValueString($result_description, "text")),
  333. GetSQLValueString($result_picture, "text"),
  334. GetSQLValueString($this->quiz_id, "int"));
  335. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  336. // find the result id
  337. $querySQL = "SELECT LAST_INSERT_ID() AS insertID";
  338. $resultID = mysql_query($querySQL, $quizroo) or die(mysql_error());
  339. $row_resultID = mysql_fetch_assoc($resultID);
  340. mysql_free_result($resultID);
  341. return $row_resultID['insertID'];
  342. }else{
  343. return false;
  344. }
  345. }
  346. // create a new result
  347. function updateResultMulti($result_title, $result_description, $result_picture, $result_id, $memberID){
  348. require('quizrooDB.php');
  349. // check if is member
  350. if($this->isOwner($memberID)){
  351. // Insert the result
  352. $insertSQL = sprintf("UPDATE q_results_multi SET `result_title` = %s, `result_description` = %s, `result_picture` = %s WHERE `result_id` = %d",
  353. htmlentities(GetSQLValueString($result_title, "text")),
  354. htmlentities(GetSQLValueString($result_description, "text")),
  355. GetSQLValueString($result_picture, "text"),
  356. GetSQLValueString($result_id, "int"));
  357. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  358. return $result_id;
  359. }else{
  360. return false;
  361. }
  362. }
  363. // create a new result HAVE TO CHANGE YL
  364. function updateResultTest($result_title, $result_description, $result_picture, $result_id, $result_max, $result_min, $memberID){
  365. require('quizrooDB.php');
  366. // check if is member
  367. if($this->isOwner($memberID)){
  368. // Insert the result
  369. $insertSQL = sprintf("UPDATE q_results_test SET `result_title` = %s, `result_description` = %s, `result_picture` = %s, `range_max` = %d, `range_min` = %d WHERE `result_id` = %d",
  370. htmlentities(GetSQLValueString($result_title, "text")),
  371. htmlentities(GetSQLValueString($result_description, "text")),
  372. GetSQLValueString($result_picture, "text"),
  373. GetSQLValueString($result_max, "int"),
  374. GetSQLValueString($result_min, "int"),
  375. GetSQLValueString($result_id, "int"));
  376. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  377. return $result_id;
  378. }else{
  379. return false;
  380. }
  381. }
  382. // create a new result
  383. function updateResult_NOTUSED($result_title, $result_description, $result_picture, $result_id, $memberID){
  384. require('quizrooDB.php');
  385. // check if is member
  386. if($this->isOwner($memberID)){
  387. // Insert the result
  388. $insertSQL = sprintf("UPDATE q_results SET `result_title` = %s, `result_description` = %s, `result_picture` = %s WHERE `result_id` = %d",
  389. htmlentities(GetSQLValueString($result_title, "text")),
  390. htmlentities(GetSQLValueString($result_description, "text")),
  391. GetSQLValueString($result_picture, "text"),
  392. GetSQLValueString($result_id, "int"));
  393. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  394. return $result_id;
  395. }else{
  396. return false;
  397. }
  398. }
  399. // remove a new result
  400. function removeResultMulti($result_id, $memberID){
  401. require('quizrooDB.php');
  402. // owner check
  403. if($this->isOwner($memberID)){
  404. // delete the result and also check if this results actually belongs to this quiz
  405. $insertSQL = sprintf("DELETE FROM q_results_multi WHERE `result_id` = %d AND `result_id` IN(%s)", GetSQLValueString($result_id, "int"), $this->getResultsMulti());
  406. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  407. return true;
  408. }else{
  409. return false;
  410. }
  411. }
  412. // remove a new result HAVE TO CHANGE YL
  413. function removeResultTest($result_id, $memberID){
  414. require('quizrooDB.php');
  415. // owner check
  416. if($this->isOwner($memberID)){
  417. // delete the result and also check if this results actually belongs to this quiz
  418. $insertSQL = sprintf("DELETE FROM q_results_test WHERE `result_id` = %d AND `result_id` IN(%s)", GetSQLValueString($result_id, "int"), $this->getResultsTest());
  419. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  420. return true;
  421. }else{
  422. return false;
  423. }
  424. }
  425. // remove a new result
  426. function removeResult_NOTUSED($result_id, $memberID){
  427. require('quizrooDB.php');
  428. // owner check
  429. if($this->isOwner($memberID)){
  430. // delete the result and also check if this results actually belongs to this quiz
  431. $insertSQL = sprintf("DELETE FROM q_results WHERE `result_id` = %d AND `result_id` IN(%s)", GetSQLValueString($result_id, "int"), $this->getResults_NOTUSED());
  432. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  433. return true;
  434. }else{
  435. return false;
  436. }
  437. }
  438. // create a new question
  439. function addQuestion($question, $memberID){
  440. require('quizrooDB.php');
  441. // check if is member
  442. if($this->isOwner($memberID)){
  443. // insert the question
  444. $insertSQL = sprintf("INSERT INTO q_questions(`question`, `fk_quiz_id`) VALUES (%s, %d)",
  445. htmlentities(GetSQLValueString($question, "text")),
  446. GetSQLValueString($this->quiz_id, "int"));
  447. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  448. // find the question id
  449. $querySQL = "SELECT LAST_INSERT_ID() AS insertID";
  450. $resultID = mysql_query($querySQL, $quizroo) or die(mysql_error());
  451. $row_resultID = mysql_fetch_assoc($resultID);
  452. $currentQuestionID = $row_resultID['insertID'];
  453. mysql_free_result($resultID);
  454. return $row_resultID['insertID'];
  455. }else{
  456. return false;
  457. }
  458. }
  459. // update a question
  460. function updateQuestion($question, $question_id, $memberID){
  461. require('quizrooDB.php');
  462. // check if is member
  463. if($this->isOwner($memberID)){
  464. // insert the question
  465. $insertSQL = sprintf("UPDATE q_questions SET `question` = %s WHERE `question_id` = %d",
  466. htmlentities(GetSQLValueString($question, "text")),
  467. GetSQLValueString($question_id, "int"));
  468. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  469. return $question_id;
  470. }else{
  471. return false;
  472. }
  473. }
  474. // remove a new question
  475. function removeQuestionMulti($question_id, $memberID){
  476. require('quizrooDB.php');
  477. // owner check
  478. if($this->isOwner($memberID)){
  479. // delete the options and also check if this results actually belongs to this quiz
  480. $insertSQL = sprintf("DELETE FROM q_options_multi WHERE `fk_question_id` = %d AND `fk_question_id` IN(%s)", GetSQLValueString($question_id, "int"), $this->getQuestions());
  481. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  482. // delete the result and also check if this results actually belongs to this quiz
  483. $insertSQL = sprintf("DELETE FROM q_questions WHERE `question_id` = %d AND `question_id` IN(%s)", GetSQLValueString($question_id, "int"), $this->getQuestions());
  484. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  485. return true;
  486. }else{
  487. return false;
  488. }
  489. }
  490. // remove a new question HAVE TO CHANGE YL
  491. function removeQuestionTest($question_id, $memberID){
  492. require('quizrooDB.php');
  493. // owner check
  494. if($this->isOwner($memberID)){
  495. // delete the options and also check if this results actually belongs to this quiz
  496. $insertSQL = sprintf("DELETE FROM q_options_test WHERE `fk_question_id` = %d AND `fk_question_id` IN(%s)", GetSQLValueString($question_id, "int"), $this->getQuestions());
  497. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  498. // delete the result and also check if this results actually belongs to this quiz
  499. $insertSQL = sprintf("DELETE FROM q_questions WHERE `question_id` = %d AND `question_id` IN(%s)", GetSQLValueString($question_id, "int"), $this->getQuestions());
  500. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  501. return true;
  502. }else{
  503. return false;
  504. }
  505. }
  506. // remove a new question
  507. function removeQuestion_NOTUSED($question_id, $memberID){
  508. require('quizrooDB.php');
  509. // owner check
  510. if($this->isOwner($memberID)){
  511. // delete the options and also check if this results actually belongs to this quiz
  512. $insertSQL = sprintf("DELETE FROM q_options WHERE `fk_question_id` = %d AND `fk_question_id` IN(%s)", GetSQLValueString($question_id, "int"), $this->getQuestions());
  513. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  514. // delete the result and also check if this results actually belongs to this quiz
  515. $insertSQL = sprintf("DELETE FROM q_questions WHERE `question_id` = %d AND `question_id` IN(%s)", GetSQLValueString($question_id, "int"), $this->getQuestions());
  516. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  517. return true;
  518. }else{
  519. return false;
  520. }
  521. }
  522. // create a option
  523. function addOptionMulti($option, $result, $weightage, $question, $memberID){
  524. require('quizrooDB.php');
  525. // check if is member
  526. if($this->isOwner($memberID)){
  527. // insert the option
  528. $insertSQL = sprintf("INSERT INTO q_options_multi(`option`, `fk_result_id`, `option_weightage`, `fk_question_id`) VALUES (%s, %d, %d, %d)",
  529. htmlentities(GetSQLValueString($option, "text")),
  530. GetSQLValueString($result, "int"),
  531. GetSQLValueString($weightage, "int"),
  532. GetSQLValueString($question, "int"));
  533. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  534. // find the question id
  535. $querySQL = "SELECT LAST_INSERT_ID() AS insertID";
  536. $resultID = mysql_query($querySQL, $quizroo) or die(mysql_error());
  537. $row_resultID = mysql_fetch_assoc($resultID);
  538. $currentQuestionID = $row_resultID['insertID'];
  539. mysql_free_result($resultID);
  540. return $row_resultID['insertID'];
  541. }else{
  542. return false;
  543. }
  544. }
  545. // create a option
  546. function addOption_NOTUSED($option, $result, $weightage, $question, $memberID){
  547. require('quizrooDB.php');
  548. // check if is member
  549. if($this->isOwner($memberID)){
  550. // insert the option
  551. $insertSQL = sprintf("INSERT INTO q_options(`option`, `fk_result`, `option_weightage`, `fk_question_id`) VALUES (%s, %d, %d, %d)",
  552. htmlentities(GetSQLValueString($option, "text")),
  553. GetSQLValueString($result, "int"),
  554. GetSQLValueString($weightage, "int"),
  555. GetSQLValueString($question, "int"));
  556. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  557. // find the question id
  558. $querySQL = "SELECT LAST_INSERT_ID() AS insertID";
  559. $resultID = mysql_query($querySQL, $quizroo) or die(mysql_error());
  560. $row_resultID = mysql_fetch_assoc($resultID);
  561. $currentQuestionID = $row_resultID['insertID'];
  562. mysql_free_result($resultID);
  563. return $row_resultID['insertID'];
  564. }else{
  565. return false;
  566. }
  567. }
  568. // create a option TEST
  569. function addOptionTest($option, $result, $question, $memberID){
  570. require('quizrooDB.php');
  571. // check if is member
  572. if($this->isOwner($memberID)){
  573. // insert the option
  574. $insertSQL = sprintf("INSERT INTO q_options_test(`option`, `isCorrect`, `fk_question_id`) VALUES (%s, %d, %d)",
  575. htmlentities(GetSQLValueString($option, "text")),
  576. GetSQLValueString($result, "int"), //cannot insert if isCorrect = 0?
  577. GetSQLValueString($question, "int"));
  578. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  579. // find the question id
  580. $querySQL = "SELECT LAST_INSERT_ID() AS insertID";
  581. $resultID = mysql_query($querySQL, $quizroo) or die(mysql_error());
  582. $row_resultID = mysql_fetch_assoc($resultID);
  583. $currentQuestionID = $row_resultID['insertID'];
  584. mysql_free_result($resultID);
  585. return $row_resultID['insertID'];
  586. }else{
  587. return false;
  588. }
  589. }
  590. // update an option
  591. function updateOptionMulti($option, $result, $weightage, $option_id, $memberID){
  592. require('quizrooDB.php');
  593. //$one = 1;
  594. // check if is member
  595. if($this->isOwner($memberID)){
  596. // insert the option
  597. $insertSQL = sprintf("UPDATE q_options_multi SET `option`=%s, `fk_result_id`=%d, `option_weightage`=%d WHERE option_id=%d",
  598. htmlentities(GetSQLValueString($option, "text")),
  599. GetSQLValueString($result, "int"),
  600. GetSQLValueString($weightage, "int"),
  601. GetSQLValueString($option_id, "int"));
  602. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  603. return $option_id;
  604. }else{
  605. return false;
  606. }
  607. }
  608. // update an option
  609. function updateOption_NOTUSED($option, $result, $weightage, $option_id, $memberID){
  610. require('quizrooDB.php');
  611. // check if is member
  612. if($this->isOwner($memberID)){
  613. // insert the option
  614. $insertSQL = sprintf("UPDATE q_options SET `option`=%s, `fk_result`=%d, `option_weightage`=%d WHERE option_id=%d",
  615. htmlentities(GetSQLValueString($option, "text")),
  616. GetSQLValueString($result, "int"),
  617. GetSQLValueString($weightage, "int"),
  618. GetSQLValueString($option_id, "int"));
  619. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  620. return $option_id;
  621. }else{
  622. return false;
  623. }
  624. }
  625. // update an option TEST
  626. function updateOptionTest($option, $result, $option_id, $memberID){
  627. require('quizrooDB.php');
  628. // check if is member
  629. if($this->isOwner($memberID)){
  630. // insert the option
  631. $insertSQL = sprintf("UPDATE q_options_test SET `option`=%s, `isCorrect`=%d WHERE option_id=%d",
  632. htmlentities(GetSQLValueString($option, "text")),
  633. GetSQLValueString($result, "int"),
  634. GetSQLValueString($option_id, "int"));
  635. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  636. return $option_id;
  637. }else{
  638. return false;
  639. }
  640. }
  641. // remove an option
  642. function removeOptionMulti($option_id, $memberID){
  643. require('quizrooDB.php');
  644. // owner check
  645. if($this->isOwner($memberID)){
  646. // delete the options and also check if this results actually belongs to this quiz
  647. $insertSQL = sprintf("DELETE FROM q_options_multi WHERE `option_id` = %d AND `fk_question_id` IN(%s)",
  648. GetSQLValueString($option_id, "int"),
  649. htmlentities(GetSQLValueString($this->getQuestions())));
  650. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  651. return true;
  652. }else{
  653. return false;
  654. }
  655. }
  656. // remove an option HAVE TO CHANGE YL
  657. function removeOptionTest($option_id, $memberID){
  658. require('quizrooDB.php');
  659. // owner check
  660. if($this->isOwner($memberID)){
  661. // delete the options and also check if this results actually belongs to this quiz
  662. $insertSQL = sprintf("DELETE FROM q_options_test WHERE `option_id` = %d AND `fk_question_id` IN(%s)", GetSQLValueString($option_id, "int"), $this->getQuestions());
  663. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  664. return true;
  665. }else{
  666. return false;
  667. }
  668. }
  669. // remove an option
  670. function removeOption_NOTUSED($option_id, $memberID){
  671. require('quizrooDB.php');
  672. // owner check
  673. if($this->isOwner($memberID)){
  674. // delete the options and also check if this results actually belongs to this quiz
  675. $insertSQL = sprintf("DELETE FROM q_options WHERE `option_id` = %d AND `fk_question_id` IN(%s)", GetSQLValueString($option_id, "int"), $this->getQuestions());
  676. mysql_query($insertSQL, $quizroo) or die(mysql_error());
  677. return true;
  678. }else{
  679. return false;
  680. }
  681. }
  682. // return the number of question in this quiz
  683. function numQuestions(){
  684. require('quizrooDB.php');
  685. $query = sprintf("SELECT question_id FROM q_questions WHERE fk_quiz_id = %d", GetSQLValueString($this->quiz_id, "int"));
  686. $getQuery = mysql_query($query, $quizroo) or die(mysql_error());
  687. $row_getQuery = mysql_fetch_assoc($getQuery);
  688. return mysql_num_rows($getQuery);
  689. }
  690. // return the publish status of the quiz
  691. function isPublished(){
  692. if($this->isPublished == 1){
  693. return true;
  694. }else{
  695. return false;
  696. }
  697. }
  698. // check if quiz is ready to be published
  699. function checkPublishMulti(){
  700. require('variables.php');
  701. // check the number of results
  702. $numResults = $this->getResultsMulti("count");
  703. // check the number of questions
  704. $numQuestions = $this->getQuestions("count");
  705. // check the number of options
  706. $listQuestion = explode(',', $this->getQuestions());
  707. if($numQuestions != 0){
  708. $questionState = true;
  709. $optionState = true;
  710. foreach($listQuestion as $question){
  711. // check the number of options for this question
  712. $numOptions = $this->getOptionsMulti($question, "count");
  713. if($numOptions < $VAR_QUIZ_MIN_OPTIONS){
  714. $optionState = false;
  715. }
  716. }
  717. }else{
  718. $questionState = false;
  719. $optionState = false;
  720. }
  721. //run through the checks, return false if failed
  722. if($numResults < $VAR_QUIZ_MIN_RESULT || $numQuestions < $VAR_QUIZ_MIN_QUESTIONS || !$optionState){
  723. return false;
  724. }else{
  725. return true;
  726. }
  727. }
  728. // check if quiz is ready to be published HAVE TO CHANGE YL
  729. function checkPublishTest($mode){
  730. require('variables.php');
  731. // check the number of results
  732. $numResults = $this->getResultsTest("count");
  733. // check the number of questions
  734. $numQuestions = $this->getQuestions("count");
  735. // check the number of options
  736. $listQuestion = explode(',', $this->getQuestions());
  737. if($numQuestions != 0){
  738. $questionState = true;
  739. $optionState = true;
  740. foreach($listQuestion as $question){
  741. // check the number of options for this question
  742. $numOptions = $this->getOptionsTest($question, "count");
  743. if($numOptions < $VAR_QUIZ_MIN_OPTIONS){
  744. $optionState = false;
  745. }
  746. }
  747. }else{
  748. $questionState = false;
  749. $optionState = false;
  750. }
  751. // run through the checks, return false if failed
  752. if($numQuestions < $VAR_QUIZ_MIN_QUESTIONS || !$optionState){
  753. return false;
  754. }else{
  755. return true;
  756. }
  757. if($mode != 'test_simple'){
  758. if($numResults < $VAR_QUIZ_MIN_RESULT){
  759. return false;
  760. }else{
  761. return true;
  762. }
  763. }
  764. }
  765. // check if quiz is ready to be published
  766. function checkPublish_NOTUSED(){
  767. require('variables.php');
  768. // check the number of results
  769. $numResults = $this->getResults_NOTUSED("count");
  770. // check the number of questions
  771. $numQuestions = $this->getQuestions("count");
  772. // check the number of options
  773. $listQuestion = explode(',', $this->getQuestions());
  774. if($numQuestions != 0){
  775. $questionState = true;
  776. $optionState = true;
  777. foreach($listQuestion as $question){
  778. // check the number of options for this question
  779. $numOptions = $this->getOptions_NOTUSED($question, "count");
  780. if($numOptions < $VAR_QUIZ_MIN_OPTIONS){
  781. $optionState = false;
  782. }
  783. }
  784. }else{
  785. $questionState = false;
  786. $optionState = false;
  787. }
  788. // run through the checks, return false if failed
  789. if($numResults < $VAR_QUIZ_MIN_RESULT || $numQuestions < $VAR_QUIZ_MIN_QUESTIONS || !$optionState){
  790. return false;
  791. }else{
  792. return true;
  793. }
  794. }
  795. // publish the quiz
  796. function publish($memberID){
  797. require('quizrooDB.php');
  798. require('variables.php');
  799. //***********************************************ADD BY LIEN************************************************//
  800. $queryMode = sprintf("SELECT display_mode FROM q_quizzes WHERE quiz_id = %d", $this->quiz_id);
  801. $resultMode = mysql_query($queryMode, $quizroo) or die(mysql_error());
  802. $row_resultMode = mysql_fetch_assoc($resultMode);
  803. $resultforMode = array();
  804. $mode = "";
  805. do{
  806. $resultforMode[] = array($row_resultMode['display_mode']);
  807. if ($row_resultMode['display_mode'] == "multi_simple")
  808. $mode = "simple";
  809. if ($row_resultMode['display_mode'] == "multi_accurate")
  810. $mode = "accurate";
  811. if ($row_resultMode['display_mode'] == "test_simple")
  812. $mode = "test_simple";
  813. if ($row_resultMode['display_mode'] == "test_custom")
  814. $mode = "test_custom";
  815. }while($row_resultMode = mysql_fetch_assoc($resultMode));
  816. //***********************************************END OF ADD BY LIEN************************************************//
  817. // check ig quiz belongs to member
  818. if($this->isOwner($memberID)){
  819. // check if the quiz is already published
  820. if(!$this->isPublished()){
  821. // run through the checks, return false if failed
  822. if($mode == "simple" || $mode == "accurate"){
  823. if(!$this->checkPublishMulti()){
  824. return -2;
  825. }
  826. }
  827. else{
  828. if(!$this->checkPublishTest($mode)){
  829. return -2;
  830. }
  831. }
  832. // check if coming from edits
  833. if($this->isPublished == 0){
  834. // set the publish flag to 1 and award the first creation score
  835. $query = sprintf("UPDATE q_quizzes SET isPublished = 1, quiz_score = %d WHERE quiz_id = %d", $GAME_BASE_POINT, $this->quiz_id);
  836. mysql_query($query, $quizroo) or die(mysql_error());
  837. $this->isPublished = 1;
  838. // check the current member stats (for level up calculation later)
  839. $queryCheck = sprintf("SELECT `level`, quiztaker_score FROM `s_members` WHERE `member_id` = %d", $this->fk_member_id);
  840. $getResults = mysql_query($queryCheck, $quizroo) or die(mysql_error());
  841. $row_getResults = mysql_fetch_assoc($getResults);
  842. $old_level = $row_getResults['level'];
  843. $old_score = $row_getResults['quiztaker_score'];
  844. mysql_free_result($getResults);
  845. // update the member's creation score
  846. $query = sprintf("UPDATE s_members SET quizcreator_score = quizcreator_score + %d, quizcreator_score_today = quizcreator_score_today + %d WHERE member_id = %s", $GAME_BASE_POINT, $GAME_BASE_POINT, $this->fk_member_id);
  847. mysql_query($query, $quizroo) or die(mysql_error());
  848. // check if the there is a levelup:
  849. ///////////////////////////////////////
  850. // check the level table
  851. $queryLevel = sprintf("SELECT id FROM `g_levels` WHERE points <= %d ORDER BY points DESC LIMIT 0, 1", $old_score + $GAME_BASE_POINT);
  852. $getLevel = mysql_query($queryLevel, $quizroo) or die(mysql_error());
  853. $row_getLevel = mysql_fetch_assoc($getLevel);
  854. $new_level = $row_getLevel['id'];
  855. if($new_level > $old_level){ // a levelup has occurred
  856. // update the member table to reflect the new level
  857. $queryUpdate = sprintf("UPDATE s_members SET level = %d WHERE member_id = %s", $new_level, $this->fk_member_id);
  858. mysql_query($queryUpdate, $quizroo) or die(mysql_error());
  859. // return the ID of the level acheievement
  860. return $new_level;
  861. }else{
  862. return -1;
  863. }
  864. }else{
  865. // set the publish flag to 1
  866. $query = sprintf("UPDATE q_quizzes SET isPublished = 1 WHERE quiz_id = %d", $this->quiz_id);
  867. mysql_query($query, $quizroo) or die(mysql_error());
  868. $this->isPublished = 1;
  869. return -1;
  870. }
  871. }else{
  872. // already published, do nothing
  873. return -1;
  874. }
  875. }else{
  876. return false;
  877. }
  878. }
  879. // re-publish the quiz
  880. function republish($memberID){
  881. require('quizrooDB.php');
  882. require('variables.php');
  883. // check if the quiz is already published
  884. if(!$this->isPublished() && $this->isOwner($memberID)){
  885. // set the publish flag to 1
  886. $query = sprintf("UPDATE q_quizzes SET isPublished = 1 WHERE quiz_id = %d", $this->quiz_id);
  887. mysql_query($query, $quizroo) or die(mysql_error());
  888. $this->isPublished = 1;
  889. return true;
  890. }else{
  891. return false;
  892. }
  893. }
  894. // unpublish the quiz
  895. function unpublish($memberID){
  896. require('quizrooDB.php');
  897. require('variables.php');
  898. // check if the quiz is already published
  899. if($this->isPublished() && $this->isOwner($memberID)){
  900. // check if it's a draft or a published quiz
  901. if($this->isPublished == 1){
  902. // set the publish flag to 2
  903. $query = sprintf("UPDATE q_quizzes SET isPublished = 2 WHERE quiz_id = %d", $this->quiz_id);
  904. $this->isPublished = 2;
  905. }else{
  906. // set back to 0
  907. $query = sprintf("UPDATE q_quizzes SET isPublished = 0 WHERE quiz_id = %d", $this->quiz_id);
  908. $this->isPublished = 0;
  909. }
  910. mysql_query($query, $quizroo) or die(mysql_error());
  911. return true;
  912. }else{
  913. return false;
  914. }
  915. }
  916. // archive the quiz
  917. function archive($memberID){
  918. require('quizrooDB.php');
  919. require('variables.php');
  920. // check if the quiz is already published
  921. if($this->isOwner($memberID)){
  922. // set the publish flag to 0
  923. $query = sprintf("UPDATE q_quizzes SET isPublished = 3 WHERE quiz_id = %d", $this->quiz_id);
  924. mysql_query($query, $quizroo) or die(mysql_error());
  925. $this->isPublished = 3;
  926. return true;
  927. }else{
  928. return false;
  929. }
  930. }
  931. // get the rating value by a member
  932. function getRating($member_id){
  933. // find out the rating of this quiz
  934. require('quizrooDB.php');
  935. $query = sprintf("SELECT rating FROM q_store_rating WHERE fk_member_id = %s AND fk_quiz_id = %d", $member_id, $this->quiz_id);
  936. $getQuery = mysql_query($query, $quizroo) or die(mysql_error());
  937. $row_getQuery = mysql_fetch_assoc($getQuery);
  938. $totalRows_getQuery = mysql_num_rows($getQuery);
  939. if($totalRows_getQuery != 0){
  940. return $row_getQuery['rating'];
  941. }else{
  942. return 0;
  943. }
  944. }
  945. // get the list of results belonging to this quiz
  946. function getResultsMulti($type = NULL){
  947. require('quizrooDB.php');
  948. $query = sprintf("SELECT result_id FROM q_results_multi WHERE fk_quiz_id = %d", $this->quiz_id);
  949. $getQuery = mysql_query($query, $quizroo) or die(mysql_error());
  950. $row_getQuery = mysql_fetch_assoc($getQuery);
  951. $totalRows_getQuery = mysql_num_rows($getQuery);
  952. if($type == "count"){
  953. // return the count
  954. return $totalRows_getQuery;
  955. }else{
  956. // return the list
  957. if($totalRows_getQuery != 0){
  958. $results = "";
  959. do{
  960. $results .= $row_getQuery['result_id'].",";
  961. }while($row_getQuery = mysql_fetch_assoc($getQuery));
  962. return substr($results, 0, -1);
  963. }else{
  964. return false;
  965. }
  966. }
  967. }
  968. // get the list of results belonging to this quiz
  969. function getResultsTest($type = NULL){
  970. require('quizrooDB.php');
  971. $query = sprintf("SELECT result_id FROM q_results_test WHERE fk_quiz_id = %d", $this->quiz_id);
  972. $getQuery = mysql_query($query, $quizroo) or die(mysql_error());
  973. $row_getQuery = mysql_fetch_assoc($getQuery);
  974. $totalRows_getQuery = mysql_num_rows($getQuery);
  975. if($type == "count"){
  976. // return the count
  977. return $totalRows_getQuery;
  978. }else{
  979. // return the list
  980. if($totalRows_getQuery != 0){
  981. $results = "";
  982. do{
  983. $results .= $row_getQuery['result_id'].",";
  984. }while($row_getQuery = mysql_fetch_assoc($getQuery));
  985. return substr($results, 0, -1);
  986. }else{
  987. return false;
  988. }
  989. }
  990. }
  991. // get the list of results belonging to this quiz
  992. function getResults_NOTUSED($type = NULL){
  993. require('quizrooDB.php');
  994. $query = sprintf("SELECT result_id FROM q_results WHERE fk_quiz_id = %d", $this->quiz_id);
  995. $getQuery = mysql_query($query, $quizroo) or die(mysql_error());
  996. $row_getQuery = mysql_fetch_assoc($getQuery);
  997. $totalRows_getQuery = mysql_num_rows($getQuery);
  998. if($type == "count"){
  999. // return the count
  1000. return $totalRows_getQuery;
  1001. }else{
  1002. // return the list
  1003. if($totalRows_getQuery != 0){
  1004. $results = "";
  1005. do{
  1006. $results .= $row_getQuery['result_id'].",";
  1007. }while($row_getQuery = mysql_fetch_assoc($getQuery));
  1008. return substr($results, 0, -1);
  1009. }else{
  1010. return false;
  1011. }
  1012. }
  1013. }
  1014. // get the list of questions belonging to this quiz
  1015. function getQuestions($type = NULL){
  1016. require('quizrooDB.php');
  1017. $query = sprintf("SELECT question_id FROM q_questions WHERE fk_quiz_id = %d", $this->quiz_id);
  1018. $getQuery = mysql_query($query, $quizroo) or die(mysql_error());
  1019. $row_getQuery = mysql_fetch_assoc($getQuery);
  1020. $totalRows_getQuery = mysql_num_rows($getQuery);
  1021. if($type == "count"){
  1022. // return the count
  1023. return $totalRows_getQuery;
  1024. }else{
  1025. // return the list
  1026. if($totalRows_getQuery != 0){
  1027. $results = "";
  1028. do{
  1029. $results .= $row_getQuery['question_id'].",";
  1030. }while($row_getQuery = mysql_fetch_assoc($getQuery));
  1031. return substr($results, 0, -1);
  1032. }else{
  1033. return false;
  1034. }
  1035. }
  1036. }
  1037. // get the list of options belonging to a question
  1038. function getOptions_NOTUSED($question_id, $type = NULL){
  1039. require('quizrooDB.php');
  1040. $query = sprintf("SELECT option_id FROM q_options WHERE fk_question_id = %d", $question_id);
  1041. $getQuery = mysql_query($query, $quizroo) or die(mysql_error());
  1042. $row_getQuery = mysql_fetch_assoc($getQuery);
  1043. $totalRows_getQuery = mysql_num_rows($getQuery);
  1044. if($type == "count"){
  1045. // return the count
  1046. return $totalRows_getQuery;
  1047. }else{
  1048. if($totalRows_getQuery != 0){
  1049. $results = "";
  1050. do{
  1051. $results .= $row_getQuery['option_id'].",";
  1052. }while($row_getQuery = mysql_fetch_assoc($getQuery));
  1053. return substr($results, 0, -1);
  1054. }else{
  1055. return false;
  1056. }
  1057. }
  1058. }
  1059. // get the list of options belonging to a question
  1060. function getOptionsTest($question_id, $type = NULL){
  1061. require('quizrooDB.php');
  1062. $query = sprintf("SELECT option_id FROM q_options_test WHERE fk_question_id = %d", $question_id);
  1063. $getQuery = mysql_query($query, $quizroo) or die(mysql_error());
  1064. $row_getQuery = mysql_fetch_assoc($getQuery);
  1065. $totalRows_getQuery = mysql_num_rows($getQuery);
  1066. if($type == "count"){
  1067. // return the count
  1068. return $totalRows_getQuery;
  1069. }else{
  1070. if($totalRows_getQuery != 0){
  1071. $results = "";
  1072. do{
  1073. $results .= $row_getQuery['option_id'].",";
  1074. }while($row_getQuery = mysql_fetch_assoc($getQuery));
  1075. return substr($results, 0, -1);
  1076. }else{
  1077. return false;
  1078. }
  1079. }
  1080. }
  1081. // get the list of options belonging to a question
  1082. function getOptionsMulti($question_id, $type = NULL){
  1083. require('quizrooDB.php');
  1084. $query = sprintf("SELECT option_id FROM q_options_multi WHERE fk_question_id = %d", $question_id);
  1085. $getQuery = mysql_query($query, $quizroo) or die(mysql_error());
  1086. $row_getQuery = mysql_fetch_assoc($getQuery);
  1087. $totalRows_getQuery = mysql_num_rows($getQuery);
  1088. if($type == "count"){
  1089. // return the count
  1090. return $totalRows_getQuery;
  1091. }else{
  1092. if($totalRows_getQuery != 0){
  1093. $results = "";
  1094. do{
  1095. $results .= $row_getQuery['option_id'].",";
  1096. }while($row_getQuery = mysql_fetch_assoc($getQuery));
  1097. return substr($results, 0, -1);
  1098. }else{
  1099. return false;
  1100. }
  1101. }
  1102. }
  1103. // get the list of attempts for this quiz
  1104. function getAttempts($unique = false){
  1105. require('quizrooDB.php');
  1106. if($unique){
  1107. $query = sprintf("SELECT COUNT(*) as count FROM (SELECT store_id FROM q_store_result WHERE fk_quiz_id = %s GROUP BY fk_member_id) t", $this->quiz_id);
  1108. }else{
  1109. $query = sprintf("SELECT COUNT(*) as count FROM (SELECT store_id FROM q_store_result WHERE fk_quiz_id = %s) t", $this->quiz_id);
  1110. }
  1111. $getQuery = mysql_query($query, $quizroo) or die(mysql_error());
  1112. $row_getQuery = mysql_fetch_assoc($getQuery);
  1113. $totalRows_getQuery = mysql_num_rows($getQuery);
  1114. if($totalRows_getQuery == 0){
  1115. return 0;
  1116. }else{
  1117. return $row_getQuery['count'];
  1118. }
  1119. }
  1120. // award points based on like(1), dislike(-1) or neutral(0)
  1121. function awardPoints($type, $member_id){
  1122. require('variables.php');
  1123. if($this->isPublished()){ // check if quiz is published
  1124. require('quizrooDB.php');
  1125. // store the current level of the quiz creator
  1126. $creator_old_level = $this->creator('level');
  1127. $creator_old_rank = $this->creator('rank');
  1128. switch($type){
  1129. case -2: // penalty deduction for 'dislike' rating
  1130. // check if taker has already disliked
  1131. if($this->getRating($member_id) != -1 && $GAME_ALLOW_DISLIKE){ // also check if dislikes are allowed by the system
  1132. // deduct the quiz score
  1133. if($this->quiz_score > 0){ // check if quiz score is more than 0
  1134. // deduct the quiz score and increment the dislike count
  1135. $query = sprintf("UPDATE q_quizzes SET quiz_score = quiz_score - %d, dislikes = dislikes + 1 WHERE quiz_id = %d", $GAME_BASE_POINT * 2, $this->quiz_id);
  1136. mysql_query($query, $quizroo) or die(mysql_error());
  1137. // update the creator's points
  1138. $query = sprintf("UPDATE s_members SET quizcreator_score = quizcreator_score - %d, quizcreator_score_today = quizcreator_score_today - %d WHERE member_id = %s", $GAME_BASE_POINT * 2, $GAME_BASE_POINT * 2, $this->fk_member_id);
  1139. mysql_query($query, $quizroo) or die(mysql_error());
  1140. }
  1141. // log the id of the awarder
  1142. if($this->getRating($member_id) != 0){ // check if member has rated before
  1143. // do an update if member has rated this quiz before
  1144. $query = sprintf("UPDATE q_store_rating SET rating = %d WHERE fk_quiz_id = %d AND fk_member_id = %s", -1, $this->quiz_id, $member_id);
  1145. mysql_query($query, $quizroo) or die(mysql_error());
  1146. }else{
  1147. // do an insert if member is rating this quiz for the first time
  1148. $query = sprintf("INSERT INTO q_store_rating(fk_member_id, fk_quiz_id, rating) VALUES(%d, %d, %d)", $member_id, $this->quiz_id, -1);
  1149. mysql_query($query, $quizroo) or die(mysql_error());
  1150. }
  1151. }
  1152. break; // end case -2
  1153. case -1: // minus the point given during the 'like'
  1154. // we make sure quiz was already liked
  1155. if($this->getRating($member_id) == 1){
  1156. // deduct the quiz score
  1157. $query = sprintf("UPDATE q_quizzes SET quiz_score = quiz_score - %d, likes = likes - 1 WHERE quiz_id = %d", $GAME_BASE_POINT, $this->quiz_id);
  1158. mysql_query($query, $quizroo) or die(mysql_error());
  1159. // update the creator's points
  1160. $query = sprintf("UPDATE s_members SET quizcreator_score = quizcreator_score - %d, quizcreator_score_today = quizcreator_score_today - %d WHERE member_id = %s", $GAME_BASE_POINT, $GAME_BASE_POINT, $this->fk_member_id);
  1161. mysql_query($query, $quizroo) or die(mysql_error());
  1162. // update member has rating of this quiz
  1163. $query = sprintf("DELETE FROM q_store_rating WHERE fk_quiz_id = %d AND fk_member_id = %s", $this->quiz_id, $member_id);
  1164. mysql_query($query, $quizroo) or die(mysql_error());
  1165. }
  1166. break; // end case -1
  1167. case 0: // award the base points or bonus award for 'like' rating
  1168. case 1: // bonus award for 'like' rating
  1169. default:// default case for no type specified
  1170. // check if taker has already liked
  1171. if($this->getRating($member_id) != 1){
  1172. // precheck the level table to see if there's a levelup
  1173. $queryCheck = sprintf("SELECT id FROM `g_levels` WHERE points <= (SELECT `quiztaker_score`+`quizcreator_score` FROM s_members WHERE member_id = %s)+%s ORDER BY points DESC LIMIT 0, 1", $this->fk_member_id, $GAME_BASE_POINT);
  1174. $getCheck = mysql_query($queryCheck, $quizroo) or die(mysql_error());
  1175. $row_getCheck = mysql_fetch_assoc($getCheck);
  1176. $creator_new_level = $row_getCheck['id'];
  1177. mysql_free_result($getCheck);
  1178. // precheck the rank table to see if there's a leveluo
  1179. $queryCheck = sprintf("SELECT fk_id FROM `g_ranks` WHERE `min` <= %d ORDER BY `min` DESC LIMIT 0, 1", $creator_new_level);
  1180. $getCheck = mysql_query($queryCheck, $quizroo) or die(mysql_error());
  1181. $row_getCheck = mysql_fetch_assoc($getCheck);
  1182. $creator_new_rank = $row_getCheck['fk_id'];
  1183. mysql_free_result($getCheck);
  1184. if($creator_new_level > $creator_old_level){ // a levelup has occurred, update the achievement log
  1185. $queryUpdate = sprintf("INSERT INTO g_achievements_log(fk_member_id, fk_achievement_id) VALUES(%d, %d)", $this->creator('member_id'), $creator_new_level);
  1186. mysql_query($queryUpdate, $quizroo) or die(mysql_error());
  1187. if($creator_new_rank > $creator_old_rank){ // a rankup also occurred, update the achievement log
  1188. $queryUpdate = sprintf("INSERT INTO g_achievements_log(fk_member_id, fk_achievement_id) VALUES(%d, %d)", $this->creator('member_id'), $creator_new_level);
  1189. mysql_query($queryUpdate, $quizroo) or die(mysql_error());
  1190. // update the creator's points and increment the level and rank
  1191. $query = sprintf("UPDATE s_members SET quizcreator_score = quizcreator_score + %d, quizcreator_score_today = quizcreator_score_today + %d, level = %d, rank = %d WHERE member_id = %s", $GAME_BASE_POINT, $GAME_BASE_POINT, $creator_new_level, $creator_new_rank, $this->fk_member_id);
  1192. mysql_query($query, $quizroo) or die(mysql_error());
  1193. }else{
  1194. // update the creator's points and increment the level
  1195. $query = sprintf("UPDATE s_members SET quizcreator_score = quizcreator_score + %d, quizcreator_score_today = quizcreator_score_today + %d, level = %d WHERE member_id = %s", $GAME_BASE_POINT, $GAME_BASE_POINT, $creator_new_level, $this->fk_member_id);
  1196. mysql_query($query, $quizroo) or die(mysql_error());
  1197. }
  1198. }else{
  1199. // no levelup, just update the creator's points
  1200. $query = sprintf("UPDATE s_members SET quizcreator_score = quizcreator_score + %d, quizcreator_score_today = quizcreator_score_today + %d WHERE member_id = %s", $GAME_BASE_POINT, $GAME_BASE_POINT, $this->fk_member_id);
  1201. mysql_query($query, $quizroo) or die(mysql_error());
  1202. }
  1203. // update the quiz score
  1204. if($type == 1){ // also increment the like count
  1205. $query = sprintf("UPDATE q_quizzes SET quiz_score = quiz_score + %d, likes = likes + 1 WHERE quiz_id = %d", $GAME_BASE_POINT, $this->quiz_id);
  1206. mysql_query($query, $quizroo) or die(mysql_error());
  1207. }else{ // just update the score
  1208. $query = sprintf("UPDATE q_quizzes SET quiz_score = quiz_score + %d WHERE quiz_id = %d", $GAME_BASE_POINT, $this->quiz_id);
  1209. mysql_query($query, $quizroo) or die(mysql_error());
  1210. }
  1211. if($type == 1){
  1212. // log the id of the awarder
  1213. if($this->getRating($member_id) != 0){ // check if member has rated before
  1214. // do an update if member has rated this quiz before
  1215. $query = sprintf("UPDATE q_store_rating SET rating = %d WHERE fk_quiz_id = %d AND fk_member_id = %s", 1, $this->quiz_id, $member_id);
  1216. mysql_query($query, $quizroo) or die(mysql_error());
  1217. }else{