Automatic installer.php lang files by installer_builder (20081122)
[moodle.git] / mod / glossary / rate.php
blob6849c7d66f3164e94557c0aa9bf9f2fe945c9d8e
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 $glossary->cmidnumber = $cm->idnumber;
33 if (!$glossary->assessed) {
34 error("Rating of items not allowed!");
37 if ($glossary->assessed == 2) {
38 require_capability('mod/glossary:rate', $context);
41 if (!empty($_SERVER['HTTP_REFERER'])) {
42 $returnurl = $_SERVER['HTTP_REFERER'];
43 } else {
44 $returnurl = $CFG->wwwroot.'/mod/glossary/view.php?id='.$cm->id;
47 if ($data = data_submitted()) { // form submitted
48 foreach ((array)$data as $entryid => $rating) {
49 if (!is_numeric($entryid)) {
50 continue;
52 if (!$entry = get_record('glossary_entries', 'id', $entryid)) {
53 continue;
56 if ($entry->glossaryid != $glossary->id) {
57 error("This is not valid entry!");
60 if ($glossary->assesstimestart and $glossary->assesstimefinish) {
61 if ($entry->timecreated < $glossary->assesstimestart or $entry->timecreated > $glossary->assesstimefinish) {
62 // we can not rate this, ignore it - this should not happen anyway unless teacher changes setting
63 continue;
67 if ($entry->userid == $USER->id) {
68 //can not rate own entry
69 continue;
72 if ($oldrating = get_record("glossary_ratings", "userid", $USER->id, "entryid", $entry->id)) {
73 //Check if we must delete the rate
74 if ($rating == -999) {
75 delete_records('glossary_ratings','userid',$oldrating->userid, 'entryid',$oldrating->entryid);
76 glossary_update_grades($glossary, $entry->userid);
78 } else if ($rating != $oldrating->rating) {
79 $oldrating->rating = $rating;
80 $oldrating->time = time();
81 if (! update_record("glossary_ratings", $oldrating)) {
82 error("Could not update an old rating ($entry = $rating)");
84 glossary_update_grades($glossary, $entry->userid);
87 } else if ($rating >= 0) {
88 $newrating = new object();
89 $newrating->userid = $USER->id;
90 $newrating->time = time();
91 $newrating->entryid = $entry->id;
92 $newrating->rating = $rating;
94 if (! insert_record("glossary_ratings", $newrating)) {
95 error("Could not insert a new rating ($entry->id = $rating)");
97 glossary_update_grades($glossary, $entry->userid);
101 redirect($returnurl, get_string("ratingssaved", "glossary"));
103 } else {
104 error("This page was not accessed correctly");