/**
  * Test that the pm_migrate_tags() function works correctly
  */
 public function test_pmmigratetags()
 {
     global $DB;
     $this->load_csv_data();
     // ELIS-7599: create bogus tag instance data (ala 1.9).
     $tag = (object) array('name' => 'bogus_tag', 'description' => 'Bogus Tag Description', 'timecreated' => 1327958800, 'timemodified' => 1327958800);
     $tagid = $DB->insert_record('local_elisprogram_tag', $tag);
     $taginstance = (object) array('instancetype' => 'cur', 'instanceid' => 999999, 'tagid' => $tagid, 'data' => '', 'timecreated' => 1327958800, 'timemodified' => 132795880);
     $DB->insert_record('local_elisprogram_tag_inst', $taginstance);
     // Migrate the legacy tag data to new ELIS fields.
     pm_migrate_tags();
     $this->assertTrue(!$DB->get_records('local_elisprogram_tag_inst', array('tagid' => $tagid)));
     // Initialize the program object.
     $program = new curriculum(1);
     $program->reset_custom_field_list();
     $program->load();
     $program = $program->to_object();
     // Get the field data from the object.
     $this->assertObjectHasAttribute('field__19upgrade_curriculum_tags', $program);
     $this->assertEquals(1, count($program->field__19upgrade_curriculum_tags));
     $this->assertEquals('Testing data', $program->field__19upgrade_curriculum_tag_data_Test_tag);
     // Let's do some extra DB-level validation (though it's probably not necessary).
     $field = $DB->get_record(field::TABLE, array('shortname' => '_19upgrade_curriculum_tags'));
     $this->assertGreaterThan(0, $field->id);
     $context = \local_elisprogram\context\program::instance($program->id);
     $this->assertTrue($DB->record_exists(field_data_char::TABLE, array('contextid' => $context->id, 'fieldid' => $field->id)));
 }
 /**
  * Test successful program update
  */
 public function test_success()
 {
     global $DB;
     // Create custom field.
     $fieldcat = new field_category();
     $fieldcat->name = 'Test';
     $fieldcat->save();
     $field = new field();
     $field->categoryid = $fieldcat->id;
     $field->shortname = 'testfield';
     $field->name = 'Test Field';
     $field->datatype = 'text';
     $field->save();
     $fieldctx = new field_contextlevel();
     $fieldctx->fieldid = $field->id;
     $fieldctx->contextlevel = CONTEXT_ELIS_PROGRAM;
     $fieldctx->save();
     // Create test program to update.
     $datagen = new elis_program_datagenerator($DB);
     $program = $datagen->create_program(array('idnumber' => 'testprogram', 'name' => 'testprogram'));
     $program = array('idnumber' => 'testprogram', 'name' => 'newtestprogramname', 'reqcredits' => 4.5, 'timetocomplete' => '6m', 'frequency' => '1y', 'field_testfield' => 'Test Field');
     $this->give_permissions(array('local/elisprogram:program_edit'));
     $response = local_datahub_elis_program_update::program_update($program);
     $this->assertNotEmpty($response);
     $this->assertInternalType('array', $response);
     $this->assertArrayHasKey('messagecode', $response);
     $this->assertArrayHasKey('message', $response);
     $this->assertArrayHasKey('record', $response);
     $this->assertEquals(get_string('ws_program_update_success_code', 'local_datahub'), $response['messagecode']);
     $this->assertEquals(get_string('ws_program_update_success_msg', 'local_datahub'), $response['message']);
     $this->assertInternalType('array', $response['record']);
     $this->assertArrayHasKey('id', $response['record']);
     // Get Program.
     $createdprg = new curriculum($response['record']['id']);
     $createdprg->load();
     $createdprg = $createdprg->to_array();
     foreach ($program as $param => $val) {
         $this->assertArrayHasKey($param, $createdprg);
         $this->assertEquals($val, $createdprg[$param]);
     }
 }
 /**
  * Performs program update
  * @throws moodle_exception If there was an error in passed parameters.
  * @throws data_object_exception If there was an error creating the entity.
  * @param array $data The incoming data parameter.
  * @return array An array of parameters, if successful.
  */
 public static function program_update(array $data)
 {
     global $USER, $DB;
     if (static::require_elis_dependencies() !== true) {
         throw new moodle_exception('ws_function_requires_elis', 'local_datahub');
     }
     // Parameter validation.
     $params = self::validate_parameters(self::program_update_parameters(), array('data' => $data));
     // Context validation.
     $context = context_user::instance($USER->id);
     self::validate_context($context);
     $data = (object) $data;
     // Validate program exists
     if (!($curid = $DB->get_field(curriculum::TABLE, 'id', array('idnumber' => $data->idnumber)))) {
         throw new data_object_exception('ws_program_update_fail_invalid_idnumber', 'local_datahub', '', $data);
     }
     // Capability checking.
     require_capability('local/elisprogram:program_edit', \local_elisprogram\context\program::instance($curid));
     // More validation
     if (isset($data->reqcredits)) {
         $reqcredits = (string) $data->reqcredits;
         $digits = strlen($reqcredits);
         $decies = 0;
         if (($decpos = strpos($reqcredits, '.')) !== false) {
             $decies = $digits - $decpos - 1;
             $digits = $decpos;
         }
         if (!is_numeric($reqcredits) || $digits > 8 || $decies > 2) {
             throw new data_object_exception('ws_program_update_fail_invalid_reqcredits', 'local_datahub', '', $data);
         }
     }
     if (isset($data->timetocomplete)) {
         $datedelta = new datedelta($data->timetocomplete);
         if (!$datedelta->getDateString()) {
             throw new data_object_exception('ws_program_update_fail_invalid_timetocomplete', 'local_datahub', '', $data);
         }
     }
     if (isset($data->frequency)) {
         $datedelta = new datedelta($data->frequency);
         if (!$datedelta->getDateString()) {
             throw new data_object_exception('ws_program_update_fail_invalid_frequency', 'local_datahub', '', $data);
         }
     }
     if (isset($data->priority)) {
         if ($data->priority < 0 || $data->priority > 10) {
             throw new data_object_exception('ws_program_update_fail_invalid_priority', 'local_datahub', '', $data);
         }
     }
     $prg = new curriculum($curid);
     $prg->load();
     $prg->set_from_data($data);
     $prg->save();
     // Respond.
     if (!empty($prg->id)) {
         $prgrec = (array) $DB->get_record(curriculum::TABLE, array('id' => $prg->id));
         $prgobj = $prg->to_array();
         // convert multi-valued custom field arrays to comma-separated listing
         $fields = self::get_program_custom_fields();
         foreach ($fields as $field) {
             // Generate name using custom field prefix.
             $fullfieldname = data_object_with_custom_fields::CUSTOM_FIELD_PREFIX . $field->shortname;
             if ($field->multivalued && isset($prgobj[$fullfieldname]) && is_array($prgobj[$fullfieldname])) {
                 $prgobj[$fullfieldname] = implode(',', $prgobj[$fullfieldname]);
             }
         }
         return array('messagecode' => get_string('ws_program_update_success_code', 'local_datahub'), 'message' => get_string('ws_program_update_success_msg', 'local_datahub'), 'record' => array_merge($prgrec, $prgobj));
     } else {
         throw new data_object_exception('ws_program_update_fail', 'local_datahub');
     }
 }
 /**
  * Test check_for_completed_nags function with completion time in the past.
  */
 public function test_checkforcompletednagsdate()
 {
     global $DB;
     $dataset = $this->createCsvDataSet(array(user::TABLE => elispm::file('tests/fixtures/pmuser.csv'), curriculum::TABLE => elispm::file('tests/fixtures/curriculum.csv'), curriculumstudent::TABLE => elispm::file('tests/fixtures/curriculum_student.csv'), course::TABLE => elispm::file('tests/fixtures/pmcourse.csv'), curriculumcourse::TABLE => elispm::file('tests/fixtures/curriculum_course.csv'), pmclass::TABLE => elispm::file('tests/fixtures/pmclass.csv'), student::TABLE => elispm::file('tests/fixtures/student.csv')));
     $this->loadDataSet($dataset);
     // Set the course to be required in the program.
     $sql = "UPDATE {" . curriculumcourse::TABLE . "} SET required = 1 WHERE curriculumid = 1 AND courseid = 100";
     $DB->execute($sql);
     // Set the completion time to a month ago and status to completed on the class enrolment.
     $completetime = time() - 2592000;
     $sql = 'UPDATE {' . student::TABLE . '} SET completetime = ' . $completetime . ', completestatusid = 2 WHERE userid = 103 AND classid = 100';
     $DB->execute($sql);
     // Execute check_for_completed_nags.
     $curriculum = new curriculum(1);
     $curriculum->load();
     $result = $curriculum->check_for_completed_nags();
     // Verify completion time in program assignment table.
     $recordset = curriculumstudent::get_curricula(103);
     foreach ($recordset as $record) {
         $this->assertEquals(1, $record->curid);
         $this->assertEquals($completetime, $record->timecompleted);
     }
 }