Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: danmarsden/moodle-mod_questionnaire
...
head fork: danmarsden/moodle-mod_questionnaire
  • 7 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Showing with 190 additions and 217 deletions.
  1. +38 −23 lib.php
  2. +108 −145 locallib.php
  3. +44 −47 questiontypes/questiontypes.class.php
  4. +0 −2  report.php
View
61 lib.php
@@ -1,4 +1,18 @@
-<?php // $Id$
+<?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/>.
/// Library of functions and constants for module questionnaire
/// (replace questionnaire with the name of your module and delete this line)
@@ -262,13 +276,13 @@ function questionnaire_user_outline($course, $user, $mod, $questionnaire) {
* Get all the questionnaire responses for a user
*/
function questionnaire_get_user_responses($surveyid, $userid) {
- global $CFG, $DB;
+ global $DB;
return $DB->get_records_sql ("SELECT *
- FROM {$CFG->prefix}questionnaire_response
- WHERE survey_id = '$surveyid'
- AND username = '$userid'
- ORDER BY submitted ASC ");
+ FROM {questionnaire_response}
+ WHERE survey_id = ?
+ AND username = ?
+ ORDER BY submitted ASC ", array($surveyid, $userid));
}
function questionnaire_user_complete($course, $user, $mod, $questionnaire) {
@@ -320,15 +334,18 @@ function questionnaire_grades($questionnaireid) {
* @return array array of grades, false if none
*/
function questionnaire_get_user_grades($questionnaire, $userid=0) {
- global $CFG, $DB;
-
- $user = $userid ? "AND u.id = $userid" : "";
+ global $DB;
+ $params = array();
+ if (!empty($userid)) {
+ $usersql = "AND u.id = ?";
+ $params[] = $userid;
+ }
$sql = "SELECT a.id, u.id AS userid, r.grade AS rawgrade, r.submitted AS dategraded, r.submitted AS datesubmitted
- FROM {$CFG->prefix}user u, {$CFG->prefix}questionnaire_attempts a, {$CFG->prefix}questionnaire_response r
- WHERE u.id = a.userid AND a.qid = $questionnaire->id AND r.id = a.rid $user";
+ FROM {user} u, {questionnaire_attempts} a, {questionnaire_response} r
+ WHERE u.id = a.userid AND a.qid = $questionnaire->id AND r.id = a.rid $usersql";
- return $DB->get_records_sql($sql);
+ return $DB->get_records_sql($sql, $params);
}
/**
@@ -367,7 +384,7 @@ function questionnaire_update_grades($questionnaire=null, $userid=0, $nullifnone
} else {
$sql = "SELECT q.*, cm.idnumber as cmidnumber, q.course as courseid
- FROM {$CFG->prefix}questionnaire q, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
+ FROM {questionnaire} q, {course_modules} cm, {modules} m
WHERE m.name='questionnaire' AND m.id=cm.module AND cm.instance=q.id";
if ($rs = $DB->get_recordset_sql($sql)) {
foreach ($rs as $questionnaire) {
@@ -435,12 +452,12 @@ function questionnaire_get_participants($questionnaireid) {
//for a given instance of questionnaire. Must include every user involved
//in the instance, independient of his role (student, teacher, admin...)
//See other modules as example.
- global $CFG, $DB;
+ global $DB;
//Get students
$users = $DB->get_records_sql('SELECT DISTINCT u.* '.
- 'FROM '.$CFG->prefix.'user u, '.
- ' '.$CFG->prefix.'questionnaire_attempts qa '.
+ 'FROM {user} u, '.
+ ' {questionnaire_attempts} qa '.
'WHERE qa.qid = \''.$questionnaireid.'\' AND '.
' u.id = qa.userid');
return ($users);
@@ -498,7 +515,7 @@ function questionnaire_get_context($cmid) {
* @return bool false if file not found, does not return if found - justsend the file
*/
function questionnaire_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
- global $CFG, $DB;
+ global $DB;
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
@@ -577,11 +594,11 @@ function questionnaire_load_capabilities($cmid) {
/// This function *really* shouldn't be needed, but since sometimes we can end up with
/// orphaned surveys, this will clean them up.
function questionnaire_cleanup() {
- global $CFG, $DB;
+ global $DB;
/// Find surveys that don't have questionnaires associated with them.
- $sql = 'SELECT qs.* FROM '.$CFG->prefix.'questionnaire_survey qs '.
- 'LEFT JOIN '.$CFG->prefix.'questionnaire q ON q.sid = qs.id '.
+ $sql = 'SELECT qs.* FROM {questionnaire_survey} qs '.
+ 'LEFT JOIN {questionnaire} q ON q.sid = qs.id '.
'WHERE q.sid IS NULL';
if ($surveys = $DB->get_records_sql($sql)) {
@@ -723,7 +740,7 @@ function questionnaire_survey_exists($sid) {
}
function questionnaire_get_survey_select($instance, $courseid=0, $sid=0, $type='') {
- global $CFG, $OUTPUT;
+ global $OUTPUT;
$surveylist = array();
if ($surveys = questionnaire_get_survey_list($courseid, $type)) {
@@ -959,5 +976,3 @@ function questionnaire_set_events($questionnaire) {
}
}
}
-
-?>
View
253 locallib.php
@@ -1,4 +1,18 @@
-<?php // $Id$
+<?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/>.
/**
* This library replaces the phpESP application with Moodle specific code. It will eventually
@@ -131,7 +145,7 @@ function add_questions($sid = false, $section = false) {
}
function view() {
- global $CFG, $USER, $SESSION, $PAGE, $OUTPUT;
+ global $CFG, $USER, $PAGE, $OUTPUT;
$PAGE->set_title(format_string($this->name));
$PAGE->set_heading(format_string($this->course->fullname));
@@ -142,12 +156,6 @@ function view() {
$questionnaire = $this;
include('tabs.php');
- $strprint = get_string('print', 'questionnaire');
- $strprinttooltip = get_string('printtooltip', 'questionnaire');
- $strprintblank = get_string('printblank', 'questionnaire');
- $strprintblanktooltip = get_string('printblanktooltip', 'questionnaire');
- $blankquestionnaire = true;
-
if (!$this->cm->visible && !$this->capabilities->viewhiddenactivities) {
notice(get_string("activityiscurrentlyhidden"));
}
@@ -192,8 +200,6 @@ function view() {
exit();
}
- $viewform = data_submitted($CFG->wwwroot."/mod/questionnaire/view.php");
-
if ((!empty($this->questions)) && $this->capabilities->printblank) {
// open print friendly as popup window
$image_url = $CFG->wwwroot.'/mod/questionnaire/images/';
@@ -211,11 +217,18 @@ function view() {
$msg = $this->print_survey($USER->id, $quser);
/// If Survey was submitted with all required fields completed ($msg is empty),
/// then record the submittal.
- if (isset($viewform->submit) && isset($viewform->submittype) &&
+ $viewform = data_submitted($CFG->wwwroot."/mod/questionnaire/view.php");
+ if (!empty($viewform->rid)) {
+ $viewform->rid = (int)$viewform->rid;
+ }
+ if (!empty($viewform->sec)) {
+ $viewform->sec = (int)$viewform->sec;
+ }
+ if (data_submitted() && confirm_sesskey() && isset($viewform->submit) && isset($viewform->submittype) &&
($viewform->submittype == "Submit Survey") && empty($msg)) {
$this->response_delete($viewform->rid, $viewform->sec);
- $this->rid = $this->response_insert($this->survey->id, $viewform->sec, $viewform->rid, $quser, $viewform);
+ $this->rid = $this->response_insert($this->survey->id, $viewform->sec, $viewform->rid, $quser);
$this->response_commit($this->rid);
/// If it was a previous save, rid is in the form...
@@ -272,7 +285,7 @@ function view() {
*
*/
function view_response($rid, $blankquestionnaire=false) {
- global $CFG, $OUTPUT;
+ global $OUTPUT;
echo $OUTPUT->box_start();
$this->print_survey_start('', 1, 1, 0, $rid, $blankquestionnaire);
@@ -297,7 +310,7 @@ function view_response($rid, $blankquestionnaire=false) {
*
*/
function view_all_responses($resps) {
- global $CFG, $QTYPENAMES, $OUTPUT;
+ global $QTYPENAMES, $OUTPUT;
echo $OUTPUT->box_start();
$this->print_survey_start('', 1, 1, 0);
@@ -518,13 +531,12 @@ function has_required($section = 0) {
/// Display Methods
function print_survey($userid=false, $quser) {
- global $USER, $PAGE, $CFG;
+ global $CFG;
- if (!$userid) {
- $userid = $USER->id;
+ $formdata = new stdClass();
+ if (data_submitted() && confirm_sesskey()) {
+ $formdata = data_submitted();
}
-
- $formdata = data_submitted('nomatch');
$formdata->rid = $this->get_response($quser);
if (!empty($formdata->rid) && (empty($formdata->sec) || intval($formdata->sec) < 1)) {
$formdata->sec = $this->response_select_max_sec($formdata->rid);
@@ -549,14 +561,14 @@ function print_survey($userid=false, $quser) {
if(!empty($formdata->resume) && ($this->resume)) {
$this->response_delete($formdata->rid, $formdata->sec);
- $formdata->rid = $this->response_insert($this->survey->id, $formdata->sec, $formdata->rid, $quser, $formdata);
+ $formdata->rid = $this->response_insert($this->survey->id, $formdata->sec, $formdata->rid, $quser);
$this->response_goto_saved($action);
return;
}
// JR save each section 's $formdata somewhere in case user returns to that page when navigating the questionnaire...
if(!empty($formdata->next)) {
$this->response_delete($formdata->rid, $formdata->sec);
- $formdata->rid = $this->response_insert($this->survey->id, $formdata->sec, $formdata->rid, $quser, $formdata);
+ $formdata->rid = $this->response_insert($this->survey->id, $formdata->sec, $formdata->rid, $quser);
$msg = $this->response_check_format($formdata->sec, $formdata);
if ( $msg ) {
$formdata->next = '';
@@ -566,7 +578,7 @@ function print_survey($userid=false, $quser) {
}
if (!empty($formdata->prev) && ($this->navigate)) {
$this->response_delete($formdata->rid, $formdata->sec);
- $formdata->rid = $this->response_insert($this->survey->id, $formdata->sec, $formdata->rid, $quser, $formdata);
+ $formdata->rid = $this->response_insert($this->survey->id, $formdata->sec, $formdata->rid, $quser);
$msg = $this->response_check_format($formdata->sec, $formdata);
if ( $msg ) {
$formdata->prev = '';
@@ -661,6 +673,7 @@ function checkbox_empty(name) {
<input type="hidden" name="sid" value="<?php echo($this->survey->id); ?>" />
<input type="hidden" name="rid" value="<?php echo (isset($formdata->rid) ? $formdata->rid : '0'); ?>" />
<input type="hidden" name="sec" value="<?php echo($formdata->sec); ?>" />
+ <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
</div>
<?php
if (isset($this->questions) && $num_sections) { // sanity check
@@ -696,15 +709,12 @@ function checkbox_empty(name) {
function survey_render($section = 1, $message = '', &$formdata) {
- $usehtmleditor = can_use_html_editor();
$this->usehtmleditor = null;
if(empty($section)) {
$section = 1;
}
- $has_choices = $this->type_has_choices();
-
$num_sections = isset($this->questionsbysec) ? count($this->questionsbysec) : 0; /// indexed by section.
if($section > $num_sections) {
return(false); // invalid section
@@ -802,10 +812,9 @@ function print_survey_start($message, $section, $num_sections, $has_required, $r
/// For a public questionnaire, look for the course that used it.
$coursename = '';
$sql = 'SELECT q.id, q.course, c.fullname '.
- 'FROM '.$CFG->prefix.'questionnaire q, '.$CFG->prefix.'questionnaire_attempts qa, '.
- $CFG->prefix.'course c '.
- 'WHERE qa.rid = '.$rid.' AND q.id = qa.qid AND c.id = q.course';
- if ($record = $DB->get_record_sql($sql)) {
+ 'FROM {questionnaire} q, {questionnaire_attempts} qa, {course} c '.
+ 'WHERE qa.rid = ? AND q.id = qa.qid AND c.id = q.course';
+ if ($record = $DB->get_record_sql($sql, array($rid))) {
$coursename = $record->fullname;
}
echo (' '.get_string('course'). ': '.$coursename);
@@ -853,10 +862,6 @@ function survey_print_render($message = '', $referer='', $courseid, $blankquesti
print_error('incorrectcourseid', 'questionnaire');
}
$this->course = $course;
- $text_input_add = ' readonly="true"';
- $radio_input_add = ' onclick="this.checked=this.defaultChecked;"';
- $sid = $this->sid;
- $usehtmleditor = can_use_html_editor();
if ($this->resume && empty($rid)) {
$rid = $this->get_response($USER->id, $rid);
@@ -872,8 +877,6 @@ function survey_print_render($message = '', $referer='', $courseid, $blankquesti
$section = 1;
}
- $has_choices = $this->type_has_choices();
-
$num_sections = isset($this->questionsbysec) ? count($this->questionsbysec) : 0;
if($section > $num_sections)
return(false); // invalid section
@@ -889,7 +892,10 @@ function survey_print_render($message = '', $referer='', $courseid, $blankquesti
echo $OUTPUT->box_start();
$this->print_survey_start($message, 1, 1, $has_required);
/// Print all sections:
- $formdata = data_submitted($referer);
+ $formdata = new stdClass();
+ if (data_submitted() && confirm_sesskey()) {
+ $formdata = data_submitted();
+ }
foreach ($this->questionsbysec as $section) {
foreach ($section as $question) {
if ($question->type_id == QUESSECTIONTEXT) {
@@ -908,15 +914,9 @@ function survey_print_render($message = '', $referer='', $courseid, $blankquesti
}
function survey_update($sdata) {
- global $CFG, $SESSION, $DB;
-
- $errstr = '';
+ global $DB;
- if (empty($sdata)) {
- $sdata = data_submitted('nomatch');
- }
- $f_arr = array();
- $v_arr = array();
+ $errstr = ''; //TODO: notused!
// new survey
if(empty($this->survey->id)) {
@@ -935,8 +935,7 @@ function survey_update($sdata) {
$this->add_survey($this->survey->id);
if(!$this->survey->id) {
- $tab = "general";
- $errstr = get_string('errnewname', 'questionnaire') .' [ : ]';
+ $errstr = get_string('errnewname', 'questionnaire') .' [ : ]'; //TODO: notused!
return(false);
}
} else {
@@ -953,7 +952,7 @@ function survey_update($sdata) {
if(trim($name) != trim(stripslashes($sdata->name))) { // $sdata will already have slashes added to it.
$count = $DB->count_records('questionnaire_survey', array('name' => $sdata->name));
if($count != 0) {
- $errstr = get_string('errnewname', 'questionnaire');
+ $errstr = get_string('errnewname', 'questionnaire'); //TODO: notused!
return(false);
}
}
@@ -967,7 +966,7 @@ function survey_update($sdata) {
$result = $DB->update_record('questionnaire_survey', $survey_record);
if(!$result) {
- $errstr = get_string('warning', 'questionnaire').' [ : ]';
+ $errstr = get_string('warning', 'questionnaire').' [ : ]'; //TODO: notused!
return(false);
}
}
@@ -1007,9 +1006,6 @@ function survey_copy($owner) {
// make copies of all the questions
$pos=1;
foreach ($this->questions as $question) {
- $tid = $question->type_id;
- $qid = $question->id;
-
// fix some fields first
unset($question->id);
$question->survey_id = $new_sid;
@@ -1087,7 +1083,6 @@ function response_check_format($section, &$formdata, $qnum='') {
$tid = $record->type_id;
$lid = $record->length;
$pid = $record->precise;
- $content = $record->content;
if ($tid != 100) {
$qnum++;
}
@@ -1329,7 +1324,6 @@ function response_import_all($rid, &$varr) {
}
function response_commit($rid) {
- global $USER;
global $DB;
$record = new object;
@@ -1383,16 +1377,14 @@ function response_select_max_pos($rid) {
$max = 0;
- global $CFG;
foreach (array('response_bool', 'resp_single', 'resp_multiple', 'response_rank', 'response_text',
'response_other', 'response_date') as $tbl) {
- $sql = 'SELECT MAX(q.position) as num FROM '.$CFG->prefix.'questionnaire_'.$tbl.' a, '.
- $CFG->prefix.'questionnaire_question q '.
- 'WHERE a.response_id = \''.$rid.'\' AND '.
+ $sql = 'SELECT MAX(q.position) as num FROM {questionnaire_'.$tbl.'} a, {questionnaire_question} q '.
+ 'WHERE a.response_id = ? AND '.
'q.id = a.question_id AND '.
- 'q.survey_id = \''.$this->sid.'\' AND '.
+ 'q.survey_id = ? AND '.
'q.deleted = \'n\'';
- if ($record = $DB->get_record_sql($sql)) {
+ if ($record = $DB->get_record_sql($sql, array($rid, $this->sid))) {
$max = (int)$record->num;
}
}
@@ -1405,7 +1397,6 @@ function response_select_max_pos($rid) {
$csvexport = true: a parameter to return a different response formatting for CSV export from normal report formatting
*/
function response_select_name($rid, $choicecodes, $choicetext) {
- global $CFG;
$res = $this->response_select($rid, 'position,type_id,name', true, $choicecodes, $choicetext);
$nam = array();
reset($res);
@@ -1461,13 +1452,10 @@ function response_select_name($rid, $choicecodes, $choicetext) {
}
function response_send_email($rid, $userid=false) {
- global $CFG, $USER;
- global $DB;
+ global $CFG, $USER, $DB;
require_once($CFG->libdir.'/phpmailer/class.phpmailer.php');
- $response = $DB->get_record('questionnaire_response', array('id' => $rid));
-
$name = s($this->name);
if ($record = $DB->get_record('questionnaire_survey', array('id' => $this->survey->id))) {
$email = $record->email;
@@ -1535,8 +1523,7 @@ function response_send_email($rid, $userid=false) {
return $return;
}
- function response_insert($sid, $section, $rid, $userid, &$formdata) {
- global $CFG;
+ function response_insert($sid, $section, $rid, $userid) {
global $DB;
if(empty($rid)) {
@@ -1549,14 +1536,13 @@ function response_insert($sid, $section, $rid, $userid, &$formdata) {
if (!empty($this->questionsbysec[$section])) {
foreach ($this->questionsbysec[$section] as $question) {
- $question->insert_response($rid, $formdata);
+ $question->insert_response($rid);
}
}
return($rid);
}
function response_select($rid, $col = null, $csvexport = false, $choicecodes=0, $choicetext=1) {
- global $CFG;
global $DB;
$sid = $this->survey->id;
@@ -1574,9 +1560,9 @@ function response_select($rid, $col = null, $csvexport = false, $choicecodes=0,
// --------------------- response_bool (yes/no)---------------------
$sql = 'SELECT q.id '.$col.', a.choice_id '.
- 'FROM '.$CFG->prefix.'questionnaire_response_bool a, '.$CFG->prefix.'questionnaire_question q '.
- 'WHERE a.response_id=\''.$rid.'\' AND a.question_id=q.id ';
- if ($records = $DB->get_records_sql($sql)) {
+ 'FROM {questionnaire_response_bool} a, {questionnaire_question} q '.
+ 'WHERE a.response_id= ? AND a.question_id=q.id ';
+ if ($records = $DB->get_records_sql($sql, array($rid))) {
foreach ($records as $qid => $row) {
$choice = $row->choice_id;
if (isset ($row->name) && $row->name == '') {
@@ -1601,19 +1587,15 @@ function response_select($rid, $col = null, $csvexport = false, $choicecodes=0,
// --------------------- response_single (radio button or dropdown)---------------------
$sql = 'SELECT q.id '.$col.', q.type_id as q_type, c.content as ccontent,c.id as cid '.
- 'FROM '.$CFG->prefix.'questionnaire_resp_single a, '.
- $CFG->prefix.'questionnaire_question q, '.
- $CFG->prefix.'questionnaire_quest_choice c '.
- 'WHERE a.response_id=\''.$rid.'\' AND a.question_id=q.id AND a.choice_id=c.id ';
- if ($records = $DB->get_records_sql($sql)) {
+ 'FROM {questionnaire_resp_single} a, {questionnaire_question} q, {questionnaire_quest_choice} c '.
+ 'WHERE a.response_id = ? AND a.question_id=q.id AND a.choice_id=c.id ';
+ if ($records = $DB->get_records_sql($sql, array($rid))) {
foreach ($records as $qid => $row) {
$cid = $row->cid;
$qtype = $row->q_type;
if ($csvexport) {
static $i = 1;
- $sql = 'SELECT * FROM '.$CFG->prefix.'questionnaire_quest_choice WHERE question_id = '.$qid.
- ' ORDER BY '.$CFG->prefix.'questionnaire_quest_choice.id ';
- $qrecords = $DB->get_records_sql($sql);
+ $qrecords = $DB->get_records('questionnaire_quest_choice', array('question_id' => $qid));
foreach($qrecords as $value) {
if ($value->id == $cid) {
$contents = choice_values($value->content);
@@ -1658,15 +1640,14 @@ function response_select($rid, $col = null, $csvexport = false, $choicecodes=0,
// --------------------- response_multiple ---------------------
$sql = 'SELECT a.id as aid, q.id as qid '.$col.',c.content as ccontent,c.id as cid '.
- 'FROM '.$CFG->prefix.'questionnaire_resp_multiple a, '.
- $CFG->prefix.'questionnaire_question q, '.$CFG->prefix.'questionnaire_quest_choice c '.
- 'WHERE a.response_id=\''.$rid.'\' AND a.question_id=q.id AND a.choice_id=c.id '.
+ 'FROM {questionnaire_resp_multiple} a, {questionnaire_question} q, {questionnaire_quest_choice} c '.
+ 'WHERE a.response_id = ? AND a.question_id=q.id AND a.choice_id=c.id '.
'ORDER BY a.id,a.question_id,c.id';
-
+ $records = $DB->get_records_sql($sql, array($rid));
if ($csvexport) {
$tmp = null;
- if ($records = $DB->get_records_sql($sql)) {
+ if (!empty($records)) {
$qids2 = array();
$oldqid = '';
foreach ($records as $qid => $row) {
@@ -1680,7 +1661,7 @@ function response_select($rid, $col = null, $csvexport = false, $choicecodes=0,
} else {
$qids2 = 'question_id= ' . $qids2;
}
- $sql = 'SELECT * FROM '.$CFG->prefix.'questionnaire_quest_choice WHERE '.$qids2.
+ $sql = 'SELECT * FROM {questionnaire_quest_choice} WHERE '.$qids2.
'ORDER BY id';
if ($records2 = $DB->get_records_sql($sql)) {
foreach ($records2 as $qid => $row2) {
@@ -1703,8 +1684,8 @@ function response_select($rid, $col = null, $csvexport = false, $choicecodes=0,
}
}
$sql = 'SELECT a.name as name, a.type_id as q_type, a.position as pos ' .
- 'FROM '.$CFG->prefix.'questionnaire_question a WHERE id = '.$qid2;
- if ($currentquestion = $DB->get_records_sql($sql)) {
+ 'FROM {questionnaire_question} a WHERE id = ?';
+ if ($currentquestion = $DB->get_records_sql($sql, array($qid2))) {
foreach ($currentquestion as $question) {
$name1 = $question->name;
$type1 = $question->q_type;
@@ -1745,7 +1726,7 @@ function response_select($rid, $col = null, $csvexport = false, $choicecodes=0,
} else {
$arr = array();
$tmp = null;
- if ($records = $DB->get_records_sql($sql)) {
+ if (!empty($records)) {
foreach ($records as $aid => $row) {
$qid = $row->qid;
$cid = $row->cid;
@@ -1788,11 +1769,10 @@ function response_select($rid, $col = null, $csvexport = false, $choicecodes=0,
// --------------------- response_other ---------------------
// this will work even for multiple !other fields within one question AND for identical !other responses in different questions JR
$sql = 'SELECT c.id as cid, c.content as content, a.response as aresponse, q.id as qid, q.position as position, q.type_id as type_id, q.name as name '.
- 'FROM '.$CFG->prefix.'questionnaire_response_other a, '.$CFG->prefix.'questionnaire_question q, '.
- $CFG->prefix.'questionnaire_quest_choice c '.
- 'WHERE a.response_id=\''.$rid.'\' AND a.question_id=q.id AND a.choice_id=c.id '.
+ 'FROM {questionnaire_response_other} a, {questionnaire_question} q, {questionnaire_quest_choice} c '.
+ 'WHERE a.response_id= ? AND a.question_id=q.id AND a.choice_id=c.id '.
'ORDER BY a.question_id,c.id ';
- if ($records = $DB->get_records_sql($sql)) {
+ if ($records = $DB->get_records_sql($sql, array($rid))) {
foreach ($records as $record) {
$newrow = array();
$position = $record->position;
@@ -1822,12 +1802,10 @@ function response_select($rid, $col = null, $csvexport = false, $choicecodes=0,
// --------------------- response_rank ---------------------
$sql = 'SELECT a.id as aid, q.id AS qid, q.precise AS precise, c.id AS cid '.$col.',c.content as ccontent,a.rank as arank '.
- 'FROM '.$CFG->prefix.'questionnaire_response_rank a, '.
- $CFG->prefix.'questionnaire_question q, '.
- $CFG->prefix.'questionnaire_quest_choice c '.
- 'WHERE a.response_id=\''.$rid.'\' AND a.question_id=q.id AND a.choice_id=c.id '.
+ 'FROM {questionnaire_response_rank} a, {questionnaire_question} q, {questionnaire_quest_choice c} '.
+ 'WHERE a.response_id= ? AND a.question_id=q.id AND a.choice_id=c.id '.
'ORDER BY aid, a.question_id,c.id';
- if ($records = $DB->get_records_sql($sql)) {
+ if ($records = $DB->get_records_sql($sql, array($rid))) {
foreach ($records as $row) {
/// Next two are 'qid' and 'cid', each with numeric and hash keys.
$osgood = false;
@@ -1877,7 +1855,7 @@ function response_select($rid, $col = null, $csvexport = false, $choicecodes=0,
// --------------------- response_text ---------------------
$sql = 'SELECT q.id '.$col.',a.response as aresponse '.
- 'FROM '.$CFG->prefix.'questionnaire_response_text a, '.$CFG->prefix.'questionnaire_question q '.
+ 'FROM {questionnaire_response_text} a, {questionnaire_question} q '.
'WHERE a.response_id=\''.$rid.'\' AND a.question_id=q.id ';
if ($records = $DB->get_records_sql($sql)) {
foreach ($records as $qid => $row) {
@@ -1897,7 +1875,7 @@ function response_select($rid, $col = null, $csvexport = false, $choicecodes=0,
// --------------------- response_date ---------------------
$sql = 'SELECT q.id '.$col.',a.response as aresponse '.
- 'FROM '.$CFG->prefix.'questionnaire_response_date a, '.$CFG->prefix.'questionnaire_question q '.
+ 'FROM {questionnaire_response_date} a, {questionnaire_question} q '.
'WHERE a.response_id=\''.$rid.'\' AND a.question_id=q.id ';
if ($records = $DB->get_records_sql($sql)) {
$dateformat = get_string('strfdate', 'questionnaire');
@@ -1929,8 +1907,7 @@ function response_select($rid, $col = null, $csvexport = false, $choicecodes=0,
}
function response_goto_thankyou() {
- global $CFG, $USER;
- global $DB;
+ global $CFG, $USER, $DB;
$select = 'id = '.$this->survey->id;
$fields = 'thanks_page,thank_head,thank_body';
@@ -2000,8 +1977,7 @@ function response_goto_saved($url) {
/// Survey Results Methods
function survey_results_navbar($curr_rid, $userid=false) {
- global $CFG;
- global $DB;
+ global $CFG, $DB;
$stranonymous = get_string('anonymous', 'questionnaire');
@@ -2153,11 +2129,9 @@ function survey_results_navbar_student($curr_rid, $userid, $instance, $resps, $r
Returns empty string on sucess, else returns an error
string. */
function survey_results($precision = 1, $showTotals = 1, $qid = '', $cids = '', $rid = '', $guicross='', $uid=false, $groupid='', $sort='') {
- global $CFG, $SESSION;
- global $DB;
+ global $SESSION, $DB;
$SESSION->questionnaire->noresponses = false;
- $bg = '';
if(empty($precision)) {
$precision = 1;
}
@@ -2222,10 +2196,8 @@ function survey_results($precision = 1, $showTotals = 1, $qid = '', $cids = '',
if (!empty($rid)) {
$rids = $rid;
if (is_array($rids)) {
- $ridstr = "IN (" . ereg_replace("([^,]+)","'\\1'", join(",", $rids)) .")";
$navbar = false;
} else {
- $ridstr = "= '${rid}'";
$navbar = true;
}
$total = 1;
@@ -2236,8 +2208,8 @@ function survey_results($precision = 1, $showTotals = 1, $qid = '', $cids = '',
if($cross) {
if(!empty($cidstr))
$sql = "SELECT A.response_id, R.id
- FROM ".$CFG->prefix.'questionnaire_'.$crossTable." A,
- ".$CFG->prefix."questionnaire_response R
+ FROM {questionnaire_".$crossTable."} A,
+ {questionnaire_response} R
WHERE A.response_id=R.id AND
R.complete='y' AND
A.question_id='${qid}' AND
@@ -2245,8 +2217,8 @@ function survey_results($precision = 1, $showTotals = 1, $qid = '', $cids = '',
ORDER BY A.response_id";
else
$sql = "SELECT A.response_id, R.id
- FROM ".$CFG->prefix.'questionnaire_'.$crossTable." A,
- ".$CFG->prefix."questionnaire_response R
+ FROM {questionnaire_".$crossTable."} A,
+ {questionnaire_response} R
WHERE A.response_id=R.id AND
R.complete='y' AND
A.question_id='${qid}' AND
@@ -2254,21 +2226,21 @@ function survey_results($precision = 1, $showTotals = 1, $qid = '', $cids = '',
ORDER BY A.response_id";
} else if ($uid !== false) { // one participant only
$sql = "SELECT r.id, r.survey_id
- FROM ".$CFG->prefix."questionnaire_response r
+ FROM {questionnaire_response} r
WHERE r.survey_id='{$this->survey->id}' AND
r.username = $uid AND
r.complete='y'
ORDER BY r.id";
} else if ($groupid == -1) { // all participants
$sql = "SELECT R.id, R.survey_id
- FROM ".$CFG->prefix."questionnaire_response R
+ FROM {questionnaire_response} R
WHERE R.survey_id='{$this->survey->id}' AND
R.complete='y'
ORDER BY R.id";
} else if ($groupid == -2) { // all members of any group
$sql = "SELECT R.id, R.survey_id
- FROM ".$CFG->prefix."questionnaire_response R,
- ".$CFG->prefix."groups_members GM
+ FROM {questionnaire_response} R,
+ {groups_members} GM
WHERE R.survey_id='{$this->survey->id}' AND
R.complete='y' AND
GM.groupid>0 AND
@@ -2276,16 +2248,16 @@ function survey_results($precision = 1, $showTotals = 1, $qid = '', $cids = '',
ORDER BY R.id";
} else if ($groupid == -3) { // not members of any group
$sql = "SELECT R.id, R.survey_id, U.id AS userid
- FROM ".$CFG->prefix."questionnaire_response R,
- ".$CFG->prefix."user U
+ FROM {questionnaire_response} R,
+ {user} U
WHERE R.survey_id='{$this->survey->id}' AND
R.complete='y' AND
".$castsql."=U.id
ORDER BY userid";
} else { // members of a specific group
$sql = "SELECT R.id, R.survey_id
- FROM ".$CFG->prefix."questionnaire_response R,
- ".$CFG->prefix."groups_members GM
+ FROM {questionnaire_response} R,
+ {groups_members} GM
WHERE R.survey_id='{$this->survey->id}' AND
R.complete='y' AND
GM.groupid=".$groupid." AND
@@ -2295,7 +2267,6 @@ function survey_results($precision = 1, $showTotals = 1, $qid = '', $cids = '',
if (!($rows = $DB->get_records_sql($sql))) {
echo (get_string('noresponses','questionnaire'));
$SESSION->questionnaire->noresponses = true;
- $noresponses = $SESSION->questionnaire->noresponses;
return;
}
if ($groupid == -3) { // members of no group
@@ -2316,10 +2287,6 @@ function survey_results($precision = 1, $showTotals = 1, $qid = '', $cids = '',
foreach ($rows as $row) {
array_push($rids, $row->id);
}
- // create a string suitable for inclusion in a SQL statement
- // containing the desired Response IDs
- // ex: "('304','311','317','345','365','370','403','439')"
- $ridstr = "IN (" . ereg_replace("([^,]+)","'\\1'", join(",", $rids)) .")";
}
if ($navbar) {
@@ -2345,7 +2312,6 @@ function survey_results($precision = 1, $showTotals = 1, $qid = '', $cids = '',
$i=0; // question number counter
foreach ($this->questions as $question) {
// process each question
- $totals = $showTotals;
if ($question->type_id == 99) {
continue;
@@ -2422,8 +2388,7 @@ function survey_results($precision = 1, $showTotals = 1, $qid = '', $cids = '',
Exports the results of a survey to an array.
*/
function generate_csv($rid='', $userid='', $choicecodes=1, $choicetext=0) {
- global $CFG, $SESSION;
- global $DB;
+ global $SESSION, $DB;
if (isset($SESSION->questionnaire->currentgroupid)) {
$groupid = $SESSION->questionnaire->currentgroupid;
@@ -2482,7 +2447,7 @@ function generate_csv($rid='', $userid='', $choicecodes=1, $choicetext=0) {
if (!($records = $DB->get_records_select('questionnaire_question', $select, null, 'position', $fields))) {
$records = array();
}
- global $CFG;
+
$num = 1;
foreach ($records as $record) {
// establish the table's field names
@@ -2495,8 +2460,8 @@ function generate_csv($rid='', $userid='', $choicecodes=1, $choicetext=0) {
if ($type == 4 || $type == 5 || $type == 8) {
/* single or multiple or rate */
$sql = "SELECT c.id as cid, q.id as qid, q.precise AS precise, q.name, c.content
- FROM ".$CFG->prefix."questionnaire_question q ".
- 'LEFT JOIN '.$CFG->prefix."questionnaire_quest_choice c ON question_id = q.id ".
+ FROM {questionnaire_question} q ".
+ "LEFT JOIN {questionnaire_quest_choice} c ON question_id = q.id ".
'WHERE q.id = '.$qid.' ORDER BY cid ASC';
if (!($records2 = $DB->get_records_sql($sql))) {
$records2 = array();
@@ -2602,7 +2567,7 @@ function generate_csv($rid='', $userid='', $choicecodes=1, $choicetext=0) {
}
} else if ($userid) { // download CSV for one user's own responses'
$sql = "SELECT R.id, R.survey_id, R.submitted, R.username
- FROM ".$CFG->prefix."questionnaire_response R
+ FROM {questionnaire_response} R
WHERE R.survey_id='{$this->survey->id}' AND
R.complete='y' AND
R.username='$userid'
@@ -2615,14 +2580,14 @@ function generate_csv($rid='', $userid='', $choicecodes=1, $choicetext=0) {
$castsql = $DB->sql_cast_char2int('R.username');
if ($groupid == -1) { // all participants
$sql = "SELECT R.id, R.survey_id, R.submitted, R.username
- FROM ".$CFG->prefix."questionnaire_response R
+ FROM {questionnaire_response} R
WHERE R.survey_id='{$this->survey->id}' AND
R.complete='y'
ORDER BY R.id";
} else if ($groupid == -2) { // all members of any group
$sql = "SELECT R.id, R.survey_id, R.submitted, R.username
- FROM ".$CFG->prefix."questionnaire_response R,
- ".$CFG->prefix."groups_members GM
+ FROM {questionnaire_response} R,
+ {groups_members} GM
WHERE R.survey_id='{$this->survey->id}' AND
R.complete='y' AND
GM.groupid>0 AND
@@ -2630,16 +2595,16 @@ function generate_csv($rid='', $userid='', $choicecodes=1, $choicetext=0) {
ORDER BY R.id";
} else if ($groupid == -3) { // not members of any group
$sql = "SELECT R.id, R.survey_id, R.submitted, U.id AS username
- FROM ".$CFG->prefix."questionnaire_response R,
- ".$CFG->prefix."user U
+ FROM {questionnaire_response} R,
+ {user} U
WHERE R.survey_id='{$this->survey->id}' AND
R.complete='y' AND
".$castsql."=U.id
ORDER BY username";
} else { // members of a specific group
$sql = "SELECT R.id, R.survey_id, R.submitted, R.username
- FROM ".$CFG->prefix."questionnaire_response R,
- ".$CFG->prefix."groups_members GM
+ FROM {questionnaire_response} R,
+ {groups_members} GM
WHERE R.survey_id='{$this->survey->id}' AND
R.complete='y' AND
GM.groupid=".$groupid." AND
@@ -2683,10 +2648,9 @@ function generate_csv($rid='', $userid='', $choicecodes=1, $choicetext=0) {
} else {
/// For a public questionnaire, look for the course that used it.
$sql = 'SELECT q.id, q.course, c.fullname '.
- 'FROM '.$CFG->prefix.'questionnaire q, '.$CFG->prefix.'questionnaire_attempts qa, '.
- $CFG->prefix.'course c '.
- 'WHERE qa.rid = '.$qid.' AND q.id = qa.qid AND c.id = q.course';
- if ($record = $DB->get_record_sql($sql)) {
+ 'FROM {questionnaire} q, {questionnaire_attempts} qa, {course} c '.
+ 'WHERE qa.rid = ? AND q.id = qa.qid AND c.id = q.course';
+ if ($record = $DB->get_record_sql($sql, array($qid))) {
$courseid = $record->course;
$coursename = $record->fullname;
} else {
@@ -3008,7 +2972,7 @@ function questionnaire_notify($message) {
/// deprecated
function questionnaire_preview ($questionnaire) {
- global $CFG, $DB;
+ global $DB;
/// Print the page header
/// Templates may not have questionnaires yet...
$tempsid = $questionnaire->survey->id; // this is needed for Preview cases later on
@@ -3085,4 +3049,3 @@ function choice_values($content) {
}
return $contents;
}
-?>
View
91 questiontypes/questiontypes.class.php
@@ -201,56 +201,53 @@ function get_choices() {
/// The following methods are defined by the tables they use. Questions should call the
/// appropriate function based on its table.
- function insert_response($rid, $formdata) {
+ function insert_response($rid) {
$method = 'insert_'.$this->response_table;
if (method_exists($this, $method)) {
- return $this->$method($rid, $formdata);
+ return $this->$method($rid);
} else {
return false;
}
}
- function insert_response_bool($rid, $formdata) {
+ function insert_response_bool($rid) {
global $DB;
-
- if (isset($formdata->{'q'.$this->id})
- && !empty($formdata->{'q'.$this->id}) // if "no answer" then choice is empty (CONTRIB-846)
- ) {
+ $val = optional_param('q'.$this->id, '', PARAM_ALPHANUMEXT);
+ if (!empty($val)) { // if "no answer" then choice is empty (CONTRIB-846)
$record = new Object();
$record->response_id = $rid;
$record->question_id = $this->id;
- $record->choice_id = $formdata->{'q'.$this->id};
+ $record->choice_id = $val;
return $DB->insert_record('questionnaire_'.$this->response_table, $record);
} else {
return false;
}
}
- function insert_response_text($rid, $formdata) {
+ function insert_response_text($rid) {
global $DB;
-
+ $val = optional_param('q'.$this->id, '', PARAM_CLEAN);
// only insert if non-empty content
if($this->type_id == 10) { // numeric
- $formdata->{'q'.$this->id} = ereg_replace("[^0-9.\-]*(-?[0-9]*\.?[0-9]*).*", '\1', $formdata->{'q'.$this->id});
+ $val = ereg_replace("[^0-9.\-]*(-?[0-9]*\.?[0-9]*).*", '\1', $val);
}
- if(ereg("[^ \t\n]",$formdata->{'q'.$this->id})) {
+ if(ereg("[^ \t\n]",$val)) {
$record = new Object();
$record->response_id = $rid;
$record->question_id = $this->id;
- $record->response = $formdata->{'q'.$this->id};
+ $record->response = $val;
return $DB->insert_record('questionnaire_'.$this->response_table, $record);
} else {
return false;
}
}
- function insert_response_date($rid, $formdata) {
+ function insert_response_date($rid) {
global $DB;
-
- $checkdateresult = '';
- $checkdateresult = check_date($formdata->{'q'.$this->id});
- $thisdate = $formdata->{'q'.$this->id};
+ $val = optional_param('q'.$this->id, '', PARAM_CLEAN);
+ $checkdateresult = check_date($val);
+ $thisdate = $val;
if (substr($checkdateresult,0,5) == 'wrong') {
return false;
}
@@ -263,16 +260,16 @@ function insert_response_date($rid, $formdata) {
return $DB->insert_record('questionnaire_'.$this->response_table, $record);
}
- function insert_resp_single($rid, $formdata) {
+ function insert_resp_single($rid) {
global $DB;
-
- if(!empty($formdata->{'q'.$this->id})) {
+ $val = optional_param('q'.$this->id, null, PARAM_CLEAN);
+ if(!empty($val)) {
foreach ($this->choices as $cid => $choice) {
if (strpos($choice->content, '!other') === 0) {
- if (!isset($formdata->{'q'.$this->id.'_'.$cid})) {
+ $other = optional_param('q'.$this->id.'_'.$cid, null, PARAM_CLEAN);
+ if (!isset($other)) {
continue;
}
- $other = clean_param($formdata->{'q'.$this->id.'_'.$cid}, PARAM_CLEAN);
if(ereg("[^ \t\n]",$other)) {
$record = new Object();
$record->response_id = $rid;
@@ -280,18 +277,18 @@ function insert_resp_single($rid, $formdata) {
$record->choice_id = $cid;
$record->response = $other;
$resid = $DB->insert_record('questionnaire_response_other', $record);
- $formdata->{'q'.$this->id} = $cid;
+ $val = $cid;
break;
}
}
}
}
- if(ereg("other_q([0-9]+)", (isset($formdata->{'q'.$this->id})?$formdata->{'q'.$this->id}:''), $regs)) {
+ if(ereg("other_q([0-9]+)", (isset($val)?$val:''), $regs)) {
$cid=$regs[1];
- if (!isset($formdata->{'q'.$this->id.'_'.$cid})) {
+ $other = optional_param('q'.$this->id.'_'.$cid, null, PARAM_CLEAN);
+ if (!isset($other)) {
break; // out of the case
}
- $other = clean_param($formdata->{'q'.$this->id.'_'.$cid}, PARAM_CLEAN);
if(ereg("[^ \t\n]",$other)) {
$record = new object;
$record->response_id = $rid;
@@ -299,13 +296,13 @@ function insert_resp_single($rid, $formdata) {
$record->choice_id = $cid;
$record->response = $other;
$resid = $DB->insert_record('questionnaire_response_other', $record);
- $formdata->{'q'.$this->id} = $cid;
+ $val = $cid;
}
}
$record = new Object();
$record->response_id = $rid;
$record->question_id = $this->id;
- $record->choice_id = isset($formdata->{'q'.$this->id}) ? $formdata->{'q'.$this->id} : 0;
+ $record->choice_id = isset($val) ? $val : 0;
if ($record->choice_id) {// if "no answer" then choice_id is empty (CONTRIB-846)
return $DB->insert_record('questionnaire_'.$this->response_table, $record);
} else {
@@ -313,20 +310,20 @@ function insert_resp_single($rid, $formdata) {
}
}
- function insert_resp_multiple($rid, $formdata) {
+ function insert_resp_multiple($rid) {
global $DB;
-
+ $val = optional_param('q'.$this->id, null, PARAM_CLEAN);
foreach ($this->choices as $cid => $choice) {
if (strpos($choice->content, '!other') === 0) {
- if (!isset($formdata->{'q'.$this->id.'_'.$cid}) || empty($formdata->{'q'.$this->id.'_'.$cid})) {
+ $other = optional_param('q'.$this->id.'_'.$cid, '', PARAM_CLEAN);
+ if (empty($other)) {
continue;
}
- if (!isset($formdata->{'q'.$this->id})) {
- $formdata->{'q'.$this->id} = array($cid);
+ if (!isset($val)) {
+ $val = array($cid);
} else {
- array_push($formdata->{'q'.$this->id}, $cid);
+ array_push($val, $cid);
}
- $other = clean_param($formdata->{'q'.$this->id.'_'.$cid}, PARAM_CLEAN);
if(ereg("[^ \t\n]",$other)) {
$record = new Object();
$record->response_id = $rid;
@@ -338,11 +335,11 @@ function insert_resp_multiple($rid, $formdata) {
}
}
- if(!isset($formdata->{'q'.$this->id}) || count($formdata->{'q'.$this->id}) < 1) {
+ if(!isset($val) || count($val) < 1) {
return false;
}
- foreach($formdata->{'q'.$this->id} as $cid) {
+ foreach($val as $cid) {
$cid = clean_param($cid, PARAM_CLEAN);
if ($cid != 0) { //do not save response if choice is empty
if(ereg("other_q[0-9]+", $cid))
@@ -357,20 +354,20 @@ function insert_resp_multiple($rid, $formdata) {
return $resid;
}
- function insert_response_rank($rid, $formdata) {
+ function insert_response_rank($rid) {
global $DB;
-
+ $val = optional_param('q'.$this->id, null, PARAM_CLEAN);
if($this->type_id == 8) { // Rank
$resid = false;
foreach ($this->choices as $cid => $choice) {
- if (!isset($formdata->{'q'.$this->id.'_'.$cid})) {
+ $other = optional_param('q'.$this->id.'_'.$cid, null, PARAM_CLEAN);
+ if (!isset($other)) {
continue;
}
- $val = clean_param($formdata->{'q'.$this->id.'_'.$cid}, PARAM_CLEAN);
- if($val == get_string('notapplicable', 'questionnaire')) {
+ if($other == get_string('notapplicable', 'questionnaire')) {
$rank = -1;
} else {
- $rank = intval($val);
+ $rank = intval($other);
}
$record = new Object();
$record->response_id = $rid;
@@ -381,11 +378,11 @@ function insert_response_rank($rid, $formdata) {
}
return $resid;
} else { // THIS SHOULD NEVER HAPPEN
- $r = clean_param($formdata->{'q'.$this->id}, PARAM_CLEAN);
- if($formdata->{'q'.$this->id} == get_string('notapplicable', 'questionnaire')) {
+ $r = $val;
+ if($val == get_string('notapplicable', 'questionnaire')) {
$rank = -1;
} else {
- $rank = intval($formdata->{'q'.$this->id});
+ $rank = intval($val);
}
$record = new Object();
$record->response_id = $rid;
View
2  report.php
@@ -87,8 +87,6 @@
/// Tab setup:
$SESSION->questionnaire->current_tab = 'allreport';
- $formdata = data_submitted();
-
$strcrossanalyze = get_string('crossanalyze', 'questionnaire');
$strcrosstabulate = get_string('crosstabulate', 'questionnaire');
$strdeleteallresponses = get_string('deleteallresponses', 'questionnaire');

No commit comments for this range

Something went wrong with that request. Please try again.