-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsave.php
91 lines (75 loc) · 2.56 KB
/
save.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Ajax requests to this script saves the ratings and comments.
*
* Require POST params:
* "save" can be "vote" or "comment" (save type),
* "questionid" is necessary for every request,
* "rate" is necessary if the save type is "vote"
* "text" is necessary if the save type is "comment"
*
* @package qbehaviour_studentquiz
* @copyright 2016 HSR (http://www.hsr.ch)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('AJAX_SCRIPT', true);
require_once(dirname(__FILE__) . '/../../../config.php');
require_login();
$data = new \stdClass();
if (!isset($USER->id) || empty($USER->id)) {
return;
}
$data->userid = $USER->id;
$questionid = required_param('questionid', PARAM_INT);
$data->questionid = $questionid;
$save = required_param('save', PARAM_NOTAGS);
require_sesskey();
switch($save) {
case 'vote': qbehaviour_studentquiz_save_vote($data);
break;
case 'comment': qbehaviour_studentquiz_save_comment($data);
break;
}
header('Content-Type: text/html; charset=utf-8');
/**
* Saves question rating
*
* @param stdClass $data requires userid, questionid
*/
function qbehaviour_studentquiz_save_vote($data) {
global $DB, $USER;
$data->vote = required_param('rate', PARAM_INT);
$row = $DB->get_record('studentquiz_vote', array('userid' => $USER->id, 'questionid' => $data->questionid));
if ($row === false) {
$DB->insert_record('studentquiz_vote', $data);
} else {
$row->vote = $data->vote;
$DB->update_record('studentquiz_vote', $row);
}
}
/**
* Saves question comment
*
* @param stdClass $data requires userid, questionid
*/
function qbehaviour_studentquiz_save_comment($data) {
global $DB;
$text = required_param('text', PARAM_TEXT);
$data->comment = $text;
$data->created = usertime(time(), usertimezone());
$DB->insert_record('studentquiz_comment', $data);
}