. /** * @package mod_choice * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ /** @global int $CHOICE_COLUMN_HEIGHT */ global $CHOICE_COLUMN_HEIGHT; $CHOICE_COLUMN_HEIGHT = 300; /** @global int $CHOICE_COLUMN_WIDTH */ global $CHOICE_COLUMN_WIDTH; $CHOICE_COLUMN_WIDTH = 300; define('CHOICE_PUBLISH_ANONYMOUS', '0'); define('CHOICE_PUBLISH_NAMES', '1'); define('CHOICE_SHOWRESULTS_NOT', '0'); define('CHOICE_SHOWRESULTS_AFTER_ANSWER', '1'); define('CHOICE_SHOWRESULTS_AFTER_CLOSE', '2'); define('CHOICE_SHOWRESULTS_ALWAYS', '3'); define('CHOICE_DISPLAY_HORIZONTAL', '0'); define('CHOICE_DISPLAY_VERTICAL', '1'); /** @global array $CHOICE_PUBLISH */ global $CHOICE_PUBLISH; $CHOICE_PUBLISH = array (CHOICE_PUBLISH_ANONYMOUS => get_string('publishanonymous', 'choice'), CHOICE_PUBLISH_NAMES => get_string('publishnames', 'choice')); /** @global array $CHOICE_SHOWRESULTS */ global $CHOICE_SHOWRESULTS; $CHOICE_SHOWRESULTS = array (CHOICE_SHOWRESULTS_NOT => get_string('publishnot', 'choice'), CHOICE_SHOWRESULTS_AFTER_ANSWER => get_string('publishafteranswer', 'choice'), CHOICE_SHOWRESULTS_AFTER_CLOSE => get_string('publishafterclose', 'choice'), CHOICE_SHOWRESULTS_ALWAYS => get_string('publishalways', 'choice')); /** @global array $CHOICE_DISPLAY */ global $CHOICE_DISPLAY; $CHOICE_DISPLAY = array (CHOICE_DISPLAY_HORIZONTAL => get_string('displayhorizontal', 'choice'), CHOICE_DISPLAY_VERTICAL => get_string('displayvertical','choice')); /// Standard functions ///////////////////////////////////////////////////////// /** * @global object * @param object $course * @param object $user * @param object $mod * @param object $choice * @return object|null */ function choice_user_outline($course, $user, $mod, $choice) { global $DB; if ($answer = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $user->id))) { $result = new stdClass(); $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'"; $result->time = $answer->timemodified; return $result; } return NULL; } /** * @global object * @param object $course * @param object $user * @param object $mod * @param object $choice * @return string|void */ function choice_user_complete($course, $user, $mod, $choice) { global $DB; if ($answer = $DB->get_record('choice_answers', array("choiceid" => $choice->id, "userid" => $user->id))) { $result = new stdClass(); $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'"; $result->time = $answer->timemodified; echo get_string("answered", "choice").": $result->info. ".get_string("updated", '', userdate($result->time)); } else { print_string("notanswered", "choice"); } } /** * Given an object containing all the necessary data, * (defined by the form in mod_form.php) this function * will create a new instance and return the id number * of the new instance. * * @global object * @param object $choice * @return int */ function choice_add_instance($choice) { global $DB; $choice->timemodified = time(); if (empty($choice->timerestrict)) { $choice->timeopen = 0; $choice->timeclose = 0; } //insert answers $choice->id = $DB->insert_record("choice", $choice); foreach ($choice->option as $key => $value) { $value = trim($value); if (isset($value) && $value <> '') { $option = new stdClass(); $option->text = $value; $option->choiceid = $choice->id; if (isset($choice->limit[$key])) { $option->maxanswers = $choice->limit[$key]; } $option->timemodified = time(); $DB->insert_record("choice_options", $option); } } return $choice->id; } /** * Given an object containing all the necessary data, * (defined by the form in mod_form.php) this function * will update an existing instance with new data. * * @global object * @param object $choice * @return bool */ function choice_update_instance($choice) { global $DB; $choice->id = $choice->instance; $choice->timemodified = time(); if (empty($choice->timerestrict)) { $choice->timeopen = 0; $choice->timeclose = 0; } //update, delete or insert answers foreach ($choice->option as $key => $value) { $value = trim($value); $option = new stdClass(); $option->text = $value; $option->choiceid = $choice->id; if (isset($choice->limit[$key])) { $option->maxanswers = $choice->limit[$key]; } $option->timemodified = time(); if (isset($choice->optionid[$key]) && !empty($choice->optionid[$key])){//existing choice record $option->id=$choice->optionid[$key]; if (isset($value) && $value <> '') { $DB->update_record("choice_options", $option); } else { //empty old option - needs to be deleted. $DB->delete_records("choice_options", array("id"=>$option->id)); } } else { if (isset($value) && $value <> '') { $DB->insert_record("choice_options", $option); } } } return $DB->update_record('choice', $choice); } /** * @global object * @param object $choice * @param object $user * @param object $coursemodule * @param array $allresponses * @return array */ function choice_prepare_options($choice, $user, $coursemodule, $allresponses) { global $DB; $cdisplay = array('options'=>array()); $cdisplay['limitanswers'] = true; $context = context_module::instance($coursemodule->id); foreach ($choice->option as $optionid => $text) { if (isset($text)) { //make sure there are no dud entries in the db with blank text values. $option = new stdClass; $option->attributes = new stdClass; $option->attributes->value = $optionid; $option->text = format_string($text); $option->maxanswers = $choice->maxanswers[$optionid]; $option->displaylayout = $choice->display; if (isset($allresponses[$optionid])) { $option->countanswers = count($allresponses[$optionid]); } else { $option->countanswers = 0; } if ($DB->record_exists('choice_answers', array('choiceid' => $choice->id, 'userid' => $user->id, 'optionid' => $optionid))) { $option->attributes->checked = true; } if ( $choice->limitanswers && ($option->countanswers >= $option->maxanswers) && empty($option->attributes->checked)) { $option->attributes->disabled = true; } $cdisplay['options'][] = $option; } } $cdisplay['hascapability'] = is_enrolled($context, NULL, 'mod/choice:choose'); //only enrolled users are allowed to make a choice if ($choice->allowupdate && $DB->record_exists('choice_answers', array('choiceid'=> $choice->id, 'userid'=> $user->id))) { $cdisplay['allowupdate'] = true; } return $cdisplay; } /** * @global object * @param int $formanswer * @param object $choice * @param int $userid * @param object $course Course object * @param object $cm */ function choice_user_submit_response($formanswer, $choice, $userid, $course, $cm) { global $DB, $CFG; require_once($CFG->libdir.'/completionlib.php'); if (empty($formanswer)) { print_error('atleastoneoption', 'choice'); } if (is_array($formanswer)) { if (!$choice->allowmultiple) { print_error('multiplenotallowederror', 'choice'); } $formanswers = $formanswer; } else { $formanswers = array($formanswer); } $current = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid)); $context = context_module::instance($cm->id); $choicesexceeded = false; $countanswers = array(); foreach ($formanswers as $val) { $countanswers[$val] = 0; } if($choice->limitanswers) { // Find out whether groups are being used and enabled if (groups_get_activity_groupmode($cm) > 0) { $currentgroup = groups_get_activity_group($cm); } else { $currentgroup = 0; } list ($insql, $params) = $DB->get_in_or_equal($formanswers, SQL_PARAMS_NAMED); if($currentgroup) { // If groups are being used, retrieve responses only for users in // current group global $CFG; $params['groupid'] = $currentgroup; $sql = "SELECT ca.* FROM {choice_answers} ca INNER JOIN {groups_members} gm ON ca.userid=gm.userid WHERE optionid $insql AND gm.groupid= :groupid"; } else { // Groups are not used, retrieve all answers for this option ID $sql = "SELECT ca.* FROM {choice_answers} ca WHERE optionid $insql"; } $answers = $DB->get_records_sql($sql, $params); if ($answers) { foreach ($answers as $a) { //only return enrolled users. if (is_enrolled($context, $a->userid, 'mod/choice:choose')) { $countanswers[$a->optionid]++; } } } foreach ($countanswers as $opt => $count) { if ($count > $choice->maxanswers[$opt]) { $choicesexceeded = true; break; } } } // Check the user hasn't exceeded the maximum selections for the choice(s) they have selected. if (!($choice->limitanswers && $choicesexceeded)) { $answersnapshots = array(); if ($current) { $existingchoices = array(); foreach ($current as $c) { if (in_array($c->optionid, $formanswers)) { $existingchoices[] = $c->optionid; $DB->set_field('choice_answers', 'timemodified', time(), array('id' => $c->id)); $answersnapshots[] = $c; } else { $DB->delete_records('choice_answers', array('id' => $c->id)); } } // Add new ones. foreach ($formanswers as $f) { if (!in_array($f, $existingchoices)) { $newanswer = new stdClass(); $newanswer->optionid = $f; $newanswer->choiceid = $choice->id; $newanswer->userid = $userid; $newanswer->timemodified = time(); $newanswer->id = $DB->insert_record("choice_answers", $newanswer); $answersnapshots[] = $newanswer; } } $eventdata = array(); $eventdata['context'] = $context; $eventdata['objectid'] = $choice->id; $eventdata['userid'] = $userid; $eventdata['courseid'] = $course->id; $eventdata['other'] = array(); $eventdata['other']['choiceid'] = $choice->id; $eventdata['other']['optionid'] = $formanswer; $event = \mod_choice\event\answer_updated::create($eventdata); $event->add_record_snapshot('course', $course); $event->add_record_snapshot('course_modules', $cm); $event->add_record_snapshot('choice', $choice); foreach ($answersnapshots as $record) { $event->add_record_snapshot('choice_answers', $record); } $event->trigger(); } else { foreach ($formanswers as $answer) { $newanswer = new stdClass(); $newanswer->choiceid = $choice->id; $newanswer->userid = $userid; $newanswer->optionid = $answer; $newanswer->timemodified = time(); $newanswer->id = $DB->insert_record("choice_answers", $newanswer); $answersnapshots[] = $newanswer; } // Update completion state $completion = new completion_info($course); if ($completion->is_enabled($cm) && $choice->completionsubmit) { $completion->update_state($cm, COMPLETION_COMPLETE); } $eventdata = array(); $eventdata['context'] = $context; $eventdata['objectid'] = $choice->id; $eventdata['userid'] = $userid; $eventdata['courseid'] = $course->id; $eventdata['other'] = array(); $eventdata['other']['choiceid'] = $choice->id; $eventdata['other']['optionid'] = $formanswers; $event = \mod_choice\event\answer_submitted::create($eventdata); $event->add_record_snapshot('course', $course); $event->add_record_snapshot('course_modules', $cm); $event->add_record_snapshot('choice', $choice); foreach ($answersnapshots as $record) { $event->add_record_snapshot('choice_answers', $record); } $event->trigger(); } } else { // Check to see if current choice already selected - if not display error. $currentids = array_keys($current); if (array_diff($currentids, $formanswers) || array_diff($formanswers, $currentids) ) { print_error('choicefull', 'choice'); } } } /** * @param array $user * @param object $cm * @return void Output is echo'd */ function choice_show_reportlink($user, $cm) { $userschosen = array(); foreach($user as $optionid => $userlist) { if ($optionid) { $userschosen = array_merge($userschosen, array_keys($userlist)); } } $responsecount = count(array_unique($userschosen)); echo '