Skip to content

Commit

Permalink
MDL-27471 Adding mandatory component & ratingarea to the ratings API …
Browse files Browse the repository at this point in the history
…+ 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
Sam Hemelryk authored and stronk7 committed May 23, 2011
1 parent 7a01a2d commit 2b04c41
Show file tree
Hide file tree
Showing 47 changed files with 1,404 additions and 896 deletions.
2 changes: 1 addition & 1 deletion course/moodleform_mod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down
1 change: 1 addition & 0 deletions lang/en/error.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
12 changes: 7 additions & 5 deletions lib/db/install.xml
Original file line number Diff line number Diff line change
@@ -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"
>
Expand Down Expand Up @@ -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"/>
Expand All @@ -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">
Expand Down Expand Up @@ -2753,4 +2755,4 @@
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>
50 changes: 50 additions & 0 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
177 changes: 42 additions & 135 deletions lib/outputrenderers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion lib/rsslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions mod/assignment/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
3 changes: 2 additions & 1 deletion mod/chat/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions mod/choice/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
8 changes: 5 additions & 3 deletions mod/data/backup/moodle2/backup_data_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
}

Expand Down
Loading

0 comments on commit 2b04c41

Please sign in to comment.