Skip to content

Commit

Permalink
MDL-59365 enrol_manual: Rewrite the yui enrolment popup in amd
Browse files Browse the repository at this point in the history
The add/roles thing was only converted to bootstrap 2/4 markup and the yui left in place.

The modal for adding users to a course was unsavable so I rewrote it with an mform in a popup, still calling
the same (barely) modified ajax script.

The webservice for searching cohorts was taken from admin/tool/lp and moved into /cohort. I added a generic "cohort"
mform element at the same time.

The webservice for searching for users was taken from the original ajax script.
  • Loading branch information
Damyon Wiese authored and danpoltawski committed Jul 11, 2017
1 parent bdf31b0 commit a60e8ba
Show file tree
Hide file tree
Showing 33 changed files with 1,134 additions and 1,224 deletions.
113 changes: 0 additions & 113 deletions admin/tool/lp/classes/external.php
Expand Up @@ -920,119 +920,6 @@ public static function search_users_returns() {
));
}

/**
* Returns the description of external function parameters.
*
* @return external_function_parameters
*/
public static function search_cohorts_parameters() {
$query = new external_value(
PARAM_RAW,
'Query string'
);
$includes = new external_value(
PARAM_ALPHA,
'What other contexts to fetch the frameworks from. (all, parents, self)',
VALUE_DEFAULT,
'parents'
);
$limitfrom = new external_value(
PARAM_INT,
'limitfrom we are fetching the records from',
VALUE_DEFAULT,
0
);
$limitnum = new external_value(
PARAM_INT,
'Number of records to fetch',
VALUE_DEFAULT,
25
);
return new external_function_parameters(array(
'query' => $query,
'context' => self::get_context_parameters(),
'includes' => $includes,
'limitfrom' => $limitfrom,
'limitnum' => $limitnum
));
}

/**
* Search cohorts.
* TODO: MDL-52243 Move this function to cohorts/externallib.php
*
* @param string $query
* @param array $context
* @param string $includes
* @param int $limitfrom
* @param int $limitnum
* @return array
*/
public static function search_cohorts($query, $context, $includes = 'parents', $limitfrom = 0, $limitnum = 25) {
global $DB, $CFG, $PAGE;
require_once($CFG->dirroot . '/cohort/lib.php');

$params = self::validate_parameters(self::search_cohorts_parameters(), array(
'query' => $query,
'context' => $context,
'includes' => $includes,
'limitfrom' => $limitfrom,
'limitnum' => $limitnum,
));
$query = $params['query'];
$includes = $params['includes'];
$context = self::get_context_from_params($params['context']);
$limitfrom = $params['limitfrom'];
$limitnum = $params['limitnum'];

self::validate_context($context);
$output = $PAGE->get_renderer('tool_lp');

$manager = has_capability('moodle/cohort:manage', $context);
if (!$manager) {
require_capability('moodle/cohort:view', $context);
}

// TODO Make this more efficient.
if ($includes == 'self') {
$results = cohort_get_cohorts($context->id, $limitfrom, $limitnum, $query);
$results = $results['cohorts'];
} else if ($includes == 'parents') {
$results = cohort_get_cohorts($context->id, $limitfrom, $limitnum, $query);
$results = $results['cohorts'];
if (!$context instanceof context_system) {
$results = array_merge($results, cohort_get_available_cohorts($context, COHORT_ALL, $limitfrom, $limitnum, $query));
}
} else if ($includes == 'all') {
$results = cohort_get_all_cohorts($limitfrom, $limitnum, $query);
$results = $results['cohorts'];
} else {
throw new coding_exception('Invalid parameter value for \'includes\'.');
}

$cohorts = array();
foreach ($results as $key => $cohort) {
$cohortcontext = context::instance_by_id($cohort->contextid);
$exporter = new cohort_summary_exporter($cohort, array('context' => $cohortcontext));
$newcohort = $exporter->export($output);

$cohorts[$key] = $newcohort;
}

return array('cohorts' => $cohorts);
}

/**
* Returns description of external function result value.
*
* @return external_description
*/
public static function search_cohorts_returns() {
return new external_single_structure(array(
'cohorts' => new external_multiple_structure(cohort_summary_exporter::get_read_structure())
));
}

/**
* Returns description of external function.
*
Expand Down
5 changes: 3 additions & 2 deletions admin/tool/lp/db/services.php
Expand Up @@ -131,10 +131,11 @@
'capabilities' => '',
'ajax' => true,
),
// This function was originally in this plugin but has been moved to core.
'tool_lp_search_cohorts' => array(
'classname' => 'tool_lp\external',
'classname' => 'core_cohort_external',
'methodname' => 'search_cohorts',
'classpath' => '',
'classpath' => 'cohort/externallib.php',
'description' => 'Search for cohorts.',
'type' => 'read',
'capabilities' => 'moodle/cohort:view',
Expand Down
72 changes: 0 additions & 72 deletions admin/tool/lp/tests/externallib_test.php
Expand Up @@ -461,76 +461,4 @@ public function test_data_for_user_competency_summary() {
$this->assertEquals('A', $summary->evidence[1]->gradename);
}

/**
* Search cohorts.
*/
public function test_search_cohorts() {
$this->resetAfterTest(true);

$syscontext = array('contextid' => context_system::instance()->id);
$catcontext = array('contextid' => context_coursecat::instance($this->category->id)->id);
$othercatcontext = array('contextid' => context_coursecat::instance($this->othercategory->id)->id);

$cohort1 = $this->getDataGenerator()->create_cohort(array_merge($syscontext, array('name' => 'Cohortsearch 1')));
$cohort2 = $this->getDataGenerator()->create_cohort(array_merge($catcontext, array('name' => 'Cohortsearch 2')));
$cohort3 = $this->getDataGenerator()->create_cohort(array_merge($othercatcontext, array('name' => 'Cohortsearch 3')));

// Check for parameter $includes = 'parents'.

// A user without permission in the system.
$this->setUser($this->user);
try {
$result = external::search_cohorts("Cohortsearch", $syscontext, 'parents');
$this->fail('Invalid permissions in system');
} catch (required_capability_exception $e) {
// All good.
}

// A user without permission in a category.
$this->setUser($this->catuser);
try {
$result = external::search_cohorts("Cohortsearch", $catcontext, 'parents');
$this->fail('Invalid permissions in category');
} catch (required_capability_exception $e) {
// All good.
}

// A user with permissions in the system.
$this->setUser($this->creator);
$result = external::search_cohorts("Cohortsearch", $syscontext, 'parents');
$this->assertEquals(1, count($result['cohorts']));
$this->assertEquals('Cohortsearch 1', $result['cohorts'][$cohort1->id]->name);

// A user with permissions in the category.
$this->setUser($this->catcreator);
$result = external::search_cohorts("Cohortsearch", $catcontext, 'parents');
$this->assertEquals(2, count($result['cohorts']));
$cohorts = array();
foreach ($result['cohorts'] as $cohort) {
$cohorts[] = $cohort->name;
}
$this->assertTrue(in_array('Cohortsearch 1', $cohorts));
$this->assertTrue(in_array('Cohortsearch 2', $cohorts));

// Check for parameter $includes = 'self'.
$this->setUser($this->creator);
$result = external::search_cohorts("Cohortsearch", $othercatcontext, 'self');
$this->assertEquals(1, count($result['cohorts']));
$this->assertEquals('Cohortsearch 3', $result['cohorts'][$cohort3->id]->name);

// Check for parameter $includes = 'all'.
$this->setUser($this->creator);
$result = external::search_cohorts("Cohortsearch", $syscontext, 'all');
$this->assertEquals(3, count($result['cohorts']));

// Detect invalid parameter $includes.
$this->setUser($this->creator);
try {
$result = external::search_cohorts("Cohortsearch", $syscontext, 'invalid');
$this->fail('Invalid parameter includes');
} catch (coding_exception $e) {
// All good.
}
}

}
2 changes: 1 addition & 1 deletion admin/tool/lp/version.php
Expand Up @@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die();


$plugin->version = 2017051500; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2017062700; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2017050500; // Requires this Moodle version.
$plugin->component = 'tool_lp'; // Full name of the plugin (used for diagnostics).
131 changes: 131 additions & 0 deletions cohort/externallib.php
Expand Up @@ -274,6 +274,137 @@ public static function get_cohorts_returns() {
);
}

/**
* Returns the description of external function parameters.
*
* @return external_function_parameters
*/
public static function search_cohorts_parameters() {
$query = new external_value(
PARAM_RAW,
'Query string'
);
$includes = new external_value(
PARAM_ALPHA,
'What other contexts to fetch the frameworks from. (all, parents, self)',
VALUE_DEFAULT,
'parents'
);
$limitfrom = new external_value(
PARAM_INT,
'limitfrom we are fetching the records from',
VALUE_DEFAULT,
0
);
$limitnum = new external_value(
PARAM_INT,
'Number of records to fetch',
VALUE_DEFAULT,
25
);
return new external_function_parameters(array(
'query' => $query,
'context' => self::get_context_parameters(),
'includes' => $includes,
'limitfrom' => $limitfrom,
'limitnum' => $limitnum
));
}

/**
* Search cohorts.
*
* @param string $query
* @param array $context
* @param string $includes
* @param int $limitfrom
* @param int $limitnum
* @return array
*/
public static function search_cohorts($query, $context, $includes = 'parents', $limitfrom = 0, $limitnum = 25) {
global $DB, $CFG, $PAGE;
require_once($CFG->dirroot . '/cohort/lib.php');

$params = self::validate_parameters(self::search_cohorts_parameters(), array(
'query' => $query,
'context' => $context,
'includes' => $includes,
'limitfrom' => $limitfrom,
'limitnum' => $limitnum,
));
$query = $params['query'];
$includes = $params['includes'];
$context = self::get_context_from_params($params['context']);
$limitfrom = $params['limitfrom'];
$limitnum = $params['limitnum'];

self::validate_context($context);
$output = $PAGE->get_renderer('tool_lp');

$manager = has_capability('moodle/cohort:manage', $context);
if (!$manager) {
require_capability('moodle/cohort:view', $context);
}

// TODO Make this more efficient.
if ($includes == 'self') {
$results = cohort_get_cohorts($context->id, $limitfrom, $limitnum, $query);
$results = $results['cohorts'];
} else if ($includes == 'parents') {
$results = cohort_get_cohorts($context->id, $limitfrom, $limitnum, $query);
$results = $results['cohorts'];
if (!$context instanceof context_system) {
$results = array_merge($results, cohort_get_available_cohorts($context, COHORT_ALL, $limitfrom, $limitnum, $query));
}
} else if ($includes == 'all') {
$results = cohort_get_all_cohorts($limitfrom, $limitnum, $query);
$results = $results['cohorts'];
} else {
throw new coding_exception('Invalid parameter value for \'includes\'.');
}

$cohorts = array();
foreach ($results as $key => $cohort) {
$cohortcontext = context::instance_by_id($cohort->contextid);
if (!isset($cohort->description)) {
$cohort->description = '';
}
if (!isset($cohort->descriptionformat)) {
$cohort->descriptionformat = FORMAT_PLAIN;
}

list($cohort->description, $cohort->descriptionformat) =
external_format_text($cohort->description, $cohort->descriptionformat,
$cohortcontext->id, 'cohort', 'description', $cohort->id);

$cohorts[$key] = $cohort;
}

return array('cohorts' => $cohorts);
}

/**
* Returns description of external function result value.
*
* @return external_description
*/
public static function search_cohorts_returns() {
return new external_single_structure(array(
'cohorts' => new external_multiple_structure(
new external_single_structure(array(
'id' => new external_value(PARAM_INT, 'ID of the cohort'),
'name' => new external_value(PARAM_RAW, 'cohort name'),
'idnumber' => new external_value(PARAM_RAW, 'cohort idnumber'),
'description' => new external_value(PARAM_RAW, 'cohort description'),
'descriptionformat' => new external_format_value('description'),
'visible' => new external_value(PARAM_BOOL, 'cohort visible'),
))
)
));
}



/**
* Returns description of method parameters
*
Expand Down
27 changes: 27 additions & 0 deletions cohort/lib.php
Expand Up @@ -330,6 +330,33 @@ function cohort_can_view_cohort($cohortorid, $currentcontext) {
return false;
}

/**
* Get a cohort by id. Also does a visibility check and returns false if the user cannot see this cohort.
*
* @param stdClass|int $cohortorid cohort object or id
* @param context $currentcontext current context (course) where visibility is checked
* @return stdClass|boolean
*/
function cohort_get_cohort($cohortorid, $currentcontext) {
global $DB;
if (is_numeric($cohortorid)) {
$cohort = $DB->get_record('cohort', array('id' => $cohortorid), 'id, contextid, visible');
} else {
$cohort = $cohortorid;
}

if ($cohort && in_array($cohort->contextid, $currentcontext->get_parent_context_ids())) {
if ($cohort->visible) {
return $cohort;
}
$cohortcontext = context::instance_by_id($cohort->contextid);
if (has_capability('moodle/cohort:view', $cohortcontext)) {
return $cohort;
}
}
return false;
}

/**
* Produces a part of SQL query to filter cohorts by the search string
*
Expand Down

0 comments on commit a60e8ba

Please sign in to comment.