Esempio n. 1
0
 /**
  * Performs track 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 track_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::track_update_parameters(), array('data' => $data));
     // Context validation.
     $context = context_user::instance($USER->id);
     self::validate_context($context);
     $data = (object) $data;
     $record = new stdClass();
     $record = $data;
     // need all custom fields, etc.
     // Initialize version1elis importplugin for utility functions.
     $importplugin = rlip_dataplugin_factory::factory('dhimport_version1elis');
     // Validate
     if (empty($data->idnumber) || !($trkid = $DB->get_field(track::TABLE, 'id', array('idnumber' => $data->idnumber)))) {
         throw new data_object_exception('ws_track_update_fail_invalid_idnumber', 'local_datahub', '', $data);
     }
     unset($record->idnumber);
     // Capability checking.
     require_capability('local/elisprogram:track_edit', \local_elisprogram\context\track::instance($trkid));
     if (isset($data->startdate)) {
         $startdate = $importplugin->parse_date($data->startdate);
         if (empty($startdate)) {
             throw new data_object_exception('ws_track_update_fail_invalid_startdate', 'local_datahub', '', $data);
         } else {
             $record->startdate = $startdate;
         }
     }
     if (isset($data->enddate)) {
         $enddate = $importplugin->parse_date($data->enddate);
         if (empty($enddate)) {
             throw new data_object_exception('ws_track_update_fail_invalid_enddate', 'local_datahub', '', $data);
         } else {
             $record->enddate = $enddate;
         }
     }
     $track = new track($trkid);
     $track->load();
     $track->set_from_data($record);
     $track->save();
     // Respond.
     if (!empty($track->id)) {
         $trackrec = (array) $DB->get_record(track::TABLE, array('id' => $track->id));
         $trackobj = $track->to_array();
         // convert multi-valued custom field arrays to comma-separated listing
         $fields = self::get_track_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($trackobj[$fullfieldname]) && is_array($trackobj[$fullfieldname])) {
                 $trackobj[$fullfieldname] = implode(',', $trackobj[$fullfieldname]);
             }
         }
         return array('messagecode' => get_string('ws_track_update_success_code', 'local_datahub'), 'message' => get_string('ws_track_update_success_msg', 'local_datahub'), 'record' => array_merge($trackrec, $trackobj));
     } else {
         throw new data_object_exception('ws_track_update_fail', 'local_datahub');
     }
 }
Esempio n. 2
0
 /**
  * Create an ELIS track
  * @param curriculum &$cur An ELIS program to assign the track to.
  * @param field &$field A custom field to set when creating program.
  * @return track The created track.
  */
 public function create_track(curriculum &$cur, field &$field)
 {
     $data = new stdClass();
     $data->curid = $cur->id;
     $data->idnumber = 'TRK1';
     $data->name = 'Track 1';
     $data->description = 'Track Description';
     $data->startdate = 0;
     $data->enddate = '0';
     $fieldvar = 'field_' . $field->shortname;
     $data->{$fieldvar} = 'test field data';
     $trk = new track();
     $trk->set_from_data($data);
     $trk->save();
     return $trk;
 }
 /**
  * Tests contexts in track data object.
  *
  * Covers:
  * local/elisprogram/lib/data/track.class.php:291
  */
 public function test_deletetrack()
 {
     $this->setup_curriculum();
     $data = new stdClass();
     $data->curid = 1;
     $data->idnumber = 'TRK1';
     $data->name = 'Track 1';
     $data->description = 'Track Description';
     $data->startdate = 0;
     $data->enddate = '0';
     $trk = new track();
     $trk->set_from_data($data);
     $trk->save();
     $trk = new track($trk->id);
     $trk->delete();
 }
Esempio n. 4
0
 function track_update($record, $filename)
 {
     global $DB, $CFG;
     $message = "";
     //field length checking
     $lengthcheck = $this->check_track_field_lengths($record, $filename);
     if (!$lengthcheck) {
         return false;
     }
     if (isset($record->idnumber)) {
         if (isset($record->assignment)) {
             $message = "track with idnumber \"{$record->idnumber}\" was not re-assigned to program with idnumber \"{$record->assignment}\" because moving tracks between programs is not supported.";
         }
         if (!($id = $DB->get_field(track::TABLE, 'id', array('idnumber' => $record->idnumber)))) {
             $this->fslogger->log_failure("idnumber value of \"{$record->idnumber}\" does not refer to a valid track.", 0, $filename, $this->linenumber, $record, "track");
             return false;
         }
     }
     if (!$this->validate_track_data('update', $record, $filename)) {
         return false;
     }
     $record = $this->add_custom_field_prefixes($record);
     //custom field validation
     if (!$this->validate_custom_field_data('update', $record, $filename, 'track')) {
         return false;
     }
     $record->id = $id;
     $record->timemodified = time();
     $track = new track();
     $track->set_from_data($record);
     $track->save();
     //log success
     $success_message = "Track with idnumber \"{$record->idnumber}\" successfully updated.";
     $this->fslogger->log_success($success_message, 0, $filename, $this->linenumber);
     return true;
 }