MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / glossary / rate.php
blob3d086b7f76c629dbb3de25dbe5cf81ea7f688286
1 <?php // $Id$
3 // Collect ratings, store them, then return to where we came from
6 require_once('../../config.php');
7 require_once('lib.php');
9 $glossaryid = required_param('glossaryid', PARAM_INT); // The forum the rated posts are from
11 if (!$glossary = get_record('glossary', 'id', $glossaryid)) {
12 error("Incorrect glossary id");
15 if (!$course = get_record('course', 'id', $glossary->course)) {
16 error("Course ID was incorrect");
19 if (!$cm = get_coursemodule_from_instance('glossary', $glossary->id)) {
20 error("Course Module ID was incorrect");
23 require_login($course, false, $cm);
25 if (isguestuser()) {
26 error("Guests are not allowed to rate entries.");
29 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
31 if (!$glossary->assessed) {
32 error("Rating of items not allowed!");
35 if ($glossary->assessed == 2) {
36 require_capability('mod/glossary:rate', $context);
39 if (!empty($_SERVER['HTTP_REFERER'])) {
40 $returnurl = $_SERVER['HTTP_REFERER'];
41 } else {
42 $returnurl = $CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id;
45 if ($data = data_submitted()) { // form submitted
46 foreach ((array)$data as $entryid => $rating) {
47 if (!is_numeric($entryid)) {
48 continue;
50 if (!$entry = get_record('glossary_entries', 'id', $entryid)) {
51 continue;
54 if ($entry->glossaryid != $glossary->id) {
55 error("This is not valid entry!");
58 if ($glossary->assesstimestart and $glossary->assesstimefinish) {
59 if ($entry->timecreated < $glossary->assesstimestart or $entry->timecreated > $glossary->assesstimefinish) {
60 // we can not rate this, ignore it - this should not happen anyway unless teacher changes setting
61 continue;
65 if ($entry->userid == $USER->id) {
66 //can not rate own entry
67 continue;
70 if ($oldrating = get_record("glossary_ratings", "userid", $USER->id, "entryid", $entry->id)) {
71 //Check if we must delete the rate
72 if ($rating == -999) {
73 delete_records('glossary_ratings','userid',$oldrating->userid, 'entryid',$oldrating->entryid);
74 glossary_update_grades($glossary, $entry->userid);
76 } else if ($rating != $oldrating->rating) {
77 $oldrating->rating = $rating;
78 $oldrating->time = time();
79 if (! update_record("glossary_ratings", $oldrating)) {
80 error("Could not update an old rating ($entry = $rating)");
82 glossary_update_grades($glossary, $entry->userid);
85 } else if ($rating >= 0) {
86 $newrating = new object();
87 $newrating->userid = $USER->id;
88 $newrating->time = time();
89 $newrating->entryid = $entry->id;
90 $newrating->rating = $rating;
92 if (! insert_record("glossary_ratings", $newrating)) {
93 error("Could not insert a new rating ($entry->id = $rating)");
95 glossary_update_grades($glossary, $entry->userid);
99 redirect($returnurl, get_string("ratingssaved", "glossary"));
101 } else {
102 error("This page was not accessed correctly");