hotspot_admin.inc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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]=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. } else {
  153. $msgErr=get_lang('MinHotspot');
  154. }
  155. }
  156. if($moreAnswers)
  157. {
  158. if ($nbrAnswers < 12)
  159. {
  160. $nbrAnswers++;
  161. // Add a new answer
  162. $_SESSION['tmp_answers']['answer'][]='';
  163. $_SESSION['tmp_answers']['comment'][]='';
  164. $_SESSION['tmp_answers']['weighting'][]='1';
  165. $_SESSION['tmp_answers']['hotspot_coordinates'][]='0;0|0|0';
  166. $_SESSION['tmp_answers']['hotspot_type'][]='square';
  167. }
  168. else
  169. {
  170. $msgErr=get_lang('MaxHotspot');
  171. }
  172. }
  173. if($debug>0){echo str_repeat('&nbsp;',2).'$usedInSeveralExercises is untrue'."<br />\n";}
  174. if($debug>0){echo str_repeat('&nbsp;',4).'$answerType is HOT_SPOT'."<br />\n";}
  175. $hotspot_colors = array("", // $i starts from 1 on next loop (ugly fix)
  176. "#4271B5",
  177. "#FE8E16",
  178. "#3B3B3B",
  179. "#BCD631",
  180. "#D63173",
  181. "#D7D7D7",
  182. "#90AFDD",
  183. "#AF8640",
  184. "#4F9242",
  185. "#F4EB24",
  186. "#ED2024",
  187. "#45C7F0",
  188. "#F7BDE2");
  189. ?>
  190. <h3>
  191. <?php echo get_lang('Question').": ".$questionName.' <img src="../img/info3.gif" title="'.strip_tags(get_lang('HotspotChoose')).'" alt="'.strip_tags(get_lang('HotspotChoose')).'" />'; ?>
  192. </h3>
  193. <?php
  194. if(!empty($msgErr))
  195. {
  196. Display::display_normal_message($msgErr); //main API
  197. }
  198. ?>
  199. <form method="post" action="<?php echo api_get_self(); ?>?hotspotadmin=<?php echo $modifyAnswers; ?>" name="frm_exercise">
  200. <table border="0" cellpadding="0" cellspacing="2" width="100%">
  201. <tr>
  202. <td colspan="2" valign="bottom">
  203. <?php
  204. $navigator_info = api_get_navigator();
  205. //ie6 fix
  206. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  207. ?>
  208. <input type="submit" class="minus" name="lessAnswers" value="<?php echo get_lang('LessHotspots'); ?>" >
  209. <input type="submit" class="plus" name="moreAnswers" value="<?php echo get_lang('MoreHotspots'); ?>" />
  210. <input type="submit" class="cancel" name="cancelAnswers" value="<?php echo get_lang('Cancel'); ?>" onclick="javascript:if(!confirm('<?php echo addslashes(api_htmlentities(get_lang('ConfirmYourChoice'))); ?>')) return false;" >
  211. <input type="submit" class="save" name="submitAnswers" value="<?php echo get_lang('Ok'); ?>" />
  212. <?php
  213. } else {
  214. ?>
  215. <button type="submit" class="minus" name="lessAnswers" value="<?php echo get_lang('LessHotspots'); ?>" ><?php echo get_lang('LessHotspots'); ?></button>
  216. <button type="submit" class="plus" name="moreAnswers" value="<?php echo get_lang('MoreHotspots'); ?>" /><?php echo get_lang('MoreHotspots'); ?></button>
  217. <button type="submit" class="cancel" name="cancelAnswers" value="<?php echo get_lang('Cancel'); ?>" onclick="javascript:if(!confirm('<?php echo addslashes(api_htmlentities(get_lang('ConfirmYourChoice'))); ?>')) return false;" ><?php echo get_lang('Cancel'); ?></button>
  218. <button type="submit" class="save" name="submitAnswers" value="<?php echo get_lang('Ok'); ?>" /><?php echo get_lang('AddQuestionToExercise'); ?></button>
  219. <?php
  220. }
  221. ?>
  222. </td>
  223. </tr>
  224. <tr>
  225. <td valign="top" style="border:1px solid #4271b5;border-top:none;border-bottom:none;border-right:none;" >
  226. <input type="hidden" name="formSent" value="1" />
  227. <input type="hidden" name="nbrAnswers" value="<?php echo $nbrAnswers; ?>" />
  228. <table class="data_table">
  229. <!--
  230. <tr>
  231. <td colspan="5"><?php echo get_lang('AnswerHotspot'); ?> :</td>
  232. </tr>
  233. -->
  234. <tr>
  235. <th width="5">&nbsp;<?php /* echo get_lang('Hotspot'); */ ?></th>
  236. <th ><?php echo get_lang('HotspotDescription'); ?>*</th>
  237. <th ><?php echo get_lang('Comment'); ?></th>
  238. <th><?php echo get_lang('QuestionWeighting'); ?>*</th>
  239. </tr>
  240. <?php
  241. for($i=1;$i <= $nbrAnswers;$i++) {
  242. ?>
  243. <tr>
  244. <td valign="top"><div style="height: 15px; width: 15px; background-color: <?php echo $hotspot_colors[$i]; ?>"> </div></td>
  245. <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>
  246. <?php
  247. require_once(api_get_path(LIBRARY_PATH) . "/fckeditor/fckeditor.php");
  248. $oFCKeditor = new FCKeditor("comment[$i]") ;
  249. $content = $comment[$i];
  250. $oFCKeditor->ToolbarSet = 'TestProposedAnswer';
  251. $oFCKeditor->Config['ToolbarStartExpanded'] = 'false';
  252. $oFCKeditor->Width = '100%';
  253. $oFCKeditor->Height = '100';
  254. $oFCKeditor->Value = $content;
  255. $return = $oFCKeditor->CreateHtml();
  256. /*<td align="left"><textarea wrap="virtual" rows="1" cols="25" name="comment[<?php echo $i; ?>]" style="width: 100%"><?php echo api_htmlentities($comment[$i], ENT_QUOTES, api_get_system_encoding()); ?></textarea></td>*/
  257. ?>
  258. <td align="left"><?php echo $return; ?></td>
  259. <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); ?>" />
  260. <input type="hidden" name="hotspot_coordinates[<?php echo $i; ?>]" value="<?php echo (empty($hotspot_coordinates[$i]) ? '0;0|0|0' : $hotspot_coordinates[$i]); ?>" />
  261. <input type="hidden" name="hotspot_type[<?php echo $i; ?>]" value="<?php echo (empty($hotspot_type[$i]) ? 'square' : $hotspot_type[$i]); ?>" /></td>
  262. </tr>
  263. <?php
  264. }
  265. ?>
  266. </table>
  267. </td>
  268. </tr>
  269. <tr>
  270. <td colspan="2" valign="top" style="border:1px solid #ccc;border-top:none">
  271. <script type="text/javascript">
  272. <!--
  273. // Version check based upon the values entered above in "Globals"
  274. var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
  275. // Check to see if the version meets the requirements for playback
  276. if (hasReqestedVersion) { // if we've detected an acceptable version
  277. var oeTags = '<object type="application/x-shockwave-flash" data="../plugin/hotspot/hotspot_admin.swf?modifyAnswers=<?php echo $modifyAnswers ?>" width="600" height="450">'
  278. + '<param name="movie" value="../plugin/hotspot/hotspot_admin.swf?modifyAnswers=<?php echo $modifyAnswers ?>" />'
  279. + '<param name="test" value="OOoowww fo shooww" />'
  280. + '</object>';
  281. document.write(oeTags); // embed the Flash Content SWF when all tests are passed
  282. } else { // flash is too old or we can't detect the plugin
  283. var alternateContent = 'Error<br \/>'
  284. + 'This content requires the Macromedia Flash Player.<br \/>'
  285. + '<a href=http://www.macromedia.com/go/getflash/>Get Flash<\/a>';
  286. document.write(alternateContent); // insert non-flash content
  287. }
  288. // -->
  289. </script>
  290. </td>
  291. </tr>
  292. </table>
  293. </form>
  294. <?php
  295. if($debug>0){echo str_repeat('&nbsp;',0).'$modifyAnswers was set - end'."<br />\n";}
  296. }
  297. ?>