Skip to content

Commit df813ac

Browse files
committed
Merge pull request 4clojure#235 from hans/develop
Show solutions from top users on problem solutions page
2 parents b2fba69 + d0b9983 commit df813ac

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

src/foreclojure/problems.clj

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
[foreclojure.template :only [def-page content-page]]
1212
[foreclojure.social :only [tweet-link]]
1313
[foreclojure.feeds :only [create-feed]]
14-
[foreclojure.users :only [golfer? get-user-id disable-codebox?]]
14+
[foreclojure.users :only [golfer? get-user-id disable-codebox? get-ranked-users]]
1515
[foreclojure.solutions :only [save-solution get-solution]]
1616
[clojail.core :exclude [safe-read]]
1717
[clojail.testers :only [secure-tester blacklist-symbols]]
@@ -338,35 +338,57 @@ Return a map, {:message, :error, :url, :num-tests-passed}."
338338
" to view unapproved problems")))
339339
(error "No such problem!"))))
340340

341+
(defn solution-block [username code]
342+
[:div.follower-solution
343+
[:div.solution-username
344+
(link-to (str "/user/" username) username)
345+
"'s solution:"]
346+
[:pre.solution-code
347+
(escape-html code)]])
348+
341349
(def-page show-solutions-page [problem-id]
342350
{:title "4Clojure - Problem Solutions"
343351
:content
344352
(list
345353
[:div.message (session/flash-get :message)]
346354
[:div#problems-error.error (session/flash-get :error)]
347-
[:h3 {:style "margin-top: -20px;"} "Solutions:"]
355+
[:h3 {:style "margin-top: -20px;"} "Your solution"]
348356
(with-user [{:keys [_id following]}]
349357
(list
350358
(let [user-code (get-solution :public _id problem-id)]
351359
[:pre.solution-code.solution-user-code
352360
(escape-html user-code)])
353-
(if (empty? following)
354-
[:p "You can only see solutions of users whom you follow. Click on any name from the " (link-to "/users" "users") " listing page to see their profile, and click follow from there."]
355-
(if (some (complement nil?) (map #(get-solution :public % problem-id) following))
356-
(interpose [:hr.solution]
357-
(for [f-user-id following
358-
:let [f-user (:user (from-mongo
359-
(fetch-one :users
360-
:where {:_id f-user-id}
361-
:only [:user])))
362-
f-code (get-solution :public
363-
f-user-id problem-id)]
364-
:when f-code]
365-
[:div.follower-solution
366-
[:div.solution-username (str f-user "'s solution:")]
367-
[:pre.solution-code
368-
(escape-html f-code)]]))
369-
[:p "None of the users you follow have solved this problem yet!"])))))})
361+
362+
[:h3 "Your followers' solutions"]
363+
(if (some (complement nil?) (map #(get-solution :public % problem-id) following))
364+
(interpose
365+
[:hr.solution]
366+
(for [f-user-id following
367+
:let [f-user (:user (from-mongo
368+
(fetch-one :users
369+
:where {:_id f-user-id}
370+
:only [:user])))
371+
f-code (get-solution :public
372+
f-user-id problem-id)]
373+
:when f-code]
374+
(solution-block f-user f-code)))
375+
[:p "None of the users you follow have solved this problem yet!"])
376+
377+
[:h3 "Top users' solutions"]
378+
[:p "These solutions were written by some of the top users of 4Clojure."]
379+
(interpose
380+
[:hr.solution]
381+
(let [top-users (take 15 (get-ranked-users))
382+
top-users-m (zipmap (map :_id top-users) top-users)
383+
384+
solns (fetch :solutions
385+
:where {:user {:$in (map :_id top-users)}
386+
:code {:$ne nil}}
387+
:limit 5)]
388+
(for [soln solns
389+
:let [username (:user (top-users-m (:user soln)))
390+
code (:code soln)]]
391+
(solution-block username code)))))))})
370392

371393
(defn show-solutions [id]
372394
(let [problem-id (Integer. id)

0 commit comments

Comments
 (0)