first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,52 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_form_class.php');
class feedback_captcha_form extends feedback_item_form {
protected $type = "captcha";
public function definition() {
$item = $this->_customdata['item'];
$common = $this->_customdata['common'];
$positionlist = $this->_customdata['positionlist'];
$position = $this->_customdata['position'];
$mform =& $this->_form;
$mform->addElement('header', 'general', get_string($this->type, 'feedback'));
$mform->addElement('advcheckbox', 'required', get_string('required', 'feedback'), '' , null , array(0, 1));
$mform->addElement('text',
'name',
get_string('item_name', 'feedback'),
array('size'=>FEEDBACK_ITEM_NAME_TEXTBOX_SIZE, 'maxlength'=>255));
$mform->addElement('text',
'label',
get_string('item_label', 'feedback'),
array('size'=>FEEDBACK_ITEM_LABEL_TEXTBOX_SIZE, 'maxlength'=>255));
$mform->addElement('select',
'presentation',
get_string('count_of_nums', 'feedback').'&nbsp;',
array_slice(range(0, 10), 3, 10, true));
parent::definition();
$this->set_data($item);
}
}
+213
View File
@@ -0,0 +1,213 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') OR die('not allowed');
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
class feedback_item_captcha extends feedback_item_base {
protected $type = "captcha";
public function build_editform($item, $feedback, $cm) {
global $DB;
$editurl = new moodle_url('/mod/feedback/edit.php', array('id'=>$cm->id));
// There are no settings for recaptcha.
if (isset($item->id) AND $item->id > 0) {
notice(get_string('there_are_no_settings_for_recaptcha', 'feedback'), $editurl->out());
exit;
}
// Only one recaptcha can be in a feedback.
$params = array('feedback' => $feedback->id, 'typ' => $this->type);
if ($DB->record_exists('feedback_item', $params)) {
notice(get_string('only_one_captcha_allowed', 'feedback'), $editurl->out());
exit;
}
$this->item = $item;
$this->item_form = true; // Dummy.
$lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->id));
$this->item->feedback = $feedback->id;
$this->item->template = 0;
$this->item->name = get_string('captcha', 'feedback');
$this->item->label = '';
$this->item->presentation = '';
$this->item->typ = $this->type;
$this->item->hasvalue = $this->get_hasvalue();
$this->item->position = $lastposition + 1;
$this->item->required = 1;
$this->item->dependitem = 0;
$this->item->dependvalue = '';
$this->item->options = '';
}
public function show_editform() {
}
public function is_cancelled() {
return false;
}
public function get_data() {
return true;
}
public function save_item() {
global $DB;
if (!$this->item) {
return false;
}
if (empty($this->item->id)) {
$this->item->id = $DB->insert_record('feedback_item', $this->item);
} else {
$DB->update_record('feedback_item', $this->item);
}
return $DB->get_record('feedback_item', array('id'=>$this->item->id));
}
public function get_printval($item, $value) {
return '';
}
public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
return $itemnr;
}
public function excelprint_item(&$worksheet, $row_offset,
$xls_formats, $item,
$groupid, $courseid = false) {
return $row_offset;
}
/**
* Returns the formatted name of the item for the complete form or response view
*
* @param stdClass $item
* @param bool $withpostfix
* @return string
*/
public function get_display_name($item, $withpostfix = true) {
return get_string('captcha', 'feedback');
}
/**
* Adds an input element to the complete form
*
* @param stdClass $item
* @param mod_feedback_complete_form $form
*/
public function complete_form_element($item, $form) {
$name = $this->get_display_name($item);
$inputname = $item->typ . '_' . $item->id;
if ($form->get_mode() != mod_feedback_complete_form::MODE_COMPLETE) {
// Span to hold the element id. The id is used for drag and drop reordering.
$form->add_form_element($item,
['static', $inputname, $name, html_writer::span('', '', ['id' => 'feedback_item_' . $item->id])],
false,
false);
} else {
// Add recaptcha element that is used during the form validation.
$form->add_form_element($item,
['recaptcha', $inputname . 'recaptcha', $name],
false,
false);
// Add hidden element with value "1" that will be saved in the values table after completion.
$form->add_form_element($item, ['hidden', $inputname, 1], false);
$form->set_element_type($inputname, PARAM_INT);
}
// Add recaptcha validation to the form.
$form->add_validation_rule(function($values, $files) use ($item, $form) {
$elementname = $item->typ . '_' . $item->id . 'recaptcha';
$recaptchaelement = $form->get_form_element($elementname);
if (empty($values['g-recaptcha-response'])) {
return array($elementname => get_string('required'));
} else {
$response = $values['g-recaptcha-response'];
if (true !== ($result = $recaptchaelement->verify($response))) {
return array($elementname => $result);
}
}
return true;
});
}
public function create_value($data) {
global $USER;
return $USER->sesskey;
}
public function get_hasvalue() {
global $CFG;
// Is recaptcha configured in moodle?
if (empty($CFG->recaptchaprivatekey) OR empty($CFG->recaptchapublickey)) {
return 0;
}
return 1;
}
public function can_switch_require() {
return false;
}
/**
* Returns the list of actions allowed on this item in the edit mode
*
* @param stdClass $item
* @param stdClass $feedback
* @param cm_info $cm
* @return action_menu_link[]
*/
public function edit_actions($item, $feedback, $cm) {
$actions = parent::edit_actions($item, $feedback, $cm);
unset($actions['update']);
return $actions;
}
public function get_data_for_external($item) {
global $CFG;
if (empty($CFG->recaptchaprivatekey) || empty($CFG->recaptchapublickey)) {
return null;
}
// With reCAPTCHA v2 the captcha will be rendered by the mobile client using just the publickey.
$data[] = $CFG->recaptchapublickey;
return json_encode($data);
}
/**
* Return the analysis data ready for external functions.
*
* @param stdClass $item the item (question) information
* @param int $groupid the group id to filter data (optional)
* @param int $courseid the course id (optional)
* @return array an array of data with non scalar types json encoded
* @since Moodle 3.3
*/
public function get_analysed_for_external($item, $groupid = false, $courseid = false) {
return [];
}
}
+124
View File
@@ -0,0 +1,124 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once('../../../../config.php');
$id = required_param('id', PARAM_INT);
$PAGE->set_url('/mod/feedback/item/captcha/print_captcha.php', array('id'=>$id));
if ($id) {
if (! $cm = get_coursemodule_from_id('feedback', $id)) {
throw new \moodle_exception('invalidcoursemodule');
}
if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
throw new \moodle_exception('coursemisconf');
}
if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
throw new \moodle_exception('invalidcoursemodule');
}
}
if (!isset($SESSION->feedback->item->captcha)) {
throw new \moodle_exception('captchanotset', 'feedback');
}
$height = 40;
$charcount = $SESSION->feedback->item->captcha->charcount;
$fontfile = $CFG->libdir.'/default.ttf';
$ttfbox = imagettfbbox ( 30, 0, $fontfile, 'H' );//the text to measure
$charwidth = $ttfbox[2];
$width = $charcount * $charwidth;
$scale = 0.3;
$elipsesize = intval((($width + $height)/2) / 5);
$factor_x = intval($width * $scale);
$factor_y = intval($height * $scale);
//I split the colors in three ranges
//given are the max-min-values
$colors = array(array(0, 40), array(50, 200), array(210, 255));
list($col_text1, $col_el, $col_text2) = $colors;
//if the text is in color_1 so the elipses can be in color_2 or color_3
//if the text is in color_2 so the elipses can be in color_1 or color_3
//and so on.
$textcolnum = rand(1, 3);
//create the numbers to print out
$nums = array();
for ($i = 0; $i < $charcount; $i++) {
$nums[] = rand(0, 9); //Ziffern von 0-
}
//to draw enough elipses so I draw 0.2 * width and 0.2 * height
//we need th colors for that
$properties = array();
for ($x = 0; $x < $factor_x; $x++) {
for ($y = 0; $y < $factor_y; $y++) {
$propobj = new stdClass();
$propobj->x = intval($x / $scale);
$propobj->y = intval($y / $scale);
$propobj->red = get_random_color($col_el[0], $col_el[1]);
$propobj->green = get_random_color($col_el[0], $col_el[1]);
$propobj->blue = get_random_color($col_el[0], $col_el[1]);
$properties[] = $propobj;
}
}
shuffle($properties);
// create a blank image
$image = imagecreatetruecolor($width, $height);
$bg = imagecolorallocate($image, 0, 0, 0);
for ($i = 0; $i < ($factor_x * $factor_y); $i++) {
$propobj = $properties[$i];
// choose a color for the ellipse
$col_ellipse = imagecolorallocate($image, $propobj->red, $propobj->green, $propobj->blue);
// draw the white ellipse
imagefilledellipse($image, $propobj->x, $propobj->y, $elipsesize, $elipsesize, $col_ellipse);
}
$checkchar = '';
for ($i = 0; $i < $charcount; $i++) {
$colnum = rand(1, 2);
$textcol = new stdClass();
$textcol->red = get_random_color(${'col_text'.$colnum}[0], ${'col_text'.$colnum}[1]);
$textcol->green = get_random_color(${'col_text'.$colnum}[0], ${'col_text'.$colnum}[1]);
$textcol->blue = get_random_color(${'col_text'.$colnum}[0], ${'col_text'.$colnum}[1]);
$color_text = imagecolorallocate($image, $textcol->red, $textcol->green, $textcol->blue);
$angle_text = rand(-20, 20);
$left_text = $i * $charwidth;
$text = $nums[$i];
$checkchar .= $text;
imagettftext($image, 30, $angle_text, $left_text, 35, $color_text, $fontfile, $text);
}
$SESSION->feedback->item->captcha->checkchar = $checkchar;
// output the picture
header("Content-type: image/png");
imagepng($image);
function get_random_color($val1 = 0, $val2 = 255) {
$min = $val1 < $val2 ? $val1 : $val2;
$max = $val1 > $val2 ? $val1 : $val2;
return rand($min, $max);
}
+374
View File
@@ -0,0 +1,374 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
abstract class feedback_item_base {
/** @var string type of the element, should be overridden by each item type */
protected $type;
/** @var feedback_item_form */
protected $item_form;
/** @var stdClass */
protected $item;
/**
* constructor
*/
public function __construct() {
}
/**
* Displays the form for editing an item
*
* this function only can used after the call of build_editform()
*/
public function show_editform() {
$this->item_form->display();
}
/**
* Checks if the editing form was cancelled
*
* @return bool
*/
public function is_cancelled() {
return $this->item_form->is_cancelled();
}
/**
* Gets submitted data from the edit form and saves it in $this->item
*
* @return bool
*/
public function get_data() {
if ($this->item !== null) {
return true;
}
if ($this->item = $this->item_form->get_data()) {
return true;
}
return false;
}
/**
* Set the item data (to be used by data generators).
*
* @param stdClass $itemdata the item data to set
* @since Moodle 3.3
*/
public function set_data($itemdata) {
$this->item = $itemdata;
}
/**
* Creates and returns an instance of the form for editing the item
*
* @param stdClass $item
* @param stdClass $feedback
* @param cm_info|stdClass $cm
*/
abstract public function build_editform($item, $feedback, $cm);
/**
* Saves the item after it has been edited (or created)
*/
abstract public function save_item();
/**
* Converts the value from complete_form data to the string value that is stored in the db.
* @param mixed $value element from mod_feedback_complete_form::get_data() with the name $item->typ.'_'.$item->id
* @return string
*/
public function create_value($value) {
return strval($value);
}
/**
* Compares the dbvalue with the dependvalue
*
* @param stdClass $item
* @param string $dbvalue is the value input by user in the format as it is stored in the db
* @param string $dependvalue is the value that it needs to be compared against
*/
public function compare_value($item, $dbvalue, $dependvalue) {
return strval($dbvalue) === strval($dependvalue);
}
/**
* Wether this item type has a value that is expected from the user and saved in the stored values.
* @return int
*/
public function get_hasvalue() {
return 1;
}
/**
* Wether this item can be set as both required and not
* @return bool
*/
public function can_switch_require() {
return true;
}
/**
* Adds summary information about an item to the Excel export file
*
* @param object $worksheet a reference to the pear_spreadsheet-object
* @param integer $row_offset
* @param stdClass $xls_formats see analysis_to_excel.php
* @param object $item the db-object from feedback_item
* @param integer $groupid
* @param integer $courseid
* @return integer the new row_offset
*/
abstract public function excelprint_item(&$worksheet, $row_offset,
$xls_formats, $item,
$groupid, $courseid = false);
/**
* Prints analysis for the current item
*
* @param $item the db-object from feedback_item
* @param string $itemnr
* @param integer $groupid
* @param integer $courseid
* @return integer the new itemnr
*/
abstract public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false);
/**
* Prepares the value for exporting to Excel
*
* @param object $item the db-object from feedback_item
* @param object $value object with item-related value from feedback_values in the 'value' property
* @return string
*/
abstract public function get_printval($item, $value);
/**
* Returns the formatted name of the item for the complete form or response view
*
* @param stdClass $item
* @param bool $withpostfix
* @return string
*/
public function get_display_name($item, $withpostfix = true) {
return format_text($item->name, FORMAT_HTML, array('noclean' => true, 'para' => false)) .
($withpostfix ? $this->get_display_name_postfix($item) : '');
}
/**
* Returns the postfix to be appended to the display name that is based on other settings
*
* @param stdClass $item
* @return string
*/
public function get_display_name_postfix($item) {
return '';
}
/**
* Adds an input element to the complete form
*
* This method is called:
* - to display the form when user completes feedback
* - to display existing elements when teacher edits the feedback items
* - to display the feedback preview (print.php)
* - to display the completed response
* - to preview a feedback template
*
* If it is important which mode the form is in, use $form->get_mode()
*
* Each item type must add a single form element with the name $item->typ.'_'.$item->id
* This element must always be present in form data even if nothing is selected (i.e. use advcheckbox and not checkbox).
* To add an element use either:
* $form->add_form_element() - adds a single element to the form
* $form->add_form_group_element() - adds a group element to the form
*
* Other useful methods:
* $form->get_item_value()
* $form->set_element_default()
* $form->add_validation_rule()
* $form->set_element_type()
*
* The element must support freezing so it can be used for viewing the response as well.
* If the desired form element does not support freezing, check $form->is_frozen()
* and create a static element instead.
*
* @param stdClass $item
* @param mod_feedback_complete_form $form
*/
abstract public function complete_form_element($item, $form);
/**
* Returns the list of actions allowed on this item in the edit mode
*
* @param stdClass $item
* @param stdClass $feedback
* @param cm_info $cm
* @return action_menu_link[]
*/
public function edit_actions($item, $feedback, $cm) {
$actions = array();
$strupdate = get_string('edit_item', 'feedback');
$actions['update'] = new action_menu_link_secondary(
new moodle_url('/mod/feedback/edit_item.php', array('id' => $item->id)),
new pix_icon('t/edit', $strupdate, 'moodle', array('class' => 'iconsmall', 'title' => '')),
$strupdate,
array('class' => 'editing_update', 'data-action' => 'update')
);
if ($this->can_switch_require()) {
if ($item->required == 1) {
$buttontitle = get_string('switch_item_to_not_required', 'feedback');
$buttonimg = 'required';
} else {
$buttontitle = get_string('switch_item_to_required', 'feedback');
$buttonimg = 'notrequired';
}
$actions['required'] = new action_menu_link_secondary(
new moodle_url('/mod/feedback/edit.php', array('id' => $cm->id,
'switchitemrequired' => $item->id, 'sesskey' => sesskey())),
new pix_icon($buttonimg, $buttontitle, 'feedback', array('class' => 'iconsmall', 'title' => '')),
$buttontitle,
array('class' => 'editing_togglerequired', 'data-action' => 'togglerequired')
);
}
$strdelete = get_string('delete_item', 'feedback');
$actions['delete'] = new action_menu_link_secondary(
new moodle_url('/mod/feedback/edit.php', array('id' => $cm->id, 'deleteitem' => $item->id, 'sesskey' => sesskey())),
new pix_icon('t/delete', $strdelete, 'moodle', array('class' => 'iconsmall', 'title' => '')),
$strdelete,
array('class' => 'editing_delete', 'data-action' => 'delete')
);
return $actions;
}
/**
* Return extra data for external functions.
*
* Some items may have additional configuration data or default values that should be returned for external functions:
* - Info elements: The default value information (course or category name)
* - Captcha: The recaptcha challenge hash key
*
* @param stdClass $item the item object
* @return str the data, may be json_encoded for large structures
*/
public function get_data_for_external($item) {
return null;
}
/**
* Return the analysis data ready for external functions.
*
* @param stdClass $item the item (question) information
* @param int $groupid the group id to filter data (optional)
* @param int $courseid the course id (optional)
* @return array an array of data with non scalar types json encoded
* @since Moodle 3.3
*/
abstract public function get_analysed_for_external($item, $groupid = false, $courseid = false);
}
//a dummy class to realize pagebreaks
class feedback_item_pagebreak extends feedback_item_base {
protected $type = "pagebreak";
public function show_editform() {
}
/**
* Checks if the editing form was cancelled
* @return bool
*/
public function is_cancelled() {
}
public function get_data() {
}
public function build_editform($item, $feedback, $cm) {
}
public function save_item() {
}
public function create_value($data) {
}
public function get_hasvalue() {
return 0;
}
public function excelprint_item(&$worksheet, $row_offset,
$xls_formats, $item,
$groupid, $courseid = false) {
}
public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
}
public function get_printval($item, $value) {
}
public function can_switch_require() {
return false;
}
/**
* Adds an input element to the complete form
*
* @param stdClass $item
* @param mod_feedback_complete_form $form
*/
public function complete_form_element($item, $form) {
$form->add_form_element($item,
['static',
$item->typ.'_'.$item->id,
'',
html_writer::empty_tag('hr', ['class' => 'feedback_pagebreak', 'id' => 'feedback_item_' . $item->id])
]);
}
/**
* Returns the list of actions allowed on this item in the edit mode
*
* @param stdClass $item
* @param stdClass $feedback
* @param cm_info $cm
* @return action_menu_link[]
*/
public function edit_actions($item, $feedback, $cm) {
$actions = array();
$strdelete = get_string('delete_pagebreak', 'feedback');
$actions['delete'] = new action_menu_link_secondary(
new moodle_url('/mod/feedback/edit.php', array('id' => $cm->id, 'deleteitem' => $item->id, 'sesskey' => sesskey())),
new pix_icon('t/delete', $strdelete, 'moodle', array('class' => 'iconsmall', 'title' => '')),
$strdelete,
array('class' => 'editing_delete', 'data-action' => 'delete')
);
return $actions;
}
/**
* Return the analysis data ready for external functions.
*
* @param stdClass $item the item (question) information
* @param int $groupid the group id to filter data (optional)
* @param int $courseid the course id (optional)
* @return array an array of data with non scalar types json encoded
* @since Moodle 3.3
*/
public function get_analysed_for_external($item, $groupid = false, $courseid = false) {
return;
}
}
@@ -0,0 +1,131 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once($CFG->libdir.'/formslib.php');
define('FEEDBACK_ITEM_NAME_TEXTBOX_SIZE', 80);
define('FEEDBACK_ITEM_LABEL_TEXTBOX_SIZE', 20);
abstract class feedback_item_form extends moodleform {
/** @var string type value in the hidden element. */
protected $type;
public function definition() {
$item = $this->_customdata['item']; //the item object
//common is an array like:
// array('cmid'=>$cm->id,
// 'id'=>isset($item->id) ? $item->id : NULL,
// 'typ'=>$item->typ,
// 'items'=>$feedbackitems,
// 'feedback'=>$feedback->id);
$common = $this->_customdata['common'];
//positionlist is an array with possible positions for the item location
$positionlist = $this->_customdata['positionlist'];
//the current position of the item
$position = $this->_customdata['position'];
$mform =& $this->_form;
if (array_filter(array_keys($common['items']))) {
$mform->addElement('select',
'dependitem',
get_string('dependitem', 'feedback').'&nbsp;',
$common['items']
);
$mform->addHelpButton('dependitem', 'depending', 'feedback');
$mform->addElement('text',
'dependvalue',
get_string('dependvalue', 'feedback'),
array('size'=>FEEDBACK_ITEM_LABEL_TEXTBOX_SIZE, 'maxlength'=>255));
$mform->hideIf('dependvalue', 'dependitem', 'eq', '0');
} else {
$mform->addElement('hidden', 'dependitem', 0);
$mform->addElement('hidden', 'dependvalue', '');
}
$mform->setType('dependitem', PARAM_INT);
$mform->setType('dependvalue', PARAM_RAW);
$position_select = $mform->addElement('select',
'position',
get_string('position', 'feedback').'&nbsp;',
$positionlist);
$position_select->setValue($position);
$mform->addElement('hidden', 'cmid', $common['cmid']);
$mform->setType('cmid', PARAM_INT);
$mform->addElement('hidden', 'id', $common['id']);
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'feedback', $common['feedback']);
$mform->setType('feedback', PARAM_INT);
$mform->addElement('hidden', 'template', 0);
$mform->setType('template', PARAM_INT);
$mform->setType('name', PARAM_RAW);
$mform->setType('label', PARAM_NOTAGS);
$mform->addElement('hidden', 'typ', $this->type);
$mform->setType('typ', PARAM_ALPHA);
$mform->addElement('hidden', 'hasvalue', 0);
$mform->setType('hasvalue', PARAM_INT);
$mform->addElement('hidden', 'options', '');
$mform->setType('options', PARAM_ALPHA);
$buttonarray = array();
if (!empty($item->id)) {
$buttonarray[] = &$mform->createElement('submit',
'update_item',
get_string('update_item', 'feedback'));
$buttonarray[] = &$mform->createElement('submit',
'clone_item',
get_string('save_as_new_item', 'feedback'));
} else {
$mform->addElement('hidden', 'clone_item', 0);
$mform->setType('clone_item', PARAM_INT);
$buttonarray[] = &$mform->createElement('submit',
'save_item',
get_string('savechanges'));
}
$buttonarray[] = &$mform->createElement('cancel');
$mform->addGroup($buttonarray, 'buttonar', '&nbsp;', array(' '), false);
}
/**
* Return submitted data if properly submitted or returns NULL if validation fails or
* if there is no submitted data.
*
* @return object submitted data; NULL if not valid or not submitted or cancelled
*/
public function get_data() {
if ($item = parent::get_data()) {
if (!isset($item->dependvalue)) {
$item->dependvalue = '';
}
}
return $item;
}
}
+58
View File
@@ -0,0 +1,58 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_form_class.php');
class feedback_info_form extends feedback_item_form {
protected $type = "info";
/** @var object Form element. */
protected $infotype;
public function definition() {
$item = $this->_customdata['item'];
$common = $this->_customdata['common'];
$positionlist = $this->_customdata['positionlist'];
$position = $this->_customdata['position'];
$presentationoptions = $this->_customdata['presentationoptions'];
$mform =& $this->_form;
$mform->addElement('header', 'general', get_string($this->type, 'feedback'));
$mform->addElement('hidden', 'required', 0);
$mform->setType('required', PARAM_INT);
$mform->addElement('text',
'name',
get_string('item_name', 'feedback'),
array('size'=>FEEDBACK_ITEM_NAME_TEXTBOX_SIZE, 'maxlength'=>255));
$mform->addElement('text',
'label',
get_string('item_label', 'feedback'),
array('size'=>FEEDBACK_ITEM_LABEL_TEXTBOX_SIZE, 'maxlength'=>255));
$this->infotype = &$mform->addElement('select',
'presentation',
get_string('infotype', 'feedback'),
$presentationoptions);
parent::definition();
$this->set_data($item);
}
}
+322
View File
@@ -0,0 +1,322 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') OR die('not allowed');
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
class feedback_item_info extends feedback_item_base {
protected $type = "info";
/** Mode recording response time (for non-anonymous feedbacks only) */
const MODE_RESPONSETIME = 1;
/** Mode recording current course */
const MODE_COURSE = 2;
/** Mode recording current course category */
const MODE_CATEGORY = 3;
/** Special constant to keep the current timestamp as value for the form element */
const CURRENTTIMESTAMP = '__CURRENT__TIMESTAMP__';
public function build_editform($item, $feedback, $cm) {
global $DB, $CFG;
require_once('info_form.php');
//get the lastposition number of the feedback_items
$position = $item->position;
$lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->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) ? self::MODE_COURSE : $item->presentation;
$item->required = 0;
//all items for dependitem
$feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
$commonparams = array('cmid'=>$cm->id,
'id'=>isset($item->id) ? $item->id : null,
'typ'=>$item->typ,
'items'=>$feedbackitems,
'feedback'=>$feedback->id);
// Options for the 'presentation' select element.
$presentationoptions = array();
if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO || $item->presentation == self::MODE_RESPONSETIME) {
// "Response time" is hidden anyway in case of anonymous feedback, no reason to offer this option.
// However if it was already selected leave it in the dropdown.
$presentationoptions[self::MODE_RESPONSETIME] = get_string('responsetime', 'feedback');
}
$presentationoptions[self::MODE_COURSE] = get_string('course');
$presentationoptions[self::MODE_CATEGORY] = get_string('coursecategory');
//build the form
$this->item_form = new feedback_info_form('edit_item.php',
array('item'=>$item,
'common'=>$commonparams,
'positionlist'=>$positionlist,
'position' => $position,
'presentationoptions' => $presentationoptions));
}
public function save_item() {
global $DB;
if (!$this->get_data()) {
return false;
}
$item = $this->item;
if (isset($item->clone_item) AND $item->clone_item) {
$item->id = ''; //to clone this item
$item->position++;
}
$item->hasvalue = $this->get_hasvalue();
if (!$item->id) {
$item->id = $DB->insert_record('feedback_item', $item);
} else {
$DB->update_record('feedback_item', $item);
}
return $DB->get_record('feedback_item', array('id'=>$item->id));
}
/**
* Helper function for collected data, both for analysis page and export to excel
*
* @param stdClass $item the db-object from feedback_item
* @param int|false $groupid
* @param int $courseid
* @return stdClass
*/
protected function get_analysed($item, $groupid = false, $courseid = false) {
$presentation = $item->presentation;
$analysed_val = new stdClass();
$analysed_val->data = null;
$analysed_val->name = $item->name;
$values = feedback_get_group_values($item, $groupid, $courseid);
if ($values) {
$data = array();
foreach ($values as $value) {
$datavalue = new stdClass();
switch($presentation) {
case self::MODE_RESPONSETIME:
$datavalue->value = $value->value;
$datavalue->show = $value->value ? userdate($datavalue->value) : '';
break;
case self::MODE_COURSE:
$datavalue->value = $value->value;
$datavalue->show = $datavalue->value;
break;
case self::MODE_CATEGORY:
$datavalue->value = $value->value;
$datavalue->show = $datavalue->value;
break;
}
$data[] = $datavalue;
}
$analysed_val->data = $data;
}
return $analysed_val;
}
public function get_printval($item, $value) {
if (strval($value->value) === '') {
return '';
}
return $item->presentation == self::MODE_RESPONSETIME ?
userdate($value->value) : $value->value;
}
public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
echo "<table class=\"analysis itemtype_{$item->typ}\">";
$analysed_item = $this->get_analysed($item, $groupid, $courseid);
$data = $analysed_item->data;
if (is_array($data)) {
echo '<tr><th class="text-left">';
echo $itemnr . ' ';
if (strval($item->label) !== '') {
echo '('. format_string($item->label).') ';
}
echo format_text($item->name, FORMAT_HTML, array('noclean' => true, 'para' => false));
echo '</th></tr>';
$sizeofdata = count($data);
for ($i = 0; $i < $sizeofdata; $i++) {
$class = strlen(trim($data[$i]->show)) ? '' : ' class="isempty"';
echo '<tr'.$class.'><td class="singlevalue">';
echo str_replace("\n", '<br />', $data[$i]->show);
echo '</td></tr>';
}
}
echo '</table>';
}
public function excelprint_item(&$worksheet, $row_offset,
$xls_formats, $item,
$groupid, $courseid = false) {
$analysed_item = $this->get_analysed($item, $groupid, $courseid);
$worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
$worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
$data = $analysed_item->data;
if (is_array($data)) {
$worksheet->write_string($row_offset, 2, $data[0]->show, $xls_formats->value_bold);
$row_offset++;
$sizeofdata = count($data);
for ($i = 1; $i < $sizeofdata; $i++) {
$worksheet->write_string($row_offset, 2, $data[$i]->show, $xls_formats->default);
$row_offset++;
}
}
$row_offset++;
return $row_offset;
}
/**
* Calculates the value of the item (time, course, course category)
*
* @param stdClass $item
* @param stdClass $feedback
* @param int $courseid
* @return string
*/
protected function get_current_value($item, $feedback, $courseid) {
global $DB;
switch ($item->presentation) {
case self::MODE_RESPONSETIME:
if ($feedback->anonymous != FEEDBACK_ANONYMOUS_YES) {
// Response time is not allowed in anonymous feedbacks.
return time();
}
break;
case self::MODE_COURSE:
$course = get_course($courseid);
return format_string($course->shortname, true,
array('context' => context_course::instance($course->id)));
break;
case self::MODE_CATEGORY:
if ($courseid !== SITEID) {
$coursecategory = $DB->get_record_sql('SELECT cc.id, cc.name FROM {course_categories} cc, {course} c '
. 'WHERE c.category = cc.id AND c.id = ?', array($courseid));
return format_string($coursecategory->name, true,
array('context' => context_coursecat::instance($coursecategory->id)));
}
break;
}
return '';
}
/**
* Adds an input element to the complete form
*
* @param stdClass $item
* @param mod_feedback_complete_form $form
*/
public function complete_form_element($item, $form) {
if ($form->get_mode() == mod_feedback_complete_form::MODE_VIEW_RESPONSE) {
$value = strval($form->get_item_value($item));
} else {
$value = $this->get_current_value($item,
$form->get_feedback(), $form->get_current_course_id());
}
$printval = $this->get_printval($item, (object)['value' => $value]);
$class = '';
switch ($item->presentation) {
case self::MODE_RESPONSETIME:
$class = 'info-responsetime';
$value = $value ? self::CURRENTTIMESTAMP : '';
break;
case self::MODE_COURSE:
$class = 'info-course';
break;
case self::MODE_CATEGORY:
$class = 'info-category';
break;
}
$name = $this->get_display_name($item);
$inputname = $item->typ . '_' . $item->id;
$element = $form->add_form_element($item,
['select', $inputname, $name,
array($value => $printval),
array('class' => $class)],
false,
false);
$form->set_element_default($inputname, $value);
$element->freeze();
if ($form->get_mode() == mod_feedback_complete_form::MODE_COMPLETE) {
$element->setPersistantFreeze(true);
}
}
/**
* Converts the value from complete_form data to the string value that is stored in the db.
* @param mixed $value element from mod_feedback_complete_form::get_data() with the name $item->typ.'_'.$item->id
* @return string
*/
public function create_value($value) {
if ($value === self::CURRENTTIMESTAMP) {
return strval(time());
}
return parent::create_value($value);
}
public function can_switch_require() {
return false;
}
public function get_data_for_external($item) {
global $DB;
$feedback = $DB->get_record('feedback', array('id' => $item->feedback), '*', MUST_EXIST);
// Return the default value (course name, category name or timestamp).
return $this->get_current_value($item, $feedback, $feedback->course);
}
/**
* Return the analysis data ready for external functions.
*
* @param stdClass $item the item (question) information
* @param int $groupid the group id to filter data (optional)
* @param int $courseid the course id (optional)
* @return array an array of data with non scalar types json encoded
* @since Moodle 3.3
*/
public function get_analysed_for_external($item, $groupid = false, $courseid = false) {
$externaldata = array();
$data = $this->get_analysed($item, $groupid, $courseid);
if (is_array($data->data)) {
foreach ($data->data as $d) {
$externaldata[] = json_encode($d);
}
}
return $externaldata;
}
}
+51
View File
@@ -0,0 +1,51 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_form_class.php');
class feedback_label_form extends feedback_item_form {
protected $type = "label";
private $area;
public function definition() {
global $CFG;
$item = $this->_customdata['item'];
$common = $this->_customdata['common'];
$presentationoptions = $this->_customdata['presentationoptions'];
$positionlist = $this->_customdata['positionlist'];
$position = $this->_customdata['position'];
$mform =& $this->_form;
$mform->addElement('hidden', 'required', 0);
$mform->setType('required', PARAM_INT);
$mform->addElement('hidden', 'name', 'label');
$mform->setType('template', PARAM_ALPHA);
$mform->addElement('hidden', 'label', '');
$mform->setType('label', PARAM_ALPHA);
$mform->addElement('header', 'general', get_string($this->type, 'feedback'));
$mform->addElement('editor', 'presentation_editor', get_string('labelcontents', 'feedback'), null, $presentationoptions);
$mform->setType('presentation_editor', PARAM_RAW);
parent::definition();
$this->set_data($item);
}
}
+259
View File
@@ -0,0 +1,259 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') OR die('not allowed');
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
require_once($CFG->libdir.'/formslib.php');
class feedback_item_label extends feedback_item_base {
protected $type = "label";
private $presentationoptions = null;
private $context;
/**
* Constructor
*/
public function __construct() {
$this->presentationoptions = array('maxfiles' => EDITOR_UNLIMITED_FILES,
'trusttext'=>true);
}
public function build_editform($item, $feedback, $cm) {
global $DB, $CFG;
require_once('label_form.php');
//get the lastposition number of the feedback_items
$position = $item->position;
$lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->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);
//all items for dependitem
$feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
$commonparams = array('cmid'=>$cm->id,
'id'=>isset($item->id) ? $item->id : null,
'typ'=>$item->typ,
'items'=>$feedbackitems,
'feedback'=>$feedback->id);
$this->context = context_module::instance($cm->id);
//preparing the editor for new file-api
$item->presentationformat = FORMAT_HTML;
$item->presentationtrust = 1;
// Append editor context to presentation options, giving preference to existing context.
$this->presentationoptions = array_merge(array('context' => $this->context),
$this->presentationoptions);
$item = file_prepare_standard_editor($item,
'presentation', //name of the form element
$this->presentationoptions,
$this->context,
'mod_feedback',
'item', //the filearea
$item->id);
//build the form
$customdata = array('item' => $item,
'common' => $commonparams,
'positionlist' => $positionlist,
'position' => $position,
'presentationoptions' => $this->presentationoptions);
$this->item_form = new feedback_label_form('edit_item.php', $customdata);
}
public function save_item() {
global $DB;
if (!$this->get_data()) {
return false;
}
$item = $this->item;
if (isset($item->clone_item) AND $item->clone_item) {
$item->id = ''; //to clone this item
$item->position++;
}
$item->presentation = '';
$item->hasvalue = $this->get_hasvalue();
if (!$item->id) {
$item->id = $DB->insert_record('feedback_item', $item);
} else {
$DB->update_record('feedback_item', $item);
}
$item = file_postupdate_standard_editor($item,
'presentation',
$this->presentationoptions,
$this->context,
'mod_feedback',
'item',
$item->id);
$DB->update_record('feedback_item', $item);
return $DB->get_record('feedback_item', array('id'=>$item->id));
}
/**
* prepares the item for output or export to file
* @param stdClass $item
* @return string
*/
private function print_item($item) {
global $DB, $CFG;
require_once($CFG->libdir . '/filelib.php');
//is the item a template?
if (!$item->feedback AND $item->template) {
$template = $DB->get_record('feedback_template', array('id'=>$item->template));
if ($template->ispublic) {
$context = context_system::instance();
} else {
$context = context_course::instance($template->course);
}
$filearea = 'template';
} else {
$cm = get_coursemodule_from_instance('feedback', $item->feedback);
$context = context_module::instance($cm->id);
$filearea = 'item';
}
$item->presentationformat = FORMAT_HTML;
$item->presentationtrust = 1;
$output = file_rewrite_pluginfile_urls($item->presentation,
'pluginfile.php',
$context->id,
'mod_feedback',
$filearea,
$item->id);
$formatoptions = array('overflowdiv'=>true, 'trusted'=>$CFG->enabletrusttext);
echo format_text($output, FORMAT_HTML, $formatoptions);
}
/**
* @param stdClass $item
* @param bool|true $withpostfix
* @return string
*/
public function get_display_name($item, $withpostfix = true) {
return '';
}
/**
* Adds an input element to the complete form
*
* @param stdClass $item
* @param mod_feedback_complete_form $form
*/
public function complete_form_element($item, $form) {
global $DB;
if (!$item->feedback AND $item->template) {
// This is a template.
$template = $DB->get_record('feedback_template', array('id' => $item->template));
if ($template->ispublic) {
$context = context_system::instance();
} else {
$context = context_course::instance($template->course);
}
$filearea = 'template';
} else {
// This is a question in the current feedback.
$context = $form->get_cm()->context;
$filearea = 'item';
}
$output = file_rewrite_pluginfile_urls($item->presentation, 'pluginfile.php',
$context->id, 'mod_feedback', $filearea, $item->id);
$formatoptions = array('overflowdiv' => true, 'noclean' => true);
$output = format_text($output, FORMAT_HTML, $formatoptions);
$output = html_writer::div($output, '', ['id' => 'feedback_item_' . $item->id]);
$inputname = $item->typ . '_' . $item->id;
$name = $this->get_display_name($item);
$form->add_form_element($item, ['static', $inputname, $name, $output], false, false);
}
public function compare_value($item, $dbvalue, $dependvalue) {
return false;
}
public function postupdate($item) {
global $DB;
$context = context_module::instance($item->cmid);
$item = file_postupdate_standard_editor($item,
'presentation',
$this->presentationoptions,
$context,
'mod_feedback',
'item',
$item->id);
$DB->update_record('feedback_item', $item);
return $item->id;
}
public function get_hasvalue() {
return 0;
}
public function can_switch_require() {
return false;
}
public function excelprint_item(&$worksheet,
$row_offset,
$xls_formats,
$item,
$groupid,
$courseid = false) {
}
public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
}
public function get_printval($item, $value) {
}
/**
* Return the analysis data ready for external functions.
*
* @param stdClass $item the item (question) information
* @param int $groupid the group id to filter data (optional)
* @param int $courseid the course id (optional)
* @return array an array of data with non scalar types json encoded
* @since Moodle 3.3
*/
public function get_analysed_for_external($item, $groupid = false, $courseid = false) {
return [];
}
}
+516
View File
@@ -0,0 +1,516 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') OR die('not allowed');
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
define('FEEDBACK_MULTICHOICE_TYPE_SEP', '>>>>>');
define('FEEDBACK_MULTICHOICE_LINE_SEP', '|');
define('FEEDBACK_MULTICHOICE_ADJUST_SEP', '<<<<<');
define('FEEDBACK_MULTICHOICE_IGNOREEMPTY', 'i');
define('FEEDBACK_MULTICHOICE_HIDENOSELECT', 'h');
class feedback_item_multichoice extends feedback_item_base {
protected $type = "multichoice";
public function build_editform($item, $feedback, $cm) {
global $DB, $CFG;
require_once('multichoice_form.php');
//get the lastposition number of the feedback_items
$position = $item->position;
$lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->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;
$info = $this->get_info($item);
$item->ignoreempty = $this->ignoreempty($item);
$item->hidenoselect = $this->hidenoselect($item);
//all items for dependitem
$feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
$commonparams = array('cmid'=>$cm->id,
'id'=>isset($item->id) ? $item->id : null,
'typ'=>$item->typ,
'items'=>$feedbackitems,
'feedback'=>$feedback->id);
//build the form
$customdata = array('item' => $item,
'common' => $commonparams,
'positionlist' => $positionlist,
'position' => $position,
'info' => $info);
$this->item_form = new feedback_multichoice_form('edit_item.php', $customdata);
}
public function save_item() {
global $DB;
if (!$this->get_data()) {
return false;
}
$item = $this->item;
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('feedback_item', $item);
} else {
$DB->update_record('feedback_item', $item);
}
return $DB->get_record('feedback_item', array('id'=>$item->id));
}
//gets an array with three values(typ, name, XXX)
//XXX is an object with answertext, answercount and quotient
/**
* Helper function for collected data, both for analysis page and export to excel
*
* @param stdClass $item the db-object from feedback_item
* @param int $groupid
* @param int $courseid
* @return array|null
*/
protected function get_analysed($item, $groupid = false, $courseid = false) {
$info = $this->get_info($item);
$analysed_item = array();
$analysed_item[] = $item->typ;
$analysed_item[] = format_string($item->name);
//get the possible answers
$answers = null;
$answers = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
if (!is_array($answers)) {
return null;
}
//get the values
$values = feedback_get_group_values($item, $groupid, $courseid, $this->ignoreempty($item));
if (!$values) {
return null;
}
//get answertext, answercount and quotient for each answer
$analysed_answer = array();
if ($info->subtype == 'c') {
$sizeofanswers = count($answers);
for ($i = 1; $i <= $sizeofanswers; $i++) {
$ans = new stdClass();
$ans->answertext = $answers[$i-1];
$ans->answercount = 0;
foreach ($values as $value) {
//ist die Antwort gleich dem index der Antworten + 1?
$vallist = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $value->value);
foreach ($vallist as $val) {
if ($val == $i) {
$ans->answercount++;
}
}
}
$ans->quotient = $ans->answercount / count($values);
$analysed_answer[] = $ans;
}
} else {
$sizeofanswers = count($answers);
for ($i = 1; $i <= $sizeofanswers; $i++) {
$ans = new stdClass();
$ans->answertext = $answers[$i-1];
$ans->answercount = 0;
foreach ($values as $value) {
//ist die Antwort gleich dem index der Antworten + 1?
if ($value->value == $i) {
$ans->answercount++;
}
}
$ans->quotient = $ans->answercount / count($values);
$analysed_answer[] = $ans;
}
}
$analysed_item[] = $analysed_answer;
return $analysed_item;
}
public function get_printval($item, $value) {
$info = $this->get_info($item);
$printval = '';
if (!isset($value->value)) {
return $printval;
}
$presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
if ($info->subtype == 'c') {
$vallist = array_values(explode (FEEDBACK_MULTICHOICE_LINE_SEP, $value->value));
$sizeofvallist = count($vallist);
$sizeofpresentation = count($presentation);
for ($i = 0; $i < $sizeofvallist; $i++) {
for ($k = 0; $k < $sizeofpresentation; $k++) {
if ($vallist[$i] == ($k + 1)) {//Die Werte beginnen bei 1, das Array aber mit 0
$printval .= trim(format_string($presentation[$k])) . chr(10);
break;
}
}
}
} else {
$index = 1;
foreach ($presentation as $pres) {
if ($value->value == $index) {
$printval = format_string($pres);
break;
}
$index++;
}
}
return $printval;
}
public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
global $OUTPUT;
$analysed_item = $this->get_analysed($item, $groupid, $courseid);
if ($analysed_item) {
$itemname = $analysed_item[1];
echo "<table class=\"analysis itemtype_{$item->typ}\">";
echo '<tr><th class="text-left">';
echo $itemnr . ' ';
if (strval($item->label) !== '') {
echo '('. format_string($item->label).') ';
}
echo format_string($itemname);
echo '</th></tr>';
$analysed_vals = $analysed_item[2];
$count = 0;
$data = [];
foreach ($analysed_vals as $val) {
$quotient = format_float($val->quotient * 100, 2);
$strquotient = '';
if ($val->quotient > 0) {
$strquotient = ' ('. $quotient . ' %)';
}
$answertext = format_text(trim($val->answertext), FORMAT_HTML,
array('noclean' => true, 'para' => false));
$data['labels'][$count] = $answertext;
$data['series'][$count] = $val->answercount;
$data['series_labels'][$count] = $val->answercount . $strquotient;
$count++;
}
$chart = new \core\chart_bar();
$chart->set_horizontal(true);
$series = new \core\chart_series(format_string(get_string("responses", "feedback")), $data['series']);
$series->set_labels($data['series_labels']);
$chart->add_series($series);
$chart->set_labels($data['labels']);
echo '<tr><td>'. $OUTPUT->render($chart) . '</td></tr>';
echo "</table>";
}
}
public function excelprint_item(&$worksheet, $row_offset,
$xls_formats, $item,
$groupid, $courseid = false) {
$analysed_item = $this->get_analysed($item, $groupid, $courseid);
if (!$analysed_item) {
return $row_offset;
}
$data = $analysed_item[2];
//frage schreiben
$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)) {
$sizeofdata = count($data);
for ($i = 0; $i < $sizeofdata; $i++) {
$analysed_data = $data[$i];
$worksheet->write_string($row_offset,
$i + 2,
trim($analysed_data->answertext),
$xls_formats->head2);
$worksheet->write_number($row_offset + 1,
$i + 2,
$analysed_data->answercount,
$xls_formats->default);
$worksheet->write_number($row_offset + 2,
$i + 2,
$analysed_data->quotient,
$xls_formats->procent);
}
}
$row_offset += 3;
return $row_offset;
}
/**
* Options for the multichoice element
* @param stdClass $item
* @return array
*/
protected function get_options($item) {
$info = $this->get_info($item);
$presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
$options = array();
foreach ($presentation as $idx => $optiontext) {
$options[$idx + 1] = format_text($optiontext, FORMAT_HTML, array('noclean' => true, 'para' => false));
}
if ($info->subtype === 'r' && !$this->hidenoselect($item)) {
$options = array(0 => get_string('not_selected', 'feedback')) + $options;
}
return $options;
}
/**
* Adds an input element to the complete form
*
* This element has many options - it can be displayed as group or radio elements,
* group of checkboxes or a dropdown list.
*
* @param stdClass $item
* @param mod_feedback_complete_form $form
*/
public function complete_form_element($item, $form) {
$info = $this->get_info($item);
$name = $this->get_display_name($item);
$class = 'multichoice-' . $info->subtype;
$inputname = $item->typ . '_' . $item->id;
$options = $this->get_options($item);
$separator = !empty($info->horizontal) ? ' ' : '<br>';
$tmpvalue = $form->get_item_value($item) ?? 0; // Used for element defaults, so must be a valid value (not null).
// Subtypes:
// r = radio
// c = checkbox
// d = dropdown.
if ($info->subtype === 'd' || ($info->subtype === 'r' && $form->is_frozen())) {
// Display as a dropdown in the complete form or a single value in the response view.
$element = $form->add_form_element($item,
['select', $inputname, $name, array(0 => '') + $options, array('class' => $class)],
false, false);
$form->set_element_default($inputname, $tmpvalue);
$form->set_element_type($inputname, PARAM_INT);
} else if ($info->subtype === 'c' && $form->is_frozen()) {
// Display list of checkbox values in the response view.
$objs = [];
foreach (explode(FEEDBACK_MULTICHOICE_LINE_SEP, $form->get_item_value($item)) as $v) {
$objs[] = ['static', $inputname."[$v]", '', isset($options[$v]) ? $options[$v] : ''];
}
$element = $form->add_form_group_element($item, 'group_'.$inputname, $name, $objs, $separator, $class);
} else {
// Display group or radio or checkbox elements.
$class .= ' multichoice-' . ($info->horizontal ? 'horizontal' : 'vertical');
$objs = [];
if ($info->subtype === 'c') {
// Checkboxes.
$objs[] = ['hidden', $inputname.'[0]', 0];
$form->set_element_type($inputname.'[0]', PARAM_INT);
foreach ($options as $idx => $label) {
$objs[] = ['advcheckbox', $inputname.'['.$idx.']', '', $label, null, array(0, $idx)];
$form->set_element_type($inputname.'['.$idx.']', PARAM_INT);
}
// Span to hold the element id. The id is used for drag and drop reordering.
$objs[] = ['static', '', '', html_writer::span('', '', ['id' => 'feedback_item_' . $item->id])];
$element = $form->add_form_group_element($item, 'group_'.$inputname, $name, $objs, $separator, $class);
if ($tmpvalue) {
foreach (explode(FEEDBACK_MULTICHOICE_LINE_SEP, $tmpvalue) as $v) {
$form->set_element_default($inputname.'['.$v.']', $v);
}
}
} else {
// Radio.
if (!array_key_exists(0, $options)) {
// Always add a hidden element to the group to guarantee we get a value in the submit data.
$objs[] = ['hidden', $inputname, 0];
}
foreach ($options as $idx => $label) {
$objs[] = ['radio', $inputname, '', $label, $idx];
}
// Span to hold the element id. The id is used for drag and drop reordering.
$objs[] = ['static', '', '', html_writer::span('', '', ['id' => 'feedback_item_' . $item->id])];
$element = $form->add_form_group_element($item, 'group_'.$inputname, $name, $objs, $separator, $class);
$form->set_element_default($inputname, $tmpvalue);
$form->set_element_type($inputname, PARAM_INT);
}
}
// Process 'required' rule.
if ($item->required) {
$elementname = $element->getName();
$form->add_validation_rule(function($values) use ($elementname, $item) {
$inputname = $item->typ . '_' . $item->id;
return empty($values[$inputname]) || (is_array($values[$inputname]) && !array_filter($values[$inputname])) ?
array($elementname => get_string('required')) : true;
});
}
}
/**
* Prepares value that user put in the form for storing in DB
* @param array $value
* @return string
*/
public function create_value($value) {
// Could be an array (multichoice checkbox) or single value (multichoice radio or dropdown).
$value = is_array($value) ? $value : [$value];
$value = array_unique(array_filter($value));
return join(FEEDBACK_MULTICHOICE_LINE_SEP, $value);
}
/**
* Compares the dbvalue with the dependvalue
*
* @param stdClass $item
* @param string $dbvalue is the value input by user in the format as it is stored in the db
* @param string $dependvalue is the value that it needs to be compared against
*/
public function compare_value($item, $dbvalue, $dependvalue) {
if (is_array($dbvalue)) {
$dbvalues = $dbvalue;
} else {
$dbvalues = explode(FEEDBACK_MULTICHOICE_LINE_SEP, $dbvalue);
}
$info = $this->get_info($item);
$presentation = explode (FEEDBACK_MULTICHOICE_LINE_SEP, $info->presentation);
$index = 1;
foreach ($presentation as $pres) {
foreach ($dbvalues as $dbval) {
if ($dbval == $index AND trim($pres) == $dependvalue) {
return true;
}
}
$index++;
}
return false;
}
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;
$parts = explode(FEEDBACK_MULTICHOICE_TYPE_SEP, $item->presentation);
$info->subtype = $parts[0];
if (count($parts) > 1) {
$info->presentation = $parts[1];
}
if (!isset($info->subtype)) {
$info->subtype = 'r';
}
if ($info->subtype != 'd') {
$parts = explode(FEEDBACK_MULTICHOICE_ADJUST_SEP, $info->presentation);
$info->presentation = $parts[0];
if (count($parts) > 1) {
$info->horizontal = $parts[1];
}
if (isset($info->horizontal) AND $info->horizontal == 1) {
$info->horizontal = true;
} else {
$info->horizontal = false;
}
}
return $info;
}
public function set_ignoreempty($item, $ignoreempty=true) {
$item->options = str_replace(FEEDBACK_MULTICHOICE_IGNOREEMPTY, '', $item->options);
if ($ignoreempty) {
$item->options .= FEEDBACK_MULTICHOICE_IGNOREEMPTY;
}
}
public function ignoreempty($item) {
if (strstr($item->options, FEEDBACK_MULTICHOICE_IGNOREEMPTY)) {
return true;
}
return false;
}
public function set_hidenoselect($item, $hidenoselect=true) {
$item->options = str_replace(FEEDBACK_MULTICHOICE_HIDENOSELECT, '', $item->options);
if ($hidenoselect) {
$item->options .= FEEDBACK_MULTICHOICE_HIDENOSELECT;
}
}
public function hidenoselect($item) {
if (strstr($item->options, FEEDBACK_MULTICHOICE_HIDENOSELECT)) {
return true;
}
return false;
}
/**
* Return the analysis data ready for external functions.
*
* @param stdClass $item the item (question) information
* @param int $groupid the group id to filter data (optional)
* @param int $courseid the course id (optional)
* @return array an array of data with non scalar types json encoded
* @since Moodle 3.3
*/
public function get_analysed_for_external($item, $groupid = false, $courseid = false) {
$externaldata = array();
$data = $this->get_analysed($item, $groupid, $courseid);
if ($data && !empty($data[2]) && is_array($data[2])) {
foreach ($data[2] as $d) {
$externaldata[] = json_encode($d);
}
}
return $externaldata;
}
}
@@ -0,0 +1,117 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_form_class.php');
class feedback_multichoice_form extends feedback_item_form {
protected $type = "multichoice";
public function definition() {
$item = $this->_customdata['item'];
$common = $this->_customdata['common'];
$positionlist = $this->_customdata['positionlist'];
$position = $this->_customdata['position'];
$mform =& $this->_form;
$mform->addElement('header', 'general', get_string($this->type, 'feedback'));
$mform->addElement('advcheckbox', 'required', get_string('required', 'feedback'), '' , null , array(0, 1));
$mform->addElement('text',
'name',
get_string('item_name', 'feedback'),
array('size' => FEEDBACK_ITEM_NAME_TEXTBOX_SIZE,
'maxlength' => 255));
$mform->addElement('text',
'label',
get_string('item_label', 'feedback'),
array('size' => FEEDBACK_ITEM_LABEL_TEXTBOX_SIZE,
'maxlength' => 255));
$mform->addElement('select',
'subtype',
get_string('multichoicetype', 'feedback').'&nbsp;',
array('r'=>get_string('radio', 'feedback'),
'c'=>get_string('check', 'feedback'),
'd'=>get_string('dropdown', 'feedback')));
$mform->addElement('select',
'horizontal',
get_string('adjustment', 'feedback').'&nbsp;',
array(0 => get_string('vertical', 'feedback'),
1 => get_string('horizontal', 'feedback')));
$mform->hideIf('horizontal', 'subtype', 'eq', 'd');
$mform->addElement('selectyesno',
'hidenoselect',
get_string('hide_no_select_option', 'feedback'));
$mform->hideIf('hidenoselect', 'subtype', 'ne', 'r');
$mform->addElement('selectyesno',
'ignoreempty',
get_string('do_not_analyse_empty_submits', 'feedback'));
$mform->addElement('textarea', 'values', get_string('multichoice_values', 'feedback'),
'wrap="virtual" rows="10" cols="65"');
$mform->addElement('static', 'hint', '', get_string('use_one_line_for_each_value', 'feedback'));
parent::definition();
$this->set_data($item);
}
public function set_data($item) {
$info = $this->_customdata['info'];
$item->horizontal = $info->horizontal;
$item->subtype = $info->subtype;
$itemvalues = str_replace(FEEDBACK_MULTICHOICE_LINE_SEP, "\n", $info->presentation);
$itemvalues = str_replace("\n\n", "\n", $itemvalues);
$item->values = $itemvalues;
return parent::set_data($item);
}
public function get_data() {
if (!$item = parent::get_data()) {
return false;
}
$presentation = str_replace("\n", FEEDBACK_MULTICHOICE_LINE_SEP, trim($item->values));
if (!isset($item->subtype)) {
$subtype = 'r';
} else {
$subtype = substr($item->subtype, 0, 1);
}
if (isset($item->horizontal) AND $item->horizontal == 1 AND $subtype != 'd') {
$presentation .= FEEDBACK_MULTICHOICE_ADJUST_SEP.'1';
}
if (!isset($item->hidenoselect)) {
$item->hidenoselect = 1;
}
if (!isset($item->ignoreempty)) {
$item->ignoreempty = 0;
}
$item->presentation = $subtype.FEEDBACK_MULTICHOICE_TYPE_SEP.$presentation;
return $item;
}
}
+509
View File
@@ -0,0 +1,509 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') OR die('not allowed');
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
define('FEEDBACK_RADIORATED_ADJUST_SEP', '<<<<<');
define('FEEDBACK_MULTICHOICERATED_MAXCOUNT', 10); //count of possible items
define('FEEDBACK_MULTICHOICERATED_VALUE_SEP', '####');
define('FEEDBACK_MULTICHOICERATED_VALUE_SEP2', '/');
define('FEEDBACK_MULTICHOICERATED_TYPE_SEP', '>>>>>');
define('FEEDBACK_MULTICHOICERATED_LINE_SEP', '|');
define('FEEDBACK_MULTICHOICERATED_ADJUST_SEP', '<<<<<');
define('FEEDBACK_MULTICHOICERATED_IGNOREEMPTY', 'i');
define('FEEDBACK_MULTICHOICERATED_HIDENOSELECT', 'h');
class feedback_item_multichoicerated extends feedback_item_base {
protected $type = "multichoicerated";
public function build_editform($item, $feedback, $cm) {
global $DB, $CFG;
require_once('multichoicerated_form.php');
//get the lastposition number of the feedback_items
$position = $item->position;
$lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->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;
$info = $this->get_info($item);
$item->ignoreempty = $this->ignoreempty($item);
$item->hidenoselect = $this->hidenoselect($item);
//all items for dependitem
$feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
$commonparams = array('cmid'=>$cm->id,
'id'=>isset($item->id) ? $item->id : null,
'typ'=>$item->typ,
'items'=>$feedbackitems,
'feedback'=>$feedback->id);
//build the form
$customdata = array('item' => $item,
'common' => $commonparams,
'positionlist' => $positionlist,
'position' => $position,
'info' => $info);
$this->item_form = new feedback_multichoicerated_form('edit_item.php', $customdata);
}
/**
* Saves item
*
* @return stdClass
*/
public function save_item() {
global $DB;
if (!$this->get_data()) {
return false;
}
$item = $this->item;
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('feedback_item', $item);
} else {
$DB->update_record('feedback_item', $item);
}
return $DB->get_record('feedback_item', array('id'=>$item->id));
}
/**
* Helper function for collected data, both for analysis page and export to excel
*
* @param stdClass $item the db-object from feedback_item
* @param int $groupid
* @param int $courseid
* @return array|null
*/
protected 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 = null;
$lines = explode (FEEDBACK_MULTICHOICERATED_LINE_SEP, $info->presentation);
if (!is_array($lines)) {
return null;
}
//die Werte holen
$values = feedback_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(FEEDBACK_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 (FEEDBACK_MULTICHOICERATED_LINE_SEP, $info->presentation);
$index = 1;
foreach ($presentation as $pres) {
if ($value->value == $index) {
$item_label = explode(FEEDBACK_MULTICHOICERATED_VALUE_SEP, $pres);
$printval = format_string($item_label[1]);
break;
}
$index++;
}
return $printval;
}
public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
global $OUTPUT;
$analysed_item = $this->get_analysed($item, $groupid, $courseid);
if ($analysed_item) {
echo "<table class=\"analysis itemtype_{$item->typ}\">";
echo '<tr><th class="text-left">';
echo $itemnr . ' ';
if (strval($item->label) !== '') {
echo '('. format_string($item->label).') ';
}
echo format_string($analysed_item[1]);
echo '</th></tr>';
$analysed_vals = $analysed_item[2];
$avg = 0.0;
$count = 0;
$data = [];
foreach ($analysed_vals as $val) {
$avg += $val->avg;
$quotient = format_float($val->quotient * 100, 2);
$answertext = '('.$val->value.') ' . format_text(trim($val->answertext), FORMAT_HTML,
array('noclean' => true, 'para' => false));
if ($val->quotient > 0) {
$strquotient = ' ('.$quotient.' %)';
} else {
$strquotient = '';
}
$data['labels'][$count] = $answertext;
$data['series'][$count] = $val->answercount;
$data['series_labels'][$count] = $val->answercount . $strquotient;
$count++;
}
$chart = new \core\chart_bar();
$chart->set_horizontal(true);
$series = new \core\chart_series(format_string(get_string("responses", "feedback")), $data['series']);
$series->set_labels($data['series_labels']);
$chart->add_series($series);
$chart->set_labels($data['labels']);
echo '<tr><td>'. $OUTPUT->render($chart) . '</td></tr>';
$avg = format_float($avg, 2);
echo '<tr><td class="text-left"><b>';
echo get_string('average', 'feedback').': '.$avg.'</b>';
echo '</td></tr>';
echo '</table>';
}
}
public function excelprint_item(&$worksheet, $row_offset,
$xls_formats, $item,
$groupid, $courseid = false) {
$analysed_item = $this->get_analysed($item, $groupid, $courseid);
if (!$analysed_item) {
return $row_offset;
}
$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', 'feedback'),
$xls_formats->value_bold);
$worksheet->write_number($row_offset + 1,
count($data) + 2,
$avg,
$xls_formats->value_bold);
}
$row_offset +=2;
return $row_offset;
}
/**
* Options for the multichoice element
* @param stdClass $item
* @return array
*/
protected function get_options($item) {
$info = $this->get_info($item);
$lines = explode(FEEDBACK_MULTICHOICERATED_LINE_SEP, $info->presentation);
$options = array();
foreach ($lines as $idx => $line) {
list($weight, $optiontext) = explode(FEEDBACK_MULTICHOICERATED_VALUE_SEP, $line);
$a = new stdclass();
$a->weight = $weight;
$a->name = format_text($optiontext, FORMAT_HTML, array('noclean' => true, 'para' => false));
$options[$idx + 1] = get_string('multichoiceoption', 'feedback', $a);
}
if ($info->subtype === 'r' && !$this->hidenoselect($item)) {
$options = array(0 => get_string('not_selected', 'feedback')) + $options;
}
return $options;
}
/**
* Adds an input element to the complete form
*
* @param stdClass $item
* @param mod_feedback_complete_form $form
*/
public function complete_form_element($item, $form) {
$info = $this->get_info($item);
$name = $this->get_display_name($item);
$class = 'multichoicerated-' . $info->subtype;
$inputname = $item->typ . '_' . $item->id;
$options = $this->get_options($item);
if ($info->subtype === 'd' || $form->is_frozen()) {
$el = $form->add_form_element($item,
['select', $inputname, $name, array('' => '') + $options, array('class' => $class)]);
} else {
$objs = array();
if (!array_key_exists(0, $options)) {
// Always add '0' as hidden element, otherwise form submit data may not have this element.
$objs[] = ['hidden', $inputname];
}
foreach ($options as $idx => $label) {
$objs[] = ['radio', $inputname, '', $label, $idx];
}
// Span to hold the element id. The id is used for drag and drop reordering.
$objs[] = ['static', '', '', html_writer::span('', '', ['id' => 'feedback_item_' . $item->id])];
$separator = $info->horizontal ? ' ' : '<br>';
$class .= ' multichoicerated-' . ($info->horizontal ? 'horizontal' : 'vertical');
$el = $form->add_form_group_element($item, 'group_'.$inputname, $name, $objs, $separator, $class);
$form->set_element_type($inputname, PARAM_INT);
// Set previously input values.
$form->set_element_default($inputname, $form->get_item_value($item));
// Process "required" rule.
if ($item->required) {
$form->add_validation_rule(function($values, $files) use ($item) {
$inputname = $item->typ . '_' . $item->id;
return empty($values[$inputname]) ? array('group_' . $inputname => get_string('required')) : true;
});
}
}
}
/**
* Compares the dbvalue with the dependvalue
*
* @param stdClass $item
* @param string $dbvalue is the value input by user in the format as it is stored in the db
* @param string $dependvalue is the value that it needs to be compared against
*/
public function compare_value($item, $dbvalue, $dependvalue) {
if (is_array($dbvalue)) {
$dbvalues = $dbvalue;
} else {
$dbvalues = explode(FEEDBACK_MULTICHOICERATED_LINE_SEP, $dbvalue);
}
$info = $this->get_info($item);
$presentation = explode (FEEDBACK_MULTICHOICERATED_LINE_SEP, $info->presentation);
$index = 1;
foreach ($presentation as $pres) {
$presvalues = explode(FEEDBACK_MULTICHOICERATED_VALUE_SEP, $pres);
foreach ($dbvalues as $dbval) {
if ($dbval == $index AND trim($presvalues[1]) == $dependvalue) {
return true;
}
}
$index++;
}
return false;
}
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;
$parts = explode(FEEDBACK_MULTICHOICERATED_TYPE_SEP, $item->presentation);
$info->subtype = $parts[0];
if (count($parts) > 1) {
$info->presentation = $parts[1];
}
if (!isset($info->subtype)) {
$info->subtype = 'r';
}
if ($info->subtype != 'd') {
$parts = explode(FEEDBACK_MULTICHOICERATED_ADJUST_SEP, $info->presentation);
$info->presentation = $parts[0];
if (count($parts) > 1) {
$info->horizontal = $parts[1];
}
if (isset($info->horizontal) AND $info->horizontal == 1) {
$info->horizontal = true;
} else {
$info->horizontal = false;
}
}
$info->values = $this->prepare_presentation_values_print($info->presentation,
FEEDBACK_MULTICHOICERATED_VALUE_SEP,
FEEDBACK_MULTICHOICERATED_VALUE_SEP2);
return $info;
}
public function prepare_presentation_values($linesep1,
$linesep2,
$valuestring,
$valuesep1,
$valuesep2) {
$lines = explode($linesep1, $valuestring);
$newlines = array();
foreach ($lines as $line) {
$value = '';
$text = '';
if (strpos($line, $valuesep1) === false) {
$value = 0;
$text = $line;
} else {
@list($value, $text) = explode($valuesep1, $line, 2);
}
$value = intval($value);
$newlines[] = $value.$valuesep2.$text;
}
$newlines = implode($linesep2, $newlines);
return $newlines;
}
public function prepare_presentation_values_print($valuestring, $valuesep1, $valuesep2) {
$valuestring = str_replace(array("\n","\r"), "", $valuestring);
return $this->prepare_presentation_values(FEEDBACK_MULTICHOICERATED_LINE_SEP,
"\n",
$valuestring,
$valuesep1,
$valuesep2);
}
public function prepare_presentation_values_save($valuestring, $valuesep1, $valuesep2) {
$valuestring = str_replace("\r", "\n", $valuestring);
$valuestring = str_replace("\n\n", "\n", $valuestring);
return $this->prepare_presentation_values("\n",
FEEDBACK_MULTICHOICERATED_LINE_SEP,
$valuestring,
$valuesep1,
$valuesep2);
}
public function set_ignoreempty($item, $ignoreempty=true) {
$item->options = str_replace(FEEDBACK_MULTICHOICERATED_IGNOREEMPTY, '', $item->options);
if ($ignoreempty) {
$item->options .= FEEDBACK_MULTICHOICERATED_IGNOREEMPTY;
}
}
public function ignoreempty($item) {
if (strstr($item->options, FEEDBACK_MULTICHOICERATED_IGNOREEMPTY)) {
return true;
}
return false;
}
public function set_hidenoselect($item, $hidenoselect=true) {
$item->options = str_replace(FEEDBACK_MULTICHOICERATED_HIDENOSELECT, '', $item->options);
if ($hidenoselect) {
$item->options .= FEEDBACK_MULTICHOICERATED_HIDENOSELECT;
}
}
public function hidenoselect($item) {
if (strstr($item->options, FEEDBACK_MULTICHOICERATED_HIDENOSELECT)) {
return true;
}
return false;
}
/**
* Return the analysis data ready for external functions.
*
* @param stdClass $item the item (question) information
* @param int $groupid the group id to filter data (optional)
* @param int $courseid the course id (optional)
* @return array an array of data with non scalar types json encoded
* @since Moodle 3.3
*/
public function get_analysed_for_external($item, $groupid = false, $courseid = false) {
$externaldata = array();
$data = $this->get_analysed($item, $groupid, $courseid);
if ($data && !empty($data[2]) && is_array($data[2])) {
foreach ($data[2] as $d) {
$externaldata[] = json_encode($d);
}
}
return $externaldata;
}
}
@@ -0,0 +1,126 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_form_class.php');
class feedback_multichoicerated_form extends feedback_item_form {
protected $type = "multichoicerated";
/** @var object Form element */
protected $values;
public function definition() {
$item = $this->_customdata['item'];
$common = $this->_customdata['common'];
$positionlist = $this->_customdata['positionlist'];
$position = $this->_customdata['position'];
$mform =& $this->_form;
$mform->addElement('header', 'general', get_string($this->type, 'feedback'));
$mform->addElement('advcheckbox', 'required', get_string('required', 'feedback'), '' , null , array(0, 1));
$mform->addElement('text',
'name',
get_string('item_name', 'feedback'),
array('size'=>FEEDBACK_ITEM_NAME_TEXTBOX_SIZE,
'maxlength'=>255));
$mform->addElement('text',
'label',
get_string('item_label', 'feedback'),
array('size'=>FEEDBACK_ITEM_LABEL_TEXTBOX_SIZE,
'maxlength'=>255));
$mform->addElement('select',
'subtype',
get_string('multichoicetype', 'feedback').'&nbsp;',
array('r'=>get_string('radio', 'feedback'),
'd'=>get_string('dropdown', 'feedback')));
$mform->addElement('select',
'horizontal',
get_string('adjustment', 'feedback').'&nbsp;',
array(0 => get_string('vertical', 'feedback'),
1 => get_string('horizontal', 'feedback')));
$mform->hideIf('horizontal', 'subtype', 'eq', 'd');
$mform->addElement('selectyesno',
'hidenoselect',
get_string('hide_no_select_option', 'feedback'));
$mform->hideIf('hidenoselect', 'subtype', 'eq', 'd');
$mform->addElement('selectyesno',
'ignoreempty',
get_string('do_not_analyse_empty_submits', 'feedback'));
$mform->disabledIf('ignoreempty', 'required', 'eq', '1');
$this->values = $mform->addElement('textarea',
'values',
get_string('multichoice_values', 'feedback'),
'wrap="virtual" rows="10" cols="65"');
$mform->addElement('static',
'hint',
'',
get_string('use_one_line_for_each_value', 'feedback'));
parent::definition();
$this->set_data($item);
}
public function set_data($item) {
$info = $this->_customdata['info'];
$item->horizontal = $info->horizontal;
$item->subtype = $info->subtype;
$item->values = $info->values;
return parent::set_data($item);
}
public function get_data() {
if (!$item = parent::get_data()) {
return false;
}
$itemobj = new feedback_item_multichoicerated();
$presentation = $itemobj->prepare_presentation_values_save(trim($item->values),
FEEDBACK_MULTICHOICERATED_VALUE_SEP2,
FEEDBACK_MULTICHOICERATED_VALUE_SEP);
if (!isset($item->subtype)) {
$subtype = 'r';
} else {
$subtype = substr($item->subtype, 0, 1);
}
if (isset($item->horizontal) AND $item->horizontal == 1 AND $subtype != 'd') {
$presentation .= FEEDBACK_MULTICHOICERATED_ADJUST_SEP.'1';
}
$item->presentation = $subtype.FEEDBACK_MULTICHOICERATED_TYPE_SEP.$presentation;
if (!isset($item->hidenoselect)) {
$item->hidenoselect = 1;
}
if (!isset($item->ignoreempty)) {
$item->ignoreempty = 0;
}
return $item;
}
}
+329
View File
@@ -0,0 +1,329 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') OR die('not allowed');
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
class feedback_item_numeric extends feedback_item_base {
protected $type = "numeric";
public function build_editform($item, $feedback, $cm) {
global $DB, $CFG;
require_once('numeric_form.php');
//get the lastposition number of the feedback_items
$position = $item->position;
$lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->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;
$range_from_to = explode('|', $item->presentation);
if (isset($range_from_to[0]) AND is_numeric($range_from_to[0])) {
$range_from = $this->format_float($range_from_to[0]);
} else {
$range_from = '-';
}
if (isset($range_from_to[1]) AND is_numeric($range_from_to[1])) {
$range_to = $this->format_float($range_from_to[1]);
} else {
$range_to = '-';
}
$item->rangefrom = $range_from;
$item->rangeto = $range_to;
//all items for dependitem
$feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
$commonparams = array('cmid'=>$cm->id,
'id'=>isset($item->id) ? $item->id : null,
'typ'=>$item->typ,
'items'=>$feedbackitems,
'feedback'=>$feedback->id);
//build the form
$customdata = array('item' => $item,
'common' => $commonparams,
'positionlist' => $positionlist,
'position' => $position);
$this->item_form = new feedback_numeric_form('edit_item.php', $customdata);
}
public function save_item() {
global $DB;
if (!$this->get_data()) {
return false;
}
$item = $this->item;
if (isset($item->clone_item) AND $item->clone_item) {
$item->id = ''; //to clone this item
$item->position++;
}
$item->hasvalue = $this->get_hasvalue();
if (!$item->id) {
$item->id = $DB->insert_record('feedback_item', $item);
} else {
$DB->update_record('feedback_item', $item);
}
return $DB->get_record('feedback_item', array('id'=>$item->id));
}
/**
* Helper function for collected data, both for analysis page and export to excel
*
* @param stdClass $item the db-object from feedback_item
* @param int $groupid
* @param int $courseid
* @return stdClass
*/
protected function get_analysed($item, $groupid = false, $courseid = false) {
global $DB;
$analysed = new stdClass();
$analysed->data = array();
$analysed->name = $item->name;
$values = feedback_get_group_values($item, $groupid, $courseid);
$avg = 0.0;
$counter = 0;
if ($values) {
$data = array();
foreach ($values as $value) {
if (is_numeric($value->value)) {
$data[] = $value->value;
$avg += $value->value;
$counter++;
}
}
$avg = $counter > 0 ? $avg / $counter : null;
$analysed->data = $data;
$analysed->avg = $avg;
}
return $analysed;
}
public function get_printval($item, $value) {
if (!isset($value->value)) {
return '';
}
return $value->value;
}
public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
$values = $this->get_analysed($item, $groupid, $courseid);
if (isset($values->data) AND is_array($values->data)) {
echo "<table class=\"analysis itemtype_{$item->typ}\">";
echo '<tr><th class="text-left">';
echo $itemnr . ' ';
if (strval($item->label) !== '') {
echo '('. format_string($item->label).') ';
}
echo format_text($item->name, FORMAT_HTML, array('noclean' => true, 'para' => false));
echo '</th></tr>';
foreach ($values->data as $value) {
echo '<tr><td class="singlevalue">';
echo $this->format_float($value);
echo '</td></tr>';
}
if (isset($values->avg)) {
$avg = format_float($values->avg, 2);
} else {
$avg = '-';
}
echo '<tr><td><b>';
echo get_string('average', 'feedback').': '.$avg;
echo '</b></td></tr>';
echo '</table>';
}
}
public function excelprint_item(&$worksheet, $row_offset,
$xls_formats, $item,
$groupid, $courseid = false) {
$analysed_item = $this->get_analysed($item, $groupid, $courseid);
$worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
$worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
$data = $analysed_item->data;
if (is_array($data)) {
// Export average.
$worksheet->write_string($row_offset,
2,
get_string('average', 'feedback'),
$xls_formats->value_bold);
if (isset($analysed_item->avg)) {
$worksheet->write_number($row_offset + 1,
2,
$analysed_item->avg,
$xls_formats->value_bold);
} else {
$worksheet->write_string($row_offset + 1,
2,
'',
$xls_formats->value_bold);
}
$row_offset++;
}
$row_offset++;
return $row_offset;
}
/**
* Prints the float nicely in the localized format
*
* Similar to format_float() but automatically calculates the number of decimal places
*
* @param float $value The float to print
* @return string
*/
protected function format_float($value) {
if (!is_numeric($value)) {
return null;
}
$decimal = is_int($value) ? 0 : strlen(substr(strrchr($value, '.'), 1));
return format_float($value, $decimal);
}
/**
* Returns human-readable boundaries (min - max)
* @param stdClass $item
* @return string
*/
protected function get_boundaries_for_display($item) {
list($rangefrom, $rangeto) = explode('|', $item->presentation);
if (!isset($rangefrom) || !is_numeric($rangefrom)) {
$rangefrom = null;
}
if (!isset($rangeto) || !is_numeric($rangeto)) {
$rangeto = null;
}
if (is_null($rangefrom) && is_numeric($rangeto)) {
return ' (' . get_string('maximal', 'feedback') .
': ' . $this->format_float($rangeto) . ')';
}
if (is_numeric($rangefrom) && is_null($rangeto)) {
return ' (' . get_string('minimal', 'feedback') .
': ' . $this->format_float($rangefrom) . ')';
}
if (is_null($rangefrom) && is_null($rangeto)) {
return '';
}
return ' (' . $this->format_float($rangefrom) .
' - ' . $this->format_float($rangeto) . ')';
}
/**
* Returns the postfix to be appended to the display name that is based on other settings
*
* @param stdClass $item
* @return string
*/
public function get_display_name_postfix($item) {
return html_writer::span($this->get_boundaries_for_display($item), 'boundaries');
}
/**
* Adds an input element to the complete form
*
* @param stdClass $item
* @param mod_feedback_complete_form $form
*/
public function complete_form_element($item, $form) {
$name = $this->get_display_name($item);
$inputname = $item->typ . '_' . $item->id;
$form->add_form_element($item,
['text', $inputname, $name],
true,
false
);
$form->set_element_type($inputname, PARAM_NOTAGS);
$tmpvalue = $this->format_float($form->get_item_value($item));
$form->set_element_default($inputname, $tmpvalue);
// Add form validation rule to check for boundaries.
$form->add_validation_rule(function($values, $files) use ($item) {
$inputname = $item->typ . '_' . $item->id;
list($rangefrom, $rangeto) = explode('|', $item->presentation);
if (!isset($values[$inputname]) || trim($values[$inputname]) === '') {
return $item->required ? array($inputname => get_string('required')) : true;
}
$value = unformat_float($values[$inputname], true);
if ($value === false) {
return array($inputname => get_string('invalidnum', 'error'));
}
if ((is_numeric($rangefrom) && $value < floatval($rangefrom)) ||
(is_numeric($rangeto) && $value > floatval($rangeto))) {
return array($inputname => get_string('numberoutofrange', 'feedback'));
}
return true;
});
}
public function create_value($data) {
$data = unformat_float($data, true);
if (is_numeric($data)) {
$data = floatval($data);
} else {
$data = '';
}
return $data;
}
/**
* Return the analysis data ready for external functions.
*
* @param stdClass $item the item (question) information
* @param int $groupid the group id to filter data (optional)
* @param int $courseid the course id (optional)
* @return array an array of data with non scalar types json encoded
* @since Moodle 3.3
*/
public function get_analysed_for_external($item, $groupid = false, $courseid = false) {
$externaldata = array();
$data = $this->get_analysed($item, $groupid, $courseid);
if (is_array($data->data)) {
return $data->data; // No need to json, scalar type.
}
return $externaldata;
}
}
@@ -0,0 +1,88 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_form_class.php');
class feedback_numeric_form extends feedback_item_form {
protected $type = "numeric";
public function definition() {
$item = $this->_customdata['item'];
$common = $this->_customdata['common'];
$positionlist = $this->_customdata['positionlist'];
$position = $this->_customdata['position'];
$mform =& $this->_form;
$mform->addElement('header', 'general', get_string($this->type, 'feedback'));
$mform->addElement('advcheckbox', 'required', get_string('required', 'feedback'), '' , null , array(0, 1));
$mform->addElement('text',
'name',
get_string('item_name', 'feedback'),
array('size'=>FEEDBACK_ITEM_NAME_TEXTBOX_SIZE, 'maxlength'=>255));
$mform->addElement('text',
'label',
get_string('item_label', 'feedback'),
array('size'=>FEEDBACK_ITEM_LABEL_TEXTBOX_SIZE, 'maxlength'=>255));
$mform->addElement('text',
'rangefrom',
get_string('numeric_range_from', 'feedback'),
array('size'=>10, 'maxlength'=>10));
$mform->setType('rangefrom', PARAM_RAW);
$mform->addElement('text',
'rangeto',
get_string('numeric_range_to', 'feedback'),
array('size'=>10, 'maxlength'=>10));
$mform->setType('rangeto', PARAM_RAW);
parent::definition();
$this->set_data($item);
}
public function get_data() {
if (!$item = parent::get_data()) {
return false;
}
$num1 = unformat_float($item->rangefrom, true);
if ($num1 === false || $num1 === null) {
$num1 = '-';
}
$num2 = unformat_float($item->rangeto, true);
if ($num2 === false || $num2 === null) {
$num2 = '-';
}
if ($num1 === '-' OR $num2 === '-') {
$item->presentation = $num1 . '|'. $num2;
return $item;
}
if ($num1 > $num2) {
$item->presentation = $num2 . '|'. $num1;
} else {
$item->presentation = $num1 . '|'. $num2;
}
return $item;
}
}
+221
View File
@@ -0,0 +1,221 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') OR die('not allowed');
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
class feedback_item_textarea extends feedback_item_base {
protected $type = "textarea";
public function build_editform($item, $feedback, $cm) {
global $DB, $CFG;
require_once('textarea_form.php');
//get the lastposition number of the feedback_items
$position = $item->position;
$lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->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;
$width_and_height = explode('|', $item->presentation);
if (isset($width_and_height[0]) AND $width_and_height[0] >= 5) {
$itemwidth = $width_and_height[0];
} else {
$itemwidth = 30;
}
if (isset($width_and_height[1])) {
$itemheight = $width_and_height[1];
} else {
$itemheight = 5;
}
$item->itemwidth = $itemwidth;
$item->itemheight = $itemheight;
//all items for dependitem
$feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
$commonparams = array('cmid'=>$cm->id,
'id'=>isset($item->id) ? $item->id : null,
'typ'=>$item->typ,
'items'=>$feedbackitems,
'feedback'=>$feedback->id);
//build the form
$customdata = array('item' => $item,
'common' => $commonparams,
'positionlist' => $positionlist,
'position' => $position);
$this->item_form = new feedback_textarea_form('edit_item.php', $customdata);
}
public function save_item() {
global $DB;
if (!$this->get_data()) {
return false;
}
$item = $this->item;
if (isset($item->clone_item) AND $item->clone_item) {
$item->id = ''; //to clone this item
$item->position++;
}
$item->hasvalue = $this->get_hasvalue();
if (!$item->id) {
$item->id = $DB->insert_record('feedback_item', $item);
} else {
$DB->update_record('feedback_item', $item);
}
return $DB->get_record('feedback_item', array('id'=>$item->id));
}
/**
* Helper function for collected data for exporting to excel
*
* @param stdClass $item the db-object from feedback_item
* @param int $groupid
* @param int $courseid
* @param bool $excel Indicate if being used for Excel
* @return stdClass
*/
protected function get_analysed($item, $groupid = false, $courseid = false, bool $excel = false) {
global $DB;
$analysed_val = new stdClass();
$analysed_val->data = array();
$analysed_val->name = $item->name;
$values = feedback_get_group_values($item, $groupid, $courseid);
if ($values) {
$data = array();
foreach ($values as $value) {
// Convert line breaks except for Excel.
$data[] = $excel ? $value->value : str_replace("\n", '<br />', $value->value);
}
$analysed_val->data = $data;
}
return $analysed_val;
}
public function get_printval($item, $value) {
if (!isset($value->value)) {
return '';
}
return $value->value;
}
public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
$values = feedback_get_group_values($item, $groupid, $courseid);
if ($values) {
echo "<table class=\"analysis itemtype_{$item->typ}\">";
echo '<tr><th class="text-left">';
echo $itemnr . ' ';
if (strval($item->label) !== '') {
echo '('. format_string($item->label).') ';
}
echo format_text($item->name, FORMAT_HTML, array('noclean' => true, 'para' => false));
echo '</th></tr>';
foreach ($values as $value) {
$class = strlen(trim($value->value)) ? '' : ' class="isempty"';
echo '<tr'.$class.'>';
echo '<td class="singlevalue">';
echo str_replace("\n", '<br />', $value->value);
echo '</td>';
echo '</tr>';
}
echo '</table>';
}
}
public function excelprint_item(&$worksheet, $row_offset,
$xls_formats, $item,
$groupid, $courseid = false) {
$analyseditem = $this->get_analysed($item, $groupid, $courseid, true);
$worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
$worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
$data = $analyseditem->data;
if (is_array($data)) {
if (isset($data[0])) {
$worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[0], ENT_QUOTES), $xls_formats->value_bold);
}
$row_offset++;
$sizeofdata = count($data);
for ($i = 1; $i < $sizeofdata; $i++) {
$worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[$i], ENT_QUOTES), $xls_formats->default);
$row_offset++;
}
}
$row_offset++;
return $row_offset;
}
/**
* Adds an input element to the complete form
*
* @param stdClass $item
* @param mod_feedback_complete_form $form
*/
public function complete_form_element($item, $form) {
$name = $this->get_display_name($item);
$inputname = $item->typ . '_' . $item->id;
list($cols, $rows) = explode ("|", $item->presentation);
$form->add_form_element($item,
['textarea', $inputname, $name, array('rows' => $rows, 'cols' => $cols)]);
$form->set_element_type($inputname, PARAM_NOTAGS);
}
public function create_value($data) {
return s($data);
}
/**
* Return the analysis data ready for external functions.
*
* @param stdClass $item the item (question) information
* @param int $groupid the group id to filter data (optional)
* @param int $courseid the course id (optional)
* @return array an array of data with non scalar types json encoded
* @since Moodle 3.3
*/
public function get_analysed_for_external($item, $groupid = false, $courseid = false) {
$externaldata = array();
$data = $this->get_analysed($item, $groupid, $courseid);
if (is_array($data->data)) {
return $data->data; // No need to json, scalar type.
}
return $externaldata;
}
}
@@ -0,0 +1,66 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_form_class.php');
class feedback_textarea_form extends feedback_item_form {
protected $type = "textarea";
public function definition() {
$item = $this->_customdata['item'];
$common = $this->_customdata['common'];
$positionlist = $this->_customdata['positionlist'];
$position = $this->_customdata['position'];
$mform =& $this->_form;
$mform->addElement('header', 'general', get_string($this->type, 'feedback'));
$mform->addElement('advcheckbox', 'required', get_string('required', 'feedback'), '' , null , array(0, 1));
$mform->addElement('text',
'name',
get_string('item_name', 'feedback'),
array('size'=>FEEDBACK_ITEM_NAME_TEXTBOX_SIZE, 'maxlength'=>255));
$mform->addElement('text',
'label',
get_string('item_label', 'feedback'),
array('size'=>FEEDBACK_ITEM_LABEL_TEXTBOX_SIZE, 'maxlength'=>255));
$mform->addElement('select',
'itemwidth',
get_string('textarea_width', 'feedback').'&nbsp;',
array_slice(range(0, 80), 5, 80, true));
$mform->addElement('select',
'itemheight',
get_string('textarea_height', 'feedback').'&nbsp;',
array_slice(range(0, 40), 5, 40, true));
parent::definition();
$this->set_data($item);
}
public function get_data() {
if (!$item = parent::get_data()) {
return false;
}
$item->presentation = $item->itemwidth . '|'. $item->itemheight;
return $item;
}
}
+218
View File
@@ -0,0 +1,218 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
defined('MOODLE_INTERNAL') OR die('not allowed');
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_class.php');
class feedback_item_textfield extends feedback_item_base {
protected $type = "textfield";
public function build_editform($item, $feedback, $cm) {
global $DB, $CFG;
require_once('textfield_form.php');
//get the lastposition number of the feedback_items
$position = $item->position;
$lastposition = $DB->count_records('feedback_item', array('feedback'=>$feedback->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;
$size_and_length = explode('|', $item->presentation);
if (isset($size_and_length[0]) AND $size_and_length[0] >= 5) {
$itemsize = $size_and_length[0];
} else {
$itemsize = 30;
}
$itemlength = isset($size_and_length[1]) ? $size_and_length[1] : 255;
$item->itemsize = $itemsize;
$item->itemmaxlength = $itemlength;
//all items for dependitem
$feedbackitems = feedback_get_depend_candidates_for_item($feedback, $item);
$commonparams = array('cmid' => $cm->id,
'id' => isset($item->id) ? $item->id : null,
'typ' => $item->typ,
'items' => $feedbackitems,
'feedback' => $feedback->id);
//build the form
$customdata = array('item' => $item,
'common' => $commonparams,
'positionlist' => $positionlist,
'position' => $position);
$this->item_form = new feedback_textfield_form('edit_item.php', $customdata);
}
public function save_item() {
global $DB;
if (!$this->get_data()) {
return false;
}
$item = $this->item;
if (isset($item->clone_item) AND $item->clone_item) {
$item->id = ''; //to clone this item
$item->position++;
}
$item->hasvalue = $this->get_hasvalue();
if (!$item->id) {
$item->id = $DB->insert_record('feedback_item', $item);
} else {
$DB->update_record('feedback_item', $item);
}
return $DB->get_record('feedback_item', array('id'=>$item->id));
}
/**
* Helper function for collected data for exporting to excel
*
* @param stdClass $item the db-object from feedback_item
* @param int $groupid
* @param int $courseid
* @return stdClass
*/
protected function get_analysed($item, $groupid = false, $courseid = false) {
$analysed_val = new stdClass();
$analysed_val->data = null;
$analysed_val->name = $item->name;
$values = feedback_get_group_values($item, $groupid, $courseid);
if ($values) {
$data = array();
foreach ($values as $value) {
$data[] = str_replace("\n", '<br />', $value->value);
}
$analysed_val->data = $data;
}
return $analysed_val;
}
public function get_printval($item, $value) {
if (!isset($value->value)) {
return '';
}
return $value->value;
}
public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false) {
$values = feedback_get_group_values($item, $groupid, $courseid);
if ($values) {
echo "<table class=\"analysis itemtype_{$item->typ}\">";
echo '<tr><th class="text-left">';
echo $itemnr . ' ';
if (strval($item->label) !== '') {
echo '('. format_string($item->label).') ';
}
echo $this->get_display_name($item);
echo '</th></tr>';
foreach ($values as $value) {
$class = strlen(trim($value->value)) ? '' : ' class="isempty"';
echo '<tr'.$class.'><td class="singlevalue">';
echo str_replace("\n", '<br />', $value->value);
echo '</td></tr>';
}
echo '</table>';
}
}
public function excelprint_item(&$worksheet, $row_offset,
$xls_formats, $item,
$groupid, $courseid = false) {
$analysed_item = $this->get_analysed($item, $groupid, $courseid);
$worksheet->write_string($row_offset, 0, $item->label, $xls_formats->head2);
$worksheet->write_string($row_offset, 1, $item->name, $xls_formats->head2);
$data = $analysed_item->data;
if (is_array($data)) {
$worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[0], ENT_QUOTES), $xls_formats->value_bold);
$row_offset++;
$sizeofdata = count($data);
for ($i = 1; $i < $sizeofdata; $i++) {
$worksheet->write_string($row_offset, 2, htmlspecialchars_decode($data[$i], ENT_QUOTES), $xls_formats->default);
$row_offset++;
}
}
$row_offset++;
return $row_offset;
}
/**
* Adds an input element to the complete form
*
* @param stdClass $item
* @param mod_feedback_complete_form $form
*/
public function complete_form_element($item, $form) {
$name = $this->get_display_name($item);
$inputname = $item->typ . '_' . $item->id;
list($size, $maxlength) = explode ("|", $item->presentation);
$form->add_form_element($item,
['text', $inputname, $name, ['maxlength' => $maxlength, 'size' => $size]]);
$form->set_element_type($inputname, PARAM_NOTAGS);
$form->add_element_rule($inputname, get_string('maximumchars', '', $maxlength), 'maxlength', $maxlength, 'client');
}
/**
* Converts the value from complete_form data to the string value that is stored in the db.
* @param mixed $value element from mod_feedback_complete_form::get_data() with the name $item->typ.'_'.$item->id
* @return string
*/
public function create_value($value) {
return s($value);
}
/**
* Return the analysis data ready for external functions.
*
* @param stdClass $item the item (question) information
* @param int $groupid the group id to filter data (optional)
* @param int $courseid the course id (optional)
* @return array an array of data with non scalar types json encoded
* @since Moodle 3.3
*/
public function get_analysed_for_external($item, $groupid = false, $courseid = false) {
$externaldata = array();
$data = $this->get_analysed($item, $groupid, $courseid);
if (is_array($data->data)) {
return $data->data; // No need to json, scalar type.
}
return $externaldata;
}
}
@@ -0,0 +1,67 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once($CFG->dirroot.'/mod/feedback/item/feedback_item_form_class.php');
class feedback_textfield_form extends feedback_item_form {
protected $type = "textfield";
public function definition() {
$item = $this->_customdata['item'];
$common = $this->_customdata['common'];
$positionlist = $this->_customdata['positionlist'];
$position = $this->_customdata['position'];
$mform =& $this->_form;
$mform->addElement('header', 'general', get_string($this->type, 'feedback'));
$mform->addElement('advcheckbox', 'required', get_string('required', 'feedback'), '' , null , array(0, 1));
$mform->addElement('text',
'name',
get_string('item_name', 'feedback'),
array('size'=>FEEDBACK_ITEM_NAME_TEXTBOX_SIZE, 'maxlength'=>255));
$mform->addElement('text',
'label',
get_string('item_label', 'feedback'),
array('size'=>FEEDBACK_ITEM_LABEL_TEXTBOX_SIZE, 'maxlength'=>255));
$mform->addElement('select',
'itemsize',
get_string('textfield_size', 'feedback').'&nbsp;',
array_slice(range(0, 255), 5, 255, true));
$mform->addElement('text',
'itemmaxlength',
get_string('textfield_maxlength', 'feedback'));
$mform->setType('itemmaxlength', PARAM_INT);
$mform->addRule('itemmaxlength', null, 'numeric', null, 'client');
parent::definition();
$this->set_data($item);
}
public function get_data() {
if (!$item = parent::get_data()) {
return false;
}
$item->presentation = $item->itemsize . '|'. $item->itemmaxlength;
return $item;
}
}