]> git.mjollnir.org Git - moodle.git/commitdiff
New functions for login failure display
authormoodler <moodler>
Sun, 25 Jul 2004 13:47:38 +0000 (13:47 +0000)
committermoodler <moodler>
Sun, 25 Jul 2004 13:47:38 +0000 (13:47 +0000)
lib/datalib.php
lib/weblib.php

index 79eb6f6e97e093cd34b32b70c911094912a5619f..b9bb407914794c4ecac3571698f89b796bfa8566 100644 (file)
@@ -307,7 +307,7 @@ function count_records($table, $field1="", $value1="", $field2="", $value2="", $
 * @param    type description
 *
 */
-function count_records_select($table, $select="") {
+function count_records_select($table, $select="", $countitem="COUNT(*)") {
 
     global $CFG;
 
@@ -315,7 +315,7 @@ function count_records_select($table, $select="") {
         $select = "WHERE $select";
     }
 
-    return count_records_sql("SELECT COUNT(*) FROM $CFG->prefix$table $select");
+    return count_records_sql("SELECT $countitem FROM $CFG->prefix$table $select");
 }
 
 
@@ -571,7 +571,7 @@ function get_records_sql($sql) {
         }
         return false;
     }
-
+    
     if ( $rs->RecordCount() > 0 ) {
         if ($records = $rs->GetAssoc(true)) {
             foreach ($records as $key => $record) {
@@ -704,6 +704,33 @@ function get_field($table, $return, $field1, $value1, $field2="", $value2="", $f
     }
 }
 
+
+/**
+* Get a single field from a database record
+* 
+* longdesc
+*
+* @param    type description
+*/
+function get_field_sql($sql) {
+
+    global $db, $CFG;
+
+    $rs = $db->Execute($sql);
+    if (!$rs) {
+        if (isset($CFG->debug) and $CFG->debug > 7) {
+            notify($db->ErrorMsg()."<br /><br />$sql");
+        }
+        return false;
+    }
+
+    if ( $rs->RecordCount() == 1 ) {
+        return $rs->fields[0];
+    } else {
+        return false;
+    }
+}
+
 /**
 * Set a single field in a database record
 * 
@@ -2127,7 +2154,7 @@ function add_to_log($courseid, $module, $action, $url="", $info="", $cm=0, $user
         if (isset($USER->realuser)) {  // Don't log
             return;
         }
-        $userid = empty($USER->id) ? "" : $USER->id;
+        $userid = empty($USER->id) ? "0" : $USER->id;
     }
 
     $timenow = time();
@@ -2183,8 +2210,7 @@ function get_logs($select, $order="l.time DESC", $limitfrom="", $limitnum="", &$
         $order = "ORDER BY $order";
     }
 
-    $selectsql = "{$CFG->prefix}log l, {$CFG->prefix}user u WHERE $select";
-
+    $selectsql = "{$CFG->prefix}log l LEFT JOIN {$CFG->prefix}user u ON l.userid = u.id ".((strlen($select) > 0) ? "WHERE $select" : "");
     $totalcount = count_records_sql("SELECT COUNT(*) FROM $selectsql");
 
     return get_records_sql("SELECT l.*, u.firstname, u.lastname, u.picture 
@@ -2234,6 +2260,37 @@ function get_logs_userday($userid, $courseid, $daystart) {
                         GROUP BY hour ");
 }
 
+/**
+ * Returns an object with counts of failed login attempts
+ *
+ * Returns information about failed login attempts.  If the current user is 
+ * an admin, then two numbers are returned:  the number of attempts and the 
+ * number of accounts.  For non-admins, only the attempts on the given user
+ * are shown.
+ *
+ * @param mode      - admin, teacher or everybody
+ * @param username  - the username we are searching for
+ * @param lastlogin - the date from which we are searching
+ */
+
+function count_login_failures($mode, $username, $lastlogin) {
+
+    $select = "module='login' AND action='error' AND time > $lastlogin";
+
+    if (isadmin()) {    // Return information about all accounts
+        if ($count->attempts = count_records_select('log', $select)) {
+            $count->accounts = count_records_select('log', $select, 'COUNT(DISTINCT info)');
+            return $count;
+        }
+    } else if ($mode == 'everybody' or ($mode == 'teacher' and isteacher())) {
+        if ($count->attempts = count_records_select('log', "$select AND info = '$username'")) {
+            return $count;
+        }
+    }
+    return NULL;
+}
+
+
 /// GENERAL HELPFUL THINGS  ///////////////////////////////////
 
 /**
index c766fdeb1018f0c032370ef90bd16311d5a03e1d..317ffca1df0b0a909162655241ebce67795177ec 100644 (file)
@@ -976,6 +976,27 @@ function print_header ($title="", $heading="", $navigation="", $focus="", $meta=
             $menu = "<font size=\"2\"><a target=\"$CFG->framename\" href=\"$wwwroot/login/index.php\">".get_string("login")."</a></font>";
         }
     }
+    
+    if (isset($SESSION->justloggedin)) {
+        unset($SESSION->justloggedin);
+        if (!empty($CFG->displayloginfailures)) {
+            if (!empty($USER->username) and !isguest()) {
+                if ($count = count_login_failures($CFG->displayloginfailures, $USER->username, $USER->lastlogin)) {
+                    $menu .= '&nbsp;<font size="1">';
+                    if (empty($count->accounts)) {
+                        $menu .= get_string('failedloginattempts', '', $count);
+                    } else {
+                        $menu .= get_string('failedloginattemptsall', '', $count);
+                    }
+                    if (isadmin()) {
+                        $menu .= ' (<a href="'.$CFG->wwwroot.'/course/log.php'.
+                                             '?chooselog=1&id=1&modid=site_errors">'.get_string('logs').'</a>)';
+                    }
+                    $menu .= '</font>';
+                }
+            }
+        }
+    }
 
     // Add a stylesheet for the HTML editor
     $meta = "<style type=\"text/css\">@import url($CFG->wwwroot/lib/editor/htmlarea.css);</style>\n$meta\n";
@@ -2292,6 +2313,9 @@ function rebuildnolinktag($text) {
     return $text;
 }
 
+
+
+
 // ================================================
 // THREE FUNCTIONS MOVED HERE FROM course/lib.php
 // ================================================
@@ -2369,6 +2393,8 @@ function print_side_block_start($heading='', $attributes = array()) {
     echo '<tbody style="background-color: '.$THEME->cellcontent2.';"><tr><td class="sideblockmain">';
 }
 
+
+
 function print_side_block_end() {
     echo '</td></tr></tbody></table><br />';
     echo "\n";