exercise_show_functions.lib.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /* See license terms in /license.txt */
  3. /**
  4. ==============================================================================
  5. * EVENTS LIBRARY
  6. *
  7. * This is the events library for Chamilo.
  8. * Functions of this library are used to record informations when some kind
  9. * of event occur. Each event has his own types of informations then each event
  10. * use its own function.
  11. *
  12. * @package chamilo.library
  13. * @todo convert queries to use Database API
  14. ==============================================================================
  15. */
  16. $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
  17. $TBL_EXERCICES = Database::get_course_table(TABLE_QUIZ_TEST);
  18. $TBL_QUESTIONS = Database::get_course_table(TABLE_QUIZ_QUESTION);
  19. $TBL_REPONSES = Database::get_course_table(TABLE_QUIZ_ANSWER);
  20. $main_user_table = Database :: get_main_table(TABLE_MAIN_USER);
  21. $main_course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  22. $TBL_TRACK_EXERCICES = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  23. $TBL_TRACK_ATTEMPT = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  24. class ExerciseShowFunctions {
  25. /**
  26. * Shows the answer to a fill-in-the-blanks question, as HTML
  27. * @param string Answer text
  28. * @param int Exercise ID
  29. * @param int Question ID
  30. * @return void
  31. */
  32. function display_fill_in_blanks_answer($answer,$id,$questionId)
  33. { global $feedback_type;
  34. ?>
  35. <tr>
  36. <td>
  37. <?php echo nl2br(Security::remove_XSS($answer,COURSEMANAGERLOWSECURITY)); ?>
  38. </td>
  39. <?php
  40. if(!api_is_allowed_to_edit(null,true) && $feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {?>
  41. <td>
  42. <?php
  43. $comm = ExerciseShowFunctions::get_comments($id,$questionId);
  44. ?>
  45. </td>
  46. <?php } ?>
  47. </tr>
  48. <?php
  49. }
  50. /**
  51. * Shows the answer to a free-answer question, as HTML
  52. * @param string Answer text
  53. * @param int Exercise ID
  54. * @param int Question ID
  55. * @return void
  56. */
  57. function display_free_answer($answer,$id,$questionId) {
  58. global $feedback_type;
  59. ?>
  60. <tr>
  61. <td>
  62. <?php if (!empty($answer)) {echo nl2br(Security::remove_XSS($answer,COURSEMANAGERLOWSECURITY));} ?>
  63. </td>
  64. <?php if(!api_is_allowed_to_edit(null,true) && $feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) {?>
  65. <td>
  66. <?php
  67. $comm = ExerciseShowFunctions::get_comments($id,$questionId);
  68. ?>
  69. </td>
  70. <?php }?>
  71. </tr>
  72. <?php
  73. }
  74. /**
  75. * Displays the answer to a hotspot question
  76. *
  77. * @param int $answerId
  78. * @param string $answer
  79. * @param string $studentChoice
  80. * @param string $answerComment
  81. */
  82. function display_hotspot_answer($answerId, $answer, $studentChoice, $answerComment) {
  83. global $feedback_type;
  84. $hotspot_colors = array("", // $i starts from 1 on next loop (ugly fix)
  85. "#4271B5",
  86. "#FE8E16",
  87. "#3B3B3B",
  88. "#BCD631",
  89. "#D63173",
  90. "#D7D7D7",
  91. "#90AFDD",
  92. "#AF8640",
  93. "#4F9242",
  94. "#F4EB24",
  95. "#ED2024",
  96. "#45C7F0",
  97. "#F7BDE2");
  98. ?>
  99. <tr>
  100. <td width="100px" valign="top" align="left">
  101. <div style="width:100%;">
  102. <div style="height:11px; width:11px; background-color:<?php echo $hotspot_colors[$answerId]; ?>; display:inline; float:left; margin-top:3px;"></div>
  103. <div style="float:left; padding-left:5px;">
  104. <?php echo $answerId; ?>
  105. </div>
  106. <div><?php echo '&nbsp;'.$answer ?></div>
  107. </div>
  108. </td>
  109. <td width="50px" style="padding-right:15px" valign="top" align="left">
  110. <?php
  111. $my_choice = ($studentChoice)?get_lang('Correct'):get_lang('Fault'); echo $my_choice; ?>
  112. </td>
  113. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  114. <td valign="top" align="left" >
  115. <?php
  116. $answerComment=api_parse_tex($answerComment);
  117. if($studentChoice) {
  118. echo '<span style="font-weight: bold; color: #008000;">'.nl2br(make_clickable($answerComment)).'</span>';
  119. } else {
  120. echo '<span style="font-weight: bold; color: #FF0000;">'.nl2br(make_clickable($answerComment)).'</span>';
  121. }
  122. ?>
  123. </td>
  124. <?php } else { ?>
  125. <td>&nbsp;</td>
  126. <?php } ?>
  127. </tr>
  128. <?php
  129. }
  130. /**
  131. * Display the answers to a multiple choice question
  132. *
  133. * @param integer Answer type
  134. * @param integer Student choice
  135. * @param string Textual answer
  136. * @param string Comment on answer
  137. * @param string Correct answer comment
  138. * @param integer Exercise ID
  139. * @param integer Question ID
  140. * @param boolean Whether to show the answer comment or not
  141. * @return void
  142. */
  143. function display_unique_or_multiple_answer($answerType, $studentChoice, $answer, $answerComment, $answerCorrect, $id, $questionId, $ans)
  144. {
  145. global $feedback_type;
  146. ?>
  147. <tr>
  148. <td width="5%" align="center">
  149. <img src="../img/<?php echo ($answerType == UNIQUE_ANSWER)?'radio':'checkbox'; echo $studentChoice?'_on':'_off'; ?>.gif"
  150. border="0" alt="" />
  151. </td>
  152. <td width="5%" align="center">
  153. <img src="../img/<?php echo ($answerType == UNIQUE_ANSWER)?'radio':'checkbox'; echo $answerCorrect?'_on':'_off'; ?>.gif"
  154. border="0" alt=" " />
  155. </td>
  156. <td width="40%" style="border-bottom: 1px solid #4171B5;">
  157. <?php
  158. $answer=api_parse_tex($answer);
  159. echo $answer;
  160. ?>
  161. </td>
  162. <?php if ($feedback_type != EXERCISE_FEEDBACK_TYPE_EXAM) { ?>
  163. <td width="20%" style="border-bottom: 1px solid #4171B5;">
  164. <?php
  165. $answerComment=api_parse_tex($answerComment);
  166. if($studentChoice)
  167. {
  168. if(!$answerCorrect)
  169. {
  170. echo '<span style="font-weight: bold; color: #FF0000;">'.nl2br(make_clickable($answerComment)).'</span>';
  171. }
  172. else{
  173. echo '<span style="font-weight: bold; color: #008000;">'.nl2br(make_clickable($answerComment)).'</span>';
  174. }
  175. }
  176. else
  177. {
  178. echo '&nbsp;';
  179. }
  180. ?>
  181. </td>
  182. <?php
  183. if ($ans==1) {
  184. $comm = ExerciseShowFunctions::get_comments($id,$questionId);
  185. }
  186. ?>
  187. <?php } else { ?>
  188. <td>&nbsp;</td>
  189. <?php } ?>
  190. </tr>
  191. <?php
  192. }
  193. /**
  194. * This function gets the comments of an exercise
  195. *
  196. * @param int $id
  197. * @param int $question_id
  198. * @return str the comment
  199. */
  200. function get_comments($id,$question_id)
  201. {
  202. global $TBL_TRACK_ATTEMPT;
  203. $sql = "SELECT teacher_comment FROM ".$TBL_TRACK_ATTEMPT." where exe_id='".Database::escape_string($id)."' and question_id = '".Database::escape_string($question_id)."' ORDER by question_id";
  204. $sqlres = Database::query($sql);
  205. $comm = Database::result($sqlres,0,"teacher_comment");
  206. return $comm;
  207. }
  208. function send_notification($arrques, $arrans, $to) {
  209. global $courseName, $exerciseTitle, $url_email;
  210. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  211. $user_info = UserManager::get_user_info_by_id(api_get_user_id());
  212. if (api_get_course_setting('email_alert_manager_on_new_quiz') != 1 ) {
  213. return '';
  214. }
  215. $mycharset = api_get_system_encoding();
  216. $msg = '<html><head>
  217. <link rel="stylesheet" href="'.api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/default.css" type="text/css">
  218. <meta content="text/html; charset='.$mycharset.'" http-equiv="content-type"></head>';
  219. if(count($arrques)>0) {
  220. $msg .= '<body>
  221. <p>'.get_lang('OpenQuestionsAttempted').' :
  222. </p>
  223. <p>'.get_lang('AttemptDetails').' : <br />
  224. </p>
  225. <table width="730" height="136" border="0" cellpadding="3" cellspacing="3">
  226. <tr>
  227. <td width="229" valign="top"><h2>&nbsp;&nbsp;'.get_lang('CourseName').'</h2></td>
  228. <td width="469" valign="top"><h2>#course#</h2></td>
  229. </tr>
  230. <tr>
  231. <td width="229" valign="top" class="outerframe">&nbsp;&nbsp;'.get_lang('TestAttempted').'</span></td>
  232. <td width="469" valign="top" class="outerframe">#exercise#</td>
  233. </tr>
  234. <tr>
  235. <td valign="top">&nbsp;&nbsp;<span class="style10">'.get_lang('StudentName').'</span></td>
  236. <td valign="top" >#firstName# #lastName#</td>
  237. </tr>
  238. <tr>
  239. <td valign="top" >&nbsp;&nbsp;'.get_lang('StudentEmail').' </td>
  240. <td valign="top"> #mail#</td>
  241. </tr></table>
  242. <p><br />'.get_lang('OpenQuestionsAttemptedAre').' :</p>
  243. <table width="730" height="136" border="0" cellpadding="3" cellspacing="3">';
  244. for($i=0;$i<sizeof($arrques);$i++) {
  245. $msg.='
  246. <tr>
  247. <td width="220" valign="top" bgcolor="#E5EDF8">&nbsp;&nbsp;<span class="style10">'.get_lang('Question').'</span></td>
  248. <td width="473" valign="top" bgcolor="#F3F3F3"><span class="style16"> #questionName#</span></td>
  249. </tr>
  250. <tr>
  251. <td width="220" valign="top" bgcolor="#E5EDF8">&nbsp;&nbsp;<span class="style10">'.get_lang('Answer').' </span></td>
  252. <td valign="top" bgcolor="#F3F3F3"><span class="style16"> #answer#</span></td>
  253. </tr>';
  254. $msg1= str_replace("#exercise#",$exerciseTitle,$msg);
  255. $msg= str_replace("#firstName#",$user_info['firstname'],$msg1);
  256. $msg1= str_replace("#lastName#",$user_info['lastname'],$msg);
  257. $msg= str_replace("#mail#",$user_info['email'],$msg1);
  258. $msg1= str_replace("#questionName#",$arrques[$i],$msg);
  259. $msg= str_replace("#answer#",$arrans[$i],$msg1);
  260. $msg1= str_replace("#i#",$i,$msg);
  261. $msg= str_replace("#course#",$courseName,$msg1);
  262. }
  263. $msg.='</table><br>
  264. <span class="style16">'.get_lang('ClickToCommentAndGiveFeedback').',<br />
  265. <a href="#url#">#url#</a></span></body></html>';
  266. $msg1= str_replace("#url#",$url_email,$msg);
  267. $mail_content = $msg1;
  268. $subject = get_lang('OpenQuestionsAttempted');
  269. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  270. $email_admin = api_get_setting('emailAdministrator');
  271. $result = @api_mail_html('', $to, $subject, $mail_content, $sender_name, $email_admin, array('charset'=>$mycharset));
  272. } else {
  273. $msg .= '<body>
  274. <p>'.get_lang('ExerciseAttempted').' <br />
  275. </p>
  276. <table width="730" height="136" border="0" cellpadding="3" cellspacing="3">
  277. <tr>
  278. <td width="229" valign="top"><h2>&nbsp;&nbsp;'.get_lang('CourseName').'</h2></td>
  279. <td width="469" valign="top"><h2>#course#</h2></td>
  280. </tr>
  281. <tr>
  282. <td width="229" valign="top" class="outerframe">&nbsp;&nbsp;'.get_lang('TestAttempted').'</span></td>
  283. <td width="469" valign="top" class="outerframe">#exercise#</td>
  284. </tr>
  285. <tr>
  286. <td valign="top">&nbsp;&nbsp;<span class="style10">'.get_lang('StudentName').'</span></td>
  287. '.(api_is_western_name_order() ? '<td valign="top" >#firstName# #lastName#</td>' : '<td valign="top" >#lastName# #firstName#</td>').'
  288. </tr>
  289. <tr>
  290. <td valign="top" >&nbsp;&nbsp;'.get_lang('StudentEmail').' </td>
  291. <td valign="top"> #mail#</td>
  292. </tr></table>';
  293. $msg= str_replace("#exercise#",$exerciseTitle,$msg);
  294. $msg= str_replace("#firstName#",$user_info['firstname'],$msg);
  295. $msg= str_replace("#lastName#",$user_info['lastname'],$msg);
  296. $msg= str_replace("#mail#",$user_info['email'],$msg);
  297. $msg= str_replace("#course#",$courseName,$msg);
  298. $msg.='<br />
  299. <span class="style16">'.get_lang('ClickToCommentAndGiveFeedback').',<br />
  300. <a href="#url#">#url#</a></span></body></html>';
  301. $msg= str_replace("#url#",$url_email,$msg);
  302. $mail_content = $msg;
  303. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  304. $email_admin = api_get_setting('emailAdministrator');
  305. $subject = get_lang('ExerciseAttempted');
  306. $result = @api_mail_html('', $to, $subject, $mail_content, $sender_name, $email_admin, array('charset'=>$mycharset));
  307. }
  308. }
  309. }