예제 #1
0
 private static function load_one($f)
 {
     if (!file_exists($f)) {
         return false;
     }
     $csv = new CsvParser(file_get_contents($f), CsvParser::TYPE_GUESS);
     $csv->set_comment_chars("%#");
     if ($req = $csv->next()) {
         if (array_search("name", $req) !== false) {
             $csv->set_header($req);
         } else {
             $csv->set_header(array("name", "html"));
             $csv->unshift($req);
         }
         while (($req = $csv->next()) !== false) {
             self::$messages[$req["name"]] = (object) $req;
         }
         return true;
     } else {
         return false;
     }
 }
예제 #2
0
파일: pset.php 프로젝트: kohler/peteramati
function upload_grades($pset, $text, $fname)
{
    global $Conf;
    assert($pset->gitless_grades);
    $csv = new CsvParser($text);
    $csv->set_header($csv->next());
    while ($line = $csv->next()) {
        if (($who = get($line, "github_username")) && $who !== "-") {
            $user = $Conf->user_by_query("github_username=?", [$who]);
        } else {
            if (($who = get($line, "seascode_username")) && $who !== "-") {
                $user = $Conf->user_by_query("seascode_username=?", [$who]);
            } else {
                if (($who = get($line, "huid")) && $who !== "-") {
                    $user = $Conf->user_by_query("huid=?", [$who]);
                } else {
                    if (($who = get($line, "username")) && $who !== "-") {
                        $user = $Conf->user_by_query("github_username=? or seascode_username=? order by github_username=? desc limit 1", [$who, $who, $who]);
                    } else {
                        if (($who = get($line, "email")) && $who !== "-") {
                            $user = $Conf->user_by_email($who);
                        } else {
                            if ($who = get($line, "name")) {
                                list($first, $last) = Text::split_name($who);
                                $user = $Conf->user_by_query("firstName like '?s%' and lastName=?", [$first, $last]);
                                if ($user && $user->firstName != $first && !str_starts_with($user->firstName, "{$first} ")) {
                                    $user = null;
                                }
                            } else {
                                continue;
                            }
                        }
                    }
                }
            }
        }
        if ($user) {
            if (!save_grades($user, $pset, null, $line, true)) {
                $Conf->errorMsg("no grades set for “" . htmlspecialchars($who) . "”");
            }
        } else {
            $Conf->errorMsg(htmlspecialchars($fname) . ":" . $csv->lineno() . ": unknown user “" . htmlspecialchars($who) . "”");
        }
    }
    return true;
}
예제 #3
0
파일: pset.php 프로젝트: benesch/peteramati
function upload_grades($pset, $text, $fname)
{
    global $Conf;
    assert($pset->gitless_grades);
    $csv = new CsvParser($text);
    $csv->set_header($csv->next());
    while ($line = $csv->next()) {
        if (@$line["seascode_username"]) {
            $who = $line["seascode_username"];
            $user = Contact::find_by_username($who);
        } else {
            if (@$line["email"]) {
                $who = $line["email"];
                $user = Contact::find_by_email($who);
            } else {
                if (@$line["name"]) {
                    $who = $line["name"];
                    list($first, $last) = Text::split_name($who);
                    $user = Contact::find_by_query("firstName like '" . sqlqtrim($first) . "%' and lastName='" . sqlqtrim($last) . "'");
                    if ($user && $user->firstName != $first && !str_starts_with($user->firstName, "{$first} ")) {
                        $user = null;
                    }
                } else {
                    continue;
                }
            }
        }
        if ($user) {
            if (!save_grades($user, $pset, null, $line, true)) {
                $Conf->errorMsg("no grades set for “" . htmlspecialchars($who) . "”");
            }
        } else {
            $Conf->errorMsg(htmlspecialchars($fname) . ":" . $csv->lineno() . ": unknown user “" . htmlspecialchars($who) . "”");
        }
    }
    return true;
}
예제 #4
0
function parseBulkFile($text, $filename)
{
    global $Conf;
    $text = cleannl($text);
    if (!is_valid_utf8($text)) {
        $text = windows_1252_to_utf8($text);
    }
    $filename = $filename ? "{$filename}:" : "line ";
    $success = array();
    if (!preg_match('/\\A[^\\r\\n]*(?:,|\\A)(?:user|email)(?:[,\\r\\n]|\\z)/', $text) && !preg_match('/\\A[^\\r\\n]*,[^\\r\\n]*,/', $text)) {
        $tarr = CsvParser::split_lines($text);
        foreach ($tarr as &$t) {
            if (($t = trim($t)) && $t[0] !== "#" && $t[0] !== "%") {
                $t = CsvGenerator::quote($t);
            }
            $t .= "\n";
        }
        unset($t);
        $text = join("", $tarr);
    }
    $csv = new CsvParser($text);
    $csv->set_comment_chars("#%");
    $line = $csv->next();
    if ($line && (array_search("email", $line) !== false || array_search("user", $line) !== false)) {
        $csv->set_header($line);
    } else {
        $csv->set_header(array("user"));
        $csv->unshift($line);
    }
    $cj_template = (object) array();
    $topic_revmap = array();
    foreach ($Conf->topic_map() as $id => $name) {
        $topic_revmap[strtolower($name)] = $id;
    }
    $unknown_topics = array();
    $errors = array();
    while (($line = $csv->next()) !== false) {
        $cj = clone $cj_template;
        foreach ($line as $k => $v) {
            $cj->{$k} = $v;
        }
        foreach (array("firstname" => "firstName", "first" => "firstName", "lastname" => "lastName", "last" => "lastName", "fullname" => "name", "fullName" => "name", "voice" => "voicePhoneNumber", "phone" => "voicePhoneNumber", "address1" => "addressLine1", "province" => "state", "region" => "state", "address2" => "addressLine2", "postalcode" => "zipCode", "zip" => "zipCode", "tags" => "contactTags") as $k => $x) {
            if (isset($cj->{$k}) && !isset($cj->{$x})) {
                $cj->{$x} = $cj->{$k};
            }
        }
        // thou shalt not set passwords by bulk update
        unset($cj->password, $cj->password_plaintext, $cj->new_password);
        if (isset($cj->name) && !isset($cj->firstName) && !isset($cj->lastName)) {
            list($cj->firstName, $cj->lastName) = Text::split_name($cj->name);
        }
        if (count($topic_revmap)) {
            foreach (array_keys($line) as $k) {
                if (preg_match('/^topic:\\s*(.*?)\\s*$/i', $k, $m)) {
                    if (($ti = @$topic_revmap[strtolower($m[1])]) !== null) {
                        $x = $line[$k];
                        if (strtolower($x) === "low") {
                            $x = -2;
                        } else {
                            if (strtolower($x) === "high") {
                                $x = 4;
                            } else {
                                if (!is_numeric($x)) {
                                    $x = 0;
                                }
                            }
                        }
                        if (!@$cj->topics) {
                            $cj->topics = (object) array();
                        }
                        $cj->topics->{$ti} = $x;
                    } else {
                        $unknown_topics[$m[1]] = true;
                    }
                }
            }
        }
        $cj->id = "new";
        $ustatus = new UserStatus(array("send_email" => true, "no_deprivilege_self" => true));
        if ($saved_user = save_user($cj, $ustatus, null, true)) {
            $success[] = "<a href=\"" . hoturl("profile", "u=" . urlencode($saved_user->email)) . "\">" . Text::user_html_nolink($saved_user) . "</a>";
        }
        foreach ($ustatus->error_messages() as $e) {
            $errors[] = "<span class='lineno'>" . $filename . $csv->lineno() . ":</span> " . $e;
        }
    }
    if (count($unknown_topics)) {
        $errors[] = "There were unrecognized topics (" . htmlspecialchars(commajoin($unknown_topics)) . ").";
    }
    if (count($success) == 1) {
        $successMsg = "Saved account " . $success[0] . ".";
    } else {
        if (count($success)) {
            $successMsg = "Saved " . plural($success, "account") . ": " . commajoin($success) . ".";
        }
    }
    if (count($errors)) {
        $errorMsg = "<div class='parseerr'><p>" . join("</p>\n<p>", $errors) . "</p></div>";
    }
    if (count($success) && count($errors)) {
        $Conf->confirmMsg($successMsg . "<br />{$errorMsg}");
    } else {
        if (count($success)) {
            $Conf->confirmMsg($successMsg);
        } else {
            if (count($errors)) {
                Conf::msg_error($errorMsg);
            } else {
                $Conf->warnMsg("Nothing to do.");
            }
        }
    }
    return count($errors) == 0;
}