Exemplo n.º 1
0
 /**
  * print_legend
  */
 function print_legend()
 {
     if (empty($this->legend)) {
         return false;
     }
     $stringids = array();
     foreach ($this->legend as $column => $responses) {
         foreach ($responses as $i => $stringid) {
             $stringids[$stringid] = true;
         }
     }
     $strings = hotpot::get_strings(array_keys($stringids));
     unset($stringids, $column, $responses, $i, $stringid);
     foreach ($this->legend as $column => $responses) {
         echo html_writer::start_tag('table');
         echo html_writer::start_tag('tbody');
         foreach ($responses as $i => $response) {
             if (isset($strings[$response])) {
                 $response_string = $strings[$response]->string;
             } else {
                 $response_string = 'Unrecognized string id: ' . $response;
             }
             echo html_writer::tag('tr', html_writer::tag('td', $this->format_header($column)) . html_writer::tag('td', $this->format_legend_index($i)) . html_writer::tag('td', $response_string));
             $column = ' ';
         }
         echo html_writer::end_tag('tbody');
         echo html_writer::end_tag('table');
     }
 }
Exemplo n.º 2
0
 /**
  * review
  *
  * we use call_user_func() a lot in this function to prevent syntax error in PHP 5.2.x
  * note that PHP 5.4 does not allow pass-by-reference in call_user_func()
  * in such a case we must use call_user_func_array($callback, array(&$pass_by_reference))
  */
 static function review($hotpot, $class)
 {
     global $DB;
     // for the time-being we set this setting manually here
     // but one day it will be settable in "mod/hotpot/mod_form.php"
     //$hotpot->reviewoptions = hotpot::REVIEW_DURINGATTEMPT | hotpot::REVIEW_AFTERATTEMPT | hotpot::REVIEW_AFTERCLOSE;
     // the $hotpot should already have the $attempt attached to it
     if (!($reviewoptions = $hotpot->can_reviewattempt())) {
         // oops, we are not allowed to review this $hotpot->attempt
         if ($hotpot->timeclose && $hotpot->timeclose > $hotpot->time) {
             // quiz is closed
             if ($hotpot->reviewoptions & hotpot::REVIEW_AFTERATTEMPT) {
                 return get_string('noreviewbeforeclose', 'mod_hotpot', userdate($hotpot->timeclose));
             }
         } else {
             // quiz is still open
             if ($hotpot->reviewoptions & hotpot::REVIEW_AFTERCLOSE) {
                 return get_string('noreviewafterclose', 'mod_hotpot');
             }
         }
         return get_string('noreview', 'mod_hotpot');
     }
     // we need to know if this user can review all attempts (i.e. a "teacher")
     $reviewallattempts = $hotpot->can_reviewallattempts();
     // if necessary, remove score and weighting fields
     $response_num_fields = call_user_func(array($class, 'response_num_fields'));
     if (!($reviewallattempts || $reviewoptions & hotpot::REVIEW_SCORES)) {
         $response_num_fields = preg_grep('/^score|weighting$/', $response_num_fields, PREG_GREP_INVERT);
     }
     // if necessary, remove reponses fields
     $response_text_fields = call_user_func(array($class, 'response_text_fields'));
     if (!($reviewallattempts || $reviewoptions & hotpot::REVIEW_RESPONSES)) {
         $response_text_fields = array();
     }
     // set flag to remove, if necessary, labels that show whether responses are correct or not
     if (!($reviewallattempts || $reviewoptions & hotpot::REVIEW_ANSWERS)) {
         $neutralize_text_fields = true;
     } else {
         $neutralize_text_fields = false;
     }
     $table = new html_table();
     $table->id = 'responses';
     $table->class = 'generaltable generalbox';
     $table->cellpadding = 4;
     $table->cellspacing = 0;
     if (count($response_num_fields)) {
         $question_colspan = count($response_num_fields) * 2;
         $textfield_colspan = $question_colspan - 1;
     } else {
         $question_colspan = 2;
         $textfield_colspan = 1;
     }
     $strtimeformat = get_string('strftimerecentfull');
     $attempt_fields = call_user_func(array($class, 'attempt_fields'));
     foreach ($attempt_fields as $field) {
         $row = new html_table_row();
         // add heading
         $text = call_user_func(array($class, 'format_attempt_heading'), $field);
         $cell = new html_table_cell($text, array('class' => 'attemptfield'));
         $row->cells[] = $cell;
         // add data
         $text = call_user_func(array($class, 'format_attempt_data'), $hotpot->attempt, $field, $strtimeformat);
         $cell = new html_table_cell($text, array('class' => 'attemptvalue'));
         $cell->colspan = $textfield_colspan;
         $row->cells[] = $cell;
         $table->data[] = $row;
     }
     // get questions and responses relevant to this quiz attempt
     $questions = $DB->get_records('hotpot_questions', array('hotpotid' => $hotpot->id));
     $responses = $DB->get_records('hotpot_responses', array('attemptid' => $hotpot->attempt->id));
     if (empty($questions) || empty($responses)) {
         $row = new html_table_row();
         $cell = new html_table_cell(get_string('noresponses', 'mod_hotpot'), array('class' => 'noresponses'));
         $cell->colspan = $question_colspan;
         $row->cells[] = $cell;
         $table->data[] = $row;
     } else {
         // we have some responses, so print them
         foreach ($responses as $response) {
             if (empty($questions[$response->questionid])) {
                 continue;
                 // invalid question id - shouldn't happen !!
             }
             // add separator
             if (count($table->data)) {
                 $callback = array($class, 'add_separator');
                 // PHP 5.2
                 call_user_func_array($callback, array(&$table, $question_colspan));
             }
             // question text
             if (call_user_func(array($class, 'show_question_text'))) {
                 if ($text = hotpot::get_question_text($questions[$response->questionid])) {
                     $callback = array($class, 'add_question_text');
                     // PHP 5.2
                     call_user_func_array($callback, array(&$table, $text, $question_colspan));
                 }
             }
             // string fields
             $neutral_text = '';
             foreach ($response_text_fields as $field) {
                 if (empty($response->{$field})) {
                     continue;
                     // shouldn't happen !!
                 }
                 $text = array();
                 if ($records = hotpot::get_strings($response->{$field})) {
                     foreach ($records as $record) {
                         $text[] = $record->string;
                     }
                 }
                 unset($records);
                 if (!($text = implode(',', $text))) {
                     continue;
                     // skip empty rows
                 }
                 if ($neutralize_text_fields) {
                     $neutral_text .= ($neutral_text ? ',' : '') . $text;
                 } else {
                     $callback = array($class, 'add_text_field');
                     // PHP 5.2
                     call_user_func_array($callback, array(&$table, $field, $text, $textfield_colspan));
                 }
             }
             if ($neutral_text) {
                 $callback = array($class, 'add_text_field');
                 // PHP 5.2
                 call_user_func_array($callback, array(&$table, 'responses', $neutral_text, $textfield_colspan));
             }
             // numeric fields
             $row = new html_table_row();
             foreach ($response_num_fields as $field) {
                 $callback = array($class, 'add_num_field');
                 // PHP 5.2
                 call_user_func_array($callback, array(&$row, $field, $response->{$field}));
             }
             $table->data[] = $row;
         }
     }
     return html_writer::table($table);
 }