Merge branch 'MDL-40047-master' of git://github.com/ankitagarwal/moodle

This commit is contained in:
Eloy Lafuente (stronk7)
2013-10-01 23:59:05 +02:00
15 changed files with 1023 additions and 40 deletions
+1 -1
View File
@@ -29,7 +29,7 @@
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_chat_genarator_testcase extends advanced_testcase {
class mod_chat_generator_testcase extends advanced_testcase {
public function test_create_instance() {
global $DB;
@@ -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/>.
/**
* This file contains an event for when a feedback activity is viewed.
*
* @package mod_feedback
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_feedback\event;
defined('MOODLE_INTERNAL') || die();
/**
* Event for when a feedback activity is viewed.
*
* @package mod_feedback
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_module_viewed extends \core\event\content_viewed {
/**
* Init method.
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['level'] = self::LEVEL_PARTICIPATING;
$this->data['objecttable'] = 'feedback';
}
/**
* Returns non-localised description of what happened.
*
* @return string
*/
public function get_description() {
return 'User with id ' . $this->userid . ' viewed feedback activity ' . $this->get_url();
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventcoursemoduleviewed', 'mod_feedback');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/feedback/view.php', array('id' => $this->other['cmid']));
}
/**
* Define whether a user can view the event or not. Make sure no one except admin can see details of an anonymous response.
*
* @param int|\stdClass $userorid ID of the user.
* @return bool True if the user can view the event, false otherwise.
*/
public function can_view($userorid = null) {
global $USER;
if (empty($userorid)) {
$userorid = $USER;
}
if ($this->other['anonymous'] == FEEDBACK_ANONYMOUS_YES) {
return is_siteadmin($userorid);
} else {
return has_capability('mod/feedback:viewreports', $this->context, $userorid);
}
}
/**
* Replace add_to_log() statement.Do this only for the case when anonymous mode is off,
* since this is what was happening before.
*
* @return array of parameters to be passed to legacy add_to_log() function.
*/
protected function get_legacy_logdata() {
if ($this->other['anonymous'] == FEEDBACK_ANONYMOUS_YES) {
return null;
} else {
return array($this->courseid, 'feedback', 'view', 'view.php?id=' . $this->other['cmid'], $this->objectid,
$this->other['cmid']);
}
}
/**
* Custom validations.
*
* @throws \coding_exception in case of any problems.
*/
protected function validate_data() {
if (!isset($this->other['anonymous'])) {
throw new \coding_exception("Field other['anonymous'] cannot be empty");
}
if (!isset($this->other['cmid'])) {
throw new \coding_exception("Field other['cmid'] cannot be empty");
}
// Call parent validations.
parent::validate_data();
}
}
@@ -0,0 +1,74 @@
<?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/>.
/**
* mod_feedback instances list viewed event.
*
* @package mod_feedback
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_feedback\event;
defined('MOODLE_INTERNAL') || die();
/**
* mod_feedback instances list viewed event class.
*
* @package mod_feedback
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class instances_list_viewed extends \core\event\course_module_instances_list_viewed {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "User $this->userid viewed the list of feedback activities in the course $this->courseid.";
}
/**
* Return the legacy event log data.
*
* @return array
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'feedback', 'view all', 'index.php?id=' . $this->courseid, '');
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventinstanceslistviewed', 'mod_feedback');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/feedback/index.php', array('id' => $this->courseid));
}
}
@@ -0,0 +1,112 @@
<?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/>.
/**
* Event to be triggered when a feedback response is deleted.
*
* @package mod_feedback
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
namespace mod_feedback\event;
defined('MOODLE_INTERNAL') || die();
/**
* Class response_deleted
*
* Class for event to be triggered when a feedback response is deleted.
*
* @package mod_feedback
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class response_deleted extends \core\event\base {
/**
* Set basic properties for the event.
*/
protected function init() {
$this->data['objecttable'] = 'feedback_completed';
$this->data['crud'] = 'd';
$this->data['level'] = self::LEVEL_PARTICIPATING;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventresponsedeleted', 'mod_feedback');
}
/**
* Returns non-localised event description with id's for admin use only.
*
* @return string
*/
public function get_description() {
return 'The feedback from user ' . $this->other['relateduserid']. ' was deleted by' . $this->userid;
}
/**
* Replace add_to_log() statement.
*
* @return array of parameters to be passed to legacy add_to_log() function.
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'feedback', 'delete', 'view.php?id=' . $this->other['cmid'], $this->other['instanceid'],
$this->other['instanceid']);
}
/**
* Define whether a user can view the event or not. Make sure no one except admin can see details of an anonymous response.
*
* @param int|\stdClass $userorid ID of the user.
* @return bool True if the user can view the event, false otherwise.
*/
public function can_view($userorid = null) {
global $USER;
if (empty($userorid)) {
$userorid = $USER;
}
if ($this->other['anonymous'] == FEEDBACK_ANONYMOUS_YES) {
return is_siteadmin($userorid);
} else {
return has_capability('mod/feedback:viewreports', $this->context, $userorid);
}
}
/**
* Custom validations
*
* @throws \coding_exception in case of any problems.
*/
protected function validate_data() {
if (!isset($this->other['anonymous'])) {
throw new \coding_exception("Field other['anonymous'] cannot be empty");
}
if (!isset($this->other['cmid'])) {
throw new \coding_exception("Field other['cmid'] cannot be empty");
}
if (!isset($this->other['instanceid'])) {
throw new \coding_exception("Field other['instanceid'] cannot be empty");
}
}
}
@@ -0,0 +1,134 @@
<?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/>.
/**
* Event to be triggered when a feedback response is submitted.
*
* @package mod_feedback
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
namespace mod_feedback\event;
defined('MOODLE_INTERNAL') || die();
/**
* Class response_submitted
*
* Class for event to be triggered when a feedback response is submitted.
*
* @package mod_feedback
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class response_submitted extends \core\event\base {
/**
* Set basic properties for the event.
*/
protected function init() {
global $CFG;
require_once($CFG->dirroot.'/mod/feedback/lib.php');
$this->data['objecttable'] = 'feedback_completed';
$this->data['crud'] = 'c';
$this->data['level'] = self::LEVEL_PARTICIPATING;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventresponsesubmitted', 'mod_feedback');
}
/**
* Returns non-localised event description with id's for admin use only.
*
* @return string
*/
public function get_description() {
return 'The user ' . $this->other['relateduserid']. ' submited a feedback';
}
/**
* Returns relevant URL based on the anonymous mode of the response.
* @return \moodle_url
*/
public function get_url() {
if ($this->other['anonymous'] == FEEDBACK_ANONYMOUS_YES) {
return new \moodle_url('/mod/feedback/show_entries.php' , array('id' => $this->other['cmid'],
'do_show' => 'showoneentry' , 'userid' => $this->relateduserid));
} else {
return new \moodle_url('/mod/feedback/show_entries_anonym.php', array('id' => $this->other['cmid'],
'do_show' => 'showoneentry', 'showall', 'showcompleted' => $this->objectid));
}
}
/**
* Replace add_to_log() statement. Do this only for the case when anonymous mode is off,
* since this is what was happening before.
*
* @return array of parameters to be passed to legacy add_to_log() function.
*/
protected function get_legacy_logdata() {
if ($this->other['anonymous'] == FEEDBACK_ANONYMOUS_YES) {
return null;
} else {
return array($this->courseid, 'feedback', 'submit', 'view.php?id=' . $this->other['cmid'],
$this->other['instanceid'], $this->other['cmid'], $this->relateduserid);
}
}
/**
* Define whether a user can view the event or not. Make sure no one except admin can see details of an anonymous response.
*
* @param int|\stdClass $userorid ID of the user.
* @return bool True if the user can view the event, false otherwise.
*/
public function can_view($userorid = null) {
global $USER;
if (empty($userorid)) {
$userorid = $USER;
}
if ($this->other['anonymous'] == FEEDBACK_ANONYMOUS_YES) {
return is_siteadmin($userorid);
} else {
return has_capability('mod/feedback:viewreports', $this->context, $userorid);
}
}
/**
* Custom validations.
*
* @throws \coding_exception in case of any problems.
*/
protected function validate_data() {
if (!isset($this->other['anonymous'])) {
throw new \coding_exception("Field other['anonymous'] cannot be empty");
}
if (!isset($this->other['cmid'])) {
throw new \coding_exception("Field other['cmid'] cannot be empty");
}
if (!isset($this->other['instanceid'])) {
throw new \coding_exception("Field other['instanceid'] cannot be empty");
}
}
}
+2 -19
View File
@@ -204,21 +204,12 @@ if ($feedback_can_submit) {
if (!isset($SESSION->feedback->is_started) OR !$SESSION->feedback->is_started == true) {
print_error('error', '', $CFG->wwwroot.'/course/view.php?id='.$course->id);
}
//checken, ob alle required items einen wert haben
// Check if all required items have a value.
if (feedback_check_values($startitempos, $lastitempos)) {
$userid = $USER->id; //arb
if ($completedid = feedback_save_values($USER->id, true)) {
if ($userid > 0) {
add_to_log($course->id,
'feedback',
'startcomplete',
'view.php?id='.$cm->id,
$feedback->id,
$cm->id,
$userid);
}
if (!$gonextpage AND !$gopreviouspage) {
$preservevalues = false;//es kann gespeichert werden
$preservevalues = false;// It can be stored.
}
} else {
@@ -265,14 +256,6 @@ if ($feedback_can_submit) {
if ($new_completed_id) {
$savereturn = 'saved';
if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
add_to_log($course->id,
'feedback',
'submit',
'view.php?id='.$cm->id,
$feedback->id,
$cm->id,
$userid);
feedback_send_email($cm, $feedback, $course, $userid);
} else {
feedback_send_email_anonym($cm, $feedback, $course, $userid);
-6
View File
@@ -208,12 +208,6 @@ if ($feedback_can_submit) {
if (feedback_check_values($startitempos, $lastitempos)) {
$userid = $USER->id; //arb
if ($completedid = feedback_save_guest_values(sesskey())) {
add_to_log($course->id,
'feedback',
'startcomplete',
'view.php?id='.$cm->id,
$feedback->id);
//now it can be saved
if (!$gonextpage AND !$gopreviouspage) {
$preservevalues = false;
-7
View File
@@ -76,13 +76,6 @@ if ($mform->is_cancelled()) {
if (isset($formdata->confirmdelete) AND $formdata->confirmdelete == 1) {
if ($completed = $DB->get_record('feedback_completed', array('id'=>$completedid))) {
feedback_delete_completed($completedid);
add_to_log($course->id,
'feedback',
'delete',
'view.php?id='.$cm->id,
$feedback->id,
$cm->id);
if ($return == 'entriesanonym') {
redirect('show_entries_anonym.php?id='.$id);
} else {
+3 -2
View File
@@ -40,8 +40,9 @@ $context = context_course::instance($course->id);
require_login($course);
$PAGE->set_pagelayout('incourse');
add_to_log($course->id, 'feedback', 'view all', $url->out(false), $course->id);
// Trigger instances list viewed event.
$event = \mod_feedback\event\instances_list_viewed::create(array('context' => $context));
$event->trigger();
/// Print the page header
$strfeedbacks = get_string("modulenameplural", "feedback");
+4
View File
@@ -102,6 +102,10 @@ You can view it <a href="{$a->url}">here</a>.';
$string['entries_saved'] = 'Your answers have been saved. Thank you.';
$string['export_questions'] = 'Export questions';
$string['export_to_excel'] = 'Export to Excel';
$string['eventcoursemoduleviewed'] = 'Course module viewed';
$string['eventresponsedeleted'] = 'Response deleted';
$string['eventresponsesubmitted'] = 'Response submitted';
$string['eventinstanceslistviewed'] = 'Instances list viewed';
$string['feedback:addinstance'] = 'Add a new feedback';
$string['feedbackclose'] = 'Allow answers to';
$string['feedback:complete'] = 'Complete a feedback';
+38 -2
View File
@@ -27,6 +27,8 @@
require_once($CFG->libdir.'/eventslib.php');
/** Include calendar/lib.php */
require_once($CFG->dirroot.'/calendar/lib.php');
// Include forms lib.
require_once($CFG->libdir.'/formslib.php');
define('FEEDBACK_ANONYMOUS_YES', 1);
define('FEEDBACK_ANONYMOUS_NO', 2);
@@ -1906,6 +1908,23 @@ function feedback_save_tmp_values($feedbackcompletedtmp, $feedbackcompleted, $us
//drop all the tmpvalues
$DB->delete_records('feedback_valuetmp', array('completed'=>$tmpcplid));
$DB->delete_records('feedback_completedtmp', array('id'=>$tmpcplid));
// Trigger event for the delete action we performed.
$cm = get_coursemodule_from_instance('feedback', $feedbackcompleted->feedback);
$event = \mod_feedback\event\response_submitted::create(array(
'relateduserid' => $userid,
'objectid' => $feedbackcompleted->id,
'context' => context_module::instance($cm->id),
'other' => array(
'cmid' => $cm->id,
'instanceid' => $feedbackcompleted->feedback,
'anonymous' => $feedbackcompleted->anonymous_response
)
));
$event->add_record_snapshot('feedback_completed', $feedbackcompleted);
$event->trigger();
return $feedbackcompleted->id;
}
@@ -2660,8 +2679,25 @@ function feedback_delete_completed($completedid) {
if ($completion->is_enabled($cm) && $feedback->completionsubmit) {
$completion->update_state($cm, COMPLETION_INCOMPLETE, $completed->userid);
}
//last we delete the completed-record
return $DB->delete_records('feedback_completed', array('id'=>$completed->id));
// Last we delete the completed-record.
$return = $DB->delete_records('feedback_completed', array('id'=>$completed->id));
// Trigger event for the delete action we performed.
$event = \mod_feedback\event\response_deleted::create(array(
'relateduserid' => $completed->userid,
'objectid' => $completedid,
'courseid' => $course->id,
'context' => context_module::instance($cm->id),
'other' => array('cmid' => $cm->id, 'instanceid' => $feedback->id, 'anonymous' => $completed->anonymous_response)
));
$event->add_record_snapshot('feedback_completed', $completed);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('feedback', $feedback);
$event->trigger();
return $return;
}
////////////////////////////////////////////////
+345
View File
@@ -0,0 +1,345 @@
<?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/>.
/**
* Tests for feedback events.
*
* @package mod_feedback
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
global $CFG;
/**
* Class mod_feedback_events_testcase
*
* Class for tests related to feedback events.
*
* @package mod_feedback
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
class mod_feedback_events_testcase extends advanced_testcase {
/** @var stdClass A user who likes to interact with feedback activity. */
private $eventuser;
/** @var stdClass A course used to hold feedback activities for testing. */
private $eventcourse;
/** @var stdClass A feedback activity used for feedback event testing. */
private $eventfeedback;
/** @var stdClass course module object . */
private $eventcm;
/** @var stdClass A feedback item. */
private $eventfeedbackitem;
/** @var stdClass A feedback activity response submitted by user. */
private $eventfeedbackcompleted;
/** @var stdClass value associated with $eventfeedbackitem . */
private $eventfeedbackvalue;
public function setUp() {
global $DB;
$this->setAdminUser();
$gen = $this->getDataGenerator();
$this->eventuser = $gen->create_user(); // Create a user.
$course = $gen->create_course(); // Create a course.
// Assign manager role, so user can see reports.
role_assign(1, $this->eventuser->id, context_course::instance($course->id));
// Add a feedback activity to the created course.
$record = new stdClass();
$record->course = $course->id;
$feedback = $gen->create_module('feedback', $record);
$this->eventfeedback = $DB->get_record('feedback', array('id' => $feedback->id), '*', MUST_EXIST); // Get exact copy.
$this->eventcm = get_coursemodule_from_instance('feedback', $this->eventfeedback->id, false, MUST_EXIST);
// Create a feedback item.
$item = new stdClass();
$item->feedback = $this->eventfeedback->id;
$item->type = 'numeric';
$item->presentation = '0|0';
$itemid = $DB->insert_record('feedback_item', $item);
$this->eventfeedbackitem = $DB->get_record('feedback_item', array('id' => $itemid), '*', MUST_EXIST);
// Create a response from a user.
$response = new stdClass();
$response->feedback = $this->eventfeedback->id;
$response->userid = $this->eventuser->id;
$response->anonymous_response = FEEDBACK_ANONYMOUS_YES;
$completedid = $DB->insert_record('feedback_completed', $response);
$this->eventfeedbackcompleted = $DB->get_record('feedback_completed', array('id' => $completedid), '*', MUST_EXIST);
$value = new stdClass();
$value->course_id = $course->id;
$value->item = $this->eventfeedbackitem->id;
$value->completed = $this->eventfeedbackcompleted->id;
$value->value = 25; // User response value.
$valueid = $DB->insert_record('feedback_value', $value);
$this->eventfeedbackvalue = $DB->get_record('feedback_value', array('id' => $valueid), '*', MUST_EXIST);
// Do this in the end to get correct sortorder and cacherev values.
$this->eventcourse = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
}
/**
* Tests for event response_deleted.
*/
public function test_response_deleted_event() {
global $USER, $DB;
$this->resetAfterTest();
// Create and delete a module.
$sink = $this->redirectEvents();
feedback_delete_completed($this->eventfeedbackcompleted->id);
$events = $sink->get_events();
$event = array_pop($events); // Delete feedback event.
$sink->close();
// Validate event data.
$this->assertInstanceOf('\mod_feedback\event\response_deleted', $event);
$this->assertEquals($this->eventfeedbackcompleted->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($this->eventuser->id, $event->relateduserid);
$this->assertEquals('feedback_completed', $event->objecttable);
$this->assertEquals(null, $event->get_url());
$this->assertEquals($this->eventfeedbackcompleted, $event->get_record_snapshot('feedback_completed', $event->objectid));
$this->assertEquals($this->eventcourse, $event->get_record_snapshot('course', $event->courseid));
$this->assertEquals($this->eventfeedback, $event->get_record_snapshot('feedback', $event->other['instanceid']));
// Test legacy data.
$arr = array($this->eventcourse->id, 'feedback', 'delete', 'view.php?id=' . $this->eventcm->id, $this->eventfeedback->id,
$this->eventfeedback->id);
$this->assertEventLegacyLogData($arr, $event);
// Test can_view() .
$this->setUser($this->eventuser);
$this->assertFalse($event->can_view());
$this->setAdminUser();
$this->assertTrue($event->can_view());
// Create a response, with anonymous set to no and test can_view().
$response = new stdClass();
$response->feedback = $this->eventcm->instance;
$response->userid = $this->eventuser->id;
$response->anonymous_response = FEEDBACK_ANONYMOUS_NO;
$completedid = $DB->insert_record('feedback_completed', $response);
$DB->get_record('feedback_completed', array('id' => $completedid), '*', MUST_EXIST);
$value = new stdClass();
$value->course_id = $this->eventcourse->id;
$value->item = $this->eventfeedbackitem->id;
$value->completed = $completedid;
$value->value = 25; // User response value.
$DB->insert_record('feedback_valuetmp', $value);
// Save the feedback.
$sink = $this->redirectEvents();
feedback_delete_completed($completedid);
$events = $sink->get_events();
$event = array_pop($events); // Response submitted feedback event.
$sink->close();
// Test can_view() .
$this->setUser($this->eventuser);
$this->assertTrue($event->can_view());
$this->setAdminUser();
$this->assertTrue($event->can_view());
}
/**
* Tests for event validations related to feedback response deletion.
*/
public function test_response_deleted_event_exceptions() {
$this->resetAfterTest();
$context = context_module::instance($this->eventcm->id);
// Test not setting instanceid.
try {
\mod_feedback\event\response_deleted::create(array(
'context' => $context,
'objectid' => $this->eventfeedbackcompleted->id,
'other' => array('cmid' => $this->eventcm->id, 'anonymous' => 2)
));
$this->fail("Event validation should not allow \\mod_feedback\\event\\response_deleted to be triggered without
other['instanceid']");
} catch (coding_exception $e) {
$this->assertContains("Field other['instanceid'] cannot be empty", $e->getMessage());
}
// Test not setting cmid.
try {
\mod_feedback\event\response_deleted::create(array(
'context' => $context,
'objectid' => $this->eventfeedbackcompleted->id,
'other' => array('instanceid' => $this->eventfeedback->id, 'anonymous' => 2)
));
$this->fail("Event validation should not allow \\mod_feedback\\event\\response_deleted to be triggered without
other['cmid']");
} catch (coding_exception $e) {
$this->assertContains("Field other['cmid'] cannot be empty", $e->getMessage());
}
// Test not setting anonymous.
try {
\mod_feedback\event\response_submitted::create(array(
'context' => $context,
'objectid' => $this->eventfeedbackcompleted->id,
'other' => array('cmid' => $this->eventcm->id, 'instanceid' => $this->eventfeedback->id)
));
$this->fail("Event validation should not allow \\mod_feedback\\event\\response_deleted to be triggered without
other['anonymous']");
} catch (coding_exception $e) {
$this->assertContains("Field other['anonymous'] cannot be empty", $e->getMessage());
}
}
/**
* Tests for event response_submitted.
*/
public function test_response_submitted_event() {
global $USER, $DB;
$this->resetAfterTest();
$this->setUser($this->eventuser);
// Create a temporary response, with anonymous set to yes.
$response = new stdClass();
$response->feedback = $this->eventcm->instance;
$response->userid = $this->eventuser->id;
$response->anonymous_response = FEEDBACK_ANONYMOUS_YES;
$completedid = $DB->insert_record('feedback_completedtmp', $response);
$completed = $DB->get_record('feedback_completedtmp', array('id' => $completedid), '*', MUST_EXIST);
$value = new stdClass();
$value->course_id = $this->eventcourse->id;
$value->item = $this->eventfeedbackitem->id;
$value->completed = $completedid;
$value->value = 25; // User response value.
$DB->insert_record('feedback_valuetmp', $value);
// Save the feedback.
$sink = $this->redirectEvents();
$id = feedback_save_tmp_values($completed, false, $USER->id);
$events = $sink->get_events();
$event = array_pop($events); // Response submitted feedback event.
$sink->close();
// Validate event data. Feedback is anonymous.
$this->assertInstanceOf('\mod_feedback\event\response_submitted', $event);
$this->assertEquals($id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals($USER->id, $event->relateduserid);
$this->assertEquals('feedback_completed', $event->objecttable);
$this->assertEquals($this->eventcm->id, $event->other['cmid']);
$this->assertEquals($this->eventcm->instance, $event->other['instanceid']);
$this->assertEquals(FEEDBACK_ANONYMOUS_YES, $event->other['anonymous']);
$this->setUser($this->eventuser);
$this->assertFalse($event->can_view());
$this->setAdminUser();
$this->assertTrue($event->can_view());
// Test legacy data.
$this->assertEventLegacyLogData(null, $event);
// Create a temporary response, with anonymous set to no.
$response = new stdClass();
$response->feedback = $this->eventcm->instance;
$response->userid = $this->eventuser->id;
$response->anonymous_response = FEEDBACK_ANONYMOUS_NO;
$completedid = $DB->insert_record('feedback_completedtmp', $response);
$completed = $DB->get_record('feedback_completedtmp', array('id' => $completedid), '*', MUST_EXIST);
$value = new stdClass();
$value->course_id = $this->eventcourse->id;
$value->item = $this->eventfeedbackitem->id;
$value->completed = $completedid;
$value->value = 25; // User response value.
$DB->insert_record('feedback_valuetmp', $value);
// Save the feedback.
$sink = $this->redirectEvents();
feedback_save_tmp_values($completed, false, $USER->id);
$events = $sink->get_events();
$event = array_pop($events); // Response submitted feedback event.
$sink->close();
// Test legacy data.
$arr = array($this->eventcourse->id, 'feedback', 'submit', 'view.php?id=' . $this->eventcm->id, $this->eventfeedback->id,
$this->eventfeedback->id, $USER->id);
$this->assertEventLegacyLogData($arr, $event);
// Test can_view().
$this->assertTrue($event->can_view());
$this->setAdminUser();
$this->assertTrue($event->can_view());
}
/**
* Tests for event validations related to feedback response submittion.
*/
public function test_response_submitted_event_exceptions() {
$this->resetAfterTest();
$context = context_module::instance($this->eventcm->id);
// Test not setting instanceid.
try {
\mod_feedback\event\response_submitted::create(array(
'context' => $context,
'objectid' => $this->eventfeedbackcompleted->id,
'other' => array('cmid' => $this->eventcm->id, 'anonymous' => 2)
));
$this->fail("Event validation should not allow \\mod_feedback\\event\\response_deleted to be triggered without
other['instanceid']");
} catch (coding_exception $e) {
$this->assertContains("Field other['instanceid'] cannot be empty", $e->getMessage());
}
// Test not setting cmid.
try {
\mod_feedback\event\response_submitted::create(array(
'context' => $context,
'objectid' => $this->eventfeedbackcompleted->id,
'other' => array('instanceid' => $this->eventfeedback->id, 'anonymous' => 2)
));
$this->fail("Event validation should not allow \\mod_feedback\\event\\response_deleted to be triggered without
other['cmid']");
} catch (coding_exception $e) {
$this->assertContains("Field other['cmid'] cannot be empty", $e->getMessage());
}
// Test not setting anonymous.
try {
\mod_feedback\event\response_submitted::create(array(
'context' => $context,
'objectid' => $this->eventfeedbackcompleted->id,
'other' => array('cmid' => $this->eventcm->id, 'instanceid' => $this->eventfeedback->id)
));
$this->fail("Event validation should not allow \\mod_feedback\\event\\response_deleted to be triggered without
other['anonymous']");
} catch (coding_exception $e) {
$this->assertContains("Field other['anonymous'] cannot be empty", $e->getMessage());
}
}
}
+114
View File
@@ -0,0 +1,114 @@
<?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/>.
/**
* mod_feedback data generator.
*
* @package mod_feedback
* @category test
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* mod_feedback data generator class.
*
* @package mod_feedback
* @category test
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_feedback_generator extends testing_module_generator {
/**
* Create new feedback module instance
*
* @param array|stdClass $record
* @param array $options
* @throws coding_exception
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once("$CFG->dirroot/mod/feedback/lib.php");
$this->instancecount++;
$i = $this->instancecount;
$record = (object)(array)$record;
$options = (array)$options;
if (empty($record->course)) {
throw new coding_exception('Module generator requires $record->course.');
}
if (!isset($record->name)) {
$record->name = get_string('pluginname', 'feedback') . ' ' . $i;
}
if (!isset($record->intro)) {
$record->intro = 'Test feedback ' . $i;
}
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
if (!isset($record->anonymous)) {
$record->anonymous = FEEDBACK_ANONYMOUS_YES;
}
if (!isset($record->email_notification)) {
$record->email_notification = 0;
}
if (!isset($record->multiple_submit)) {
$record->multiple_submit = 0;
}
if (!isset($record->autonumbering)) {
$record->autonumbering = 0;
}
if (!isset($record->site_after_submit)) {
$record->site_after_submit = '';
}
if (!isset($record->page_after_submit)) {
$record->page_after_submit = 'This is page after submit';
}
if (!isset($record->page_after_submitformat)) {
$record->page_after_submitformat = FORMAT_MOODLE;
}
if (!isset($record->publish_stats)) {
$record->publish_stats = 0;
}
if (!isset($record->timeopen)) {
$record->timeopen = 0;
}
if (!isset($record->timeclose)) {
$record->timeclose = 0;
}
if (!isset($record->timemodified)) {
$record->timemodified = time();
}
if (!isset($record->completionsubmit)) {
$record->completionsubmit = 0;
}
// Hack to bypass draft processing of feedback_add_instance.
$record->page_after_submit_editor['itemid'] = false;
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = feedback_add_instance($record);
return $this->post_add_instance($id, $record->coursemodule);
}
}
+57
View File
@@ -0,0 +1,57 @@
<?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/>.
/**
* Genarator tests.
*
* @package mod_feedback
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
global $CFG;
/**
* Genarator tests class.
*
* @package mod_feedback
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_feedback_generator_testcase extends advanced_testcase {
public function test_create_instance() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$this->assertFalse($DB->record_exists('feedback', array('course' => $course->id)));
$feedback = $this->getDataGenerator()->create_module('feedback', array('course' => $course->id));
$this->assertEquals(1, $DB->count_records('feedback', array('course' => $course->id)));
$this->assertTrue($DB->record_exists('feedback', array('course' => $course->id)));
$this->assertTrue($DB->record_exists('feedback', array('id' => $feedback->id)));
$params = array('course' => $course->id, 'name' => 'One more feedback');
$feedback = $this->getDataGenerator()->create_module('feedback', $params);
$this->assertEquals(2, $DB->count_records('feedback', array('course' => $course->id)));
$this->assertEquals('One more feedback', $DB->get_field_select('feedback', 'name', 'id = :id',
array('id' => $feedback->id)));
}
}
+15 -3
View File
@@ -96,9 +96,21 @@ if ($courseid AND $courseid != SITEID) {
}
}
if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
add_to_log($course->id, 'feedback', 'view', 'view.php?id='.$cm->id, $feedback->id, $cm->id);
}
// Trigger module viewed event.
$event = \mod_feedback\event\course_module_viewed::create(array(
'objectid' => $feedback->id,
'context' => $context,
'other' => array(
'cmid' => $cm->id,
'instanceid' => $feedback->id,
'anonymous' => $feedback->anonymous,
'content' => 'feedbackmoduleview'
)
));
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('feedback', $feedback);
$event->trigger();
/// Print the page header
$strfeedbacks = get_string("modulenameplural", "feedback");