public function createQuestionPdf() { $dbEntry = entryPeer::retrieveByPK($this->entryId); $title = "Here are the questions from [" . $dbEntry->getName() . "]"; KalturaLog::debug("Questions from [" . $dbEntry->getName() . "]"); $this->pdf->addTitle($title, $this->titleStyle); $questionType = QuizPlugin::getCuePointTypeCoreValue(QuizCuePointType::QUIZ_QUESTION); $questions = CuePointPeer::retrieveByEntryId($this->entryId, array($questionType)); //arange the array so that the questions will be the key for the array $questArray = array(); foreach ($questions as $question) { $questArray[$question->getName()] = $question; } //sort the array alphabetically according to its key; i.e. the question ksort($questArray, SORT_LOCALE_STRING); $questNum = 0; foreach ($questArray as $question) { $questNum += 1; $this->pdf->addList($questNum, $question->getName(), $this->listWithAddLineBeforeStyle); $this->pdf->addHeadline(6, "Optional Answers:", $this->heading6Style); $ansNum = 0; foreach ($question->getOptionalAnswers() as $optionalAnswer) { $ansNum += 1; $this->pdf->addList($ansNum, $optionalAnswer->getText(), $this->indentListStyle); } } }
/** * @param $entryId * @return int */ public function calculateScore() { $finalScore = 0; $answerType = QuizPlugin::getCuePointTypeCoreValue(QuizCuePointType::QUIZ_ANSWER); $answers = CuePointPeer::retrieveByEntryId($this->getEntryId(), array($answerType)); foreach ($answers as $answer) { /** * @var AnswerCuePoint $answer */ $question = CuePointPeer::retrieveByPK($answer->getParentId()); /** * @var QuestionCuePoint $question */ $optionalAnswers = $question->getOptionalAnswers(); foreach ($optionalAnswers as $optionalAnswer) { /** * @var kOptionalAnswer $optionalAnswer */ if ($optionalAnswer->getKey() === $answer->getAnswerKey()) { if ($optionalAnswer->getIsCorrect()) { $finalScore += $optionalAnswer->getWeight(); } } } } return $finalScore; }
public function calculateScoreAndCorrectAnswers() { //TODO when we have indexing of CuePoints in the sphinx we don't need the answerIds in the custom_data since we can just get the answers of the specific userEntry $answerIds = $this->getAnswerIds(); $questionType = QuizPlugin::getCuePointTypeCoreValue(QuizCuePointType::QUIZ_QUESTION); $questions = CuePointPeer::retrieveByEntryId($this->getEntryId(), array($questionType)); $totalPoints = 0; $userPoints = 0; $numOfCorrectAnswers = 0; foreach ($questions as $question) { $optionalAnswers = $question->getOptionalAnswers(); $answers = array(); if (isset($answerIds[$question->getId()])) { $answerId = $answerIds[$question->getId()]; //TODO change to retrieveByPks (multiple, only one query, no need for multiple) $currAnswer = CuePointPeer::retrieveByPK($answerId); if ($currAnswer->getIsCorrect()) { $numOfCorrectAnswers++; } $answers[] = $currAnswer; } list($totalForQuestion, $userPointsForQuestion) = $this->getCorrectAnswerWeight($optionalAnswers, $answers); $totalPoints += $totalForQuestion; $userPoints += $userPointsForQuestion; } $score = $totalPoints ? $userPoints / $totalPoints : 0; return array($score, $numOfCorrectAnswers); }
public function validateForInsert($propertiesToSkip = array()) { parent::validateForInsert($propertiesToSkip); $dbEntry = entryPeer::retrieveByPK($this->entryId); QuizPlugin::validateAndGetQuiz($dbEntry); if (!QuizPlugin::validateUserEntitledForQuizEdit($dbEntry)) { throw new KalturaAPIException(KalturaErrors::INVALID_USER_ID); } }
public function validateForInsert($propertiesToSkip = array()) { parent::validateForInsert($propertiesToSkip); $dbEntry = entryPeer::retrieveByPK($this->entryId); QuizPlugin::validateAndGetQuiz($dbEntry); if (!QuizPlugin::validateUserEntitledForQuizEdit($dbEntry)) { KalturaLog::debug('Update quiz questions is allowed only with admin KS or entry owner or co-editor'); throw new KalturaAPIException(KalturaErrors::INVALID_USER_ID); } }
public static function fromDbArray($arr, KalturaDetachedResponseProfile $responseProfile = null) { $newArr = new KalturaQuizArray(); if ($arr == null) { return $newArr; } foreach ($arr as $obj) { $kQuiz = QuizPlugin::getQuizData($obj); if (!is_null($kQuiz)) { $quiz = new KalturaQuiz(); $quiz->fromObject($kQuiz, $responseProfile); $newArr[] = $quiz; } } return $newArr; }
public function createQuestionPdf() { $dbEntry = entryPeer::retrieveByPK($this->entryId); $title = "Here are the questions from [" . $dbEntry->getName() . "]"; $this->pdf->addTitle($title, $this->titleStyle); $questionType = QuizPlugin::getCuePointTypeCoreValue(QuizCuePointType::QUIZ_QUESTION); $questions = CuePointPeer::retrieveByEntryId($this->entryId, array($questionType)); $questNum = 0; foreach ($questions as $question) { $questNum += 1; $this->pdf->addList($questNum, $question->getName(), $this->listWithAddLineBeforeStyle); $this->pdf->addHeadline(6, "Optional Answers:", $this->heading6Style); $ansNum = 0; foreach ($question->getOptionalAnswers() as $optionalAnswer) { $ansNum += 1; $this->pdf->addList($ansNum, $optionalAnswer->getText(), $this->indentListStyle); } } }
/** * Submits the quiz so that it's status will be submitted and calculates the score for the quiz * * @action submitQuiz * @actionAlias userEntry.submitQuiz * @param int $id * @return KalturaQuizUserEntry * @throws KalturaAPIException */ public function submitQuizAction($id) { $dbUserEntry = UserEntryPeer::retrieveByPK($id); if (!$dbUserEntry) { throw new KalturaAPIException(KalturaErrors::INVALID_OBJECT_ID, $id); } if ($dbUserEntry->getType() != QuizPlugin::getCoreValue('UserEntryType', QuizUserEntryType::QUIZ)) { throw new KalturaAPIException(KalturaErrors::INVALID_OBJECT_TYPE, $dbUserEntry->getType()); } /** * @var QuizUserEntry $dbUserEntry */ $score = $dbUserEntry->calculateScore(); $dbUserEntry->setScore($score); // $dbUserEntry->setStatus(QuizUserEntryStatus::QUIZ_SUBMITTED); $dbUserEntry->setStatus(QuizPlugin::getCoreValue('UserEntryStatus', QuizUserEntryStatus::QUIZ_SUBMITTED)); $dbUserEntry->save(); $userEntry = new KalturaQuizUserEntry(); $userEntry->fromObject($dbUserEntry, $this->getResponseProfile()); return $userEntry; }
public function calculateScore() { $answerIds = $this->getAnswerIds(); $questionType = QuizPlugin::getCuePointTypeCoreValue(QuizCuePointType::QUIZ_QUESTION); $questions = CuePointPeer::retrieveByEntryId($this->getEntryId(), array($questionType)); $totalPoints = 0; $userPoints = 0; foreach ($questions as $question) { $optionalAnswers = $question->getOptionalAnswers(); $answers = array(); if (isset($answerIds[$question->getId()])) { $answerId = $answerIds[$question->getId()]; $currAnswer = CuePointPeer::retrieveByPK($answerId); $answers[] = $currAnswer; } list($totalForQuestion, $userPointsForQuestion) = $this->getCorrectAnswerWeight($optionalAnswers, $answers); $totalPoints += $totalForQuestion; $userPoints += $userPointsForQuestion; } return $totalPoints ? $userPoints / $totalPoints : 0; }
/** * Submits the quiz so that it's status will be submitted and calculates the score for the quiz * * @action submitQuiz * @actionAlias userEntry.submitQuiz * @param int $id * @return KalturaQuizUserEntry * @throws KalturaAPIException */ public function submitQuizAction($id) { $dbUserEntry = UserEntryPeer::retrieveByPK($id); if (!$dbUserEntry) { throw new KalturaAPIException(KalturaErrors::INVALID_OBJECT_ID, $id); } if ($dbUserEntry->getType() != QuizPlugin::getCoreValue('UserEntryType', QuizUserEntryType::QUIZ)) { throw new KalturaAPIException(KalturaQuizErrors::PROVIDED_ENTRY_IS_NOT_A_QUIZ, $id); } $dbUserEntry->setStatus(QuizPlugin::getCoreValue('UserEntryStatus', QuizUserEntryStatus::QUIZ_SUBMITTED)); $userEntry = new KalturaQuizUserEntry(); $userEntry->fromObject($dbUserEntry, $this->getResponseProfile()); $entryId = $dbUserEntry->getEntryId(); $entry = entryPeer::retrieveByPK($entryId); if (!$entry) { throw new KalturaAPIException(KalturaErrors::INVALID_OBJECT_ID, $entryId); } $kQuiz = QuizPlugin::getQuizData($entry); if (!$kQuiz) { throw new KalturaAPIException(KalturaQuizErrors::PROVIDED_ENTRY_IS_NOT_A_QUIZ, $entryId); } list($score, $numOfCorrectAnswers) = $dbUserEntry->calculateScoreAndCorrectAnswers(); $dbUserEntry->setScore($score); $dbUserEntry->setNumOfCorrectAnswers($numOfCorrectAnswers); if ($kQuiz->getShowGradeAfterSubmission() == KalturaNullableBoolean::TRUE_VALUE || $this->getKs()->isAdmin() == true) { $userEntry->score = $score; } else { $userEntry->score = null; } $c = new Criteria(); $c->add(CuePointPeer::ENTRY_ID, $dbUserEntry->getEntryId(), Criteria::EQUAL); $c->add(CuePointPeer::TYPE, QuizPlugin::getCoreValue('CuePointType', QuizCuePointType::QUIZ_QUESTION)); $dbUserEntry->setNumOfQuestions(CuePointPeer::doCount($c)); $dbUserEntry->setStatus(QuizPlugin::getCoreValue('UserEntryStatus', QuizUserEntryStatus::QUIZ_SUBMITTED)); $dbUserEntry->save(); return $userEntry; }
public function getMetadataObjectType() { return QuizPlugin::getCoreValue('MetadataObjectType', QuizCuePointMetadataObjectType::ANSWER_CUE_POINT); }
/** * KalturaQuizUserEntry constructor. */ public function __construct() { $this->type = QuizPlugin::getCoreValue('UserEntryType', QuizUserEntryType::QUIZ); }
public function validateForUpdate($sourceObject, $propertiesToSkip = array()) { parent::validateForUpdate($sourceObject, $propertiesToSkip); $dbEntry = entryPeer::retrieveByPK($this->entryId); $kQuiz = QuizPlugin::validateAndGetQuiz($dbEntry); $this->validateUserEntry(); if (!$kQuiz->getAllowAnswerUpdate()) { throw new KalturaAPIException(KalturaQuizErrors::ANSWER_UPDATE_IS_NOT_ALLOWED, $sourceObject->getEntryId()); } }
public static function dependsOn() { $cuePointDependency = new KalturaDependency(CuePointPlugin::getPluginName()); $quizDependency = new KalturaDependency(QuizPlugin::getPluginName()); return array($cuePointDependency, $quizDependency); }
/** * Allows to get a quiz * * @action get * @param string $entryId * @return KalturaQuiz * @throws KalturaErrors::ENTRY_ID_NOT_FOUND * @throws KalturaQuizErrors::PROVIDED_ENTRY_IS_NOT_A_QUIZ */ public function getAction($entryId) { $dbEntry = entryPeer::retrieveByPK($entryId); if (!$dbEntry) { throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId); } $kQuiz = QuizPlugin::getQuizData($dbEntry); if (is_null($kQuiz)) { throw new KalturaAPIException(KalturaQuizErrors::PROVIDED_ENTRY_IS_NOT_A_QUIZ, $entryId); } $quiz = new KalturaQuiz(); $quiz->fromObject($kQuiz); return $quiz; }
public function getTypeListResponse(KalturaFilterPager $pager, KalturaDetachedResponseProfile $responseProfile = null, $type = null) { return parent::getTypeListResponse($pager, $responseProfile, QuizPlugin::getCoreValue('CuePointType', QuizCuePointType::QUIZ_QUESTION)); }
/** * sends a with an api request for pdf from quiz object * * @action getUrl * @param string $entryId * @param KalturaQuizOutputType $quizOutputType * @return string * @throws KalturaErrors::ENTRY_ID_NOT_FOUND * @throws KalturaQuizErrors::PROVIDED_ENTRY_IS_NOT_A_QUIZ * @throws KalturaQuizErrors::QUIZ_CANNOT_BE_DOWNLOAD */ public function getUrlAction($entryId, $quizOutputType) { KalturaLog::debug("Create a URL PDF Document download for entry id [ " . $entryId . " ]"); $dbEntry = entryPeer::retrieveByPK($entryId); if (!$dbEntry) { throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId); } $kQuiz = QuizPlugin::getQuizData($dbEntry); if (is_null($kQuiz)) { throw new KalturaAPIException(KalturaQuizErrors::PROVIDED_ENTRY_IS_NOT_A_QUIZ, $entryId); } //validity check if (!$kQuiz->getAllowDownload()) { throw new KalturaAPIException(KalturaQuizErrors::QUIZ_CANNOT_BE_DOWNLOAD); } $finalPath = '/api_v3/service/quiz_quiz/action/serve/quizOutputType/'; $finalPath .= "{$quizOutputType}"; $finalPath .= '/entryId/'; $finalPath .= "{$entryId}"; $ksObj = $this->getKs(); $ksStr = $ksObj ? $ksObj->getOriginalString() : null; $finalPath .= "/ks/" . $ksStr; $partnerId = $this->getPartnerId(); $downloadUrl = myPartnerUtils::getCdnHost($partnerId) . $finalPath; return $downloadUrl; }
public function attachToFinalCriteria(Criteria $c) { $c->addCondition(entryIndex::DYNAMIC_ATTRIBUTES . '.' . QuizPlugin::getDynamicAttributeName() . ' = 1'); return parent::attachToFinalCriteria($c); }
/** * creates a pdf from quiz object * * @action servePdf * @param string $entryId * @return file * @throws KalturaErrors::ENTRY_ID_NOT_FOUND * @throws KalturaQuizErrors::PROVIDED_ENTRY_IS_NOT_A_QUIZ */ public function servePdfAction($entryId) { $dbEntry = entryPeer::retrieveByPK($entryId); if (!$dbEntry) { throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $entryId); } $kQuiz = QuizPlugin::getQuizData($dbEntry); if (is_null($kQuiz)) { throw new KalturaAPIException(KalturaQuizErrors::PROVIDED_ENTRY_IS_NOT_A_QUIZ, $entryId); } $kp = new kQuizPdf($entryId); $kp->createQuestionPdf(); return $kp->submitDocument(); }
/** * @param $objectIds * @param $orderBy * @param $questions * @param $ans * @return array */ protected function getAggregateDataForQuestions($questions, $orderBy) { $ans = array(); foreach ($questions as $question) { $numOfCorrectAnswers = 0; /** * @var QuestionCuePoint $question */ $c = new Criteria(); $c->add(CuePointPeer::ENTRY_ID, $question->getEntryId()); $c->add(CuePointPeer::TYPE, QuizPlugin::getCoreValue('CuePointType', QuizCuePointType::QUIZ_ANSWER)); $c->add(CuePointPeer::PARENT_ID, $question->getId()); $anonKuserIds = $this->getAnonymousKuserIds($question->getPartnerId()); if (!empty($anonKuserIds)) { $c->add(CuePointPeer::KUSER_ID, $anonKuserIds, Criteria::NOT_IN); } $answers = CuePointPeer::doSelect($c); $numOfAnswers = 0; foreach ($answers as $answer) { /** * @var AnswerCuePoint $answer */ $quizUserEntryId = $answer->getQuizUserEntryId(); if ($this->isQuizUserEntrySubmitted($quizUserEntryId)) { $numOfAnswers++; $optionalAnswers = $question->getOptionalAnswers(); $correct = false; foreach ($optionalAnswers as $optionalAnswer) { /** * @var kOptionalAnswer $optionalAnswer */ if ($optionalAnswer->getKey() === $answer->getAnswerKey()) { if ($optionalAnswer->getIsCorrect()) { $numOfCorrectAnswers++; break; } } } } } if ($numOfAnswers) { $pctg = $numOfCorrectAnswers / $numOfAnswers; } else { $pctg = 0.0; } $ans[] = array('question_id' => $question->getId(), 'percentage' => $pctg * 100, 'num_of_correct_answers' => $numOfCorrectAnswers, 'num_of_wrong_answers' => $numOfAnswers - $numOfCorrectAnswers); } uasort($ans, $this->getSortFunction($orderBy)); return $ans; }
/** * @param $objectIds * @param $c criteria * @return criteria */ protected function createGetCuePointByUserIdsCriteria($objectIds, $c) { $c->add(CuePointPeer::TYPE, QuizPlugin::getCoreValue('CuePointType', QuizCuePointType::QUIZ_ANSWER)); $userIds = QuizPlugin::getObjectIdsAsArray($objectIds); $kuserIds = array(); foreach ($userIds as $userId) { $kuser = kuserPeer::getKuserByPartnerAndUid(kCurrentContext::$ks_partner_id, $userId); if ($kuser) { $kuserIds[] = $kuser->getKuserId(); } } $c->add(CuePointPeer::KUSER_ID, $kuserIds, Criteria::IN); return $c; }
public function applyCondition(IKalturaDbQuery $query) { $query->addCondition(entryIndex::DYNAMIC_ATTRIBUTES . '.' . QuizPlugin::getDynamicAttributeName() . ' = ' . ($this->isQuiz ? '1' : '0')); }