/** * testGenrateCSVToOutput * @dataProvider findDataProvider * */ public function testGenrateCSVToOutput($data, $fields, $options, $expected) { $options = array_merge($options, array('forceOutput' => true)); ob_start(); CsvGenerator::generate($data, $fields, $options); $result = ob_get_contents(); ob_end_clean(); $this->assertEquals($result, $expected); }
function run(Contact $user, $qreq, $ssel) { global $Conf, $Opt; // maybe download preferences for someone else $Rev = $user; if (($cid = cvtint($qreq->reviewer)) > 0 && $user->privChair) { if (!($Rev = Contact::find_by_id($cid))) { return Conf::msg_error("No such reviewer"); } } if (!$Rev->isPC) { return self::EPERM; } $q = $Conf->paperQuery($Rev, array("paperId" => $ssel->selection(), "topics" => 1, "reviewerPreference" => 1)); $result = Dbl::qe_raw($q); $texts = array(); while ($prow = PaperInfo::fetch($result, $Rev)) { $t = $prow->paperId . "," . CsvGenerator::quote($prow->title); if ($prow->conflictType > 0) { $t .= ",conflict"; } else { $t .= "," . unparse_preference($prow); } $t .= "\n"; if ($this->extended) { if ($Rev->can_view_authors($prow, false)) { $t .= prefix_word_wrap("# Authors: ", $prow->pretty_text_author_list(), "# "); } $t .= prefix_word_wrap("# Abstract: ", rtrim($prow->abstract), "# "); if ($prow->topicIds != "") { $t .= prefix_word_wrap("# Topics: ", $prow->unparse_topics_text(), "# "); } $t .= "\n"; } defappend($texts[$prow->paperId], $t); } downloadCSV(join("", $ssel->reorder($texts)), ["paper", "title", "preference"], "revprefs"); }
function downloadText($text, $filename, $inline = false) { global $Opt; $csvg = new CsvGenerator(CsvGenerator::TYPE_TAB); $csvg->download_headers($Opt["downloadPrefix"] . $filename . $csvg->extension(), !$inline); if ($text !== false) { $csvg->add($text); $csvg->download(); exit; } }
function download_psets_report($request) { global $Conf; $where = array(); $report = $request["report"]; $nonanonymous = false; foreach (explode(" ", strtolower($report)) as $rep) { if ($rep === "college") { $where[] = "not c.extension"; } else { if ($rep === "extension") { $where[] = "c.extension"; } else { if ($rep === "nonanonymous") { $nonanonymous = true; } } } } if (count($where)) { $where = array("(" . join(" or ", $where) . ")"); } $where[] = "(c.roles&" . Contact::ROLE_PCLIKE . ")=0"; $where[] = "not c.dropped"; $where = join(" and ", $where); $sel_pset = null; if (get($request, "pset") && !($sel_pset = $Conf->pset_by_key($request["pset"]))) { return $Conf->errorMsg("No such pset"); } $students = array(); if (isset($request["fields"])) { $selection = explode(",", $request["fields"]); } else { $selection = array("name", "grade", "username", "huid", "extension"); } $maxbyg = array(); $max = $max_noextra = 0; foreach ($Conf->psets() as $pset) { if (!$pset->disabled && (!$sel_pset || $sel_pset === $pset)) { collect_pset_info($students, $pset, $where, !!$sel_pset, $nonanonymous); if ($g = $pset->group) { if (!isset($maxbyg[$g])) { $maxbyg[$g] = $maxbyg["{$g}_noextra"] = 0; } foreach ($pset->grades as $ge) { if ($ge->max && !$ge->no_total) { $maxbyg[$g] += $ge->max; if (!$ge->is_extra) { $maxbyg["{$g}_noextra"] += $ge->max; } } } } } } foreach ($Conf->psets() as $pset) { if (!$pset->disabled && (!$sel_pset || $sel_pset === $pset)) { set_ranks($students, $selection, $pset->psetkey); if ($pset->has_extra) { set_ranks($students, $selection, $pset->psetkey . "_noextra"); } if ($sel_pset) { foreach ($pset->grades as $ge) { $selection[] = $ge->name; } } } } if (!$sel_pset) { set_ranks($students, $selection, "psets"); set_ranks($students, $selection, "psets_noextra"); set_ranks($students, $selection, "tests"); $m_noextra = $maxbyg["psets_noextra"]; $m_psets = $maxbyg["psets"]; $m_tests = $maxbyg["tests"]; foreach ($students as $s) { $s->performance = sprintf("%.1f", 100 * (0.9 * ($s->psets_noextra / $m_noextra) + 0.75 * ($s->psets / $m_psets) + 1.2 * ($s->tests / $m_tests))); } set_ranks($students, $selection, "performance"); } $csv = new CsvGenerator(); $csv->set_header($selection); $csv->set_selection($selection); foreach ($students as $s) { $csv->add($s); } $csv->download_headers("gradereport.csv"); $csv->download(); exit; }
function get_pcassignment_csv() { global $user_chair; list($header, $texts) = SearchAction::pcassignments_csv_data($user_chair, range(1, 30)); $csvg = new CsvGenerator(); $csvg->set_header($header); $csvg->set_selection($header); $csvg->add($texts); return $csvg->unparse(); }
public function unparse() { $csvg = new CsvGenerator(); $csvg->set_header($this->header, true); $csvg->set_selection($this->header); $csvg->add($this->data); return $csvg->unparse(); }
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; }
function run(Contact $user, $qreq, $ssel) { global $Conf; $result = Dbl::qe_raw($Conf->paperQuery($user, ["paperId" => $ssel->selection()])); $papers = []; while ($prow = PaperInfo::fetch($result, $user)) { if ($user->can_view_pdf($prow)) { $papers[$prow->paperId] = $prow; } } $csvg = downloadCSV(false, ["paper", "title", "pages", "format"], "formatcheck"); echo $csvg->headerline; $format = $Conf->setting_data("sub_banal", ""); foreach ($ssel->reorder($papers) as $prow) { $pages = "?"; if ($prow->mimetype == "application/pdf") { $cf = new CheckFormat(); $dtype = $prow->finalPaperStorageId ? DTYPE_FINAL : DTYPE_SUBMISSION; if ($cf->analyzePaper($prow->paperId, $dtype, $format)) { $format = array(); foreach (CheckFormat::$error_types as $en => $etxt) { if ($cf->errors & $en) { $format[] = $etxt; } } $format = empty($format) ? "ok" : join(",", $format); $pages = $cf->pages; } else { $format = "error"; } } else { $format = "notpdf"; } echo $prow->paperId, ",", CsvGenerator::quote($prow->title), ",", $pages, ",", CsvGenerator::quote($format), "\n"; ob_flush(); flush(); } exit; }
static function settags_api($user, $qreq, $prow) { global $Conf; if ($qreq->cancelsettags) { json_exit(["ok" => true]); } if ($prow && !$user->can_view_paper($prow)) { json_exit(["ok" => false, "error" => "No such paper."]); } // save tags using assigner $x = array("paper,action,tag"); if ($prow) { if (isset($qreq->tags)) { $x[] = "{$prow->paperId},tag,all#clear"; foreach (TagInfo::split($qreq->tags) as $t) { $x[] = "{$prow->paperId},tag," . CsvGenerator::quote($t); } } foreach (TagInfo::split((string) $qreq->addtags) as $t) { $x[] = "{$prow->paperId},tag," . CsvGenerator::quote($t); } foreach (TagInfo::split((string) $qreq->deltags) as $t) { $x[] = "{$prow->paperId},tag," . CsvGenerator::quote($t . "#clear"); } } else { if (isset($qreq->tagassignment)) { $pids = []; $pid = -1; foreach (preg_split('/[\\s,]+/', $qreq->tagassignment) as $w) { if ($w !== "" && ctype_digit($w)) { $pid = intval($w); } else { if ($w !== "" && $pid > 0) { $x[] = "{$pid},tag," . CsvGenerator::quote($w); $pids[$pid] = true; } } } } } $assigner = new AssignmentSet($user, $user->is_admin_force()); $assigner->parse(join("\n", $x)); $error = join("<br />", $assigner->errors_html()); $ok = $assigner->execute(); // exit if ($ok && $prow) { $prow->load_tags(); $treport = self::tagreport($user, $prow); if ($treport->warnings) { $Conf->warnMsg(join("<br>", $treport->warnings)); } $taginfo = (object) ["ok" => true, "pid" => $prow->paperId]; $prow->add_tag_info_json($taginfo, $user); json_exit($taginfo, true); } else { if ($ok) { $p = []; $result = Dbl::qe_raw($Conf->paperQuery($user, ["paperId" => array_keys($pids), "tags" => true])); while ($prow = PaperInfo::fetch($result, $user)) { $p[$prow->paperId] = (object) []; $prow->add_tag_info_json($p[$prow->paperId], $user); } json_exit(["ok" => true, "p" => $p]); } else { json_exit(["ok" => false, "error" => $error], true); } } }