Merge branch 'MDL-62808_master' of git://github.com/markn86/moodle
[moodle.git] / mod / choice / index.php
blob0fdff7f593a035efc511990940c5aaaef3434f61
1 <?php
3 require_once("../../config.php");
4 require_once("lib.php");
6 $id = required_param('id',PARAM_INT); // course
8 $PAGE->set_url('/mod/choice/index.php', array('id'=>$id));
10 if (!$course = $DB->get_record('course', array('id'=>$id))) {
11 print_error('invalidcourseid');
14 require_course_login($course);
15 $PAGE->set_pagelayout('incourse');
17 $eventdata = array('context' => context_course::instance($id));
18 $event = \mod_choice\event\course_module_instance_list_viewed::create($eventdata);
19 $event->add_record_snapshot('course', $course);
20 $event->trigger();
22 $strchoice = get_string("modulename", "choice");
23 $strchoices = get_string("modulenameplural", "choice");
24 $PAGE->set_title($strchoices);
25 $PAGE->set_heading($course->fullname);
26 $PAGE->navbar->add($strchoices);
27 echo $OUTPUT->header();
29 if (! $choices = get_all_instances_in_course("choice", $course)) {
30 notice(get_string('thereareno', 'moodle', $strchoices), "../../course/view.php?id=$course->id");
33 $usesections = course_format_uses_sections($course->format);
35 $sql = "SELECT cha.*
36 FROM {choice} ch, {choice_answers} cha
37 WHERE cha.choiceid = ch.id AND
38 ch.course = ? AND cha.userid = ?";
40 $answers = array () ;
41 if (isloggedin() and !isguestuser() and $allanswers = $DB->get_records_sql($sql, array($course->id, $USER->id))) {
42 foreach ($allanswers as $aa) {
43 $answers[$aa->choiceid] = $aa;
45 unset($allanswers);
49 $timenow = time();
51 $table = new html_table();
53 if ($usesections) {
54 $strsectionname = get_string('sectionname', 'format_'.$course->format);
55 $table->head = array ($strsectionname, get_string("question"), get_string("answer"));
56 $table->align = array ("center", "left", "left");
57 } else {
58 $table->head = array (get_string("question"), get_string("answer"));
59 $table->align = array ("left", "left");
62 $currentsection = "";
64 foreach ($choices as $choice) {
65 if (!empty($answers[$choice->id])) {
66 $answer = $answers[$choice->id];
67 } else {
68 $answer = "";
70 if (!empty($answer->optionid)) {
71 $aa = format_string(choice_get_option_text($choice, $answer->optionid));
72 } else {
73 $aa = "";
75 if ($usesections) {
76 $printsection = "";
77 if ($choice->section !== $currentsection) {
78 if ($choice->section) {
79 $printsection = get_section_name($course, $choice->section);
81 if ($currentsection !== "") {
82 $table->data[] = 'hr';
84 $currentsection = $choice->section;
88 //Calculate the href
89 if (!$choice->visible) {
90 //Show dimmed if the mod is hidden
91 $tt_href = "<a class=\"dimmed\" href=\"view.php?id=$choice->coursemodule\">".format_string($choice->name,true)."</a>";
92 } else {
93 //Show normal if the mod is visible
94 $tt_href = "<a href=\"view.php?id=$choice->coursemodule\">".format_string($choice->name,true)."</a>";
96 if ($usesections) {
97 $table->data[] = array ($printsection, $tt_href, $aa);
98 } else {
99 $table->data[] = array ($tt_href, $aa);
102 echo "<br />";
103 echo html_writer::table($table);
105 echo $OUTPUT->footer();