コード例 #1
0
ファイル: lib.php プロジェクト: evltuma/moodle
 /**
  * Create a new framework.
  *
  * @param array|stdClass $record
  * @return competency_framework
  */
 public function create_framework($record = null)
 {
     $generator = phpunit_util::get_data_generator();
     $this->frameworkcount++;
     $i = $this->frameworkcount;
     $record = (object) $record;
     if (!isset($record->shortname)) {
         $record->shortname = "Framework shortname {$i}";
     }
     if (!isset($record->idnumber)) {
         $record->idnumber = "frm{$i}";
     }
     if (!isset($record->description)) {
         $record->description = "Framework {$i} description ";
     }
     if (!isset($record->descriptionformat)) {
         $record->descriptionformat = FORMAT_HTML;
     }
     if (!isset($record->visible)) {
         $record->visible = 1;
     }
     if (!isset($record->scaleid)) {
         if (isset($record->scaleconfiguration)) {
             throw new coding_exception('Scale configuration must be provided with a scale.');
         }
         if (!$this->scale) {
             $this->scale = $generator->create_scale(array('scale' => 'A,B,C,D'));
         }
         $record->scaleid = $this->scale->id;
     }
     if (!isset($record->scaleconfiguration)) {
         $scale = grade_scale::fetch(array('id' => $record->scaleid));
         $values = $scale->load_items();
         foreach ($values as $key => $value) {
             // Add a key (make the first value 1).
             $values[$key] = array('id' => $key + 1, 'name' => $value);
         }
         if (count($values) < 2) {
             throw new coding_exception('Please provide the scale configuration for one-item scales.');
         }
         $scaleconfig = array();
         // Last item is proficient.
         $item = array_pop($values);
         array_unshift($scaleconfig, array('id' => $item['id'], 'proficient' => 1));
         // Second-last item is default and proficient.
         $item = array_pop($values);
         array_unshift($scaleconfig, array('id' => $item['id'], 'scaledefault' => 1, 'proficient' => 1));
         array_unshift($scaleconfig, array('scaleid' => $record->scaleid));
         $record->scaleconfiguration = json_encode($scaleconfig);
     }
     if (is_array($record->scaleconfiguration) || is_object($record->scaleconfiguration)) {
         // Conveniently encode the config.
         $record->scaleconfiguration = json_encode($record->scaleconfiguration);
     }
     if (!isset($record->contextid)) {
         $record->contextid = context_system::instance()->id;
     }
     $framework = new competency_framework(0, $record);
     $framework->create();
     return $framework;
 }