hotspot_admin.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php //$id:$
  2. /* For licensing terms, see /dokeos_license.txt */
  3. //error_log(__FILE__);
  4. /**
  5. * This script allows to manage answers. It is included from the script admin.php
  6. * @package dokeos.exercise
  7. * @author Toon Keppens
  8. * @version $Id: admin.php 10680 2007-01-11 21:26:23Z pcool $
  9. */
  10. require_once(api_get_path(LIBRARY_PATH).'text.lib.php');
  11. // ALLOWED_TO_INCLUDE is defined in admin.php
  12. if(!defined('ALLOWED_TO_INCLUDE')) {
  13. exit();
  14. }
  15. $modifyAnswers = $_GET['hotspotadmin'];
  16. if(!is_object($objQuestion)) {
  17. $objQuestion = Question :: read($modifyAnswers);
  18. }
  19. $questionName=$objQuestion->selectTitle();
  20. $answerType=$objQuestion->selectType();
  21. $pictureName=$objQuestion->selectPicture();
  22. $debug = 0; // debug variable to get where we are
  23. $okPicture=empty($pictureName)?false:true;
  24. // if we come from the warning box "this question is used in serveral exercises"
  25. if($modifyIn)
  26. {
  27. if($debug>0){echo '$modifyIn was set'."<br />\n";}
  28. // if the user has chosed to modify the question only in the current exercise
  29. if($modifyIn == 'thisExercise')
  30. {
  31. // duplicates the question
  32. $questionId=$objQuestion->duplicate();
  33. // deletes the old question
  34. $objQuestion->delete($exerciseId);
  35. // removes the old question ID from the question list of the Exercise object
  36. $objExercise->removeFromList($modifyAnswers);
  37. // adds the new question ID into the question list of the Exercise object
  38. $objExercise->addToList($questionId);
  39. // construction of the duplicated Question
  40. $objQuestion = Question :: read($questionId);
  41. // adds the exercise ID into the exercise list of the Question object
  42. $objQuestion->addToList($exerciseId);
  43. // copies answers from $modifyAnswers to $questionId
  44. $objAnswer->duplicate($questionId);
  45. // construction of the duplicated Answers
  46. $objAnswer=new Answer($questionId);
  47. }
  48. $color=unserialize($color);
  49. $reponse=unserialize($reponse);
  50. $comment=unserialize($comment);
  51. $weighting=unserialize($weighting);
  52. $hotspot_coordinates=unserialize($hotspot_coordinates);
  53. $hotspot_type=unserialize($hotspot_type);
  54. unset($buttonBack);
  55. }
  56. // the answer form has been submitted
  57. if($submitAnswers || $buttonBack)
  58. {
  59. if($debug>0){echo '$submitAnswers or $buttonBack was set'."<br />\n";}
  60. $questionWeighting=$nbrGoodAnswers=0;
  61. for($i=1;$i <= $nbrAnswers;$i++)
  62. {
  63. if($debug>0){echo str_repeat('&nbsp;',4).'$answerType is HOT_SPOT'."<br />\n";}
  64. $reponse[$i]=trim($reponse[$i]);
  65. $comment[$i]=trim($comment[$i]);
  66. $weighting[$i]=$weighting[$i]; // it can be float
  67. // checks if field is empty
  68. if(empty($reponse[$i]) && $reponse[$i] != '0') {
  69. $msgErr=get_lang('HotspotGiveAnswers');
  70. // clears answers already recorded into the Answer object
  71. $objAnswer->cancel();
  72. break;
  73. }
  74. if($weighting[$i] <= 0) {
  75. $msgErr=get_lang('HotspotWeightingError');
  76. // clears answers already recorded into the Answer object
  77. $objAnswer->cancel();
  78. break;
  79. }
  80. if($hotspot_coordinates[$i] == '0;0|0|0' || empty($hotspot_coordinates[$i])) {
  81. $msgErr=get_lang('HotspotNotDrawn');
  82. // clears answers already recorded into the Answer object
  83. $objAnswer->cancel();
  84. break;
  85. }
  86. } // end for()
  87. if(empty($msgErr)) {
  88. for($i=1;$i <= $nbrAnswers;$i++) {
  89. if($debug>0){echo str_repeat('&nbsp;',4).'$answerType is HOT_SPOT'."<br />\n";}
  90. $reponse[$i]=trim($reponse[$i]);
  91. $comment[$i]=addslashes(trim($comment[$i]));
  92. $weighting[$i]=($weighting[$i]); //it can be float
  93. if($weighting[$i]) {
  94. $questionWeighting+=$weighting[$i];
  95. }
  96. // creates answer
  97. $objAnswer->createAnswer($reponse[$i], '',$comment[$i],$weighting[$i],$i,$hotspot_coordinates[$i],$hotspot_type[$i]);
  98. } // end for()
  99. // saves the answers into the data base
  100. $objAnswer->save();
  101. // sets the total weighting of the question
  102. $objQuestion->updateWeighting($questionWeighting);
  103. $objQuestion->save($exerciseId);
  104. $editQuestion=$questionId;
  105. unset($modifyAnswers);
  106. echo '<script type="text/javascript">window.location.href="admin.php"</script>';
  107. }
  108. if($debug>0){echo '$modifyIn was set - end'."<br />\n";}
  109. }
  110. if($modifyAnswers)
  111. {
  112. if($debug>0){echo str_repeat('&nbsp;',0).'$modifyAnswers is set'."<br />\n";}
  113. // construction of the Answer object
  114. $objAnswer=new Answer($objQuestion -> id);
  115. api_session_register('objAnswer');
  116. if($debug>0){echo str_repeat('&nbsp;',2).'$answerType is HOT_SPOT'."<br />\n";}
  117. $TBL_ANSWERS = Database::get_course_table(TABLE_QUIZ_ANSWER);
  118. if(!$nbrAnswers)
  119. {
  120. $nbrAnswers=$objAnswer->selectNbrAnswers();
  121. $reponse=Array();
  122. $comment=Array();
  123. $weighting=Array();
  124. $hotspot_coordinates=Array();
  125. $hotspot_type=array();
  126. for($i=1;$i <= $nbrAnswers;$i++)
  127. {
  128. $reponse[$i]=$objAnswer->selectAnswer($i);
  129. $comment[$i]=$objAnswer->selectComment($i);
  130. $weighting[$i]=$objAnswer->selectWeighting($i);
  131. $hotspot_coordinates[$i]=$objAnswer->selectHotspotCoordinates($i);
  132. $hotspot_type[$i]=$objAnswer->selectHotspotType($i);
  133. }
  134. }
  135. $_SESSION['tmp_answers'] = array();
  136. $_SESSION['tmp_answers']['answer'] = $reponse;
  137. $_SESSION['tmp_answers']['comment'] = $comment;
  138. $_SESSION['tmp_answers']['weighting'] = $weighting;
  139. $_SESSION['tmp_answers']['hotspot_coordinates'] = $hotspot_coordinates;
  140. $_SESSION['tmp_answers']['hotspot_type'] = $hotspot_type;
  141. if($lessAnswers)
  142. {
  143. // At least 1 answer
  144. if ($nbrAnswers > 1) {
  145. $nbrAnswers--;
  146. // Remove the last answer
  147. $tmp = array_pop($_SESSION['tmp_answers']['answer']);
  148. $tmp = array_pop($_SESSION['tmp_answers']['comment']);
  149. $tmp = array_pop($_SESSION['tmp_answers']['weighting']);
  150. $tmp = array_pop($_SESSION['tmp_answers']['hotspot_coordinates']);
  151. $tmp = array_pop($_SESSION['tmp_answers']['hotspot_type']);
  152. }
  153. else
  154. {
  155. $msgErr=get_lang('MinHotspot');
  156. }
  157. }
  158. if($moreAnswers)
  159. {
  160. if ($nbrAnswers < 12)
  161. {
  162. $nbrAnswers++;
  163. // Add a new answer
  164. $_SESSION['tmp_answers']['answer'][]='';
  165. $_SESSION['tmp_answers']['comment'][]='';
  166. $_SESSION['tmp_answers']['weighting'][]='1';
  167. $_SESSION['tmp_answers']['hotspot_coordinates'][]='0;0|0|0';
  168. $_SESSION['tmp_answers']['hotspot_type'][]='square';
  169. }
  170. else
  171. {
  172. $msgErr=get_lang('MaxHotspot');
  173. }
  174. }
  175. if($debug>0){echo str_repeat('&nbsp;',2).'$usedInSeveralExercises is untrue'."<br />\n";}
  176. if($debug>0){echo str_repeat('&nbsp;',4).'$answerType is HOT_SPOT'."<br />\n";}
  177. $hotspot_colors = array("", // $i starts from 1 on next loop (ugly fix)
  178. "#4271B5",
  179. "#FE8E16",
  180. "#3B3B3B",
  181. "#BCD631",
  182. "#D63173",
  183. "#D7D7D7",
  184. "#90AFDD",
  185. "#AF8640",
  186. "#4F9242",
  187. "#F4EB24",
  188. "#ED2024",
  189. "#45C7F0",
  190. "#F7BDE2");
  191. ?>
  192. <h3>
  193. <?php echo $langQuestion.": ".$questionName.' <img src="../img/info3.gif" title="'.strip_tags(get_lang('HotspotChoose')).'" alt="'.strip_tags(get_lang('HotspotChoose')).'" />'; ?>
  194. </h3>
  195. <?php
  196. if(!empty($msgErr))
  197. {
  198. Display::display_normal_message($msgErr); //main API
  199. }
  200. ?>
  201. <form method="post" action="<?php echo api_get_self(); ?>?hotspotadmin=<?php echo $modifyAnswers; ?>" name="frm_exercise">
  202. <table border="0" cellpadding="0" cellspacing="2" width="100%">
  203. <tr>
  204. <td colspan="2" valign="bottom">
  205. <button type="submit" class="minus" name="lessAnswers" value="<?php echo get_lang('LessHotspots'); ?>" ><?php echo get_lang('LessHotspots'); ?></button>
  206. <button type="submit" class="plus" name="moreAnswers" value="<?php echo get_lang('MoreHotspots'); ?>" /><?php echo get_lang('langMoreHotspotsImage'); ?></button>
  207. <button type="submit" class="cancel" name="cancelAnswers" value="<?php echo get_lang('Cancel'); ?>" onclick="javascript:if(!confirm('<?php echo addslashes(htmlentities(get_lang('ConfirmYourChoice'))); ?>')) return false;" ><?php echo get_lang('Cancel'); ?></button>
  208. <button type="submit" class="save" name="submitAnswers" value="<?php echo get_lang('Ok'); ?>" /><?php echo get_lang('AddQuestionToExercise'); ?></button>
  209. </td>
  210. </tr>
  211. <tr>
  212. <td valign="top" style="border:1px solid #4271b5;border-top:none;border-bottom:none;border-right:none;" >
  213. <input type="hidden" name="formSent" value="1" />
  214. <input type="hidden" name="nbrAnswers" value="<?php echo $nbrAnswers; ?>" />
  215. <table class="data_table">
  216. <!--
  217. <tr>
  218. <td colspan="5"><?php echo get_lang('AnswerHotspot'); ?> :</td>
  219. </tr>
  220. -->
  221. <tr>
  222. <th width="5">&nbsp;<?php /* echo get_lang('Hotspot'); */ ?></th>
  223. <th ><?php echo get_lang('HotspotDescription'); ?>*</th>
  224. <th ><?php echo get_lang('Comment'); ?></th>
  225. <th><?php echo get_lang('QuestionWeighting'); ?>*</th>
  226. </tr>
  227. <?php
  228. for($i=1;$i <= $nbrAnswers;$i++) {
  229. ?>
  230. <tr>
  231. <td valign="top"><div style="height: 15px; width: 15px; background-color: <?php echo $hotspot_colors[$i]; ?>"> </div></td>
  232. <td valign="top" align="left"><input type="text" name="reponse[<?php echo $i; ?>]" value="<?php echo api_htmlentities($reponse[$i], ENT_QUOTES, api_get_system_encoding()); ?>" size="45" /></td>
  233. <td align="left"><textarea wrap="virtual" rows="1" cols="25" name="comment[<?php echo $i; ?>]" style="width: 100%"><?php echo stripslashes(api_htmlentities($comment[$i], ENT_QUOTES, api_get_system_encoding())); ?></textarea></td>
  234. <td valign="top"><input type="text" name="weighting[<?php echo $i; ?>]" size="5" value="<?php echo (isset($weighting[$i]) ? float_format($weighting[$i],1) : 10); ?>" />
  235. <input type="hidden" name="hotspot_coordinates[<?php echo $i; ?>]" value="<?php echo (empty($hotspot_coordinates[$i]) ? '0;0|0|0' : $hotspot_coordinates[$i]); ?>" />
  236. <input type="hidden" name="hotspot_type[<?php echo $i; ?>]" value="<?php echo (empty($hotspot_type[$i]) ? 'square' : $hotspot_type[$i]); ?>" /></td>
  237. </tr>
  238. <?php
  239. }
  240. ?>
  241. </table>
  242. </td>
  243. </tr>
  244. <tr>
  245. <td colspan="2" valign="top" style="border:1px solid #ccc;border-top:none">
  246. <script type="text/javascript">
  247. <!--
  248. // Version check based upon the values entered above in "Globals"
  249. var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
  250. // Check to see if the version meets the requirements for playback
  251. if (hasReqestedVersion) { // if we've detected an acceptable version
  252. var oeTags = '<object type="application/x-shockwave-flash" data="../plugin/hotspot/hotspot_admin.swf?modifyAnswers=<?php echo $modifyAnswers ?>" width="720" height="650">'
  253. + '<param name="movie" value="../plugin/hotspot/hotspot_admin.swf?modifyAnswers=<?php echo $modifyAnswers ?>" />'
  254. + '<param name="test" value="OOoowww fo shooww" />'
  255. + '</object>';
  256. document.write(oeTags); // embed the Flash Content SWF when all tests are passed
  257. } else { // flash is too old or we can't detect the plugin
  258. var alternateContent = 'Error<br \/>'
  259. + 'This content requires the Macromedia Flash Player.<br \/>'
  260. + '<a href=http://www.macromedia.com/go/getflash/>Get Flash<\/a>';
  261. document.write(alternateContent); // insert non-flash content
  262. }
  263. // -->
  264. </script>
  265. </td>
  266. </tr>
  267. </table>
  268. </form>
  269. <?php
  270. if($debug>0){echo str_repeat('&nbsp;',0).'$modifyAnswers was set - end'."<br />\n";}
  271. }
  272. ?>