MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / data / rate.php
blobe756de4878a52eaff14e17fa2a3585487767c51c
1 <?php // $Id$
2 require_once('../../config.php');
3 require_once('lib.php');
5 $dataid = required_param('dataid', PARAM_INT); // The forum the rated posts are from
7 if (!$data = get_record('data', 'id', $dataid)) {
8 error("Incorrect data id");
11 if (!$course = get_record('course', 'id', $data->course)) {
12 error("Course ID was incorrect");
15 if (!$cm = get_coursemodule_from_instance('data', $data->id)) {
16 error("Course Module ID was incorrect");
19 require_login($course, false, $cm);
21 if (isguestuser()) {
22 error("Guests are not allowed to rate entries.");
25 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
26 require_capability('mod/data:rate', $context);
28 if (!$data->assessed) {
29 error("Rating of items not allowed!");
32 if (!$frmdata = data_submitted() or !confirm_sesskey()) {
33 error("This page was not accessed correctly");
36 $count = 0;
38 foreach ((array)$frmdata as $recordid => $rating) {
39 if (!is_numeric($recordid)) {
40 continue;
43 if (!$record = get_record('data_records', 'id', $recordid)) {
44 error("Record ID is incorrect");
47 if ($data->id != $record->dataid) {
48 error("Incorrect record.");
51 if ($record->userid == $USER->id) {
52 continue;
55 // input validation ok
57 $count++;
59 if ($oldrating = get_record('data_ratings', 'userid', $USER->id, 'recordid', $record->id)) {
60 if ($rating == -999) {
61 delete_records('data_ratings', 'userid', $oldrating->userid, 'recordid', $oldrating->recordid);
62 data_update_grades($data, $record->userid);
64 } else if ($rating != $oldrating->rating) {
65 $oldrating->rating = $rating;
66 if (! update_record('data_ratings', $oldrating)) {
67 error("Could not update an old rating ($record->id = $rating)");
69 data_update_grades($data, $record->userid);
73 } else if ($rating) {
74 $newrating = new object();
75 $newrating->userid = $USER->id;
76 $newrating->recordid = $record->id;
77 $newrating->rating = $rating;
78 if (! insert_record('data_ratings', $newrating)) {
79 error("Could not insert a new rating ($record->id = $rating)");
81 data_update_grades($data, $record->userid);
85 if ($count == 0) {
86 error("Incorrect submitted ratings data");
89 if (!empty($_SERVER['HTTP_REFERER'])) {
90 redirect($_SERVER['HTTP_REFERER'], get_string('ratingssaved', 'data'));
91 } else {
92 // try to guess where to return
93 if ($count == 1) {
94 redirect('view.php?mode=single&amp;rid='.$record->id, get_string('ratingssaved', 'data'));
95 } else {
96 redirect('view.php?d='.$data->id, get_string('ratingssaved', 'data'));