Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/survey/view.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00026     require_once("../../config.php");
00027     require_once("lib.php");
00028 
00029     $id = required_param('id', PARAM_INT);    // Course Module ID
00030 
00031     if (! $cm = get_coursemodule_from_id('survey', $id)) {
00032         print_error('invalidcoursemodule');
00033     }
00034 
00035     if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
00036         print_error('coursemisconf');
00037     }
00038 
00039     $PAGE->set_url('/mod/survey/view.php', array('id'=>$id));
00040     require_login($course->id, false, $cm);
00041     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00042 
00043     require_capability('mod/survey:participate', $context);
00044 
00045     if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) {
00046         print_error('invalidsurveyid', 'survey');
00047     }
00048     $trimmedintro = trim($survey->intro);
00049     if (empty($trimmedintro)) {
00050         $tempo = $DB->get_field("survey", "intro", array("id"=>$survey->template));
00051         $survey->intro = get_string($tempo, "survey");
00052     }
00053 
00054     if (! $template = $DB->get_record("survey", array("id"=>$survey->template))) {
00055         print_error('invalidtmptid', 'survey');
00056     }
00057 
00058 // Update 'viewed' state if required by completion system
00059 require_once($CFG->libdir . '/completionlib.php');
00060 $completion = new completion_info($course);
00061 $completion->set_module_viewed($cm);
00062 
00063     $showscales = ($template->name != 'ciqname');
00064 
00065     $strsurvey = get_string("modulename", "survey");
00066     $PAGE->set_title(format_string($survey->name));
00067     $PAGE->set_heading($course->fullname);
00068     echo $OUTPUT->header();
00069 
00071     if ($groupmode = groups_get_activity_groupmode($cm)) {   // Groups are being used
00072         $currentgroup = groups_get_activity_group($cm);
00073     } else {
00074         $currentgroup = 0;
00075     }
00076     $groupingid = $cm->groupingid;
00077 
00078     if (has_capability('mod/survey:readresponses', $context) or ($groupmode == VISIBLEGROUPS)) {
00079         $currentgroup = 0;
00080     }
00081 
00082     if (has_capability('mod/survey:readresponses', $context)) {
00083         $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
00084         echo "<div class=\"reportlink\"><a href=\"report.php?id=$cm->id\">".
00085               get_string("viewsurveyresponses", "survey", $numusers)."</a></div>";
00086     } else if (!$cm->visible) {
00087         notice(get_string("activityiscurrentlyhidden"));
00088     }
00089 
00090     if (!is_enrolled($context)) {
00091         echo $OUTPUT->notification(get_string("guestsnotallowed", "survey"));
00092     }
00093 
00094 
00095 //  Check the survey hasn't already been filled out.
00096 
00097     if (survey_already_done($survey->id, $USER->id)) {
00098 
00099         add_to_log($course->id, "survey", "view graph", "view.php?id=$cm->id", $survey->id, $cm->id);
00100         $numusers = survey_count_responses($survey->id, $currentgroup, $groupingid);
00101 
00102         if ($showscales) {
00103             echo $OUTPUT->heading(get_string("surveycompleted", "survey"));
00104             echo $OUTPUT->heading(get_string("peoplecompleted", "survey", $numusers));
00105             echo '<div class="resultgraph">';
00106             survey_print_graph("id=$cm->id&amp;sid=$USER->id&amp;group=$currentgroup&amp;type=student.png");
00107             echo '</div>';
00108 
00109         } else {
00110 
00111             echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox', 'intro');
00112             echo $OUTPUT->spacer(array('height'=>30, 'width'=>1, 'br'=>true)); // should be done with CSS instead
00113 
00114             $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions));
00115             $questionorder = explode(",", $survey->questions);
00116             foreach ($questionorder as $key => $val) {
00117                 $question = $questions[$val];
00118                 if ($question->type == 0 or $question->type == 1) {
00119                     if ($answer = survey_get_user_answer($survey->id, $question->id, $USER->id)) {
00120                         $table = new html_table();
00121                         $table->head = array(get_string($question->text, "survey"));
00122                         $table->align = array ("left");
00123                         $table->data[] = array(s($answer->answer1));//no html here, just plain text
00124                         echo html_writer::table($table);
00125                         echo $OUTPUT->spacer(clone($spacer)) . '<br />';
00126                     }
00127                 }
00128             }
00129         }
00130 
00131         echo $OUTPUT->footer();
00132         exit;
00133     }
00134 
00135 //  Start the survey form
00136     add_to_log($course->id, "survey", "view form", "view.php?id=$cm->id", $survey->id, $cm->id);
00137 
00138     echo "<form method=\"post\" action=\"save.php\" id=\"surveyform\">";
00139     echo '<div>';
00140     echo "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
00141     echo "<input type=\"hidden\" name=\"sesskey\" value=\"".sesskey()."\" />";
00142 
00143     echo $OUTPUT->box(format_module_intro('survey', $survey, $cm->id), 'generalbox boxaligncenter bowidthnormal', 'intro');
00144     echo '<div>'. get_string('allquestionrequireanswer', 'survey'). '</div>';
00145 
00146 // Get all the major questions and their proper order
00147     if (! $questions = $DB->get_records_list("survey_questions", "id", explode(',', $survey->questions))) {
00148         print_error('cannotfindquestion', 'survey');
00149     }
00150     $questionorder = explode( ",", $survey->questions);
00151 
00152 // Cycle through all the questions in order and print them
00153 
00154     global $qnum;  //TODO: ugly globals hack for survey_print_*()
00155     global $checklist; //TODO: ugly globals hack for survey_print_*()
00156     $qnum = 0;
00157     $checklist = array();
00158     foreach ($questionorder as $key => $val) {
00159         $question = $questions["$val"];
00160         $question->id = $val;
00161 
00162         if ($question->type >= 0) {
00163 
00164             if ($question->text) {
00165                 $question->text = get_string($question->text, "survey");
00166             }
00167 
00168             if ($question->shorttext) {
00169                 $question->shorttext = get_string($question->shorttext, "survey");
00170             }
00171 
00172             if ($question->intro) {
00173                 $question->intro = get_string($question->intro, "survey");
00174             }
00175 
00176             if ($question->options) {
00177                 $question->options = get_string($question->options, "survey");
00178             }
00179 
00180             if ($question->multi) {
00181                 survey_print_multi($question);
00182             } else {
00183                 survey_print_single($question);
00184             }
00185         }
00186     }
00187 
00188     if (!is_enrolled($context)) {
00189         echo '</div>';
00190         echo "</form>";
00191         echo $OUTPUT->footer();
00192         exit;
00193     }
00194 
00195     $checkarray = Array('questions'=>Array());
00196     if (!empty($checklist)) {
00197        foreach ($checklist as $question => $default) {
00198            $checkarray['questions'][] = Array('question'=>$question, 'default'=>$default);
00199        }
00200     }
00201     $PAGE->requires->js('/mod/survey/survey.js');
00202     $PAGE->requires->data_for_js('surveycheck', $checkarray);
00203     $PAGE->requires->string_for_js('questionsnotanswered', 'survey');
00204     $PAGE->requires->js_function_call('survey_attach_onsubmit');
00205 
00206     echo '<br />';
00207     echo '<input type="submit" value="'.get_string("clicktocontinue", "survey").'" />';
00208     echo '</div>';
00209     echo "</form>";
00210 
00211     echo $OUTPUT->footer();
00212 
00213 
 All Data Structures Namespaces Files Functions Variables Enumerations