Code source wiki de Page Ratings Macro
Modifié par Pascale STEIMETZ-LE CACHEUX le 2026/03/23 18:22
Afficher les derniers auteurs
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{velocity}} | ||
| 2 | #** | ||
| 3 | * This script is used to receive the ajax requests from the PageRatings macro | ||
| 4 | *# | ||
| 5 | ## Globals | ||
| 6 | #set($action = $request.action) | ||
| 7 | #set($docRef = $request.docRef) | ||
| 8 | #set($ratingVal = $request.ratingVal) | ||
| 9 | #set($pageRatingsDataSpace = 'PageRatings') | ||
| 10 | #set($ip = $request.getRemoteAddr()) | ||
| 11 | #set($user = $context.user) | ||
| 12 | #set($date = $datetool.getDate()) | ||
| 13 | #if("$!action" != '' && ($action == 'saveRatingEvent' || $action == 'checkPageRating' || $action == 'getRatedPages' || $action == 'pageRatingsCSVExport' || $action == 'clearRatingData')) | ||
| 14 | ## Save the rating | ||
| 15 | #if($action == 'saveRatingEvent' && "$!ratingVal" != '' && "$!docRef" != '' && $xwiki.exists($docRef)) | ||
| 16 | ## Check if the current user was already make the rating | ||
| 17 | ## 1) If yes : change the value of its rating | ||
| 18 | ## 2) If no : create a new document + object to save his ratingis | ||
| 19 | ## If the user is a Guest use his IP adresse to identify it otherwise use his username | ||
| 20 | #set($isNewRating = true) | ||
| 21 | #if($isGuest) | ||
| 22 | #set($condition = "rating.user='$user' and rating.docRef='$docRef' and rating.ip='$ip'") | ||
| 23 | #else | ||
| 24 | #set($condition = "rating.user='$user' and rating.docRef='$docRef'") | ||
| 25 | #end | ||
| 26 | #set($xwql = "from doc.object(PageRatingsCode.PageRatingsClass) as rating where $condition") | ||
| 27 | #set($results = $services.query.xwql($xwql).setLimit(1).execute()) | ||
| 28 | #if($results.size() > 0) | ||
| 29 | ## The user already made the rating | ||
| 30 | #set($isNewRating = false) | ||
| 31 | #set($ratingDocRef = $results.get(0)) | ||
| 32 | #set($ratingDoc = $xwiki.getDocument($ratingDocRef)) | ||
| 33 | #set($ratingObj = $ratingDoc.getObject("PageRatingsCode.PageRatingsClass")) | ||
| 34 | #else | ||
| 35 | ## New user | ||
| 36 | ## Create a new rating document with a PageRatingsClass object | ||
| 37 | #set($name = "rating${user}${docRef}") | ||
| 38 | #set($ratingDocRef = "${pageRatingsDataSpace}.${xwiki.getUniquePageName($pageRatingsDataSpace,$name)}") | ||
| 39 | #set($ratingDoc = $xwiki.getDocument($ratingDocRef)) | ||
| 40 | #set($ratingObj = $ratingDoc.newObject("PageRatingsCode.PageRatingsClass")) | ||
| 41 | #end | ||
| 42 | #if($isNewRating) | ||
| 43 | #set($discard = $ratingObj.set('docRef',$docRef)) | ||
| 44 | #set($discard = $ratingObj.set('user',$user)) | ||
| 45 | #set($discard = $ratingObj.set('date',$date)) | ||
| 46 | #set($discard = $ratingObj.set('ip',$ip)) | ||
| 47 | #end | ||
| 48 | #set($discard = $ratingObj.set('val',$ratingVal)) | ||
| 49 | #set($discard = $ratingDoc.saveWithProgrammingRights()) | ||
| 50 | {"ratingDocRef" : $ratingDoc.getDocumentReference(), "isNewRating" : $isNewRating} | ||
| 51 | #elseif($action == 'checkPageRating' && "$!docRef" != '' && $xwiki.exists($docRef)) | ||
| 52 | ## check if the current user make the rating | ||
| 53 | #set($ratingDone = false) | ||
| 54 | #if($isGuest) | ||
| 55 | #set($condition = "rating.user='$user' and rating.docRef='$docRef' and rating.ip='$ip'") | ||
| 56 | #else | ||
| 57 | #set($condition = "rating.user='$user' and rating.docRef='$docRef'") | ||
| 58 | #end | ||
| 59 | #set($xwql = "from doc.object(PageRatingsCode.PageRatingsClass) as rating where $condition") | ||
| 60 | #set($results = $services.query.xwql($xwql).setLimit(1).execute()) | ||
| 61 | #if($results.size() > 0) | ||
| 62 | #set($ratingDone = true) | ||
| 63 | #set($ratingDocRef = $results.get(0)) | ||
| 64 | #set($ratingDoc = $xwiki.getDocument("$ratingDocRef")) | ||
| 65 | #set($ratingObj = $ratingDoc.getObject("PageRatingsCode.PageRatingsClass")) | ||
| 66 | #set($rval = $ratingObj.val) | ||
| 67 | #end | ||
| 68 | {"ratingDone" : $ratingDone, "ratingVal" : "$!rval"} | ||
| 69 | #elseif($action == 'getRatedPages') | ||
| 70 | ## Get rated pages | ||
| 71 | #set($offset = $util.parseInt($request.get('offset'))) | ||
| 72 | #set($offset = $offset - 1) | ||
| 73 | #if($offset < 0) | ||
| 74 | #set($offset = 0) | ||
| 75 | #end | ||
| 76 | #set($limit = $util.parseInt($request.get('limit'))) | ||
| 77 | #set($filterValue = "$!{request.get('page')}") | ||
| 78 | #set($isFiltred = false) | ||
| 79 | #if("$!filterValue" != '') | ||
| 80 | #set($isFiltred = true) | ||
| 81 | #set($filterCond = " and doc.fullName like :filterValue") | ||
| 82 | #end | ||
| 83 | #set($xwql = "where doc.content like :macroName and doc.fullName <> 'PageRatingsCode.PageRatingsMacro' $!filterCond order by doc.title") | ||
| 84 | #set($query = $services.query.xwql($xwql).bindValue("macroName","%{{likeButton%%/}}%")) | ||
| 85 | #if($isFiltred) | ||
| 86 | #set($query = $query.bindValue("filterValue","%$filterValue%")) | ||
| 87 | #end | ||
| 88 | #set($totalRows = $query.count()) | ||
| 89 | #set($results = $query.setLimit($limit).setOffset($offset).execute()) | ||
| 90 | #set($returnedRows = $results.size()) | ||
| 91 | ## === | ||
| 92 | ## JSON | ||
| 93 | ## === | ||
| 94 | #set($rows = []) | ||
| 95 | #foreach($page in $results) | ||
| 96 | #set($csvExportURL = $xwiki.getURL("PageRatingsCode.PageRatingsMacro","view","action=pageRatingsCSVExport&docRef=${page}&xpage=plain&outputSyntax=plain")) | ||
| 97 | #set($clearRatingDataURL = $xwiki.getURL("PageRatingsCode.PageRatingsMacro","view","action=clearRatingData&docRef=${page}&xpage=plain&outputSyntax=plain")) | ||
| 98 | #set($htmlLink = "<a href='$csvExportURL'>Export CSV</a>") | ||
| 99 | #if($hasAdmin) | ||
| 100 | #set($htmlLink = "${htmlLink} | <a href='javascript:;' rel='$clearRatingDataURL' class='clearRatingData'>Clear rating data</a>") | ||
| 101 | #end | ||
| 102 | #set($row = {"doc_viewable" : true,"page" : $page, "actions" : $htmlLink}) | ||
| 103 | #set($discard = $rows.add($row)) | ||
| 104 | #end | ||
| 105 | { | ||
| 106 | "totalrows": $totalRows, | ||
| 107 | "matchingtags": {}, | ||
| 108 | "tags" : [], | ||
| 109 | "returnedrows": $returnedRows, | ||
| 110 | "offset": ($offset + 1), | ||
| 111 | "reqNo": $util.parseInt($request.reqNo), | ||
| 112 | "rows": $jsontool.serialize($rows) | ||
| 113 | } | ||
| 114 | #elseif($action == 'pageRatingsCSVExport') | ||
| 115 | #if("$!docRef" != '' && $xwiki.exists($docRef)) | ||
| 116 | ## Export data for a specific page | ||
| 117 | #set($exportAll = false) | ||
| 118 | #set($xwql = "from doc.object(PageRatingsCode.PageRatingsClass) as rating where rating.docRef='$docRef' order by rating.val desc, rating.date desc") | ||
| 119 | #set($pageDoc = $xwiki.getDocument($docRef)) | ||
| 120 | #set($csvFileName = "${pageDoc.space}.${pageDoc.name}-pageRatings.csv") | ||
| 121 | #else | ||
| 122 | ## Export for all pages | ||
| 123 | #set($exportAll = true) | ||
| 124 | #set($xwql = "from doc.object(PageRatingsCode.PageRatingsClass) as rating where doc.fullName <> 'PageRatingsCode.PageRatingsTemplate' and rating.docRef <> '' order by rating.docRef, rating.val desc, rating.date desc") | ||
| 125 | #set($csvFileName = "allPagesRatings.csv") | ||
| 126 | #end | ||
| 127 | #set($results = $services.query.xwql($xwql).execute()) | ||
| 128 | #set($discard = $response.setContentType("text/csv")) | ||
| 129 | #set($discard = $response.setHeader("Content-disposition", "attachment; filename=$csvFileName")) | ||
| 130 | Page;User;Rating;Date | ||
| 131 | #foreach($rating in $results) | ||
| 132 | #set($ratingDoc = $xwiki.getDocument($rating)) | ||
| 133 | #set($ratingObj = $ratingDoc.getObject('PageRatingsCode.PageRatingsClass')) | ||
| 134 | #set($ratingVal = "Like") | ||
| 135 | #if($!ratingObj.val == "0") | ||
| 136 | #set($ratingVal = "Unlike") | ||
| 137 | #end | ||
| 138 | ## Insert an empty line before each rated page | ||
| 139 | #if($ratedDoc != $ratingObj.docRef) | ||
| 140 | #if("$!ratedDoc" != '') | ||
| 141 | "";"";"";"" | ||
| 142 | #end | ||
| 143 | #set($ratedDoc = $ratingObj.docRef) | ||
| 144 | #end | ||
| 145 | "$!ratingObj.docRef";"$!ratingObj.user";"$!ratingVal";"$!ratingObj.date" | ||
| 146 | #end | ||
| 147 | #elseif($action == "clearRatingData" && "$!docRef" != '' && $xwiki.exists($docRef)) | ||
| 148 | ## Remove all ratings documents related to a page | ||
| 149 | #if($hasAdmin) | ||
| 150 | #set($xwql = "from doc.object(PageRatingsCode.PageRatingsClass) as rating where rating.docRef='$docRef' order by rating.val desc, rating.date desc") | ||
| 151 | #set($results = $services.query.xwql($xwql).execute()) | ||
| 152 | #foreach($page in $results) | ||
| 153 | #if("$!page" != '' && $xwiki.exists($page)) | ||
| 154 | #set($discard = $xwiki.getDocument($page).delete()) | ||
| 155 | #end | ||
| 156 | #end | ||
| 157 | #end | ||
| 158 | #end | ||
| 159 | #end | ||
| 160 | {{/velocity}} |