1<?php 2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */ 3 4 5/** 6 * @author Björn Heyser <bheyser@databay.de> 7 * @version $Id$ 8 * 9 * @package Modules/Test 10 */ 11class ilTestGradingMessageBuilder 12{ 13 /** 14 * @var ilLanguage 15 */ 16 private $lng; 17 18 /** 19 * @var ilObjTest 20 */ 21 private $testOBJ; 22 23 /** 24 * @var ilTemplate 25 */ 26 private $tpl; 27 28 /** 29 * @var array 30 */ 31 private $resultData; 32 33 /** 34 * @var integer 35 */ 36 private $activeId; 37 38 /** 39 * @param ilLanguage $lng 40 * @param ilObjTest $testOBJ 41 */ 42 public function __construct(ilLanguage $lng, ilObjTest $testOBJ) 43 { 44 $this->lng = $lng; 45 $this->testOBJ = $testOBJ; 46 } 47 48 public function setActiveId($activeId) 49 { 50 $this->activeId = $activeId; 51 } 52 53 public function getActiveId() 54 { 55 return $this->activeId; 56 } 57 58 public function buildMessage() 59 { 60 $this->loadResultData(); 61 62 if ($this->testOBJ->isShowGradingStatusEnabled()) { 63 $this->addMessagePart($this->buildGradingStatusMsg()); 64 } 65 66 if ($this->testOBJ->areObligationsEnabled()) { 67 $this->addMessagePart($this->buildObligationsMsg()); 68 } 69 70 if ($this->testOBJ->isShowGradingMarkEnabled()) { 71 $this->addMessagePart($this->buildGradingMarkMsg()); 72 } 73 74 if ($this->testOBJ->getECTSOutput()) { 75 $this->addMessagePart($this->buildEctsGradeMsg()); 76 } 77 } 78 79 private function addMessagePart($msgPart) 80 { 81 $this->messageText[] = $msgPart; 82 } 83 84 private function getFullMessage() 85 { 86 return implode(' ', $this->messageText); 87 } 88 89 private function isPassed() 90 { 91 return (bool) $this->resultData['passed']; 92 } 93 94 public function sendMessage() 95 { 96 if (!$this->testOBJ->isShowGradingStatusEnabled()) { 97 ilUtil::sendInfo($this->getFullMessage()); 98 } elseif ($this->isPassed()) { 99 ilUtil::sendSuccess($this->getFullMessage()); 100 } else { 101 ilUtil::sendFailure($this->getFullMessage()); 102 } 103 } 104 105 private function loadResultData() 106 { 107 $this->resultData = $this->testOBJ->getResultsForActiveId($this->getActiveId()); 108 109 if ($this->testOBJ->getECTSOutput()) { 110 $ectsMark = $this->testOBJ->getECTSGrade( 111 $this->testOBJ->getTotalPointsPassedArray(), 112 $this->resultData['reached_points'], 113 $this->resultData['max_points'] 114 ); 115 116 $this->resultData['ects_grade'] = strtoupper($ectsMark); 117 } 118 } 119 120 private function buildGradingStatusMsg() 121 { 122 if ($this->isPassed()) { 123 return $this->lng->txt('grading_status_passed_msg'); 124 } 125 126 return $this->lng->txt('grading_status_failed_msg'); 127 } 128 129 private function buildGradingMarkMsg() 130 { 131 $markMsg = $this->lng->txt('grading_mark_msg'); 132 133 $markMsg = str_replace("[mark]", $this->getMarkOfficial(), $markMsg); 134 $markMsg = str_replace("[markshort]", $this->getMarkShort(), $markMsg); 135 $markMsg = str_replace("[percentage]", $this->getPercentage(), $markMsg); 136 $markMsg = str_replace("[reached]", $this->getReachedPoints(), $markMsg); 137 $markMsg = str_replace("[max]", $this->getMaxPoints(), $markMsg); 138 139 return $markMsg; 140 } 141 142 private function getMarkOfficial() 143 { 144 return $this->resultData['mark_official']; 145 } 146 147 private function getMarkShort() 148 { 149 return $this->resultData['mark_short']; 150 } 151 152 private function getPercentage() 153 { 154 $percentage = 0; 155 156 if ($this->getMaxPoints() > 0) { 157 $percentage = $this->getReachedPoints() / $this->getMaxPoints(); 158 } 159 160 return sprintf("%.2f", $percentage); 161 } 162 163 private function getReachedPoints() 164 { 165 return $this->resultData['reached_points']; 166 } 167 168 private function getMaxPoints() 169 { 170 return $this->resultData['max_points']; 171 } 172 173 private function buildObligationsMsg() 174 { 175 if ($this->areObligationsAnswered()) { 176 return $this->lng->txt('grading_obligations_answered_msg'); 177 } 178 179 return $this->lng->txt('grading_obligations_missing_msg'); 180 } 181 182 private function areObligationsAnswered() 183 { 184 return (bool) $this->resultData['obligations_answered']; 185 } 186 187 private function buildEctsGradeMsg() 188 { 189 return str_replace('[markects]', $this->getEctsGrade(), $this->lng->txt('mark_tst_ects')); 190 } 191 192 private function getEctsGrade() 193 { 194 return $this->resultData['ects_grade']; 195 } 196 197 public function buildList() 198 { 199 $this->loadResultData(); 200 201 $this->initListTemplate(); 202 203 if ($this->testOBJ->isShowGradingStatusEnabled()) { 204 $passedStatusLangVar = $this->isPassed() ? 'passed_official' : 'failed_official'; 205 206 $this->populateListEntry( 207 $this->lng->txt('passed_status'), 208 $this->lng->txt($passedStatusLangVar) 209 ); 210 } 211 212 if ($this->testOBJ->areObligationsEnabled()) { 213 if ($this->areObligationsAnswered()) { 214 $obligAnsweredStatusLangVar = 'grading_obligations_answered_listentry'; 215 } else { 216 $obligAnsweredStatusLangVar = 'grading_obligations_missing_listentry'; 217 } 218 219 $this->populateListEntry( 220 $this->lng->txt('grading_obligations_listlabel'), 221 $this->lng->txt($obligAnsweredStatusLangVar) 222 ); 223 } 224 225 if ($this->testOBJ->isShowGradingMarkEnabled()) { 226 $this->populateListEntry($this->lng->txt('tst_mark'), $this->getMarkOfficial()); 227 } 228 229 if ($this->testOBJ->getECTSOutput()) { 230 $this->populateListEntry($this->lng->txt('ects_grade'), $this->getEctsGrade()); 231 } 232 233 $this->parseListTemplate(); 234 } 235 236 public function initListTemplate() 237 { 238 $this->tpl = new ilTemplate('tpl.tst_grading_msg_list.html', true, true, 'Modules/Test'); 239 } 240 241 private function populateListEntry($label, $value) 242 { 243 $this->tpl->setCurrentBlock('grading_msg_entry'); 244 $this->tpl->setVariable('LABEL', $label); 245 $this->tpl->setVariable('VALUE', $value); 246 $this->tpl->parseCurrentBlock(); 247 } 248 249 private function parseListTemplate() 250 { 251 $this->tpl->setCurrentBlock('grading_msg_list'); 252 $this->tpl->parseCurrentBlock(); 253 } 254 255 public function getList() 256 { 257 return $this->tpl->get(); 258 } 259} 260