├── .gitignore ├── LICENSE ├── README.md ├── create.html └── index.php /.gitignore: -------------------------------------------------------------------------------- 1 | conf.php 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Simon Forsberg 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeReview-Shield [![Code Review](http://www.zomis.net/codereview/shield/?qid=95459&dummy)](http://codereview.stackexchange.com/q/95459/31562) 2 | 3 | get a shields.io-like 'shield' for your Code Review question 4 | 5 | # Usage 6 | 7 | Add this to your GitHub repository `README.md` file, or anywhere else that supports markdown: 8 | 9 | [![Code Review](http://www.zomis.net/codereview/shield/?qid=qqqqq)](http://codereview.stackexchange.com/q/qqqqq/uuuuuuu) 10 | 11 | Replace `qqqqq` with the question id and `uuuuuuu` with your user id. 12 | 13 | The quickest way to create your shield is to use the [Code Review Shield creator](http://www.zomis.net/codereview/shield/create.html) and paste the 'share' link for your question. 14 | 15 | # Modes 16 | 17 | By default, the shield decides the mode depending on the status of the question. 18 | 19 | - If the question does not have any answers, the mode `score` is used with red background 20 | - If the question has answers but no accepted answers, the mode `answers` is used with orange background 21 | - If the question has an accepted answer, the mode `views` is used with green background 22 | 23 | You can specify the mode yourself in the URL by using `&mode=yourmode`, for example: 24 | 25 | ### score mode 26 | 27 | [![Code Review](http://www.zomis.net/codereview/shield/?qid=95459&mode=score)](http://codereview.stackexchange.com/q/95459/31562) 28 | 29 | [![Code Review](http://www.zomis.net/codereview/shield/?qid=95459&mode=score)](http://codereview.stackexchange.com/q/95459/31562) 30 | 31 | ### answers mode 32 | 33 | [![Code Review](http://www.zomis.net/codereview/shield/?qid=95459&mode=answers)](http://codereview.stackexchange.com/q/95459/31562) 34 | 35 | [![Code Review](http://www.zomis.net/codereview/shield/?qid=95459&mode=answers)](http://codereview.stackexchange.com/q/95459/31562) 36 | 37 | ### views mode 38 | 39 | [![Code Review](http://www.zomis.net/codereview/shield/?qid=95459&mode=views)](http://codereview.stackexchange.com/q/95459/31562) 40 | 41 | [![Code Review](http://www.zomis.net/codereview/shield/?qid=95459&mode=views)](http://codereview.stackexchange.com/q/95459/31562) 42 | -------------------------------------------------------------------------------- /create.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Make your own Code Review Shield 5 | 6 | 7 |

8 | Convert the 'share' link to a Code Review Shield. 9 | Input something like 10 |

http://codereview.stackexchange.com/q/95459/31562
11 |

12 |

Enter share link:

13 | 14 | 34 |

Resulting Code Review Shield markdown:

35 |
36 | 37 |
38 |

Resulting Code Review Shield HTML:

39 |
40 | 41 |
42 | 43 | 44 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | prepare($sql); 46 | $sql_params = array(); 47 | foreach ($dbfields as $field_name) { 48 | if (isset($question[$field_name])) { 49 | $sql_params[':' . $field_name] = $question[$field_name]; 50 | } else { 51 | $sql_params[':' . $field_name] = 0; 52 | } 53 | } 54 | $sql_params[':qid'] = $qid; 55 | $sql_params[':time'] = time(); 56 | $result = $stmt->execute($sql_params); 57 | if ($result) { 58 | useData($question); 59 | } else { 60 | die($stmt->errorInfo()); 61 | } 62 | return $json; 63 | } 64 | 65 | function useData($data) { 66 | $text = 'reviewed'; 67 | if (isset($data['accepted_answer_id']) && $data['accepted_answer_id'] != 0) { 68 | $color = '97ca00'; 69 | $mode = 'views'; 70 | } elseif ($data['answer_count'] >= 1) { 71 | $color = 'ff8000'; 72 | $right = $data['score'] . ' score'; 73 | $mode = 'answers'; 74 | } else { 75 | $color = 'e05d44'; 76 | $text = 'reviewing'; 77 | $mode = 'score'; 78 | } 79 | if (isset($_GET['mode'])) { 80 | $mode = $_GET['mode']; 81 | } 82 | $data['answers'] = $data['answer_count']; 83 | $data['views'] = $data['view_count']; 84 | $right = $data[$mode] . ' ' . $mode; 85 | 86 | header('Content-type: image/svg+xml; charset=utf-8'); 87 | header('Cache-Control: no-cache, private'); 88 | header('Vary: Accept'); 89 | $etag = $mode . $color . $data[$mode]; 90 | header('Etag: "' . $etag . '"'); 91 | 92 | $svg = << 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | $text 108 | $text 109 | $right 110 | $right 111 | 112 | 113 | END; 114 | echo $svg; 115 | } 116 | 117 | function showError($message) { 118 | header('Content-type: image/svg+xml; charset=utf-8'); 119 | header('Cache-Control: no-cache, private'); 120 | header('Vary: Accept'); 121 | $etag = md5($message); 122 | header('Etag: "' . $etag . '"'); 123 | 124 | $svg = << 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | $message 134 | $message 135 | 136 | 137 | END; 138 | echo $svg; 139 | } 140 | 141 | function dbOrAPI($qid, $db) { 142 | 143 | $sql = 'SELECT is_answered, favorite_count, answer_count, view_count, score, fetch_time, accepted_answer_id, question_id FROM cr_badge WHERE question_id = :qid;'; 144 | 145 | $stmt = $db->prepare($sql); 146 | $result = $stmt->execute(array(':qid' => $qid)); 147 | if ($result) { 148 | $row = $stmt->fetch(PDO::FETCH_ASSOC); 149 | if (!$row) { 150 | return fetchQuestion($qid, $db); 151 | } 152 | if (!$row['question_id']) { 153 | return fetchQuestion($qid, $db); 154 | } 155 | $time = $row['fetch_time']; 156 | if ($time < time() - 3600) { // if time was updated more than one hour ago 157 | // fetch data again 158 | fetchQuestion($qid, $db); 159 | } else { 160 | useData($row); 161 | } 162 | } else { 163 | return showError($stmt->errorInfo()); 164 | } 165 | } 166 | 167 | if (isset($_GET['qid'])) { 168 | $qid = $_GET['qid']; 169 | try { 170 | $db = new PDO($dbhostname, $dbuser, $dbpass); 171 | dbOrAPI($qid, $db); 172 | } catch (PDOException $e) { 173 | showError($e->getMessage()); 174 | } 175 | } else { 176 | showError('No qid set'); 177 | } 178 | --------------------------------------------------------------------------------