Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merged files

  • Loading branch information...
commit cc4dea38d476c0d492ea89b9b72a96820363d0d6 2 parents 80661cd + c39af5b
David Davis authored
6 config.clj
View
@@ -4,6 +4,6 @@
:db-pwd nil
:host "smtp.googlemail.com"
:user "team@4clojure.com"
- :pass ""
- :problem-submission true
- :advanced-user-count 1}
+ :problem-submission false
+ :advanced-user-count 50
+ :pass ""}
44 resources/public/css/style.css
View
@@ -177,6 +177,8 @@ div.message {
color: red;
margin-bottom: 8px;
font-size: 16px;
+ width: 75%;
+ float: left;
}
span#prob-title {
@@ -340,11 +342,49 @@ button:active {
}
#golfgraph{
- width: 90%
+ width: 90%;
+ display: none;
}
#golfgraph img{
- height: 300px;
padding-top: 25px;
}
+#golf-scores{
+ color: black;
+ font-size: 16px;
+ width: 25%;
+ float: right;
+ background-color:#bbddee;
+ overflow: auto;
+ -moz-border-radius: 15px;
+ border-radius: 15px;
+ text-align: center;
+}
+
+#instruct{
+ font-weight: bold;
+ width: 100%;
+}
+
+#golfheader{
+ font-weight: bold;
+ background-color:#445599;
+ color: #fff;
+ text-align: center;
+ width: 100%;
+}
+
+#graph-link{
+ width: 100%;
+ text-align: center;
+ float: right;
+ font-weight: bold;
+ font-size: 12px;
+ padding-bottom: 5px;
+}
+
+a.graph-class :hover {color: #445599;}
+
+
+
20 resources/public/script/foreclojure.js
View
@@ -2,6 +2,7 @@ $(document).ready(function() {
configureDataTables();
configureCodeBox();
+ configureGolf();
$("form#run-code button#approve-button").live("click", function(e) {
e.preventDefault();
@@ -73,7 +74,24 @@ function configureCodeBox(){
$('#code').val(text);
});
}
+}
+
+function configureGolf(){
+ $('#graph-link').click(function() {
+ $('#code-div').toggle('fast', function() {
+ // Animation complete.
+ });
+ $('#golfgraph').toggle('fast', function() {
+ // Animation complete.
+ });
+ var text = $('#graph-link').html();
+ if (text && text == 'View Chart'){
+ $('#graph-link').html("View Code");
+ }else{
+ $('#graph-link').html("View Chart");
+ }
-
+
+});
}
2  src/foreclojure/core.clj
View
@@ -23,7 +23,7 @@
(add-index! :users [:user] :unique true)
(add-index! :users [[:solved -1]])
-; (reconcile-solved-count)
+(reconcile-solved-count)
(defroutes main-routes
(GET "/" [] (welcome-page))
17 src/foreclojure/data_set.clj
View
@@ -902,9 +902,22 @@ number of prime numbers."
:tags ["hard"]
:tests ["(= (__ [ [1]\n [2 4]\n [5 1 4]\n [2 3 4 5]])\n (+ 1 2 1 3)\n 7)"
"(= (__ [ [3]\n [2 4]\n [1 9 3]\n [9 9 2 4]\n [4 6 6 7 8]\n [5 7 3 5 1 4]])\n (+ 3 4 3 2 7 1)\n 20)"]})
+
+ (insert! :problems
+ {:_id 80
+ :title "Perfect Numbers"
+ :times-solved 0
+ :description "A number is \"perfect\" if the sum of its divisors equal the number itself. 6 is a perfect number because 1+2+3=6. Write a function which returns true for perfect numbers and false otherwise."
+ :approved true
+ :tags ["medium"]
+ :tests ["(= (__ 6) true)"
+ "(= (__ 7) false)"
+ "(= (__ 496) true)"
+ "(= (__ 500) false)"
+ "(= (__ 8128) true)"]})
(insert! :problems
- {:_id 80
+ {:_id 81
:title "Create an Equation"
:times-solved 0
:description "Write a function which takes three or more integers. Using these integers, your function should generate clojure code representing an equation. The following rules for the equation must be satisfied:\n\n 1. All integers must be used once and only once.\n 2. The order of the integers must be maintained when reading the equation left-to-right.\n 3. The only functions you may use are +, *, or =.\n 4. The equation must use the minimum number of parentheses.\n 5. If no satisfying equation exists, return nil."
@@ -923,7 +936,7 @@ number of prime numbers."
(insert! :problems
- {:_id 81
+ {:_id 82
:title "Power Set"
:times-solved 0
:description "A power set is the set of all subsets of a given set. Given a list, produce a set of sublists while preserving the order of elements."
113 src/foreclojure/problems.clj
View
@@ -94,29 +94,27 @@
{:_id _id}
{:$set {user-score-key score}}))))))
-(defn mark-completed [id code & [user approved]]
+(defn mark-completed [problem code & [user]]
(let [user (or user (session/session-get :user))
+ {:keys [_id approved]} problem
gist-link (html [:div.share
[:a.novisited {:href "/share/code"} "Share"]
" this solution with your friends!"])
-
message
- (cond (and user (not approved))
- (str "You've solved the unapproved problem. Now you can approve it!")
- (user)
- (do
- (when (not-any? #{id} (get-solved user))
- (update! :users {:user user} {:$addToSet {:solved id}})
- (update! :problems {:_id id} {:$inc {:times-solved 1}})
- (send total-solved inc))
- (record-golf-score! user id (code-length code))
- (str "Congratulations, you've solved the problem!"
- "<br />" (next-problem-link id))))
- else
- (str "You've solved the problem! If you "
- (login-link "log in") " we can track your progress.")]
- (session/session-put! :code [id code])
- (flash-msg (str message " " gist-link) (str "/problem/" id))))
+ (cond
+ (not approved) (str "You've solved the unapproved problem. Now you can approve it!")
+ user (do
+ (when (not-any? #{_id} (get-solved user))
+ (update! :users {:user user} {:$addToSet {:solved _id}})
+ (update! :problems {:_id _id} {:$inc {:times-solved 1}})
+ (send total-solved inc))
+ (record-golf-score! user _id (code-length code))
+ (str "Congratulations, you've solved the problem!"
+ "<br />" (next-problem-link _id)))
+ :else (str "You've solved the problem! If you "
+ (login-link "log in") " we can track your progress."))]
+ (session/session-put! :code [_id code])
+ (flash-msg (str message " " gist-link) (str "/problem/" _id))))
(def restricted-list '[use require in-ns future agent send send-off pmap pcalls])
@@ -134,7 +132,7 @@
(defn run-code [id raw-code]
(let [code (.trim raw-code)
- {:keys [tests restricted approved]} (get-problem id)
+ {:keys [tests restricted] :as problem} (get-problem id)
sb-tester (get-tester restricted)]
(session/flash-put! :code code)
(try
@@ -145,7 +143,7 @@
i 0]
(session/flash-put! :failing-test i)
(if-not test
- (mark-completed id code () approved)
+ (mark-completed problem code)
(let [testcase (s/replace test "__" user-forms)]
(if (sb sb-tester (first (read-string-safely testcase)))
(recur more (inc i))
@@ -176,29 +174,44 @@
(when settings
[:img {:src url}])))
+(defn render-golf-score []
+ (let [{:keys [id best score] :as settings}
+ (session/session-get :golf-chart)]
+ (when settings
+ [:div#golf-scores
+ [:p#golfheader (str "Code Golf Score: " score)]
+ [:a.graph-class {:href "#"
+ :onclick "return false"}
+ [:span#graph-link "View Chart"]]])))
+
(def-page code-box [id]
(let [{:keys [title tags description restricted tests approved user]}
(get-problem (Integer. id))]
[:div
- [:span {:id "prob-title"} (if approved title (str "Unapproved: " title))]
+ [:span#prob-title
+ (when-not approved
+ "Unapproved: ")
+ title]
[:hr]
- [:div {:id "tags"} "Tags: "
+ [:div#tags "Tags: "
(s/join " " tags)]
[:br]
- (if (not approved)
- [:div {:id "submitter"} "Submitted by: " user])
+ (when-not approved
+ [:div#submitter "Submitted by: " user])
[:br]
- [:div {:id "prob-desc"}
+ [:div#prob-desc
description[:br]
(render-test-cases tests)
(when restricted
- [:div {:id "restrictions"}
+ [:div#restrictions
[:u "Special Restrictions"] [:br]
(map (partial vector :li) restricted)])]
[:div
- [:div.message (session/flash-get :message)]
- [:b "Code which fills in the blank:" [:br]]]
- (form-to {:id "run-code"} [:post *url*]
+ [:div.message
+ [:span#message-text (session/flash-get :message)]]
+ (render-golf-score)]
+ (form-to [:post *url*]
+ [:p#instruct "Code which fills in the blank: "]
(text-area {:id "code-box"
:spellcheck "false"}
:code (session/flash-get :code))
@@ -207,14 +220,14 @@
(hidden-field :id id)
[:br]
[:button.large {:id "run-button" :type "submit"} "Run"]
- (if (not approved)
+ (when-not approved
[:span [:button.large {:id "reject-button"} "Reject"]
[:button.large {:id "approve-button"} "Approve"]]))
]))
(def-page problem-page []
[:div.message (session/flash-get :message)]
- [:div.error {:id "problems-error"} (session/flash-get :error)]
+ [:div#problems-error.error (session/flash-get :error)]
(link-to "/problems/rss" [:div {:class "rss"}])
[:table#problem-table.my-table
[:thead
@@ -243,7 +256,7 @@
(def-page unapproved-problem-page []
[:div.message (session/flash-get :message)]
- [:div.error {:id "problems-error"} (session/flash-get :error)]
+ [:div#problems-error.error (session/flash-get :error)]
[:table#unapproved-problems.my-table
[:thead
[:tr
@@ -292,22 +305,21 @@
(defn create-problem
"create a user submitted problem"
[title tags description code]
- (if (and (:problem-submission config)
- (>= (count (get-solved (session/session-get :user)))
- (:advanced-user-count config)))
- (do
- (mongo! :db :mydb)
- (insert! :problems
- {:_id (get-next-id)
- :title title
- :times-solved 0
- :description description
- :tags (s/split tags #"\s+")
- :tests (s/split-lines code)
- :user (session/session-get :user)
- :approved false})
- (flash-msg "Thank you for submitting a problem! Be sure to check back to see it posted." "/problems"))
- (flash-error "You are not authorized to submit a problem." "/problems")))
+ (let [user (session/session-get :user)]
+ (if (can-submit? user)
+ (do
+ (mongo! :db :mydb)
+ (insert! :problems
+ {:_id (get-next-id)
+ :title title
+ :times-solved 0
+ :description description
+ :tags (s/split tags #"\s+")
+ :tests (s/split-lines code)
+ :user user
+ :approved false})
+ (flash-msg "Thank you for submitting a problem! Be sure to check back to see it posted." "/problems"))
+ (flash-error "You are not authorized to submit a problem." "/problems"))))
(defn approve-problem [id]
"take a user submitted problem and approve it"
@@ -316,8 +328,9 @@
(update! :problems
{:_id id}
{:$set {:approved true}})
- (flash-msg (str "Problem " id " has been approved!") (str "/problem/" id)))
- (flash-error "You do not have access to this page" "/problems")))
+ (flash-msg (str "Problem " id " has been approved!")
+ (str "/problem/" id)))
+ (flash-error "You don't have access to this page" "/problems")))
(defn reject-problem [id]
"reject a user submitted problem by deleting it from the database"
2  src/foreclojure/register.clj
View
@@ -39,7 +39,7 @@
{:user lower-user
:pwd (.encryptPassword (StrongPasswordEncryptor.) pwd)
:email email})
- (session/session-put! :user user)
+ (session/session-put! :user lower-user)
(response/redirect "/"))
(flash-error why "/register"))))
135 src/foreclojure/utils.clj
View
@@ -86,72 +86,73 @@
"evenrow"
"oddrow")})
-(defn get-solved [user]
- (set
- (:solved (from-mongo
- (fetch-one :users
- :where {:user user}
- :only [:solved])))))
-
-(defn approver? [user]
- (:approver (from-mongo
- (fetch-one :users
- :where {:user user}
- :only [:approver]))))
-
-(defn html-doc [& body]
- (html
- (doctype :html5)
- [:html
- [:head
- [:title "4Clojure"]
- [:link {:rel "alternate" :type "application/atom+xml" :title "Atom" :href "http://4clojure.com/problems/rss"}]
- [:link {:rel "shortcut icon" :href "/favicon.ico"}]
- (include-js "/script/jquery-1.5.2.min.js" "/script/jquery.dataTables.min.js")
- (include-js "/script/foreclojure.js")
- (include-js "/script/xregexp.js" "/script/shCore.js" "/script/shBrushClojure.js")
- (include-js "/script/ace/src/ace.js" "/script/ace/src/theme-textmate.js" "/script/ace/src/mode-clojure.js")
- (include-css "/css/style.css" "/css/demo_table.css" "/css/shCore.css" "/css/shThemeDefault.css")
- [:style {:type "text/css"}
- ".syntaxhighlighter { overflow-y: hidden !important; }"]]
- [:script {:type "text/javascript"} "SyntaxHighlighter.all()"]
- [:body
- [:div#top
- [:a {:href "/"} [:img#logo {:src "/images/logo.png"}]]]
-
- [:div#content
- (if (session/session-get :user)
- [:div#account.header-option
- [:a {:href "/login/update"} "Account Settings"]])
- (if (approver? (session/session-get :user))
- [:div#manage-unapproved.header-option
- [:a {:href "/problems/unapproved"} "View Unapproved Problems"]])
- [:br]
- [:div#menu
- [:a.menu {:href "/"} "Main Page"]
- [:a.menu {:href "/problems"} "Problem List"]
- [:a.menu {:href "/users"} "Top Users"]
- [:a.menu {:href "/directions"} "Getting Started"]
- [:a.menu {:href "http://try-clojure.org"} "REPL"]
- [:a.menu {:href "http://clojuredocs.org"} "Docs"]
- (if (and (:problem-submission config)
- (>= (count (get-solved (session/session-get :user)))
- (:advanced-user-count config)))
- [:a.menu {:href "/problems/submit"} "Submit a Problem"])
- [:span#user-info
- (if-let [user (session/session-get :user)]
- [:div
- [:span#username (str "Logged in as " user )]
- [:a#logout {:href "/logout"} "Logout"]]
- [:div
- [:a#login {:href (login-url)} "Login"]
- [:a#register {:href "/register"} "Register"]])]]
- [:div#content_body body]
- [:div#footer
- "The content on 4clojure.com is available under the EPL v 1.0 license."
- [:a#contact {:href "mailto:team@4clojure.com"} "Contact us!"]]
- (javascript-tag
- " var _gaq = _gaq || [];
+(defn user-attribute [attr]
+ (fn [username]
+ (attr (from-mongo
+ (fetch-one :users
+ :where {:user username}
+ :only [attr])))))
+
+(def get-solved (comp set (user-attribute :solved)))
+(def approver? (user-attribute :approver))
+
+(defn can-submit? [username]
+ (and (:problem-submission config)
+ (>= (count (get-solved username))
+ (:advanced-user-count config))))
+
+(defn html-doc [& body]
+ (let [user (session/session-get :user)]
+ (html
+ (doctype :html5)
+ [:html
+ [:head
+ [:title "4Clojure"]
+ [:link {:rel "alternate" :type "application/atom+xml" :title "Atom" :href "http://4clojure.com/problems/rss"}]
+ [:link {:rel "shortcut icon" :href "/favicon.ico"}]
+ (include-js "/script/jquery-1.5.2.min.js" "/script/jquery.dataTables.min.js")
+ (include-js "/script/foreclojure.js")
+ (include-js "/script/xregexp.js" "/script/shCore.js" "/script/shBrushClojure.js")
+ (include-js "/script/ace/src/ace.js" "/script/ace/src/mode-clojure.js")
+ (include-css "/css/style.css" "/css/demo_table.css" "/css/shCore.css" "/css/shThemeDefault.css")
+ [:style {:type "text/css"}
+ ".syntaxhighlighter { overflow-y: hidden !important; }"]]
+ [:script {:type "text/javascript"} "SyntaxHighlighter.all()"]
+ [:body
+ [:div#top
+ [:a {:href "/"} [:img#logo {:src "/images/logo.png"}]]]
+
+ [:div#content
+ (when user
+ [:div#account.header-option
+ (link-to "/login/update" "Account Settings")]
+ (when (approver? user)
+ [:div#manage-unapproved.header-option
+ (link-to "/problems/unapproved" "View Unapproved Problems")]))
+ [:br]
+ [:div#menu
+ [:a.menu {:href "/"} "Main Page"]
+ [:a.menu {:href "/problems"} "Problem List"]
+ [:a.menu {:href "/users"} "Top Users"]
+ [:a.menu {:href "/directions"} "Getting Started"]
+ [:a.menu {:href "http://try-clojure.org"} "REPL"]
+ [:a.menu {:href "http://clojuredocs.org"} "Docs"]
+ (when (can-submit? user)
+ [:a.menu {:href "/problems/submit"} "Submit a Problem"])
+ [:span#user-info
+ (if user
+ [:div
+ [:span#username (str "Logged in as " user)]
+ [:a#logout {:href "/logout"} "Logout"]]
+ [:div
+ [:a#login {:href (login-url)} "Login"]
+ [:a#register {:href "/register"} "Register"]])]]
+ [:div#content_body body]
+ [:div#footer
+ "The content on 4clojure.com is available under the EPL v 1.0 license."
+ [:a#contact {:href "mailto:team@4clojure.com"} "Contact us!"]]
+ (javascript-tag
+ " var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-22844856-1']);
_gaq.push(['_trackPageview']);
@@ -161,4 +162,4 @@
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
"
- )]]]))
+ )]]])))
Please sign in to comment.
Something went wrong with that request. Please try again.