Skip to content

Commit

Permalink
Merge branch 'MDL-52669-master' of git://github.com/jleyva/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
danpoltawski committed Mar 1, 2016
2 parents 2158e87 + 19429de commit b8b7f33
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,7 @@
'mod_imscp_view_imscp',
'mod_imscp_get_imscps_by_courses',
'mod_quiz_get_quizzes_by_courses',
'mod_quiz_view_quiz',
'mod_glossary_get_glossaries_by_courses',
'mod_wiki_get_wikis_by_courses',
'mod_wiki_view_wiki',
Expand Down
59 changes: 59 additions & 0 deletions mod/quiz/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,63 @@ function.', VALUE_OPTIONAL),
);
}

/**
* Describes the parameters for view_quiz.
*
* @return external_external_function_parameters
* @since Moodle 3.1
*/
public static function view_quiz_parameters() {
return new external_function_parameters (
array(
'quizid' => new external_value(PARAM_INT, 'quiz instance id'),
)
);
}

/**
* Trigger the course module viewed event and update the module completion status.
*
* @param int $quizid quiz instance id
* @return array of warnings and status result
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function view_quiz($quizid) {
global $DB;

$params = self::validate_parameters(self::view_quiz_parameters(), array('quizid' => $quizid));
$warnings = array();

// Request and permission validation.
$quiz = $DB->get_record('quiz', array('id' => $params['quizid']), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($quiz, 'quiz');

$context = context_module::instance($cm->id);
self::validate_context($context);

// Trigger course_module_viewed event and completion.
quiz_view($quiz, $course, $cm, $context);

$result = array();
$result['status'] = true;
$result['warnings'] = $warnings;
return $result;
}

/**
* Describes the view_quiz return value.
*
* @return external_single_structure
* @since Moodle 3.1
*/
public static function view_quiz_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
'warnings' => new external_warnings(),
)
);
}

}
8 changes: 8 additions & 0 deletions mod/quiz/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@
'type' => 'read',
'capabilities' => 'mod/quiz:view'
),

'mod_quiz_view_quiz' => array(
'classname' => 'mod_quiz_external',
'methodname' => 'view_quiz',
'description' => 'Trigger the course module viewed event and update the module completion status.',
'type' => 'write',
'capabilities' => 'mod/quiz:view'
),
);
26 changes: 26 additions & 0 deletions mod/quiz/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2122,3 +2122,29 @@ function quiz_add_random_questions($quiz, $addonpage, $categoryid, $number,
quiz_add_quiz_question($question->id, $quiz, $addonpage);
}
}

/**
* Mark the activity completed (if required) and trigger the course_module_viewed event.
*
* @param stdClass $quiz quiz object
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @since Moodle 3.1
*/
function quiz_view($quiz, $course, $cm, $context) {

$params = array(
'objectid' => $quiz->id,
'context' => $context
);

$event = \mod_quiz\event\course_module_viewed::create($params);
$event->add_record_snapshot('quiz', $quiz);
$event->trigger();

// Completion.
$completion = new completion_info($course);
$completion->set_module_viewed($cm);

}
62 changes: 62 additions & 0 deletions mod/quiz/tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,66 @@ public function test_mod_quiz_get_quizzes_by_courses() {

}

/**
* Test test_view_quiz
*/
public function test_view_quiz() {
global $DB;

// Test invalid instance id.
try {
mod_quiz_external::view_quiz(0);
$this->fail('Exception expected due to invalid mod_quiz instance id.');
} catch (moodle_exception $e) {
$this->assertEquals('invalidrecord', $e->errorcode);
}

// Test not-enrolled user.
$usernotenrolled = self::getDataGenerator()->create_user();
$this->setUser($usernotenrolled);
try {
mod_quiz_external::view_quiz($this->quiz->id);
$this->fail('Exception expected due to not enrolled user.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}

// Test user with full capabilities.
$this->setUser($this->student);

// Trigger and capture the event.
$sink = $this->redirectEvents();

$result = mod_quiz_external::view_quiz($this->quiz->id);
$result = external_api::clean_returnvalue(mod_quiz_external::view_quiz_returns(), $result);
$this->assertTrue($result['status']);

$events = $sink->get_events();
$this->assertCount(1, $events);
$event = array_shift($events);

// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_quiz\event\course_module_viewed', $event);
$this->assertEquals($this->context, $event->get_context());
$moodlequiz = new \moodle_url('/mod/quiz/view.php', array('id' => $this->cm->id));
$this->assertEquals($moodlequiz, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());

// Test user with no capabilities.
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
assign_capability('mod/quiz:view', CAP_PROHIBIT, $this->studentrole->id, $this->context->id);
// Empty all the caches that may be affected by this change.
accesslib_clear_all_caches_for_unit_testing();
course_modinfo::clear_instance_cache();

try {
mod_quiz_external::view_quiz($this->quiz->id);
$this->fail('Exception expected due to missing capability.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}

}

}
43 changes: 42 additions & 1 deletion mod/quiz/tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @copyright 2008 Tim Hunt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_quiz_locallib_testcase extends basic_testcase {
class mod_quiz_locallib_testcase extends advanced_testcase {

public function test_quiz_rescale_grade() {
$quiz = new stdClass();
Expand Down Expand Up @@ -154,4 +154,45 @@ public function test_quiz_question_tostring() {
$this->assertEquals('<span class="questionname">The question name</span> ' .
'<span class="questiontext">What sort of INEQUALITY is x &lt; y[?]</span>', $summary);
}

/**
* Test quiz_view
* @return void
*/
public function test_quiz_view() {
global $CFG;

$CFG->enablecompletion = 1;
$this->resetAfterTest();

$this->setAdminUser();
// Setup test data.
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
$quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id),
array('completion' => 2, 'completionview' => 1));
$context = context_module::instance($quiz->cmid);
$cm = get_coursemodule_from_instance('quiz', $quiz->id);

// Trigger and capture the event.
$sink = $this->redirectEvents();

quiz_view($quiz, $course, $cm, $context);

$events = $sink->get_events();
// 2 additional events thanks to completion.
$this->assertCount(3, $events);
$event = array_shift($events);

// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_quiz\event\course_module_viewed', $event);
$this->assertEquals($context, $event->get_context());
$moodleurl = new \moodle_url('/mod/quiz/view.php', array('id' => $cm->id));
$this->assertEquals($moodleurl, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
// Check completion status.
$completion = new completion_info($course);
$completiondata = $completion->get_data($cm);
$this->assertEquals(1, $completiondata->completionstate);
}
}
2 changes: 1 addition & 1 deletion mod/quiz/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2015111601;
$plugin->version = 2015111602;
$plugin->requires = 2015111000;
$plugin->component = 'mod_quiz';
$plugin->cron = 60;
13 changes: 2 additions & 11 deletions mod/quiz/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,8 @@
has_capability('mod/quiz:ignoretimelimits', $context, null, false));
$quiz = $quizobj->get_quiz();

// Log this request.
$params = array(
'objectid' => $quiz->id,
'context' => $context
);
$event = \mod_quiz\event\course_module_viewed::create($params);
$event->add_record_snapshot('quiz', $quiz);
$event->trigger();

$completion = new completion_info($course);
$completion->set_module_viewed($cm);
// Trigger course_module_viewed event and completion.
quiz_view($quiz, $course, $cm, $context);

// Initialize $PAGE, compute blocks.
$PAGE->set_url('/mod/quiz/view.php', array('id' => $cm->id));
Expand Down

0 comments on commit b8b7f33

Please sign in to comment.