/** * This function renders the form elements when adding a customcert element. * * @param mod_customcert_edit_element_form $mform the edit_form instance */ public function render_form_elements($mform) { // Get the user profile fields. $userfields = array('firstname' => get_user_field_name('firstname'), 'lastname' => get_user_field_name('lastname'), 'email' => get_user_field_name('email'), 'city' => get_user_field_name('city'), 'country' => get_user_field_name('country'), 'url' => get_user_field_name('url'), 'icq' => get_user_field_name('icq'), 'skype' => get_user_field_name('skype'), 'aim' => get_user_field_name('aim'), 'yahoo' => get_user_field_name('yahoo'), 'msn' => get_user_field_name('msn'), 'idnumber' => get_user_field_name('idnumber'), 'institution' => get_user_field_name('institution'), 'department' => get_user_field_name('department'), 'phone1' => get_user_field_name('phone1'), 'phone2' => get_user_field_name('phone2'), 'address' => get_user_field_name('address')); // Get the user custom fields. $arrcustomfields = \availability_profile\condition::get_custom_profile_fields(); $customfields = array(); foreach ($arrcustomfields as $key => $customfield) { $customfields[$customfield->id] = $key; } // Combine the two. $fields = $userfields + $customfields; core_collator::asort($fields); // Create the select box where the user field is selected. $mform->addElement('select', 'userfield', get_string('userfield', 'customcertelement_userfield'), $fields); $mform->setType('userfield', PARAM_ALPHANUM); $mform->addHelpButton('userfield', 'userfield', 'customcertelement_userfield'); parent::render_form_elements($mform); }
/** * Tests getting user list SQL. This is a different test from the above because * there is some additional code in this function so more variants need testing. */ public function test_get_user_list_sql() { global $DB, $CFG; $this->resetAfterTest(); $CFG->enableavailability = true; // Erase static cache before test. condition::wipe_static_cache(); // For testing, make another info field with default value. $DB->insert_record('user_info_field', array('shortname' => 'tonguestyle', 'name' => 'Tongue style', 'categoryid' => 1, 'datatype' => 'text', 'defaultdata' => 'Slimy')); $otherprofilefield = $DB->get_record('user_info_field', array('shortname' => 'tonguestyle')); // Make a test course and some users. $generator = $this->getDataGenerator(); $course = $generator->create_course(); $student1 = $generator->create_user(array('institution' => 'Unseen University')); $student2 = $generator->create_user(array('institution' => 'Hogwarts')); $student3 = $generator->create_user(array('institution' => 'Unseen University')); $student4 = $generator->create_user(array('institution' => '0')); $allusers = array(); foreach (array($student1, $student2, $student3, $student4) as $student) { $generator->enrol_user($student->id, $course->id); $allusers[$student->id] = $student; } $this->set_field($student1->id, 'poison dart'); $this->set_field($student2->id, 'poison dart'); $this->set_field($student3->id, 'Rough', $otherprofilefield->id); $this->info = new \core_availability\mock_info($course); // Test standard field condition (positive). $this->cond = new condition((object) array('sf' => 'institution', 'op' => condition::OP_CONTAINS, 'v' => 'Univ')); $this->assert_user_list_sql_results(array($student1->id, $student3->id)); // Now try it negative. $this->assert_user_list_sql_results(array($student2->id, $student4->id), true); // Try all the other condition types. $this->cond = new condition((object) array('sf' => 'institution', 'op' => condition::OP_DOES_NOT_CONTAIN, 'v' => 's')); $this->assert_user_list_sql_results(array($student4->id)); $this->cond = new condition((object) array('sf' => 'institution', 'op' => condition::OP_IS_EQUAL_TO, 'v' => 'Hogwarts')); $this->assert_user_list_sql_results(array($student2->id)); $this->cond = new condition((object) array('sf' => 'institution', 'op' => condition::OP_STARTS_WITH, 'v' => 'U')); $this->assert_user_list_sql_results(array($student1->id, $student3->id)); $this->cond = new condition((object) array('sf' => 'institution', 'op' => condition::OP_ENDS_WITH, 'v' => 'rts')); $this->assert_user_list_sql_results(array($student2->id)); $this->cond = new condition((object) array('sf' => 'institution', 'op' => condition::OP_IS_EMPTY)); $this->assert_user_list_sql_results(array($student4->id)); $this->cond = new condition((object) array('sf' => 'institution', 'op' => condition::OP_IS_NOT_EMPTY)); $this->assert_user_list_sql_results(array($student1->id, $student2->id, $student3->id)); // Try with a custom field condition that doesn't have a default. $this->cond = new condition((object) array('cf' => 'frogtype', 'op' => condition::OP_CONTAINS, 'v' => 'poison')); $this->assert_user_list_sql_results(array($student1->id, $student2->id)); $this->cond = new condition((object) array('cf' => 'frogtype', 'op' => condition::OP_IS_EMPTY)); $this->assert_user_list_sql_results(array($student3->id, $student4->id)); // Try with one that does have a default. $this->cond = new condition((object) array('cf' => 'tonguestyle', 'op' => condition::OP_STARTS_WITH, 'v' => 'Sli')); $this->assert_user_list_sql_results(array($student1->id, $student2->id, $student4->id)); $this->cond = new condition((object) array('cf' => 'tonguestyle', 'op' => condition::OP_IS_EMPTY)); $this->assert_user_list_sql_results(array()); }
/** * Test a specific type of module. * * @param string $modulename - the module name to test */ private function update_specific_module_test($modulename) { global $DB, $CFG; $this->resetAfterTest(true); $this->setAdminUser(); // Warnings: you'll need to change this line if ever you come to test a module not following Moodle standard. require_once $CFG->dirroot . '/mod/' . $modulename . '/lib.php'; // Enable avaibility. // If not enabled all conditional fields will be ignored. set_config('enableavailability', 1); // Enable course completion. // If not enabled all completion settings will be ignored. set_config('enablecompletion', COMPLETION_ENABLED); // Enable forum RSS feeds. set_config('enablerssfeeds', 1); set_config('forum_enablerssfeeds', 1); $course = $this->getDataGenerator()->create_course(array('numsections' => 1, 'enablecompletion' => COMPLETION_ENABLED), array('createsections' => true)); $grouping = $this->getDataGenerator()->create_grouping(array('courseid' => $course->id)); // Create assign module instance for testing gradeitem. $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign'); $params['course'] = $course->id; $instance = $generator->create_instance($params); $assigncm = get_coursemodule_from_instance('assign', $instance->id); // Create the test forum to update. $initvalues = new stdClass(); $initvalues->introformat = FORMAT_HTML; $initvalues->course = $course->id; $forum = self::getDataGenerator()->create_module('forum', $initvalues); // Retrieve course module. $cm = get_coursemodule_from_instance('forum', $forum->id); // Module test values. $moduleinfo = new stdClass(); // Always mandatory generic values to any module. $moduleinfo->coursemodule = $cm->id; $moduleinfo->modulename = $modulename; $moduleinfo->course = $course->id; $moduleinfo->groupingid = $grouping->id; $moduleinfo->visible = true; // Sometimes optional generic values for some modules. $moduleinfo->name = 'My test module'; $moduleinfo->showdescription = 1; // standard boolean require_once $CFG->libdir . '/gradelib.php'; $gradecats = grade_get_categories_menu($moduleinfo->course, false); $gradecatid = current(array_keys($gradecats)); // Retrieve the first key of $gradecats $moduleinfo->gradecat = $gradecatid; $moduleinfo->groupmode = VISIBLEGROUPS; $moduleinfo->cmidnumber = 'idnumber_XXX'; // Completion common to all module. $moduleinfo->completion = COMPLETION_TRACKING_AUTOMATIC; $moduleinfo->completionview = COMPLETION_VIEW_REQUIRED; $moduleinfo->completiongradeitemnumber = 1; $moduleinfo->completionexpected = time() + 7 * 24 * 3600; $moduleinfo->completionunlocked = 1; // Conditional activity. $coursegradeitem = grade_item::fetch_course_item($moduleinfo->course); //the activity will become available only when the user reach some grade into the course itself. $moduleinfo->availability = json_encode(\core_availability\tree::get_root_json(array(\availability_date\condition::get_json('>=', time()), \availability_date\condition::get_json('<', time() + 7 * 24 * 3600), \availability_grade\condition::get_json($coursegradeitem->id, 10, 80), \availability_profile\condition::get_json(false, 'email', 'contains', '@'), \availability_completion\condition::get_json($assigncm->id, COMPLETION_COMPLETE)), '&')); // Grading and Advanced grading. require_once $CFG->dirroot . '/rating/lib.php'; $moduleinfo->assessed = RATING_AGGREGATE_AVERAGE; $moduleinfo->scale = 10; // Note: it could be minus (for specific course scale). It is a signed number. $moduleinfo->assesstimestart = time(); $moduleinfo->assesstimefinish = time() + 7 * 24 * 3600; // RSS. $moduleinfo->rsstype = 2; $moduleinfo->rssarticles = 10; // Optional intro editor (depends of module). $draftid_editor = 0; file_prepare_draft_area($draftid_editor, null, null, null, null); $moduleinfo->introeditor = array('text' => 'This is a module', 'format' => FORMAT_HTML, 'itemid' => $draftid_editor); // Following is the advanced grading method area called 'submissions' for the 'assign' module. if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) { $moduleinfo->grade = 100; } // Plagiarism form values. // No plagiarism plugin installed by default. Use this space to make your own test. // Values specific to the module. $modulesetvalues = $modulename . '_update_set_values'; $this->{$modulesetvalues}($moduleinfo); // Create the module. $result = update_module($moduleinfo); // Retrieve the module info. $dbmodinstance = $DB->get_record($moduleinfo->modulename, array('id' => $result->instance)); $dbcm = get_coursemodule_from_instance($moduleinfo->modulename, $result->instance); // Retrieve the grade item. $gradeitem = $DB->get_record('grade_items', array('courseid' => $moduleinfo->course, 'iteminstance' => $dbmodinstance->id, 'itemmodule' => $moduleinfo->modulename)); // Compare the values common to all module instances. $this->assertEquals($moduleinfo->modulename, $dbcm->modname); $this->assertEquals($moduleinfo->course, $dbcm->course); $this->assertEquals($moduleinfo->groupingid, $dbcm->groupingid); $this->assertEquals($moduleinfo->visible, $dbcm->visible); $this->assertEquals($moduleinfo->completion, $dbcm->completion); $this->assertEquals($moduleinfo->completionview, $dbcm->completionview); $this->assertEquals($moduleinfo->completiongradeitemnumber, $dbcm->completiongradeitemnumber); $this->assertEquals($moduleinfo->completionexpected, $dbcm->completionexpected); $this->assertEquals($moduleinfo->availability, $dbcm->availability); $this->assertEquals($moduleinfo->showdescription, $dbcm->showdescription); $this->assertEquals($moduleinfo->groupmode, $dbcm->groupmode); $this->assertEquals($moduleinfo->cmidnumber, $dbcm->idnumber); $this->assertEquals($moduleinfo->gradecat, $gradeitem->categoryid); // Optional grade testing. if (plugin_supports('mod', $modulename, FEATURE_GRADE_HAS_GRADE, false) && !plugin_supports('mod', $modulename, FEATURE_RATE, false)) { $this->assertEquals($moduleinfo->grade, $dbmodinstance->grade); } // Some optional (but quite common) to some module. $this->assertEquals($moduleinfo->name, $dbmodinstance->name); $this->assertEquals($moduleinfo->intro, $dbmodinstance->intro); $this->assertEquals($moduleinfo->introformat, $dbmodinstance->introformat); // Test specific to the module. $modulerunasserts = $modulename . '_update_run_asserts'; $this->{$modulerunasserts}($moduleinfo, $dbmodinstance); return $moduleinfo; }
/** * Tests the filter_users (bulk checking) function. */ public function test_filter_users() { global $DB, $CFG; $this->resetAfterTest(); $CFG->enableavailability = true; // Erase static cache before test. condition::wipe_static_cache(); // Make a test course and some users. $generator = $this->getDataGenerator(); $course = $generator->create_course(); $student1 = $generator->create_user(array('institution' => 'Unseen University')); $student2 = $generator->create_user(array('institution' => 'Hogwarts')); $student3 = $generator->create_user(array('institution' => 'Unseen University')); $allusers = array(); foreach (array($student1, $student2, $student3) as $student) { $generator->enrol_user($student->id, $course->id); $allusers[$student->id] = $student; } $this->set_field($student1->id, 'poison dart'); $this->set_field($student2->id, 'poison dart'); $info = new \core_availability\mock_info($course); $checker = new \core_availability\capability_checker($info->get_context()); // Test standard field condition (positive and negative). $cond = new condition((object) array('sf' => 'institution', 'op' => 'contains', 'v' => 'Unseen')); $result = array_keys($cond->filter_user_list($allusers, false, $info, $checker)); ksort($result); $this->assertEquals(array($student1->id, $student3->id), $result); $result = array_keys($cond->filter_user_list($allusers, true, $info, $checker)); ksort($result); $this->assertEquals(array($student2->id), $result); // Test custom field condition. $cond = new condition((object) array('cf' => 'frogtype', 'op' => 'contains', 'v' => 'poison')); $result = array_keys($cond->filter_user_list($allusers, false, $info, $checker)); ksort($result); $this->assertEquals(array($student1->id, $student2->id), $result); $result = array_keys($cond->filter_user_list($allusers, true, $info, $checker)); ksort($result); $this->assertEquals(array($student3->id), $result); }