3 Ratings plugin for Elgg
4 Initial author: Marcus Povey <marcus@dushka.co.uk>
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");
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");
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
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)
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
);
105 /** Draw ratings values out in a specific way */
106 function ratings_drawrating($dbrow)
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");
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);
127 $annotation .= __gettext("Be the first to rate this!");
133 // Default rating (dig it): X number of people have dug this
134 $result = get_records('ratingsdetails','rating_ident', $dbrow->ident
);
138 $num = count($result);
140 if ((!$result) ||
($num != 1))
141 $annotation .= $num . " " . __gettext("users rated this!");
143 $annotation .= $num . " " . __gettext("user rated this!");
149 /** HACK: Output the given code as document.write */
150 function ratings_todocwrite($text)
153 foreach (explode("\n",addslashes($text)) as $line)
154 $body .= "document.write(\"" . trim($line) . "\");\n";
159 /** Draw ratings form values out in a specific way */
160 function ratings_drawratingform($dbrow, $ratedby)
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" />
182 if ($dbrow->ratingtype
=='125')
185 $buttontxt = __gettext("Rate!");
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
195 <div id
="ratings_$object_id">
196 <div id
="ratings_ajaxmessages_$object_id"></div
>
197 <form id
="ratingsfrm_$object_id">
199 <span id
="ratings_link_$object_id"><p
>
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" />
208 $ajaxy = ratings_todocwrite($ajaxy);
210 $annotation .= <<< END
212 <script type
="text/javascript">
218 <form id
="ratingsfrm_$object_id" action
="{$CFG->wwwroot}mod/ratings/rating_action.php" action
="POST">
223 <input type
="hidden" name
="returnurl" value
="$returnurl" />
224 <input type
="submit" name
="$buttontxt" value
="$buttontxt" />
232 // Default rating (dig it)
233 $buttontxt = __gettext("Rate this..");
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">
240 <input type
="hidden" name
="rating" value
="1" />
241 <input type
="hidden" name
="displaymode" value
="xml" />
245 $ajaxy = ratings_todocwrite($ajaxy);
247 $annotation .= <<< END
249 <script type
="text/javascript">
255 <form id
="ratingsfrm_$object_id" action
="{$CFG->wwwroot}mod/ratings/rating_action.php" action
="POST">
257 <input type
="hidden" name
="rating" value
="1" />
258 <input type
="hidden" name
="returnurl" value
="$returnurl" />
259 <input type
="submit" name
="$buttontxt" value
="$buttontxt" />
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;
278 /** Display rating & allow ratethis - draw form/alreadyrated */
279 function ratings_displayobjectannotations($object, $object_type, $view)
283 $object_id = $object->ident
;
286 // Pull ratings details from db
287 $result = get_record('ratings','object_id', $object_id, 'object_type', $object_type);
291 $annotation .= "<div id=\"ratings\">";
294 $annotation .= ratings_drawrating($result);
296 // See if we have already rated this before
299 if (!ratings_ratedbefore($result, $_SESSION['userid']))
303 $annotation .= ratings_drawratingform($result, $_SESSION['userid']);
307 $annotation .= "</div>";
311 // Return the annotation