From 8b69c341c0ea4f2daa2b749bc78f9b3cf0ac8393 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Thu, 18 Mar 2010 03:47:12 +0000 Subject: [PATCH] rating MDL-21657 more changes based on Petr's feedback --- lib/db/upgrade.php | 32 +++++++++++--------------------- lib/outputrenderers.php | 8 ++++---- mod/glossary/db/upgrade.php | 2 ++ rating/index.php | 5 ++--- rating/lib.php | 30 +++++++++++++++--------------- rating/rate.php | 10 ++-------- version.php | 2 +- 7 files changed, 37 insertions(+), 52 deletions(-) diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 0e65bcfa422..ef0b489a40f 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -3051,7 +3051,7 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL"); upgrade_main_savepoint($result, 2010021800); } - if ($result && $oldversion < 2010031602) { + if ($result && $oldversion < 2010031800) { //drop the erroneously created ratings table $table = new xmldb_table('ratings'); if ($dbman->table_exists($table)) { @@ -3060,6 +3060,9 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL"); //create the rating table (replaces module specific rating implementations) $table = new xmldb_table('rating'); + if ($dbman->table_exists($table)) { + $dbman->drop_table($table); + } /// Adding fields to table rating $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); @@ -3086,7 +3089,7 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL"); $dbman->create_table($table); } - //migrate ratings out of the modules into the central rating table + //pull in the ratings from the modules into the central rating table //migrate forumratings //forum ratings only have a single time column so use it for both time created and modified @@ -3099,38 +3102,25 @@ INNER JOIN {forum} f on f.id=d.forum'; //migrate glossary_ratings //glossary ratings only have a single time column so use it for both time created and modified - $ratingssql = 'SELECT r.id AS rid, r.entryid AS itemid, r.rating, r.userid, r.time AS timecreated, r.time AS timemodified, g.id AS mid, g.scale -FROM {glossary_ratings} r INNER JOIN {glossary_entries} ge ON ge.id=r.entryid + $ratingssql = 'SELECT r.id AS rid, r.entryid AS itemid, r.rating, r.userid, r.time AS timecreated, r.time AS timemodified, g.scale, g.id AS mid +FROM {glossary_ratings} r +INNER JOIN {glossary_entries} ge ON ge.id=r.entryid INNER JOIN {glossary} g ON g.id=ge.glossaryid'; $result = $result && upgrade_module_ratings($ratingssql,'glossary'); //migrate data_ratings //data ratings didnt store time created and modified so Im using the times from the record the rating was attached to $ratingssql = 'SELECT r.id AS rid, r.recordid AS itemid, r.rating, r.userid, re.timecreated, re.timemodified, d.scale, d.id AS mid -FROM {data_ratings} r INNER JOIN {data_records} re ON r.recordid=re.id +FROM {data_ratings} r +INNER JOIN {data_records} re ON r.recordid=re.id INNER JOIN {data} d ON d.id=re.dataid'; $result = $result && upgrade_module_ratings($ratingssql,'data'); - - //todo set permissions based on current value of glossary.assessed //todo drop forum_ratings, data_ratings and glossary_ratings - upgrade_main_savepoint($result, 2010031602); + upgrade_main_savepoint($result, 2010031800); } - //upgrade.php was out of step with install.xml for 16 hours. Its theoretically possible someone could - //have done a fresh install during that time with version 2010031602 and wound up with a table called 'ratings' - if ($result && $oldversion < 2010031700) { - $table = new xmldb_table('ratings'); - - if ( $dbman->table_exists($table) ) { - $dbman->rename_table($table, 'rating'); - } - - upgrade_main_savepoint($result, 2010031700); - } - - return $result; } diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index e3fc138ee97..25e29116054 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -1245,7 +1245,7 @@ class core_renderer extends renderer_base { $strrate = get_string("rate", "rating"); $strratings = ''; //the string we'll return - if($rating->settings->permissions[RATING_VIEW] || $rating->settings->permissions[RATING_VIEW_ALL]) { + if($rating->settings->permissions->canview || $rating->settings->permissions->canviewall) { switch ($rating->settings->aggregationmethod) { case RATING_AGGREGATE_AVERAGE : $strratings .= get_string("aggregateavg", "forum"); @@ -1283,11 +1283,11 @@ class core_renderer extends renderer_base { $aggstr = "{$ratingstr} / $scalemax ({$rating->count}) "; - if ($rating->settings->permissions[RATING_VIEW_ALL]) { + if ($rating->settings->permissions->canviewall) { $link = new moodle_url("/rating/index.php?contextid={$rating->context->id}&itemid={$rating->itemid}&scaleid={$rating->scaleid}"); $action = new popup_action('click', $link, 'ratings', array('height' => 400, 'width' => 600)); $strratings .= $this->action_link($link, $aggstr, $action); - } else if ($rating->settings->permissions[RATING_VIEW_ALL]) { + } else if ($rating->settings->permissions->canview) { $strratings .= $aggstr; } } @@ -1295,7 +1295,7 @@ class core_renderer extends renderer_base { //todo andrew alter the below if to deny guest users the ability to post ratings. //Petr to define "guest" $formstart = null; - if($rating->settings->permissions[RATING_POST]) { + if($rating->settings->permissions->canrate) { //dont use $rating->userid below as it will be null if the user hasnt already rated the item $formstart = << diff --git a/mod/glossary/db/upgrade.php b/mod/glossary/db/upgrade.php index 5df0485653a..fa850d3ca26 100644 --- a/mod/glossary/db/upgrade.php +++ b/mod/glossary/db/upgrade.php @@ -257,6 +257,8 @@ function xmldb_glossary_upgrade($oldversion) { upgrade_mod_savepoint($result, 2009110800, 'glossary'); } + //todo andrew set rating permissions based on current value of glossary.assessed + return $result; } diff --git a/rating/index.php b/rating/index.php index 88e03b725cc..7c471b94f27 100644 --- a/rating/index.php +++ b/rating/index.php @@ -40,11 +40,10 @@ if ($sort !== 0) { } $PAGE->set_url($url); -$permissions = rating::get_rating_permissions(context); -if (!$permissions[RATING_VIEW]) { +if ( !has_capability(RATING_VIEW,$context) ) { print_error('noviewrate', 'rating'); } -if (!$permissions[RATING_VIEW_ALL] and $USER->id != $item->userid) { +if ( !has_capability(RATING_VIEW,$context) and $USER->id != $item->userid) { print_error('noviewanyrate', 'rating'); } diff --git a/rating/lib.php b/rating/lib.php index 93e3cca05e3..ee314b8b9e1 100644 --- a/rating/lib.php +++ b/rating/lib.php @@ -23,9 +23,9 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -define('RATING_VIEW','view'); -define('RATING_VIEW_ALL','viewall'); -define('RATING_POST','post'); +define('RATING_VIEW','moodle/rating:view'); +define('RATING_VIEW_ALL','moodle/rating:viewall'); +define('RATING_RATE','moodle/rating:rate'); define('RATING_UNSET_RATING', -999); @@ -69,6 +69,12 @@ class rating implements renderable { */ public $userid; + /** + * settings for this rating. Necessary to render the rating. + * @var stdclass + */ + public $settings; + /** * Constructor. * @param context $context the current context object @@ -243,8 +249,6 @@ class rating implements renderable { $ratingsrecords = $DB->get_records_sql($sql, $params); //now create the rating sub objects - $permissions = rating::get_rating_permissions($context); - $scaleobj = new stdClass(); $scalemax = null; @@ -268,12 +272,17 @@ class rating implements renderable { $scalemax = $scaleid; } + //should $settings and $settings->permissions be declared as proper classes? $settings = new stdclass(); //settings that are common to all ratings objects in this context $settings->scale = $scaleobj; //the scale to use now - $settings->permissions = $permissions; $settings->aggregationmethod = $aggregate; $settings->returnurl = $returnurl; + $settings->permissions = new stdclass(); + $settings->permissions->canview = has_capability(RATING_VIEW,$context); + $settings->permissions->canviewall = has_capability(RATING_VIEW_ALL,$context); + $settings->permissions->canrate = has_capability(RATING_RATE,$context); + $rating = null; foreach($items as $item) { $rating = null; @@ -317,13 +326,4 @@ class rating implements renderable { } return $items; } - - /** - * Static method that retrieves the current user's permissions to do with ratings - * @param context $contextid the current context - * @return returns the array of permissions - */ - public static function get_rating_permissions($context) { - return array(RATING_VIEW=>has_capability('moodle/rating:view',$context), RATING_VIEW_ALL=>has_capability('moodle/rating:viewall',$context), RATING_POST=>has_capability('moodle/rating:rate',$context)); - } } //end rating class definition \ No newline at end of file diff --git a/rating/rate.php b/rating/rate.php index 64805e5e432..66d4526a8d4 100644 --- a/rating/rate.php +++ b/rating/rate.php @@ -48,8 +48,7 @@ if( !isloggedin() && !$returnurl ){ //session has expired and its an ajax reques list($context, $course, $cm) = get_context_info_array($contextid); require_login($course, false, $cm); -$permissions = rating::get_rating_permissions($context); -if( !$permissions[RATING_POST] ) { +if( !has_capability(RATING_RATE,$context) ) { if( $returnurl ) { //if its a non-ajax request echo $OUTPUT->header(); echo get_string('ratepermissiondenied', 'ratings'); @@ -67,12 +66,7 @@ if( !$permissions[RATING_POST] ) { $userid = $USER->id; $PAGE->set_url('/lib/rate.php', array( - 'contextid'=>$contextid, - 'itemid'=>$itemid, - 'scaleid'=>$scaleid, - 'rating'=>$userrating, - 'userid'=>$userid, - 'returnurl'=>$returnurl, + 'contextid'=>$contextid )); //todo how can we validate the forum post,glossary entry or whatever id? diff --git a/version.php b/version.php index fee207cff05..ef1f291f775 100644 --- a/version.php +++ b/version.php @@ -6,7 +6,7 @@ // This is compared against the values stored in the database to determine // whether upgrades should be performed (see lib/db/*.php) - $version = 2010031700; // YYYYMMDD = date of the last version bump + $version = 2010031800; // YYYYMMDD = date of the last version bump // XX = daily increments $release = '2.0 dev (Build: 20100318)'; // Human-friendly version name