/** * Mainline for performing the back-end request denail/approval process * @uses $CFG * @uses $DB */ function display_approverequest() { // action_approverequest() global $CFG, $DB; // determine the action we are taking $approval_action = $this->required_param('approvalaction', PARAM_CLEAN); // make sure we have the necessary request $requestid = $this->required_param('request', PARAM_INT); if (!($request = $DB->get_record('block_courserequest', array('id' => $requestid)))) { $target = str_replace($CFG->wwwroot, '', $this->url); print_error('errorinvalidrequestid', 'block_courserequest', $target, $requestid); } // add additional information to the request that is needed by the form by default // and may not be part of the submitted info $this->add_approval_form_constants($request); // obtain the submitted form $target = $this->get_new_page(array('action' => 'approverequest'), true); $approveform = new pending_request_approve_form($target->url); $approveform->set_data($request); // cancel back to the base page if applicable if ($approveform->is_cancelled()) { redirect($this->url, '', 0); } // obtain the submitted data if ($formdata = $approveform->get_data(false)) { if ($approval_action == 'deny') { // deny the request $this->deny_request($request, $formdata); } else { // approve the request $this->approve_request($request, $formdata); } // redirect so reload won't resubmit form data $target = $this->get_new_page(array('action' => 'viewrequest', 'request' => $requestid), true); redirect($target->url); } $approveform->display(); }
function display_create() { // action_create() global $CFG, $DB; $target = $this->get_new_page(array('action' => 'create'), true); $form = new create_form($target->url); $data = $form->get_data(); if ($form->is_cancelled()) { redirect($this->url); // TBV return; } else { if ($data) { global $USER; require_once $CFG->dirroot . '/local/elisprogram/lib/data/course.class.php'; $request = new stdClass(); $request->userid = $USER->id; $request->firstname = $data->first; $request->lastname = $data->last; $request->email = $data->email; if (empty($data->title)) { $course_record = $DB->get_record(course::TABLE, array('id' => $data->courseid)); $data->title = $course_record->name; } $request->title = $data->title; $request->courseid = $data->courseid; $request->usecoursetemplate = empty($data->usecoursetemplate) ? 0 : 1; $request->requeststatus = 'pending'; $request->timemodified = $request->timecreated = time(); $id = $DB->insert_record('block_courserequest', $request); $fields = $DB->get_records('block_courserequest_fields'); $fields = $fields ? $fields : array(); foreach ($fields as $reqfield) { $field = new field($reqfield->fieldid); if (!$field->id || !isset($field->owners['manual'])) { // skip nonexistent fields, or fields without manual editing continue; } $fielddata = new stdClass(); $fielddata->requestid = $id; $fielddata->fieldid = $reqfield->fieldid; // key that represents the appropriate attribute on the object $field_key = "field_{$field->shortname}"; // make sure the field was enabled, especially for preventing the // storage of course fields when using an existing course if (isset($data->{$field_key})) { // check for multiple fields if (is_array($data->{$field_key})) { $fielddata->data = serialize($data->{$field_key}); $fielddata->multiple = '1'; } else { $fielddata->data = $data->{$field_key}; } // remember the context level that the field corresponds to $fielddata->contextlevel = $reqfield->contextlevel; $DB->insert_record('block_courserequest_data', $fielddata); } } require_once $CFG->dirroot . '/local/elisprogram/lib/notifications.php'; $syscontext = context_system::instance(); $config = get_config('block_courserequest'); if (has_capability('block/courserequest:approve', $syscontext)) { // Since we want to automatically approve requests for people approval permission, let's go ahead and create the course/class $requestid = $id; if (!($request = $DB->get_record('block_courserequest', array('id' => $requestid)))) { $target = str_replace($CFG->wwwroot, '', $this->url); print_error('errorinvalidrequestid', 'block_courserequest', $target, $requestid); } $target = $this->get_new_page(array('action' => 'approveconfirm')); $approveform = new pending_request_approve_form($target->url, $request); $request->request = $request->id; $approveform->set_data($request); // We're not actually approving the request, redirect back to the approval table. if ($approveform->is_cancelled()) { redirect($this->url, '', 0); } // Do we have to create a brand new course? if (empty($request->courseid)) { $crsdata = array('name' => $request->title, 'idnumber' => $data->crsidnumber, 'syllabus' => ''); $newcourse = new course($crsdata); if (!empty($config->use_course_fields)) { // course fields are enabled, so add the relevant data $this->add_custom_fields($request->id, 'course', $newcourse); } $newcourse->save(); // ->add() // do the course role assignment, if applicable if (!empty($config->course_role)) { if ($context = \local_elisprogram\context\course::instance($newcourse->id)) { // TBD: role_assign() now throws exceptions! $result = role_assign($config->course_role, $request->userid, $context->id, ECR_CD_ROLE_COMPONENT); } } $courseid = $newcourse->id; } else { $courseid = $request->courseid; // TBV: addslashes() } // Create the new class if we are using an existing course, or if // create_class_with_course is on. if (!empty($request->courseid) || !empty($config->create_class_with_course)) { require_once $CFG->dirroot . '/local/elisprogram/lib/data/pmclass.class.php'; $clsdata = array('name' => $request->title, 'courseid' => $courseid, 'idnumber' => $data->clsidnumber, 'starttimehour' => 25, 'starttimeminute' => 61, 'endtimehour' => 25, 'endtimeminute' => 61); $newclass = new pmclass($clsdata); $newclass->autocreate = false; if (!empty($config->use_class_fields)) { // class fields are enabled, so add the relevant data $this->add_custom_fields($request->id, 'class', $newclass); } $newclass->save(); // ->add() } // Update the request record to mark it as being approved. $request->requeststatus = 'approved'; $request->statusnote = ''; $DB->update_record('block_courserequest', $request); // TBV: addslashes_object() // assign role to requester in the newly created class if (!empty($newclass->id)) { if (!empty($config->class_role)) { $context = \local_elisprogram\context\pmclass::instance($newclass->id); // TBD: role_assign() now throws exceptions! role_assign($config->class_role, $request->userid, $context->id, ECR_CI_ROLE_COMPONENT); } } // create a new Moodle course from the course template if applicable if (!empty($data->usecoursetemplate) && !empty($newclass->id)) { moodle_attach_class($newclass->id, 0, '', true, true, true); // copy role over into Moodle course if (!empty($config->class_role)) { require_once $CFG->dirroot . '/local/elisprogram/lib/data/classmoodlecourse.class.php'; if ($class_moodle_record = $DB->get_record(classmoodlecourse::TABLE, array('classid' => $newclass->id))) { $context = context_course::instance($class_moodle_record->moodlecourseid); // TBD: role_assign() now throws exceptions! role_assign($config->class_role, $request->userid, $context->id, ECR_MC_ROLE_COMPONENT); } } } // Send a notification to the requesting user that their course / class is ready. // set additional course / class information for use in the message if (isset($newclass->idnumber) && isset($newclass->id)) { $request->classidnumber = $newclass->idnumber; $request->classid = $newclass->id; } $request->newcourseid = $courseid; // calculate the actual message $notice = block_courserequest_get_approval_message($request); // send it to the requester notification::notify($notice, $DB->get_record('user', array('id' => $request->userid))); print_string('request_submitted_and_auto_approved', 'block_courserequest'); } else { // find users with approve capabilities in the system context $admin = get_users_by_capability($syscontext, 'block/courserequest:approve'); foreach ($admin as $userto) { notification::notify(get_string('new_request_notification', 'block_courserequest', $data), $userto); } print_string('request_submitted', 'block_courserequest'); } redirect($this->url); return; } } $form->display(); }