Permalink
Browse files

MDL-27471 Adding mandatory component & ratingarea to the ratings API …

…+ other fixes

* Added the component and ratingarea fields and implemented it throughout
  the rating API as mandatory fields
* Cleanup rating indexes
* Upgrade forum/data/glossary ratings
* Moved the logic in the render_rating method to methods of the rating object.
* Added new callback for checking ratingareas
* Cleaned comments here and there
* Mark the xxx_get_participants methods as deprecated
* Refactor rate_ajax and ratingsuser_can_view_aggregate methods
* Cleaned up rating/index.php to use html_table object and moved inline styles to CSS.
* Added missing properties of the rating object that were being set throughout the rating
  API.
  • Loading branch information...
1 parent 7a01a2d commit 2b04c41c5894b6bd65501adb7674e7c6e26c0b36 Sam Hemelryk committed with stronk7 May 11, 2011
Showing with 1,404 additions and 896 deletions.
  1. +1 −1 course/moodleform_mod.php
  2. +1 −0 lang/en/error.php
  3. +7 −5 lib/db/install.xml
  4. +50 −0 lib/db/upgrade.php
  5. +42 −135 lib/outputrenderers.php
  6. +1 −1 lib/rsslib.php
  7. +2 −0 mod/assignment/lib.php
  8. +2 −1 mod/chat/lib.php
  9. +2 −0 mod/choice/lib.php
  10. +5 −3 mod/data/backup/moodle2/backup_data_stepslib.php
  11. +8 −0 mod/data/backup/moodle2/restore_data_stepslib.php
  12. +23 −0 mod/data/db/upgrade.php
  13. +80 −60 mod/data/lib.php
  14. +2 −2 mod/data/version.php
  15. +3 −2 mod/data/view.php
  16. +1 −0 mod/feedback/lib.php
  17. +2 −0 mod/folder/lib.php
  18. +5 −3 mod/forum/backup/moodle2/backup_forum_stepslib.php
  19. +8 −0 mod/forum/backup/moodle2/restore_forum_stepslib.php
  20. +22 −0 mod/forum/db/upgrade.php
  21. +142 −153 mod/forum/lib.php
  22. +30 −39 mod/forum/user.php
  23. +3 −5 mod/forum/version.php
  24. +0 −4 mod/forum/view.php
  25. +5 −3 mod/glossary/backup/moodle2/backup_glossary_stepslib.php
  26. +9 −0 mod/glossary/backup/moodle2/restore_glossary_stepslib.php
  27. +23 −0 mod/glossary/db/upgrade.php
  28. +3 −1 mod/glossary/deleteentry.php
  29. +86 −62 mod/glossary/lib.php
  30. +2 −2 mod/glossary/version.php
  31. +3 −3 mod/glossary/view.php
  32. +2 −0 mod/imscp/lib.php
  33. +2 −0 mod/label/lib.php
  34. +2 −2 mod/lesson/lib.php
  35. +2 −0 mod/page/lib.php
  36. +2 −2 mod/quiz/lib.php
  37. +2 −0 mod/resource/lib.php
  38. +2 −1 mod/survey/lib.php
  39. +2 −0 mod/url/lib.php
  40. +2 −0 mod/wiki/lib.php
  41. +2 −0 mod/workshop/lib.php
  42. +37 −32 rating/index.php
  43. +696 −301 rating/lib.php
  44. +27 −24 rating/rate.php
  45. +45 −48 rating/rate_ajax.php
  46. +5 −0 theme/base/style/core.css
  47. +1 −1 version.php
@@ -351,7 +351,7 @@ function standard_coursemodule_elements(){
if ($this->_features->rating) {
require_once($CFG->dirroot.'/rating/lib.php');
- $rm = new rating_manager();
+ $rm = new rating_manager();;
$mform->addElement('header', 'modstandardratings', get_string('ratings', 'rating'));
View
@@ -303,6 +303,7 @@
$string['invalidpasswordpolicy'] = 'Invalid password policy';
$string['invalidpaymentmethod'] = 'Invalid payment method: {$a}';
$string['invalidqueryparam'] = 'ERROR: Incorrect number of query parameters. Expected {$a->expected}, got {$a->actual}.';
+$string['invalidratingarea'] = 'Invalid rating area';
$string['invalidrecord'] = 'Can not find data record in database table {$a}.';
$string['invalidrecordunknown'] = 'Can not find data record in database.';
$string['invalidrequest'] = 'Invalid request';
View
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="lib/db" VERSION="20110209" COMMENT="XMLDB file for core Moodle tables"
+<XMLDB PATH="lib/db" VERSION="20110523" COMMENT="XMLDB file for core Moodle tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
@@ -2591,8 +2591,10 @@
<TABLE NAME="rating" COMMENT="moodle ratings" PREVIOUS="blog_external" NEXT="license">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="contextid"/>
- <FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="id" NEXT="itemid"/>
- <FIELD NAME="itemid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="contextid" NEXT="scaleid"/>
+ <FIELD NAME="contextid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="id" NEXT="component"/>
+ <FIELD NAME="component" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false" PREVIOUS="contextid" NEXT="ratingarea"/>
+ <FIELD NAME="ratingarea" TYPE="char" LENGTH="50" NOTNULL="true" SEQUENCE="false" PREVIOUS="component" NEXT="itemid"/>
+ <FIELD NAME="itemid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="ratingarea" NEXT="scaleid"/>
<FIELD NAME="scaleid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" SEQUENCE="false" PREVIOUS="itemid" NEXT="rating"/>
<FIELD NAME="rating" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="scaleid" NEXT="userid"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="false" PREVIOUS="rating" NEXT="timecreated"/>
@@ -2605,7 +2607,7 @@
<KEY NAME="userid" TYPE="foreign" FIELDS="userid" REFTABLE="user" REFFIELDS="id" COMMENT="Relates to user.id" PREVIOUS="contextid"/>
</KEYS>
<INDEXES>
- <INDEX NAME="itemid" UNIQUE="false" FIELDS="itemid"/>
+ <INDEX NAME="uniqueuserrating" UNIQUE="false" FIELDS="component, ratingarea, contextid, itemid" COMMENT="These fields define a unique user rating of an item"/>
</INDEXES>
</TABLE>
<TABLE NAME="license" COMMENT="store licenses used by moodle" PREVIOUS="rating" NEXT="registration_hubs">
@@ -2753,4 +2755,4 @@
</KEYS>
</TABLE>
</TABLES>
-</XMLDB>
+</XMLDB>
View
@@ -6062,6 +6062,56 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2011022100.01);
}
+ if ($oldversion < 2011052300.00) {
+ $table = new xmldb_table('rating');
+
+ // Add the component field to the ratings table
+ upgrade_set_timeout(60 * 20);
+ $field = new xmldb_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, 'unknown', 'contextid');
+ if (!$dbman->field_exists($table, $field)) {
+ $dbman->add_field($table, $field);
+ }
+
+ // Add the ratingarea field to the ratings table
+ upgrade_set_timeout(60 * 20);
+ $field = new xmldb_field('ratingarea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, 'unknown', 'component');
+ if (!$dbman->field_exists($table, $field)) {
+ $dbman->add_field($table, $field);
+ }
+
+ upgrade_main_savepoint(true, 2011052300.00);
+ }
+
+ if ($oldversion < 2011052300.01) {
+
+ // Define index uniqueuserrating (unique) to be added to rating
+ $table = new xmldb_table('rating');
+ $index = new xmldb_index('uniqueuserrating', XMLDB_INDEX_NOTUNIQUE, array('component', 'ratingarea', 'contextid', 'itemid'));
+
+ // Conditionally launch add index uniqueuserrating
+ if (!$dbman->index_exists($table, $index)) {
+ $dbman->add_index($table, $index);
+ }
+
+ // Main savepoint reached
+ upgrade_main_savepoint(true, 2011052300.01);
+ }
+
+ if ($oldversion < 2011052300.02) {
+
+ // Define index itemid (not unique) to be dropped form rating
+ $table = new xmldb_table('rating');
+ $index = new xmldb_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));
+
+ // Conditionally launch drop index itemid
+ if ($dbman->index_exists($table, $index)) {
+ $dbman->drop_index($table, $index);
+ }
+
+ // Main savepoint reached
+ upgrade_main_savepoint(true, 2011052300.02);
+ }
+
return true;
}
View
@@ -1410,95 +1410,38 @@ protected function render_pix_emoticon(pix_emoticon $emoticon) {
*/
function render_rating(rating $rating) {
global $CFG, $USER;
- static $havesetupjavascript = false;
- if( $rating->settings->aggregationmethod == RATING_AGGREGATE_NONE ){
+ if ($rating->settings->aggregationmethod == RATING_AGGREGATE_NONE) {
return null;//ratings are turned off
}
- $useajax = !empty($CFG->enableajax);
-
- //include required Javascript
- if( !$havesetupjavascript && $useajax ) {
- $this->page->requires->js_init_call('M.core_rating.init');
- $havesetupjavascript = true;
- }
-
- //check the item we're rating was created in the assessable time window
- $inassessablewindow = true;
- if ( $rating->settings->assesstimestart && $rating->settings->assesstimefinish ) {
- if ($rating->itemtimecreated < $rating->settings->assesstimestart || $rating->itemtimecreated > $rating->settings->assesstimefinish) {
- $inassessablewindow = false;
- }
- }
+ $ratingmanager = new rating_manager();
+ // Initialise the JavaScript so ratings can be done by AJAX.
+ $ratingmanager->initialise_rating_javascript($this->page);
$strrate = get_string("rate", "rating");
$ratinghtml = ''; //the string we'll return
- //permissions check - can they view the aggregate?
- $canviewaggregate = false;
-
- //if its the current user's item and they have permission to view the aggregate on their own items
- if ( $rating->itemuserid==$USER->id && $rating->settings->permissions->view && $rating->settings->pluginpermissions->view) {
- $canviewaggregate = true;
- }
-
- //if the item doesnt belong to anyone or its another user's items and they can see the aggregate on items they don't own
- //Note that viewany doesnt mean you can see the aggregate or ratings of your own items
- if ( (empty($rating->itemuserid) or $rating->itemuserid!=$USER->id) && $rating->settings->permissions->viewany && $rating->settings->pluginpermissions->viewany ) {
- $canviewaggregate = true;
- }
-
- if ($canviewaggregate==true) {
- $aggregatelabel = '';
- switch ($rating->settings->aggregationmethod) {
- case RATING_AGGREGATE_AVERAGE :
- $aggregatelabel .= get_string("aggregateavg", "rating");
- break;
- case RATING_AGGREGATE_COUNT :
- $aggregatelabel .= get_string("aggregatecount", "rating");
- break;
- case RATING_AGGREGATE_MAXIMUM :
- $aggregatelabel .= get_string("aggregatemax", "rating");
- break;
- case RATING_AGGREGATE_MINIMUM :
- $aggregatelabel .= get_string("aggregatemin", "rating");
- break;
- case RATING_AGGREGATE_SUM :
- $aggregatelabel .= get_string("aggregatesum", "rating");
- break;
- }
- $aggregatelabel .= get_string('labelsep', 'langconfig');
+ // permissions check - can they view the aggregate?
+ if ($rating->user_can_view_aggregate()) {
- //$scalemax = 0;//no longer displaying scale max
- $aggregatestr = '';
+ $aggregatelabel = $ratingmanager->get_aggregate_label($rating->settings->aggregationmethod);
+ $aggregatestr = $rating->get_aggregate_string();
- //only display aggregate if aggregation method isn't COUNT
- if ($rating->aggregate && $rating->settings->aggregationmethod!= RATING_AGGREGATE_COUNT) {
- if ($rating->settings->aggregationmethod!= RATING_AGGREGATE_SUM && is_array($rating->settings->scale->scaleitems)) {
- $aggregatestr .= $rating->settings->scale->scaleitems[round($rating->aggregate)];//round aggregate as we're using it as an index
- }
- else { //aggregation is SUM or the scale is numeric
- $aggregatestr .= round($rating->aggregate,1);
- }
+ $aggregatehtml = html_writer::tag('span', $aggregatestr, array('id' => 'ratingaggregate'.$rating->itemid)).' ';
+ $aggregatehtml .= html_writer::start_tag('span', array('id'=>"ratingcount{$rating->itemid}"));
+ if ($rating->count > 0) {
+ $aggregatehtml .= "({$rating->count})";
} else {
- $aggregatestr = '';
- }
-
- $countstr = html_writer::start_tag('span', array('id'=>"ratingcount{$rating->itemid}"));
- if ($rating->count>0) {
- $countstr .= "({$rating->count})";
+ $aggregatehtml .= '-';
}
- $countstr .= html_writer::end_tag('span');
-
- //$aggregatehtml = "{$ratingstr} / $scalemax ({$rating->count}) ";
- $aggregatehtml = "<span id='ratingaggregate{$rating->itemid}'>{$aggregatestr}</span> $countstr ";
+ $aggregatehtml .= html_writer::end_tag('span').' ';
$ratinghtml .= html_writer::tag('span', $aggregatelabel, array('class'=>'rating-aggregate-label'));
if ($rating->settings->permissions->viewall && $rating->settings->pluginpermissions->viewall) {
- $url = "/rating/index.php?contextid={$rating->context->id}&itemid={$rating->itemid}&scaleid={$rating->settings->scale->id}";
- $nonpopuplink = new moodle_url($url);
- $popuplink = new moodle_url("$url&popup=1");
+
+ $nonpopuplink = $rating->get_view_ratings_url();
+ $popuplink = $rating->get_view_ratings_url(true);
$action = new popup_action('click', $popuplink, 'ratings', array('height' => 400, 'width' => 600));
$ratinghtml .= $this->action_link($nonpopuplink, $aggregatehtml, $action);
@@ -1508,81 +1451,45 @@ function render_rating(rating $rating) {
}
$formstart = null;
- //if the item doesn't belong to the current user, the user has permission to rate
- //and we're within the assessable period
- if ($rating->itemuserid!=$USER->id
- && $rating->settings->permissions->rate
- && $rating->settings->pluginpermissions->rate
- && $inassessablewindow) {
-
- //start the rating form
- $formstart = html_writer::start_tag('form',
- array('id'=>"postrating{$rating->itemid}", 'class'=>'postratingform', 'method'=>'post', 'action'=>"{$CFG->wwwroot}/rating/rate.php"));
-
- $formstart .= html_writer::start_tag('div', array('class'=>'ratingform'));
-
- //add the hidden inputs
-
- $attributes = array('type'=>'hidden', 'class'=>'ratinginput', 'name'=>'contextid', 'value'=>$rating->context->id);
- $formstart .= html_writer::empty_tag('input', $attributes);
-
- $attributes['name'] = 'component';
- $attributes['value'] = $rating->settings->component;
- $formstart .= html_writer::empty_tag('input', $attributes);
-
- $attributes['name'] = 'itemid';
- $attributes['value'] = $rating->itemid;
- $formstart .= html_writer::empty_tag('input', $attributes);
-
- $attributes['name'] = 'scaleid';
- $attributes['value'] = $rating->settings->scale->id;
- $formstart .= html_writer::empty_tag('input', $attributes);
+ // if the item doesn't belong to the current user, the user has permission to rate
+ // and we're within the assessable period
+ if ($rating->user_can_rate()) {
- $attributes['name'] = 'returnurl';
- $attributes['value'] = $rating->settings->returnurl;
- $formstart .= html_writer::empty_tag('input', $attributes);
+ $rateurl = $rating->get_rate_url();
+ $inputs = $rateurl->params();
- $attributes['name'] = 'rateduserid';
- $attributes['value'] = $rating->itemuserid;
- $formstart .= html_writer::empty_tag('input', $attributes);
-
- $attributes['name'] = 'aggregation';
- $attributes['value'] = $rating->settings->aggregationmethod;
- $formstart .= html_writer::empty_tag('input', $attributes);
-
- $attributes['name'] = 'sesskey';
- $attributes['value'] = sesskey();;
- $formstart .= html_writer::empty_tag('input', $attributes);
+ //start the rating form
+ $formattrs = array(
+ 'id' => "postrating{$rating->itemid}",
+ 'class' => 'postratingform',
+ 'method' => 'post',
+ 'action' => $rateurl->out_omit_querystring()
+ );
+ $formstart = html_writer::start_tag('form', $formattrs);
+ $formstart .= html_writer::start_tag('div', array('class' => 'ratingform'));
+
+ // add the hidden inputs
+ foreach ($inputs as $name => $value) {
+ $attributes = array('type' => 'hidden', 'class' => 'ratinginput', 'name' => $name, 'value' => $value);
+ $formstart .= html_writer::empty_tag('input', $attributes);
+ }
if (empty($ratinghtml)) {
$ratinghtml .= $strrate.': ';
}
-
$ratinghtml = $formstart.$ratinghtml;
- //generate an array of values for numeric scales
- $scalearray = $rating->settings->scale->scaleitems;
- if (!is_array($scalearray)) { //almost certainly a numerical scale
- $intscalearray = intval($scalearray);//just in case they've passed "5" instead of 5
- $scalearray = array();
- if( is_int($intscalearray) && $intscalearray>0 ) {
- for($i=0; $i<=$rating->settings->scale->scaleitems; $i++) {
- $scalearray[$i] = $i;
- }
- }
- }
-
- $scalearray = array(RATING_UNSET_RATING => $strrate.'...') + $scalearray;
- $ratinghtml .= html_writer::select($scalearray, 'rating', $rating->rating, false, array('class'=>'postratingmenu ratinginput','id'=>'menurating'.$rating->itemid));
+ $scalearray = array(RATING_UNSET_RATING => $strrate.'...') + $rating->settings->scale->scaleitems;
+ $scaleattrs = array('class'=>'postratingmenu ratinginput','id'=>'menurating'.$rating->itemid);
+ $ratinghtml .= html_writer::select($scalearray, 'rating', $rating->rating, false, $scaleattrs);
//output submit button
-
$ratinghtml .= html_writer::start_tag('span', array('class'=>"ratingsubmit"));
- $attributes = array('type'=>'submit', 'class'=>'postratingmenusubmit', 'id'=>'postratingsubmit'.$rating->itemid, 'value'=>s(get_string('rate', 'rating')));
+ $attributes = array('type' => 'submit', 'class' => 'postratingmenusubmit', 'id' => 'postratingsubmit'.$rating->itemid, 'value' => s(get_string('rate', 'rating')));
$ratinghtml .= html_writer::empty_tag('input', $attributes);
- if (is_array($rating->settings->scale->scaleitems)) {
+ if (!$rating->settings->scale->isnumeric) {
$ratinghtml .= $this->help_icon_scale($rating->settings->scale->courseid, $rating->settings->scale);
}
$ratinghtml .= html_writer::end_tag('span');
View
@@ -280,7 +280,7 @@ function rss_add_items($items) {
$result .= rss_add_enclosures($item);
$result .= rss_full_tag('pubDate',3,false,gmdate('D, d M Y H:i:s',$item->pubdate).' GMT'); # MDL-12563
//Include the author if exists
- if (isset($item->author)) {
+ if (isset($item->author) && !empty($item->author)) {
//$result .= rss_full_tag('author',3,false,$item->author);
//We put it in the description instead because it's more important
//for moodle than most other feeds, and most rss software seems to ignore
View
@@ -2772,6 +2772,8 @@ function assignment_grade_item_delete($assignment) {
/**
* Returns the users with data in one assignment (students and teachers)
*
+ * @todo: deprecated - to be deleted in 2.2
+ *
* @param $assignmentid int
* @return array of user objects
*/
View
@@ -430,7 +430,8 @@ function chat_cron () {
* Returns the users with data in one chat
* (users with records in chat_messages, students)
*
- * @global object
+ * @todo: deprecated - to be deleted in 2.2
+ *
* @param int $chatid
* @param int $groupid
* @return array
View
@@ -591,6 +591,8 @@ function choice_delete_instance($id) {
* Returns the users with data in one choice
* (users with records in choice_responses, students)
*
+ * @todo: deprecated - to be deleted in 2.2
+ *
* @param int $choiceid
* @return array
*/
@@ -69,7 +69,7 @@ protected function define_structure() {
$ratings = new backup_nested_element('ratings');
$rating = new backup_nested_element('rating', array('id'), array(
- 'scaleid', 'value', 'userid', 'timecreated', 'timemodified'));
+ 'component', 'ratingarea', 'scaleid', 'value', 'userid', 'timecreated', 'timemodified'));
// Build the tree
$data->add_child($fields);
@@ -99,8 +99,10 @@ protected function define_structure() {
$content->set_source_table('data_content', array('recordid' => backup::VAR_PARENTID));
- $rating->set_source_table('rating', array('contextid' => backup::VAR_CONTEXTID,
- 'itemid' => backup::VAR_PARENTID));
+ $rating->set_source_table('rating', array('contextid' => backup::VAR_CONTEXTID,
+ 'itemid' => backup::VAR_PARENTID,
+ 'component' => 'mod_data',
+ 'ratingarea' => 'entry'));
$rating->set_source_alias('rating', 'value');
}
Oops, something went wrong.

0 comments on commit 2b04c41

Please sign in to comment.