Merge branch 'wip-MDL-40049-master-i' of git://github.com/abgreeve/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2013-10-01 23:42:00 +02:00
commit d1ed736f1d
13 changed files with 878 additions and 6 deletions

View File

@ -0,0 +1,103 @@
<?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_choice answer submitted event.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\event;
defined('MOODLE_INTERNAL') || die();
/**
* mod_choice answer submitted event class.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class answer_submitted extends \core\event\base {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "User {$this->userid} has made their choice {$this->objectid} in {$this->other['choiceid']}.";
}
/**
* Return legacy data for add_to_log().
*
* @return array
*/
protected function get_legacy_logdata() {
$legacylogdata = array($this->courseid,
'choice',
'choose',
'view.php?id=' . $this->context->instanceid,
$this->other['choiceid'],
$this->context->instanceid);
return $legacylogdata;
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_answer_created', 'mod_choice');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/choice/view.php', array('id' => $this->context->instanceid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'c';
$this->data['level'] = self::LEVEL_PARTICIPATING;
$this->data['objecttable'] = 'choice_answers';
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
if (!isset($this->other['choiceid'])) {
throw new \coding_exception('choiceid must be set in $other.');
}
}
}

View File

@ -0,0 +1,103 @@
<?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_choice answer updated event.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\event;
defined('MOODLE_INTERNAL') || die();
/**
* mod_choice answer updated event class.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class answer_updated extends \core\event\base {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "User {$this->userid} has updated their choice {$this->objectid} in {$this->other['choiceid']}.";
}
/**
* Return legacy data for add_to_log().
*
* @return array
*/
protected function get_legacy_logdata() {
$legacylogdata = array($this->courseid,
'choice',
'choose again',
'view.php?id=' . $this->context->instanceid,
$this->other['choiceid'],
$this->context->instanceid);
return $legacylogdata;
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_answer_updated', 'mod_choice');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/choice/view.php', array('id' => $this->context->instanceid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'u';
$this->data['level'] = self::LEVEL_PARTICIPATING;
$this->data['objecttable'] = 'choice_answers';
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
if (!isset($this->other['choiceid'])) {
throw new \coding_exception('choiceid must be set in $other.');
}
}
}

View File

@ -0,0 +1,73 @@
<?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 choice activity is viewed.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\event;
defined('MOODLE_INTERNAL') || die();
/**
* Event for when a choice activity is viewed.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve
* @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'] = 'choice';
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_choice_viewed', 'choice');
}
/**
* Returns relevant URL.
* @return \moodle_url
*/
public function get_url() {
$url = '/mod/choice/view.php';
return new \moodle_url($url, array('id' => $this->context->instanceid));
}
/**
* replace add_to_log() statement.
*
* @return array of parameters to be passed to legacy add_to_log() function.
*/
protected function get_legacy_logdata() {
$url = new \moodle_url('view.php', array('id' => $this->context->instanceid));
return array($this->courseid, 'choice', 'view', $url->out(), $this->objectid, $this->context->instanceid);
}
}

View File

@ -0,0 +1,72 @@
<?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/>.
/**
* Course module instances list_viewed event.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\event;
defined('MOODLE_INTERNAL') || die();
/**
* Course module instances list viewed event class for mod_choice.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve
* @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 choice activities in the course $this->courseid.";
}
/**
* Return the legacy event log data.
*
* @return array
*/
protected function get_legacy_logdata() {
return array($this->courseid, 'choice', 'view all', 'index.php?id=' . $this->courseid, '');
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_instances_list_viewed', 'mod_choice');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/choice/index.php', array('id' => $this->courseid));
}
}

View File

@ -0,0 +1,73 @@
<?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 choice activity report is viewed.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\event;
defined('MOODLE_INTERNAL') || die();
/**
* Event for when a choice activity report is viewed.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_viewed extends \core\event\content_viewed {
/**
* Init method.
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['level'] = self::LEVEL_TEACHING;
$this->data['objecttable'] = 'choice';
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('event_report_viewed', 'choice');
}
/**
* Returns relevant URL.
* @return \moodle_url
*/
public function get_url() {
$url = '/mod/choice/report.php';
return new \moodle_url($url, array('id' => $this->context->instanceid));
}
/**
* replace add_to_log() statement.
*
* @return array of parameters to be passed to legacy add_to_log() function.
*/
protected function get_legacy_logdata() {
$url = new \moodle_url('report.php', array('id' => $this->context->instanceid));
return array($this->courseid, 'choice', 'report', $url->out(), $this->objectid, $this->context->instanceid);
}
}

View File

@ -14,7 +14,9 @@
require_course_login($course);
$PAGE->set_pagelayout('incourse');
add_to_log($course->id, "choice", "view all", "index.php?id=$course->id", "");
$eventdata = array('context' => context_course::instance($id));
$event = \mod_choice\event\instances_list_viewed::create($eventdata);
$event->trigger();
$strchoice = get_string("modulename", "choice");
$strchoices = get_string("modulenameplural", "choice");

View File

@ -30,6 +30,11 @@ $string['completionsubmit'] = 'Show as complete when user makes a choice';
$string['displayhorizontal'] = 'Display horizontally';
$string['displaymode'] = 'Display mode for the options';
$string['displayvertical'] = 'Display vertically';
$string['event_answer_created'] = 'Choice made';
$string['event_answer_updated'] = 'Choice updated';
$string['event_choice_viewed'] = 'Choice viewed';
$string['event_report_viewed'] = 'Choice report viewed';
$string['event_instances_list_viewed'] = 'Instances list viewed';
$string['expired'] = 'Sorry, this activity closed on {$a} and is no longer available';
$string['atleastoneoption'] = 'You need to provide at least one possible answer.';
$string['full'] = '(Full)';

View File

@ -293,21 +293,49 @@ WHERE
$newanswer->optionid = $formanswer;
$newanswer->timemodified = time();
$DB->update_record("choice_answers", $newanswer);
add_to_log($course->id, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id);
$eventdata = array();
$eventdata['context'] = $context;
$eventdata['objectid'] = $newanswer->id;
$eventdata['userid'] = $userid;
$eventdata['courseid'] = $course->id;
$eventdata['other'] = array();
$eventdata['other']['choiceid'] = $choice->id;
$eventdata['other']['optionid'] = $formanswer;
$event = \mod_choice\event\answer_updated::create($eventdata);
$event->add_record_snapshot('choice_answers', $newanswer);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('course_modules', $cm);
$event->trigger();
} else {
$newanswer = new stdClass();
$newanswer->choiceid = $choice->id;
$newanswer->userid = $userid;
$newanswer->optionid = $formanswer;
$newanswer->timemodified = time();
$DB->insert_record("choice_answers", $newanswer);
$newanswer->id = $DB->insert_record("choice_answers", $newanswer);
// Update completion state
$completion = new completion_info($course);
if ($completion->is_enabled($cm) && $choice->completionsubmit) {
$completion->update_state($cm, COMPLETION_COMPLETE);
}
add_to_log($course->id, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id);
$eventdata = array();
$eventdata['context'] = $context;
$eventdata['objectid'] = $newanswer->id;
$eventdata['userid'] = $userid;
$eventdata['courseid'] = $course->id;
$eventdata['other'] = array();
$eventdata['other']['choiceid'] = $choice->id;
$eventdata['other']['optionid'] = $formanswer;
$event = \mod_choice\event\answer_submitted::create($eventdata);
$event->add_record_snapshot('choice_answers', $newanswer);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('course_modules', $cm);
$event->trigger();
}
} else {
if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error

View File

@ -43,7 +43,15 @@
$strchoices = get_string("modulenameplural", "choice");
$strresponses = get_string("responses", "choice");
add_to_log($course->id, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id);
$eventdata = array();
$eventdata['objectid'] = $choice->id;
$eventdata['context'] = $context;
$eventdata['courseid'] = $course->id;
$eventdata['other']['content'] = 'choicereportcontentviewed';
$event = \mod_choice\event\report_viewed::create($eventdata);
$event->set_page_detail();
$event->trigger();
if (data_submitted() && $action == 'delete' && has_capability('mod/choice:deleteresponses',$context) && confirm_sesskey()) {
choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses.

View File

@ -0,0 +1,258 @@
<?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/>.
/**
* Events tests.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/choice/lib.php');
/**
* Events tests class.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_choice_events_testcase extends advanced_testcase {
/** @var choice_object */
protected $choice;
/** @var course_object */
protected $course;
/** @var cm_object Course module object. */
protected $cm;
/** @var context_object */
protected $context;
/**
* Setup
* often
* used
* objects
* for
* the
* following
* tests.
*/
protected function setup() {
global $DB;
$this->resetAfterTest();
$this->course = $this->getDataGenerator()->create_course();
$this->choice = $this->getDataGenerator()->create_module('choice', array('course' => $this->course->id));
$this->cm = $DB->get_record('course_modules', array('id' => $this->choice->cmid));
$this->context = context_module::instance($this->choice->id);
}
/**
* Test to ensure that event data is being stored correctly.
*/
public function test_answer_submitted() {
// Generate user data.
$user = $this->getDataGenerator()->create_user();
// Redirect event.
$sink = $this->redirectEvents();
choice_user_submit_response(3, $this->choice, $user->id, $this->course, $this->cm);
$events = $sink->get_events();
// Data checking.
$this->assertCount(1, $events);
$this->assertInstanceOf('\mod_choice\event\answer_submitted', $events[0]);
$this->assertEquals($user->id, $events[0]->userid);
$this->assertEquals(context_module::instance($this->choice->id), $events[0]->get_context());
$this->assertEquals(1, $events[0]->other['choiceid']);
$this->assertEquals(3, $events[0]->other['optionid']);
$expected = array($this->course->id, "choice", "choose", 'view.php?id=' . $this->cm->id, $this->choice->id, $this->cm->id);
$this->assertEventLegacyLogData($expected, $events[0]);
$sink->close();
}
/**
* Test custom validations.
*/
public function test_answer_submitted_other_exception() {
// Generate user data.
$user = $this->getDataGenerator()->create_user();
$eventdata = array();
$eventdata['context'] = $this->context;
$eventdata['objectid'] = 2;
$eventdata['userid'] = $user->id;
$eventdata['courseid'] = $this->course->id;
$eventdata['other'] = array();
// Make sure content identifier is always set.
$this->setExpectedException('coding_exception');
$event = \mod_choice\event\answer_submitted::create($eventdata);
$event->trigger();
}
/**
* Test to ensure that event data is being stored correctly.
*/
public function test_answer_updated() {
// Generate user data.
$user = $this->getDataGenerator()->create_user();
// Create the first answer.
choice_user_submit_response(2, $this->choice, $user->id, $this->course, $this->cm);
// Redirect event.
$sink = $this->redirectEvents();
// Now choose a different answer.
choice_user_submit_response(3, $this->choice, $user->id, $this->course, $this->cm);
$events = $sink->get_events();
// Data checking.
$this->assertCount(1, $events);
$this->assertInstanceOf('\mod_choice\event\answer_updated', $events[0]);
$this->assertEquals($user->id, $events[0]->userid);
$this->assertEquals(context_module::instance($this->choice->id), $events[0]->get_context());
$this->assertEquals(1, $events[0]->other['choiceid']);
$this->assertEquals(3, $events[0]->other['optionid']);
$expected = array($this->course->id, "choice", "choose again", 'view.php?id=' . $this->cm->id,
$this->choice->id, $this->cm->id);
$this->assertEventLegacyLogData($expected, $events[0]);
$sink->close();
}
/**
* Test custom validations
* for answer_updated event.
*/
public function test_answer_updated_other_exception() {
// Generate user data.
$user = $this->getDataGenerator()->create_user();
$eventdata = array();
$eventdata['context'] = $this->context;
$eventdata['objectid'] = 2;
$eventdata['userid'] = $user->id;
$eventdata['courseid'] = $this->course->id;
$eventdata['other'] = array();
// Make sure content identifier is always set.
$this->setExpectedException('coding_exception');
$event = \mod_choice\event\answer_updated::create($eventdata);
$event->trigger();
}
/**
* Test to ensure that event data is being stored correctly.
*/
public function test_report_viewed() {
global $USER;
$this->resetAfterTest();
// Generate user data.
$this->setAdminUser();
$eventdata = array();
$eventdata['objectid'] = $this->choice->id;
$eventdata['context'] = $this->context;
$eventdata['courseid'] = $this->course->id;
$eventdata['other']['content'] = 'choicereportcontentviewed';
// This is fired in a page view so we can't run this through a function.
$event = \mod_choice\event\report_viewed::create($eventdata);
// Redirect event.
$sink = $this->redirectEvents();
$event->trigger();
$event = $sink->get_events();
// Data checking.
$this->assertCount(1, $event);
$this->assertInstanceOf('\mod_choice\event\report_viewed', $event[0]);
$this->assertEquals($USER->id, $event[0]->userid);
$this->assertEquals(context_module::instance($this->choice->id), $event[0]->get_context());
$expected = array($this->course->id, "choice", "report", 'report.php?id=' . $this->context->instanceid,
$this->choice->id, $this->context->instanceid);
$this->assertEventLegacyLogData($expected, $event[0]);
$sink->close();
}
/**
* Test to ensure that event data is being stored correctly.
*/
public function test_course_module_viewed() {
global $USER;
// Generate user data.
$this->setAdminUser();
$eventdata = array();
$eventdata['objectid'] = $this->choice->id;
$eventdata['context'] = $this->context;
$eventdata['courseid'] = $this->course->id;
$eventdata['other']['content'] = 'pageresourceview';
// This is fired in a page view so we can't run this through a function.
$event = \mod_choice\event\course_module_viewed::create($eventdata);
// Redirect event.
$sink = $this->redirectEvents();
$event->trigger();
$event = $sink->get_events();
// Data checking.
$this->assertCount(1, $event);
$this->assertInstanceOf('\mod_choice\event\course_module_viewed', $event[0]);
$this->assertEquals($USER->id, $event[0]->userid);
$this->assertEquals(context_module::instance($this->choice->id), $event[0]->get_context());
$expected = array($this->course->id, "choice", "view", 'view.php?id=' . $this->context->instanceid,
$this->choice->id, $this->context->instanceid);
$this->assertEventLegacyLogData($expected, $event[0]);
$sink->close();
}
/**
* Test to ensure that event data is being stored correctly.
*/
public function test_instances_list_viewed() {
global $USER;
// Not much can be tested here as the event is only triggered on a page load,
// let's just check that the event contains the expected basic information.
$this->setAdminUser();
$params = array('context' => context_course::instance($this->course->id));
$event = \mod_choice\event\instances_list_viewed::create($params);
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\mod_choice\event\instances_list_viewed', $event);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals(context_course::instance($this->course->id), $event->get_context());
$expected = array($this->course->id, 'choice', 'view all', 'index.php?id=' . $this->course->id, '');
$this->assertEventLegacyLogData($expected, $event);
}
}

View File

@ -0,0 +1,81 @@
<?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_choice data generator.
*
* @package mod_choice
* @category test
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* mod_choice data generator class.
*
* @package mod_choice
* @category test
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_choice_generator extends testing_module_generator {
/**
* Create new choice module instance
*
* @param array|stdClass $record
* @param array $options
* @return stdClass activity record with extra cmid field
*/
public function create_instance($record = null, array $options = null) {
global $CFG;
require_once("$CFG->dirroot/mod/choice/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', 'choice') . ' ' . $i;
}
if (!isset($record->intro)) {
$record->intro = 'Test choice ' . $i;
}
if (!isset($record->introformat)) {
$record->introformat = FORMAT_MOODLE;
}
if (!isset($record->timemodified)) {
$record->timemodified = time();
}
if (!isset($record->option)) {
$record->option = array();
$record->option[] = 'Soft Drink';
$record->option[] = 'Beer';
$record->option[] = 'Wine';
$record->option[] = 'Spirits';
}
$record->coursemodule = $this->precreate_course_module($record->course, $options);
$id = choice_add_instance($record);
return $this->post_add_instance($id, $record->coursemodule);
}
}

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/>.
/**
* Generator tests.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Generator tests class.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_choice_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('choice', array('course' => $course->id)));
$choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
$this->assertEquals(1, $DB->count_records('choice', array('course' => $course->id)));
$this->assertTrue($DB->record_exists('choice', array('course' => $course->id)));
$this->assertTrue($DB->record_exists('choice', array('id' => $choice->id)));
$params = array('course' => $course->id, 'name' => 'One more choice');
$choice = $this->getDataGenerator()->create_module('choice', $params);
$this->assertEquals(2, $DB->count_records('choice', array('course' => $course->id)));
$this->assertEquals('One more choice', $DB->get_field_select('choice', 'name', 'id = :id', array('id' => $choice->id)));
$params = new stdClass();
$params->course = $course->id;
$params->option = array('fried rice', 'spring rolls', 'sweet and sour pork', 'satay beef', 'gyouza');
$choice = $this->getDataGenerator()->create_module('choice', $params);
$this->assertEquals(5, $DB->count_records('choice_options', array('choiceid' => $choice->id)));
}
}

View File

@ -78,7 +78,15 @@
/// Display the choice and possibly results
add_to_log($course->id, "choice", "view", "view.php?id=$cm->id", $choice->id, $cm->id);
$eventdata = array();
$eventdata['objectid'] = $choice->id;
$eventdata['context'] = $context;
$eventdata['courseid'] = $course->id;
$eventdata['other']['content'] = 'pageresourceview';
$event = \mod_choice\event\course_module_viewed::create($eventdata);
$event->set_page_detail();
$event->trigger();
/// Check to see if groups are being used in this choice
$groupmode = groups_get_activity_groupmode($cm);