Example #1
0
 /**
  * attemptssummary
  *
  * @param xxx $hotpot
  * @return xxx
  */
 public function attemptssummary($hotpot)
 {
     global $CFG;
     if (!($countattempts = $hotpot->get_attempts())) {
         return '';
     }
     $output = '';
     // array to store attemptids of certain kinds of attempts
     $attemptids = array('all' => array(), 'inprogress' => array(), 'timedout' => array(), 'abandoned' => array(), 'completed' => array(), 'zeroduration' => array(), 'zeroscore' => array());
     $dateformat = get_string('strftimerecentfull');
     // cache selectcolumn switch
     if ($hotpot->can_deleteattempts()) {
         $showselectcolumn = true;
     } else {
         $showselectcolumn = false;
     }
     // cache report links flag
     if ($hotpot->can_reviewattempts()) {
         $showreportlinks = true;
     } else {
         $showreportlinks = false;
     }
     // set resume tab text
     if ($hotpot->can_preview()) {
         $resumetab = 'preview';
     } else {
         if ($hotpot->can_view()) {
             $resumetab = 'info';
         } else {
             $resumetab = '';
         }
     }
     // start attempts table (info + resume buttons)
     $table = new html_table();
     $table->attributes['class'] = 'hotpotattemptssummary';
     $table->head = array(get_string('attemptnumber', 'hotpot'), get_string('status', 'hotpot'), get_string('duration', 'hotpot'), get_string('lastaccess', 'hotpot'));
     $table->align = array('center', 'center', 'left', 'left');
     $table->size = array('', '', '', '');
     if ($hotpot->gradeweighting) {
         // insert grade column
         array_splice($table->head, 1, 0, array(get_string('score', 'hotpot')));
         array_splice($table->align, 1, 0, array('center'));
         array_splice($table->size, 1, 0, array(''));
     }
     if ($showselectcolumn) {
         // prepend select column
         array_splice($table->head, 0, 0, ' ');
         array_splice($table->align, 0, 0, array('center'));
         array_splice($table->size, 0, 0, array(''));
     }
     // echo rows of attempt info
     foreach ($hotpot->attempts as $attempt) {
         $row = new html_table_row();
         // set duration
         if ($attempt->timestart && $attempt->timefinish) {
             $duration = $attempt->timefinish - $attempt->timestart;
         } else {
             if ($attempt->starttime && $attempt->endtime) {
                 $duration = $attempt->endtime - $attempt->starttime;
             } else {
                 if ($attempt->timestart && $attempt->timemodified) {
                     $duration = $attempt->timemodified - $attempt->timestart;
                 } else {
                     $duration = 0;
                 }
             }
         }
         if ($showselectcolumn) {
             $id = '[' . $attempt->id . ']';
             $row->cells[] = new html_table_cell(html_writer::checkbox('selected' . $id, 1, false));
             switch ($attempt->status) {
                 case hotpot::STATUS_INPROGRESS:
                     $attemptids['inprogress'][] = $id;
                     break;
                 case hotpot::STATUS_TIMEDOUT:
                     $attemptids['timedout'][] = $id;
                     break;
                 case hotpot::STATUS_ABANDONED:
                     $attemptids['abandoned'][] = $id;
                     break;
                 case hotpot::STATUS_COMPLETED:
                     $attemptids['completed'][] = $id;
                     break;
             }
             if ($attempt->score == 0) {
                 $attemptids['zeroscore'][] = $id;
             }
             if ($duration == 0) {
                 $attemptids['zeroduration'][] = $id;
             }
             $attemptids['all'][] = $id;
         }
         $row->cells[] = new html_table_cell($attempt->attempt);
         if ($hotpot->gradeweighting) {
             $text = $attempt->score . '%';
             if ($showreportlinks) {
                 $url = $hotpot->review_url($attempt);
                 $text = html_writer::link($url, $text);
             }
             $row->cells[] = new html_table_cell($text);
         }
         $row->cells[] = new html_table_cell($hotpot->format_status($attempt->status));
         $row->cells[] = new html_table_cell($hotpot->format_time($duration));
         $row->cells[] = new html_table_cell(userdate($attempt->timemodified, $dateformat));
         $table->data[] = $row;
     }
     // start form if necessary
     if ($showselectcolumn) {
         $onsubmit = '' . "var x=false;" . "var obj=document.getElementsByTagName('input');" . "if(obj){" . "for(var i in obj){" . "if(obj[i].name && obj[i].name.substr(0,9)=='selected[' && obj[i].checked){" . "x=true;" . "break;" . "}" . "}" . "if(!x){" . "alert('" . get_string('checksomeboxes', 'hotpot') . "');" . "}" . "}" . "if(x){" . "x=confirm('" . get_string('confirmdeleteattempts', 'hotpot') . "');" . "}" . "if(this.elements['confirmed']){" . "this.elements['confirmed'].value=(x?1:0);" . "}" . "return x;";
         $params = array('id' => $hotpot->cm->id, 'sesskey' => sesskey(), 'confirmed' => 0, 'action' => 'deleteselected');
         $output .= $this->form_start('view.php', $params, array('onsubmit' => $onsubmit)) . "\n";
     }
     // echo the summary of attempts
     $output .= html_writer::table($table);
     // end form if necessary
     if ($showselectcolumn) {
         $output .= $this->box_start('generalbox', 'hotpotdeleteattempts');
         $output .= '' . '<script type="text/javascript">' . "\n" . '//<!CDATA[' . "\n" . "function hotpot_set_checked(nameFilter, indexFilter, checkedValue) {\n" . "\tvar partMatchName = new RegExp(nameFilter);\n" . "\tvar fullMatchName = new RegExp(nameFilter+indexFilter);\n" . "\tvar inputs = document.getElementsByTagName('input');\n" . "\tif (inputs) {\n" . "\t\tvar i_max = inputs.length;\n" . "\t} else {\n" . "\t\tvar i_max = 0;\n" . "\t}\n" . "\tfor (var i=0; i<i_max; i++) {\n" . "\t\tif (inputs[i].type=='checkbox' && inputs[i].name.match(partMatchName)) {\n" . "\t\t\tif (inputs[i].name.match(fullMatchName)) {\n" . "\t\t\t\tinputs[i].checked = checkedValue;\n" . "\t\t\t} else {\n" . "\t\t\t\tinputs[i].checked = false;\n" . "\t\t\t}\n" . "\t\t}\n" . "\t}\n" . "\treturn true;\n" . "}\n" . "function hotpot_set_checked_attempts(obj) {\n" . "\tvar indexFilter = obj.options[obj.selectedIndex].value;\n" . "\tif (indexFilter=='none') {\n" . "\t\tcheckedValue = 0;\n" . "\t} else {\n" . "\t\tcheckedValue = 1;\n" . "\t}\n" . "\tif (indexFilter=='none' || indexFilter=='all') {\n" . "\t\tindexFilter = '\\\\[\\\\d+\\\\]';\n" . "\t} else {\n" . "\t\tindexFilter = indexFilter.replace(new RegExp('^[^:]*:'), '');\n" . "\t\tindexFilter = indexFilter.replace(new RegExp(',', 'g'), '|');\n" . "\t\tindexFilter = indexFilter.replace(new RegExp('\\\\[', 'g'), '\\\\[');\n" . "\t\tindexFilter = indexFilter.replace(new RegExp('\\\\]', 'g'), '\\\\]');\n" . "\t}\n" . "\thotpot_set_checked('selected', indexFilter, checkedValue);" . "}\n" . '//]]>' . "\n" . '</script>' . "\n";
         $onchange = 'return hotpot_set_checked_attempts(this)';
         // set up attempt status drop down menu
         $options = array('none' => get_string('none'));
         foreach ($attemptids as $type => $ids) {
             if ($total = count($ids)) {
                 if ($type == 'all') {
                     $options['all'] = get_string('all');
                     if ($total > 1) {
                         $options['all'] .= " ({$total})";
                     }
                 } else {
                     $options[$type . ':' . implode(',', $ids)] = get_string($type, 'hotpot') . " ({$total})";
                 }
             }
         }
         // add attempt selection/deletion form controls
         $table = new html_table();
         $table->attributes['class'] = 'hotpotdeleteattempts';
         $table->data[] = new html_table_row(array(new html_table_cell(get_string('selectattempts', 'hotpot') . ':'), new html_table_cell(html_writer::select($options, 'selectattempts', null, array('' => 'choosedots'), array('onchange' => $onchange)))));
         // original onselect was 'return hotpot_set_checked_attempts(this)'
         // Moodle 2.0 uses component_actions, some thing like this ...
         // actually this doesn't work, but it is close - maybe :-)
         // $action = new component_action('select', 'hotpot_set_checked_attempts', array('this'));
         // $this->add_action_handler($action, 'menuselectattempts');
         $table->data[] = new html_table_row(array(new html_table_cell('&nbsp;'), new html_table_cell(html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('deleteattempts', 'hotpot'))))));
         $output .= html_writer::table($table);
         $output .= $this->box_end();
         $output .= $this->form_end();
     }
     return $output;
 }