Skip to content

Commit

Permalink
MDL-49231 mod_glossary: External function view_glossary
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Massart authored and jleyva committed Dec 31, 2015
1 parent ef343c3 commit d0d4372
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,7 @@
'mod_wiki_get_wikis_by_courses',
'mod_wiki_view_wiki',
'mod_wiki_view_page',
'mod_glossary_view_glossary',
),
'enabled' => 0,
'restrictedusers' => 0,
Expand Down
66 changes: 65 additions & 1 deletion mod/glossary/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.1
*/
defined('MOODLE_INTERNAL') || die;

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

require_once($CFG->libdir . '/externallib.php');
require_once($CFG->dirroot . '/mod/glossary/lib.php');

/**
* Glossary module external functions.
Expand Down Expand Up @@ -157,4 +160,65 @@ public static function get_glossaries_by_courses_returns() {
'warnings' => new external_warnings())
);
}

/**
* Returns the description of the external function parameters.
*
* @return external_function_parameters
* @since Moodle 3.1
*/
public static function view_glossary_parameters() {
return new external_function_parameters(array(
'id' => new external_value(PARAM_INT, 'Glossary instance ID'),
'mode' => new external_value(PARAM_ALPHA, 'The mode in which the glossary is viewed'),
));
}

/**
* Notify that the course module was viewed.
*
* @param int $id The glossary instance ID.
* @return array of warnings and status result
* @since Moodle 3.1
* @throws moodle_exception
*/
public static function view_glossary($id, $mode) {
global $DB;

$params = self::validate_parameters(self::view_glossary_parameters(), array(
'id' => $id,
'mode' => $mode
));
$id = $params['id'];
$mode = $params['mode'];
$warnings = array();

// Fetch and confirm.
$glossary = $DB->get_record('glossary', array('id' => $id), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($glossary, 'glossary');
$context = context_module::instance($cm->id);
self::validate_context($context);

// Trigger module viewed event.
glossary_view($glossary, $course, $cm, $context, $mode);

return array(
'status' => true,
'warnings' => $warnings
);
}

/**
* Returns the description of the external function return value.
*
* @return external_description
* @since Moodle 3.1
*/
public static function view_glossary_returns() {
return new external_single_structure(array(
'status' => new external_value(PARAM_BOOL, 'True on success'),
'warnings' => new external_warnings()
));
}

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

'mod_glossary_view_glossary' => array(
'classname' => 'mod_glossary_external',
'methodname' => 'view_glossary',
'description' => 'Notify the glossary as being viewed.',
'type' => 'write',
'capabilities' => 'mod/glossary:view'
),

);
31 changes: 31 additions & 0 deletions mod/glossary/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3295,3 +3295,34 @@ function glossary_get_visible_tabs($displayformat) {

return $showtabs;
}

/**
* Notify that the glossary was viewed.
*
* This will trigger relevant events and activity completion.
*
* @param stdClass $glossary The glossary object.
* @param stdClass $course The course object.
* @param stdClass $cm The course module object.
* @param stdClass $context The context object.
* @param string $mode The mode in which the glossary was viewed.
* @since Moodle 3.1
*/
function glossary_view($glossary, $course, $cm, $context, $mode) {

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

// Trigger the course module viewed event.
$event = \mod_glossary\event\course_module_viewed::create(array(
'objectid' => $glossary->id,
'context' => $context,
'other' => array('mode' => $mode)
));
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('glossary', $glossary);
$event->trigger();
}

45 changes: 45 additions & 0 deletions mod/glossary/tests/external_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,49 @@ public function test_get_glossaries_by_courses() {
$this->assertEquals('Third Glossary', $glossaries['glossaries'][0]['name']);
}

public function test_view_glossary() {
$this->resetAfterTest(true);

// Generate all the things.
$c1 = $this->getDataGenerator()->create_course();
$g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
$u1 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($u1->id, $c1->id);

$sink = $this->redirectEvents();
$this->setUser($u1);
$return = mod_glossary_external::view_glossary($g1->id, 'letter');
$return = external_api::clean_returnvalue(mod_glossary_external::view_glossary_returns(), $return);
$events = $sink->get_events();

// Assertion.
$this->assertTrue($return['status']);
$this->assertEmpty($return['warnings']);
$this->assertCount(1, $events);
$this->assertEquals('\mod_glossary\event\course_module_viewed', $events[0]->eventname);
$sink->close();
}

public function test_view_glossary_without_permission() {
$this->resetAfterTest(true);

// Generate all the things.
$c1 = $this->getDataGenerator()->create_course();
$g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
$u1 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($u1->id, $c1->id);
$ctx = context_module::instance($g1->cmid);

// Revoke permission.
$roles = get_archetype_roles('user');
$role = array_shift($roles);
assign_capability('mod/glossary:view', CAP_PROHIBIT, $role->id, $ctx, true);
accesslib_clear_all_caches_for_unit_testing();

// Assertion.
$this->setUser($u1);
$this->setExpectedException('require_login_exception', 'Activity is hidden');
mod_glossary_external::view_glossary($g1->id, 'letter');
}

}
95 changes: 95 additions & 0 deletions mod/glossary/tests/lib_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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/>.

/**
* Glossary lib tests.
*
* @package mod_glossary
* @copyright 2015 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

global $CFG;
require_once($CFG->dirroot . '/mod/glossary/lib.php');

/**
* Glossary lib testcase.
*
* @package mod_glossary
* @copyright 2015 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_glossary_lib_testcase extends advanced_testcase {

public function test_glossary_view() {
global $CFG;
$origcompletion = $CFG->enablecompletion;
$CFG->enablecompletion = true;
$this->resetAfterTest(true);

// Generate all the things.
$c1 = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
$g1 = $this->getDataGenerator()->create_module('glossary', array(
'course' => $c1->id,
'completion' => COMPLETION_TRACKING_AUTOMATIC,
'completionview' => 1
));
$g2 = $this->getDataGenerator()->create_module('glossary', array(
'course' => $c1->id,
'completion' => COMPLETION_TRACKING_AUTOMATIC,
'completionview' => 1
));
$u1 = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($u1->id, $c1->id);
$modinfo = course_modinfo::instance($c1->id);
$cm1 = $modinfo->get_cm($g1->cmid);
$cm2 = $modinfo->get_cm($g2->cmid);
$ctx1 = $cm1->context;
$completion = new completion_info($c1);

$this->setUser($u1);

// Confirm what we've set up.
$this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm1, false, $u1->id)->viewed);
$this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm1, false, $u1->id)->completionstate);
$this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm2, false, $u1->id)->viewed);
$this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm2, false, $u1->id)->completionstate);

// Simulate the view call.
$sink = $this->redirectEvents();
glossary_view($g1, $c1, $cm1, $ctx1, 'letter');
$events = $sink->get_events();

// Assertions.
$this->assertCount(3, $events);
$this->assertEquals('\core\event\course_module_completion_updated', $events[0]->eventname);
$this->assertEquals('\core\event\course_module_completion_updated', $events[1]->eventname);
$this->assertEquals('\mod_glossary\event\course_module_viewed', $events[2]->eventname);
$this->assertEquals($g1->id, $events[2]->objectid);
$this->assertEquals('letter', $events[2]->other['mode']);
$this->assertEquals(COMPLETION_VIEWED, $completion->get_data($cm1, false, $u1->id)->viewed);
$this->assertEquals(COMPLETION_COMPLETE, $completion->get_data($cm1, false, $u1->id)->completionstate);
$this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm2, false, $u1->id)->viewed);
$this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm2, false, $u1->id)->completionstate);

// Tear down.
$sink->close();
$CFG->enablecompletion = $origcompletion;
}

}
2 changes: 1 addition & 1 deletion mod/glossary/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; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2015111602; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2015111000; // Requires this Moodle version
$plugin->component = 'mod_glossary'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;
14 changes: 1 addition & 13 deletions mod/glossary/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,7 @@
}

// Trigger module viewed event.
$event = \mod_glossary\event\course_module_viewed::create(array(
'objectid' => $glossary->id,
'context' => $context,
'other' => array('mode' => $mode)
));
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('glossary', $glossary);
$event->trigger();

// Mark as viewed
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
glossary_view($glossary, $course, $cm, $context, $mode);

/// Printing the heading
$strglossaries = get_string("modulenameplural", "glossary");
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

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

$version = 2015123100.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2015123100.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down

0 comments on commit d0d4372

Please sign in to comment.