2 require_once('../../config.php');
3 require_once('lib.php');
5 if (!($data = data_submitted($CFG->wwwroot
.'/mod/data/view.php')) or !confirm_sesskey()) {
6 error("This page was not accessed correctly");
11 foreach ((array)$data as $recordid => $rating) {
12 if (!is_numeric($recordid)) {
16 if (!$record = get_record('data_records', 'id', $recordid)) {
17 error("Record ID is incorrect");
19 if (!$data = get_record('data', 'id', $record->dataid
)) {
20 error("Data ID is incorrect");
22 if (!$course = get_record('course', 'id', $data->course
)) {
23 error("Course is misconfigured");
25 if (!$cm = get_coursemodule_from_instance('data', $data->id
, $course->id
)) {
26 error("Course Module ID was incorrect");
29 require_login($course->id
, false, $cm);
31 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
34 error("Guests are not allowed to rate posts");
37 if (!$data->assessed
or !has_capability('mod/data:rate', $context)) {
38 error("Not allowed to rate.");
41 if ($record->userid
== $USER->id
) {
42 error("You can not rate your own data");
45 if (!$scale = make_grades_menu($data->scale
)) {
46 error("Icorrect scale");
49 if (!array_key_exists($rating, $scale)) {
50 error("Icorrect rating value");
53 // input validation ok
57 if ($oldrating = get_record('data_ratings', 'userid', $USER->id
, 'recordid', $record->id
)) {
58 if ($rating != $oldrating->rating
) {
59 $oldrating->rating
= $rating;
60 if (! update_record('data_ratings', $oldrating)) {
61 error("Could not update an old rating ($record->id = $rating)");
65 $newrating = new object();
66 $newrating->userid
= $USER->id
;
67 $newrating->recordid
= $record->id
;
68 $newrating->rating
= $rating;
69 if (! insert_record('data_ratings', $newrating)) {
70 error("Could not insert a new rating ($record->id = $rating)");
76 error("Incorrect submitted ratings data");
79 if (!empty($_SERVER['HTTP_REFERER'])) {
80 redirect($_SERVER['HTTP_REFERER'], get_string('ratingssaved', 'data'));
82 // try to guess where to return
84 redirect('view.php?mode=single&rid='.$record->id
, get_string('ratingssaved', 'data'));
86 redirect('view.php?d='.$data->id
, get_string('ratingssaved', 'data'));