Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
1 <?php 2 3 // This file is part of Moodle - http://moodle.org/ 4 // 5 // Moodle is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // Moodle is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 17 18 /** 19 * @package mod_survey 20 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} 21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 22 */ 23 24 /** 25 * Graph size 26 * @global int $SURVEY_GHEIGHT 27 */ 28 global $SURVEY_GHEIGHT; 29 $SURVEY_GHEIGHT = 500; 30 /** 31 * Graph size 32 * @global int $SURVEY_GWIDTH 33 */ 34 global $SURVEY_GWIDTH; 35 $SURVEY_GWIDTH = 900; 36 /** 37 * Question Type 38 * @global array $SURVEY_QTYPE 39 */ 40 global $SURVEY_QTYPE; 41 $SURVEY_QTYPE = array ( 42 "-3" => "Virtual Actual and Preferred", 43 "-2" => "Virtual Preferred", 44 "-1" => "Virtual Actual", 45 "0" => "Text", 46 "1" => "Actual", 47 "2" => "Preferred", 48 "3" => "Actual and Preferred", 49 ); 50 51 52 define("SURVEY_COLLES_ACTUAL", "1"); 53 define("SURVEY_COLLES_PREFERRED", "2"); 54 define("SURVEY_COLLES_PREFERRED_ACTUAL", "3"); 55 define("SURVEY_ATTLS", "4"); 56 define("SURVEY_CIQ", "5"); 57 // Question length to wrap. 58 define("SURVEY_QLENGTH_WRAP", "80"); 59 60 require_once (__DIR__ . '/deprecatedlib.php'); 61 62 // STANDARD FUNCTIONS //////////////////////////////////////////////////////// 63 /** 64 * Given an object containing all the necessary data, 65 * (defined by the form in mod_form.php) this function 66 * will create a new instance and return the id number 67 * of the new instance. 68 * 69 * @global object 70 * @param object $survey 71 * @return int|bool 72 */ 73 function survey_add_instance($survey) { 74 global $DB; 75 76 if (!$template = $DB->get_record("survey", array("id"=>$survey->template))) { 77 return 0; 78 } 79 80 $survey->questions = $template->questions; 81 $survey->timecreated = time(); 82 $survey->timemodified = $survey->timecreated; 83 84 $id = $DB->insert_record("survey", $survey); 85 86 $completiontimeexpected = !empty($survey->completionexpected) ? $survey->completionexpected : null; 87 \core_completion\api::update_completion_date_event($survey->coursemodule, 'survey', $id, $completiontimeexpected); 88 89 return $id; 90 91 } 92 93 /** 94 * Given an object containing all the necessary data, 95 * (defined by the form in mod_form.php) this function 96 * will update an existing instance with new data. 97 * 98 * @global object 99 * @param object $survey 100 * @return bool 101 */ 102 function survey_update_instance($survey) { 103 global $DB; 104 105 if (!$template = $DB->get_record("survey", array("id"=>$survey->template))) { 106 return 0; 107 } 108 109 $survey->id = $survey->instance; 110 $survey->questions = $template->questions; 111 $survey->timemodified = time(); 112 113 $completiontimeexpected = !empty($survey->completionexpected) ? $survey->completionexpected : null; 114 \core_completion\api::update_completion_date_event($survey->coursemodule, 'survey', $survey->id, $completiontimeexpected); 115 116 return $DB->update_record("survey", $survey); 117 } 118 119 /** 120 * Given an ID of an instance of this module, 121 * this function will permanently delete the instance 122 * and any data that depends on it. 123 * 124 * @global object 125 * @param int $id 126 * @return bool 127 */ 128 function survey_delete_instance($id) { 129 global $DB; 130 131 if (! $survey = $DB->get_record("survey", array("id"=>$id))) { 132 return false; 133 } 134 135 $cm = get_coursemodule_from_instance('survey', $id); 136 \core_completion\api::update_completion_date_event($cm->id, 'survey', $id, null); 137 138 $result = true; 139 140 if (! $DB->delete_records("survey_analysis", array("survey"=>$survey->id))) { 141 $result = false; 142 } 143 144 if (! $DB->delete_records("survey_answers", array("survey"=>$survey->id))) { 145 $result = false; 146 } 147 148 if (! $DB->delete_records("survey", array("id"=>$survey->id))) { 149 $result = false; 150 } 151 152 return $result; 153 } 154 155 /** 156 * @global object 157 * @param object $course 158 * @param object $user 159 * @param object $mod 160 * @param object $survey 161 * @return $result 162 */ 163 function survey_user_outline($course, $user, $mod, $survey) { 164 global $DB; 165 166 if ($answers = $DB->get_records("survey_answers", array('survey'=>$survey->id, 'userid'=>$user->id))) { 167 $lastanswer = array_pop($answers); 168 169 $result = new stdClass(); 170 $result->info = get_string("done", "survey"); 171 $result->time = $lastanswer->time; 172 return $result; 173 } 174 return NULL; 175 } 176 177 /** 178 * @global stdObject 179 * @global object 180 * @uses SURVEY_CIQ 181 * @param object $course 182 * @param object $user 183 * @param object $mod 184 * @param object $survey 185 */ 186 function survey_user_complete($course, $user, $mod, $survey) { 187 global $CFG, $DB, $OUTPUT; 188 189 if (survey_already_done($survey->id, $user->id)) { 190 if ($survey->template == SURVEY_CIQ) { // print out answers for critical incidents 191 $table = new html_table(); 192 $table->align = array("left", "left"); 193 194 $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions)); 195 $questionorder = explode(",", $survey->questions); 196 197 foreach ($questionorder as $key=>$val) { 198 $question = $questions[$val]; 199 $questiontext = get_string($question->shorttext, "survey"); 200 201 if ($answer = survey_get_user_answer($survey->id, $question->id, $user->id)) { 202 $answertext = "$answer->answer1"; 203 } else { 204 $answertext = "No answer"; 205 } 206 $table->data[] = array("<b>$questiontext</b>", s($answertext)); 207 } 208 echo html_writer::table($table); 209 210 } else { 211 212 survey_print_graph("id=$mod->id&sid=$user->id&type=student.png"); 213 } 214 215 } else { 216 print_string("notdone", "survey"); 217 } 218 } 219 220 /** 221 * @global stdClass 222 * @global object 223 * @param object $course 224 * @param mixed $viewfullnames 225 * @param int $timestamp 226 * @return bool 227 */ 228 function survey_print_recent_activity($course, $viewfullnames, $timestart) { 229 global $CFG, $DB, $OUTPUT; 230 231 $modinfo = get_fast_modinfo($course); 232 $ids = array(); 233 foreach ($modinfo->cms as $cm) { 234 if ($cm->modname != 'survey') { 235 continue; 236 } 237 if (!$cm->uservisible) { 238 continue; 239 } 240 $ids[$cm->instance] = $cm->instance; 241 } 242 243 if (!$ids) { 244 return false; 245 } 246 247 $slist = implode(',', $ids); // there should not be hundreds of glossaries in one course, right? 248 249 $userfieldsapi = \core_user\fields::for_userpic(); 250 $allusernames = $userfieldsapi->get_sql('u', false, '', '', false)->selects; 251 $rs = $DB->get_recordset_sql("SELECT sa.userid, sa.survey, MAX(sa.time) AS time, 252 $allusernames 253 FROM {survey_answers} sa 254 JOIN {user} u ON u.id = sa.userid 255 WHERE sa.survey IN ($slist) AND sa.time > ? 256 GROUP BY sa.userid, sa.survey, $allusernames 257 ORDER BY time ASC", array($timestart)); 258 if (!$rs->valid()) { 259 $rs->close(); // Not going to iterate (but exit), close rs 260 return false; 261 } 262 263 $surveys = array(); 264 265 foreach ($rs as $survey) { 266 $cm = $modinfo->instances['survey'][$survey->survey]; 267 $survey->name = $cm->name; 268 $survey->cmid = $cm->id; 269 $surveys[] = $survey; 270 } 271 $rs->close(); 272 273 if (!$surveys) { 274 return false; 275 } 276 277 echo $OUTPUT->heading(get_string('newsurveyresponses', 'survey') . ':', 6); 278 foreach ($surveys as $survey) { 279 $url = $CFG->wwwroot.'/mod/survey/view.php?id='.$survey->cmid; 280 print_recent_activity_note($survey->time, $survey, $survey->name, $url, false, $viewfullnames); 281 } 282 283 return true; 284 } 285 286 // SQL FUNCTIONS //////////////////////////////////////////////////////// 287 288 /** 289 * @global object 290 * @param sting $log 291 * @return array 292 */ 293 function survey_log_info($log) { 294 global $DB; 295 return $DB->get_record_sql("SELECT s.name, u.firstname, u.lastname, u.picture 296 FROM {survey} s, {user} u 297 WHERE s.id = ? AND u.id = ?", array($log->info, $log->userid)); 298 } 299 300 /** 301 * @global object 302 * @param int $surveyid 303 * @param int $groupid 304 * @param int $groupingid 305 * @return array 306 */ 307 function survey_get_responses($surveyid, $groupid, $groupingid) { 308 global $DB; 309 310 $params = array('surveyid'=>$surveyid, 'groupid'=>$groupid, 'groupingid'=>$groupingid); 311 312 if ($groupid) { 313 $groupsjoin = "JOIN {groups_members} gm ON u.id = gm.userid AND gm.groupid = :groupid "; 314 315 } else if ($groupingid) { 316 $groupsjoin = "JOIN {groups_members} gm ON u.id = gm.userid 317 JOIN {groupings_groups} gg ON gm.groupid = gg.groupid AND gg.groupingid = :groupingid "; 318 } else { 319 $groupsjoin = ""; 320 } 321 322 $userfieldsapi = \core_user\fields::for_userpic(); 323 $userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects; 324 return $DB->get_records_sql("SELECT $userfields, MAX(a.time) as time 325 FROM {survey_answers} a 326 JOIN {user} u ON a.userid = u.id 327 $groupsjoin 328 WHERE a.survey = :surveyid 329 GROUP BY $userfields 330 ORDER BY time ASC", $params); 331 } 332 333 /** 334 * @global object 335 * @param int $survey 336 * @param int $user 337 * @return array 338 */ 339 function survey_get_analysis($survey, $user) { 340 global $DB; 341 342 return $DB->get_record_sql("SELECT notes 343 FROM {survey_analysis} 344 WHERE survey=? AND userid=?", array($survey, $user)); 345 } 346 347 /** 348 * @global object 349 * @param int $survey 350 * @param int $user 351 * @param string $notes 352 */ 353 function survey_update_analysis($survey, $user, $notes) { 354 global $DB; 355 356 return $DB->execute("UPDATE {survey_analysis} 357 SET notes=? 358 WHERE survey=? 359 AND userid=?", array($notes, $survey, $user)); 360 } 361 362 /** 363 * @global object 364 * @param int $surveyid 365 * @param int $groupid 366 * @param string $sort 367 * @return array 368 */ 369 function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") { 370 global $DB; 371 372 $params = array('surveyid'=>$surveyid, 'questionid'=>$questionid); 373 374 if ($groupid) { 375 $groupfrom = ', {groups_members} gm'; 376 $groupsql = 'AND gm.groupid = :groupid AND u.id = gm.userid'; 377 $params['groupid'] = $groupid; 378 } else { 379 $groupfrom = ''; 380 $groupsql = ''; 381 } 382 383 $userfieldsapi = \core_user\fields::for_userpic(); 384 $userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects; 385 return $DB->get_records_sql("SELECT sa.*, $userfields 386 FROM {survey_answers} sa, {user} u $groupfrom 387 WHERE sa.survey = :surveyid 388 AND sa.question = :questionid 389 AND u.id = sa.userid $groupsql 390 ORDER BY $sort", $params); 391 } 392 393 /** 394 * @global object 395 * @param int $surveyid 396 * @param int $questionid 397 * @param int $userid 398 * @return array 399 */ 400 function survey_get_user_answer($surveyid, $questionid, $userid) { 401 global $DB; 402 403 return $DB->get_record_sql("SELECT sa.* 404 FROM {survey_answers} sa 405 WHERE sa.survey = ? 406 AND sa.question = ? 407 AND sa.userid = ?", array($surveyid, $questionid, $userid)); 408 } 409 410 // MODULE FUNCTIONS //////////////////////////////////////////////////////// 411 /** 412 * @global object 413 * @param int $survey 414 * @param int $user 415 * @param string $notes 416 * @return bool|int 417 */ 418 function survey_add_analysis($survey, $user, $notes) { 419 global $DB; 420 421 $record = new stdClass(); 422 $record->survey = $survey; 423 $record->userid = $user; 424 $record->notes = $notes; 425 426 return $DB->insert_record("survey_analysis", $record, false); 427 } 428 /** 429 * @global object 430 * @param int $survey 431 * @param int $user 432 * @return bool 433 */ 434 function survey_already_done($survey, $user) { 435 global $DB; 436 437 return $DB->record_exists("survey_answers", array("survey"=>$survey, "userid"=>$user)); 438 } 439 /** 440 * @param int $surveyid 441 * @param int $groupid 442 * @param int $groupingid 443 * @return int 444 */ 445 function survey_count_responses($surveyid, $groupid, $groupingid) { 446 if ($responses = survey_get_responses($surveyid, $groupid, $groupingid)) { 447 return count($responses); 448 } else { 449 return 0; 450 } 451 } 452 453 /** 454 * @param int $cmid 455 * @param array $results 456 * @param int $courseid 457 */ 458 function survey_print_all_responses($cmid, $results, $courseid) { 459 global $OUTPUT; 460 $table = new html_table(); 461 $table->head = array ("", get_string("name"), get_string("time")); 462 $table->align = array ("", "left", "left"); 463 $table->size = array (35, "", "" ); 464 465 foreach ($results as $a) { 466 $table->data[] = array($OUTPUT->user_picture($a, array('courseid'=>$courseid)), 467 html_writer::link("report.php?action=student&student=$a->id&id=$cmid", fullname($a)), 468 userdate($a->time)); 469 } 470 471 echo html_writer::table($table); 472 } 473 474 /** 475 * @global object 476 * @param int $templateid 477 * @return string 478 */ 479 function survey_get_template_name($templateid) { 480 global $DB; 481 482 if ($templateid) { 483 if ($ss = $DB->get_record("surveys", array("id"=>$templateid))) { 484 return $ss->name; 485 } 486 } else { 487 return ""; 488 } 489 } 490 491 492 /** 493 * @param string $name 494 * @param array $numwords 495 * @return string 496 */ 497 function survey_shorten_name ($name, $numwords) { 498 $words = explode(" ", $name); 499 $output = ''; 500 for ($i=0; $i < $numwords; $i++) { 501 $output .= $words[$i]." "; 502 } 503 return $output; 504 } 505 506 /** 507 * @todo Check this function 508 * 509 * @global object 510 * @global object 511 * @global int 512 * @global void This is never defined 513 * @global object This is defined twice? 514 * @param object $question 515 */ 516 function survey_print_multi($question) { 517 global $USER, $DB, $qnum, $DB, $OUTPUT; //TODO: this is sloppy globals abuse 518 519 $stripreferthat = get_string("ipreferthat", "survey"); 520 $strifoundthat = get_string("ifoundthat", "survey"); 521 $strdefault = get_string('notyetanswered', 'survey'); 522 $strresponses = get_string('responses', 'survey'); 523 524 echo $OUTPUT->heading($question->text, 3); 525 echo "\n<table width=\"90%\" cellpadding=\"4\" cellspacing=\"1\" border=\"0\" class=\"surveytable\">"; 526 527 $options = explode( ",", $question->options); 528 $numoptions = count($options); 529 530 // COLLES Actual (which is having questions of type 1) and COLLES Preferred (type 2) 531 // expect just one answer per question. COLLES Actual and Preferred (type 3) expects 532 // two answers per question. ATTLS (having a single question of type 1) expects one 533 // answer per question. CIQ is not using multiquestions (i.e. a question with subquestions). 534 // Note that the type of subquestions does not really matter, it's the type of the 535 // question itself that determines everything. 536 $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false; 537 538 // COLLES Preferred (having questions of type 2) will use the radio elements with the name 539 // like qP1, qP2 etc. COLLES Actual and ATTLS have radios like q1, q2 etc. 540 if ($question->type == 2) { 541 $P = "P"; 542 } else { 543 $P = ""; 544 } 545 546 echo "<colgroup colspan=\"7\"></colgroup>"; 547 echo "<tr class=\"smalltext\"><th scope=\"row\">$strresponses</th>"; 548 echo "<th scope=\"col\" class=\"hresponse\">". get_string('notyetanswered', 'survey'). "</th>"; 549 foreach ($options as $key => $val) { 550 echo "<th scope=\"col\" class=\"hresponse\">$val</th>\n"; 551 } 552 echo "</tr>\n"; 553 554 echo "<tr><th scope=\"colgroup\" colspan=\"7\">$question->intro</th></tr>\n"; 555 556 $subquestions = survey_get_subquestions($question); 557 558 foreach ($subquestions as $q) { 559 $qnum++; 560 if ($oneanswer) { 561 $rowclass = survey_question_rowclass($qnum); 562 } else { 563 $rowclass = survey_question_rowclass(round($qnum / 2)); 564 } 565 if ($q->text) { 566 $q->text = get_string($q->text, "survey"); 567 } 568 569 echo "<tr class=\"$rowclass rblock\">"; 570 if ($oneanswer) { 571 echo "<th scope=\"row\" class=\"optioncell\">"; 572 echo "<b class=\"qnumtopcell\">$qnum</b> "; 573 echo $q->text ."</th>\n"; 574 575 $default = get_accesshide($strdefault); 576 echo "<td class=\"whitecell\"><label for=\"q$P$q->id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"q$P" . $q->id . "_D\" value=\"0\" checked=\"checked\" data-survey-default=\"true\" />$default</label></td>"; 577 578 for ($i=1;$i<=$numoptions;$i++) { 579 $hiddentext = get_accesshide($options[$i-1]); 580 $id = "q$P" . $q->id . "_$i"; 581 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$P$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>"; 582 } 583 } else { 584 echo "<th scope=\"row\" class=\"optioncell\">"; 585 echo "<b class=\"qnumtopcell\">$qnum</b> "; 586 $qnum++; 587 echo "<span class=\"preferthat\">$stripreferthat</span> "; 588 echo "<span class=\"option\">$q->text</span></th>\n"; 589 590 $default = get_accesshide($strdefault); 591 echo '<td class="whitecell"><label for="qP'.$q->id.'"><input type="radio" name="qP'.$q->id.'" id="qP'.$q->id.'" value="0" checked="checked" data-survey-default="true" />'.$default.'</label></td>'; 592 593 594 for ($i=1;$i<=$numoptions;$i++) { 595 $hiddentext = get_accesshide($options[$i-1]); 596 $id = "qP" . $q->id . "_$i"; 597 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"qP$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>"; 598 } 599 echo "</tr>"; 600 601 echo "<tr class=\"$rowclass rblock\">"; 602 echo "<th scope=\"row\" class=\"optioncell\">"; 603 echo "<b class=\"qnumtopcell\">$qnum</b> "; 604 echo "<span class=\"foundthat\">$strifoundthat</span> "; 605 echo "<span class=\"option\">$q->text</span></th>\n"; 606 607 $default = get_accesshide($strdefault); 608 echo '<td class="whitecell"><label for="q'. $q->id .'"><input type="radio" name="q'.$q->id. '" id="q'. $q->id .'" value="0" checked="checked" data-survey-default="true" />'.$default.'</label></td>'; 609 610 for ($i=1;$i<=$numoptions;$i++) { 611 $hiddentext = get_accesshide($options[$i-1]); 612 $id = "q" . $q->id . "_$i"; 613 echo "<td><label for=\"$id\"><input type=\"radio\" name=\"q$q->id\" id=\"$id\" value=\"$i\" />$hiddentext</label></td>"; 614 } 615 } 616 echo "</tr>\n"; 617 } 618 echo "</table>"; 619 } 620 621 622 /** 623 * @global object 624 * @global int 625 * @param object $question 626 */ 627 function survey_print_single($question) { 628 global $DB, $qnum, $OUTPUT; 629 630 $rowclass = survey_question_rowclass(0); 631 632 $qnum++; 633 634 echo "<br />\n"; 635 echo "<table width=\"90%\" cellpadding=\"4\" cellspacing=\"0\">\n"; 636 echo "<tr class=\"$rowclass\">"; 637 echo "<th scope=\"row\" class=\"optioncell\"><label for=\"q$question->id\"><b class=\"qnumtopcell\">$qnum</b> "; 638 echo "<span class=\"questioncell\">$question->text</span></label></th>\n"; 639 echo "<td class=\"questioncell smalltext\">\n"; 640 641 642 if ($question->type == 0) { // Plain text field 643 echo "<textarea rows=\"3\" cols=\"30\" class=\"form-control\" name=\"q$question->id\" id=\"q$question->id\">$question->options</textarea>"; 644 645 } else if ($question->type > 0) { // Choose one of a number 646 $strchoose = get_string("choose"); 647 echo "<select name=\"q$question->id\" id=\"q$question->id\" class=\"custom-select\">"; 648 echo "<option value=\"0\" selected=\"selected\">$strchoose...</option>"; 649 $options = explode( ",", $question->options); 650 foreach ($options as $key => $val) { 651 $key++; 652 echo "<option value=\"$key\">$val</option>"; 653 } 654 echo "</select>"; 655 656 } else if ($question->type < 0) { // Choose several of a number 657 $options = explode( ",", $question->options); 658 echo $OUTPUT->notification("This question type not supported yet"); 659 } 660 661 echo "</td></tr></table>"; 662 663 } 664 665 /** 666 * 667 * @param int $qnum 668 * @return string 669 */ 670 function survey_question_rowclass($qnum) { 671 672 if ($qnum) { 673 return $qnum % 2 ? 'r0' : 'r1'; 674 } else { 675 return 'r0'; 676 } 677 } 678 679 /** 680 * @global object 681 * @global int 682 * @global int 683 * @param string $url 684 */ 685 function survey_print_graph($url) { 686 global $CFG, $SURVEY_GHEIGHT, $SURVEY_GWIDTH; 687 688 echo "<img class='resultgraph' height=\"$SURVEY_GHEIGHT\" width=\"$SURVEY_GWIDTH\"". 689 " src=\"$CFG->wwwroot/mod/survey/graph.php?$url\" alt=\"".get_string("surveygraph", "survey")."\" />"; 690 } 691 692 /** 693 * List the actions that correspond to a view of this module. 694 * This is used by the participation report. 695 * 696 * Note: This is not used by new logging system. Event with 697 * crud = 'r' and edulevel = LEVEL_PARTICIPATING will 698 * be considered as view action. 699 * 700 * @return array 701 */ 702 function survey_get_view_actions() { 703 return array('download','view all','view form','view graph','view report'); 704 } 705 706 /** 707 * List the actions that correspond to a post of this module. 708 * This is used by the participation report. 709 * 710 * Note: This is not used by new logging system. Event with 711 * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING 712 * will be considered as post action. 713 * 714 * @return array 715 */ 716 function survey_get_post_actions() { 717 return array('submit'); 718 } 719 720 721 /** 722 * Implementation of the function for printing the form elements that control 723 * whether the course reset functionality affects the survey. 724 * 725 * @param object $mform form passed by reference 726 */ 727 function survey_reset_course_form_definition(&$mform) { 728 $mform->addElement('header', 'surveyheader', get_string('modulenameplural', 'survey')); 729 $mform->addElement('checkbox', 'reset_survey_answers', get_string('deleteallanswers','survey')); 730 $mform->addElement('checkbox', 'reset_survey_analysis', get_string('deleteanalysis','survey')); 731 $mform->disabledIf('reset_survey_analysis', 'reset_survey_answers', 'checked'); 732 } 733 734 /** 735 * Course reset form defaults. 736 * @return array 737 */ 738 function survey_reset_course_form_defaults($course) { 739 return array('reset_survey_answers'=>1, 'reset_survey_analysis'=>1); 740 } 741 742 /** 743 * Actual implementation of the reset course functionality, delete all the 744 * survey responses for course $data->courseid. 745 * 746 * @global object 747 * @param $data the data submitted from the reset course. 748 * @return array status array 749 */ 750 function survey_reset_userdata($data) { 751 global $DB; 752 753 $componentstr = get_string('modulenameplural', 'survey'); 754 $status = array(); 755 756 $surveyssql = "SELECT ch.id 757 FROM {survey} ch 758 WHERE ch.course=?"; 759 $params = array($data->courseid); 760 761 if (!empty($data->reset_survey_answers)) { 762 $DB->delete_records_select('survey_answers', "survey IN ($surveyssql)", $params); 763 $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params); 764 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false); 765 } 766 767 if (!empty($data->reset_survey_analysis)) { 768 $DB->delete_records_select('survey_analysis', "survey IN ($surveyssql)", $params); 769 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallanswers', 'survey'), 'error'=>false); 770 } 771 772 // No date shifting. 773 // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset. 774 // See MDL-9367. 775 776 return $status; 777 } 778 779 /** 780 * @uses FEATURE_GROUPS 781 * @uses FEATURE_GROUPINGS 782 * @uses FEATURE_MOD_INTRO 783 * @uses FEATURE_COMPLETION_TRACKS_VIEWS 784 * @uses FEATURE_GRADE_HAS_GRADE 785 * @uses FEATURE_GRADE_OUTCOMES 786 * @param string $feature FEATURE_xx constant for requested feature 787 * @return mixed True if module supports feature, false if not, null if doesn't know 788 */ 789 function survey_supports($feature) { 790 switch($feature) { 791 case FEATURE_GROUPS: return true; 792 case FEATURE_GROUPINGS: return true; 793 case FEATURE_MOD_INTRO: return true; 794 case FEATURE_COMPLETION_TRACKS_VIEWS: return true; 795 case FEATURE_COMPLETION_HAS_RULES: return true; 796 case FEATURE_GRADE_HAS_GRADE: return false; 797 case FEATURE_GRADE_OUTCOMES: return false; 798 case FEATURE_BACKUP_MOODLE2: return true; 799 case FEATURE_SHOW_DESCRIPTION: return true; 800 801 default: return null; 802 } 803 } 804 805 /** 806 * This function extends the settings navigation block for the site. 807 * 808 * It is safe to rely on PAGE here as we will only ever be within the module 809 * context when this is called 810 * 811 * @param navigation_node $settings 812 * @param navigation_node $surveynode 813 */ 814 function survey_extend_settings_navigation($settings, $surveynode) { 815 global $PAGE, $DB; 816 817 $cm = get_coursemodule_from_id('survey', $PAGE->cm->id); 818 $context = context_module::instance($cm->id); 819 820 // Check to see if groups are being used in this survey, confirm user can access. 821 $groupmode = groups_get_activity_groupmode($cm); 822 $currentgroup = groups_get_activity_group($cm, true); 823 824 if (has_capability('mod/survey:readresponses', $context) && 825 !($currentgroup === 0 && $groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $context))) { 826 827 $responsesnode = $surveynode->add(get_string("responsereports", "survey")); 828 829 $survey = $DB->get_record("survey", ["id" => $cm->instance]); 830 if ($survey && ($survey->template != SURVEY_CIQ)) { 831 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action' => 'summary')); 832 $responsesnode->add(get_string("summary", "survey"), $url); 833 834 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action' => 'scales')); 835 $responsesnode->add(get_string("scales", "survey"), $url); 836 } 837 838 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'questions')); 839 $responsesnode->add(get_string("question", "survey"), $url); 840 841 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'students')); 842 $responsesnode->add(get_string('participants'), $url); 843 844 if (has_capability('mod/survey:download', $PAGE->cm->context)) { 845 $url = new moodle_url('/mod/survey/report.php', array('id' => $PAGE->cm->id, 'action'=>'download')); 846 $surveynode->add(get_string('downloadresults', 'survey'), $url); 847 } 848 } 849 } 850 851 /** 852 * Return a list of page types 853 * @param string $pagetype current page type 854 * @param stdClass $parentcontext Block's parent context 855 * @param stdClass $currentcontext Current context of block 856 */ 857 function survey_page_type_list($pagetype, $parentcontext, $currentcontext) { 858 $module_pagetype = array('mod-survey-*'=>get_string('page-mod-survey-x', 'survey')); 859 return $module_pagetype; 860 } 861 862 /** 863 * Mark the activity completed (if required) and trigger the course_module_viewed event. 864 * 865 * @param stdClass $survey survey object 866 * @param stdClass $course course object 867 * @param stdClass $cm course module object 868 * @param stdClass $context context object 869 * @param string $viewed which page viewed 870 * @since Moodle 3.0 871 */ 872 function survey_view($survey, $course, $cm, $context, $viewed) { 873 874 // Trigger course_module_viewed event. 875 $params = array( 876 'context' => $context, 877 'objectid' => $survey->id, 878 'courseid' => $course->id, 879 'other' => array('viewed' => $viewed) 880 ); 881 882 $event = \mod_survey\event\course_module_viewed::create($params); 883 $event->add_record_snapshot('course_modules', $cm); 884 $event->add_record_snapshot('course', $course); 885 $event->add_record_snapshot('survey', $survey); 886 $event->trigger(); 887 888 // Completion. 889 $completion = new completion_info($course); 890 $completion->set_module_viewed($cm); 891 } 892 893 /** 894 * Helper function for ordering a set of questions by the given ids. 895 * 896 * @param array $questions array of questions objects 897 * @param array $questionorder array of questions ids indicating the correct order 898 * @return array list of questions ordered 899 * @since Moodle 3.0 900 */ 901 function survey_order_questions($questions, $questionorder) { 902 903 $finalquestions = array(); 904 foreach ($questionorder as $qid) { 905 $finalquestions[] = $questions[$qid]; 906 } 907 return $finalquestions; 908 } 909 910 /** 911 * Translate the question texts and options. 912 * 913 * @param stdClass $question question object 914 * @return stdClass question object with all the text fields translated 915 * @since Moodle 3.0 916 */ 917 function survey_translate_question($question) { 918 919 if ($question->text) { 920 $question->text = get_string($question->text, "survey"); 921 } 922 923 if ($question->shorttext) { 924 $question->shorttext = get_string($question->shorttext, "survey"); 925 } 926 927 if ($question->intro) { 928 $question->intro = get_string($question->intro, "survey"); 929 } 930 931 if ($question->options) { 932 $question->options = get_string($question->options, "survey"); 933 } 934 return $question; 935 } 936 937 /** 938 * Returns the questions for a survey (ordered). 939 * 940 * @param stdClass $survey survey object 941 * @return array list of questions ordered 942 * @since Moodle 3.0 943 * @throws moodle_exception 944 */ 945 function survey_get_questions($survey) { 946 global $DB; 947 948 $questionids = explode(',', $survey->questions); 949 if (! $questions = $DB->get_records_list("survey_questions", "id", $questionids)) { 950 throw new moodle_exception('cannotfindquestion', 'survey'); 951 } 952 953 return survey_order_questions($questions, $questionids); 954 } 955 956 /** 957 * Returns subquestions for a given question (ordered). 958 * 959 * @param stdClass $question questin object 960 * @return array list of subquestions ordered 961 * @since Moodle 3.0 962 */ 963 function survey_get_subquestions($question) { 964 global $DB; 965 966 $questionids = explode(',', $question->multi); 967 $questions = $DB->get_records_list("survey_questions", "id", $questionids); 968 969 return survey_order_questions($questions, $questionids); 970 } 971 972 /** 973 * Save the answer for the given survey 974 * 975 * @param stdClass $survey a survey object 976 * @param array $answersrawdata the answers to be saved 977 * @param stdClass $course a course object (required for trigger the submitted event) 978 * @param stdClass $context a context object (required for trigger the submitted event) 979 * @since Moodle 3.0 980 */ 981 function survey_save_answers($survey, $answersrawdata, $course, $context) { 982 global $DB, $USER; 983 984 $answers = array(); 985 986 // Sort through the data and arrange it. 987 // This is necessary because some of the questions may have two answers, eg Question 1 -> 1 and P1. 988 foreach ($answersrawdata as $key => $val) { 989 if ($key != "userid" && $key != "id") { 990 if (substr($key, 0, 1) == "q") { 991 $key = clean_param(substr($key, 1), PARAM_ALPHANUM); // Keep everything but the 'q', number or P number. 992 } 993 if (substr($key, 0, 1) == "P") { 994 $realkey = (int) substr($key, 1); 995 $answers[$realkey][1] = $val; 996 } else { 997 $answers[$key][0] = $val; 998 } 999 } 1000 } 1001 1002 // Now store the data. 1003 $timenow = time(); 1004 $answerstoinsert = array(); 1005 foreach ($answers as $key => $val) { 1006 if ($key != 'sesskey') { 1007 $newdata = new stdClass(); 1008 $newdata->time = $timenow; 1009 $newdata->userid = $USER->id; 1010 $newdata->survey = $survey->id; 1011 $newdata->question = $key; 1012 if (!empty($val[0])) { 1013 $newdata->answer1 = $val[0]; 1014 } else { 1015 $newdata->answer1 = ""; 1016 } 1017 if (!empty($val[1])) { 1018 $newdata->answer2 = $val[1]; 1019 } else { 1020 $newdata->answer2 = ""; 1021 } 1022 1023 $answerstoinsert[] = $newdata; 1024 } 1025 } 1026 1027 if (!empty($answerstoinsert)) { 1028 $DB->insert_records("survey_answers", $answerstoinsert); 1029 } 1030 1031 // Update completion state. 1032 $cm = get_coursemodule_from_instance('survey', $survey->id, $course->id); 1033 $completion = new completion_info($course); 1034 if (isloggedin() && !isguestuser() && $completion->is_enabled($cm) && $survey->completionsubmit) { 1035 $completion->update_state($cm, COMPLETION_COMPLETE); 1036 } 1037 1038 $params = array( 1039 'context' => $context, 1040 'courseid' => $course->id, 1041 'other' => array('surveyid' => $survey->id) 1042 ); 1043 $event = \mod_survey\event\response_submitted::create($params); 1044 $event->trigger(); 1045 } 1046 1047 /** 1048 * Check if the module has any update that affects the current user since a given time. 1049 * 1050 * @param cm_info $cm course module data 1051 * @param int $from the time to check updates from 1052 * @param array $filter if we need to check only specific updates 1053 * @return stdClass an object with the different type of areas indicating if they were updated or not 1054 * @since Moodle 3.2 1055 */ 1056 function survey_check_updates_since(cm_info $cm, $from, $filter = array()) { 1057 global $DB, $USER; 1058 1059 $updates = new stdClass(); 1060 if (!has_capability('mod/survey:participate', $cm->context)) { 1061 return $updates; 1062 } 1063 $updates = course_check_module_updates_since($cm, $from, array(), $filter); 1064 1065 $updates->answers = (object) array('updated' => false); 1066 $select = 'survey = ? AND userid = ? AND time > ?'; 1067 $params = array($cm->instance, $USER->id, $from); 1068 $answers = $DB->get_records_select('survey_answers', $select, $params, '', 'id'); 1069 if (!empty($answers)) { 1070 $updates->answers->updated = true; 1071 $updates->answers->itemids = array_keys($answers); 1072 } 1073 1074 // Now, teachers should see other students updates. 1075 if (has_capability('mod/survey:readresponses', $cm->context)) { 1076 $select = 'survey = ? AND time > ?'; 1077 $params = array($cm->instance, $from); 1078 1079 if (groups_get_activity_groupmode($cm) == SEPARATEGROUPS) { 1080 $groupusers = array_keys(groups_get_activity_shared_group_members($cm)); 1081 if (empty($groupusers)) { 1082 return $updates; 1083 } 1084 list($insql, $inparams) = $DB->get_in_or_equal($groupusers); 1085 $select .= ' AND userid ' . $insql; 1086 $params = array_merge($params, $inparams); 1087 } 1088 1089 $updates->useranswers = (object) array('updated' => false); 1090 $answers = $DB->get_records_select('survey_answers', $select, $params, '', 'id'); 1091 if (!empty($answers)) { 1092 $updates->useranswers->updated = true; 1093 $updates->useranswers->itemids = array_keys($answers); 1094 } 1095 } 1096 return $updates; 1097 } 1098 1099 /** 1100 * This function receives a calendar event and returns the action associated with it, or null if there is none. 1101 * 1102 * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event 1103 * is not displayed on the block. 1104 * 1105 * @param calendar_event $event 1106 * @param \core_calendar\action_factory $factory 1107 * @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default). 1108 * @return \core_calendar\local\event\entities\action_interface|null 1109 */ 1110 function mod_survey_core_calendar_provide_event_action(calendar_event $event, 1111 \core_calendar\action_factory $factory, 1112 int $userid = 0) { 1113 global $USER; 1114 1115 if (empty($userid)) { 1116 $userid = $USER->id; 1117 } 1118 1119 $cm = get_fast_modinfo($event->courseid, $userid)->instances['survey'][$event->instance]; 1120 $context = context_module::instance($cm->id); 1121 1122 if (!has_capability('mod/survey:participate', $context, $userid)) { 1123 return null; 1124 } 1125 1126 $completion = new \completion_info($cm->get_course()); 1127 1128 $completiondata = $completion->get_data($cm, false, $userid); 1129 1130 if ($completiondata->completionstate != COMPLETION_INCOMPLETE) { 1131 return null; 1132 } 1133 1134 return $factory->create_instance( 1135 get_string('view'), 1136 new \moodle_url('/mod/survey/view.php', ['id' => $cm->id]), 1137 1, 1138 true 1139 ); 1140 } 1141 1142 /** 1143 * Add a get_coursemodule_info function in case any survey type wants to add 'extra' information 1144 * for the course (see resource). 1145 * 1146 * Given a course_module object, this function returns any "extra" information that may be needed 1147 * when printing this activity in a course listing. See get_array_of_activities() in course/lib.php. 1148 * 1149 * @param stdClass $coursemodule The coursemodule object (record). 1150 * @return cached_cm_info An object on information that the courses 1151 * will know about (most noticeably, an icon). 1152 */ 1153 function survey_get_coursemodule_info($coursemodule) { 1154 global $DB; 1155 1156 $dbparams = ['id' => $coursemodule->instance]; 1157 $fields = 'id, name, intro, introformat, completionsubmit'; 1158 if (!$survey = $DB->get_record('survey', $dbparams, $fields)) { 1159 return false; 1160 } 1161 1162 $result = new cached_cm_info(); 1163 $result->name = $survey->name; 1164 1165 if ($coursemodule->showdescription) { 1166 // Convert intro to html. Do not filter cached version, filters run at display time. 1167 $result->content = format_module_intro('survey', $survey, $coursemodule->id, false); 1168 } 1169 1170 // Populate the custom completion rules as key => value pairs, but only if the completion mode is 'automatic'. 1171 if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) { 1172 $result->customdata['customcompletionrules']['completionsubmit'] = $survey->completionsubmit; 1173 } 1174 1175 return $result; 1176 } 1177 1178 /** 1179 * Callback which returns human-readable strings describing the active completion custom rules for the module instance. 1180 * 1181 * @param cm_info|stdClass $cm object with fields ->completion and ->customdata['customcompletionrules'] 1182 * @return array $descriptions the array of descriptions for the custom rules. 1183 */ 1184 function mod_survey_get_completion_active_rule_descriptions($cm) { 1185 // Values will be present in cm_info, and we assume these are up to date. 1186 if (empty($cm->customdata['customcompletionrules']) 1187 || $cm->completion != COMPLETION_TRACKING_AUTOMATIC) { 1188 return []; 1189 } 1190 1191 $descriptions = []; 1192 foreach ($cm->customdata['customcompletionrules'] as $key => $val) { 1193 switch ($key) { 1194 case 'completionsubmit': 1195 if (!empty($val)) { 1196 $descriptions[] = get_string('completionsubmit', 'survey'); 1197 } 1198 break; 1199 default: 1200 break; 1201 } 1202 } 1203 return $descriptions; 1204 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body