. defined('MOODLE_INTERNAL') OR die('not allowed'); require_once($CFG->dirroot.'/mod/apply/item/apply_item_class.php'); define('APPLY_RADIORATED_ADJUST_SEP', '<<<<<'); define('APPLY_MULTICHOICERATED_MAXCOUNT', 10); //count of possible items define('APPLY_MULTICHOICERATED_VALUE_SEP', '####'); define('APPLY_MULTICHOICERATED_VALUE_SEP2', '/'); define('APPLY_MULTICHOICERATED_TYPE_SEP', '>>>>>'); define('APPLY_MULTICHOICERATED_LINE_SEP', '|'); define('APPLY_MULTICHOICERATED_ADJUST_SEP', '<<<<<'); define('APPLY_MULTICHOICERATED_IGNOREEMPTY', 'i'); define('APPLY_MULTICHOICERATED_HIDENOSELECT', 'h'); define('APPLY_MULTICHOICERATED_STYLE_FIELD_SEP', ':::::'); define('APPLY_MULTICHOICERATED_STYLE_SEP', '-----'); class apply_item_multichoicerated extends apply_item_base { protected $type = "multichoicerated"; private $commonparams; private $item_form; private $item; public function init() { } public function build_editform($item, $apply, $cm) { global $DB, $CFG; require_once('multichoicerated_form.php'); //get the lastposition number of the apply_items $position = $item->position; $lastposition = $DB->count_records('apply_item', array('apply_id'=>$apply->id)); if ($position == -1) { $i_formselect_last = $lastposition + 1; $i_formselect_value = $lastposition + 1; $item->position = $lastposition + 1; } else { $i_formselect_last = $lastposition; $i_formselect_value = $item->position; } //the elements for position dropdownlist $positionlist = array_slice(range(0, $i_formselect_last), 1, $i_formselect_last, true); $item->presentation = empty($item->presentation) ? '' : $item->presentation; $presen = explode(APPLY_MULTICHOICERATED_STYLE_FIELD_SEP, $item->presentation); if (isset($presen[1])) { $styles = explode(APPLY_MULTICHOICERATED_STYLE_SEP, $presen[1]); $outside_style = isset($styles[0]) ? $styles[0] : get_string('outside_style_default', 'apply'); $item_style = isset($styles[1]) ? $styles[1] : get_string('item_style_default', 'apply'); } else { $outside_style = get_string('outside_style_default', 'apply'); $item_style = get_string('item_style_default', 'apply'); } $item->outside_style = $outside_style; $item->item_style = $item_style; $info = $this->get_info($item); $item->ignoreempty = $this->ignoreempty($item); $item->hidenoselect = $this->hidenoselect($item); //all items for dependitem $applyitems = apply_get_depend_candidates_for_item($apply, $item); $commonparams = array('cmid'=>$cm->id, 'id'=>isset($item->id) ? $item->id : null, 'typ'=>$item->typ, 'items'=>$applyitems, 'apply_id'=>$apply->id); //build the form $customdata = array('item' => $item, 'common' => $commonparams, 'positionlist' => $positionlist, 'position' => $position, 'info' => $info); $this->item_form = new apply_multichoicerated_form('edit_item.php', $customdata); } //this function only can used after the call of build_editform() public function show_editform() { $this->item_form->display(); } public function is_cancelled() { return $this->item_form->is_cancelled(); } public function get_data() { if ($this->item = $this->item_form->get_data()) { return true; } return false; } public function save_item() { global $DB; if (!$item = $this->item_form->get_data()) { return false; } if (isset($item->clone_item) AND $item->clone_item) { $item->id = ''; //to clone this item $item->position++; } $this->set_ignoreempty($item, $item->ignoreempty); $this->set_hidenoselect($item, $item->hidenoselect); $item->hasvalue = $this->get_hasvalue(); if (!$item->id) { $item->id = $DB->insert_record('apply_item', $item); } else { $DB->update_record('apply_item', $item); } return $DB->get_record('apply_item', array('id'=>$item->id)); } //gets an array with three values(typ, name, XXX) //XXX is an object with answertext, answercount and quotient public function get_analysed($item, $groupid = false, $courseid = false) { $analysed_item = array(); $analysed_item[] = $item->typ; $analysed_item[] = $item->name; //die moeglichen Antworten extrahieren $info = $this->get_info($item); $lines = explode(APPLY_MULTICHOICERATED_LINE_SEP, $info->presentation); if (!is_array($lines)) return null; //die Werte holen $values = apply_get_group_values($item, $groupid, $courseid, $this->ignoreempty($item)); if (!$values) return null; //schleife ueber den Werten und ueber die Antwortmoeglichkeiten $analysed_answer = array(); $sizeoflines = count($lines); for ($i = 1; $i <= $sizeoflines; $i++) { $item_values = explode(APPLY_MULTICHOICERATED_VALUE_SEP, $lines[$i-1]); $ans = new stdClass(); $ans->answertext = $item_values[1]; $avg = 0.0; $anscount = 0; foreach ($values as $value) { //ist die Antwort gleich dem index der Antworten + 1? if ($value->value == $i) { $avg += $item_values[0]; //erst alle Werte aufsummieren $anscount++; } } $ans->answercount = $anscount; $ans->avg = doubleval($avg) / doubleval(count($values)); $ans->value = $item_values[0]; $ans->quotient = $ans->answercount / count($values); $analysed_answer[] = $ans; } $analysed_item[] = $analysed_answer; return $analysed_item; } public function get_printval($item, $value) { $printval = ''; if (!isset($value->value)) return $printval; $info = $this->get_info($item); $presentation = explode(APPLY_MULTICHOICERATED_LINE_SEP, $info->presentation); $index = 1; foreach ($presentation as $pres) { if ($value->value == $index) { $item_label = explode(APPLY_MULTICHOICERATED_VALUE_SEP, $pres); $printval = $item_label[1]; break; } $index++; } return $printval; } public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) { global $OUTPUT; $sep_dec = get_string('separator_decimal', 'apply'); if (substr($sep_dec, 0, 2) == '[[') { $sep_dec = APPLY_DECIMAL; } $sep_thous = get_string('separator_thousand', 'apply'); if (substr($sep_thous, 0, 2) == '[[') { $sep_thous = APPLY_THOUSAND; } $analysed_item = $this->get_analysed($item, $groupid, $courseid); if ($analysed_item) { echo ''; echo $itemnr.' ('.$item->label.') '.$analysed_item[1]; echo ''; $analysed_vals = $analysed_item[2]; $pixnr = 0; $avg = 0.0; foreach ($analysed_vals as $val) { $intvalue = $pixnr % 10; $pix = $OUTPUT->image_url('multichoice/' . $intvalue, 'apply'); $pixnr++; $pixwidth = intval($val->quotient * APPLY_MAX_PIX_LENGTH); $avg += $val->avg; $quotient = number_format(($val->quotient * 100), 2, $sep_dec, $sep_thous); echo ''; echo ''; echo '-  '.trim($val->answertext).' ('.$val->value.'):'; echo ''; echo ''.$intvalue.''; echo $val->answercount; if ($val->quotient > 0) { echo ' ('.$quotient.' %)'; } else { echo ''; } echo ''; } $avg = number_format(($avg), 2, $sep_dec, $sep_thous); echo ''; echo get_string('average', 'apply').': '.$avg.''; echo ''; } } public function excelprint_item(&$worksheet, $row_offset, $xls_formats, $item, $groupid, $courseid = false) { $analysed_item = $this->get_analysed($item, $groupid, $courseid); $data = $analysed_item[2]; //write the item $worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2); $worksheet->write_string($row_offset, 1, $analysed_item[1], $xls_formats->head2); if (is_array($data)) { $avg = 0.0; $sizeofdata = count($data); for ($i = 0; $i < $sizeofdata; $i++) { $analysed_data = $data[$i]; $worksheet->write_string($row_offset, $i + 2, trim($analysed_data->answertext).' ('.$analysed_data->value.')', $xls_formats->value_bold); $worksheet->write_number($row_offset + 1, $i + 2, $analysed_data->answercount, $xls_formats->default); $avg += $analysed_data->avg; } //mittelwert anzeigen $worksheet->write_string($row_offset, count($data) + 2, get_string('average', 'apply'), $xls_formats->value_bold); $worksheet->write_number($row_offset + 1, count($data) + 2, $avg, $xls_formats->value_bold); } $row_offset +=2; return $row_offset; } /** * print the item at the edit-page of apply * * @global object * @param object $item * @return void */ public function print_item_preview($item) { global $OUTPUT, $DB; /* $presen = explode(APPLY_MULTICHOICERATED_STYLE_FIELD_SEP, $item->presentation); if (isset($presen[1])) { $styles = explode(APPLY_MULTICHOICERATED_STYLE_SEP, $presen[1]); $outside_style = isset($styles[0]) ? $styles[0] : get_string('outside_style_default', 'apply'); $item_style = isset($styles[1]) ? $styles[1] : get_string('item_style_default', 'apply'); } else { $outside_style = get_string('outside_style_default', 'apply'); $item_style = get_string('item_style_default', 'apply'); } */ $item->outside_style = ''; //$outside_style; $item->item_style = ''; //$item_style; // $info = $this->get_info($item); $lines = explode(APPLY_MULTICHOICERATED_LINE_SEP, $info->presentation); $requiredmark = ($item->required == 1) ? $str_required_mark : ''; $align = right_to_left() ? 'right' : 'left'; $str_required_mark = '*'; //print the question and label $output = ''; $output .= '
'; $output .= '('.$item->label.') '; $output .= format_text($item->name.$requiredmark, true, false, false).' ['.$item->position.']'; if ($item->dependitem) { if ($dependitem = $DB->get_record('apply_item', array('id'=>$item->dependitem))) { $output .= ' '; $output .= '('.$dependitem->label.'->'.$item->dependvalue.')'; $output .= ''; } } $output .= '
'; apply_open_table_item_tag($output, true); //print the presentation echo '
'; apply_item_box_start($item); switch($info->subtype) { case 'r': $this->print_item_radio($item, false, $info, $align, true, $lines); break; case 'd': $this->print_item_dropdown($item, false, $info, $align, true, $lines); break; } apply_item_box_end(); echo '
'; apply_close_table_item_tag(); } /** * print the item at the complete-page of apply * * @global object * @param object $item * @param string $value * @param bool $highlightrequire * @return void */ public function print_item_submit($item, $value = '', $highlightrequire = false) { global $OUTPUT; /* $presen = explode(APPLY_MULTICHOICERATED_STYLE_FIELD_SEP, $item->presentation); if (isset($presen[1])) { $styles = explode(APPLY_MULTICHOICERATED_STYLE_SEP, $presen[1]); $outside_style = isset($styles[0]) ? $styles[0] : get_string('outside_style_default', 'apply'); $item_style = isset($styles[1]) ? $styles[1] : get_string('item_style_default', 'apply'); } else { $outside_style = get_string('outside_style_default', 'apply'); $item_style = get_string('item_style_default', 'apply'); } */ $item->outside_style = ''; //$outside_style; $item->item_style = ''; //$item_style; // $info = $this->get_info($item); $lines = explode(APPLY_MULTICHOICERATED_LINE_SEP, $info->presentation); if ($highlightrequire AND $item->required AND intval($value) <= 0) { $highlight = ' missingrequire'; } else { $highlight = ''; } $align = right_to_left() ? 'right' : 'left'; $str_required_mark = '*'; $requiredmark = ($item->required == 1) ? $str_required_mark : ''; //print the question and label $output = ''; $output .= '
'; $output .= format_text($item->name.$requiredmark, true, false, false); $output .= '
'; apply_open_table_item_tag($output); //print the presentation echo '
'; apply_item_box_start($item); switch($info->subtype) { case 'r': $this->print_item_radio($item, $value, $info, $align, false, $lines); break; case 'd': $this->print_item_dropdown($item, $value, $info, $align, false, $lines); break; } apply_item_box_end(); echo '
'; apply_close_table_item_tag(); } /** * print the item at the complete-page of apply * * @global object * @param object $item * @param string $value * @return void */ public function print_item_show_value($item, $value = '') { global $OUTPUT; $presen = explode(APPLY_MULTICHOICERATED_STYLE_FIELD_SEP, $item->presentation); if (isset($presen[1])) { $styles = explode(APPLY_MULTICHOICERATED_STYLE_SEP, $presen[1]); $outside_style = isset($styles[0]) ? $styles[0] : get_string('outside_style_default', 'apply'); $item_style = isset($styles[1]) ? $styles[1] : get_string('item_style_default', 'apply'); } else { $outside_style = get_string('outside_style_default', 'apply'); $item_style = get_string('item_style_default', 'apply'); } $item->outside_style = $outside_style; $item->item_style = $item_style; $info = $this->get_info($item); $item->ignoreempty = $this->ignoreempty($item); $item->hidenoselect = $this->hidenoselect($item); // $info = $this->get_info($item); $lines = explode(APPLY_MULTICHOICERATED_LINE_SEP, $info->presentation); $align = right_to_left() ? 'right' : 'left'; $requiredmark = ($item->required == 1)?'*':''; //print the question and label $output = ''; $output .= '
'; $output .= format_text($item->name . $requiredmark, true, false, false); $output .= '
'; apply_open_table_item_tag($output); //print the presentation $index = 1; $match = false; echo '
'; echo $OUTPUT->box_start('generalbox boxalign'.$align); apply_item_box_start($item); // foreach ($lines as $line) { if ($value == $index) { $item_value = explode(APPLY_MULTICHOICERATED_VALUE_SEP, $line); echo text_to_html($item_value[1], true, false, false); $match = true; break; } $index++; } if (!$match) echo ' '; apply_item_box_end(); echo $OUTPUT->box_end(); echo '
'; apply_close_table_item_tag(); } public function check_value($value, $item) { if ((!isset($value) OR $value == '' OR $value == 0) AND $item->required != 1) { return true; } if (intval($value) > 0) { return true; } return false; } public function create_value($data) { $data = trim($data); return $data; } //compares the dbvalue with the dependvalue //dbvalue is the number of one selection //dependvalue is the presentation of one selection public function compare_value($item, $dbvalue, $dependvalue) { if (is_array($dbvalue)) { $dbvalues = $dbvalue; } else { $dbvalues = explode(APPLY_MULTICHOICERATED_LINE_SEP, $dbvalue); } $info = $this->get_info($item); $presentation = explode(APPLY_MULTICHOICERATED_LINE_SEP, $info->presentation); $index = 1; foreach ($presentation as $pres) { $presvalues = explode(APPLY_MULTICHOICERATED_VALUE_SEP, $pres); foreach ($dbvalues as $dbval) { if ($dbval == $index AND trim($presvalues[1]) == $dependvalue) { return true; } } $index++; } return false; } public function get_presentation($data) { $present = $this->prepare_presentation_values_save(trim($data->itemvalues), APPLY_MULTICHOICERATED_VALUE_SEP2, APPLY_MULTICHOICERATED_VALUE_SEP); if (!isset($data->subtype)) { $subtype = 'r'; } else { $subtype = substr($data->subtype, 0, 1); } if (isset($data->horizontal) AND $data->horizontal == 1 AND $subtype != 'd') { $present .= APPLY_MULTICHOICERATED_ADJUST_SEP.'1'; } $presentation = $subtype.APPLY_MULTICHOICERATED_TYPE_SEP.$present. APPLY_MULTICHOICERATED_STYLE_FIELD_SEP.$data->outside_style. APPLY_MULTICHOICERATED_STYLE_SEP.$data->item_style; return $subtype.APPLY_MULTICHOICERATED_TYPE_SEP.$present; } public function get_hasvalue() { return 1; } public function get_info($item) { $presentation = empty($item->presentation) ? '' : $item->presentation; $info = new stdClass(); //check the subtype of the multichoice //it can be check(c), radio(r) or dropdown(d) $info->subtype = ''; $info->presentation = ''; $info->horizontal = false; $presen = explode(APPLY_MULTICHOICERATED_STYLE_FIELD_SEP, $item->presentation); $parts = explode(APPLY_MULTICHOICERATED_TYPE_SEP, $presen[0]); @list($info->subtype, $info->presentation) = $parts; if (!isset($info->subtype)) { $info->subtype = 'r'; } if ($info->subtype != 'd') { $parts = explode(APPLY_MULTICHOICERATED_ADJUST_SEP, $info->presentation); @list($info->presentation, $info->horizontal) = $parts; if (isset($info->horizontal) AND $info->horizontal == 1) { $info->horizontal = true; } else { $info->horizontal = false; } } $info->values = $this->prepare_presentation_values_print($info->presentation, APPLY_MULTICHOICERATED_VALUE_SEP, APPLY_MULTICHOICERATED_VALUE_SEP2); return $info; } private function print_item_radio($item, $value, $info, $align, $showrating, $lines) { $index = 1; $checked = ''; if ($info->horizontal) { $hv = 'h'; } else { $hv = 'v'; } echo '