Simple status box for the sidebar.
[elgg_plugins.git] / ratings / lib.php
blob74f55eb8d889b3a162f44cd29db621770d5f6859
1 <?php
2 /*
3 Ratings plugin for Elgg
4 Initial author: Marcus Povey <marcus@dushka.co.uk>
5 */
7 function ratings_init()
9 global $CFG, $db, $metatags, $function, $template;
11 // Register javascript
12 $metatags .= "<script type=\"text/javascript\" src=\"{$CFG->wwwroot}mod/ratings/ratings.js\"><!-- ratings js --></script>";
14 // Set up the database
15 $tables = $db->Metatables();
16 if ((!in_array($CFG->prefix . "ratings", $tables)) || (!in_array($CFG->prefix . "ratingsdetails", $tables)))
18 if (file_exists($CFG->dirroot . "mod/ratings/$CFG->dbtype.sql"))
20 modify_database($CFG->dirroot . "mod/ratings/$CFG->dbtype.sql");
22 else
24 error("Error: Your database ($CFG->dbtype) is not yet fully supported by the Elgg ratings. See the mod/ratings directory.");
27 print_continue("index.php");
28 exit;
31 // If rating type not set then set a default rating TODO: Do this better
32 if ((!isset($CFG->ratings)) || (!is_array($CFG->ratings)))
33 $CFG->ratings = array();
35 if (!isset($CFG->ratings['default_type']))
36 $CFG->ratings['default_type'] = 'default';
38 // Add annotation support
39 display_set_display_annotation_function("file::file", "ratings_displayobjectannotations");
40 display_set_display_annotation_function("weblog_post::post", "ratings_displayobjectannotations");
41 display_set_display_annotation_function("mediastream::media", "ratings_displayobjectannotations");
44 // Now add hooks for various publish events
45 listen_for_event("file","publish","ratings_newobjectpublish");
47 listen_for_event("weblog_post","publish","ratings_newobjectpublish");
48 $function['weblog:post:links'][] = $CFG->dirroot . "mod/ratings/lib/weblog_post_links.php"; // Add ratings link to blog entry
50 listen_for_event("mediastream", "publish", "ratings_newobjectpublish"); // Listen to media stream events
52 // Template additions
53 $template['css'] .= file_get_contents($CFG->dirroot . "mod/ratings/css");
57 function ratings_pagesetup()
61 /** Enable ratings for a given object */
62 function ratings_enable($object_id, $object_type, $rating_type, $owner)
64 $dataobject = new stdClass;
65 $dataobject->object_id = $object_id;
66 $dataobject->object_type = $object_type;
67 $dataobject->owner = $owner;
68 $dataobject->ratingtype = $rating_type;
70 return @insert_record('ratings', $dataobject, false);
73 /** Store the rating a given user gives a rating object. Returns true or false */
74 function ratings_storerating($ident, $userid, $rating)
76 $dataobject = new stdClass;
77 $dataobject->rating_ident = $ident;
78 $dataobject->ratedby = $userid;
79 $dataobject->rating = $rating;
80 $dataobject->ts = time();
82 return @insert_record('ratingsdetails', $dataobject, false);
85 /** Hook listening for a publish event for an object and creating the appropriate entry in the ratings db. */
86 function ratings_newobjectpublish($object_type, $event, $object)
88 global $CFG;
90 if ($event == "publish")
92 if ($object_type=="file")
93 ratings_enable($object->ident, 'file::file', $CFG->ratings['default_type'], $object->owner);
95 if ($object_type=="weblog_post")
96 ratings_enable($object->ident, 'weblog_post::post', $CFG->ratings['default_type'], $object->owner);
98 if ($object_type=="mediastream")
99 ratings_enable($object->ident, "mediastream::media", $CFG->ratings['default_type'], $object->owner);
102 return $object;
105 /** Draw ratings values out in a specific way */
106 function ratings_drawrating($dbrow)
108 global $CFG;
110 $annotation = "";
112 if ($dbrow->ratingtype=='125')
114 // One to 5 rating based numbers of people rated
115 $result = get_record_sql("select ident, sum(rating) as rating, count(distinct ratedby) as ratedby from {$CFG->prefix}ratingsdetails where rating_ident={$dbrow->ident} group by rating_ident");
117 if ($result)
119 $sum = $result->rating;
120 $outof = (5 * (int)$result->ratedby);
121 $outoffive = (($sum / $outof) / 2) * 10;
123 $annotation .= sprintf(__gettext("%d users gave this an average rating %d/5"), $result->ratedby, $outoffive);
125 else
127 $annotation .= __gettext("Be the first to rate this!");
131 else
133 // Default rating (dig it): X number of people have dug this
134 $result = get_records('ratingsdetails','rating_ident', $dbrow->ident);
136 $num = 0;
137 if ($result)
138 $num = count($result);
140 if ((!$result) || ($num != 1))
141 $annotation .= $num . " " . __gettext("users rated this!");
142 else
143 $annotation .= $num . " " . __gettext("user rated this!");
146 return $annotation;
149 /** HACK: Output the given code as document.write */
150 function ratings_todocwrite($text)
152 $body = "";
153 foreach (explode("\n",addslashes($text)) as $line)
154 $body .= "document.write(\"" . trim($line) . "\");\n";
156 return $body;
159 /** Draw ratings form values out in a specific way */
160 function ratings_drawratingform($dbrow, $ratedby)
162 global $CFG;
164 $annotation = "";
166 $object_id = $dbrow->object_id;
167 $object_type = $dbrow->object_type;
168 $ident = $dbrow->ident;
169 $ratingtype = $dbrow->ratingtype;
170 $returnurl = urlencode($_SERVER['REQUEST_URI']);
172 // Common form elements
173 $frm_elements_common = <<< END
174 <input type="hidden" name="rating_ident" value="$ident" />
175 <input type="hidden" name="action" value="rating::add" />
176 <input type="hidden" name="object_id" value="{$object_id}" />
177 <input type="hidden" name="object_type" value="{$object_type}" />
178 <input type="hidden" name="ratedby" value="{$ratedby}" />
179 <input type="hidden" name="ratingtype" value="$ratingtype" />
180 END;
182 if ($dbrow->ratingtype=='125')
184 // One to 5 rating
185 $buttontxt = __gettext("Rate!");
186 $rate = <<< END
187 1 <input type="radio" name="rating" value="1" />
188 <input type="radio" name="rating" value="2" />
189 <input type="radio" name="rating" value="3" />
190 <input type="radio" name="rating" value="4" />
191 <input type="radio" name="rating" value="5" /> 5
192 END;
194 $ajaxy = <<< END
195 <div id="ratings_$object_id">
196 <div id="ratings_ajaxmessages_$object_id"></div>
197 <form id="ratingsfrm_$object_id">
198 $frm_elements_common
199 <span id="ratings_link_$object_id"><p>
200 $rate
201 <span style="cursor:hand; cursor:pointer" onclick="ratings_sendcomment('{$CFG->wwwroot}mod/ratings/rating_action.php','ratingsfrm_$object_id', $object_id)"> $buttontxt</span></p></span>
203 <input type="hidden" name="displaymode" value="xml" />
204 </form>
206 </div>
207 END;
208 $ajaxy = ratings_todocwrite($ajaxy);
210 $annotation .= <<< END
212 <script type="text/javascript">
213 <!--
214 $ajaxy
216 </script>
217 <noscript>
218 <form id="ratingsfrm_$object_id" action="{$CFG->wwwroot}mod/ratings/rating_action.php" action="POST">
219 $frm_elements_common
222 $rate
223 <input type="hidden" name="returnurl" value="$returnurl" />
224 <input type="submit" name="$buttontxt" value="$buttontxt" />
225 </p>
226 </form>
227 </noscript>
228 END;
230 else
232 // Default rating (dig it)
233 $buttontxt = __gettext("Rate this..");
234 $ajaxy = <<< END
235 <div id="ratings_$object_id">
236 <div id="ratings_ajaxmessages_$object_id"></div>
237 <span id="ratings_link_$object_id" style="cursor:hand; cursor:pointer" onclick="ratings_sendcomment('{$CFG->wwwroot}mod/ratings/rating_action.php','ratingsfrm_$object_id', $object_id)">$buttontxt</span>
238 <form id="ratingsfrm_$object_id">
239 $frm_elements_common
240 <input type="hidden" name="rating" value="1" />
241 <input type="hidden" name="displaymode" value="xml" />
242 </form>
243 </div>
244 END;
245 $ajaxy = ratings_todocwrite($ajaxy);
247 $annotation .= <<< END
249 <script type="text/javascript">
250 <!--
251 $ajaxy
253 </script>
254 <noscript>
255 <form id="ratingsfrm_$object_id" action="{$CFG->wwwroot}mod/ratings/rating_action.php" action="POST">
256 $frm_elements_common
257 <input type="hidden" name="rating" value="1" />
258 <input type="hidden" name="returnurl" value="$returnurl" />
259 <input type="submit" name="$buttontxt" value="$buttontxt" />
260 </form>
261 </noscript>
262 END;
265 return $annotation;
269 /** Have we rated this object before */
270 function ratings_ratedbefore($object_rated, $ratinguserid)
272 $result = get_record('ratingsdetails', 'rating_ident', $object_rated->ident, 'ratedby', $ratinguserid);
273 if ($result) return true;
275 return false;
278 /** Display rating & allow ratethis - draw form/alreadyrated */
279 function ratings_displayobjectannotations($object, $object_type, $view)
281 global $CFG, $db;
283 $object_id = $object->ident;
284 $annotation = "";
286 // Pull ratings details from db
287 $result = get_record('ratings','object_id', $object_id, 'object_type', $object_type);
289 if ($result)
291 $annotation .= "<div id=\"ratings\">";
293 // Add rating
294 $annotation .= ratings_drawrating($result);
296 // See if we have already rated this before
297 if (isLoggedin())
299 if (!ratings_ratedbefore($result, $_SESSION['userid']))
302 // Not rated before
303 $annotation .= ratings_drawratingform($result, $_SESSION['userid']);
307 $annotation .= "</div>";
311 // Return the annotation
312 return $annotation;