Skip to content

Commit

Permalink
Merge branch 'test-status', remote branch 'dbyrne/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
amalloy committed Apr 30, 2011
2 parents 0fb0650 + f4e70b6 commit dc59e50
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 30 deletions.
4 changes: 2 additions & 2 deletions resources/public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ span.error, div.error {
color: red;
}

div.congrats {
div.message {
color: red;
margin-bottom: 8px;
font-size: 16px;
Expand All @@ -176,7 +176,7 @@ div#prob-desc {
overflow: hidden;
}

div.testcases {
table.testcases {
font-size: 1.3em;
float: left;
}
Expand Down
Binary file added resources/public/images/bluelight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/public/images/greenlight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/public/images/redlight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 42 additions & 26 deletions src/foreclojure/problems.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns foreclojure.problems
(:use foreclojure.utils
[foreclojure.social :only [tweet-link gist!]]
[foreclojure.feeds :only [create-feed]]
(:use (foreclojure utils
[social :only [tweet-link gist!]]
[feeds :only [create-feed]])
[clojail core testers]
somnium.congomongo
(hiccup form-helpers page-helpers core)
Expand Down Expand Up @@ -29,6 +29,18 @@
:only [:_id :title :tags :times-solved]
:sort {:_id 1})))

(defn next-unsolved-problem [solved-problems]
(when-let [unsolved (->> (get-problem-list)
(remove (comp (set solved-problems) :_id))
(seq))]
(apply min-key :_id unsolved)))

(defn next-problem-link [completed-problem-id]
(when-let [{:keys [solved]} (get-user (session/session-get :user))]
(if-let [{:keys [_id title]} (next-unsolved-problem solved)]
(str "Now try <a href='/problem/" _id "'>" title "</a>!")
"You've solved them all! Come back later for more!")))

(defn get-recent-problems [n]
(map get-problem (map :_id (take-last n (get-problem-list)))))

Expand All @@ -54,10 +66,11 @@
(update! :users {:user user} {:$addToSet {:solved id}})
(update! :problems {:_id id} {:$inc {:times-solved 1}})
(send total-solved inc))
"Congratulations, you've solved the problem!")
(str "Congratulations, you've solved the problem!"
"<br />" (next-problem-link id)))
"You've solved the problem! If you log in we can track your progress.")]
(session/session-put! :code [id code])
(flash-msg (str message " " gist-link) "/problems")))
(flash-msg (str message " " gist-link) (str "/problem/" id))))

(def restricted-list '[use require in-ns future agent send send-off pmap pcalls])

Expand All @@ -69,27 +82,23 @@
(defn run-code [id raw-code]
(let [code (.trim raw-code)
{:keys [tests restricted]} (get-problem id)
sb-tester (get-tester restricted)]
sb-tester (get-tester restricted)
this-url (str "/problem/" id)]
(session/flash-put! :code code)
(if (empty? code)
(do
(session/flash-put! :code code)
(flash-error "Empty input is not allowed"
(str "/problem/" id)))
(flash-msg "Empty input is not allowed" this-url)
(try
(loop [[test & more] tests]
(loop [[test & more] tests
i 0]
(session/flash-put! :failing-test i)
(if-not test
(mark-completed id code)
(let [testcase (s/replace test "__" (str code))]
(if (sb sb-tester (safe-read testcase))
(recur more)
(do
(session/flash-put! :code code)
(flash-error "You failed the unit tests."
(str "/problem/" id)))))))
(recur more (inc i))
(flash-msg "You failed the unit tests." this-url)))))
(catch Exception e
(do
(session/flash-put! :code code)
(flash-error (.getMessage e) (str "/problem/" id))))))))
(flash-msg (.getMessage e) this-url))))))


(def-page code-box [id]
Expand All @@ -102,17 +111,25 @@
[:br]
[:div {:id "prob-desc"}
(problem :description)[:br]
[:div {:class "testcases"}
[:pre {:class "brush: clojure;gutter: false;toolbar: false;light: true"}
(for [test (:tests problem)]
(str test "\n"))]]
[:table {:class "testcases"}
(let [tests (:tests problem)]
(for [i (range (count tests))]
[:tr
[:td
(let [f (session/flash-get :failing-test)]
(cond (or (nil? f) (> i f)) [:img {:src "/images/bluelight.png"}]
(= i f) [:img {:src "/images/redlight.png"}]
:else [:img {:src "/images/greenlight.png"}]))]
[:td
[:pre {:class "brush: clojure;gutter: false;toolbar: false;light: true"}
(nth tests i)]]]))]
(if-let [restricted (problem :restricted)]
[:div {:id "restrictions"}
[:u "Special Restrictions"] [:br]
(map (partial vector :li) restricted)])]
[:div
[:b "Code which fills in the blank:" [:br]
[:span {:class "error"} (session/flash-get :error)]]]
[:div.message (session/flash-get :message)]
[:b "Code which fills in the blank:" [:br]]]
(form-to [:post "/run-code"]
(text-area {:id "code-box"
:spellcheck "false"}
Expand All @@ -122,7 +139,6 @@
[:button.large {:id "run-button" :type "submit"} "Run"])]))

(def-page problem-page []
[:div.congrats (session/flash-get :message)]
(link-to "/problems/rss" [:div {:class "rss"}])
[:table#problem-table.my-table
[:thead
Expand Down
7 changes: 5 additions & 2 deletions src/foreclojure/utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@
(walk/postwalk (transform-if float? int)
data))

(defn get-user [username]
(from-mongo
(fetch-one :users :where {:user username})))

(defmacro with-user [[user-binding] & body]
`(if-let [username# (session/session-get :user)]
(let [~user-binding (from-mongo
(fetch-one :users :where {:user username#}))]
(let [~user-binding (get-user username#)]
~@body)
[:span.error "You must " (link-to "/login" "Log in") " to do this."]))

Expand Down

0 comments on commit dc59e50

Please sign in to comment.