From 63e87951e6ce93aadc2b891556bf832fe4b5226c Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Thu, 22 Apr 2010 05:15:23 +0000 Subject: [PATCH] rating MDL-21657 updated version of ratings including forum and glossary integration --- course/moodleform_mod.php | 11 +- lib/db/upgrade.php | 82 +- lib/db/upgradelib.php | 14 +- lib/outputrenderers.php | 168 ++-- mod/data/db/upgrade.php | 7 +- mod/forum/backuplib.php | 10 +- mod/forum/db/install.xml | 24 +- mod/forum/db/upgrade.php | 22 +- mod/forum/lib.php | 862 +++--------------- mod/forum/mod_form.php | 22 - mod/forum/restorelib.php | 17 +- mod/forum/settings.php | 5 +- mod/forum/version.php | 4 +- mod/glossary/backuplib.php | 5 +- mod/glossary/db/upgrade.php | 21 +- mod/glossary/deleteentry.php | 9 +- .../formats/TEMPLATE/TEMPLATE_format.php | 11 +- .../formats/continuous/continuous_format.php | 10 +- .../formats/dictionary/dictionary_format.php | 8 +- .../encyclopedia/encyclopedia_format.php | 13 +- .../formats/entrylist/entrylist_format.php | 12 +- mod/glossary/formats/faq/faq_format.php | 8 +- .../fullwithauthor/fullwithauthor_format.php | 8 +- .../fullwithoutauthor_format.php | 8 +- mod/glossary/lib.php | 206 +---- mod/glossary/report.php | 1 + mod/glossary/restorelib.php | 5 +- mod/glossary/version.php | 4 +- mod/glossary/view.php | 43 +- rating/index.php | 33 +- rating/lib.php | 204 ++++- rating/module.js | 15 +- rating/rate.php | 22 +- rating/rate_ajax.php | 45 +- version.php | 2 +- 35 files changed, 707 insertions(+), 1234 deletions(-) diff --git a/course/moodleform_mod.php b/course/moodleform_mod.php index f354ceb22f6..07387256d3a 100644 --- a/course/moodleform_mod.php +++ b/course/moodleform_mod.php @@ -343,15 +343,18 @@ abstract class moodleform_mod extends moodleform { } 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')); diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 32509e89072..2c10419cc8c 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -3465,47 +3465,6 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL"); 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')); @@ -3578,6 +3537,47 @@ WHERE gradeitemid IS NOT NULL AND grademax IS NOT NULL"); 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; } diff --git a/lib/db/upgradelib.php b/lib/db/upgradelib.php index 4d4926dd691..a9f1da11703 100644 --- a/lib/db/upgradelib.php +++ b/lib/db/upgradelib.php @@ -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(); @@ -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; } @@ -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; -} \ No newline at end of file +}*/ \ No newline at end of file diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 82327d660f9..dc19233a82d 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -1245,6 +1245,10 @@ class core_renderer extends renderer_base { 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 @@ -1253,98 +1257,122 @@ class core_renderer extends renderer_base { $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 = "({$rating->count})"; + } else { + $countstr = ""; + } + + //$aggregatehtml = "{$ratingstr} / $scalemax ({$rating->count}) "; + $aggregatehtml = "$aggregatelabel: {$aggregatestr} $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 = << + $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; - $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 .= ''; + //output submit button + $ratinghtml .= ''; - 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 .= '
'; } - $strratings .= ''; } - return $strratings; + return $ratinghtml; } /* diff --git a/mod/data/db/upgrade.php b/mod/data/db/upgrade.php index 270942d40df..e20d4a74471 100644 --- a/mod/data/db/upgrade.php +++ b/mod/data/db/upgrade.php @@ -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'); @@ -265,10 +266,10 @@ JOIN {data_records} re ON r.recordid=re.id 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; } diff --git a/mod/forum/backuplib.php b/mod/forum/backuplib.php index b7921cfac0a..f0d5abdad68 100644 --- a/mod/forum/backuplib.php +++ b/mod/forum/backuplib.php @@ -38,11 +38,11 @@ // | forum_posts // |-------------(UL,pk->id,fk->discussion, // | nt->parent,files) - // | | - // | | - // | | - // forum_read forum_ratings - // (UL,pk->id,fk->post (UL,pk->id,fk->post) + // | + // | + // | + // forum_read + // (UL,pk->id,fk->post // // Meaning: pk->primary key field of the table // fk->foreign key to link with parent diff --git a/mod/forum/db/install.xml b/mod/forum/db/install.xml index abef6b11b66..13473f4415b 100644 --- a/mod/forum/db/install.xml +++ b/mod/forum/db/install.xml @@ -1,5 +1,5 @@ - @@ -88,7 +88,7 @@ - +
@@ -105,23 +105,7 @@
- - - - - - - - - - - - - - - -
- +
@@ -168,4 +152,4 @@
-
+ \ No newline at end of file diff --git a/mod/forum/db/upgrade.php b/mod/forum/db/upgrade.php index 4fb3401151b..1252f2d3ab3 100644 --- a/mod/forum/db/upgrade.php +++ b/mod/forum/db/upgrade.php @@ -285,21 +285,29 @@ function xmldb_forum_upgrade($oldversion) { upgrade_mod_savepoint($result, 2009050400, 'forum'); } - if($result && $oldversion < 2010041300) { + if($result && $oldversion < 2010042200) { //migrate forumratings to the central rating table require_once($CFG->dirroot . '/lib/db/upgradelib.php'); //forum ratings only have a single time column so use it for both time created and modified - $ratingssql = 'SELECT r.id AS rid, r.post AS itemid, r.rating, r.userid, r.time AS timecreated, r.time AS timemodified, f.scale, f.id AS mid + $sql = "INSERT {rating} (contextid, scaleid, itemid, rating, userid, timecreated, timemodified) +SELECT cxt.id, f.scale, r.post AS itemid, r.rating, r.userid, r.time AS timecreated, r.time AS timemodified FROM {forum_ratings} r -JOIN {forum_posts} p on p.id=r.post -JOIN {forum_discussions} d on d.id=p.discussion -JOIN {forum} f on f.id=d.forum'; - $result = $result && upgrade_module_ratings($ratingssql,'forum'); +JOIN {forum_posts} p ON p.id=r.post +JOIN {forum_discussions} d ON d.id=p.discussion +JOIN {forum} f ON f.id=d.forum +JOIN {course_modules} cm ON cm.instance=f.id +JOIN {context} cxt ON cxt.instanceid=cm.id +JOIN {modules} m ON m.id=cm.module +WHERE m.name = :modname AND cxt.contextlevel = :contextlevel"; + $params['modname'] = 'forum'; + $params['contextlevel'] = CONTEXT_MODULE; + + $DB->execute($sql, $params); //todo andrew drop forum_ratings - upgrade_mod_savepoint($result, 2010041300, 'forum'); + upgrade_mod_savepoint($result, 2010042200, 'forum'); } return $result; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 0b94b1f30df..a4f2325a0c9 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -43,17 +43,6 @@ define('FORUM_TRACKING_OFF', 0); define('FORUM_TRACKING_OPTIONAL', 1); define('FORUM_TRACKING_ON', 2); -//todo andrew remove this -define('FORUM_UNSET_POST_RATING', -999); - -//todo andrew and remove these -define ('FORUM_AGGREGATE_NONE', 0); //no ratings -define ('FORUM_AGGREGATE_AVG', 1); -define ('FORUM_AGGREGATE_COUNT', 2); -define ('FORUM_AGGREGATE_MAX', 3); -define ('FORUM_AGGREGATE_MIN', 4); -define ('FORUM_AGGREGATE_SUM', 5); - /// STANDARD FUNCTIONS /////////////////////////////////////////////////////////// /** @@ -1158,41 +1147,13 @@ function forum_user_complete($course, $user, $mod, $forum) { } $discussions = forum_get_user_involved_discussions($forum->id, $user->id); - // preload all user ratings for these discussions - one query only and minimal memory - $cm->cache->ratings = array(); - $cm->cache->myratings = array(); - if ($postratings = forum_get_all_user_ratings($user->id, $discussions)) { - foreach ($postratings as $pr) { - if (!isset($cm->cache->ratings[$pr->postid])) { - $cm->cache->ratings[$pr->postid] = array(); - } - $cm->cache->ratings[$pr->postid][$pr->id] = $pr->rating; - - if ($pr->userid == $USER->id) { - $cm->cache->myratings[$pr->postid] = $pr->rating; - } - } - unset($postratings); - } - foreach ($posts as $post) { if (!isset($discussions[$post->discussion])) { continue; } $discussion = $discussions[$post->discussion]; - $ratings = null; - - if ($forum->assessed) { - if ($scale = make_grades_menu($forum->scale)) { - $ratings =new object(); - $ratings->scale = $scale; - $ratings->assesstimestart = $forum->assesstimestart; - $ratings->assesstimefinish = $forum->assesstimefinish; - $ratings->allow = false; - } - } - forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false, $ratings); + forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false); } } else { echo "

".get_string("noposts", "forum")."

"; @@ -1458,92 +1419,26 @@ function forum_print_recent_activity($course, $viewfullnames, $timestart) { * @param int $userid optional user id, 0 means all users * @return array array of grades, false if none */ - //todo andrew pretty sure I can remove this function forum_get_user_grades($forum, $userid=0) { - global $CFG, $DB; - - $params= array(); - if ($userid) { - $params[] = $userid; - $user = "AND u.id = ?"; - } else { - $user = ""; - } - - $params[] = $forum->id; + global $CFG; + + require_once($CFG->dirroot.'/rating/lib.php'); + $rm = new rating_manager(); + + $ratingoptions = new stdclass(); - $aggtype = $forum->assessed; - switch ($aggtype) { - case FORUM_AGGREGATE_COUNT : - $sql = "SELECT u.id, u.id AS userid, COUNT(fr.rating) AS rawgrade - FROM {user} u, {forum_posts} fp, - {forum_ratings} fr, {forum_discussions} fd - WHERE u.id = fp.userid AND fp.discussion = fd.id AND fr.post = fp.id - AND fr.userid != u.id AND fd.forum = ? - $user - GROUP BY u.id"; - break; - case FORUM_AGGREGATE_MAX : - $sql = "SELECT u.id, u.id AS userid, MAX(fr.rating) AS rawgrade - FROM {user} u, {forum_posts} fp, - {forum_ratings} fr, {forum_discussions} fd - WHERE u.id = fp.userid AND fp.discussion = fd.id AND fr.post = fp.id - AND fr.userid != u.id AND fd.forum = ? - $user - GROUP BY u.id"; - break; - case FORUM_AGGREGATE_MIN : - $sql = "SELECT u.id, u.id AS userid, MIN(fr.rating) AS rawgrade - FROM {user} u, {forum_posts} fp, - {forum_ratings} fr, {forum_discussions} fd - WHERE u.id = fp.userid AND fp.discussion = fd.id AND fr.post = fp.id - AND fr.userid != u.id AND fd.forum = ? - $user - GROUP BY u.id"; - break; - case FORUM_AGGREGATE_SUM : - $sql = "SELECT u.id, u.id AS userid, SUM(fr.rating) AS rawgrade - FROM {user} u, {forum_posts} fp, - {forum_ratings} fr, {forum_discussions} fd - WHERE u.id = fp.userid AND fp.discussion = fd.id AND fr.post = fp.id - AND fr.userid != u.id AND fd.forum = ? - $user - GROUP BY u.id"; - break; - default : //avg - $sql = "SELECT u.id, u.id AS userid, AVG(fr.rating) AS rawgrade - FROM {user} u, {forum_posts} fp, - {forum_ratings} fr, {forum_discussions} fd - WHERE u.id = fp.userid AND fp.discussion = fd.id AND fr.post = fp.id - AND fr.userid != u.id AND fd.forum = ? - $user - GROUP BY u.id"; - break; - } + //need these to work backwards to get a context id. Is there a better way to get contextid from a module instance? + $ratingoptions->modulename = 'forum'; + $ratingoptions->moduleid = $forum->id; + //$ratingoptions->cmidnumber = $forum->cmidnumber; - if ($results = $DB->get_records_sql($sql, $params)) { - // it could throw off the grading if count and sum returned a rawgrade higher than scale - // so to prevent it we review the results and ensure that rawgrade does not exceed the scale, if it does we set rawgrade = scale (i.e. full credit) - foreach ($results as $rid=>$result) { - if ($forum->scale >= 0) { - //numeric - if ($result->rawgrade > $forum->scale) { - $results[$rid]->rawgrade = $forum->scale; - } - } else { - //scales - if ($scale = $DB->get_record('scale', array('id' => -$forum->scale))) { - $scale = explode(',', $scale->scale); - $max = count($scale); - if ($result->rawgrade > $max) { - $results[$rid]->rawgrade = $max; - } - } - } - } - } + $ratingoptions->userid = $userid; + $ratingoptions->aggregationmethod = $forum->assessed; + $ratingoptions->scaleid = $forum->scale; + $ratingoptions->itemtable = 'forum_posts'; + $ratingoptions->itemtableusercolumn = 'userid'; - return $results; + return $rm->get_user_grades($ratingoptions); } /** @@ -1662,7 +1557,7 @@ function forum_grade_item_delete($forum) { /** * Returns the users with data in one forum - * (users with records in forum_subscriptions, forum_posts and forum_ratings, students) + * (users with records in forum_subscriptions, forum_posts, students) * * @global object * @global object @@ -1688,12 +1583,12 @@ function forum_get_participants($forumid) { p.discussion = d.id AND u.id = p.userid", array($forumid)); - //Get students from forum_ratings + //Get students from the ratings table $st_ratings = $DB->get_records_sql("SELECT DISTINCT u.id, u.id FROM {user} u, {forum_discussions} d, {forum_posts} p, - {forum_ratings} r + {ratings} r WHERE d.forum = ? AND p.discussion = d.id AND r.post = p.id AND @@ -2126,63 +2021,6 @@ function forum_search_posts($searchterms, $courseid=0, $limitfrom=0, $limitnum=5 return $DB->get_records_sql($searchsql, $params, $limitfrom, $limitnum); } -/** - * Returns a list of ratings for all posts in discussion - * - * @global object - * @global object - * @param object $discussion - * @return array of ratings or false - */ -function forum_get_all_discussion_ratings($discussion) { - global $CFG, $DB; - return $DB->get_records_sql("SELECT r.id, r.userid, p.id AS postid, r.rating - FROM {forum_ratings} r, - {forum_posts} p - WHERE r.post = p.id AND p.discussion = ? - ORDER BY p.id ASC", array($discussion->id)); -} - -/** - * Returns a list of ratings for one specific user for all posts in discussion - * @global object - * @global object - * @param object $discussions the discussions for which we return all ratings - * @param int $userid the user for who we return all ratings - * @return array - */ -function forum_get_all_user_ratings($userid, $discussions) { - global $CFG, $DB; - - - foreach ($discussions as $discussion) { - if (!isset($discussionsid)){ - $discussionsid = $discussion->id; - } - else { - $discussionsid .= ",".$discussion->id; - } - } - - $sql = "SELECT r.id, r.userid, p.id AS postid, r.rating - FROM {forum_ratings} r, - {forum_posts} p - WHERE r.post = p.id AND p.userid = :userid"; - - - $params = array(); - $params['userid'] = $userid; - //postgres compability - if (!isset($discussionsid)) { - $sql .=" AND p.discussion IN (".$discussionsid.")"; - } - $sql .=" ORDER BY p.id ASC"; - - return $DB->get_records_sql($sql, $params); - - -} - /** * Returns a list of ratings for a particular post - sorted. * @@ -2192,14 +2030,15 @@ function forum_get_all_user_ratings($userid, $discussions) { * @param string $sort * @return array Array of ratings or false */ -function forum_get_ratings($postid, $sort="u.firstname ASC") { - global $CFG, $DB; - return $DB->get_records_sql("SELECT u.*, r.rating, r.time - FROM {forum_ratings} r, - {user} u - WHERE r.post = ? - AND r.userid = u.id - ORDER BY $sort", array($postid)); +function forum_get_ratings($context, $postid, $sort="u.firstname ASC") { + global $PAGE; + + $options = new stdclass(); + $options->context = $PAGE->context; + $options->itemid = $postid; + $options->sort = "ORDER BY $sort"; + + get_all_ratings_for_item($options); } /** @@ -2588,7 +2427,7 @@ function forum_count_discussions($forum, $cm, $course) { } /** - * How many unrated posts are in the given discussion for a given user? + * How many posts by other users are unrated by a given user in the given discussion? * * @global object * @global object @@ -2606,9 +2445,9 @@ function forum_count_unrated_posts($discussionid, $userid) { if ($rated = $DB->get_record_sql("SELECT count(*) as num FROM {forum_posts} p, - {forum_ratings} r + {rating} r WHERE p.discussion = ? - AND p.id = r.post + AND p.id = r.itemid AND r.userid = ?", array($discussionid, $userid))) { $difference = $posts->num - $rated->num; if ($difference > 0) { @@ -3238,7 +3077,6 @@ function forum_make_mail_post($course, $cm, $forum, $discussion, $post, $userfro * @param boolean $ownpost Whether this post belongs to the current user. * @param boolean $reply Whether to print a 'reply' link at the bottom of the message. * @param boolean $link Just print a shortened version of the post as a link to the full post. - * @param object $ratings -- I don't really know -- * @param string $footer Extra stuff to print after the message. * @param string $highlight Space-separated list of terms to highlight. * @param int $post_read true, false or -99. If we already know whether this user @@ -3249,9 +3087,10 @@ function forum_make_mail_post($course, $cm, $forum, $discussion, $post, $userfro * (the default) then print a dummy 'you can't see this post' post. * If false, don't output anything at all. * @param bool|null $istracked + * @return void */ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=false, $reply=false, $link=false, - $ratings=NULL, $footer="", $highlight="", $post_read=null, $dummyifcantsee=true, $istracked=null) { + $footer="", $highlight="", $post_read=null, $dummyifcantsee=true, $istracked=null) { global $USER, $CFG, $OUTPUT; @@ -3514,7 +3353,7 @@ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=fa } $porfoliohtml = $button->to_html(PORTFOLIO_ADD_TEXT_LINK); - if(!empty($porfoliohtml)){ + if (!empty($porfoliohtml)) { $commands[] = $porfoliohtml; } } @@ -3525,62 +3364,8 @@ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=fa // Ratings - - $ratingsmenuused = false; - if (!empty($ratings) and isloggedin()) { - echo '
'; - $useratings = true; - if ($ratings->assesstimestart and $ratings->assesstimefinish) { - if ($post->created < $ratings->assesstimestart or $post->created > $ratings->assesstimefinish) { - $useratings = false; - } - } - if ($useratings) { - $mypost = ($USER->id == $post->userid); - - $canviewallratings = $cm->cache->caps['mod/forum:viewanyrating']; - - if (isset($cm->cache->ratings)) { - if (isset($cm->cache->ratings[$post->id])) { - $allratings = $cm->cache->ratings[$post->id]; - } else { - $allratings = array(); // no reatings present yet - } - } else { - $allratings = NULL; // not preloaded - } - - if (isset($cm->cache->myratings)) { - if (isset($cm->cache->myratings[$post->id])) { - $myrating = $cm->cache->myratings[$post->id]; - } else { - $myrating = FORUM_UNSET_POST_RATING; // no reatings present yet - } - } else { - $myrating = NULL; // not preloaded - } - - if ($canviewallratings and !$mypost) { - echo '' . - forum_print_ratings($post->id, $ratings->scale, $forum->assessed, $canviewallratings, $allratings, true) . - ''; - if (!empty($ratings->allow)) { - echo ' '; - forum_print_rating_menu($post->id, $USER->id, $ratings->scale, $myrating); - $ratingsmenuused = true; - } - - } else if ($mypost) { - echo '' . - forum_print_ratings($post->id, $ratings->scale, $forum->assessed, true, $allratings, true) . - ''; - - } else if (!empty($ratings->allow) ) { - forum_print_rating_menu($post->id, $USER->id, $ratings->scale, $myrating); - $ratingsmenuused = true; - } - } - echo '
'; + if (!empty($post->rating)) { + echo $OUTPUT->render($post->rating); } // Link to post if required @@ -3605,8 +3390,6 @@ function forum_print_post($post, $discussion, $forum, &$cm, $course, $ownpost=fa if ($istracked && !$CFG->forum_usermarksread && !$post_read) { forum_tp_mark_post_read($USER->id, $post, $forum->id); } - - return $ratingsmenuused; } @@ -3797,384 +3580,6 @@ function forum_shorten_post($message) { return substr($message, 0, $truncate); } - -/** - * Print the multiple ratings on a post given to the current user by others. - * Forumid prevents the double lookup of the forumid in discussion to determine the aggregate type - * Scale is an array of ratings - * - * @uses FORUM_AGGREGATE_AVG - * @uses FORUM_AGGREGATE_COUNT - * @uses FORUM_AGGREGATE_MAX - * @uses FORUM_AGGREGATE_MIN - * @uses FORUM_AGGREGATE_SUM - * @param int $postid - * @param array $scale - * @param int $aggregatetype - * @param bool $link - * @param array $ratings - * @param bool $return - * @return string|void - */ -function forum_print_ratings($postid, $scale, $aggregatetype, $link=true, $ratings=null, $return=false) { - global $OUTPUT; - $strratings = ''; - - switch ($aggregatetype) { - case FORUM_AGGREGATE_AVG : - $agg = forum_get_ratings_mean($postid, $scale, $ratings); - $strratings = get_string("aggregateavg", "forum"); - break; - case FORUM_AGGREGATE_COUNT : - $agg = forum_get_ratings_count($postid, $scale, $ratings); - $strratings = get_string("aggregatecount", "forum"); - break; - case FORUM_AGGREGATE_MAX : - $agg = forum_get_ratings_max($postid, $scale, $ratings); - $strratings = get_string("aggregatemax", "forum"); - break; - case FORUM_AGGREGATE_MIN : - $agg = forum_get_ratings_min($postid, $scale, $ratings); - $strratings = get_string("aggregatemin", "forum"); - break; - case FORUM_AGGREGATE_SUM : - $agg = forum_get_ratings_sum($postid, $scale, $ratings); - $strratings = get_string("aggregatesum", "forum"); - break; - } - - if ($agg !== "") { - - if (empty($strratings)) { - $strratings = get_string("ratings", "forum"); - } - - $strratings .= ': '; - - if ($link) { - - $link = new moodle_url("/mod/forum/report.php?id=$postid"); - $action = new popup_action('click', $link, 'ratings', array('height' => 400, 'width' => 600)); - $strratings .= $OUTPUT->action_link($link, $agg, $action); - } else { - $strratings .= "$agg "; - } - - if ($return) { - return $strratings; - } else { - echo $strratings; - } - } -} - - -/** - * Return the mean rating of a post given to the current user by others. - * - * @global object - * @param int $postid - * @param array $scal Scale is an array of possible ratings in the scale - * @param array $ratings Ratings is an optional simple array of actual ratings (just integers) - * @return string - */ -function forum_get_ratings_mean($postid, $scale, $ratings=NULL) { - global $DB; - if (is_null($ratings)) { - $ratings = array(); - if ($rates = $DB->get_records("forum_ratings", array("post" => $postid))) { - foreach ($rates as $rate) { - $ratings[] = $rate->rating; - } - } - } - - $count = count($ratings); - - if ($count == 0 ) { - return ""; - - } else if ($count == 1) { - $rating = reset($ratings); - return $scale[$rating]; - - } else { - $total = 0; - foreach ($ratings as $rating) { - $total += $rating; - } - $mean = round( ((float)$total/(float)$count) + 0.001); // Little fudge factor so that 0.5 goes UP - - if (isset($scale[$mean])) { - return $scale[$mean]." ($count)"; - } else { - return "$mean ($count)"; // Should never happen, hopefully - } - } -} - -/** - * Return the count of the ratings of a post given to the current user by others. - * - * For numerical grades, the scale index is the same as the real grade value from interval {0..n} - * and $scale looks like Array( 0 => '0/n', 1 => '1/n', ..., n => 'n/n' ) - * - * For scales, the index is the order of the scale item {1..n} - * and $scale looks like Array( 1 => 'poor', 2 => 'weak', 3 => 'good' ) - * In case of no ratings done yet, we have nothing to display. - * - * @global object - * @param int $postid - * @param array $scale Possible ratings in the scale - the end of the scale is the highest or max grade - * @param array $ratings An optional simple array of actual ratings (just integers) - * @return string - */ -function forum_get_ratings_count($postid, $scale, $ratings=NULL) { - global $DB; - if (is_null($ratings)) { - $ratings = array(); - if ($rates = $DB->get_records("forum_ratings", array("post" => $postid))) { - foreach ($rates as $rate) { - $ratings[] = $rate->rating; - } - } - } - - $count = count($ratings); - if (! array_key_exists(0, $scale)) { - $scaleused = true; - } else { - $scaleused = false; - } - - if ($count == 0) { - if ($scaleused) { // If no rating given yet and we use a scale - return get_string('noratinggiven', 'forum'); - } else { - return ''; - } - } - - $maxgradeidx = max(array_keys($scale)); // For numerical grades, the index is the same as the real grade value {0..n} - // and $scale looks like Array( 0 => '0/n', 1 => '1/n', ..., n => 'n/n' ) - // For scales, the index is the order of the scale item {1..n} - // and $scale looks like Array( 1 => 'poor', 2 => 'weak', 3 => 'good' ) - - if ($count > $maxgradeidx) { // The count exceeds the max grade - $a = new stdClass(); - $a->count = $count; - $a->grade = $scale[$maxgradeidx]; - return get_string('aggregatecountformat', 'forum', $a); - } else { // Display the count and the aggregated grade for this post - $a = new stdClass(); - $a->count = $count; - $a->grade = $scale[$count]; - return get_string('aggregatecountformat', 'forum', $a); - } -} - -/** - * Return the max rating of a post given to the current user by others. - * - * @global object - * @param int $postid - * @param array $scale Scale is an array of possible ratings in the scale - * @param array $ratings Ratings is an optional simple array of actual ratings (just integers) - */ -function forum_get_ratings_max($postid, $scale, $ratings=NULL) { - - global $DB; - if (is_null($ratings)) { - $ratings = array(); - if ($rates = $DB->get_records("forum_ratings", array("post" => $postid))) { - foreach ($rates as $rate) { - $ratings[] = $rate->rating; - } - } - } - - $count = count($ratings); - - if ($count == 0 ) { - return ""; - - } else if ($count == 1) { //this works for max - $rating = reset($ratings); - return $scale[$rating]; - - } else { - $max = max($ratings); - - if (isset($scale[$max])) { - return $scale[$max]." ($count)"; - } else { - return "$max ($count)"; // Should never happen, hopefully - } - } -} - -/** - * Return the min rating of a post given to the current user by others. - * - * @global object - * @param int postid - * @param array $scale Scale is an array of possible ratings in the scale - * @param array $ratings Ratings is an optional simple array of actual ratings (just integers) - * @return string - */ -function forum_get_ratings_min($postid, $scale, $ratings=NULL) { - global $DB; - if (is_null($ratings)) { - $ratings = array(); - if ($rates = $DB->get_records("forum_ratings", array("post" => $postid))) { - foreach ($rates as $rate) { - $ratings[] = $rate->rating; - } - } - } - - $count = count($ratings); - - if ($count == 0 ) { - return ""; - - } else if ($count == 1) { - $rating = reset($ratings); - return $scale[$rating]; //this works for min - - } else { - $min = min($ratings); - - if (isset($scale[$min])) { - return $scale[$min]." ($count)"; - } else { - return "$min ($count)"; // Should never happen, hopefully - } - } -} - - -/** - * Return the sum or total of ratings of a post given to the current user by others. - * - * @global object - * @param int $postid - * @param array $scale Scale is an array of possible ratings in the scale - * @param array $ratings Ratings is an optional simple array of actual ratings (just integers) - * @return string - */ -function forum_get_ratings_sum($postid, $scale, $ratings=NULL) { - global $DB; - if (is_null($ratings)) { - $ratings = array(); - if ($rates = $DB->get_records("forum_ratings", array("post" => $postid))) { - foreach ($rates as $rate) { - $ratings[] = $rate->rating; - } - } - } - - $count = count($ratings); - $scalecount = count($scale)-1; //this should give us the last element of the scale aka the max grade with $scale[$scalecount] - - if ($count == 0 ) { - return ""; - - } else if ($count == 1) { //this works for max. - $rating = reset($ratings); - return $scale[$rating]; - - } else { - $total = 0; - foreach ($ratings as $rating) { - $total += $rating; - } - if ($total > $scale[$scalecount]) { //if the total exceeds the max grade then set it to the max grade - $total = $scale[$scalecount]; - } - if (isset($scale[$total])) { - return $scale[$total]." ($count)"; - } else { - return "$total ($count)"; // Should never happen, hopefully - } - } -} - -/** - * Return a summary of post ratings given to the current user by others. - * - * @global object - * @param int $postid - * @param array $scale Scale is an array of possible ratings in the scale - * @param array $ratings Ratings is an optional simple array of actual ratings (just integers) - * @return string - */ -function forum_get_ratings_summary($postid, $scale, $ratings=NULL) { - global $DB; - if (is_null($ratings)) { - $ratings = array(); - if ($rates = $DB->get_records("forum_ratings", array("post" => $postid))) { - foreach ($rates as $rate) { - $rating[] = $rate->rating; - } - } - } - - - if (!$count = count($ratings)) { - return ""; - } - - - foreach ($scale as $key => $scaleitem) { - $sumrating[$key] = 0; - } - - foreach ($ratings as $rating) { - $sumrating[$rating]++; - } - - $summary = ""; - foreach ($scale as $key => $scaleitem) { - $summary = $sumrating[$key].$summary; - if ($key > 1) { - $summary = "/$summary"; - } - } - return $summary; -} - -/** - * Print the menu of ratings as part of a larger form. - * If the post has already been - set that value. - * - * @global object - * @param int $postid - * @param int $userid - * @param array $scale is an array of ratings - * @param int $myrating - */ - //todo andrew remove this function -function forum_print_rating_menu($postid, $userid, $scale, $myrating=NULL) { - - static $strrate; - global $DB, $OUTPUT; - - if (is_null($myrating)) { - if (!$rating = $DB->get_record("forum_ratings", array("userid" => $userid, "post" => $postid))) { - $myrating = FORUM_UNSET_POST_RATING; - } else { - $myrating = $rating->rating; - } - } - - if (empty($strrate)) { - $strrate = get_string("rate", "forum"); - } - $scale = array(FORUM_UNSET_POST_RATING => $strrate.'...') + $scale; - echo html_writer::select($scale, $postid, $myrating, false, array('class'=>'forumpostratingmenu')); -} - /** * Print the drop down that allows the user to select how they want to have * the discussion displayed. @@ -4732,7 +4137,7 @@ function forum_delete_discussion($discussion, $fulldelete, $course, $cm, $forum) * @return bool */ function forum_delete_post($post, $children, $course, $cm, $forum, $skipcompletion=false) { - global $DB; + global $DB, $CFG; $context = get_context_instance(CONTEXT_MODULE, $cm->id); @@ -4747,7 +4152,12 @@ function forum_delete_post($post, $children, $course, $cm, $forum, $skipcompleti } //delete ratings - $DB->delete_records("forum_ratings", array("post" => $post->id)); + require_once($CFG->dirroot.'/rating/lib.php'); + $delopt = new stdclass(); + $delopt->contextid = $context->id; + $delopt->itemid = $post->id; + $rm = new rating_manager(); + $rm->delete_ratings($delopt); //delete attachments $fs = get_file_storage(); @@ -5766,6 +5176,7 @@ function forum_print_latest_discussions($course, $forum, $maxdiscussions=-1, $di function forum_print_discussion($course, $cm, $forum, $discussion, $post, $mode, $canreply=NULL, $canrate=false) { global $USER, $CFG, $DB, $PAGE, $OUTPUT; + require_once($CFG->dirroot.'/rating/lib.php'); if (isloggedin()) { $ownpost = ($USER->id == $post->userid); @@ -5812,41 +5223,24 @@ function forum_print_discussion($course, $cm, $forum, $discussion, $post, $mode, unset($postersgroups); } - $ratings = NULL; - $ratingsmenuused = false; - $ratingsformused = false; - if ($forum->assessed and isloggedin()) { - if ($scale = make_grades_menu($forum->scale)) { - $ratings =new object(); - $ratings->scale = $scale; - $ratings->assesstimestart = $forum->assesstimestart; - $ratings->assesstimefinish = $forum->assesstimefinish; - $ratings->allow = $canrate; - - if ($ratings->allow) { - echo '
'; - echo '
'; - echo ''; - echo ''; - $ratingsformused = true; - } - // preload all ratings - one query only and minimal memory - $cm->cache->ratings = array(); - $cm->cache->myratings = array(); - if ($postratings = forum_get_all_discussion_ratings($discussion)) { - foreach ($postratings as $pr) { - if (!isset($cm->cache->ratings[$pr->postid])) { - $cm->cache->ratings[$pr->postid] = array(); - } - $cm->cache->ratings[$pr->postid][$pr->id] = $pr->rating; - if ($pr->userid == $USER->id) { - $cm->cache->myratings[$pr->postid] = $pr->rating; - } - } - unset($postratings); - } - } + //load ratings + $ratingoptions = new stdclass(); + $ratingoptions->context = $cm->context; + $ratingoptions->items = $posts; + $ratingoptions->aggregate = $forum->assessed;//the aggregation method + $ratingoptions->scaleid = $forum->scale; + $ratingoptions->userid = $USER->id; + if ($forum->type == 'single' or !$discussion->id) { + $ratingoptions->returnurl = "$CFG->wwwroot/mod/forum/view.php?id=$cm->id"; + } else { + $ratingoptions->returnurl = "$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id"; } + $ratingoptions->assesstimestart = $forum->assesstimestart; + $ratingoptions->assesstimefinish = $forum->assesstimefinish; + + $rm = new rating_manager(); + $posts = $rm->get_ratings($ratingoptions); + $post->forum = $forum->id; // Add the forum id to the post object, later used by forum_print_post $post->forumtype = $forum->type; @@ -5855,50 +5249,24 @@ function forum_print_discussion($course, $cm, $forum, $discussion, $post, $mode, $postread = !empty($post->postread); - if (forum_print_post($post, $discussion, $forum, $cm, $course, $ownpost, $reply, false, $ratings, - '', '', $postread, true, $forumtracked)) { - $ratingsmenuused = true; - } + forum_print_post($post, $discussion, $forum, $cm, $course, $ownpost, $reply, false, + '', '', $postread, true, $forumtracked); switch ($mode) { case FORUM_MODE_FLATOLDEST : case FORUM_MODE_FLATNEWEST : default: - if (forum_print_posts_flat($course, $cm, $forum, $discussion, $post, $mode, $ratings, $reply, $forumtracked, $posts)) { - $ratingsmenuused = true; - } + forum_print_posts_flat($course, $cm, $forum, $discussion, $post, $mode, $reply, $forumtracked, $posts); break; case FORUM_MODE_THREADED : - if (forum_print_posts_threaded($course, $cm, $forum, $discussion, $post, 0, $ratings, $reply, $forumtracked, $posts)) { - $ratingsmenuused = true; - } + forum_print_posts_threaded($course, $cm, $forum, $discussion, $post, 0, $reply, $forumtracked, $posts); break; case FORUM_MODE_NESTED : - if (forum_print_posts_nested($course, $cm, $forum, $discussion, $post, $ratings, $reply, $forumtracked, $posts)) { - $ratingsmenuused = true; - } + forum_print_posts_nested($course, $cm, $forum, $discussion, $post, $reply, $forumtracked, $posts); break; } - - if ($ratingsformused) { - if ($ratingsmenuused) { - echo '
'; - echo ''; - if (ajaxenabled() && !empty($CFG->forum_ajaxrating)) { /// AJAX enabled, standard submission form - $PAGE->requires->js_function_call('add_menu_listeners', array($OUTPUT->pix_url('i/loading_small')), true); - } - if ($forum->scale < 0) { - if ($scale = $DB->get_record("scale", array("id" => abs($forum->scale)))) { - echo $OUTPUT->help_icon_scale($course->id, $scale); - } - } - echo '
'; - } - echo '
'; - echo '
'; - } } @@ -5916,12 +5284,12 @@ function forum_print_discussion($course, $cm, $forum, $discussion, $post, $mode, * @param bool $reply * @param bool $forumtracked * @param array $posts + * @return void */ function forum_print_posts_flat($course, &$cm, $forum, $discussion, $post, $mode, $ratings, $reply, $forumtracked, $posts) { global $USER, $CFG; $link = false; - $ratingsmenuused = false; if ($mode == FORUM_MODE_FLATNEWEST) { $sort = "ORDER BY created DESC"; @@ -5938,28 +5306,23 @@ function forum_print_posts_flat($course, &$cm, $forum, $discussion, $post, $mode $postread = !empty($post->postread); - if (forum_print_post($post, $discussion, $forum, $cm, $course, $ownpost, $reply, $link, $ratings, - '', '', $postread, true, $forumtracked)) { - $ratingsmenuused = true; - } + forum_print_post($post, $discussion, $forum, $cm, $course, $ownpost, $reply, $link, $ratings, + '', '', $postread, true, $forumtracked); } - - return $ratingsmenuused; } - /** * @todo Document this function * * @global object * @global object * @uses CONTEXT_MODULE + * @return void */ -function forum_print_posts_threaded($course, &$cm, $forum, $discussion, $parent, $depth, $ratings, $reply, $forumtracked, $posts) { +function forum_print_posts_threaded($course, &$cm, $forum, $discussion, $parent, $depth, $reply, $forumtracked, $posts) { global $USER, $CFG; $link = false; - $ratingsmenuused = false; if (!empty($posts[$parent->id]->children)) { $posts = $posts[$parent->id]->children; @@ -5976,10 +5339,8 @@ function forum_print_posts_threaded($course, &$cm, $forum, $discussion, $parent, $postread = !empty($post->postread); - if (forum_print_post($post, $discussion, $forum, $cm, $course, $ownpost, $reply, $link, $ratings, - '', '', $postread, true, $forumtracked)) { - $ratingsmenuused = true; - } + forum_print_post($post, $discussion, $forum, $cm, $course, $ownpost, $reply, $link, $ratings, + '', '', $postread, true, $forumtracked); } else { if (!forum_user_can_see_post($forum, $discussion, $post, NULL, $cm)) { echo "\n"; @@ -6004,25 +5365,22 @@ function forum_print_posts_threaded($course, &$cm, $forum, $discussion, $parent, echo ""; } - if (forum_print_posts_threaded($course, $cm, $forum, $discussion, $post, $depth-1, $ratings, $reply, $forumtracked, $posts)) { - $ratingsmenuused = true; - } + forum_print_posts_threaded($course, $cm, $forum, $discussion, $post, $depth-1, $ratings, $reply, $forumtracked, $posts); echo "\n"; } } - return $ratingsmenuused; } /** * @todo Document this function * @global object * @global object + * @return void */ -function forum_print_posts_nested($course, &$cm, $forum, $discussion, $parent, $ratings, $reply, $forumtracked, $posts) { +function forum_print_posts_nested($course, &$cm, $forum, $discussion, $parent, $reply, $forumtracked, $posts) { global $USER, $CFG; $link = false; - $ratingsmenuused = false; if (!empty($posts[$parent->id]->children)) { $posts = $posts[$parent->id]->children; @@ -6039,17 +5397,12 @@ function forum_print_posts_nested($course, &$cm, $forum, $discussion, $parent, $ $post->subject = format_string($post->subject); $postread = !empty($post->postread); - if (forum_print_post($post, $discussion, $forum, $cm, $course, $ownpost, $reply, $link, $ratings, - '', '', $postread, true, $forumtracked)) { - $ratingsmenuused = true; - } - if (forum_print_posts_nested($course, $cm, $forum, $discussion, $post, $ratings, $reply, $forumtracked, $posts)) { - $ratingsmenuused = true; - } + forum_print_post($post, $discussion, $forum, $cm, $course, $ownpost, $reply, $link, + '', '', $postread, true, $forumtracked); + forum_print_posts_nested($course, $cm, $forum, $discussion, $post, $reply, $forumtracked, $posts); echo "\n"; } } - return $ratingsmenuused; } /** @@ -7512,6 +6865,7 @@ function forum_reset_gradebook($courseid, $type='') { */ function forum_reset_userdata($data) { global $CFG, $DB; + require_once($CFG->dirroot.'/rating/lib.php'); $componentstr = get_string('modulenameplural', 'forum'); $status = array(); @@ -7519,12 +6873,11 @@ function forum_reset_userdata($data) { $params = array($data->courseid); $removeposts = false; + $typesql = ""; if (!empty($data->reset_forum_all)) { $removeposts = true; - $typesql = ""; $typesstr = get_string('resetforumsall', 'forum'); $types = array(); - } else if (!empty($data->reset_forum_types)){ $removeposts = true; $typesql = ""; @@ -7539,7 +6892,6 @@ function forum_reset_userdata($data) { $params[] = $type; } $typesstr = get_string('resetforums', 'forum').': '.implode(', ', $types); - } $alldiscussionssql = "SELECT fd.id FROM {forum_discussions} fd, {forum} f @@ -7553,20 +6905,31 @@ function forum_reset_userdata($data) { FROM {forum_posts} fp, {forum_discussions} fd, {forum} f WHERE f.course=? AND f.id=fd.forum AND fd.id=fp.discussion"; + $forumssql = $forums = $rm = null; + if( $removeposts || !empty($data->reset_forum_ratings) ) { + $forumssql = "$allforumssql $typesql"; + $forums = $forums = $DB->get_records_sql($forumssql, $params); + $rm = new rating_manager(); + } + if ($removeposts) { $discussionssql = "$alldiscussionssql $typesql"; - $forumssql = "$allforumssql $typesql"; $postssql = "$allpostssql $typesql"; // now get rid of all attachments $fs = get_file_storage(); - if ($forums = $DB->get_records_sql($forumssql, $params)) { + $ratingdeloptions = new stdclass(); + if ($forums) { foreach ($forums as $forumid=>$unused) { if (!$cm = get_coursemodule_from_instance('forum', $forumid)) { continue; } $context = get_context_instance(CONTEXT_MODULE, $cm->id); $fs->delete_area_files($context->id, 'forum_attachment'); + + //remove ratings + $ratingdeloptions->contextid = $context->id; + $rm->delete_ratings($ratingdeloptions); } } @@ -7579,9 +6942,6 @@ function forum_reset_userdata($data) { // remove posts from queue $DB->delete_records_select('forum_queue', "discussionid IN ($discussionssql)", $params); - // remove ratings - $DB->delete_records_select('forum_ratings', "post IN ($postssql)", $params); - // all posts - initial posts must be kept in single simple discussion forums $DB->delete_records_select('forum_posts', "discussion IN ($discussionssql) AND parent <> 0", $params); // first all children $DB->delete_records_select('forum_posts', "discussion IN ($discussionssql AND f.type <> 'single') AND parent = 0", $params); // now the initial posts for non single simple @@ -7603,9 +6963,23 @@ function forum_reset_userdata($data) { $status[] = array('component'=>$componentstr, 'item'=>$typesstr, 'error'=>false); } - // remove all ratings + // remove all ratings in this course's forums if (!empty($data->reset_forum_ratings)) { - $DB->delete_records_select('forum_ratings', "post IN ($allpostssql)", $params); + $ratingdeloptions = new stdclass(); + + if ($forums) { + foreach ($forums as $forumid=>$unused) { + if (!$cm = get_coursemodule_from_instance('forum', $forumid)) { + continue; + } + $context = get_context_instance(CONTEXT_MODULE, $cm->id); + + //remove ratings + $ratingdeloptions->contextid = $context->id; + $rm->delete_ratings($ratingdeloptions); + } + } + // remove all grades from gradebook if (empty($data->reset_gradebook_grades)) { forum_reset_gradebook($data->courseid); @@ -7890,20 +7264,6 @@ function forum_convert_to_roles($forum, $forummodid, $teacherroles=array(), return true; } -/** - * Returns array of forum aggregate types - * - * @return array - */ -function forum_get_aggregate_types() { - return array (FORUM_AGGREGATE_NONE => get_string('aggregatenone', 'forum'), - FORUM_AGGREGATE_AVG => get_string('aggregateavg', 'forum'), - FORUM_AGGREGATE_COUNT => get_string('aggregatecount', 'forum'), - FORUM_AGGREGATE_MAX => get_string('aggregatemax', 'forum'), - FORUM_AGGREGATE_MIN => get_string('aggregatemin', 'forum'), - FORUM_AGGREGATE_SUM => get_string('aggregatesum', 'forum')); -} - /** * Returns array of forum layout modes * diff --git a/mod/forum/mod_form.php b/mod/forum/mod_form.php index aa71c9d6133..1f823dc7a96 100644 --- a/mod/forum/mod_form.php +++ b/mod/forum/mod_form.php @@ -106,28 +106,6 @@ class mod_forum_mod_form extends moodleform_mod { $mform->setHelpButton('rssarticles', array('rssarticles', get_string('rssarticles'), 'forum')); } -//------------------------------------------------------------------------------- - /*$mform->addElement('header', '', get_string('grade')); - - $mform->addElement('select', 'assessed', get_string('aggregatetype', 'forum') , forum_get_aggregate_types()); - $mform->setDefault('assessed', 0); - $mform->setHelpButton('assessed', array('assessaggregate', get_string('aggregatetype', 'forum'), 'forum')); - - $mform->addElement('modgrade', 'scale', get_string('grade'), false); - $mform->disabledIf('scale', 'assessed', 'eq', 0); - - $mform->addElement('checkbox', 'ratingtime', get_string('ratingtime', 'forum')); - $mform->disabledIf('ratingtime', 'assessed', 'eq', 0); - - $mform->addElement('date_time_selector', 'assesstimestart', get_string('from')); - $mform->disabledIf('assesstimestart', 'assessed', 'eq', 0); - $mform->disabledIf('assesstimestart', 'ratingtime'); - - $mform->addElement('date_time_selector', 'assesstimefinish', get_string('to')); - $mform->disabledIf('assesstimefinish', 'assessed', 'eq', 0); - $mform->disabledIf('assesstimefinish', 'ratingtime');*/ - - //------------------------------------------------------------------------------- $mform->addElement('header', '', get_string('blockafter', 'forum')); $options = array(); diff --git a/mod/forum/restorelib.php b/mod/forum/restorelib.php index c7d0439b833..117818b5569 100644 --- a/mod/forum/restorelib.php +++ b/mod/forum/restorelib.php @@ -38,11 +38,11 @@ // | forum_posts // |-------------(UL,pk->id,fk->discussion, // | nt->parent,files) - // | | - // | | - // | | - // forum_read forum_ratings - // (UL,pk->id,fk->post (UL,pk->id,fk->post) + // | + // | + // | + // forum_read + // (UL,pk->id,fk->post // // Meaning: pk->primary key field of the table // fk->foreign key to link with parent @@ -529,9 +529,6 @@ $status = forum_restore_files ($rec->old_id, $new_forum_id, $oldid, $newid, $restore); - //Now restore post ratings - $status = forum_ratings_restore_mods($newid,$pos_info,$restore); - } else { $status = false; } @@ -618,7 +615,7 @@ } //This function restores the forum_ratings - function forum_ratings_restore_mods($new_post_id,$info,$restore) { + /*function forum_ratings_restore_mods($new_post_id,$info,$restore) { global $CFG, $DB; $status = true; @@ -676,7 +673,7 @@ } return $status; - } + }*/ //This function converts texts in FORMAT_WIKI to FORMAT_MARKDOWN for //some texts in the module diff --git a/mod/forum/settings.php b/mod/forum/settings.php index 6a8b00754d2..04a83a604e3 100644 --- a/mod/forum/settings.php +++ b/mod/forum/settings.php @@ -88,7 +88,8 @@ $settings->add(new admin_setting_configcheckbox('forum_enabletimedposts', get_st $settings->add(new admin_setting_configcheckbox('forum_logblocked', get_string('logblocked', 'forum'), get_string('configlogblocked', 'forum'), 1)); -$settings->add(new admin_setting_configcheckbox('forum_ajaxrating', get_string('ajaxrating', 'forum'), - get_string('configajaxrating', 'forum'), 0)); +//no using the central ajax setting for ratings. MDL-21657 +//$settings->add(new admin_setting_configcheckbox('forum_ajaxrating', get_string('ajaxrating', 'forum'), +// get_string('configajaxrating', 'forum'), 0)); diff --git a/mod/forum/version.php b/mod/forum/version.php index 2131d5ee2fe..269fa1624ee 100644 --- a/mod/forum/version.php +++ b/mod/forum/version.php @@ -24,8 +24,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -$module->version = 2010041300; -$module->requires = 2010041300; // Requires this Moodle version +$module->version = 2010042200; +$module->requires = 2010042200; // Requires this Moodle version $module->cron = 60; diff --git a/mod/glossary/backuplib.php b/mod/glossary/backuplib.php index cc9e64aa1c0..45e2376aefe 100644 --- a/mod/glossary/backuplib.php +++ b/mod/glossary/backuplib.php @@ -246,7 +246,8 @@ } //Backup glossary_ratings contents (executed from backup_glossary_entries) - function backup_glossary_ratings ($bf,$preferences,$entryid) { + //todo andrew can this be removed? + /*function backup_glossary_ratings ($bf,$preferences,$entryid) { global $CFG, $DB; $status = true; @@ -267,7 +268,7 @@ $status =fwrite ($bf,end_tag("RATINGS",6,true)); } return $status; - } + }*/ //Backup glossary_alias contents (executed from backup_glossary_entries) function backup_glossary_aliases ($bf,$preferences,$entryid) { diff --git a/mod/glossary/db/upgrade.php b/mod/glossary/db/upgrade.php index e8db43b758b..073fbd2e0b2 100644 --- a/mod/glossary/db/upgrade.php +++ b/mod/glossary/db/upgrade.php @@ -260,22 +260,29 @@ function xmldb_glossary_upgrade($oldversion) { upgrade_mod_savepoint($result, 2009110800, 'glossary'); } - if($result && $oldversion < 2010041300) { + if($result && $oldversion < 2010042200) { //migrate glossary_ratings to the central rating table require_once($CFG->dirroot . '/lib/db/upgradelib.php'); //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.scale, g.id AS mid + $sql = "INSERT {rating} (contextid, scaleid, itemid, rating, userid, timecreated, timemodified) +SELECT cxt.id, g.scale, r.entryid AS itemid, r.rating, r.userid, r.time AS timecreated, r.time AS timemodified FROM {glossary_ratings} r JOIN {glossary_entries} ge ON ge.id=r.entryid -JOIN {glossary} g ON g.id=ge.glossaryid'; - $result = $result && upgrade_module_ratings($ratingssql,'glossary'); +JOIN {glossary} g ON g.id=ge.glossaryid +JOIN {course_modules} cm ON cm.instance=g.id +JOIN {context} cxt ON cxt.instanceid=cm.id +JOIN {modules} m ON m.id=cm.module +WHERE m.name = :modname AND cxt.contextlevel = :contextlevel"; - //todo andrew drop glossary_ratings + $params['modname'] = 'glossary'; + $params['contextlevel'] = CONTEXT_MODULE; + + $DB->execute($sql, $params); - //todo andrew set rating permissions based on current value of glossary.assessed + //todo andrew drop glossary_ratings - upgrade_mod_savepoint($result, 2010041300, 'glossary'); + upgrade_mod_savepoint($result, 2010042200, 'glossary'); } return $result; diff --git a/mod/glossary/deleteentry.php b/mod/glossary/deleteentry.php index dca873aecc8..2603e032e37 100644 --- a/mod/glossary/deleteentry.php +++ b/mod/glossary/deleteentry.php @@ -94,8 +94,15 @@ if ($confirm and confirm_sesskey()) { // the operation was confirmed. $fs->delete_area_files($context->id, 'glossary_attachment', $entry->id); $DB->delete_records("comments", array('itemid'=>$entry->id, 'commentarea'=>'glossary_entry', 'contextid'=>$context->id)); $DB->delete_records("glossary_alias", array("entryid"=>$entry->id)); - $DB->delete_records("glossary_ratings", array("entryid"=>$entry->id)); $DB->delete_records("glossary_entries", array("id"=>$entry->id)); + + //delete glossary entry ratings + require_once($CFG->dirroot.'/rating/lib.php'); + $delopt = new stdclass(); + $delopt->contextid = $context->id; + $delopt->itemid = $entry->id; + $rm = new rating_manager(); + $rm->delete_ratings($delopt); } add_to_log($course->id, "glossary", "delete entry", "view.php?id=$cm->id&mode=$prevmode&hook=$hook", $entry->id,$cm->id); diff --git a/mod/glossary/formats/TEMPLATE/TEMPLATE_format.php b/mod/glossary/formats/TEMPLATE/TEMPLATE_format.php index 22f27dfa4af..d7b3550caad 100755 --- a/mod/glossary/formats/TEMPLATE/TEMPLATE_format.php +++ b/mod/glossary/formats/TEMPLATE/TEMPLATE_format.php @@ -1,6 +1,6 @@ '; echo ''; @@ -102,11 +99,9 @@ function glossary_show_entry_TEMPLATE($course, $cm, $glossary, $entry, $mode='', print_string('noentry', 'glossary'); echo ''; } - - return $return; } -function glossary_print_entry_TEMPLATE($course, $cm, $glossary, $entry, $mode='', $hook='', $printicons=1, $ratings=NULL) { +function glossary_print_entry_TEMPLATE($course, $cm, $glossary, $entry, $mode='', $hook='', $printicons=1) { //The print view for this format is exactly the normal view, so we use it //Anyway, you can modify this to use your own print format!! diff --git a/mod/glossary/formats/continuous/continuous_format.php b/mod/glossary/formats/continuous/continuous_format.php index 8c50003ec23..581bd3e712f 100644 --- a/mod/glossary/formats/continuous/continuous_format.php +++ b/mod/glossary/formats/continuous/continuous_format.php @@ -1,6 +1,6 @@ '; echo ''; - $return = glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook, $printicons, $ratings, $aliases); + glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook, $printicons, $aliases); echo ''; echo ''; echo "\n"; - - return $return; } -function glossary_print_entry_continuous($course, $cm, $glossary, $entry, $mode='', $hook='', $printicons=1, $ratings=NULL) { +function glossary_print_entry_continuous($course, $cm, $glossary, $entry, $mode='', $hook='', $printicons=1) { //The print view for this format is exactly the normal view, so we use it @@ -33,7 +31,7 @@ function glossary_print_entry_continuous($course, $cm, $glossary, $entry, $mode= $entry->definition = ''.$entry->definition.''; //Call to view function (without icons, ratings and aliases) and return its result - return glossary_show_entry_continuous($course, $cm, $glossary, $entry, $mode, $hook, false, false, false); + glossary_show_entry_continuous($course, $cm, $glossary, $entry, $mode, $hook, false, false, false); } diff --git a/mod/glossary/formats/dictionary/dictionary_format.php b/mod/glossary/formats/dictionary/dictionary_format.php index 0224eaade2b..37cc6eda6f5 100644 --- a/mod/glossary/formats/dictionary/dictionary_format.php +++ b/mod/glossary/formats/dictionary/dictionary_format.php @@ -1,6 +1,6 @@ '; echo ''; - $return = glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook, $printicons, $ratings, $aliases); + glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook, $printicons, $aliases); echo ''; echo ''; echo "\n"; - - return $return; } -function glossary_print_entry_dictionary($course, $cm, $glossary, $entry, $mode='', $hook='', $printicons=1, $ratings=NULL) { +function glossary_print_entry_dictionary($course, $cm, $glossary, $entry, $mode='', $hook='', $printicons=1) { //The print view for this format is exactly the normal view, so we use it diff --git a/mod/glossary/formats/encyclopedia/encyclopedia_format.php b/mod/glossary/formats/encyclopedia/encyclopedia_format.php index 52248483c41..5b301f78ebc 100644 --- a/mod/glossary/formats/encyclopedia/encyclopedia_format.php +++ b/mod/glossary/formats/encyclopedia/encyclopedia_format.php @@ -1,13 +1,12 @@ get_record('user', array('id'=>$entry->userid)); $strby = get_string('writtenby', 'glossary'); - $return = false; if ($entry) { echo ''; echo ''; @@ -50,11 +49,11 @@ function glossary_show_entry_encyclopedia($course, $cm, $glossary, $entry, $mode } glossary_print_entry_definition($entry, $glossary, $cm); - if ($printicons or $ratings or $aliases) { + if ($printicons or $aliases) { echo ''; echo ''; echo ''; echo ''; echo "
'; - $return = glossary_print_entry_lower_section($course, $cm, $glossary, $entry,$mode,$hook,$printicons,$ratings, $aliases); + glossary_print_entry_lower_section($course, $cm, $glossary, $entry,$mode,$hook,$printicons,$aliases); echo ' '; } @@ -66,11 +65,9 @@ function glossary_show_entry_encyclopedia($course, $cm, $glossary, $entry, $mode print_string('noentry', 'glossary'); echo ''; } - - return $return; } -function glossary_print_entry_encyclopedia($course, $cm, $glossary, $entry, $mode='', $hook='', $printicons=1, $ratings=NULL) { +function glossary_print_entry_encyclopedia($course, $cm, $glossary, $entry, $mode='', $hook='', $printicons=1) { //The print view for this format is exactly the normal view, so we use it @@ -79,7 +76,7 @@ function glossary_print_entry_encyclopedia($course, $cm, $glossary, $entry, $mod //Call to view function (without icons, ratings and aliases) and return its result - return glossary_show_entry_encyclopedia($course, $cm, $glossary, $entry, $mode, $hook, false, false, false); + return glossary_show_entry_encyclopedia($course, $cm, $glossary, $entry, $mode, $hook, false, false); } diff --git a/mod/glossary/formats/entrylist/entrylist_format.php b/mod/glossary/formats/entrylist/entrylist_format.php index a937bd202f8..a7ee88563f3 100644 --- a/mod/glossary/formats/entrylist/entrylist_format.php +++ b/mod/glossary/formats/entrylist/entrylist_format.php @@ -1,6 +1,6 @@ rating) { echo '
'; echo ''; - $return = glossary_print_entry_ratings($course, $entry, $ratings); + $return = glossary_print_entry_ratings($course, $entry); echo ''; } echo '
'; @@ -36,7 +36,7 @@ function glossary_show_entry_entrylist($course, $cm, $glossary, $entry, $mode='' return $return; } -function glossary_print_entry_entrylist($course, $cm, $glossary, $entry, $mode='', $hook='', $printicons=1, $ratings=NULL) { +function glossary_print_entry_entrylist($course, $cm, $glossary, $entry, $mode='', $hook='', $printicons=1) { //The print view for this format is different from the normal view, so we implement it here completely global $CFG, $USER; @@ -52,12 +52,10 @@ function glossary_print_entry_entrylist($course, $cm, $glossary, $entry, $mode=' glossary_print_entry_concept($entry); echo ': '; glossary_print_entry_definition($entry, $glossary, $cm); - $return = glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook, false, false, false); + glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook, false, false); echo '
\n"; - - return $return; } diff --git a/mod/glossary/formats/faq/faq_format.php b/mod/glossary/formats/faq/faq_format.php index 8c3b3559dd0..99ec7597191 100644 --- a/mod/glossary/formats/faq/faq_format.php +++ b/mod/glossary/formats/faq/faq_format.php @@ -1,8 +1,7 @@ '; @@ -34,7 +33,7 @@ function glossary_show_entry_faq($course, $cm, $glossary, $entry, $mode="", $hoo echo ''; echo ''; - $return = glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook, $printicons, $ratings, $aliases); + glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook, $printicons, $aliases); echo ''; } else { @@ -42,10 +41,9 @@ function glossary_show_entry_faq($course, $cm, $glossary, $entry, $mode="", $hoo print_string('noentry', 'glossary'); echo ''; } - return $return; } -function glossary_print_entry_faq($course, $cm, $glossary, $entry, $mode='', $hook='', $printicons=1, $ratings=NULL) { +function glossary_print_entry_faq($course, $cm, $glossary, $entry, $mode='', $hook='', $printicons=1) { //The print view for this format is exactly the normal view, so we use it diff --git a/mod/glossary/formats/fullwithauthor/fullwithauthor_format.php b/mod/glossary/formats/fullwithauthor/fullwithauthor_format.php index 68ba3ae93be..bae5a3184a1 100644 --- a/mod/glossary/formats/fullwithauthor/fullwithauthor_format.php +++ b/mod/glossary/formats/fullwithauthor/fullwithauthor_format.php @@ -1,13 +1,12 @@ get_record('user', array('id'=>$entry->userid)); $strby = get_string('writtenby', 'glossary'); - $return = false; if ($entry) { echo ''; echo ''; @@ -48,7 +47,7 @@ function glossary_show_entry_fullwithauthor($course, $cm, $glossary, $entry, $mo echo ''; echo ''; echo "
 '; - $return = glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook, $printicons, $ratings, $aliases); + glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook, $printicons, $aliases); echo ' '; echo '
\n"; @@ -57,10 +56,9 @@ function glossary_show_entry_fullwithauthor($course, $cm, $glossary, $entry, $mo print_string('noentry', 'glossary'); echo ''; } - return $return; } -function glossary_print_entry_fullwithauthor($course, $cm, $glossary, $entry, $mode="", $hook="", $printicons=1, $ratings=NULL) { +function glossary_print_entry_fullwithauthor($course, $cm, $glossary, $entry, $mode="", $hook="", $printicons=1) { //The print view for this format is exactly the normal view, so we use it diff --git a/mod/glossary/formats/fullwithoutauthor/fullwithoutauthor_format.php b/mod/glossary/formats/fullwithoutauthor/fullwithoutauthor_format.php index 66866f8cbf6..92145b711c8 100644 --- a/mod/glossary/formats/fullwithoutauthor/fullwithoutauthor_format.php +++ b/mod/glossary/formats/fullwithoutauthor/fullwithoutauthor_format.php @@ -1,10 +1,9 @@ '; echo ''; @@ -33,7 +32,7 @@ function glossary_show_entry_fullwithoutauthor($course, $cm, $glossary, $entry, echo ''; echo ''; - $return = glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook, $printicons, $ratings, $aliases); + glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook, $printicons, $aliases); echo ' '; echo ''; @@ -43,10 +42,9 @@ function glossary_show_entry_fullwithoutauthor($course, $cm, $glossary, $entry, print_string('noentry', 'glossary'); echo ''; } - return $return; } -function glossary_print_entry_fullwithoutauthor($course, $cm, $glossary, $entry, $mode="", $hook="", $printicons=1, $ratings=NULL) { +function glossary_print_entry_fullwithoutauthor($course, $cm, $glossary, $entry, $mode="", $hook="", $printicons=1) { //The print view for this format is exactly the normal view, so we use it diff --git a/mod/glossary/lib.php b/mod/glossary/lib.php index dd3cafed425..710fa2ece9b 100644 --- a/mod/glossary/lib.php +++ b/mod/glossary/lib.php @@ -109,9 +109,9 @@ function glossary_update_instance($glossary) { $glossary->timemodified = time(); $glossary->id = $glossary->instance; - if (empty($glossary->userating)) { - $glossary->assessed = 0; - } + //if (empty($glossary->userating)) { +// $glossary->assessed = 0; + // } if (empty($glossary->ratingtime) or empty($glossary->assessed)) { $glossary->assesstimestart = 0; @@ -423,7 +423,7 @@ function glossary_cron () { * @return array array of grades, false if none */ function glossary_get_user_grades($glossary, $userid=0) { - global $DB; + /*global $DB; $params = array('userid'=>$userid, 'gid'=>$glossary->id); @@ -436,7 +436,25 @@ function glossary_get_user_grades($glossary, $userid=0) { $user GROUP BY u.id"; - return $DB->get_records_sql($sql, $params); + return $DB->get_records_sql($sql, $params);*/ + global $CFG; + + require_once($CFG->dirroot.'/rating/lib.php'); + $rm = new rating_manager(); + + $ratingoptions = new stdclass(); + + //need these to work backwards to get a context id. Is there a better way to get contextid from a module instance? + $ratingoptions->modulename = 'glossary'; + $ratingoptions->moduleid = $glossary->id; + + $ratingoptions->userid = $userid; + $ratingoptions->aggregationmethod = $glossary->assessed; + $ratingoptions->scaleid = $glossary->scale; + $ratingoptions->itemtable = 'glossary_entries'; + $ratingoptions->itemtableusercolumn = 'userid'; + + return $rm->get_user_grades($ratingoptions); } /** @@ -760,11 +778,10 @@ function glossary_get_entries_search($concept, $courseid) { * @param string $hook * @param int $printicons * @param int $displayformat - * @param array $ratings * @param bool $printview * @return mixed */ -function glossary_print_entry($course, $cm, $glossary, $entry, $mode='',$hook='',$printicons = 1, $displayformat = -1, $ratings = NULL, $printview = false) { +function glossary_print_entry($course, $cm, $glossary, $entry, $mode='',$hook='',$printicons = 1, $displayformat = -1, $printview = false) { global $USER, $CFG; $return = false; if ( $displayformat < 0 ) { @@ -781,7 +798,7 @@ function glossary_print_entry($course, $cm, $glossary, $entry, $mode='',$hook='' if (file_exists($formatfile)) { include_once($formatfile); if (function_exists($functionname)) { - $return = $functionname($course, $cm, $glossary, $entry,$mode,$hook,$printicons,$ratings); + $return = $functionname($course, $cm, $glossary, $entry,$mode,$hook,$printicons); } else if ($printview) { //If the glossary_print_entry_XXXX function doesn't exist, print default (old) print format $return = glossary_print_entry_default($entry, $glossary, $cm); @@ -1041,21 +1058,18 @@ function glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode='',$h * @param string $mode * @param object $hook * @param bool $printicons - * @param array $ratings * @param bool $aliases - * @return mixed + * @return void */ -function glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook, $printicons, $ratings, $aliases=true) { - +function glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook, $printicons, $aliases=true) { if ($aliases) { $aliases = glossary_print_entry_aliases($course, $cm, $glossary, $entry, $mode, $hook,'html'); } $icons = ''; - $return = ''; if ($printicons) { $icons = glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode, $hook,'html'); } - if ($aliases || $icons || $ratings) { + if ($aliases || $icons || $entry->rating) { echo ''; if ( $aliases ) { echo ''; } - if ($ratings) { + if ($entry->rating) { echo ''; } echo '
' . @@ -1064,14 +1078,13 @@ function glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $m if ($icons) { echo '
'.$icons.'
'; - $return = glossary_print_entry_ratings($course, $entry, $ratings); + glossary_print_entry_ratings($course, $entry); echo '
'; } - return $return; } /** @@ -1743,11 +1756,10 @@ function glossary_sort_entries ( $entry0, $entry1 ) { * @global object * @param object $course * @param object $entry - * @param array $ratings * @return bool */ -function glossary_print_entry_ratings($course, $entry, $ratings = NULL) { - global $USER, $CFG, $DB; +function glossary_print_entry_ratings($course, $entry) { +/* global $USER, $CFG, $DB; $glossary = $DB->get_record('glossary', array('id'=>$entry->glossaryid)); $glossarymod = $DB->get_record('modules', array('name'=>'glossary')); @@ -1757,10 +1769,9 @@ function glossary_print_entry_ratings($course, $entry, $ratings = NULL) { $context = get_context_instance(CONTEXT_MODULE, $cm->id); - $ratingsmenuused = false; if (!empty($ratings) and isloggedin()) { $useratings = true; - if ($ratings->assesstimestart and $ratings->assesstimefinish) { + if ($entry->rating->settings->assesstimestart and $entry->rating->settings->assesstimefinish) { if ($entry->timecreated < $ratings->assesstimestart or $entry->timecreated > $ratings->assesstimefinish) { $useratings = false; } @@ -1781,6 +1792,11 @@ function glossary_print_entry_ratings($course, $entry, $ratings = NULL) { } } return $ratingsmenuused; + */ + global $OUTPUT; + if( !empty($entry->rating) ){ + echo $OUTPUT->render($entry->rating); + } } /** @@ -2128,150 +2144,6 @@ function glossary_count_unrated_entries($glossaryid, $userid) { } } -/** - * Print the multiple ratings on a entry given to the current user by others. - * Scale is an array of ratings - * - * @param int $entryid - * @param array $scale - */ -function glossary_print_ratings_mean($entryid, $scale) { - global $OUTPUT; - static $strrate; - - $mean = glossary_get_ratings_mean($entryid, $scale); - - if ($mean !== "") { - - if (empty($strratings)) { - $strratings = get_string("ratings", "glossary"); - } - - echo "$strratings: "; - $link = new moodle_url("/mod/glossary/report.php?id=$entryid"); - $action = new popup_action('click', $link, "ratings"); - echo $OUTPUT->action_link($link, $mean, $action); - } -} - -/** - * Return the mean rating of a entry given to the current user by others. - * Scale is an array of possible ratings in the scale - * Ratings is an optional simple array of actual ratings (just integers) - * - * @global object - * @param int $entryid - * @param array $scale - * @param array $ratings - * @return string - */ -function glossary_get_ratings_mean($entryid, $scale, $ratings=NULL) { - global $DB; - - if (!$ratings) { - $ratings = array(); - if ($rates = $DB->get_records("glossary_ratings", array("entryid"=>$entryid))) { - foreach ($rates as $rate) { - $ratings[] = $rate->rating; - } - } - } - - $count = count($ratings); - - if ($count == 0) { - return ""; - - } else if ($count == 1) { - return $scale[$ratings[0]]; - - } else { - $total = 0; - foreach ($ratings as $rating) { - $total += $rating; - } - $mean = round( ((float)$total/(float)$count) + 0.001); // Little fudge factor so that 0.5 goes UP - - if (isset($scale[$mean])) { - return $scale[$mean]." ($count)"; - } else { - return "$mean ($count)"; // Should never happen, hopefully - } - } -} - -/** - * Return a summary of entry ratings given to the current user by others. - * Scale is an array of possible ratings in the scale - * Ratings is an optional simple array of actual ratings (just integers) - * - * @global object - * @param int $entryid - * @param array $scale - * @param array $ratings - * @return string - */ -function glossary_get_ratings_summary($entryid, $scale, $ratings=NULL) { - global $DB; - - if (!$ratings) { - $ratings = array(); - if ($rates = $DB->get_records("glossary_ratings", array("entryid"=>$entryid))) { - foreach ($rates as $rate) { - $rating[] = $rate->rating; - } - } - } - - - if (!$count = count($ratings)) { - return ""; - } - - - foreach ($scale as $key => $scaleitem) { - $sumrating[$key] = 0; - } - - foreach ($ratings as $rating) { - $sumrating[$rating]++; - } - - $summary = ""; - foreach ($scale as $key => $scaleitem) { - $summary = $sumrating[$key].$summary; - if ($key > 1) { - $summary = "/$summary"; - } - } - return $summary; -} - -/** - * Print the menu of ratings as part of a larger form. - * If the entry has already been - set that value. - * Scale is an array of ratings - * - * @global object - * @param int $entryid - * @param int $userid - * @param array $scale - */ -function glossary_print_rating_menu($entryid, $userid, $scale) { - global $DB, $OUTPUT; - - static $strrate; - - if (!$rating = $DB->get_record("glossary_ratings", array("userid"=>$userid, "entryid"=>$entryid))) { - $rating->rating = -999; - } - - if (empty($strrate)) { - $strrate = get_string("rate", "glossary"); - } - echo html_writer::select($scale, $entryid, $rating->rating, array('-999'=>"$strrate...")); -} - /** * * Returns the html code to represent any pagging bar. Paramenters are: @@ -2669,7 +2541,7 @@ function glossary_supports($feature) { case FEATURE_COMPLETION_TRACKS_VIEWS: return true; case FEATURE_GRADE_HAS_GRADE: return true; case FEATURE_GRADE_OUTCOMES: return true; - case FEATURE_RATE: return false; + case FEATURE_RATE: return true; default: return null; } diff --git a/mod/glossary/report.php b/mod/glossary/report.php index b05a9ec5c16..529adeefd7a 100644 --- a/mod/glossary/report.php +++ b/mod/glossary/report.php @@ -1,6 +1,7 @@ version = 2010041300; -$module->requires = 2010041300; // Requires this Moodle version +$module->version = 2010042200; +$module->requires = 2010042200; // Requires this Moodle version $module->cron = 0; // Period for cron to check this module (secs) diff --git a/mod/glossary/view.php b/mod/glossary/view.php index d6011c8a69e..9490d90682b 100644 --- a/mod/glossary/view.php +++ b/mod/glossary/view.php @@ -381,7 +381,6 @@ require("sql.php"); /// printing the entries $entriesshown = 0; $currentpivot = ''; -$ratingsmenuused = NULL; $paging = NULL; if ($allentries) { @@ -399,7 +398,27 @@ if ($allentries) { echo $paging; echo ''; - $ratings = NULL; + + //load ratings + require_once('../../rating/lib.php'); + $ratingoptions = new stdclass(); + $ratingoptions->context = $cm->context; + $ratingoptions->items = $allentries; + $ratingoptions->aggregate = $glossary->assessed;//the aggregation method + $ratingoptions->scaleid = $glossary->scale; + $ratingoptions->userid = $USER->id; + //if (!empty($_SERVER['HTTP_REFERER'])) { +// $ratingoptions->returnurl = $_SERVER['HTTP_REFERER']; + // } else { + $ratingoptions->returnurl = $CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id; + //} + $ratingoptions->assesstimestart = $glossary->assesstimestart; + $ratingoptions->assesstimefinish = $glossary->assesstimefinish; + + $rm = new rating_manager(); + $allentries = $rm->get_ratings($ratingoptions); + + /*$ratings = NULL; $ratingsmenuused = false; if ($glossary->assessed and isloggedin() and !isguestuser()) { $ratings = new object(); @@ -418,7 +437,7 @@ if ($allentries) { echo "
"; echo "id\" />"; echo ""; - } + }*/ foreach ($allentries as $entry) { @@ -483,11 +502,7 @@ if ($allentries) { } /// and finally print the entry. - - if ( glossary_print_entry($course, $cm, $glossary, $entry, $mode, $hook,1,$displayformat,$ratings) ) { - $ratingsmenuused = true; - } - + glossary_print_entry($course, $cm, $glossary, $entry, $mode, $hook,1,$displayformat); $entriesshown++; } } @@ -495,18 +510,6 @@ if ( !$entriesshown ) { echo $OUTPUT->box(get_string("noentries","glossary"), "generalbox boxaligncenter boxwidthwide"); } - -if ($ratingsmenuused) { - - echo "
"; - if ($glossary->scale < 0) { - if ($scale = $DB->get_record("scale", array("id"=>abs($glossary->scale)))) { - echo $OUTPUT->help_icon_scale($course->id, $scale); - } - } - echo "
"; -} - if (!empty($formsent)) { // close the form properly if used echo "
"; diff --git a/rating/index.php b/rating/index.php index 79390a7ac92..e3c41ba7778 100644 --- a/rating/index.php +++ b/rating/index.php @@ -30,6 +30,7 @@ $contextid = required_param('contextid', PARAM_INT); $itemid = required_param('itemid', PARAM_INT); $scaleid = required_param('scaleid', PARAM_INT); $sort = optional_param('sort', '', PARAM_ALPHA); +$popup = optional_param('popup', 0, PARAM_INT);//==1 if in a popup window? list($context, $course, $cm) = get_context_info_array($contextid); require_login($course, false, $cm); @@ -39,11 +40,14 @@ if ($sort !== 0) { $url->param('sort', $sort); } $PAGE->set_url($url); +if ($popup) { + $PAGE->set_pagelayout('popup'); +} -if ( !has_capability('moodle/rating:view',$context) ) { +if (!has_capability('moodle/rating:view',$context)) { print_error('noviewrate', 'rating'); } -if ( !has_capability('moodle/rating:viewall',$context) and $USER->id != $item->userid) { +if (!has_capability('moodle/rating:viewall',$context) and $USER->id != $item->userid) { print_error('noviewanyrate', 'rating'); } @@ -60,29 +64,30 @@ $strrating = get_string('rating', 'rating'); $strname = get_string('name'); $strtime = get_string('time'); -//Is there something more meaningful we can put in the title? -//$PAGE->set_title("$strratings: ".format_string($post->subject)); +//Is there something more meaningful we can put in the title? It used to be forum post title $PAGE->set_title("$strratings: ".format_string($itemid)); echo $OUTPUT->header(); -//if (!$ratings = forum_get_ratings($post->id, $sqlsort)) { $ratingoptions = new stdclass(); $ratingoptions->context = $context; $ratingoptions->itemid = $itemid; -$ratingoptions->sort = $sort; +$ratingoptions->sort = $sqlsort; $rm = new rating_manager(); -$ratings = $rm->load_ratings_for_item($ratingoptions); +$ratings = $rm->get_all_ratings_for_item($ratingoptions); if (!$ratings) { - //print_error('noresult', 'forum', '', format_string($post->subject)); - print_error('noresult'); + print_error('noresult', 'forum', '', format_string($itemid)); } else { + $sortargs = "contextid=$contextid&itemid=$itemid&scaleid=$scaleid"; + if($popup) { + $sortargs.="&popup=$popup"; + } echo ""; echo ""; echo ""; - echo ""; - echo ""; - echo ""; + echo ""; + echo ""; + echo ""; echo ""; foreach ($ratings as $rating) { @@ -107,5 +112,7 @@ if (!$ratings) { echo "
"; } -echo $OUTPUT->close_window_button(); +if ($popup) { + echo $OUTPUT->close_window_button(); +} echo $OUTPUT->footer(); diff --git a/rating/lib.php b/rating/lib.php index 0b76899253a..f0cff2e2430 100644 --- a/rating/lib.php +++ b/rating/lib.php @@ -25,7 +25,7 @@ define('RATING_UNSET_RATING', -999); -//define ('RATING_AGGREGATE_NONE', 0); //no ratings +define ('RATING_AGGREGATE_NONE', 0); //no ratings define ('RATING_AGGREGATE_AVERAGE', 1); define ('RATING_AGGREGATE_COUNT', 2); define ('RATING_AGGREGATE_MAXIMUM', 3); @@ -107,13 +107,13 @@ class rating implements renderable { $ratingoptions = new stdclass(); $ratingoptions->context = $this->context; $ratingoptions->items = $items; - $ratingoptions->aggregate = RATING_AGGREGATE_AVERAGE;//we dont actually care what aggregation is applied + $ratingoptions->aggregate = RATING_AGGREGATE_AVERAGE;//we dont actually care what aggregation method is applied $ratingoptions->scaleid = $this->scaleid; $ratingoptions->userid = $this->userid; $rm = new rating_manager(); - $items = $rm->load_ratings($ratingoptions); - if( !isset($items[0]->rating) || !isset($items[0]->rating->id) ) { + $items = $rm->get_ratings($ratingoptions); + if( empty($items) || empty($items[0]->rating) || empty($items[0]->rating->id) ) { $data->contextid = $this->context->id; $data->rating = $rating; $data->scaleid = $this->scaleid; @@ -149,9 +149,9 @@ class rating implements renderable { * Remove this rating from the database * @return void */ - public function delete_rating() { + //public function delete_rating() { //todo implement this if its actually needed - } + //} } //end rating class definition /** @@ -162,6 +162,33 @@ class rating implements renderable { * @since Moodle 2.0 */ class rating_manager { + + /** + * Delete one or more ratings. Specify either a rating id, an item id or just the context id. + * @param object $options { + * contextid => int the context in which the ratings exist [required] + * ratingid => int the id of an individual rating to delete [optional] + * itemid => int delete all ratings attached to this item [optional] + * } + * @return void + */ + public function delete_ratings($options) { + global $DB; + + if( !empty($options->ratingid) ) { + //delete a single rating + $DB->delete_records('rating', array('contextid'=>$options->contextid, 'id'=>$options->ratingid) ); + } + else if( !empty($options->itemid) ) { + //delete all ratings for an item + $DB->delete_records('rating', array('contextid'=>$options->contextid, 'itemid'=>$options->itemid) ); + } + else { + //delete all ratings for this context + $DB->delete_records('rating', array('contextid'=>$options->contextid) ); + } + } + /** * Returns an array of ratings for a given item (forum post, glossary entry etc) * This returns all users ratings for a single item @@ -172,9 +199,14 @@ class rating_manager { * } * @return array an array of ratings */ - public function load_ratings_for_item($options) { + public function get_all_ratings_for_item($options) { global $DB; + $sortclause = ''; + if( !empty($options->sort) ) { + $sortclause = "ORDER BY $options->sort"; + } + $userfields = user_picture::fields('u','uid'); $sql = "SELECT r.id, r.rating, r.itemid, r.userid, r.timemodified, $userfields @@ -182,7 +214,7 @@ class rating_manager { LEFT JOIN {user} u ON r.userid = u.id WHERE r.contextid = :contextid AND r.itemid = :itemid - {$options->sort}"; + {$sortclause}"; $params['contextid'] = $options->context->id; $params['itemid'] = $options->itemid; @@ -200,17 +232,21 @@ class rating_manager { * scaleid => int the scale from which the user can select a rating [required] * userid => int the id of the current user [optional] * returnurl => string the url to return the user to after submitting a rating. Can be left null for ajax requests [optional] + * assesstimestart => int only allow rating of items created after this timestamp [optional] + * assesstimefinish => int only allow rating of items created before this timestamp [optional] * @return array the array of items with their ratings attached at $items[0]->rating */ - public function load_ratings($options) { + public function get_ratings($options) { global $DB, $USER, $PAGE, $CFG; if(empty($options->items)) { return $options->items; } - if (is_null($options->userid)) { + if (empty($options->userid)) { $userid = $USER->id; + } else { + $userid = $options->userid; } $aggregatestr = $this->get_aggregation_method($options->aggregate); @@ -241,7 +277,7 @@ class rating_manager { GROUP BY r.itemid, ur.rating, ur.id, ur.userid, ur.scaleid ORDER BY r.itemid"; - $params['userid'] = $options->userid; + $params['userid'] = $userid; $params['contextid'] = $options->context->id; $ratingsrecords = $DB->get_records_sql($sql, $params); @@ -251,15 +287,26 @@ class rating_manager { $scalemax = null; //we could look for a scale id on each item to allow each item to use a different scale - if($options->scaleid < 0 ) { //if its a scale (not numeric) $scalerecord = $DB->get_record('scale', array('id' => -$options->scaleid)); if ($scalerecord) { - $scaleobj->scaleitems = explode(',', $scalerecord->scale); - $scaleobj->id = $scalerecord->id; + $scalearray = explode(',', $scalerecord->scale); + + //is there a more efficient way to get the indexes to start at 1 instead of 0? + //this will go away when scales are refactored + $c = count($scalearray); + $n = null; + for($i=0; $i<$c; $i++) { + $n = $i+1; + $scaleobj->scaleitems["$n"] = $scalearray[$i];//treat index as a string to allow sorting without changing the value + } + krsort($scaleobj->scaleitems);//have the highest grade scale item appear first + + $scaleobj->id = $options->scaleid;//dont use the one from the record or we "forget" that its negative $scaleobj->name = $scalerecord->name; + $scaleobj->courseid = $scalerecord->courseid; - $scalemax = count($scaleobj->scale)-1; + $scalemax = count($scaleobj->scaleitems); } } else { //its numeric @@ -278,6 +325,14 @@ class rating_manager { $settings->returnurl = $options->returnurl; } + $settings->assesstimestart = $settings->assesstimefinish = null; + if( !empty($options->assesstimestart) ) { + $settings->assesstimestart = $options->assesstimestart; + } + if( !empty($options->assesstimefinish) ) { + $settings->assesstimefinish = $options->assesstimefinish; + } + $settings->permissions = new stdclass(); $settings->permissions->canview = has_capability('moodle/rating:view',$options->context); $settings->permissions->canviewall = has_capability('moodle/rating:viewall',$options->context); @@ -302,6 +357,15 @@ class rating_manager { $rating->aggregate = $rec->aggrrating; //unset($rec->aggrrating); $rating->count = $rec->numratings; //unset($rec->numratings); $rating->rating = $rec->usersrating; //unset($rec->usersrating); + if( !empty($item->created) ) { + $rating->itemtimecreated = $item->created;//the forum_posts table has created instead of timecreated + } + else if(!empty($item->timecreated)) { + $rating->itemtimecreated = $item->timecreated; + } + else { + $rating->itemtimecreated = null; + } break; } } @@ -320,8 +384,14 @@ class rating_manager { $rating->itemid = $item->id; $rating->userid = null; $rating->scaleid = null; + $rating->itemtimecreated = null; } + if( !empty($item->userid) ) { + $rating->itemuserid = $item->userid; + } else { + $rating->itemuserid = null; + } $rating->settings = $settings; $item->rating = $rating; @@ -332,16 +402,118 @@ class rating_manager { if ($rating->rating > $scalemax) { $rating->rating = $scalemax; } + if ($rating->aggregate > $scalemax) { + $rating->aggregate = $scalemax; + } } + return $options->items; } + /** + * Returns an array of grades calculated by aggregating item ratings. + * @param object $options { + * userid => int the id of the user whose items have been rated. NOT the user who submitted the ratings [required] + * aggregationmethod => int the aggregation method to apply when calculating grades ie RATING_AGGREGATE_AVERAGE [required] + * scaleid => int the scale from which the user can select a rating. Used for bounds checking. [required] + * itemtable => int the table containing the items [required] + * itemtableusercolum => int the column of the user table containing the item owner's user id [required] + * + * contextid => int the context in which the rated items exist [optional] + * + * modulename => string the name of the module [optional] + * moduleid => int the id of the module instance [optional] + * + * @return array the array of the user's grades + */ + public function get_user_grades($options) { + global $DB; + + $contextid = null; + + //if the calling code doesn't supply a context id we'll have to figure it out + if( !empty($options->contextid) ) { + $contextid = $options->contextid; + } + else if( !empty($options->cmid) ) { + //not implemented as not currently used although cmid is potentially available (the forum supplies it) + //Is there a convenient way to get a context id from a cm id? + //$cmidnumber = $options->cmidnumber; + } + else if ( !empty($options->modulename) && !empty($options->moduleid) ) { + $modulename = $options->modulename; + $moduleid = $options->moduleid; + + //going direct to the db for the context id seems wrong + list($ctxselect, $ctxjoin) = context_instance_preload_sql('cm.id', CONTEXT_MODULE, 'ctx'); + $sql = "SELECT cm.* $ctxselect + FROM {course_modules} cm + LEFT JOIN {modules} mo ON mo.id = cm.module + LEFT JOIN {{$modulename}} m ON m.id = cm.instance $ctxjoin + WHERE mo.name=:modulename AND m.id=:moduleid"; + $contextrecord = $DB->get_record_sql($sql, array('modulename'=>$modulename, 'moduleid'=>$moduleid), '*', MUST_EXIST); + $contextid = $contextrecord->ctxid; + } + + $params = array(); + $params['contextid']= $contextid; + $itemtable = $options->itemtable; + $itemtableusercolumn= $options->itemtableusercolumn; + $scaleid = $options->scaleid; + $params['userid1'] = $params['userid2'] = $params['userid3'] = $options->userid; + + $aggregationstring = $this->get_aggregation_method($options->aggregationmethod); + + $sql = "SELECT :userid1 as id, :userid2 AS userid, $aggregationstring(r.rating) AS rawgrade + FROM {rating} r + WHERE r.contextid=:contextid + AND r.itemid IN (SELECT i.id AS itemid FROM {{$itemtable}} i WHERE i.{$itemtableusercolumn} = :userid3)"; + + $results = $DB->get_records_sql($sql, $params); + if ($results) { + // it could throw off the grading if count and sum returned a rawgrade higher than scale + // so to prevent it we review the results and ensure that rawgrade does not exceed the scale, if it does we set rawgrade = scale (i.e. full credit) + foreach ($results as $rid=>$result) { + if ($options->scaleid >= 0) { + //numeric + if ($result->rawgrade > $options->scaleid) { + $results[$rid]->rawgrade = $options->scaleid; + } + } else { + //scales + if ($scale = $DB->get_record('scale', array('id' => -$options->scaleid))) { + $scale = explode(',', $scale->scale); + $max = count($scale); + if ($result->rawgrade > $max) { + $results[$rid]->rawgrade = $max; + } + } + } + } + } + return $results; + } + + /** + * Returns array of aggregate types. Used by ratings. + * + * @return array + */ + public function get_aggregate_types() { + return array (RATING_AGGREGATE_NONE => get_string('aggregatenone', 'forum'), + RATING_AGGREGATE_AVERAGE => get_string('aggregateavg', 'forum'), + RATING_AGGREGATE_COUNT => get_string('aggregatecount', 'forum'), + RATING_AGGREGATE_MAXIMUM => get_string('aggregatemax', 'forum'), + RATING_AGGREGATE_MINIMUM => get_string('aggregatemin', 'forum'), + RATING_AGGREGATE_SUM => get_string('aggregatesum', 'forum')); + } + /** * Converts an aggregation method constant into something that can be included in SQL * @param int $aggregate An aggregation constant. For example, RATING_AGGREGATE_AVERAGE. * @return string an SQL aggregation method */ - private function get_aggregation_method($aggregate) { + public function get_aggregation_method($aggregate) { $aggregatestr = null; switch($aggregate){ case RATING_AGGREGATE_AVERAGE: diff --git a/rating/module.js b/rating/module.js index e1467ac7e0d..258b4f1b90b 100644 --- a/rating/module.js +++ b/rating/module.js @@ -34,16 +34,23 @@ M.core_rating={ on : { complete : function(tid, outcome, args) { try { - outcome = this.Y.JSON.parse(outcome.responseText); + var responseobj = this.Y.JSON.parse(outcome.responseText); + var itemid = responseobj.itemid; + + var node = this.Y.one('#ratingaggregate'+itemid); + node.set('innerHTML',responseobj.aggregate); + + var node = this.Y.one('#ratingcount'+itemid); + node.set('innerHTML',"("+responseobj.count+")"); } catch(e) { - //this.form.submit(); - alert(outcome.responseText); + //todo put up an overlay or similar rather than an alert + alert(e.message+" "+outcome.responseText); } if(outcome.success){ //do nothing } else if (outcome.error){ - //todo andrew put up an overlay or similar rather than an alert + //todo put up an overlay or similar rather than an alert alert(outcome.error); } } diff --git a/rating/rate.php b/rating/rate.php index 2fbcca3eabd..37742848ae7 100644 --- a/rating/rate.php +++ b/rating/rate.php @@ -32,7 +32,8 @@ $contextid = required_param('contextid', PARAM_INT); $itemid = required_param('itemid', PARAM_INT); $scaleid = required_param('scaleid', PARAM_INT); $userrating = required_param('rating', PARAM_INT); -$returnurl = required_param('returnurl', null, PARAM_LOCALURL);//required for non-ajax requests +$rateduserid = required_param('rateduserid', PARAM_INT);//which user is being rated. Required to update their grade +$returnurl = required_param('returnurl', PARAM_LOCALURL);//required for non-ajax requests $result = new stdClass; @@ -46,8 +47,6 @@ if( !has_capability('moodle/rating:rate',$context) ) { die(); } -//todo andrew deny access to guest user. Petr to define "guest" - $userid = $USER->id; $PAGE->set_url('/lib/rate.php', array( @@ -66,4 +65,19 @@ $rating = new rating($ratingoptions); $rating->update_rating($userrating); -redirect($CFG->wwwroot.'/'.$returnurl); \ No newline at end of file +//todo add a setting to turn grade updating off for those who don't want them in gradebook +//note that this needs to be done in both rate.php and rate_ajax.php +if(true){ + //tell the module that its grades have changed + if ( !$modinstance = $DB->get_record($cm->modname, array('id' => $cm->instance)) ) { + print_error('invalidid'); + } + $modinstance->cmidnumber = $cm->id; //MDL-12961 + $functionname = $cm->modname.'_update_grades'; + require_once($CFG->dirroot."/mod/{$cm->modname}/lib.php"); + if(function_exists($functionname)) { + $functionname($modinstance, $rateduserid); + } +} + +redirect($returnurl); \ No newline at end of file diff --git a/rating/rate_ajax.php b/rating/rate_ajax.php index 5833a18d7d7..99f12a8fb70 100644 --- a/rating/rate_ajax.php +++ b/rating/rate_ajax.php @@ -32,6 +32,8 @@ $contextid = required_param('contextid', PARAM_INT); $itemid = required_param('itemid', PARAM_INT); $scaleid = required_param('scaleid', PARAM_INT); $userrating = required_param('rating', PARAM_INT); +$rateduserid = required_param('rateduserid', PARAM_INT);//which user is being rated. Required to update their grade +$aggregationmethod = optional_param('aggregation', PARAM_INT);//we're going to calculate the aggregate and return it to the client $result = new stdClass; @@ -51,8 +53,6 @@ if( !has_capability('moodle/rating:rate',$context) ) { die(); } -//todo andrew deny access to guest user. Petr to define "guest" - $userid = $USER->id; $PAGE->set_url('/lib/rate.php', array( @@ -71,6 +71,47 @@ $rating = new rating($ratingoptions); $rating->update_rating($userrating); +//todo add a setting to turn grade updating off for those who don't want them in gradebook +//note that this needs to be done in both rate.php and rate_ajax.php +if(true){ + //tell the module that its grades have changed + if ( $modinstance = $DB->get_record($cm->modname, array('id' => $cm->instance)) ) { + $modinstance->cmidnumber = $cm->id; //MDL-12961 + $functionname = $cm->modname.'_update_grades'; + require_once("../mod/{$cm->modname}/lib.php"); + if(function_exists($functionname)) { + $functionname($modinstance, $rateduserid); + } + } +} + +//need to retrieve the updated item to get its new aggregate value +$item = new stdclass(); +$item->id = $rating->itemid; +$items = array($item); + +//most of $ratingoptions variables are set correctly +$ratingoptions->items = $items; +$ratingoptions->aggregate = $aggregationmethod; + +$rm = new rating_manager(); +$items = $rm->get_ratings($ratingoptions); + +//for custom scales return text not the value +//this scales weirdness will go away when scales are refactored +$scalearray = null; +$aggregatetoreturn = round($items[0]->rating->aggregate,1); +if($rating->scaleid < 0 ) { //if its a scale (not numeric) + $scalerecord = $DB->get_record('scale', array('id' => -$rating->scaleid)); + if ($scalerecord) { + $scalearray = explode(',', $scalerecord->scale); + } + $aggregatetoreturn = $scalearray[$aggregatetoreturn-1]; +} + $result = new stdClass; $result->success = true; +$result->aggregate = $aggregatetoreturn; +$result->count = $items[0]->rating->count; +$result->itemid = $rating->itemid; echo json_encode($result); \ No newline at end of file diff --git a/version.php b/version.php index ae265d78fdb..814f04ec4bc 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 = 2010042100; // YYYYMMDD = date of the last version bump + $version = 2010042200; // YYYYMMDD = date of the last version bump // XX = daily increments $release = '2.0 dev (Build: 20100422)'; // Human-friendly version name -- 2.43.0
 $strname$strrating$strtime$strname$strrating$strtime