Skip to content

Commit

Permalink
rating MDL-21657 updated version of ratings including forum and gloss…
Browse files Browse the repository at this point in the history
…ary integration
  • Loading branch information
Andrew Davis committed Apr 22, 2010
1 parent 1889117 commit 63e8795
Show file tree
Hide file tree
Showing 35 changed files with 707 additions and 1,234 deletions.
11 changes: 7 additions & 4 deletions course/moodleform_mod.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,18 @@ function standard_coursemodule_elements(){
}

if (plugin_supports('mod', $this->_modname, FEATURE_RATE, false)) {
$mform->addElement('header', 'modstandardratings', get_string('ratings', 'ratings'));
require_once($CFG->dirroot.'/rating/lib.php');
$rm = new rating_manager();

$mform->addElement('header', 'modstandardratings', get_string('ratings', 'rating'));

//$mform->addElement('checkbox', 'assessed', get_string('allowratings', 'ratings') , get_string('ratingsuse', 'ratings'));

$mform->addElement('select', 'assessed', get_string('aggregatetype', 'ratings') , forum_get_aggregate_types());
$mform->addElement('select', 'assessed', get_string('aggregatetype', 'rating') , $rm->get_aggregate_types());
$mform->setDefault('assessed', 0);
$mform->setHelpButton('assessed', array('assessaggregate', get_string('aggregatetype', 'ratings'), 'forum'));
$mform->setHelpButton('assessed', array('assessaggregate', get_string('aggregatetype', 'rating'), 'forum'));

$mform->addElement('modgrade', 'scale', get_string('grade'), false);
$mform->addElement('modgrade', 'scale', get_string('scale'), false);
$mform->disabledIf('scale', 'assessed', 'eq', 0);

$mform->addElement('checkbox', 'ratingtime', get_string('ratingtime', 'forum'));
Expand Down
82 changes: 41 additions & 41 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3465,47 +3465,6 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint($result, 2010040901);
}

if ($result && $oldversion < 2010041300) {
//drop the erroneously created ratings table
$table = new xmldb_table('ratings');
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}

//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);
$table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);

$table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
$table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('rating', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);

$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);

/// Adding keys to table rating
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
$table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));

/// Adding indexes to table rating
$table->add_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));

/// Create table for ratings
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}

upgrade_main_savepoint($result, 2010041300);
}

if ($result && $oldversion < 2010041301) {
$sql = "UPDATE {block} SET name=? WHERE name=?";
$DB->execute($sql, array('navigation', 'global_navigation_tree'));
Expand Down Expand Up @@ -3578,6 +3537,47 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint($result, 2010042100);
}

if ($result && $oldversion < 2010042200) {
//drop the previously created ratings table
$table = new xmldb_table('ratings');
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}

//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);
$table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);

$table->add_field('itemid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
$table->add_field('scaleid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('rating', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);

$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);

/// Adding keys to table rating
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('contextid', XMLDB_KEY_FOREIGN, array('contextid'), 'context', array('id'));
$table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));

/// Adding indexes to table rating
$table->add_index('itemid', XMLDB_INDEX_NOTUNIQUE, array('itemid'));

/// Create table for ratings
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}

upgrade_main_savepoint($result, 2010042200);
}

return $result;
}

Expand Down
14 changes: 7 additions & 7 deletions lib/db/upgradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ function upgrade_cleanup_unwanted_block_contexts($contextidarray) {
* @param string $modulename the name of the module
* @return boolean success flag
*/
function upgrade_module_ratings($ratingssql, $modulename) {
/*function upgrade_module_ratings($ratingssql, $modulename) {
global $DB;
$contextid = null;
$contextarray = array();
Expand All @@ -420,15 +420,15 @@ function upgrade_module_ratings($ratingssql, $modulename) {
//all posts within a given forum, glossary etc will have the same context id so store them in an array
if( !array_key_exists($old_rating->mid, $contextarray) ) {
$sql = "SELECT cxt.id
$sql = "SELECT cxt.id
FROM {context} cxt
JOIN {course_modules} cm ON cm.id = cxt.instanceid
JOIN {modules} m ON cm.module = m.id
WHERE m.name = :modulename AND cm.instance = :moduleinstanceid AND cxt.contextlevel = :contextmodule";
$params = array();
$params['modulename'] = $modulename;
$params['modulename'] = $modulename;
$params['moduleinstanceid'] = $old_rating->mid;
$params['contextmodule'] = CONTEXT_MODULE;
$params['contextmodule'] = CONTEXT_MODULE;
$results = $DB->get_record_sql($sql, $params);
$contextarray[$old_rating->mid] = $results->id;
}
Expand All @@ -444,11 +444,11 @@ function upgrade_module_ratings($ratingssql, $modulename) {
$rating->timemodified = $old_rating->timemodified;
if( !$DB->insert_record('rating', $rating) ) {
return false;
}
return false;
}
}
$ratings->close();
return true;
}
}*/
168 changes: 98 additions & 70 deletions lib/outputrenderers.php
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,10 @@ function render_rating(rating $rating) {
global $CFG, $USER;
static $havesetupjavascript = false;

if( $rating->settings->aggregationmethod == RATING_AGGREGATE_NONE ){
return null;//ratings are turned off
}

$useajax = !empty($CFG->enableajax);

//include required Javascript
Expand All @@ -1253,98 +1257,122 @@ function render_rating(rating $rating) {
$havesetupjavascript = true;
}

$strrate = get_string("rate", "rating");
$strratings = ''; //the string we'll return

if($rating->settings->permissions->canview || $rating->settings->permissions->canviewall) {
switch ($rating->settings->aggregationmethod) {
case RATING_AGGREGATE_AVERAGE :
$strratings .= get_string("aggregateavg", "forum");
break;
case RATING_AGGREGATE_COUNT :
$strratings .= get_string("aggregatecount", "forum");
break;
case RATING_AGGREGATE_MAXIMUM :
$strratings .= get_string("aggregatemax", "forum");
break;
case RATING_AGGREGATE_MINIMUM :
$strratings .= get_string("aggregatemin", "forum");
break;
case RATING_AGGREGATE_SUM :
$strratings .= get_string("aggregatesum", "forum");
break;
//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 || $item->itemtimecreated > $rating->settings->assesstimefinish) {
$inassessablewindow = false;
}
}

if (empty($strratings)) {
$strratings .= $strrate;
}
$strratings .= ': ';
$strrate = get_string("rate", "rating");
$ratinghtml = ''; //the string we'll return

//if the item doesnt belong to the current user
if ($rating->itemuserid!=$USER->id ) {
if ($rating->settings->permissions->canview || $rating->settings->permissions->canviewall) {
$aggregatelabel = '';
switch ($rating->settings->aggregationmethod) {
case RATING_AGGREGATE_AVERAGE :
$aggregatelabel .= get_string("aggregateavg", "forum");
break;
case RATING_AGGREGATE_COUNT :
$aggregatelabel .= get_string("aggregatecount", "forum");
break;
case RATING_AGGREGATE_MAXIMUM :
$aggregatelabel .= get_string("aggregatemax", "forum");
break;
case RATING_AGGREGATE_MINIMUM :
$aggregatelabel .= get_string("aggregatemin", "forum");
break;
case RATING_AGGREGATE_SUM :
$aggregatelabel .= get_string("aggregatesum", "forum");
break;
}

$scalemax = 0;
$ratingstr = null;
//$scalemax = 0;//no longer displaying scale max
$aggregatestr = '';

if ( is_array($rating->settings->scale->scaleitems) ) {
$scalemax = $rating->settings->scale->scaleitems[ count($rating->settings->scale->scaleitems)-1 ];
$ratingstr = $rating->settings->scale->scaleitems[$rating->rating];
}
else { //its numeric
$scalemax = $rating->settings->scale->scaleitems;
$ratingstr = round($rating->aggregate,1);
}
if ($rating->rating) { //this will prevent the user seeing the aggregate until they have submitted a rating
if (is_array($rating->settings->scale->scaleitems)) {
//$scalemax = $rating->settings->scale->scaleitems[ count($rating->settings->scale->scaleitems) ];
$aggregatestr .= $rating->settings->scale->scaleitems[round($rating->aggregate)];//round aggregate as we're using it as an index
}
else { //its numeric
//$scalemax = $rating->settings->scale->scaleitems;
$aggregatestr .= round($rating->aggregate,1);
}
}

$aggstr = "{$ratingstr} / $scalemax ({$rating->count}) ";
$countstr = null;
if ($rating->count>0) {
$countstr = "<span id='ratingcount{$rating->itemid}'>({$rating->count})</span>";
} else {
$countstr = "<span id='ratingcount{$rating->itemid}'></span>";
}

//$aggregatehtml = "{$ratingstr} / $scalemax ({$rating->count}) ";
$aggregatehtml = "$aggregatelabel: <span id='ratingaggregate{$rating->itemid}'>{$aggregatestr}</span> $countstr ";

if ($rating->settings->permissions->canviewall) {
$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");

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->canview) {
$strratings .= $aggstr;
$action = new popup_action('click', $popuplink, 'ratings', array('height' => 400, 'width' => 600));
$ratinghtml .= $this->action_link($nonpopuplink, $aggregatehtml, $action);
} else if ($rating->settings->permissions->canview) {
$ratinghtml .= $aggregatehtml;
}
}
}

//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->canrate) {
//dont use $rating->userid below as it will be null if the user hasnt already rated the item
$formstart = <<<END
<form id="postrating{$rating->itemid}" class="postratingform" method="post" action="rating/rate.php">
$formstart = null;
//if the item doesnt belong to the current user, the user has permission to rate
//and we're not outside of a defined assessable period
//if( $rating->itemuserid!=$USER->id && $rating->settings->permissions->canrate && $inassessablewindow) {
if ($rating->itemuserid!=$USER->id && $rating->settings->permissions->canrate && $inassessablewindow) {
$formstart = <<<END
<form id="postrating{$rating->itemid}" class="postratingform" method="post" action="{$CFG->wwwroot}/rating/rate.php">
<div class="ratingform">
<input type="hidden" class="ratinginput" name="contextid" value="{$rating->context->id}" />
<input type="hidden" class="ratinginput" name="itemid" value="{$rating->itemid}" />
<input type="hidden" class="ratinginput" name="scaleid" value="{$rating->settings->scale->id}" />
<input type="hidden" class="ratinginput" name="returnurl" value="{$rating->settings->returnurl}" />
<input type="hidden" class="ratinginput" name="rateduserid" value="{$rating->itemuserid}" />
<input type="hidden" class="ratinginput" name="aggregation" value="{$rating->settings->aggregationmethod}" />
END;
$strratings = $formstart.$strratings;

//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 theyve passed "5" instead of 5
if( is_int($intscalearray) && $intscalearray>0 ){
$scalearray = array();
for($i=0; $i<=$rating->settings->scale->scaleitems; $i++) {
$scalearray[$i] = $i;
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 theyve passed "5" instead of 5
if( is_int($intscalearray) && $intscalearray>0 ){
$scalearray = array();
for($i=0; $i<=$rating->settings->scale->scaleitems; $i++) {
$scalearray[$i] = $i;
}
}
}
}

$scalearray = array(RATING_UNSET_RATING => $strrate.'...') + $scalearray;
$strratings .= html_writer::select($scalearray, 'rating', $rating->rating, false, array('class'=>'postratingmenu ratinginput','id'=>'menurating'.$rating->itemid));
$scalearray = array(RATING_UNSET_RATING => $strrate.'...') + $scalearray;
$ratinghtml .= html_writer::select($scalearray, 'rating', $rating->rating, false, array('class'=>'postratingmenu ratinginput','id'=>'menurating'.$rating->itemid));

//output submit button
$strratings .= '<span class="ratingsubmit"><input type="submit" class="postratingmenusubmit" id="postratingsubmit'.$rating->itemid.'" value="'.s(get_string('rate', 'rating')).'" />';
//output submit button
$ratinghtml .= '<span class="ratingsubmit"><input type="submit" class="postratingmenusubmit" id="postratingsubmit'.$rating->itemid.'" value="'.s(get_string('rate', 'rating')).'" />';

if ( is_array($rating->settings->scale) ) {
//todo andrew where can we get the course id from?
//$strratings .= $this->help_icon_scale($course->id, $scale);
$strratings .= $this->help_icon_scale(1, $rating->settings->scale);
if (is_array($rating->settings->scale->scaleitems)) {
$ratinghtml .= $this->help_icon_scale($rating->settings->scale->courseid, $rating->settings->scale);
}
$ratinghtml .= '</span></div></form>';
}
$strratings .= '</span></div></form>';
}

return $strratings;
return $ratinghtml;
}

/*
Expand Down
7 changes: 4 additions & 3 deletions mod/data/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ function xmldb_data_upgrade($oldversion) {
upgrade_mod_savepoint($result, 2010031602, 'data');
}

if($result && $oldversion < 2010041300) {
/*leave this commented out until the data module is switched to the new rating system MDL-21657
* if($result && $oldversion < 2010041300) {
//migrate data_ratings to the central rating table
require_once($CFG->dirroot . '/lib/db/upgradelib.php');
Expand All @@ -265,10 +266,10 @@ function xmldb_data_upgrade($oldversion) {
JOIN {data} d ON d.id=re.dataid';
$result = $result && upgrade_module_ratings($ratingssql,'data');
//todo andrew drop data_ratings
//todo drop data_ratings
upgrade_mod_savepoint($result, 2010041300, 'data');
}
}*/

return $result;
}
Expand Down
Loading

0 comments on commit 63e8795

Please sign in to comment.