* * @copyright 2004 Stud.IP-Project * @access public * @package evaluation * */ class EvaluationExportManagerCSV extends EvaluationExportManager { # Define all required variables ============================================= # var $evalquestions_residual = []; # ============================================================ end: variables # # Define constructor and destructor ========================================= # /** * Constructor * @access public * @param string $evalID The ID of the evaluation for export */ function __construct($evalID) { /* Set default values ------------------------------------------------- */ register_shutdown_function([&$this, "_EvaluationExportManagerCSV"]); ini_set('memory_limit', '256M'); parent::__construct($evalID); $this->instanceof = INSTANCEOF_EVALEXPORTMANAGERCSV; $this->extension = EVALEXPORT_EXTENSION; /* -------------------------------------------------------------------- */ } /** * Destructor. Closes all files and removes old temp files * @access public */ function _EvaluationExportManagerCSV () { } # =========================================== end: constructor and destructor # # Define public functions =================================================== # /** * Exports the evaluation * @access public */ function export () { parent::export (); if ($this->isError ()) return; $this->exportHeader (); $this->exportContent(); } /** * Exports the headline * @access public */ function exportHeader () { if (empty ($this->filehandle)) return $this->throwError (1, _("ExportManager::Konnte temporäre Datei nicht öffnen.")); fputs ($this->filehandle, "\xEF\xBB\xBF"); fputs ($this->filehandle, EVALEXPORT_DELIMITER . _("Nummer") . EVALEXPORT_DELIMITER.EVALEXPORT_SEPERATOR); fputs ($this->filehandle, EVALEXPORT_DELIMITER . _("Datum") . EVALEXPORT_DELIMITER.EVALEXPORT_SEPERATOR); fputs ($this->filehandle, EVALEXPORT_DELIMITER . _("Benutzername") . EVALEXPORT_DELIMITER.EVALEXPORT_SEPERATOR); fputs ($this->filehandle, EVALEXPORT_DELIMITER . _("Nachname") . EVALEXPORT_DELIMITER.EVALEXPORT_SEPERATOR); fputs ($this->filehandle, EVALEXPORT_DELIMITER . _("Vorname") . EVALEXPORT_DELIMITER.EVALEXPORT_SEPERATOR); fputs ($this->filehandle, EVALEXPORT_DELIMITER . _("E-Mail") . EVALEXPORT_DELIMITER.EVALEXPORT_SEPERATOR); /* for each question -------------------------------------------------- */ foreach ($this->evalquestions as $evalquestion) { $type = $evalquestion->getType (); $residual = ""; /* Questiontype: likert scale -------------------------------------- */ if ($type == EVALQUESTION_TYPE_LIKERT) { EvaluationAnswerDB::addChildren($evalquestion); $header = $evalquestion->getText ().":"; while ($answer = &$evalquestion->getNextChild ()) { if ($answer->isResidual ()) { $residual = $evalquestion->getText ().":".$answer->getText (); } else { $header .= $answer->getText (); $header .= "(".$answer->getPosition ().")"; $header .= ","; } } $header = mb_substr ($header, 0, mb_strlen ($header) - 1); $this->addCol ($header); if (!empty ($residual)) { $this->addCol ($residual); $this->evalquestions_residual[$evalquestion->getObjectID()] = true; } /* ----------------------------------------------------- end: likert */ /* Questiontype: pol scale ----------------------------------------- */ } elseif ($type == EVALQUESTION_TYPE_POL) { EvaluationAnswerDB::addChildren($evalquestion); $header = $evalquestion->getText ().":"; $answer = $evalquestion->getNextChild (); $header .= $answer->getText (); $header .= "(".$answer->getPosition ().")"; $header .= "-"; while ($answer = &$evalquestion->getNextChild ()) { if ($answer->isResidual ()) $residual = $evalquestion->getText ().":".$answer->getText (); else $last = $answer->getText ()."(".$answer->getPosition ().")"; } $header .= $last; $this->addCol ($header); if (!empty ($residual)) { $this->addCol ($residual); $this->evalquestions_residual[$evalquestion->getObjectID()] = true; } /* -------------------------------------------------------- end: pol */ /* Questiontype: multiple chioice ---------------------------------- */ } elseif ($type == EVALQUESTION_TYPE_MC) { if ($evalquestion->isMultiplechoice ()) { EvaluationAnswerDB::addChildren($evalquestion); while ($answer = &$evalquestion->getNextChild ()) { $header = $evalquestion->getText (); $header .= ":".$answer->getText (); $this->addCol ($header); } } else { $header = $evalquestion->getText (); $this->addCol ($header); } /* --------------------------------------------------------- end: mc */ /* Questiontype: undefined ----------------------------------------- */ } else { return $this->throwError (2, _("ExportManager::Ungültiger Typ.")); } /* -------------------------------------------------- end: undefined */ } /* ---------------------------------------------- end: foreach question */ fputs ($this->filehandle, EVALEXPORT_ENDROW); } /** * Exports the content * @access public */ function exportContent () { $counter = 0; $answers = []; $db = DBManager::get(); $stmt = $db->prepare("SELECT user_id,text,value,position,residual, MAX(evaldate) as evaldate, GROUP_CONCAT(evalanswer_id) as evalanswer_id FROM evalanswer INNER JOIN evalanswer_user USING ( evalanswer_id ) WHERE parent_id = ? GROUP BY user_id"); foreach ($this->evalquestions as $evalquestion) { $stmt->execute([$evalquestion->getObjectID()]); $answers[$evalquestion->getObjectID()] = $stmt->fetchGrouped(); } /* One row for each user --------------------------------------------- */ foreach ($this->users as $userID) { /* Userinformation if available ----------------------------------- */ $username = ""; $name = ""; $surname = ""; $email = ""; $evaldate = ""; if (!$this->eval->isAnonymous ()) { $data = DBManager::get()->query("SELECT username, Vorname, Nachname, Email " . "FROM auth_user_md5 WHERE user_id = " . DBManager::get()->quote($userID))->fetchAll(PDO::FETCH_NUM); if (is_array($data[0])) { list($username, $name, $surname, $email) = $data[0]; } } if ($timestamp = $answers[$this->evalquestions[0]->getObjectID()][$userID]['evaldate']) { $evaldate = date('Y-m-d H:i:s', $timestamp); } fputs ($this->filehandle, EVALEXPORT_DELIMITER . ++$counter . EVALEXPORT_DELIMITER.EVALEXPORT_SEPERATOR); fputs ($this->filehandle, EVALEXPORT_DELIMITER . $evaldate . EVALEXPORT_DELIMITER.EVALEXPORT_SEPERATOR); fputs ($this->filehandle, EVALEXPORT_DELIMITER . $username . EVALEXPORT_DELIMITER.EVALEXPORT_SEPERATOR); fputs ($this->filehandle, EVALEXPORT_DELIMITER . $surname . EVALEXPORT_DELIMITER.EVALEXPORT_SEPERATOR); fputs ($this->filehandle, EVALEXPORT_DELIMITER . $name . EVALEXPORT_DELIMITER.EVALEXPORT_SEPERATOR); fputs ($this->filehandle, EVALEXPORT_DELIMITER . $email . EVALEXPORT_DELIMITER.EVALEXPORT_SEPERATOR); /* ------------------------------------------------- end: user info */ /* One colum for each question ------------------------------------ */ foreach ($this->evalquestions as $evalquestion) { $type = $evalquestion->getType (); /* Questiontype: pol or likert scale --------------------------- */ if ($type == EVALQUESTION_TYPE_LIKERT || $type == EVALQUESTION_TYPE_POL) { $hasResidual = $this->evalquestions_residual[$evalquestion->getObjectID()]; $entry = ""; $residual = 0; if ($answer = $answers[$evalquestion->getObjectID()][$userID]) { if ($answer['residual']) { $residual = 1; } else { $entry = $answer['position']; } } $this->addCol ($entry); if ($hasResidual) { $this->addCol ($residual); } } /* ------------------------------------------------- end: likert */ /* Questiontype: multiple chioice ------------------------------ */ elseif ($type == EVALQUESTION_TYPE_MC) { if ($evalquestion->isMultiplechoice ()) { $mc_answers = explode(',', $answers[$evalquestion->getObjectID()][$userID]['evalanswer_id']); while ($answer = &$evalquestion->getNextChild ()) { $this->addCol ((int)in_array($answer->getObjectID(), $mc_answers)); } } else { $entry = ""; if ($answer = $answers[$evalquestion->getObjectID()][$userID]) { $entry = preg_replace ("(\r\n|\n|\r)", " ", $answer['text']); } $this->addCol ($entry); } } /* ------------------------------------------------------ end: mc */ /* Questiontype: undefined -------------------------------------- */ else { return $this->throwError (1, _("ExportManager::Ungültiger Fragetyp.")); } /* ----------------------------------------------- end: undefined */ } /* ------------------------------------------ end: col for question */ fputs ($this->filehandle, EVALEXPORT_ENDROW); } /* -------------------------------------------- end: row for each user */ } # ===================================================== end: public functions # # Define private functions ================================================== # /** * Adds a row for the text * @param string $text The text for the row * @access private */ function addCol ($text) { $col = str_replace (EVALEXPORT_DELIMITER, EVALEXPORT_NODELIMITER, $text); fputs ($this->filehandle, EVALEXPORT_DELIMITER.$col.EVALEXPORT_DELIMITER.EVALEXPORT_SEPERATOR); } # ==================================================== end: private functions # } ?>