1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369:
<?php
class NotationService
{
private static $js_already_included = false;
private static $user;
private static $db_querier;
private static $lang;
public static function __static()
{
self::$user = AppContext::get_current_user();
self::$db_querier = PersistenceContext::get_querier();
self::$lang = LangLoader::get('main');
}
public static function display_static_image(Notation $notation)
{
$notation_scale = $notation->get_notation_scale();
if (!empty($notation_scale))
{
$template = new FileTemplate('framework/content/notation/notation.tpl');
$average_notes = $notation->get_average_notes();
$int = intval($average_notes);
$decimal = floatval('0.' . TextHelper::substr($average_notes, TextHelper::strpos($average_notes, '.') + 1));
for ($i = 1; $i <= $notation->get_notation_scale(); $i++)
{
$star_full = false;
$star_half = false;
$star_empty = false;
$width = 0;
if ($int >= $i || ($int+1 == $i && $decimal == 1)) {
$star_full = true;
$star_width = 'star-width-100';
}
else if ($int+1 == $i && $decimal >= 0.90) {
$star_full = true;
$star_width = 'star-width-90';
}
else if ($int+1 == $i && $decimal >= 0.75 && $decimal < 0.9) {
$star_full = true;
$star_width = 'star-width-75';
}
else if ($int+1 == $i && $decimal >= 0.5 && $decimal < 0.75) {
$star_half = true;
$star_width = 'star-width-50';
}
else if ($int+1 == $i && $decimal >= 0.25 && $decimal < 0.5) {
$star_half = true;
$star_width = 'star-width-25';
}
else if ($int+1 == $i && $decimal >= 0.1 && $decimal < 0.25) {
$star_empty = true;
$star_width = 'star-width-10';
}
else {
$star_empty = true;
$star_width = 'star-width-0';
}
$template->assign_block_vars('star', array(
'I' => $i,
'STAR_EMPTY' => $star_empty,
'STAR_HALF' => $star_half,
'STAR_FULL' => $star_full,
'STAR_WIDTH' => $star_width
));
}
$count_notes = $notation->get_number_notes();
$template->put_all(array(
'C_STATIC_DISPLAY' => true,
'C_NOTES' => $count_notes > 0,
'ID_IN_MODULE' => $notation->get_id_in_module(),
'NUMBER_NOTES' => $notation->get_number_notes(),
'AVERAGE_NOTES' => $average_notes,
'NOTATION_SCALE' => $notation->get_notation_scale(),
'L_NO_NOTE' => LangLoader::get_message('no_note', 'common'),
));
return $template->render();
}
else
{
throw new Exception('The notation scale is empty');
}
}
public static function display_active_image(Notation $notation)
{
$note_post = AppContext::get_request()->get_int('note', 0);
$id_post = AppContext::get_request()->get_int('id', 0);
if (!empty($note_post) && !empty($id_post))
{
$notation->set_id_in_module($id_post);
$notation->set_note($note_post);
self::register_notation($notation);
}
else
{
$template = new FileTemplate('framework/content/notation/notation.tpl');
$average_notes = $notation->get_average_notes();
$int = intval($average_notes);
$decimal = floatval('0.' . TextHelper::substr($average_notes, TextHelper::strpos($average_notes, '.') + 1));
for ($i = 1; $i <= $notation->get_notation_scale(); $i++)
{
$star_full = false;
$star_half = false;
$star_empty = false;
$width = 0;
if ($int >= $i || ($int+1 == $i && $decimal == 1)) {
$star_full = true;
$star_width = 'star-width-100';
}
else if ($int+1 == $i && $decimal >= 0.90) {
$star_full = true;
$star_width = 'star-width-90';
}
else if ($int+1 == $i && $decimal >= 0.75 && $decimal < 0.9) {
$star_full = true;
$star_width = 'star-width-75';
}
else if ($int+1 == $i && $decimal >= 0.5 && $decimal < 0.75) {
$star_half = true;
$star_width = 'star-width-50';
}
else if ($int+1 == $i && $decimal >= 0.25 && $decimal < 0.5) {
$star_half = true;
$star_width = 'star-width-25';
}
else if ($int+1 == $i && $decimal >= 0.1 && $decimal < 0.25) {
$star_empty = true;
$star_width = 'star-width-10';
}
else {
$star_empty = true;
$star_width = 'star-width-0';
}
$template->assign_block_vars('star', array(
'I' => $i,
'STAR_EMPTY' => $star_empty,
'STAR_HALF' => $star_half,
'STAR_FULL' => $star_full,
'STAR_WIDTH' => $star_width
));
}
$count_notes = $notation->get_number_notes();
$template->put_all(array(
'C_JS_NOT_ALREADY_INCLUDED' => !self::$js_already_included,
'C_NOTES' => $count_notes > 0,
'C_MORE_1_NOTES' => $count_notes > 1,
'CURRENT_URL' => REWRITED_SCRIPT,
'ID_IN_MODULE' => $notation->get_id_in_module(),
'NOTATION_SCALE' => $notation->get_notation_scale(),
'NUMBER_NOTES' => $count_notes,
'AVERAGE_NOTES' => $average_notes,
'ALREADY_NOTE' => $notation->user_already_noted(),
'L_NO_NOTE' => LangLoader::get_message('no_note', 'common'),
'L_AUTH_ERROR' => LangLoader::get_message('error.auth', 'status-messages-common'),
'L_ALREADY_NOTE' => self::$lang['already_vote'],
'L_NOTES' => LangLoader::get_message('notes', 'common'),
'L_NOTE' => LangLoader::get_message('note', 'common'),
'L_VALID_NOTE' => LangLoader::get_message('add_note', 'common')
));
self::$js_already_included = true;
return $template->render();
}
}
public static function update_notation_scale($module_name, $old_notation_scale, $new_notation_scale)
{
if ($old_notation_scale !== $new_notation_scale)
{
$coefficient = $new_notation_scale / $old_notation_scale;
self::$db_querier->inject("UPDATE " . DB_TABLE_AVERAGE_NOTES . " SET average_notes = average_notes * " . $coefficient . " WHERE module_name = '". $module_name . "'");
self::$db_querier->inject("UPDATE " . DB_TABLE_NOTE . " SET note = note * " . $coefficient . " WHERE module_name = '". $module_name . "'");
}
}
public static function delete_notes_id_in_module($module_name, $id_in_module)
{
try {
$condition = 'WHERE module_name=:module_name AND id_in_module=:id_in_module';
$parameters = array('module_name' => $module_name, 'id_in_module' => $id_in_module);
self::$db_querier->delete(DB_TABLE_AVERAGE_NOTES, $condition, $parameters);
self::$db_querier->delete(DB_TABLE_NOTE, $condition, $parameters);
} catch (MySQLQuerierException $e) {
}
}
public static function delete_notes_module($module_name)
{
try {
$condition = 'WHERE module_name=:module_name';
$parameters = array('module_name' => $module_name);
self::$db_querier->delete(DB_TABLE_AVERAGE_NOTES, $condition, $parameters);
self::$db_querier->delete(DB_TABLE_NOTE, $condition, $parameters);
} catch (MySQLQuerierException $e) {
}
}
public static function get_number_notes(Notation $notation)
{
try {
return self::$db_querier->get_column_value(DB_TABLE_AVERAGE_NOTES, 'number_notes', 'WHERE module_name = :module_name AND id_in_module = :id_in_module',
array('module_name' => $notation->get_module_name(), 'id_in_module' => $notation->get_id_in_module()));
} catch (RowNotFoundException $e) {
return 0;
}
}
public static function get_average_notes(Notation $notation)
{
try {
return self::$db_querier->get_column_value(DB_TABLE_AVERAGE_NOTES, 'average_notes', 'WHERE module_name = :module_name AND id_in_module = :id_in_module',
array('module_name' => $notation->get_module_name(), 'id_in_module' => $notation->get_id_in_module()));
} catch (RowNotFoundException $e) {
return 0;
}
}
public static function get_informations_note(Notation $notation)
{
try {
return self::$db_querier->select_single_row_query('SELECT average_notes, number_notes, (SELECT COUNT(*) FROM '. DB_TABLE_NOTE .'
WHERE user_id=:user_id AND module_name=:module_name AND id_in_module=:id_in_module) AS user_already_noted
FROM ' . DB_TABLE_AVERAGE_NOTES . '
WHERE module_name = :module_name AND id_in_module = :id_in_module', array(
'module_name' => $notation->get_module_name(),
'id_in_module' => $notation->get_id_in_module(),
'user_id' => $notation->get_user_id()
));
} catch (RowNotFoundException $e) {
return array(
'average_notes' => 0,
'number_notes' => 0,
'user_already_noted' => 0
);
}
}
private static function register_notation(Notation $notation)
{
if (self::$user->check_level(User::MEMBER_LEVEL))
{
$note_is_valid = $notation->get_note() >= 0 && $notation->get_note() <= $notation->get_notation_scale() ? true : false;
$member_already_notation = self::$db_querier->count(DB_TABLE_NOTE, 'WHERE user_id=:user_id AND module_name=:module_name AND id_in_module=:id_in_module', array(
'module_name' => $notation->get_module_name(),
'id_in_module' => $notation->get_id_in_module(),
'user_id' => $notation->get_user_id()
));
if (!$member_already_notation && $note_is_valid)
{
self::$db_querier->insert(DB_TABLE_NOTE, array(
'module_name' => $notation->get_module_name(),
'id_in_module' => $notation->get_id_in_module(),
'user_id' => $notation->get_user_id(),
'note' => $notation->get_note()
));
$condition = 'WHERE module_name=:module_name AND id_in_module=:id_in_module';
$parameters = array('module_name' => $notation->get_module_name(), 'id_in_module' => $notation->get_id_in_module());
$nbr_notes = self::$db_querier->count(DB_TABLE_AVERAGE_NOTES, $condition, $parameters);
if ($nbr_notes == 0)
{
self::$db_querier->insert(DB_TABLE_AVERAGE_NOTES, array(
'module_name' => $notation->get_module_name(),
'id_in_module' => $notation->get_id_in_module(),
'average_notes' => self::calculates_average_notes($notation),
'number_notes' => 1
));
}
else
{
self::$db_querier->update(DB_TABLE_AVERAGE_NOTES, array(
'average_notes' => self::calculates_average_notes($notation),
'number_notes' => self::get_number_notes($notation) + 1)
, $condition, $parameters);
}
}
}
else
{
DispatchManager::redirect(PHPBoostErrors::user_not_authorized());
}
}
private static function calculates_average_notes(Notation $notation)
{
try {
$result = self::$db_querier->select_rows(DB_TABLE_NOTE, array('note'), 'WHERE module_name=:module_name AND id_in_module=:id_in_module',
array('module_name' => $notation->get_module_name(), 'id_in_module' => $notation->get_id_in_module()));
$notes = 0;
while ($row = $result->fetch())
{
$notes += $row['note'];
}
$result->dispose();
return (round(($notes / $result->get_rows_count()) / 0.25) * 0.25);
} catch (RowNotFoundException $e) {
return 0;
}
}
}
?>