X-Git-Url: https://git.rohieb.name/iserv-mod-error-reporter.git/blobdiff_plain/ae87133905d183987eb75369fe94bb739a2b1cb9..cbadf327e8508d53c8e2b633c99d2eaa470252d8:/inc/class_erErrorReportManager.inc?ds=sidebyside diff --git a/inc/class_erErrorReportManager.inc b/inc/class_erErrorReportManager.inc index 1536159..4e3a127 100644 --- a/inc/class_erErrorReportManager.inc +++ b/inc/class_erErrorReportManager.inc @@ -1,28 +1,28 @@ null, writeErrorReport() will use the next ID which is not used in the - * table. + * To add an error reports, simply create an instance of this class and call + * writeErrorReport() with an erErrorReport object as the only parameter. If + * the $nUid member of the erErrorReport object is null, + * writeErrorReport() will use the next ID which is not used in the table. * * @par * An error report that has been written to the database can be changed using - * writeErrorReport(), setting the $nUid member of the erErrorReport parameter to the UID of the - * error report to be changed. + * writeErrorReport(), setting the $nUid member of the erErrorReport parameter + * to the UID of the error report to be changed. * * @par - * To change the visibility of an error report to @e hidden, call setErrorReportDoneFlag() with - * the UID of the error report and set the second parameter to false. + * To change the visibility of an error report to @e hidden, call + * setErrorReportDoneFlag() with the UID of the error report and set the second + * parameter to false. * * @par - * To delete an error report from the database, call deleteErrorReport() with the UID of the - * error report you want to delete. + * To delete an error report from the database, call deleteErrorReport() with + * the UID of the error report you want to delete. * * @par - * Error reports can be retrieved by their UID (getErrorReportByID()), by creation date - * (getErrorReportsByDate()), by owner (getErrorReportsByOwner()), by machine to which the - * report refers (getErrorReportsByMachine()) and by the person who wrote the comment - * (getErrorReportsByCommentOwner()). All error reports in the database can be retrieved using - * getErrorReports(). All of these functions return a object of type erErrorReport or an array - * of erErrorReport objects. + * Error reports can be retrieved by their UID (getErrorReportByID()), by + * creation date (getErrorReportsByDate()), by owner (getErrorReportsByOwner()), + * by machine to which the report refers (getErrorReportsByMachine()) and by + * the person who wrote the comment (getErrorReportsByCommentOwner()). All + * error reports in the database can be retrieved using getErrorReports(). All + * of these functions return a object of type erErrorReport or an array of + * erErrorReport objects. * * @par Example - * The following example creates a new error report “This doesn’t work”, written by the user - * “testuser” on February 5, 2007, which refers to the machine “Client-26” and is not visible for - * non-admin users. Then it adds a nice comment, makes the error report visible and writes it to - * the database. + * The following example creates a new error report “This doesn’t work”, written + * by the user “testuser” on February 5, 2007, which refers to the machine + * “Client-26” and is not visible for non-admin users. Then it adds a nice + * comment, makes the error report visible and writes it to the database. * @code * setComment("We know that already", "admin"); - * - * $nNewID = $obj->writeErrorReport($em); // $em->nUid is null, so writeErrorReport() - * // calculates the UID by itself - * ?> + * // $em->nUid is null, so writeErrorReport() calculates the UID by itself + * $nNewID = $obj->writeErrorReport($em); * @endcode */ @@ -121,7 +125,8 @@ class erErrorReportManager { /** * Constructor - * @param $objcfg (object of type erConfig) Pointer to the configuration class for retrieving the + * @param $objcfg (object of type erConfig) Pointer to the configuration + * class for retrieving the * configuration data * @return erErrorReportManager */ @@ -129,20 +134,33 @@ class erErrorReportManager { $this->objcfg = &$objcfg; } - //////////////////////////////////////////// QUERYING /////////////////////////////////////////// + /////////////////////////////////// QUERYING ///////////////////////////////// /** * @name Quering * @{ */ + /** + * Get the number of reported errors + * @return int + */ + public function getNumErrorReports() { + $ar = pg_fetch_array(db_query("SELECT COUNT(*) AS count FROM ". + "mod_errorreporter;"), 0); + return intval($ar["count"]); + } + /** * Get all error reports in the database. - * If this function fails, it returns null. Call getLastError() to get more information. - * @param $arcSort (array of constants) Defines the sorting of the returned array. - * See the documentation of the $arcSort parameter of buildOrderByClause() for more information. + * If this function fails, it returns null. Call getLastError() to + * get more information. + * @param $arcSort (array of constants) Defines the sorting of the returned + * array. See the documentation of the $arcSort parameter of + * buildOrderByClause() for more information. * @return array of objects of type erErrorReport */ - public function getErrorReports($arcSort = array(ER_ERM_SORT_UID => ER_ERM_SORT_ASC)) { + public function getErrorReports($arcSort = array(ER_ERM_SORT_UID => + ER_ERM_SORT_ASC)) { return $this->readFromSQL("", $arcSort); } @@ -163,34 +181,42 @@ class erErrorReportManager { /** * Get all error reports which have been created on the specified date. - * If this function fails, it returns null. Call getLastError() to get more information. + * If this function fails, it returns null. Call getLastError() to + * get more information. * @param $tsDate (timestamp) Date - * @param $arcSort (array of constants) Defines the sorting of the returned array. - * See the documentation of the $arcSort parameter of buildOrderByClause() for more information. + * @param $arcSort (array of constants) Defines the sorting of the returned + * array. See the documentation of the $arcSort parameter of + * buildOrderByClause() for more information. * @return array of objects of type erErrorReport */ - public function getErrorReportsByDate($tsDate, $arcSort = array(ER_ERM_SORT_UID => ER_ERM_SORT_ASC)) { + public function getErrorReportsByDate($tsDate, $arcSort = + array(ER_ERM_SORT_UID => ER_ERM_SORT_ASC)) { return $this->readFromSQL("er_date = ".qdb(date("Y\-m\-d", $tsDate)), $arcSort); } /** * Get all error reports which have been created by the specified user. - * If this function fails, it returns null. Call getLastError() to get more information. + * If this function fails, it returns null. Call getLastError() to + * get more information. * @param $strAct (string) Account name - * @param $arcSort (array of constants) Defines the sorting of the returned array. - * See the documentation of the $arcSort parameter of buildOrderByClause() for more information. + * @param $arcSort (array of constants) Defines the sorting of the returned + * array. See the documentation of the $arcSort parameter of + * buildOrderByClause() for more information. * @return array of objects of type erErrorReport */ - public function getErrorReportsByOwner($strAct, $arcSort = array(ER_ERM_SORT_UID => ER_ERM_SORT_ASC)) { + public function getErrorReportsByOwner($strAct, $arcSort = + array(ER_ERM_SORT_UID => ER_ERM_SORT_ASC)) { return $this->readFromSQL("er_act = ".qdb($strAct), $arcSort); } /** * Get all error reports which are related to a specified machine. - * If this function fails, it returns null. Call getLastError() to get more information. + * If this function fails, it returns null. Call getLastError() to + * get more information. * @param $strMachine (string) Machine name - * @param $arcSort (array of constants) Defines the sorting of the returned array. - * See the documentation of the $arcSort parameter of buildOrderByClause() for more information. + * @param $arcSort (array of constants) Defines the sorting of the returned + * array. See the documentation of the $arcSort parameter of + * buildOrderByClause() for more information. * @return array of objects of type erErrorReport */ public function getErrorReportsByMachine($strMachine, @@ -200,10 +226,12 @@ class erErrorReportManager { /** * Get all error reports which have been commented by the specified user. - * If this function fails, it returns null. Call getLastError() to get more information. + * If this function fails, it returns null. Call getLastError() to + * get more information. * @param $strAct (string) Account name - * @param $arcSort (array of constants) Defines the sorting of the returned array. - * See the documentation of the $arcSort parameter of buildOrderByClause() for more information. + * @param $arcSort (array of constants) Defines the sorting of the returned + * array. See the documentation of the $arcSort parameter of + * buildOrderByClause() for more information. * @return array of objects of type erErrorReport */ public function getErrorReportsByCommentOwner($strAct, @@ -215,36 +243,43 @@ class erErrorReportManager { /** * Get datasets from the SQL tables. - * This function should not be called directly. Use the getBy* functions instead. - * If this function fails, it returns null. Call getLastError() to get more information. + * This function should not be called directly. Use the getBy* functions + * instead. If this function fails, it returns null. Call + * getLastError() to get more information. * @internal * @param $strWhere (string) Parameters for the SQL WHERE clause - * @param $arcSort (array of constants) Defines the sorting of the returned array. - * See the documentation of the $arcSort parameter of buildOrderByClause() for more information. + * @param $arcSort (array of constants) Defines the sorting of the returned + * array. See the documentation of the $arcSort parameter of + * buildOrderByClause() for more information. * @return array of objects of type erErrorReport */ - protected function readFromSQL($strWhere = "", $arcSort = array(ER_ERM_SORT_UID => ER_ERM_SORT_ASC)) { + protected function readFromSQL($strWhere = "", $arcSort = + array(ER_ERM_SORT_UID => ER_ERM_SORT_ASC)) { $arReturn = array(); - // only allow visible reports for non-admins, but all reports for admins and owners + // only allow visible reports for non-admins, but all reports for + // admins and owners if($this->objcfg->userIsAdmin()) { $strWhereClause = (trim($strWhere) == "") ? "" : "WHERE $strWhere"; } else { - $strWhereClause = "WHERE ".((trim($strWhere) == "") ? "" : "$strWhere AND "). - sprintf("((er_act = '%s') or (er_hidden IS NULL OR NOT er_hidden))", $_SESSION["act"]); + $strWhereClause = "WHERE ".((trim($strWhere) == "") ? "" : + "$strWhere AND ").sprintf("((er_act = '%s') or (er_hidden IS NULL OR ". + " er_hidden))", $_SESSION["act"]); } $strSortClause = $this->buildOrderByClause($arcSort); // fetch the error reports - $hQuery = db_query("SELECT * FROM mod_errorreporter $strWhereClause $strSortClause;"); + $hQuery = db_query("SELECT * FROM mod_errorreporter $strWhereClause ". + "$strSortClause;"); if(!is_resource($hQuery)) { setLastError(ER_ERROR_SQL); return null; } while($arResult = pg_fetch_array($hQuery)) { - $er = new erErrorReport(strtotime($arResult["er_date"]), $arResult["er_act"], - $arResult["er_machine"], $arResult["er_text"], $arResult["er_hidden"] == "t"); + $er = new erErrorReport(strtotime($arResult["er_date"]), + $arResult["er_act"], $arResult["er_machine"], $arResult["er_text"], + $arResult["er_hidden"] == "t"); $er->setUid($arResult["er_uid"]); $er->setComment($arResult["er_comment"], $arResult["er_commentact"]); $arReturn[] = $er; @@ -254,10 +289,11 @@ class erErrorReportManager { /** * Build a ORDER BY clause from an array. - * This helper function takes an array of sorting constants as input and returns an ORDER BY - * clause. The values in the clause are in the same order as the constants in the array. - * The function in the following example returns an ORDER BY clause where the datasets are first - * sorted ascending by visibility, then descending by the machine name to which they refer and + * This helper function takes an array of sorting constants as input and + * returns an ORDER BY clause. The values in the clause are in the same order + * as the constants in the array. The function in the following example + * returns an ORDER BY clause where the datasets are first sorted ascending + * by visibility, then descending by the machine name to which they refer and * finally ascending by their UID: * @code * $strOrder = buildOrderClause(array( @@ -268,8 +304,9 @@ class erErrorReportManager { * @endcode * The output is: ORDER BY er_hidden ASC, er_machine DESC, er_uid ASC * @internal - * @param $arcSort (array of constants) The array which declares the order of the SQL rows. Use - * @ref errorreportmanager_sorting as keys and @ref errorreportmanager_sorting_dir as values. + * @param $arcSort (array of constants) The array which declares the order of + * the SQL rows. Use @ref errorreportmanager_sorting as keys and + * @ref errorreportmanager_sorting_dir as values. * @return string */ protected function buildOrderByClause($arcSort) { @@ -278,27 +315,41 @@ class erErrorReportManager { foreach($arcSort as $cSort => $cDir) { switch($cSort) { case ER_ERM_SORT_UID: - $arstrSort[] = "er_uid ".($cDir == ER_ERM_SORT_DESC ? "DESC" : "ASC"); break; + $arstrSort[] = "er_uid ".($cDir == ER_ERM_SORT_DESC ? "DESC" : "ASC"); + break; case ER_ERM_SORT_DATE: - $arstrSort[] = "er_date ".($cDir == ER_ERM_SORT_DESC ? "DESC" : "ASC"); break; + $arstrSort[] = "er_date ".($cDir == ER_ERM_SORT_DESC ? "DESC" : + "ASC"); + break; case ER_ERM_SORT_OWNER: - $arstrSort[] = "er_act ".($cDir == ER_ERM_SORT_DESC ? "DESC" : "ASC"); break; + $arstrSort[] = "er_act ".($cDir == ER_ERM_SORT_DESC ? "DESC" : "ASC"); + break; case ER_ERM_SORT_MACHINE: - $arstrSort[] = "er_machine ".($cDir == ER_ERM_SORT_DESC ? "DESC" : "ASC"); break; + $arstrSort[] = "er_machine ".($cDir == ER_ERM_SORT_DESC ? "DESC" : + "ASC"); + break; case ER_ERM_SORT_TEXT: - $arstrSort[] = "er_text ".($cDir == ER_ERM_SORT_DESC ? "DESC" : "ASC"); break; + $arstrSort[] = "er_text ".($cDir == ER_ERM_SORT_DESC ? "DESC" : + "ASC"); + break; case ER_ERM_SORT_COMMENT: - $arstrSort[] = "er_comment ".($cDir == ER_ERM_SORT_DESC ? "DESC" : "ASC"); break; + $arstrSort[] = "er_comment ".($cDir == ER_ERM_SORT_DESC ? "DESC" : + "ASC"); + break; case ER_ERM_SORT_COMMENTOWNER: - $arstrSort[] = "er_commentact ".($cDir == ER_ERM_SORT_DESC ? "DESC" : "ASC"); break; + $arstrSort[] = "er_commentact ".($cDir == ER_ERM_SORT_DESC ? "DESC" : + "ASC"); + break; case ER_ERM_SORT_VISIBILITY: - $arstrSort[] = "er_hidden ".($cDir == ER_ERM_SORT_DESC ? "DESC" : "ASC"); break; + $arstrSort[] = "er_hidden ".($cDir == ER_ERM_SORT_DESC ? "DESC" : + "ASC"); + break; } } return ("ORDER BY ".join(", ", $arstrSort)); } - //////////////////////////////////// ERROR MESSAGE MANAGEMENT /////////////////////////////////// + ////////////////////////// ERROR MESSAGE MANAGEMENT ////////////////////////// /** * @name Error report management @@ -307,9 +358,9 @@ class erErrorReportManager { /** * Insert or update an error report in the database. - * When the $nUid member of the $erErrorReport object is null, a new unique ID is - * assigned and the error report is written to the database. Otherwise, the corresponding - * record in the table is updated. + * When the $nUid member of the $erErrorReport object is null, a new + * unique ID is assigned and the error report is written to the database. + * Otherwise, the record having the respective UID in the table is updated. * @param $er (erErrorReport) * @return (int) The unique ID of the inserted or updated record. * @throws Exception @@ -327,7 +378,7 @@ class erErrorReportManager { $arPut = array(); if($er->getUid() == null) { // er_uid is now serial - /*$hQuery = db_query("SELECT MAX(er_uid) AS id FROM mod_errorreporter;"); + /*$hQuery = db_query("SELECT MAX(er_uid) AS id FROM mod_errorreporter;"); if(!is_resource($hQuery)) { setLastError(ER_ERROR_SQL); return -1; @@ -339,10 +390,11 @@ class erErrorReportManager { } else { $strWhere = "er_uid = ".qdb(intval($er->getUid())); $oOldReport = $this->getErrorReportByID($er->getUid()); - $strLog = sprintf("Fehlermeldung geändert, Rechner war „%s“, Text war „%s“. ". - "Neuer Rechner: „%s“, neuer Text: „%s“, Kommentar: %s", - $oOldReport->getMachine(), $oOldReport->getText(), $er->getMachine(), - $er->getText(), (strlen($er->getComment()) > 0) ? "„".$er->getComment()."“" : "(leer)"); + $strLog = sprintf("Fehlermeldung geändert, Rechner war „%s“, Text war ". + "„%s“. Neuer Rechner: „%s“, neuer Text: „%s“, Kommentar: %s", + $oOldReport->getMachine(), $oOldReport->getText(), $er->getMachine(), + $er->getText(), (strlen($er->getComment()) > 0) ? + "„" . $er->getComment() . "“" : "(leer)"); } $arPut["er_date"] = date("Y\-m\-d\ G\:i\:s", $er->getDate()); $arPut["er_act"] = $er->getOwner(); @@ -357,18 +409,24 @@ class erErrorReportManager { // send notification mail, but only if the message is new if($this->objcfg->isMailNotify() and $er->getUid() == null) { - $strMailText = sprintf("\nEin Benutzer hat eine Fehlermeldung mit dem ". - "Fehlermeldungs-Assistenten abgeschickt. Nachfolgend finden sich Angaben über den ". - "Fehler.\n\n\n". - "\n\n". - "\n
Fehler gemeldet von:%s
Datum:%s
Betroffener Rechner:%s
%s
\n
\nDiese Mail wurde automatisch ". - "generiert.\n", q(erGetRealUserName($er->getOwner())), - q(date("d\.m\.Y\ G\:i\:s", $er->getDate())), q($er->getMachine()), q($er->getText())); + $strMailText = sprintf("\nEin Benutzer hat eine ". + "Fehlermeldung mit dem Fehlermeldungs-Assistenten abgeschickt. ". + "Nachfolgend finden sich Angaben über den Fehler.\n". + "\n\n". + "\n". + "\n". + "\n
Fehler gemeldet von:%s
Datum:%s
Betroffener Rechner:%s
%s
\n
\n". + "Diese Mail wurde automatisch generiert.\n". + "", q(erGetRealUserName($er->getOwner())), + q(date("d\.m\.Y\ G\:i\:s", $er->getDate())), q($er->getMachine()), + q($er->getText())); $strMailSubject = sprintf("%s hat einen Fehler im System gemeldet", q(erGetRealUserName($er->getOwner()))); - $strMailHeader = sprintf("From: %s <%s>\nContent-Type: text/html; charset=utf-8", + $strMailHeader = sprintf("From: %s <%s>\n". + "Content-Type: text/html; charset=utf-8", q(erGetRealUserName($er->getOwner())), user_mail_addr($er->getOwner())); - mail($this->objcfg->getMailNotifyAddr(), $strMailSubject, $strMailText, $strMailHeader); + mail($this->objcfg->getMailNotifyAddr(), $strMailSubject, $strMailText, + $strMailHeader); } return $er->getUid(); @@ -376,8 +434,9 @@ class erErrorReportManager { /** * Delete an error report from the database. - * @param $nErrorReportID (int) Unique ID of the error report to delete or -1 if an error - * occured. In this case, call getLastError() to get more information. + * @param $nErrorReportID (int) Unique ID of the error report to delete or -1 + * if an error occured. In this case, call getLastError() to get more + * information. */ public function deleteErrorReport($nErrorReportID) { if(!($this->objcfg->userIsAdmin() or $this->userIsOwner($nErrorReportID))) { @@ -386,9 +445,10 @@ class erErrorReportManager { } $oOldReport = $this->getErrorReportByID($nErrorReportID); - db_query(sprintf("DELETE FROM mod_errorreporter WHERE er_uid = '%d';", intval($nErrorReportID))); - erInsertLog(sprintf("Fehlermeldung gelöscht, Rechner war „%s“, Text war „%s“", - $oOldReport->getMachine(), $oOldReport->getText())); + db_query(sprintf("DELETE FROM mod_errorreporter WHERE er_uid = '%d';", + intval($nErrorReportID))); + erInsertLog(sprintf("Fehlermeldung gelöscht, Rechner war „%s“, Text war ". + "„%s“", $oOldReport->getMachine(), $oOldReport->getText())); } /** @@ -401,7 +461,8 @@ class erErrorReportManager { if(!$_SESSION["act"]) { return false; // user is not logged in } else { - $hQuery = db_query(sprintf("SELECT er_act FROM mod_errorreporter WHERE er_uid = %d;", intval($nID))); + $hQuery = db_query(sprintf("SELECT er_act FROM mod_errorreporter WHERE ". + "er_uid = %d;", intval($nID))); if(!is_resource($hQuery)) { setLastError(ER_ERROR_SQL); return false; @@ -411,4 +472,4 @@ class erErrorReportManager { } } } -?> \ No newline at end of file +?>